1/*
2 * Copyright (c) 2003 Bruce M. Simpson.
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *        This product includes software developed by Bruce M. Simpson.
16 * 4. Neither the name of Bruce M. Simpson nor the names of other
17 *    contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BRUCE M. SIMPSON AND AFFILIATES
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL BRUCE M. SIMPSON OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34#include <sys/param.h>
35#include <sys/sysctl.h>
36#include <sys/ioctl.h>
37#include <sys/socket.h>
38#include <net/if.h>
39#include <net/if_dl.h>
40#include <net/route.h>
41#include <errno.h>
42#include <ifaddrs.h>
43#include <stdlib.h>
44#include <stdint.h>
45#include <string.h>
46
47#define	SALIGN	(sizeof(uint32_t) - 1)
48#define	SA_RLEN(sa)	((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
49(SALIGN + 1))
50#define	MAX_SYSCTL_TRY	5
51#define	RTA_MASKS	(RTA_GATEWAY | RTA_IFP | RTA_IFA)
52
53/* FreeBSD uses NET_RT_IFMALIST and RTM_NEWMADDR from <sys/socket.h> */
54/* We can use NET_RT_IFLIST2 and RTM_NEWMADDR2 on Darwin */
55#define DARWIN_COMPAT
56
57#ifdef DARWIN_COMPAT
58#define GIM_SYSCTL_MIB NET_RT_IFLIST2
59#define GIM_RTM_ADDR RTM_NEWMADDR2
60#else
61#define GIM_SYSCTL_MIB NET_RT_IFMALIST
62#define GIM_RTM_ADDR RTM_NEWMADDR
63#endif
64
65int
66getifmaddrs(struct ifmaddrs **pif)
67{
68	int icnt = 1;
69	int dcnt = 0;
70	int ntry = 0;
71	size_t len;
72	size_t needed;
73	int mib[6];
74	int i;
75	char *buf;
76	char *data;
77	char *next;
78	char *p;
79	struct ifma_msghdr2 *ifmam;
80	struct ifmaddrs *ifa, *ift;
81	struct rt_msghdr *rtm;
82	struct sockaddr *sa;
83
84	mib[0] = CTL_NET;
85	mib[1] = PF_ROUTE;
86	mib[2] = 0;             /* protocol */
87	mib[3] = 0;             /* wildcard address family */
88	mib[4] = GIM_SYSCTL_MIB;
89	mib[5] = 0;             /* no flags */
90	do {
91		if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
92			return (-1);
93		if ((buf = malloc(needed)) == NULL)
94			return (-1);
95		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
96			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
97				free(buf);
98				return (-1);
99			}
100			free(buf);
101			buf = NULL;
102		}
103	} while (buf == NULL);
104
105	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
106		rtm = (struct rt_msghdr *)(void *)next;
107		if (rtm->rtm_version != RTM_VERSION)
108			continue;
109		switch (rtm->rtm_type) {
110			case GIM_RTM_ADDR:
111				ifmam = (struct ifma_msghdr2 *)(void *)rtm;
112				if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
113					break;
114				icnt++;
115				p = (char *)(ifmam + 1);
116				for (i = 0; i < RTAX_MAX; i++) {
117					if ((RTA_MASKS & ifmam->ifmam_addrs &
118						 (1 << i)) == 0)
119						continue;
120					sa = (struct sockaddr *)(void *)p;
121					len = SA_RLEN(sa);
122					dcnt += len;
123					p += len;
124				}
125				break;
126		}
127	}
128
129	data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
130	if (data == NULL) {
131		free(buf);
132		return (-1);
133	}
134
135	ifa = (struct ifmaddrs *)(void *)data;
136	data += sizeof(struct ifmaddrs) * icnt;
137
138	memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
139	ift = ifa;
140
141	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
142		rtm = (struct rt_msghdr *)(void *)next;
143		if (rtm->rtm_version != RTM_VERSION)
144			continue;
145
146		switch (rtm->rtm_type) {
147			case GIM_RTM_ADDR:
148				ifmam = (struct ifma_msghdr2 *)(void *)rtm;
149				if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
150					break;
151
152				p = (char *)(ifmam + 1);
153				for (i = 0; i < RTAX_MAX; i++) {
154					if ((RTA_MASKS & ifmam->ifmam_addrs &
155						 (1 << i)) == 0)
156						continue;
157					sa = (struct sockaddr *)(void *)p;
158					len = SA_RLEN(sa);
159					switch (i) {
160						case RTAX_GATEWAY:
161							ift->ifma_lladdr =
162							(struct sockaddr *)(void *)data;
163							memcpy(data, p, len);
164							data += len;
165							break;
166
167						case RTAX_IFP:
168							ift->ifma_name =
169							(struct sockaddr *)(void *)data;
170							memcpy(data, p, len);
171							data += len;
172							break;
173
174						case RTAX_IFA:
175							ift->ifma_addr =
176							(struct sockaddr *)(void *)data;
177							memcpy(data, p, len);
178							data += len;
179							break;
180
181						default:
182							data += len;
183							break;
184					}
185					p += len;
186				}
187				ift->ifma_next = ift + 1;
188				ift = ift->ifma_next;
189				break;
190		}
191	}
192
193	free(buf);
194
195	if (ift > ifa) {
196		ift--;
197		ift->ifma_next = NULL;
198		*pif = ifa;
199	} else {
200		*pif = NULL;
201		free(ifa);
202	}
203	return (0);
204}
205
206void
207freeifmaddrs(struct ifmaddrs *ifmp)
208{
209	free(ifmp);
210}
211