2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 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.2.1 2004/03/09 06:12:10 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 REQUIRE(mctx != NULL);
73 REQUIRE(iterp != NULL);
74 REQUIRE(*iterp == NULL);
76 iter = isc_mem_get(mctx, sizeof(*iter));
78 return (ISC_R_NOMEMORY);
84 * Determine the amount of memory needed.
87 if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
88 UNEXPECTED_ERROR(__FILE__, __LINE__,
89 isc_msgcat_get(isc_msgcat,
90 ISC_MSGSET_IFITERSYSCTL,
91 ISC_MSG_GETIFLISTSIZE,
93 "list size: sysctl: %s"),
95 result = ISC_R_UNEXPECTED;
98 iter->bufsize = bufsize;
100 iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
101 if (iter->buf == NULL) {
102 result = ISC_R_NOMEMORY;
107 if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
108 UNEXPECTED_ERROR(__FILE__, __LINE__,
109 isc_msgcat_get(isc_msgcat,
110 ISC_MSGSET_IFITERSYSCTL,
112 "getting interface list: "
115 result = ISC_R_UNEXPECTED;
118 iter->bufused = bufused;
119 INSIST(iter->bufused <= iter->bufsize);
122 * A newly created iterator has an undefined position
123 * until isc_interfaceiter_first() is called.
125 iter->pos = (unsigned int) -1;
126 iter->result = ISC_R_FAILURE;
128 iter->magic = IFITER_MAGIC;
130 return (ISC_R_SUCCESS);
133 if (iter->buf != NULL)
134 isc_mem_put(mctx, iter->buf, iter->bufsize);
135 isc_mem_put(mctx, iter, sizeof *iter);
140 * Get information about the current interface to iter->current.
141 * If successful, return ISC_R_SUCCESS.
142 * If the interface has an unsupported address family,
143 * return ISC_R_IGNORE. In case of other failure,
144 * return ISC_R_UNEXPECTED.
148 internal_current(isc_interfaceiter_t *iter) {
149 struct ifa_msghdr *ifam, *ifam_end;
151 REQUIRE(VALID_IFITER(iter));
152 REQUIRE (iter->pos < (unsigned int) iter->bufused);
154 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
155 ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
157 if (ifam->ifam_type == RTM_IFINFO) {
158 struct if_msghdr *ifm = (struct if_msghdr *) ifam;
159 struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
160 unsigned int namelen;
162 memset(&iter->current, 0, sizeof(iter->current));
164 namelen = sdl->sdl_nlen;
165 if (namelen > sizeof(iter->current.name) - 1)
166 namelen = sizeof(iter->current.name) - 1;
168 memset(iter->current.name, 0, sizeof(iter->current.name));
169 memcpy(iter->current.name, sdl->sdl_data, namelen);
171 iter->current.flags = 0;
173 if ((ifam->ifam_flags & IFF_UP) != 0)
174 iter->current.flags |= INTERFACE_F_UP;
176 if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
177 iter->current.flags |= INTERFACE_F_POINTTOPOINT;
179 if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
180 iter->current.flags |= INTERFACE_F_LOOPBACK;
183 * This is not an interface address.
184 * Force another iteration.
186 return (ISC_R_IGNORE);
187 } else if (ifam->ifam_type == RTM_NEWADDR) {
190 struct sockaddr *mask_sa = NULL;
191 struct sockaddr *addr_sa = NULL;
192 struct sockaddr *dst_sa = NULL;
194 struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
195 family = sa->sa_family;
197 for (i = 0; i < RTAX_MAX; i++)
199 if ((ifam->ifam_addrs & (1 << i)) == 0)
202 INSIST(sa < (struct sockaddr *) ifam_end);
205 case RTAX_NETMASK: /* Netmask */
208 case RTAX_IFA: /* Interface address */
211 case RTAX_BRD: /* Broadcast or destination address */
215 #ifdef ISC_PLATFORM_HAVESALEN
216 sa = (struct sockaddr *)((char*)(sa)
217 + ROUNDUP(sa->sa_len));
221 * Do as the contributed SGI code does.
223 sa = (struct sockaddr *)((char*)(sa)
224 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
227 sa = (struct sockaddr *)((char*)(sa)
228 + ROUNDUP(sizeof(struct sockaddr)));
234 return (ISC_R_IGNORE);
236 family = addr_sa->sa_family;
237 if (family != AF_INET) /* XXX IP6 */
238 return (ISC_R_IGNORE);
240 iter->current.af = family;
242 get_addr(family, &iter->current.address, addr_sa);
245 get_addr(family, &iter->current.netmask, mask_sa);
247 if (dst_sa != NULL &&
248 (iter->current.flags & IFF_POINTOPOINT) != 0)
249 get_addr(family, &iter->current.dstaddress, dst_sa);
251 return (ISC_R_SUCCESS);
253 printf(isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL,
254 ISC_MSG_UNEXPECTEDTYPE,
255 "warning: unexpected interface list "
257 return (ISC_R_IGNORE);
262 * Step the iterator to the next interface. Unlike
263 * isc_interfaceiter_next(), this may leave the iterator
264 * positioned on an interface that will ultimately
265 * be ignored. Return ISC_R_NOMORE if there are no more
266 * interfaces, otherwise ISC_R_SUCCESS.
269 internal_next(isc_interfaceiter_t *iter) {
270 struct ifa_msghdr *ifam;
271 REQUIRE (iter->pos < (unsigned int) iter->bufused);
273 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
275 iter->pos += ifam->ifam_msglen;
277 if (iter->pos >= iter->bufused)
278 return (ISC_R_NOMORE);
280 return (ISC_R_SUCCESS);
284 internal_destroy(isc_interfaceiter_t *iter) {
285 UNUSED(iter); /* Unused. */