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