2 * WPA Supplicant - Layer2 packet handling with Microsoft NDISUIO
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 implementation requires Windows specific event loop implementation,
15 * i.e., eloop_win.c. In addition, the NDISUIO connection is shared with
16 * driver_ndis.c, so only that driver interface can be used and
17 * CONFIG_USE_NDISUIO must be defined.
19 * WinXP version of the code uses overlapped I/O and a single threaded design
20 * with callback functions from I/O code. WinCE version uses a separate RX
21 * thread that blocks on ReadFile() whenever the media status is connected.
31 #endif /* _WIN32_WCE */
35 #include "l2_packet.h"
39 #define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK
40 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
41 CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
42 #define IOCTL_NDISUIO_SET_ETHER_TYPE \
43 _NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \
44 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
45 #endif /* _WIN32_WCE */
47 /* From driver_ndis.c to shared the handle to NDISUIO */
48 HANDLE
driver_ndis_get_ndisuio_handle(void);
51 * NDISUIO supports filtering of only one ethertype at the time, so we must
52 * fake support for two (EAPOL and RSN pre-auth) by switching to pre-auth
53 * whenever wpa_supplicant is trying to pre-authenticate and then switching
54 * back to EAPOL when pre-authentication has been completed.
57 struct l2_packet_data
;
59 struct l2_packet_ndisuio_global
{
61 unsigned short first_proto
;
62 struct l2_packet_data
*l2
[2];
66 HANDLE ready_for_read
;
68 #endif /* _WIN32_WCE */
71 static struct l2_packet_ndisuio_global
*l2_ndisuio_global
= NULL
;
73 struct l2_packet_data
{
75 u8 own_addr
[ETH_ALEN
];
76 void (*rx_callback
)(void *ctx
, const u8
*src_addr
,
77 const u8
*buf
, size_t len
);
78 void *rx_callback_ctx
;
79 int l2_hdr
; /* whether to include layer 2 (Ethernet) header in calls to
80 * rx_callback and l2_packet_send() */
83 OVERLAPPED rx_overlapped
;
84 #endif /* _WIN32_WCE */
90 int l2_packet_get_own_addr(struct l2_packet_data
*l2
, u8
*addr
)
92 os_memcpy(addr
, l2
->own_addr
, ETH_ALEN
);
97 int l2_packet_send(struct l2_packet_data
*l2
, const u8
*dst_addr
, u16 proto
,
98 const u8
*buf
, size_t len
)
102 struct l2_ethhdr
*eth
;
104 OVERLAPPED overlapped
;
105 #endif /* _WIN32_WCE */
113 #else /* _WIN32_WCE */
114 os_memset(&overlapped
, 0, sizeof(overlapped
));
116 #endif /* _WIN32_WCE */
119 res
= WriteFile(driver_ndis_get_ndisuio_handle(), buf
, len
,
122 size_t mlen
= sizeof(*eth
) + len
;
123 eth
= os_malloc(mlen
);
127 os_memcpy(eth
->h_dest
, dst_addr
, ETH_ALEN
);
128 os_memcpy(eth
->h_source
, l2
->own_addr
, ETH_ALEN
);
129 eth
->h_proto
= htons(proto
);
130 os_memcpy(eth
+ 1, buf
, len
);
131 res
= WriteFile(driver_ndis_get_ndisuio_handle(), eth
, mlen
,
137 DWORD err
= GetLastError();
139 if (err
== ERROR_IO_PENDING
) {
140 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): Wait for pending "
141 "write to complete");
142 res
= GetOverlappedResult(
143 driver_ndis_get_ndisuio_handle(), &overlapped
,
146 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): "
147 "GetOverlappedResult failed: %d",
148 (int) GetLastError());
153 #endif /* _WIN32_WCE */
154 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): WriteFile failed: %d",
155 (int) GetLastError());
163 static void l2_packet_callback(struct l2_packet_data
*l2
);
166 static void l2_packet_rx_thread_try_read(struct l2_packet_data
*l2
)
170 wpa_printf(MSG_MSGDUMP
, "l2_packet_rx_thread: -> ReadFile");
171 if (!ReadFile(driver_ndis_get_ndisuio_handle(), l2
->rx_buf
,
172 sizeof(l2
->rx_buf
), &l2
->rx_written
, NULL
)) {
173 DWORD err
= GetLastError();
174 wpa_printf(MSG_DEBUG
, "l2_packet_rx_thread: ReadFile failed: "
177 * ReadFile on NDISUIO/WinCE returns ERROR_DEVICE_NOT_CONNECTED
178 * error whenever the connection is not up. Yield the thread to
179 * avoid triggering a busy loop. Connection event should stop
180 * us from looping for long, but we need to allow enough CPU
181 * for the main thread to process the media disconnection.
187 wpa_printf(MSG_DEBUG
, "l2_packet_rx_thread: Read %d byte packet",
188 (int) l2
->rx_written
);
191 * Notify the main thread about the availability of a frame and wait
192 * for the frame to be processed.
194 SetEvent(l2
->rx_avail
);
195 handles
[0] = l2_ndisuio_global
->stop_request
;
196 handles
[1] = l2_ndisuio_global
->rx_processed
;
197 WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
198 ResetEvent(l2_ndisuio_global
->rx_processed
);
202 static DWORD WINAPI
l2_packet_rx_thread(LPVOID arg
)
204 struct l2_packet_data
*l2
= arg
;
209 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): RX thread started");
210 handles
[0] = l2_ndisuio_global
->stop_request
;
211 handles
[1] = l2_ndisuio_global
->ready_for_read
;
214 * Unfortunately, NDISUIO on WinCE does not seem to support waiting
215 * on the handle. There do not seem to be anything else that we could
216 * wait for either. If one were to modify NDISUIO to set a named event
217 * whenever packets are available, this event could be used here to
218 * avoid having to poll for new packets or we could even move to use a
219 * single threaded design.
221 * In addition, NDISUIO on WinCE is returning
222 * ERROR_DEVICE_NOT_CONNECTED whenever ReadFile() is attempted while
223 * the adapter is not in connected state. For now, we are just using a
224 * local event to allow ReadFile calls only after having received NDIS
225 * media connect event. This event could be easily converted to handle
226 * another event if the protocol driver is replaced with somewhat more
230 while (l2_ndisuio_global
&& run
) {
231 res
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
234 wpa_printf(MSG_DEBUG
, "l2_packet_rx_thread: Received "
235 "request to stop RX thread");
238 case WAIT_OBJECT_0
+ 1:
239 l2_packet_rx_thread_try_read(l2
);
243 wpa_printf(MSG_DEBUG
, "l2_packet_rx_thread: "
244 "WaitForMultipleObjects failed: %d",
245 (int) GetLastError());
251 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): RX thread stopped");
255 #else /* _WIN32_WCE */
256 static int l2_ndisuio_start_read(struct l2_packet_data
*l2
, int recursive
)
258 os_memset(&l2
->rx_overlapped
, 0, sizeof(l2
->rx_overlapped
));
259 l2
->rx_overlapped
.hEvent
= l2
->rx_avail
;
260 if (!ReadFile(driver_ndis_get_ndisuio_handle(), l2
->rx_buf
,
261 sizeof(l2
->rx_buf
), &l2
->rx_written
, &l2
->rx_overlapped
))
263 DWORD err
= GetLastError();
264 if (err
!= ERROR_IO_PENDING
) {
265 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): ReadFile failed: "
270 * Once read is completed, l2_packet_rx_event() will be
274 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): ReadFile returned data "
275 "without wait for completion");
277 l2_packet_callback(l2
);
282 #endif /* _WIN32_WCE */
285 static void l2_packet_callback(struct l2_packet_data
*l2
)
287 const u8
*rx_buf
, *rx_src
;
289 struct l2_ethhdr
*ethhdr
= (struct l2_ethhdr
*) l2
->rx_buf
;
291 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): Read %d bytes",
292 (int) l2
->rx_written
);
294 if (l2
->l2_hdr
|| l2
->rx_written
< sizeof(*ethhdr
)) {
295 rx_buf
= (u8
*) ethhdr
;
296 rx_len
= l2
->rx_written
;
298 rx_buf
= (u8
*) (ethhdr
+ 1);
299 rx_len
= l2
->rx_written
- sizeof(*ethhdr
);
301 rx_src
= ethhdr
->h_source
;
303 l2
->rx_callback(l2
->rx_callback_ctx
, rx_src
, rx_buf
, rx_len
);
305 l2_ndisuio_start_read(l2
, 1);
306 #endif /* _WIN32_WCE */
310 static void l2_packet_rx_event(void *eloop_data
, void *user_data
)
312 struct l2_packet_data
*l2
= eloop_data
;
314 if (l2_ndisuio_global
)
315 l2
= l2_ndisuio_global
->l2
[l2_ndisuio_global
->refcount
- 1];
317 ResetEvent(l2
->rx_avail
);
320 if (!GetOverlappedResult(driver_ndis_get_ndisuio_handle(),
321 &l2
->rx_overlapped
, &l2
->rx_written
, FALSE
)) {
322 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): GetOverlappedResult "
323 "failed: %d", (int) GetLastError());
326 #endif /* _WIN32_WCE */
328 l2_packet_callback(l2
);
331 SetEvent(l2_ndisuio_global
->rx_processed
);
332 #endif /* _WIN32_WCE */
336 static int l2_ndisuio_set_ether_type(unsigned short protocol
)
338 USHORT proto
= htons(protocol
);
341 if (!DeviceIoControl(driver_ndis_get_ndisuio_handle(),
342 IOCTL_NDISUIO_SET_ETHER_TYPE
, &proto
,
343 sizeof(proto
), NULL
, 0, &written
, NULL
)) {
344 wpa_printf(MSG_ERROR
, "L2(NDISUIO): "
345 "IOCTL_NDISUIO_SET_ETHER_TYPE failed: %d",
346 (int) GetLastError());
354 struct l2_packet_data
* l2_packet_init(
355 const char *ifname
, const u8
*own_addr
, unsigned short protocol
,
356 void (*rx_callback
)(void *ctx
, const u8
*src_addr
,
357 const u8
*buf
, size_t len
),
358 void *rx_callback_ctx
, int l2_hdr
)
360 struct l2_packet_data
*l2
;
362 if (l2_ndisuio_global
== NULL
) {
363 l2_ndisuio_global
= os_zalloc(sizeof(*l2_ndisuio_global
));
364 if (l2_ndisuio_global
== NULL
)
366 l2_ndisuio_global
->first_proto
= protocol
;
368 if (l2_ndisuio_global
->refcount
>= 2) {
369 wpa_printf(MSG_ERROR
, "L2(NDISUIO): Not more than two "
370 "simultaneous connections allowed");
373 l2_ndisuio_global
->refcount
++;
375 l2
= os_zalloc(sizeof(struct l2_packet_data
));
378 l2_ndisuio_global
->l2
[l2_ndisuio_global
->refcount
- 1] = l2
;
380 os_strlcpy(l2
->ifname
, ifname
, sizeof(l2
->ifname
));
381 l2
->rx_callback
= rx_callback
;
382 l2
->rx_callback_ctx
= rx_callback_ctx
;
386 os_memcpy(l2
->own_addr
, own_addr
, ETH_ALEN
);
388 if (l2_ndisuio_set_ether_type(protocol
) < 0) {
393 if (l2_ndisuio_global
->refcount
> 1) {
394 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): Temporarily setting "
395 "filtering ethertype to %04x", protocol
);
396 if (l2_ndisuio_global
->l2
[0])
397 l2
->rx_avail
= l2_ndisuio_global
->l2
[0]->rx_avail
;
401 l2
->rx_avail
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
402 if (l2
->rx_avail
== NULL
) {
407 eloop_register_event(l2
->rx_avail
, sizeof(l2
->rx_avail
),
408 l2_packet_rx_event
, l2
, NULL
);
411 l2_ndisuio_global
->stop_request
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
413 * This event is being set based on media connect/disconnect
414 * notifications in driver_ndis.c.
416 l2_ndisuio_global
->ready_for_read
=
417 CreateEvent(NULL
, TRUE
, FALSE
, TEXT("WpaSupplicantConnected"));
418 l2_ndisuio_global
->rx_processed
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
419 if (l2_ndisuio_global
->stop_request
== NULL
||
420 l2_ndisuio_global
->ready_for_read
== NULL
||
421 l2_ndisuio_global
->rx_processed
== NULL
) {
422 if (l2_ndisuio_global
->stop_request
) {
423 CloseHandle(l2_ndisuio_global
->stop_request
);
424 l2_ndisuio_global
->stop_request
= NULL
;
426 if (l2_ndisuio_global
->ready_for_read
) {
427 CloseHandle(l2_ndisuio_global
->ready_for_read
);
428 l2_ndisuio_global
->ready_for_read
= NULL
;
430 if (l2_ndisuio_global
->rx_processed
) {
431 CloseHandle(l2_ndisuio_global
->rx_processed
);
432 l2_ndisuio_global
->rx_processed
= NULL
;
434 eloop_unregister_event(l2
->rx_avail
, sizeof(l2
->rx_avail
));
439 l2_ndisuio_global
->rx_thread
= CreateThread(NULL
, 0,
440 l2_packet_rx_thread
, l2
, 0,
442 if (l2_ndisuio_global
->rx_thread
== NULL
) {
443 wpa_printf(MSG_INFO
, "L2(NDISUIO): Failed to create RX "
444 "thread: %d", (int) GetLastError());
445 eloop_unregister_event(l2
->rx_avail
, sizeof(l2
->rx_avail
));
446 CloseHandle(l2_ndisuio_global
->stop_request
);
447 l2_ndisuio_global
->stop_request
= NULL
;
451 #else /* _WIN32_WCE */
452 l2_ndisuio_start_read(l2
, 0);
453 #endif /* _WIN32_WCE */
459 void l2_packet_deinit(struct l2_packet_data
*l2
)
464 if (l2_ndisuio_global
) {
465 l2_ndisuio_global
->refcount
--;
466 l2_ndisuio_global
->l2
[l2_ndisuio_global
->refcount
] = NULL
;
467 if (l2_ndisuio_global
->refcount
) {
468 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): restore filtering "
470 l2_ndisuio_global
->first_proto
);
471 l2_ndisuio_set_ether_type(
472 l2_ndisuio_global
->first_proto
);
477 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): Waiting for RX thread to "
479 SetEvent(l2_ndisuio_global
->stop_request
);
481 * Cancel pending ReadFile() in the RX thread (if we were still
482 * connected at this point).
484 if (!DeviceIoControl(driver_ndis_get_ndisuio_handle(),
485 IOCTL_CANCEL_READ
, NULL
, 0, NULL
, 0, NULL
,
487 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): IOCTL_CANCEL_READ "
488 "failed: %d", (int) GetLastError());
489 /* RX thread will exit blocking ReadFile once NDISUIO
490 * notices that the adapter is disconnected. */
492 WaitForSingleObject(l2_ndisuio_global
->rx_thread
, INFINITE
);
493 wpa_printf(MSG_DEBUG
, "L2(NDISUIO): RX thread exited");
494 CloseHandle(l2_ndisuio_global
->rx_thread
);
495 CloseHandle(l2_ndisuio_global
->stop_request
);
496 CloseHandle(l2_ndisuio_global
->ready_for_read
);
497 CloseHandle(l2_ndisuio_global
->rx_processed
);
498 #endif /* _WIN32_WCE */
500 os_free(l2_ndisuio_global
);
501 l2_ndisuio_global
= NULL
;
505 CancelIo(driver_ndis_get_ndisuio_handle());
506 #endif /* _WIN32_WCE */
508 eloop_unregister_event(l2
->rx_avail
, sizeof(l2
->rx_avail
));
509 CloseHandle(l2
->rx_avail
);
514 int l2_packet_get_ip_addr(struct l2_packet_data
*l2
, char *buf
, size_t len
)
520 void l2_packet_notify_auth_start(struct l2_packet_data
*l2
)