4 * Copyright (C) 2004, 2005, 2007 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: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp */
24 * Obtain the list of network interfaces using sysctl.
25 * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
32 #include <net/route.h>
33 #include <net/if_dl.h>
35 /* XXX what about Alpha? */
37 #define ROUNDUP(a) ((a) > 0 ? \
38 (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
41 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
45 #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S')
46 #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
48 struct isc_interfaceiter
{
49 unsigned int magic
; /* Magic number. */
51 void *buf
; /* Buffer for sysctl data. */
52 unsigned int bufsize
; /* Bytes allocated. */
53 unsigned int bufused
; /* Bytes used. */
54 unsigned int pos
; /* Current offset in
56 isc_interface_t current
; /* Current interface data. */
57 isc_result_t result
; /* Last result code. */
64 0, /* Any address family. */
70 isc_interfaceiter_create(isc_mem_t
*mctx
, isc_interfaceiter_t
**iterp
) {
71 isc_interfaceiter_t
*iter
;
75 char strbuf
[ISC_STRERRORSIZE
];
77 REQUIRE(mctx
!= NULL
);
78 REQUIRE(iterp
!= NULL
);
79 REQUIRE(*iterp
== NULL
);
81 iter
= isc_mem_get(mctx
, sizeof(*iter
));
83 return (ISC_R_NOMEMORY
);
89 * Determine the amount of memory needed.
92 if (sysctl(mib
, 6, NULL
, &bufsize
, NULL
, (size_t) 0) < 0) {
93 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
94 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
95 isc_msgcat_get(isc_msgcat
,
96 ISC_MSGSET_IFITERSYSCTL
,
97 ISC_MSG_GETIFLISTSIZE
,
99 "list size: sysctl: %s"),
101 result
= ISC_R_UNEXPECTED
;
104 iter
->bufsize
= bufsize
;
106 iter
->buf
= isc_mem_get(iter
->mctx
, iter
->bufsize
);
107 if (iter
->buf
== NULL
) {
108 result
= ISC_R_NOMEMORY
;
113 if (sysctl(mib
, 6, iter
->buf
, &bufused
, NULL
, (size_t) 0) < 0) {
114 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
115 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
116 isc_msgcat_get(isc_msgcat
,
117 ISC_MSGSET_IFITERSYSCTL
,
119 "getting interface list: "
122 result
= ISC_R_UNEXPECTED
;
125 iter
->bufused
= bufused
;
126 INSIST(iter
->bufused
<= iter
->bufsize
);
129 * A newly created iterator has an undefined position
130 * until isc_interfaceiter_first() is called.
132 iter
->pos
= (unsigned int) -1;
133 iter
->result
= ISC_R_FAILURE
;
135 iter
->magic
= IFITER_MAGIC
;
137 return (ISC_R_SUCCESS
);
140 if (iter
->buf
!= NULL
)
141 isc_mem_put(mctx
, iter
->buf
, iter
->bufsize
);
142 isc_mem_put(mctx
, iter
, sizeof(*iter
));
147 * Get information about the current interface to iter->current.
148 * If successful, return ISC_R_SUCCESS.
149 * If the interface has an unsupported address family,
150 * return ISC_R_IGNORE. In case of other failure,
151 * return ISC_R_UNEXPECTED.
155 internal_current(isc_interfaceiter_t
*iter
) {
156 struct ifa_msghdr
*ifam
, *ifam_end
;
158 REQUIRE(VALID_IFITER(iter
));
159 REQUIRE (iter
->pos
< (unsigned int) iter
->bufused
);
161 ifam
= (struct ifa_msghdr
*) ((char *) iter
->buf
+ iter
->pos
);
162 ifam_end
= (struct ifa_msghdr
*) ((char *) iter
->buf
+ iter
->bufused
);
164 if (ifam
->ifam_type
== RTM_IFINFO
) {
165 struct if_msghdr
*ifm
= (struct if_msghdr
*) ifam
;
166 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) (ifm
+ 1);
167 unsigned int namelen
;
169 memset(&iter
->current
, 0, sizeof(iter
->current
));
171 namelen
= sdl
->sdl_nlen
;
172 if (namelen
> sizeof(iter
->current
.name
) - 1)
173 namelen
= sizeof(iter
->current
.name
) - 1;
175 memset(iter
->current
.name
, 0, sizeof(iter
->current
.name
));
176 memcpy(iter
->current
.name
, sdl
->sdl_data
, namelen
);
178 iter
->current
.flags
= 0;
180 if ((ifam
->ifam_flags
& IFF_UP
) != 0)
181 iter
->current
.flags
|= INTERFACE_F_UP
;
183 if ((ifam
->ifam_flags
& IFF_POINTOPOINT
) != 0)
184 iter
->current
.flags
|= INTERFACE_F_POINTTOPOINT
;
186 if ((ifam
->ifam_flags
& IFF_LOOPBACK
) != 0)
187 iter
->current
.flags
|= INTERFACE_F_LOOPBACK
;
189 if ((ifam
->ifam_flags
& IFF_BROADCAST
) != 0)
190 iter
->current
.flags
|= INTERFACE_F_BROADCAST
;
193 if ((ifam
->ifam_flags
& IFF_MULTICAST
) != 0)
194 iter
->current
.flags
|= INTERFACE_F_MULTICAST
;
198 * This is not an interface address.
199 * Force another iteration.
201 return (ISC_R_IGNORE
);
202 } else if (ifam
->ifam_type
== RTM_NEWADDR
) {
205 struct sockaddr
*mask_sa
= NULL
;
206 struct sockaddr
*addr_sa
= NULL
;
207 struct sockaddr
*dst_sa
= NULL
;
209 struct sockaddr
*sa
= (struct sockaddr
*)(ifam
+ 1);
210 family
= sa
->sa_family
;
212 for (i
= 0; i
< RTAX_MAX
; i
++)
214 if ((ifam
->ifam_addrs
& (1 << i
)) == 0)
217 INSIST(sa
< (struct sockaddr
*) ifam_end
);
220 case RTAX_NETMASK
: /* Netmask */
223 case RTAX_IFA
: /* Interface address */
226 case RTAX_BRD
: /* Broadcast or destination address */
230 #ifdef ISC_PLATFORM_HAVESALEN
231 sa
= (struct sockaddr
*)((char*)(sa
)
232 + ROUNDUP(sa
->sa_len
));
236 * Do as the contributed SGI code does.
238 sa
= (struct sockaddr
*)((char*)(sa
)
239 + ROUNDUP(_FAKE_SA_LEN_DST(sa
)));
242 sa
= (struct sockaddr
*)((char*)(sa
)
243 + ROUNDUP(sizeof(struct sockaddr
)));
249 return (ISC_R_IGNORE
);
251 family
= addr_sa
->sa_family
;
252 if (family
!= AF_INET
&& family
!= AF_INET6
)
253 return (ISC_R_IGNORE
);
255 iter
->current
.af
= family
;
257 get_addr(family
, &iter
->current
.address
, addr_sa
,
261 get_addr(family
, &iter
->current
.netmask
, mask_sa
,
264 if (dst_sa
!= NULL
&&
265 (iter
->current
.flags
& INTERFACE_F_POINTTOPOINT
) != 0)
266 get_addr(family
, &iter
->current
.dstaddress
, dst_sa
,
269 if (dst_sa
!= NULL
&&
270 (iter
->current
.flags
& INTERFACE_F_BROADCAST
) != 0)
271 get_addr(family
, &iter
->current
.broadcast
, dst_sa
,
274 return (ISC_R_SUCCESS
);
276 printf(isc_msgcat_get(isc_msgcat
, ISC_MSGSET_IFITERSYSCTL
,
277 ISC_MSG_UNEXPECTEDTYPE
,
278 "warning: unexpected interface list "
280 return (ISC_R_IGNORE
);
285 * Step the iterator to the next interface. Unlike
286 * isc_interfaceiter_next(), this may leave the iterator
287 * positioned on an interface that will ultimately
288 * be ignored. Return ISC_R_NOMORE if there are no more
289 * interfaces, otherwise ISC_R_SUCCESS.
292 internal_next(isc_interfaceiter_t
*iter
) {
293 struct ifa_msghdr
*ifam
;
294 REQUIRE (iter
->pos
< (unsigned int) iter
->bufused
);
296 ifam
= (struct ifa_msghdr
*) ((char *) iter
->buf
+ iter
->pos
);
298 iter
->pos
+= ifam
->ifam_msglen
;
300 if (iter
->pos
>= iter
->bufused
)
301 return (ISC_R_NOMORE
);
303 return (ISC_R_SUCCESS
);
307 internal_destroy(isc_interfaceiter_t
*iter
) {
308 UNUSED(iter
); /* Unused. */
315 void internal_first(isc_interfaceiter_t
*iter
) {