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