xref: /netbsd-src/usr.sbin/rpcbind/util.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: util.c,v 1.21 2017/08/16 08:44:40 christos Exp $	*/
2 /* $FreeBSD: head/usr.sbin/rpcbind/util.c 300973 2016-05-29 20:28:01Z ngie $ */
3 
4 /*-
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Frank van der Linden.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
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 THE FOUNDATION 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/types.h>
34 #include <sys/socket.h>
35 #include <sys/queue.h>
36 #include <net/if.h>
37 #include <netinet/in.h>
38 #include <assert.h>
39 #include <ifaddrs.h>
40 #include <poll.h>
41 #include <rpc/rpc.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <netdb.h>
47 #include <netconfig.h>
48 #include <stdio.h>
49 #include <arpa/inet.h>
50 #include <err.h>
51 
52 #ifdef RPCBIND_RUMP
53 #include <rump/rump.h>
54 #include <rump/rump_syscalls.h>
55 #endif
56 
57 #include "rpcbind.h"
58 
59 static struct sockaddr_in *local_in4;
60 #ifdef INET6
61 static struct sockaddr_in6 *local_in6;
62 #endif
63 
64 static int bitmaskcmp(void *, void *, void *, int);
65 
66 /*
67  * For all bits set in "mask", compare the corresponding bits in
68  * "dst" and "src", and see if they match. Returns 0 if the addresses
69  * match.
70  */
71 static int
72 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
73 {
74 	int i, j;
75 	u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
76 	u_int8_t bitmask;
77 
78 	for (i = 0; i < bytelen; i++) {
79 		for (j = 0; j < 8; j++) {
80 			bitmask = 1 << j;
81 			if (!(netmask[i] & bitmask))
82 				continue;
83 			if ((p1[i] & bitmask) != (p2[i] & bitmask))
84 				return 1;
85 		}
86 	}
87 
88 	return 0;
89 }
90 
91 char *
92 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
93     const char *netid)
94 {
95 	struct ifaddrs *ifap, *ifp, *bestif;
96 #ifdef INET6
97 	struct sockaddr_in6 *servsin6, *sin6mask, *clntsin6, *ifsin6, *realsin6;
98 	struct sockaddr_in6 *newsin6;
99 #endif
100 	struct sockaddr_in *servsin, *sinmask, *clntsin, *newsin, *ifsin;
101 	struct netbuf *serv_nbp, *clnt_nbp = NULL, tbuf;
102 	struct sockaddr *serv_sa;
103 	struct sockaddr *clnt_sa;
104 	struct sockaddr_storage ss;
105 	struct netconfig *nconf;
106 	struct sockaddr *clnt = caller->buf;
107 	char *ret = NULL;
108 
109 #ifdef INET6
110 	servsin6 = ifsin6 = newsin6 = NULL;	/* XXXGCC -Wuninitialized */
111 #endif
112 	servsin = newsin = NULL;		/* XXXGCC -Wuninitialized */
113 
114 #ifdef RPCBIND_DEBUG
115 	if (debugging)
116 		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
117 		    clnt_uaddr, netid);
118 #endif
119 	nconf = getnetconfigent(netid);
120 	if (nconf == NULL)
121 		return NULL;
122 
123 	/*
124 	 * Local merge, just return a duplicate.
125 	 */
126 	if (clnt_uaddr != NULL && strncmp(clnt_uaddr, "0.0.0.0.", 8) == 0)
127 		return strdup(clnt_uaddr);
128 
129 	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
130 	if (serv_nbp == NULL)
131 		return NULL;
132 
133 	serv_sa = (struct sockaddr *)serv_nbp->buf;
134 	if (clnt_uaddr != NULL) {
135 		clnt_nbp = uaddr2taddr(nconf, clnt_uaddr);
136 		if (clnt_nbp == NULL) {
137 			free(serv_nbp);
138 			return NULL;
139 		}
140 		clnt_sa = (struct sockaddr *)clnt_nbp->buf;
141 		if (clnt_sa->sa_family == AF_LOCAL) {
142 			free(serv_nbp);
143 			free(clnt_nbp);
144 			free(clnt_sa);
145 			return strdup(serv_uaddr);
146 		}
147 	} else {
148 		clnt_sa = (struct sockaddr *)
149 		    malloc(sizeof (struct sockaddr_storage));
150 		memcpy(clnt_sa, clnt, clnt->sa_len);
151 	}
152 
153 	if (getifaddrs(&ifp) < 0) {
154 		free(serv_nbp);
155 		free(clnt_sa);
156 		if (clnt_nbp != NULL)
157 			free(clnt_nbp);
158 		return 0;
159 	}
160 
161 	/*
162 	 * Loop through all interfaces. For each interface, see if the
163 	 * network portion of its address is equal to that of the client.
164 	 * If so, we have found the interface that we want to use.
165 	 */
166 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
167 		if (ifap->ifa_addr->sa_family != clnt->sa_family ||
168 		    !(ifap->ifa_flags & IFF_UP))
169 			continue;
170 
171 		switch (clnt->sa_family) {
172 		case AF_INET:
173 			/*
174 			 * realsin: address that recvfrom gave us.
175 			 * ifsin: address of interface being examined.
176 			 * clntsin: address that client want us to contact
177 			 *           it on
178 			 * servsin: local address of RPC service.
179 			 * sinmask: netmask of this interface
180 			 * newsin: initially a copy of clntsin, eventually
181 			 *         the merged address
182 			 */
183 			servsin = (struct sockaddr_in *)serv_sa;
184 			clntsin = (struct sockaddr_in *)clnt_sa;
185 			sinmask = (struct sockaddr_in *)ifap->ifa_netmask;
186 			newsin = (struct sockaddr_in *)&ss;
187 			ifsin = (struct sockaddr_in *)ifap->ifa_addr;
188 			if (!bitmaskcmp(&ifsin->sin_addr, &clntsin->sin_addr,
189 			    &sinmask->sin_addr, sizeof (struct in_addr))) {
190 				goto found;
191 			}
192 			break;
193 #ifdef INET6
194 		case AF_INET6:
195 			/*
196 			 * realsin6: address that recvfrom gave us.
197 			 * ifsin6: address of interface being examined.
198 			 * clntsin6: address that client want us to contact
199 			 *           it on
200 			 * servsin6: local address of RPC service.
201 			 * sin6mask: netmask of this interface
202 			 * newsin6: initially a copy of clntsin, eventually
203 			 *          the merged address
204 			 *
205 			 * For v6 link local addresses, if the client contacted
206 			 * us via a link-local address, and wants us to reply
207 			 * to one, use the scope id to see which one.
208 			 */
209 			realsin6 = (struct sockaddr_in6 *)clnt;
210 			ifsin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
211 			inet6_getscopeid(ifsin6, 1);
212 			clntsin6 = (struct sockaddr_in6 *)clnt_sa;
213 			servsin6 = (struct sockaddr_in6 *)serv_sa;
214 			sin6mask = (struct sockaddr_in6 *)ifap->ifa_netmask;
215 			newsin6 = (struct sockaddr_in6 *)&ss;
216 			if (IN6_IS_ADDR_LINKLOCAL(&ifsin6->sin6_addr) &&
217 			    IN6_IS_ADDR_LINKLOCAL(&realsin6->sin6_addr) &&
218 			    IN6_IS_ADDR_LINKLOCAL(&clntsin6->sin6_addr)) {
219 				if (ifsin6->sin6_scope_id !=
220 				    realsin6->sin6_scope_id)
221 					continue;
222 				goto found;
223 			}
224 			if (!bitmaskcmp(&ifsin6->sin6_addr,
225 			    &clntsin6->sin6_addr, &sin6mask->sin6_addr,
226 			    sizeof (struct in6_addr)))
227 				goto found;
228 			break;
229 #endif
230 		default:
231 			goto freeit;
232 		}
233 	}
234 	/*
235 	 * Didn't find anything. Get the first possibly useful interface,
236 	 * preferring "normal" interfaces to point-to-point and loopback
237 	 * ones.
238 	 */
239 	bestif = NULL;
240 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
241 		if (ifap->ifa_addr->sa_family != clnt->sa_family ||
242 		    !(ifap->ifa_flags & IFF_UP))
243 			continue;
244 		if (!(ifap->ifa_flags & IFF_LOOPBACK) &&
245 		    !(ifap->ifa_flags & IFF_POINTOPOINT)) {
246 			bestif = ifap;
247 			break;
248 		}
249 		if (bestif == NULL)
250 			bestif = ifap;
251 		else if ((bestif->ifa_flags & IFF_LOOPBACK) &&
252 		    !(ifap->ifa_flags & IFF_LOOPBACK))
253 			bestif = ifap;
254 	}
255 	ifap = bestif;
256 found:
257 	switch (clnt->sa_family) {
258 	case AF_INET:
259 		memcpy(newsin, ifap->ifa_addr, clnt_sa->sa_len);
260 		newsin->sin_port = servsin->sin_port;
261 		tbuf.len = clnt_sa->sa_len;
262 		tbuf.maxlen = sizeof (struct sockaddr_storage);
263 		tbuf.buf = newsin;
264 		break;
265 #ifdef INET6
266 	case AF_INET6:
267 		assert(newsin6);
268 		memcpy(newsin6, ifsin6, clnt_sa->sa_len);
269 		newsin6->sin6_port = servsin6->sin6_port;
270 		tbuf.maxlen = sizeof (struct sockaddr_storage);
271 		tbuf.len = clnt_sa->sa_len;
272 		tbuf.buf = newsin6;
273 		break;
274 #endif
275 	default:
276 		goto freeit;
277 	}
278 	if (ifap != NULL)
279 		ret = taddr2uaddr(nconf, &tbuf);
280 freeit:
281 	freenetconfigent(nconf);
282 	free(serv_sa);
283 	free(serv_nbp);
284 	if (clnt_sa != NULL)
285 		free(clnt_sa);
286 	if (clnt_nbp != NULL)
287 		free(clnt_nbp);
288 	freeifaddrs(ifp);
289 
290 #ifdef RPCBIND_DEBUG
291 	if (debugging)
292 		fprintf(stderr, "addrmerge: returning %s\n", ret);
293 #endif
294 	return ret;
295 }
296 
297 void
298 network_init()
299 {
300 #ifdef INET6
301 	struct ifaddrs *ifap, *ifp;
302 	struct ipv6_mreq mreq6;
303 	unsigned int ifindex;
304 	int s;
305 #endif
306 	int ecode;
307 	struct addrinfo hints, *res;
308 
309 	memset(&hints, 0, sizeof hints);
310 	hints.ai_family = AF_INET;
311 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
312 		if (debugging)
313 			fprintf(stderr, "can't get local ip4 address: %s\n",
314 			    gai_strerror(ecode));
315 	} else {
316 		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
317 		if (local_in4 == NULL) {
318 			if (debugging)
319 				fprintf(stderr, "can't alloc local ip4 addr\n");
320 		}
321 		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
322 	}
323 
324 #ifdef INET6
325 	hints.ai_family = AF_INET6;
326 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
327 		if (debugging)
328 			fprintf(stderr, "can't get local ip6 address: %s\n",
329 			    gai_strerror(ecode));
330 	} else {
331 		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
332 		if (local_in6 == NULL) {
333 			if (debugging)
334 				fprintf(stderr, "can't alloc local ip6 addr\n");
335 		}
336 		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
337 	}
338 
339 	/*
340 	 * Now join the RPC ipv6 multicast group on all interfaces.
341 	 */
342 	if (getifaddrs(&ifp) < 0)
343 		return;
344 
345 	mreq6.ipv6mr_interface = 0;
346 	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
347 
348 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
349 
350 	/*
351 	 * Loop through all interfaces. For each interface, see if the
352 	 * network portion of its address is equal to that of the client.
353 	 * If so, we have found the interface that we want to use.
354 	 */
355 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
356 		if (ifap->ifa_addr->sa_family != AF_INET6 ||
357 		    !(ifap->ifa_flags & IFF_MULTICAST))
358 			continue;
359 		ifindex = if_nametoindex(ifap->ifa_name);
360 		if (ifindex == mreq6.ipv6mr_interface)
361 			/*
362 			 * Already did this one.
363 			 */
364 			continue;
365 		mreq6.ipv6mr_interface = ifindex;
366 		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
367 		    sizeof mreq6) < 0)
368 			if (debugging)
369 				warn("setsockopt v6 multicast");
370 	}
371 	freeifaddrs(ifp);
372 #endif
373 
374 	/* close(s); */
375 }
376 
377 struct sockaddr *
378 local_sa(int af)
379 {
380 	switch (af) {
381 	case AF_INET:
382 		return (struct sockaddr *)local_in4;
383 #ifdef INET6
384 	case AF_INET6:
385 		return (struct sockaddr *)local_in6;
386 #endif
387 	default:
388 		return NULL;
389 	}
390 }
391