1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 2003 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file
21*a466cc55SCy Schubert * \brief
22*a466cc55SCy Schubert * Obtain the list of network interfaces using the getifaddrs(3) library.
23*a466cc55SCy Schubert */
24*a466cc55SCy Schubert
25*a466cc55SCy Schubert #include <ifaddrs.h>
26*a466cc55SCy Schubert
27*a466cc55SCy Schubert /*% Iterator Magic */
28*a466cc55SCy Schubert #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G')
29*a466cc55SCy Schubert /*% Valid Iterator */
30*a466cc55SCy Schubert #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
31*a466cc55SCy Schubert
32*a466cc55SCy Schubert #ifdef __linux
33*a466cc55SCy Schubert static isc_boolean_t seenv6 = ISC_FALSE;
34*a466cc55SCy Schubert #endif
35*a466cc55SCy Schubert
36*a466cc55SCy Schubert /*% Iterator structure */
37*a466cc55SCy Schubert struct isc_interfaceiter {
38*a466cc55SCy Schubert unsigned int magic; /*%< Magic number. */
39*a466cc55SCy Schubert isc_mem_t *mctx;
40*a466cc55SCy Schubert void *buf; /*%< (unused) */
41*a466cc55SCy Schubert unsigned int bufsize; /*%< (always 0) */
42*a466cc55SCy Schubert struct ifaddrs *ifaddrs; /*%< List of ifaddrs */
43*a466cc55SCy Schubert struct ifaddrs *pos; /*%< Ptr to current ifaddr */
44*a466cc55SCy Schubert isc_interface_t current; /*%< Current interface data. */
45*a466cc55SCy Schubert isc_result_t result; /*%< Last result code. */
46*a466cc55SCy Schubert #ifdef __linux
47*a466cc55SCy Schubert FILE * proc;
48*a466cc55SCy Schubert char entry[ISC_IF_INET6_SZ];
49*a466cc55SCy Schubert isc_result_t valid;
50*a466cc55SCy Schubert #endif
51*a466cc55SCy Schubert };
52*a466cc55SCy Schubert
53*a466cc55SCy Schubert isc_result_t
isc_interfaceiter_create(isc_mem_t * mctx,isc_interfaceiter_t ** iterp)54*a466cc55SCy Schubert isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
55*a466cc55SCy Schubert isc_interfaceiter_t *iter;
56*a466cc55SCy Schubert isc_result_t result;
57*a466cc55SCy Schubert char strbuf[ISC_STRERRORSIZE];
58*a466cc55SCy Schubert int trys, ret;
59*a466cc55SCy Schubert
60*a466cc55SCy Schubert REQUIRE(mctx != NULL);
61*a466cc55SCy Schubert REQUIRE(iterp != NULL);
62*a466cc55SCy Schubert REQUIRE(*iterp == NULL);
63*a466cc55SCy Schubert
64*a466cc55SCy Schubert iter = isc_mem_get(mctx, sizeof(*iter));
65*a466cc55SCy Schubert if (iter == NULL)
66*a466cc55SCy Schubert return (ISC_R_NOMEMORY);
67*a466cc55SCy Schubert
68*a466cc55SCy Schubert iter->mctx = mctx;
69*a466cc55SCy Schubert iter->buf = NULL;
70*a466cc55SCy Schubert iter->bufsize = 0;
71*a466cc55SCy Schubert iter->ifaddrs = NULL;
72*a466cc55SCy Schubert #ifdef __linux
73*a466cc55SCy Schubert /*
74*a466cc55SCy Schubert * Only open "/proc/net/if_inet6" if we have never seen a IPv6
75*a466cc55SCy Schubert * address returned by getifaddrs().
76*a466cc55SCy Schubert */
77*a466cc55SCy Schubert if (!seenv6) {
78*a466cc55SCy Schubert iter->proc = fopen("/proc/net/if_inet6", "r");
79*a466cc55SCy Schubert if (iter->proc == NULL) {
80*a466cc55SCy Schubert isc__strerror(errno, strbuf, sizeof(strbuf));
81*a466cc55SCy Schubert isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
82*a466cc55SCy Schubert ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
83*a466cc55SCy Schubert "failed to open /proc/net/if_inet6");
84*a466cc55SCy Schubert }
85*a466cc55SCy Schubert } else
86*a466cc55SCy Schubert iter->proc = NULL;
87*a466cc55SCy Schubert iter->valid = ISC_R_FAILURE;
88*a466cc55SCy Schubert #endif
89*a466cc55SCy Schubert
90*a466cc55SCy Schubert /* If interrupted, try again */
91*a466cc55SCy Schubert for (trys = 0; trys < 3; trys++) {
92*a466cc55SCy Schubert if ((ret = getifaddrs(&iter->ifaddrs)) >= 0)
93*a466cc55SCy Schubert break;
94*a466cc55SCy Schubert if (errno != EINTR)
95*a466cc55SCy Schubert break;
96*a466cc55SCy Schubert }
97*a466cc55SCy Schubert if (ret < 0) {
98*a466cc55SCy Schubert isc__strerror(errno, strbuf, sizeof(strbuf));
99*a466cc55SCy Schubert UNEXPECTED_ERROR(__FILE__, __LINE__,
100*a466cc55SCy Schubert "getting interface addresses: %s: %s",
101*a466cc55SCy Schubert isc_msgcat_get(isc_msgcat,
102*a466cc55SCy Schubert ISC_MSGSET_IFITERGETIFADDRS,
103*a466cc55SCy Schubert ISC_MSG_GETIFADDRS,
104*a466cc55SCy Schubert "getifaddrs"),
105*a466cc55SCy Schubert strbuf);
106*a466cc55SCy Schubert result = ISC_R_UNEXPECTED;
107*a466cc55SCy Schubert goto failure;
108*a466cc55SCy Schubert }
109*a466cc55SCy Schubert
110*a466cc55SCy Schubert /*
111*a466cc55SCy Schubert * A newly created iterator has an undefined position
112*a466cc55SCy Schubert * until isc_interfaceiter_first() is called.
113*a466cc55SCy Schubert */
114*a466cc55SCy Schubert iter->pos = NULL;
115*a466cc55SCy Schubert iter->result = ISC_R_FAILURE;
116*a466cc55SCy Schubert
117*a466cc55SCy Schubert iter->magic = IFITER_MAGIC;
118*a466cc55SCy Schubert *iterp = iter;
119*a466cc55SCy Schubert return (ISC_R_SUCCESS);
120*a466cc55SCy Schubert
121*a466cc55SCy Schubert failure:
122*a466cc55SCy Schubert #ifdef __linux
123*a466cc55SCy Schubert if (iter->proc != NULL)
124*a466cc55SCy Schubert fclose(iter->proc);
125*a466cc55SCy Schubert #endif
126*a466cc55SCy Schubert if (iter->ifaddrs != NULL) /* just in case */
127*a466cc55SCy Schubert freeifaddrs(iter->ifaddrs);
128*a466cc55SCy Schubert isc_mem_put(mctx, iter, sizeof(*iter));
129*a466cc55SCy Schubert return (result);
130*a466cc55SCy Schubert }
131*a466cc55SCy Schubert
132*a466cc55SCy Schubert /*
133*a466cc55SCy Schubert * Get information about the current interface to iter->current.
134*a466cc55SCy Schubert * If successful, return ISC_R_SUCCESS.
135*a466cc55SCy Schubert * If the interface has an unsupported address family,
136*a466cc55SCy Schubert * return ISC_R_IGNORE.
137*a466cc55SCy Schubert */
138*a466cc55SCy Schubert
139*a466cc55SCy Schubert static isc_result_t
internal_current(isc_interfaceiter_t * iter)140*a466cc55SCy Schubert internal_current(isc_interfaceiter_t *iter) {
141*a466cc55SCy Schubert struct ifaddrs *ifa;
142*a466cc55SCy Schubert int family;
143*a466cc55SCy Schubert unsigned int namelen;
144*a466cc55SCy Schubert
145*a466cc55SCy Schubert REQUIRE(VALID_IFITER(iter));
146*a466cc55SCy Schubert
147*a466cc55SCy Schubert ifa = iter->pos;
148*a466cc55SCy Schubert
149*a466cc55SCy Schubert #ifdef __linux
150*a466cc55SCy Schubert /*
151*a466cc55SCy Schubert * [Bug 2792]
152*a466cc55SCy Schubert * burnicki: iter->pos is usually never NULL here (anymore?),
153*a466cc55SCy Schubert * so linux_if_inet6_current(iter) is never called here.
154*a466cc55SCy Schubert * However, that routine would check (under Linux), if the
155*a466cc55SCy Schubert * interface is in a tentative state, e.g. if there's no link
156*a466cc55SCy Schubert * yet but an IPv6 address has already be assigned.
157*a466cc55SCy Schubert */
158*a466cc55SCy Schubert if (iter->pos == NULL)
159*a466cc55SCy Schubert return (linux_if_inet6_current(iter));
160*a466cc55SCy Schubert #endif
161*a466cc55SCy Schubert
162*a466cc55SCy Schubert INSIST(ifa != NULL);
163*a466cc55SCy Schubert INSIST(ifa->ifa_name != NULL);
164*a466cc55SCy Schubert
165*a466cc55SCy Schubert
166*a466cc55SCy Schubert #ifdef IFF_RUNNING
167*a466cc55SCy Schubert /*
168*a466cc55SCy Schubert * [Bug 2792]
169*a466cc55SCy Schubert * burnicki: if the interface is not running then
170*a466cc55SCy Schubert * it may be in a tentative state. See above.
171*a466cc55SCy Schubert */
172*a466cc55SCy Schubert if ((ifa->ifa_flags & IFF_RUNNING) == 0)
173*a466cc55SCy Schubert return (ISC_R_IGNORE);
174*a466cc55SCy Schubert #endif
175*a466cc55SCy Schubert
176*a466cc55SCy Schubert if (ifa->ifa_addr == NULL)
177*a466cc55SCy Schubert return (ISC_R_IGNORE);
178*a466cc55SCy Schubert
179*a466cc55SCy Schubert family = ifa->ifa_addr->sa_family;
180*a466cc55SCy Schubert if (family != AF_INET && family != AF_INET6)
181*a466cc55SCy Schubert return (ISC_R_IGNORE);
182*a466cc55SCy Schubert
183*a466cc55SCy Schubert #ifdef __linux
184*a466cc55SCy Schubert if (family == AF_INET6)
185*a466cc55SCy Schubert seenv6 = ISC_TRUE;
186*a466cc55SCy Schubert #endif
187*a466cc55SCy Schubert
188*a466cc55SCy Schubert memset(&iter->current, 0, sizeof(iter->current));
189*a466cc55SCy Schubert
190*a466cc55SCy Schubert namelen = strlen(ifa->ifa_name);
191*a466cc55SCy Schubert if (namelen > sizeof(iter->current.name) - 1)
192*a466cc55SCy Schubert namelen = sizeof(iter->current.name) - 1;
193*a466cc55SCy Schubert
194*a466cc55SCy Schubert memset(iter->current.name, 0, sizeof(iter->current.name));
195*a466cc55SCy Schubert memcpy(iter->current.name, ifa->ifa_name, namelen);
196*a466cc55SCy Schubert
197*a466cc55SCy Schubert iter->current.flags = 0;
198*a466cc55SCy Schubert
199*a466cc55SCy Schubert if ((ifa->ifa_flags & IFF_UP) != 0)
200*a466cc55SCy Schubert iter->current.flags |= INTERFACE_F_UP;
201*a466cc55SCy Schubert
202*a466cc55SCy Schubert if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
203*a466cc55SCy Schubert iter->current.flags |= INTERFACE_F_POINTTOPOINT;
204*a466cc55SCy Schubert
205*a466cc55SCy Schubert if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
206*a466cc55SCy Schubert iter->current.flags |= INTERFACE_F_LOOPBACK;
207*a466cc55SCy Schubert
208*a466cc55SCy Schubert if ((ifa->ifa_flags & IFF_BROADCAST) != 0)
209*a466cc55SCy Schubert iter->current.flags |= INTERFACE_F_BROADCAST;
210*a466cc55SCy Schubert
211*a466cc55SCy Schubert #ifdef IFF_MULTICAST
212*a466cc55SCy Schubert if ((ifa->ifa_flags & IFF_MULTICAST) != 0)
213*a466cc55SCy Schubert iter->current.flags |= INTERFACE_F_MULTICAST;
214*a466cc55SCy Schubert #endif
215*a466cc55SCy Schubert
216*a466cc55SCy Schubert iter->current.af = family;
217*a466cc55SCy Schubert
218*a466cc55SCy Schubert get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name);
219*a466cc55SCy Schubert
220*a466cc55SCy Schubert if (ifa->ifa_netmask != NULL)
221*a466cc55SCy Schubert get_addr(family, &iter->current.netmask, ifa->ifa_netmask,
222*a466cc55SCy Schubert ifa->ifa_name);
223*a466cc55SCy Schubert
224*a466cc55SCy Schubert if (ifa->ifa_dstaddr != NULL &&
225*a466cc55SCy Schubert (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
226*a466cc55SCy Schubert get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr,
227*a466cc55SCy Schubert ifa->ifa_name);
228*a466cc55SCy Schubert
229*a466cc55SCy Schubert if (ifa->ifa_broadaddr != NULL &&
230*a466cc55SCy Schubert (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
231*a466cc55SCy Schubert get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr,
232*a466cc55SCy Schubert ifa->ifa_name);
233*a466cc55SCy Schubert
234*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
235*a466cc55SCy Schubert iter->current.ifindex = if_nametoindex(iter->current.name);
236*a466cc55SCy Schubert #endif
237*a466cc55SCy Schubert return (ISC_R_SUCCESS);
238*a466cc55SCy Schubert }
239*a466cc55SCy Schubert
240*a466cc55SCy Schubert /*
241*a466cc55SCy Schubert * Step the iterator to the next interface. Unlike
242*a466cc55SCy Schubert * isc_interfaceiter_next(), this may leave the iterator
243*a466cc55SCy Schubert * positioned on an interface that will ultimately
244*a466cc55SCy Schubert * be ignored. Return ISC_R_NOMORE if there are no more
245*a466cc55SCy Schubert * interfaces, otherwise ISC_R_SUCCESS.
246*a466cc55SCy Schubert */
247*a466cc55SCy Schubert static isc_result_t
internal_next(isc_interfaceiter_t * iter)248*a466cc55SCy Schubert internal_next(isc_interfaceiter_t *iter) {
249*a466cc55SCy Schubert
250*a466cc55SCy Schubert if (iter->pos != NULL)
251*a466cc55SCy Schubert iter->pos = iter->pos->ifa_next;
252*a466cc55SCy Schubert if (iter->pos == NULL) {
253*a466cc55SCy Schubert #ifdef __linux
254*a466cc55SCy Schubert if (!seenv6)
255*a466cc55SCy Schubert return (linux_if_inet6_next(iter));
256*a466cc55SCy Schubert #endif
257*a466cc55SCy Schubert return (ISC_R_NOMORE);
258*a466cc55SCy Schubert }
259*a466cc55SCy Schubert
260*a466cc55SCy Schubert return (ISC_R_SUCCESS);
261*a466cc55SCy Schubert }
262*a466cc55SCy Schubert
263*a466cc55SCy Schubert static void
internal_destroy(isc_interfaceiter_t * iter)264*a466cc55SCy Schubert internal_destroy(isc_interfaceiter_t *iter) {
265*a466cc55SCy Schubert
266*a466cc55SCy Schubert #ifdef __linux
267*a466cc55SCy Schubert if (iter->proc != NULL)
268*a466cc55SCy Schubert fclose(iter->proc);
269*a466cc55SCy Schubert iter->proc = NULL;
270*a466cc55SCy Schubert #endif
271*a466cc55SCy Schubert if (iter->ifaddrs)
272*a466cc55SCy Schubert freeifaddrs(iter->ifaddrs);
273*a466cc55SCy Schubert iter->ifaddrs = NULL;
274*a466cc55SCy Schubert }
275*a466cc55SCy Schubert
276*a466cc55SCy Schubert static
internal_first(isc_interfaceiter_t * iter)277*a466cc55SCy Schubert void internal_first(isc_interfaceiter_t *iter) {
278*a466cc55SCy Schubert
279*a466cc55SCy Schubert #ifdef __linux
280*a466cc55SCy Schubert linux_if_inet6_first(iter);
281*a466cc55SCy Schubert #endif
282*a466cc55SCy Schubert iter->pos = iter->ifaddrs;
283*a466cc55SCy Schubert }
284