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