Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / contrib / wpa_supplicant-0.5.8 / ctrl_iface_unix.c
blobe9bc47239c639d3cf8fa77ced6b07e2f4f01f9fa
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <sys/un.h>
17 #include <sys/stat.h>
18 #include <grp.h>
20 #include "common.h"
21 #include "eloop.h"
22 #include "config.h"
23 #include "eapol_sm.h"
24 #include "wpa_supplicant_i.h"
25 #include "ctrl_iface.h"
27 /* Per-interface ctrl_iface */
29 /**
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.
36 struct wpa_ctrl_dst {
37 struct wpa_ctrl_dst *next;
38 struct sockaddr_un addr;
39 socklen_t addrlen;
40 int debug_level;
41 int errors;
45 struct ctrl_iface_priv {
46 struct wpa_supplicant *wpa_s;
47 int sock;
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,
54 size_t len);
57 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
58 struct sockaddr_un *from,
59 socklen_t fromlen)
61 struct wpa_ctrl_dst *dst;
63 dst = os_zalloc(sizeof(*dst));
64 if (dst == NULL)
65 return -1;
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;
70 priv->ctrl_dst = dst;
71 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
72 (u8 *) from->sun_path, fromlen - sizeof(from->sun_family));
73 return 0;
77 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
78 struct sockaddr_un *from,
79 socklen_t fromlen)
81 struct wpa_ctrl_dst *dst, *prev = NULL;
83 dst = priv->ctrl_dst;
84 while (dst) {
85 if (fromlen == dst->addrlen &&
86 os_memcmp(from->sun_path, dst->addr.sun_path,
87 fromlen - sizeof(from->sun_family)) == 0) {
88 if (prev == NULL)
89 priv->ctrl_dst = dst->next;
90 else
91 prev->next = dst->next;
92 os_free(dst);
93 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
94 (u8 *) from->sun_path,
95 fromlen - sizeof(from->sun_family));
96 return 0;
98 prev = dst;
99 dst = dst->next;
101 return -1;
105 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
106 struct sockaddr_un *from,
107 socklen_t fromlen,
108 char *level)
110 struct wpa_ctrl_dst *dst;
112 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
114 dst = priv->ctrl_dst;
115 while (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);
123 return 0;
125 dst = dst->next;
128 return -1;
132 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
133 void *sock_ctx)
135 struct wpa_supplicant *wpa_s = eloop_ctx;
136 struct ctrl_iface_priv *priv = sock_ctx;
137 char buf[256];
138 int res;
139 struct sockaddr_un from;
140 socklen_t fromlen = sizeof(from);
141 char *reply = NULL;
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);
147 if (res < 0) {
148 perror("recvfrom(ctrl_iface)");
149 return;
151 buf[res] = '\0';
153 if (os_strcmp(buf, "ATTACH") == 0) {
154 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
155 reply_len = 1;
156 else {
157 new_attached = 1;
158 reply_len = 2;
160 } else if (os_strcmp(buf, "DETACH") == 0) {
161 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
162 reply_len = 1;
163 else
164 reply_len = 2;
165 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
166 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
167 buf + 6))
168 reply_len = 1;
169 else
170 reply_len = 2;
171 } else {
172 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
173 &reply_len);
176 if (reply) {
177 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
178 fromlen);
179 os_free(reply);
180 } else if (reply_len == 1) {
181 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
182 fromlen);
183 } else if (reply_len == 2) {
184 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
185 fromlen);
188 if (new_attached)
189 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
193 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
195 char *buf;
196 size_t len;
197 char *pbuf, *dir = NULL, *gid_str = NULL;
199 if (wpa_s->conf->ctrl_interface == NULL)
200 return NULL;
202 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
203 if (pbuf == NULL)
204 return NULL;
205 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
206 dir = pbuf + 4;
207 gid_str = os_strstr(dir, " GROUP=");
208 if (gid_str) {
209 *gid_str = '\0';
210 gid_str += 7;
212 } else
213 dir = pbuf;
215 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
216 buf = os_malloc(len);
217 if (buf == NULL) {
218 os_free(pbuf);
219 return NULL;
222 os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
223 #ifdef __CYGWIN__
225 /* Windows/WinPcap uses interface names that are not suitable
226 * as a file name - convert invalid chars to underscores */
227 char *pos = buf;
228 while (*pos) {
229 if (*pos == '\\')
230 *pos = '_';
231 pos++;
234 #endif /* __CYGWIN__ */
235 os_free(pbuf);
236 return buf;
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)
245 return;
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;
255 char *fname = NULL;
256 gid_t gid = 0;
257 int gid_set = 0;
258 char *buf, *dir = NULL, *gid_str = NULL;
259 struct group *grp;
260 char *endp;
262 priv = os_zalloc(sizeof(*priv));
263 if (priv == NULL)
264 return NULL;
265 priv->wpa_s = wpa_s;
266 priv->sock = -1;
268 if (wpa_s->conf->ctrl_interface == NULL)
269 return priv;
271 buf = os_strdup(wpa_s->conf->ctrl_interface);
272 if (buf == NULL)
273 goto fail;
274 if (os_strncmp(buf, "DIR=", 4) == 0) {
275 dir = buf + 4;
276 gid_str = os_strstr(dir, " GROUP=");
277 if (gid_str) {
278 *gid_str = '\0';
279 gid_str += 7;
281 } else {
282 dir = buf;
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.");
290 } else {
291 perror("mkdir[ctrl_interface]");
292 goto fail;
296 if (gid_str) {
297 grp = getgrnam(gid_str);
298 if (grp) {
299 gid = grp->gr_gid;
300 gid_set = 1;
301 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
302 " (from group name '%s')",
303 (int) gid, gid_str);
304 } else {
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 "
309 "'%s'", gid_str);
310 goto fail;
312 gid_set = 1;
313 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
314 (int) gid);
318 if (gid_set && chown(dir, -1, gid) < 0) {
319 perror("chown[ctrl_interface]");
320 goto fail;
323 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
324 sizeof(addr.sun_path))
325 goto fail;
327 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
328 if (priv->sock < 0) {
329 perror("socket(PF_UNIX)");
330 goto fail;
333 os_memset(&addr, 0, sizeof(addr));
334 addr.sun_family = AF_UNIX;
335 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
336 if (fname == NULL)
337 goto fail;
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",
341 strerror(errno));
342 if (connect(priv->sock, (struct sockaddr *) &addr,
343 sizeof(addr)) < 0) {
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'",
351 fname);
352 goto fail;
354 if (bind(priv->sock, (struct sockaddr *) &addr,
355 sizeof(addr)) < 0) {
356 perror("bind(PF_UNIX)");
357 goto fail;
359 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
360 "ctrl_iface socket '%s'", fname);
361 } else {
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);
366 os_free(fname);
367 fname = NULL;
368 goto fail;
372 if (gid_set && chown(fname, -1, gid) < 0) {
373 perror("chown[ctrl_interface/ifname]");
374 goto fail;
377 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
378 perror("chmod[ctrl_interface/ifname]");
379 goto fail;
381 os_free(fname);
383 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
384 wpa_s, priv);
385 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
387 os_free(buf);
388 return priv;
390 fail:
391 if (priv->sock >= 0)
392 close(priv->sock);
393 os_free(priv);
394 if (fname) {
395 unlink(fname);
396 os_free(fname);
398 os_free(buf);
399 return NULL;
403 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
405 struct wpa_ctrl_dst *dst, *prev;
407 if (priv->sock > -1) {
408 char *fname;
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");
419 os_sleep(1, 0);
421 close(priv->sock);
422 priv->sock = -1;
423 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
424 if (fname) {
425 unlink(fname);
426 os_free(fname);
429 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
430 if (buf == NULL)
431 goto free_dst;
432 if (os_strncmp(buf, "DIR=", 4) == 0) {
433 dir = buf + 4;
434 gid_str = os_strstr(dir, " GROUP=");
435 if (gid_str) {
436 *gid_str = '\0';
437 gid_str += 7;
439 } else
440 dir = buf;
442 if (rmdir(dir) < 0) {
443 if (errno == ENOTEMPTY) {
444 wpa_printf(MSG_DEBUG, "Control interface "
445 "directory not empty - leaving it "
446 "behind");
447 } else {
448 perror("rmdir[ctrl_interface]");
451 os_free(buf);
454 free_dst:
455 dst = priv->ctrl_dst;
456 while (dst) {
457 prev = dst;
458 dst = dst->next;
459 os_free(prev);
461 os_free(priv);
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
469 * @buf: Message data
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,
476 size_t len)
478 struct wpa_ctrl_dst *dst, *next;
479 char levelstr[10];
480 int idx;
481 struct msghdr msg;
482 struct iovec io[2];
484 dst = priv->ctrl_dst;
485 if (priv->sock < 0 || dst == NULL)
486 return;
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;
492 io[1].iov_len = len;
493 os_memset(&msg, 0, sizeof(msg));
494 msg.msg_iov = io;
495 msg.msg_iovlen = 2;
497 idx = 0;
498 while (dst) {
499 next = dst->next;
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)");
508 dst->errors++;
509 if (dst->errors > 10) {
510 wpa_supplicant_ctrl_iface_detach(
511 priv, &dst->addr,
512 dst->addrlen);
514 } else
515 dst->errors = 0;
517 idx++;
518 dst = next;
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;
535 int sock;
539 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
540 void *sock_ctx)
542 struct wpa_global *global = eloop_ctx;
543 char buf[256];
544 int res;
545 struct sockaddr_un from;
546 socklen_t fromlen = sizeof(from);
547 char *reply;
548 size_t reply_len;
550 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
551 (struct sockaddr *) &from, &fromlen);
552 if (res < 0) {
553 perror("recvfrom(ctrl_iface)");
554 return;
556 buf[res] = '\0';
558 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
559 &reply_len);
561 if (reply) {
562 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
563 fromlen);
564 os_free(reply);
565 } else if (reply_len) {
566 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
567 fromlen);
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));
579 if (priv == NULL)
580 return NULL;
581 priv->global = global;
582 priv->sock = -1;
584 if (global->params.ctrl_interface == NULL)
585 return priv;
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)");
593 goto fail;
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,
603 sizeof(addr)) < 0) {
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);
612 goto fail;
614 if (bind(priv->sock, (struct sockaddr *) &addr,
615 sizeof(addr)) < 0) {
616 perror("bind(PF_UNIX)");
617 goto fail;
619 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
620 "ctrl_iface socket '%s'",
621 global->params.ctrl_interface);
622 } else {
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 "
626 "not used anymore",
627 global->params.ctrl_interface);
628 goto fail;
632 eloop_register_read_sock(priv->sock,
633 wpa_supplicant_global_ctrl_iface_receive,
634 global, NULL);
636 return priv;
638 fail:
639 if (priv->sock >= 0)
640 close(priv->sock);
641 os_free(priv);
642 return NULL;
646 void
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);
651 close(priv->sock);
653 if (priv->global->params.ctrl_interface)
654 unlink(priv->global->params.ctrl_interface);
655 os_free(priv);