174462Salfred/*
274462Salfred * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
374462Salfred * $FreeBSD$
474462Salfred */
574462Salfred
674462Salfred/*-
774462Salfred * Copyright (c) 2000 The NetBSD Foundation, Inc.
874462Salfred * All rights reserved.
974462Salfred *
1074462Salfred * This code is derived from software contributed to The NetBSD Foundation
1174462Salfred * by Frank van der Linden.
1274462Salfred *
1374462Salfred * Redistribution and use in source and binary forms, with or without
1474462Salfred * modification, are permitted provided that the following conditions
1574462Salfred * are met:
1674462Salfred * 1. Redistributions of source code must retain the above copyright
1774462Salfred *    notice, this list of conditions and the following disclaimer.
1874462Salfred * 2. Redistributions in binary form must reproduce the above copyright
1974462Salfred *    notice, this list of conditions and the following disclaimer in the
2074462Salfred *    documentation and/or other materials provided with the distribution.
2174462Salfred *
2274462Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2374462Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2474462Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2574462Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2674462Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2774462Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2874462Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2974462Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3074462Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3174462Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3274462Salfred * POSSIBILITY OF SUCH DAMAGE.
3374462Salfred */
3474462Salfred
3574462Salfred#include <sys/types.h>
3674462Salfred#include <sys/socket.h>
3774462Salfred#include <sys/queue.h>
3874462Salfred#include <net/if.h>
3974462Salfred#include <netinet/in.h>
4074462Salfred#include <ifaddrs.h>
4174462Salfred#include <sys/poll.h>
4274462Salfred#include <rpc/rpc.h>
4374462Salfred#include <errno.h>
4474462Salfred#include <stdlib.h>
4574462Salfred#include <string.h>
4674462Salfred#include <unistd.h>
4774462Salfred#include <netdb.h>
4874462Salfred#include <netconfig.h>
4974462Salfred#include <stdio.h>
5074462Salfred#include <arpa/inet.h>
5174462Salfred
5274462Salfred#include "rpcbind.h"
5374462Salfred
5474462Salfredstatic struct sockaddr_in *local_in4;
5574462Salfred#ifdef INET6
5674462Salfredstatic struct sockaddr_in6 *local_in6;
5774462Salfred#endif
5874462Salfred
59296994Sasomersstatic int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
6074462Salfred
6174462Salfred/*
6274462Salfred * For all bits set in "mask", compare the corresponding bits in
6379720Siedowse * "dst" and "src", and see if they match. Returns 0 if the addresses
6479720Siedowse * match.
6574462Salfred */
6674462Salfredstatic int
67296994Sasomersbitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
6874462Salfred{
6979720Siedowse	int i;
70296994Sasomers	u_int8_t *p1, *p2, *netmask;
71296994Sasomers	int bytelen;
7274462Salfred
73296994Sasomers	if (dst->sa_family != src->sa_family ||
74296994Sasomers	    dst->sa_family != mask->sa_family)
75296994Sasomers		return (1);
76296994Sasomers
77296994Sasomers	switch (dst->sa_family) {
78296994Sasomers	case AF_INET:
79296994Sasomers		p1 = (uint8_t*) &SA2SINADDR(dst);
80296994Sasomers		p2 = (uint8_t*) &SA2SINADDR(src);
81296994Sasomers		netmask = (uint8_t*) &SA2SINADDR(mask);
82296994Sasomers		bytelen = sizeof(struct in_addr);
83296994Sasomers		break;
84296994Sasomers#ifdef INET6
85296994Sasomers	case AF_INET6:
86296994Sasomers		p1 = (uint8_t*) &SA2SIN6ADDR(dst);
87296994Sasomers		p2 = (uint8_t*) &SA2SIN6ADDR(src);
88296994Sasomers		netmask = (uint8_t*) &SA2SIN6ADDR(mask);
89296994Sasomers		bytelen = sizeof(struct in6_addr);
90296994Sasomers		break;
91296994Sasomers#endif
92296994Sasomers	default:
93296994Sasomers		return (1);
94296994Sasomers	}
95296994Sasomers
9679720Siedowse	for (i = 0; i < bytelen; i++)
9779720Siedowse		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
9879720Siedowse			return (1);
9979720Siedowse	return (0);
10074462Salfred}
10174462Salfred
10274462Salfred/*
10378705Siedowse * Find a server address that can be used by `caller' to contact
10478705Siedowse * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
10578705Siedowse * non-NULL, it is used instead of `caller' as a hint suggesting
10678705Siedowse * the best address (e.g. the `r_addr' field of an rpc, which
10778705Siedowse * contains the rpcbind server address that the caller used).
10878705Siedowse *
10978705Siedowse * Returns the best server address as a malloc'd "universal address"
11078705Siedowse * string which should be freed by the caller. On error, returns NULL.
11178705Siedowse */
11274462Salfredchar *
113296994Sasomersaddrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
114296994Sasomers	  const char *netid)
11574462Salfred{
11678705Siedowse	struct ifaddrs *ifap, *ifp = NULL, *bestif;
11778705Siedowse	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
11878705Siedowse	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
11974462Salfred	struct sockaddr_storage ss;
12074462Salfred	struct netconfig *nconf;
121296994Sasomers	char *caller_uaddr = NULL;
122301636Sngie#ifdef ND_DEBUG
123296994Sasomers	const char *hint_uaddr = NULL;
124301636Sngie#endif
12574462Salfred	char *ret = NULL;
126296994Sasomers	int bestif_goodness;
12774462Salfred
12874462Salfred#ifdef ND_DEBUG
12974462Salfred	if (debugging)
13074462Salfred		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
13178705Siedowse		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
13274462Salfred#endif
13378705Siedowse	caller_sa = caller->buf;
13478705Siedowse	if ((nconf = rpcbind_get_conf(netid)) == NULL)
13578705Siedowse		goto freeit;
13678705Siedowse	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
13778705Siedowse		goto freeit;
13874462Salfred
13974462Salfred	/*
14078705Siedowse	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
14178705Siedowse	 * address family is different from that of the caller.
14274462Salfred	 */
14378705Siedowse	hint_sa = NULL;
14474462Salfred	if (clnt_uaddr != NULL) {
145301636Sngie#ifdef ND_DEBUG
14678705Siedowse		hint_uaddr = clnt_uaddr;
147301636Sngie#endif
14878705Siedowse		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
14978705Siedowse			goto freeit;
15078705Siedowse		hint_sa = hint_nbp->buf;
15174462Salfred	}
15278705Siedowse	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
153301636Sngie#ifdef ND_DEBUG
15478705Siedowse		hint_uaddr = caller_uaddr;
155301636Sngie#endif
15678705Siedowse		hint_sa = caller->buf;
15778705Siedowse	}
15874462Salfred
15978705Siedowse#ifdef ND_DEBUG
16078705Siedowse	if (debugging)
16178705Siedowse		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
16278705Siedowse#endif
16378705Siedowse	/* Local caller, just return the server address. */
16478705Siedowse	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
16578705Siedowse	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
16678705Siedowse		ret = strdup(serv_uaddr);
16778705Siedowse		goto freeit;
16876037Siedowse	}
16974462Salfred
17078705Siedowse	if (getifaddrs(&ifp) < 0)
17178705Siedowse		goto freeit;
17278705Siedowse
17374462Salfred	/*
174296994Sasomers	 * Loop through all interface addresses.  We are listening to an address
175296994Sasomers	 * if any of the following are true:
176296994Sasomers	 * a) It's a loopback address
177296994Sasomers	 * b) It was specified with the -h command line option
178296994Sasomers	 * c) There were no -h command line options.
179296994Sasomers	 *
180296994Sasomers	 * Among addresses on which we are listening, choose in order of
181296994Sasomers	 * preference an address that is:
182296994Sasomers	 *
183296994Sasomers	 * a) Equal to the hint
184296994Sasomers	 * b) A link local address with the same scope ID as the client's
185296994Sasomers	 *    address, if the client's address is also link local
186296994Sasomers	 * c) An address on the same subnet as the client's address
187296994Sasomers	 * d) A non-localhost, non-p2p address
188296994Sasomers	 * e) Any usable address
18974462Salfred	 */
19078705Siedowse	bestif = NULL;
191296994Sasomers	bestif_goodness = 0;
19274462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
19378705Siedowse		ifsa = ifap->ifa_addr;
19478705Siedowse		ifmasksa = ifap->ifa_netmask;
19578705Siedowse
196296994Sasomers		/* Skip addresses where we don't listen */
19778705Siedowse		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
19874462Salfred		    !(ifap->ifa_flags & IFF_UP))
19974462Salfred			continue;
20074462Salfred
201203710Simp		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
202203710Simp			continue;
203203710Simp
204296994Sasomers		if ((hint_sa->sa_family == AF_INET) &&
205296994Sasomers		    ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
206296994Sasomers		      ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
207296994Sasomers			const int goodness = 4;
208296994Sasomers
209296994Sasomers			bestif_goodness = goodness;
210296994Sasomers			bestif = ifap;
211296994Sasomers			goto found;
212296994Sasomers		}
21374462Salfred#ifdef INET6
214296994Sasomers		if ((hint_sa->sa_family == AF_INET6) &&
215296994Sasomers		    (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
216296994Sasomers				 &((struct sockaddr_in6*)ifsa)->sin6_addr,
217296994Sasomers				 sizeof(struct in6_addr))) &&
218296994Sasomers		    (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
219296994Sasomers		    (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
220296994Sasomers			const int goodness = 4;
221296994Sasomers
222296994Sasomers			bestif_goodness = goodness;
223296994Sasomers			bestif = ifap;
224296994Sasomers			goto found;
225296994Sasomers		}
226296994Sasomers		if (hint_sa->sa_family == AF_INET6) {
22774462Salfred			/*
22878705Siedowse			 * For v6 link local addresses, if the caller is on
22978705Siedowse			 * a link-local address then use the scope id to see
23078705Siedowse			 * which one.
23174462Salfred			 */
23278705Siedowse			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
23378705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
23478705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
23578705Siedowse				if (SA2SIN6(ifsa)->sin6_scope_id ==
23678705Siedowse				    SA2SIN6(caller_sa)->sin6_scope_id) {
237296994Sasomers					const int goodness = 3;
238296994Sasomers
239296994Sasomers					if (bestif_goodness < goodness) {
240296994Sasomers						bestif = ifap;
241296994Sasomers						bestif_goodness = goodness;
242296994Sasomers					}
24378705Siedowse				}
244296994Sasomers			}
245296994Sasomers		}
246296994Sasomers#endif /* INET6 */
247296994Sasomers		if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
248296994Sasomers			const int goodness = 2;
249296994Sasomers
250296994Sasomers			if (bestif_goodness < goodness) {
25178705Siedowse				bestif = ifap;
252296994Sasomers				bestif_goodness = goodness;
25374462Salfred			}
25474462Salfred		}
255296994Sasomers		if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
256296994Sasomers			const int goodness = 1;
25778705Siedowse
258296994Sasomers			if (bestif_goodness < goodness) {
259296994Sasomers				bestif = ifap;
260296994Sasomers				bestif_goodness = goodness;
261296994Sasomers			}
262296994Sasomers		}
263296994Sasomers		if (bestif == NULL)
26474462Salfred			bestif = ifap;
26574462Salfred	}
26678705Siedowse	if (bestif == NULL)
26778705Siedowse		goto freeit;
26878705Siedowse
26974462Salfredfound:
27078705Siedowse	/*
271218909Sbrucec	 * Construct the new address using the address from
27278705Siedowse	 * `bestif', and the port number from `serv_uaddr'.
27378705Siedowse	 */
27478705Siedowse	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
27578705Siedowse	if (serv_nbp == NULL)
27678705Siedowse		goto freeit;
27778705Siedowse	serv_sa = serv_nbp->buf;
27878705Siedowse
27978705Siedowse	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
28078705Siedowse	switch (ss.ss_family) {
28176037Siedowse	case AF_INET:
28278705Siedowse		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
28378705Siedowse		break;
28476037Siedowse#ifdef INET6
28576037Siedowse	case AF_INET6:
28678705Siedowse		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
28776037Siedowse		break;
28876037Siedowse#endif
28976037Siedowse	}
29078705Siedowse	tbuf.len = ss.ss_len;
29178705Siedowse	tbuf.maxlen = sizeof(ss);
29278705Siedowse	tbuf.buf = &ss;
29378705Siedowse	ret = taddr2uaddr(nconf, &tbuf);
29478705Siedowse
29574462Salfredfreeit:
296301646Sngie	free(caller_uaddr);
29778705Siedowse	if (hint_nbp != NULL) {
29878705Siedowse		free(hint_nbp->buf);
29978705Siedowse		free(hint_nbp);
30078705Siedowse	}
30178705Siedowse	if (serv_nbp != NULL) {
30278705Siedowse		free(serv_nbp->buf);
30378705Siedowse		free(serv_nbp);
30478705Siedowse	}
30578705Siedowse	if (ifp != NULL)
30678705Siedowse		freeifaddrs(ifp);
30774462Salfred
30874462Salfred#ifdef ND_DEBUG
30974462Salfred	if (debugging)
31074462Salfred		fprintf(stderr, "addrmerge: returning %s\n", ret);
31174462Salfred#endif
31274462Salfred	return ret;
31374462Salfred}
31474462Salfred
31574462Salfredvoid
316224001Sdelphijnetwork_init(void)
31774462Salfred{
31874462Salfred#ifdef INET6
31974462Salfred	struct ifaddrs *ifap, *ifp;
32074462Salfred	struct ipv6_mreq mreq6;
321104592Salfred	unsigned int ifindex;
322104592Salfred	int s;
32374462Salfred#endif
32474462Salfred	int ecode;
32574462Salfred	struct addrinfo hints, *res;
32674462Salfred
32774462Salfred	memset(&hints, 0, sizeof hints);
32874462Salfred	hints.ai_family = AF_INET;
32974462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
33074462Salfred		if (debugging)
33174462Salfred			fprintf(stderr, "can't get local ip4 address: %s\n",
33274462Salfred			    gai_strerror(ecode));
33374462Salfred	} else {
33474462Salfred		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
33574462Salfred		if (local_in4 == NULL) {
33674462Salfred			if (debugging)
33774462Salfred				fprintf(stderr, "can't alloc local ip4 addr\n");
338301636Sngie			exit(1);
33974462Salfred		}
34074462Salfred		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
341301636Sngie		freeaddrinfo(res);
34274462Salfred	}
34374462Salfred
34474462Salfred#ifdef INET6
34574462Salfred	hints.ai_family = AF_INET6;
34674462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
34774462Salfred		if (debugging)
34874462Salfred			fprintf(stderr, "can't get local ip6 address: %s\n",
34974462Salfred			    gai_strerror(ecode));
35074462Salfred	} else {
35174462Salfred		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
35274462Salfred		if (local_in6 == NULL) {
35374462Salfred			if (debugging)
35474462Salfred				fprintf(stderr, "can't alloc local ip6 addr\n");
355301636Sngie			exit(1);
35674462Salfred		}
35774462Salfred		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
358301636Sngie		freeaddrinfo(res);
35974462Salfred	}
36074462Salfred
36174462Salfred	/*
36274462Salfred	 * Now join the RPC ipv6 multicast group on all interfaces.
36374462Salfred	 */
36474462Salfred	if (getifaddrs(&ifp) < 0)
36574462Salfred		return;
36674462Salfred
36774462Salfred	mreq6.ipv6mr_interface = 0;
36874462Salfred	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
36974462Salfred
37074462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
371301636Sngie	if (s == -1) {
372301636Sngie		if (debugging)
373301636Sngie			fprintf(stderr, "couldn't create ip6 socket");
374301636Sngie		goto done_inet6;
375301636Sngie	}
37674462Salfred
37774462Salfred	/*
37879720Siedowse	 * Loop through all interfaces. For each IPv6 multicast-capable
37979720Siedowse	 * interface, join the RPC multicast group on that interface.
38074462Salfred	 */
38174462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
38274462Salfred		if (ifap->ifa_addr->sa_family != AF_INET6 ||
38374462Salfred		    !(ifap->ifa_flags & IFF_MULTICAST))
38474462Salfred			continue;
38574462Salfred		ifindex = if_nametoindex(ifap->ifa_name);
38674462Salfred		if (ifindex == mreq6.ipv6mr_interface)
38774462Salfred			/*
38874462Salfred			 * Already did this one.
38974462Salfred			 */
39074462Salfred			continue;
39174462Salfred		mreq6.ipv6mr_interface = ifindex;
39274462Salfred		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
39374462Salfred		    sizeof mreq6) < 0)
39474462Salfred			if (debugging)
39574462Salfred				perror("setsockopt v6 multicast");
39674462Salfred	}
397301636Sngiedone_inet6:
398301636Sngie	freeifaddrs(ifp);
39974462Salfred#endif
40074462Salfred
40174462Salfred	/* close(s); */
40274462Salfred}
40374462Salfred
40474462Salfredstruct sockaddr *
40574462Salfredlocal_sa(int af)
40674462Salfred{
40774462Salfred	switch (af) {
40874462Salfred	case AF_INET:
40974462Salfred		return (struct sockaddr *)local_in4;
41074462Salfred#ifdef INET6
41174462Salfred	case AF_INET6:
41274462Salfred		return (struct sockaddr *)local_in6;
41374462Salfred#endif
41474462Salfred	default:
41574462Salfred		return NULL;
41674462Salfred	}
41774462Salfred}
418