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