2 * Copyright (C) 2004, 2005, 2007, 2008 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.45 2008/12/01 03:51:47 marka 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 #ifdef HAVE_LINUX_IF_ADDR_H
55 # include <linux/if_addr.h>
58 /* Common utility functions */
61 * Extract the network address part from a "struct sockaddr".
63 * The address family is given explicitly
64 * instead of using src->sa_family, because the latter does not work
65 * for copying a network mask obtained by SIOCGIFNETMASK (it does
66 * not have a valid address family).
70 get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
73 struct sockaddr_in6 *sa6;
75 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
76 !defined(ISC_PLATFORM_HAVESCOPEID)
80 /* clear any remaining value for safety */
81 memset(dst, 0, sizeof(*dst));
87 &((struct sockaddr_in *)(void *)src)->sin_addr,
88 sizeof(struct in_addr));
91 sa6 = (struct sockaddr_in6 *)(void *)src;
92 memcpy(&dst->type.in6, &sa6->sin6_addr,
93 sizeof(struct in6_addr));
94 #ifdef ISC_PLATFORM_HAVESCOPEID
95 if (sa6->sin6_scope_id != 0)
96 isc_netaddr_setzone(dst, sa6->sin6_scope_id);
99 * BSD variants embed scope zone IDs in the 128bit
100 * address as a kernel internal form. Unfortunately,
101 * the embedded IDs are not hidden from applications
102 * when getting access to them by sysctl or ioctl.
103 * We convert the internal format to the pure address
104 * part and the zone ID part.
105 * Since multicast addresses should not appear here
106 * and they cannot be distinguished from netmasks,
107 * we only consider unicast link-local addresses.
109 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
112 memcpy(&zone16, &sa6->sin6_addr.s6_addr[2],
114 zone16 = ntohs(zone16);
116 /* the zone ID is embedded */
117 isc_netaddr_setzone(dst,
118 (isc_uint32_t)zone16);
119 dst->type.in6.s6_addr[2] = 0;
120 dst->type.in6.s6_addr[3] = 0;
121 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
122 } else if (ifname != NULL) {
126 * sin6_scope_id is still not provided,
127 * but the corresponding interface name
128 * is know. Use the interface ID as
131 zone = if_nametoindex(ifname);
133 isc_netaddr_setzone(dst,
149 * Include system-dependent code.
153 #define ISC_IF_INET6_SZ \
154 sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
155 static isc_result_t linux_if_inet6_next(isc_interfaceiter_t *);
156 static isc_result_t linux_if_inet6_current(isc_interfaceiter_t *);
157 static void linux_if_inet6_first(isc_interfaceiter_t *iter);
161 #include "ifiter_getifaddrs.c"
162 #elif HAVE_IFLIST_SYSCTL
163 #include "ifiter_sysctl.c"
165 #include "ifiter_ioctl.c"
170 linux_if_inet6_first(isc_interfaceiter_t *iter) {
171 if (iter->proc != NULL) {
173 (void)linux_if_inet6_next(iter);
175 iter->valid = ISC_R_NOMORE;
179 linux_if_inet6_next(isc_interfaceiter_t *iter) {
180 if (iter->proc != NULL &&
181 fgets(iter->entry, sizeof(iter->entry), iter->proc) != NULL)
182 iter->valid = ISC_R_SUCCESS;
184 iter->valid = ISC_R_NOMORE;
185 return (iter->valid);
189 linux_if_inet6_current(isc_interfaceiter_t *iter) {
191 char name[IF_NAMESIZE+1];
192 struct in6_addr addr6;
193 unsigned int ifindex;
194 int prefix, scope, flags;
198 if (iter->valid != ISC_R_SUCCESS)
199 return (iter->valid);
200 if (iter->proc == NULL) {
201 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
202 ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
203 "/proc/net/if_inet6:iter->proc == NULL");
204 return (ISC_R_FAILURE);
207 res = sscanf(iter->entry, "%32[a-f0-9] %x %x %x %x %16s\n",
208 address, &ifindex, &prefix, &scope, &flags, name);
210 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
211 ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
212 "/proc/net/if_inet6:sscanf() -> %d (expected 6)",
214 return (ISC_R_FAILURE);
216 if (strlen(address) != 32) {
217 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
218 ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
219 "/proc/net/if_inet6:strlen(%s) != 32", address);
220 return (ISC_R_FAILURE);
223 ** Ignore DAD addresses --
224 ** we can't bind to them until they are resolved
226 #ifdef IFA_F_TENTATIVE
227 if (flags & IFA_F_TENTATIVE)
228 return (ISC_R_IGNORE);
231 for (i = 0; i < 16; i++) {
233 static const char hex[] = "0123456789abcdef";
234 byte = ((strchr(hex, address[i * 2]) - hex) << 4) |
235 (strchr(hex, address[i * 2 + 1]) - hex);
236 addr6.s6_addr[i] = byte;
238 iter->current.af = AF_INET6;
239 iter->current.flags = INTERFACE_F_UP;
240 isc_netaddr_fromin6(&iter->current.address, &addr6);
241 iter->current.ifindex = ifindex;
242 if (isc_netaddr_islinklocal(&iter->current.address)) {
243 isc_netaddr_setzone(&iter->current.address,
244 (isc_uint32_t)ifindex);
246 for (i = 0; i < 16; i++) {
248 addr6.s6_addr[i] = 0xff;
251 addr6.s6_addr[i] = (0xff << (8 - prefix)) & 0xff;
255 isc_netaddr_fromin6(&iter->current.netmask, &addr6);
256 strncpy(iter->current.name, name, sizeof(iter->current.name));
257 return (ISC_R_SUCCESS);
262 * The remaining code is common to the sysctl and ioctl case.
266 isc_interfaceiter_current(isc_interfaceiter_t *iter,
267 isc_interface_t *ifdata)
269 REQUIRE(iter->result == ISC_R_SUCCESS);
270 memcpy(ifdata, &iter->current, sizeof(*ifdata));
271 return (ISC_R_SUCCESS);
275 isc_interfaceiter_first(isc_interfaceiter_t *iter) {
278 REQUIRE(VALID_IFITER(iter));
280 internal_first(iter);
282 result = internal_current(iter);
283 if (result != ISC_R_IGNORE)
285 result = internal_next(iter);
286 if (result != ISC_R_SUCCESS)
289 iter->result = result;
294 isc_interfaceiter_next(isc_interfaceiter_t *iter) {
297 REQUIRE(VALID_IFITER(iter));
298 REQUIRE(iter->result == ISC_R_SUCCESS);
301 result = internal_next(iter);
302 if (result != ISC_R_SUCCESS)
304 result = internal_current(iter);
305 if (result != ISC_R_IGNORE)
308 iter->result = result;
313 isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
315 isc_interfaceiter_t *iter;
316 REQUIRE(iterp != NULL);
318 REQUIRE(VALID_IFITER(iter));
320 internal_destroy(iter);
321 if (iter->buf != NULL)
322 isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
325 isc_mem_put(iter->mctx, iter, sizeof(*iter));