Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- 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 | | |
35 | | #ifdef HAVE_CONFIG_H |
36 | | #include <config.h> |
37 | | #endif |
38 | | |
39 | | #include <sys/types.h> |
40 | | #include <sys/socket.h> |
41 | | #include <netinet/in.h> |
42 | | |
43 | | #include <net/if.h> |
44 | | |
45 | | #include <errno.h> |
46 | | #include <stdio.h> |
47 | | #include <stdlib.h> |
48 | | #include <string.h> |
49 | | #include <ifaddrs.h> |
50 | | |
51 | | #include "pcap-int.h" |
52 | | |
53 | | #ifdef HAVE_OS_PROTO_H |
54 | | #include "os-proto.h" |
55 | | #endif |
56 | | |
57 | | /* |
58 | | * We don't do this on Solaris 11 and later, as it appears there aren't |
59 | | * any AF_PACKET addresses on interfaces, so we don't need this, and |
60 | | * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>, |
61 | | * and their definitions of some data structures collide. |
62 | | */ |
63 | | #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) |
64 | | # ifdef HAVE_NETPACKET_PACKET_H |
65 | | /* Linux distributions with newer glibc */ |
66 | | # include <netpacket/packet.h> |
67 | | # else /* HAVE_NETPACKET_PACKET_H */ |
68 | | /* LynxOS, Linux distributions with older glibc */ |
69 | | # ifdef __Lynx__ |
70 | | /* LynxOS */ |
71 | | # include <netpacket/if_packet.h> |
72 | | # else /* __Lynx__ */ |
73 | | /* Linux */ |
74 | | # include <linux/types.h> |
75 | | # include <linux/if_packet.h> |
76 | | # endif /* __Lynx__ */ |
77 | | # endif /* HAVE_NETPACKET_PACKET_H */ |
78 | | #endif /* (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) */ |
79 | | |
80 | | /* |
81 | | * This is fun. |
82 | | * |
83 | | * In older BSD systems, socket addresses were fixed-length, and |
84 | | * "sizeof (struct sockaddr)" gave the size of the structure. |
85 | | * All addresses fit within a "struct sockaddr". |
86 | | * |
87 | | * In newer BSD systems, the socket address is variable-length, and |
88 | | * there's an "sa_len" field giving the length of the structure; |
89 | | * this allows socket addresses to be longer than 2 bytes of family |
90 | | * and 14 bytes of data. |
91 | | * |
92 | | * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553 |
93 | | * variant of the old BSD scheme (with "struct sockaddr_storage" rather |
94 | | * than "struct sockaddr"), and some use the new BSD scheme. |
95 | | * |
96 | | * Some versions of GNU libc use neither scheme, but has an "SA_LEN()" |
97 | | * macro that determines the size based on the address family. Other |
98 | | * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553 |
99 | | * but not in the final version). On the latter systems, we explicitly |
100 | | * check the AF_ type to determine the length; we assume that on |
101 | | * all those systems we have "struct sockaddr_storage". |
102 | | */ |
103 | | #ifndef SA_LEN |
104 | | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
105 | | #define SA_LEN(addr) ((addr)->sa_len) |
106 | | #else /* HAVE_STRUCT_SOCKADDR_SA_LEN */ |
107 | | #ifdef HAVE_STRUCT_SOCKADDR_STORAGE |
108 | | static size_t |
109 | | get_sa_len(struct sockaddr *addr) |
110 | 0 | { |
111 | 0 | switch (addr->sa_family) { |
112 | | |
113 | 0 | #ifdef AF_INET |
114 | 0 | case AF_INET: |
115 | 0 | return (sizeof (struct sockaddr_in)); |
116 | 0 | #endif |
117 | | |
118 | 0 | #ifdef AF_INET6 |
119 | 0 | case AF_INET6: |
120 | 0 | return (sizeof (struct sockaddr_in6)); |
121 | 0 | #endif |
122 | | |
123 | 0 | #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) |
124 | 0 | case AF_PACKET: |
125 | 0 | return (sizeof (struct sockaddr_ll)); |
126 | 0 | #endif |
127 | | |
128 | 0 | default: |
129 | 0 | return (sizeof (struct sockaddr)); |
130 | 0 | } |
131 | 0 | } |
132 | 0 | #define SA_LEN(addr) (get_sa_len(addr)) |
133 | | #else /* HAVE_STRUCT_SOCKADDR_STORAGE */ |
134 | | #define SA_LEN(addr) (sizeof (struct sockaddr)) |
135 | | #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */ |
136 | | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ |
137 | | #endif /* SA_LEN */ |
138 | | |
139 | | /* |
140 | | * Get a list of all interfaces that are up and that we can open. |
141 | | * Returns -1 on error, 0 otherwise. |
142 | | * The list, as returned through "alldevsp", may be null if no interfaces |
143 | | * could be opened. |
144 | | */ |
145 | | int |
146 | | pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, |
147 | | int (*check_usable)(const char *), get_if_flags_func get_flags_func) |
148 | 0 | { |
149 | 0 | struct ifaddrs *ifap, *ifa; |
150 | 0 | struct sockaddr *addr, *netmask, *broadaddr, *dstaddr; |
151 | 0 | size_t addr_size, broadaddr_size, dstaddr_size; |
152 | 0 | int ret = 0; |
153 | 0 | char *p, *q; |
154 | | |
155 | | /* |
156 | | * Get the list of interface addresses. |
157 | | * |
158 | | * Note: this won't return information about interfaces |
159 | | * with no addresses, so, if a platform has interfaces |
160 | | * with no interfaces on which traffic can be captured, |
161 | | * we must check for those interfaces as well (see, for |
162 | | * example, what's done on Linux). |
163 | | * |
164 | | * LAN interfaces will probably have link-layer |
165 | | * addresses; I don't know whether all implementations |
166 | | * of "getifaddrs()" now, or in the future, will return |
167 | | * those. |
168 | | */ |
169 | 0 | if (getifaddrs(&ifap) != 0) { |
170 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
171 | 0 | errno, "getifaddrs"); |
172 | 0 | return (-1); |
173 | 0 | } |
174 | 0 | for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { |
175 | | /* |
176 | | * If this entry has a colon followed by a number at |
177 | | * the end, we assume it's a logical interface. Those |
178 | | * are just the way you assign multiple IP addresses to |
179 | | * a real interface on Linux, so an entry for a logical |
180 | | * interface should be treated like the entry for the |
181 | | * real interface; we do that by stripping off the ":" |
182 | | * and the number. |
183 | | * |
184 | | * XXX - should we do this only on Linux? |
185 | | */ |
186 | 0 | p = strchr(ifa->ifa_name, ':'); |
187 | 0 | if (p != NULL) { |
188 | | /* |
189 | | * We have a ":"; is it followed by a number? |
190 | | */ |
191 | 0 | q = p + 1; |
192 | 0 | while (PCAP_ISDIGIT(*q)) |
193 | 0 | q++; |
194 | 0 | if (*q == '\0') { |
195 | | /* |
196 | | * All digits after the ":" until the end. |
197 | | * Strip off the ":" and everything after |
198 | | * it. |
199 | | */ |
200 | 0 | *p = '\0'; |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | | /* |
205 | | * Can we capture on this device? |
206 | | */ |
207 | 0 | if (!(*check_usable)(ifa->ifa_name)) { |
208 | | /* |
209 | | * No. |
210 | | */ |
211 | 0 | continue; |
212 | 0 | } |
213 | | |
214 | | /* |
215 | | * "ifa_addr" was apparently null on at least one |
216 | | * interface on some system. Therefore, we supply |
217 | | * the address and netmask only if "ifa_addr" is |
218 | | * non-null (if there's no address, there's obviously |
219 | | * no netmask). |
220 | | */ |
221 | 0 | if (ifa->ifa_addr != NULL) { |
222 | 0 | addr = ifa->ifa_addr; |
223 | 0 | addr_size = SA_LEN(addr); |
224 | 0 | netmask = ifa->ifa_netmask; |
225 | 0 | } else { |
226 | 0 | addr = NULL; |
227 | 0 | addr_size = 0; |
228 | 0 | netmask = NULL; |
229 | 0 | } |
230 | | |
231 | | /* |
232 | | * Note that, on some platforms, ifa_broadaddr and |
233 | | * ifa_dstaddr could be the same field (true on at |
234 | | * least some versions of *BSD and macOS), so we |
235 | | * can't just check whether the broadcast address |
236 | | * is null and add it if so and check whether the |
237 | | * destination address is null and add it if so. |
238 | | * |
239 | | * Therefore, we must also check the IFF_BROADCAST |
240 | | * flag, and only add a broadcast address if it's |
241 | | * set, and check the IFF_POINTTOPOINT flag, and |
242 | | * only add a destination address if it's set (as |
243 | | * per man page recommendations on some of those |
244 | | * platforms). |
245 | | */ |
246 | 0 | if (ifa->ifa_flags & IFF_BROADCAST && |
247 | 0 | ifa->ifa_broadaddr != NULL) { |
248 | 0 | broadaddr = ifa->ifa_broadaddr; |
249 | 0 | broadaddr_size = SA_LEN(broadaddr); |
250 | 0 | } else { |
251 | 0 | broadaddr = NULL; |
252 | 0 | broadaddr_size = 0; |
253 | 0 | } |
254 | 0 | if (ifa->ifa_flags & IFF_POINTOPOINT && |
255 | 0 | ifa->ifa_dstaddr != NULL) { |
256 | 0 | dstaddr = ifa->ifa_dstaddr; |
257 | 0 | dstaddr_size = SA_LEN(ifa->ifa_dstaddr); |
258 | 0 | } else { |
259 | 0 | dstaddr = NULL; |
260 | 0 | dstaddr_size = 0; |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | * Add information for this address to the list. |
265 | | */ |
266 | 0 | if (add_addr_to_if(devlistp, ifa->ifa_name, ifa->ifa_flags, |
267 | 0 | get_flags_func, |
268 | 0 | addr, addr_size, netmask, addr_size, |
269 | 0 | broadaddr, broadaddr_size, dstaddr, dstaddr_size, |
270 | 0 | errbuf) < 0) { |
271 | 0 | ret = -1; |
272 | 0 | break; |
273 | 0 | } |
274 | 0 | } |
275 | |
|
276 | 0 | freeifaddrs(ifap); |
277 | |
|
278 | 0 | return (ret); |
279 | 0 | } |