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>
63 #ifdef HAVE_OS_PROTO_H
68 * Get a list of all interfaces that are up and that we can open.
69 * Returns -1 on error, 0 otherwise.
70 * The list, as returned through "alldevsp", may be null if no interfaces
71 * were up and could be opened.
73 * This is the implementation used on platforms that have SIOCGLIFCONF
74 * but don't have "getifaddrs()". (Solaris 8 and later; we use
75 * SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
78 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf,
79 int (*check_usable)(const char *))
81 pcap_if_t *devlist = NULL;
82 register int fd4, fd6, fd;
83 register struct lifreq *ifrp, *ifend;
91 struct lifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
92 struct sockaddr *netmask, *broadaddr, *dstaddr;
96 * Create a socket from which to fetch the list of interfaces,
97 * and from which to fetch IPv4 information.
99 fd4 = socket(AF_INET, SOCK_DGRAM, 0);
101 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
102 "socket: %s", pcap_strerror(errno));
107 * Create a socket from which to fetch IPv6 information.
109 fd6 = socket(AF_INET6, SOCK_DGRAM, 0);
111 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
112 "socket: %s", pcap_strerror(errno));
118 * How many entries will SIOCGLIFCONF return?
120 ifn.lifn_family = AF_UNSPEC;
123 if (ioctl(fd4, SIOCGLIFNUM, (char *)&ifn) < 0) {
124 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
125 "SIOCGLIFNUM: %s", pcap_strerror(errno));
132 * Allocate a buffer for those entries.
134 buf_size = ifn.lifn_count * sizeof (struct lifreq);
135 buf = malloc(buf_size);
137 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
138 "malloc: %s", pcap_strerror(errno));
147 ifc.lifc_len = buf_size;
149 ifc.lifc_family = AF_UNSPEC;
151 memset(buf, 0, buf_size);
152 if (ioctl(fd4, SIOCGLIFCONF, (char *)&ifc) < 0) {
153 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
154 "SIOCGLIFCONF: %s", pcap_strerror(errno));
162 * Loop over the entries.
164 ifrp = (struct lifreq *)buf;
165 ifend = (struct lifreq *)(buf + ifc.lifc_len);
167 for (; ifrp < ifend; ifrp++) {
169 * Skip entries that begin with "dummy".
170 * XXX - what are these? Is this Linux-specific?
171 * Are there platforms on which we shouldn't do this?
173 if (strncmp(ifrp->lifr_name, "dummy", 5) == 0)
177 * Can we capture on this device?
179 if (!(*check_usable)(ifrp->lifr_name)) {
189 if (((struct sockaddr *)&ifrp->lifr_addr)->sa_family == AF_INET6)
195 * Get the flags for this interface.
197 strncpy(ifrflags.lifr_name, ifrp->lifr_name,
198 sizeof(ifrflags.lifr_name));
199 if (ioctl(fd, SIOCGLIFFLAGS, (char *)&ifrflags) < 0) {
202 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
203 "SIOCGLIFFLAGS: %.*s: %s",
204 (int)sizeof(ifrflags.lifr_name),
206 pcap_strerror(errno));
212 * Get the netmask for this address on this interface.
214 strncpy(ifrnetmask.lifr_name, ifrp->lifr_name,
215 sizeof(ifrnetmask.lifr_name));
216 memcpy(&ifrnetmask.lifr_addr, &ifrp->lifr_addr,
217 sizeof(ifrnetmask.lifr_addr));
218 if (ioctl(fd, SIOCGLIFNETMASK, (char *)&ifrnetmask) < 0) {
219 if (errno == EADDRNOTAVAIL) {
225 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
226 "SIOCGLIFNETMASK: %.*s: %s",
227 (int)sizeof(ifrnetmask.lifr_name),
228 ifrnetmask.lifr_name,
229 pcap_strerror(errno));
234 netmask = (struct sockaddr *)&ifrnetmask.lifr_addr;
237 * Get the broadcast address for this address on this
238 * interface (if any).
240 if (ifrflags.lifr_flags & IFF_BROADCAST) {
241 strncpy(ifrbroadaddr.lifr_name, ifrp->lifr_name,
242 sizeof(ifrbroadaddr.lifr_name));
243 memcpy(&ifrbroadaddr.lifr_addr, &ifrp->lifr_addr,
244 sizeof(ifrbroadaddr.lifr_addr));
245 if (ioctl(fd, SIOCGLIFBRDADDR,
246 (char *)&ifrbroadaddr) < 0) {
247 if (errno == EADDRNOTAVAIL) {
253 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
254 "SIOCGLIFBRDADDR: %.*s: %s",
255 (int)sizeof(ifrbroadaddr.lifr_name),
256 ifrbroadaddr.lifr_name,
257 pcap_strerror(errno));
262 broadaddr = (struct sockaddr *)&ifrbroadaddr.lifr_broadaddr;
265 * Not a broadcast interface, so no broadcast
272 * Get the destination address for this address on this
273 * interface (if any).
275 if (ifrflags.lifr_flags & IFF_POINTOPOINT) {
276 strncpy(ifrdstaddr.lifr_name, ifrp->lifr_name,
277 sizeof(ifrdstaddr.lifr_name));
278 memcpy(&ifrdstaddr.lifr_addr, &ifrp->lifr_addr,
279 sizeof(ifrdstaddr.lifr_addr));
280 if (ioctl(fd, SIOCGLIFDSTADDR,
281 (char *)&ifrdstaddr) < 0) {
282 if (errno == EADDRNOTAVAIL) {
288 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
289 "SIOCGLIFDSTADDR: %.*s: %s",
290 (int)sizeof(ifrdstaddr.lifr_name),
291 ifrdstaddr.lifr_name,
292 pcap_strerror(errno));
297 dstaddr = (struct sockaddr *)&ifrdstaddr.lifr_dstaddr;
303 * If this entry has a colon followed by a number at
304 * the end, it's a logical interface. Those are just
305 * the way you assign multiple IP addresses to a real
306 * interface, so an entry for a logical interface should
307 * be treated like the entry for the real interface;
308 * we do that by stripping off the ":" and the number.
310 p = strchr(ifrp->lifr_name, ':');
313 * We have a ":"; is it followed by a number?
316 while (isdigit((unsigned char)*q))
320 * All digits after the ":" until the end.
321 * Strip off the ":" and everything after
330 * Add information for this address to the list.
332 if (add_addr_to_iflist(&devlist, ifrp->lifr_name,
333 if_flags_to_pcap_flags(ifrp->lifr_name, ifrflags.lifr_flags),
334 (struct sockaddr *)&ifrp->lifr_addr,
335 sizeof (struct sockaddr_storage),
336 netmask, sizeof (struct sockaddr_storage),
337 broadaddr, sizeof (struct sockaddr_storage),
338 dstaddr, sizeof (struct sockaddr_storage), errbuf) < 0) {
349 * We had an error; free the list we've been constructing.
351 if (devlist != NULL) {
352 pcap_freealldevs(devlist);