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 "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
;
199 if (wpa_s
->conf
->ctrl_interface
== NULL
)
202 pbuf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
205 if (os_strncmp(pbuf
, "DIR=", 4) == 0) {
207 gid_str
= os_strstr(dir
, " GROUP=");
215 len
= os_strlen(dir
) + os_strlen(wpa_s
->ifname
) + 2;
216 buf
= os_malloc(len
);
222 os_snprintf(buf
, len
, "%s/%s", dir
, wpa_s
->ifname
);
225 /* Windows/WinPcap uses interface names that are not suitable
226 * as a file name - convert invalid chars to underscores */
234 #endif /* __CYGWIN__ */
240 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx
, int level
,
241 const char *txt
, size_t len
)
243 struct wpa_supplicant
*wpa_s
= ctx
;
244 if (wpa_s
== NULL
|| wpa_s
->ctrl_iface
== NULL
)
246 wpa_supplicant_ctrl_iface_send(wpa_s
->ctrl_iface
, level
, txt
, len
);
250 struct ctrl_iface_priv
*
251 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant
*wpa_s
)
253 struct ctrl_iface_priv
*priv
;
254 struct sockaddr_un addr
;
258 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
262 priv
= os_zalloc(sizeof(*priv
));
268 if (wpa_s
->conf
->ctrl_interface
== NULL
)
271 buf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
274 if (os_strncmp(buf
, "DIR=", 4) == 0) {
276 gid_str
= os_strstr(dir
, " GROUP=");
283 gid_str
= wpa_s
->conf
->ctrl_interface_group
;
286 if (mkdir(dir
, S_IRWXU
| S_IRWXG
) < 0) {
287 if (errno
== EEXIST
) {
288 wpa_printf(MSG_DEBUG
, "Using existing control "
289 "interface directory.");
291 perror("mkdir[ctrl_interface]");
297 grp
= getgrnam(gid_str
);
301 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d"
302 " (from group name '%s')",
305 /* Group name not found - try to parse this as gid */
306 gid
= strtol(gid_str
, &endp
, 10);
307 if (*gid_str
== '\0' || *endp
!= '\0') {
308 wpa_printf(MSG_DEBUG
, "CTRL: Invalid group "
313 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d",
318 if (gid_set
&& chown(dir
, -1, gid
) < 0) {
319 perror("chown[ctrl_interface]");
323 if (os_strlen(dir
) + 1 + os_strlen(wpa_s
->ifname
) >=
324 sizeof(addr
.sun_path
))
327 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
328 if (priv
->sock
< 0) {
329 perror("socket(PF_UNIX)");
333 os_memset(&addr
, 0, sizeof(addr
));
334 addr
.sun_family
= AF_UNIX
;
335 fname
= wpa_supplicant_ctrl_iface_path(wpa_s
);
338 os_strncpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
339 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
340 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
342 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
344 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
345 " allow connections - assuming it was left"
346 "over from forced program termination");
347 if (unlink(fname
) < 0) {
348 perror("unlink[ctrl_iface]");
349 wpa_printf(MSG_ERROR
, "Could not unlink "
350 "existing ctrl_iface socket '%s'",
354 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
356 perror("bind(PF_UNIX)");
359 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
360 "ctrl_iface socket '%s'", fname
);
362 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
363 "be in use - cannot override it");
364 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
365 "not used anymore", fname
);
372 if (gid_set
&& chown(fname
, -1, gid
) < 0) {
373 perror("chown[ctrl_interface/ifname]");
377 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
378 perror("chmod[ctrl_interface/ifname]");
383 eloop_register_read_sock(priv
->sock
, wpa_supplicant_ctrl_iface_receive
,
385 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
403 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv
*priv
)
405 struct wpa_ctrl_dst
*dst
, *prev
;
407 if (priv
->sock
> -1) {
409 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
410 eloop_unregister_read_sock(priv
->sock
);
411 if (priv
->ctrl_dst
) {
413 * Wait a second before closing the control socket if
414 * there are any attached monitors in order to allow
415 * them to receive any pending messages.
417 wpa_printf(MSG_DEBUG
, "CTRL_IFACE wait for attached "
418 "monitors to receive messages");
423 fname
= wpa_supplicant_ctrl_iface_path(priv
->wpa_s
);
429 buf
= os_strdup(priv
->wpa_s
->conf
->ctrl_interface
);
432 if (os_strncmp(buf
, "DIR=", 4) == 0) {
434 gid_str
= os_strstr(dir
, " GROUP=");
442 if (rmdir(dir
) < 0) {
443 if (errno
== ENOTEMPTY
) {
444 wpa_printf(MSG_DEBUG
, "Control interface "
445 "directory not empty - leaving it "
448 perror("rmdir[ctrl_interface]");
455 dst
= priv
->ctrl_dst
;
466 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
467 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
468 * @level: Priority level of the message
470 * @len: Message length
472 * Send a packet to all monitor programs attached to the control interface.
474 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
475 int level
, const char *buf
,
478 struct wpa_ctrl_dst
*dst
, *next
;
484 dst
= priv
->ctrl_dst
;
485 if (priv
->sock
< 0 || dst
== NULL
)
488 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
489 io
[0].iov_base
= levelstr
;
490 io
[0].iov_len
= os_strlen(levelstr
);
491 io
[1].iov_base
= (char *) buf
;
493 os_memset(&msg
, 0, sizeof(msg
));
500 if (level
>= dst
->debug_level
) {
501 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
502 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
503 sizeof(dst
->addr
.sun_family
));
504 msg
.msg_name
= (void *) &dst
->addr
;
505 msg
.msg_namelen
= dst
->addrlen
;
506 if (sendmsg(priv
->sock
, &msg
, 0) < 0) {
507 perror("sendmsg(CTRL_IFACE monitor)");
509 if (dst
->errors
> 10) {
510 wpa_supplicant_ctrl_iface_detach(
523 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv
*priv
)
525 wpa_printf(MSG_DEBUG
, "CTRL_IFACE - %s - wait for monitor",
526 priv
->wpa_s
->ifname
);
527 eloop_wait_for_read_sock(priv
->sock
);
531 /* Global ctrl_iface */
533 struct ctrl_iface_global_priv
{
534 struct wpa_global
*global
;
539 static void wpa_supplicant_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
542 struct wpa_global
*global
= eloop_ctx
;
545 struct sockaddr_un from
;
546 socklen_t fromlen
= sizeof(from
);
550 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
551 (struct sockaddr
*) &from
, &fromlen
);
553 perror("recvfrom(ctrl_iface)");
558 reply
= wpa_supplicant_global_ctrl_iface_process(global
, buf
,
562 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
565 } else if (reply_len
) {
566 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
572 struct ctrl_iface_global_priv
*
573 wpa_supplicant_global_ctrl_iface_init(struct wpa_global
*global
)
575 struct ctrl_iface_global_priv
*priv
;
576 struct sockaddr_un addr
;
578 priv
= os_zalloc(sizeof(*priv
));
581 priv
->global
= global
;
584 if (global
->params
.ctrl_interface
== NULL
)
587 wpa_printf(MSG_DEBUG
, "Global control interface '%s'",
588 global
->params
.ctrl_interface
);
590 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
591 if (priv
->sock
< 0) {
592 perror("socket(PF_UNIX)");
596 os_memset(&addr
, 0, sizeof(addr
));
597 addr
.sun_family
= AF_UNIX
;
598 os_strncpy(addr
.sun_path
, global
->params
.ctrl_interface
,
599 sizeof(addr
.sun_path
));
600 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
601 perror("bind(PF_UNIX)");
602 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
604 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
605 " allow connections - assuming it was left"
606 "over from forced program termination");
607 if (unlink(global
->params
.ctrl_interface
) < 0) {
608 perror("unlink[ctrl_iface]");
609 wpa_printf(MSG_ERROR
, "Could not unlink "
610 "existing ctrl_iface socket '%s'",
611 global
->params
.ctrl_interface
);
614 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
616 perror("bind(PF_UNIX)");
619 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
620 "ctrl_iface socket '%s'",
621 global
->params
.ctrl_interface
);
623 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
624 "be in use - cannot override it");
625 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
627 global
->params
.ctrl_interface
);
632 eloop_register_read_sock(priv
->sock
,
633 wpa_supplicant_global_ctrl_iface_receive
,
647 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv
*priv
)
649 if (priv
->sock
>= 0) {
650 eloop_unregister_read_sock(priv
->sock
);
653 if (priv
->global
->params
.ctrl_interface
)
654 unlink(priv
->global
->params
.ctrl_interface
);