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