Hook {wpa_supplicant, hostapd} 0.5.8 into building.
[dragonfly/port-amd64.git] / usr.sbin / 802_11 / wpa_supplicant / Packet32.c
blob0727565ae4774197a931a36204782aa2792e9464
1 /*-
2 * Copyright (c) 2005
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
7 * are met:
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.4 2007/07/11 16:04:08 sam Exp $
33 * $DragonFly: src/usr.sbin/802_11/wpa_supplicant/Packet32.c,v 1.3 2007/08/07 11:25:37 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>
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_var.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <netdb.h>
59 #include <net/route.h>
61 #include <netproto/802_11/ieee80211_ioctl.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <pcap.h>
69 #include "Packet32.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 {
77 ULONG Length;
78 ULONG KeyIndex;
79 ULONG KeyLength;
80 NDIS_802_11_MAC_ADDRESS BSSID;
81 NDIS_802_11_KEY_RSC KeyRSC;
82 UCHAR KeyMaterial[1];
83 } NDIS_802_11_KEY;
85 typedef struct NDIS_802_11_KEY_COMPAT {
86 ULONG Length;
87 ULONG KeyIndex;
88 ULONG KeyLength;
89 NDIS_802_11_MAC_ADDRESS BSSID;
90 UCHAR Pad[6]; /* Make struct layout match Windows. */
91 NDIS_802_11_KEY_RSC KeyRSC;
92 #ifdef notdef
93 UCHAR KeyMaterial[1];
94 #endif
95 } NDIS_802_11_KEY_COMPAT;
97 #define TRUE 1
98 #define FALSE 0
100 struct adapter {
101 int socket;
102 char name[IFNAMSIZ];
103 int prev_roaming;
106 PCHAR
107 PacketGetVersion(void)
109 return("FreeBSD WinPcap compatibility shim v1.0");
112 void *
113 PacketOpenAdapter(iface)
114 CHAR *iface;
116 struct adapter *a;
117 int s;
118 int ifflags;
119 struct ifreq ifr;
120 struct ieee80211req ireq;
122 s = socket(PF_INET, SOCK_DGRAM, 0);
124 if (s == -1)
125 return(NULL);
127 a = malloc(sizeof(struct adapter));
128 if (a == NULL)
129 return(NULL);
131 a->socket = s;
132 if (strncmp(iface, "\\Device\\NPF_", 12) == 0)
133 iface += 12;
134 else if (strncmp(iface, "\\DEVICE\\", 8) == 0)
135 iface += 8;
136 snprintf(a->name, IFNAMSIZ, "%s", iface);
138 /* Turn off net80211 roaming */
139 bzero((char *)&ireq, sizeof(ireq));
140 strncpy(ireq.i_name, iface, sizeof (ifr.ifr_name));
141 ireq.i_type = IEEE80211_IOC_ROAMING;
142 if (ioctl(a->socket, SIOCG80211, &ireq) == 0) {
143 a->prev_roaming = ireq.i_val;
144 ireq.i_val = IEEE80211_ROAMING_MANUAL;
145 if (ioctl(a->socket, SIOCS80211, &ireq) < 0)
146 fprintf(stderr,
147 "Could not set IEEE80211_ROAMING_MANUAL\n");
150 bzero((char *)&ifr, sizeof(ifr));
151 strncpy(ifr.ifr_name, iface, sizeof (ifr.ifr_name));
152 if (ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
153 free(a);
154 close(s);
155 return(NULL);
157 ifr.ifr_flags |= IFF_UP;
158 if (ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
159 free(a);
160 close(s);
161 return(NULL);
164 return(a);
168 PacketRequest(iface, set, oid)
169 void *iface;
170 BOOLEAN set;
171 PACKET_OID_DATA *oid;
173 struct adapter *a;
174 uint32_t retval;
175 struct ifreq ifr;
176 NDIS_802_11_KEY *old;
177 NDIS_802_11_KEY_COMPAT *new;
178 PACKET_OID_DATA *o = NULL;
180 if (iface == NULL)
181 return(-1);
183 a = iface;
184 bzero((char *)&ifr, sizeof(ifr));
187 * This hack is necessary to work around a difference
188 * betwee the GNU C and Microsoft C compilers. The NDIS_802_11_KEY
189 * structure has a uint64_t in it, right after an array of
190 * chars. The Microsoft compiler inserts padding right before
191 * the 64-bit value to align it on a 64-bit boundary, but
192 * GCC only aligns it on a 32-bit boundary. Trying to pass
193 * the GCC-formatted structure to an NDIS binary driver
194 * fails because some of the fields appear to be at the
195 * wrong offsets.
197 * To get around this, if we detect someone is trying to do
198 * a set operation on OID_802_11_ADD_KEY, we shuffle the data
199 * into a properly padded structure and pass that into the
200 * driver instead. This allows the driver_ndis.c code supplied
201 * with wpa_supplicant to work unmodified.
204 if (set == TRUE && oid->Oid == OID_802_11_ADD_KEY) {
205 old = (NDIS_802_11_KEY *)&oid->Data;
206 o = malloc(sizeof(PACKET_OID_DATA) +
207 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
208 if (o == NULL)
209 return(0);
210 bzero((char *)o, sizeof(PACKET_OID_DATA) +
211 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
212 o->Oid = oid->Oid;
213 o->Length = sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength;
214 new = (NDIS_802_11_KEY_COMPAT *)&o->Data;
215 new->KeyRSC = old->KeyRSC;
216 new->Length = o->Length;
217 new->KeyIndex = old->KeyIndex;
218 new->KeyLength = old->KeyLength;
219 bcopy(old->BSSID, new->BSSID, sizeof(NDIS_802_11_MAC_ADDRESS));
220 bcopy(old->KeyMaterial, (char *)new +
221 sizeof(NDIS_802_11_KEY_COMPAT), new->KeyLength);
222 ifr.ifr_data = (caddr_t)o;
223 } else
224 ifr.ifr_data = (caddr_t)oid;
226 strlcpy(ifr.ifr_name, a->name, sizeof(ifr.ifr_name));
228 if (set == TRUE)
229 retval = ioctl(a->socket, SIOCSDRVSPEC, &ifr);
230 else
231 retval = ioctl(a->socket, SIOCGDRVSPEC, &ifr);
233 if (o != NULL)
234 free(o);
236 if (retval)
237 return(0);
239 return(1);
243 PacketGetAdapterNames(namelist, len)
244 CHAR *namelist;
245 ULONG *len;
247 int mib[6];
248 size_t needed;
249 struct if_msghdr *ifm;
250 struct sockaddr_dl *sdl;
251 char *buf, *lim, *next;
252 char *plist;
253 int spc;
254 int i, ifcnt = 0;
256 plist = namelist;
257 spc = 0;
259 bzero(plist, *len);
261 needed = 0;
262 mib[0] = CTL_NET;
263 mib[1] = PF_ROUTE;
264 mib[2] = 0; /* protocol */
265 mib[3] = 0; /* wildcard address family */
266 mib[4] = NET_RT_IFLIST;
267 mib[5] = 0; /* no flags */
269 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
270 return(FALSE);
272 buf = malloc (needed);
273 if (buf == NULL)
274 return(FALSE);
276 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
277 free(buf);
278 return(FALSE);
281 lim = buf + needed;
283 /* Generate interface name list. */
285 next = buf;
286 while (next < lim) {
287 ifm = (struct if_msghdr *)next;
288 if (ifm->ifm_type == RTM_IFINFO) {
289 sdl = (struct sockaddr_dl *)(ifm + 1);
290 if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
291 if ((spc + sdl->sdl_nlen) > *len) {
292 free(buf);
293 return(FALSE);
295 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
296 plist += (sdl->sdl_nlen + 1);
297 spc += (sdl->sdl_nlen + 1);
298 ifcnt++;
301 next += ifm->ifm_msglen;
305 /* Insert an extra "" as a spacer */
307 plist++;
308 spc++;
311 * Now generate the interface description list. There
312 * must be a unique description for each interface, and
313 * they have to match what the ndis_events program will
314 * feed in later. To keep this simple, we just repeat
315 * the interface list over again.
318 next = buf;
319 while (next < lim) {
320 ifm = (struct if_msghdr *)next;
321 if (ifm->ifm_type == RTM_IFINFO) {
322 sdl = (struct sockaddr_dl *)(ifm + 1);
323 if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
324 if ((spc + sdl->sdl_nlen) > *len) {
325 free(buf);
326 return(FALSE);
328 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
329 plist += (sdl->sdl_nlen + 1);
330 spc += (sdl->sdl_nlen + 1);
331 ifcnt++;
334 next += ifm->ifm_msglen;
337 free (buf);
339 *len = spc + 1;
341 return(TRUE);
344 void
345 PacketCloseAdapter(iface)
346 void *iface;
348 struct adapter *a;
349 struct ifreq ifr;
350 struct ieee80211req ireq;
352 if (iface == NULL)
353 return;
355 a = iface;
357 /* Reset net80211 roaming */
358 bzero((char *)&ireq, sizeof(ireq));
359 strncpy(ireq.i_name, a->name, sizeof (ifr.ifr_name));
360 ireq.i_type = IEEE80211_IOC_ROAMING;
361 ireq.i_val = a->prev_roaming;
362 ioctl(a->socket, SIOCS80211, &ireq);
364 bzero((char *)&ifr, sizeof(ifr));
365 strncpy(ifr.ifr_name, a->name, sizeof (ifr.ifr_name));
366 ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr);
367 ifr.ifr_flags &= ~IFF_UP;
368 ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr);
369 close(a->socket);
370 free(a);
372 return;