]> The Tcpdump Group git mirrors - libpcap/blob - inet.c
75ecc12eb9abd6c0d969d1e8dfbb702eaf6017ce
[libpcap] / inet.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[] =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.49 2002-07-27 18:45:35 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 #ifdef HAVE_LIMITS_H
66 #include <limits.h>
67 #else
68 #define INT_MAX 2147483647
69 #endif
70 #ifdef HAVE_IFADDRS_H
71 #include <ifaddrs.h>
72 #endif
73
74 #include "pcap-int.h"
75
76 #ifdef HAVE_OS_PROTO_H
77 #include "os-proto.h"
78 #endif
79
80 /* Not all systems have IFF_LOOPBACK */
81 #ifdef IFF_LOOPBACK
82 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
83 #else
84 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
85 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
86 #endif
87
88 /*
89 * This is fun.
90 *
91 * In older BSD systems, socket addresses were fixed-length, and
92 * "sizeof (struct sockaddr)" gave the size of the structure.
93 * All addresses fit within a "struct sockaddr".
94 *
95 * In newer BSD systems, the socket address is variable-length, and
96 * there's an "sa_len" field giving the length of the structure;
97 * this allows socket addresses to be longer than 2 bytes of family
98 * and 14 bytes of data.
99 *
100 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
101 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
102 * than "struct sockaddr"), and some use the new BSD scheme.
103 *
104 * GNU libc uses neither scheme, but has an "SA_LEN()" macro that
105 * determines the size based on the address family.
106 */
107 #ifndef SA_LEN
108 #ifdef HAVE_SOCKADDR_SA_LEN
109 #define SA_LEN(addr) ((addr)->sa_len)
110 #else /* HAVE_SOCKADDR_SA_LEN */
111 #ifdef HAVE_SOCKADDR_STORAGE
112 #define SA_LEN(addr) (sizeof (struct sockaddr_storage))
113 #else /* HAVE_SOCKADDR_STORAGE */
114 #define SA_LEN(addr) (sizeof (struct sockaddr))
115 #endif /* HAVE_SOCKADDR_STORAGE */
116 #endif /* HAVE_SOCKADDR_SA_LEN */
117 #endif /* SA_LEN */
118
119 static struct sockaddr *
120 dup_sockaddr(struct sockaddr *sa)
121 {
122 struct sockaddr *newsa;
123 unsigned int size;
124
125 size = SA_LEN(sa);
126 if ((newsa = malloc(size)) == NULL)
127 return (NULL);
128 return (memcpy(newsa, sa, size));
129 }
130
131 static int
132 get_instance(char *name)
133 {
134 char *cp, *endcp;
135 int n;
136
137 if (strcmp(name, "any") == 0) {
138 /*
139 * Give the "any" device an artificially high instance
140 * number, so it shows up after all other non-loopback
141 * interfaces.
142 */
143 return INT_MAX;
144 }
145
146 endcp = name + strlen(name);
147 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
148 continue;
149
150 if (isdigit((unsigned char)*cp))
151 n = atoi(cp);
152 else
153 n = 0;
154 return (n);
155 }
156
157 static int
158 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, char *name,
159 u_int flags, const char *description, char *errbuf)
160 {
161 pcap_t *p;
162 pcap_if_t *curdev, *prevdev, *nextdev;
163 int this_instance;
164
165 /*
166 * Can we open this interface for live capture?
167 */
168 p = pcap_open_live(name, 68, 0, 0, errbuf);
169 if (p == NULL) {
170 /*
171 * No. Don't bother including it.
172 * Don't treat this as an error, though.
173 */
174 *curdev_ret = NULL;
175 return (0);
176 }
177 pcap_close(p);
178
179 /*
180 * Is there already an entry in the list for this interface?
181 */
182 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
183 if (strcmp(name, curdev->name) == 0)
184 break; /* yes, we found it */
185 }
186 if (curdev == NULL) {
187 /*
188 * No, we didn't find it.
189 * Allocate a new entry.
190 */
191 curdev = malloc(sizeof(pcap_if_t));
192 if (curdev == NULL) {
193 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
194 "malloc: %s", pcap_strerror(errno));
195 return (-1);
196 }
197
198 /*
199 * Fill in the entry.
200 */
201 curdev->next = NULL;
202 curdev->name = malloc(strlen(name) + 1);
203 strcpy(curdev->name, name);
204 if (description != NULL) {
205 /*
206 * We have a description for this interface.
207 */
208 curdev->description = malloc(strlen(description) + 1);
209 strcpy(curdev->description, description);
210 } else {
211 /*
212 * We don't.
213 */
214 curdev->description = NULL;
215 }
216 curdev->addresses = NULL; /* list starts out as empty */
217 curdev->flags = 0;
218 if (ISLOOPBACK(name, flags))
219 curdev->flags |= PCAP_IF_LOOPBACK;
220
221 /*
222 * Add it to the list, in the appropriate location.
223 * First, get the instance number of this interface.
224 */
225 this_instance = get_instance(name);
226
227 /*
228 * Now look for the last interface with an instance number
229 * less than or equal to the new interface's instance
230 * number - except that non-loopback interfaces are
231 * arbitrarily treated as having interface numbers less
232 * than those of loopback interfaces, so the loopback
233 * interfaces are put at the end of the list.
234 *
235 * We start with "prevdev" being NULL, meaning we're before
236 * the first element in the list.
237 */
238 prevdev = NULL;
239 for (;;) {
240 /*
241 * Get the interface after this one.
242 */
243 if (prevdev == NULL) {
244 /*
245 * The next element is the first element.
246 */
247 nextdev = *alldevs;
248 } else
249 nextdev = prevdev->next;
250
251 /*
252 * Are we at the end of the list?
253 */
254 if (nextdev == NULL) {
255 /*
256 * Yes - we have to put the new entry
257 * after "prevdev".
258 */
259 break;
260 }
261
262 /*
263 * Is the new interface a non-loopback interface
264 * and the next interface a loopback interface?
265 */
266 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
267 (nextdev->flags & PCAP_IF_LOOPBACK)) {
268 /*
269 * Yes, we should put the new entry
270 * before "nextdev", i.e. after "prevdev".
271 */
272 break;
273 }
274
275 /*
276 * Is the new interface's instance number less
277 * than the next interface's instance number,
278 * and is it the case that the new interface is a
279 * non-loopback interface or the next interface is
280 * a loopback interface?
281 *
282 * (The goal of both loopback tests is to make
283 * sure that we never put a loopback interface
284 * before any non-loopback interface and that we
285 * always put a non-loopback interface before all
286 * loopback interfaces.)
287 */
288 if (this_instance < get_instance(nextdev->name) &&
289 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
290 (nextdev->flags & PCAP_IF_LOOPBACK))) {
291 /*
292 * Yes - we should put the new entry
293 * before "nextdev", i.e. after "prevdev".
294 */
295 break;
296 }
297
298 prevdev = nextdev;
299 }
300
301 /*
302 * Insert before "nextdev".
303 */
304 curdev->next = nextdev;
305
306 /*
307 * Insert after "prevdev" - unless "prevdev" is null,
308 * in which case this is the first interface.
309 */
310 if (prevdev == NULL) {
311 /*
312 * This is the first interface. Pass back a
313 * pointer to it, and put "curdev" before
314 * "nextdev".
315 */
316 *alldevs = curdev;
317 } else
318 prevdev->next = curdev;
319 }
320
321 *curdev_ret = curdev;
322 return (0);
323 }
324
325 int
326 add_addr_to_iflist(pcap_if_t **alldevs, char *name, u_int flags,
327 struct sockaddr *addr, struct sockaddr *netmask,
328 struct sockaddr *broadaddr, struct sockaddr *dstaddr, char *errbuf)
329 {
330 pcap_if_t *curdev;
331 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
332
333 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
334 /*
335 * Error - give up.
336 */
337 return (-1);
338 }
339 if (curdev == NULL) {
340 /*
341 * Device wasn't added because it can't be opened.
342 * Not a fatal error.
343 */
344 return (0);
345 }
346
347 /*
348 * "curdev" is an entry for this interface; add an entry for this
349 * address to its list of addresses.
350 *
351 * Allocate the new entry and fill it in.
352 */
353 curaddr = malloc(sizeof(pcap_addr_t));
354 if (curaddr == NULL) {
355 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
356 "malloc: %s", pcap_strerror(errno));
357 return (-1);
358 }
359
360 curaddr->next = NULL;
361 if (addr != NULL) {
362 curaddr->addr = dup_sockaddr(addr);
363 if (curaddr->addr == NULL) {
364 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
365 "malloc: %s", pcap_strerror(errno));
366 free(curaddr);
367 return (-1);
368 }
369 } else
370 curaddr->addr = NULL;
371
372 if (netmask != NULL) {
373 curaddr->netmask = dup_sockaddr(netmask);
374 if (curaddr->netmask == NULL) {
375 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
376 "malloc: %s", pcap_strerror(errno));
377 free(curaddr);
378 return (-1);
379 }
380 } else
381 curaddr->netmask = NULL;
382
383 if (broadaddr != NULL) {
384 curaddr->broadaddr = dup_sockaddr(broadaddr);
385 if (curaddr->broadaddr == NULL) {
386 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
387 "malloc: %s", pcap_strerror(errno));
388 free(curaddr);
389 return (-1);
390 }
391 } else
392 curaddr->broadaddr = NULL;
393
394 if (dstaddr != NULL) {
395 curaddr->dstaddr = dup_sockaddr(dstaddr);
396 if (curaddr->dstaddr == NULL) {
397 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
398 "malloc: %s", pcap_strerror(errno));
399 free(curaddr);
400 return (-1);
401 }
402 } else
403 curaddr->dstaddr = NULL;
404
405 /*
406 * Find the end of the list of addresses.
407 */
408 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
409 nextaddr = prevaddr->next;
410 if (nextaddr == NULL) {
411 /*
412 * This is the end of the list.
413 */
414 break;
415 }
416 }
417
418 if (prevaddr == NULL) {
419 /*
420 * The list was empty; this is the first member.
421 */
422 curdev->addresses = curaddr;
423 } else {
424 /*
425 * "prevaddr" is the last member of the list; append
426 * this member to it.
427 */
428 prevaddr->next = curaddr;
429 }
430
431 return (0);
432 }
433
434 int
435 pcap_add_if(pcap_if_t **devlist, char *name, u_int flags,
436 const char *description, char *errbuf)
437 {
438 pcap_if_t *curdev;
439
440 return (add_or_find_if(&curdev, devlist, name, flags, description,
441 errbuf));
442 }
443
444
445 /*
446 * Free a list of interfaces.
447 */
448 void
449 pcap_freealldevs(pcap_if_t *alldevs)
450 {
451 pcap_if_t *curdev, *nextdev;
452 pcap_addr_t *curaddr, *nextaddr;
453
454 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
455 nextdev = curdev->next;
456
457 /*
458 * Free all addresses.
459 */
460 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
461 nextaddr = curaddr->next;
462 if (curaddr->addr)
463 free(curaddr->addr);
464 if (curaddr->netmask)
465 free(curaddr->netmask);
466 if (curaddr->broadaddr)
467 free(curaddr->broadaddr);
468 if (curaddr->dstaddr)
469 free(curaddr->dstaddr);
470 free(curaddr);
471 }
472
473 /*
474 * Free the name string.
475 */
476 free(curdev->name);
477
478 /*
479 * Free the description string, if any.
480 */
481 if (curdev->description != NULL)
482 free(curdev->description);
483
484 /*
485 * Free the interface.
486 */
487 free(curdev);
488 }
489 }
490
491 /*
492 * Return the name of a network interface attached to the system, or NULL
493 * if none can be found. The interface must be configured up; the
494 * lowest unit number is preferred; loopback is ignored.
495 */
496 char *
497 pcap_lookupdev(errbuf)
498 register char *errbuf;
499 {
500 pcap_if_t *alldevs;
501 /* for old BSD systems, including bsdi3 */
502 #ifndef IF_NAMESIZE
503 #define IF_NAMESIZE IFNAMSIZ
504 #endif
505 static char device[IF_NAMESIZE + 1];
506 char *ret;
507
508 if (pcap_findalldevs(&alldevs, errbuf) == -1)
509 return (NULL);
510
511 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
512 /*
513 * There are no devices on the list, or the first device
514 * on the list is a loopback device, which means there
515 * are no non-loopback devices on the list. This means
516 * we can't return any device.
517 *
518 * XXX - why not return a loopback device? If we can't
519 * capture on it, it won't be on the list, and if it's
520 * on the list, there aren't any non-loopback devices,
521 * so why not just supply it as the default device?
522 */
523 (void)strlcpy(errbuf, "no suitable device found",
524 PCAP_ERRBUF_SIZE);
525 ret = NULL;
526 } else {
527 /*
528 * Return the name of the first device on the list.
529 */
530 (void)strlcpy(device, alldevs->name, sizeof(device));
531 ret = device;
532 }
533
534 pcap_freealldevs(alldevs);
535 return (ret);
536 }
537
538 int
539 pcap_lookupnet(device, netp, maskp, errbuf)
540 register char *device;
541 register bpf_u_int32 *netp, *maskp;
542 register char *errbuf;
543 {
544 register int fd;
545 register struct sockaddr_in *sin;
546 struct ifreq ifr;
547
548 /*
549 * The pseudo-device "any" listens on all interfaces and therefore
550 * has the network address and -mask "0.0.0.0" therefore catching
551 * all traffic. Using NULL for the interface is the same as "any".
552 */
553 if (!device || strcmp(device, "any") == 0) {
554 *netp = *maskp = 0;
555 return 0;
556 }
557
558 fd = socket(AF_INET, SOCK_DGRAM, 0);
559 if (fd < 0) {
560 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
561 pcap_strerror(errno));
562 return (-1);
563 }
564 memset(&ifr, 0, sizeof(ifr));
565 #ifdef linux
566 /* XXX Work around Linux kernel bug */
567 ifr.ifr_addr.sa_family = AF_INET;
568 #endif
569 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
570 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
571 if (errno == EADDRNOTAVAIL) {
572 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
573 "%s: no IPv4 address assigned", device);
574 } else {
575 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
576 "SIOCGIFADDR: %s: %s",
577 device, pcap_strerror(errno));
578 }
579 (void)close(fd);
580 return (-1);
581 }
582 sin = (struct sockaddr_in *)&ifr.ifr_addr;
583 *netp = sin->sin_addr.s_addr;
584 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
585 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
586 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
587 (void)close(fd);
588 return (-1);
589 }
590 (void)close(fd);
591 *maskp = sin->sin_addr.s_addr;
592 if (*maskp == 0) {
593 if (IN_CLASSA(*netp))
594 *maskp = IN_CLASSA_NET;
595 else if (IN_CLASSB(*netp))
596 *maskp = IN_CLASSB_NET;
597 else if (IN_CLASSC(*netp))
598 *maskp = IN_CLASSC_NET;
599 else {
600 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
601 "inet class for 0x%x unknown", *netp);
602 return (-1);
603 }
604 }
605 *netp &= *maskp;
606 return (0);
607 }