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