17a6072eroberto/*
2047f369cy * Copyright (C) 2004, 2005, 2007-2009  Internet Systems Consortium, Inc. ("ISC")
37a6072eroberto * Copyright (C) 2003  Internet Software Consortium.
47a6072eroberto *
5d54cfbdroberto * Permission to use, copy, modify, and/or distribute this software for any
67a6072eroberto * purpose with or without fee is hereby granted, provided that the above
77a6072eroberto * copyright notice and this permission notice appear in all copies.
87a6072eroberto *
97a6072eroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
107a6072eroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
117a6072eroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
127a6072eroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
137a6072eroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
147a6072eroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
157a6072eroberto * PERFORMANCE OF THIS SOFTWARE.
167a6072eroberto */
177a6072eroberto
18047f369cy/* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */
197a6072eroberto
20d54cfbdroberto/*! \file
21d54cfbdroberto * \brief
227a6072eroberto * Obtain the list of network interfaces using the getifaddrs(3) library.
237a6072eroberto */
247a6072eroberto
257a6072eroberto#include <ifaddrs.h>
267a6072eroberto
27d54cfbdroberto/*% Iterator Magic */
287a6072eroberto#define IFITER_MAGIC		ISC_MAGIC('I', 'F', 'I', 'G')
29d54cfbdroberto/*% Valid Iterator */
307a6072eroberto#define VALID_IFITER(t)		ISC_MAGIC_VALID(t, IFITER_MAGIC)
317a6072eroberto
32d54cfbdroberto#ifdef __linux
33d54cfbdrobertostatic isc_boolean_t seenv6 = ISC_FALSE;
34d54cfbdroberto#endif
35d54cfbdroberto
36d54cfbdroberto/*% Iterator structure */
377a6072erobertostruct isc_interfaceiter {
38d54cfbdroberto	unsigned int		magic;		/*%< Magic number. */
397a6072eroberto	isc_mem_t		*mctx;
40d54cfbdroberto	void			*buf;		/*%< (unused) */
41d54cfbdroberto	unsigned int		bufsize;	/*%< (always 0) */
42d54cfbdroberto	struct ifaddrs		*ifaddrs;	/*%< List of ifaddrs */
43d54cfbdroberto	struct ifaddrs		*pos;		/*%< Ptr to current ifaddr */
44d54cfbdroberto	isc_interface_t		current;	/*%< Current interface data. */
45d54cfbdroberto	isc_result_t		result;		/*%< Last result code. */
46d54cfbdroberto#ifdef  __linux
47d54cfbdroberto	FILE *                  proc;
48d54cfbdroberto	char                    entry[ISC_IF_INET6_SZ];
49d54cfbdroberto	isc_result_t            valid;
50d54cfbdroberto#endif
517a6072eroberto};
527a6072eroberto
537a6072erobertoisc_result_t
547a6072erobertoisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
557a6072eroberto	isc_interfaceiter_t *iter;
567a6072eroberto	isc_result_t result;
577a6072eroberto	char strbuf[ISC_STRERRORSIZE];
58047f369cy	int trys, ret;
597a6072eroberto
60d54cfbdroberto	REQUIRE(mctx != NULL);
617a6072eroberto	REQUIRE(iterp != NULL);
627a6072eroberto	REQUIRE(*iterp == NULL);
637a6072eroberto
647a6072eroberto	iter = isc_mem_get(mctx, sizeof(*iter));
657a6072eroberto	if (iter == NULL)
667a6072eroberto		return (ISC_R_NOMEMORY);
677a6072eroberto
687a6072eroberto	iter->mctx = mctx;
697a6072eroberto	iter->buf = NULL;
707a6072eroberto	iter->bufsize = 0;
717a6072eroberto	iter->ifaddrs = NULL;
72d54cfbdroberto#ifdef __linux
73d54cfbdroberto	/*
74d54cfbdroberto	 * Only open "/proc/net/if_inet6" if we have never seen a IPv6
75d54cfbdroberto	 * address returned by getifaddrs().
76d54cfbdroberto	 */
77047f369cy	if (!seenv6) {
78d54cfbdroberto		iter->proc = fopen("/proc/net/if_inet6", "r");
79047f369cy		if (iter->proc == NULL) {
80047f369cy			isc__strerror(errno, strbuf, sizeof(strbuf));
81047f369cy			isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
82047f369cy				      ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
83047f369cy				      "failed to open /proc/net/if_inet6");
84047f369cy		}
85047f369cy	} else
86d54cfbdroberto		iter->proc = NULL;
87d54cfbdroberto	iter->valid = ISC_R_FAILURE;
88d54cfbdroberto#endif
897a6072eroberto
90047f369cy	/* If interrupted, try again */
91047f369cy	for (trys = 0; trys < 3; trys++) {
92047f369cy		if ((ret = getifaddrs(&iter->ifaddrs)) >= 0)
93047f369cy			break;
94047f369cy		if (errno != EINTR)
95047f369cy			break;
96047f369cy	}
97047f369cy	if (ret < 0) {
987a6072eroberto		isc__strerror(errno, strbuf, sizeof(strbuf));
99bdc155dcy		UNEXPECTED_ERROR(__FILE__, __LINE__,
100bdc155dcy                		 "getting interface addresses: %s: %s",
101bdc155dcy				 isc_msgcat_get(isc_msgcat,
102bdc155dcy						ISC_MSGSET_IFITERGETIFADDRS,
103bdc155dcy						ISC_MSG_GETIFADDRS,
104bdc155dcy						"getifaddrs"),
105bdc155dcy				 strbuf);
1067a6072eroberto		result = ISC_R_UNEXPECTED;
1077a6072eroberto		goto failure;
1087a6072eroberto	}
1097a6072eroberto
1107a6072eroberto	/*
1117a6072eroberto	 * A newly created iterator has an undefined position
1127a6072eroberto	 * until isc_interfaceiter_first() is called.
1137a6072eroberto	 */
1147a6072eroberto	iter->pos = NULL;
1157a6072eroberto	iter->result = ISC_R_FAILURE;
1167a6072eroberto
1177a6072eroberto	iter->magic = IFITER_MAGIC;
1187a6072eroberto	*iterp = iter;
1197a6072eroberto	return (ISC_R_SUCCESS);
1207a6072eroberto
1217a6072eroberto failure:
122d54cfbdroberto#ifdef __linux
123d54cfbdroberto	if (iter->proc != NULL)
124d54cfbdroberto		fclose(iter->proc);
125d54cfbdroberto#endif
1267a6072eroberto	if (iter->ifaddrs != NULL) /* just in case */
1277a6072eroberto		freeifaddrs(iter->ifaddrs);
1287a6072eroberto	isc_mem_put(mctx, iter, sizeof(*iter));
1297a6072eroberto	return (result);
1307a6072eroberto}
1317a6072eroberto
1327a6072eroberto/*
1337a6072eroberto * Get information about the current interface to iter->current.
1347a6072eroberto * If successful, return ISC_R_SUCCESS.
1357a6072eroberto * If the interface has an unsupported address family,
1367a6072eroberto * return ISC_R_IGNORE.
1377a6072eroberto */
1387a6072eroberto
1397a6072erobertostatic isc_result_t
1407a6072erobertointernal_current(isc_interfaceiter_t *iter) {
1417a6072eroberto	struct ifaddrs *ifa;
1427a6072eroberto	int family;
1437a6072eroberto	unsigned int namelen;
1447a6072eroberto
1457a6072eroberto	REQUIRE(VALID_IFITER(iter));
1467a6072eroberto
1477a6072eroberto	ifa = iter->pos;
1487a6072eroberto
149d54cfbdroberto#ifdef __linux
1505ef283fcy	/*
1515ef283fcy	 * [Bug 2792]
1525ef283fcy	 * burnicki: iter->pos is usually never NULL here (anymore?),
1535ef283fcy	 * so linux_if_inet6_current(iter) is never called here.
1545ef283fcy	 * However, that routine would check (under Linux), if the
1555ef283fcy	 * interface is in a tentative state, e.g. if there's no link
1565ef283fcy	 * yet but an IPv6 address has already be assigned.
1575ef283fcy	 */
158d54cfbdroberto	if (iter->pos == NULL)
159d54cfbdroberto		return (linux_if_inet6_current(iter));
160d54cfbdroberto#endif
161d54cfbdroberto
1627a6072eroberto	INSIST(ifa != NULL);
1637a6072eroberto	INSIST(ifa->ifa_name != NULL);
164d54cfbdroberto
1655ef283fcy
1665ef283fcy#ifdef IFF_RUNNING
1675ef283fcy	/*
1685ef283fcy	 * [Bug 2792]
1695ef283fcy	 * burnicki: if the interface is not running then
1705ef283fcy	 * it may be in a tentative state. See above.
1715ef283fcy	 */
1725ef283fcy	if ((ifa->ifa_flags & IFF_RUNNING) == 0)
1735ef283fcy		return (ISC_R_IGNORE);
1745ef283fcy#endif
1755ef283fcy
176d54cfbdroberto	if (ifa->ifa_addr == NULL)
177d54cfbdroberto		return (ISC_R_IGNORE);
1787a6072eroberto
1797a6072eroberto	family = ifa->ifa_addr->sa_family;
1807a6072eroberto	if (family != AF_INET && family != AF_INET6)
1817a6072eroberto		return (ISC_R_IGNORE);
1827a6072eroberto
183d54cfbdroberto#ifdef __linux
184d54cfbdroberto	if (family == AF_INET6)
185d54cfbdroberto		seenv6 = ISC_TRUE;
186d54cfbdroberto#endif
187d54cfbdroberto
1887a6072eroberto	memset(&iter->current, 0, sizeof(iter->current));
1897a6072eroberto
1907a6072eroberto	namelen = strlen(ifa->ifa_name);
1917a6072eroberto	if (namelen > sizeof(iter->current.name) - 1)
1927a6072eroberto		namelen = sizeof(iter->current.name) - 1;
1937a6072eroberto
1947a6072eroberto	memset(iter->current.name, 0, sizeof(iter->current.name));
1957a6072eroberto	memcpy(iter->current.name, ifa->ifa_name, namelen);
1967a6072eroberto
1977a6072eroberto	iter->current.flags = 0;
1987a6072eroberto
1997a6072eroberto	if ((ifa->ifa_flags & IFF_UP) != 0)
2007a6072eroberto		iter->current.flags |= INTERFACE_F_UP;
2017a6072eroberto
2027a6072eroberto	if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
2037a6072eroberto		iter->current.flags |= INTERFACE_F_POINTTOPOINT;
2047a6072eroberto
2057a6072eroberto	if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
2067a6072eroberto		iter->current.flags |= INTERFACE_F_LOOPBACK;
2077a6072eroberto
208d54cfbdroberto	if ((ifa->ifa_flags & IFF_BROADCAST) != 0)
2097a6072eroberto		iter->current.flags |= INTERFACE_F_BROADCAST;
2107a6072eroberto
2117a6072eroberto#ifdef IFF_MULTICAST
212d54cfbdroberto	if ((ifa->ifa_flags & IFF_MULTICAST) != 0)
2137a6072eroberto		iter->current.flags |= INTERFACE_F_MULTICAST;
2147a6072eroberto#endif
215d54cfbdroberto
2167a6072eroberto	iter->current.af = family;
2177a6072eroberto
2187a6072eroberto	get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name);
2197a6072eroberto
2207a6072eroberto	if (ifa->ifa_netmask != NULL)
2217a6072eroberto		get_addr(family, &iter->current.netmask, ifa->ifa_netmask,
2227a6072eroberto			 ifa->ifa_name);
2237a6072eroberto
2247a6072eroberto	if (ifa->ifa_dstaddr != NULL &&
2257a6072eroberto	    (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
2267a6072eroberto		get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr,
2277a6072eroberto			 ifa->ifa_name);
2287a6072eroberto
2297a6072eroberto	if (ifa->ifa_broadaddr != NULL &&
2307a6072eroberto	    (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
2317a6072eroberto		get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr,
2327a6072eroberto			 ifa->ifa_name);
2337a6072eroberto
234f63afe2cy#ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
235f63afe2cy	iter->current.ifindex = if_nametoindex(iter->current.name);
236f63afe2cy#endif
2377a6072eroberto	return (ISC_R_SUCCESS);
2387a6072eroberto}
2397a6072eroberto
2407a6072eroberto/*
2417a6072eroberto * Step the iterator to the next interface.  Unlike
2427a6072eroberto * isc_interfaceiter_next(), this may leave the iterator
2437a6072eroberto * positioned on an interface that will ultimately
2447a6072eroberto * be ignored.  Return ISC_R_NOMORE if there are no more
2457a6072eroberto * interfaces, otherwise ISC_R_SUCCESS.
2467a6072eroberto */
2477a6072erobertostatic isc_result_t
2487a6072erobertointernal_next(isc_interfaceiter_t *iter) {
2497a6072eroberto
250d54cfbdroberto	if (iter->pos != NULL)
251d54cfbdroberto		iter->pos = iter->pos->ifa_next;
252d54cfbdroberto	if (iter->pos == NULL) {
253d54cfbdroberto#ifdef __linux
254d54cfbdroberto		if (!seenv6)
255d54cfbdroberto			return (linux_if_inet6_next(iter));
256d54cfbdroberto#endif
2577a6072eroberto		return (ISC_R_NOMORE);
258d54cfbdroberto	}
2597a6072eroberto
2607a6072eroberto	return (ISC_R_SUCCESS);
2617a6072eroberto}
2627a6072eroberto
2637a6072erobertostatic void
2647a6072erobertointernal_destroy(isc_interfaceiter_t *iter) {
265d54cfbdroberto
266d54cfbdroberto#ifdef __linux
267d54cfbdroberto	if (iter->proc != NULL)
268d54cfbdroberto		fclose(iter->proc);
269d54cfbdroberto	iter->proc = NULL;
270d54cfbdroberto#endif
2717a6072eroberto	if (iter->ifaddrs)
2727a6072eroberto		freeifaddrs(iter->ifaddrs);
2737a6072eroberto	iter->ifaddrs = NULL;
2747a6072eroberto}
2757a6072eroberto
2767a6072erobertostatic
2777a6072erobertovoid internal_first(isc_interfaceiter_t *iter) {
278d54cfbdroberto
279d54cfbdroberto#ifdef __linux
280d54cfbdroberto	linux_if_inet6_first(iter);
281d54cfbdroberto#endif
2827a6072eroberto	iter->pos = iter->ifaddrs;
2837a6072eroberto}
284