Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / usr.sbin / rpcbind / util.c
blob8fe42fe5e6e52044625d06bfc2a52245c346651b
1 /*-
2 * Copyright (c) 2000 The NetBSD Foundation, Inc.
3 * All rights reserved.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Frank van der Linden.
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.
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.
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 * $DragonFly$
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46 #include <ifaddrs.h>
47 #include <sys/poll.h>
48 #include <rpc/rpc.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <netdb.h>
54 #include <netconfig.h>
55 #include <stdio.h>
56 #include <arpa/inet.h>
58 #include "rpcbind.h"
60 #define SA2SIN(sa) ((struct sockaddr_in *)(sa))
61 #define SA2SINADDR(sa) (SA2SIN(sa)->sin_addr)
62 #ifdef INET6
63 #define SA2SIN6(sa) ((struct sockaddr_in6 *)(sa))
64 #define SA2SIN6ADDR(sa) (SA2SIN6(sa)->sin6_addr)
65 #endif
67 static struct sockaddr_in *local_in4;
68 #ifdef INET6
69 static struct sockaddr_in6 *local_in6;
70 #endif
72 static int bitmaskcmp(void *, void *, void *, int);
73 #ifdef INET6
74 static void in6_fillscopeid(struct sockaddr_in6 *);
75 #endif
78 * For all bits set in "mask", compare the corresponding bits in
79 * "dst" and "src", and see if they match. Returns 0 if the addresses
80 * match.
82 static int
83 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
85 int i;
86 u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
88 for (i = 0; i < bytelen; i++)
89 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
90 return (1);
91 return (0);
95 * Similar to code in ifconfig.c. Fill in the scope ID for link-local
96 * addresses returned by getifaddrs().
98 #ifdef INET6
99 static void
100 in6_fillscopeid(struct sockaddr_in6 *sin6)
102 u_int16_t ifindex;
104 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
105 ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
106 if (sin6->sin6_scope_id == 0 && ifindex != 0) {
107 sin6->sin6_scope_id = ifindex;
108 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
112 #endif
115 * Find a server address that can be used by `caller' to contact
116 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
117 * non-NULL, it is used instead of `caller' as a hint suggesting
118 * the best address (e.g. the `r_addr' field of an rpc, which
119 * contains the rpcbind server address that the caller used).
121 * Returns the best server address as a malloc'd "universal address"
122 * string which should be freed by the caller. On error, returns NULL.
124 char *
125 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
126 char *netid)
128 struct ifaddrs *ifap, *ifp = NULL, *bestif;
129 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
130 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
131 struct sockaddr_storage ss;
132 struct netconfig *nconf;
133 char *caller_uaddr = NULL, *hint_uaddr = NULL;
134 char *ret = NULL;
136 #ifdef ND_DEBUG
137 if (debugging)
138 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
139 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
140 #endif
141 caller_sa = caller->buf;
142 if ((nconf = rpcbind_get_conf(netid)) == NULL)
143 goto freeit;
144 if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
145 goto freeit;
148 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
149 * address family is different from that of the caller.
151 hint_sa = NULL;
152 if (clnt_uaddr != NULL) {
153 hint_uaddr = clnt_uaddr;
154 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
155 goto freeit;
156 hint_sa = hint_nbp->buf;
158 if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
159 hint_uaddr = caller_uaddr;
160 hint_sa = caller->buf;
163 #ifdef ND_DEBUG
164 if (debugging)
165 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
166 #endif
167 /* Local caller, just return the server address. */
168 if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
169 strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
170 ret = strdup(serv_uaddr);
171 goto freeit;
174 if (getifaddrs(&ifp) < 0)
175 goto freeit;
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.
182 bestif = NULL;
183 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
184 ifsa = ifap->ifa_addr;
185 ifmasksa = ifap->ifa_netmask;
187 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
188 !(ifap->ifa_flags & IFF_UP))
189 continue;
191 switch (hint_sa->sa_family) {
192 case AF_INET:
194 * If the hint address matches this interface
195 * address/netmask, then we're done.
197 if (!bitmaskcmp(&SA2SINADDR(ifsa),
198 &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
199 sizeof(struct in_addr))) {
200 bestif = ifap;
201 goto found;
203 break;
204 #ifdef INET6
205 case AF_INET6:
207 * For v6 link local addresses, if the caller is on
208 * a link-local address then use the scope id to see
209 * which one.
211 in6_fillscopeid(SA2SIN6(ifsa));
212 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
213 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
214 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
215 if (SA2SIN6(ifsa)->sin6_scope_id ==
216 SA2SIN6(caller_sa)->sin6_scope_id) {
217 bestif = ifap;
218 goto found;
220 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
221 &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
222 sizeof(struct in6_addr))) {
223 bestif = ifap;
224 goto found;
226 break;
227 #endif
228 default:
229 continue;
233 * Remember the first possibly useful interface, preferring
234 * "normal" to point-to-point and loopback ones.
236 if (bestif == NULL ||
237 (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
238 (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
239 bestif = ifap;
241 if (bestif == NULL)
242 goto freeit;
244 found:
246 * Construct the new address using the the address from
247 * `bestif', and the port number from `serv_uaddr'.
249 serv_nbp = uaddr2taddr(nconf, serv_uaddr);
250 if (serv_nbp == NULL)
251 goto freeit;
252 serv_sa = serv_nbp->buf;
254 memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
255 switch (ss.ss_family) {
256 case AF_INET:
257 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
258 break;
259 #ifdef INET6
260 case AF_INET6:
261 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
262 break;
263 #endif
265 tbuf.len = ss.ss_len;
266 tbuf.maxlen = sizeof(ss);
267 tbuf.buf = &ss;
268 ret = taddr2uaddr(nconf, &tbuf);
270 freeit:
271 if (caller_uaddr != NULL)
272 free(caller_uaddr);
273 if (hint_nbp != NULL) {
274 free(hint_nbp->buf);
275 free(hint_nbp);
277 if (serv_nbp != NULL) {
278 free(serv_nbp->buf);
279 free(serv_nbp);
281 if (ifp != NULL)
282 freeifaddrs(ifp);
284 #ifdef ND_DEBUG
285 if (debugging)
286 fprintf(stderr, "addrmerge: returning %s\n", ret);
287 #endif
288 return ret;
291 void
292 network_init(void)
294 #ifdef INET6
295 struct ifaddrs *ifap, *ifp;
296 struct ipv6_mreq mreq6;
297 unsigned int ifindex;
298 int s;
299 #endif
300 int ecode;
301 struct addrinfo hints, *res;
303 memset(&hints, 0, sizeof hints);
304 hints.ai_family = AF_INET;
305 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
306 if (debugging)
307 fprintf(stderr, "can't get local ip4 address: %s\n",
308 gai_strerror(ecode));
309 } else {
310 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
311 if (local_in4 == NULL) {
312 if (debugging)
313 fprintf(stderr, "can't alloc local ip4 addr\n");
315 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
318 #ifdef INET6
319 hints.ai_family = AF_INET6;
320 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
321 if (debugging)
322 fprintf(stderr, "can't get local ip6 address: %s\n",
323 gai_strerror(ecode));
324 } else {
325 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
326 if (local_in6 == NULL) {
327 if (debugging)
328 fprintf(stderr, "can't alloc local ip6 addr\n");
330 memcpy(local_in6, res->ai_addr, sizeof *local_in6);
334 * Now join the RPC ipv6 multicast group on all interfaces.
336 if (getifaddrs(&ifp) < 0)
337 return;
339 mreq6.ipv6mr_interface = 0;
340 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
342 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
345 * Loop through all interfaces. For each IPv6 multicast-capable
346 * interface, join the RPC multicast group on that interface.
348 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
349 if (ifap->ifa_addr->sa_family != AF_INET6 ||
350 !(ifap->ifa_flags & IFF_MULTICAST))
351 continue;
352 ifindex = if_nametoindex(ifap->ifa_name);
353 if (ifindex == mreq6.ipv6mr_interface)
355 * Already did this one.
357 continue;
358 mreq6.ipv6mr_interface = ifindex;
359 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
360 sizeof mreq6) < 0)
361 if (debugging)
362 perror("setsockopt v6 multicast");
364 #endif
366 /* close(s); */
369 struct sockaddr *
370 local_sa(int af)
372 switch (af) {
373 case AF_INET:
374 return (struct sockaddr *)local_in4;
375 #ifdef INET6
376 case AF_INET6:
377 return (struct sockaddr *)local_in6;
378 #endif
379 default:
380 return NULL;