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