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 /* 34 * This file implements a small portion of the Winpcap API for the 35 * Windows NDIS interface in wpa_supplicant. It provides just enough 36 * routines to fool wpa_supplicant into thinking it's really running 37 * in a Windows environment. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/param.h> 42 #include <sys/socket.h> 43 #include <sys/ioctl.h> 44 #include <sys/errno.h> 45 #include <sys/sysctl.h> 46 #include <sys/fcntl.h> 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_var.h> 50 51 #include <netinet/in.h> 52 #include <arpa/inet.h> 53 #include <netdb.h> 54 #include <net/route.h> 55 56 #ifdef __FreeBSD__ 57 #include <net80211/ieee80211_ioctl.h> 58 #endif 59 #ifdef __DragonFly__ 60 #include <netproto/802_11/ieee80211_ioctl.h> 61 #endif 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 int prev_roaming; 104 }; 105 106 PCHAR 107 PacketGetVersion(void) 108 { 109 return("FreeBSD WinPcap compatibility shim v1.0"); 110 } 111 112 void * 113 PacketOpenAdapter(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(void *iface, BOOLEAN set, PACKET_OID_DATA *oid) 168 { 169 struct adapter *a; 170 uint32_t retval; 171 struct ifreq ifr; 172 NDIS_802_11_KEY *old; 173 NDIS_802_11_KEY_COMPAT *new; 174 PACKET_OID_DATA *o = NULL; 175 176 if (iface == NULL) 177 return(-1); 178 179 a = iface; 180 bzero((char *)&ifr, sizeof(ifr)); 181 182 /* 183 * This hack is necessary to work around a difference 184 * betwee the GNU C and Microsoft C compilers. The NDIS_802_11_KEY 185 * structure has a uint64_t in it, right after an array of 186 * chars. The Microsoft compiler inserts padding right before 187 * the 64-bit value to align it on a 64-bit boundary, but 188 * GCC only aligns it on a 32-bit boundary. Trying to pass 189 * the GCC-formatted structure to an NDIS binary driver 190 * fails because some of the fields appear to be at the 191 * wrong offsets. 192 * 193 * To get around this, if we detect someone is trying to do 194 * a set operation on OID_802_11_ADD_KEY, we shuffle the data 195 * into a properly padded structure and pass that into the 196 * driver instead. This allows the driver_ndis.c code supplied 197 * with wpa_supplicant to work unmodified. 198 */ 199 200 if (set == TRUE && oid->Oid == OID_802_11_ADD_KEY) { 201 old = (NDIS_802_11_KEY *)&oid->Data; 202 o = malloc(sizeof(PACKET_OID_DATA) + 203 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength); 204 if (o == NULL) 205 return(0); 206 bzero((char *)o, sizeof(PACKET_OID_DATA) + 207 sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength); 208 o->Oid = oid->Oid; 209 o->Length = sizeof(NDIS_802_11_KEY_COMPAT) + old->KeyLength; 210 new = (NDIS_802_11_KEY_COMPAT *)&o->Data; 211 new->KeyRSC = old->KeyRSC; 212 new->Length = o->Length; 213 new->KeyIndex = old->KeyIndex; 214 new->KeyLength = old->KeyLength; 215 bcopy(old->BSSID, new->BSSID, sizeof(NDIS_802_11_MAC_ADDRESS)); 216 bcopy(old->KeyMaterial, (char *)new + 217 sizeof(NDIS_802_11_KEY_COMPAT), new->KeyLength); 218 ifr.ifr_data = (caddr_t)o; 219 } else 220 ifr.ifr_data = (caddr_t)oid; 221 222 strlcpy(ifr.ifr_name, a->name, sizeof(ifr.ifr_name)); 223 224 if (set == TRUE) 225 retval = ioctl(a->socket, SIOCSDRVSPEC, &ifr); 226 else 227 retval = ioctl(a->socket, SIOCGDRVSPEC, &ifr); 228 229 if (o != NULL) 230 free(o); 231 232 if (retval) 233 return(0); 234 235 return(1); 236 } 237 238 int 239 PacketGetAdapterNames(CHAR *namelist, ULONG *len) 240 { 241 int mib[6]; 242 size_t needed; 243 struct if_msghdr *ifm; 244 struct sockaddr_dl *sdl; 245 char *buf, *lim, *next; 246 char *plist; 247 int spc; 248 int i, ifcnt = 0; 249 250 plist = namelist; 251 spc = 0; 252 253 bzero(plist, *len); 254 255 needed = 0; 256 mib[0] = CTL_NET; 257 mib[1] = PF_ROUTE; 258 mib[2] = 0; /* protocol */ 259 mib[3] = 0; /* wildcard address family */ 260 mib[4] = NET_RT_IFLIST; 261 mib[5] = 0; /* no flags */ 262 263 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0) 264 return(FALSE); 265 266 buf = malloc (needed); 267 if (buf == NULL) 268 return(FALSE); 269 270 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) { 271 free(buf); 272 return(FALSE); 273 } 274 275 lim = buf + needed; 276 277 /* Generate interface name list. */ 278 279 next = buf; 280 while (next < lim) { 281 ifm = (struct if_msghdr *)next; 282 if (ifm->ifm_type == RTM_IFINFO) { 283 sdl = (struct sockaddr_dl *)(ifm + 1); 284 if (strnstr(sdl->sdl_data, "wlan", sdl->sdl_nlen)) { 285 if ((spc + sdl->sdl_nlen) > *len) { 286 free(buf); 287 return(FALSE); 288 } 289 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen); 290 plist += (sdl->sdl_nlen + 1); 291 spc += (sdl->sdl_nlen + 1); 292 ifcnt++; 293 } 294 } 295 next += ifm->ifm_msglen; 296 } 297 298 299 /* Insert an extra "" as a spacer */ 300 301 plist++; 302 spc++; 303 304 /* 305 * Now generate the interface description list. There 306 * must be a unique description for each interface, and 307 * they have to match what the ndis_events program will 308 * feed in later. To keep this simple, we just repeat 309 * the interface list over again. 310 */ 311 312 next = buf; 313 while (next < lim) { 314 ifm = (struct if_msghdr *)next; 315 if (ifm->ifm_type == RTM_IFINFO) { 316 sdl = (struct sockaddr_dl *)(ifm + 1); 317 if (strnstr(sdl->sdl_data, "wlan", sdl->sdl_nlen)) { 318 if ((spc + sdl->sdl_nlen) > *len) { 319 free(buf); 320 return(FALSE); 321 } 322 strncpy(plist, sdl->sdl_data, sdl->sdl_nlen); 323 plist += (sdl->sdl_nlen + 1); 324 spc += (sdl->sdl_nlen + 1); 325 ifcnt++; 326 } 327 } 328 next += ifm->ifm_msglen; 329 } 330 331 free (buf); 332 333 *len = spc + 1; 334 335 return(TRUE); 336 } 337 338 void 339 PacketCloseAdapter(void *iface) 340 { 341 struct adapter *a; 342 struct ifreq ifr; 343 struct ieee80211req ireq; 344 345 if (iface == NULL) 346 return; 347 348 a = iface; 349 350 /* Reset net80211 roaming */ 351 bzero((char *)&ireq, sizeof(ireq)); 352 strncpy(ireq.i_name, a->name, sizeof (ifr.ifr_name)); 353 ireq.i_type = IEEE80211_IOC_ROAMING; 354 ireq.i_val = a->prev_roaming; 355 ioctl(a->socket, SIOCS80211, &ireq); 356 357 bzero((char *)&ifr, sizeof(ifr)); 358 strncpy(ifr.ifr_name, a->name, sizeof (ifr.ifr_name)); 359 ioctl(a->socket, SIOCGIFFLAGS, (caddr_t)&ifr); 360 ifr.ifr_flags &= ~IFF_UP; 361 ioctl(a->socket, SIOCSIFFLAGS, (caddr_t)&ifr); 362 close(a->socket); 363 free(a); 364 365 return; 366 } 367