2 * WPA Supplicant / privileged helper program
3 * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #endif /* __linux__ */
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"
24 #define WPA_PRIV_MAX_L2 3
26 struct wpa_priv_interface
{
27 struct wpa_priv_interface
*next
;
35 const struct wpa_driver_ops
*driver
;
37 void *drv_global_priv
;
38 struct sockaddr_un drv_addr
;
39 socklen_t drv_addr_len
;
42 struct l2_packet_data
*l2
[WPA_PRIV_MAX_L2
];
43 struct sockaddr_un l2_addr
[WPA_PRIV_MAX_L2
];
44 socklen_t l2_addr_len
[WPA_PRIV_MAX_L2
];
46 struct wpa_priv_interface
*parent
;
48 } l2_ctx
[WPA_PRIV_MAX_L2
];
51 struct wpa_priv_global
{
52 struct wpa_priv_interface
*interfaces
;
56 static void wpa_priv_cmd_register(struct wpa_priv_interface
*iface
,
57 struct sockaddr_un
*from
, socklen_t fromlen
)
61 if (iface
->drv_priv
) {
62 wpa_printf(MSG_DEBUG
, "Cleaning up forgotten driver instance");
63 if (iface
->driver
->deinit
)
64 iface
->driver
->deinit(iface
->drv_priv
);
65 iface
->drv_priv
= NULL
;
66 if (iface
->drv_global_priv
) {
67 iface
->driver
->global_deinit(iface
->drv_global_priv
);
68 iface
->drv_global_priv
= NULL
;
70 iface
->wpas_registered
= 0;
73 for (i
= 0; i
< WPA_PRIV_MAX_L2
; i
++) {
76 "Cleaning up forgotten l2_packet instance");
77 l2_packet_deinit(iface
->l2
[i
]);
82 if (iface
->driver
->init2
) {
83 if (iface
->driver
->global_init
) {
84 iface
->drv_global_priv
=
85 iface
->driver
->global_init(iface
->ctx
);
86 if (!iface
->drv_global_priv
) {
88 "Failed to initialize driver global context");
92 iface
->drv_global_priv
= NULL
;
94 iface
->drv_priv
= iface
->driver
->init2(iface
, iface
->ifname
,
95 iface
->drv_global_priv
);
96 } else if (iface
->driver
->init
) {
97 iface
->drv_priv
= iface
->driver
->init(iface
, iface
->ifname
);
101 if (iface
->drv_priv
== NULL
) {
102 wpa_printf(MSG_DEBUG
, "Failed to initialize driver wrapper");
106 wpa_printf(MSG_DEBUG
, "Driver wrapper '%s' initialized for interface "
107 "'%s'", iface
->driver_name
, iface
->ifname
);
109 os_memcpy(&iface
->drv_addr
, from
, fromlen
);
110 iface
->drv_addr_len
= fromlen
;
111 iface
->wpas_registered
= 1;
113 if (iface
->driver
->set_param
&&
114 iface
->driver
->set_param(iface
->drv_priv
, NULL
) < 0) {
115 wpa_printf(MSG_ERROR
, "Driver interface rejected param");
120 static void wpa_priv_cmd_unregister(struct wpa_priv_interface
*iface
,
121 struct sockaddr_un
*from
)
123 if (iface
->drv_priv
) {
124 if (iface
->driver
->deinit
)
125 iface
->driver
->deinit(iface
->drv_priv
);
126 iface
->drv_priv
= NULL
;
127 if (iface
->drv_global_priv
) {
128 iface
->driver
->global_deinit(iface
->drv_global_priv
);
129 iface
->drv_global_priv
= NULL
;
131 iface
->wpas_registered
= 0;
136 static void wpa_priv_cmd_scan(struct wpa_priv_interface
*iface
,
137 void *buf
, size_t len
)
139 struct wpa_driver_scan_params params
;
140 struct privsep_cmd_scan
*scan
;
142 int freqs
[PRIVSEP_MAX_SCAN_FREQS
+ 1];
144 if (iface
->drv_priv
== NULL
)
147 if (len
< sizeof(*scan
)) {
148 wpa_printf(MSG_DEBUG
, "Invalid scan request");
154 os_memset(¶ms
, 0, sizeof(params
));
155 if (scan
->num_ssids
> WPAS_MAX_SCAN_SSIDS
) {
156 wpa_printf(MSG_DEBUG
, "Invalid scan request (num_ssids)");
159 params
.num_ssids
= scan
->num_ssids
;
160 for (i
= 0; i
< scan
->num_ssids
; i
++) {
161 params
.ssids
[i
].ssid
= scan
->ssids
[i
];
162 params
.ssids
[i
].ssid_len
= scan
->ssid_lens
[i
];
165 if (scan
->num_freqs
> PRIVSEP_MAX_SCAN_FREQS
) {
166 wpa_printf(MSG_DEBUG
, "Invalid scan request (num_freqs)");
169 if (scan
->num_freqs
) {
170 for (i
= 0; i
< scan
->num_freqs
; i
++)
171 freqs
[i
] = scan
->freqs
[i
];
173 params
.freqs
= freqs
;
176 if (iface
->driver
->scan2
)
177 iface
->driver
->scan2(iface
->drv_priv
, ¶ms
);
181 static void wpa_priv_get_scan_results2(struct wpa_priv_interface
*iface
,
182 struct sockaddr_un
*from
,
185 struct wpa_scan_results
*res
;
186 u8
*buf
= NULL
, *pos
, *end
;
190 res
= iface
->driver
->get_scan_results2(iface
->drv_priv
);
194 buf
= os_malloc(60000);
200 os_memcpy(pos
, &val
, sizeof(int));
203 for (i
= 0; i
< res
->num
; i
++) {
204 struct wpa_scan_res
*r
= res
->res
[i
];
205 val
= sizeof(*r
) + r
->ie_len
+ r
->beacon_ie_len
;
206 if (end
- pos
< (int) sizeof(int) + val
)
208 os_memcpy(pos
, &val
, sizeof(int));
210 os_memcpy(pos
, r
, val
);
214 sendto(iface
->fd
, buf
, pos
- buf
, 0, (struct sockaddr
*) from
, fromlen
);
217 wpa_scan_results_free(res
);
222 wpa_scan_results_free(res
);
223 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, fromlen
);
227 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface
*iface
,
228 struct sockaddr_un
*from
,
231 if (iface
->drv_priv
== NULL
)
234 if (iface
->driver
->get_scan_results2
)
235 wpa_priv_get_scan_results2(iface
, from
, fromlen
);
237 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, fromlen
);
241 static void wpa_priv_cmd_authenticate(struct wpa_priv_interface
*iface
,
242 void *buf
, size_t len
)
244 struct wpa_driver_auth_params params
;
245 struct privsep_cmd_authenticate
*auth
;
248 if (iface
->drv_priv
== NULL
|| iface
->driver
->authenticate
== NULL
)
251 if (len
< sizeof(*auth
)) {
252 wpa_printf(MSG_DEBUG
, "Invalid authentication request");
257 if (sizeof(*auth
) + auth
->ie_len
+ auth
->auth_data_len
> len
) {
258 wpa_printf(MSG_DEBUG
, "Authentication request overflow");
262 os_memset(¶ms
, 0, sizeof(params
));
263 params
.freq
= auth
->freq
;
264 params
.bssid
= auth
->bssid
;
265 params
.ssid
= auth
->ssid
;
266 if (auth
->ssid_len
> SSID_MAX_LEN
)
268 params
.ssid_len
= auth
->ssid_len
;
269 params
.auth_alg
= auth
->auth_alg
;
270 for (i
= 0; i
< 4; i
++) {
271 if (auth
->wep_key_len
[i
]) {
272 params
.wep_key
[i
] = auth
->wep_key
[i
];
273 params
.wep_key_len
[i
] = auth
->wep_key_len
[i
];
276 params
.wep_tx_keyidx
= auth
->wep_tx_keyidx
;
277 params
.local_state_change
= auth
->local_state_change
;
278 params
.p2p
= auth
->p2p
;
280 params
.ie
= (u8
*) (auth
+ 1);
281 params
.ie_len
= auth
->ie_len
;
283 if (auth
->auth_data_len
) {
284 params
.auth_data
= ((u8
*) (auth
+ 1)) + auth
->ie_len
;
285 params
.auth_data_len
= auth
->auth_data_len
;
288 res
= iface
->driver
->authenticate(iface
->drv_priv
, ¶ms
);
289 wpa_printf(MSG_DEBUG
, "drv->authenticate: res=%d", res
);
293 static void wpa_priv_cmd_associate(struct wpa_priv_interface
*iface
,
294 void *buf
, size_t len
)
296 struct wpa_driver_associate_params params
;
297 struct privsep_cmd_associate
*assoc
;
301 if (iface
->drv_priv
== NULL
|| iface
->driver
->associate
== NULL
)
304 if (len
< sizeof(*assoc
)) {
305 wpa_printf(MSG_DEBUG
, "Invalid association request");
310 if (sizeof(*assoc
) + assoc
->wpa_ie_len
> len
) {
311 wpa_printf(MSG_DEBUG
, "Association request overflow");
315 os_memset(¶ms
, 0, sizeof(params
));
316 bssid
= assoc
->bssid
;
317 if (bssid
[0] | bssid
[1] | bssid
[2] | bssid
[3] | bssid
[4] | bssid
[5])
318 params
.bssid
= bssid
;
319 params
.ssid
= assoc
->ssid
;
320 if (assoc
->ssid_len
> SSID_MAX_LEN
)
322 params
.ssid_len
= assoc
->ssid_len
;
323 params
.freq
.mode
= assoc
->hwmode
;
324 params
.freq
.freq
= assoc
->freq
;
325 params
.freq
.channel
= assoc
->channel
;
326 if (assoc
->wpa_ie_len
) {
327 params
.wpa_ie
= (u8
*) (assoc
+ 1);
328 params
.wpa_ie_len
= assoc
->wpa_ie_len
;
330 params
.pairwise_suite
= assoc
->pairwise_suite
;
331 params
.group_suite
= assoc
->group_suite
;
332 params
.key_mgmt_suite
= assoc
->key_mgmt_suite
;
333 params
.auth_alg
= assoc
->auth_alg
;
334 params
.mode
= assoc
->mode
;
336 res
= iface
->driver
->associate(iface
->drv_priv
, ¶ms
);
337 wpa_printf(MSG_DEBUG
, "drv->associate: res=%d", res
);
341 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface
*iface
,
342 struct sockaddr_un
*from
, socklen_t fromlen
)
346 if (iface
->drv_priv
== NULL
)
349 if (iface
->driver
->get_bssid
== NULL
||
350 iface
->driver
->get_bssid(iface
->drv_priv
, bssid
) < 0)
353 sendto(iface
->fd
, bssid
, ETH_ALEN
, 0, (struct sockaddr
*) from
,
358 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, fromlen
);
362 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface
*iface
,
363 struct sockaddr_un
*from
, socklen_t fromlen
)
365 u8 ssid
[sizeof(int) + SSID_MAX_LEN
];
368 if (iface
->drv_priv
== NULL
)
371 if (iface
->driver
->get_ssid
== NULL
)
374 os_memset(ssid
, 0, sizeof(ssid
));
375 res
= iface
->driver
->get_ssid(iface
->drv_priv
, &ssid
[sizeof(int)]);
376 if (res
< 0 || res
> SSID_MAX_LEN
)
378 os_memcpy(ssid
, &res
, sizeof(int));
380 sendto(iface
->fd
, ssid
, sizeof(ssid
), 0, (struct sockaddr
*) from
,
385 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, fromlen
);
389 static void wpa_priv_cmd_set_key(struct wpa_priv_interface
*iface
,
390 void *buf
, size_t len
)
392 struct privsep_cmd_set_key
*params
;
394 struct wpa_driver_set_key_params p
;
396 if (iface
->drv_priv
== NULL
|| iface
->driver
->set_key
== NULL
)
399 if (len
!= sizeof(*params
)) {
400 wpa_printf(MSG_DEBUG
, "Invalid set_key request");
406 os_memset(&p
, 0, sizeof(p
));
407 p
.ifname
= iface
->ifname
;
409 p
.addr
= params
->addr
;
410 p
.key_idx
= params
->key_idx
;
411 p
.set_tx
= params
->set_tx
;
412 p
.seq
= params
->seq_len
? params
->seq
: NULL
;
413 p
.seq_len
= params
->seq_len
;
414 p
.key
= params
->key_len
? params
->key
: NULL
;
415 p
.key_len
= params
->key_len
;
417 res
= iface
->driver
->set_key(iface
->drv_priv
, &p
);
418 wpa_printf(MSG_DEBUG
, "drv->set_key: res=%d", res
);
422 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface
*iface
,
423 struct sockaddr_un
*from
, socklen_t fromlen
)
425 struct wpa_driver_capa capa
;
427 if (iface
->drv_priv
== NULL
)
430 if (iface
->driver
->get_capa
== NULL
||
431 iface
->driver
->get_capa(iface
->drv_priv
, &capa
) < 0)
434 /* For now, no support for passing extended_capa pointers */
435 capa
.extended_capa
= NULL
;
436 capa
.extended_capa_mask
= NULL
;
437 capa
.extended_capa_len
= 0;
438 sendto(iface
->fd
, &capa
, sizeof(capa
), 0, (struct sockaddr
*) from
,
443 sendto(iface
->fd
, "", 0, 0, (struct sockaddr
*) from
, fromlen
);
447 static void wpa_priv_l2_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
450 struct wpa_priv_l2
*l2_ctx
= ctx
;
451 struct wpa_priv_interface
*iface
= l2_ctx
->parent
;
455 io
[0].iov_base
= (u8
*) src_addr
;
456 io
[0].iov_len
= ETH_ALEN
;
457 io
[1].iov_base
= (u8
*) buf
;
460 os_memset(&msg
, 0, sizeof(msg
));
463 msg
.msg_name
= &iface
->l2_addr
[l2_ctx
->idx
];
464 msg
.msg_namelen
= iface
->l2_addr_len
[l2_ctx
->idx
];
466 if (sendmsg(iface
->fd
, &msg
, 0) < 0) {
467 wpa_printf(MSG_ERROR
, "sendmsg(l2 rx): %s", strerror(errno
));
472 static int wpa_priv_allowed_l2_proto(u16 proto
)
474 return proto
== ETH_P_EAPOL
|| proto
== ETH_P_RSN_PREAUTH
||
475 proto
== ETH_P_80211_ENCAP
;
479 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface
*iface
,
480 struct sockaddr_un
*from
,
482 void *buf
, size_t len
)
485 u8 own_addr
[ETH_ALEN
];
490 if (len
!= 2 * sizeof(int)) {
491 wpa_printf(MSG_DEBUG
, "Invalid l2_register length %lu",
492 (unsigned long) len
);
497 if (!wpa_priv_allowed_l2_proto(proto
)) {
498 wpa_printf(MSG_DEBUG
, "Refused l2_packet connection for "
499 "ethertype 0x%x", proto
);
503 for (idx
= 0; idx
< WPA_PRIV_MAX_L2
; idx
++) {
507 if (idx
== WPA_PRIV_MAX_L2
) {
508 wpa_printf(MSG_DEBUG
, "No free l2_packet connection found");
512 os_memcpy(&iface
->l2_addr
[idx
], from
, fromlen
);
513 iface
->l2_addr_len
[idx
] = fromlen
;
515 iface
->l2_ctx
[idx
].idx
= idx
;
516 iface
->l2_ctx
[idx
].parent
= iface
;
517 iface
->l2
[idx
] = l2_packet_init(iface
->ifname
, NULL
, proto
,
518 wpa_priv_l2_rx
, &iface
->l2_ctx
[idx
],
520 if (!iface
->l2
[idx
]) {
521 wpa_printf(MSG_DEBUG
, "Failed to initialize l2_packet "
522 "instance for protocol %d", proto
);
526 if (l2_packet_get_own_addr(iface
->l2
[idx
], own_addr
) < 0) {
527 wpa_printf(MSG_DEBUG
, "Failed to get own address from "
529 l2_packet_deinit(iface
->l2
[idx
]);
530 iface
->l2
[idx
] = NULL
;
534 res
= sendto(iface
->fd
, own_addr
, ETH_ALEN
, 0,
535 (struct sockaddr
*) from
, fromlen
);
536 wpa_printf(MSG_DEBUG
, "L2 registration[idx=%d]: res=%d", idx
, res
);
540 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface
*iface
,
541 struct sockaddr_un
*from
,
546 for (idx
= 0; idx
< WPA_PRIV_MAX_L2
; idx
++) {
547 if (iface
->l2_addr_len
[idx
] == fromlen
&&
548 os_memcmp(&iface
->l2_addr
[idx
], from
, fromlen
) == 0)
551 if (idx
== WPA_PRIV_MAX_L2
) {
552 wpa_printf(MSG_DEBUG
,
553 "No registered l2_packet socket found for unregister request");
557 if (iface
->l2
[idx
]) {
558 l2_packet_deinit(iface
->l2
[idx
]);
559 iface
->l2
[idx
] = NULL
;
564 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface
*iface
,
565 struct sockaddr_un
*from
)
569 for (idx
= 0; idx
< WPA_PRIV_MAX_L2
; idx
++) {
571 l2_packet_notify_auth_start(iface
->l2
[idx
]);
576 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface
*iface
,
577 struct sockaddr_un
*from
, socklen_t fromlen
,
578 void *buf
, size_t len
)
585 for (idx
= 0; idx
< WPA_PRIV_MAX_L2
; idx
++) {
586 if (iface
->l2_addr_len
[idx
] == fromlen
&&
587 os_memcmp(&iface
->l2_addr
[idx
], from
, fromlen
) == 0)
590 if (idx
== WPA_PRIV_MAX_L2
) {
591 wpa_printf(MSG_DEBUG
,
592 "No registered l2_packet socket found for send request");
596 if (iface
->l2
[idx
] == NULL
)
599 if (len
< ETH_ALEN
+ 2) {
600 wpa_printf(MSG_DEBUG
, "Too short L2 send packet (len=%lu)",
601 (unsigned long) len
);
606 os_memcpy(&proto
, (char *) buf
+ ETH_ALEN
, 2);
608 if (!wpa_priv_allowed_l2_proto(proto
)) {
609 wpa_printf(MSG_DEBUG
, "Refused l2_packet send for ethertype "
614 res
= l2_packet_send(iface
->l2
[idx
], dst_addr
, proto
,
615 (unsigned char *) buf
+ ETH_ALEN
+ 2,
617 wpa_printf(MSG_DEBUG
, "L2 send[idx=%d]: res=%d", idx
, res
);
621 static void wpa_priv_cmd_set_country(struct wpa_priv_interface
*iface
,
624 if (iface
->drv_priv
== NULL
|| iface
->driver
->set_country
== NULL
||
628 iface
->driver
->set_country(iface
->drv_priv
, buf
);
632 static void wpa_priv_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
634 struct wpa_priv_interface
*iface
= eloop_ctx
;
635 char buf
[2000], *pos
;
639 struct sockaddr_un from
;
640 socklen_t fromlen
= sizeof(from
);
642 res
= recvfrom(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &from
,
645 wpa_printf(MSG_ERROR
, "recvfrom: %s", strerror(errno
));
649 if (res
< (int) sizeof(int)) {
650 wpa_printf(MSG_DEBUG
, "Too short command (len=%d)", res
);
654 os_memcpy(&cmd
, buf
, sizeof(int));
655 wpa_printf(MSG_DEBUG
, "Command %d for interface %s",
657 cmd_buf
= &buf
[sizeof(int)];
658 cmd_len
= res
- sizeof(int);
661 case PRIVSEP_CMD_REGISTER
:
662 wpa_priv_cmd_register(iface
, &from
, fromlen
);
664 case PRIVSEP_CMD_UNREGISTER
:
665 wpa_priv_cmd_unregister(iface
, &from
);
667 case PRIVSEP_CMD_SCAN
:
668 wpa_priv_cmd_scan(iface
, cmd_buf
, cmd_len
);
670 case PRIVSEP_CMD_GET_SCAN_RESULTS
:
671 wpa_priv_cmd_get_scan_results(iface
, &from
, fromlen
);
673 case PRIVSEP_CMD_ASSOCIATE
:
674 wpa_priv_cmd_associate(iface
, cmd_buf
, cmd_len
);
676 case PRIVSEP_CMD_GET_BSSID
:
677 wpa_priv_cmd_get_bssid(iface
, &from
, fromlen
);
679 case PRIVSEP_CMD_GET_SSID
:
680 wpa_priv_cmd_get_ssid(iface
, &from
, fromlen
);
682 case PRIVSEP_CMD_SET_KEY
:
683 wpa_priv_cmd_set_key(iface
, cmd_buf
, cmd_len
);
685 case PRIVSEP_CMD_GET_CAPA
:
686 wpa_priv_cmd_get_capa(iface
, &from
, fromlen
);
688 case PRIVSEP_CMD_L2_REGISTER
:
689 wpa_priv_cmd_l2_register(iface
, &from
, fromlen
,
692 case PRIVSEP_CMD_L2_UNREGISTER
:
693 wpa_priv_cmd_l2_unregister(iface
, &from
, fromlen
);
695 case PRIVSEP_CMD_L2_NOTIFY_AUTH_START
:
696 wpa_priv_cmd_l2_notify_auth_start(iface
, &from
);
698 case PRIVSEP_CMD_L2_SEND
:
699 wpa_priv_cmd_l2_send(iface
, &from
, fromlen
, cmd_buf
, cmd_len
);
701 case PRIVSEP_CMD_SET_COUNTRY
:
703 if (pos
+ cmd_len
>= buf
+ sizeof(buf
))
706 wpa_priv_cmd_set_country(iface
, pos
);
708 case PRIVSEP_CMD_AUTHENTICATE
:
709 wpa_priv_cmd_authenticate(iface
, cmd_buf
, cmd_len
);
715 static void wpa_priv_interface_deinit(struct wpa_priv_interface
*iface
)
719 if (iface
->drv_priv
) {
720 if (iface
->driver
->deinit
)
721 iface
->driver
->deinit(iface
->drv_priv
);
722 if (iface
->drv_global_priv
)
723 iface
->driver
->global_deinit(iface
->drv_global_priv
);
726 if (iface
->fd
>= 0) {
727 eloop_unregister_read_sock(iface
->fd
);
729 unlink(iface
->sock_name
);
732 for (i
= 0; i
< WPA_PRIV_MAX_L2
; i
++) {
734 l2_packet_deinit(iface
->l2
[i
]);
737 os_free(iface
->ifname
);
738 os_free(iface
->driver_name
);
739 os_free(iface
->sock_name
);
744 static struct wpa_priv_interface
*
745 wpa_priv_interface_init(void *ctx
, const char *dir
, const char *params
)
747 struct wpa_priv_interface
*iface
;
750 struct sockaddr_un addr
;
753 pos
= os_strchr(params
, ':');
757 iface
= os_zalloc(sizeof(*iface
));
764 iface
->driver_name
= dup_binstr(params
, len
);
765 if (iface
->driver_name
== NULL
) {
766 wpa_priv_interface_deinit(iface
);
770 for (i
= 0; wpa_drivers
[i
]; i
++) {
771 if (os_strcmp(iface
->driver_name
,
772 wpa_drivers
[i
]->name
) == 0) {
773 iface
->driver
= wpa_drivers
[i
];
777 if (iface
->driver
== NULL
) {
778 wpa_printf(MSG_ERROR
, "Unsupported driver '%s'",
780 wpa_priv_interface_deinit(iface
);
785 iface
->ifname
= os_strdup(pos
);
786 if (iface
->ifname
== NULL
) {
787 wpa_priv_interface_deinit(iface
);
791 len
= os_strlen(dir
) + 1 + os_strlen(iface
->ifname
);
792 iface
->sock_name
= os_malloc(len
+ 1);
793 if (iface
->sock_name
== NULL
) {
794 wpa_priv_interface_deinit(iface
);
798 os_snprintf(iface
->sock_name
, len
+ 1, "%s/%s", dir
, iface
->ifname
);
799 if (os_strlen(iface
->sock_name
) >= sizeof(addr
.sun_path
)) {
800 wpa_priv_interface_deinit(iface
);
804 iface
->fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
806 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
807 wpa_priv_interface_deinit(iface
);
811 os_memset(&addr
, 0, sizeof(addr
));
812 addr
.sun_family
= AF_UNIX
;
813 os_strlcpy(addr
.sun_path
, iface
->sock_name
, sizeof(addr
.sun_path
));
815 if (bind(iface
->fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
816 wpa_printf(MSG_DEBUG
, "bind(PF_UNIX) failed: %s",
818 if (connect(iface
->fd
, (struct sockaddr
*) &addr
,
820 wpa_printf(MSG_DEBUG
, "Socket exists, but does not "
821 "allow connections - assuming it was "
822 "leftover from forced program termination");
823 if (unlink(iface
->sock_name
) < 0) {
824 wpa_printf(MSG_ERROR
,
825 "Could not unlink existing ctrl_iface socket '%s': %s",
826 iface
->sock_name
, strerror(errno
));
829 if (bind(iface
->fd
, (struct sockaddr
*) &addr
,
831 wpa_printf(MSG_ERROR
,
832 "wpa-priv-iface-init: bind(PF_UNIX): %s",
836 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
837 "socket '%s'", iface
->sock_name
);
839 wpa_printf(MSG_INFO
, "Socket exists and seems to be "
840 "in use - cannot override it");
841 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
842 "not used anymore", iface
->sock_name
);
847 if (chmod(iface
->sock_name
, S_IRWXU
| S_IRWXG
| S_IRWXO
) < 0) {
848 wpa_printf(MSG_ERROR
, "chmod: %s", strerror(errno
));
852 eloop_register_read_sock(iface
->fd
, wpa_priv_receive
, iface
, NULL
);
857 wpa_priv_interface_deinit(iface
);
862 static int wpa_priv_send_event(struct wpa_priv_interface
*iface
, int event
,
863 const void *data
, size_t data_len
)
868 io
[0].iov_base
= &event
;
869 io
[0].iov_len
= sizeof(event
);
870 io
[1].iov_base
= (u8
*) data
;
871 io
[1].iov_len
= data_len
;
873 os_memset(&msg
, 0, sizeof(msg
));
875 msg
.msg_iovlen
= data
? 2 : 1;
876 msg
.msg_name
= &iface
->drv_addr
;
877 msg
.msg_namelen
= iface
->drv_addr_len
;
879 if (sendmsg(iface
->fd
, &msg
, 0) < 0) {
880 wpa_printf(MSG_ERROR
, "sendmsg(wpas_socket): %s",
889 static void wpa_priv_send_auth(struct wpa_priv_interface
*iface
,
890 union wpa_event_data
*data
)
892 size_t buflen
= sizeof(struct privsep_event_auth
) + data
->auth
.ies_len
;
893 struct privsep_event_auth
*auth
;
896 buf
= os_zalloc(buflen
);
900 auth
= (struct privsep_event_auth
*) buf
;
901 pos
= (u8
*) (auth
+ 1);
903 os_memcpy(auth
->peer
, data
->auth
.peer
, ETH_ALEN
);
904 os_memcpy(auth
->bssid
, data
->auth
.bssid
, ETH_ALEN
);
905 auth
->auth_type
= data
->auth
.auth_type
;
906 auth
->auth_transaction
= data
->auth
.auth_transaction
;
907 auth
->status_code
= data
->auth
.status_code
;
908 if (data
->auth
.ies
) {
909 os_memcpy(pos
, data
->auth
.ies
, data
->auth
.ies_len
);
910 auth
->ies_len
= data
->auth
.ies_len
;
913 wpa_priv_send_event(iface
, PRIVSEP_EVENT_AUTH
, buf
, buflen
);
919 static void wpa_priv_send_assoc(struct wpa_priv_interface
*iface
, int event
,
920 union wpa_event_data
*data
)
922 size_t buflen
= 3 * sizeof(int);
927 buflen
+= data
->assoc_info
.req_ies_len
+
928 data
->assoc_info
.resp_ies_len
+
929 data
->assoc_info
.beacon_ies_len
;
932 buf
= os_malloc(buflen
);
938 if (data
&& data
->assoc_info
.req_ies
) {
939 len
= data
->assoc_info
.req_ies_len
;
940 os_memcpy(pos
, &len
, sizeof(int));
942 os_memcpy(pos
, data
->assoc_info
.req_ies
, len
);
946 os_memcpy(pos
, &len
, sizeof(int));
950 if (data
&& data
->assoc_info
.resp_ies
) {
951 len
= data
->assoc_info
.resp_ies_len
;
952 os_memcpy(pos
, &len
, sizeof(int));
954 os_memcpy(pos
, data
->assoc_info
.resp_ies
, len
);
958 os_memcpy(pos
, &len
, sizeof(int));
962 if (data
&& data
->assoc_info
.beacon_ies
) {
963 len
= data
->assoc_info
.beacon_ies_len
;
964 os_memcpy(pos
, &len
, sizeof(int));
966 os_memcpy(pos
, data
->assoc_info
.beacon_ies
, len
);
970 os_memcpy(pos
, &len
, sizeof(int));
974 wpa_priv_send_event(iface
, event
, buf
, buflen
);
980 static void wpa_priv_send_interface_status(struct wpa_priv_interface
*iface
,
981 union wpa_event_data
*data
)
991 ievent
= data
->interface_status
.ievent
;
992 maxlen
= sizeof(data
->interface_status
.ifname
);
993 ifname
= data
->interface_status
.ifname
;
994 for (len
= 0; len
< maxlen
&& ifname
[len
]; len
++)
997 buf
= os_malloc(sizeof(int) + len
);
1001 os_memcpy(buf
, &ievent
, sizeof(int));
1002 os_memcpy(buf
+ sizeof(int), ifname
, len
);
1004 wpa_priv_send_event(iface
, PRIVSEP_EVENT_INTERFACE_STATUS
,
1005 buf
, sizeof(int) + len
);
1012 static void wpa_priv_send_ft_response(struct wpa_priv_interface
*iface
,
1013 union wpa_event_data
*data
)
1018 if (data
== NULL
|| data
->ft_ies
.ies
== NULL
)
1021 len
= sizeof(int) + ETH_ALEN
+ data
->ft_ies
.ies_len
;
1022 buf
= os_malloc(len
);
1027 os_memcpy(pos
, &data
->ft_ies
.ft_action
, sizeof(int));
1029 os_memcpy(pos
, data
->ft_ies
.target_ap
, ETH_ALEN
);
1031 os_memcpy(pos
, data
->ft_ies
.ies
, data
->ft_ies
.ies_len
);
1033 wpa_priv_send_event(iface
, PRIVSEP_EVENT_FT_RESPONSE
, buf
, len
);
1040 void wpa_supplicant_event(void *ctx
, enum wpa_event_type event
,
1041 union wpa_event_data
*data
)
1043 struct wpa_priv_interface
*iface
= ctx
;
1045 wpa_printf(MSG_DEBUG
, "%s - event=%d", __func__
, event
);
1047 if (!iface
->wpas_registered
) {
1048 wpa_printf(MSG_DEBUG
, "Driver event received, but "
1049 "wpa_supplicant not registered");
1055 wpa_priv_send_assoc(iface
, PRIVSEP_EVENT_ASSOC
, data
);
1057 case EVENT_DISASSOC
:
1058 wpa_priv_send_event(iface
, PRIVSEP_EVENT_DISASSOC
, NULL
, 0);
1060 case EVENT_ASSOCINFO
:
1063 wpa_priv_send_assoc(iface
, PRIVSEP_EVENT_ASSOCINFO
, data
);
1065 case EVENT_MICHAEL_MIC_FAILURE
:
1068 wpa_priv_send_event(iface
, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE
,
1069 &data
->michael_mic_failure
.unicast
,
1072 case EVENT_SCAN_STARTED
:
1073 wpa_priv_send_event(iface
, PRIVSEP_EVENT_SCAN_STARTED
, NULL
,
1076 case EVENT_SCAN_RESULTS
:
1077 wpa_priv_send_event(iface
, PRIVSEP_EVENT_SCAN_RESULTS
, NULL
,
1080 case EVENT_INTERFACE_STATUS
:
1081 wpa_priv_send_interface_status(iface
, data
);
1083 case EVENT_PMKID_CANDIDATE
:
1086 wpa_priv_send_event(iface
, PRIVSEP_EVENT_PMKID_CANDIDATE
,
1087 &data
->pmkid_candidate
,
1088 sizeof(struct pmkid_candidate
));
1090 case EVENT_FT_RESPONSE
:
1091 wpa_priv_send_ft_response(iface
, data
);
1094 wpa_priv_send_auth(iface
, data
);
1097 wpa_printf(MSG_DEBUG
, "Unsupported driver event %d (%s) - TODO",
1098 event
, event_to_string(event
));
1104 void wpa_supplicant_event_global(void *ctx
, enum wpa_event_type event
,
1105 union wpa_event_data
*data
)
1107 struct wpa_priv_global
*global
= ctx
;
1108 struct wpa_priv_interface
*iface
;
1110 if (event
!= EVENT_INTERFACE_STATUS
)
1113 for (iface
= global
->interfaces
; iface
; iface
= iface
->next
) {
1114 if (os_strcmp(iface
->ifname
, data
->interface_status
.ifname
) ==
1118 if (iface
&& iface
->driver
->get_ifindex
) {
1119 unsigned int ifindex
;
1121 ifindex
= iface
->driver
->get_ifindex(iface
->drv_priv
);
1122 if (ifindex
!= data
->interface_status
.ifindex
) {
1123 wpa_printf(MSG_DEBUG
,
1124 "%s: interface status ifindex %d mismatch (%d)",
1125 iface
->ifname
, ifindex
,
1126 data
->interface_status
.ifindex
);
1131 wpa_supplicant_event(iface
, event
, data
);
1135 void wpa_supplicant_rx_eapol(void *ctx
, const u8
*src_addr
,
1136 const u8
*buf
, size_t len
)
1138 struct wpa_priv_interface
*iface
= ctx
;
1141 int event
= PRIVSEP_EVENT_RX_EAPOL
;
1143 wpa_printf(MSG_DEBUG
, "RX EAPOL from driver");
1144 io
[0].iov_base
= &event
;
1145 io
[0].iov_len
= sizeof(event
);
1146 io
[1].iov_base
= (u8
*) src_addr
;
1147 io
[1].iov_len
= ETH_ALEN
;
1148 io
[2].iov_base
= (u8
*) buf
;
1149 io
[2].iov_len
= len
;
1151 os_memset(&msg
, 0, sizeof(msg
));
1154 msg
.msg_name
= &iface
->drv_addr
;
1155 msg
.msg_namelen
= iface
->drv_addr_len
;
1157 if (sendmsg(iface
->fd
, &msg
, 0) < 0)
1158 wpa_printf(MSG_ERROR
, "sendmsg(wpas_socket): %s",
1163 static void wpa_priv_terminate(int sig
, void *signal_ctx
)
1165 wpa_printf(MSG_DEBUG
, "wpa_priv termination requested");
1170 static void wpa_priv_fd_workaround(void)
1174 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
1175 * fd 0, 1, and 2 closed. This will cause some issues because many
1176 * places in wpa_supplicant are still printing out to stdout. As a
1177 * workaround, make sure that fd's 0, 1, and 2 are not used for other
1179 for (i
= 0; i
< 3; i
++) {
1180 s
= open("/dev/null", O_RDWR
);
1186 #endif /* __linux__ */
1190 static void usage(void)
1192 printf("wpa_priv v%s\n"
1193 "Copyright (c) 2007-2017, Jouni Malinen <j@w1.fi> and "
1197 " wpa_priv [-Bdd] [-c<ctrl dir>] [-P<pid file>] "
1198 "<driver:ifname> \\\n"
1199 " [driver:ifname ...]\n",
1204 int main(int argc
, char *argv
[])
1208 char *pid_file
= NULL
;
1210 char *ctrl_dir
= "/var/run/wpa_priv";
1211 struct wpa_priv_global global
;
1212 struct wpa_priv_interface
*iface
;
1214 if (os_program_init())
1217 wpa_priv_fd_workaround();
1219 os_memset(&global
, 0, sizeof(global
));
1220 global
.interfaces
= NULL
;
1223 c
= getopt(argc
, argv
, "Bc:dP:");
1237 pid_file
= os_rel2abs_path(optarg
);
1245 if (optind
>= argc
) {
1250 wpa_printf(MSG_DEBUG
, "wpa_priv control directory: '%s'", ctrl_dir
);
1253 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1257 for (i
= optind
; i
< argc
; i
++) {
1258 wpa_printf(MSG_DEBUG
, "Adding driver:interface %s", argv
[i
]);
1259 iface
= wpa_priv_interface_init(&global
, ctrl_dir
, argv
[i
]);
1262 iface
->next
= global
.interfaces
;
1263 global
.interfaces
= iface
;
1266 if (daemonize
&& os_daemonize(pid_file
) && eloop_sock_requeue())
1269 eloop_register_signal_terminate(wpa_priv_terminate
, NULL
);
1275 iface
= global
.interfaces
;
1277 struct wpa_priv_interface
*prev
= iface
;
1278 iface
= iface
->next
;
1279 wpa_priv_interface_deinit(prev
);
1286 os_daemonize_terminate(pid_file
);
1288 os_program_deinit();