1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */ 2 /* 3 * Copyright (c) 1994, 1995, 1996, 1997, 1998 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the Computer Systems 17 * Engineering Group at Lawrence Berkeley Laboratory. 18 * 4. Neither the name of the University nor of the Laboratory may be used 19 * to endorse or promote products derived from this software without 20 * specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char rcsid[] _U_ = 37 "@(#) $Header: /tcpdump/master/libpcap/fad-gifc.c,v 1.12 2008-08-06 07:34:09 guy Exp $ (LBL)"; 38 #endif 39 40 #ifdef HAVE_CONFIG_H 41 #include "config.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/ioctl.h> 46 #include <sys/socket.h> 47 #ifdef HAVE_SYS_SOCKIO_H 48 #include <sys/sockio.h> 49 #endif 50 #include <sys/time.h> /* concession to AIX */ 51 52 struct mbuf; /* Squelch compiler warnings on some platforms for */ 53 struct rtentry; /* declarations in <net/if.h> */ 54 #include <net/if.h> 55 #include <netinet/in.h> 56 57 #include <ctype.h> 58 #include <errno.h> 59 #include <memory.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "pcap-int.h" 66 67 #ifdef HAVE_OS_PROTO_H 68 #include "os-proto.h" 69 #endif 70 71 /* 72 * This is fun. 73 * 74 * In older BSD systems, socket addresses were fixed-length, and 75 * "sizeof (struct sockaddr)" gave the size of the structure. 76 * All addresses fit within a "struct sockaddr". 77 * 78 * In newer BSD systems, the socket address is variable-length, and 79 * there's an "sa_len" field giving the length of the structure; 80 * this allows socket addresses to be longer than 2 bytes of family 81 * and 14 bytes of data. 82 * 83 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553 84 * variant of the old BSD scheme (with "struct sockaddr_storage" rather 85 * than "struct sockaddr"), and some use the new BSD scheme. 86 * 87 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()" 88 * macro that determines the size based on the address family. Other 89 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553 90 * but not in the final version). 91 * 92 * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have 93 * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the 94 * address in an entry returned by SIOCGIFCONF. 95 */ 96 #ifndef SA_LEN 97 #ifdef HAVE_SOCKADDR_SA_LEN 98 #define SA_LEN(addr) ((addr)->sa_len) 99 #else /* HAVE_SOCKADDR_SA_LEN */ 100 #define SA_LEN(addr) (sizeof (struct sockaddr)) 101 #endif /* HAVE_SOCKADDR_SA_LEN */ 102 #endif /* SA_LEN */ 103 104 /* 105 * This is also fun. 106 * 107 * There is no ioctl that returns the amount of space required for all 108 * the data that SIOCGIFCONF could return, and if a buffer is supplied 109 * that's not large enough for all the data SIOCGIFCONF could return, 110 * on at least some platforms it just returns the data that'd fit with 111 * no indication that there wasn't enough room for all the data, much 112 * less an indication of how much more room is required. 113 * 114 * The only way to ensure that we got all the data is to pass a buffer 115 * large enough that the amount of space in the buffer *not* filled in 116 * is greater than the largest possible entry. 117 * 118 * We assume that's "sizeof(ifreq.ifr_name)" plus 255, under the assumption 119 * that no address is more than 255 bytes (on systems where the "sa_len" 120 * field in a "struct sockaddr" is 1 byte, e.g. newer BSDs, that's the 121 * case, and addresses are unlikely to be bigger than that in any case). 122 */ 123 #define MAX_SA_LEN 255 124 125 /* 126 * Get a list of all interfaces that are up and that we can open. 127 * Returns -1 on error, 0 otherwise. 128 * The list, as returned through "alldevsp", may be null if no interfaces 129 * were up and could be opened. 130 * 131 * This is the implementation used on platforms that have SIOCGIFCONF but 132 * don't have any other mechanism for getting a list of interfaces. 133 * 134 * XXX - or platforms that have other, better mechanisms but for which 135 * we don't yet have code to use that mechanism; I think there's a better 136 * way on Linux, for example. 137 */ 138 int 139 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf) 140 { 141 pcap_if_t *devlist = NULL; 142 register int fd; 143 register struct ifreq *ifrp, *ifend, *ifnext; 144 int n; 145 struct ifconf ifc; 146 char *buf = NULL; 147 unsigned buf_size; 148 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER) 149 char *p, *q; 150 #endif 151 struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr; 152 struct sockaddr *netmask, *broadaddr, *dstaddr; 153 size_t netmask_size, broadaddr_size, dstaddr_size; 154 int ret = 0; 155 156 /* 157 * Create a socket from which to fetch the list of interfaces. 158 */ 159 fd = socket(AF_INET, SOCK_DGRAM, 0); 160 if (fd < 0) { 161 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 162 "socket: %s", pcap_strerror(errno)); 163 return (-1); 164 } 165 166 /* 167 * Start with an 8K buffer, and keep growing the buffer until 168 * we have more than "sizeof(ifrp->ifr_name) + MAX_SA_LEN" 169 * bytes left over in the buffer or we fail to get the 170 * interface list for some reason other than EINVAL (which is 171 * presumed here to mean "buffer is too small"). 172 */ 173 buf_size = 8192; 174 for (;;) { 175 buf = malloc(buf_size); 176 if (buf == NULL) { 177 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 178 "malloc: %s", pcap_strerror(errno)); 179 (void)close(fd); 180 return (-1); 181 } 182 183 ifc.ifc_len = buf_size; 184 ifc.ifc_buf = buf; 185 memset(buf, 0, buf_size); 186 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 187 && errno != EINVAL) { 188 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 189 "SIOCGIFCONF: %s", pcap_strerror(errno)); 190 (void)close(fd); 191 free(buf); 192 return (-1); 193 } 194 if (ifc.ifc_len < buf_size && 195 (buf_size - ifc.ifc_len) > sizeof(ifrp->ifr_name) + MAX_SA_LEN) 196 break; 197 free(buf); 198 buf_size *= 2; 199 } 200 201 ifrp = (struct ifreq *)buf; 202 ifend = (struct ifreq *)(buf + ifc.ifc_len); 203 204 for (; ifrp < ifend; ifrp = ifnext) { 205 /* 206 * XXX - what if this isn't an IPv4 address? Can 207 * we still get the netmask, etc. with ioctls on 208 * an IPv4 socket? 209 * 210 * The answer is probably platform-dependent, and 211 * if the answer is "no" on more than one platform, 212 * the way you work around it is probably platform- 213 * dependent as well. 214 */ 215 n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name); 216 if (n < sizeof(*ifrp)) 217 ifnext = ifrp + 1; 218 else 219 ifnext = (struct ifreq *)((char *)ifrp + n); 220 221 /* 222 * XXX - The 32-bit compatibility layer for Linux on IA-64 223 * is slightly broken. It correctly converts the structures 224 * to and from kernel land from 64 bit to 32 bit but 225 * doesn't update ifc.ifc_len, leaving it larger than the 226 * amount really used. This means we read off the end 227 * of the buffer and encounter an interface with an 228 * "empty" name. Since this is highly unlikely to ever 229 * occur in a valid case we can just finish looking for 230 * interfaces if we see an empty name. 231 */ 232 if (!(*ifrp->ifr_name)) 233 break; 234 235 /* 236 * Skip entries that begin with "dummy". 237 * XXX - what are these? Is this Linux-specific? 238 * Are there platforms on which we shouldn't do this? 239 */ 240 if (strncmp(ifrp->ifr_name, "dummy", 5) == 0) 241 continue; 242 243 /* 244 * Get the flags for this interface, and skip it if it's 245 * not up. 246 */ 247 strncpy(ifrflags.ifr_name, ifrp->ifr_name, 248 sizeof(ifrflags.ifr_name)); 249 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) { 250 if (errno == ENXIO) 251 continue; 252 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 253 "SIOCGIFFLAGS: %.*s: %s", 254 (int)sizeof(ifrflags.ifr_name), 255 ifrflags.ifr_name, 256 pcap_strerror(errno)); 257 ret = -1; 258 break; 259 } 260 if (!(ifrflags.ifr_flags & IFF_UP)) 261 continue; 262 263 /* 264 * Get the netmask for this address on this interface. 265 */ 266 strncpy(ifrnetmask.ifr_name, ifrp->ifr_name, 267 sizeof(ifrnetmask.ifr_name)); 268 memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr, 269 sizeof(ifrnetmask.ifr_addr)); 270 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) { 271 if (errno == EADDRNOTAVAIL) { 272 /* 273 * Not available. 274 */ 275 netmask = NULL; 276 netmask_size = 0; 277 } else { 278 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 279 "SIOCGIFNETMASK: %.*s: %s", 280 (int)sizeof(ifrnetmask.ifr_name), 281 ifrnetmask.ifr_name, 282 pcap_strerror(errno)); 283 ret = -1; 284 break; 285 } 286 } else { 287 netmask = &ifrnetmask.ifr_addr; 288 netmask_size = SA_LEN(netmask); 289 } 290 291 /* 292 * Get the broadcast address for this address on this 293 * interface (if any). 294 */ 295 if (ifrflags.ifr_flags & IFF_BROADCAST) { 296 strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name, 297 sizeof(ifrbroadaddr.ifr_name)); 298 memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr, 299 sizeof(ifrbroadaddr.ifr_addr)); 300 if (ioctl(fd, SIOCGIFBRDADDR, 301 (char *)&ifrbroadaddr) < 0) { 302 if (errno == EADDRNOTAVAIL) { 303 /* 304 * Not available. 305 */ 306 broadaddr = NULL; 307 broadaddr_size = 0; 308 } else { 309 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 310 "SIOCGIFBRDADDR: %.*s: %s", 311 (int)sizeof(ifrbroadaddr.ifr_name), 312 ifrbroadaddr.ifr_name, 313 pcap_strerror(errno)); 314 ret = -1; 315 break; 316 } 317 } else { 318 broadaddr = &ifrbroadaddr.ifr_broadaddr; 319 broadaddr_size = SA_LEN(broadaddr); 320 } 321 } else { 322 /* 323 * Not a broadcast interface, so no broadcast 324 * address. 325 */ 326 broadaddr = NULL; 327 broadaddr_size = 0; 328 } 329 330 /* 331 * Get the destination address for this address on this 332 * interface (if any). 333 */ 334 if (ifrflags.ifr_flags & IFF_POINTOPOINT) { 335 strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name, 336 sizeof(ifrdstaddr.ifr_name)); 337 memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr, 338 sizeof(ifrdstaddr.ifr_addr)); 339 if (ioctl(fd, SIOCGIFDSTADDR, 340 (char *)&ifrdstaddr) < 0) { 341 if (errno == EADDRNOTAVAIL) { 342 /* 343 * Not available. 344 */ 345 dstaddr = NULL; 346 dstaddr_size = 0; 347 } else { 348 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 349 "SIOCGIFDSTADDR: %.*s: %s", 350 (int)sizeof(ifrdstaddr.ifr_name), 351 ifrdstaddr.ifr_name, 352 pcap_strerror(errno)); 353 ret = -1; 354 break; 355 } 356 } else { 357 dstaddr = &ifrdstaddr.ifr_dstaddr; 358 dstaddr_size = SA_LEN(dstaddr); 359 } 360 } else { 361 /* 362 * Not a point-to-point interface, so no destination 363 * address. 364 */ 365 dstaddr = NULL; 366 dstaddr_size = 0; 367 } 368 369 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER) 370 /* 371 * If this entry has a colon followed by a number at 372 * the end, it's a logical interface. Those are just 373 * the way you assign multiple IP addresses to a real 374 * interface, so an entry for a logical interface should 375 * be treated like the entry for the real interface; 376 * we do that by stripping off the ":" and the number. 377 */ 378 p = strchr(ifrp->ifr_name, ':'); 379 if (p != NULL) { 380 /* 381 * We have a ":"; is it followed by a number? 382 */ 383 q = p + 1; 384 while (isdigit((unsigned char)*q)) 385 q++; 386 if (*q == '\0') { 387 /* 388 * All digits after the ":" until the end. 389 * Strip off the ":" and everything after 390 * it. 391 */ 392 *p = '\0'; 393 } 394 } 395 #endif 396 397 /* 398 * Add information for this address to the list. 399 */ 400 if (add_addr_to_iflist(&devlist, ifrp->ifr_name, 401 ifrflags.ifr_flags, &ifrp->ifr_addr, 402 SA_LEN(&ifrp->ifr_addr), netmask, netmask_size, 403 broadaddr, broadaddr_size, dstaddr, dstaddr_size, 404 errbuf) < 0) { 405 ret = -1; 406 break; 407 } 408 } 409 free(buf); 410 (void)close(fd); 411 412 if (ret != -1) { 413 /* 414 * We haven't had any errors yet; do any platform-specific 415 * operations to add devices. 416 */ 417 if (pcap_platform_finddevs(&devlist, errbuf) < 0) 418 ret = -1; 419 } 420 421 if (ret == -1) { 422 /* 423 * We had an error; free the list we've been constructing. 424 */ 425 if (devlist != NULL) { 426 pcap_freealldevs(devlist); 427 devlist = NULL; 428 } 429 } 430 431 *alldevsp = devlist; 432 return (ret); 433 } 434