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/inet.c,v 1.74 2006-12-30 09:54:57 guy Exp $ (LBL)";
45 #include <pcap-stdinc.h>
48 #include <sys/param.h>
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
58 struct mbuf
; /* Squelch compiler warnings on some platforms for */
59 struct rtentry
; /* declarations in <net/if.h> */
61 #include <netinet/in.h>
70 #if !defined(WIN32) && !defined(__BORLANDC__)
72 #endif /* !WIN32 && !__BORLANDC__ */
76 #define INT_MAX 2147483647
81 #ifdef HAVE_OS_PROTO_H
85 /* Not all systems have IFF_LOOPBACK */
87 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
89 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
90 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
94 dup_sockaddr(struct sockaddr
*sa
, size_t sa_length
)
96 struct sockaddr
*newsa
;
98 if ((newsa
= malloc(sa_length
)) == NULL
)
100 return (memcpy(newsa
, sa
, sa_length
));
104 get_instance(const char *name
)
106 const char *cp
, *endcp
;
109 if (strcmp(name
, "any") == 0) {
111 * Give the "any" device an artificially high instance
112 * number, so it shows up after all other non-loopback
118 endcp
= name
+ strlen(name
);
119 for (cp
= name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
122 if (isdigit((unsigned char)*cp
))
130 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, const char *name
,
131 u_int flags
, const char *description
, char *errbuf
)
134 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
138 * Is there already an entry in the list for this interface?
140 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
141 if (strcmp(name
, curdev
->name
) == 0)
142 break; /* yes, we found it */
145 if (curdev
== NULL
) {
147 * No, we didn't find it.
149 * Can we open this interface for live capture?
151 * We do this check so that interfaces that are
152 * supplied by the interface enumeration mechanism
153 * we're using but that don't support packet capture
154 * aren't included in the list. Loopback interfaces
155 * on Solaris are an example of this; we don't just
156 * omit loopback interfaces on all platforms because
157 * you *can* capture on loopback interfaces on some
160 * On OS X, we don't do this check if the device
161 * name begins with "wlt"; at least some versions
162 * of OS X offer monitor mode capturing by having
163 * a separate "monitor mode" device for each wireless
164 * adapter, rather than by implementing the ioctls
165 * that {Free,Net,Open,DragonFly}BSD provide.
166 * Opening that device puts the adapter into monitor
167 * mode, which, at least for some adapters, causes
168 * them to deassociate from the network with which
169 * they're associated.
171 * Instead, we try to open the corresponding "en"
172 * device (so that we don't end up with, for users
173 * without sufficient privilege to open capture
174 * devices, a list of adapters that only includes
178 if (strncmp(name
, "wlt", 3) == 0) {
183 * Try to allocate a buffer for the "en"
186 en_name_len
= strlen(name
) - 1;
187 en_name
= malloc(en_name_len
+ 1);
188 if (en_name
== NULL
) {
189 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
190 "malloc: %s", pcap_strerror(errno
));
193 strcpy(en_name
, "en");
194 strcat(en_name
, name
+ 3);
195 p
= pcap_open_live(en_name
, 68, 0, 0, errbuf
);
199 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
202 * No. Don't bother including it.
203 * Don't treat this as an error, though.
211 * Yes, we can open it.
212 * Allocate a new entry.
214 curdev
= malloc(sizeof(pcap_if_t
));
215 if (curdev
== NULL
) {
216 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
217 "malloc: %s", pcap_strerror(errno
));
225 curdev
->name
= strdup(name
);
226 if (curdev
->name
== NULL
) {
227 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
228 "malloc: %s", pcap_strerror(errno
));
232 if (description
!= NULL
) {
234 * We have a description for this interface.
236 curdev
->description
= strdup(description
);
237 if (curdev
->description
== NULL
) {
238 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
239 "malloc: %s", pcap_strerror(errno
));
248 curdev
->description
= NULL
;
250 curdev
->addresses
= NULL
; /* list starts out as empty */
252 if (ISLOOPBACK(name
, flags
))
253 curdev
->flags
|= PCAP_IF_LOOPBACK
;
256 * Add it to the list, in the appropriate location.
257 * First, get the instance number of this interface.
259 this_instance
= get_instance(name
);
262 * Now look for the last interface with an instance number
263 * less than or equal to the new interface's instance
264 * number - except that non-loopback interfaces are
265 * arbitrarily treated as having interface numbers less
266 * than those of loopback interfaces, so the loopback
267 * interfaces are put at the end of the list.
269 * We start with "prevdev" being NULL, meaning we're before
270 * the first element in the list.
275 * Get the interface after this one.
277 if (prevdev
== NULL
) {
279 * The next element is the first element.
283 nextdev
= prevdev
->next
;
286 * Are we at the end of the list?
288 if (nextdev
== NULL
) {
290 * Yes - we have to put the new entry
297 * Is the new interface a non-loopback interface
298 * and the next interface a loopback interface?
300 if (!(curdev
->flags
& PCAP_IF_LOOPBACK
) &&
301 (nextdev
->flags
& PCAP_IF_LOOPBACK
)) {
303 * Yes, we should put the new entry
304 * before "nextdev", i.e. after "prevdev".
310 * Is the new interface's instance number less
311 * than the next interface's instance number,
312 * and is it the case that the new interface is a
313 * non-loopback interface or the next interface is
314 * a loopback interface?
316 * (The goal of both loopback tests is to make
317 * sure that we never put a loopback interface
318 * before any non-loopback interface and that we
319 * always put a non-loopback interface before all
320 * loopback interfaces.)
322 if (this_instance
< get_instance(nextdev
->name
) &&
323 (!(curdev
->flags
& PCAP_IF_LOOPBACK
) ||
324 (nextdev
->flags
& PCAP_IF_LOOPBACK
))) {
326 * Yes - we should put the new entry
327 * before "nextdev", i.e. after "prevdev".
336 * Insert before "nextdev".
338 curdev
->next
= nextdev
;
341 * Insert after "prevdev" - unless "prevdev" is null,
342 * in which case this is the first interface.
344 if (prevdev
== NULL
) {
346 * This is the first interface. Pass back a
347 * pointer to it, and put "curdev" before
352 prevdev
->next
= curdev
;
355 *curdev_ret
= curdev
;
360 * XXX - on FreeBSDs that support it, should it get the sysctl named
361 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
362 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
363 * with my Cisco 350 card, so the name isn't entirely descriptive. The
364 * "dev.an.0.%pnpinfo" has a better description, although one might argue
365 * that the problem is really a driver bug - if it can find out that it's
366 * a Cisco 340 or 350, rather than an old Aironet card, it should use
367 * that in the description.
369 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well?
370 * Do any other UN*Xes support getting a description?
373 add_addr_to_iflist(pcap_if_t
**alldevs
, const char *name
, u_int flags
,
374 struct sockaddr
*addr
, size_t addr_size
,
375 struct sockaddr
*netmask
, size_t netmask_size
,
376 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
377 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
381 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
383 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, NULL
, errbuf
) == -1) {
389 if (curdev
== NULL
) {
391 * Device wasn't added because it can't be opened.
398 * "curdev" is an entry for this interface; add an entry for this
399 * address to its list of addresses.
401 * Allocate the new entry and fill it in.
403 curaddr
= malloc(sizeof(pcap_addr_t
));
404 if (curaddr
== NULL
) {
405 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
406 "malloc: %s", pcap_strerror(errno
));
410 curaddr
->next
= NULL
;
412 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
413 if (curaddr
->addr
== NULL
) {
414 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
415 "malloc: %s", pcap_strerror(errno
));
420 curaddr
->addr
= NULL
;
422 if (netmask
!= NULL
) {
423 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
424 if (curaddr
->netmask
== NULL
) {
425 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
426 "malloc: %s", pcap_strerror(errno
));
427 if (curaddr
->addr
!= NULL
)
433 curaddr
->netmask
= NULL
;
435 if (broadaddr
!= NULL
) {
436 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
437 if (curaddr
->broadaddr
== NULL
) {
438 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
439 "malloc: %s", pcap_strerror(errno
));
440 if (curaddr
->netmask
!= NULL
)
441 free(curaddr
->netmask
);
442 if (curaddr
->addr
!= NULL
)
448 curaddr
->broadaddr
= NULL
;
450 if (dstaddr
!= NULL
) {
451 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
452 if (curaddr
->dstaddr
== NULL
) {
453 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
454 "malloc: %s", pcap_strerror(errno
));
455 if (curaddr
->broadaddr
!= NULL
)
456 free(curaddr
->broadaddr
);
457 if (curaddr
->netmask
!= NULL
)
458 free(curaddr
->netmask
);
459 if (curaddr
->addr
!= NULL
)
465 curaddr
->dstaddr
= NULL
;
468 * Find the end of the list of addresses.
470 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
471 nextaddr
= prevaddr
->next
;
472 if (nextaddr
== NULL
) {
474 * This is the end of the list.
480 if (prevaddr
== NULL
) {
482 * The list was empty; this is the first member.
484 curdev
->addresses
= curaddr
;
487 * "prevaddr" is the last member of the list; append
490 prevaddr
->next
= curaddr
;
497 pcap_add_if(pcap_if_t
**devlist
, const char *name
, u_int flags
,
498 const char *description
, char *errbuf
)
502 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
508 * Free a list of interfaces.
511 pcap_freealldevs(pcap_if_t
*alldevs
)
513 pcap_if_t
*curdev
, *nextdev
;
514 pcap_addr_t
*curaddr
, *nextaddr
;
516 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
517 nextdev
= curdev
->next
;
520 * Free all addresses.
522 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
523 nextaddr
= curaddr
->next
;
526 if (curaddr
->netmask
)
527 free(curaddr
->netmask
);
528 if (curaddr
->broadaddr
)
529 free(curaddr
->broadaddr
);
530 if (curaddr
->dstaddr
)
531 free(curaddr
->dstaddr
);
536 * Free the name string.
541 * Free the description string, if any.
543 if (curdev
->description
!= NULL
)
544 free(curdev
->description
);
547 * Free the interface.
553 #if !defined(WIN32) && !defined(MSDOS)
556 * Return the name of a network interface attached to the system, or NULL
557 * if none can be found. The interface must be configured up; the
558 * lowest unit number is preferred; loopback is ignored.
561 pcap_lookupdev(errbuf
)
562 register char *errbuf
;
565 /* for old BSD systems, including bsdi3 */
567 #define IF_NAMESIZE IFNAMSIZ
569 static char device
[IF_NAMESIZE
+ 1];
572 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
575 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
577 * There are no devices on the list, or the first device
578 * on the list is a loopback device, which means there
579 * are no non-loopback devices on the list. This means
580 * we can't return any device.
582 * XXX - why not return a loopback device? If we can't
583 * capture on it, it won't be on the list, and if it's
584 * on the list, there aren't any non-loopback devices,
585 * so why not just supply it as the default device?
587 (void)strlcpy(errbuf
, "no suitable device found",
592 * Return the name of the first device on the list.
594 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
598 pcap_freealldevs(alldevs
);
603 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
604 register const char *device
;
605 register bpf_u_int32
*netp
, *maskp
;
606 register char *errbuf
;
609 register struct sockaddr_in
*sin
;
613 * The pseudo-device "any" listens on all interfaces and therefore
614 * has the network address and -mask "0.0.0.0" therefore catching
615 * all traffic. Using NULL for the interface is the same as "any".
617 if (!device
|| strcmp(device
, "any") == 0
619 || strstr(device
, "dag") != NULL
621 #ifdef HAVE_SEPTEL_API
622 || strstr(device
, "septel") != NULL
624 #ifdef PCAP_SUPPORT_BT
625 || strstr(device
, "bluetooth") != NULL
627 #ifdef PCAP_SUPPORT_USB
628 || strstr(device
, "usb") != NULL
635 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
637 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
638 pcap_strerror(errno
));
641 memset(&ifr
, 0, sizeof(ifr
));
643 /* XXX Work around Linux kernel bug */
644 ifr
.ifr_addr
.sa_family
= AF_INET
;
646 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
647 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
648 if (errno
== EADDRNOTAVAIL
) {
649 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
650 "%s: no IPv4 address assigned", device
);
652 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
653 "SIOCGIFADDR: %s: %s",
654 device
, pcap_strerror(errno
));
659 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
660 *netp
= sin
->sin_addr
.s_addr
;
661 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
662 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
663 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
668 *maskp
= sin
->sin_addr
.s_addr
;
670 if (IN_CLASSA(*netp
))
671 *maskp
= IN_CLASSA_NET
;
672 else if (IN_CLASSB(*netp
))
673 *maskp
= IN_CLASSB_NET
;
674 else if (IN_CLASSC(*netp
))
675 *maskp
= IN_CLASSC_NET
;
677 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
678 "inet class for 0x%x unknown", *netp
);
689 * Return the name of a network interface attached to the system, or NULL
690 * if none can be found. The interface must be configured up; the
691 * lowest unit number is preferred; loopback is ignored.
694 pcap_lookupdev(errbuf
)
695 register char *errbuf
;
698 DWORD dwWindowsMajorVersion
;
699 dwVersion
= GetVersion(); /* get the OS version */
700 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
702 if (dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4) {
704 * Windows 95, 98, ME.
706 ULONG NameLength
= 8192;
707 static char AdaptersName
[8192];
709 if (PacketGetAdapterNames(AdaptersName
,&NameLength
) )
710 return (AdaptersName
);
715 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
717 ULONG NameLength
= 8192;
718 static WCHAR AdaptersName
[8192];
721 WCHAR
*TAdaptersName
= (WCHAR
*)malloc(8192 * sizeof(WCHAR
));
724 if(TAdaptersName
== NULL
)
726 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "memory allocation failure");
730 if ( !PacketGetAdapterNames((PTSTR
)TAdaptersName
,&NameLength
) )
732 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
733 "PacketGetAdapterNames: %s",
734 pcap_win32strerror());
740 tAstr
= (char*)TAdaptersName
;
741 tUstr
= (WCHAR
*)AdaptersName
;
744 * Convert and copy the device names
746 while(sscanf(tAstr
, "%S", tUstr
) > 0)
748 tAstr
+= strlen(tAstr
) + 1;
749 tUstr
+= wcslen(tUstr
) + 1;
758 * Copy the descriptions
762 strcpy((char*)tUstr
, tAstr
);
763 (char*)tUstr
+= strlen(tAstr
) + 1;;
764 tAstr
+= strlen(tAstr
) + 1;
768 return (char *)(AdaptersName
);
774 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
775 register const char *device
;
776 register bpf_u_int32
*netp
, *maskp
;
777 register char *errbuf
;
780 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
781 * in order to skip non IPv4 (i.e. IPv6 addresses)
783 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
784 LONG if_addr_size
= 1;
785 struct sockaddr_in
*t_addr
;
788 if (!PacketGetNetInfoEx((void *)device
, if_addrs
, &if_addr_size
)) {
793 for(i
=0; i
<MAX_NETWORK_ADDRESSES
; i
++)
795 if(if_addrs
[i
].IPAddress
.ss_family
== AF_INET
)
797 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].IPAddress
);
798 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
799 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].SubnetMask
);
800 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
812 #endif /* !WIN32 && !MSDOS */