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