]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.sbin/rpcbind/util.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.sbin / rpcbind / util.c
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 static struct sockaddr_in *local_in4;
62 #ifdef INET6
63 static struct sockaddr_in6 *local_in6;
64 #endif
65
66 static int bitmaskcmp(void *, void *, void *, int);
67 #ifdef INET6
68 static void in6_fillscopeid(struct sockaddr_in6 *);
69 #endif
70
71 /*
72  * For all bits set in "mask", compare the corresponding bits in
73  * "dst" and "src", and see if they match. Returns 0 if the addresses
74  * match.
75  */
76 static int
77 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
78 {
79         int i;
80         u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
81
82         for (i = 0; i < bytelen; i++)
83                 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
84                         return (1);
85         return (0);
86 }
87
88 /*
89  * Similar to code in ifconfig.c. Fill in the scope ID for link-local
90  * addresses returned by getifaddrs().
91  */
92 #ifdef INET6
93 static void
94 in6_fillscopeid(struct sockaddr_in6 *sin6)
95 {
96         u_int16_t ifindex;
97
98         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
99                 ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
100                 if (sin6->sin6_scope_id == 0 && ifindex != 0) {
101                         sin6->sin6_scope_id = ifindex;
102                         *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
103                 }
104         }
105 }
106 #endif
107
108 /*
109  * Find a server address that can be used by `caller' to contact
110  * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
111  * non-NULL, it is used instead of `caller' as a hint suggesting
112  * the best address (e.g. the `r_addr' field of an rpc, which
113  * contains the rpcbind server address that the caller used).
114  *
115  * Returns the best server address as a malloc'd "universal address"
116  * string which should be freed by the caller. On error, returns NULL.
117  */
118 char *
119 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
120           char *netid)
121 {
122         struct ifaddrs *ifap, *ifp = NULL, *bestif;
123         struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
124         struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
125         struct sockaddr_storage ss;
126         struct netconfig *nconf;
127         char *caller_uaddr = NULL, *hint_uaddr = NULL;
128         char *ret = NULL;
129
130 #ifdef ND_DEBUG
131         if (debugging)
132                 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
133                     clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
134 #endif
135         caller_sa = caller->buf;
136         if ((nconf = rpcbind_get_conf(netid)) == NULL)
137                 goto freeit;
138         if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
139                 goto freeit;
140
141         /*
142          * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
143          * address family is different from that of the caller.
144          */
145         hint_sa = NULL;
146         if (clnt_uaddr != NULL) {
147                 hint_uaddr = clnt_uaddr;
148                 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
149                         goto freeit;
150                 hint_sa = hint_nbp->buf;
151         }
152         if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
153                 hint_uaddr = caller_uaddr;
154                 hint_sa = caller->buf;
155         }
156
157 #ifdef ND_DEBUG
158         if (debugging)
159                 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
160 #endif
161         /* Local caller, just return the server address. */
162         if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
163             strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
164                 ret = strdup(serv_uaddr);
165                 goto freeit;
166         }
167
168         if (getifaddrs(&ifp) < 0)
169                 goto freeit;
170
171         /*
172          * Loop through all interfaces. For each interface, see if it
173          * is either the loopback interface (which we always listen
174          * on) or is one of the addresses the program bound to (the
175          * wildcard by default, or a subset if -h is specified) and
176          * the network portion of its address is equal to that of the
177          * client.  If so, we have found the interface that we want to
178          * use.
179          */
180         bestif = NULL;
181         for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
182                 ifsa = ifap->ifa_addr;
183                 ifmasksa = ifap->ifa_netmask;
184
185                 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
186                     !(ifap->ifa_flags & IFF_UP))
187                         continue;
188
189                 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
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 }