4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: interfaceiter.c,v 1.44.120.2 2009/02/16 23:47:15 tbox Exp */
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_SOCKIO_H
29 #include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
37 #include <isc/interfaceiter.h>
39 #include <isc/magic.h>
43 #include <isc/print.h>
44 #include <isc/result.h>
45 #include <isc/strerror.h>
46 #include <isc/string.h>
47 #include <isc/types.h>
50 /* Must follow <isc/net.h>. */
56 /* Common utility functions */
59 * Extract the network address part from a "struct sockaddr".
61 * The address family is given explicitly
62 * instead of using src->sa_family, because the latter does not work
63 * for copying a network mask obtained by SIOCGIFNETMASK (it does
64 * not have a valid address family).
68 get_addr(unsigned int family
, isc_netaddr_t
*dst
, struct sockaddr
*src
,
71 struct sockaddr_in6
*sa6
;
73 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
74 !defined(ISC_PLATFORM_HAVESCOPEID)
78 /* clear any remaining value for safety */
79 memset(dst
, 0, sizeof(*dst
));
85 &((struct sockaddr_in
*) src
)->sin_addr
,
86 sizeof(struct in_addr
));
89 sa6
= (struct sockaddr_in6
*)src
;
90 memcpy(&dst
->type
.in6
, &sa6
->sin6_addr
,
91 sizeof(struct in6_addr
));
92 #ifdef ISC_PLATFORM_HAVESCOPEID
93 if (sa6
->sin6_scope_id
!= 0)
94 isc_netaddr_setzone(dst
, sa6
->sin6_scope_id
);
97 * BSD variants embed scope zone IDs in the 128bit
98 * address as a kernel internal form. Unfortunately,
99 * the embedded IDs are not hidden from applications
100 * when getting access to them by sysctl or ioctl.
101 * We convert the internal format to the pure address
102 * part and the zone ID part.
103 * Since multicast addresses should not appear here
104 * and they cannot be distinguished from netmasks,
105 * we only consider unicast link-local addresses.
107 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
110 memcpy(&zone16
, &sa6
->sin6_addr
.s6_addr
[2],
112 zone16
= ntohs(zone16
);
114 /* the zone ID is embedded */
115 isc_netaddr_setzone(dst
,
116 (isc_uint32_t
)zone16
);
117 dst
->type
.in6
.s6_addr
[2] = 0;
118 dst
->type
.in6
.s6_addr
[3] = 0;
119 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
120 } else if (ifname
!= NULL
) {
124 * sin6_scope_id is still not provided,
125 * but the corresponding interface name
126 * is know. Use the interface ID as
129 zone
= if_nametoindex(ifname
);
131 isc_netaddr_setzone(dst
,
147 * Include system-dependent code.
151 #define ISC_IF_INET6_SZ \
152 sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
153 static isc_result_t
linux_if_inet6_next(isc_interfaceiter_t
*);
154 static isc_result_t
linux_if_inet6_current(isc_interfaceiter_t
*);
155 static void linux_if_inet6_first(isc_interfaceiter_t
*iter
);
159 #include "ifiter_getifaddrs.c"
160 #elif HAVE_IFLIST_SYSCTL
161 #include "ifiter_sysctl.c"
163 #include "ifiter_ioctl.c"
168 linux_if_inet6_first(isc_interfaceiter_t
*iter
) {
169 if (iter
->proc
!= NULL
) {
171 (void)linux_if_inet6_next(iter
);
173 iter
->valid
= ISC_R_NOMORE
;
177 linux_if_inet6_next(isc_interfaceiter_t
*iter
) {
178 if (iter
->proc
!= NULL
&&
179 fgets(iter
->entry
, sizeof(iter
->entry
), iter
->proc
) != NULL
)
180 iter
->valid
= ISC_R_SUCCESS
;
182 iter
->valid
= ISC_R_NOMORE
;
183 return (iter
->valid
);
187 linux_if_inet6_current(isc_interfaceiter_t
*iter
) {
189 char name
[IF_NAMESIZE
+1];
190 struct in6_addr addr6
;
191 int ifindex
, prefix
, flag3
, flag4
;
195 if (iter
->valid
!= ISC_R_SUCCESS
)
196 return (iter
->valid
);
197 if (iter
->proc
== NULL
) {
198 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
199 ISC_LOGMODULE_INTERFACE
, ISC_LOG_ERROR
,
200 "/proc/net/if_inet6:iter->proc == NULL");
201 return (ISC_R_FAILURE
);
204 res
= sscanf(iter
->entry
, "%32[a-f0-9] %x %x %x %x %16s\n",
205 address
, &ifindex
, &prefix
, &flag3
, &flag4
, name
);
207 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
208 ISC_LOGMODULE_INTERFACE
, ISC_LOG_ERROR
,
209 "/proc/net/if_inet6:sscanf() -> %d (expected 6)",
211 return (ISC_R_FAILURE
);
213 if (strlen(address
) != 32) {
214 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
215 ISC_LOGMODULE_INTERFACE
, ISC_LOG_ERROR
,
216 "/proc/net/if_inet6:strlen(%s) != 32", address
);
217 return (ISC_R_FAILURE
);
219 for (i
= 0; i
< 16; i
++) {
221 static const char hex
[] = "0123456789abcdef";
222 byte
= ((strchr(hex
, address
[i
* 2]) - hex
) << 4) |
223 (strchr(hex
, address
[i
* 2 + 1]) - hex
);
224 addr6
.s6_addr
[i
] = byte
;
226 iter
->current
.af
= AF_INET6
;
227 iter
->current
.flags
= INTERFACE_F_UP
;
228 isc_netaddr_fromin6(&iter
->current
.address
, &addr6
);
229 if (isc_netaddr_islinklocal(&iter
->current
.address
)) {
230 isc_netaddr_setzone(&iter
->current
.address
,
231 (isc_uint32_t
)ifindex
);
233 for (i
= 0; i
< 16; i
++) {
235 addr6
.s6_addr
[i
] = 0xff;
238 addr6
.s6_addr
[i
] = (0xff << (8 - prefix
)) & 0xff;
242 isc_netaddr_fromin6(&iter
->current
.netmask
, &addr6
);
243 strncpy(iter
->current
.name
, name
, sizeof(iter
->current
.name
));
244 return (ISC_R_SUCCESS
);
249 * The remaining code is common to the sysctl and ioctl case.
253 isc_interfaceiter_current(isc_interfaceiter_t
*iter
,
254 isc_interface_t
*ifdata
)
256 REQUIRE(iter
->result
== ISC_R_SUCCESS
);
257 memcpy(ifdata
, &iter
->current
, sizeof(*ifdata
));
258 return (ISC_R_SUCCESS
);
262 isc_interfaceiter_first(isc_interfaceiter_t
*iter
) {
265 REQUIRE(VALID_IFITER(iter
));
267 internal_first(iter
);
269 result
= internal_current(iter
);
270 if (result
!= ISC_R_IGNORE
)
272 result
= internal_next(iter
);
273 if (result
!= ISC_R_SUCCESS
)
276 iter
->result
= result
;
281 isc_interfaceiter_next(isc_interfaceiter_t
*iter
) {
284 REQUIRE(VALID_IFITER(iter
));
285 REQUIRE(iter
->result
== ISC_R_SUCCESS
);
288 result
= internal_next(iter
);
289 if (result
!= ISC_R_SUCCESS
)
291 result
= internal_current(iter
);
292 if (result
!= ISC_R_IGNORE
)
295 iter
->result
= result
;
300 isc_interfaceiter_destroy(isc_interfaceiter_t
**iterp
)
302 isc_interfaceiter_t
*iter
;
303 REQUIRE(iterp
!= NULL
);
305 REQUIRE(VALID_IFITER(iter
));
307 internal_destroy(iter
);
308 if (iter
->buf
!= NULL
)
309 isc_mem_put(iter
->mctx
, iter
->buf
, iter
->bufsize
);
312 isc_mem_put(iter
->mctx
, iter
, sizeof(*iter
));