]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wpa_priv.c
Do not start new radio work on scan completion during ext work
[thirdparty/hostap.git] / wpa_supplicant / wpa_priv.c
1 /*
2 * WPA Supplicant / privileged helper program
3 * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10 #ifdef __linux__
11 #include <fcntl.h>
12 #endif /* __linux__ */
13 #include <sys/un.h>
14 #include <sys/stat.h>
15
16 #include "common.h"
17 #include "eloop.h"
18 #include "common/version.h"
19 #include "drivers/driver.h"
20 #include "l2_packet/l2_packet.h"
21 #include "common/privsep_commands.h"
22 #include "common/ieee802_11_defs.h"
23
24
25 struct wpa_priv_interface {
26 struct wpa_priv_interface *next;
27 char *driver_name;
28 char *ifname;
29 char *sock_name;
30 int fd;
31
32 struct wpa_driver_ops *driver;
33 void *drv_priv;
34 struct sockaddr_un drv_addr;
35 int wpas_registered;
36
37 /* TODO: add support for multiple l2 connections */
38 struct l2_packet_data *l2;
39 struct sockaddr_un l2_addr;
40 };
41
42
43 static void wpa_priv_cmd_register(struct wpa_priv_interface *iface,
44 struct sockaddr_un *from)
45 {
46 if (iface->drv_priv) {
47 wpa_printf(MSG_DEBUG, "Cleaning up forgotten driver instance");
48 if (iface->driver->deinit)
49 iface->driver->deinit(iface->drv_priv);
50 iface->drv_priv = NULL;
51 iface->wpas_registered = 0;
52 }
53
54 if (iface->l2) {
55 wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
56 "instance");
57 l2_packet_deinit(iface->l2);
58 iface->l2 = NULL;
59 }
60
61 if (iface->driver->init == NULL)
62 return;
63
64 iface->drv_priv = iface->driver->init(iface, iface->ifname);
65 if (iface->drv_priv == NULL) {
66 wpa_printf(MSG_DEBUG, "Failed to initialize driver wrapper");
67 return;
68 }
69
70 wpa_printf(MSG_DEBUG, "Driver wrapper '%s' initialized for interface "
71 "'%s'", iface->driver_name, iface->ifname);
72
73 os_memcpy(&iface->drv_addr, from, sizeof(iface->drv_addr));
74 iface->wpas_registered = 1;
75
76 if (iface->driver->set_param &&
77 iface->driver->set_param(iface->drv_priv, NULL) < 0) {
78 wpa_printf(MSG_ERROR, "Driver interface rejected param");
79 }
80 }
81
82
83 static void wpa_priv_cmd_unregister(struct wpa_priv_interface *iface,
84 struct sockaddr_un *from)
85 {
86 if (iface->drv_priv) {
87 if (iface->driver->deinit)
88 iface->driver->deinit(iface->drv_priv);
89 iface->drv_priv = NULL;
90 iface->wpas_registered = 0;
91 }
92 }
93
94
95 static void wpa_priv_cmd_scan(struct wpa_priv_interface *iface,
96 char *buf, size_t len)
97 {
98 struct wpa_driver_scan_params params;
99
100 if (iface->drv_priv == NULL)
101 return;
102
103 os_memset(&params, 0, sizeof(params));
104 if (len) {
105 params.ssids[0].ssid = (u8 *) buf;
106 params.ssids[0].ssid_len = len;
107 params.num_ssids = 1;
108 }
109
110 if (iface->driver->scan2)
111 iface->driver->scan2(iface->drv_priv, &params);
112 }
113
114
115 static void wpa_priv_get_scan_results2(struct wpa_priv_interface *iface,
116 struct sockaddr_un *from)
117 {
118 struct wpa_scan_results *res;
119 u8 *buf = NULL, *pos, *end;
120 int val;
121 size_t i;
122
123 res = iface->driver->get_scan_results2(iface->drv_priv);
124 if (res == NULL)
125 goto fail;
126
127 buf = os_malloc(60000);
128 if (buf == NULL)
129 goto fail;
130 pos = buf;
131 end = buf + 60000;
132 val = res->num;
133 os_memcpy(pos, &val, sizeof(int));
134 pos += sizeof(int);
135
136 for (i = 0; i < res->num; i++) {
137 struct wpa_scan_res *r = res->res[i];
138 val = sizeof(*r) + r->ie_len;
139 if (end - pos < (int) sizeof(int) + val)
140 break;
141 os_memcpy(pos, &val, sizeof(int));
142 pos += sizeof(int);
143 os_memcpy(pos, r, val);
144 pos += val;
145 }
146
147 sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from,
148 sizeof(*from));
149
150 os_free(buf);
151 wpa_scan_results_free(res);
152 return;
153
154 fail:
155 os_free(buf);
156 wpa_scan_results_free(res);
157 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
158 }
159
160
161 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface *iface,
162 struct sockaddr_un *from)
163 {
164 if (iface->drv_priv == NULL)
165 return;
166
167 if (iface->driver->get_scan_results2)
168 wpa_priv_get_scan_results2(iface, from);
169 else
170 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from,
171 sizeof(*from));
172 }
173
174
175 static void wpa_priv_cmd_associate(struct wpa_priv_interface *iface,
176 void *buf, size_t len)
177 {
178 struct wpa_driver_associate_params params;
179 struct privsep_cmd_associate *assoc;
180 u8 *bssid;
181 int res;
182
183 if (iface->drv_priv == NULL || iface->driver->associate == NULL)
184 return;
185
186 if (len < sizeof(*assoc)) {
187 wpa_printf(MSG_DEBUG, "Invalid association request");
188 return;
189 }
190
191 assoc = buf;
192 if (sizeof(*assoc) + assoc->wpa_ie_len > len) {
193 wpa_printf(MSG_DEBUG, "Association request overflow");
194 return;
195 }
196
197 os_memset(&params, 0, sizeof(params));
198 bssid = assoc->bssid;
199 if (bssid[0] | bssid[1] | bssid[2] | bssid[3] | bssid[4] | bssid[5])
200 params.bssid = bssid;
201 params.ssid = assoc->ssid;
202 if (assoc->ssid_len > 32)
203 return;
204 params.ssid_len = assoc->ssid_len;
205 params.freq = assoc->freq;
206 if (assoc->wpa_ie_len) {
207 params.wpa_ie = (u8 *) (assoc + 1);
208 params.wpa_ie_len = assoc->wpa_ie_len;
209 }
210 params.pairwise_suite = assoc->pairwise_suite;
211 params.group_suite = assoc->group_suite;
212 params.key_mgmt_suite = assoc->key_mgmt_suite;
213 params.auth_alg = assoc->auth_alg;
214 params.mode = assoc->mode;
215
216 res = iface->driver->associate(iface->drv_priv, &params);
217 wpa_printf(MSG_DEBUG, "drv->associate: res=%d", res);
218 }
219
220
221 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface *iface,
222 struct sockaddr_un *from)
223 {
224 u8 bssid[ETH_ALEN];
225
226 if (iface->drv_priv == NULL)
227 goto fail;
228
229 if (iface->driver->get_bssid == NULL ||
230 iface->driver->get_bssid(iface->drv_priv, bssid) < 0)
231 goto fail;
232
233 sendto(iface->fd, bssid, ETH_ALEN, 0, (struct sockaddr *) from,
234 sizeof(*from));
235 return;
236
237 fail:
238 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
239 }
240
241
242 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface *iface,
243 struct sockaddr_un *from)
244 {
245 u8 ssid[sizeof(int) + 32];
246 int res;
247
248 if (iface->drv_priv == NULL)
249 goto fail;
250
251 if (iface->driver->get_ssid == NULL)
252 goto fail;
253
254 res = iface->driver->get_ssid(iface->drv_priv, &ssid[sizeof(int)]);
255 if (res < 0 || res > 32)
256 goto fail;
257 os_memcpy(ssid, &res, sizeof(int));
258
259 sendto(iface->fd, ssid, sizeof(ssid), 0, (struct sockaddr *) from,
260 sizeof(*from));
261 return;
262
263 fail:
264 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
265 }
266
267
268 static void wpa_priv_cmd_set_key(struct wpa_priv_interface *iface,
269 void *buf, size_t len)
270 {
271 struct privsep_cmd_set_key *params;
272 int res;
273
274 if (iface->drv_priv == NULL || iface->driver->set_key == NULL)
275 return;
276
277 if (len != sizeof(*params)) {
278 wpa_printf(MSG_DEBUG, "Invalid set_key request");
279 return;
280 }
281
282 params = buf;
283
284 res = iface->driver->set_key(iface->ifname, iface->drv_priv,
285 params->alg,
286 params->addr, params->key_idx,
287 params->set_tx,
288 params->seq_len ? params->seq : NULL,
289 params->seq_len,
290 params->key_len ? params->key : NULL,
291 params->key_len);
292 wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res);
293 }
294
295
296 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface,
297 struct sockaddr_un *from)
298 {
299 struct wpa_driver_capa capa;
300
301 if (iface->drv_priv == NULL)
302 goto fail;
303
304 if (iface->driver->get_capa == NULL ||
305 iface->driver->get_capa(iface->drv_priv, &capa) < 0)
306 goto fail;
307
308 sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from,
309 sizeof(*from));
310 return;
311
312 fail:
313 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
314 }
315
316
317 static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf,
318 size_t len)
319 {
320 struct wpa_priv_interface *iface = ctx;
321 struct msghdr msg;
322 struct iovec io[2];
323
324 io[0].iov_base = (u8 *) src_addr;
325 io[0].iov_len = ETH_ALEN;
326 io[1].iov_base = (u8 *) buf;
327 io[1].iov_len = len;
328
329 os_memset(&msg, 0, sizeof(msg));
330 msg.msg_iov = io;
331 msg.msg_iovlen = 2;
332 msg.msg_name = &iface->l2_addr;
333 msg.msg_namelen = sizeof(iface->l2_addr);
334
335 if (sendmsg(iface->fd, &msg, 0) < 0) {
336 perror("sendmsg(l2 rx)");
337 }
338 }
339
340
341 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface,
342 struct sockaddr_un *from,
343 void *buf, size_t len)
344 {
345 int *reg_cmd = buf;
346 u8 own_addr[ETH_ALEN];
347 int res;
348 u16 proto;
349
350 if (len != 2 * sizeof(int)) {
351 wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu",
352 (unsigned long) len);
353 return;
354 }
355
356 proto = reg_cmd[0];
357 if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
358 wpa_printf(MSG_DEBUG, "Refused l2_packet connection for "
359 "ethertype 0x%x", proto);
360 return;
361 }
362
363 if (iface->l2) {
364 wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
365 "instance");
366 l2_packet_deinit(iface->l2);
367 iface->l2 = NULL;
368 }
369
370 os_memcpy(&iface->l2_addr, from, sizeof(iface->l2_addr));
371
372 iface->l2 = l2_packet_init(iface->ifname, NULL, proto,
373 wpa_priv_l2_rx, iface, reg_cmd[1]);
374 if (iface->l2 == NULL) {
375 wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet "
376 "instance for protocol %d", proto);
377 return;
378 }
379
380 if (l2_packet_get_own_addr(iface->l2, own_addr) < 0) {
381 wpa_printf(MSG_DEBUG, "Failed to get own address from "
382 "l2_packet");
383 l2_packet_deinit(iface->l2);
384 iface->l2 = NULL;
385 return;
386 }
387
388 res = sendto(iface->fd, own_addr, ETH_ALEN, 0,
389 (struct sockaddr *) from, sizeof(*from));
390 wpa_printf(MSG_DEBUG, "L2 registration: res=%d", res);
391 }
392
393
394 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface,
395 struct sockaddr_un *from)
396 {
397 if (iface->l2) {
398 l2_packet_deinit(iface->l2);
399 iface->l2 = NULL;
400 }
401 }
402
403
404 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface,
405 struct sockaddr_un *from)
406 {
407 if (iface->l2)
408 l2_packet_notify_auth_start(iface->l2);
409 }
410
411
412 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
413 struct sockaddr_un *from,
414 void *buf, size_t len)
415 {
416 u8 *dst_addr;
417 u16 proto;
418 int res;
419
420 if (iface->l2 == NULL)
421 return;
422
423 if (len < ETH_ALEN + 2) {
424 wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)",
425 (unsigned long) len);
426 return;
427 }
428
429 dst_addr = buf;
430 os_memcpy(&proto, buf + ETH_ALEN, 2);
431
432 if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
433 wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
434 "0x%x", proto);
435 return;
436 }
437
438 res = l2_packet_send(iface->l2, dst_addr, proto, buf + ETH_ALEN + 2,
439 len - ETH_ALEN - 2);
440 wpa_printf(MSG_DEBUG, "L2 send: res=%d", res);
441 }
442
443
444 static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface,
445 char *buf)
446 {
447 if (iface->drv_priv == NULL || iface->driver->set_country == NULL ||
448 *buf == '\0')
449 return;
450
451 iface->driver->set_country(iface->drv_priv, buf);
452 }
453
454
455 static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx)
456 {
457 struct wpa_priv_interface *iface = eloop_ctx;
458 char buf[2000], *pos;
459 void *cmd_buf;
460 size_t cmd_len;
461 int res, cmd;
462 struct sockaddr_un from;
463 socklen_t fromlen = sizeof(from);
464
465 res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
466 &fromlen);
467 if (res < 0) {
468 perror("recvfrom");
469 return;
470 }
471
472 if (res < (int) sizeof(int)) {
473 wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res);
474 return;
475 }
476
477 os_memcpy(&cmd, buf, sizeof(int));
478 wpa_printf(MSG_DEBUG, "Command %d for interface %s",
479 cmd, iface->ifname);
480 cmd_buf = &buf[sizeof(int)];
481 cmd_len = res - sizeof(int);
482
483 switch (cmd) {
484 case PRIVSEP_CMD_REGISTER:
485 wpa_priv_cmd_register(iface, &from);
486 break;
487 case PRIVSEP_CMD_UNREGISTER:
488 wpa_priv_cmd_unregister(iface, &from);
489 break;
490 case PRIVSEP_CMD_SCAN:
491 wpa_priv_cmd_scan(iface, cmd_buf, cmd_len);
492 break;
493 case PRIVSEP_CMD_GET_SCAN_RESULTS:
494 wpa_priv_cmd_get_scan_results(iface, &from);
495 break;
496 case PRIVSEP_CMD_ASSOCIATE:
497 wpa_priv_cmd_associate(iface, cmd_buf, cmd_len);
498 break;
499 case PRIVSEP_CMD_GET_BSSID:
500 wpa_priv_cmd_get_bssid(iface, &from);
501 break;
502 case PRIVSEP_CMD_GET_SSID:
503 wpa_priv_cmd_get_ssid(iface, &from);
504 break;
505 case PRIVSEP_CMD_SET_KEY:
506 wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len);
507 break;
508 case PRIVSEP_CMD_GET_CAPA:
509 wpa_priv_cmd_get_capa(iface, &from);
510 break;
511 case PRIVSEP_CMD_L2_REGISTER:
512 wpa_priv_cmd_l2_register(iface, &from, cmd_buf, cmd_len);
513 break;
514 case PRIVSEP_CMD_L2_UNREGISTER:
515 wpa_priv_cmd_l2_unregister(iface, &from);
516 break;
517 case PRIVSEP_CMD_L2_NOTIFY_AUTH_START:
518 wpa_priv_cmd_l2_notify_auth_start(iface, &from);
519 break;
520 case PRIVSEP_CMD_L2_SEND:
521 wpa_priv_cmd_l2_send(iface, &from, cmd_buf, cmd_len);
522 break;
523 case PRIVSEP_CMD_SET_COUNTRY:
524 pos = cmd_buf;
525 if (pos + cmd_len >= buf + sizeof(buf))
526 break;
527 pos[cmd_len] = '\0';
528 wpa_priv_cmd_set_country(iface, pos);
529 break;
530 }
531 }
532
533
534 static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface)
535 {
536 if (iface->drv_priv && iface->driver->deinit)
537 iface->driver->deinit(iface->drv_priv);
538
539 if (iface->fd >= 0) {
540 eloop_unregister_read_sock(iface->fd);
541 close(iface->fd);
542 unlink(iface->sock_name);
543 }
544
545 if (iface->l2)
546 l2_packet_deinit(iface->l2);
547
548 os_free(iface->ifname);
549 os_free(iface->driver_name);
550 os_free(iface->sock_name);
551 os_free(iface);
552 }
553
554
555 static struct wpa_priv_interface *
556 wpa_priv_interface_init(const char *dir, const char *params)
557 {
558 struct wpa_priv_interface *iface;
559 char *pos;
560 size_t len;
561 struct sockaddr_un addr;
562 int i;
563
564 pos = os_strchr(params, ':');
565 if (pos == NULL)
566 return NULL;
567
568 iface = os_zalloc(sizeof(*iface));
569 if (iface == NULL)
570 return NULL;
571 iface->fd = -1;
572
573 len = pos - params;
574 iface->driver_name = dup_binstr(params, len);
575 if (iface->driver_name == NULL) {
576 wpa_priv_interface_deinit(iface);
577 return NULL;
578 }
579
580 for (i = 0; wpa_drivers[i]; i++) {
581 if (os_strcmp(iface->driver_name,
582 wpa_drivers[i]->name) == 0) {
583 iface->driver = wpa_drivers[i];
584 break;
585 }
586 }
587 if (iface->driver == NULL) {
588 wpa_printf(MSG_ERROR, "Unsupported driver '%s'",
589 iface->driver_name);
590 wpa_priv_interface_deinit(iface);
591 return NULL;
592 }
593
594 pos++;
595 iface->ifname = os_strdup(pos);
596 if (iface->ifname == NULL) {
597 wpa_priv_interface_deinit(iface);
598 return NULL;
599 }
600
601 len = os_strlen(dir) + 1 + os_strlen(iface->ifname);
602 iface->sock_name = os_malloc(len + 1);
603 if (iface->sock_name == NULL) {
604 wpa_priv_interface_deinit(iface);
605 return NULL;
606 }
607
608 os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname);
609 if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) {
610 wpa_priv_interface_deinit(iface);
611 return NULL;
612 }
613
614 iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
615 if (iface->fd < 0) {
616 perror("socket(PF_UNIX)");
617 wpa_priv_interface_deinit(iface);
618 return NULL;
619 }
620
621 os_memset(&addr, 0, sizeof(addr));
622 addr.sun_family = AF_UNIX;
623 os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path));
624
625 if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
626 wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s",
627 strerror(errno));
628 if (connect(iface->fd, (struct sockaddr *) &addr,
629 sizeof(addr)) < 0) {
630 wpa_printf(MSG_DEBUG, "Socket exists, but does not "
631 "allow connections - assuming it was "
632 "leftover from forced program termination");
633 if (unlink(iface->sock_name) < 0) {
634 perror("unlink[ctrl_iface]");
635 wpa_printf(MSG_ERROR, "Could not unlink "
636 "existing ctrl_iface socket '%s'",
637 iface->sock_name);
638 goto fail;
639 }
640 if (bind(iface->fd, (struct sockaddr *) &addr,
641 sizeof(addr)) < 0) {
642 perror("wpa-priv-iface-init: bind(PF_UNIX)");
643 goto fail;
644 }
645 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
646 "socket '%s'", iface->sock_name);
647 } else {
648 wpa_printf(MSG_INFO, "Socket exists and seems to be "
649 "in use - cannot override it");
650 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
651 "not used anymore", iface->sock_name);
652 goto fail;
653 }
654 }
655
656 if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
657 perror("chmod");
658 goto fail;
659 }
660
661 eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL);
662
663 return iface;
664
665 fail:
666 wpa_priv_interface_deinit(iface);
667 return NULL;
668 }
669
670
671 static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event,
672 const void *data, size_t data_len)
673 {
674 struct msghdr msg;
675 struct iovec io[2];
676
677 io[0].iov_base = &event;
678 io[0].iov_len = sizeof(event);
679 io[1].iov_base = (u8 *) data;
680 io[1].iov_len = data_len;
681
682 os_memset(&msg, 0, sizeof(msg));
683 msg.msg_iov = io;
684 msg.msg_iovlen = data ? 2 : 1;
685 msg.msg_name = &iface->drv_addr;
686 msg.msg_namelen = sizeof(iface->drv_addr);
687
688 if (sendmsg(iface->fd, &msg, 0) < 0) {
689 perror("sendmsg(wpas_socket)");
690 return -1;
691 }
692
693 return 0;
694 }
695
696
697 static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event,
698 union wpa_event_data *data)
699 {
700 size_t buflen = 3 * sizeof(int);
701 u8 *buf, *pos;
702 int len;
703
704 if (data) {
705 buflen += data->assoc_info.req_ies_len +
706 data->assoc_info.resp_ies_len +
707 data->assoc_info.beacon_ies_len;
708 }
709
710 buf = os_malloc(buflen);
711 if (buf == NULL)
712 return;
713
714 pos = buf;
715
716 if (data && data->assoc_info.req_ies) {
717 len = data->assoc_info.req_ies_len;
718 os_memcpy(pos, &len, sizeof(int));
719 pos += sizeof(int);
720 os_memcpy(pos, data->assoc_info.req_ies, len);
721 pos += len;
722 } else {
723 len = 0;
724 os_memcpy(pos, &len, sizeof(int));
725 pos += sizeof(int);
726 }
727
728 if (data && data->assoc_info.resp_ies) {
729 len = data->assoc_info.resp_ies_len;
730 os_memcpy(pos, &len, sizeof(int));
731 pos += sizeof(int);
732 os_memcpy(pos, data->assoc_info.resp_ies, len);
733 pos += len;
734 } else {
735 len = 0;
736 os_memcpy(pos, &len, sizeof(int));
737 pos += sizeof(int);
738 }
739
740 if (data && data->assoc_info.beacon_ies) {
741 len = data->assoc_info.beacon_ies_len;
742 os_memcpy(pos, &len, sizeof(int));
743 pos += sizeof(int);
744 os_memcpy(pos, data->assoc_info.beacon_ies, len);
745 pos += len;
746 } else {
747 len = 0;
748 os_memcpy(pos, &len, sizeof(int));
749 pos += sizeof(int);
750 }
751
752 wpa_priv_send_event(iface, event, buf, buflen);
753
754 os_free(buf);
755 }
756
757
758 static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface,
759 union wpa_event_data *data)
760 {
761 int ievent;
762 size_t len, maxlen;
763 u8 *buf;
764 char *ifname;
765
766 if (data == NULL)
767 return;
768
769 ievent = data->interface_status.ievent;
770 maxlen = sizeof(data->interface_status.ifname);
771 ifname = data->interface_status.ifname;
772 for (len = 0; len < maxlen && ifname[len]; len++)
773 ;
774
775 buf = os_malloc(sizeof(int) + len);
776 if (buf == NULL)
777 return;
778
779 os_memcpy(buf, &ievent, sizeof(int));
780 os_memcpy(buf + sizeof(int), ifname, len);
781
782 wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS,
783 buf, sizeof(int) + len);
784
785 os_free(buf);
786
787 }
788
789
790 static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface,
791 union wpa_event_data *data)
792 {
793 size_t len;
794 u8 *buf, *pos;
795
796 if (data == NULL || data->ft_ies.ies == NULL)
797 return;
798
799 len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len;
800 buf = os_malloc(len);
801 if (buf == NULL)
802 return;
803
804 pos = buf;
805 os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int));
806 pos += sizeof(int);
807 os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN);
808 pos += ETH_ALEN;
809 os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len);
810
811 wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len);
812
813 os_free(buf);
814
815 }
816
817
818 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
819 union wpa_event_data *data)
820 {
821 struct wpa_priv_interface *iface = ctx;
822
823 wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event);
824
825 if (!iface->wpas_registered) {
826 wpa_printf(MSG_DEBUG, "Driver event received, but "
827 "wpa_supplicant not registered");
828 return;
829 }
830
831 switch (event) {
832 case EVENT_ASSOC:
833 wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data);
834 break;
835 case EVENT_DISASSOC:
836 wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0);
837 break;
838 case EVENT_ASSOCINFO:
839 if (data == NULL)
840 return;
841 wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data);
842 break;
843 case EVENT_MICHAEL_MIC_FAILURE:
844 if (data == NULL)
845 return;
846 wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE,
847 &data->michael_mic_failure.unicast,
848 sizeof(int));
849 break;
850 case EVENT_SCAN_RESULTS:
851 wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL,
852 0);
853 break;
854 case EVENT_INTERFACE_STATUS:
855 wpa_priv_send_interface_status(iface, data);
856 break;
857 case EVENT_PMKID_CANDIDATE:
858 if (data == NULL)
859 return;
860 wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE,
861 &data->pmkid_candidate,
862 sizeof(struct pmkid_candidate));
863 break;
864 case EVENT_STKSTART:
865 if (data == NULL)
866 return;
867 wpa_priv_send_event(iface, PRIVSEP_EVENT_STKSTART,
868 &data->stkstart.peer, ETH_ALEN);
869 break;
870 case EVENT_FT_RESPONSE:
871 wpa_priv_send_ft_response(iface, data);
872 break;
873 default:
874 wpa_printf(MSG_DEBUG, "Unsupported driver event %d - TODO",
875 event);
876 break;
877 }
878 }
879
880
881 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
882 const u8 *buf, size_t len)
883 {
884 struct wpa_priv_interface *iface = ctx;
885 struct msghdr msg;
886 struct iovec io[3];
887 int event = PRIVSEP_EVENT_RX_EAPOL;
888
889 wpa_printf(MSG_DEBUG, "RX EAPOL from driver");
890 io[0].iov_base = &event;
891 io[0].iov_len = sizeof(event);
892 io[1].iov_base = (u8 *) src_addr;
893 io[1].iov_len = ETH_ALEN;
894 io[2].iov_base = (u8 *) buf;
895 io[2].iov_len = len;
896
897 os_memset(&msg, 0, sizeof(msg));
898 msg.msg_iov = io;
899 msg.msg_iovlen = 3;
900 msg.msg_name = &iface->drv_addr;
901 msg.msg_namelen = sizeof(iface->drv_addr);
902
903 if (sendmsg(iface->fd, &msg, 0) < 0)
904 perror("sendmsg(wpas_socket)");
905 }
906
907
908 static void wpa_priv_terminate(int sig, void *signal_ctx)
909 {
910 wpa_printf(MSG_DEBUG, "wpa_priv termination requested");
911 eloop_terminate();
912 }
913
914
915 static void wpa_priv_fd_workaround(void)
916 {
917 #ifdef __linux__
918 int s, i;
919 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
920 * fd 0, 1, and 2 closed. This will cause some issues because many
921 * places in wpa_supplicant are still printing out to stdout. As a
922 * workaround, make sure that fd's 0, 1, and 2 are not used for other
923 * sockets. */
924 for (i = 0; i < 3; i++) {
925 s = open("/dev/null", O_RDWR);
926 if (s > 2) {
927 close(s);
928 break;
929 }
930 }
931 #endif /* __linux__ */
932 }
933
934
935 static void usage(void)
936 {
937 printf("wpa_priv v" VERSION_STR "\n"
938 "Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> and "
939 "contributors\n"
940 "\n"
941 "usage:\n"
942 " wpa_priv [-Bdd] [-P<pid file>] <driver:ifname> "
943 "[driver:ifname ...]\n");
944 }
945
946
947 int main(int argc, char *argv[])
948 {
949 int c, i;
950 int ret = -1;
951 char *pid_file = NULL;
952 int daemonize = 0;
953 char *ctrl_dir = "/var/run/wpa_priv";
954 struct wpa_priv_interface *interfaces = NULL, *iface;
955
956 if (os_program_init())
957 return -1;
958
959 wpa_priv_fd_workaround();
960
961 for (;;) {
962 c = getopt(argc, argv, "Bc:dP:");
963 if (c < 0)
964 break;
965 switch (c) {
966 case 'B':
967 daemonize++;
968 break;
969 case 'c':
970 ctrl_dir = optarg;
971 break;
972 case 'd':
973 wpa_debug_level--;
974 break;
975 case 'P':
976 pid_file = os_rel2abs_path(optarg);
977 break;
978 default:
979 usage();
980 goto out;
981 }
982 }
983
984 if (optind >= argc) {
985 usage();
986 goto out;
987 }
988
989 wpa_printf(MSG_DEBUG, "wpa_priv control directory: '%s'", ctrl_dir);
990
991 if (eloop_init()) {
992 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
993 goto out;
994 }
995
996 for (i = optind; i < argc; i++) {
997 wpa_printf(MSG_DEBUG, "Adding driver:interface %s", argv[i]);
998 iface = wpa_priv_interface_init(ctrl_dir, argv[i]);
999 if (iface == NULL)
1000 goto out;
1001 iface->next = interfaces;
1002 interfaces = iface;
1003 }
1004
1005 if (daemonize && os_daemonize(pid_file))
1006 goto out;
1007
1008 eloop_register_signal_terminate(wpa_priv_terminate, NULL);
1009 eloop_run();
1010
1011 ret = 0;
1012
1013 out:
1014 iface = interfaces;
1015 while (iface) {
1016 struct wpa_priv_interface *prev = iface;
1017 iface = iface->next;
1018 wpa_priv_interface_deinit(prev);
1019 }
1020
1021 eloop_destroy();
1022
1023 os_daemonize_terminate(pid_file);
1024 os_free(pid_file);
1025 os_program_deinit();
1026
1027 return ret;
1028 }