2 * WPA Supplicant / privileged helper program
3 * Copyright (c) 2007, 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.
18 #endif /* __linux__ */
25 #include "drivers/driver.h"
26 #include "l2_packet/l2_packet.h"
27 #include "privsep_commands.h"
28 #include "ieee802_11_defs.h"
31 #define ETH_P_EAPOL 0x888e
34 #ifndef ETH_P_RSN_PREAUTH
35 #define ETH_P_RSN_PREAUTH 0x88c7
39 struct wpa_priv_interface
{
40 struct wpa_priv_interface
*next
;
46 struct wpa_driver_ops
*driver
;
48 struct sockaddr_un drv_addr
;
51 /* TODO: add support for multiple l2 connections */
52 struct l2_packet_data
*l2
;
53 struct sockaddr_un l2_addr
;
57 static void wpa_priv_cmd_register(struct wpa_priv_interface
*iface
,
58 struct sockaddr_un
*from
)
60 if (iface
->drv_priv
) {
61 wpa_printf(MSG_DEBUG
, "Cleaning up forgotten driver instance");
62 if (iface
->driver
->set_wpa
)
63 iface
->driver
->set_wpa(iface
->drv_priv
, 0);
64 if (iface
->driver
->deinit
)
65 iface
->driver
->deinit(iface
->drv_priv
);
66 iface
->drv_priv
= NULL
;
67 iface
->wpas_registered
= 0;
71 wpa_printf(MSG_DEBUG
, "Cleaning up forgotten l2_packet "
73 l2_packet_deinit(iface
->l2
);
77 if (iface
->driver
->init
== NULL
)
80 iface
->drv_priv
= iface
->driver
->init(iface
, iface
->ifname
);
81 if (iface
->drv_priv
== NULL
) {
82 wpa_printf(MSG_DEBUG
, "Failed to initialize driver wrapper");
86 wpa_printf(MSG_DEBUG
, "Driver wrapper '%s' initialized for interface "
87 "'%s'", iface
->driver_name
, iface
->ifname
);
89 os_memcpy(&iface
->drv_addr
, from
, sizeof(iface
->drv_addr
));
90 iface
->wpas_registered
= 1;
92 if (iface
->driver
->set_param
&&
93 iface
->driver
->set_param(iface
->drv_priv
, NULL
) < 0) {
94 wpa_printf(MSG_ERROR
, "Driver interface rejected param");
97 if (iface
->driver
->set_wpa
)
98 iface
->driver
->set_wpa(iface
->drv_priv
, 1);
102 static void wpa_priv_cmd_unregister(struct wpa_priv_interface
*iface
,
103 struct sockaddr_un
*from
)
105 if (iface
->drv_priv
) {
106 if (iface
->driver
->set_wpa
)
107 iface
->driver
->set_wpa(iface
->drv_priv
, 0);
108 if (iface
->driver
->deinit
)
109 iface
->driver
->deinit(iface
->drv_priv
);
110 iface
->drv_priv
= NULL
;
111 iface
->wpas_registered
= 0;
116 static void wpa_priv_cmd_set_wpa(struct wpa_priv_interface
*iface
,
117 char *buf
, size_t len
)
119 if (iface
->drv_priv
== NULL
|| len
!= sizeof(int))
122 if (iface
->driver
->set_wpa
)
123 iface
->driver
->set_wpa(iface
->drv_priv
, *((int *) buf
));
127 static void wpa_priv_cmd_scan(struct wpa_priv_interface
*iface
,
128 char *buf
, size_t len
)
130 if (iface
->drv_priv
== NULL
)
133 if (iface
->driver
->scan
)
134 iface
->driver
->scan(iface
->drv_priv
, len
? (u8
*) buf
: NULL
,
139 static void wpa_priv_get_scan_results2(struct wpa_priv_interface
*iface
,
140 struct sockaddr_un
*from
)
142 struct wpa_scan_results
*res
;
143 u8
*buf
= NULL
, *pos
, *end
;
147 res
= iface
->driver
->get_scan_results2(iface
->drv_priv
);
151 buf
= os_malloc(60000);
157 os_memcpy(pos
, &val
, sizeof(int));
160 for (i
= 0; i
< res
->num
; i
++) {
161 struct wpa_scan_res
*r
= res
->res
[i
];
162 val
= sizeof(*r
) + r
->ie_len
;
163 if (end
- pos
< (int) sizeof(int) + val
)
165 os_memcpy(pos
, &val
, sizeof(int));
167 os_memcpy(pos
, r
, val
);
171 sendto(iface
->fd
, buf
, pos
- buf
, 0, (struct sockaddr
*) from
,
181 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, sizeof(*from
));
185 static void wpa_priv_send_old_scan_results(struct wpa_priv_interface
*iface
,
186 struct sockaddr_un
*from
)
188 #define SCAN_AP_LIMIT 128
190 struct wpa_scan_result
*results
= NULL
;
191 u8
*buf
= NULL
, *pos
, *end
;
192 struct wpa_scan_res nres
;
194 results
= os_malloc(SCAN_AP_LIMIT
* sizeof(*results
));
198 res
= iface
->driver
->get_scan_results(iface
->drv_priv
, results
,
200 if (res
< 0 || res
> SCAN_AP_LIMIT
)
203 buf
= os_malloc(60000);
208 os_memcpy(pos
, &res
, sizeof(int));
211 os_memset(&nres
, 0, sizeof(nres
));
212 for (i
= 0; i
< res
; i
++) {
213 struct wpa_scan_result
*r
= &results
[i
];
216 ie_len
= 2 + r
->ssid_len
+ r
->rsn_ie_len
+ r
->wpa_ie_len
;
222 val
= sizeof(nres
) + ie_len
;
223 if (end
- pos
< (int) sizeof(int) + val
)
225 os_memcpy(pos
, &val
, sizeof(int));
228 os_memcpy(nres
.bssid
, r
->bssid
, ETH_ALEN
);
232 nres
.noise
= r
->noise
;
233 nres
.level
= r
->level
;
235 nres
.ie_len
= ie_len
;
237 os_memcpy(pos
, &nres
, sizeof(nres
));
241 *pos
++ = WLAN_EID_SSID
;
242 *pos
++ = r
->ssid_len
;
243 os_memcpy(pos
, r
->ssid
, r
->ssid_len
);
247 /* Fake Supported Rate IE to include max rate */
248 *pos
++ = WLAN_EID_SUPP_RATES
;
254 os_memcpy(pos
, r
->rsn_ie
, r
->rsn_ie_len
);
255 pos
+= r
->rsn_ie_len
;
258 if (r
->mdie_present
) {
259 os_memcpy(pos
, r
->mdie
, 5);
264 os_memcpy(pos
, r
->wpa_ie
, r
->wpa_ie_len
);
265 pos
+= r
->wpa_ie_len
;
269 sendto(iface
->fd
, buf
, pos
- buf
, 0, (struct sockaddr
*) from
,
279 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, sizeof(*from
));
283 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface
*iface
,
284 struct sockaddr_un
*from
)
286 if (iface
->drv_priv
== NULL
)
289 if (iface
->driver
->get_scan_results2
)
290 wpa_priv_get_scan_results2(iface
, from
);
291 else if (iface
->driver
->get_scan_results
)
292 wpa_priv_send_old_scan_results(iface
, from
);
294 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
,
299 static void wpa_priv_cmd_associate(struct wpa_priv_interface
*iface
,
300 void *buf
, size_t len
)
302 struct wpa_driver_associate_params params
;
303 struct privsep_cmd_associate
*assoc
;
307 if (iface
->drv_priv
== NULL
|| iface
->driver
->associate
== NULL
)
310 if (len
< sizeof(*assoc
)) {
311 wpa_printf(MSG_DEBUG
, "Invalid association request");
316 if (sizeof(*assoc
) + assoc
->wpa_ie_len
> len
) {
317 wpa_printf(MSG_DEBUG
, "Association request overflow");
321 os_memset(¶ms
, 0, sizeof(params
));
322 bssid
= assoc
->bssid
;
323 if (bssid
[0] | bssid
[1] | bssid
[2] | bssid
[3] | bssid
[4] | bssid
[5])
324 params
.bssid
= bssid
;
325 params
.ssid
= assoc
->ssid
;
326 if (assoc
->ssid_len
> 32)
328 params
.ssid_len
= assoc
->ssid_len
;
329 params
.freq
= assoc
->freq
;
330 if (assoc
->wpa_ie_len
) {
331 params
.wpa_ie
= (u8
*) (assoc
+ 1);
332 params
.wpa_ie_len
= assoc
->wpa_ie_len
;
334 params
.pairwise_suite
= assoc
->pairwise_suite
;
335 params
.group_suite
= assoc
->group_suite
;
336 params
.key_mgmt_suite
= assoc
->key_mgmt_suite
;
337 params
.auth_alg
= assoc
->auth_alg
;
338 params
.mode
= assoc
->mode
;
340 res
= iface
->driver
->associate(iface
->drv_priv
, ¶ms
);
341 wpa_printf(MSG_DEBUG
, "drv->associate: res=%d", res
);
345 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface
*iface
,
346 struct sockaddr_un
*from
)
350 if (iface
->drv_priv
== NULL
)
353 if (iface
->driver
->get_bssid
== NULL
||
354 iface
->driver
->get_bssid(iface
->drv_priv
, bssid
) < 0)
357 sendto(iface
->fd
, bssid
, ETH_ALEN
, 0, (struct sockaddr
*) from
,
362 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, sizeof(*from
));
366 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface
*iface
,
367 struct sockaddr_un
*from
)
369 u8 ssid
[sizeof(int) + 32];
372 if (iface
->drv_priv
== NULL
)
375 if (iface
->driver
->get_ssid
== NULL
)
378 res
= iface
->driver
->get_ssid(iface
->drv_priv
, &ssid
[sizeof(int)]);
379 if (res
< 0 || res
> 32)
381 os_memcpy(ssid
, &res
, sizeof(int));
383 sendto(iface
->fd
, ssid
, sizeof(ssid
), 0, (struct sockaddr
*) from
,
388 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, sizeof(*from
));
392 static void wpa_priv_cmd_set_key(struct wpa_priv_interface
*iface
,
393 void *buf
, size_t len
)
395 struct privsep_cmd_set_key
*params
;
398 if (iface
->drv_priv
== NULL
|| iface
->driver
->set_key
== NULL
)
401 if (len
!= sizeof(*params
)) {
402 wpa_printf(MSG_DEBUG
, "Invalid set_key request");
408 res
= iface
->driver
->set_key(iface
->drv_priv
, params
->alg
,
409 params
->addr
, params
->key_idx
,
411 params
->seq_len
? params
->seq
: NULL
,
413 params
->key_len
? params
->key
: NULL
,
415 wpa_printf(MSG_DEBUG
, "drv->set_key: res=%d", res
);
419 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface
*iface
,
420 struct sockaddr_un
*from
)
422 struct wpa_driver_capa capa
;
424 if (iface
->drv_priv
== NULL
)
427 if (iface
->driver
->get_capa
== NULL
||
428 iface
->driver
->get_capa(iface
->drv_priv
, &capa
) < 0)
431 sendto(iface
->fd
, &capa
, sizeof(capa
), 0, (struct sockaddr
*) from
,
436 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, sizeof(*from
));
440 static void wpa_priv_l2_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
443 struct wpa_priv_interface
*iface
= ctx
;
447 io
[0].iov_base
= (u8
*) src_addr
;
448 io
[0].iov_len
= ETH_ALEN
;
449 io
[1].iov_base
= (u8
*) buf
;
452 os_memset(&msg
, 0, sizeof(msg
));
455 msg
.msg_name
= &iface
->l2_addr
;
456 msg
.msg_namelen
= sizeof(iface
->l2_addr
);
458 if (sendmsg(iface
->fd
, &msg
, 0) < 0) {
459 perror("sendmsg(l2 rx)");
464 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface
*iface
,
465 struct sockaddr_un
*from
,
466 void *buf
, size_t len
)
469 u8 own_addr
[ETH_ALEN
];
473 if (len
!= 2 * sizeof(int)) {
474 wpa_printf(MSG_DEBUG
, "Invalid l2_register length %lu",
475 (unsigned long) len
);
480 if (proto
!= ETH_P_EAPOL
&& proto
!= ETH_P_RSN_PREAUTH
) {
481 wpa_printf(MSG_DEBUG
, "Refused l2_packet connection for "
482 "ethertype 0x%x", proto
);
487 wpa_printf(MSG_DEBUG
, "Cleaning up forgotten l2_packet "
489 l2_packet_deinit(iface
->l2
);
493 os_memcpy(&iface
->l2_addr
, from
, sizeof(iface
->l2_addr
));
495 iface
->l2
= l2_packet_init(iface
->ifname
, NULL
, proto
,
496 wpa_priv_l2_rx
, iface
, reg_cmd
[1]);
497 if (iface
->l2
== NULL
) {
498 wpa_printf(MSG_DEBUG
, "Failed to initialize l2_packet "
499 "instance for protocol %d", proto
);
503 if (l2_packet_get_own_addr(iface
->l2
, own_addr
) < 0) {
504 wpa_printf(MSG_DEBUG
, "Failed to get own address from "
506 l2_packet_deinit(iface
->l2
);
511 res
= sendto(iface
->fd
, own_addr
, ETH_ALEN
, 0,
512 (struct sockaddr
*) from
, sizeof(*from
));
513 wpa_printf(MSG_DEBUG
, "L2 registration: res=%d", res
);
517 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface
*iface
,
518 struct sockaddr_un
*from
)
521 l2_packet_deinit(iface
->l2
);
527 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface
*iface
,
528 struct sockaddr_un
*from
)
531 l2_packet_notify_auth_start(iface
->l2
);
535 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface
*iface
,
536 struct sockaddr_un
*from
,
537 void *buf
, size_t len
)
543 if (iface
->l2
== NULL
)
546 if (len
< ETH_ALEN
+ 2) {
547 wpa_printf(MSG_DEBUG
, "Too short L2 send packet (len=%lu)",
548 (unsigned long) len
);
553 os_memcpy(&proto
, buf
+ ETH_ALEN
, 2);
555 if (proto
!= ETH_P_EAPOL
&& proto
!= ETH_P_RSN_PREAUTH
) {
556 wpa_printf(MSG_DEBUG
, "Refused l2_packet send for ethertype "
561 res
= l2_packet_send(iface
->l2
, dst_addr
, proto
, buf
+ ETH_ALEN
+ 2,
563 wpa_printf(MSG_DEBUG
, "L2 send: res=%d", res
);
567 static void wpa_priv_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
569 struct wpa_priv_interface
*iface
= eloop_ctx
;
574 struct sockaddr_un from
;
575 socklen_t fromlen
= sizeof(from
);
577 res
= recvfrom(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &from
,
584 if (res
< (int) sizeof(int)) {
585 wpa_printf(MSG_DEBUG
, "Too short command (len=%d)", res
);
589 os_memcpy(&cmd
, buf
, sizeof(int));
590 wpa_printf(MSG_DEBUG
, "Command %d for interface %s",
592 cmd_buf
= &buf
[sizeof(int)];
593 cmd_len
= res
- sizeof(int);
596 case PRIVSEP_CMD_REGISTER
:
597 wpa_priv_cmd_register(iface
, &from
);
599 case PRIVSEP_CMD_UNREGISTER
:
600 wpa_priv_cmd_unregister(iface
, &from
);
602 case PRIVSEP_CMD_SET_WPA
:
603 wpa_priv_cmd_set_wpa(iface
, cmd_buf
, cmd_len
);
605 case PRIVSEP_CMD_SCAN
:
606 wpa_priv_cmd_scan(iface
, cmd_buf
, cmd_len
);
608 case PRIVSEP_CMD_GET_SCAN_RESULTS
:
609 wpa_priv_cmd_get_scan_results(iface
, &from
);
611 case PRIVSEP_CMD_ASSOCIATE
:
612 wpa_priv_cmd_associate(iface
, cmd_buf
, cmd_len
);
614 case PRIVSEP_CMD_GET_BSSID
:
615 wpa_priv_cmd_get_bssid(iface
, &from
);
617 case PRIVSEP_CMD_GET_SSID
:
618 wpa_priv_cmd_get_ssid(iface
, &from
);
620 case PRIVSEP_CMD_SET_KEY
:
621 wpa_priv_cmd_set_key(iface
, cmd_buf
, cmd_len
);
623 case PRIVSEP_CMD_GET_CAPA
:
624 wpa_priv_cmd_get_capa(iface
, &from
);
626 case PRIVSEP_CMD_L2_REGISTER
:
627 wpa_priv_cmd_l2_register(iface
, &from
, cmd_buf
, cmd_len
);
629 case PRIVSEP_CMD_L2_UNREGISTER
:
630 wpa_priv_cmd_l2_unregister(iface
, &from
);
632 case PRIVSEP_CMD_L2_NOTIFY_AUTH_START
:
633 wpa_priv_cmd_l2_notify_auth_start(iface
, &from
);
635 case PRIVSEP_CMD_L2_SEND
:
636 wpa_priv_cmd_l2_send(iface
, &from
, cmd_buf
, cmd_len
);
642 static void wpa_priv_interface_deinit(struct wpa_priv_interface
*iface
)
644 if (iface
->drv_priv
&& iface
->driver
->deinit
)
645 iface
->driver
->deinit(iface
->drv_priv
);
647 if (iface
->fd
>= 0) {
648 eloop_unregister_read_sock(iface
->fd
);
650 unlink(iface
->sock_name
);
654 l2_packet_deinit(iface
->l2
);
656 os_free(iface
->ifname
);
657 os_free(iface
->driver_name
);
658 os_free(iface
->sock_name
);
663 extern struct wpa_driver_ops
*wpa_supplicant_drivers
[];
665 static struct wpa_priv_interface
*
666 wpa_priv_interface_init(const char *dir
, const char *params
)
668 struct wpa_priv_interface
*iface
;
671 struct sockaddr_un addr
;
674 pos
= os_strchr(params
, ':');
678 iface
= os_zalloc(sizeof(*iface
));
684 iface
->driver_name
= os_malloc(len
+ 1);
685 if (iface
->driver_name
== NULL
) {
686 wpa_priv_interface_deinit(iface
);
689 os_memcpy(iface
->driver_name
, params
, len
);
690 iface
->driver_name
[len
] = '\0';
692 for (i
= 0; wpa_supplicant_drivers
[i
]; i
++) {
693 if (os_strcmp(iface
->driver_name
,
694 wpa_supplicant_drivers
[i
]->name
) == 0) {
695 iface
->driver
= wpa_supplicant_drivers
[i
];
699 if (iface
->driver
== NULL
) {
700 wpa_printf(MSG_ERROR
, "Unsupported driver '%s'",
702 wpa_priv_interface_deinit(iface
);
707 iface
->ifname
= os_strdup(pos
);
708 if (iface
->ifname
== NULL
) {
709 wpa_priv_interface_deinit(iface
);
713 len
= os_strlen(dir
) + 1 + os_strlen(iface
->ifname
);
714 iface
->sock_name
= os_malloc(len
+ 1);
715 if (iface
->sock_name
== NULL
) {
716 wpa_priv_interface_deinit(iface
);
720 os_snprintf(iface
->sock_name
, len
+ 1, "%s/%s", dir
, iface
->ifname
);
721 if (os_strlen(iface
->sock_name
) >= sizeof(addr
.sun_path
)) {
722 wpa_priv_interface_deinit(iface
);
726 iface
->fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
728 perror("socket(PF_UNIX)");
729 wpa_priv_interface_deinit(iface
);
733 os_memset(&addr
, 0, sizeof(addr
));
734 addr
.sun_family
= AF_UNIX
;
735 os_strlcpy(addr
.sun_path
, iface
->sock_name
, sizeof(addr
.sun_path
));
737 if (bind(iface
->fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
738 wpa_printf(MSG_DEBUG
, "bind(PF_UNIX) failed: %s",
740 if (connect(iface
->fd
, (struct sockaddr
*) &addr
,
742 wpa_printf(MSG_DEBUG
, "Socket exists, but does not "
743 "allow connections - assuming it was "
744 "leftover from forced program termination");
745 if (unlink(iface
->sock_name
) < 0) {
746 perror("unlink[ctrl_iface]");
747 wpa_printf(MSG_ERROR
, "Could not unlink "
748 "existing ctrl_iface socket '%s'",
752 if (bind(iface
->fd
, (struct sockaddr
*) &addr
,
754 perror("bind(PF_UNIX)");
757 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
758 "socket '%s'", iface
->sock_name
);
760 wpa_printf(MSG_INFO
, "Socket exists and seems to be "
761 "in use - cannot override it");
762 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
763 "not used anymore", iface
->sock_name
);
768 if (chmod(iface
->sock_name
, S_IRWXU
| S_IRWXG
| S_IRWXO
) < 0) {
773 eloop_register_read_sock(iface
->fd
, wpa_priv_receive
, iface
, NULL
);
778 wpa_priv_interface_deinit(iface
);
783 static int wpa_priv_send_event(struct wpa_priv_interface
*iface
, int event
,
784 const void *data
, size_t data_len
)
789 io
[0].iov_base
= &event
;
790 io
[0].iov_len
= sizeof(event
);
791 io
[1].iov_base
= (u8
*) data
;
792 io
[1].iov_len
= data_len
;
794 os_memset(&msg
, 0, sizeof(msg
));
796 msg
.msg_iovlen
= data
? 2 : 1;
797 msg
.msg_name
= &iface
->drv_addr
;
798 msg
.msg_namelen
= sizeof(iface
->drv_addr
);
800 if (sendmsg(iface
->fd
, &msg
, 0) < 0) {
801 perror("sendmsg(wpas_socket)");
809 static void wpa_priv_send_assoc(struct wpa_priv_interface
*iface
, int event
,
810 union wpa_event_data
*data
)
812 size_t buflen
= 3 * sizeof(int);
817 buflen
+= data
->assoc_info
.req_ies_len
+
818 data
->assoc_info
.resp_ies_len
+
819 data
->assoc_info
.beacon_ies_len
;
822 buf
= os_malloc(buflen
);
828 if (data
&& data
->assoc_info
.req_ies
) {
829 len
= data
->assoc_info
.req_ies_len
;
830 os_memcpy(pos
, &len
, sizeof(int));
832 os_memcpy(pos
, data
->assoc_info
.req_ies
, len
);
836 os_memcpy(pos
, &len
, sizeof(int));
840 if (data
&& data
->assoc_info
.resp_ies
) {
841 len
= data
->assoc_info
.resp_ies_len
;
842 os_memcpy(pos
, &len
, sizeof(int));
844 os_memcpy(pos
, data
->assoc_info
.resp_ies
, len
);
848 os_memcpy(pos
, &len
, sizeof(int));
852 if (data
&& data
->assoc_info
.beacon_ies
) {
853 len
= data
->assoc_info
.beacon_ies_len
;
854 os_memcpy(pos
, &len
, sizeof(int));
856 os_memcpy(pos
, data
->assoc_info
.beacon_ies
, len
);
860 os_memcpy(pos
, &len
, sizeof(int));
864 wpa_priv_send_event(iface
, event
, buf
, buflen
);
870 static void wpa_priv_send_interface_status(struct wpa_priv_interface
*iface
,
871 union wpa_event_data
*data
)
881 ievent
= data
->interface_status
.ievent
;
882 maxlen
= sizeof(data
->interface_status
.ifname
);
883 ifname
= data
->interface_status
.ifname
;
884 for (len
= 0; len
< maxlen
&& ifname
[len
]; len
++)
887 buf
= os_malloc(sizeof(int) + len
);
891 os_memcpy(buf
, &ievent
, sizeof(int));
892 os_memcpy(buf
+ sizeof(int), ifname
, len
);
894 wpa_priv_send_event(iface
, PRIVSEP_EVENT_INTERFACE_STATUS
,
895 buf
, sizeof(int) + len
);
902 static void wpa_priv_send_ft_response(struct wpa_priv_interface
*iface
,
903 union wpa_event_data
*data
)
908 if (data
== NULL
|| data
->ft_ies
.ies
== NULL
)
911 len
= sizeof(int) + ETH_ALEN
+ data
->ft_ies
.ies_len
;
912 buf
= os_malloc(len
);
917 os_memcpy(pos
, &data
->ft_ies
.ft_action
, sizeof(int));
919 os_memcpy(pos
, data
->ft_ies
.target_ap
, ETH_ALEN
);
921 os_memcpy(pos
, data
->ft_ies
.ies
, data
->ft_ies
.ies_len
);
923 wpa_priv_send_event(iface
, PRIVSEP_EVENT_FT_RESPONSE
, buf
, len
);
930 void wpa_supplicant_event(void *ctx
, wpa_event_type event
,
931 union wpa_event_data
*data
)
933 struct wpa_priv_interface
*iface
= ctx
;
935 wpa_printf(MSG_DEBUG
, "%s - event=%d", __func__
, event
);
937 if (!iface
->wpas_registered
) {
938 wpa_printf(MSG_DEBUG
, "Driver event received, but "
939 "wpa_supplicant not registered");
945 wpa_priv_send_assoc(iface
, PRIVSEP_EVENT_ASSOC
, data
);
948 wpa_priv_send_event(iface
, PRIVSEP_EVENT_DISASSOC
, NULL
, 0);
950 case EVENT_ASSOCINFO
:
953 wpa_priv_send_assoc(iface
, PRIVSEP_EVENT_ASSOCINFO
, data
);
955 case EVENT_MICHAEL_MIC_FAILURE
:
958 wpa_priv_send_event(iface
, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE
,
959 &data
->michael_mic_failure
.unicast
,
962 case EVENT_SCAN_RESULTS
:
963 wpa_priv_send_event(iface
, PRIVSEP_EVENT_SCAN_RESULTS
, NULL
,
966 case EVENT_INTERFACE_STATUS
:
967 wpa_priv_send_interface_status(iface
, data
);
969 case EVENT_PMKID_CANDIDATE
:
972 wpa_priv_send_event(iface
, PRIVSEP_EVENT_PMKID_CANDIDATE
,
973 &data
->pmkid_candidate
,
974 sizeof(struct pmkid_candidate
));
979 wpa_priv_send_event(iface
, PRIVSEP_EVENT_STKSTART
,
980 &data
->stkstart
.peer
, ETH_ALEN
);
982 case EVENT_FT_RESPONSE
:
983 wpa_priv_send_ft_response(iface
, data
);
986 wpa_printf(MSG_DEBUG
, "Unsupported driver event %d - TODO",
993 void wpa_supplicant_rx_eapol(void *ctx
, const u8
*src_addr
,
994 const u8
*buf
, size_t len
)
996 struct wpa_priv_interface
*iface
= ctx
;
999 int event
= PRIVSEP_EVENT_RX_EAPOL
;
1001 wpa_printf(MSG_DEBUG
, "RX EAPOL from driver");
1002 io
[0].iov_base
= &event
;
1003 io
[0].iov_len
= sizeof(event
);
1004 io
[1].iov_base
= (u8
*) src_addr
;
1005 io
[1].iov_len
= ETH_ALEN
;
1006 io
[2].iov_base
= (u8
*) buf
;
1007 io
[2].iov_len
= len
;
1009 os_memset(&msg
, 0, sizeof(msg
));
1012 msg
.msg_name
= &iface
->drv_addr
;
1013 msg
.msg_namelen
= sizeof(iface
->drv_addr
);
1015 if (sendmsg(iface
->fd
, &msg
, 0) < 0)
1016 perror("sendmsg(wpas_socket)");
1019 static void wpa_priv_terminate(int sig
, void *eloop_ctx
, void *signal_ctx
)
1021 wpa_printf(MSG_DEBUG
, "wpa_priv termination requested");
1026 static void wpa_priv_fd_workaround(void)
1030 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
1031 * fd 0, 1, and 2 closed. This will cause some issues because many
1032 * places in wpa_supplicant are still printing out to stdout. As a
1033 * workaround, make sure that fd's 0, 1, and 2 are not used for other
1035 for (i
= 0; i
< 3; i
++) {
1036 s
= open("/dev/null", O_RDWR
);
1042 #endif /* __linux__ */
1046 static void usage(void)
1048 printf("wpa_priv v" VERSION_STR
"\n"
1049 "Copyright (c) 2007, Jouni Malinen <j@w1.fi> and contributors\n"
1052 " wpa_priv [-Bdd] [-P<pid file>] <driver:ifname> "
1053 "[driver:ifname ...]\n");
1057 extern int wpa_debug_level
;
1059 int main(int argc
, char *argv
[])
1063 char *pid_file
= NULL
;
1065 char *ctrl_dir
= "/var/run/wpa_priv";
1066 struct wpa_priv_interface
*interfaces
= NULL
, *iface
;
1068 if (os_program_init())
1071 wpa_priv_fd_workaround();
1074 c
= getopt(argc
, argv
, "Bc:dP:");
1088 pid_file
= os_rel2abs_path(optarg
);
1096 if (optind
>= argc
) {
1101 wpa_printf(MSG_DEBUG
, "wpa_priv control directory: '%s'", ctrl_dir
);
1103 if (eloop_init(NULL
)) {
1104 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1108 for (i
= optind
; i
< argc
; i
++) {
1109 wpa_printf(MSG_DEBUG
, "Adding driver:interface %s", argv
[i
]);
1110 iface
= wpa_priv_interface_init(ctrl_dir
, argv
[i
]);
1113 iface
->next
= interfaces
;
1117 if (daemonize
&& os_daemonize(pid_file
))
1120 eloop_register_signal_terminate(wpa_priv_terminate
, NULL
);
1128 struct wpa_priv_interface
*prev
= iface
;
1129 iface
= iface
->next
;
1130 wpa_priv_interface_deinit(prev
);
1135 os_daemonize_terminate(pid_file
);
1137 os_program_deinit();