xref: /freebsd/usr.sbin/rpcbind/util.c (revision 22d7dd834bc5cd189810e414701e3ad1e98102e4)
1 /*
2  * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2000 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Frank van der Linden.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/queue.h>
39 #include <net/if.h>
40 #include <netinet/in.h>
41 #include <ifaddrs.h>
42 #include <sys/poll.h>
43 #include <rpc/rpc.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <netdb.h>
47 #include <netconfig.h>
48 #include <stdio.h>
49 #include <arpa/inet.h>
50 
51 #include "rpcbind.h"
52 
53 static struct sockaddr_in *local_in4;
54 #ifdef INET6
55 static struct sockaddr_in6 *local_in6;
56 #endif
57 
58 static int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
59 
60 /*
61  * For all bits set in "mask", compare the corresponding bits in
62  * "dst" and "src", and see if they match. Returns 0 if the addresses
63  * match.
64  */
65 static int
66 bitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
67 {
68 	int i;
69 	u_int8_t *p1, *p2, *netmask;
70 	int bytelen;
71 
72 	if (dst->sa_family != src->sa_family ||
73 	    dst->sa_family != mask->sa_family)
74 		return (1);
75 
76 	switch (dst->sa_family) {
77 	case AF_INET:
78 		p1 = (uint8_t*) &SA2SINADDR(dst);
79 		p2 = (uint8_t*) &SA2SINADDR(src);
80 		netmask = (uint8_t*) &SA2SINADDR(mask);
81 		bytelen = sizeof(struct in_addr);
82 		break;
83 #ifdef INET6
84 	case AF_INET6:
85 		p1 = (uint8_t*) &SA2SIN6ADDR(dst);
86 		p2 = (uint8_t*) &SA2SIN6ADDR(src);
87 		netmask = (uint8_t*) &SA2SIN6ADDR(mask);
88 		bytelen = sizeof(struct in6_addr);
89 		break;
90 #endif
91 	default:
92 		return (1);
93 	}
94 
95 	for (i = 0; i < bytelen; i++)
96 		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
97 			return (1);
98 	return (0);
99 }
100 
101 /*
102  * Find a server address that can be used by `caller' to contact
103  * the local service specified by `serv_uaddr'. If `contct_uaddr' is
104  * non-NULL, it is used instead of `caller' as a hint suggesting
105  * the best address (e.g. the `r_addr' field of an rpc, which
106  * contains the rpcbind server address that the caller used).
107  *
108  * Returns the best server address as a malloc'd "universal address"
109  * string which should be freed by the caller. On error, returns NULL.
110  */
111 char *
112 addrmerge(struct netbuf *caller, const char *serv_uaddr,
113     const char *contct_uaddr, const char *netid)
114 {
115 	struct ifaddrs *ifap, *ifp = NULL, *bestif;
116 	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
117 	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
118 	struct sockaddr_storage ss;
119 	struct netconfig *nconf;
120 	char *caller_uaddr = NULL;
121 #ifdef ND_DEBUG
122 	const char *hint_uaddr = NULL;
123 #endif
124 	char *ret = NULL;
125 	int bestif_goodness;
126 
127 #ifdef ND_DEBUG
128 	if (debugging)
129 		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
130 		    contct_uaddr == NULL ? "NULL" : contct_uaddr, netid);
131 #endif
132 	caller_sa = caller->buf;
133 	if ((nconf = rpcbind_get_conf(netid)) == NULL)
134 		goto freeit;
135 	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
136 		goto freeit;
137 
138 	/*
139 	 * Use `contct_uaddr' as the hint if non-NULL, but ignore it if its
140 	 * address family is different from that of the caller.
141 	 */
142 	hint_sa = NULL;
143 	if (contct_uaddr != NULL) {
144 #ifdef ND_DEBUG
145 		hint_uaddr = contct_uaddr;
146 #endif
147 		if ((hint_nbp = uaddr2taddr(nconf, contct_uaddr)) == NULL)
148 			goto freeit;
149 		hint_sa = hint_nbp->buf;
150 	}
151 	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
152 #ifdef ND_DEBUG
153 		hint_uaddr = caller_uaddr;
154 #endif
155 		hint_sa = caller->buf;
156 	}
157 
158 #ifdef ND_DEBUG
159 	if (debugging)
160 		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
161 #endif
162 	/* Local caller, just return the server address. */
163 	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
164 	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
165 		ret = strdup(serv_uaddr);
166 		goto freeit;
167 	}
168 
169 	if (getifaddrs(&ifp) < 0)
170 		goto freeit;
171 
172 	/*
173 	 * Loop through all interface addresses.  We are listening to an address
174 	 * if any of the following are true:
175 	 * a) It's a loopback address
176 	 * b) It was specified with the -h command line option
177 	 * c) There were no -h command line options.
178 	 *
179 	 * Among addresses on which we are listening, choose in order of
180 	 * preference an address that is:
181 	 *
182 	 * a) Equal to the hint
183 	 * b) A link local address with the same scope ID as the client's
184 	 *    address, if the client's address is also link local
185 	 * c) An address on the same subnet as the client's address
186 	 * d) A non-localhost, non-p2p address
187 	 * e) Any usable address
188 	 */
189 	bestif = NULL;
190 	bestif_goodness = 0;
191 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
192 		ifsa = ifap->ifa_addr;
193 		ifmasksa = ifap->ifa_netmask;
194 
195 		/* Skip addresses where we don't listen */
196 		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
197 		    !(ifap->ifa_flags & IFF_UP))
198 			continue;
199 
200 		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
201 			continue;
202 
203 		if ((hint_sa->sa_family == AF_INET) &&
204 		    ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
205 		      ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
206 			const int goodness = 4;
207 
208 			bestif_goodness = goodness;
209 			bestif = ifap;
210 			goto found;
211 		}
212 #ifdef INET6
213 		if ((hint_sa->sa_family == AF_INET6) &&
214 		    (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
215 				 &((struct sockaddr_in6*)ifsa)->sin6_addr,
216 				 sizeof(struct in6_addr))) &&
217 		    (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
218 		    (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
219 			const int goodness = 4;
220 
221 			bestif_goodness = goodness;
222 			bestif = ifap;
223 			goto found;
224 		}
225 		if (hint_sa->sa_family == AF_INET6) {
226 			/*
227 			 * For v6 link local addresses, if the caller is on
228 			 * a link-local address then use the scope id to see
229 			 * which one.
230 			 */
231 			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa))) {
232 				if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
233 				    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa)) &&
234 				    (SA2SIN6(ifsa)->sin6_scope_id ==
235 				     SA2SIN6(caller_sa)->sin6_scope_id)) {
236 					const int goodness = 3;
237 
238 					if (bestif_goodness < goodness) {
239 						bestif = ifap;
240 						bestif_goodness = goodness;
241 					}
242 				} else {
243 					continue;
244 				}
245 			}
246 		}
247 #endif /* INET6 */
248 		if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
249 			const int goodness = 2;
250 
251 			if (bestif_goodness < goodness) {
252 				bestif = ifap;
253 				bestif_goodness = goodness;
254 			}
255 		}
256 		if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
257 			const int goodness = 1;
258 
259 			if (bestif_goodness < goodness) {
260 				bestif = ifap;
261 				bestif_goodness = goodness;
262 			}
263 		}
264 		if (bestif == NULL)
265 			bestif = ifap;
266 	}
267 	if (bestif == NULL)
268 		goto freeit;
269 
270 found:
271 	/*
272 	 * Construct the new address using the address from
273 	 * `bestif', and the port number from `serv_uaddr'.
274 	 */
275 	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
276 	if (serv_nbp == NULL)
277 		goto freeit;
278 	serv_sa = serv_nbp->buf;
279 
280 	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
281 	switch (ss.ss_family) {
282 	case AF_INET:
283 		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
284 		break;
285 #ifdef INET6
286 	case AF_INET6:
287 		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
288 		break;
289 #endif
290 	}
291 	tbuf.len = ss.ss_len;
292 	tbuf.maxlen = sizeof(ss);
293 	tbuf.buf = &ss;
294 	ret = taddr2uaddr(nconf, &tbuf);
295 
296 freeit:
297 	free(caller_uaddr);
298 	if (hint_nbp != NULL) {
299 		free(hint_nbp->buf);
300 		free(hint_nbp);
301 	}
302 	if (serv_nbp != NULL) {
303 		free(serv_nbp->buf);
304 		free(serv_nbp);
305 	}
306 	if (ifp != NULL)
307 		freeifaddrs(ifp);
308 
309 #ifdef ND_DEBUG
310 	if (debugging)
311 		fprintf(stderr, "addrmerge: returning %s\n", ret);
312 #endif
313 	return ret;
314 }
315 
316 void
317 network_init(void)
318 {
319 #ifdef INET6
320 	struct ifaddrs *ifap, *ifp;
321 	struct ipv6_mreq mreq6;
322 	unsigned int ifindex;
323 	int s;
324 #endif
325 	int ecode;
326 	struct addrinfo hints, *res;
327 
328 	memset(&hints, 0, sizeof hints);
329 	hints.ai_family = AF_INET;
330 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
331 		if (debugging)
332 			fprintf(stderr, "can't get local ip4 address: %s\n",
333 			    gai_strerror(ecode));
334 	} else {
335 		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
336 		if (local_in4 == NULL) {
337 			if (debugging)
338 				fprintf(stderr, "can't alloc local ip4 addr\n");
339 			exit(1);
340 		}
341 		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
342 		freeaddrinfo(res);
343 	}
344 
345 #ifdef INET6
346 	hints.ai_family = AF_INET6;
347 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
348 		if (debugging)
349 			fprintf(stderr, "can't get local ip6 address: %s\n",
350 			    gai_strerror(ecode));
351 	} else {
352 		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
353 		if (local_in6 == NULL) {
354 			if (debugging)
355 				fprintf(stderr, "can't alloc local ip6 addr\n");
356 			exit(1);
357 		}
358 		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
359 		freeaddrinfo(res);
360 	}
361 
362 	/*
363 	 * Now join the RPC ipv6 multicast group on all interfaces.
364 	 */
365 	if (getifaddrs(&ifp) < 0)
366 		return;
367 
368 	mreq6.ipv6mr_interface = 0;
369 	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
370 
371 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
372 	if (s == -1) {
373 		if (debugging)
374 			fprintf(stderr, "couldn't create ip6 socket");
375 		goto done_inet6;
376 	}
377 
378 	/*
379 	 * Loop through all interfaces. For each IPv6 multicast-capable
380 	 * interface, join the RPC multicast group on that interface.
381 	 */
382 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
383 		if (ifap->ifa_addr->sa_family != AF_INET6 ||
384 		    !(ifap->ifa_flags & IFF_MULTICAST))
385 			continue;
386 		ifindex = if_nametoindex(ifap->ifa_name);
387 		if (ifindex == mreq6.ipv6mr_interface)
388 			/*
389 			 * Already did this one.
390 			 */
391 			continue;
392 		mreq6.ipv6mr_interface = ifindex;
393 		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
394 		    sizeof mreq6) < 0)
395 			if (debugging)
396 				perror("setsockopt v6 multicast");
397 	}
398 done_inet6:
399 	freeifaddrs(ifp);
400 #endif
401 
402 	/* close(s); */
403 }
404 
405 struct sockaddr *
406 local_sa(int af)
407 {
408 	switch (af) {
409 	case AF_INET:
410 		return (struct sockaddr *)local_in4;
411 #ifdef INET6
412 	case AF_INET6:
413 		return (struct sockaddr *)local_in6;
414 #endif
415 	default:
416 		return NULL;
417 	}
418 }
419