Coverage Report

Created: 2025-07-18 06:49

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