1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
39 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #ifdef HAVE_SYS_SOCKIO_H
44 #include <sys/sockio.h>
46 #include <sys/time.h> /* concession to AIX */
48 struct mbuf
; /* Squelch compiler warnings on some platforms for */
49 struct rtentry
; /* declarations in <net/if.h> */
51 #include <netinet/in.h>
62 #ifdef HAVE_OS_PROTO_H
67 * Get a list of all interfaces that are up and that we can open.
68 * Returns -1 on error, 0 otherwise.
69 * The list, as returned through "alldevsp", may be null if no interfaces
70 * were up and could be opened.
72 * This is the implementation used on platforms that have SIOCGLIFCONF
73 * but don't have "getifaddrs()". (Solaris 8 and later; we use
74 * SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
77 pcap_findalldevs_interfaces(pcap_if_list_t
*devlistp
, char *errbuf
,
78 int (*check_usable
)(const char *), get_if_flags_func get_flags_func
)
80 register int fd4
, fd6
, fd
;
81 register struct lifreq
*ifrp
, *ifend
;
89 struct lifreq ifrflags
, ifrnetmask
, ifrbroadaddr
, ifrdstaddr
;
90 struct sockaddr
*netmask
, *broadaddr
, *dstaddr
;
94 * Create a socket from which to fetch the list of interfaces,
95 * and from which to fetch IPv4 information.
97 fd4
= socket(AF_INET
, SOCK_DGRAM
, 0);
99 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
100 errno
, "socket: AF_INET");
105 * Create a socket from which to fetch IPv6 information.
107 fd6
= socket(AF_INET6
, SOCK_DGRAM
, 0);
109 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
110 errno
, "socket: AF_INET6");
116 * How many entries will SIOCGLIFCONF return?
118 ifn
.lifn_family
= AF_UNSPEC
;
121 if (ioctl(fd4
, SIOCGLIFNUM
, (char *)&ifn
) < 0) {
122 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
123 errno
, "SIOCGLIFNUM");
130 * Allocate a buffer for those entries.
132 buf_size
= ifn
.lifn_count
* sizeof (struct lifreq
);
133 buf
= malloc(buf_size
);
135 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
145 ifc
.lifc_len
= buf_size
;
147 ifc
.lifc_family
= AF_UNSPEC
;
149 memset(buf
, 0, buf_size
);
150 if (ioctl(fd4
, SIOCGLIFCONF
, (char *)&ifc
) < 0) {
151 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
152 errno
, "SIOCGLIFCONF");
160 * Loop over the entries.
162 ifrp
= (struct lifreq
*)buf
;
163 ifend
= (struct lifreq
*)(buf
+ ifc
.lifc_len
);
165 for (; ifrp
< ifend
; ifrp
++) {
167 * Skip entries that begin with "dummy".
168 * XXX - what are these? Is this Linux-specific?
169 * Are there platforms on which we shouldn't do this?
171 if (strncmp(ifrp
->lifr_name
, "dummy", 5) == 0)
175 * Can we capture on this device?
177 if (!(*check_usable
)(ifrp
->lifr_name
)) {
187 if (((struct sockaddr
*)&ifrp
->lifr_addr
)->sa_family
== AF_INET6
)
193 * Get the flags for this interface.
195 strncpy(ifrflags
.lifr_name
, ifrp
->lifr_name
,
196 sizeof(ifrflags
.lifr_name
));
197 if (ioctl(fd
, SIOCGLIFFLAGS
, (char *)&ifrflags
) < 0) {
200 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
201 errno
, "SIOCGLIFFLAGS: %.*s",
202 (int)sizeof(ifrflags
.lifr_name
),
209 * Get the netmask for this address on this interface.
211 strncpy(ifrnetmask
.lifr_name
, ifrp
->lifr_name
,
212 sizeof(ifrnetmask
.lifr_name
));
213 memcpy(&ifrnetmask
.lifr_addr
, &ifrp
->lifr_addr
,
214 sizeof(ifrnetmask
.lifr_addr
));
215 if (ioctl(fd
, SIOCGLIFNETMASK
, (char *)&ifrnetmask
) < 0) {
216 if (errno
== EADDRNOTAVAIL
) {
222 pcap_fmt_errmsg_for_errno(errbuf
,
223 PCAP_ERRBUF_SIZE
, errno
,
224 "SIOCGLIFNETMASK: %.*s",
225 (int)sizeof(ifrnetmask
.lifr_name
),
226 ifrnetmask
.lifr_name
);
231 netmask
= (struct sockaddr
*)&ifrnetmask
.lifr_addr
;
234 * Get the broadcast address for this address on this
235 * interface (if any).
237 if (ifrflags
.lifr_flags
& IFF_BROADCAST
) {
238 strncpy(ifrbroadaddr
.lifr_name
, ifrp
->lifr_name
,
239 sizeof(ifrbroadaddr
.lifr_name
));
240 memcpy(&ifrbroadaddr
.lifr_addr
, &ifrp
->lifr_addr
,
241 sizeof(ifrbroadaddr
.lifr_addr
));
242 if (ioctl(fd
, SIOCGLIFBRDADDR
,
243 (char *)&ifrbroadaddr
) < 0) {
244 if (errno
== EADDRNOTAVAIL
) {
250 pcap_fmt_errmsg_for_errno(errbuf
,
251 PCAP_ERRBUF_SIZE
, errno
,
252 "SIOCGLIFBRDADDR: %.*s",
253 (int)sizeof(ifrbroadaddr
.lifr_name
),
254 ifrbroadaddr
.lifr_name
);
259 broadaddr
= (struct sockaddr
*)&ifrbroadaddr
.lifr_broadaddr
;
262 * Not a broadcast interface, so no broadcast
269 * Get the destination address for this address on this
270 * interface (if any).
272 if (ifrflags
.lifr_flags
& IFF_POINTOPOINT
) {
273 strncpy(ifrdstaddr
.lifr_name
, ifrp
->lifr_name
,
274 sizeof(ifrdstaddr
.lifr_name
));
275 memcpy(&ifrdstaddr
.lifr_addr
, &ifrp
->lifr_addr
,
276 sizeof(ifrdstaddr
.lifr_addr
));
277 if (ioctl(fd
, SIOCGLIFDSTADDR
,
278 (char *)&ifrdstaddr
) < 0) {
279 if (errno
== EADDRNOTAVAIL
) {
285 pcap_fmt_errmsg_for_errno(errbuf
,
286 PCAP_ERRBUF_SIZE
, errno
,
287 "SIOCGLIFDSTADDR: %.*s",
288 (int)sizeof(ifrdstaddr
.lifr_name
),
289 ifrdstaddr
.lifr_name
);
294 dstaddr
= (struct sockaddr
*)&ifrdstaddr
.lifr_dstaddr
;
300 * If this entry has a colon followed by a number at
301 * the end, it's a logical interface. Those are just
302 * the way you assign multiple IP addresses to a real
303 * interface, so an entry for a logical interface should
304 * be treated like the entry for the real interface;
305 * we do that by stripping off the ":" and the number.
307 p
= strchr(ifrp
->lifr_name
, ':');
310 * We have a ":"; is it followed by a number?
313 while (PCAP_ISDIGIT(*q
))
317 * All digits after the ":" until the end.
318 * Strip off the ":" and everything after
327 * Add information for this address to the list.
329 if (add_addr_to_if(devlistp
, ifrp
->lifr_name
,
330 ifrflags
.lifr_flags
, get_flags_func
,
331 (struct sockaddr
*)&ifrp
->lifr_addr
,
332 sizeof (struct sockaddr_storage
),
333 netmask
, sizeof (struct sockaddr_storage
),
334 broadaddr
, sizeof (struct sockaddr_storage
),
335 dstaddr
, sizeof (struct sockaddr_storage
), errbuf
) < 0) {