2 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: interfaceiter.c,v 1.44.120.2 2009/02/16 23:47:15 tbox Exp $ */
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #ifdef HAVE_SYS_SOCKIO_H
27 #include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
35 #include <isc/interfaceiter.h>
37 #include <isc/magic.h>
41 #include <isc/print.h>
42 #include <isc/result.h>
43 #include <isc/strerror.h>
44 #include <isc/string.h>
45 #include <isc/types.h>
48 /* Must follow <isc/net.h>. */
54 /* Common utility functions */
57 * Extract the network address part from a "struct sockaddr".
59 * The address family is given explicitly
60 * instead of using src->sa_family, because the latter does not work
61 * for copying a network mask obtained by SIOCGIFNETMASK (it does
62 * not have a valid address family).
66 get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
69 struct sockaddr_in6 *sa6;
71 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
72 !defined(ISC_PLATFORM_HAVESCOPEID)
76 /* clear any remaining value for safety */
77 memset(dst, 0, sizeof(*dst));
83 &((struct sockaddr_in *) src)->sin_addr,
84 sizeof(struct in_addr));
87 sa6 = (struct sockaddr_in6 *)src;
88 memcpy(&dst->type.in6, &sa6->sin6_addr,
89 sizeof(struct in6_addr));
90 #ifdef ISC_PLATFORM_HAVESCOPEID
91 if (sa6->sin6_scope_id != 0)
92 isc_netaddr_setzone(dst, sa6->sin6_scope_id);
95 * BSD variants embed scope zone IDs in the 128bit
96 * address as a kernel internal form. Unfortunately,
97 * the embedded IDs are not hidden from applications
98 * when getting access to them by sysctl or ioctl.
99 * We convert the internal format to the pure address
100 * part and the zone ID part.
101 * Since multicast addresses should not appear here
102 * and they cannot be distinguished from netmasks,
103 * we only consider unicast link-local addresses.
105 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
108 memcpy(&zone16, &sa6->sin6_addr.s6_addr[2],
110 zone16 = ntohs(zone16);
112 /* the zone ID is embedded */
113 isc_netaddr_setzone(dst,
114 (isc_uint32_t)zone16);
115 dst->type.in6.s6_addr[2] = 0;
116 dst->type.in6.s6_addr[3] = 0;
117 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
118 } else if (ifname != NULL) {
122 * sin6_scope_id is still not provided,
123 * but the corresponding interface name
124 * is know. Use the interface ID as
127 zone = if_nametoindex(ifname);
129 isc_netaddr_setzone(dst,
145 * Include system-dependent code.
149 #define ISC_IF_INET6_SZ \
150 sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
151 static isc_result_t linux_if_inet6_next(isc_interfaceiter_t *);
152 static isc_result_t linux_if_inet6_current(isc_interfaceiter_t *);
153 static void linux_if_inet6_first(isc_interfaceiter_t *iter);
157 #include "ifiter_getifaddrs.c"
158 #elif HAVE_IFLIST_SYSCTL
159 #include "ifiter_sysctl.c"
161 #include "ifiter_ioctl.c"
166 linux_if_inet6_first(isc_interfaceiter_t *iter) {
167 if (iter->proc != NULL) {
169 (void)linux_if_inet6_next(iter);
171 iter->valid = ISC_R_NOMORE;
175 linux_if_inet6_next(isc_interfaceiter_t *iter) {
176 if (iter->proc != NULL &&
177 fgets(iter->entry, sizeof(iter->entry), iter->proc) != NULL)
178 iter->valid = ISC_R_SUCCESS;
180 iter->valid = ISC_R_NOMORE;
181 return (iter->valid);
185 linux_if_inet6_current(isc_interfaceiter_t *iter) {
187 char name[IF_NAMESIZE+1];
188 struct in6_addr addr6;
189 int ifindex, prefix, flag3, flag4;
193 if (iter->valid != ISC_R_SUCCESS)
194 return (iter->valid);
195 if (iter->proc == NULL) {
196 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
197 ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
198 "/proc/net/if_inet6:iter->proc == NULL");
199 return (ISC_R_FAILURE);
202 res = sscanf(iter->entry, "%32[a-f0-9] %x %x %x %x %16s\n",
203 address, &ifindex, &prefix, &flag3, &flag4, name);
205 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
206 ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
207 "/proc/net/if_inet6:sscanf() -> %d (expected 6)",
209 return (ISC_R_FAILURE);
211 if (strlen(address) != 32) {
212 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
213 ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
214 "/proc/net/if_inet6:strlen(%s) != 32", address);
215 return (ISC_R_FAILURE);
217 for (i = 0; i < 16; i++) {
219 static const char hex[] = "0123456789abcdef";
220 byte = ((strchr(hex, address[i * 2]) - hex) << 4) |
221 (strchr(hex, address[i * 2 + 1]) - hex);
222 addr6.s6_addr[i] = byte;
224 iter->current.af = AF_INET6;
225 iter->current.flags = INTERFACE_F_UP;
226 isc_netaddr_fromin6(&iter->current.address, &addr6);
227 if (isc_netaddr_islinklocal(&iter->current.address)) {
228 isc_netaddr_setzone(&iter->current.address,
229 (isc_uint32_t)ifindex);
231 for (i = 0; i < 16; i++) {
233 addr6.s6_addr[i] = 0xff;
236 addr6.s6_addr[i] = (0xff << (8 - prefix)) & 0xff;
240 isc_netaddr_fromin6(&iter->current.netmask, &addr6);
241 strncpy(iter->current.name, name, sizeof(iter->current.name));
242 return (ISC_R_SUCCESS);
247 * The remaining code is common to the sysctl and ioctl case.
251 isc_interfaceiter_current(isc_interfaceiter_t *iter,
252 isc_interface_t *ifdata)
254 REQUIRE(iter->result == ISC_R_SUCCESS);
255 memcpy(ifdata, &iter->current, sizeof(*ifdata));
256 return (ISC_R_SUCCESS);
260 isc_interfaceiter_first(isc_interfaceiter_t *iter) {
263 REQUIRE(VALID_IFITER(iter));
265 internal_first(iter);
267 result = internal_current(iter);
268 if (result != ISC_R_IGNORE)
270 result = internal_next(iter);
271 if (result != ISC_R_SUCCESS)
274 iter->result = result;
279 isc_interfaceiter_next(isc_interfaceiter_t *iter) {
282 REQUIRE(VALID_IFITER(iter));
283 REQUIRE(iter->result == ISC_R_SUCCESS);
286 result = internal_next(iter);
287 if (result != ISC_R_SUCCESS)
289 result = internal_current(iter);
290 if (result != ISC_R_IGNORE)
293 iter->result = result;
298 isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
300 isc_interfaceiter_t *iter;
301 REQUIRE(iterp != NULL);
303 REQUIRE(VALID_IFITER(iter));
305 internal_destroy(iter);
306 if (iter->buf != NULL)
307 isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
310 isc_mem_put(iter->mctx, iter, sizeof(*iter));