]> The Tcpdump Group git mirrors - libpcap/blob - fad-glifc.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / fad-glifc.c
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/param.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42
43 #include <net/if.h>
44 #include <netinet/in.h>
45
46 #include <errno.h>
47 #include <memory.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "pcap-int.h"
54
55 #ifdef HAVE_OS_PROTO_H
56 #include "os-proto.h"
57 #endif
58
59 /*
60 * Only Solaris 10 uses this file.
61 */
62
63 /*
64 * Get a list of all interfaces that are up and that we can open.
65 * Returns -1 on error, 0 otherwise.
66 * The list, as returned through "alldevsp", may be null if no interfaces
67 * were up and could be opened.
68 *
69 * This is the implementation used on platforms that have SIOCGLIFCONF
70 * but don't have "getifaddrs()". (Solaris 8 and later; we use
71 * SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
72 */
73 int
74 pcapint_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
75 int (*check_usable)(const char *), get_if_flags_func get_flags_func)
76 {
77 register int fd4, fd6, fd;
78 register struct lifreq *ifrp, *ifend;
79 struct lifnum ifn;
80 struct lifconf ifc;
81 char *buf = NULL;
82 unsigned buf_size;
83 char *p, *q;
84 struct lifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
85 struct sockaddr *netmask, *broadaddr, *dstaddr;
86 int ret = 0;
87
88 /*
89 * Create a socket from which to fetch the list of interfaces,
90 * and from which to fetch IPv4 information.
91 */
92 fd4 = socket(AF_INET, SOCK_DGRAM, 0);
93 if (fd4 < 0) {
94 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
95 errno, "socket: AF_INET");
96 return (-1);
97 }
98
99 /*
100 * Create a socket from which to fetch IPv6 information.
101 */
102 fd6 = socket(AF_INET6, SOCK_DGRAM, 0);
103 if (fd6 < 0) {
104 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
105 errno, "socket: AF_INET6");
106 (void)close(fd4);
107 return (-1);
108 }
109
110 /*
111 * How many entries will SIOCGLIFCONF return?
112 */
113 ifn.lifn_family = AF_UNSPEC;
114 ifn.lifn_flags = 0;
115 ifn.lifn_count = 0;
116 if (ioctl(fd4, SIOCGLIFNUM, (char *)&ifn) < 0) {
117 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
118 errno, "SIOCGLIFNUM");
119 (void)close(fd6);
120 (void)close(fd4);
121 return (-1);
122 }
123
124 /*
125 * Allocate a buffer for those entries.
126 */
127 buf_size = ifn.lifn_count * sizeof (struct lifreq);
128 buf = malloc(buf_size);
129 if (buf == NULL) {
130 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
131 errno, "malloc");
132 (void)close(fd6);
133 (void)close(fd4);
134 return (-1);
135 }
136
137 /*
138 * Get the entries.
139 */
140 ifc.lifc_len = buf_size;
141 ifc.lifc_buf = buf;
142 ifc.lifc_family = AF_UNSPEC;
143 ifc.lifc_flags = 0;
144 memset(buf, 0, buf_size);
145 if (ioctl(fd4, SIOCGLIFCONF, (char *)&ifc) < 0) {
146 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
147 errno, "SIOCGLIFCONF");
148 (void)close(fd6);
149 (void)close(fd4);
150 free(buf);
151 return (-1);
152 }
153
154 /*
155 * Loop over the entries.
156 */
157 ifrp = (struct lifreq *)buf;
158 ifend = (struct lifreq *)(buf + ifc.lifc_len);
159
160 for (; ifrp < ifend; ifrp++) {
161 /*
162 * Can we capture on this device?
163 */
164 if (!(*check_usable)(ifrp->lifr_name)) {
165 /*
166 * No.
167 */
168 continue;
169 }
170
171 /*
172 * IPv6 or not?
173 */
174 if (((struct sockaddr *)&ifrp->lifr_addr)->sa_family == AF_INET6)
175 fd = fd6;
176 else
177 fd = fd4;
178
179 /*
180 * Get the flags for this interface.
181 */
182 pcapint_strlcpy(ifrflags.lifr_name, ifrp->lifr_name,
183 sizeof(ifrflags.lifr_name));
184 if (ioctl(fd, SIOCGLIFFLAGS, (char *)&ifrflags) < 0) {
185 if (errno == ENXIO)
186 continue;
187 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
188 errno, "SIOCGLIFFLAGS: %.*s",
189 (int)sizeof(ifrflags.lifr_name),
190 ifrflags.lifr_name);
191 ret = -1;
192 break;
193 }
194
195 /*
196 * Get the netmask for this address on this interface.
197 */
198 pcapint_strlcpy(ifrnetmask.lifr_name, ifrp->lifr_name,
199 sizeof(ifrnetmask.lifr_name));
200 memcpy(&ifrnetmask.lifr_addr, &ifrp->lifr_addr,
201 sizeof(ifrnetmask.lifr_addr));
202 if (ioctl(fd, SIOCGLIFNETMASK, (char *)&ifrnetmask) < 0) {
203 if (errno == EADDRNOTAVAIL) {
204 /*
205 * Not available.
206 */
207 netmask = NULL;
208 } else {
209 pcapint_fmt_errmsg_for_errno(errbuf,
210 PCAP_ERRBUF_SIZE, errno,
211 "SIOCGLIFNETMASK: %.*s",
212 (int)sizeof(ifrnetmask.lifr_name),
213 ifrnetmask.lifr_name);
214 ret = -1;
215 break;
216 }
217 } else
218 netmask = (struct sockaddr *)&ifrnetmask.lifr_addr;
219
220 /*
221 * Get the broadcast address for this address on this
222 * interface (if any).
223 */
224 if (ifrflags.lifr_flags & IFF_BROADCAST) {
225 pcapint_strlcpy(ifrbroadaddr.lifr_name, ifrp->lifr_name,
226 sizeof(ifrbroadaddr.lifr_name));
227 memcpy(&ifrbroadaddr.lifr_addr, &ifrp->lifr_addr,
228 sizeof(ifrbroadaddr.lifr_addr));
229 if (ioctl(fd, SIOCGLIFBRDADDR,
230 (char *)&ifrbroadaddr) < 0) {
231 if (errno == EADDRNOTAVAIL) {
232 /*
233 * Not available.
234 */
235 broadaddr = NULL;
236 } else {
237 pcapint_fmt_errmsg_for_errno(errbuf,
238 PCAP_ERRBUF_SIZE, errno,
239 "SIOCGLIFBRDADDR: %.*s",
240 (int)sizeof(ifrbroadaddr.lifr_name),
241 ifrbroadaddr.lifr_name);
242 ret = -1;
243 break;
244 }
245 } else
246 broadaddr = (struct sockaddr *)&ifrbroadaddr.lifr_broadaddr;
247 } else {
248 /*
249 * Not a broadcast interface, so no broadcast
250 * address.
251 */
252 broadaddr = NULL;
253 }
254
255 /*
256 * Get the destination address for this address on this
257 * interface (if any).
258 */
259 if (ifrflags.lifr_flags & IFF_POINTOPOINT) {
260 pcapint_strlcpy(ifrdstaddr.lifr_name, ifrp->lifr_name,
261 sizeof(ifrdstaddr.lifr_name));
262 memcpy(&ifrdstaddr.lifr_addr, &ifrp->lifr_addr,
263 sizeof(ifrdstaddr.lifr_addr));
264 if (ioctl(fd, SIOCGLIFDSTADDR,
265 (char *)&ifrdstaddr) < 0) {
266 if (errno == EADDRNOTAVAIL) {
267 /*
268 * Not available.
269 */
270 dstaddr = NULL;
271 } else {
272 pcapint_fmt_errmsg_for_errno(errbuf,
273 PCAP_ERRBUF_SIZE, errno,
274 "SIOCGLIFDSTADDR: %.*s",
275 (int)sizeof(ifrdstaddr.lifr_name),
276 ifrdstaddr.lifr_name);
277 ret = -1;
278 break;
279 }
280 } else
281 dstaddr = (struct sockaddr *)&ifrdstaddr.lifr_dstaddr;
282 } else
283 dstaddr = NULL;
284
285 /*
286 * If this entry has a colon followed by a number at
287 * the end, it's a logical interface. Those are just
288 * the way you assign multiple IP addresses to a real
289 * interface, so an entry for a logical interface should
290 * be treated like the entry for the real interface;
291 * we do that by stripping off the ":" and the number.
292 */
293 p = strchr(ifrp->lifr_name, ':');
294 if (p != NULL) {
295 /*
296 * We have a ":"; is it followed by a number?
297 */
298 q = p + 1;
299 while (PCAP_ISDIGIT(*q))
300 q++;
301 if (*q == '\0') {
302 /*
303 * All digits after the ":" until the end.
304 * Strip off the ":" and everything after
305 * it.
306 */
307 *p = '\0';
308 }
309 }
310
311 /*
312 * Add information for this address to the list.
313 */
314 if (pcapint_add_addr_to_if(devlistp, ifrp->lifr_name,
315 ifrflags.lifr_flags, get_flags_func,
316 (struct sockaddr *)&ifrp->lifr_addr,
317 sizeof (struct sockaddr_storage),
318 netmask, sizeof (struct sockaddr_storage),
319 broadaddr, sizeof (struct sockaddr_storage),
320 dstaddr, sizeof (struct sockaddr_storage), errbuf) < 0) {
321 ret = -1;
322 break;
323 }
324 }
325 free(buf);
326 (void)close(fd6);
327 (void)close(fd4);
328
329 return (ret);
330 }