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