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 if (os_strlen(dir
) + 1 + os_strlen(wpa_s
->ifname
) >=
336 sizeof(addr
.sun_path
)) {
337 wpa_printf(MSG_ERROR
, "ctrl_iface path limit exceeded");
341 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
342 if (priv
->sock
< 0) {
343 perror("socket(PF_UNIX)");
347 os_memset(&addr
, 0, sizeof(addr
));
349 addr
.sun_len
= sizeof(addr
);
350 #endif /* __FreeBSD__ */
351 addr
.sun_family
= AF_UNIX
;
352 fname
= wpa_supplicant_ctrl_iface_path(wpa_s
);
355 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
356 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
357 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
359 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
361 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
362 " allow connections - assuming it was left"
363 "over from forced program termination");
364 if (unlink(fname
) < 0) {
365 perror("unlink[ctrl_iface]");
366 wpa_printf(MSG_ERROR
, "Could not unlink "
367 "existing ctrl_iface socket '%s'",
371 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
373 perror("bind(PF_UNIX)");
376 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
377 "ctrl_iface socket '%s'", fname
);
379 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
380 "be in use - cannot override it");
381 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
382 "not used anymore", fname
);
389 if (gid_set
&& chown(fname
, -1, gid
) < 0) {
390 perror("chown[ctrl_interface/ifname]");
394 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
395 perror("chmod[ctrl_interface/ifname]");
400 eloop_register_read_sock(priv
->sock
, wpa_supplicant_ctrl_iface_receive
,
402 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
420 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv
*priv
)
422 struct wpa_ctrl_dst
*dst
, *prev
;
424 if (priv
->sock
> -1) {
426 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
427 eloop_unregister_read_sock(priv
->sock
);
428 if (priv
->ctrl_dst
) {
430 * Wait a second before closing the control socket if
431 * there are any attached monitors in order to allow
432 * them to receive any pending messages.
434 wpa_printf(MSG_DEBUG
, "CTRL_IFACE wait for attached "
435 "monitors to receive messages");
440 fname
= wpa_supplicant_ctrl_iface_path(priv
->wpa_s
);
446 buf
= os_strdup(priv
->wpa_s
->conf
->ctrl_interface
);
449 if (os_strncmp(buf
, "DIR=", 4) == 0) {
451 gid_str
= os_strstr(dir
, " GROUP=");
459 if (rmdir(dir
) < 0) {
460 if (errno
== ENOTEMPTY
) {
461 wpa_printf(MSG_DEBUG
, "Control interface "
462 "directory not empty - leaving it "
465 perror("rmdir[ctrl_interface]");
472 dst
= priv
->ctrl_dst
;
483 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
484 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
485 * @level: Priority level of the message
487 * @len: Message length
489 * Send a packet to all monitor programs attached to the control interface.
491 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
492 int level
, const char *buf
,
495 struct wpa_ctrl_dst
*dst
, *next
;
501 dst
= priv
->ctrl_dst
;
502 if (priv
->sock
< 0 || dst
== NULL
)
505 res
= os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
506 if (res
< 0 || (size_t) res
>= sizeof(levelstr
))
508 io
[0].iov_base
= levelstr
;
509 io
[0].iov_len
= os_strlen(levelstr
);
510 io
[1].iov_base
= (char *) buf
;
512 os_memset(&msg
, 0, sizeof(msg
));
519 if (level
>= dst
->debug_level
) {
520 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
521 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
522 offsetof(struct sockaddr_un
, sun_path
));
523 msg
.msg_name
= (void *) &dst
->addr
;
524 msg
.msg_namelen
= dst
->addrlen
;
525 if (sendmsg(priv
->sock
, &msg
, 0) < 0) {
527 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
529 idx
, errno
, strerror(errno
));
531 if (dst
->errors
> 10 || _errno
== ENOENT
) {
532 wpa_supplicant_ctrl_iface_detach(
545 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv
*priv
)
549 struct sockaddr_un from
;
550 socklen_t fromlen
= sizeof(from
);
553 wpa_printf(MSG_DEBUG
, "CTRL_IFACE - %s - wait for monitor to "
554 "attach", priv
->wpa_s
->ifname
);
555 eloop_wait_for_read_sock(priv
->sock
);
557 res
= recvfrom(priv
->sock
, buf
, sizeof(buf
) - 1, 0,
558 (struct sockaddr
*) &from
, &fromlen
);
560 perror("recvfrom(ctrl_iface)");
565 if (os_strcmp(buf
, "ATTACH") == 0) {
566 /* handle ATTACH signal of first monitor interface */
567 if (!wpa_supplicant_ctrl_iface_attach(priv
, &from
,
569 sendto(priv
->sock
, "OK\n", 3, 0,
570 (struct sockaddr
*) &from
, fromlen
);
574 sendto(priv
->sock
, "FAIL\n", 5, 0,
575 (struct sockaddr
*) &from
, fromlen
);
578 /* return FAIL for all other signals */
579 sendto(priv
->sock
, "FAIL\n", 5, 0,
580 (struct sockaddr
*) &from
, fromlen
);
586 /* Global ctrl_iface */
588 struct ctrl_iface_global_priv
{
589 struct wpa_global
*global
;
594 static void wpa_supplicant_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
597 struct wpa_global
*global
= eloop_ctx
;
600 struct sockaddr_un from
;
601 socklen_t fromlen
= sizeof(from
);
605 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
606 (struct sockaddr
*) &from
, &fromlen
);
608 perror("recvfrom(ctrl_iface)");
613 reply
= wpa_supplicant_global_ctrl_iface_process(global
, buf
,
617 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
620 } else if (reply_len
) {
621 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
627 struct ctrl_iface_global_priv
*
628 wpa_supplicant_global_ctrl_iface_init(struct wpa_global
*global
)
630 struct ctrl_iface_global_priv
*priv
;
631 struct sockaddr_un addr
;
633 priv
= os_zalloc(sizeof(*priv
));
636 priv
->global
= global
;
639 if (global
->params
.ctrl_interface
== NULL
)
642 wpa_printf(MSG_DEBUG
, "Global control interface '%s'",
643 global
->params
.ctrl_interface
);
645 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
646 if (priv
->sock
< 0) {
647 perror("socket(PF_UNIX)");
651 os_memset(&addr
, 0, sizeof(addr
));
653 addr
.sun_len
= sizeof(addr
);
654 #endif /* __FreeBSD__ */
655 addr
.sun_family
= AF_UNIX
;
656 os_strlcpy(addr
.sun_path
, global
->params
.ctrl_interface
,
657 sizeof(addr
.sun_path
));
658 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
659 perror("bind(PF_UNIX)");
660 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
662 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
663 " allow connections - assuming it was left"
664 "over from forced program termination");
665 if (unlink(global
->params
.ctrl_interface
) < 0) {
666 perror("unlink[ctrl_iface]");
667 wpa_printf(MSG_ERROR
, "Could not unlink "
668 "existing ctrl_iface socket '%s'",
669 global
->params
.ctrl_interface
);
672 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
674 perror("bind(PF_UNIX)");
677 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
678 "ctrl_iface socket '%s'",
679 global
->params
.ctrl_interface
);
681 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
682 "be in use - cannot override it");
683 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
685 global
->params
.ctrl_interface
);
690 eloop_register_read_sock(priv
->sock
,
691 wpa_supplicant_global_ctrl_iface_receive
,
705 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv
*priv
)
707 if (priv
->sock
>= 0) {
708 eloop_unregister_read_sock(priv
->sock
);
711 if (priv
->global
->params
.ctrl_interface
)
712 unlink(priv
->global
->params
.ctrl_interface
);