2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
6 * SPDX-License-Identifier: BSD-2-Clause
8 * Copyright (c) 2000 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Frank van der Linden.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/queue.h>
40 #include <netinet/in.h>
47 #include <netconfig.h>
49 #include <arpa/inet.h>
53 static struct sockaddr_in *local_in4;
55 static struct sockaddr_in6 *local_in6;
58 static int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
61 * For all bits set in "mask", compare the corresponding bits in
62 * "dst" and "src", and see if they match. Returns 0 if the addresses
66 bitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
69 u_int8_t *p1, *p2, *netmask;
72 if (dst->sa_family != src->sa_family ||
73 dst->sa_family != mask->sa_family)
76 switch (dst->sa_family) {
78 p1 = (uint8_t*) &SA2SINADDR(dst);
79 p2 = (uint8_t*) &SA2SINADDR(src);
80 netmask = (uint8_t*) &SA2SINADDR(mask);
81 bytelen = sizeof(struct in_addr);
85 p1 = (uint8_t*) &SA2SIN6ADDR(dst);
86 p2 = (uint8_t*) &SA2SIN6ADDR(src);
87 netmask = (uint8_t*) &SA2SIN6ADDR(mask);
88 bytelen = sizeof(struct in6_addr);
95 for (i = 0; i < bytelen; i++)
96 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
102 * Find a server address that can be used by `caller' to contact
103 * the local service specified by `serv_uaddr'. If `contct_uaddr' is
104 * non-NULL, it is used instead of `caller' as a hint suggesting
105 * the best address (e.g. the `r_addr' field of an rpc, which
106 * contains the rpcbind server address that the caller used).
108 * Returns the best server address as a malloc'd "universal address"
109 * string which should be freed by the caller. On error, returns NULL.
112 addrmerge(struct netbuf *caller, const char *serv_uaddr,
113 const char *contct_uaddr, const char *netid)
115 struct ifaddrs *ifap, *ifp = NULL, *bestif;
116 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
117 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
118 struct sockaddr_storage ss;
119 struct netconfig *nconf;
120 char *caller_uaddr = NULL;
122 const char *hint_uaddr = NULL;
129 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
130 contct_uaddr == NULL ? "NULL" : contct_uaddr, netid);
132 caller_sa = caller->buf;
133 if ((nconf = rpcbind_get_conf(netid)) == NULL)
135 if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
139 * Use `contct_uaddr' as the hint if non-NULL, but ignore it if its
140 * address family is different from that of the caller.
143 if (contct_uaddr != NULL) {
145 hint_uaddr = contct_uaddr;
147 if ((hint_nbp = uaddr2taddr(nconf, contct_uaddr)) == NULL)
149 hint_sa = hint_nbp->buf;
151 if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
153 hint_uaddr = caller_uaddr;
155 hint_sa = caller->buf;
160 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
162 /* Local caller, just return the server address. */
163 if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
164 strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
165 ret = strdup(serv_uaddr);
169 if (getifaddrs(&ifp) < 0)
173 * Loop through all interface addresses. We are listening to an address
174 * if any of the following are true:
175 * a) It's a loopback address
176 * b) It was specified with the -h command line option
177 * c) There were no -h command line options.
179 * Among addresses on which we are listening, choose in order of
180 * preference an address that is:
182 * a) Equal to the hint
183 * b) A link local address with the same scope ID as the client's
184 * address, if the client's address is also link local
185 * c) An address on the same subnet as the client's address
186 * d) A non-localhost, non-p2p address
187 * e) Any usable address
191 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
192 ifsa = ifap->ifa_addr;
193 ifmasksa = ifap->ifa_netmask;
195 /* Skip addresses where we don't listen */
196 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
197 !(ifap->ifa_flags & IFF_UP))
200 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa))
203 if ((hint_sa->sa_family == AF_INET) &&
204 ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr ==
205 ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) {
206 const int goodness = 4;
208 bestif_goodness = goodness;
213 if ((hint_sa->sa_family == AF_INET6) &&
214 (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr,
215 &((struct sockaddr_in6*)ifsa)->sin6_addr,
216 sizeof(struct in6_addr))) &&
217 (((struct sockaddr_in6*)hint_sa)->sin6_scope_id ==
218 (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) {
219 const int goodness = 4;
221 bestif_goodness = goodness;
225 if (hint_sa->sa_family == AF_INET6) {
227 * For v6 link local addresses, if the caller is on
228 * a link-local address then use the scope id to see
231 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa))) {
232 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
233 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa)) &&
234 (SA2SIN6(ifsa)->sin6_scope_id ==
235 SA2SIN6(caller_sa)->sin6_scope_id)) {
236 const int goodness = 3;
238 if (bestif_goodness < goodness) {
240 bestif_goodness = goodness;
248 if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) {
249 const int goodness = 2;
251 if (bestif_goodness < goodness) {
253 bestif_goodness = goodness;
256 if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
257 const int goodness = 1;
259 if (bestif_goodness < goodness) {
261 bestif_goodness = goodness;
272 * Construct the new address using the address from
273 * `bestif', and the port number from `serv_uaddr'.
275 serv_nbp = uaddr2taddr(nconf, serv_uaddr);
276 if (serv_nbp == NULL)
278 serv_sa = serv_nbp->buf;
280 memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
281 switch (ss.ss_family) {
283 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
287 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
291 tbuf.len = ss.ss_len;
292 tbuf.maxlen = sizeof(ss);
294 ret = taddr2uaddr(nconf, &tbuf);
298 if (hint_nbp != NULL) {
302 if (serv_nbp != NULL) {
311 fprintf(stderr, "addrmerge: returning %s\n", ret);
320 struct ifaddrs *ifap, *ifp;
321 struct ipv6_mreq mreq6;
322 unsigned int ifindex;
326 struct addrinfo hints, *res;
328 memset(&hints, 0, sizeof hints);
329 hints.ai_family = AF_INET;
330 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
332 fprintf(stderr, "can't get local ip4 address: %s\n",
333 gai_strerror(ecode));
335 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
336 if (local_in4 == NULL) {
338 fprintf(stderr, "can't alloc local ip4 addr\n");
341 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
346 hints.ai_family = AF_INET6;
347 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
349 fprintf(stderr, "can't get local ip6 address: %s\n",
350 gai_strerror(ecode));
352 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
353 if (local_in6 == NULL) {
355 fprintf(stderr, "can't alloc local ip6 addr\n");
358 memcpy(local_in6, res->ai_addr, sizeof *local_in6);
363 * Now join the RPC ipv6 multicast group on all interfaces.
365 if (getifaddrs(&ifp) < 0)
368 mreq6.ipv6mr_interface = 0;
369 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
371 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
374 fprintf(stderr, "couldn't create ip6 socket");
379 * Loop through all interfaces. For each IPv6 multicast-capable
380 * interface, join the RPC multicast group on that interface.
382 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
383 if (ifap->ifa_addr->sa_family != AF_INET6 ||
384 !(ifap->ifa_flags & IFF_MULTICAST))
386 ifindex = if_nametoindex(ifap->ifa_name);
387 if (ifindex == mreq6.ipv6mr_interface)
389 * Already did this one.
392 mreq6.ipv6mr_interface = ifindex;
393 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
396 perror("setsockopt v6 multicast");
410 return (struct sockaddr *)local_in4;
413 return (struct sockaddr *)local_in6;