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