1127664Sbms/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */ 2127664Sbms/* 3127664Sbms * Copyright (c) 1994, 1995, 1996, 1997, 1998 4127664Sbms * The Regents of the University of California. All rights reserved. 5127664Sbms * 6127664Sbms * Redistribution and use in source and binary forms, with or without 7127664Sbms * modification, are permitted provided that the following conditions 8127664Sbms * are met: 9127664Sbms * 1. Redistributions of source code must retain the above copyright 10127664Sbms * notice, this list of conditions and the following disclaimer. 11127664Sbms * 2. Redistributions in binary form must reproduce the above copyright 12127664Sbms * notice, this list of conditions and the following disclaimer in the 13127664Sbms * documentation and/or other materials provided with the distribution. 14127664Sbms * 3. All advertising materials mentioning features or use of this software 15127664Sbms * must display the following acknowledgement: 16127664Sbms * This product includes software developed by the Computer Systems 17127664Sbms * Engineering Group at Lawrence Berkeley Laboratory. 18127664Sbms * 4. Neither the name of the University nor of the Laboratory may be used 19127664Sbms * to endorse or promote products derived from this software without 20127664Sbms * specific prior written permission. 21127664Sbms * 22127664Sbms * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23127664Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24127664Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25127664Sbms * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26127664Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27127664Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28127664Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29127664Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30127664Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31127664Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32127664Sbms * SUCH DAMAGE. 33127664Sbms */ 34127664Sbms 35127664Sbms#ifndef lint 36127664Sbmsstatic const char rcsid[] _U_ = 37214518Srpaulo "@(#) $Header: /tcpdump/master/libpcap/fad-getad.c,v 1.12 2007-09-14 00:44:55 guy Exp $ (LBL)"; 38127664Sbms#endif 39127664Sbms 40127664Sbms#ifdef HAVE_CONFIG_H 41127664Sbms#include "config.h" 42127664Sbms#endif 43127664Sbms 44127664Sbms#include <sys/types.h> 45127664Sbms#include <sys/socket.h> 46127664Sbms#include <netinet/in.h> 47127664Sbms 48127664Sbms#include <net/if.h> 49127664Sbms 50147894Ssam#include <ctype.h> 51127664Sbms#include <errno.h> 52127664Sbms#include <stdio.h> 53127664Sbms#include <stdlib.h> 54147894Ssam#include <string.h> 55127664Sbms#include <ifaddrs.h> 56127664Sbms 57127664Sbms#include "pcap-int.h" 58127664Sbms 59127664Sbms#ifdef HAVE_OS_PROTO_H 60127664Sbms#include "os-proto.h" 61127664Sbms#endif 62127664Sbms 63146768Ssam#ifdef AF_PACKET 64235426Sdelphij# ifdef HAVE_NETPACKET_PACKET_H 65235426Sdelphij/* Solaris 11 and later, Linux distributions with newer glibc */ 66235426Sdelphij# include <netpacket/packet.h> 67235426Sdelphij# else /* HAVE_NETPACKET_PACKET_H */ 68235426Sdelphij/* LynxOS, Linux distributions with older glibc */ 69172677Smlaier# ifdef __Lynx__ 70214518Srpaulo/* LynxOS */ 71214518Srpaulo# include <netpacket/if_packet.h> 72235426Sdelphij# else /* __Lynx__ */ 73214518Srpaulo/* Linux */ 74214518Srpaulo# include <linux/types.h> 75214518Srpaulo# include <linux/if_packet.h> 76235426Sdelphij# endif /* __Lynx__ */ 77235426Sdelphij# endif /* HAVE_NETPACKET_PACKET_H */ 78235426Sdelphij#endif /* AF_PACKET */ 79146768Ssam 80127664Sbms/* 81127664Sbms * This is fun. 82127664Sbms * 83127664Sbms * In older BSD systems, socket addresses were fixed-length, and 84127664Sbms * "sizeof (struct sockaddr)" gave the size of the structure. 85127664Sbms * All addresses fit within a "struct sockaddr". 86127664Sbms * 87127664Sbms * In newer BSD systems, the socket address is variable-length, and 88127664Sbms * there's an "sa_len" field giving the length of the structure; 89127664Sbms * this allows socket addresses to be longer than 2 bytes of family 90127664Sbms * and 14 bytes of data. 91127664Sbms * 92127664Sbms * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553 93127664Sbms * variant of the old BSD scheme (with "struct sockaddr_storage" rather 94127664Sbms * than "struct sockaddr"), and some use the new BSD scheme. 95127664Sbms * 96127664Sbms * Some versions of GNU libc use neither scheme, but has an "SA_LEN()" 97127664Sbms * macro that determines the size based on the address family. Other 98127664Sbms * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553 99127664Sbms * but not in the final version). On the latter systems, we explicitly 100127664Sbms * check the AF_ type to determine the length; we assume that on 101127664Sbms * all those systems we have "struct sockaddr_storage". 102127664Sbms */ 103127664Sbms#ifndef SA_LEN 104127664Sbms#ifdef HAVE_SOCKADDR_SA_LEN 105127664Sbms#define SA_LEN(addr) ((addr)->sa_len) 106127664Sbms#else /* HAVE_SOCKADDR_SA_LEN */ 107127664Sbms#ifdef HAVE_SOCKADDR_STORAGE 108127664Sbmsstatic size_t 109127664Sbmsget_sa_len(struct sockaddr *addr) 110127664Sbms{ 111127664Sbms switch (addr->sa_family) { 112127664Sbms 113127664Sbms#ifdef AF_INET 114127664Sbms case AF_INET: 115127664Sbms return (sizeof (struct sockaddr_in)); 116127664Sbms#endif 117127664Sbms 118127664Sbms#ifdef AF_INET6 119127664Sbms case AF_INET6: 120127664Sbms return (sizeof (struct sockaddr_in6)); 121127664Sbms#endif 122127664Sbms 123146768Ssam#ifdef AF_PACKET 124146768Ssam case AF_PACKET: 125146768Ssam return (sizeof (struct sockaddr_ll)); 126146768Ssam#endif 127146768Ssam 128127664Sbms default: 129127664Sbms return (sizeof (struct sockaddr)); 130127664Sbms } 131127664Sbms} 132127664Sbms#define SA_LEN(addr) (get_sa_len(addr)) 133127664Sbms#else /* HAVE_SOCKADDR_STORAGE */ 134127664Sbms#define SA_LEN(addr) (sizeof (struct sockaddr)) 135127664Sbms#endif /* HAVE_SOCKADDR_STORAGE */ 136127664Sbms#endif /* HAVE_SOCKADDR_SA_LEN */ 137127664Sbms#endif /* SA_LEN */ 138127664Sbms 139127664Sbms/* 140127664Sbms * Get a list of all interfaces that are up and that we can open. 141127664Sbms * Returns -1 on error, 0 otherwise. 142127664Sbms * The list, as returned through "alldevsp", may be null if no interfaces 143127664Sbms * were up and could be opened. 144127664Sbms */ 145127664Sbmsint 146251129Sdelphijpcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf) 147127664Sbms{ 148127664Sbms pcap_if_t *devlist = NULL; 149127664Sbms struct ifaddrs *ifap, *ifa; 150127664Sbms struct sockaddr *addr, *netmask, *broadaddr, *dstaddr; 151127664Sbms size_t addr_size, broadaddr_size, dstaddr_size; 152127664Sbms int ret = 0; 153147894Ssam char *p, *q; 154127664Sbms 155127664Sbms /* 156127664Sbms * Get the list of interface addresses. 157127664Sbms * 158127664Sbms * Note: this won't return information about interfaces 159127664Sbms * with no addresses; are there any such interfaces 160127664Sbms * that would be capable of receiving packets? 161127664Sbms * (Interfaces incapable of receiving packets aren't 162127664Sbms * very interesting from libpcap's point of view.) 163127664Sbms * 164127664Sbms * LAN interfaces will probably have link-layer 165127664Sbms * addresses; I don't know whether all implementations 166127664Sbms * of "getifaddrs()" now, or in the future, will return 167127664Sbms * those. 168127664Sbms */ 169127664Sbms if (getifaddrs(&ifap) != 0) { 170127664Sbms (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 171127664Sbms "getifaddrs: %s", pcap_strerror(errno)); 172127664Sbms return (-1); 173127664Sbms } 174127664Sbms for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 175127664Sbms /* 176127664Sbms * Is this interface up? 177127664Sbms */ 178127664Sbms if (!(ifa->ifa_flags & IFF_UP)) { 179127664Sbms /* 180127664Sbms * No, so don't add it to the list. 181127664Sbms */ 182127664Sbms continue; 183127664Sbms } 184127664Sbms 185127664Sbms /* 186127664Sbms * "ifa_addr" was apparently null on at least one 187127664Sbms * interface on some system. 188127664Sbms * 189127664Sbms * "ifa_broadaddr" may be non-null even on 190127664Sbms * non-broadcast interfaces, and was null on 191127664Sbms * at least one OpenBSD 3.4 system on at least 192127664Sbms * one interface with IFF_BROADCAST set. 193127664Sbms * 194127664Sbms * "ifa_dstaddr" was, on at least one FreeBSD 4.1 195127664Sbms * system, non-null on a non-point-to-point 196127664Sbms * interface. 197127664Sbms * 198127664Sbms * Therefore, we supply the address and netmask only 199127664Sbms * if "ifa_addr" is non-null (if there's no address, 200127664Sbms * there's obviously no netmask), and supply the 201127664Sbms * broadcast and destination addresses if the appropriate 202127664Sbms * flag is set *and* the appropriate "ifa_" entry doesn't 203127664Sbms * evaluate to a null pointer. 204127664Sbms */ 205127664Sbms if (ifa->ifa_addr != NULL) { 206127664Sbms addr = ifa->ifa_addr; 207127664Sbms addr_size = SA_LEN(addr); 208127664Sbms netmask = ifa->ifa_netmask; 209127664Sbms } else { 210127664Sbms addr = NULL; 211127664Sbms addr_size = 0; 212127664Sbms netmask = NULL; 213127664Sbms } 214127664Sbms if (ifa->ifa_flags & IFF_BROADCAST && 215127664Sbms ifa->ifa_broadaddr != NULL) { 216127664Sbms broadaddr = ifa->ifa_broadaddr; 217127664Sbms broadaddr_size = SA_LEN(broadaddr); 218127664Sbms } else { 219127664Sbms broadaddr = NULL; 220127664Sbms broadaddr_size = 0; 221127664Sbms } 222127664Sbms if (ifa->ifa_flags & IFF_POINTOPOINT && 223127664Sbms ifa->ifa_dstaddr != NULL) { 224127664Sbms dstaddr = ifa->ifa_dstaddr; 225127664Sbms dstaddr_size = SA_LEN(ifa->ifa_dstaddr); 226127664Sbms } else { 227127664Sbms dstaddr = NULL; 228127664Sbms dstaddr_size = 0; 229127664Sbms } 230127664Sbms 231127664Sbms /* 232147894Ssam * If this entry has a colon followed by a number at 233147894Ssam * the end, we assume it's a logical interface. Those 234147894Ssam * are just the way you assign multiple IP addresses to 235147894Ssam * a real interface on Linux, so an entry for a logical 236147894Ssam * interface should be treated like the entry for the 237147894Ssam * real interface; we do that by stripping off the ":" 238147894Ssam * and the number. 239147894Ssam * 240147894Ssam * XXX - should we do this only on Linux? 241147894Ssam */ 242147894Ssam p = strchr(ifa->ifa_name, ':'); 243147894Ssam if (p != NULL) { 244147894Ssam /* 245147894Ssam * We have a ":"; is it followed by a number? 246147894Ssam */ 247147894Ssam q = p + 1; 248147894Ssam while (isdigit((unsigned char)*q)) 249147894Ssam q++; 250147894Ssam if (*q == '\0') { 251147894Ssam /* 252147894Ssam * All digits after the ":" until the end. 253147894Ssam * Strip off the ":" and everything after 254147894Ssam * it. 255147894Ssam */ 256147894Ssam *p = '\0'; 257147894Ssam } 258147894Ssam } 259147894Ssam 260147894Ssam /* 261127664Sbms * Add information for this address to the list. 262127664Sbms */ 263127664Sbms if (add_addr_to_iflist(&devlist, ifa->ifa_name, 264127664Sbms ifa->ifa_flags, addr, addr_size, netmask, addr_size, 265127664Sbms broadaddr, broadaddr_size, dstaddr, dstaddr_size, 266127664Sbms errbuf) < 0) { 267127664Sbms ret = -1; 268127664Sbms break; 269127664Sbms } 270127664Sbms } 271127664Sbms 272127664Sbms freeifaddrs(ifap); 273127664Sbms 274127664Sbms if (ret == -1) { 275127664Sbms /* 276127664Sbms * We had an error; free the list we've been constructing. 277127664Sbms */ 278127664Sbms if (devlist != NULL) { 279127664Sbms pcap_freealldevs(devlist); 280127664Sbms devlist = NULL; 281127664Sbms } 282127664Sbms } 283127664Sbms 284127664Sbms *alldevsp = devlist; 285127664Sbms return (ret); 286127664Sbms} 287