1258945Sroberto/* 2280849Scy * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC") 3258945Sroberto * Copyright (C) 2003 Internet Software Consortium. 4258945Sroberto * 5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any 6258945Sroberto * purpose with or without fee is hereby granted, provided that the above 7258945Sroberto * copyright notice and this permission notice appear in all copies. 8258945Sroberto * 9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11258945Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15258945Sroberto * PERFORMANCE OF THIS SOFTWARE. 16258945Sroberto */ 17258945Sroberto 18280849Scy/* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */ 19258945Sroberto 20258945Sroberto/*! \file 21258945Sroberto * \brief 22258945Sroberto * Obtain the list of network interfaces using the getifaddrs(3) library. 23258945Sroberto */ 24258945Sroberto 25258945Sroberto#include <ifaddrs.h> 26258945Sroberto 27258945Sroberto/*% Iterator Magic */ 28258945Sroberto#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G') 29258945Sroberto/*% Valid Iterator */ 30258945Sroberto#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC) 31258945Sroberto 32258945Sroberto#ifdef __linux 33258945Srobertostatic isc_boolean_t seenv6 = ISC_FALSE; 34258945Sroberto#endif 35258945Sroberto 36258945Sroberto/*% Iterator structure */ 37258945Srobertostruct isc_interfaceiter { 38258945Sroberto unsigned int magic; /*%< Magic number. */ 39258945Sroberto isc_mem_t *mctx; 40258945Sroberto void *buf; /*%< (unused) */ 41258945Sroberto unsigned int bufsize; /*%< (always 0) */ 42258945Sroberto struct ifaddrs *ifaddrs; /*%< List of ifaddrs */ 43258945Sroberto struct ifaddrs *pos; /*%< Ptr to current ifaddr */ 44258945Sroberto isc_interface_t current; /*%< Current interface data. */ 45258945Sroberto isc_result_t result; /*%< Last result code. */ 46258945Sroberto#ifdef __linux 47258945Sroberto FILE * proc; 48258945Sroberto char entry[ISC_IF_INET6_SZ]; 49258945Sroberto isc_result_t valid; 50258945Sroberto#endif 51258945Sroberto}; 52258945Sroberto 53258945Srobertoisc_result_t 54258945Srobertoisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { 55258945Sroberto isc_interfaceiter_t *iter; 56258945Sroberto isc_result_t result; 57258945Sroberto char strbuf[ISC_STRERRORSIZE]; 58280849Scy int trys, ret; 59258945Sroberto 60258945Sroberto REQUIRE(mctx != NULL); 61258945Sroberto REQUIRE(iterp != NULL); 62258945Sroberto REQUIRE(*iterp == NULL); 63258945Sroberto 64258945Sroberto iter = isc_mem_get(mctx, sizeof(*iter)); 65258945Sroberto if (iter == NULL) 66258945Sroberto return (ISC_R_NOMEMORY); 67258945Sroberto 68258945Sroberto iter->mctx = mctx; 69258945Sroberto iter->buf = NULL; 70258945Sroberto iter->bufsize = 0; 71258945Sroberto iter->ifaddrs = NULL; 72258945Sroberto#ifdef __linux 73258945Sroberto /* 74258945Sroberto * Only open "/proc/net/if_inet6" if we have never seen a IPv6 75258945Sroberto * address returned by getifaddrs(). 76258945Sroberto */ 77280849Scy if (!seenv6) { 78258945Sroberto iter->proc = fopen("/proc/net/if_inet6", "r"); 79280849Scy if (iter->proc == NULL) { 80280849Scy isc__strerror(errno, strbuf, sizeof(strbuf)); 81280849Scy isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 82280849Scy ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING, 83280849Scy "failed to open /proc/net/if_inet6"); 84280849Scy } 85280849Scy } else 86258945Sroberto iter->proc = NULL; 87258945Sroberto iter->valid = ISC_R_FAILURE; 88258945Sroberto#endif 89258945Sroberto 90280849Scy /* If interrupted, try again */ 91280849Scy for (trys = 0; trys < 3; trys++) { 92280849Scy if ((ret = getifaddrs(&iter->ifaddrs)) >= 0) 93280849Scy break; 94280849Scy if (errno != EINTR) 95280849Scy break; 96280849Scy } 97280849Scy if (ret < 0) { 98258945Sroberto isc__strerror(errno, strbuf, sizeof(strbuf)); 99258945Sroberto UNEXPECTED_ERROR(__FILE__, __LINE__, 100280849Scy "getting interface addresses: %s: %s", 101258945Sroberto isc_msgcat_get(isc_msgcat, 102258945Sroberto ISC_MSGSET_IFITERGETIFADDRS, 103258945Sroberto ISC_MSG_GETIFADDRS, 104280849Scy "getifaddrs"), 105258945Sroberto strbuf); 106258945Sroberto result = ISC_R_UNEXPECTED; 107258945Sroberto goto failure; 108258945Sroberto } 109258945Sroberto 110258945Sroberto /* 111258945Sroberto * A newly created iterator has an undefined position 112258945Sroberto * until isc_interfaceiter_first() is called. 113258945Sroberto */ 114258945Sroberto iter->pos = NULL; 115258945Sroberto iter->result = ISC_R_FAILURE; 116258945Sroberto 117258945Sroberto iter->magic = IFITER_MAGIC; 118258945Sroberto *iterp = iter; 119258945Sroberto return (ISC_R_SUCCESS); 120258945Sroberto 121258945Sroberto failure: 122258945Sroberto#ifdef __linux 123258945Sroberto if (iter->proc != NULL) 124258945Sroberto fclose(iter->proc); 125258945Sroberto#endif 126258945Sroberto if (iter->ifaddrs != NULL) /* just in case */ 127258945Sroberto freeifaddrs(iter->ifaddrs); 128258945Sroberto isc_mem_put(mctx, iter, sizeof(*iter)); 129258945Sroberto return (result); 130258945Sroberto} 131258945Sroberto 132258945Sroberto/* 133258945Sroberto * Get information about the current interface to iter->current. 134258945Sroberto * If successful, return ISC_R_SUCCESS. 135258945Sroberto * If the interface has an unsupported address family, 136258945Sroberto * return ISC_R_IGNORE. 137258945Sroberto */ 138258945Sroberto 139258945Srobertostatic isc_result_t 140258945Srobertointernal_current(isc_interfaceiter_t *iter) { 141258945Sroberto struct ifaddrs *ifa; 142258945Sroberto int family; 143258945Sroberto unsigned int namelen; 144258945Sroberto 145258945Sroberto REQUIRE(VALID_IFITER(iter)); 146258945Sroberto 147258945Sroberto ifa = iter->pos; 148258945Sroberto 149258945Sroberto#ifdef __linux 150285169Scy /* 151285169Scy * [Bug 2792] 152285169Scy * burnicki: iter->pos is usually never NULL here (anymore?), 153285169Scy * so linux_if_inet6_current(iter) is never called here. 154285169Scy * However, that routine would check (under Linux), if the 155285169Scy * interface is in a tentative state, e.g. if there's no link 156285169Scy * yet but an IPv6 address has already be assigned. 157285169Scy */ 158258945Sroberto if (iter->pos == NULL) 159258945Sroberto return (linux_if_inet6_current(iter)); 160258945Sroberto#endif 161258945Sroberto 162258945Sroberto INSIST(ifa != NULL); 163258945Sroberto INSIST(ifa->ifa_name != NULL); 164258945Sroberto 165285169Scy 166285169Scy#ifdef IFF_RUNNING 167285169Scy /* 168285169Scy * [Bug 2792] 169285169Scy * burnicki: if the interface is not running then 170285169Scy * it may be in a tentative state. See above. 171285169Scy */ 172285169Scy if ((ifa->ifa_flags & IFF_RUNNING) == 0) 173285169Scy return (ISC_R_IGNORE); 174285169Scy#endif 175285169Scy 176258945Sroberto if (ifa->ifa_addr == NULL) 177258945Sroberto return (ISC_R_IGNORE); 178258945Sroberto 179258945Sroberto family = ifa->ifa_addr->sa_family; 180258945Sroberto if (family != AF_INET && family != AF_INET6) 181258945Sroberto return (ISC_R_IGNORE); 182258945Sroberto 183258945Sroberto#ifdef __linux 184258945Sroberto if (family == AF_INET6) 185258945Sroberto seenv6 = ISC_TRUE; 186258945Sroberto#endif 187258945Sroberto 188258945Sroberto memset(&iter->current, 0, sizeof(iter->current)); 189258945Sroberto 190258945Sroberto namelen = strlen(ifa->ifa_name); 191258945Sroberto if (namelen > sizeof(iter->current.name) - 1) 192258945Sroberto namelen = sizeof(iter->current.name) - 1; 193258945Sroberto 194258945Sroberto memset(iter->current.name, 0, sizeof(iter->current.name)); 195258945Sroberto memcpy(iter->current.name, ifa->ifa_name, namelen); 196258945Sroberto 197258945Sroberto iter->current.flags = 0; 198258945Sroberto 199258945Sroberto if ((ifa->ifa_flags & IFF_UP) != 0) 200258945Sroberto iter->current.flags |= INTERFACE_F_UP; 201258945Sroberto 202258945Sroberto if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) 203258945Sroberto iter->current.flags |= INTERFACE_F_POINTTOPOINT; 204258945Sroberto 205258945Sroberto if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 206258945Sroberto iter->current.flags |= INTERFACE_F_LOOPBACK; 207258945Sroberto 208258945Sroberto if ((ifa->ifa_flags & IFF_BROADCAST) != 0) 209258945Sroberto iter->current.flags |= INTERFACE_F_BROADCAST; 210258945Sroberto 211258945Sroberto#ifdef IFF_MULTICAST 212258945Sroberto if ((ifa->ifa_flags & IFF_MULTICAST) != 0) 213258945Sroberto iter->current.flags |= INTERFACE_F_MULTICAST; 214258945Sroberto#endif 215258945Sroberto 216258945Sroberto iter->current.af = family; 217258945Sroberto 218258945Sroberto get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name); 219258945Sroberto 220258945Sroberto if (ifa->ifa_netmask != NULL) 221258945Sroberto get_addr(family, &iter->current.netmask, ifa->ifa_netmask, 222258945Sroberto ifa->ifa_name); 223258945Sroberto 224258945Sroberto if (ifa->ifa_dstaddr != NULL && 225258945Sroberto (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) 226258945Sroberto get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr, 227258945Sroberto ifa->ifa_name); 228258945Sroberto 229258945Sroberto if (ifa->ifa_broadaddr != NULL && 230258945Sroberto (iter->current.flags & INTERFACE_F_BROADCAST) != 0) 231258945Sroberto get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr, 232258945Sroberto ifa->ifa_name); 233258945Sroberto 234282408Scy#ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 235282408Scy iter->current.ifindex = if_nametoindex(iter->current.name); 236282408Scy#endif 237258945Sroberto return (ISC_R_SUCCESS); 238258945Sroberto} 239258945Sroberto 240258945Sroberto/* 241258945Sroberto * Step the iterator to the next interface. Unlike 242258945Sroberto * isc_interfaceiter_next(), this may leave the iterator 243258945Sroberto * positioned on an interface that will ultimately 244258945Sroberto * be ignored. Return ISC_R_NOMORE if there are no more 245258945Sroberto * interfaces, otherwise ISC_R_SUCCESS. 246258945Sroberto */ 247258945Srobertostatic isc_result_t 248258945Srobertointernal_next(isc_interfaceiter_t *iter) { 249258945Sroberto 250258945Sroberto if (iter->pos != NULL) 251258945Sroberto iter->pos = iter->pos->ifa_next; 252258945Sroberto if (iter->pos == NULL) { 253258945Sroberto#ifdef __linux 254258945Sroberto if (!seenv6) 255258945Sroberto return (linux_if_inet6_next(iter)); 256258945Sroberto#endif 257258945Sroberto return (ISC_R_NOMORE); 258258945Sroberto } 259258945Sroberto 260258945Sroberto return (ISC_R_SUCCESS); 261258945Sroberto} 262258945Sroberto 263258945Srobertostatic void 264258945Srobertointernal_destroy(isc_interfaceiter_t *iter) { 265258945Sroberto 266258945Sroberto#ifdef __linux 267258945Sroberto if (iter->proc != NULL) 268258945Sroberto fclose(iter->proc); 269258945Sroberto iter->proc = NULL; 270258945Sroberto#endif 271258945Sroberto if (iter->ifaddrs) 272258945Sroberto freeifaddrs(iter->ifaddrs); 273258945Sroberto iter->ifaddrs = NULL; 274258945Sroberto} 275258945Sroberto 276258945Srobertostatic 277258945Srobertovoid internal_first(isc_interfaceiter_t *iter) { 278258945Sroberto 279258945Sroberto#ifdef __linux 280258945Sroberto linux_if_inet6_first(iter); 281258945Sroberto#endif 282258945Sroberto iter->pos = iter->ifaddrs; 283258945Sroberto} 284