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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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>
52 #include <net/if_dl.h>
53 #include <net/if_var.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
58 #include <net/route.h>
68 #define OID_802_11_ADD_KEY 0x0d01011D
70 typedef ULONGLONG NDIS_802_11_KEY_RSC;
71 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
73 typedef struct NDIS_802_11_KEY {
77 NDIS_802_11_MAC_ADDRESS BSSID;
78 NDIS_802_11_KEY_RSC KeyRSC;
82 typedef struct NDIS_802_11_KEY_COMPAT {
86 NDIS_802_11_MAC_ADDRESS BSSID;
87 UCHAR Pad[6]; /* Make struct layout match Windows. */
88 NDIS_802_11_KEY_RSC KeyRSC;
92 } NDIS_802_11_KEY_COMPAT;
103 PacketGetVersion(void)
105 return("FreeBSD WinPcap compatibility shim v1.0");
109 PacketOpenAdapter(iface)
117 s = socket(PF_INET, SOCK_DGRAM, 0);
122 a = malloc(sizeof(struct adapter));
127 snprintf(a->name, IFNAMSIZ, "%s", iface);
129 bzero((char *)&ifr, sizeof(ifr));
130 strncpy(ifr.ifr_name, iface, sizeof (ifr.ifr_name));
131 if (ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
136 ifr.ifr_flags |= IFF_UP;
137 if (ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
147 PacketRequest(iface, set, oid)
150 PACKET_OID_DATA *oid;
155 NDIS_802_11_KEY *old;
156 NDIS_802_11_KEY_COMPAT *new;
157 PACKET_OID_DATA *o = NULL;
163 bzero((char *)&ifr, sizeof(ifr));
166 * This hack is necessary to work around a difference
167 * betwee the GNU C and Microsoft C compilers. The NDIS_802_11_KEY
168 * structure has a uint64_t in it, right after an array of
169 * chars. The Microsoft compiler inserts padding right before
170 * the 64-bit value to align it on a 64-bit boundary, but
171 * GCC only aligns it on a 32-bit boundary. Trying to pass
172 * the GCC-formatted structure to an NDIS binary driver
173 * fails because some of the fields appear to be at the
176 * To get around this, if we detect someone is trying to do
177 * a set operation on OID_802_11_ADD_KEY, we shuffle the data
178 * into a properly padded structure and pass that into the
179 * driver instead. This allows the driver_ndis.c code supplied
180 * with wpa_supplicant to work unmodified.
183 if (set == TRUE && oid->Oid == OID_802_11_ADD_KEY) {
184 old = (NDIS_802_11_KEY *)&oid->Data;
185 o = malloc(sizeof(PACKET_OID_DATA) +
186 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
189 bzero((char *)o, sizeof(PACKET_OID_DATA) +
190 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
192 o->Length = sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength;
193 new = (NDIS_802_11_KEY_COMPAT *)&o->Data;
194 new->KeyRSC = old->KeyRSC;
195 new->Length = o->Length;
196 new->KeyIndex = old->KeyIndex;
197 new->KeyLength = old->KeyLength;
198 bcopy(old->BSSID, new->BSSID, sizeof(NDIS_802_11_MAC_ADDRESS));
199 bcopy(old->KeyMaterial, (char *)new +
200 sizeof(NDIS_802_11_KEY_COMPAT), new->KeyLength);
201 ifr.ifr_data = (caddr_t)o;
203 ifr.ifr_data = (caddr_t)oid;
205 strlcpy(ifr.ifr_name, a->name, sizeof(ifr.ifr_name));
208 retval = ioctl(a->socket, SIOCSDRVSPEC, &ifr);
210 retval = ioctl(a->socket, SIOCGDRVSPEC, &ifr);
222 PacketGetAdapterNames(namelist, len)
228 struct if_msghdr *ifm;
229 struct sockaddr_dl *sdl;
230 char *buf, *lim, *next;
243 mib[2] = 0; /* protocol */
244 mib[3] = 0; /* wildcard address family */
245 mib[4] = NET_RT_IFLIST;
246 mib[5] = 0; /* no flags */
248 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
251 buf = malloc (needed);
255 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
262 /* Generate interface name list. */
266 ifm = (struct if_msghdr *)next;
267 if (ifm->ifm_type == RTM_IFINFO) {
268 sdl = (struct sockaddr_dl *)(ifm + 1);
269 if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
270 if ((spc + sdl->sdl_nlen) > *len) {
274 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
275 plist += (sdl->sdl_nlen + 1);
276 spc += (sdl->sdl_nlen + 1);
280 next += ifm->ifm_msglen;
284 /* Insert an extra "" as a spacer */
290 * Now generate the interface description list. There
291 * must be a unique description for each interface, and
292 * they have to match what the ndis_events program will
293 * feed in later. To keep this simple, we just repeat
294 * the interface list over again.
299 ifm = (struct if_msghdr *)next;
300 if (ifm->ifm_type == RTM_IFINFO) {
301 sdl = (struct sockaddr_dl *)(ifm + 1);
302 if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
303 if ((spc + sdl->sdl_nlen) > *len) {
307 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
308 plist += (sdl->sdl_nlen + 1);
309 spc += (sdl->sdl_nlen + 1);
313 next += ifm->ifm_msglen;
324 PacketCloseAdapter(iface)
335 bzero((char *)&ifr, sizeof(ifr));
336 strncpy(ifr.ifr_name, a->name, sizeof (ifr.ifr_name));
337 ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr);
338 ifr.ifr_flags &= ~IFF_UP;
339 ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr);
346 #if __FreeBSD_version < 600000
349 * The version of libpcap in FreeBSD 5.2.1 doesn't have these routines.
350 * Call me insane if you will, but I still run 5.2.1 on my laptop, and
351 * I'd like to use WPA there.
355 pcap_get_selectable_fd(pcap_t *p)
357 return(pcap_fileno(p));
361 * The old version of libpcap opens its BPF descriptor in read-only
362 * mode. We need to temporarily create a new one we can write to.
366 pcap_inject(pcap_t *p, const void *buf, size_t len)
370 char device[sizeof "/dev/bpf0000000000"];
374 * Go through all the minors and find one that isn't in use.
377 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
378 fd = open(device, O_RDWR);
379 } while (fd < 0 && errno == EBUSY);
384 bzero((char *)&ifr, sizeof(ifr));
385 ioctl(pcap_fileno(p), BIOCGETIF, (caddr_t)&ifr);
386 ioctl(fd, BIOCSETIF, (caddr_t)&ifr);
388 res = write(fd, buf, len);