55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*
6fb9b25791778f69002eb72be6235e20d98ec452Tinderbox User * Copyright (C) 2003-2005, 2007-2009, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*! \file
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * \brief
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * Obtain the list of network interfaces using the getifaddrs(3) library.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#include <ifaddrs.h>
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*% Iterator Magic */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G')
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*% Valid Iterator */
dcf426e9b546d4bcc0681904551752af43c1bcd6Evan Hunt#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
f44202ab640d22e17b4c74bdad7817622918bd27Mark Andrews
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Huntstatic isc_boolean_t seenv6 = ISC_FALSE;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*% Iterator structure */
55e5c51e661e23e24573db84114a3837817745c9Evan Huntstruct isc_interfaceiter {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt unsigned int magic; /*%< Magic number. */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_mem_t *mctx;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt void *buf; /*%< (unused) */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt unsigned int bufsize; /*%< (always 0) */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt struct ifaddrs *ifaddrs; /*%< List of ifaddrs */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt struct ifaddrs *pos; /*%< Ptr to current ifaddr */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_interface_t current; /*%< Current interface data. */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_result_t result; /*%< Last result code. */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt FILE * proc;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt char entry[ISC_IF_INET6_SZ];
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_result_t valid;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt};
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Huntisc_result_t
55e5c51e661e23e24573db84114a3837817745c9Evan Huntisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_interfaceiter_t *iter;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_result_t result;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt char strbuf[ISC_STRERRORSIZE];
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt REQUIRE(mctx != NULL);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt REQUIRE(iterp != NULL);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt REQUIRE(*iterp == NULL);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter = isc_mem_get(mctx, sizeof(*iter));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter == NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (ISC_R_NOMEMORY);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->mctx = mctx;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->buf = NULL;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->bufsize = 0;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->ifaddrs = NULL;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt /*
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * Only open "/proc/net/if_inet6" if we have never seen a IPv6
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * address returned by getifaddrs().
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (!seenv6)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->proc = fopen("/proc/net/if_inet6", "r");
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt else
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->proc = NULL;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->valid = ISC_R_FAILURE;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (getifaddrs(&iter->ifaddrs) < 0) {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_msgcat_get(isc_msgcat,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt ISC_MSGSET_IFITERGETIFADDRS,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt ISC_MSG_GETIFADDRS,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt "getting interface "
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt "addresses: getifaddrs: %s"),
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt strbuf);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt result = ISC_R_UNEXPECTED;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt goto failure;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt }
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt /*
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * A newly created iterator has an undefined position
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * until isc_interfaceiter_first() is called.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->pos = NULL;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->result = ISC_R_FAILURE;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->magic = IFITER_MAGIC;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt *iterp = iter;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (ISC_R_SUCCESS);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt failure:
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter->proc != NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt fclose(iter->proc);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter->ifaddrs != NULL) /* just in case */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt freeifaddrs(iter->ifaddrs);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt isc_mem_put(mctx, iter, sizeof(*iter));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (result);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt}
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * Get information about the current interface to iter->current.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * If successful, return ISC_R_SUCCESS.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * If the interface has an unsupported address family,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * return ISC_R_IGNORE.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt */
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Huntstatic isc_result_t
55e5c51e661e23e24573db84114a3837817745c9Evan Huntinternal_current(isc_interfaceiter_t *iter) {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt struct ifaddrs *ifa;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt int family;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt unsigned int namelen;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt REQUIRE(VALID_IFITER(iter));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt ifa = iter->pos;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter->pos == NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (linux_if_inet6_current(iter));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt INSIST(ifa != NULL);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt INSIST(ifa->ifa_name != NULL);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (ifa->ifa_addr == NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (ISC_R_IGNORE);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt family = ifa->ifa_addr->sa_family;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (family != AF_INET && family != AF_INET6)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (ISC_R_IGNORE);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (family == AF_INET6)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt seenv6 = ISC_TRUE;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt memset(&iter->current, 0, sizeof(iter->current));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt namelen = strlen(ifa->ifa_name);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (namelen > sizeof(iter->current.name) - 1)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt namelen = sizeof(iter->current.name) - 1;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt memset(iter->current.name, 0, sizeof(iter->current.name));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt memmove(iter->current.name, ifa->ifa_name, namelen);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->current.flags = 0;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if ((ifa->ifa_flags & IFF_UP) != 0)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->current.flags |= INTERFACE_F_UP;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->current.flags |= INTERFACE_F_POINTTOPOINT;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
a6d43d18b1f6164fd144b2fa25ea57f5566b3bf9Evan Hunt if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
a6d43d18b1f6164fd144b2fa25ea57f5566b3bf9Evan Hunt iter->current.flags |= INTERFACE_F_LOOPBACK;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->current.af = family;
a6d43d18b1f6164fd144b2fa25ea57f5566b3bf9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (ifa->ifa_netmask != NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt get_addr(family, &iter->current.netmask, ifa->ifa_netmask,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt ifa->ifa_name);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (ifa->ifa_dstaddr != NULL &&
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
609b8d08176469485edce25f3c2f50365bbd3819Mark Andrews get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr,
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt ifa->ifa_name);
609b8d08176469485edce25f3c2f50365bbd3819Mark Andrews
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (ISC_R_SUCCESS);
609b8d08176469485edce25f3c2f50365bbd3819Mark Andrews}
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt/*
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * Step the iterator to the next interface. Unlike
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * isc_interfaceiter_next(), this may leave the iterator
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * positioned on an interface that will ultimately
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * be ignored. Return ISC_R_NOMORE if there are no more
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * interfaces, otherwise ISC_R_SUCCESS.
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt */
55e5c51e661e23e24573db84114a3837817745c9Evan Huntstatic isc_result_t
55e5c51e661e23e24573db84114a3837817745c9Evan Huntinternal_next(isc_interfaceiter_t *iter) {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
bfde61d5194a534d800f3b90008d1f52261922c5Mark Andrews if (iter->pos != NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->pos = iter->pos->ifa_next;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter->pos == NULL) {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (!seenv6)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt return (linux_if_inet6_next(iter));
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
bfde61d5194a534d800f3b90008d1f52261922c5Mark Andrews return (ISC_R_NOMORE);
5cedb1e345e3dae492423a73f0bc9b117608f704Evan Hunt }
5cedb1e345e3dae492423a73f0bc9b117608f704Evan Hunt
bfde61d5194a534d800f3b90008d1f52261922c5Mark Andrews return (ISC_R_SUCCESS);
bfde61d5194a534d800f3b90008d1f52261922c5Mark Andrews}
5cedb1e345e3dae492423a73f0bc9b117608f704Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Huntstatic void
55e5c51e661e23e24573db84114a3837817745c9Evan Huntinternal_destroy(isc_interfaceiter_t *iter) {
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter->proc != NULL)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt fclose(iter->proc);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->proc = NULL;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt if (iter->ifaddrs)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt freeifaddrs(iter->ifaddrs);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt iter->ifaddrs = NULL;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt}
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt
55e5c51e661e23e24573db84114a3837817745c9Evan Huntstatic
55e5c51e661e23e24573db84114a3837817745c9Evan Huntvoid internal_first(isc_interfaceiter_t *iter) {
bfde61d5194a534d800f3b90008d1f52261922c5Mark Andrews
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#ifdef __linux
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt linux_if_inet6_first(iter);
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt#endif
bfde61d5194a534d800f3b90008d1f52261922c5Mark Andrews iter->pos = iter->ifaddrs;
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt}
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt