2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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: ifiter_sysctl.c,v 1.14.12.7 2004/03/08 09:04:56 marka Exp $ */
21 * Obtain the list of network interfaces using sysctl.
22 * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
26 #include <sys/param.h>
27 #include <sys/sysctl.h>
29 #include <net/route.h>
30 #include <net/if_dl.h>
32 /* XXX what about Alpha? */
34 #define ROUNDUP(a) ((a) > 0 ? \
35 (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
38 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
42 #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S')
43 #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
45 struct isc_interfaceiter {
46 unsigned int magic; /* Magic number. */
48 void *buf; /* Buffer for sysctl data. */
49 unsigned int bufsize; /* Bytes allocated. */
50 unsigned int bufused; /* Bytes used. */
51 unsigned int pos; /* Current offset in
53 isc_interface_t current; /* Current interface data. */
54 isc_result_t result; /* Last result code. */
61 0, /* Any address family. */
67 isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
68 isc_interfaceiter_t *iter;
72 char strbuf[ISC_STRERRORSIZE];
74 REQUIRE(iterp != NULL);
75 REQUIRE(*iterp == NULL);
77 iter = isc_mem_get(mctx, sizeof(*iter));
79 return (ISC_R_NOMEMORY);
85 * Determine the amount of memory needed.
88 if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
89 isc__strerror(errno, strbuf, sizeof(strbuf));
90 UNEXPECTED_ERROR(__FILE__, __LINE__,
91 isc_msgcat_get(isc_msgcat,
92 ISC_MSGSET_IFITERSYSCTL,
93 ISC_MSG_GETIFLISTSIZE,
95 "list size: sysctl: %s"),
97 result = ISC_R_UNEXPECTED;
100 iter->bufsize = bufsize;
102 iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
103 if (iter->buf == NULL) {
104 result = ISC_R_NOMEMORY;
109 if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
110 isc__strerror(errno, strbuf, sizeof(strbuf));
111 UNEXPECTED_ERROR(__FILE__, __LINE__,
112 isc_msgcat_get(isc_msgcat,
113 ISC_MSGSET_IFITERSYSCTL,
115 "getting interface list: "
118 result = ISC_R_UNEXPECTED;
121 iter->bufused = bufused;
122 INSIST(iter->bufused <= iter->bufsize);
125 * A newly created iterator has an undefined position
126 * until isc_interfaceiter_first() is called.
128 iter->pos = (unsigned int) -1;
129 iter->result = ISC_R_FAILURE;
131 iter->magic = IFITER_MAGIC;
133 return (ISC_R_SUCCESS);
136 if (iter->buf != NULL)
137 isc_mem_put(mctx, iter->buf, iter->bufsize);
138 isc_mem_put(mctx, iter, sizeof(*iter));
143 * Get information about the current interface to iter->current.
144 * If successful, return ISC_R_SUCCESS.
145 * If the interface has an unsupported address family,
146 * return ISC_R_IGNORE. In case of other failure,
147 * return ISC_R_UNEXPECTED.
151 internal_current(isc_interfaceiter_t *iter) {
152 struct ifa_msghdr *ifam, *ifam_end;
154 REQUIRE(VALID_IFITER(iter));
155 REQUIRE (iter->pos < (unsigned int) iter->bufused);
157 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
158 ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
160 if (ifam->ifam_type == RTM_IFINFO) {
161 struct if_msghdr *ifm = (struct if_msghdr *) ifam;
162 struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
163 unsigned int namelen;
165 memset(&iter->current, 0, sizeof(iter->current));
167 namelen = sdl->sdl_nlen;
168 if (namelen > sizeof(iter->current.name) - 1)
169 namelen = sizeof(iter->current.name) - 1;
171 memset(iter->current.name, 0, sizeof(iter->current.name));
172 memcpy(iter->current.name, sdl->sdl_data, namelen);
174 iter->current.flags = 0;
176 if ((ifam->ifam_flags & IFF_UP) != 0)
177 iter->current.flags |= INTERFACE_F_UP;
179 if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
180 iter->current.flags |= INTERFACE_F_POINTTOPOINT;
182 if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
183 iter->current.flags |= INTERFACE_F_LOOPBACK;
185 if ((ifam->ifam_flags & IFF_BROADCAST) != 0) {
186 iter->current.flags |= INTERFACE_F_BROADCAST;
189 if ((ifam->ifam_flags & IFF_MULTICAST) != 0) {
190 iter->current.flags |= INTERFACE_F_MULTICAST;
195 * This is not an interface address.
196 * Force another iteration.
198 return (ISC_R_IGNORE);
199 } else if (ifam->ifam_type == RTM_NEWADDR) {
202 struct sockaddr *mask_sa = NULL;
203 struct sockaddr *addr_sa = NULL;
204 struct sockaddr *dst_sa = NULL;
206 struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
207 family = sa->sa_family;
209 for (i = 0; i < RTAX_MAX; i++)
211 if ((ifam->ifam_addrs & (1 << i)) == 0)
214 INSIST(sa < (struct sockaddr *) ifam_end);
217 case RTAX_NETMASK: /* Netmask */
220 case RTAX_IFA: /* Interface address */
223 case RTAX_BRD: /* Broadcast or destination address */
227 #ifdef ISC_PLATFORM_HAVESALEN
228 sa = (struct sockaddr *)((char*)(sa)
229 + ROUNDUP(sa->sa_len));
233 * Do as the contributed SGI code does.
235 sa = (struct sockaddr *)((char*)(sa)
236 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
239 sa = (struct sockaddr *)((char*)(sa)
240 + ROUNDUP(sizeof(struct sockaddr)));
246 return (ISC_R_IGNORE);
248 family = addr_sa->sa_family;
249 if (family != AF_INET && family != AF_INET6)
250 return (ISC_R_IGNORE);
252 iter->current.af = family;
254 get_addr(family, &iter->current.address, addr_sa,
258 get_addr(family, &iter->current.netmask, mask_sa,
261 if (dst_sa != NULL &&
262 (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
263 get_addr(family, &iter->current.dstaddress, dst_sa,
266 if (dst_sa != NULL &&
267 (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
268 get_addr(family, &iter->current.broadcast, dst_sa,
272 return (ISC_R_SUCCESS);
274 printf(isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL,
275 ISC_MSG_UNEXPECTEDTYPE,
276 "warning: unexpected interface list "
278 return (ISC_R_IGNORE);
283 * Step the iterator to the next interface. Unlike
284 * isc_interfaceiter_next(), this may leave the iterator
285 * positioned on an interface that will ultimately
286 * be ignored. Return ISC_R_NOMORE if there are no more
287 * interfaces, otherwise ISC_R_SUCCESS.
290 internal_next(isc_interfaceiter_t *iter) {
291 struct ifa_msghdr *ifam;
292 REQUIRE (iter->pos < (unsigned int) iter->bufused);
294 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
296 iter->pos += ifam->ifam_msglen;
298 if (iter->pos >= iter->bufused)
299 return (ISC_R_NOMORE);
301 return (ISC_R_SUCCESS);
305 internal_destroy(isc_interfaceiter_t *iter) {
306 UNUSED(iter); /* Unused. */
313 void internal_first(isc_interfaceiter_t *iter) {