util.c revision 293229
174462Salfred/*
274462Salfred * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
374462Salfred * $FreeBSD: head/usr.sbin/rpcbind/util.c 293229 2016-01-06 00:00:11Z asomers $
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
59293229Sasomersstatic 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
67293229Sasomersbitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
6874462Salfred{
6979720Siedowse	int i;
70293229Sasomers	u_int8_t *p1, *p2, *netmask;
71293229Sasomers	int bytelen;
7274462Salfred
73293229Sasomers	if (dst->sa_family != src->sa_family ||
74293229Sasomers	    dst->sa_family != mask->sa_family)
75293229Sasomers		return (1);
76293229Sasomers
77293229Sasomers	switch (dst->sa_family) {
78293229Sasomers	case AF_INET:
79293229Sasomers		p1 = (uint8_t*) &SA2SINADDR(dst);
80293229Sasomers		p2 = (uint8_t*) &SA2SINADDR(src);
81293229Sasomers		netmask = (uint8_t*) &SA2SINADDR(mask);
82293229Sasomers		bytelen = sizeof(struct in_addr);
83293229Sasomers		break;
84293229Sasomers#ifdef INET6
85293229Sasomers	case AF_INET6:
86293229Sasomers		p1 = (uint8_t*) &SA2SIN6ADDR(dst);
87293229Sasomers		p2 = (uint8_t*) &SA2SIN6ADDR(src);
88293229Sasomers		netmask = (uint8_t*) &SA2SIN6ADDR(mask);
89293229Sasomers		bytelen = sizeof(struct in6_addr);
90293229Sasomers		break;
91293229Sasomers#endif
92293229Sasomers	default:
93293229Sasomers		return (1);
94293229Sasomers	}
95293229Sasomers
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 *
113293229Sasomersaddrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
114293229Sasomers	  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;
121293229Sasomers	char *caller_uaddr = NULL;
122293229Sasomers	const char *hint_uaddr = NULL;
12374462Salfred	char *ret = NULL;
124293229Sasomers	int bestif_goodness;
12574462Salfred
12674462Salfred#ifdef ND_DEBUG
12774462Salfred	if (debugging)
12874462Salfred		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
12978705Siedowse		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
13074462Salfred#endif
13178705Siedowse	caller_sa = caller->buf;
13278705Siedowse	if ((nconf = rpcbind_get_conf(netid)) == NULL)
13378705Siedowse		goto freeit;
13478705Siedowse	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
13578705Siedowse		goto freeit;
13674462Salfred
13774462Salfred	/*
13878705Siedowse	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
13978705Siedowse	 * address family is different from that of the caller.
14074462Salfred	 */
14178705Siedowse	hint_sa = NULL;
14274462Salfred	if (clnt_uaddr != NULL) {
14378705Siedowse		hint_uaddr = clnt_uaddr;
14478705Siedowse		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
14578705Siedowse			goto freeit;
14678705Siedowse		hint_sa = hint_nbp->buf;
14774462Salfred	}
14878705Siedowse	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
14978705Siedowse		hint_uaddr = caller_uaddr;
15078705Siedowse		hint_sa = caller->buf;
15178705Siedowse	}
15274462Salfred
15378705Siedowse#ifdef ND_DEBUG
15478705Siedowse	if (debugging)
15578705Siedowse		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
15678705Siedowse#endif
15778705Siedowse	/* Local caller, just return the server address. */
15878705Siedowse	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
15978705Siedowse	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
16078705Siedowse		ret = strdup(serv_uaddr);
16178705Siedowse		goto freeit;
16276037Siedowse	}
16374462Salfred
16478705Siedowse	if (getifaddrs(&ifp) < 0)
16578705Siedowse		goto freeit;
16678705Siedowse
16774462Salfred	/*
168293229Sasomers	 * Loop through all interface addresses.  We are listening to an address
169293229Sasomers	 * if any of the following are true:
170293229Sasomers	 * a) It's a loopback address
171293229Sasomers	 * b) It was specified with the -h command line option
172293229Sasomers	 * c) There were no -h command line options.
173293229Sasomers	 *
174293229Sasomers	 * Among addresses on which we are listening, choose in order of
175293229Sasomers	 * preference an address that is:
176293229Sasomers	 *
177293229Sasomers	 * a) Equal to the hint
178293229Sasomers	 * b) A link local address with the same scope ID as the client's
179293229Sasomers	 *    address, if the client's address is also link local
180293229Sasomers	 * c) An address on the same subnet as the client's address
181293229Sasomers	 * d) A non-localhost, non-p2p address
182293229Sasomers	 * e) Any usable address
18374462Salfred	 */
18478705Siedowse	bestif = NULL;
185293229Sasomers	bestif_goodness = 0;
18674462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
18778705Siedowse		ifsa = ifap->ifa_addr;
18878705Siedowse		ifmasksa = ifap->ifa_netmask;
18978705Siedowse
190293229Sasomers		/* Skip addresses where we don't listen */
19178705Siedowse		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
19274462Salfred		    !(ifap->ifa_flags & IFF_UP))
19374462Salfred			continue;
19474462Salfred
195203710Simp		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
196203710Simp			continue;
197203710Simp
198293229Sasomers		if ((hint_sa->sa_family == AF_INET) &&
199293229Sasomers		    ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
200293229Sasomers		      ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
201293229Sasomers			const int goodness = 4;
202293229Sasomers
203293229Sasomers			bestif_goodness = goodness;
204293229Sasomers			bestif = ifap;
205293229Sasomers			goto found;
206293229Sasomers		}
20774462Salfred#ifdef INET6
208293229Sasomers		if ((hint_sa->sa_family == AF_INET6) &&
209293229Sasomers		    (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
210293229Sasomers				 &((struct sockaddr_in6*)ifsa)->sin6_addr,
211293229Sasomers				 sizeof(struct in6_addr))) &&
212293229Sasomers		    (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
213293229Sasomers		    (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
214293229Sasomers			const int goodness = 4;
215293229Sasomers
216293229Sasomers			bestif_goodness = goodness;
217293229Sasomers			bestif = ifap;
218293229Sasomers			goto found;
219293229Sasomers		}
220293229Sasomers		if (hint_sa->sa_family == AF_INET6) {
22174462Salfred			/*
22278705Siedowse			 * For v6 link local addresses, if the caller is on
22378705Siedowse			 * a link-local address then use the scope id to see
22478705Siedowse			 * which one.
22574462Salfred			 */
22678705Siedowse			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
22778705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
22878705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
22978705Siedowse				if (SA2SIN6(ifsa)->sin6_scope_id ==
23078705Siedowse				    SA2SIN6(caller_sa)->sin6_scope_id) {
231293229Sasomers					const int goodness = 3;
232293229Sasomers
233293229Sasomers					if (bestif_goodness < goodness) {
234293229Sasomers						bestif = ifap;
235293229Sasomers						bestif_goodness = goodness;
236293229Sasomers					}
23778705Siedowse				}
238293229Sasomers			}
239293229Sasomers		}
240293229Sasomers#endif /* INET6 */
241293229Sasomers		if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
242293229Sasomers			const int goodness = 2;
243293229Sasomers
244293229Sasomers			if (bestif_goodness < goodness) {
24578705Siedowse				bestif = ifap;
246293229Sasomers				bestif_goodness = goodness;
24774462Salfred			}
24874462Salfred		}
249293229Sasomers		if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
250293229Sasomers			const int goodness = 1;
25178705Siedowse
252293229Sasomers			if (bestif_goodness < goodness) {
253293229Sasomers				bestif = ifap;
254293229Sasomers				bestif_goodness = goodness;
255293229Sasomers			}
256293229Sasomers		}
257293229Sasomers		if (bestif == NULL)
25874462Salfred			bestif = ifap;
25974462Salfred	}
26078705Siedowse	if (bestif == NULL)
26178705Siedowse		goto freeit;
26278705Siedowse
26374462Salfredfound:
26478705Siedowse	/*
265218909Sbrucec	 * Construct the new address using the address from
26678705Siedowse	 * `bestif', and the port number from `serv_uaddr'.
26778705Siedowse	 */
26878705Siedowse	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
26978705Siedowse	if (serv_nbp == NULL)
27078705Siedowse		goto freeit;
27178705Siedowse	serv_sa = serv_nbp->buf;
27278705Siedowse
27378705Siedowse	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
27478705Siedowse	switch (ss.ss_family) {
27576037Siedowse	case AF_INET:
27678705Siedowse		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
27778705Siedowse		break;
27876037Siedowse#ifdef INET6
27976037Siedowse	case AF_INET6:
28078705Siedowse		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
28176037Siedowse		break;
28276037Siedowse#endif
28376037Siedowse	}
28478705Siedowse	tbuf.len = ss.ss_len;
28578705Siedowse	tbuf.maxlen = sizeof(ss);
28678705Siedowse	tbuf.buf = &ss;
28778705Siedowse	ret = taddr2uaddr(nconf, &tbuf);
28878705Siedowse
28974462Salfredfreeit:
29078705Siedowse	if (caller_uaddr != NULL)
29178705Siedowse		free(caller_uaddr);
29278705Siedowse	if (hint_nbp != NULL) {
29378705Siedowse		free(hint_nbp->buf);
29478705Siedowse		free(hint_nbp);
29578705Siedowse	}
29678705Siedowse	if (serv_nbp != NULL) {
29778705Siedowse		free(serv_nbp->buf);
29878705Siedowse		free(serv_nbp);
29978705Siedowse	}
30078705Siedowse	if (ifp != NULL)
30178705Siedowse		freeifaddrs(ifp);
30274462Salfred
30374462Salfred#ifdef ND_DEBUG
30474462Salfred	if (debugging)
30574462Salfred		fprintf(stderr, "addrmerge: returning %s\n", ret);
30674462Salfred#endif
30774462Salfred	return ret;
30874462Salfred}
30974462Salfred
31074462Salfredvoid
311224001Sdelphijnetwork_init(void)
31274462Salfred{
31374462Salfred#ifdef INET6
31474462Salfred	struct ifaddrs *ifap, *ifp;
31574462Salfred	struct ipv6_mreq mreq6;
316104592Salfred	unsigned int ifindex;
317104592Salfred	int s;
31874462Salfred#endif
31974462Salfred	int ecode;
32074462Salfred	struct addrinfo hints, *res;
32174462Salfred
32274462Salfred	memset(&hints, 0, sizeof hints);
32374462Salfred	hints.ai_family = AF_INET;
32474462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
32574462Salfred		if (debugging)
32674462Salfred			fprintf(stderr, "can't get local ip4 address: %s\n",
32774462Salfred			    gai_strerror(ecode));
32874462Salfred	} else {
32974462Salfred		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
33074462Salfred		if (local_in4 == NULL) {
33174462Salfred			if (debugging)
33274462Salfred				fprintf(stderr, "can't alloc local ip4 addr\n");
33374462Salfred		}
33474462Salfred		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
33574462Salfred	}
33674462Salfred
33774462Salfred#ifdef INET6
33874462Salfred	hints.ai_family = AF_INET6;
33974462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
34074462Salfred		if (debugging)
34174462Salfred			fprintf(stderr, "can't get local ip6 address: %s\n",
34274462Salfred			    gai_strerror(ecode));
34374462Salfred	} else {
34474462Salfred		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
34574462Salfred		if (local_in6 == NULL) {
34674462Salfred			if (debugging)
34774462Salfred				fprintf(stderr, "can't alloc local ip6 addr\n");
34874462Salfred		}
34974462Salfred		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
35074462Salfred	}
35174462Salfred
35274462Salfred	/*
35374462Salfred	 * Now join the RPC ipv6 multicast group on all interfaces.
35474462Salfred	 */
35574462Salfred	if (getifaddrs(&ifp) < 0)
35674462Salfred		return;
35774462Salfred
35874462Salfred	mreq6.ipv6mr_interface = 0;
35974462Salfred	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
36074462Salfred
36174462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
36274462Salfred
36374462Salfred	/*
36479720Siedowse	 * Loop through all interfaces. For each IPv6 multicast-capable
36579720Siedowse	 * interface, join the RPC multicast group on that interface.
36674462Salfred	 */
36774462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
36874462Salfred		if (ifap->ifa_addr->sa_family != AF_INET6 ||
36974462Salfred		    !(ifap->ifa_flags & IFF_MULTICAST))
37074462Salfred			continue;
37174462Salfred		ifindex = if_nametoindex(ifap->ifa_name);
37274462Salfred		if (ifindex == mreq6.ipv6mr_interface)
37374462Salfred			/*
37474462Salfred			 * Already did this one.
37574462Salfred			 */
37674462Salfred			continue;
37774462Salfred		mreq6.ipv6mr_interface = ifindex;
37874462Salfred		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
37974462Salfred		    sizeof mreq6) < 0)
38074462Salfred			if (debugging)
38174462Salfred				perror("setsockopt v6 multicast");
38274462Salfred	}
38374462Salfred#endif
38474462Salfred
38574462Salfred	/* close(s); */
38674462Salfred}
38774462Salfred
38874462Salfredstruct sockaddr *
38974462Salfredlocal_sa(int af)
39074462Salfred{
39174462Salfred	switch (af) {
39274462Salfred	case AF_INET:
39374462Salfred		return (struct sockaddr *)local_in4;
39474462Salfred#ifdef INET6
39574462Salfred	case AF_INET6:
39674462Salfred		return (struct sockaddr *)local_in6;
39774462Salfred#endif
39874462Salfred	default:
39974462Salfred		return NULL;
40074462Salfred	}
40174462Salfred}
402