xref: /netbsd-src/external/bsd/libpcap/dist/fad-gifc.c (revision 3587d6f89c746bbb4f886219ddacd41ace480ecf)
1 /*	$NetBSD: fad-gifc.c,v 1.6 2023/08/17 15:18:12 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-gifc.c,v 1.6 2023/08/17 15:18:12 christos Exp $");
39 
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43 
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #ifdef HAVE_SYS_SOCKIO_H
48 #include <sys/sockio.h>
49 #endif
50 #include <sys/time.h>				/* concession to AIX */
51 
52 struct mbuf;		/* Squelch compiler warnings on some platforms for */
53 struct rtentry;		/* declarations in <net/if.h> */
54 #include <net/if.h>
55 #include <netinet/in.h>
56 
57 #include <errno.h>
58 #include <memory.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <limits.h>
64 
65 #include "pcap-int.h"
66 
67 #ifdef HAVE_OS_PROTO_H
68 #include "os-proto.h"
69 #endif
70 
71 /*
72  * This is fun.
73  *
74  * In older BSD systems, socket addresses were fixed-length, and
75  * "sizeof (struct sockaddr)" gave the size of the structure.
76  * All addresses fit within a "struct sockaddr".
77  *
78  * In newer BSD systems, the socket address is variable-length, and
79  * there's an "sa_len" field giving the length of the structure;
80  * this allows socket addresses to be longer than 2 bytes of family
81  * and 14 bytes of data.
82  *
83  * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
84  * variant of the old BSD scheme (with "struct sockaddr_storage" rather
85  * than "struct sockaddr"), and some use the new BSD scheme.
86  *
87  * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
88  * macro that determines the size based on the address family.  Other
89  * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
90  * but not in the final version).
91  *
92  * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have
93  * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the
94  * address in an entry returned by SIOCGIFCONF.
95  */
96 #ifndef SA_LEN
97 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
98 #define SA_LEN(addr)	((addr)->sa_len)
99 #else /* HAVE_STRUCT_SOCKADDR_SA_LEN */
100 #define SA_LEN(addr)	(sizeof (struct sockaddr))
101 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
102 #endif /* SA_LEN */
103 
104 /*
105  * This is also fun.
106  *
107  * There is no ioctl that returns the amount of space required for all
108  * the data that SIOCGIFCONF could return, and if a buffer is supplied
109  * that's not large enough for all the data SIOCGIFCONF could return,
110  * on at least some platforms it just returns the data that'd fit with
111  * no indication that there wasn't enough room for all the data, much
112  * less an indication of how much more room is required.
113  *
114  * The only way to ensure that we got all the data is to pass a buffer
115  * large enough that the amount of space in the buffer *not* filled in
116  * is greater than the largest possible entry.
117  *
118  * We assume that's "sizeof(ifreq.ifr_name)" plus 255, under the assumption
119  * that no address is more than 255 bytes (on systems where the "sa_len"
120  * field in a "struct sockaddr" is 1 byte, e.g. newer BSDs, that's the
121  * case, and addresses are unlikely to be bigger than that in any case).
122  */
123 #define MAX_SA_LEN	255
124 
125 /*
126  * Get a list of all interfaces that are up and that we can open.
127  * Returns -1 on error, 0 otherwise.
128  * The list, as returned through "alldevsp", may be null if no interfaces
129  * were up and could be opened.
130  *
131  * This is the implementation used on platforms that have SIOCGIFCONF but
132  * don't have any other mechanism for getting a list of interfaces.
133  *
134  * XXX - or platforms that have other, better mechanisms but for which
135  * we don't yet have code to use that mechanism; I think there's a better
136  * way on Linux, for example, but if that better way is "getifaddrs()",
137  * we already have that.
138  */
139 int
140 pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
141     int (*check_usable)(const char *), get_if_flags_func get_flags_func)
142 {
143 	register int fd;
144 	register struct ifreq *ifrp, *ifend, *ifnext;
145 	size_t n;
146 	struct ifconf ifc;
147 	char *buf = NULL;
148 	unsigned buf_size;
149 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
150 	char *p, *q;
151 #endif
152 	struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
153 	struct sockaddr *netmask, *broadaddr, *dstaddr;
154 	size_t netmask_size, broadaddr_size, dstaddr_size;
155 	int ret = 0;
156 
157 	/*
158 	 * Create a socket from which to fetch the list of interfaces.
159 	 */
160 	fd = socket(AF_INET, SOCK_DGRAM, 0);
161 	if (fd < 0) {
162 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
163 		    errno, "socket");
164 		return (-1);
165 	}
166 
167 	/*
168 	 * Start with an 8K buffer, and keep growing the buffer until
169 	 * we have more than "sizeof(ifrp->ifr_name) + MAX_SA_LEN"
170 	 * bytes left over in the buffer or we fail to get the
171 	 * interface list for some reason other than EINVAL (which is
172 	 * presumed here to mean "buffer is too small").
173 	 */
174 	buf_size = 8192;
175 	for (;;) {
176 		/*
177 		 * Don't let the buffer size get bigger than INT_MAX.
178 		 */
179 		if (buf_size > INT_MAX) {
180 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
181 			    "interface information requires more than %u bytes",
182 			    INT_MAX);
183 			(void)close(fd);
184 			return (-1);
185 		}
186 		buf = malloc(buf_size);
187 		if (buf == NULL) {
188 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
189 			    errno, "malloc");
190 			(void)close(fd);
191 			return (-1);
192 		}
193 
194 		ifc.ifc_len = buf_size;
195 		ifc.ifc_buf = buf;
196 		memset(buf, 0, buf_size);
197 		if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
198 		    && errno != EINVAL) {
199 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
200 			    errno, "SIOCGIFCONF");
201 			(void)close(fd);
202 			free(buf);
203 			return (-1);
204 		}
205 		if (ifc.ifc_len < (int)buf_size &&
206 		    (buf_size - ifc.ifc_len) > sizeof(ifrp->ifr_name) + MAX_SA_LEN)
207 			break;
208 		free(buf);
209 		buf_size *= 2;
210 	}
211 
212 	ifrp = (struct ifreq *)buf;
213 	ifend = (struct ifreq *)(buf + ifc.ifc_len);
214 
215 	for (; ifrp < ifend; ifrp = ifnext) {
216 		/*
217 		 * XXX - what if this isn't an IPv4 address?  Can
218 		 * we still get the netmask, etc. with ioctls on
219 		 * an IPv4 socket?
220 		 *
221 		 * The answer is probably platform-dependent, and
222 		 * if the answer is "no" on more than one platform,
223 		 * the way you work around it is probably platform-
224 		 * dependent as well.
225 		 */
226 		n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
227 		if (n < sizeof(*ifrp))
228 			ifnext = ifrp + 1;
229 		else
230 			ifnext = (struct ifreq *)((char *)ifrp + n);
231 
232 		/*
233 		 * XXX - The 32-bit compatibility layer for Linux on IA-64
234 		 * is slightly broken. It correctly converts the structures
235 		 * to and from kernel land from 64 bit to 32 bit but
236 		 * doesn't update ifc.ifc_len, leaving it larger than the
237 		 * amount really used. This means we read off the end
238 		 * of the buffer and encounter an interface with an
239 		 * "empty" name. Since this is highly unlikely to ever
240 		 * occur in a valid case we can just finish looking for
241 		 * interfaces if we see an empty name.
242 		 */
243 		if (!(*ifrp->ifr_name))
244 			break;
245 
246 		/*
247 		 * Skip entries that begin with "dummy".
248 		 * XXX - what are these?  Is this Linux-specific?
249 		 * Are there platforms on which we shouldn't do this?
250 		 */
251 		if (strncmp(ifrp->ifr_name, "dummy", 5) == 0)
252 			continue;
253 
254 		/*
255 		 * Can we capture on this device?
256 		 */
257 		if (!(*check_usable)(ifrp->ifr_name)) {
258 			/*
259 			 * No.
260 			 */
261 			continue;
262 		}
263 
264 		/*
265 		 * Get the flags for this interface.
266 		 */
267 		strncpy(ifrflags.ifr_name, ifrp->ifr_name,
268 		    sizeof(ifrflags.ifr_name));
269 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
270 			if (errno == ENXIO)
271 				continue;
272 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
273 			    errno, "SIOCGIFFLAGS: %.*s",
274 			    (int)sizeof(ifrflags.ifr_name),
275 			    ifrflags.ifr_name);
276 			ret = -1;
277 			break;
278 		}
279 
280 		/*
281 		 * Get the netmask for this address on this interface.
282 		 */
283 		strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
284 		    sizeof(ifrnetmask.ifr_name));
285 		memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
286 		    sizeof(ifrnetmask.ifr_addr));
287 		if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
288 			if (errno == EADDRNOTAVAIL) {
289 				/*
290 				 * Not available.
291 				 */
292 				netmask = NULL;
293 				netmask_size = 0;
294 			} else {
295 				pcap_fmt_errmsg_for_errno(errbuf,
296 				    PCAP_ERRBUF_SIZE, errno,
297 				    "SIOCGIFNETMASK: %.*s",
298 				    (int)sizeof(ifrnetmask.ifr_name),
299 				    ifrnetmask.ifr_name);
300 				ret = -1;
301 				break;
302 			}
303 		} else {
304 			netmask = &ifrnetmask.ifr_addr;
305 			netmask_size = SA_LEN(netmask);
306 		}
307 
308 		/*
309 		 * Get the broadcast address for this address on this
310 		 * interface (if any).
311 		 */
312 		if (ifrflags.ifr_flags & IFF_BROADCAST) {
313 			strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
314 			    sizeof(ifrbroadaddr.ifr_name));
315 			memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
316 			    sizeof(ifrbroadaddr.ifr_addr));
317 			if (ioctl(fd, SIOCGIFBRDADDR,
318 			    (char *)&ifrbroadaddr) < 0) {
319 				if (errno == EADDRNOTAVAIL) {
320 					/*
321 					 * Not available.
322 					 */
323 					broadaddr = NULL;
324 					broadaddr_size = 0;
325 				} else {
326 					pcap_fmt_errmsg_for_errno(errbuf,
327 					    PCAP_ERRBUF_SIZE, errno,
328 					    "SIOCGIFBRDADDR: %.*s",
329 					    (int)sizeof(ifrbroadaddr.ifr_name),
330 					    ifrbroadaddr.ifr_name);
331 					ret = -1;
332 					break;
333 				}
334 			} else {
335 				broadaddr = &ifrbroadaddr.ifr_broadaddr;
336 				broadaddr_size = SA_LEN(broadaddr);
337 			}
338 		} else {
339 			/*
340 			 * Not a broadcast interface, so no broadcast
341 			 * address.
342 			 */
343 			broadaddr = NULL;
344 			broadaddr_size = 0;
345 		}
346 
347 		/*
348 		 * Get the destination address for this address on this
349 		 * interface (if any).
350 		 */
351 		if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
352 			strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
353 			    sizeof(ifrdstaddr.ifr_name));
354 			memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
355 			    sizeof(ifrdstaddr.ifr_addr));
356 			if (ioctl(fd, SIOCGIFDSTADDR,
357 			    (char *)&ifrdstaddr) < 0) {
358 				if (errno == EADDRNOTAVAIL) {
359 					/*
360 					 * Not available.
361 					 */
362 					dstaddr = NULL;
363 					dstaddr_size = 0;
364 				} else {
365 					pcap_fmt_errmsg_for_errno(errbuf,
366 					    PCAP_ERRBUF_SIZE, errno,
367 					    "SIOCGIFDSTADDR: %.*s",
368 					    (int)sizeof(ifrdstaddr.ifr_name),
369 					    ifrdstaddr.ifr_name);
370 					ret = -1;
371 					break;
372 				}
373 			} else {
374 				dstaddr = &ifrdstaddr.ifr_dstaddr;
375 				dstaddr_size = SA_LEN(dstaddr);
376 			}
377 		} else {
378 			/*
379 			 * Not a point-to-point interface, so no destination
380 			 * address.
381 			 */
382 			dstaddr = NULL;
383 			dstaddr_size = 0;
384 		}
385 
386 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
387 		/*
388 		 * If this entry has a colon followed by a number at
389 		 * the end, it's a logical interface.  Those are just
390 		 * the way you assign multiple IP addresses to a real
391 		 * interface, so an entry for a logical interface should
392 		 * be treated like the entry for the real interface;
393 		 * we do that by stripping off the ":" and the number.
394 		 */
395 		p = strchr(ifrp->ifr_name, ':');
396 		if (p != NULL) {
397 			/*
398 			 * We have a ":"; is it followed by a number?
399 			 */
400 			q = p + 1;
401 			while (PCAP_ISDIGIT(*q))
402 				q++;
403 			if (*q == '\0') {
404 				/*
405 				 * All digits after the ":" until the end.
406 				 * Strip off the ":" and everything after
407 				 * it.
408 				 */
409 				*p = '\0';
410 			}
411 		}
412 #endif
413 
414 		/*
415 		 * Add information for this address to the list.
416 		 */
417 		if (add_addr_to_if(devlistp, ifrp->ifr_name,
418 		    ifrflags.ifr_flags, get_flags_func,
419 		    &ifrp->ifr_addr, SA_LEN(&ifrp->ifr_addr),
420 		    netmask, netmask_size, broadaddr, broadaddr_size,
421 		    dstaddr, dstaddr_size, errbuf) < 0) {
422 			ret = -1;
423 			break;
424 		}
425 	}
426 	free(buf);
427 	(void)close(fd);
428 
429 	return (ret);
430 }
431