174462Salfred/*
274462Salfred * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
374462Salfred * $FreeBSD: stable/11/usr.sbin/rpcbind/util.c 330449 2018-03-05 07:26:05Z eadler $
474462Salfred */
574462Salfred
674462Salfred/*-
7330449Seadler * SPDX-License-Identifier: BSD-2-Clause-NetBSD
8330449Seadler *
974462Salfred * Copyright (c) 2000 The NetBSD Foundation, Inc.
1074462Salfred * All rights reserved.
1174462Salfred *
1274462Salfred * This code is derived from software contributed to The NetBSD Foundation
1374462Salfred * by Frank van der Linden.
1474462Salfred *
1574462Salfred * Redistribution and use in source and binary forms, with or without
1674462Salfred * modification, are permitted provided that the following conditions
1774462Salfred * are met:
1874462Salfred * 1. Redistributions of source code must retain the above copyright
1974462Salfred *    notice, this list of conditions and the following disclaimer.
2074462Salfred * 2. Redistributions in binary form must reproduce the above copyright
2174462Salfred *    notice, this list of conditions and the following disclaimer in the
2274462Salfred *    documentation and/or other materials provided with the distribution.
2374462Salfred *
2474462Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2574462Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2674462Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2774462Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2874462Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2974462Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3074462Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3174462Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3274462Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3374462Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3474462Salfred * POSSIBILITY OF SUCH DAMAGE.
3574462Salfred */
3674462Salfred
3774462Salfred#include <sys/types.h>
3874462Salfred#include <sys/socket.h>
3974462Salfred#include <sys/queue.h>
4074462Salfred#include <net/if.h>
4174462Salfred#include <netinet/in.h>
4274462Salfred#include <ifaddrs.h>
4374462Salfred#include <sys/poll.h>
4474462Salfred#include <rpc/rpc.h>
4574462Salfred#include <errno.h>
4674462Salfred#include <stdlib.h>
4774462Salfred#include <string.h>
4874462Salfred#include <unistd.h>
4974462Salfred#include <netdb.h>
5074462Salfred#include <netconfig.h>
5174462Salfred#include <stdio.h>
5274462Salfred#include <arpa/inet.h>
5374462Salfred
5474462Salfred#include "rpcbind.h"
5574462Salfred
5674462Salfredstatic struct sockaddr_in *local_in4;
5774462Salfred#ifdef INET6
5874462Salfredstatic struct sockaddr_in6 *local_in6;
5974462Salfred#endif
6074462Salfred
61293229Sasomersstatic int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
6274462Salfred
6374462Salfred/*
6474462Salfred * For all bits set in "mask", compare the corresponding bits in
6579720Siedowse * "dst" and "src", and see if they match. Returns 0 if the addresses
6679720Siedowse * match.
6774462Salfred */
6874462Salfredstatic int
69293229Sasomersbitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
7074462Salfred{
7179720Siedowse	int i;
72293229Sasomers	u_int8_t *p1, *p2, *netmask;
73293229Sasomers	int bytelen;
7474462Salfred
75293229Sasomers	if (dst->sa_family != src->sa_family ||
76293229Sasomers	    dst->sa_family != mask->sa_family)
77293229Sasomers		return (1);
78293229Sasomers
79293229Sasomers	switch (dst->sa_family) {
80293229Sasomers	case AF_INET:
81293229Sasomers		p1 = (uint8_t*) &SA2SINADDR(dst);
82293229Sasomers		p2 = (uint8_t*) &SA2SINADDR(src);
83293229Sasomers		netmask = (uint8_t*) &SA2SINADDR(mask);
84293229Sasomers		bytelen = sizeof(struct in_addr);
85293229Sasomers		break;
86293229Sasomers#ifdef INET6
87293229Sasomers	case AF_INET6:
88293229Sasomers		p1 = (uint8_t*) &SA2SIN6ADDR(dst);
89293229Sasomers		p2 = (uint8_t*) &SA2SIN6ADDR(src);
90293229Sasomers		netmask = (uint8_t*) &SA2SIN6ADDR(mask);
91293229Sasomers		bytelen = sizeof(struct in6_addr);
92293229Sasomers		break;
93293229Sasomers#endif
94293229Sasomers	default:
95293229Sasomers		return (1);
96293229Sasomers	}
97293229Sasomers
9879720Siedowse	for (i = 0; i < bytelen; i++)
9979720Siedowse		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
10079720Siedowse			return (1);
10179720Siedowse	return (0);
10274462Salfred}
10374462Salfred
10474462Salfred/*
10578705Siedowse * Find a server address that can be used by `caller' to contact
10678705Siedowse * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
10778705Siedowse * non-NULL, it is used instead of `caller' as a hint suggesting
10878705Siedowse * the best address (e.g. the `r_addr' field of an rpc, which
10978705Siedowse * contains the rpcbind server address that the caller used).
11078705Siedowse *
11178705Siedowse * Returns the best server address as a malloc'd "universal address"
11278705Siedowse * string which should be freed by the caller. On error, returns NULL.
11378705Siedowse */
11474462Salfredchar *
115293229Sasomersaddrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
116293229Sasomers	  const char *netid)
11774462Salfred{
11878705Siedowse	struct ifaddrs *ifap, *ifp = NULL, *bestif;
11978705Siedowse	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
12078705Siedowse	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
12174462Salfred	struct sockaddr_storage ss;
12274462Salfred	struct netconfig *nconf;
123293229Sasomers	char *caller_uaddr = NULL;
124300867Sngie#ifdef ND_DEBUG
125293229Sasomers	const char *hint_uaddr = NULL;
126300867Sngie#endif
12774462Salfred	char *ret = NULL;
128293229Sasomers	int bestif_goodness;
12974462Salfred
13074462Salfred#ifdef ND_DEBUG
13174462Salfred	if (debugging)
13274462Salfred		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
13378705Siedowse		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
13474462Salfred#endif
13578705Siedowse	caller_sa = caller->buf;
13678705Siedowse	if ((nconf = rpcbind_get_conf(netid)) == NULL)
13778705Siedowse		goto freeit;
13878705Siedowse	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
13978705Siedowse		goto freeit;
14074462Salfred
14174462Salfred	/*
14278705Siedowse	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
14378705Siedowse	 * address family is different from that of the caller.
14474462Salfred	 */
14578705Siedowse	hint_sa = NULL;
14674462Salfred	if (clnt_uaddr != NULL) {
147300867Sngie#ifdef ND_DEBUG
14878705Siedowse		hint_uaddr = clnt_uaddr;
149300867Sngie#endif
15078705Siedowse		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
15178705Siedowse			goto freeit;
15278705Siedowse		hint_sa = hint_nbp->buf;
15374462Salfred	}
15478705Siedowse	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
155300867Sngie#ifdef ND_DEBUG
15678705Siedowse		hint_uaddr = caller_uaddr;
157300867Sngie#endif
15878705Siedowse		hint_sa = caller->buf;
15978705Siedowse	}
16074462Salfred
16178705Siedowse#ifdef ND_DEBUG
16278705Siedowse	if (debugging)
16378705Siedowse		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
16478705Siedowse#endif
16578705Siedowse	/* Local caller, just return the server address. */
16678705Siedowse	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
16778705Siedowse	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
16878705Siedowse		ret = strdup(serv_uaddr);
16978705Siedowse		goto freeit;
17076037Siedowse	}
17174462Salfred
17278705Siedowse	if (getifaddrs(&ifp) < 0)
17378705Siedowse		goto freeit;
17478705Siedowse
17574462Salfred	/*
176293229Sasomers	 * Loop through all interface addresses.  We are listening to an address
177293229Sasomers	 * if any of the following are true:
178293229Sasomers	 * a) It's a loopback address
179293229Sasomers	 * b) It was specified with the -h command line option
180293229Sasomers	 * c) There were no -h command line options.
181293229Sasomers	 *
182293229Sasomers	 * Among addresses on which we are listening, choose in order of
183293229Sasomers	 * preference an address that is:
184293229Sasomers	 *
185293229Sasomers	 * a) Equal to the hint
186293229Sasomers	 * b) A link local address with the same scope ID as the client's
187293229Sasomers	 *    address, if the client's address is also link local
188293229Sasomers	 * c) An address on the same subnet as the client's address
189293229Sasomers	 * d) A non-localhost, non-p2p address
190293229Sasomers	 * e) Any usable address
19174462Salfred	 */
19278705Siedowse	bestif = NULL;
193293229Sasomers	bestif_goodness = 0;
19474462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
19578705Siedowse		ifsa = ifap->ifa_addr;
19678705Siedowse		ifmasksa = ifap->ifa_netmask;
19778705Siedowse
198293229Sasomers		/* Skip addresses where we don't listen */
19978705Siedowse		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
20074462Salfred		    !(ifap->ifa_flags & IFF_UP))
20174462Salfred			continue;
20274462Salfred
203203710Simp		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
204203710Simp			continue;
205203710Simp
206293229Sasomers		if ((hint_sa->sa_family == AF_INET) &&
207293229Sasomers		    ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
208293229Sasomers		      ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
209293229Sasomers			const int goodness = 4;
210293229Sasomers
211293229Sasomers			bestif_goodness = goodness;
212293229Sasomers			bestif = ifap;
213293229Sasomers			goto found;
214293229Sasomers		}
21574462Salfred#ifdef INET6
216293229Sasomers		if ((hint_sa->sa_family == AF_INET6) &&
217293229Sasomers		    (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
218293229Sasomers				 &((struct sockaddr_in6*)ifsa)->sin6_addr,
219293229Sasomers				 sizeof(struct in6_addr))) &&
220293229Sasomers		    (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
221293229Sasomers		    (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
222293229Sasomers			const int goodness = 4;
223293229Sasomers
224293229Sasomers			bestif_goodness = goodness;
225293229Sasomers			bestif = ifap;
226293229Sasomers			goto found;
227293229Sasomers		}
228293229Sasomers		if (hint_sa->sa_family == AF_INET6) {
22974462Salfred			/*
23078705Siedowse			 * For v6 link local addresses, if the caller is on
23178705Siedowse			 * a link-local address then use the scope id to see
23278705Siedowse			 * which one.
23374462Salfred			 */
23478705Siedowse			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
23578705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
23678705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
23778705Siedowse				if (SA2SIN6(ifsa)->sin6_scope_id ==
23878705Siedowse				    SA2SIN6(caller_sa)->sin6_scope_id) {
239293229Sasomers					const int goodness = 3;
240293229Sasomers
241293229Sasomers					if (bestif_goodness < goodness) {
242293229Sasomers						bestif = ifap;
243293229Sasomers						bestif_goodness = goodness;
244293229Sasomers					}
24578705Siedowse				}
246293229Sasomers			}
247293229Sasomers		}
248293229Sasomers#endif /* INET6 */
249293229Sasomers		if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
250293229Sasomers			const int goodness = 2;
251293229Sasomers
252293229Sasomers			if (bestif_goodness < goodness) {
25378705Siedowse				bestif = ifap;
254293229Sasomers				bestif_goodness = goodness;
25574462Salfred			}
25674462Salfred		}
257293229Sasomers		if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
258293229Sasomers			const int goodness = 1;
25978705Siedowse
260293229Sasomers			if (bestif_goodness < goodness) {
261293229Sasomers				bestif = ifap;
262293229Sasomers				bestif_goodness = goodness;
263293229Sasomers			}
264293229Sasomers		}
265293229Sasomers		if (bestif == NULL)
26674462Salfred			bestif = ifap;
26774462Salfred	}
26878705Siedowse	if (bestif == NULL)
26978705Siedowse		goto freeit;
27078705Siedowse
27174462Salfredfound:
27278705Siedowse	/*
273218909Sbrucec	 * Construct the new address using the address from
27478705Siedowse	 * `bestif', and the port number from `serv_uaddr'.
27578705Siedowse	 */
27678705Siedowse	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
27778705Siedowse	if (serv_nbp == NULL)
27878705Siedowse		goto freeit;
27978705Siedowse	serv_sa = serv_nbp->buf;
28078705Siedowse
28178705Siedowse	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
28278705Siedowse	switch (ss.ss_family) {
28376037Siedowse	case AF_INET:
28478705Siedowse		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
28578705Siedowse		break;
28676037Siedowse#ifdef INET6
28776037Siedowse	case AF_INET6:
28878705Siedowse		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
28976037Siedowse		break;
29076037Siedowse#endif
29176037Siedowse	}
29278705Siedowse	tbuf.len = ss.ss_len;
29378705Siedowse	tbuf.maxlen = sizeof(ss);
29478705Siedowse	tbuf.buf = &ss;
29578705Siedowse	ret = taddr2uaddr(nconf, &tbuf);
29678705Siedowse
29774462Salfredfreeit:
298300945Sngie	free(caller_uaddr);
29978705Siedowse	if (hint_nbp != NULL) {
30078705Siedowse		free(hint_nbp->buf);
30178705Siedowse		free(hint_nbp);
30278705Siedowse	}
30378705Siedowse	if (serv_nbp != NULL) {
30478705Siedowse		free(serv_nbp->buf);
30578705Siedowse		free(serv_nbp);
30678705Siedowse	}
30778705Siedowse	if (ifp != NULL)
30878705Siedowse		freeifaddrs(ifp);
30974462Salfred
31074462Salfred#ifdef ND_DEBUG
31174462Salfred	if (debugging)
31274462Salfred		fprintf(stderr, "addrmerge: returning %s\n", ret);
31374462Salfred#endif
31474462Salfred	return ret;
31574462Salfred}
31674462Salfred
31774462Salfredvoid
318224001Sdelphijnetwork_init(void)
31974462Salfred{
32074462Salfred#ifdef INET6
32174462Salfred	struct ifaddrs *ifap, *ifp;
32274462Salfred	struct ipv6_mreq mreq6;
323104592Salfred	unsigned int ifindex;
324104592Salfred	int s;
32574462Salfred#endif
32674462Salfred	int ecode;
32774462Salfred	struct addrinfo hints, *res;
32874462Salfred
32974462Salfred	memset(&hints, 0, sizeof hints);
33074462Salfred	hints.ai_family = AF_INET;
33174462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
33274462Salfred		if (debugging)
33374462Salfred			fprintf(stderr, "can't get local ip4 address: %s\n",
33474462Salfred			    gai_strerror(ecode));
33574462Salfred	} else {
33674462Salfred		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
33774462Salfred		if (local_in4 == NULL) {
33874462Salfred			if (debugging)
33974462Salfred				fprintf(stderr, "can't alloc local ip4 addr\n");
340300932Sngie			exit(1);
34174462Salfred		}
34274462Salfred		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
343300972Smarkj		freeaddrinfo(res);
34474462Salfred	}
34574462Salfred
34674462Salfred#ifdef INET6
34774462Salfred	hints.ai_family = AF_INET6;
34874462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
34974462Salfred		if (debugging)
35074462Salfred			fprintf(stderr, "can't get local ip6 address: %s\n",
35174462Salfred			    gai_strerror(ecode));
35274462Salfred	} else {
35374462Salfred		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
35474462Salfred		if (local_in6 == NULL) {
35574462Salfred			if (debugging)
35674462Salfred				fprintf(stderr, "can't alloc local ip6 addr\n");
357300932Sngie			exit(1);
35874462Salfred		}
35974462Salfred		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
360300972Smarkj		freeaddrinfo(res);
36174462Salfred	}
36274462Salfred
36374462Salfred	/*
36474462Salfred	 * Now join the RPC ipv6 multicast group on all interfaces.
36574462Salfred	 */
36674462Salfred	if (getifaddrs(&ifp) < 0)
36774462Salfred		return;
36874462Salfred
36974462Salfred	mreq6.ipv6mr_interface = 0;
37074462Salfred	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
37174462Salfred
37274462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
373300932Sngie	if (s == -1) {
374300932Sngie		if (debugging)
375300932Sngie			fprintf(stderr, "couldn't create ip6 socket");
376300973Sngie		goto done_inet6;
377300932Sngie	}
37874462Salfred
37974462Salfred	/*
38079720Siedowse	 * Loop through all interfaces. For each IPv6 multicast-capable
38179720Siedowse	 * interface, join the RPC multicast group on that interface.
38274462Salfred	 */
38374462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
38474462Salfred		if (ifap->ifa_addr->sa_family != AF_INET6 ||
38574462Salfred		    !(ifap->ifa_flags & IFF_MULTICAST))
38674462Salfred			continue;
38774462Salfred		ifindex = if_nametoindex(ifap->ifa_name);
38874462Salfred		if (ifindex == mreq6.ipv6mr_interface)
38974462Salfred			/*
39074462Salfred			 * Already did this one.
39174462Salfred			 */
39274462Salfred			continue;
39374462Salfred		mreq6.ipv6mr_interface = ifindex;
39474462Salfred		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
39574462Salfred		    sizeof mreq6) < 0)
39674462Salfred			if (debugging)
39774462Salfred				perror("setsockopt v6 multicast");
39874462Salfred	}
399300973Sngiedone_inet6:
400300934Sngie	freeifaddrs(ifp);
40174462Salfred#endif
40274462Salfred
40374462Salfred	/* close(s); */
40474462Salfred}
40574462Salfred
40674462Salfredstruct sockaddr *
40774462Salfredlocal_sa(int af)
40874462Salfred{
40974462Salfred	switch (af) {
41074462Salfred	case AF_INET:
41174462Salfred		return (struct sockaddr *)local_in4;
41274462Salfred#ifdef INET6
41374462Salfred	case AF_INET6:
41474462Salfred		return (struct sockaddr *)local_in6;
41574462Salfred#endif
41674462Salfred	default:
41774462Salfred		return NULL;
41874462Salfred	}
41974462Salfred}
420