util.c revision 300973
11590Srgrimes/*
21590Srgrimes * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
31590Srgrimes * $FreeBSD: head/usr.sbin/rpcbind/util.c 300973 2016-05-29 20:28:01Z ngie $
41590Srgrimes */
51590Srgrimes
61590Srgrimes/*-
71590Srgrimes * Copyright (c) 2000 The NetBSD Foundation, Inc.
81590Srgrimes * All rights reserved.
91590Srgrimes *
101590Srgrimes * This code is derived from software contributed to The NetBSD Foundation
111590Srgrimes * by Frank van der Linden.
121590Srgrimes *
131590Srgrimes * Redistribution and use in source and binary forms, with or without
141590Srgrimes * modification, are permitted provided that the following conditions
151590Srgrimes * are met:
161590Srgrimes * 1. Redistributions of source code must retain the above copyright
171590Srgrimes *    notice, this list of conditions and the following disclaimer.
181590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
191590Srgrimes *    notice, this list of conditions and the following disclaimer in the
201590Srgrimes *    documentation and/or other materials provided with the distribution.
211590Srgrimes *
221590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
231590Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
241590Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
251590Srgrimes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
261590Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
271590Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
281590Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
291590Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
301590Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
311590Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
321590Srgrimes * POSSIBILITY OF SUCH DAMAGE.
33207705Sdelphij */
34207705Sdelphij
35207705Sdelphij#include <sys/types.h>
36207705Sdelphij#include <sys/socket.h>
37207705Sdelphij#include <sys/queue.h>
38207705Sdelphij#include <net/if.h>
3993604Sobrien#include <netinet/in.h>
4093604Sobrien#include <ifaddrs.h>
411590Srgrimes#include <sys/poll.h>
421590Srgrimes#include <rpc/rpc.h>
431590Srgrimes#include <errno.h>
441590Srgrimes#include <stdlib.h>
451590Srgrimes#include <string.h>
461590Srgrimes#include <unistd.h>
4772945Sknu#include <netdb.h>
48200462Sdelphij#include <netconfig.h>
491590Srgrimes#include <stdio.h>
501590Srgrimes#include <arpa/inet.h>
511590Srgrimes
521590Srgrimes#include "rpcbind.h"
531590Srgrimes
54207705Sdelphijstatic struct sockaddr_in *local_in4;
5591400Sdwmalone#ifdef INET6
561590Srgrimesstatic struct sockaddr_in6 *local_in6;
57176478Simp#endif
581590Srgrimes
5976250Sphkstatic int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
6076250Sphk
6176250Sphk/*
62157555Sceri * For all bits set in "mask", compare the corresponding bits in
63157555Sceri * "dst" and "src", and see if they match. Returns 0 if the addresses
64157555Sceri * match.
6576250Sphk */
66127796Sbmilekicstatic int
6776250Sphkbitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
6876250Sphk{
6976250Sphk	int i;
7076250Sphk	u_int8_t *p1, *p2, *netmask;
7176250Sphk	int bytelen;
7276250Sphk
7376250Sphk	if (dst->sa_family != src->sa_family ||
74176478Simp	    dst->sa_family != mask->sa_family)
75176478Simp		return (1);
7676250Sphk
77129812Seik	switch (dst->sa_family) {
7876250Sphk	case AF_INET:
7976250Sphk		p1 = (uint8_t*) &SA2SINADDR(dst);
8076250Sphk		p2 = (uint8_t*) &SA2SINADDR(src);
81176478Simp		netmask = (uint8_t*) &SA2SINADDR(mask);
8276250Sphk		bytelen = sizeof(struct in_addr);
83176478Simp		break;
8476250Sphk#ifdef INET6
85176478Simp	case AF_INET6:
86176478Simp		p1 = (uint8_t*) &SA2SIN6ADDR(dst);
87176478Simp		p2 = (uint8_t*) &SA2SIN6ADDR(src);
8876250Sphk		netmask = (uint8_t*) &SA2SIN6ADDR(mask);
89176478Simp		bytelen = sizeof(struct in6_addr);
9076250Sphk		break;
91238780Sjilles#endif
92176478Simp	default:
9376250Sphk		return (1);
9476250Sphk	}
9576250Sphk
9676250Sphk	for (i = 0; i < bytelen; i++)
97176478Simp		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
9876250Sphk			return (1);
99176478Simp	return (0);
10076250Sphk}
10176250Sphk
10276250Sphk/*
10376250Sphk * Find a server address that can be used by `caller' to contact
10476250Sphk * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
105176478Simp * non-NULL, it is used instead of `caller' as a hint suggesting
10676250Sphk * the best address (e.g. the `r_addr' field of an rpc, which
10776250Sphk * contains the rpcbind server address that the caller used).
10876250Sphk *
109157555Sceri * Returns the best server address as a malloc'd "universal address"
110157555Sceri * string which should be freed by the caller. On error, returns NULL.
111157555Sceri */
112157555Scerichar *
113157555Sceriaddrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
114157555Sceri	  const char *netid)
11576250Sphk{
11676250Sphk	struct ifaddrs *ifap, *ifp = NULL, *bestif;
11776250Sphk	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
11876250Sphk	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
119157555Sceri	struct sockaddr_storage ss;
12076250Sphk	struct netconfig *nconf;
12176250Sphk	char *caller_uaddr = NULL;
12276250Sphk#ifdef ND_DEBUG
12376250Sphk	const char *hint_uaddr = NULL;
124157555Sceri#endif
12576250Sphk	char *ret = NULL;
12676250Sphk	int bestif_goodness;
12776250Sphk
12876250Sphk#ifdef ND_DEBUG
12976250Sphk	if (debugging)
130238780Sjilles		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
131176478Simp		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
132111084Sdes#endif
13376250Sphk	caller_sa = caller->buf;
13476250Sphk	if ((nconf = rpcbind_get_conf(netid)) == NULL)
13576250Sphk		goto freeit;
13676250Sphk	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
13776250Sphk		goto freeit;
13876250Sphk
13976250Sphk	/*
14076250Sphk	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
14176250Sphk	 * address family is different from that of the caller.
142176478Simp	 */
14376250Sphk	hint_sa = NULL;
144176478Simp	if (clnt_uaddr != NULL) {
14576250Sphk#ifdef ND_DEBUG
146176478Simp		hint_uaddr = clnt_uaddr;
14776250Sphk#endif
148247730Sdwmalone		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
149176478Simp			goto freeit;
15076250Sphk		hint_sa = hint_nbp->buf;
151176478Simp	}
15276250Sphk	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
153176478Simp#ifdef ND_DEBUG
15476250Sphk		hint_uaddr = caller_uaddr;
155176478Simp#endif
1561590Srgrimes		hint_sa = caller->buf;
1571590Srgrimes	}
1581590Srgrimes
1591590Srgrimes#ifdef ND_DEBUG
1601590Srgrimes	if (debugging)
1611590Srgrimes		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
1621590Srgrimes#endif
1631590Srgrimes	/* Local caller, just return the server address. */
1641590Srgrimes	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
1651590Srgrimes	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
1661590Srgrimes		ret = strdup(serv_uaddr);
167116333Smarkm		goto freeit;
1681590Srgrimes	}
16991400Sdwmalone
1701590Srgrimes	if (getifaddrs(&ifp) < 0)
1711590Srgrimes		goto freeit;
1721590Srgrimes
1731590Srgrimes	/*
1741590Srgrimes	 * Loop through all interface addresses.  We are listening to an address
17591400Sdwmalone	 * if any of the following are true:
176222390Sjilles	 * a) It's a loopback address
1771590Srgrimes	 * b) It was specified with the -h command line option
1781590Srgrimes	 * c) There were no -h command line options.
17976250Sphk	 *
1801590Srgrimes	 * Among addresses on which we are listening, choose in order of
1811590Srgrimes	 * preference an address that is:
1821590Srgrimes	 *
1831590Srgrimes	 * a) Equal to the hint
18476250Sphk	 * b) A link local address with the same scope ID as the client's
185116333Smarkm	 *    address, if the client's address is also link local
1861590Srgrimes	 * c) An address on the same subnet as the client's address
1871590Srgrimes	 * d) A non-localhost, non-p2p address
1881590Srgrimes	 * e) Any usable address
1891590Srgrimes	 */
1901590Srgrimes	bestif = NULL;
1911590Srgrimes	bestif_goodness = 0;
1921590Srgrimes	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
1931590Srgrimes		ifsa = ifap->ifa_addr;
194207705Sdelphij		ifmasksa = ifap->ifa_netmask;
195116333Smarkm
1961590Srgrimes		/* Skip addresses where we don't listen */
19791400Sdwmalone		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
1981590Srgrimes		    !(ifap->ifa_flags & IFF_UP))
199			continue;
200
201		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
202			continue;
203
204		if ((hint_sa->sa_family == AF_INET) &&
205		    ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
206		      ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
207			const int goodness = 4;
208
209			bestif_goodness = goodness;
210			bestif = ifap;
211			goto found;
212		}
213#ifdef INET6
214		if ((hint_sa->sa_family == AF_INET6) &&
215		    (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
216				 &((struct sockaddr_in6*)ifsa)->sin6_addr,
217				 sizeof(struct in6_addr))) &&
218		    (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
219		    (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
220			const int goodness = 4;
221
222			bestif_goodness = goodness;
223			bestif = ifap;
224			goto found;
225		}
226		if (hint_sa->sa_family == AF_INET6) {
227			/*
228			 * For v6 link local addresses, if the caller is on
229			 * a link-local address then use the scope id to see
230			 * which one.
231			 */
232			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
233			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
234			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
235				if (SA2SIN6(ifsa)->sin6_scope_id ==
236				    SA2SIN6(caller_sa)->sin6_scope_id) {
237					const int goodness = 3;
238
239					if (bestif_goodness < goodness) {
240						bestif = ifap;
241						bestif_goodness = goodness;
242					}
243				}
244			}
245		}
246#endif /* INET6 */
247		if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
248			const int goodness = 2;
249
250			if (bestif_goodness < goodness) {
251				bestif = ifap;
252				bestif_goodness = goodness;
253			}
254		}
255		if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
256			const int goodness = 1;
257
258			if (bestif_goodness < goodness) {
259				bestif = ifap;
260				bestif_goodness = goodness;
261			}
262		}
263		if (bestif == NULL)
264			bestif = ifap;
265	}
266	if (bestif == NULL)
267		goto freeit;
268
269found:
270	/*
271	 * Construct the new address using the address from
272	 * `bestif', and the port number from `serv_uaddr'.
273	 */
274	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
275	if (serv_nbp == NULL)
276		goto freeit;
277	serv_sa = serv_nbp->buf;
278
279	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
280	switch (ss.ss_family) {
281	case AF_INET:
282		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
283		break;
284#ifdef INET6
285	case AF_INET6:
286		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
287		break;
288#endif
289	}
290	tbuf.len = ss.ss_len;
291	tbuf.maxlen = sizeof(ss);
292	tbuf.buf = &ss;
293	ret = taddr2uaddr(nconf, &tbuf);
294
295freeit:
296	free(caller_uaddr);
297	if (hint_nbp != NULL) {
298		free(hint_nbp->buf);
299		free(hint_nbp);
300	}
301	if (serv_nbp != NULL) {
302		free(serv_nbp->buf);
303		free(serv_nbp);
304	}
305	if (ifp != NULL)
306		freeifaddrs(ifp);
307
308#ifdef ND_DEBUG
309	if (debugging)
310		fprintf(stderr, "addrmerge: returning %s\n", ret);
311#endif
312	return ret;
313}
314
315void
316network_init(void)
317{
318#ifdef INET6
319	struct ifaddrs *ifap, *ifp;
320	struct ipv6_mreq mreq6;
321	unsigned int ifindex;
322	int s;
323#endif
324	int ecode;
325	struct addrinfo hints, *res;
326
327	memset(&hints, 0, sizeof hints);
328	hints.ai_family = AF_INET;
329	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
330		if (debugging)
331			fprintf(stderr, "can't get local ip4 address: %s\n",
332			    gai_strerror(ecode));
333	} else {
334		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
335		if (local_in4 == NULL) {
336			if (debugging)
337				fprintf(stderr, "can't alloc local ip4 addr\n");
338			exit(1);
339		}
340		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
341		freeaddrinfo(res);
342	}
343
344#ifdef INET6
345	hints.ai_family = AF_INET6;
346	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
347		if (debugging)
348			fprintf(stderr, "can't get local ip6 address: %s\n",
349			    gai_strerror(ecode));
350	} else {
351		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
352		if (local_in6 == NULL) {
353			if (debugging)
354				fprintf(stderr, "can't alloc local ip6 addr\n");
355			exit(1);
356		}
357		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
358		freeaddrinfo(res);
359	}
360
361	/*
362	 * Now join the RPC ipv6 multicast group on all interfaces.
363	 */
364	if (getifaddrs(&ifp) < 0)
365		return;
366
367	mreq6.ipv6mr_interface = 0;
368	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
369
370	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
371	if (s == -1) {
372		if (debugging)
373			fprintf(stderr, "couldn't create ip6 socket");
374		goto done_inet6;
375	}
376
377	/*
378	 * Loop through all interfaces. For each IPv6 multicast-capable
379	 * interface, join the RPC multicast group on that interface.
380	 */
381	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
382		if (ifap->ifa_addr->sa_family != AF_INET6 ||
383		    !(ifap->ifa_flags & IFF_MULTICAST))
384			continue;
385		ifindex = if_nametoindex(ifap->ifa_name);
386		if (ifindex == mreq6.ipv6mr_interface)
387			/*
388			 * Already did this one.
389			 */
390			continue;
391		mreq6.ipv6mr_interface = ifindex;
392		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
393		    sizeof mreq6) < 0)
394			if (debugging)
395				perror("setsockopt v6 multicast");
396	}
397done_inet6:
398	freeifaddrs(ifp);
399#endif
400
401	/* close(s); */
402}
403
404struct sockaddr *
405local_sa(int af)
406{
407	switch (af) {
408	case AF_INET:
409		return (struct sockaddr *)local_in4;
410#ifdef INET6
411	case AF_INET6:
412		return (struct sockaddr *)local_in6;
413#endif
414	default:
415		return NULL;
416	}
417}
418