xref: /freebsd/usr.sbin/rpcbind/util.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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(void *, void *, void *, int);
74 #ifdef INET6
75 static void in6_fillscopeid(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. Returns 0 if the addresses
81  * match.
82  */
83 static int
84 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
85 {
86 	int i;
87 	u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
88 
89 	for (i = 0; i < bytelen; i++)
90 		if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
91 			return (1);
92 	return (0);
93 }
94 
95 /*
96  * Similar to code in ifconfig.c. Fill in the scope ID for link-local
97  * addresses returned by getifaddrs().
98  */
99 #ifdef INET6
100 static void
101 in6_fillscopeid(struct sockaddr_in6 *sin6)
102 {
103 	u_int16_t ifindex;
104 
105         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
106 		ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
107 		if (sin6->sin6_scope_id == 0 && ifindex != 0) {
108 			sin6->sin6_scope_id = ifindex;
109 			*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
110 		}
111 	}
112 }
113 #endif
114 
115 /*
116  * Find a server address that can be used by `caller' to contact
117  * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
118  * non-NULL, it is used instead of `caller' as a hint suggesting
119  * the best address (e.g. the `r_addr' field of an rpc, which
120  * contains the rpcbind server address that the caller used).
121  *
122  * Returns the best server address as a malloc'd "universal address"
123  * string which should be freed by the caller. On error, returns NULL.
124  */
125 char *
126 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
127 	  char *netid)
128 {
129 	struct ifaddrs *ifap, *ifp = NULL, *bestif;
130 	struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
131 	struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
132 	struct sockaddr_storage ss;
133 	struct netconfig *nconf;
134 	char *caller_uaddr = NULL, *hint_uaddr = NULL;
135 	char *ret = NULL;
136 
137 #ifdef ND_DEBUG
138 	if (debugging)
139 		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
140 		    clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
141 #endif
142 	caller_sa = caller->buf;
143 	if ((nconf = rpcbind_get_conf(netid)) == NULL)
144 		goto freeit;
145 	if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
146 		goto freeit;
147 
148 	/*
149 	 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
150 	 * address family is different from that of the caller.
151 	 */
152 	hint_sa = NULL;
153 	if (clnt_uaddr != NULL) {
154 		hint_uaddr = clnt_uaddr;
155 		if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
156 			goto freeit;
157 		hint_sa = hint_nbp->buf;
158 	}
159 	if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
160 		hint_uaddr = caller_uaddr;
161 		hint_sa = caller->buf;
162 	}
163 
164 #ifdef ND_DEBUG
165 	if (debugging)
166 		fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
167 #endif
168 	/* Local caller, just return the server address. */
169 	if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
170 	    strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
171 		ret = strdup(serv_uaddr);
172 		goto freeit;
173 	}
174 
175 	if (getifaddrs(&ifp) < 0)
176 		goto freeit;
177 
178 	/*
179 	 * Loop through all interfaces. For each interface, see if the
180 	 * network portion of its address is equal to that of the client.
181 	 * If so, we have found the interface that we want to use.
182 	 */
183 	bestif = NULL;
184 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
185 		ifsa = ifap->ifa_addr;
186 		ifmasksa = ifap->ifa_netmask;
187 
188 		if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
189 		    !(ifap->ifa_flags & IFF_UP))
190 			continue;
191 
192 		switch (hint_sa->sa_family) {
193 		case AF_INET:
194 			/*
195 			 * If the hint address matches this interface
196 			 * address/netmask, then we're done.
197 			 */
198 			if (!bitmaskcmp(&SA2SINADDR(ifsa),
199 			    &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
200 			    sizeof(struct in_addr))) {
201 				bestif = ifap;
202 				goto found;
203 			}
204 			break;
205 #ifdef INET6
206 		case AF_INET6:
207 			/*
208 			 * For v6 link local addresses, if the caller is on
209 			 * a link-local address then use the scope id to see
210 			 * which one.
211 			 */
212 			in6_fillscopeid(SA2SIN6(ifsa));
213 			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
214 			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
215 			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
216 				if (SA2SIN6(ifsa)->sin6_scope_id ==
217 				    SA2SIN6(caller_sa)->sin6_scope_id) {
218 					bestif = ifap;
219 					goto found;
220 				}
221 			} else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
222 			    &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
223 			    sizeof(struct in6_addr))) {
224 				bestif = ifap;
225 				goto found;
226 			}
227 			break;
228 #endif
229 		default:
230 			continue;
231 		}
232 
233 		/*
234 		 * Remember the first possibly useful interface, preferring
235 		 * "normal" to point-to-point and loopback ones.
236 		 */
237 		if (bestif == NULL ||
238 		    (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
239 		    (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
240 			bestif = ifap;
241 	}
242 	if (bestif == NULL)
243 		goto freeit;
244 
245 found:
246 	/*
247 	 * Construct the new address using the the address from
248 	 * `bestif', and the port number from `serv_uaddr'.
249 	 */
250 	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
251 	if (serv_nbp == NULL)
252 		goto freeit;
253 	serv_sa = serv_nbp->buf;
254 
255 	memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
256 	switch (ss.ss_family) {
257 	case AF_INET:
258 		SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
259 		break;
260 #ifdef INET6
261 	case AF_INET6:
262 		SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
263 		break;
264 #endif
265 	}
266 	tbuf.len = ss.ss_len;
267 	tbuf.maxlen = sizeof(ss);
268 	tbuf.buf = &ss;
269 	ret = taddr2uaddr(nconf, &tbuf);
270 
271 freeit:
272 	if (caller_uaddr != NULL)
273 		free(caller_uaddr);
274 	if (hint_nbp != NULL) {
275 		free(hint_nbp->buf);
276 		free(hint_nbp);
277 	}
278 	if (serv_nbp != NULL) {
279 		free(serv_nbp->buf);
280 		free(serv_nbp);
281 	}
282 	if (ifp != NULL)
283 		freeifaddrs(ifp);
284 
285 #ifdef ND_DEBUG
286 	if (debugging)
287 		fprintf(stderr, "addrmerge: returning %s\n", ret);
288 #endif
289 	return ret;
290 }
291 
292 void
293 network_init()
294 {
295 #ifdef INET6
296 	struct ifaddrs *ifap, *ifp;
297 	struct ipv6_mreq mreq6;
298 	unsigned int ifindex;
299 	int s;
300 #endif
301 	int ecode;
302 	struct addrinfo hints, *res;
303 
304 	memset(&hints, 0, sizeof hints);
305 	hints.ai_family = AF_INET;
306 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
307 		if (debugging)
308 			fprintf(stderr, "can't get local ip4 address: %s\n",
309 			    gai_strerror(ecode));
310 	} else {
311 		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
312 		if (local_in4 == NULL) {
313 			if (debugging)
314 				fprintf(stderr, "can't alloc local ip4 addr\n");
315 		}
316 		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
317 	}
318 
319 #ifdef INET6
320 	hints.ai_family = AF_INET6;
321 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
322 		if (debugging)
323 			fprintf(stderr, "can't get local ip6 address: %s\n",
324 			    gai_strerror(ecode));
325 	} else {
326 		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
327 		if (local_in6 == NULL) {
328 			if (debugging)
329 				fprintf(stderr, "can't alloc local ip6 addr\n");
330 		}
331 		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
332 	}
333 
334 	/*
335 	 * Now join the RPC ipv6 multicast group on all interfaces.
336 	 */
337 	if (getifaddrs(&ifp) < 0)
338 		return;
339 
340 	mreq6.ipv6mr_interface = 0;
341 	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
342 
343 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
344 
345 	/*
346 	 * Loop through all interfaces. For each IPv6 multicast-capable
347 	 * interface, join the RPC multicast group on that interface.
348 	 */
349 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
350 		if (ifap->ifa_addr->sa_family != AF_INET6 ||
351 		    !(ifap->ifa_flags & IFF_MULTICAST))
352 			continue;
353 		ifindex = if_nametoindex(ifap->ifa_name);
354 		if (ifindex == mreq6.ipv6mr_interface)
355 			/*
356 			 * Already did this one.
357 			 */
358 			continue;
359 		mreq6.ipv6mr_interface = ifindex;
360 		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
361 		    sizeof mreq6) < 0)
362 			if (debugging)
363 				perror("setsockopt v6 multicast");
364 	}
365 #endif
366 
367 	/* close(s); */
368 }
369 
370 struct sockaddr *
371 local_sa(int af)
372 {
373 	switch (af) {
374 	case AF_INET:
375 		return (struct sockaddr *)local_in4;
376 #ifdef INET6
377 	case AF_INET6:
378 		return (struct sockaddr *)local_in6;
379 #endif
380 	default:
381 		return NULL;
382 	}
383 }
384