4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
26 #include <libinetutil.h>
31 #include <sys/socket.h>
32 #include <sys/sockio.h>
35 * Create a list of the addresses on physical interface `ifname' with at least
36 * one of the flags in `set' set and all of the flags in `clear' clear.
37 * Return the number of items in the list, or -1 on failure.
40 ifaddrlistx(const char *ifname
, uint64_t set
, uint64_t clear
,
41 ifaddrlistx_t
**ifaddrsp
)
46 ifaddrlistx_t
*ifaddrp
, *ifaddrs
= NULL
;
47 int i
, nlifr
, naddr
= 0;
53 struct sockaddr_storage addr
;
55 (void) memset(&lifc
, 0, sizeof (lifc
));
56 flags
= LIFC_NOXMIT
| LIFC_ALLZONES
| LIFC_TEMPORARY
| LIFC_UNDER_IPMP
;
59 * We need both IPv4 and IPv6 sockets to query both IPv4 and IPv6
62 if ((s4
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1 ||
63 (s6
= socket(AF_INET6
, SOCK_DGRAM
, 0)) == -1) {
68 * Get the number of network interfaces of type `family'.
70 lifn
.lifn_family
= AF_UNSPEC
;
71 lifn
.lifn_flags
= flags
;
73 if (ioctl(s4
, SIOCGLIFNUM
, &lifn
) == -1)
77 * Pad the interface count to detect when additional interfaces have
78 * been configured between SIOCGLIFNUM and SIOCGLIFCONF.
82 lifc
.lifc_flags
= flags
;
83 lifc
.lifc_family
= AF_UNSPEC
;
84 lifc
.lifc_len
= lifn
.lifn_count
* sizeof (struct lifreq
);
85 if ((lifc
.lifc_buf
= realloc(lifc
.lifc_buf
, lifc
.lifc_len
)) == NULL
)
88 if (ioctl(s4
, SIOCGLIFCONF
, &lifc
) == -1)
92 * If every lifr_req slot is taken, then additional interfaces must
93 * have been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
94 * Recalculate to make sure we didn't miss any interfaces.
96 nlifr
= lifc
.lifc_len
/ sizeof (struct lifreq
);
97 if (nlifr
>= lifn
.lifn_count
)
101 * Populate the ifaddrlistx by querying each matching interface. If a
102 * query ioctl returns ENXIO, then the interface must have been
103 * removed after the SIOCGLIFCONF completed -- so we just ignore it.
105 for (lifrp
= lifc
.lifc_req
, i
= 0; i
< nlifr
; i
++, lifrp
++) {
106 if ((cp
= strchr(lifrp
->lifr_name
, ':')) != NULL
)
109 if (strcmp(lifrp
->lifr_name
, ifname
) != 0)
115 addr
= lifrp
->lifr_addr
;
116 isv6
= addr
.ss_family
== AF_INET6
;
117 if (ioctl(isv6
? s6
: s4
, SIOCGLIFFLAGS
, lifrp
) == -1) {
123 if (set
!= 0 && ((lifrp
->lifr_flags
& set
) == 0) ||
124 (lifrp
->lifr_flags
& clear
) != 0)
128 * We've got a match; allocate a new record.
130 if ((ifaddrp
= malloc(sizeof (ifaddrlistx_t
))) == NULL
)
133 (void) strlcpy(ifaddrp
->ia_name
, lifrp
->lifr_name
, LIFNAMSIZ
);
134 ifaddrp
->ia_flags
= lifrp
->lifr_flags
;
135 ifaddrp
->ia_addr
= addr
;
136 ifaddrp
->ia_next
= ifaddrs
;
151 ifaddrlistx_free(ifaddrs
);
157 * Free the provided ifaddrlistx_t.
160 ifaddrlistx_free(ifaddrlistx_t
*ifaddrp
)
162 ifaddrlistx_t
*next_ifaddrp
;
164 for (; ifaddrp
!= NULL
; ifaddrp
= next_ifaddrp
) {
165 next_ifaddrp
= ifaddrp
->ia_next
;