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.
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "wpa_supplicant_i.h"
25 #include "ctrl_iface.h"
27 /* Per-interface ctrl_iface */
30 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
32 * This structure is used to store information about registered control
33 * interface monitors into struct wpa_supplicant. This data is private to
34 * ctrl_iface_unix.c and should not be touched directly from other files.
37 struct wpa_ctrl_dst
*next
;
38 struct sockaddr_un addr
;
45 struct ctrl_iface_priv
{
46 struct wpa_supplicant
*wpa_s
;
48 struct wpa_ctrl_dst
*ctrl_dst
;
52 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
53 int level
, const char *buf
,
57 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv
*priv
,
58 struct sockaddr_un
*from
,
61 struct wpa_ctrl_dst
*dst
;
63 dst
= os_zalloc(sizeof(*dst
));
66 os_memcpy(&dst
->addr
, from
, sizeof(struct sockaddr_un
));
67 dst
->addrlen
= fromlen
;
68 dst
->debug_level
= MSG_INFO
;
69 dst
->next
= priv
->ctrl_dst
;
71 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor attached",
72 (u8
*) from
->sun_path
, fromlen
- sizeof(from
->sun_family
));
77 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv
*priv
,
78 struct sockaddr_un
*from
,
81 struct wpa_ctrl_dst
*dst
, *prev
= NULL
;
85 if (fromlen
== dst
->addrlen
&&
86 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
87 fromlen
- sizeof(from
->sun_family
)) == 0) {
89 priv
->ctrl_dst
= dst
->next
;
91 prev
->next
= dst
->next
;
93 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor detached",
94 (u8
*) from
->sun_path
,
95 fromlen
- sizeof(from
->sun_family
));
105 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv
*priv
,
106 struct sockaddr_un
*from
,
110 struct wpa_ctrl_dst
*dst
;
112 wpa_printf(MSG_DEBUG
, "CTRL_IFACE LEVEL %s", level
);
114 dst
= priv
->ctrl_dst
;
116 if (fromlen
== dst
->addrlen
&&
117 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
118 fromlen
- sizeof(from
->sun_family
)) == 0) {
119 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE changed monitor "
120 "level", (u8
*) from
->sun_path
,
121 fromlen
- sizeof(from
->sun_family
));
122 dst
->debug_level
= atoi(level
);
132 static void wpa_supplicant_ctrl_iface_receive(int sock
, void *eloop_ctx
,
135 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
136 struct ctrl_iface_priv
*priv
= sock_ctx
;
139 struct sockaddr_un from
;
140 socklen_t fromlen
= sizeof(from
);
142 size_t reply_len
= 0;
143 int new_attached
= 0;
145 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
146 (struct sockaddr
*) &from
, &fromlen
);
148 perror("recvfrom(ctrl_iface)");
153 if (os_strcmp(buf
, "ATTACH") == 0) {
154 if (wpa_supplicant_ctrl_iface_attach(priv
, &from
, fromlen
))
160 } else if (os_strcmp(buf
, "DETACH") == 0) {
161 if (wpa_supplicant_ctrl_iface_detach(priv
, &from
, fromlen
))
165 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
166 if (wpa_supplicant_ctrl_iface_level(priv
, &from
, fromlen
,
172 reply
= wpa_supplicant_ctrl_iface_process(wpa_s
, buf
,
177 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
180 } else if (reply_len
== 1) {
181 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
183 } else if (reply_len
== 2) {
184 sendto(sock
, "OK\n", 3, 0, (struct sockaddr
*) &from
,
189 eapol_sm_notify_ctrl_attached(wpa_s
->eapol
);
193 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant
*wpa_s
)
197 char *pbuf
, *dir
= NULL
, *gid_str
= NULL
;
200 if (wpa_s
->conf
->ctrl_interface
== NULL
)
203 pbuf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
206 if (os_strncmp(pbuf
, "DIR=", 4) == 0) {
208 gid_str
= os_strstr(dir
, " GROUP=");
216 len
= os_strlen(dir
) + os_strlen(wpa_s
->ifname
) + 2;
217 buf
= os_malloc(len
);
223 res
= os_snprintf(buf
, len
, "%s/%s", dir
, wpa_s
->ifname
);
224 if (res
< 0 || (size_t) res
>= len
) {
231 /* Windows/WinPcap uses interface names that are not suitable
232 * as a file name - convert invalid chars to underscores */
240 #endif /* __CYGWIN__ */
246 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx
, int level
,
247 const char *txt
, size_t len
)
249 struct wpa_supplicant
*wpa_s
= ctx
;
250 if (wpa_s
== NULL
|| wpa_s
->ctrl_iface
== NULL
)
252 wpa_supplicant_ctrl_iface_send(wpa_s
->ctrl_iface
, level
, txt
, len
);
256 struct ctrl_iface_priv
*
257 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant
*wpa_s
)
259 struct ctrl_iface_priv
*priv
;
260 struct sockaddr_un addr
;
264 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
268 priv
= os_zalloc(sizeof(*priv
));
274 if (wpa_s
->conf
->ctrl_interface
== NULL
)
277 buf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
280 if (os_strncmp(buf
, "DIR=", 4) == 0) {
282 gid_str
= os_strstr(dir
, " GROUP=");
289 gid_str
= wpa_s
->conf
->ctrl_interface_group
;
292 if (mkdir(dir
, S_IRWXU
| S_IRWXG
) < 0) {
293 if (errno
== EEXIST
) {
294 wpa_printf(MSG_DEBUG
, "Using existing control "
295 "interface directory.");
297 perror("mkdir[ctrl_interface]");
303 grp
= getgrnam(gid_str
);
307 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d"
308 " (from group name '%s')",
311 /* Group name not found - try to parse this as gid */
312 gid
= strtol(gid_str
, &endp
, 10);
313 if (*gid_str
== '\0' || *endp
!= '\0') {
314 wpa_printf(MSG_ERROR
, "CTRL: Invalid group "
319 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d",
324 if (gid_set
&& chown(dir
, -1, gid
) < 0) {
325 perror("chown[ctrl_interface]");
329 if (os_strlen(dir
) + 1 + os_strlen(wpa_s
->ifname
) >=
330 sizeof(addr
.sun_path
)) {
331 wpa_printf(MSG_ERROR
, "ctrl_iface path limit exceeded");
335 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
336 if (priv
->sock
< 0) {
337 perror("socket(PF_UNIX)");
341 os_memset(&addr
, 0, sizeof(addr
));
342 addr
.sun_family
= AF_UNIX
;
343 fname
= wpa_supplicant_ctrl_iface_path(wpa_s
);
346 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
347 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
348 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
350 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
352 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
353 " allow connections - assuming it was left"
354 "over from forced program termination");
355 if (unlink(fname
) < 0) {
356 perror("unlink[ctrl_iface]");
357 wpa_printf(MSG_ERROR
, "Could not unlink "
358 "existing ctrl_iface socket '%s'",
362 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
364 perror("bind(PF_UNIX)");
367 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
368 "ctrl_iface socket '%s'", fname
);
370 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
371 "be in use - cannot override it");
372 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
373 "not used anymore", fname
);
380 if (gid_set
&& chown(fname
, -1, gid
) < 0) {
381 perror("chown[ctrl_interface/ifname]");
385 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
386 perror("chmod[ctrl_interface/ifname]");
391 eloop_register_read_sock(priv
->sock
, wpa_supplicant_ctrl_iface_receive
,
393 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
411 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv
*priv
)
413 struct wpa_ctrl_dst
*dst
, *prev
;
415 if (priv
->sock
> -1) {
417 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
418 eloop_unregister_read_sock(priv
->sock
);
419 if (priv
->ctrl_dst
) {
421 * Wait a second before closing the control socket if
422 * there are any attached monitors in order to allow
423 * them to receive any pending messages.
425 wpa_printf(MSG_DEBUG
, "CTRL_IFACE wait for attached "
426 "monitors to receive messages");
431 fname
= wpa_supplicant_ctrl_iface_path(priv
->wpa_s
);
437 buf
= os_strdup(priv
->wpa_s
->conf
->ctrl_interface
);
440 if (os_strncmp(buf
, "DIR=", 4) == 0) {
442 gid_str
= os_strstr(dir
, " GROUP=");
450 if (rmdir(dir
) < 0) {
451 if (errno
== ENOTEMPTY
) {
452 wpa_printf(MSG_DEBUG
, "Control interface "
453 "directory not empty - leaving it "
456 perror("rmdir[ctrl_interface]");
463 dst
= priv
->ctrl_dst
;
474 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
475 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
476 * @level: Priority level of the message
478 * @len: Message length
480 * Send a packet to all monitor programs attached to the control interface.
482 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
483 int level
, const char *buf
,
486 struct wpa_ctrl_dst
*dst
, *next
;
492 dst
= priv
->ctrl_dst
;
493 if (priv
->sock
< 0 || dst
== NULL
)
496 res
= os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
497 if (res
< 0 || (size_t) res
>= sizeof(levelstr
))
499 io
[0].iov_base
= levelstr
;
500 io
[0].iov_len
= os_strlen(levelstr
);
501 io
[1].iov_base
= (char *) buf
;
503 os_memset(&msg
, 0, sizeof(msg
));
510 if (level
>= dst
->debug_level
) {
511 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
512 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
513 sizeof(dst
->addr
.sun_family
));
514 msg
.msg_name
= (void *) &dst
->addr
;
515 msg
.msg_namelen
= dst
->addrlen
;
516 if (sendmsg(priv
->sock
, &msg
, 0) < 0) {
517 perror("sendmsg(CTRL_IFACE monitor)");
519 if (dst
->errors
> 10) {
520 wpa_supplicant_ctrl_iface_detach(
533 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv
*priv
)
537 struct sockaddr_un from
;
538 socklen_t fromlen
= sizeof(from
);
541 wpa_printf(MSG_DEBUG
, "CTRL_IFACE - %s - wait for monitor to "
542 "attach", priv
->wpa_s
->ifname
);
543 eloop_wait_for_read_sock(priv
->sock
);
545 res
= recvfrom(priv
->sock
, buf
, sizeof(buf
) - 1, 0,
546 (struct sockaddr
*) &from
, &fromlen
);
548 perror("recvfrom(ctrl_iface)");
553 if (os_strcmp(buf
, "ATTACH") == 0) {
554 /* handle ATTACH signal of first monitor interface */
555 if (!wpa_supplicant_ctrl_iface_attach(priv
, &from
,
557 sendto(priv
->sock
, "OK\n", 3, 0,
558 (struct sockaddr
*) &from
, fromlen
);
562 sendto(priv
->sock
, "FAIL\n", 5, 0,
563 (struct sockaddr
*) &from
, fromlen
);
566 /* return FAIL for all other signals */
567 sendto(priv
->sock
, "FAIL\n", 5, 0,
568 (struct sockaddr
*) &from
, fromlen
);
574 /* Global ctrl_iface */
576 struct ctrl_iface_global_priv
{
577 struct wpa_global
*global
;
582 static void wpa_supplicant_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
585 struct wpa_global
*global
= eloop_ctx
;
588 struct sockaddr_un from
;
589 socklen_t fromlen
= sizeof(from
);
593 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
594 (struct sockaddr
*) &from
, &fromlen
);
596 perror("recvfrom(ctrl_iface)");
601 reply
= wpa_supplicant_global_ctrl_iface_process(global
, buf
,
605 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
608 } else if (reply_len
) {
609 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
615 struct ctrl_iface_global_priv
*
616 wpa_supplicant_global_ctrl_iface_init(struct wpa_global
*global
)
618 struct ctrl_iface_global_priv
*priv
;
619 struct sockaddr_un addr
;
621 priv
= os_zalloc(sizeof(*priv
));
624 priv
->global
= global
;
627 if (global
->params
.ctrl_interface
== NULL
)
630 wpa_printf(MSG_DEBUG
, "Global control interface '%s'",
631 global
->params
.ctrl_interface
);
633 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
634 if (priv
->sock
< 0) {
635 perror("socket(PF_UNIX)");
639 os_memset(&addr
, 0, sizeof(addr
));
640 addr
.sun_family
= AF_UNIX
;
641 os_strlcpy(addr
.sun_path
, global
->params
.ctrl_interface
,
642 sizeof(addr
.sun_path
));
643 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
644 perror("bind(PF_UNIX)");
645 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
647 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
648 " allow connections - assuming it was left"
649 "over from forced program termination");
650 if (unlink(global
->params
.ctrl_interface
) < 0) {
651 perror("unlink[ctrl_iface]");
652 wpa_printf(MSG_ERROR
, "Could not unlink "
653 "existing ctrl_iface socket '%s'",
654 global
->params
.ctrl_interface
);
657 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
659 perror("bind(PF_UNIX)");
662 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
663 "ctrl_iface socket '%s'",
664 global
->params
.ctrl_interface
);
666 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
667 "be in use - cannot override it");
668 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
670 global
->params
.ctrl_interface
);
675 eloop_register_read_sock(priv
->sock
,
676 wpa_supplicant_global_ctrl_iface_receive
,
690 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv
*priv
)
692 if (priv
->sock
>= 0) {
693 eloop_unregister_read_sock(priv
->sock
);
696 if (priv
->global
->params
.ctrl_interface
)
697 unlink(priv
->global
->params
.ctrl_interface
);