2 * WPA Supplicant - Layer2 packet handling with WinPcap RX thread
3 * Copyright (c) 2003-2006, 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.
14 * This l2_packet implementation is explicitly for WinPcap and Windows events.
15 * l2_packet_pcap.c has support for WinPcap, but it requires polling to receive
16 * frames which means relatively long latency for EAPOL RX processing. The
17 * implementation here uses a separate thread to allow WinPcap to be receiving
18 * all the time to reduce latency for EAPOL receiving from about 100 ms to 3 ms
19 * when comparing l2_packet_pcap.c to l2_packet_winpcap.c. Extra sleep of 50 ms
20 * is added in to receive thread whenever no EAPOL frames has been received for
21 * a while. Whenever an EAPOL handshake is expected, this sleep is removed.
23 * The RX thread receives a frame and signals main thread through Windows event
24 * about the availability of a new frame. Processing the received frame is
25 * synchronized with pair of Windows events so that no extra buffer or queuing
26 * mechanism is needed. This implementation requires Windows specific event
27 * loop implementation, i.e., eloop_win.c.
29 * WinPcap has pcap_getevent() that could, in theory at least, be used to
30 * implement this kind of waiting with a simpler single-thread design. However,
31 * that event handle is not really signaled immediately when receiving each
32 * frame, so it does not really work for this kind of use.
40 #include "l2_packet.h"
43 static const u8 pae_group_addr
[ETH_ALEN
] =
44 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
47 * Number of pcap_dispatch() iterations to do without extra wait after each
48 * received EAPOL packet or authentication notification. This is used to reduce
49 * latency for EAPOL receive.
51 static const size_t no_wait_count
= 750;
53 struct l2_packet_data
{
55 unsigned int num_fast_poll
;
57 u8 own_addr
[ETH_ALEN
];
58 void (*rx_callback
)(void *ctx
, const u8
*src_addr
,
59 const u8
*buf
, size_t len
);
60 void *rx_callback_ctx
;
61 int l2_hdr
; /* whether to include layer 2 (Ethernet) header in calls to
62 * rx_callback and l2_packet_send() */
64 HANDLE rx_avail
, rx_done
, rx_thread
, rx_thread_done
, rx_notify
;
71 int l2_packet_get_own_addr(struct l2_packet_data
*l2
, u8
*addr
)
73 os_memcpy(addr
, l2
->own_addr
, ETH_ALEN
);
78 int l2_packet_send(struct l2_packet_data
*l2
, const u8
*dst_addr
, u16 proto
,
79 const u8
*buf
, size_t len
)
82 struct l2_ethhdr
*eth
;
88 ret
= pcap_sendpacket(l2
->pcap
, buf
, len
);
90 size_t mlen
= sizeof(*eth
) + len
;
91 eth
= os_malloc(mlen
);
95 os_memcpy(eth
->h_dest
, dst_addr
, ETH_ALEN
);
96 os_memcpy(eth
->h_source
, l2
->own_addr
, ETH_ALEN
);
97 eth
->h_proto
= htons(proto
);
98 os_memcpy(eth
+ 1, buf
, len
);
99 ret
= pcap_sendpacket(l2
->pcap
, (u8
*) eth
, mlen
);
107 /* pcap_dispatch() callback for the RX thread */
108 static void l2_packet_receive_cb(u_char
*user
, const struct pcap_pkthdr
*hdr
,
109 const u_char
*pkt_data
)
111 struct l2_packet_data
*l2
= (struct l2_packet_data
*) user
;
112 struct l2_ethhdr
*ethhdr
;
114 if (pkt_data
== NULL
|| hdr
->caplen
< sizeof(*ethhdr
))
117 ethhdr
= (struct l2_ethhdr
*) pkt_data
;
119 l2
->rx_buf
= (u8
*) ethhdr
;
120 l2
->rx_len
= hdr
->caplen
;
122 l2
->rx_buf
= (u8
*) (ethhdr
+ 1);
123 l2
->rx_len
= hdr
->caplen
- sizeof(*ethhdr
);
125 l2
->rx_src
= ethhdr
->h_source
;
126 SetEvent(l2
->rx_avail
);
127 WaitForSingleObject(l2
->rx_done
, INFINITE
);
128 ResetEvent(l2
->rx_done
);
129 l2
->rx_no_wait
= no_wait_count
;
133 /* main RX loop that is running in a separate thread */
134 static DWORD WINAPI
l2_packet_receive_thread(LPVOID arg
)
136 struct l2_packet_data
*l2
= arg
;
138 while (l2
->running
) {
139 pcap_dispatch(l2
->pcap
, 1, l2_packet_receive_cb
,
141 if (l2
->rx_no_wait
> 0)
143 if (WaitForSingleObject(l2
->rx_notify
,
144 l2
->rx_no_wait
? 0 : 50) ==
146 l2
->rx_no_wait
= no_wait_count
;
147 ResetEvent(l2
->rx_notify
);
150 SetEvent(l2
->rx_thread_done
);
156 /* main thread RX event handler */
157 static void l2_packet_rx_event(void *eloop_data
, void *user_data
)
159 struct l2_packet_data
*l2
= eloop_data
;
160 l2
->rx_callback(l2
->rx_callback_ctx
, l2
->rx_src
, l2
->rx_buf
,
162 ResetEvent(l2
->rx_avail
);
163 SetEvent(l2
->rx_done
);
167 static int l2_packet_init_libpcap(struct l2_packet_data
*l2
,
168 unsigned short protocol
)
170 bpf_u_int32 pcap_maskp
, pcap_netp
;
171 char pcap_filter
[200], pcap_err
[PCAP_ERRBUF_SIZE
];
172 struct bpf_program pcap_fp
;
174 pcap_lookupnet(l2
->ifname
, &pcap_netp
, &pcap_maskp
, pcap_err
);
175 l2
->pcap
= pcap_open_live(l2
->ifname
, 2500, 0, 1, pcap_err
);
176 if (l2
->pcap
== NULL
) {
177 fprintf(stderr
, "pcap_open_live: %s\n", pcap_err
);
178 fprintf(stderr
, "ifname='%s'\n", l2
->ifname
);
181 os_snprintf(pcap_filter
, sizeof(pcap_filter
),
182 "not ether src " MACSTR
" and "
183 "( ether dst " MACSTR
" or ether dst " MACSTR
" ) and "
185 MAC2STR(l2
->own_addr
), /* do not receive own packets */
186 MAC2STR(l2
->own_addr
), MAC2STR(pae_group_addr
),
188 if (pcap_compile(l2
->pcap
, &pcap_fp
, pcap_filter
, 1, pcap_netp
) < 0) {
189 fprintf(stderr
, "pcap_compile: %s\n", pcap_geterr(l2
->pcap
));
193 if (pcap_setfilter(l2
->pcap
, &pcap_fp
) < 0) {
194 fprintf(stderr
, "pcap_setfilter: %s\n", pcap_geterr(l2
->pcap
));
198 pcap_freecode(&pcap_fp
);
204 struct l2_packet_data
* l2_packet_init(
205 const char *ifname
, const u8
*own_addr
, unsigned short protocol
,
206 void (*rx_callback
)(void *ctx
, const u8
*src_addr
,
207 const u8
*buf
, size_t len
),
208 void *rx_callback_ctx
, int l2_hdr
)
210 struct l2_packet_data
*l2
;
213 l2
= os_zalloc(sizeof(struct l2_packet_data
));
216 if (os_strncmp(ifname
, "\\Device\\NPF_", 12) == 0)
217 os_strlcpy(l2
->ifname
, ifname
, sizeof(l2
->ifname
));
219 os_snprintf(l2
->ifname
, sizeof(l2
->ifname
), "\\Device\\NPF_%s",
221 l2
->rx_callback
= rx_callback
;
222 l2
->rx_callback_ctx
= rx_callback_ctx
;
226 os_memcpy(l2
->own_addr
, own_addr
, ETH_ALEN
);
228 if (l2_packet_init_libpcap(l2
, protocol
)) {
233 l2
->rx_avail
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
234 l2
->rx_done
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
235 l2
->rx_notify
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
236 if (l2
->rx_avail
== NULL
|| l2
->rx_done
== NULL
||
237 l2
->rx_notify
== NULL
) {
238 CloseHandle(l2
->rx_avail
);
239 CloseHandle(l2
->rx_done
);
240 CloseHandle(l2
->rx_notify
);
241 pcap_close(l2
->pcap
);
246 eloop_register_event(l2
->rx_avail
, sizeof(l2
->rx_avail
),
247 l2_packet_rx_event
, l2
, NULL
);
250 l2
->rx_thread
= CreateThread(NULL
, 0, l2_packet_receive_thread
, l2
, 0,
257 static void l2_packet_deinit_timeout(void *eloop_ctx
, void *timeout_ctx
)
259 struct l2_packet_data
*l2
= eloop_ctx
;
261 if (l2
->rx_thread_done
&&
262 WaitForSingleObject(l2
->rx_thread_done
, 2000) != WAIT_OBJECT_0
) {
263 wpa_printf(MSG_DEBUG
, "l2_packet_winpcap: RX thread did not "
265 TerminateThread(l2
->rx_thread
, 0);
267 CloseHandle(l2
->rx_thread_done
);
268 CloseHandle(l2
->rx_thread
);
270 pcap_close(l2
->pcap
);
271 eloop_unregister_event(l2
->rx_avail
, sizeof(l2
->rx_avail
));
272 CloseHandle(l2
->rx_avail
);
273 CloseHandle(l2
->rx_done
);
274 CloseHandle(l2
->rx_notify
);
279 void l2_packet_deinit(struct l2_packet_data
*l2
)
284 l2
->rx_thread_done
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
287 pcap_breakloop(l2
->pcap
);
290 * RX thread may be waiting in l2_packet_receive_cb() for l2->rx_done
291 * event and this event is set in l2_packet_rx_event(). However,
292 * l2_packet_deinit() may end up being called from l2->rx_callback(),
293 * so we need to return from here and complete deinitialization in
294 * a registered timeout to avoid having to forcefully kill the RX
297 eloop_register_timeout(0, 0, l2_packet_deinit_timeout
, l2
, NULL
);
301 int l2_packet_get_ip_addr(struct l2_packet_data
*l2
, char *buf
, size_t len
)
303 pcap_if_t
*devs
, *dev
;
304 struct pcap_addr
*addr
;
305 struct sockaddr_in
*saddr
;
307 char err
[PCAP_ERRBUF_SIZE
+ 1];
309 if (pcap_findalldevs(&devs
, err
) < 0) {
310 wpa_printf(MSG_DEBUG
, "pcap_findalldevs: %s\n", err
);
314 for (dev
= devs
; dev
&& !found
; dev
= dev
->next
) {
315 if (os_strcmp(dev
->name
, l2
->ifname
) != 0)
318 addr
= dev
->addresses
;
320 saddr
= (struct sockaddr_in
*) addr
->addr
;
321 if (saddr
&& saddr
->sin_family
== AF_INET
) {
322 os_strlcpy(buf
, inet_ntoa(saddr
->sin_addr
),
331 pcap_freealldevs(devs
);
333 return found
? 0 : -1;
337 void l2_packet_notify_auth_start(struct l2_packet_data
*l2
)
339 SetEvent(l2
->rx_notify
);