3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/usr.sbin/wpa/wpa_supplicant/Packet32.c,v 1.2.2.2 2006/04/12 17:21:08 sam Exp $
33 * $DragonFly: src/usr.sbin/802_11/wpa_supplicant/Packet32.c,v 1.2 2006/09/02 05:40:35 sephe Exp $
37 * This file implements a small portion of the Winpcap API for the
38 * Windows NDIS interface in wpa_supplicant. It provides just enough
39 * routines to fool wpa_supplicant into thinking it's really running
40 * in a Windows environment.
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/sysctl.h>
50 #include <sys/fcntl.h>
55 #include <net/if_dl.h>
56 #include <net/if_var.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
61 #include <net/route.h>
71 #define OID_802_11_ADD_KEY 0x0d01011D
73 typedef ULONGLONG NDIS_802_11_KEY_RSC;
74 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
76 typedef struct NDIS_802_11_KEY {
80 NDIS_802_11_MAC_ADDRESS BSSID;
81 NDIS_802_11_KEY_RSC KeyRSC;
85 typedef struct NDIS_802_11_KEY_COMPAT {
89 NDIS_802_11_MAC_ADDRESS BSSID;
90 UCHAR Pad[6]; /* Make struct layout match Windows. */
91 NDIS_802_11_KEY_RSC KeyRSC;
95 } NDIS_802_11_KEY_COMPAT;
106 PacketGetVersion(void)
108 return("FreeBSD WinPcap compatibility shim v1.0");
112 PacketOpenAdapter(iface)
120 s = socket(PF_INET, SOCK_DGRAM, 0);
125 a = malloc(sizeof(struct adapter));
130 snprintf(a->name, IFNAMSIZ, "%s", iface);
132 bzero((char *)&ifr, sizeof(ifr));
133 strncpy(ifr.ifr_name, iface, sizeof (ifr.ifr_name));
134 if (ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
139 ifr.ifr_flags |= IFF_UP;
140 if (ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
150 PacketRequest(iface, set, oid)
153 PACKET_OID_DATA *oid;
158 NDIS_802_11_KEY *old;
159 NDIS_802_11_KEY_COMPAT *new;
160 PACKET_OID_DATA *o = NULL;
166 bzero((char *)&ifr, sizeof(ifr));
169 * This hack is necessary to work around a difference
170 * betwee the GNU C and Microsoft C compilers. The NDIS_802_11_KEY
171 * structure has a uint64_t in it, right after an array of
172 * chars. The Microsoft compiler inserts padding right before
173 * the 64-bit value to align it on a 64-bit boundary, but
174 * GCC only aligns it on a 32-bit boundary. Trying to pass
175 * the GCC-formatted structure to an NDIS binary driver
176 * fails because some of the fields appear to be at the
179 * To get around this, if we detect someone is trying to do
180 * a set operation on OID_802_11_ADD_KEY, we shuffle the data
181 * into a properly padded structure and pass that into the
182 * driver instead. This allows the driver_ndis.c code supplied
183 * with wpa_supplicant to work unmodified.
186 if (set == TRUE && oid->Oid == OID_802_11_ADD_KEY) {
187 old = (NDIS_802_11_KEY *)&oid->Data;
188 o = malloc(sizeof(PACKET_OID_DATA) +
189 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
192 bzero((char *)o, sizeof(PACKET_OID_DATA) +
193 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
195 o->Length = sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength;
196 new = (NDIS_802_11_KEY_COMPAT *)&o->Data;
197 new->KeyRSC = old->KeyRSC;
198 new->Length = o->Length;
199 new->KeyIndex = old->KeyIndex;
200 new->KeyLength = old->KeyLength;
201 bcopy(old->BSSID, new->BSSID, sizeof(NDIS_802_11_MAC_ADDRESS));
202 bcopy(old->KeyMaterial, (char *)new +
203 sizeof(NDIS_802_11_KEY_COMPAT), new->KeyLength);
204 ifr.ifr_data = (caddr_t)o;
206 ifr.ifr_data = (caddr_t)oid;
208 strlcpy(ifr.ifr_name, a->name, sizeof(ifr.ifr_name));
211 retval = ioctl(a->socket, SIOCSDRVSPEC, &ifr);
213 retval = ioctl(a->socket, SIOCGDRVSPEC, &ifr);
225 PacketGetAdapterNames(namelist, len)
231 struct if_msghdr *ifm;
232 struct sockaddr_dl *sdl;
233 char *buf, *lim, *next;
246 mib[2] = 0; /* protocol */
247 mib[3] = 0; /* wildcard address family */
248 mib[4] = NET_RT_IFLIST;
249 mib[5] = 0; /* no flags */
251 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
254 buf = malloc (needed);
258 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
265 /* Generate interface name list. */
269 ifm = (struct if_msghdr *)next;
270 if (ifm->ifm_type == RTM_IFINFO) {
271 sdl = (struct sockaddr_dl *)(ifm + 1);
272 if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
273 if ((spc + sdl->sdl_nlen) > *len) {
277 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
278 plist += (sdl->sdl_nlen + 1);
279 spc += (sdl->sdl_nlen + 1);
283 next += ifm->ifm_msglen;
287 /* Insert an extra "" as a spacer */
293 * Now generate the interface description list. There
294 * must be a unique description for each interface, and
295 * they have to match what the ndis_events program will
296 * feed in later. To keep this simple, we just repeat
297 * the interface list over again.
302 ifm = (struct if_msghdr *)next;
303 if (ifm->ifm_type == RTM_IFINFO) {
304 sdl = (struct sockaddr_dl *)(ifm + 1);
305 if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
306 if ((spc + sdl->sdl_nlen) > *len) {
310 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
311 plist += (sdl->sdl_nlen + 1);
312 spc += (sdl->sdl_nlen + 1);
316 next += ifm->ifm_msglen;
327 PacketCloseAdapter(iface)
338 bzero((char *)&ifr, sizeof(ifr));
339 strncpy(ifr.ifr_name, a->name, sizeof (ifr.ifr_name));
340 ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr);
341 ifr.ifr_flags &= ~IFF_UP;
342 ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr);