1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
44 #include <netinet/in.h>
55 #ifdef HAVE_OS_PROTO_H
60 * Only Solaris 10 uses this file.
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.
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.)
74 pcapint_findalldevs_interfaces(pcap_if_list_t
*devlistp
, char *errbuf
,
75 int (*check_usable
)(const char *), get_if_flags_func get_flags_func
)
77 register int fd4
, fd6
, fd
;
78 register struct lifreq
*ifrp
, *ifend
;
84 struct lifreq ifrflags
, ifrnetmask
, ifrbroadaddr
, ifrdstaddr
;
85 struct sockaddr
*netmask
, *broadaddr
, *dstaddr
;
89 * Create a socket from which to fetch the list of interfaces,
90 * and from which to fetch IPv4 information.
92 fd4
= socket(AF_INET
, SOCK_DGRAM
, 0);
94 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
95 errno
, "socket: AF_INET");
100 * Create a socket from which to fetch IPv6 information.
102 fd6
= socket(AF_INET6
, SOCK_DGRAM
, 0);
104 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
105 errno
, "socket: AF_INET6");
111 * How many entries will SIOCGLIFCONF return?
113 ifn
.lifn_family
= AF_UNSPEC
;
116 if (ioctl(fd4
, SIOCGLIFNUM
, (char *)&ifn
) < 0) {
117 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
118 errno
, "SIOCGLIFNUM");
125 * Allocate a buffer for those entries.
127 buf_size
= ifn
.lifn_count
* sizeof (struct lifreq
);
128 buf
= malloc(buf_size
);
130 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
140 ifc
.lifc_len
= buf_size
;
142 ifc
.lifc_family
= AF_UNSPEC
;
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");
155 * Loop over the entries.
157 ifrp
= (struct lifreq
*)buf
;
158 ifend
= (struct lifreq
*)(buf
+ ifc
.lifc_len
);
160 for (; ifrp
< ifend
; ifrp
++) {
162 * Can we capture on this device?
164 if (!(*check_usable
)(ifrp
->lifr_name
)) {
174 if (((struct sockaddr
*)&ifrp
->lifr_addr
)->sa_family
== AF_INET6
)
180 * Get the flags for this interface.
182 pcapint_strlcpy(ifrflags
.lifr_name
, ifrp
->lifr_name
,
183 sizeof(ifrflags
.lifr_name
));
184 if (ioctl(fd
, SIOCGLIFFLAGS
, (char *)&ifrflags
) < 0) {
187 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
188 errno
, "SIOCGLIFFLAGS: %.*s",
189 (int)sizeof(ifrflags
.lifr_name
),
196 * Get the netmask for this address on this interface.
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
) {
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
);
218 netmask
= (struct sockaddr
*)&ifrnetmask
.lifr_addr
;
221 * Get the broadcast address for this address on this
222 * interface (if any).
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
) {
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
);
246 broadaddr
= (struct sockaddr
*)&ifrbroadaddr
.lifr_broadaddr
;
249 * Not a broadcast interface, so no broadcast
256 * Get the destination address for this address on this
257 * interface (if any).
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
) {
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
);
281 dstaddr
= (struct sockaddr
*)&ifrdstaddr
.lifr_dstaddr
;
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.
293 p
= strchr(ifrp
->lifr_name
, ':');
296 * We have a ":"; is it followed by a number?
299 while (PCAP_ISDIGIT(*q
))
303 * All digits after the ":" until the end.
304 * Strip off the ":" and everything after
312 * Add information for this address to the list.
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) {