1 /* $NetBSD: ifiter_sysctl.c,v 1.3 2004/02/26 18:17:13 drochner Exp $ */
4 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and 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: ifiter_sysctl.c,v 1.14.12.7 2004/03/08 09:04:56 marka Exp */
23 * Obtain the list of network interfaces using sysctl.
24 * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
31 #include <net/route.h>
32 #include <net/if_dl.h>
34 /* XXX what about Alpha? */
36 #define ROUNDUP(a) ((a) > 0 ? \
37 (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
40 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
44 #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S')
45 #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
47 struct isc_interfaceiter
{
48 unsigned int magic
; /* Magic number. */
50 void *buf
; /* Buffer for sysctl data. */
51 unsigned int bufsize
; /* Bytes allocated. */
52 unsigned int bufused
; /* Bytes used. */
53 unsigned int pos
; /* Current offset in
55 isc_interface_t current
; /* Current interface data. */
56 isc_result_t result
; /* Last result code. */
63 0, /* Any address family. */
69 isc_interfaceiter_create(isc_mem_t
*mctx
, isc_interfaceiter_t
**iterp
) {
70 isc_interfaceiter_t
*iter
;
74 char strbuf
[ISC_STRERRORSIZE
];
76 REQUIRE(iterp
!= NULL
);
77 REQUIRE(*iterp
== NULL
);
79 iter
= isc_mem_get(mctx
, sizeof(*iter
));
81 return (ISC_R_NOMEMORY
);
87 * Determine the amount of memory needed.
90 if (sysctl(mib
, 6, NULL
, &bufsize
, NULL
, (size_t) 0) < 0) {
91 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
92 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
93 isc_msgcat_get(isc_msgcat
,
94 ISC_MSGSET_IFITERSYSCTL
,
95 ISC_MSG_GETIFLISTSIZE
,
97 "list size: sysctl: %s"),
99 result
= ISC_R_UNEXPECTED
;
102 iter
->bufsize
= bufsize
;
104 iter
->buf
= isc_mem_get(iter
->mctx
, iter
->bufsize
);
105 if (iter
->buf
== NULL
) {
106 result
= ISC_R_NOMEMORY
;
111 if (sysctl(mib
, 6, iter
->buf
, &bufused
, NULL
, (size_t) 0) < 0) {
112 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
113 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
114 isc_msgcat_get(isc_msgcat
,
115 ISC_MSGSET_IFITERSYSCTL
,
117 "getting interface list: "
120 result
= ISC_R_UNEXPECTED
;
123 iter
->bufused
= bufused
;
124 INSIST(iter
->bufused
<= iter
->bufsize
);
127 * A newly created iterator has an undefined position
128 * until isc_interfaceiter_first() is called.
130 iter
->pos
= (unsigned int) -1;
131 iter
->result
= ISC_R_FAILURE
;
133 iter
->magic
= IFITER_MAGIC
;
135 return (ISC_R_SUCCESS
);
138 if (iter
->buf
!= NULL
)
139 isc_mem_put(mctx
, iter
->buf
, iter
->bufsize
);
140 isc_mem_put(mctx
, iter
, sizeof(*iter
));
145 * Get information about the current interface to iter->current.
146 * If successful, return ISC_R_SUCCESS.
147 * If the interface has an unsupported address family,
148 * return ISC_R_IGNORE. In case of other failure,
149 * return ISC_R_UNEXPECTED.
153 internal_current(isc_interfaceiter_t
*iter
) {
154 struct ifa_msghdr
*ifam
, *ifam_end
;
156 REQUIRE(VALID_IFITER(iter
));
157 REQUIRE (iter
->pos
< (unsigned int) iter
->bufused
);
159 ifam
= (struct ifa_msghdr
*) ((char *) iter
->buf
+ iter
->pos
);
160 ifam_end
= (struct ifa_msghdr
*) ((char *) iter
->buf
+ iter
->bufused
);
162 if (ifam
->ifam_type
== RTM_IFINFO
) {
163 struct if_msghdr
*ifm
= (struct if_msghdr
*) ifam
;
164 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) (ifm
+ 1);
165 unsigned int namelen
;
167 memset(&iter
->current
, 0, sizeof(iter
->current
));
169 namelen
= sdl
->sdl_nlen
;
170 if (namelen
> sizeof(iter
->current
.name
) - 1)
171 namelen
= sizeof(iter
->current
.name
) - 1;
173 memset(iter
->current
.name
, 0, sizeof(iter
->current
.name
));
174 memcpy(iter
->current
.name
, sdl
->sdl_data
, namelen
);
176 iter
->current
.flags
= 0;
178 if ((ifam
->ifam_flags
& IFF_UP
) != 0)
179 iter
->current
.flags
|= INTERFACE_F_UP
;
181 if ((ifam
->ifam_flags
& IFF_POINTOPOINT
) != 0)
182 iter
->current
.flags
|= INTERFACE_F_POINTTOPOINT
;
184 if ((ifam
->ifam_flags
& IFF_LOOPBACK
) != 0)
185 iter
->current
.flags
|= INTERFACE_F_LOOPBACK
;
187 if ((ifam
->ifam_flags
& IFF_BROADCAST
) != 0) {
188 iter
->current
.flags
|= INTERFACE_F_BROADCAST
;
191 if ((ifam
->ifam_flags
& IFF_MULTICAST
) != 0) {
192 iter
->current
.flags
|= INTERFACE_F_MULTICAST
;
197 * This is not an interface address.
198 * Force another iteration.
200 return (ISC_R_IGNORE
);
201 } else if (ifam
->ifam_type
== RTM_NEWADDR
) {
204 struct sockaddr
*mask_sa
= NULL
;
205 struct sockaddr
*addr_sa
= NULL
;
206 struct sockaddr
*dst_sa
= NULL
;
208 struct sockaddr
*sa
= (struct sockaddr
*)(ifam
+ 1);
209 family
= sa
->sa_family
;
211 for (i
= 0; i
< RTAX_MAX
; i
++)
213 if ((ifam
->ifam_addrs
& (1 << i
)) == 0)
216 INSIST(sa
< (struct sockaddr
*) ifam_end
);
219 case RTAX_NETMASK
: /* Netmask */
222 case RTAX_IFA
: /* Interface address */
225 case RTAX_BRD
: /* Broadcast or destination address */
229 #ifdef ISC_PLATFORM_HAVESALEN
230 sa
= (struct sockaddr
*)((char*)(sa
)
231 + ROUNDUP(sa
->sa_len
));
235 * Do as the contributed SGI code does.
237 sa
= (struct sockaddr
*)((char*)(sa
)
238 + ROUNDUP(_FAKE_SA_LEN_DST(sa
)));
241 sa
= (struct sockaddr
*)((char*)(sa
)
242 + ROUNDUP(sizeof(struct sockaddr
)));
248 return (ISC_R_IGNORE
);
250 family
= addr_sa
->sa_family
;
251 if (family
!= AF_INET
&& family
!= AF_INET6
)
252 return (ISC_R_IGNORE
);
254 iter
->current
.af
= family
;
256 get_addr(family
, &iter
->current
.address
, addr_sa
,
260 * If the interface does not have a address ignore it.
264 if (iter
->current
.address
.type
.in
.s_addr
== htonl(INADDR_ANY
))
265 return (ISC_R_IGNORE
);
268 if (memcmp(&iter
->current
.address
.type
.in6
, &in6addr_any
,
269 sizeof(in6addr_any
)) == 0)
270 return (ISC_R_IGNORE
);
275 get_addr(family
, &iter
->current
.netmask
, mask_sa
,
278 if (dst_sa
!= NULL
&&
279 (iter
->current
.flags
& INTERFACE_F_POINTTOPOINT
) != 0)
280 get_addr(family
, &iter
->current
.dstaddress
, dst_sa
,
283 if (dst_sa
!= NULL
&&
284 (iter
->current
.flags
& INTERFACE_F_BROADCAST
) != 0)
285 get_addr(family
, &iter
->current
.broadcast
, dst_sa
,
289 return (ISC_R_SUCCESS
);
291 printf(isc_msgcat_get(isc_msgcat
, ISC_MSGSET_IFITERSYSCTL
,
292 ISC_MSG_UNEXPECTEDTYPE
,
293 "warning: unexpected interface list "
295 return (ISC_R_IGNORE
);
300 * Step the iterator to the next interface. Unlike
301 * isc_interfaceiter_next(), this may leave the iterator
302 * positioned on an interface that will ultimately
303 * be ignored. Return ISC_R_NOMORE if there are no more
304 * interfaces, otherwise ISC_R_SUCCESS.
307 internal_next(isc_interfaceiter_t
*iter
) {
308 struct ifa_msghdr
*ifam
;
309 REQUIRE (iter
->pos
< (unsigned int) iter
->bufused
);
311 ifam
= (struct ifa_msghdr
*) ((char *) iter
->buf
+ iter
->pos
);
313 iter
->pos
+= ifam
->ifam_msglen
;
315 if (iter
->pos
>= iter
->bufused
)
316 return (ISC_R_NOMORE
);
318 return (ISC_R_SUCCESS
);
322 internal_destroy(isc_interfaceiter_t
*iter
) {
323 UNUSED(iter
); /* Unused. */
330 void internal_first(isc_interfaceiter_t
*iter
) {