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
36 static const char rcsid[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/fad-glifc.c,v 1.2.2.1 2003/11/15 23:26:39 guy Exp $ (LBL)";
44 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
51 #include <sys/time.h> /* concession to AIX */
53 struct mbuf; /* Squelch compiler warnings on some platforms for */
54 struct rtentry; /* declarations in <net/if.h> */
56 #include <netinet/in.h>
68 #ifdef HAVE_OS_PROTO_H
73 * Get a list of all interfaces that are up and that we can open.
74 * Returns -1 on error, 0 otherwise.
75 * The list, as returned through "alldevsp", may be null if no interfaces
76 * were up and could be opened.
78 * This is the implementation used on platforms that have SIOCLGIFCONF
79 * but don't have "getifaddrs()". (Solaris 8 and later; we use
80 * SIOCLGIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
83 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
85 pcap_if_t *devlist = NULL;
86 register int fd4, fd6, fd;
87 register struct lifreq *ifrp, *ifend;
92 struct lifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
93 struct sockaddr *netmask, *broadaddr, *dstaddr;
97 * Create a socket from which to fetch the list of interfaces,
98 * and from which to fetch IPv4 information.
100 fd4 = socket(AF_INET, SOCK_DGRAM, 0);
102 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
103 "socket: %s", pcap_strerror(errno));
108 * Create a socket from which to fetch IPv6 information.
110 fd6 = socket(AF_INET6, SOCK_DGRAM, 0);
112 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
113 "socket: %s", pcap_strerror(errno));
119 * How many entries will SIOCGLIFCONF return?
121 ifn.lifn_family = AF_UNSPEC;
124 if (ioctl(fd4, SIOCGLIFNUM, (char *)&ifn) < 0) {
125 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
126 "SIOCGLIFNUM: %s", pcap_strerror(errno));
133 * Allocate a buffer for those entries.
135 buf_size = ifn.lifn_count * sizeof (struct lifreq);
136 buf = malloc(buf_size);
138 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
139 "malloc: %s", pcap_strerror(errno));
148 ifc.lifc_len = buf_size;
150 ifc.lifc_family = AF_UNSPEC;
152 memset(buf, 0, buf_size);
153 if (ioctl(fd4, SIOCGLIFCONF, (char *)&ifc) < 0) {
154 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
155 "SIOCGLIFCONF: %s", pcap_strerror(errno));
163 * Loop over the entries.
165 ifrp = (struct lifreq *)buf;
166 ifend = (struct lifreq *)(buf + ifc.lifc_len);
168 for (; ifrp < ifend; ifrp++) {
172 if (((struct sockaddr *)&ifrp->lifr_addr)->sa_family == AF_INET6)
178 * Get the flags for this interface, and skip it if it's
181 strncpy(ifrflags.lifr_name, ifrp->lifr_name,
182 sizeof(ifrflags.lifr_name));
183 if (ioctl(fd, SIOCGLIFFLAGS, (char *)&ifrflags) < 0) {
186 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
187 "SIOCGLIFFLAGS: %.*s: %s",
188 (int)sizeof(ifrflags.lifr_name),
190 pcap_strerror(errno));
194 if (!(ifrflags.lifr_flags & IFF_UP))
198 * Get the netmask for this address on this interface.
200 strncpy(ifrnetmask.lifr_name, ifrp->lifr_name,
201 sizeof(ifrnetmask.lifr_name));
202 memcpy(&ifrnetmask.lifr_addr, &ifrp->lifr_addr,
203 sizeof(ifrnetmask.lifr_addr));
204 if (ioctl(fd, SIOCGLIFNETMASK, (char *)&ifrnetmask) < 0) {
205 if (errno == EADDRNOTAVAIL) {
211 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
212 "SIOCGLIFNETMASK: %.*s: %s",
213 (int)sizeof(ifrnetmask.lifr_name),
214 ifrnetmask.lifr_name,
215 pcap_strerror(errno));
220 netmask = (struct sockaddr *)&ifrnetmask.lifr_addr;
223 * Get the broadcast address for this address on this
224 * interface (if any).
226 if (ifrflags.lifr_flags & IFF_BROADCAST) {
227 strncpy(ifrbroadaddr.lifr_name, ifrp->lifr_name,
228 sizeof(ifrbroadaddr.lifr_name));
229 memcpy(&ifrbroadaddr.lifr_addr, &ifrp->lifr_addr,
230 sizeof(ifrbroadaddr.lifr_addr));
231 if (ioctl(fd, SIOCGLIFBRDADDR,
232 (char *)&ifrbroadaddr) < 0) {
233 if (errno == EADDRNOTAVAIL) {
239 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
240 "SIOCGLIFBRDADDR: %.*s: %s",
241 (int)sizeof(ifrbroadaddr.lifr_name),
242 ifrbroadaddr.lifr_name,
243 pcap_strerror(errno));
248 broadaddr = (struct sockaddr *)&ifrbroadaddr.lifr_broadaddr;
251 * Not a broadcast interface, so no broadcast
258 * Get the destination address for this address on this
259 * interface (if any).
261 if (ifrflags.lifr_flags & IFF_POINTOPOINT) {
262 strncpy(ifrdstaddr.lifr_name, ifrp->lifr_name,
263 sizeof(ifrdstaddr.lifr_name));
264 memcpy(&ifrdstaddr.lifr_addr, &ifrp->lifr_addr,
265 sizeof(ifrdstaddr.lifr_addr));
266 if (ioctl(fd, SIOCGLIFDSTADDR,
267 (char *)&ifrdstaddr) < 0) {
268 if (errno == EADDRNOTAVAIL) {
274 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
275 "SIOCGLIFDSTADDR: %.*s: %s",
276 (int)sizeof(ifrdstaddr.lifr_name),
277 ifrdstaddr.lifr_name,
278 pcap_strerror(errno));
283 dstaddr = (struct sockaddr *)&ifrdstaddr.lifr_dstaddr;
288 * Add information for this address to the list.
290 if (add_addr_to_iflist(&devlist, ifrp->lifr_name,
291 ifrflags.lifr_flags, (struct sockaddr *)&ifrp->lifr_addr,
292 sizeof (struct sockaddr_storage),
293 netmask, sizeof (struct sockaddr_storage),
294 broadaddr, sizeof (struct sockaddr_storage),
295 dstaddr, sizeof (struct sockaddr_storage), errbuf) < 0) {
306 * We haven't had any errors yet; do any platform-specific
307 * operations to add devices.
309 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
315 * We had an error; free the list we've been constructing.
317 if (devlist != NULL) {
318 pcap_freealldevs(devlist);