1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 static const char rcsid[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/fad-gifc.c,v 1.4.2.1 2003/11/15 23:26:39 guy Exp $ (LBL)";
44 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
51 #include <sys/time.h> /* concession to AIX */
53 struct mbuf; /* Squelch compiler warnings on some platforms for */
54 struct rtentry; /* declarations in <net/if.h> */
56 #include <netinet/in.h>
68 #ifdef HAVE_OS_PROTO_H
75 * In older BSD systems, socket addresses were fixed-length, and
76 * "sizeof (struct sockaddr)" gave the size of the structure.
77 * All addresses fit within a "struct sockaddr".
79 * In newer BSD systems, the socket address is variable-length, and
80 * there's an "sa_len" field giving the length of the structure;
81 * this allows socket addresses to be longer than 2 bytes of family
82 * and 14 bytes of data.
84 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
85 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
86 * than "struct sockaddr"), and some use the new BSD scheme.
88 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
89 * macro that determines the size based on the address family. Other
90 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
91 * but not in the final version).
93 * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have
94 * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the
95 * address in an entry returned by SIOCGIFCONF.
98 #ifdef HAVE_SOCKADDR_SA_LEN
99 #define SA_LEN(addr) ((addr)->sa_len)
100 #else /* HAVE_SOCKADDR_SA_LEN */
101 #define SA_LEN(addr) (sizeof (struct sockaddr))
102 #endif /* HAVE_SOCKADDR_SA_LEN */
105 #ifdef HAVE_PROC_NET_DEV
107 * Get from "/proc/net/dev" all interfaces listed there; if they're
108 * already in the list of interfaces we have, that won't add another
109 * instance, but if they're not, that'll add them.
111 * We don't bother getting any addresses for them; it appears you can't
112 * use SIOCGIFADDR on Linux to get IPv6 addresses for interfaces, and,
113 * although some other types of addresses can be fetched with SIOCGIFADDR,
114 * we don't bother with them for now.
116 * We also don't fail if we couldn't open "/proc/net/dev"; we just leave
117 * the list of interfaces as is.
120 scan_proc_net_dev(pcap_if_t **devlistp, int fd, char *errbuf)
126 char name[512]; /* XXX - pick a size */
128 struct ifreq ifrflags;
131 proc_net_f = fopen("/proc/net/dev", "r");
132 if (proc_net_f == NULL)
136 fgets(linebuf, sizeof linebuf, proc_net_f) != NULL; linenum++) {
138 * Skip the first two lines - they're headers.
146 * Skip leading white space.
148 while (*p != '\0' && isspace(*p))
150 if (*p == '\0' || *p == '\n')
151 continue; /* blank line */
154 * Get the interface name.
157 while (*p != '\0' && !isspace(*p)) {
160 * This could be the separator between a
161 * name and an alias number, or it could be
162 * the separator between a name with no
163 * alias number and the next field.
165 * If there's a colon after digits, it
166 * separates the name and the alias number,
167 * otherwise it separates the name and the
175 * That was the next field,
176 * not the alias number.
187 * Get the flags for this interface, and skip it if
190 strncpy(ifrflags.ifr_name, name, sizeof(ifrflags.ifr_name));
191 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
194 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
195 "SIOCGIFFLAGS: %.*s: %s",
196 (int)sizeof(ifrflags.ifr_name),
198 pcap_strerror(errno));
202 if (!(ifrflags.ifr_flags & IFF_UP))
206 * Add an entry for this interface, with no addresses.
208 if (pcap_add_if(devlistp, name, ifrflags.ifr_flags, NULL,
219 * Well, we didn't fail for any other reason; did we
220 * fail due to an error reading the file?
222 if (ferror(proc_net_f)) {
223 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
224 "Error reading /proc/net/dev: %s",
225 pcap_strerror(errno));
230 (void)fclose(proc_net_f);
233 #endif /* HAVE_PROC_NET_DEV */
236 * Get a list of all interfaces that are up and that we can open.
237 * Returns -1 on error, 0 otherwise.
238 * The list, as returned through "alldevsp", may be null if no interfaces
239 * were up and could be opened.
241 * This is the implementation used on platforms that have SIOCGIFCONF but
242 * don't have any other mechanism for getting a list of interfaces.
244 * XXX - or platforms that have other, better mechanisms but for which
245 * we don't yet have code to use that mechanism; I think there's a better
246 * way on Linux, for example.
249 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
251 pcap_if_t *devlist = NULL;
253 register struct ifreq *ifrp, *ifend, *ifnext;
258 struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
259 struct sockaddr *netmask, *broadaddr, *dstaddr;
260 size_t netmask_size, broadaddr_size, dstaddr_size;
264 * Create a socket from which to fetch the list of interfaces.
266 fd = socket(AF_INET, SOCK_DGRAM, 0);
268 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
269 "socket: %s", pcap_strerror(errno));
274 * Start with an 8K buffer, and keep growing the buffer until
275 * we get the entire interface list or fail to get it for some
276 * reason other than EINVAL (which is presumed here to mean
277 * "buffer is too small").
281 buf = malloc(buf_size);
283 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
284 "malloc: %s", pcap_strerror(errno));
289 ifc.ifc_len = buf_size;
291 memset(buf, 0, buf_size);
292 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
293 && errno != EINVAL) {
294 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
295 "SIOCGIFCONF: %s", pcap_strerror(errno));
300 if (ifc.ifc_len < buf_size)
306 ifrp = (struct ifreq *)buf;
307 ifend = (struct ifreq *)(buf + ifc.ifc_len);
309 for (; ifrp < ifend; ifrp = ifnext) {
311 * XXX - what if this isn't an IPv4 address? Can
312 * we still get the netmask, etc. with ioctls on
315 * The answer is probably platform-dependent, and
316 * if the answer is "no" on more than one platform,
317 * the way you work around it is probably platform-
320 n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
321 if (n < sizeof(*ifrp))
324 ifnext = (struct ifreq *)((char *)ifrp + n);
327 * Get the flags for this interface, and skip it if it's
330 strncpy(ifrflags.ifr_name, ifrp->ifr_name,
331 sizeof(ifrflags.ifr_name));
332 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
335 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
336 "SIOCGIFFLAGS: %.*s: %s",
337 (int)sizeof(ifrflags.ifr_name),
339 pcap_strerror(errno));
343 if (!(ifrflags.ifr_flags & IFF_UP))
347 * Get the netmask for this address on this interface.
349 strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
350 sizeof(ifrnetmask.ifr_name));
351 memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
352 sizeof(ifrnetmask.ifr_addr));
353 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
354 if (errno == EADDRNOTAVAIL) {
361 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
362 "SIOCGIFNETMASK: %.*s: %s",
363 (int)sizeof(ifrnetmask.ifr_name),
365 pcap_strerror(errno));
370 netmask = &ifrnetmask.ifr_addr;
371 netmask_size = SA_LEN(netmask);
375 * Get the broadcast address for this address on this
376 * interface (if any).
378 if (ifrflags.ifr_flags & IFF_BROADCAST) {
379 strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
380 sizeof(ifrbroadaddr.ifr_name));
381 memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
382 sizeof(ifrbroadaddr.ifr_addr));
383 if (ioctl(fd, SIOCGIFBRDADDR,
384 (char *)&ifrbroadaddr) < 0) {
385 if (errno == EADDRNOTAVAIL) {
392 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
393 "SIOCGIFBRDADDR: %.*s: %s",
394 (int)sizeof(ifrbroadaddr.ifr_name),
395 ifrbroadaddr.ifr_name,
396 pcap_strerror(errno));
401 broadaddr = &ifrbroadaddr.ifr_broadaddr;
402 broadaddr_size = SA_LEN(broadaddr);
406 * Not a broadcast interface, so no broadcast
414 * Get the destination address for this address on this
415 * interface (if any).
417 if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
418 strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
419 sizeof(ifrdstaddr.ifr_name));
420 memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
421 sizeof(ifrdstaddr.ifr_addr));
422 if (ioctl(fd, SIOCGIFDSTADDR,
423 (char *)&ifrdstaddr) < 0) {
424 if (errno == EADDRNOTAVAIL) {
431 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
432 "SIOCGIFDSTADDR: %.*s: %s",
433 (int)sizeof(ifrdstaddr.ifr_name),
435 pcap_strerror(errno));
440 dstaddr = &ifrdstaddr.ifr_dstaddr;
441 dstaddr_size = SA_LEN(dstaddr);
445 * Not a point-to-point interface, so no destination
453 * Add information for this address to the list.
455 if (add_addr_to_iflist(&devlist, ifrp->ifr_name,
456 ifrflags.ifr_flags, &ifrp->ifr_addr,
457 SA_LEN(&ifrp->ifr_addr), netmask, netmask_size,
458 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
466 #ifdef HAVE_PROC_NET_DEV
469 * We haven't had any errors yet; now read "/proc/net/dev",
470 * and add to the list of interfaces all interfaces listed
471 * there that we don't already have, because, on Linux,
472 * SIOCGIFCONF reports only interfaces with IPv4 addresses,
473 * so you need to read "/proc/net/dev" to get the names of
474 * the rest of the interfaces.
476 ret = scan_proc_net_dev(&devlist, fd, errbuf);
483 * We haven't had any errors yet; do any platform-specific
484 * operations to add devices.
486 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
492 * We had an error; free the list we've been constructing.
494 if (devlist != NULL) {
495 pcap_freealldevs(devlist);