xref: /freebsd/contrib/libpcap/fad-getad.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
1feb4ecdbSBruce M Simpson /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2feb4ecdbSBruce M Simpson /*
3feb4ecdbSBruce M Simpson  * Copyright (c) 1994, 1995, 1996, 1997, 1998
4feb4ecdbSBruce M Simpson  *	The Regents of the University of California.  All rights reserved.
5feb4ecdbSBruce M Simpson  *
6feb4ecdbSBruce M Simpson  * Redistribution and use in source and binary forms, with or without
7feb4ecdbSBruce M Simpson  * modification, are permitted provided that the following conditions
8feb4ecdbSBruce M Simpson  * are met:
9feb4ecdbSBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
10feb4ecdbSBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
11feb4ecdbSBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
12feb4ecdbSBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
13feb4ecdbSBruce M Simpson  *    documentation and/or other materials provided with the distribution.
14feb4ecdbSBruce M Simpson  * 3. All advertising materials mentioning features or use of this software
15feb4ecdbSBruce M Simpson  *    must display the following acknowledgement:
16feb4ecdbSBruce M Simpson  *	This product includes software developed by the Computer Systems
17feb4ecdbSBruce M Simpson  *	Engineering Group at Lawrence Berkeley Laboratory.
18feb4ecdbSBruce M Simpson  * 4. Neither the name of the University nor of the Laboratory may be used
19feb4ecdbSBruce M Simpson  *    to endorse or promote products derived from this software without
20feb4ecdbSBruce M Simpson  *    specific prior written permission.
21feb4ecdbSBruce M Simpson  *
22feb4ecdbSBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23feb4ecdbSBruce M Simpson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24feb4ecdbSBruce M Simpson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25feb4ecdbSBruce M Simpson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26feb4ecdbSBruce M Simpson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27feb4ecdbSBruce M Simpson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28feb4ecdbSBruce M Simpson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29feb4ecdbSBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30feb4ecdbSBruce M Simpson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31feb4ecdbSBruce M Simpson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32feb4ecdbSBruce M Simpson  * SUCH DAMAGE.
33feb4ecdbSBruce M Simpson  */
34feb4ecdbSBruce M Simpson 
35feb4ecdbSBruce M Simpson #ifdef HAVE_CONFIG_H
36b00ab754SHans Petter Selasky #include <config.h>
37feb4ecdbSBruce M Simpson #endif
38feb4ecdbSBruce M Simpson 
39feb4ecdbSBruce M Simpson #include <sys/types.h>
40feb4ecdbSBruce M Simpson #include <sys/socket.h>
41feb4ecdbSBruce M Simpson #include <netinet/in.h>
42feb4ecdbSBruce M Simpson 
43feb4ecdbSBruce M Simpson #include <net/if.h>
44feb4ecdbSBruce M Simpson 
45feb4ecdbSBruce M Simpson #include <errno.h>
46feb4ecdbSBruce M Simpson #include <stdio.h>
47feb4ecdbSBruce M Simpson #include <stdlib.h>
48ee2dd488SSam Leffler #include <string.h>
49feb4ecdbSBruce M Simpson #include <ifaddrs.h>
50feb4ecdbSBruce M Simpson 
51feb4ecdbSBruce M Simpson #include "pcap-int.h"
52feb4ecdbSBruce M Simpson 
53feb4ecdbSBruce M Simpson #ifdef HAVE_OS_PROTO_H
54feb4ecdbSBruce M Simpson #include "os-proto.h"
55feb4ecdbSBruce M Simpson #endif
56feb4ecdbSBruce M Simpson 
57681ed54cSXin LI /*
58681ed54cSXin LI  * We don't do this on Solaris 11 and later, as it appears there aren't
59681ed54cSXin LI  * any AF_PACKET addresses on interfaces, so we don't need this, and
60681ed54cSXin LI  * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>,
61681ed54cSXin LI  * and their definitions of some data structures collide.
62681ed54cSXin LI  */
63681ed54cSXin LI #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
64d1e87331SXin LI # ifdef HAVE_NETPACKET_PACKET_H
65681ed54cSXin LI /* Linux distributions with newer glibc */
66d1e87331SXin LI #  include <netpacket/packet.h>
67d1e87331SXin LI # else /* HAVE_NETPACKET_PACKET_H */
68d1e87331SXin LI /* LynxOS, Linux distributions with older glibc */
69ef96d74fSMax Laier # ifdef __Lynx__
70a0ee43a1SRui Paulo /* LynxOS */
71a0ee43a1SRui Paulo #  include <netpacket/if_packet.h>
72d1e87331SXin LI # else /* __Lynx__ */
73a0ee43a1SRui Paulo /* Linux */
74a0ee43a1SRui Paulo #  include <linux/types.h>
75a0ee43a1SRui Paulo #  include <linux/if_packet.h>
76d1e87331SXin LI # endif /* __Lynx__ */
77d1e87331SXin LI # endif /* HAVE_NETPACKET_PACKET_H */
78681ed54cSXin LI #endif /* (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) */
7904fb2745SSam Leffler 
80feb4ecdbSBruce M Simpson /*
81feb4ecdbSBruce M Simpson  * This is fun.
82feb4ecdbSBruce M Simpson  *
83feb4ecdbSBruce M Simpson  * In older BSD systems, socket addresses were fixed-length, and
84feb4ecdbSBruce M Simpson  * "sizeof (struct sockaddr)" gave the size of the structure.
85feb4ecdbSBruce M Simpson  * All addresses fit within a "struct sockaddr".
86feb4ecdbSBruce M Simpson  *
87feb4ecdbSBruce M Simpson  * In newer BSD systems, the socket address is variable-length, and
88feb4ecdbSBruce M Simpson  * there's an "sa_len" field giving the length of the structure;
89feb4ecdbSBruce M Simpson  * this allows socket addresses to be longer than 2 bytes of family
90feb4ecdbSBruce M Simpson  * and 14 bytes of data.
91feb4ecdbSBruce M Simpson  *
92feb4ecdbSBruce M Simpson  * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
93feb4ecdbSBruce M Simpson  * variant of the old BSD scheme (with "struct sockaddr_storage" rather
94feb4ecdbSBruce M Simpson  * than "struct sockaddr"), and some use the new BSD scheme.
95feb4ecdbSBruce M Simpson  *
96feb4ecdbSBruce M Simpson  * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
97feb4ecdbSBruce M Simpson  * macro that determines the size based on the address family.  Other
98feb4ecdbSBruce M Simpson  * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
99feb4ecdbSBruce M Simpson  * but not in the final version).  On the latter systems, we explicitly
100feb4ecdbSBruce M Simpson  * check the AF_ type to determine the length; we assume that on
101feb4ecdbSBruce M Simpson  * all those systems we have "struct sockaddr_storage".
102feb4ecdbSBruce M Simpson  */
103feb4ecdbSBruce M Simpson #ifndef SA_LEN
104b00ab754SHans Petter Selasky #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
105feb4ecdbSBruce M Simpson #define SA_LEN(addr)	((addr)->sa_len)
106b00ab754SHans Petter Selasky #else /* HAVE_STRUCT_SOCKADDR_SA_LEN */
107b00ab754SHans Petter Selasky #ifdef HAVE_STRUCT_SOCKADDR_STORAGE
108feb4ecdbSBruce M Simpson static size_t
109feb4ecdbSBruce M Simpson get_sa_len(struct sockaddr *addr)
110feb4ecdbSBruce M Simpson {
111feb4ecdbSBruce M Simpson 	switch (addr->sa_family) {
112feb4ecdbSBruce M Simpson 
113feb4ecdbSBruce M Simpson #ifdef AF_INET
114feb4ecdbSBruce M Simpson 	case AF_INET:
115feb4ecdbSBruce M Simpson 		return (sizeof (struct sockaddr_in));
116feb4ecdbSBruce M Simpson #endif
117feb4ecdbSBruce M Simpson 
118feb4ecdbSBruce M Simpson #ifdef AF_INET6
119feb4ecdbSBruce M Simpson 	case AF_INET6:
120feb4ecdbSBruce M Simpson 		return (sizeof (struct sockaddr_in6));
121feb4ecdbSBruce M Simpson #endif
122feb4ecdbSBruce M Simpson 
123681ed54cSXin LI #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
12404fb2745SSam Leffler 	case AF_PACKET:
12504fb2745SSam Leffler 		return (sizeof (struct sockaddr_ll));
12604fb2745SSam Leffler #endif
12704fb2745SSam Leffler 
128feb4ecdbSBruce M Simpson 	default:
129feb4ecdbSBruce M Simpson 		return (sizeof (struct sockaddr));
130feb4ecdbSBruce M Simpson 	}
131feb4ecdbSBruce M Simpson }
132feb4ecdbSBruce M Simpson #define SA_LEN(addr)	(get_sa_len(addr))
133b00ab754SHans Petter Selasky #else /* HAVE_STRUCT_SOCKADDR_STORAGE */
134feb4ecdbSBruce M Simpson #define SA_LEN(addr)	(sizeof (struct sockaddr))
135b00ab754SHans Petter Selasky #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
136b00ab754SHans Petter Selasky #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
137feb4ecdbSBruce M Simpson #endif /* SA_LEN */
138feb4ecdbSBruce M Simpson 
139feb4ecdbSBruce M Simpson /*
140feb4ecdbSBruce M Simpson  * Get a list of all interfaces that are up and that we can open.
141feb4ecdbSBruce M Simpson  * Returns -1 on error, 0 otherwise.
142feb4ecdbSBruce M Simpson  * The list, as returned through "alldevsp", may be null if no interfaces
143681ed54cSXin LI  * could be opened.
144feb4ecdbSBruce M Simpson  */
145feb4ecdbSBruce M Simpson int
146b00ab754SHans Petter Selasky pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
147b00ab754SHans Petter Selasky     int (*check_usable)(const char *), get_if_flags_func get_flags_func)
148feb4ecdbSBruce M Simpson {
149feb4ecdbSBruce M Simpson 	struct ifaddrs *ifap, *ifa;
150feb4ecdbSBruce M Simpson 	struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
151feb4ecdbSBruce M Simpson 	size_t addr_size, broadaddr_size, dstaddr_size;
152feb4ecdbSBruce M Simpson 	int ret = 0;
153ee2dd488SSam Leffler 	char *p, *q;
154feb4ecdbSBruce M Simpson 
155feb4ecdbSBruce M Simpson 	/*
156feb4ecdbSBruce M Simpson 	 * Get the list of interface addresses.
157feb4ecdbSBruce M Simpson 	 *
158feb4ecdbSBruce M Simpson 	 * Note: this won't return information about interfaces
159681ed54cSXin LI 	 * with no addresses, so, if a platform has interfaces
160681ed54cSXin LI 	 * with no interfaces on which traffic can be captured,
161681ed54cSXin LI 	 * we must check for those interfaces as well (see, for
162681ed54cSXin LI 	 * example, what's done on Linux).
163feb4ecdbSBruce M Simpson 	 *
164feb4ecdbSBruce M Simpson 	 * LAN interfaces will probably have link-layer
165feb4ecdbSBruce M Simpson 	 * addresses; I don't know whether all implementations
166feb4ecdbSBruce M Simpson 	 * of "getifaddrs()" now, or in the future, will return
167feb4ecdbSBruce M Simpson 	 * those.
168feb4ecdbSBruce M Simpson 	 */
169feb4ecdbSBruce M Simpson 	if (getifaddrs(&ifap) != 0) {
170b00ab754SHans Petter Selasky 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
171b00ab754SHans Petter Selasky 		    errno, "getifaddrs");
172feb4ecdbSBruce M Simpson 		return (-1);
173feb4ecdbSBruce M Simpson 	}
174feb4ecdbSBruce M Simpson 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
175feb4ecdbSBruce M Simpson 		/*
176ada6f083SXin LI 		 * If this entry has a colon followed by a number at
177ada6f083SXin LI 		 * the end, we assume it's a logical interface.  Those
178ada6f083SXin LI 		 * are just the way you assign multiple IP addresses to
179ada6f083SXin LI 		 * a real interface on Linux, so an entry for a logical
180ada6f083SXin LI 		 * interface should be treated like the entry for the
181ada6f083SXin LI 		 * real interface; we do that by stripping off the ":"
182ada6f083SXin LI 		 * and the number.
183ada6f083SXin LI 		 *
184ada6f083SXin LI 		 * XXX - should we do this only on Linux?
185ada6f083SXin LI 		 */
186ada6f083SXin LI 		p = strchr(ifa->ifa_name, ':');
187ada6f083SXin LI 		if (p != NULL) {
188ada6f083SXin LI 			/*
189ada6f083SXin LI 			 * We have a ":"; is it followed by a number?
190ada6f083SXin LI 			 */
191ada6f083SXin LI 			q = p + 1;
192*6f9cba8fSJoseph Mingrone 			while (PCAP_ISDIGIT(*q))
193ada6f083SXin LI 				q++;
194ada6f083SXin LI 			if (*q == '\0') {
195ada6f083SXin LI 				/*
196ada6f083SXin LI 				 * All digits after the ":" until the end.
197ada6f083SXin LI 				 * Strip off the ":" and everything after
198ada6f083SXin LI 				 * it.
199ada6f083SXin LI 				 */
200ada6f083SXin LI 			       *p = '\0';
201ada6f083SXin LI 			}
202ada6f083SXin LI 		}
203ada6f083SXin LI 
204ada6f083SXin LI 		/*
205ada6f083SXin LI 		 * Can we capture on this device?
206ada6f083SXin LI 		 */
207ada6f083SXin LI 		if (!(*check_usable)(ifa->ifa_name)) {
208ada6f083SXin LI 			/*
209ada6f083SXin LI 			 * No.
210ada6f083SXin LI 			 */
211ada6f083SXin LI 			continue;
212ada6f083SXin LI 		}
213ada6f083SXin LI 
214ada6f083SXin LI 		/*
215feb4ecdbSBruce M Simpson 		 * "ifa_addr" was apparently null on at least one
216681ed54cSXin LI 		 * interface on some system.  Therefore, we supply
217681ed54cSXin LI 		 * the address and netmask only if "ifa_addr" is
218681ed54cSXin LI 		 * non-null (if there's no address, there's obviously
219681ed54cSXin LI 		 * no netmask).
220feb4ecdbSBruce M Simpson 		 */
221feb4ecdbSBruce M Simpson 		if (ifa->ifa_addr != NULL) {
222feb4ecdbSBruce M Simpson 			addr = ifa->ifa_addr;
223feb4ecdbSBruce M Simpson 			addr_size = SA_LEN(addr);
224feb4ecdbSBruce M Simpson 			netmask = ifa->ifa_netmask;
225feb4ecdbSBruce M Simpson 		} else {
226feb4ecdbSBruce M Simpson 			addr = NULL;
227feb4ecdbSBruce M Simpson 			addr_size = 0;
228feb4ecdbSBruce M Simpson 			netmask = NULL;
229feb4ecdbSBruce M Simpson 		}
230681ed54cSXin LI 
231681ed54cSXin LI 		/*
232681ed54cSXin LI 		 * Note that, on some platforms, ifa_broadaddr and
233681ed54cSXin LI 		 * ifa_dstaddr could be the same field (true on at
234b00ab754SHans Petter Selasky 		 * least some versions of *BSD and macOS), so we
235681ed54cSXin LI 		 * can't just check whether the broadcast address
236681ed54cSXin LI 		 * is null and add it if so and check whether the
237681ed54cSXin LI 		 * destination address is null and add it if so.
238681ed54cSXin LI 		 *
239681ed54cSXin LI 		 * Therefore, we must also check the IFF_BROADCAST
240681ed54cSXin LI 		 * flag, and only add a broadcast address if it's
241681ed54cSXin LI 		 * set, and check the IFF_POINTTOPOINT flag, and
242681ed54cSXin LI 		 * only add a destination address if it's set (as
243681ed54cSXin LI 		 * per man page recommendations on some of those
244681ed54cSXin LI 		 * platforms).
245681ed54cSXin LI 		 */
246feb4ecdbSBruce M Simpson 		if (ifa->ifa_flags & IFF_BROADCAST &&
247feb4ecdbSBruce M Simpson 		    ifa->ifa_broadaddr != NULL) {
248feb4ecdbSBruce M Simpson 			broadaddr = ifa->ifa_broadaddr;
249feb4ecdbSBruce M Simpson 			broadaddr_size = SA_LEN(broadaddr);
250feb4ecdbSBruce M Simpson 		} else {
251feb4ecdbSBruce M Simpson 			broadaddr = NULL;
252feb4ecdbSBruce M Simpson 			broadaddr_size = 0;
253feb4ecdbSBruce M Simpson 		}
254feb4ecdbSBruce M Simpson 		if (ifa->ifa_flags & IFF_POINTOPOINT &&
255feb4ecdbSBruce M Simpson 		    ifa->ifa_dstaddr != NULL) {
256feb4ecdbSBruce M Simpson 			dstaddr = ifa->ifa_dstaddr;
257feb4ecdbSBruce M Simpson 			dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
258feb4ecdbSBruce M Simpson 		} else {
259feb4ecdbSBruce M Simpson 			dstaddr = NULL;
260feb4ecdbSBruce M Simpson 			dstaddr_size = 0;
261feb4ecdbSBruce M Simpson 		}
262feb4ecdbSBruce M Simpson 
263feb4ecdbSBruce M Simpson 		/*
264feb4ecdbSBruce M Simpson 		 * Add information for this address to the list.
265feb4ecdbSBruce M Simpson 		 */
266b00ab754SHans Petter Selasky 		if (add_addr_to_if(devlistp, ifa->ifa_name, ifa->ifa_flags,
267b00ab754SHans Petter Selasky 		    get_flags_func,
268ada6f083SXin LI 		    addr, addr_size, netmask, addr_size,
269feb4ecdbSBruce M Simpson 		    broadaddr, broadaddr_size, dstaddr, dstaddr_size,
270feb4ecdbSBruce M Simpson 		    errbuf) < 0) {
271feb4ecdbSBruce M Simpson 			ret = -1;
272feb4ecdbSBruce M Simpson 			break;
273feb4ecdbSBruce M Simpson 		}
274feb4ecdbSBruce M Simpson 	}
275feb4ecdbSBruce M Simpson 
276feb4ecdbSBruce M Simpson 	freeifaddrs(ifap);
277feb4ecdbSBruce M Simpson 
278feb4ecdbSBruce M Simpson 	return (ret);
279feb4ecdbSBruce M Simpson }
280