2017-08-16 11:44:40 +03:00
|
|
|
/* $NetBSD: util.c,v 1.21 2017/08/16 08:44:40 christos Exp $ */
|
|
|
|
/* $FreeBSD: head/usr.sbin/rpcbind/util.c 300973 2016-05-29 20:28:01Z ngie $ */
|
2000-06-03 03:15:41 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2000 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Frank van der Linden.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2006-05-25 06:37:38 +04:00
|
|
|
#include <assert.h>
|
2000-06-03 03:15:41 +04:00
|
|
|
#include <ifaddrs.h>
|
2002-09-23 07:36:03 +04:00
|
|
|
#include <poll.h>
|
2000-06-03 03:15:41 +04:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2000-06-04 08:51:50 +04:00
|
|
|
#include <string.h>
|
2000-06-03 03:15:41 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netconfig.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-05-09 21:32:04 +03:00
|
|
|
#include <err.h>
|
2000-06-03 03:15:41 +04:00
|
|
|
|
2015-11-08 19:36:28 +03:00
|
|
|
#ifdef RPCBIND_RUMP
|
|
|
|
#include <rump/rump.h>
|
|
|
|
#include <rump/rump_syscalls.h>
|
|
|
|
#endif
|
|
|
|
|
2000-06-03 03:15:41 +04:00
|
|
|
#include "rpcbind.h"
|
|
|
|
|
|
|
|
static struct sockaddr_in *local_in4;
|
|
|
|
#ifdef INET6
|
|
|
|
static struct sockaddr_in6 *local_in6;
|
|
|
|
#endif
|
|
|
|
|
2007-05-14 00:03:46 +04:00
|
|
|
static int bitmaskcmp(void *, void *, void *, int);
|
2000-06-03 03:15:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For all bits set in "mask", compare the corresponding bits in
|
2017-08-16 11:44:40 +03:00
|
|
|
* "dst" and "src", and see if they match. Returns 0 if the addresses
|
|
|
|
* match.
|
2000-06-03 03:15:41 +04:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
|
|
|
|
u_int8_t bitmask;
|
|
|
|
|
|
|
|
for (i = 0; i < bytelen; i++) {
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
bitmask = 1 << j;
|
|
|
|
if (!(netmask[i] & bitmask))
|
|
|
|
continue;
|
|
|
|
if ((p1[i] & bitmask) != (p2[i] & bitmask))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
|
2017-08-16 11:44:40 +03:00
|
|
|
const char *netid)
|
2000-06-03 03:15:41 +04:00
|
|
|
{
|
2000-08-03 04:04:30 +04:00
|
|
|
struct ifaddrs *ifap, *ifp, *bestif;
|
2000-06-03 03:15:41 +04:00
|
|
|
#ifdef INET6
|
|
|
|
struct sockaddr_in6 *servsin6, *sin6mask, *clntsin6, *ifsin6, *realsin6;
|
|
|
|
struct sockaddr_in6 *newsin6;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in *servsin, *sinmask, *clntsin, *newsin, *ifsin;
|
|
|
|
struct netbuf *serv_nbp, *clnt_nbp = NULL, tbuf;
|
|
|
|
struct sockaddr *serv_sa;
|
|
|
|
struct sockaddr *clnt_sa;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct netconfig *nconf;
|
|
|
|
struct sockaddr *clnt = caller->buf;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
2005-06-08 02:21:57 +04:00
|
|
|
#ifdef INET6
|
2005-06-02 13:32:57 +04:00
|
|
|
servsin6 = ifsin6 = newsin6 = NULL; /* XXXGCC -Wuninitialized */
|
2005-06-08 02:21:57 +04:00
|
|
|
#endif
|
2005-06-02 13:32:57 +04:00
|
|
|
servsin = newsin = NULL; /* XXXGCC -Wuninitialized */
|
|
|
|
|
2007-08-27 23:53:32 +04:00
|
|
|
#ifdef RPCBIND_DEBUG
|
2000-06-03 03:15:41 +04:00
|
|
|
if (debugging)
|
|
|
|
fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
|
|
|
|
clnt_uaddr, netid);
|
|
|
|
#endif
|
|
|
|
nconf = getnetconfigent(netid);
|
|
|
|
if (nconf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2000-08-03 04:04:30 +04:00
|
|
|
/*
|
|
|
|
* Local merge, just return a duplicate.
|
|
|
|
*/
|
|
|
|
if (clnt_uaddr != NULL && strncmp(clnt_uaddr, "0.0.0.0.", 8) == 0)
|
|
|
|
return strdup(clnt_uaddr);
|
|
|
|
|
2000-06-03 03:15:41 +04:00
|
|
|
serv_nbp = uaddr2taddr(nconf, serv_uaddr);
|
|
|
|
if (serv_nbp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
serv_sa = (struct sockaddr *)serv_nbp->buf;
|
|
|
|
if (clnt_uaddr != NULL) {
|
|
|
|
clnt_nbp = uaddr2taddr(nconf, clnt_uaddr);
|
2001-04-23 01:43:05 +04:00
|
|
|
if (clnt_nbp == NULL) {
|
|
|
|
free(serv_nbp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-03 03:15:41 +04:00
|
|
|
clnt_sa = (struct sockaddr *)clnt_nbp->buf;
|
2001-04-23 01:43:05 +04:00
|
|
|
if (clnt_sa->sa_family == AF_LOCAL) {
|
|
|
|
free(serv_nbp);
|
|
|
|
free(clnt_nbp);
|
|
|
|
free(clnt_sa);
|
2001-04-30 04:36:07 +04:00
|
|
|
return strdup(serv_uaddr);
|
2001-04-23 01:43:05 +04:00
|
|
|
}
|
2000-06-03 03:15:41 +04:00
|
|
|
} else {
|
|
|
|
clnt_sa = (struct sockaddr *)
|
|
|
|
malloc(sizeof (struct sockaddr_storage));
|
|
|
|
memcpy(clnt_sa, clnt, clnt->sa_len);
|
|
|
|
}
|
|
|
|
|
2001-04-23 01:43:05 +04:00
|
|
|
if (getifaddrs(&ifp) < 0) {
|
|
|
|
free(serv_nbp);
|
|
|
|
free(clnt_sa);
|
|
|
|
if (clnt_nbp != NULL)
|
|
|
|
free(clnt_nbp);
|
2000-06-03 03:15:41 +04:00
|
|
|
return 0;
|
2001-04-23 01:43:05 +04:00
|
|
|
}
|
2000-06-03 03:15:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all interfaces. For each interface, see if the
|
|
|
|
* network portion of its address is equal to that of the client.
|
|
|
|
* If so, we have found the interface that we want to use.
|
|
|
|
*/
|
|
|
|
for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
|
|
|
|
if (ifap->ifa_addr->sa_family != clnt->sa_family ||
|
|
|
|
!(ifap->ifa_flags & IFF_UP))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (clnt->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
/*
|
|
|
|
* realsin: address that recvfrom gave us.
|
|
|
|
* ifsin: address of interface being examined.
|
|
|
|
* clntsin: address that client want us to contact
|
|
|
|
* it on
|
|
|
|
* servsin: local address of RPC service.
|
|
|
|
* sinmask: netmask of this interface
|
|
|
|
* newsin: initially a copy of clntsin, eventually
|
|
|
|
* the merged address
|
|
|
|
*/
|
|
|
|
servsin = (struct sockaddr_in *)serv_sa;
|
|
|
|
clntsin = (struct sockaddr_in *)clnt_sa;
|
|
|
|
sinmask = (struct sockaddr_in *)ifap->ifa_netmask;
|
|
|
|
newsin = (struct sockaddr_in *)&ss;
|
|
|
|
ifsin = (struct sockaddr_in *)ifap->ifa_addr;
|
|
|
|
if (!bitmaskcmp(&ifsin->sin_addr, &clntsin->sin_addr,
|
|
|
|
&sinmask->sin_addr, sizeof (struct in_addr))) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
/*
|
|
|
|
* realsin6: address that recvfrom gave us.
|
|
|
|
* ifsin6: address of interface being examined.
|
|
|
|
* clntsin6: address that client want us to contact
|
|
|
|
* it on
|
|
|
|
* servsin6: local address of RPC service.
|
|
|
|
* sin6mask: netmask of this interface
|
|
|
|
* newsin6: initially a copy of clntsin, eventually
|
|
|
|
* the merged address
|
|
|
|
*
|
|
|
|
* For v6 link local addresses, if the client contacted
|
|
|
|
* us via a link-local address, and wants us to reply
|
|
|
|
* to one, use the scope id to see which one.
|
|
|
|
*/
|
|
|
|
realsin6 = (struct sockaddr_in6 *)clnt;
|
|
|
|
ifsin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
|
2013-10-19 21:16:25 +04:00
|
|
|
inet6_getscopeid(ifsin6, 1);
|
2000-06-03 03:15:41 +04:00
|
|
|
clntsin6 = (struct sockaddr_in6 *)clnt_sa;
|
|
|
|
servsin6 = (struct sockaddr_in6 *)serv_sa;
|
|
|
|
sin6mask = (struct sockaddr_in6 *)ifap->ifa_netmask;
|
|
|
|
newsin6 = (struct sockaddr_in6 *)&ss;
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&ifsin6->sin6_addr) &&
|
|
|
|
IN6_IS_ADDR_LINKLOCAL(&realsin6->sin6_addr) &&
|
|
|
|
IN6_IS_ADDR_LINKLOCAL(&clntsin6->sin6_addr)) {
|
|
|
|
if (ifsin6->sin6_scope_id !=
|
|
|
|
realsin6->sin6_scope_id)
|
|
|
|
continue;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (!bitmaskcmp(&ifsin6->sin6_addr,
|
|
|
|
&clntsin6->sin6_addr, &sin6mask->sin6_addr,
|
|
|
|
sizeof (struct in6_addr)))
|
2001-01-10 08:23:43 +03:00
|
|
|
goto found;
|
2000-06-03 03:15:41 +04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
goto freeit;
|
|
|
|
}
|
|
|
|
}
|
2000-08-03 04:04:30 +04:00
|
|
|
/*
|
|
|
|
* Didn't find anything. Get the first possibly useful interface,
|
|
|
|
* preferring "normal" interfaces to point-to-point and loopback
|
|
|
|
* ones.
|
|
|
|
*/
|
|
|
|
bestif = NULL;
|
|
|
|
for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
|
|
|
|
if (ifap->ifa_addr->sa_family != clnt->sa_family ||
|
|
|
|
!(ifap->ifa_flags & IFF_UP))
|
|
|
|
continue;
|
|
|
|
if (!(ifap->ifa_flags & IFF_LOOPBACK) &&
|
|
|
|
!(ifap->ifa_flags & IFF_POINTOPOINT)) {
|
|
|
|
bestif = ifap;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bestif == NULL)
|
|
|
|
bestif = ifap;
|
|
|
|
else if ((bestif->ifa_flags & IFF_LOOPBACK) &&
|
|
|
|
!(ifap->ifa_flags & IFF_LOOPBACK))
|
|
|
|
bestif = ifap;
|
|
|
|
}
|
|
|
|
ifap = bestif;
|
2000-06-03 03:15:41 +04:00
|
|
|
found:
|
2001-01-10 08:23:43 +03:00
|
|
|
switch (clnt->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
memcpy(newsin, ifap->ifa_addr, clnt_sa->sa_len);
|
|
|
|
newsin->sin_port = servsin->sin_port;
|
|
|
|
tbuf.len = clnt_sa->sa_len;
|
|
|
|
tbuf.maxlen = sizeof (struct sockaddr_storage);
|
|
|
|
tbuf.buf = newsin;
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2006-05-25 06:37:38 +04:00
|
|
|
assert(newsin6);
|
2001-01-10 08:23:43 +03:00
|
|
|
memcpy(newsin6, ifsin6, clnt_sa->sa_len);
|
|
|
|
newsin6->sin6_port = servsin6->sin6_port;
|
|
|
|
tbuf.maxlen = sizeof (struct sockaddr_storage);
|
|
|
|
tbuf.len = clnt_sa->sa_len;
|
|
|
|
tbuf.buf = newsin6;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
goto freeit;
|
|
|
|
}
|
2000-06-03 03:15:41 +04:00
|
|
|
if (ifap != NULL)
|
|
|
|
ret = taddr2uaddr(nconf, &tbuf);
|
|
|
|
freeit:
|
2000-06-22 14:59:55 +04:00
|
|
|
freenetconfigent(nconf);
|
2000-06-03 03:15:41 +04:00
|
|
|
free(serv_sa);
|
|
|
|
free(serv_nbp);
|
|
|
|
if (clnt_sa != NULL)
|
|
|
|
free(clnt_sa);
|
|
|
|
if (clnt_nbp != NULL)
|
|
|
|
free(clnt_nbp);
|
|
|
|
freeifaddrs(ifp);
|
|
|
|
|
2007-08-27 23:53:32 +04:00
|
|
|
#ifdef RPCBIND_DEBUG
|
2000-06-03 03:15:41 +04:00
|
|
|
if (debugging)
|
|
|
|
fprintf(stderr, "addrmerge: returning %s\n", ret);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
network_init()
|
|
|
|
{
|
|
|
|
#ifdef INET6
|
|
|
|
struct ifaddrs *ifap, *ifp;
|
|
|
|
struct ipv6_mreq mreq6;
|
2009-01-18 13:10:47 +03:00
|
|
|
unsigned int ifindex;
|
|
|
|
int s;
|
2000-06-03 03:15:41 +04:00
|
|
|
#endif
|
|
|
|
int ecode;
|
|
|
|
struct addrinfo hints, *res;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
|
|
|
|
if (debugging)
|
|
|
|
fprintf(stderr, "can't get local ip4 address: %s\n",
|
|
|
|
gai_strerror(ecode));
|
|
|
|
} else {
|
|
|
|
local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
|
|
|
|
if (local_in4 == NULL) {
|
|
|
|
if (debugging)
|
|
|
|
fprintf(stderr, "can't alloc local ip4 addr\n");
|
|
|
|
}
|
|
|
|
memcpy(local_in4, res->ai_addr, sizeof *local_in4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
hints.ai_family = AF_INET6;
|
|
|
|
if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
|
|
|
|
if (debugging)
|
|
|
|
fprintf(stderr, "can't get local ip6 address: %s\n",
|
|
|
|
gai_strerror(ecode));
|
|
|
|
} else {
|
|
|
|
local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
|
|
|
|
if (local_in6 == NULL) {
|
|
|
|
if (debugging)
|
|
|
|
fprintf(stderr, "can't alloc local ip6 addr\n");
|
|
|
|
}
|
|
|
|
memcpy(local_in6, res->ai_addr, sizeof *local_in6);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now join the RPC ipv6 multicast group on all interfaces.
|
|
|
|
*/
|
|
|
|
if (getifaddrs(&ifp) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mreq6.ipv6mr_interface = 0;
|
|
|
|
inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
|
|
|
|
|
|
|
|
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all interfaces. For each interface, see if the
|
|
|
|
* network portion of its address is equal to that of the client.
|
|
|
|
* If so, we have found the interface that we want to use.
|
|
|
|
*/
|
|
|
|
for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
|
|
|
|
if (ifap->ifa_addr->sa_family != AF_INET6 ||
|
|
|
|
!(ifap->ifa_flags & IFF_MULTICAST))
|
|
|
|
continue;
|
|
|
|
ifindex = if_nametoindex(ifap->ifa_name);
|
|
|
|
if (ifindex == mreq6.ipv6mr_interface)
|
|
|
|
/*
|
|
|
|
* Already did this one.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
mreq6.ipv6mr_interface = ifindex;
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
|
|
|
|
sizeof mreq6) < 0)
|
|
|
|
if (debugging)
|
2015-05-09 21:32:04 +03:00
|
|
|
warn("setsockopt v6 multicast");
|
2000-06-03 03:15:41 +04:00
|
|
|
}
|
2015-03-11 18:57:12 +03:00
|
|
|
freeifaddrs(ifp);
|
2000-06-03 03:15:41 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* close(s); */
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr *
|
|
|
|
local_sa(int af)
|
|
|
|
{
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
return (struct sockaddr *)local_in4;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
return (struct sockaddr *)local_in6;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|