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