blob: 6b275eb3c840a8f46c4e5fc86ae3cb966453a2e6 [file] [log] [blame]
The Android Open Source Project478ab6c2009-03-03 19:30:05 -08001/* -*- 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
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080035#ifdef HAVE_CONFIG_H
Haibo Huang165065a2018-07-23 17:26:52 -070036#include <config.h>
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080037#endif
38
39#include <sys/param.h>
40#include <sys/file.h>
41#include <sys/ioctl.h>
42#include <sys/socket.h>
43#ifdef HAVE_SYS_SOCKIO_H
44#include <sys/sockio.h>
45#endif
46#include <sys/time.h> /* concession to AIX */
47
48struct mbuf; /* Squelch compiler warnings on some platforms for */
49struct rtentry; /* declarations in <net/if.h> */
50#include <net/if.h>
51#include <netinet/in.h>
52
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080053#include <errno.h>
54#include <memory.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60#include "pcap-int.h"
61
62#ifdef HAVE_OS_PROTO_H
63#include "os-proto.h"
64#endif
65
66/*
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.
71 *
JP Abgrall511eca32014-02-12 13:46:45 -080072 * This is the implementation used on platforms that have SIOCGLIFCONF
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080073 * but don't have "getifaddrs()". (Solaris 8 and later; we use
JP Abgrall511eca32014-02-12 13:46:45 -080074 * SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080075 */
76int
Haibo Huang165065a2018-07-23 17:26:52 -070077pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
78 int (*check_usable)(const char *), get_if_flags_func get_flags_func)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080079{
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080080 register int fd4, fd6, fd;
81 register struct lifreq *ifrp, *ifend;
82 struct lifnum ifn;
83 struct lifconf ifc;
84 char *buf = NULL;
85 unsigned buf_size;
86#ifdef HAVE_SOLARIS
87 char *p, *q;
88#endif
89 struct lifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
90 struct sockaddr *netmask, *broadaddr, *dstaddr;
91 int ret = 0;
92
93 /*
94 * Create a socket from which to fetch the list of interfaces,
95 * and from which to fetch IPv4 information.
96 */
97 fd4 = socket(AF_INET, SOCK_DGRAM, 0);
98 if (fd4 < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -070099 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
100 errno, "socket: AF_INET");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800101 return (-1);
102 }
103
104 /*
105 * Create a socket from which to fetch IPv6 information.
106 */
107 fd6 = socket(AF_INET6, SOCK_DGRAM, 0);
108 if (fd6 < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700109 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
110 errno, "socket: AF_INET6");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800111 (void)close(fd4);
112 return (-1);
113 }
114
115 /*
116 * How many entries will SIOCGLIFCONF return?
117 */
118 ifn.lifn_family = AF_UNSPEC;
119 ifn.lifn_flags = 0;
120 ifn.lifn_count = 0;
121 if (ioctl(fd4, SIOCGLIFNUM, (char *)&ifn) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700122 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
123 errno, "SIOCGLIFNUM");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800124 (void)close(fd6);
125 (void)close(fd4);
126 return (-1);
127 }
128
129 /*
130 * Allocate a buffer for those entries.
131 */
132 buf_size = ifn.lifn_count * sizeof (struct lifreq);
133 buf = malloc(buf_size);
134 if (buf == NULL) {
Haibo Huang165065a2018-07-23 17:26:52 -0700135 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
136 errno, "malloc");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800137 (void)close(fd6);
138 (void)close(fd4);
139 return (-1);
140 }
141
142 /*
143 * Get the entries.
144 */
145 ifc.lifc_len = buf_size;
146 ifc.lifc_buf = buf;
147 ifc.lifc_family = AF_UNSPEC;
148 ifc.lifc_flags = 0;
149 memset(buf, 0, buf_size);
150 if (ioctl(fd4, SIOCGLIFCONF, (char *)&ifc) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700151 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
152 errno, "SIOCGLIFCONF");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800153 (void)close(fd6);
154 (void)close(fd4);
155 free(buf);
156 return (-1);
157 }
158
159 /*
160 * Loop over the entries.
161 */
162 ifrp = (struct lifreq *)buf;
163 ifend = (struct lifreq *)(buf + ifc.lifc_len);
164
165 for (; ifrp < ifend; ifrp++) {
166 /*
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800167 * 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?
170 */
171 if (strncmp(ifrp->lifr_name, "dummy", 5) == 0)
172 continue;
173
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800174 /*
Elliott Hughes965a4b52017-05-15 10:37:39 -0700175 * Can we capture on this device?
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800176 */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700177 if (!(*check_usable)(ifrp->lifr_name)) {
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800178 /*
Elliott Hughes965a4b52017-05-15 10:37:39 -0700179 * No.
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800180 */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700181 continue;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800182 }
Elliott Hughes965a4b52017-05-15 10:37:39 -0700183
184 /*
185 * IPv6 or not?
186 */
187 if (((struct sockaddr *)&ifrp->lifr_addr)->sa_family == AF_INET6)
188 fd = fd6;
189 else
190 fd = fd4;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800191
192 /*
Elliott Hughesd8845d72015-10-19 18:07:04 -0700193 * Get the flags for this interface.
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800194 */
195 strncpy(ifrflags.lifr_name, ifrp->lifr_name,
196 sizeof(ifrflags.lifr_name));
197 if (ioctl(fd, SIOCGLIFFLAGS, (char *)&ifrflags) < 0) {
198 if (errno == ENXIO)
199 continue;
Haibo Huang165065a2018-07-23 17:26:52 -0700200 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
201 errno, "SIOCGLIFFLAGS: %.*s",
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800202 (int)sizeof(ifrflags.lifr_name),
Haibo Huang165065a2018-07-23 17:26:52 -0700203 ifrflags.lifr_name);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800204 ret = -1;
205 break;
206 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800207
208 /*
209 * Get the netmask for this address on this interface.
210 */
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) {
217 /*
218 * Not available.
219 */
220 netmask = NULL;
221 } else {
Haibo Huang165065a2018-07-23 17:26:52 -0700222 pcap_fmt_errmsg_for_errno(errbuf,
223 PCAP_ERRBUF_SIZE, errno,
224 "SIOCGLIFNETMASK: %.*s",
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800225 (int)sizeof(ifrnetmask.lifr_name),
Haibo Huang165065a2018-07-23 17:26:52 -0700226 ifrnetmask.lifr_name);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800227 ret = -1;
228 break;
229 }
230 } else
231 netmask = (struct sockaddr *)&ifrnetmask.lifr_addr;
232
233 /*
234 * Get the broadcast address for this address on this
235 * interface (if any).
236 */
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) {
245 /*
246 * Not available.
247 */
248 broadaddr = NULL;
249 } else {
Haibo Huang165065a2018-07-23 17:26:52 -0700250 pcap_fmt_errmsg_for_errno(errbuf,
251 PCAP_ERRBUF_SIZE, errno,
252 "SIOCGLIFBRDADDR: %.*s",
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800253 (int)sizeof(ifrbroadaddr.lifr_name),
Haibo Huang165065a2018-07-23 17:26:52 -0700254 ifrbroadaddr.lifr_name);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800255 ret = -1;
256 break;
257 }
258 } else
259 broadaddr = (struct sockaddr *)&ifrbroadaddr.lifr_broadaddr;
260 } else {
261 /*
262 * Not a broadcast interface, so no broadcast
263 * address.
264 */
265 broadaddr = NULL;
266 }
267
268 /*
269 * Get the destination address for this address on this
270 * interface (if any).
271 */
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) {
280 /*
281 * Not available.
282 */
283 dstaddr = NULL;
284 } else {
Haibo Huang165065a2018-07-23 17:26:52 -0700285 pcap_fmt_errmsg_for_errno(errbuf,
286 PCAP_ERRBUF_SIZE, errno,
287 "SIOCGLIFDSTADDR: %.*s",
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800288 (int)sizeof(ifrdstaddr.lifr_name),
Haibo Huang165065a2018-07-23 17:26:52 -0700289 ifrdstaddr.lifr_name);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800290 ret = -1;
291 break;
292 }
293 } else
294 dstaddr = (struct sockaddr *)&ifrdstaddr.lifr_dstaddr;
295 } else
296 dstaddr = NULL;
297
298#ifdef HAVE_SOLARIS
299 /*
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.
306 */
307 p = strchr(ifrp->lifr_name, ':');
308 if (p != NULL) {
309 /*
310 * We have a ":"; is it followed by a number?
311 */
312 q = p + 1;
Haibo Huangee759ce2021-01-05 21:34:29 -0800313 while (PCAP_ISDIGIT(*q))
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800314 q++;
315 if (*q == '\0') {
316 /*
317 * All digits after the ":" until the end.
318 * Strip off the ":" and everything after
319 * it.
320 */
321 *p = '\0';
322 }
323 }
324#endif
325
326 /*
327 * Add information for this address to the list.
328 */
Haibo Huang165065a2018-07-23 17:26:52 -0700329 if (add_addr_to_if(devlistp, ifrp->lifr_name,
330 ifrflags.lifr_flags, get_flags_func,
Elliott Hughes965a4b52017-05-15 10:37:39 -0700331 (struct sockaddr *)&ifrp->lifr_addr,
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800332 sizeof (struct sockaddr_storage),
333 netmask, sizeof (struct sockaddr_storage),
334 broadaddr, sizeof (struct sockaddr_storage),
335 dstaddr, sizeof (struct sockaddr_storage), errbuf) < 0) {
336 ret = -1;
337 break;
338 }
339 }
340 free(buf);
341 (void)close(fd6);
342 (void)close(fd4);
343
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800344 return (ret);
345}