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