]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/wpa/wpa_supplicant/Packet32.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / wpa / wpa_supplicant / Packet32.c
1 /*-
2  * Copyright (c) 2005
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
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.
19  *
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
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.
41  */
42
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>
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_var.h>
54
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <netdb.h>
58 #include <net/route.h>
59
60 #include <net80211/ieee80211_ioctl.h>
61
62 #include <stdio.h>
63 #include <string.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 #include <pcap.h>
67
68 #include "Packet32.h"
69
70 #define OID_802_11_ADD_KEY      0x0d01011D
71
72 typedef ULONGLONG NDIS_802_11_KEY_RSC;
73 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
74
75 typedef struct NDIS_802_11_KEY {
76         ULONG Length;
77         ULONG KeyIndex;
78         ULONG KeyLength;
79         NDIS_802_11_MAC_ADDRESS BSSID;
80         NDIS_802_11_KEY_RSC KeyRSC;
81         UCHAR KeyMaterial[1];
82 } NDIS_802_11_KEY;
83
84 typedef struct NDIS_802_11_KEY_COMPAT {
85         ULONG Length;
86         ULONG KeyIndex;
87         ULONG KeyLength;
88         NDIS_802_11_MAC_ADDRESS BSSID;
89         UCHAR Pad[6]; /* Make struct layout match Windows. */
90         NDIS_802_11_KEY_RSC KeyRSC;
91 #ifdef notdef
92         UCHAR KeyMaterial[1];
93 #endif
94 } NDIS_802_11_KEY_COMPAT;
95
96 #define TRUE 1
97 #define FALSE 0
98
99 struct adapter {
100         int                     socket;
101         char                    name[IFNAMSIZ];
102         int                     prev_roaming;
103 };
104
105 PCHAR
106 PacketGetVersion(void)
107 {
108         return("FreeBSD WinPcap compatibility shim v1.0");
109 }
110
111 void *
112 PacketOpenAdapter(iface)
113         CHAR                    *iface;
114 {
115         struct adapter          *a;
116         int                     s;
117         int                     ifflags;
118         struct ifreq            ifr;
119         struct ieee80211req     ireq;
120
121         s = socket(PF_INET, SOCK_DGRAM, 0);
122
123         if (s == -1)
124                 return(NULL);
125
126         a = malloc(sizeof(struct adapter));
127         if (a == NULL)
128                 return(NULL);
129
130         a->socket = s;
131         if (strncmp(iface, "\\Device\\NPF_", 12) == 0)
132                 iface += 12;
133         else if (strncmp(iface, "\\DEVICE\\", 8) == 0)
134                 iface += 8;
135         snprintf(a->name, IFNAMSIZ, "%s", iface);
136
137         /* Turn off net80211 roaming */
138         bzero((char *)&ireq, sizeof(ireq));
139         strncpy(ireq.i_name, iface, sizeof (ifr.ifr_name));
140         ireq.i_type = IEEE80211_IOC_ROAMING;
141         if (ioctl(a->socket, SIOCG80211, &ireq) == 0) {
142                 a->prev_roaming = ireq.i_val;
143                 ireq.i_val = IEEE80211_ROAMING_MANUAL;
144                 if (ioctl(a->socket, SIOCS80211, &ireq) < 0)
145                         fprintf(stderr,
146                             "Could not set IEEE80211_ROAMING_MANUAL\n");
147         }
148
149         bzero((char *)&ifr, sizeof(ifr));
150         strncpy(ifr.ifr_name, iface, sizeof (ifr.ifr_name));
151         if (ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
152                 free(a);
153                 close(s);
154                 return(NULL);
155         }
156         ifr.ifr_flags |= IFF_UP;
157         if (ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
158                 free(a);
159                 close(s);
160                 return(NULL);
161         }
162
163         return(a);
164 }
165
166 int
167 PacketRequest(iface, set, oid)
168         void                    *iface;
169         BOOLEAN                 set;
170         PACKET_OID_DATA         *oid;
171 {
172         struct adapter          *a;
173         uint32_t                retval;
174         struct ifreq            ifr;
175         NDIS_802_11_KEY         *old;
176         NDIS_802_11_KEY_COMPAT  *new;
177         PACKET_OID_DATA         *o = NULL;
178
179         if (iface == NULL)
180                 return(-1);
181
182         a = iface;
183         bzero((char *)&ifr, sizeof(ifr));
184
185         /*
186          * This hack is necessary to work around a difference
187          * betwee the GNU C and Microsoft C compilers. The NDIS_802_11_KEY
188          * structure has a uint64_t in it, right after an array of
189          * chars. The Microsoft compiler inserts padding right before
190          * the 64-bit value to align it on a 64-bit boundary, but
191          * GCC only aligns it on a 32-bit boundary. Trying to pass
192          * the GCC-formatted structure to an NDIS binary driver
193          * fails because some of the fields appear to be at the
194          * wrong offsets.
195          *
196          * To get around this, if we detect someone is trying to do
197          * a set operation on OID_802_11_ADD_KEY, we shuffle the data
198          * into a properly padded structure and pass that into the
199          * driver instead. This allows the driver_ndis.c code supplied
200          * with wpa_supplicant to work unmodified.
201          */
202
203         if (set == TRUE && oid->Oid == OID_802_11_ADD_KEY) {
204                 old = (NDIS_802_11_KEY *)&oid->Data;
205                 o = malloc(sizeof(PACKET_OID_DATA) +
206                     sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
207                 if (o == NULL)
208                         return(0);
209                 bzero((char *)o, sizeof(PACKET_OID_DATA) +
210                     sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
211                 o->Oid = oid->Oid;
212                 o->Length = sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength;
213                 new = (NDIS_802_11_KEY_COMPAT *)&o->Data;
214                 new->KeyRSC = old->KeyRSC;
215                 new->Length = o->Length;
216                 new->KeyIndex = old->KeyIndex;
217                 new->KeyLength = old->KeyLength;
218                 bcopy(old->BSSID, new->BSSID, sizeof(NDIS_802_11_MAC_ADDRESS));
219                 bcopy(old->KeyMaterial, (char *)new +
220                     sizeof(NDIS_802_11_KEY_COMPAT), new->KeyLength);
221                 ifr.ifr_data = (caddr_t)o;
222         } else
223                 ifr.ifr_data = (caddr_t)oid;
224
225         strlcpy(ifr.ifr_name, a->name, sizeof(ifr.ifr_name));
226
227         if (set == TRUE)
228                 retval = ioctl(a->socket, SIOCSDRVSPEC, &ifr);
229         else
230                 retval = ioctl(a->socket, SIOCGDRVSPEC, &ifr);
231
232         if (o != NULL)
233                 free(o);
234
235         if (retval)
236                 return(0);
237
238         return(1);
239 }
240
241 int
242 PacketGetAdapterNames(namelist, len)
243         CHAR                    *namelist;
244         ULONG                   *len;
245 {
246         int                     mib[6];
247         size_t                  needed;
248         struct if_msghdr        *ifm;
249         struct sockaddr_dl      *sdl;
250         char                    *buf, *lim, *next;
251         char                    *plist;
252         int                     spc;
253         int                     i, ifcnt = 0;
254
255         plist = namelist;
256         spc = 0;
257
258         bzero(plist, *len);
259
260         needed = 0;
261         mib[0] = CTL_NET;
262         mib[1] = PF_ROUTE;
263         mib[2] = 0;             /* protocol */
264         mib[3] = 0;             /* wildcard address family */
265         mib[4] = NET_RT_IFLIST;
266         mib[5] = 0;             /* no flags */
267
268         if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
269                 return(FALSE);
270
271         buf = malloc (needed);
272         if (buf == NULL)
273                 return(FALSE);
274
275         if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
276                 free(buf);
277                 return(FALSE);
278         }
279
280         lim = buf + needed;
281
282         /* Generate interface name list. */
283
284         next = buf;
285         while (next < lim) {
286                 ifm = (struct if_msghdr *)next;
287                 if (ifm->ifm_type == RTM_IFINFO) {
288                         sdl = (struct sockaddr_dl *)(ifm + 1);
289                         if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
290                                 if ((spc + sdl->sdl_nlen) > *len) {
291                                         free(buf);
292                                         return(FALSE);
293                                 }
294                                 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
295                                 plist += (sdl->sdl_nlen + 1);
296                                 spc += (sdl->sdl_nlen + 1);
297                                 ifcnt++;
298                         }
299                 }
300                 next += ifm->ifm_msglen;
301         }
302
303
304         /* Insert an extra "" as a spacer */
305
306         plist++;
307         spc++;
308
309         /*
310          * Now generate the interface description list. There
311          * must be a unique description for each interface, and
312          * they have to match what the ndis_events program will
313          * feed in later. To keep this simple, we just repeat
314          * the interface list over again.
315          */
316
317         next = buf;
318         while (next < lim) {
319                 ifm = (struct if_msghdr *)next;
320                 if (ifm->ifm_type == RTM_IFINFO) {
321                         sdl = (struct sockaddr_dl *)(ifm + 1);
322                         if (strnstr(sdl->sdl_data, "ndis", sdl->sdl_nlen)) {
323                                 if ((spc + sdl->sdl_nlen) > *len) {
324                                         free(buf);
325                                         return(FALSE);
326                                 }
327                                 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
328                                 plist += (sdl->sdl_nlen + 1);
329                                 spc += (sdl->sdl_nlen + 1);
330                                 ifcnt++;
331                         }
332                 }
333                 next += ifm->ifm_msglen;
334         }
335
336         free (buf);
337
338         *len = spc + 1;
339
340         return(TRUE);
341 }
342
343 void
344 PacketCloseAdapter(iface)
345         void                    *iface;
346 {       
347         struct adapter          *a;
348         struct ifreq            ifr;
349         struct ieee80211req     ireq;
350
351         if (iface == NULL)
352                 return;
353
354         a = iface;
355
356         /* Reset net80211 roaming */
357         bzero((char *)&ireq, sizeof(ireq));
358         strncpy(ireq.i_name, a->name, sizeof (ifr.ifr_name));
359         ireq.i_type = IEEE80211_IOC_ROAMING;
360         ireq.i_val = a->prev_roaming;
361         ioctl(a->socket, SIOCS80211, &ireq);
362
363         bzero((char *)&ifr, sizeof(ifr));
364         strncpy(ifr.ifr_name, a->name, sizeof (ifr.ifr_name));
365         ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr);
366         ifr.ifr_flags &= ~IFF_UP;
367         ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr);
368         close(a->socket);
369         free(a);
370
371         return;
372 }
373
374 #if __FreeBSD_version < 600000
375
376 /*
377  * The version of libpcap in FreeBSD 5.2.1 doesn't have these routines.
378  * Call me insane if you will, but I still run 5.2.1 on my laptop, and
379  * I'd like to use WPA there.
380  */
381
382 int
383 pcap_get_selectable_fd(pcap_t *p)
384 {
385         return(pcap_fileno(p));
386 }
387
388 /*
389  * The old version of libpcap opens its BPF descriptor in read-only
390  * mode. We need to temporarily create a new one we can write to.
391  */
392
393 int
394 pcap_inject(pcap_t *p, const void *buf, size_t len)
395 {
396         int                     fd;
397         int                     res, n = 0;
398         char                    device[sizeof "/dev/bpf0000000000"];
399         struct ifreq            ifr;
400
401         /*
402          * Go through all the minors and find one that isn't in use.
403          */
404         do {
405                 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
406                 fd = open(device, O_RDWR);
407         } while (fd < 0 && errno == EBUSY);
408
409         if (fd == -1)
410                 return(-1);
411
412         bzero((char *)&ifr, sizeof(ifr));
413         ioctl(pcap_fileno(p), BIOCGETIF, (caddr_t)&ifr);
414         ioctl(fd, BIOCSETIF, (caddr_t)&ifr);
415
416         res = write(fd, buf, len);
417
418         close(fd);
419
420         return(res);
421 }
422 #endif