1feb4ecdbSBruce M Simpson /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2feb4ecdbSBruce M Simpson /*
3feb4ecdbSBruce M Simpson * Copyright (c) 1994, 1995, 1996, 1997, 1998
4feb4ecdbSBruce M Simpson * The Regents of the University of California. All rights reserved.
5feb4ecdbSBruce M Simpson *
6feb4ecdbSBruce M Simpson * Redistribution and use in source and binary forms, with or without
7feb4ecdbSBruce M Simpson * modification, are permitted provided that the following conditions
8feb4ecdbSBruce M Simpson * are met:
9feb4ecdbSBruce M Simpson * 1. Redistributions of source code must retain the above copyright
10feb4ecdbSBruce M Simpson * notice, this list of conditions and the following disclaimer.
11feb4ecdbSBruce M Simpson * 2. Redistributions in binary form must reproduce the above copyright
12feb4ecdbSBruce M Simpson * notice, this list of conditions and the following disclaimer in the
13feb4ecdbSBruce M Simpson * documentation and/or other materials provided with the distribution.
14feb4ecdbSBruce M Simpson * 3. All advertising materials mentioning features or use of this software
15feb4ecdbSBruce M Simpson * must display the following acknowledgement:
16feb4ecdbSBruce M Simpson * This product includes software developed by the Computer Systems
17feb4ecdbSBruce M Simpson * Engineering Group at Lawrence Berkeley Laboratory.
18feb4ecdbSBruce M Simpson * 4. Neither the name of the University nor of the Laboratory may be used
19feb4ecdbSBruce M Simpson * to endorse or promote products derived from this software without
20feb4ecdbSBruce M Simpson * specific prior written permission.
21feb4ecdbSBruce M Simpson *
22feb4ecdbSBruce M Simpson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23feb4ecdbSBruce M Simpson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24feb4ecdbSBruce M Simpson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25feb4ecdbSBruce M Simpson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26feb4ecdbSBruce M Simpson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27feb4ecdbSBruce M Simpson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28feb4ecdbSBruce M Simpson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29feb4ecdbSBruce M Simpson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30feb4ecdbSBruce M Simpson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31feb4ecdbSBruce M Simpson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32feb4ecdbSBruce M Simpson * SUCH DAMAGE.
33feb4ecdbSBruce M Simpson */
34feb4ecdbSBruce M Simpson
35b00ab754SHans Petter Selasky #include <config.h>
36feb4ecdbSBruce M Simpson
37feb4ecdbSBruce M Simpson #include <sys/types.h>
38feb4ecdbSBruce M Simpson #include <sys/socket.h>
39feb4ecdbSBruce M Simpson #include <netinet/in.h>
40feb4ecdbSBruce M Simpson
41feb4ecdbSBruce M Simpson #include <net/if.h>
42feb4ecdbSBruce M Simpson
43feb4ecdbSBruce M Simpson #include <errno.h>
44feb4ecdbSBruce M Simpson #include <stdio.h>
45feb4ecdbSBruce M Simpson #include <stdlib.h>
46ee2dd488SSam Leffler #include <string.h>
47feb4ecdbSBruce M Simpson #include <ifaddrs.h>
48feb4ecdbSBruce M Simpson
49feb4ecdbSBruce M Simpson #include "pcap-int.h"
50feb4ecdbSBruce M Simpson
51feb4ecdbSBruce M Simpson #ifdef HAVE_OS_PROTO_H
52feb4ecdbSBruce M Simpson #include "os-proto.h"
53feb4ecdbSBruce M Simpson #endif
54feb4ecdbSBruce M Simpson
55681ed54cSXin LI /*
56681ed54cSXin LI * We don't do this on Solaris 11 and later, as it appears there aren't
57681ed54cSXin LI * any AF_PACKET addresses on interfaces, so we don't need this, and
58681ed54cSXin LI * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>,
59681ed54cSXin LI * and their definitions of some data structures collide.
60681ed54cSXin LI */
61*afdbf109SJoseph Mingrone #if (defined(__linux__) || defined(__Lynx__)) && defined(AF_PACKET)
62d1e87331SXin LI # ifdef HAVE_NETPACKET_PACKET_H
63681ed54cSXin LI /* Linux distributions with newer glibc */
64d1e87331SXin LI # include <netpacket/packet.h>
65d1e87331SXin LI # else /* HAVE_NETPACKET_PACKET_H */
66d1e87331SXin LI /* LynxOS, Linux distributions with older glibc */
67ef96d74fSMax Laier # ifdef __Lynx__
68a0ee43a1SRui Paulo /* LynxOS */
69a0ee43a1SRui Paulo # include <netpacket/if_packet.h>
70d1e87331SXin LI # else /* __Lynx__ */
71a0ee43a1SRui Paulo /* Linux */
72a0ee43a1SRui Paulo # include <linux/types.h>
73a0ee43a1SRui Paulo # include <linux/if_packet.h>
74d1e87331SXin LI # endif /* __Lynx__ */
75d1e87331SXin LI # endif /* HAVE_NETPACKET_PACKET_H */
76*afdbf109SJoseph Mingrone #endif /* (defined(__linux__) || defined(__Lynx__)) && defined(AF_PACKET) */
7704fb2745SSam Leffler
78feb4ecdbSBruce M Simpson /*
79feb4ecdbSBruce M Simpson * This is fun.
80feb4ecdbSBruce M Simpson *
81feb4ecdbSBruce M Simpson * In older BSD systems, socket addresses were fixed-length, and
82feb4ecdbSBruce M Simpson * "sizeof (struct sockaddr)" gave the size of the structure.
83feb4ecdbSBruce M Simpson * All addresses fit within a "struct sockaddr".
84feb4ecdbSBruce M Simpson *
85feb4ecdbSBruce M Simpson * In newer BSD systems, the socket address is variable-length, and
86feb4ecdbSBruce M Simpson * there's an "sa_len" field giving the length of the structure;
87feb4ecdbSBruce M Simpson * this allows socket addresses to be longer than 2 bytes of family
88feb4ecdbSBruce M Simpson * and 14 bytes of data.
89feb4ecdbSBruce M Simpson *
90feb4ecdbSBruce M Simpson * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
91feb4ecdbSBruce M Simpson * variant of the old BSD scheme (with "struct sockaddr_storage" rather
92feb4ecdbSBruce M Simpson * than "struct sockaddr"), and some use the new BSD scheme.
93feb4ecdbSBruce M Simpson *
94feb4ecdbSBruce M Simpson * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
95feb4ecdbSBruce M Simpson * macro that determines the size based on the address family. Other
96feb4ecdbSBruce M Simpson * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
97feb4ecdbSBruce M Simpson * but not in the final version). On the latter systems, we explicitly
98feb4ecdbSBruce M Simpson * check the AF_ type to determine the length; we assume that on
99feb4ecdbSBruce M Simpson * all those systems we have "struct sockaddr_storage".
100*afdbf109SJoseph Mingrone *
101*afdbf109SJoseph Mingrone * OSes that use this file are:
102*afdbf109SJoseph Mingrone * - FreeBSD (HAVE_STRUCT_SOCKADDR_SA_LEN is defined)
103*afdbf109SJoseph Mingrone * - Haiku (HAVE_STRUCT_SOCKADDR_SA_LEN is defined)
104*afdbf109SJoseph Mingrone * - Hurd (HAVE_STRUCT_SOCKADDR_SA_LEN is defined)
105*afdbf109SJoseph Mingrone * - illumos (HAVE_STRUCT_SOCKADDR_SA_LEN is not defined)
106*afdbf109SJoseph Mingrone * - Linux (HAVE_STRUCT_SOCKADDR_SA_LEN is not defined)
107*afdbf109SJoseph Mingrone * - macOS (HAVE_STRUCT_SOCKADDR_SA_LEN is defined)
108*afdbf109SJoseph Mingrone * - NetBSD (HAVE_STRUCT_SOCKADDR_SA_LEN is defined)
109*afdbf109SJoseph Mingrone * - OpenBSD (SA_LEN() is defined)
110*afdbf109SJoseph Mingrone * - Solaris 11 (HAVE_STRUCT_SOCKADDR_SA_LEN is not defined)
111feb4ecdbSBruce M Simpson */
112feb4ecdbSBruce M Simpson #ifndef SA_LEN
113b00ab754SHans Petter Selasky #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
114feb4ecdbSBruce M Simpson #define SA_LEN(addr) ((addr)->sa_len)
115b00ab754SHans Petter Selasky #else /* HAVE_STRUCT_SOCKADDR_SA_LEN */
116b00ab754SHans Petter Selasky #ifdef HAVE_STRUCT_SOCKADDR_STORAGE
117feb4ecdbSBruce M Simpson static size_t
get_sa_len(struct sockaddr * addr)118feb4ecdbSBruce M Simpson get_sa_len(struct sockaddr *addr)
119feb4ecdbSBruce M Simpson {
120feb4ecdbSBruce M Simpson switch (addr->sa_family) {
121feb4ecdbSBruce M Simpson
122feb4ecdbSBruce M Simpson #ifdef AF_INET
123feb4ecdbSBruce M Simpson case AF_INET:
124feb4ecdbSBruce M Simpson return (sizeof (struct sockaddr_in));
125feb4ecdbSBruce M Simpson #endif
126feb4ecdbSBruce M Simpson
127feb4ecdbSBruce M Simpson #ifdef AF_INET6
128feb4ecdbSBruce M Simpson case AF_INET6:
129feb4ecdbSBruce M Simpson return (sizeof (struct sockaddr_in6));
130feb4ecdbSBruce M Simpson #endif
131feb4ecdbSBruce M Simpson
132*afdbf109SJoseph Mingrone #if (defined(__linux__) || defined(__Lynx__)) && defined(AF_PACKET)
13304fb2745SSam Leffler case AF_PACKET:
13404fb2745SSam Leffler return (sizeof (struct sockaddr_ll));
13504fb2745SSam Leffler #endif
13604fb2745SSam Leffler
137*afdbf109SJoseph Mingrone #ifdef AF_LINK
138*afdbf109SJoseph Mingrone case AF_LINK:
139*afdbf109SJoseph Mingrone return (sizeof (struct sockaddr_dl));
140*afdbf109SJoseph Mingrone #endif
141*afdbf109SJoseph Mingrone
142feb4ecdbSBruce M Simpson default:
143feb4ecdbSBruce M Simpson return (sizeof (struct sockaddr));
144feb4ecdbSBruce M Simpson }
145feb4ecdbSBruce M Simpson }
146feb4ecdbSBruce M Simpson #define SA_LEN(addr) (get_sa_len(addr))
147b00ab754SHans Petter Selasky #else /* HAVE_STRUCT_SOCKADDR_STORAGE */
148feb4ecdbSBruce M Simpson #define SA_LEN(addr) (sizeof (struct sockaddr))
149b00ab754SHans Petter Selasky #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
150b00ab754SHans Petter Selasky #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
151feb4ecdbSBruce M Simpson #endif /* SA_LEN */
152feb4ecdbSBruce M Simpson
153feb4ecdbSBruce M Simpson /*
154feb4ecdbSBruce M Simpson * Get a list of all interfaces that are up and that we can open.
155feb4ecdbSBruce M Simpson * Returns -1 on error, 0 otherwise.
156feb4ecdbSBruce M Simpson * The list, as returned through "alldevsp", may be null if no interfaces
157681ed54cSXin LI * could be opened.
158feb4ecdbSBruce M Simpson */
159feb4ecdbSBruce M Simpson int
pcapint_findalldevs_interfaces(pcap_if_list_t * devlistp,char * errbuf,int (* check_usable)(const char *),get_if_flags_func get_flags_func)160*afdbf109SJoseph Mingrone pcapint_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
161b00ab754SHans Petter Selasky int (*check_usable)(const char *), get_if_flags_func get_flags_func)
162feb4ecdbSBruce M Simpson {
163feb4ecdbSBruce M Simpson struct ifaddrs *ifap, *ifa;
164feb4ecdbSBruce M Simpson struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
165feb4ecdbSBruce M Simpson size_t addr_size, broadaddr_size, dstaddr_size;
166feb4ecdbSBruce M Simpson int ret = 0;
167ee2dd488SSam Leffler char *p, *q;
168feb4ecdbSBruce M Simpson
169feb4ecdbSBruce M Simpson /*
170feb4ecdbSBruce M Simpson * Get the list of interface addresses.
171feb4ecdbSBruce M Simpson *
172feb4ecdbSBruce M Simpson * Note: this won't return information about interfaces
173681ed54cSXin LI * with no addresses, so, if a platform has interfaces
174681ed54cSXin LI * with no interfaces on which traffic can be captured,
175681ed54cSXin LI * we must check for those interfaces as well (see, for
176681ed54cSXin LI * example, what's done on Linux).
177feb4ecdbSBruce M Simpson *
178feb4ecdbSBruce M Simpson * LAN interfaces will probably have link-layer
179feb4ecdbSBruce M Simpson * addresses; I don't know whether all implementations
180feb4ecdbSBruce M Simpson * of "getifaddrs()" now, or in the future, will return
181feb4ecdbSBruce M Simpson * those.
182feb4ecdbSBruce M Simpson */
183feb4ecdbSBruce M Simpson if (getifaddrs(&ifap) != 0) {
184*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
185b00ab754SHans Petter Selasky errno, "getifaddrs");
186feb4ecdbSBruce M Simpson return (-1);
187feb4ecdbSBruce M Simpson }
188feb4ecdbSBruce M Simpson for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
189feb4ecdbSBruce M Simpson /*
190ada6f083SXin LI * If this entry has a colon followed by a number at
191ada6f083SXin LI * the end, we assume it's a logical interface. Those
192ada6f083SXin LI * are just the way you assign multiple IP addresses to
193ada6f083SXin LI * a real interface on Linux, so an entry for a logical
194ada6f083SXin LI * interface should be treated like the entry for the
195ada6f083SXin LI * real interface; we do that by stripping off the ":"
196ada6f083SXin LI * and the number.
197ada6f083SXin LI *
198ada6f083SXin LI * XXX - should we do this only on Linux?
199ada6f083SXin LI */
200ada6f083SXin LI p = strchr(ifa->ifa_name, ':');
201ada6f083SXin LI if (p != NULL) {
202ada6f083SXin LI /*
203ada6f083SXin LI * We have a ":"; is it followed by a number?
204ada6f083SXin LI */
205ada6f083SXin LI q = p + 1;
2066f9cba8fSJoseph Mingrone while (PCAP_ISDIGIT(*q))
207ada6f083SXin LI q++;
208ada6f083SXin LI if (*q == '\0') {
209ada6f083SXin LI /*
210ada6f083SXin LI * All digits after the ":" until the end.
211ada6f083SXin LI * Strip off the ":" and everything after
212ada6f083SXin LI * it.
213ada6f083SXin LI */
214ada6f083SXin LI *p = '\0';
215ada6f083SXin LI }
216ada6f083SXin LI }
217ada6f083SXin LI
218ada6f083SXin LI /*
219ada6f083SXin LI * Can we capture on this device?
220ada6f083SXin LI */
221ada6f083SXin LI if (!(*check_usable)(ifa->ifa_name)) {
222ada6f083SXin LI /*
223ada6f083SXin LI * No.
224ada6f083SXin LI */
225ada6f083SXin LI continue;
226ada6f083SXin LI }
227ada6f083SXin LI
228ada6f083SXin LI /*
229feb4ecdbSBruce M Simpson * "ifa_addr" was apparently null on at least one
230681ed54cSXin LI * interface on some system. Therefore, we supply
231681ed54cSXin LI * the address and netmask only if "ifa_addr" is
232681ed54cSXin LI * non-null (if there's no address, there's obviously
233681ed54cSXin LI * no netmask).
234feb4ecdbSBruce M Simpson */
235feb4ecdbSBruce M Simpson if (ifa->ifa_addr != NULL) {
236feb4ecdbSBruce M Simpson addr = ifa->ifa_addr;
237feb4ecdbSBruce M Simpson addr_size = SA_LEN(addr);
238feb4ecdbSBruce M Simpson netmask = ifa->ifa_netmask;
239feb4ecdbSBruce M Simpson } else {
240feb4ecdbSBruce M Simpson addr = NULL;
241feb4ecdbSBruce M Simpson addr_size = 0;
242feb4ecdbSBruce M Simpson netmask = NULL;
243feb4ecdbSBruce M Simpson }
244681ed54cSXin LI
245681ed54cSXin LI /*
246681ed54cSXin LI * Note that, on some platforms, ifa_broadaddr and
247681ed54cSXin LI * ifa_dstaddr could be the same field (true on at
248b00ab754SHans Petter Selasky * least some versions of *BSD and macOS), so we
249681ed54cSXin LI * can't just check whether the broadcast address
250681ed54cSXin LI * is null and add it if so and check whether the
251681ed54cSXin LI * destination address is null and add it if so.
252681ed54cSXin LI *
253681ed54cSXin LI * Therefore, we must also check the IFF_BROADCAST
254681ed54cSXin LI * flag, and only add a broadcast address if it's
255681ed54cSXin LI * set, and check the IFF_POINTTOPOINT flag, and
256681ed54cSXin LI * only add a destination address if it's set (as
257681ed54cSXin LI * per man page recommendations on some of those
258681ed54cSXin LI * platforms).
259681ed54cSXin LI */
260feb4ecdbSBruce M Simpson if (ifa->ifa_flags & IFF_BROADCAST &&
261feb4ecdbSBruce M Simpson ifa->ifa_broadaddr != NULL) {
262feb4ecdbSBruce M Simpson broadaddr = ifa->ifa_broadaddr;
263feb4ecdbSBruce M Simpson broadaddr_size = SA_LEN(broadaddr);
264feb4ecdbSBruce M Simpson } else {
265feb4ecdbSBruce M Simpson broadaddr = NULL;
266feb4ecdbSBruce M Simpson broadaddr_size = 0;
267feb4ecdbSBruce M Simpson }
268feb4ecdbSBruce M Simpson if (ifa->ifa_flags & IFF_POINTOPOINT &&
269feb4ecdbSBruce M Simpson ifa->ifa_dstaddr != NULL) {
270feb4ecdbSBruce M Simpson dstaddr = ifa->ifa_dstaddr;
271feb4ecdbSBruce M Simpson dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
272feb4ecdbSBruce M Simpson } else {
273feb4ecdbSBruce M Simpson dstaddr = NULL;
274feb4ecdbSBruce M Simpson dstaddr_size = 0;
275feb4ecdbSBruce M Simpson }
276feb4ecdbSBruce M Simpson
277feb4ecdbSBruce M Simpson /*
278feb4ecdbSBruce M Simpson * Add information for this address to the list.
279feb4ecdbSBruce M Simpson */
280*afdbf109SJoseph Mingrone if (pcapint_add_addr_to_if(devlistp, ifa->ifa_name, ifa->ifa_flags,
281b00ab754SHans Petter Selasky get_flags_func,
282ada6f083SXin LI addr, addr_size, netmask, addr_size,
283feb4ecdbSBruce M Simpson broadaddr, broadaddr_size, dstaddr, dstaddr_size,
284feb4ecdbSBruce M Simpson errbuf) < 0) {
285feb4ecdbSBruce M Simpson ret = -1;
286feb4ecdbSBruce M Simpson break;
287feb4ecdbSBruce M Simpson }
288feb4ecdbSBruce M Simpson }
289feb4ecdbSBruce M Simpson
290feb4ecdbSBruce M Simpson freeifaddrs(ifap);
291feb4ecdbSBruce M Simpson
292feb4ecdbSBruce M Simpson return (ret);
293feb4ecdbSBruce M Simpson }
294