174462Salfred/*
274462Salfred * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
374462Salfred * $FreeBSD: releng/10.3/usr.sbin/rpcbind/util.c 243187 2012-11-17 20:19:00Z hrs $
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
59173412Skevlostatic int bitmaskcmp(void *, void *, void *, int);
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
6774462Salfredbitmaskcmp(void *dst, void *src, void *mask, int bytelen)
6874462Salfred{
6979720Siedowse	int i;
7074462Salfred	u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
7174462Salfred
7279720Siedowse	for (i = 0; i < bytelen; i++)
7379720Siedowse		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
7479720Siedowse			return (1);
7579720Siedowse	return (0);
7674462Salfred}
7774462Salfred
7874462Salfred/*
7978705Siedowse * Find a server address that can be used by `caller' to contact
8078705Siedowse * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
8178705Siedowse * non-NULL, it is used instead of `caller' as a hint suggesting
8278705Siedowse * the best address (e.g. the `r_addr' field of an rpc, which
8378705Siedowse * contains the rpcbind server address that the caller used).
8478705Siedowse *
8578705Siedowse * Returns the best server address as a malloc'd "universal address"
8678705Siedowse * string which should be freed by the caller. On error, returns NULL.
8778705Siedowse */
8874462Salfredchar *
8974462Salfredaddrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
9074462Salfred	  char *netid)
9174462Salfred{
9278705Siedowse	struct ifaddrs *ifap, *ifp = NULL, *bestif;
9378705Siedowse	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
9478705Siedowse	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
9574462Salfred	struct sockaddr_storage ss;
9674462Salfred	struct netconfig *nconf;
9778705Siedowse	char *caller_uaddr = NULL, *hint_uaddr = NULL;
9874462Salfred	char *ret = NULL;
9974462Salfred
10074462Salfred#ifdef ND_DEBUG
10174462Salfred	if (debugging)
10274462Salfred		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
10378705Siedowse		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
10474462Salfred#endif
10578705Siedowse	caller_sa = caller->buf;
10678705Siedowse	if ((nconf = rpcbind_get_conf(netid)) == NULL)
10778705Siedowse		goto freeit;
10878705Siedowse	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
10978705Siedowse		goto freeit;
11074462Salfred
11174462Salfred	/*
11278705Siedowse	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
11378705Siedowse	 * address family is different from that of the caller.
11474462Salfred	 */
11578705Siedowse	hint_sa = NULL;
11674462Salfred	if (clnt_uaddr != NULL) {
11778705Siedowse		hint_uaddr = clnt_uaddr;
11878705Siedowse		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
11978705Siedowse			goto freeit;
12078705Siedowse		hint_sa = hint_nbp->buf;
12174462Salfred	}
12278705Siedowse	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
12378705Siedowse		hint_uaddr = caller_uaddr;
12478705Siedowse		hint_sa = caller->buf;
12578705Siedowse	}
12674462Salfred
12778705Siedowse#ifdef ND_DEBUG
12878705Siedowse	if (debugging)
12978705Siedowse		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
13078705Siedowse#endif
13178705Siedowse	/* Local caller, just return the server address. */
13278705Siedowse	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
13378705Siedowse	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
13478705Siedowse		ret = strdup(serv_uaddr);
13578705Siedowse		goto freeit;
13676037Siedowse	}
13774462Salfred
13878705Siedowse	if (getifaddrs(&ifp) < 0)
13978705Siedowse		goto freeit;
14078705Siedowse
14174462Salfred	/*
142203710Simp	 * Loop through all interfaces. For each interface, see if it
143203710Simp	 * is either the loopback interface (which we always listen
144203710Simp	 * on) or is one of the addresses the program bound to (the
145203710Simp	 * wildcard by default, or a subset if -h is specified) and
146203710Simp	 * the network portion of its address is equal to that of the
147203710Simp	 * client.  If so, we have found the interface that we want to
148203710Simp	 * use.
14974462Salfred	 */
15078705Siedowse	bestif = NULL;
15174462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
15278705Siedowse		ifsa = ifap->ifa_addr;
15378705Siedowse		ifmasksa = ifap->ifa_netmask;
15478705Siedowse
15578705Siedowse		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
15674462Salfred		    !(ifap->ifa_flags & IFF_UP))
15774462Salfred			continue;
15874462Salfred
159203710Simp		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
160203710Simp			continue;
161203710Simp
16278705Siedowse		switch (hint_sa->sa_family) {
16374462Salfred		case AF_INET:
16474462Salfred			/*
16578705Siedowse			 * If the hint address matches this interface
16678705Siedowse			 * address/netmask, then we're done.
16774462Salfred			 */
16878705Siedowse			if (!bitmaskcmp(&SA2SINADDR(ifsa),
16978705Siedowse			    &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
17078705Siedowse			    sizeof(struct in_addr))) {
17178705Siedowse				bestif = ifap;
17274462Salfred				goto found;
17374462Salfred			}
17474462Salfred			break;
17574462Salfred#ifdef INET6
17674462Salfred		case AF_INET6:
17774462Salfred			/*
17878705Siedowse			 * For v6 link local addresses, if the caller is on
17978705Siedowse			 * a link-local address then use the scope id to see
18078705Siedowse			 * which one.
18174462Salfred			 */
18278705Siedowse			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
18378705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
18478705Siedowse			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
18578705Siedowse				if (SA2SIN6(ifsa)->sin6_scope_id ==
18678705Siedowse				    SA2SIN6(caller_sa)->sin6_scope_id) {
18778705Siedowse					bestif = ifap;
18878705Siedowse					goto found;
18978705Siedowse				}
19078705Siedowse			} else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
19178705Siedowse			    &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
19278705Siedowse			    sizeof(struct in6_addr))) {
19378705Siedowse				bestif = ifap;
19474462Salfred				goto found;
19574462Salfred			}
19674462Salfred			break;
19774462Salfred#endif
19874462Salfred		default:
19974462Salfred			continue;
20074462Salfred		}
20178705Siedowse
20278705Siedowse		/*
20378705Siedowse		 * Remember the first possibly useful interface, preferring
20478705Siedowse		 * "normal" to point-to-point and loopback ones.
20578705Siedowse		 */
20678705Siedowse		if (bestif == NULL ||
20778705Siedowse		    (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
20878705Siedowse		    (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
20974462Salfred			bestif = ifap;
21074462Salfred	}
21178705Siedowse	if (bestif == NULL)
21278705Siedowse		goto freeit;
21378705Siedowse
21474462Salfredfound:
21578705Siedowse	/*
216218909Sbrucec	 * Construct the new address using the address from
21778705Siedowse	 * `bestif', and the port number from `serv_uaddr'.
21878705Siedowse	 */
21978705Siedowse	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
22078705Siedowse	if (serv_nbp == NULL)
22178705Siedowse		goto freeit;
22278705Siedowse	serv_sa = serv_nbp->buf;
22378705Siedowse
22478705Siedowse	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
22578705Siedowse	switch (ss.ss_family) {
22676037Siedowse	case AF_INET:
22778705Siedowse		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
22878705Siedowse		break;
22976037Siedowse#ifdef INET6
23076037Siedowse	case AF_INET6:
23178705Siedowse		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
23276037Siedowse		break;
23376037Siedowse#endif
23476037Siedowse	}
23578705Siedowse	tbuf.len = ss.ss_len;
23678705Siedowse	tbuf.maxlen = sizeof(ss);
23778705Siedowse	tbuf.buf = &ss;
23878705Siedowse	ret = taddr2uaddr(nconf, &tbuf);
23978705Siedowse
24074462Salfredfreeit:
24178705Siedowse	if (caller_uaddr != NULL)
24278705Siedowse		free(caller_uaddr);
24378705Siedowse	if (hint_nbp != NULL) {
24478705Siedowse		free(hint_nbp->buf);
24578705Siedowse		free(hint_nbp);
24678705Siedowse	}
24778705Siedowse	if (serv_nbp != NULL) {
24878705Siedowse		free(serv_nbp->buf);
24978705Siedowse		free(serv_nbp);
25078705Siedowse	}
25178705Siedowse	if (ifp != NULL)
25278705Siedowse		freeifaddrs(ifp);
25374462Salfred
25474462Salfred#ifdef ND_DEBUG
25574462Salfred	if (debugging)
25674462Salfred		fprintf(stderr, "addrmerge: returning %s\n", ret);
25774462Salfred#endif
25874462Salfred	return ret;
25974462Salfred}
26074462Salfred
26174462Salfredvoid
262224001Sdelphijnetwork_init(void)
26374462Salfred{
26474462Salfred#ifdef INET6
26574462Salfred	struct ifaddrs *ifap, *ifp;
26674462Salfred	struct ipv6_mreq mreq6;
267104592Salfred	unsigned int ifindex;
268104592Salfred	int s;
26974462Salfred#endif
27074462Salfred	int ecode;
27174462Salfred	struct addrinfo hints, *res;
27274462Salfred
27374462Salfred	memset(&hints, 0, sizeof hints);
27474462Salfred	hints.ai_family = AF_INET;
27574462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
27674462Salfred		if (debugging)
27774462Salfred			fprintf(stderr, "can't get local ip4 address: %s\n",
27874462Salfred			    gai_strerror(ecode));
27974462Salfred	} else {
28074462Salfred		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
28174462Salfred		if (local_in4 == NULL) {
28274462Salfred			if (debugging)
28374462Salfred				fprintf(stderr, "can't alloc local ip4 addr\n");
28474462Salfred		}
28574462Salfred		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
28674462Salfred	}
28774462Salfred
28874462Salfred#ifdef INET6
28974462Salfred	hints.ai_family = AF_INET6;
29074462Salfred	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
29174462Salfred		if (debugging)
29274462Salfred			fprintf(stderr, "can't get local ip6 address: %s\n",
29374462Salfred			    gai_strerror(ecode));
29474462Salfred	} else {
29574462Salfred		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
29674462Salfred		if (local_in6 == NULL) {
29774462Salfred			if (debugging)
29874462Salfred				fprintf(stderr, "can't alloc local ip6 addr\n");
29974462Salfred		}
30074462Salfred		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
30174462Salfred	}
30274462Salfred
30374462Salfred	/*
30474462Salfred	 * Now join the RPC ipv6 multicast group on all interfaces.
30574462Salfred	 */
30674462Salfred	if (getifaddrs(&ifp) < 0)
30774462Salfred		return;
30874462Salfred
30974462Salfred	mreq6.ipv6mr_interface = 0;
31074462Salfred	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
31174462Salfred
31274462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
31374462Salfred
31474462Salfred	/*
31579720Siedowse	 * Loop through all interfaces. For each IPv6 multicast-capable
31679720Siedowse	 * interface, join the RPC multicast group on that interface.
31774462Salfred	 */
31874462Salfred	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
31974462Salfred		if (ifap->ifa_addr->sa_family != AF_INET6 ||
32074462Salfred		    !(ifap->ifa_flags & IFF_MULTICAST))
32174462Salfred			continue;
32274462Salfred		ifindex = if_nametoindex(ifap->ifa_name);
32374462Salfred		if (ifindex == mreq6.ipv6mr_interface)
32474462Salfred			/*
32574462Salfred			 * Already did this one.
32674462Salfred			 */
32774462Salfred			continue;
32874462Salfred		mreq6.ipv6mr_interface = ifindex;
32974462Salfred		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
33074462Salfred		    sizeof mreq6) < 0)
33174462Salfred			if (debugging)
33274462Salfred				perror("setsockopt v6 multicast");
33374462Salfred	}
33474462Salfred#endif
33574462Salfred
33674462Salfred	/* close(s); */
33774462Salfred}
33874462Salfred
33974462Salfredstruct sockaddr *
34074462Salfredlocal_sa(int af)
34174462Salfred{
34274462Salfred	switch (af) {
34374462Salfred	case AF_INET:
34474462Salfred		return (struct sockaddr *)local_in4;
34574462Salfred#ifdef INET6
34674462Salfred	case AF_INET6:
34774462Salfred		return (struct sockaddr *)local_in6;
34874462Salfred#endif
34974462Salfred	default:
35074462Salfred		return NULL;
35174462Salfred	}
35274462Salfred}
353