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