2 * Copyright (c) 2000 The NetBSD Foundation, Inc.
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
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 $
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
45 #include <netinet/in.h>
54 #include <netconfig.h>
56 #include <arpa/inet.h>
60 #define SA2SIN(sa) ((struct sockaddr_in *)(sa))
61 #define SA2SINADDR(sa) (SA2SIN(sa)->sin_addr)
63 #define SA2SIN6(sa) ((struct sockaddr_in6 *)(sa))
64 #define SA2SIN6ADDR(sa) (SA2SIN6(sa)->sin6_addr)
67 static struct sockaddr_in *local_in4;
69 static struct sockaddr_in6 *local_in6;
72 static int bitmaskcmp(void *, void *, void *, int);
74 static void in6_fillscopeid(struct sockaddr_in6 *);
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
83 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
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]))
95 * Similar to code in ifconfig.c. Fill in the scope ID for link-local
96 * addresses returned by getifaddrs().
100 in6_fillscopeid(struct sockaddr_in6 *sin6)
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;
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.
125 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
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;
138 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
139 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
141 caller_sa = caller->buf;
142 if ((nconf = rpcbind_get_conf(netid)) == NULL)
144 if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
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.
152 if (clnt_uaddr != NULL) {
153 hint_uaddr = clnt_uaddr;
154 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
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;
165 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
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);
174 if (getifaddrs(&ifp) < 0)
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.
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))
191 switch (hint_sa->sa_family) {
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))) {
207 * For v6 link local addresses, if the caller is on
208 * a link-local address then use the scope id to see
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) {
220 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
221 &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
222 sizeof(struct in6_addr))) {
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))))
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)
252 serv_sa = serv_nbp->buf;
254 memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
255 switch (ss.ss_family) {
257 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
261 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
265 tbuf.len = ss.ss_len;
266 tbuf.maxlen = sizeof(ss);
268 ret = taddr2uaddr(nconf, &tbuf);
271 if (caller_uaddr != NULL)
273 if (hint_nbp != NULL) {
277 if (serv_nbp != NULL) {
286 fprintf(stderr, "addrmerge: returning %s\n", ret);
295 struct ifaddrs *ifap, *ifp;
296 struct ipv6_mreq mreq6;
297 unsigned int ifindex;
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))) {
307 fprintf(stderr, "can't get local ip4 address: %s\n",
308 gai_strerror(ecode));
310 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
311 if (local_in4 == NULL) {
313 fprintf(stderr, "can't alloc local ip4 addr\n");
315 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
319 hints.ai_family = AF_INET6;
320 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
322 fprintf(stderr, "can't get local ip6 address: %s\n",
323 gai_strerror(ecode));
325 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
326 if (local_in6 == NULL) {
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)
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))
352 ifindex = if_nametoindex(ifap->ifa_name);
353 if (ifindex == mreq6.ipv6mr_interface)
355 * Already did this one.
358 mreq6.ipv6mr_interface = ifindex;
359 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
362 perror("setsockopt v6 multicast");
374 return (struct sockaddr *)local_in4;
377 return (struct sockaddr *)local_in6;