1335640Shselasky/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2335640Shselasky/*
3335640Shselasky * Copyright (c) 1994, 1995, 1996, 1997, 1998
4335640Shselasky *	The Regents of the University of California.  All rights reserved.
5335640Shselasky *
6335640Shselasky * Redistribution and use in source and binary forms, with or without
7335640Shselasky * modification, are permitted provided that the following conditions
8335640Shselasky * are met:
9335640Shselasky * 1. Redistributions of source code must retain the above copyright
10335640Shselasky *    notice, this list of conditions and the following disclaimer.
11335640Shselasky * 2. Redistributions in binary form must reproduce the above copyright
12335640Shselasky *    notice, this list of conditions and the following disclaimer in the
13335640Shselasky *    documentation and/or other materials provided with the distribution.
14335640Shselasky * 3. All advertising materials mentioning features or use of this software
15335640Shselasky *    must display the following acknowledgement:
16335640Shselasky *	This product includes software developed by the Computer Systems
17335640Shselasky *	Engineering Group at Lawrence Berkeley Laboratory.
18335640Shselasky * 4. Neither the name of the University nor of the Laboratory may be used
19335640Shselasky *    to endorse or promote products derived from this software without
20335640Shselasky *    specific prior written permission.
21335640Shselasky *
22335640Shselasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23335640Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24335640Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25335640Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26335640Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27335640Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28335640Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29335640Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30335640Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31335640Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32335640Shselasky * SUCH DAMAGE.
33335640Shselasky */
34335640Shselasky
35335640Shselasky#ifdef HAVE_CONFIG_H
36335640Shselasky#include <config.h>
37335640Shselasky#endif
38335640Shselasky
39335640Shselasky#include <sys/param.h>
40335640Shselasky#include <sys/ioctl.h>
41335640Shselasky#include <sys/socket.h>
42335640Shselasky#ifdef HAVE_SYS_SOCKIO_H
43335640Shselasky#include <sys/sockio.h>
44335640Shselasky#endif
45335640Shselasky#include <sys/time.h>				/* concession to AIX */
46335640Shselasky
47335640Shselaskystruct mbuf;		/* Squelch compiler warnings on some platforms for */
48335640Shselaskystruct rtentry;		/* declarations in <net/if.h> */
49335640Shselasky#include <net/if.h>
50335640Shselasky#include <netinet/in.h>
51335640Shselasky
52335640Shselasky#include <ctype.h>
53335640Shselasky#include <errno.h>
54335640Shselasky#include <memory.h>
55335640Shselasky#include <stdio.h>
56335640Shselasky#include <stdlib.h>
57335640Shselasky#include <string.h>
58335640Shselasky#include <unistd.h>
59335640Shselasky
60335640Shselasky#ifdef HAVE_LIMITS_H
61335640Shselasky#include <limits.h>
62335640Shselasky#else
63335640Shselasky#define INT_MAX		2147483647
64335640Shselasky#endif
65335640Shselasky
66335640Shselasky#include "pcap-int.h"
67335640Shselasky
68335640Shselasky#ifdef HAVE_OS_PROTO_H
69335640Shselasky#include "os-proto.h"
70335640Shselasky#endif
71335640Shselasky
72335640Shselasky/*
73335640Shselasky * This is fun.
74335640Shselasky *
75335640Shselasky * In older BSD systems, socket addresses were fixed-length, and
76335640Shselasky * "sizeof (struct sockaddr)" gave the size of the structure.
77335640Shselasky * All addresses fit within a "struct sockaddr".
78335640Shselasky *
79335640Shselasky * In newer BSD systems, the socket address is variable-length, and
80335640Shselasky * there's an "sa_len" field giving the length of the structure;
81335640Shselasky * this allows socket addresses to be longer than 2 bytes of family
82335640Shselasky * and 14 bytes of data.
83335640Shselasky *
84335640Shselasky * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
85335640Shselasky * variant of the old BSD scheme (with "struct sockaddr_storage" rather
86335640Shselasky * than "struct sockaddr"), and some use the new BSD scheme.
87335640Shselasky *
88335640Shselasky * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
89335640Shselasky * macro that determines the size based on the address family.  Other
90335640Shselasky * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
91335640Shselasky * but not in the final version).
92335640Shselasky *
93335640Shselasky * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have
94335640Shselasky * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the
95335640Shselasky * address in an entry returned by SIOCGIFCONF.
96335640Shselasky */
97335640Shselasky#ifndef SA_LEN
98335640Shselasky#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
99335640Shselasky#define SA_LEN(addr)	((addr)->sa_len)
100335640Shselasky#else /* HAVE_STRUCT_SOCKADDR_SA_LEN */
101335640Shselasky#define SA_LEN(addr)	(sizeof (struct sockaddr))
102335640Shselasky#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
103335640Shselasky#endif /* SA_LEN */
104335640Shselasky
105335640Shselasky/*
106335640Shselasky * This is also fun.
107335640Shselasky *
108335640Shselasky * There is no ioctl that returns the amount of space required for all
109335640Shselasky * the data that SIOCGIFCONF could return, and if a buffer is supplied
110335640Shselasky * that's not large enough for all the data SIOCGIFCONF could return,
111335640Shselasky * on at least some platforms it just returns the data that'd fit with
112335640Shselasky * no indication that there wasn't enough room for all the data, much
113335640Shselasky * less an indication of how much more room is required.
114335640Shselasky *
115335640Shselasky * The only way to ensure that we got all the data is to pass a buffer
116335640Shselasky * large enough that the amount of space in the buffer *not* filled in
117335640Shselasky * is greater than the largest possible entry.
118335640Shselasky *
119335640Shselasky * We assume that's "sizeof(ifreq.ifr_name)" plus 255, under the assumption
120335640Shselasky * that no address is more than 255 bytes (on systems where the "sa_len"
121335640Shselasky * field in a "struct sockaddr" is 1 byte, e.g. newer BSDs, that's the
122335640Shselasky * case, and addresses are unlikely to be bigger than that in any case).
123335640Shselasky */
124335640Shselasky#define MAX_SA_LEN	255
125335640Shselasky
126335640Shselasky/*
127335640Shselasky * Get a list of all interfaces that are up and that we can open.
128335640Shselasky * Returns -1 on error, 0 otherwise.
129335640Shselasky * The list, as returned through "alldevsp", may be null if no interfaces
130335640Shselasky * were up and could be opened.
131335640Shselasky *
132335640Shselasky * This is the implementation used on platforms that have SIOCGIFCONF but
133335640Shselasky * don't have any other mechanism for getting a list of interfaces.
134335640Shselasky *
135335640Shselasky * XXX - or platforms that have other, better mechanisms but for which
136335640Shselasky * we don't yet have code to use that mechanism; I think there's a better
137335640Shselasky * way on Linux, for example, but if that better way is "getifaddrs()",
138335640Shselasky * we already have that.
139335640Shselasky */
140335640Shselaskyint
141335640Shselaskypcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
142335640Shselasky    int (*check_usable)(const char *), get_if_flags_func get_flags_func)
143335640Shselasky{
144335640Shselasky	register int fd;
145335640Shselasky	register struct ifreq *ifrp, *ifend, *ifnext;
146335640Shselasky	size_t n;
147335640Shselasky	struct ifconf ifc;
148335640Shselasky	char *buf = NULL;
149335640Shselasky	unsigned buf_size;
150335640Shselasky#if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
151335640Shselasky	char *p, *q;
152335640Shselasky#endif
153335640Shselasky	struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
154335640Shselasky	struct sockaddr *netmask, *broadaddr, *dstaddr;
155335640Shselasky	size_t netmask_size, broadaddr_size, dstaddr_size;
156335640Shselasky	int ret = 0;
157335640Shselasky
158335640Shselasky	/*
159335640Shselasky	 * Create a socket from which to fetch the list of interfaces.
160335640Shselasky	 */
161335640Shselasky	fd = socket(AF_INET, SOCK_DGRAM, 0);
162335640Shselasky	if (fd < 0) {
163335640Shselasky		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
164335640Shselasky		    errno, "socket");
165335640Shselasky		return (-1);
166335640Shselasky	}
167335640Shselasky
168335640Shselasky	/*
169335640Shselasky	 * Start with an 8K buffer, and keep growing the buffer until
170335640Shselasky	 * we have more than "sizeof(ifrp->ifr_name) + MAX_SA_LEN"
171335640Shselasky	 * bytes left over in the buffer or we fail to get the
172335640Shselasky	 * interface list for some reason other than EINVAL (which is
173335640Shselasky	 * presumed here to mean "buffer is too small").
174335640Shselasky	 */
175335640Shselasky	buf_size = 8192;
176335640Shselasky	for (;;) {
177335640Shselasky		/*
178335640Shselasky		 * Don't let the buffer size get bigger than INT_MAX.
179335640Shselasky		 */
180335640Shselasky		if (buf_size > INT_MAX) {
181335640Shselasky			(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
182335640Shselasky			    "interface information requires more than %u bytes",
183335640Shselasky			    INT_MAX);
184335640Shselasky			(void)close(fd);
185335640Shselasky			return (-1);
186335640Shselasky		}
187335640Shselasky		buf = malloc(buf_size);
188335640Shselasky		if (buf == NULL) {
189335640Shselasky			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
190335640Shselasky			    errno, "malloc");
191335640Shselasky			(void)close(fd);
192335640Shselasky			return (-1);
193335640Shselasky		}
194335640Shselasky
195335640Shselasky		ifc.ifc_len = buf_size;
196335640Shselasky		ifc.ifc_buf = buf;
197335640Shselasky		memset(buf, 0, buf_size);
198335640Shselasky		if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
199335640Shselasky		    && errno != EINVAL) {
200335640Shselasky			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
201335640Shselasky			    errno, "SIOCGIFCONF");
202335640Shselasky			(void)close(fd);
203335640Shselasky			free(buf);
204335640Shselasky			return (-1);
205335640Shselasky		}
206335640Shselasky		if (ifc.ifc_len < (int)buf_size &&
207335640Shselasky		    (buf_size - ifc.ifc_len) > sizeof(ifrp->ifr_name) + MAX_SA_LEN)
208335640Shselasky			break;
209335640Shselasky		free(buf);
210335640Shselasky		buf_size *= 2;
211335640Shselasky	}
212335640Shselasky
213335640Shselasky	ifrp = (struct ifreq *)buf;
214335640Shselasky	ifend = (struct ifreq *)(buf + ifc.ifc_len);
215335640Shselasky
216335640Shselasky	for (; ifrp < ifend; ifrp = ifnext) {
217335640Shselasky		/*
218335640Shselasky		 * XXX - what if this isn't an IPv4 address?  Can
219335640Shselasky		 * we still get the netmask, etc. with ioctls on
220335640Shselasky		 * an IPv4 socket?
221335640Shselasky		 *
222335640Shselasky		 * The answer is probably platform-dependent, and
223335640Shselasky		 * if the answer is "no" on more than one platform,
224335640Shselasky		 * the way you work around it is probably platform-
225335640Shselasky		 * dependent as well.
226335640Shselasky		 */
227335640Shselasky		n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
228335640Shselasky		if (n < sizeof(*ifrp))
229335640Shselasky			ifnext = ifrp + 1;
230335640Shselasky		else
231335640Shselasky			ifnext = (struct ifreq *)((char *)ifrp + n);
232335640Shselasky
233335640Shselasky		/*
234335640Shselasky		 * XXX - The 32-bit compatibility layer for Linux on IA-64
235335640Shselasky		 * is slightly broken. It correctly converts the structures
236335640Shselasky		 * to and from kernel land from 64 bit to 32 bit but
237335640Shselasky		 * doesn't update ifc.ifc_len, leaving it larger than the
238335640Shselasky		 * amount really used. This means we read off the end
239335640Shselasky		 * of the buffer and encounter an interface with an
240335640Shselasky		 * "empty" name. Since this is highly unlikely to ever
241335640Shselasky		 * occur in a valid case we can just finish looking for
242335640Shselasky		 * interfaces if we see an empty name.
243335640Shselasky		 */
244335640Shselasky		if (!(*ifrp->ifr_name))
245335640Shselasky			break;
246335640Shselasky
247335640Shselasky		/*
248335640Shselasky		 * Skip entries that begin with "dummy".
249335640Shselasky		 * XXX - what are these?  Is this Linux-specific?
250335640Shselasky		 * Are there platforms on which we shouldn't do this?
251335640Shselasky		 */
252335640Shselasky		if (strncmp(ifrp->ifr_name, "dummy", 5) == 0)
253335640Shselasky			continue;
254335640Shselasky
255335640Shselasky		/*
256335640Shselasky		 * Can we capture on this device?
257335640Shselasky		 */
258335640Shselasky		if (!(*check_usable)(ifrp->ifr_name)) {
259335640Shselasky			/*
260335640Shselasky			 * No.
261335640Shselasky			 */
262335640Shselasky			continue;
263335640Shselasky		}
264335640Shselasky
265335640Shselasky		/*
266335640Shselasky		 * Get the flags for this interface.
267335640Shselasky		 */
268335640Shselasky		strncpy(ifrflags.ifr_name, ifrp->ifr_name,
269335640Shselasky		    sizeof(ifrflags.ifr_name));
270335640Shselasky		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
271335640Shselasky			if (errno == ENXIO)
272335640Shselasky				continue;
273335640Shselasky			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
274335640Shselasky			    errno, "SIOCGIFFLAGS: %.*s",
275335640Shselasky			    (int)sizeof(ifrflags.ifr_name),
276335640Shselasky			    ifrflags.ifr_name);
277335640Shselasky			ret = -1;
278335640Shselasky			break;
279335640Shselasky		}
280335640Shselasky
281335640Shselasky		/*
282335640Shselasky		 * Get the netmask for this address on this interface.
283335640Shselasky		 */
284335640Shselasky		strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
285335640Shselasky		    sizeof(ifrnetmask.ifr_name));
286335640Shselasky		memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
287335640Shselasky		    sizeof(ifrnetmask.ifr_addr));
288335640Shselasky		if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
289335640Shselasky			if (errno == EADDRNOTAVAIL) {
290335640Shselasky				/*
291335640Shselasky				 * Not available.
292335640Shselasky				 */
293335640Shselasky				netmask = NULL;
294335640Shselasky				netmask_size = 0;
295335640Shselasky			} else {
296335640Shselasky				pcap_fmt_errmsg_for_errno(errbuf,
297335640Shselasky				    PCAP_ERRBUF_SIZE, errno,
298335640Shselasky				    "SIOCGIFNETMASK: %.*s",
299335640Shselasky				    (int)sizeof(ifrnetmask.ifr_name),
300335640Shselasky				    ifrnetmask.ifr_name);
301335640Shselasky				ret = -1;
302335640Shselasky				break;
303335640Shselasky			}
304335640Shselasky		} else {
305335640Shselasky			netmask = &ifrnetmask.ifr_addr;
306335640Shselasky			netmask_size = SA_LEN(netmask);
307335640Shselasky		}
308335640Shselasky
309335640Shselasky		/*
310335640Shselasky		 * Get the broadcast address for this address on this
311335640Shselasky		 * interface (if any).
312335640Shselasky		 */
313335640Shselasky		if (ifrflags.ifr_flags & IFF_BROADCAST) {
314335640Shselasky			strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
315335640Shselasky			    sizeof(ifrbroadaddr.ifr_name));
316335640Shselasky			memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
317335640Shselasky			    sizeof(ifrbroadaddr.ifr_addr));
318335640Shselasky			if (ioctl(fd, SIOCGIFBRDADDR,
319335640Shselasky			    (char *)&ifrbroadaddr) < 0) {
320335640Shselasky				if (errno == EADDRNOTAVAIL) {
321335640Shselasky					/*
322335640Shselasky					 * Not available.
323335640Shselasky					 */
324335640Shselasky					broadaddr = NULL;
325335640Shselasky					broadaddr_size = 0;
326335640Shselasky				} else {
327335640Shselasky					pcap_fmt_errmsg_for_errno(errbuf,
328335640Shselasky					    PCAP_ERRBUF_SIZE, errno,
329335640Shselasky					    "SIOCGIFBRDADDR: %.*s",
330335640Shselasky					    (int)sizeof(ifrbroadaddr.ifr_name),
331335640Shselasky					    ifrbroadaddr.ifr_name);
332335640Shselasky					ret = -1;
333335640Shselasky					break;
334335640Shselasky				}
335335640Shselasky			} else {
336335640Shselasky				broadaddr = &ifrbroadaddr.ifr_broadaddr;
337335640Shselasky				broadaddr_size = SA_LEN(broadaddr);
338335640Shselasky			}
339335640Shselasky		} else {
340335640Shselasky			/*
341335640Shselasky			 * Not a broadcast interface, so no broadcast
342335640Shselasky			 * address.
343335640Shselasky			 */
344335640Shselasky			broadaddr = NULL;
345335640Shselasky			broadaddr_size = 0;
346335640Shselasky		}
347335640Shselasky
348335640Shselasky		/*
349335640Shselasky		 * Get the destination address for this address on this
350335640Shselasky		 * interface (if any).
351335640Shselasky		 */
352335640Shselasky		if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
353335640Shselasky			strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
354335640Shselasky			    sizeof(ifrdstaddr.ifr_name));
355335640Shselasky			memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
356335640Shselasky			    sizeof(ifrdstaddr.ifr_addr));
357335640Shselasky			if (ioctl(fd, SIOCGIFDSTADDR,
358335640Shselasky			    (char *)&ifrdstaddr) < 0) {
359335640Shselasky				if (errno == EADDRNOTAVAIL) {
360335640Shselasky					/*
361335640Shselasky					 * Not available.
362335640Shselasky					 */
363335640Shselasky					dstaddr = NULL;
364335640Shselasky					dstaddr_size = 0;
365335640Shselasky				} else {
366335640Shselasky					pcap_fmt_errmsg_for_errno(errbuf,
367335640Shselasky					    PCAP_ERRBUF_SIZE, errno,
368335640Shselasky					    "SIOCGIFDSTADDR: %.*s",
369335640Shselasky					    (int)sizeof(ifrdstaddr.ifr_name),
370335640Shselasky					    ifrdstaddr.ifr_name);
371335640Shselasky					ret = -1;
372335640Shselasky					break;
373335640Shselasky				}
374335640Shselasky			} else {
375335640Shselasky				dstaddr = &ifrdstaddr.ifr_dstaddr;
376335640Shselasky				dstaddr_size = SA_LEN(dstaddr);
377335640Shselasky			}
378335640Shselasky		} else {
379335640Shselasky			/*
380335640Shselasky			 * Not a point-to-point interface, so no destination
381335640Shselasky			 * address.
382335640Shselasky			 */
383335640Shselasky			dstaddr = NULL;
384335640Shselasky			dstaddr_size = 0;
385335640Shselasky		}
386335640Shselasky
387335640Shselasky#if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
388335640Shselasky		/*
389335640Shselasky		 * If this entry has a colon followed by a number at
390335640Shselasky		 * the end, it's a logical interface.  Those are just
391335640Shselasky		 * the way you assign multiple IP addresses to a real
392335640Shselasky		 * interface, so an entry for a logical interface should
393335640Shselasky		 * be treated like the entry for the real interface;
394335640Shselasky		 * we do that by stripping off the ":" and the number.
395335640Shselasky		 */
396335640Shselasky		p = strchr(ifrp->ifr_name, ':');
397335640Shselasky		if (p != NULL) {
398335640Shselasky			/*
399335640Shselasky			 * We have a ":"; is it followed by a number?
400335640Shselasky			 */
401335640Shselasky			q = p + 1;
402335640Shselasky			while (isdigit((unsigned char)*q))
403335640Shselasky				q++;
404335640Shselasky			if (*q == '\0') {
405335640Shselasky				/*
406335640Shselasky				 * All digits after the ":" until the end.
407335640Shselasky				 * Strip off the ":" and everything after
408335640Shselasky				 * it.
409335640Shselasky				 */
410335640Shselasky				*p = '\0';
411335640Shselasky			}
412335640Shselasky		}
413335640Shselasky#endif
414335640Shselasky
415335640Shselasky		/*
416335640Shselasky		 * Add information for this address to the list.
417335640Shselasky		 */
418335640Shselasky		if (add_addr_to_if(devlistp, ifrp->ifr_name,
419335640Shselasky		    ifrflags.ifr_flags, get_flags_func,
420335640Shselasky		    &ifrp->ifr_addr, SA_LEN(&ifrp->ifr_addr),
421335640Shselasky		    netmask, netmask_size, broadaddr, broadaddr_size,
422335640Shselasky		    dstaddr, dstaddr_size, errbuf) < 0) {
423335640Shselasky			ret = -1;
424335640Shselasky			break;
425335640Shselasky		}
426335640Shselasky	}
427335640Shselasky	free(buf);
428335640Shselasky	(void)close(fd);
429335640Shselasky
430335640Shselasky	return (ret);
431335640Shselasky}
432