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