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