]> The Tcpdump Group git mirrors - libpcap/blob - fad-gifc.c
AC_TRY_COMPILE works only for code that fits inside "main()"; the test
[libpcap] / fad-gifc.c
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.10 2005-04-08 02:15:49 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/file.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
50 #endif
51 #include <sys/time.h> /* concession to AIX */
52
53 struct mbuf; /* Squelch compiler warnings on some platforms for */
54 struct rtentry; /* declarations in <net/if.h> */
55 #include <net/if.h>
56 #include <netinet/in.h>
57
58 #include <ctype.h>
59 #include <errno.h>
60 #include <memory.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "pcap-int.h"
67
68 #ifdef HAVE_OS_PROTO_H
69 #include "os-proto.h"
70 #endif
71
72 /*
73 * This is fun.
74 *
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".
78 *
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.
83 *
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.
87 *
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).
92 *
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.
96 */
97 #ifndef SA_LEN
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 */
103 #endif /* SA_LEN */
104
105 #ifdef HAVE_PROC_NET_DEV
106 /*
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.
110 *
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.
115 *
116 * We also don't fail if we couldn't open "/proc/net/dev"; we just leave
117 * the list of interfaces as is.
118 */
119 static int
120 scan_proc_net_dev(pcap_if_t **devlistp, int fd, char *errbuf)
121 {
122 FILE *proc_net_f;
123 char linebuf[512];
124 int linenum;
125 unsigned char *p;
126 char name[512]; /* XXX - pick a size */
127 char *q, *saveq;
128 struct ifreq ifrflags;
129 int ret = 0;
130
131 proc_net_f = fopen("/proc/net/dev", "r");
132 if (proc_net_f == NULL)
133 return (0);
134
135 for (linenum = 1;
136 fgets(linebuf, sizeof linebuf, proc_net_f) != NULL; linenum++) {
137 /*
138 * Skip the first two lines - they're headers.
139 */
140 if (linenum <= 2)
141 continue;
142
143 p = &linebuf[0];
144
145 /*
146 * Skip leading white space.
147 */
148 while (*p != '\0' && isspace(*p))
149 p++;
150 if (*p == '\0' || *p == '\n')
151 continue; /* blank line */
152
153 /*
154 * Get the interface name.
155 */
156 q = &name[0];
157 while (*p != '\0' && !isspace(*p)) {
158 if (*p == ':') {
159 /*
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.
164 *
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
168 * next field.
169 */
170 saveq = q;
171 while (isdigit(*p))
172 *q++ = *p++;
173 if (*p != ':') {
174 /*
175 * That was the next field,
176 * not the alias number.
177 */
178 q = saveq;
179 }
180 break;
181 } else
182 *q++ = *p++;
183 }
184 *q = '\0';
185
186 /*
187 * Get the flags for this interface, and skip it if
188 * it's not up.
189 */
190 strncpy(ifrflags.ifr_name, name, sizeof(ifrflags.ifr_name));
191 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
192 if (errno == ENXIO)
193 continue;
194 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
195 "SIOCGIFFLAGS: %.*s: %s",
196 (int)sizeof(ifrflags.ifr_name),
197 ifrflags.ifr_name,
198 pcap_strerror(errno));
199 ret = -1;
200 break;
201 }
202 if (!(ifrflags.ifr_flags & IFF_UP))
203 continue;
204
205 /*
206 * Add an entry for this interface, with no addresses.
207 */
208 if (pcap_add_if(devlistp, name, ifrflags.ifr_flags, NULL,
209 errbuf) == -1) {
210 /*
211 * Failure.
212 */
213 ret = -1;
214 break;
215 }
216 }
217 if (ret != -1) {
218 /*
219 * Well, we didn't fail for any other reason; did we
220 * fail due to an error reading the file?
221 */
222 if (ferror(proc_net_f)) {
223 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
224 "Error reading /proc/net/dev: %s",
225 pcap_strerror(errno));
226 ret = -1;
227 }
228 }
229
230 (void)fclose(proc_net_f);
231 return (ret);
232 }
233 #endif /* HAVE_PROC_NET_DEV */
234
235 /*
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.
240 *
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.
243 *
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.
247 */
248 int
249 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
250 {
251 pcap_if_t *devlist = NULL;
252 register int fd;
253 register struct ifreq *ifrp, *ifend, *ifnext;
254 int n;
255 struct ifconf ifc;
256 char *buf = NULL;
257 unsigned buf_size;
258 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
259 char *p, *q;
260 #endif
261 struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
262 struct sockaddr *netmask, *broadaddr, *dstaddr;
263 size_t netmask_size, broadaddr_size, dstaddr_size;
264 int ret = 0;
265
266 /*
267 * Create a socket from which to fetch the list of interfaces.
268 */
269 fd = socket(AF_INET, SOCK_DGRAM, 0);
270 if (fd < 0) {
271 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
272 "socket: %s", pcap_strerror(errno));
273 return (-1);
274 }
275
276 /*
277 * Start with an 8K buffer, and keep growing the buffer until
278 * we get the entire interface list or fail to get it for some
279 * reason other than EINVAL (which is presumed here to mean
280 * "buffer is too small").
281 */
282 buf_size = 8192;
283 for (;;) {
284 buf = malloc(buf_size);
285 if (buf == NULL) {
286 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
287 "malloc: %s", pcap_strerror(errno));
288 (void)close(fd);
289 return (-1);
290 }
291
292 ifc.ifc_len = buf_size;
293 ifc.ifc_buf = buf;
294 memset(buf, 0, buf_size);
295 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
296 && errno != EINVAL) {
297 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
298 "SIOCGIFCONF: %s", pcap_strerror(errno));
299 (void)close(fd);
300 free(buf);
301 return (-1);
302 }
303 if (ifc.ifc_len < buf_size)
304 break;
305 free(buf);
306 buf_size *= 2;
307 }
308
309 ifrp = (struct ifreq *)buf;
310 ifend = (struct ifreq *)(buf + ifc.ifc_len);
311
312 for (; ifrp < ifend; ifrp = ifnext) {
313 /*
314 * XXX - what if this isn't an IPv4 address? Can
315 * we still get the netmask, etc. with ioctls on
316 * an IPv4 socket?
317 *
318 * The answer is probably platform-dependent, and
319 * if the answer is "no" on more than one platform,
320 * the way you work around it is probably platform-
321 * dependent as well.
322 */
323 n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
324 if (n < sizeof(*ifrp))
325 ifnext = ifrp + 1;
326 else
327 ifnext = (struct ifreq *)((char *)ifrp + n);
328
329 /*
330 * XXX - The 32-bit compatibility layer for Linux on IA-64
331 * is slightly broken. It correctly converts the structures
332 * to and from kernel land from 64 bit to 32 bit but
333 * doesn't update ifc.ifc_len, leaving it larger than the
334 * amount really used. This means we read off the end
335 * of the buffer and encounter an interface with an
336 * "empty" name. Since this is highly unlikely to ever
337 * occur in a valid case we can just finish looking for
338 * interfaces if we see an empty name.
339 */
340 if (!(*ifrp->ifr_name))
341 break;
342
343 /*
344 * Skip entries that begin with "dummy".
345 * XXX - what are these? Is this Linux-specific?
346 * Are there platforms on which we shouldn't do this?
347 */
348 if (strncmp(ifrp->ifr_name, "dummy", 5) == 0)
349 continue;
350
351 /*
352 * Get the flags for this interface, and skip it if it's
353 * not up.
354 */
355 strncpy(ifrflags.ifr_name, ifrp->ifr_name,
356 sizeof(ifrflags.ifr_name));
357 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
358 if (errno == ENXIO)
359 continue;
360 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
361 "SIOCGIFFLAGS: %.*s: %s",
362 (int)sizeof(ifrflags.ifr_name),
363 ifrflags.ifr_name,
364 pcap_strerror(errno));
365 ret = -1;
366 break;
367 }
368 if (!(ifrflags.ifr_flags & IFF_UP))
369 continue;
370
371 /*
372 * Get the netmask for this address on this interface.
373 */
374 strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
375 sizeof(ifrnetmask.ifr_name));
376 memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
377 sizeof(ifrnetmask.ifr_addr));
378 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
379 if (errno == EADDRNOTAVAIL) {
380 /*
381 * Not available.
382 */
383 netmask = NULL;
384 netmask_size = 0;
385 } else {
386 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
387 "SIOCGIFNETMASK: %.*s: %s",
388 (int)sizeof(ifrnetmask.ifr_name),
389 ifrnetmask.ifr_name,
390 pcap_strerror(errno));
391 ret = -1;
392 break;
393 }
394 } else {
395 netmask = &ifrnetmask.ifr_addr;
396 netmask_size = SA_LEN(netmask);
397 }
398
399 /*
400 * Get the broadcast address for this address on this
401 * interface (if any).
402 */
403 if (ifrflags.ifr_flags & IFF_BROADCAST) {
404 strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
405 sizeof(ifrbroadaddr.ifr_name));
406 memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
407 sizeof(ifrbroadaddr.ifr_addr));
408 if (ioctl(fd, SIOCGIFBRDADDR,
409 (char *)&ifrbroadaddr) < 0) {
410 if (errno == EADDRNOTAVAIL) {
411 /*
412 * Not available.
413 */
414 broadaddr = NULL;
415 broadaddr_size = 0;
416 } else {
417 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
418 "SIOCGIFBRDADDR: %.*s: %s",
419 (int)sizeof(ifrbroadaddr.ifr_name),
420 ifrbroadaddr.ifr_name,
421 pcap_strerror(errno));
422 ret = -1;
423 break;
424 }
425 } else {
426 broadaddr = &ifrbroadaddr.ifr_broadaddr;
427 broadaddr_size = SA_LEN(broadaddr);
428 }
429 } else {
430 /*
431 * Not a broadcast interface, so no broadcast
432 * address.
433 */
434 broadaddr = NULL;
435 broadaddr_size = 0;
436 }
437
438 /*
439 * Get the destination address for this address on this
440 * interface (if any).
441 */
442 if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
443 strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
444 sizeof(ifrdstaddr.ifr_name));
445 memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
446 sizeof(ifrdstaddr.ifr_addr));
447 if (ioctl(fd, SIOCGIFDSTADDR,
448 (char *)&ifrdstaddr) < 0) {
449 if (errno == EADDRNOTAVAIL) {
450 /*
451 * Not available.
452 */
453 dstaddr = NULL;
454 dstaddr_size = 0;
455 } else {
456 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
457 "SIOCGIFDSTADDR: %.*s: %s",
458 (int)sizeof(ifrdstaddr.ifr_name),
459 ifrdstaddr.ifr_name,
460 pcap_strerror(errno));
461 ret = -1;
462 break;
463 }
464 } else {
465 dstaddr = &ifrdstaddr.ifr_dstaddr;
466 dstaddr_size = SA_LEN(dstaddr);
467 }
468 } else {
469 /*
470 * Not a point-to-point interface, so no destination
471 * address.
472 */
473 dstaddr = NULL;
474 dstaddr_size = 0;
475 }
476
477 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
478 /*
479 * If this entry has a colon followed by a number at
480 * the end, it's a logical interface. Those are just
481 * the way you assign multiple IP addresses to a real
482 * interface, so an entry for a logical interface should
483 * be treated like the entry for the real interface;
484 * we do that by stripping off the ":" and the number.
485 */
486 p = strchr(ifrp->ifr_name, ':');
487 if (p != NULL) {
488 /*
489 * We have a ":"; is it followed by a number?
490 */
491 q = p + 1;
492 while (isdigit((unsigned char)*q))
493 q++;
494 if (*q == '\0') {
495 /*
496 * All digits after the ":" until the end.
497 * Strip off the ":" and everything after
498 * it.
499 */
500 *p = '\0';
501 }
502 }
503 #endif
504
505 /*
506 * Add information for this address to the list.
507 */
508 if (add_addr_to_iflist(&devlist, ifrp->ifr_name,
509 ifrflags.ifr_flags, &ifrp->ifr_addr,
510 SA_LEN(&ifrp->ifr_addr), netmask, netmask_size,
511 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
512 errbuf) < 0) {
513 ret = -1;
514 break;
515 }
516 }
517 free(buf);
518
519 #ifdef HAVE_PROC_NET_DEV
520 if (ret != -1) {
521 /*
522 * We haven't had any errors yet; now read "/proc/net/dev",
523 * and add to the list of interfaces all interfaces listed
524 * there that we don't already have, because, on Linux,
525 * SIOCGIFCONF reports only interfaces with IPv4 addresses,
526 * so you need to read "/proc/net/dev" to get the names of
527 * the rest of the interfaces.
528 */
529 ret = scan_proc_net_dev(&devlist, fd, errbuf);
530 }
531 #endif
532 (void)close(fd);
533
534 if (ret != -1) {
535 /*
536 * We haven't had any errors yet; do any platform-specific
537 * operations to add devices.
538 */
539 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
540 ret = -1;
541 }
542
543 if (ret == -1) {
544 /*
545 * We had an error; free the list we've been constructing.
546 */
547 if (devlist != NULL) {
548 pcap_freealldevs(devlist);
549 devlist = NULL;
550 }
551 }
552
553 *alldevsp = devlist;
554 return (ret);
555 }