xref: /freebsd/usr.sbin/rpcbind/util.c (revision d8b878873e7aa8df1972cc6a642804b17eb61087)
1 /*
2  * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
3  * $FreeBSD$
4  */
5 
6 /*-
7  * Copyright (c) 2000 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Frank van der Linden.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <ifaddrs.h>
41 #include <sys/poll.h>
42 #include <rpc/rpc.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <netdb.h>
48 #include <netconfig.h>
49 #include <stdio.h>
50 #include <arpa/inet.h>
51 
52 #include "rpcbind.h"
53 
54 static struct sockaddr_in *local_in4;
55 #ifdef INET6
56 static struct sockaddr_in6 *local_in6;
57 #endif
58 
59 static int bitmaskcmp(void *, void *, void *, int);
60 #ifdef INET6
61 static void in6_fillscopeid(struct sockaddr_in6 *);
62 #endif
63 
64 /*
65  * For all bits set in "mask", compare the corresponding bits in
66  * "dst" and "src", and see if they match. Returns 0 if the addresses
67  * match.
68  */
69 static int
70 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
71 {
72 	int i;
73 	u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
74 
75 	for (i = 0; i < bytelen; i++)
76 		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
77 			return (1);
78 	return (0);
79 }
80 
81 /*
82  * Similar to code in ifconfig.c. Fill in the scope ID for link-local
83  * addresses returned by getifaddrs().
84  */
85 #ifdef INET6
86 static void
87 in6_fillscopeid(struct sockaddr_in6 *sin6)
88 {
89 	u_int16_t ifindex;
90 
91         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
92 		ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
93 		if (sin6->sin6_scope_id == 0 && ifindex != 0) {
94 			sin6->sin6_scope_id = ifindex;
95 			*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
96 		}
97 	}
98 }
99 #endif
100 
101 /*
102  * Find a server address that can be used by `caller' to contact
103  * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
104  * non-NULL, it is used instead of `caller' as a hint suggesting
105  * the best address (e.g. the `r_addr' field of an rpc, which
106  * contains the rpcbind server address that the caller used).
107  *
108  * Returns the best server address as a malloc'd "universal address"
109  * string which should be freed by the caller. On error, returns NULL.
110  */
111 char *
112 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
113 	  char *netid)
114 {
115 	struct ifaddrs *ifap, *ifp = NULL, *bestif;
116 	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
117 	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
118 	struct sockaddr_storage ss;
119 	struct netconfig *nconf;
120 	char *caller_uaddr = NULL, *hint_uaddr = NULL;
121 	char *ret = NULL;
122 
123 #ifdef ND_DEBUG
124 	if (debugging)
125 		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
126 		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
127 #endif
128 	caller_sa = caller->buf;
129 	if ((nconf = rpcbind_get_conf(netid)) == NULL)
130 		goto freeit;
131 	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
132 		goto freeit;
133 
134 	/*
135 	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
136 	 * address family is different from that of the caller.
137 	 */
138 	hint_sa = NULL;
139 	if (clnt_uaddr != NULL) {
140 		hint_uaddr = clnt_uaddr;
141 		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
142 			goto freeit;
143 		hint_sa = hint_nbp->buf;
144 	}
145 	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
146 		hint_uaddr = caller_uaddr;
147 		hint_sa = caller->buf;
148 	}
149 
150 #ifdef ND_DEBUG
151 	if (debugging)
152 		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
153 #endif
154 	/* Local caller, just return the server address. */
155 	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
156 	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
157 		ret = strdup(serv_uaddr);
158 		goto freeit;
159 	}
160 
161 	if (getifaddrs(&ifp) < 0)
162 		goto freeit;
163 
164 	/*
165 	 * Loop through all interfaces. For each interface, see if it
166 	 * is either the loopback interface (which we always listen
167 	 * on) or is one of the addresses the program bound to (the
168 	 * wildcard by default, or a subset if -h is specified) and
169 	 * the network portion of its address is equal to that of the
170 	 * client.  If so, we have found the interface that we want to
171 	 * use.
172 	 */
173 	bestif = NULL;
174 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
175 		ifsa = ifap->ifa_addr;
176 		ifmasksa = ifap->ifa_netmask;
177 
178 		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
179 		    !(ifap->ifa_flags & IFF_UP))
180 			continue;
181 
182 		if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
183 			continue;
184 
185 		switch (hint_sa->sa_family) {
186 		case AF_INET:
187 			/*
188 			 * If the hint address matches this interface
189 			 * address/netmask, then we're done.
190 			 */
191 			if (!bitmaskcmp(&SA2SINADDR(ifsa),
192 			    &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
193 			    sizeof(struct in_addr))) {
194 				bestif = ifap;
195 				goto found;
196 			}
197 			break;
198 #ifdef INET6
199 		case AF_INET6:
200 			/*
201 			 * For v6 link local addresses, if the caller is on
202 			 * a link-local address then use the scope id to see
203 			 * which one.
204 			 */
205 			in6_fillscopeid(SA2SIN6(ifsa));
206 			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
207 			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
208 			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
209 				if (SA2SIN6(ifsa)->sin6_scope_id ==
210 				    SA2SIN6(caller_sa)->sin6_scope_id) {
211 					bestif = ifap;
212 					goto found;
213 				}
214 			} else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
215 			    &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
216 			    sizeof(struct in6_addr))) {
217 				bestif = ifap;
218 				goto found;
219 			}
220 			break;
221 #endif
222 		default:
223 			continue;
224 		}
225 
226 		/*
227 		 * Remember the first possibly useful interface, preferring
228 		 * "normal" to point-to-point and loopback ones.
229 		 */
230 		if (bestif == NULL ||
231 		    (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
232 		    (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
233 			bestif = ifap;
234 	}
235 	if (bestif == NULL)
236 		goto freeit;
237 
238 found:
239 	/*
240 	 * Construct the new address using the the address from
241 	 * `bestif', and the port number from `serv_uaddr'.
242 	 */
243 	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
244 	if (serv_nbp == NULL)
245 		goto freeit;
246 	serv_sa = serv_nbp->buf;
247 
248 	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
249 	switch (ss.ss_family) {
250 	case AF_INET:
251 		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
252 		break;
253 #ifdef INET6
254 	case AF_INET6:
255 		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
256 		break;
257 #endif
258 	}
259 	tbuf.len = ss.ss_len;
260 	tbuf.maxlen = sizeof(ss);
261 	tbuf.buf = &ss;
262 	ret = taddr2uaddr(nconf, &tbuf);
263 
264 freeit:
265 	if (caller_uaddr != NULL)
266 		free(caller_uaddr);
267 	if (hint_nbp != NULL) {
268 		free(hint_nbp->buf);
269 		free(hint_nbp);
270 	}
271 	if (serv_nbp != NULL) {
272 		free(serv_nbp->buf);
273 		free(serv_nbp);
274 	}
275 	if (ifp != NULL)
276 		freeifaddrs(ifp);
277 
278 #ifdef ND_DEBUG
279 	if (debugging)
280 		fprintf(stderr, "addrmerge: returning %s\n", ret);
281 #endif
282 	return ret;
283 }
284 
285 void
286 network_init()
287 {
288 #ifdef INET6
289 	struct ifaddrs *ifap, *ifp;
290 	struct ipv6_mreq mreq6;
291 	unsigned int ifindex;
292 	int s;
293 #endif
294 	int ecode;
295 	struct addrinfo hints, *res;
296 
297 	memset(&hints, 0, sizeof hints);
298 	hints.ai_family = AF_INET;
299 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
300 		if (debugging)
301 			fprintf(stderr, "can't get local ip4 address: %s\n",
302 			    gai_strerror(ecode));
303 	} else {
304 		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
305 		if (local_in4 == NULL) {
306 			if (debugging)
307 				fprintf(stderr, "can't alloc local ip4 addr\n");
308 		}
309 		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
310 	}
311 
312 #ifdef INET6
313 	hints.ai_family = AF_INET6;
314 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
315 		if (debugging)
316 			fprintf(stderr, "can't get local ip6 address: %s\n",
317 			    gai_strerror(ecode));
318 	} else {
319 		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
320 		if (local_in6 == NULL) {
321 			if (debugging)
322 				fprintf(stderr, "can't alloc local ip6 addr\n");
323 		}
324 		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
325 	}
326 
327 	/*
328 	 * Now join the RPC ipv6 multicast group on all interfaces.
329 	 */
330 	if (getifaddrs(&ifp) < 0)
331 		return;
332 
333 	mreq6.ipv6mr_interface = 0;
334 	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
335 
336 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
337 
338 	/*
339 	 * Loop through all interfaces. For each IPv6 multicast-capable
340 	 * interface, join the RPC multicast group on that interface.
341 	 */
342 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
343 		if (ifap->ifa_addr->sa_family != AF_INET6 ||
344 		    !(ifap->ifa_flags & IFF_MULTICAST))
345 			continue;
346 		ifindex = if_nametoindex(ifap->ifa_name);
347 		if (ifindex == mreq6.ipv6mr_interface)
348 			/*
349 			 * Already did this one.
350 			 */
351 			continue;
352 		mreq6.ipv6mr_interface = ifindex;
353 		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
354 		    sizeof mreq6) < 0)
355 			if (debugging)
356 				perror("setsockopt v6 multicast");
357 	}
358 #endif
359 
360 	/* close(s); */
361 }
362 
363 struct sockaddr *
364 local_sa(int af)
365 {
366 	switch (af) {
367 	case AF_INET:
368 		return (struct sockaddr *)local_in4;
369 #ifdef INET6
370 	case AF_INET6:
371 		return (struct sockaddr *)local_in6;
372 #endif
373 	default:
374 		return NULL;
375 	}
376 }
377