xref: /freebsd/contrib/wpa/src/drivers/driver_ndis_.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * WPA Supplicant - Windows/NDIS driver interface - event processing
3  * Copyright (c) 2004-2005, 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 
11 #include "common.h"
12 #include "driver.h"
13 #include "eloop.h"
14 
15 /* Keep this event processing in a separate file and without WinPcap headers to
16  * avoid conflicts with some of the header files. */
17 struct _ADAPTER;
18 typedef struct _ADAPTER * LPADAPTER;
19 #include "driver_ndis.h"
20 
21 
22 void wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data *drv);
23 void wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data *drv);
24 void wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data *drv,
25 					  const u8 *data, size_t data_len);
26 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv);
27 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv);
28 
29 
30 enum event_types { EVENT_CONNECT, EVENT_DISCONNECT,
31 		   EVENT_MEDIA_SPECIFIC, EVENT_ADAPTER_ARRIVAL,
32 		   EVENT_ADAPTER_REMOVAL };
33 
34 /* Event data:
35  * enum event_types (as int, i.e., 4 octets)
36  * data length (2 octets (big endian), optional)
37  * data (variable len, optional)
38  */
39 
40 
41 static void wpa_driver_ndis_event_process(struct wpa_driver_ndis_data *drv,
42 					  u8 *buf, size_t len)
43 {
44 	u8 *pos, *data = NULL;
45 	enum event_types type;
46 	size_t data_len = 0;
47 
48 	wpa_hexdump(MSG_MSGDUMP, "NDIS: received event data", buf, len);
49 	if (len < sizeof(int))
50 		return;
51 	type = *((int *) buf);
52 	pos = buf + sizeof(int);
53 	wpa_printf(MSG_DEBUG, "NDIS: event - type %d", type);
54 
55 	if (buf + len - pos > 2) {
56 		data_len = (int) *pos++ << 8;
57 		data_len += *pos++;
58 		if (data_len > (size_t) (buf + len - pos)) {
59 			wpa_printf(MSG_DEBUG, "NDIS: event data overflow");
60 			return;
61 		}
62 		data = pos;
63 		wpa_hexdump(MSG_MSGDUMP, "NDIS: event data", data, data_len);
64 	}
65 
66 	switch (type) {
67 	case EVENT_CONNECT:
68 		wpa_driver_ndis_event_connect(drv);
69 		break;
70 	case EVENT_DISCONNECT:
71 		wpa_driver_ndis_event_disconnect(drv);
72 		break;
73 	case EVENT_MEDIA_SPECIFIC:
74 		wpa_driver_ndis_event_media_specific(drv, data, data_len);
75 		break;
76 	case EVENT_ADAPTER_ARRIVAL:
77 		wpa_driver_ndis_event_adapter_arrival(drv);
78 		break;
79 	case EVENT_ADAPTER_REMOVAL:
80 		wpa_driver_ndis_event_adapter_removal(drv);
81 		break;
82 	}
83 }
84 
85 
86 void wpa_driver_ndis_event_pipe_cb(void *eloop_data, void *user_data)
87 {
88 	struct wpa_driver_ndis_data *drv = eloop_data;
89 	u8 buf[512];
90 	DWORD len;
91 
92 	ResetEvent(drv->event_avail);
93 	if (ReadFile(drv->events_pipe, buf, sizeof(buf), &len, NULL))
94 		wpa_driver_ndis_event_process(drv, buf, len);
95 	else {
96 		wpa_printf(MSG_DEBUG, "%s: ReadFile() failed: %d", __func__,
97 			   (int) GetLastError());
98 	}
99 }
100