1/*	$NetBSD: ifiter_sysctl.c,v 1.6 2020/05/25 20:47:23 christos Exp $	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp  */
21
22/*! \file
23 * \brief
24 * Obtain the list of network interfaces using sysctl.
25 * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
26 * and 19.16.
27 */
28
29#include <sys/param.h>
30#include <sys/sysctl.h>
31
32#include <net/route.h>
33#include <net/if_dl.h>
34
35/* XXX what about Alpha? */
36#ifdef sgi
37#define ROUNDUP(a) ((a) > 0 ? \
38		(1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
39		sizeof(__uint64_t))
40#else
41#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
42                    : sizeof(long))
43#endif
44
45#define IFITER_MAGIC		ISC_MAGIC('I', 'F', 'I', 'S')
46#define VALID_IFITER(t)		ISC_MAGIC_VALID(t, IFITER_MAGIC)
47
48struct isc_interfaceiter {
49	unsigned int		magic;		/* Magic number. */
50	isc_mem_t		*mctx;
51	void			*buf;		/* Buffer for sysctl data. */
52	unsigned int		bufsize;	/* Bytes allocated. */
53	unsigned int		bufused;	/* Bytes used. */
54	unsigned int		pos;		/* Current offset in
55						   sysctl data. */
56	isc_interface_t		current;	/* Current interface data. */
57	isc_result_t		result;		/* Last result code. */
58};
59
60static int mib[6] = {
61	CTL_NET,
62	PF_ROUTE,
63        0,
64	0, 			/* Any address family. */
65        NET_RT_IFLIST,
66	0 			/* Flags. */
67};
68
69isc_result_t
70isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
71	isc_interfaceiter_t *iter;
72	isc_result_t result;
73	size_t bufsize;
74	size_t bufused;
75	char strbuf[ISC_STRERRORSIZE];
76
77	REQUIRE(mctx != NULL);
78	REQUIRE(iterp != NULL);
79	REQUIRE(*iterp == NULL);
80
81	iter = isc_mem_get(mctx, sizeof(*iter));
82	if (iter == NULL)
83		return (ISC_R_NOMEMORY);
84
85	iter->mctx = mctx;
86	iter->buf = 0;
87
88	/*
89	 * Determine the amount of memory needed.
90	 */
91	bufsize = 0;
92	if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
93		isc__strerror(errno, strbuf, sizeof(strbuf));
94		UNEXPECTED_ERROR(__FILE__, __LINE__,
95				 isc_msgcat_get(isc_msgcat,
96						ISC_MSGSET_IFITERSYSCTL,
97						ISC_MSG_GETIFLISTSIZE,
98						"getting interface "
99						"list size: sysctl: %s"),
100				 strbuf);
101		result = ISC_R_UNEXPECTED;
102		goto failure;
103	}
104	iter->bufsize = bufsize;
105
106	iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
107	if (iter->buf == NULL) {
108		result = ISC_R_NOMEMORY;
109		goto failure;
110	}
111
112	bufused = bufsize;
113	if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
114		isc__strerror(errno, strbuf, sizeof(strbuf));
115		UNEXPECTED_ERROR(__FILE__, __LINE__,
116				 isc_msgcat_get(isc_msgcat,
117						ISC_MSGSET_IFITERSYSCTL,
118						ISC_MSG_GETIFLIST,
119						"getting interface list: "
120						"sysctl: %s"),
121				 strbuf);
122		result = ISC_R_UNEXPECTED;
123		goto failure;
124	}
125	iter->bufused = bufused;
126	INSIST(iter->bufused <= iter->bufsize);
127
128	/*
129	 * A newly created iterator has an undefined position
130	 * until isc_interfaceiter_first() is called.
131	 */
132	iter->pos = (unsigned int) -1;
133	iter->result = ISC_R_FAILURE;
134
135	iter->magic = IFITER_MAGIC;
136	*iterp = iter;
137	return (ISC_R_SUCCESS);
138
139 failure:
140	if (iter->buf != NULL)
141		isc_mem_put(mctx, iter->buf, iter->bufsize);
142	isc_mem_put(mctx, iter, sizeof(*iter));
143	return (result);
144}
145
146/*
147 * Get information about the current interface to iter->current.
148 * If successful, return ISC_R_SUCCESS.
149 * If the interface has an unsupported address family,
150 * return ISC_R_IGNORE.  In case of other failure,
151 * return ISC_R_UNEXPECTED.
152 */
153
154static isc_result_t
155internal_current(isc_interfaceiter_t *iter) {
156	struct ifa_msghdr *ifam, *ifam_end;
157
158	REQUIRE(VALID_IFITER(iter));
159	REQUIRE (iter->pos < (unsigned int) iter->bufused);
160
161	ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
162	ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
163
164	// Skip wrong RTM version headers
165	if (ifam->ifam_version != RTM_VERSION)
166		return (ISC_R_IGNORE);
167
168	if (ifam->ifam_type == RTM_IFINFO) {
169		struct if_msghdr *ifm = (struct if_msghdr *) ifam;
170		struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
171		unsigned int namelen;
172
173		memset(&iter->current, 0, sizeof(iter->current));
174
175		iter->current.ifindex = sdl->sdl_index;
176		namelen = sdl->sdl_nlen;
177		if (namelen > sizeof(iter->current.name) - 1)
178			namelen = sizeof(iter->current.name) - 1;
179
180		memset(iter->current.name, 0, sizeof(iter->current.name));
181		memcpy(iter->current.name, sdl->sdl_data, namelen);
182
183		iter->current.flags = 0;
184
185		if ((ifam->ifam_flags & IFF_UP) != 0)
186			iter->current.flags |= INTERFACE_F_UP;
187
188		if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
189			iter->current.flags |= INTERFACE_F_POINTTOPOINT;
190
191		if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
192			iter->current.flags |= INTERFACE_F_LOOPBACK;
193
194		if ((ifam->ifam_flags & IFF_BROADCAST) != 0)
195			iter->current.flags |= INTERFACE_F_BROADCAST;
196
197#ifdef IFF_MULTICAST
198		if ((ifam->ifam_flags & IFF_MULTICAST) != 0)
199			iter->current.flags |= INTERFACE_F_MULTICAST;
200#endif
201
202		/*
203		 * This is not an interface address.
204		 * Force another iteration.
205		 */
206		return (ISC_R_IGNORE);
207	} else if (ifam->ifam_type == RTM_NEWADDR) {
208		int i;
209		int family;
210		struct sockaddr *mask_sa = NULL;
211		struct sockaddr *addr_sa = NULL;
212		struct sockaddr *dst_sa = NULL;
213
214		struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
215		family = sa->sa_family;
216
217		for (i = 0; i < RTAX_MAX; i++)
218		{
219			if ((ifam->ifam_addrs & (1 << i)) == 0)
220				continue;
221
222			INSIST(sa < (struct sockaddr *) ifam_end);
223
224			switch (i) {
225			case RTAX_NETMASK: /* Netmask */
226				mask_sa = sa;
227				break;
228			case RTAX_IFA: /* Interface address */
229				addr_sa = sa;
230				break;
231			case RTAX_BRD: /* Broadcast or destination address */
232				dst_sa = sa;
233				break;
234			}
235#ifdef ISC_PLATFORM_HAVESALEN
236			sa = (struct sockaddr *)((char*)(sa)
237					 + ROUNDUP(sa->sa_len));
238#else
239#ifdef sgi
240			/*
241			 * Do as the contributed SGI code does.
242			 */
243			sa = (struct sockaddr *)((char*)(sa)
244					 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
245#else
246			/* XXX untested. */
247			sa = (struct sockaddr *)((char*)(sa)
248					 + ROUNDUP(sizeof(struct sockaddr)));
249#endif
250#endif
251		}
252
253		if (addr_sa == NULL)
254			return (ISC_R_IGNORE);
255
256		family = addr_sa->sa_family;
257		if (family != AF_INET && family != AF_INET6)
258			return (ISC_R_IGNORE);
259
260		iter->current.af = family;
261
262		get_addr(family, &iter->current.address, addr_sa,
263			 iter->current.name);
264
265		if (mask_sa != NULL)
266			get_addr(family, &iter->current.netmask, mask_sa,
267				 iter->current.name);
268
269		if (dst_sa != NULL &&
270		    (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
271			get_addr(family, &iter->current.dstaddress, dst_sa,
272				 iter->current.name);
273
274		if (dst_sa != NULL &&
275		    (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
276			get_addr(family, &iter->current.broadcast, dst_sa,
277				 iter->current.name);
278
279		return (ISC_R_SUCCESS);
280	} else {
281		printf("%s", isc_msgcat_get(isc_msgcat,
282					    ISC_MSGSET_IFITERSYSCTL,
283					    ISC_MSG_UNEXPECTEDTYPE,
284					    "warning: unexpected interface "
285					    "list message type\n"));
286		return (ISC_R_IGNORE);
287	}
288}
289
290/*
291 * Step the iterator to the next interface.  Unlike
292 * isc_interfaceiter_next(), this may leave the iterator
293 * positioned on an interface that will ultimately
294 * be ignored.  Return ISC_R_NOMORE if there are no more
295 * interfaces, otherwise ISC_R_SUCCESS.
296 */
297static isc_result_t
298internal_next(isc_interfaceiter_t *iter) {
299	struct ifa_msghdr *ifam;
300	REQUIRE (iter->pos < (unsigned int) iter->bufused);
301
302	ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
303
304	iter->pos += ifam->ifam_msglen;
305
306	if (iter->pos >= iter->bufused)
307		return (ISC_R_NOMORE);
308
309	return (ISC_R_SUCCESS);
310}
311
312static void
313internal_destroy(isc_interfaceiter_t *iter) {
314	UNUSED(iter); /* Unused. */
315	/*
316	 * Do nothing.
317	 */
318}
319
320static
321void internal_first(isc_interfaceiter_t *iter) {
322	iter->pos = 0;
323}
324