Use common driver code for Linux hwaddr get/set
[hostap-gosc2009.git] / wpa_supplicant / ctrl_iface_unix.c
blob6f78aa54292973a136927a00e8940e3bc9d4cbb4
1 /*
2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2009, 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>
19 #include <stddef.h>
21 #include "utils/common.h"
22 #include "utils/eloop.h"
23 #include "utils/list.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "config.h"
26 #include "wpa_supplicant_i.h"
27 #include "ctrl_iface.h"
29 /* Per-interface ctrl_iface */
31 /**
32 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
34 * This structure is used to store information about registered control
35 * interface monitors into struct wpa_supplicant. This data is private to
36 * ctrl_iface_unix.c and should not be touched directly from other files.
38 struct wpa_ctrl_dst {
39 struct dl_list list;
40 struct sockaddr_un addr;
41 socklen_t addrlen;
42 int debug_level;
43 int errors;
47 struct ctrl_iface_priv {
48 struct wpa_supplicant *wpa_s;
49 int sock;
50 struct dl_list ctrl_dst;
54 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
55 int level, const char *buf,
56 size_t len);
59 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
60 struct sockaddr_un *from,
61 socklen_t fromlen)
63 struct wpa_ctrl_dst *dst;
65 dst = os_zalloc(sizeof(*dst));
66 if (dst == NULL)
67 return -1;
68 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
69 dst->addrlen = fromlen;
70 dst->debug_level = MSG_INFO;
71 dl_list_add(&priv->ctrl_dst, &dst->list);
72 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
73 (u8 *) from->sun_path,
74 fromlen - offsetof(struct sockaddr_un, sun_path));
75 return 0;
79 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
80 struct sockaddr_un *from,
81 socklen_t fromlen)
83 struct wpa_ctrl_dst *dst;
85 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
86 if (fromlen == dst->addrlen &&
87 os_memcmp(from->sun_path, dst->addr.sun_path,
88 fromlen - offsetof(struct sockaddr_un, sun_path))
89 == 0) {
90 dl_list_del(&dst->list);
91 os_free(dst);
92 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
93 (u8 *) from->sun_path,
94 fromlen -
95 offsetof(struct sockaddr_un, sun_path));
96 return 0;
99 return -1;
103 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
104 struct sockaddr_un *from,
105 socklen_t fromlen,
106 char *level)
108 struct wpa_ctrl_dst *dst;
110 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
112 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
113 if (fromlen == dst->addrlen &&
114 os_memcmp(from->sun_path, dst->addr.sun_path,
115 fromlen - offsetof(struct sockaddr_un, sun_path))
116 == 0) {
117 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
118 "level", (u8 *) from->sun_path,
119 fromlen -
120 offsetof(struct sockaddr_un, sun_path));
121 dst->debug_level = atoi(level);
122 return 0;
126 return -1;
130 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
131 void *sock_ctx)
133 struct wpa_supplicant *wpa_s = eloop_ctx;
134 struct ctrl_iface_priv *priv = sock_ctx;
135 char buf[256];
136 int res;
137 struct sockaddr_un from;
138 socklen_t fromlen = sizeof(from);
139 char *reply = NULL;
140 size_t reply_len = 0;
141 int new_attached = 0;
143 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
144 (struct sockaddr *) &from, &fromlen);
145 if (res < 0) {
146 perror("recvfrom(ctrl_iface)");
147 return;
149 buf[res] = '\0';
151 if (os_strcmp(buf, "ATTACH") == 0) {
152 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
153 reply_len = 1;
154 else {
155 new_attached = 1;
156 reply_len = 2;
158 } else if (os_strcmp(buf, "DETACH") == 0) {
159 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
160 reply_len = 1;
161 else
162 reply_len = 2;
163 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
164 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
165 buf + 6))
166 reply_len = 1;
167 else
168 reply_len = 2;
169 } else {
170 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
171 &reply_len);
174 if (reply) {
175 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
176 fromlen);
177 os_free(reply);
178 } else if (reply_len == 1) {
179 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
180 fromlen);
181 } else if (reply_len == 2) {
182 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
183 fromlen);
186 if (new_attached)
187 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
191 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
193 char *buf;
194 size_t len;
195 char *pbuf, *dir = NULL, *gid_str = NULL;
196 int res;
198 if (wpa_s->conf->ctrl_interface == NULL)
199 return NULL;
201 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
202 if (pbuf == NULL)
203 return NULL;
204 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
205 dir = pbuf + 4;
206 gid_str = os_strstr(dir, " GROUP=");
207 if (gid_str) {
208 *gid_str = '\0';
209 gid_str += 7;
211 } else
212 dir = pbuf;
214 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
215 buf = os_malloc(len);
216 if (buf == NULL) {
217 os_free(pbuf);
218 return NULL;
221 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
222 if (res < 0 || (size_t) res >= len) {
223 os_free(pbuf);
224 os_free(buf);
225 return NULL;
227 #ifdef __CYGWIN__
229 /* Windows/WinPcap uses interface names that are not suitable
230 * as a file name - convert invalid chars to underscores */
231 char *pos = buf;
232 while (*pos) {
233 if (*pos == '\\')
234 *pos = '_';
235 pos++;
238 #endif /* __CYGWIN__ */
239 os_free(pbuf);
240 return buf;
244 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
245 const char *txt, size_t len)
247 struct wpa_supplicant *wpa_s = ctx;
248 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
249 return;
250 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
254 struct ctrl_iface_priv *
255 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
257 struct ctrl_iface_priv *priv;
258 struct sockaddr_un addr;
259 char *fname = NULL;
260 gid_t gid = 0;
261 int gid_set = 0;
262 char *buf, *dir = NULL, *gid_str = NULL;
263 struct group *grp;
264 char *endp;
266 priv = os_zalloc(sizeof(*priv));
267 if (priv == NULL)
268 return NULL;
269 dl_list_init(&priv->ctrl_dst);
270 priv->wpa_s = wpa_s;
271 priv->sock = -1;
273 if (wpa_s->conf->ctrl_interface == NULL)
274 return priv;
276 buf = os_strdup(wpa_s->conf->ctrl_interface);
277 if (buf == NULL)
278 goto fail;
279 if (os_strncmp(buf, "DIR=", 4) == 0) {
280 dir = buf + 4;
281 gid_str = os_strstr(dir, " GROUP=");
282 if (gid_str) {
283 *gid_str = '\0';
284 gid_str += 7;
286 } else {
287 dir = buf;
288 gid_str = wpa_s->conf->ctrl_interface_group;
291 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
292 if (errno == EEXIST) {
293 wpa_printf(MSG_DEBUG, "Using existing control "
294 "interface directory.");
295 } else {
296 perror("mkdir[ctrl_interface]");
297 goto fail;
301 if (gid_str) {
302 grp = getgrnam(gid_str);
303 if (grp) {
304 gid = grp->gr_gid;
305 gid_set = 1;
306 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
307 " (from group name '%s')",
308 (int) gid, gid_str);
309 } else {
310 /* Group name not found - try to parse this as gid */
311 gid = strtol(gid_str, &endp, 10);
312 if (*gid_str == '\0' || *endp != '\0') {
313 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
314 "'%s'", gid_str);
315 goto fail;
317 gid_set = 1;
318 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
319 (int) gid);
323 if (gid_set && chown(dir, -1, gid) < 0) {
324 perror("chown[ctrl_interface]");
325 goto fail;
328 /* Make sure the group can enter and read the directory */
329 if (gid_set &&
330 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
331 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
332 strerror(errno));
333 goto fail;
336 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
337 sizeof(addr.sun_path)) {
338 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
339 goto fail;
342 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
343 if (priv->sock < 0) {
344 perror("socket(PF_UNIX)");
345 goto fail;
348 os_memset(&addr, 0, sizeof(addr));
349 #ifdef __FreeBSD__
350 addr.sun_len = sizeof(addr);
351 #endif /* __FreeBSD__ */
352 addr.sun_family = AF_UNIX;
353 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
354 if (fname == NULL)
355 goto fail;
356 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
357 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
358 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
359 strerror(errno));
360 if (connect(priv->sock, (struct sockaddr *) &addr,
361 sizeof(addr)) < 0) {
362 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
363 " allow connections - assuming it was left"
364 "over from forced program termination");
365 if (unlink(fname) < 0) {
366 perror("unlink[ctrl_iface]");
367 wpa_printf(MSG_ERROR, "Could not unlink "
368 "existing ctrl_iface socket '%s'",
369 fname);
370 goto fail;
372 if (bind(priv->sock, (struct sockaddr *) &addr,
373 sizeof(addr)) < 0) {
374 perror("bind(PF_UNIX)");
375 goto fail;
377 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
378 "ctrl_iface socket '%s'", fname);
379 } else {
380 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
381 "be in use - cannot override it");
382 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
383 "not used anymore", fname);
384 os_free(fname);
385 fname = NULL;
386 goto fail;
390 if (gid_set && chown(fname, -1, gid) < 0) {
391 perror("chown[ctrl_interface/ifname]");
392 goto fail;
395 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
396 perror("chmod[ctrl_interface/ifname]");
397 goto fail;
399 os_free(fname);
401 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
402 wpa_s, priv);
403 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
405 os_free(buf);
406 return priv;
408 fail:
409 if (priv->sock >= 0)
410 close(priv->sock);
411 os_free(priv);
412 if (fname) {
413 unlink(fname);
414 os_free(fname);
416 os_free(buf);
417 return NULL;
421 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
423 struct wpa_ctrl_dst *dst, *prev;
425 if (priv->sock > -1) {
426 char *fname;
427 char *buf, *dir = NULL, *gid_str = NULL;
428 eloop_unregister_read_sock(priv->sock);
429 if (!dl_list_empty(&priv->ctrl_dst)) {
431 * Wait a second before closing the control socket if
432 * there are any attached monitors in order to allow
433 * them to receive any pending messages.
435 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
436 "monitors to receive messages");
437 os_sleep(1, 0);
439 close(priv->sock);
440 priv->sock = -1;
441 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
442 if (fname) {
443 unlink(fname);
444 os_free(fname);
447 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
448 if (buf == NULL)
449 goto free_dst;
450 if (os_strncmp(buf, "DIR=", 4) == 0) {
451 dir = buf + 4;
452 gid_str = os_strstr(dir, " GROUP=");
453 if (gid_str) {
454 *gid_str = '\0';
455 gid_str += 7;
457 } else
458 dir = buf;
460 if (rmdir(dir) < 0) {
461 if (errno == ENOTEMPTY) {
462 wpa_printf(MSG_DEBUG, "Control interface "
463 "directory not empty - leaving it "
464 "behind");
465 } else {
466 perror("rmdir[ctrl_interface]");
469 os_free(buf);
472 free_dst:
473 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
474 list)
475 os_free(dst);
476 os_free(priv);
481 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
482 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
483 * @level: Priority level of the message
484 * @buf: Message data
485 * @len: Message length
487 * Send a packet to all monitor programs attached to the control interface.
489 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
490 int level, const char *buf,
491 size_t len)
493 struct wpa_ctrl_dst *dst, *next;
494 char levelstr[10];
495 int idx, res;
496 struct msghdr msg;
497 struct iovec io[2];
499 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
500 return;
502 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
503 if (res < 0 || (size_t) res >= sizeof(levelstr))
504 return;
505 io[0].iov_base = levelstr;
506 io[0].iov_len = os_strlen(levelstr);
507 io[1].iov_base = (char *) buf;
508 io[1].iov_len = len;
509 os_memset(&msg, 0, sizeof(msg));
510 msg.msg_iov = io;
511 msg.msg_iovlen = 2;
513 idx = 0;
514 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
515 list) {
516 if (level >= dst->debug_level) {
517 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
518 (u8 *) dst->addr.sun_path, dst->addrlen -
519 offsetof(struct sockaddr_un, sun_path));
520 msg.msg_name = (void *) &dst->addr;
521 msg.msg_namelen = dst->addrlen;
522 if (sendmsg(priv->sock, &msg, 0) < 0) {
523 int _errno = errno;
524 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
525 "%d - %s",
526 idx, errno, strerror(errno));
527 dst->errors++;
528 if (dst->errors > 10 || _errno == ENOENT) {
529 wpa_supplicant_ctrl_iface_detach(
530 priv, &dst->addr,
531 dst->addrlen);
533 } else
534 dst->errors = 0;
536 idx++;
541 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
543 char buf[256];
544 int res;
545 struct sockaddr_un from;
546 socklen_t fromlen = sizeof(from);
548 for (;;) {
549 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
550 "attach", priv->wpa_s->ifname);
551 eloop_wait_for_read_sock(priv->sock);
553 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
554 (struct sockaddr *) &from, &fromlen);
555 if (res < 0) {
556 perror("recvfrom(ctrl_iface)");
557 continue;
559 buf[res] = '\0';
561 if (os_strcmp(buf, "ATTACH") == 0) {
562 /* handle ATTACH signal of first monitor interface */
563 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
564 fromlen)) {
565 sendto(priv->sock, "OK\n", 3, 0,
566 (struct sockaddr *) &from, fromlen);
567 /* OK to continue */
568 return;
569 } else {
570 sendto(priv->sock, "FAIL\n", 5, 0,
571 (struct sockaddr *) &from, fromlen);
573 } else {
574 /* return FAIL for all other signals */
575 sendto(priv->sock, "FAIL\n", 5, 0,
576 (struct sockaddr *) &from, fromlen);
582 /* Global ctrl_iface */
584 struct ctrl_iface_global_priv {
585 struct wpa_global *global;
586 int sock;
590 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
591 void *sock_ctx)
593 struct wpa_global *global = eloop_ctx;
594 char buf[256];
595 int res;
596 struct sockaddr_un from;
597 socklen_t fromlen = sizeof(from);
598 char *reply;
599 size_t reply_len;
601 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
602 (struct sockaddr *) &from, &fromlen);
603 if (res < 0) {
604 perror("recvfrom(ctrl_iface)");
605 return;
607 buf[res] = '\0';
609 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
610 &reply_len);
612 if (reply) {
613 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
614 fromlen);
615 os_free(reply);
616 } else if (reply_len) {
617 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
618 fromlen);
623 struct ctrl_iface_global_priv *
624 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
626 struct ctrl_iface_global_priv *priv;
627 struct sockaddr_un addr;
629 priv = os_zalloc(sizeof(*priv));
630 if (priv == NULL)
631 return NULL;
632 priv->global = global;
633 priv->sock = -1;
635 if (global->params.ctrl_interface == NULL)
636 return priv;
638 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
639 global->params.ctrl_interface);
641 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
642 if (priv->sock < 0) {
643 perror("socket(PF_UNIX)");
644 goto fail;
647 os_memset(&addr, 0, sizeof(addr));
648 #ifdef __FreeBSD__
649 addr.sun_len = sizeof(addr);
650 #endif /* __FreeBSD__ */
651 addr.sun_family = AF_UNIX;
652 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
653 sizeof(addr.sun_path));
654 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
655 perror("bind(PF_UNIX)");
656 if (connect(priv->sock, (struct sockaddr *) &addr,
657 sizeof(addr)) < 0) {
658 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
659 " allow connections - assuming it was left"
660 "over from forced program termination");
661 if (unlink(global->params.ctrl_interface) < 0) {
662 perror("unlink[ctrl_iface]");
663 wpa_printf(MSG_ERROR, "Could not unlink "
664 "existing ctrl_iface socket '%s'",
665 global->params.ctrl_interface);
666 goto fail;
668 if (bind(priv->sock, (struct sockaddr *) &addr,
669 sizeof(addr)) < 0) {
670 perror("bind(PF_UNIX)");
671 goto fail;
673 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
674 "ctrl_iface socket '%s'",
675 global->params.ctrl_interface);
676 } else {
677 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
678 "be in use - cannot override it");
679 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
680 "not used anymore",
681 global->params.ctrl_interface);
682 goto fail;
686 eloop_register_read_sock(priv->sock,
687 wpa_supplicant_global_ctrl_iface_receive,
688 global, NULL);
690 return priv;
692 fail:
693 if (priv->sock >= 0)
694 close(priv->sock);
695 os_free(priv);
696 return NULL;
700 void
701 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
703 if (priv->sock >= 0) {
704 eloop_unregister_read_sock(priv->sock);
705 close(priv->sock);
707 if (priv->global->params.ctrl_interface)
708 unlink(priv->global->params.ctrl_interface);
709 os_free(priv);