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