]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ctrl_iface_unix.c
Fix MCS set field to be based on driver info
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface_unix.c
1 /*
2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16 #include <sys/un.h>
17 #include <sys/stat.h>
18 #include <grp.h>
19 #include <stddef.h>
20
21 #include "common.h"
22 #include "eloop.h"
23 #include "config.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "wpa_supplicant_i.h"
26 #include "ctrl_iface.h"
27
28 /* Per-interface ctrl_iface */
29
30 /**
31 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
32 *
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.
36 */
37 struct wpa_ctrl_dst {
38 struct wpa_ctrl_dst *next;
39 struct sockaddr_un addr;
40 socklen_t addrlen;
41 int debug_level;
42 int errors;
43 };
44
45
46 struct ctrl_iface_priv {
47 struct wpa_supplicant *wpa_s;
48 int sock;
49 struct wpa_ctrl_dst *ctrl_dst;
50 };
51
52
53 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
54 int level, const char *buf,
55 size_t len);
56
57
58 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
59 struct sockaddr_un *from,
60 socklen_t fromlen)
61 {
62 struct wpa_ctrl_dst *dst;
63
64 dst = os_zalloc(sizeof(*dst));
65 if (dst == NULL)
66 return -1;
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;
71 priv->ctrl_dst = dst;
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;
76 }
77
78
79 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
80 struct sockaddr_un *from,
81 socklen_t fromlen)
82 {
83 struct wpa_ctrl_dst *dst, *prev = NULL;
84
85 dst = priv->ctrl_dst;
86 while (dst) {
87 if (fromlen == dst->addrlen &&
88 os_memcmp(from->sun_path, dst->addr.sun_path,
89 fromlen - offsetof(struct sockaddr_un, sun_path))
90 == 0) {
91 if (prev == NULL)
92 priv->ctrl_dst = dst->next;
93 else
94 prev->next = dst->next;
95 os_free(dst);
96 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
97 (u8 *) from->sun_path,
98 fromlen -
99 offsetof(struct sockaddr_un, sun_path));
100 return 0;
101 }
102 prev = dst;
103 dst = dst->next;
104 }
105 return -1;
106 }
107
108
109 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
110 struct sockaddr_un *from,
111 socklen_t fromlen,
112 char *level)
113 {
114 struct wpa_ctrl_dst *dst;
115
116 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
117
118 dst = priv->ctrl_dst;
119 while (dst) {
120 if (fromlen == dst->addrlen &&
121 os_memcmp(from->sun_path, dst->addr.sun_path,
122 fromlen - offsetof(struct sockaddr_un, sun_path))
123 == 0) {
124 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
125 "level", (u8 *) from->sun_path,
126 fromlen -
127 offsetof(struct sockaddr_un, sun_path));
128 dst->debug_level = atoi(level);
129 return 0;
130 }
131 dst = dst->next;
132 }
133
134 return -1;
135 }
136
137
138 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
139 void *sock_ctx)
140 {
141 struct wpa_supplicant *wpa_s = eloop_ctx;
142 struct ctrl_iface_priv *priv = sock_ctx;
143 char buf[256];
144 int res;
145 struct sockaddr_un from;
146 socklen_t fromlen = sizeof(from);
147 char *reply = NULL;
148 size_t reply_len = 0;
149 int new_attached = 0;
150
151 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
152 (struct sockaddr *) &from, &fromlen);
153 if (res < 0) {
154 perror("recvfrom(ctrl_iface)");
155 return;
156 }
157 buf[res] = '\0';
158
159 if (os_strcmp(buf, "ATTACH") == 0) {
160 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
161 reply_len = 1;
162 else {
163 new_attached = 1;
164 reply_len = 2;
165 }
166 } else if (os_strcmp(buf, "DETACH") == 0) {
167 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
168 reply_len = 1;
169 else
170 reply_len = 2;
171 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
172 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
173 buf + 6))
174 reply_len = 1;
175 else
176 reply_len = 2;
177 } else {
178 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
179 &reply_len);
180 }
181
182 if (reply) {
183 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
184 fromlen);
185 os_free(reply);
186 } else if (reply_len == 1) {
187 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
188 fromlen);
189 } else if (reply_len == 2) {
190 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
191 fromlen);
192 }
193
194 if (new_attached)
195 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
196 }
197
198
199 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
200 {
201 char *buf;
202 size_t len;
203 char *pbuf, *dir = NULL, *gid_str = NULL;
204 int res;
205
206 if (wpa_s->conf->ctrl_interface == NULL)
207 return NULL;
208
209 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
210 if (pbuf == NULL)
211 return NULL;
212 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
213 dir = pbuf + 4;
214 gid_str = os_strstr(dir, " GROUP=");
215 if (gid_str) {
216 *gid_str = '\0';
217 gid_str += 7;
218 }
219 } else
220 dir = pbuf;
221
222 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
223 buf = os_malloc(len);
224 if (buf == NULL) {
225 os_free(pbuf);
226 return NULL;
227 }
228
229 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
230 if (res < 0 || (size_t) res >= len) {
231 os_free(pbuf);
232 os_free(buf);
233 return NULL;
234 }
235 #ifdef __CYGWIN__
236 {
237 /* Windows/WinPcap uses interface names that are not suitable
238 * as a file name - convert invalid chars to underscores */
239 char *pos = buf;
240 while (*pos) {
241 if (*pos == '\\')
242 *pos = '_';
243 pos++;
244 }
245 }
246 #endif /* __CYGWIN__ */
247 os_free(pbuf);
248 return buf;
249 }
250
251
252 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
253 const char *txt, size_t len)
254 {
255 struct wpa_supplicant *wpa_s = ctx;
256 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
257 return;
258 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
259 }
260
261
262 struct ctrl_iface_priv *
263 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
264 {
265 struct ctrl_iface_priv *priv;
266 struct sockaddr_un addr;
267 char *fname = NULL;
268 gid_t gid = 0;
269 int gid_set = 0;
270 char *buf, *dir = NULL, *gid_str = NULL;
271 struct group *grp;
272 char *endp;
273
274 priv = os_zalloc(sizeof(*priv));
275 if (priv == NULL)
276 return NULL;
277 priv->wpa_s = wpa_s;
278 priv->sock = -1;
279
280 if (wpa_s->conf->ctrl_interface == NULL)
281 return priv;
282
283 buf = os_strdup(wpa_s->conf->ctrl_interface);
284 if (buf == NULL)
285 goto fail;
286 if (os_strncmp(buf, "DIR=", 4) == 0) {
287 dir = buf + 4;
288 gid_str = os_strstr(dir, " GROUP=");
289 if (gid_str) {
290 *gid_str = '\0';
291 gid_str += 7;
292 }
293 } else {
294 dir = buf;
295 gid_str = wpa_s->conf->ctrl_interface_group;
296 }
297
298 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
299 if (errno == EEXIST) {
300 wpa_printf(MSG_DEBUG, "Using existing control "
301 "interface directory.");
302 } else {
303 perror("mkdir[ctrl_interface]");
304 goto fail;
305 }
306 }
307
308 if (gid_str) {
309 grp = getgrnam(gid_str);
310 if (grp) {
311 gid = grp->gr_gid;
312 gid_set = 1;
313 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
314 " (from group name '%s')",
315 (int) gid, gid_str);
316 } else {
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 "
321 "'%s'", gid_str);
322 goto fail;
323 }
324 gid_set = 1;
325 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
326 (int) gid);
327 }
328 }
329
330 if (gid_set && chown(dir, -1, gid) < 0) {
331 perror("chown[ctrl_interface]");
332 goto fail;
333 }
334
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");
338 goto fail;
339 }
340
341 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
342 if (priv->sock < 0) {
343 perror("socket(PF_UNIX)");
344 goto fail;
345 }
346
347 os_memset(&addr, 0, sizeof(addr));
348 #ifdef __FreeBSD__
349 addr.sun_len = sizeof(addr);
350 #endif /* __FreeBSD__ */
351 addr.sun_family = AF_UNIX;
352 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
353 if (fname == NULL)
354 goto fail;
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",
358 strerror(errno));
359 if (connect(priv->sock, (struct sockaddr *) &addr,
360 sizeof(addr)) < 0) {
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'",
368 fname);
369 goto fail;
370 }
371 if (bind(priv->sock, (struct sockaddr *) &addr,
372 sizeof(addr)) < 0) {
373 perror("bind(PF_UNIX)");
374 goto fail;
375 }
376 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
377 "ctrl_iface socket '%s'", fname);
378 } else {
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);
383 os_free(fname);
384 fname = NULL;
385 goto fail;
386 }
387 }
388
389 if (gid_set && chown(fname, -1, gid) < 0) {
390 perror("chown[ctrl_interface/ifname]");
391 goto fail;
392 }
393
394 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
395 perror("chmod[ctrl_interface/ifname]");
396 goto fail;
397 }
398 os_free(fname);
399
400 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
401 wpa_s, priv);
402 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
403
404 os_free(buf);
405 return priv;
406
407 fail:
408 if (priv->sock >= 0)
409 close(priv->sock);
410 os_free(priv);
411 if (fname) {
412 unlink(fname);
413 os_free(fname);
414 }
415 os_free(buf);
416 return NULL;
417 }
418
419
420 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
421 {
422 struct wpa_ctrl_dst *dst, *prev;
423
424 if (priv->sock > -1) {
425 char *fname;
426 char *buf, *dir = NULL, *gid_str = NULL;
427 eloop_unregister_read_sock(priv->sock);
428 if (priv->ctrl_dst) {
429 /*
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.
433 */
434 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
435 "monitors to receive messages");
436 os_sleep(1, 0);
437 }
438 close(priv->sock);
439 priv->sock = -1;
440 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
441 if (fname) {
442 unlink(fname);
443 os_free(fname);
444 }
445
446 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
447 if (buf == NULL)
448 goto free_dst;
449 if (os_strncmp(buf, "DIR=", 4) == 0) {
450 dir = buf + 4;
451 gid_str = os_strstr(dir, " GROUP=");
452 if (gid_str) {
453 *gid_str = '\0';
454 gid_str += 7;
455 }
456 } else
457 dir = buf;
458
459 if (rmdir(dir) < 0) {
460 if (errno == ENOTEMPTY) {
461 wpa_printf(MSG_DEBUG, "Control interface "
462 "directory not empty - leaving it "
463 "behind");
464 } else {
465 perror("rmdir[ctrl_interface]");
466 }
467 }
468 os_free(buf);
469 }
470
471 free_dst:
472 dst = priv->ctrl_dst;
473 while (dst) {
474 prev = dst;
475 dst = dst->next;
476 os_free(prev);
477 }
478 os_free(priv);
479 }
480
481
482 /**
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
486 * @buf: Message data
487 * @len: Message length
488 *
489 * Send a packet to all monitor programs attached to the control interface.
490 */
491 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
492 int level, const char *buf,
493 size_t len)
494 {
495 struct wpa_ctrl_dst *dst, *next;
496 char levelstr[10];
497 int idx, res;
498 struct msghdr msg;
499 struct iovec io[2];
500
501 dst = priv->ctrl_dst;
502 if (priv->sock < 0 || dst == NULL)
503 return;
504
505 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
506 if (res < 0 || (size_t) res >= sizeof(levelstr))
507 return;
508 io[0].iov_base = levelstr;
509 io[0].iov_len = os_strlen(levelstr);
510 io[1].iov_base = (char *) buf;
511 io[1].iov_len = len;
512 os_memset(&msg, 0, sizeof(msg));
513 msg.msg_iov = io;
514 msg.msg_iovlen = 2;
515
516 idx = 0;
517 while (dst) {
518 next = dst->next;
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) {
526 int _errno = errno;
527 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
528 "%d - %s",
529 idx, errno, strerror(errno));
530 dst->errors++;
531 if (dst->errors > 10 || _errno == ENOENT) {
532 wpa_supplicant_ctrl_iface_detach(
533 priv, &dst->addr,
534 dst->addrlen);
535 }
536 } else
537 dst->errors = 0;
538 }
539 idx++;
540 dst = next;
541 }
542 }
543
544
545 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
546 {
547 char buf[256];
548 int res;
549 struct sockaddr_un from;
550 socklen_t fromlen = sizeof(from);
551
552 for (;;) {
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);
556
557 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
558 (struct sockaddr *) &from, &fromlen);
559 if (res < 0) {
560 perror("recvfrom(ctrl_iface)");
561 continue;
562 }
563 buf[res] = '\0';
564
565 if (os_strcmp(buf, "ATTACH") == 0) {
566 /* handle ATTACH signal of first monitor interface */
567 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
568 fromlen)) {
569 sendto(priv->sock, "OK\n", 3, 0,
570 (struct sockaddr *) &from, fromlen);
571 /* OK to continue */
572 return;
573 } else {
574 sendto(priv->sock, "FAIL\n", 5, 0,
575 (struct sockaddr *) &from, fromlen);
576 }
577 } else {
578 /* return FAIL for all other signals */
579 sendto(priv->sock, "FAIL\n", 5, 0,
580 (struct sockaddr *) &from, fromlen);
581 }
582 }
583 }
584
585
586 /* Global ctrl_iface */
587
588 struct ctrl_iface_global_priv {
589 struct wpa_global *global;
590 int sock;
591 };
592
593
594 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
595 void *sock_ctx)
596 {
597 struct wpa_global *global = eloop_ctx;
598 char buf[256];
599 int res;
600 struct sockaddr_un from;
601 socklen_t fromlen = sizeof(from);
602 char *reply;
603 size_t reply_len;
604
605 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
606 (struct sockaddr *) &from, &fromlen);
607 if (res < 0) {
608 perror("recvfrom(ctrl_iface)");
609 return;
610 }
611 buf[res] = '\0';
612
613 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
614 &reply_len);
615
616 if (reply) {
617 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
618 fromlen);
619 os_free(reply);
620 } else if (reply_len) {
621 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
622 fromlen);
623 }
624 }
625
626
627 struct ctrl_iface_global_priv *
628 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
629 {
630 struct ctrl_iface_global_priv *priv;
631 struct sockaddr_un addr;
632
633 priv = os_zalloc(sizeof(*priv));
634 if (priv == NULL)
635 return NULL;
636 priv->global = global;
637 priv->sock = -1;
638
639 if (global->params.ctrl_interface == NULL)
640 return priv;
641
642 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
643 global->params.ctrl_interface);
644
645 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
646 if (priv->sock < 0) {
647 perror("socket(PF_UNIX)");
648 goto fail;
649 }
650
651 os_memset(&addr, 0, sizeof(addr));
652 #ifdef __FreeBSD__
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,
661 sizeof(addr)) < 0) {
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);
670 goto fail;
671 }
672 if (bind(priv->sock, (struct sockaddr *) &addr,
673 sizeof(addr)) < 0) {
674 perror("bind(PF_UNIX)");
675 goto fail;
676 }
677 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
678 "ctrl_iface socket '%s'",
679 global->params.ctrl_interface);
680 } else {
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 "
684 "not used anymore",
685 global->params.ctrl_interface);
686 goto fail;
687 }
688 }
689
690 eloop_register_read_sock(priv->sock,
691 wpa_supplicant_global_ctrl_iface_receive,
692 global, NULL);
693
694 return priv;
695
696 fail:
697 if (priv->sock >= 0)
698 close(priv->sock);
699 os_free(priv);
700 return NULL;
701 }
702
703
704 void
705 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
706 {
707 if (priv->sock >= 0) {
708 eloop_unregister_read_sock(priv->sock);
709 close(priv->sock);
710 }
711 if (priv->global->params.ctrl_interface)
712 unlink(priv->global->params.ctrl_interface);
713 os_free(priv);
714 }