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