xref: /netbsd-src/external/bsd/libpcap/dist/fad-getad.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: fad-getad.c,v 1.1.1.4 2013/12/31 16:57:24 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-getad.c,v 1.12 2007-09-14 00:44:55 guy Exp  (LBL)";
40 #endif
41 
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 
50 #include <net/if.h>
51 
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <ifaddrs.h>
58 
59 #include "pcap-int.h"
60 
61 #ifdef HAVE_OS_PROTO_H
62 #include "os-proto.h"
63 #endif
64 
65 /*
66  * We don't do this on Solaris 11 and later, as it appears there aren't
67  * any AF_PACKET addresses on interfaces, so we don't need this, and
68  * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>,
69  * and their definitions of some data structures collide.
70  */
71 #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
72 # ifdef HAVE_NETPACKET_PACKET_H
73 /* Linux distributions with newer glibc */
74 #  include <netpacket/packet.h>
75 # else /* HAVE_NETPACKET_PACKET_H */
76 /* LynxOS, Linux distributions with older glibc */
77 # ifdef __Lynx__
78 /* LynxOS */
79 #  include <netpacket/if_packet.h>
80 # else /* __Lynx__ */
81 /* Linux */
82 #  include <linux/types.h>
83 #  include <linux/if_packet.h>
84 # endif /* __Lynx__ */
85 # endif /* HAVE_NETPACKET_PACKET_H */
86 #endif /* (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) */
87 
88 /*
89  * This is fun.
90  *
91  * In older BSD systems, socket addresses were fixed-length, and
92  * "sizeof (struct sockaddr)" gave the size of the structure.
93  * All addresses fit within a "struct sockaddr".
94  *
95  * In newer BSD systems, the socket address is variable-length, and
96  * there's an "sa_len" field giving the length of the structure;
97  * this allows socket addresses to be longer than 2 bytes of family
98  * and 14 bytes of data.
99  *
100  * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
101  * variant of the old BSD scheme (with "struct sockaddr_storage" rather
102  * than "struct sockaddr"), and some use the new BSD scheme.
103  *
104  * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
105  * macro that determines the size based on the address family.  Other
106  * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
107  * but not in the final version).  On the latter systems, we explicitly
108  * check the AF_ type to determine the length; we assume that on
109  * all those systems we have "struct sockaddr_storage".
110  */
111 #ifndef SA_LEN
112 #ifdef HAVE_SOCKADDR_SA_LEN
113 #define SA_LEN(addr)	((addr)->sa_len)
114 #else /* HAVE_SOCKADDR_SA_LEN */
115 #ifdef HAVE_SOCKADDR_STORAGE
116 static size_t
117 get_sa_len(struct sockaddr *addr)
118 {
119 	switch (addr->sa_family) {
120 
121 #ifdef AF_INET
122 	case AF_INET:
123 		return (sizeof (struct sockaddr_in));
124 #endif
125 
126 #ifdef AF_INET6
127 	case AF_INET6:
128 		return (sizeof (struct sockaddr_in6));
129 #endif
130 
131 #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
132 	case AF_PACKET:
133 		return (sizeof (struct sockaddr_ll));
134 #endif
135 
136 	default:
137 		return (sizeof (struct sockaddr));
138 	}
139 }
140 #define SA_LEN(addr)	(get_sa_len(addr))
141 #else /* HAVE_SOCKADDR_STORAGE */
142 #define SA_LEN(addr)	(sizeof (struct sockaddr))
143 #endif /* HAVE_SOCKADDR_STORAGE */
144 #endif /* HAVE_SOCKADDR_SA_LEN */
145 #endif /* SA_LEN */
146 
147 /*
148  * Get a list of all interfaces that are up and that we can open.
149  * Returns -1 on error, 0 otherwise.
150  * The list, as returned through "alldevsp", may be null if no interfaces
151  * were up and could be opened.
152  */
153 int
154 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
155 {
156 	pcap_if_t *devlist = NULL;
157 	struct ifaddrs *ifap, *ifa;
158 	struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
159 	size_t addr_size, broadaddr_size, dstaddr_size;
160 	int ret = 0;
161 	char *p, *q;
162 
163 	/*
164 	 * Get the list of interface addresses.
165 	 *
166 	 * Note: this won't return information about interfaces
167 	 * with no addresses; are there any such interfaces
168 	 * that would be capable of receiving packets?
169 	 * (Interfaces incapable of receiving packets aren't
170 	 * very interesting from libpcap's point of view.)
171 	 *
172 	 * LAN interfaces will probably have link-layer
173 	 * addresses; I don't know whether all implementations
174 	 * of "getifaddrs()" now, or in the future, will return
175 	 * those.
176 	 */
177 	if (getifaddrs(&ifap) != 0) {
178 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
179 		    "getifaddrs: %s", pcap_strerror(errno));
180 		return (-1);
181 	}
182 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
183 		/*
184 		 * Is this interface up?
185 		 */
186 		if (!(ifa->ifa_flags & IFF_UP)) {
187 			/*
188 			 * No, so don't add it to the list.
189 			 */
190 			continue;
191 		}
192 
193 		/*
194 		 * "ifa_addr" was apparently null on at least one
195 		 * interface on some system.
196 		 *
197 		 * "ifa_broadaddr" may be non-null even on
198 		 * non-broadcast interfaces, and was null on
199 		 * at least one OpenBSD 3.4 system on at least
200 		 * one interface with IFF_BROADCAST set.
201 		 *
202 		 * "ifa_dstaddr" was, on at least one FreeBSD 4.1
203 		 * system, non-null on a non-point-to-point
204 		 * interface.
205 		 *
206 		 * Therefore, we supply the address and netmask only
207 		 * if "ifa_addr" is non-null (if there's no address,
208 		 * there's obviously no netmask), and supply the
209 		 * broadcast and destination addresses if the appropriate
210 		 * flag is set *and* the appropriate "ifa_" entry doesn't
211 		 * evaluate to a null pointer.
212 		 */
213 		if (ifa->ifa_addr != NULL) {
214 			addr = ifa->ifa_addr;
215 			addr_size = SA_LEN(addr);
216 			netmask = ifa->ifa_netmask;
217 		} else {
218 			addr = NULL;
219 			addr_size = 0;
220 			netmask = NULL;
221 		}
222 		if (ifa->ifa_flags & IFF_BROADCAST &&
223 		    ifa->ifa_broadaddr != NULL) {
224 			broadaddr = ifa->ifa_broadaddr;
225 			broadaddr_size = SA_LEN(broadaddr);
226 		} else {
227 			broadaddr = NULL;
228 			broadaddr_size = 0;
229 		}
230 		if (ifa->ifa_flags & IFF_POINTOPOINT &&
231 		    ifa->ifa_dstaddr != NULL) {
232 			dstaddr = ifa->ifa_dstaddr;
233 			dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
234 		} else {
235 			dstaddr = NULL;
236 			dstaddr_size = 0;
237 		}
238 
239 		/*
240 		 * If this entry has a colon followed by a number at
241 		 * the end, we assume it's a logical interface.  Those
242 		 * are just the way you assign multiple IP addresses to
243 		 * a real interface on Linux, so an entry for a logical
244 		 * interface should be treated like the entry for the
245 		 * real interface; we do that by stripping off the ":"
246 		 * and the number.
247 		 *
248 		 * XXX - should we do this only on Linux?
249 		 */
250 		p = strchr(ifa->ifa_name, ':');
251 		if (p != NULL) {
252 			/*
253 			 * We have a ":"; is it followed by a number?
254 			 */
255 			q = p + 1;
256 			while (isdigit((unsigned char)*q))
257 				q++;
258 			if (*q == '\0') {
259 				/*
260 				 * All digits after the ":" until the end.
261 				 * Strip off the ":" and everything after
262 				 * it.
263 				 */
264 			       *p = '\0';
265 			}
266 		}
267 
268 		/*
269 		 * Add information for this address to the list.
270 		 */
271 		if (add_addr_to_iflist(&devlist, ifa->ifa_name,
272 		    ifa->ifa_flags, addr, addr_size, netmask, addr_size,
273 		    broadaddr, broadaddr_size, dstaddr, dstaddr_size,
274 		    errbuf) < 0) {
275 			ret = -1;
276 			break;
277 		}
278 	}
279 
280 	freeifaddrs(ifap);
281 
282 	if (ret == -1) {
283 		/*
284 		 * We had an error; free the list we've been constructing.
285 		 */
286 		if (devlist != NULL) {
287 			pcap_freealldevs(devlist);
288 			devlist = NULL;
289 		}
290 	}
291 
292 	*alldevsp = devlist;
293 	return (ret);
294 }
295