1290001Sglebius/* 2290001Sglebius * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 3290001Sglebius * Copyright (C) 1999-2003 Internet Software Consortium. 4290001Sglebius * 5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any 6290001Sglebius * purpose with or without fee is hereby granted, provided that the above 7290001Sglebius * copyright notice and this permission notice appear in all copies. 8290001Sglebius * 9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11290001Sglebius * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15290001Sglebius * PERFORMANCE OF THIS SOFTWARE. 16290001Sglebius */ 17290001Sglebius 18290001Sglebius/* $Id: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp $ */ 19290001Sglebius 20290001Sglebius/*! \file 21290001Sglebius * \brief 22290001Sglebius * Obtain the list of network interfaces using sysctl. 23290001Sglebius * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14, 24290001Sglebius * and 19.16. 25290001Sglebius */ 26290001Sglebius 27290001Sglebius#include <sys/param.h> 28290001Sglebius#include <sys/sysctl.h> 29290001Sglebius 30290001Sglebius#include <net/route.h> 31290001Sglebius#include <net/if_dl.h> 32290001Sglebius 33290001Sglebius/* XXX what about Alpha? */ 34290001Sglebius#ifdef sgi 35290001Sglebius#define ROUNDUP(a) ((a) > 0 ? \ 36290001Sglebius (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \ 37290001Sglebius sizeof(__uint64_t)) 38290001Sglebius#else 39290001Sglebius#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \ 40290001Sglebius : sizeof(long)) 41290001Sglebius#endif 42290001Sglebius 43290001Sglebius#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S') 44290001Sglebius#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC) 45290001Sglebius 46290001Sglebiusstruct isc_interfaceiter { 47290001Sglebius unsigned int magic; /* Magic number. */ 48290001Sglebius isc_mem_t *mctx; 49290001Sglebius void *buf; /* Buffer for sysctl data. */ 50290001Sglebius unsigned int bufsize; /* Bytes allocated. */ 51290001Sglebius unsigned int bufused; /* Bytes used. */ 52290001Sglebius unsigned int pos; /* Current offset in 53290001Sglebius sysctl data. */ 54290001Sglebius isc_interface_t current; /* Current interface data. */ 55290001Sglebius isc_result_t result; /* Last result code. */ 56290001Sglebius}; 57290001Sglebius 58290001Sglebiusstatic int mib[6] = { 59290001Sglebius CTL_NET, 60290001Sglebius PF_ROUTE, 61290001Sglebius 0, 62290001Sglebius 0, /* Any address family. */ 63290001Sglebius NET_RT_IFLIST, 64290001Sglebius 0 /* Flags. */ 65290001Sglebius}; 66290001Sglebius 67290001Sglebiusisc_result_t 68290001Sglebiusisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { 69290001Sglebius isc_interfaceiter_t *iter; 70290001Sglebius isc_result_t result; 71290001Sglebius size_t bufsize; 72290001Sglebius size_t bufused; 73290001Sglebius char strbuf[ISC_STRERRORSIZE]; 74290001Sglebius 75290001Sglebius REQUIRE(mctx != NULL); 76290001Sglebius REQUIRE(iterp != NULL); 77290001Sglebius REQUIRE(*iterp == NULL); 78290001Sglebius 79290001Sglebius iter = isc_mem_get(mctx, sizeof(*iter)); 80290001Sglebius if (iter == NULL) 81290001Sglebius return (ISC_R_NOMEMORY); 82290001Sglebius 83290001Sglebius iter->mctx = mctx; 84290001Sglebius iter->buf = 0; 85290001Sglebius 86290001Sglebius /* 87290001Sglebius * Determine the amount of memory needed. 88290001Sglebius */ 89290001Sglebius bufsize = 0; 90290001Sglebius if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) { 91290001Sglebius isc__strerror(errno, strbuf, sizeof(strbuf)); 92290001Sglebius UNEXPECTED_ERROR(__FILE__, __LINE__, 93290001Sglebius isc_msgcat_get(isc_msgcat, 94290001Sglebius ISC_MSGSET_IFITERSYSCTL, 95290001Sglebius ISC_MSG_GETIFLISTSIZE, 96290001Sglebius "getting interface " 97290001Sglebius "list size: sysctl: %s"), 98290001Sglebius strbuf); 99290001Sglebius result = ISC_R_UNEXPECTED; 100290001Sglebius goto failure; 101290001Sglebius } 102290001Sglebius iter->bufsize = bufsize; 103290001Sglebius 104290001Sglebius iter->buf = isc_mem_get(iter->mctx, iter->bufsize); 105290001Sglebius if (iter->buf == NULL) { 106290001Sglebius result = ISC_R_NOMEMORY; 107290001Sglebius goto failure; 108290001Sglebius } 109290001Sglebius 110290001Sglebius bufused = bufsize; 111290001Sglebius if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) { 112290001Sglebius isc__strerror(errno, strbuf, sizeof(strbuf)); 113290001Sglebius UNEXPECTED_ERROR(__FILE__, __LINE__, 114290001Sglebius isc_msgcat_get(isc_msgcat, 115290001Sglebius ISC_MSGSET_IFITERSYSCTL, 116290001Sglebius ISC_MSG_GETIFLIST, 117290001Sglebius "getting interface list: " 118290001Sglebius "sysctl: %s"), 119290001Sglebius strbuf); 120290001Sglebius result = ISC_R_UNEXPECTED; 121290001Sglebius goto failure; 122290001Sglebius } 123290001Sglebius iter->bufused = bufused; 124290001Sglebius INSIST(iter->bufused <= iter->bufsize); 125290001Sglebius 126290001Sglebius /* 127290001Sglebius * A newly created iterator has an undefined position 128290001Sglebius * until isc_interfaceiter_first() is called. 129290001Sglebius */ 130290001Sglebius iter->pos = (unsigned int) -1; 131290001Sglebius iter->result = ISC_R_FAILURE; 132290001Sglebius 133290001Sglebius iter->magic = IFITER_MAGIC; 134290001Sglebius *iterp = iter; 135290001Sglebius return (ISC_R_SUCCESS); 136290001Sglebius 137290001Sglebius failure: 138290001Sglebius if (iter->buf != NULL) 139290001Sglebius isc_mem_put(mctx, iter->buf, iter->bufsize); 140290001Sglebius isc_mem_put(mctx, iter, sizeof(*iter)); 141290001Sglebius return (result); 142290001Sglebius} 143290001Sglebius 144290001Sglebius/* 145290001Sglebius * Get information about the current interface to iter->current. 146290001Sglebius * If successful, return ISC_R_SUCCESS. 147290001Sglebius * If the interface has an unsupported address family, 148290001Sglebius * return ISC_R_IGNORE. In case of other failure, 149290001Sglebius * return ISC_R_UNEXPECTED. 150290001Sglebius */ 151290001Sglebius 152290001Sglebiusstatic isc_result_t 153290001Sglebiusinternal_current(isc_interfaceiter_t *iter) { 154290001Sglebius struct ifa_msghdr *ifam, *ifam_end; 155290001Sglebius 156290001Sglebius REQUIRE(VALID_IFITER(iter)); 157290001Sglebius REQUIRE (iter->pos < (unsigned int) iter->bufused); 158290001Sglebius 159290001Sglebius ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos); 160290001Sglebius ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused); 161290001Sglebius 162290001Sglebius // Skip wrong RTM version headers 163290001Sglebius if (ifam->ifam_version != RTM_VERSION) 164290001Sglebius return (ISC_R_IGNORE); 165290001Sglebius 166290001Sglebius if (ifam->ifam_type == RTM_IFINFO) { 167290001Sglebius struct if_msghdr *ifm = (struct if_msghdr *) ifam; 168290001Sglebius struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1); 169290001Sglebius unsigned int namelen; 170290001Sglebius 171290001Sglebius memset(&iter->current, 0, sizeof(iter->current)); 172290001Sglebius 173290001Sglebius iter->current.ifindex = sdl->sdl_index; 174290001Sglebius namelen = sdl->sdl_nlen; 175290001Sglebius if (namelen > sizeof(iter->current.name) - 1) 176290001Sglebius namelen = sizeof(iter->current.name) - 1; 177290001Sglebius 178290001Sglebius memset(iter->current.name, 0, sizeof(iter->current.name)); 179290001Sglebius memcpy(iter->current.name, sdl->sdl_data, namelen); 180290001Sglebius 181290001Sglebius iter->current.flags = 0; 182290001Sglebius 183290001Sglebius if ((ifam->ifam_flags & IFF_UP) != 0) 184290001Sglebius iter->current.flags |= INTERFACE_F_UP; 185290001Sglebius 186290001Sglebius if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0) 187290001Sglebius iter->current.flags |= INTERFACE_F_POINTTOPOINT; 188290001Sglebius 189290001Sglebius if ((ifam->ifam_flags & IFF_LOOPBACK) != 0) 190290001Sglebius iter->current.flags |= INTERFACE_F_LOOPBACK; 191290001Sglebius 192290001Sglebius if ((ifam->ifam_flags & IFF_BROADCAST) != 0) 193290001Sglebius iter->current.flags |= INTERFACE_F_BROADCAST; 194290001Sglebius 195290001Sglebius#ifdef IFF_MULTICAST 196290001Sglebius if ((ifam->ifam_flags & IFF_MULTICAST) != 0) 197290001Sglebius iter->current.flags |= INTERFACE_F_MULTICAST; 198290001Sglebius#endif 199290001Sglebius 200290001Sglebius /* 201290001Sglebius * This is not an interface address. 202290001Sglebius * Force another iteration. 203290001Sglebius */ 204290001Sglebius return (ISC_R_IGNORE); 205290001Sglebius } else if (ifam->ifam_type == RTM_NEWADDR) { 206290001Sglebius int i; 207290001Sglebius int family; 208290001Sglebius struct sockaddr *mask_sa = NULL; 209290001Sglebius struct sockaddr *addr_sa = NULL; 210290001Sglebius struct sockaddr *dst_sa = NULL; 211290001Sglebius 212290001Sglebius struct sockaddr *sa = (struct sockaddr *)(ifam + 1); 213290001Sglebius family = sa->sa_family; 214290001Sglebius 215290001Sglebius for (i = 0; i < RTAX_MAX; i++) 216290001Sglebius { 217290001Sglebius if ((ifam->ifam_addrs & (1 << i)) == 0) 218290001Sglebius continue; 219290001Sglebius 220290001Sglebius INSIST(sa < (struct sockaddr *) ifam_end); 221290001Sglebius 222290001Sglebius switch (i) { 223290001Sglebius case RTAX_NETMASK: /* Netmask */ 224290001Sglebius mask_sa = sa; 225290001Sglebius break; 226290001Sglebius case RTAX_IFA: /* Interface address */ 227290001Sglebius addr_sa = sa; 228290001Sglebius break; 229290001Sglebius case RTAX_BRD: /* Broadcast or destination address */ 230290001Sglebius dst_sa = sa; 231290001Sglebius break; 232290001Sglebius } 233290001Sglebius#ifdef ISC_PLATFORM_HAVESALEN 234290001Sglebius sa = (struct sockaddr *)((char*)(sa) 235290001Sglebius + ROUNDUP(sa->sa_len)); 236290001Sglebius#else 237290001Sglebius#ifdef sgi 238290001Sglebius /* 239290001Sglebius * Do as the contributed SGI code does. 240290001Sglebius */ 241290001Sglebius sa = (struct sockaddr *)((char*)(sa) 242290001Sglebius + ROUNDUP(_FAKE_SA_LEN_DST(sa))); 243290001Sglebius#else 244290001Sglebius /* XXX untested. */ 245290001Sglebius sa = (struct sockaddr *)((char*)(sa) 246290001Sglebius + ROUNDUP(sizeof(struct sockaddr))); 247290001Sglebius#endif 248290001Sglebius#endif 249290001Sglebius } 250290001Sglebius 251290001Sglebius if (addr_sa == NULL) 252290001Sglebius return (ISC_R_IGNORE); 253290001Sglebius 254290001Sglebius family = addr_sa->sa_family; 255290001Sglebius if (family != AF_INET && family != AF_INET6) 256290001Sglebius return (ISC_R_IGNORE); 257290001Sglebius 258290001Sglebius iter->current.af = family; 259290001Sglebius 260290001Sglebius get_addr(family, &iter->current.address, addr_sa, 261290001Sglebius iter->current.name); 262290001Sglebius 263290001Sglebius if (mask_sa != NULL) 264290001Sglebius get_addr(family, &iter->current.netmask, mask_sa, 265290001Sglebius iter->current.name); 266290001Sglebius 267290001Sglebius if (dst_sa != NULL && 268290001Sglebius (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) 269290001Sglebius get_addr(family, &iter->current.dstaddress, dst_sa, 270290001Sglebius iter->current.name); 271290001Sglebius 272290001Sglebius if (dst_sa != NULL && 273290001Sglebius (iter->current.flags & INTERFACE_F_BROADCAST) != 0) 274290001Sglebius get_addr(family, &iter->current.broadcast, dst_sa, 275290001Sglebius iter->current.name); 276290001Sglebius 277290001Sglebius return (ISC_R_SUCCESS); 278290001Sglebius } else { 279290001Sglebius printf("%s", isc_msgcat_get(isc_msgcat, 280290001Sglebius ISC_MSGSET_IFITERSYSCTL, 281290001Sglebius ISC_MSG_UNEXPECTEDTYPE, 282290001Sglebius "warning: unexpected interface " 283290001Sglebius "list message type\n")); 284290001Sglebius return (ISC_R_IGNORE); 285290001Sglebius } 286290001Sglebius} 287290001Sglebius 288290001Sglebius/* 289290001Sglebius * Step the iterator to the next interface. Unlike 290290001Sglebius * isc_interfaceiter_next(), this may leave the iterator 291290001Sglebius * positioned on an interface that will ultimately 292290001Sglebius * be ignored. Return ISC_R_NOMORE if there are no more 293290001Sglebius * interfaces, otherwise ISC_R_SUCCESS. 294290001Sglebius */ 295290001Sglebiusstatic isc_result_t 296290001Sglebiusinternal_next(isc_interfaceiter_t *iter) { 297290001Sglebius struct ifa_msghdr *ifam; 298290001Sglebius REQUIRE (iter->pos < (unsigned int) iter->bufused); 299290001Sglebius 300290001Sglebius ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos); 301290001Sglebius 302290001Sglebius iter->pos += ifam->ifam_msglen; 303290001Sglebius 304290001Sglebius if (iter->pos >= iter->bufused) 305290001Sglebius return (ISC_R_NOMORE); 306290001Sglebius 307290001Sglebius return (ISC_R_SUCCESS); 308290001Sglebius} 309290001Sglebius 310290001Sglebiusstatic void 311290001Sglebiusinternal_destroy(isc_interfaceiter_t *iter) { 312290001Sglebius UNUSED(iter); /* Unused. */ 313290001Sglebius /* 314290001Sglebius * Do nothing. 315290001Sglebius */ 316290001Sglebius} 317290001Sglebius 318290001Sglebiusstatic 319290001Sglebiusvoid internal_first(isc_interfaceiter_t *iter) { 320290001Sglebius iter->pos = 0; 321290001Sglebius} 322