]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - usr.sbin/rpcbind/util.c
MFC r319414:
[FreeBSD/stable/9.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  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <ifaddrs.h>
41 #include <sys/poll.h>
42 #include <rpc/rpc.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <netdb.h>
48 #include <netconfig.h>
49 #include <stdio.h>
50 #include <arpa/inet.h>
51
52 #include "rpcbind.h"
53
54 static struct sockaddr_in *local_in4;
55 #ifdef INET6
56 static struct sockaddr_in6 *local_in6;
57 #endif
58
59 static int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
60 #ifdef INET6
61 static void in6_fillscopeid(struct sockaddr_in6 *);
62 #endif
63
64 /*
65  * For all bits set in "mask", compare the corresponding bits in
66  * "dst" and "src", and see if they match. Returns 0 if the addresses
67  * match.
68  */
69 static int
70 bitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
71 {
72         int i;
73         u_int8_t *p1, *p2, *netmask;
74         int bytelen;
75
76         if (dst->sa_family != src->sa_family ||
77             dst->sa_family != mask->sa_family)
78                 return (1);
79
80         switch (dst->sa_family) {
81         case AF_INET:
82                 p1 = (uint8_t*) &SA2SINADDR(dst);
83                 p2 = (uint8_t*) &SA2SINADDR(src);
84                 netmask = (uint8_t*) &SA2SINADDR(mask);
85                 bytelen = sizeof(struct in_addr);
86                 break;
87 #ifdef INET6
88         case AF_INET6:
89                 p1 = (uint8_t*) &SA2SIN6ADDR(dst);
90                 p2 = (uint8_t*) &SA2SIN6ADDR(src);
91                 netmask = (uint8_t*) &SA2SIN6ADDR(mask);
92                 bytelen = sizeof(struct in6_addr);
93                 break;
94 #endif
95         default:
96                 return (1);
97         }
98
99         for (i = 0; i < bytelen; i++)
100                 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
101                         return (1);
102         return (0);
103 }
104
105 /*
106  * Similar to code in ifconfig.c. Fill in the scope ID for link-local
107  * addresses returned by getifaddrs().
108  */
109 #ifdef INET6
110 static void
111 in6_fillscopeid(struct sockaddr_in6 *sin6)
112 {
113         u_int16_t ifindex;
114
115         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
116                 ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
117                 if (sin6->sin6_scope_id == 0 && ifindex != 0) {
118                         sin6->sin6_scope_id = ifindex;
119                         *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
120                 }
121         }
122 }
123 #endif
124
125 /*
126  * Find a server address that can be used by `caller' to contact
127  * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
128  * non-NULL, it is used instead of `caller' as a hint suggesting
129  * the best address (e.g. the `r_addr' field of an rpc, which
130  * contains the rpcbind server address that the caller used).
131  *
132  * Returns the best server address as a malloc'd "universal address"
133  * string which should be freed by the caller. On error, returns NULL.
134  */
135 char *
136 addrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr,
137           const char *netid)
138 {
139         struct ifaddrs *ifap, *ifp = NULL, *bestif;
140         struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
141         struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
142         struct sockaddr_storage ss;
143         struct netconfig *nconf;
144         char *caller_uaddr = NULL;
145 #ifdef ND_DEBUG
146         const char *hint_uaddr = NULL;
147 #endif
148         char *ret = NULL;
149         int bestif_goodness;
150
151 #ifdef ND_DEBUG
152         if (debugging)
153                 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
154                     clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
155 #endif
156         caller_sa = caller->buf;
157         if ((nconf = rpcbind_get_conf(netid)) == NULL)
158                 goto freeit;
159         if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
160                 goto freeit;
161
162         /*
163          * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
164          * address family is different from that of the caller.
165          */
166         hint_sa = NULL;
167         if (clnt_uaddr != NULL) {
168 #ifdef ND_DEBUG
169                 hint_uaddr = clnt_uaddr;
170 #endif
171                 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
172                         goto freeit;
173                 hint_sa = hint_nbp->buf;
174         }
175         if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
176 #ifdef ND_DEBUG
177                 hint_uaddr = caller_uaddr;
178 #endif
179                 hint_sa = caller->buf;
180         }
181
182 #ifdef ND_DEBUG
183         if (debugging)
184                 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
185 #endif
186         /* Local caller, just return the server address. */
187         if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
188             strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
189                 ret = strdup(serv_uaddr);
190                 goto freeit;
191         }
192
193         if (getifaddrs(&ifp) < 0)
194                 goto freeit;
195
196         /*
197          * Loop through all interface addresses.  We are listening to an address
198          * if any of the following are true:
199          * a) It's a loopback address
200          * b) It was specified with the -h command line option
201          * c) There were no -h command line options.
202          *
203          * Among addresses on which we are listening, choose in order of
204          * preference an address that is:
205          *
206          * a) Equal to the hint
207          * b) A link local address with the same scope ID as the client's
208          *    address, if the client's address is also link local
209          * c) An address on the same subnet as the client's address
210          * d) A non-localhost, non-p2p address
211          * e) Any usable address
212          */
213         bestif = NULL;
214         bestif_goodness = 0;
215         for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
216                 ifsa = ifap->ifa_addr;
217                 ifmasksa = ifap->ifa_netmask;
218
219                 /* Skip addresses where we don't listen */
220                 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
221                     !(ifap->ifa_flags & IFF_UP))
222                         continue;
223
224                 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
225                         continue;
226
227                 if ((hint_sa->sa_family == AF_INET) &&
228                     ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr == 
229                       ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
230                         const int goodness = 4;
231
232                         bestif_goodness = goodness;
233                         bestif = ifap;
234                         goto found;
235                 }
236 #ifdef INET6
237                 if ((hint_sa->sa_family == AF_INET6) &&
238                     (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
239                                  &((struct sockaddr_in6*)ifsa)->sin6_addr,
240                                  sizeof(struct in6_addr))) &&
241                     (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
242                     (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
243                         const int goodness = 4;
244
245                         bestif_goodness = goodness;
246                         bestif = ifap;
247                         goto found;
248                 }
249                 if (hint_sa->sa_family == AF_INET6) {
250                         /*
251                          * For v6 link local addresses, if the caller is on
252                          * a link-local address then use the scope id to see
253                          * which one.
254                          */
255                         in6_fillscopeid(SA2SIN6(ifsa));
256                         if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
257                             IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
258                             IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
259                                 if (SA2SIN6(ifsa)->sin6_scope_id ==
260                                     SA2SIN6(caller_sa)->sin6_scope_id) {
261                                         const int goodness = 3;
262
263                                         if (bestif_goodness < goodness) {
264                                                 bestif = ifap;
265                                                 bestif_goodness = goodness;
266                                         }
267                                 }
268                         }
269                 }
270 #endif /* INET6 */
271                 if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
272                         const int goodness = 2;
273
274                         if (bestif_goodness < goodness) {
275                                 bestif = ifap;
276                                 bestif_goodness = goodness;
277                         }
278                 }
279                 if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
280                         const int goodness = 1;
281
282                         if (bestif_goodness < goodness) {
283                                 bestif = ifap;
284                                 bestif_goodness = goodness;
285                         }
286                 }
287                 if (bestif == NULL)
288                         bestif = ifap;
289         }
290         if (bestif == NULL)
291                 goto freeit;
292
293 found:
294         /*
295          * Construct the new address using the address from
296          * `bestif', and the port number from `serv_uaddr'.
297          */
298         serv_nbp = uaddr2taddr(nconf, serv_uaddr);
299         if (serv_nbp == NULL)
300                 goto freeit;
301         serv_sa = serv_nbp->buf;
302
303         memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
304         switch (ss.ss_family) {
305         case AF_INET:
306                 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
307                 break;
308 #ifdef INET6
309         case AF_INET6:
310                 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
311                 break;
312 #endif
313         }
314         tbuf.len = ss.ss_len;
315         tbuf.maxlen = sizeof(ss);
316         tbuf.buf = &ss;
317         ret = taddr2uaddr(nconf, &tbuf);
318
319 freeit:
320         free(caller_uaddr);
321         if (hint_nbp != NULL) {
322                 free(hint_nbp->buf);
323                 free(hint_nbp);
324         }
325         if (serv_nbp != NULL) {
326                 free(serv_nbp->buf);
327                 free(serv_nbp);
328         }
329         if (ifp != NULL)
330                 freeifaddrs(ifp);
331
332 #ifdef ND_DEBUG
333         if (debugging)
334                 fprintf(stderr, "addrmerge: returning %s\n", ret);
335 #endif
336         return ret;
337 }
338
339 void
340 network_init(void)
341 {
342 #ifdef INET6
343         struct ifaddrs *ifap, *ifp;
344         struct ipv6_mreq mreq6;
345         unsigned int ifindex;
346         int s;
347 #endif
348         int ecode;
349         struct addrinfo hints, *res;
350
351         memset(&hints, 0, sizeof hints);
352         hints.ai_family = AF_INET;
353         if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
354                 if (debugging)
355                         fprintf(stderr, "can't get local ip4 address: %s\n",
356                             gai_strerror(ecode));
357         } else {
358                 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
359                 if (local_in4 == NULL) {
360                         if (debugging)
361                                 fprintf(stderr, "can't alloc local ip4 addr\n");
362                         exit(1);
363                 }
364                 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
365                 freeaddrinfo(res);
366         }
367
368 #ifdef INET6
369         hints.ai_family = AF_INET6;
370         if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
371                 if (debugging)
372                         fprintf(stderr, "can't get local ip6 address: %s\n",
373                             gai_strerror(ecode));
374         } else {
375                 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
376                 if (local_in6 == NULL) {
377                         if (debugging)
378                                 fprintf(stderr, "can't alloc local ip6 addr\n");
379                         exit(1);
380                 }
381                 memcpy(local_in6, res->ai_addr, sizeof *local_in6);
382                 freeaddrinfo(res);
383         }
384
385         /*
386          * Now join the RPC ipv6 multicast group on all interfaces.
387          */
388         if (getifaddrs(&ifp) < 0)
389                 return;
390
391         mreq6.ipv6mr_interface = 0;
392         inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
393
394         s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
395         if (s == -1) {
396                 if (debugging)
397                         fprintf(stderr, "couldn't create ip6 socket");
398                 goto done_inet6;
399         }
400
401         /*
402          * Loop through all interfaces. For each IPv6 multicast-capable
403          * interface, join the RPC multicast group on that interface.
404          */
405         for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
406                 if (ifap->ifa_addr->sa_family != AF_INET6 ||
407                     !(ifap->ifa_flags & IFF_MULTICAST))
408                         continue;
409                 ifindex = if_nametoindex(ifap->ifa_name);
410                 if (ifindex == mreq6.ipv6mr_interface)
411                         /*
412                          * Already did this one.
413                          */
414                         continue;
415                 mreq6.ipv6mr_interface = ifindex;
416                 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
417                     sizeof mreq6) < 0)
418                         if (debugging)
419                                 perror("setsockopt v6 multicast");
420         }
421 done_inet6:
422         freeifaddrs(ifp);
423 #endif
424
425         /* close(s); */
426 }
427
428 struct sockaddr *
429 local_sa(int af)
430 {
431         switch (af) {
432         case AF_INET:
433                 return (struct sockaddr *)local_in4;
434 #ifdef INET6
435         case AF_INET6:
436                 return (struct sockaddr *)local_in6;
437 #endif
438         default:
439                 return NULL;
440         }
441 }