xref: /netbsd-src/external/bsd/libpcap/dist/fad-getad.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: fad-getad.c,v 1.4 2018/09/03 15:26:43 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.4 2018/09/03 15:26:43 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_STRUCT_SOCKADDR_SA_LEN
111 #define SA_LEN(addr)	((addr)->sa_len)
112 #else /* HAVE_STRUCT_SOCKADDR_SA_LEN */
113 #ifdef HAVE_STRUCT_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_STRUCT_SOCKADDR_STORAGE */
140 #define SA_LEN(addr)	(sizeof (struct sockaddr))
141 #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
142 #endif /* HAVE_STRUCT_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_list_t *devlistp, char *errbuf,
153     int (*check_usable)(const char *), get_if_flags_func get_flags_func)
154 {
155 	struct ifaddrs *ifap, *ifa;
156 	struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
157 	size_t addr_size, broadaddr_size, dstaddr_size;
158 	int ret = 0;
159 	char *p, *q;
160 
161 	/*
162 	 * Get the list of interface addresses.
163 	 *
164 	 * Note: this won't return information about interfaces
165 	 * with no addresses, so, if a platform has interfaces
166 	 * with no interfaces on which traffic can be captured,
167 	 * we must check for those interfaces as well (see, for
168 	 * example, what's done on Linux).
169 	 *
170 	 * LAN interfaces will probably have link-layer
171 	 * addresses; I don't know whether all implementations
172 	 * of "getifaddrs()" now, or in the future, will return
173 	 * those.
174 	 */
175 	if (getifaddrs(&ifap) != 0) {
176 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
177 		    errno, "getifaddrs");
178 		return (-1);
179 	}
180 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
181 		/*
182 		 * If this entry has a colon followed by a number at
183 		 * the end, we assume it's a logical interface.  Those
184 		 * are just the way you assign multiple IP addresses to
185 		 * a real interface on Linux, so an entry for a logical
186 		 * interface should be treated like the entry for the
187 		 * real interface; we do that by stripping off the ":"
188 		 * and the number.
189 		 *
190 		 * XXX - should we do this only on Linux?
191 		 */
192 		p = strchr(ifa->ifa_name, ':');
193 		if (p != NULL) {
194 			/*
195 			 * We have a ":"; is it followed by a number?
196 			 */
197 			q = p + 1;
198 			while (isdigit((unsigned char)*q))
199 				q++;
200 			if (*q == '\0') {
201 				/*
202 				 * All digits after the ":" until the end.
203 				 * Strip off the ":" and everything after
204 				 * it.
205 				 */
206 			       *p = '\0';
207 			}
208 		}
209 
210 		/*
211 		 * Can we capture on this device?
212 		 */
213 		if (!(*check_usable)(ifa->ifa_name)) {
214 			/*
215 			 * No.
216 			 */
217 			continue;
218 		}
219 
220 		/*
221 		 * "ifa_addr" was apparently null on at least one
222 		 * interface on some system.  Therefore, we supply
223 		 * the address and netmask only if "ifa_addr" is
224 		 * non-null (if there's no address, there's obviously
225 		 * no netmask).
226 		 */
227 		if (ifa->ifa_addr != NULL) {
228 			addr = ifa->ifa_addr;
229 			addr_size = SA_LEN(addr);
230 			netmask = ifa->ifa_netmask;
231 		} else {
232 			addr = NULL;
233 			addr_size = 0;
234 			netmask = NULL;
235 		}
236 
237 		/*
238 		 * Note that, on some platforms, ifa_broadaddr and
239 		 * ifa_dstaddr could be the same field (true on at
240 		 * least some versions of *BSD and macOS), so we
241 		 * can't just check whether the broadcast address
242 		 * is null and add it if so and check whether the
243 		 * destination address is null and add it if so.
244 		 *
245 		 * Therefore, we must also check the IFF_BROADCAST
246 		 * flag, and only add a broadcast address if it's
247 		 * set, and check the IFF_POINTTOPOINT flag, and
248 		 * only add a destination address if it's set (as
249 		 * per man page recommendations on some of those
250 		 * platforms).
251 		 */
252 		if (ifa->ifa_flags & IFF_BROADCAST &&
253 		    ifa->ifa_broadaddr != NULL) {
254 			broadaddr = ifa->ifa_broadaddr;
255 			broadaddr_size = SA_LEN(broadaddr);
256 		} else {
257 			broadaddr = NULL;
258 			broadaddr_size = 0;
259 		}
260 		if (ifa->ifa_flags & IFF_POINTOPOINT &&
261 		    ifa->ifa_dstaddr != NULL) {
262 			dstaddr = ifa->ifa_dstaddr;
263 			dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
264 		} else {
265 			dstaddr = NULL;
266 			dstaddr_size = 0;
267 		}
268 
269 		/*
270 		 * Add information for this address to the list.
271 		 */
272 		if (add_addr_to_if(devlistp, ifa->ifa_name, ifa->ifa_flags,
273 		    get_flags_func,
274 		    addr, addr_size, netmask, addr_size,
275 		    broadaddr, broadaddr_size, dstaddr, dstaddr_size,
276 		    errbuf) < 0) {
277 			ret = -1;
278 			break;
279 		}
280 	}
281 
282 	freeifaddrs(ifap);
283 
284 	return (ret);
285 }
286