- Move pcap_{get_selectable_fd,pcap_inject}() from 802_11/wpa_supplicant/Packet32.c
[dragonfly.git] / usr.sbin / 802_11 / 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  * $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 $
34  */
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 <sys/time.h>
52
53 #include <net/bpf.h>
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_var.h>
57
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <netdb.h>
61 #include <net/route.h>
62
63 #include <stdio.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <pcap.h>
68
69 #include "Packet32.h"
70
71 #define OID_802_11_ADD_KEY      0x0d01011D
72
73 typedef ULONGLONG NDIS_802_11_KEY_RSC;
74 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
75
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;
84
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;
96
97 #define TRUE 1
98 #define FALSE 0
99
100 struct adapter {
101         int                     socket;
102         char                    name[IFNAMSIZ];
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
120         s = socket(PF_INET, SOCK_DGRAM, 0);
121
122         if (s == -1)
123                 return(NULL);
124
125         a = malloc(sizeof(struct adapter));
126         if (a == NULL)
127                 return(NULL);
128
129         a->socket = s;
130         snprintf(a->name, IFNAMSIZ, "%s", iface);
131
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) {
135                 free(a);
136                 close(s);
137                 return(NULL);
138         }
139         ifr.ifr_flags |= IFF_UP;
140         if (ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
141                 free(a);
142                 close(s);
143                 return(NULL);
144         }
145
146         return(a);
147 }
148
149 int
150 PacketRequest(iface, set, oid)
151         void                    *iface;
152         BOOLEAN                 set;
153         PACKET_OID_DATA         *oid;
154 {
155         struct adapter          *a;
156         uint32_t                retval;
157         struct ifreq            ifr;
158         NDIS_802_11_KEY         *old;
159         NDIS_802_11_KEY_COMPAT  *new;
160         PACKET_OID_DATA         *o = NULL;
161
162         if (iface == NULL)
163                 return(-1);
164
165         a = iface;
166         bzero((char *)&ifr, sizeof(ifr));
167
168         /*
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
177          * wrong offsets.
178          *
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.
184          */
185
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);
190                 if (o == NULL)
191                         return(0);
192                 bzero((char *)o, sizeof(PACKET_OID_DATA) +
193                     sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength);
194                 o->Oid = oid->Oid;
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;
205         } else
206                 ifr.ifr_data = (caddr_t)oid;
207
208         strlcpy(ifr.ifr_name, a->name, sizeof(ifr.ifr_name));
209
210         if (set == TRUE)
211                 retval = ioctl(a->socket, SIOCSDRVSPEC, &ifr);
212         else
213                 retval = ioctl(a->socket, SIOCGDRVSPEC, &ifr);
214
215         if (o != NULL)
216                 free(o);
217
218         if (retval)
219                 return(0);
220
221         return(1);
222 }
223
224 int
225 PacketGetAdapterNames(namelist, len)
226         CHAR                    *namelist;
227         ULONG                   *len;
228 {
229         int                     mib[6];
230         size_t                  needed;
231         struct if_msghdr        *ifm;
232         struct sockaddr_dl      *sdl;
233         char                    *buf, *lim, *next;
234         char                    *plist;
235         int                     spc;
236         int                     i, ifcnt = 0;
237
238         plist = namelist;
239         spc = 0;
240
241         bzero(plist, *len);
242
243         needed = 0;
244         mib[0] = CTL_NET;
245         mib[1] = PF_ROUTE;
246         mib[2] = 0;             /* protocol */
247         mib[3] = 0;             /* wildcard address family */
248         mib[4] = NET_RT_IFLIST;
249         mib[5] = 0;             /* no flags */
250
251         if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
252                 return(FALSE);
253
254         buf = malloc (needed);
255         if (buf == NULL)
256                 return(FALSE);
257
258         if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
259                 free(buf);
260                 return(FALSE);
261         }
262
263         lim = buf + needed;
264
265         /* Generate interface name list. */
266
267         next = buf;
268         while (next < lim) {
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) {
274                                         free(buf);
275                                         return(FALSE);
276                                 }
277                                 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
278                                 plist += (sdl->sdl_nlen + 1);
279                                 spc += (sdl->sdl_nlen + 1);
280                                 ifcnt++;
281                         }
282                 }
283                 next += ifm->ifm_msglen;
284         }
285
286
287         /* Insert an extra "" as a spacer */
288
289         plist++;
290         spc++;
291
292         /*
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.
298          */
299
300         next = buf;
301         while (next < lim) {
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) {
307                                         free(buf);
308                                         return(FALSE);
309                                 }
310                                 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen);
311                                 plist += (sdl->sdl_nlen + 1);
312                                 spc += (sdl->sdl_nlen + 1);
313                                 ifcnt++;
314                         }
315                 }
316                 next += ifm->ifm_msglen;
317         }
318
319         free (buf);
320
321         *len = spc + 1;
322
323         return(TRUE);
324 }
325
326 void
327 PacketCloseAdapter(iface)
328         void                    *iface;
329 {       
330         struct adapter          *a;
331         struct ifreq            ifr;
332
333         if (iface == NULL)
334                 return;
335
336         a = iface;
337
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);
343         close(a->socket);
344         free(a);
345
346         return;
347 }