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 $
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
44 #include <netinet/in.h>
53 #include <netconfig.h>
55 #include <arpa/inet.h>
59 #define SA2SIN(sa) ((struct sockaddr_in *)(sa))
60 #define SA2SINADDR(sa) (SA2SIN(sa)->sin_addr)
62 #define SA2SIN6(sa) ((struct sockaddr_in6 *)(sa))
63 #define SA2SIN6ADDR(sa) (SA2SIN6(sa)->sin6_addr)
66 static struct sockaddr_in
*local_in4
;
68 static struct sockaddr_in6
*local_in6
;
71 static int bitmaskcmp(void *, void *, void *, int);
73 static void in6_fillscopeid(struct sockaddr_in6
*);
77 * For all bits set in "mask", compare the corresponding bits in
78 * "dst" and "src", and see if they match. Returns 0 if the addresses
82 bitmaskcmp(void *dst
, void *src
, void *mask
, int bytelen
)
85 u_int8_t
*p1
= dst
, *p2
= src
, *netmask
= mask
;
87 for (i
= 0; i
< bytelen
; i
++)
88 if ((p1
[i
] & netmask
[i
]) != (p2
[i
] & netmask
[i
]))
94 * Similar to code in ifconfig.c. Fill in the scope ID for link-local
95 * addresses returned by getifaddrs().
99 in6_fillscopeid(struct sockaddr_in6
*sin6
)
103 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
104 ifindex
= ntohs(*(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2]);
105 if (sin6
->sin6_scope_id
== 0 && ifindex
!= 0) {
106 sin6
->sin6_scope_id
= ifindex
;
107 *(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2] = 0;
114 * Find a server address that can be used by `caller' to contact
115 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
116 * non-NULL, it is used instead of `caller' as a hint suggesting
117 * the best address (e.g. the `r_addr' field of an rpc, which
118 * contains the rpcbind server address that the caller used).
120 * Returns the best server address as a malloc'd "universal address"
121 * string which should be freed by the caller. On error, returns NULL.
124 addrmerge(struct netbuf
*caller
, char *serv_uaddr
, char *clnt_uaddr
,
127 struct ifaddrs
*ifap
, *ifp
= NULL
, *bestif
;
128 struct netbuf
*serv_nbp
= NULL
, *hint_nbp
= NULL
, tbuf
;
129 struct sockaddr
*caller_sa
, *hint_sa
, *ifsa
, *ifmasksa
, *serv_sa
;
130 struct sockaddr_storage ss
;
131 struct netconfig
*nconf
;
132 char *caller_uaddr
= NULL
, *hint_uaddr
= NULL
;
137 fprintf(stderr
, "addrmerge(caller, %s, %s, %s\n", serv_uaddr
,
138 clnt_uaddr
== NULL
? "NULL" : clnt_uaddr
, netid
);
140 caller_sa
= caller
->buf
;
141 if ((nconf
= rpcbind_get_conf(netid
)) == NULL
)
143 if ((caller_uaddr
= taddr2uaddr(nconf
, caller
)) == NULL
)
147 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
148 * address family is different from that of the caller.
151 if (clnt_uaddr
!= NULL
) {
152 hint_uaddr
= clnt_uaddr
;
153 if ((hint_nbp
= uaddr2taddr(nconf
, clnt_uaddr
)) == NULL
)
155 hint_sa
= hint_nbp
->buf
;
157 if (hint_sa
== NULL
|| hint_sa
->sa_family
!= caller_sa
->sa_family
) {
158 hint_uaddr
= caller_uaddr
;
159 hint_sa
= caller
->buf
;
164 fprintf(stderr
, "addrmerge: hint %s\n", hint_uaddr
);
166 /* Local caller, just return the server address. */
167 if (strncmp(caller_uaddr
, "0.0.0.0.", 8) == 0 ||
168 strncmp(caller_uaddr
, "::.", 3) == 0 || caller_uaddr
[0] == '/') {
169 ret
= strdup(serv_uaddr
);
173 if (getifaddrs(&ifp
) < 0)
177 * Loop through all interfaces. For each interface, see if the
178 * network portion of its address is equal to that of the client.
179 * If so, we have found the interface that we want to use.
182 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
183 ifsa
= ifap
->ifa_addr
;
184 ifmasksa
= ifap
->ifa_netmask
;
186 if (ifsa
== NULL
|| ifsa
->sa_family
!= hint_sa
->sa_family
||
187 !(ifap
->ifa_flags
& IFF_UP
))
190 switch (hint_sa
->sa_family
) {
193 * If the hint address matches this interface
194 * address/netmask, then we're done.
196 if (!bitmaskcmp(&SA2SINADDR(ifsa
),
197 &SA2SINADDR(hint_sa
), &SA2SINADDR(ifmasksa
),
198 sizeof(struct in_addr
))) {
206 * For v6 link local addresses, if the caller is on
207 * a link-local address then use the scope id to see
210 in6_fillscopeid(SA2SIN6(ifsa
));
211 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa
)) &&
212 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa
)) &&
213 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa
))) {
214 if (SA2SIN6(ifsa
)->sin6_scope_id
==
215 SA2SIN6(caller_sa
)->sin6_scope_id
) {
219 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa
),
220 &SA2SIN6ADDR(hint_sa
), &SA2SIN6ADDR(ifmasksa
),
221 sizeof(struct in6_addr
))) {
232 * Remember the first possibly useful interface, preferring
233 * "normal" to point-to-point and loopback ones.
235 if (bestif
== NULL
||
236 (!(ifap
->ifa_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) &&
237 (bestif
->ifa_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
))))
245 * Construct the new address using the the address from
246 * `bestif', and the port number from `serv_uaddr'.
248 serv_nbp
= uaddr2taddr(nconf
, serv_uaddr
);
249 if (serv_nbp
== NULL
)
251 serv_sa
= serv_nbp
->buf
;
253 memcpy(&ss
, bestif
->ifa_addr
, bestif
->ifa_addr
->sa_len
);
254 switch (ss
.ss_family
) {
256 SA2SIN(&ss
)->sin_port
= SA2SIN(serv_sa
)->sin_port
;
260 SA2SIN6(&ss
)->sin6_port
= SA2SIN6(serv_sa
)->sin6_port
;
264 tbuf
.len
= ss
.ss_len
;
265 tbuf
.maxlen
= sizeof(ss
);
267 ret
= taddr2uaddr(nconf
, &tbuf
);
270 if (caller_uaddr
!= NULL
)
272 if (hint_nbp
!= NULL
) {
276 if (serv_nbp
!= NULL
) {
285 fprintf(stderr
, "addrmerge: returning %s\n", ret
);
294 struct ifaddrs
*ifap
, *ifp
;
295 struct ipv6_mreq mreq6
;
296 unsigned int ifindex
;
300 struct addrinfo hints
, *res
;
302 memset(&hints
, 0, sizeof hints
);
303 hints
.ai_family
= AF_INET
;
304 if ((ecode
= getaddrinfo(NULL
, "sunrpc", &hints
, &res
))) {
306 fprintf(stderr
, "can't get local ip4 address: %s\n",
307 gai_strerror(ecode
));
309 local_in4
= (struct sockaddr_in
*)malloc(sizeof *local_in4
);
310 if (local_in4
== NULL
) {
312 fprintf(stderr
, "can't alloc local ip4 addr\n");
314 memcpy(local_in4
, res
->ai_addr
, sizeof *local_in4
);
318 hints
.ai_family
= AF_INET6
;
319 if ((ecode
= getaddrinfo(NULL
, "sunrpc", &hints
, &res
))) {
321 fprintf(stderr
, "can't get local ip6 address: %s\n",
322 gai_strerror(ecode
));
324 local_in6
= (struct sockaddr_in6
*)malloc(sizeof *local_in6
);
325 if (local_in6
== NULL
) {
327 fprintf(stderr
, "can't alloc local ip6 addr\n");
329 memcpy(local_in6
, res
->ai_addr
, sizeof *local_in6
);
333 * Now join the RPC ipv6 multicast group on all interfaces.
335 if (getifaddrs(&ifp
) < 0)
338 mreq6
.ipv6mr_interface
= 0;
339 inet_pton(AF_INET6
, RPCB_MULTICAST_ADDR
, &mreq6
.ipv6mr_multiaddr
);
341 s
= socket(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
344 * Loop through all interfaces. For each IPv6 multicast-capable
345 * interface, join the RPC multicast group on that interface.
347 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
348 if (ifap
->ifa_addr
->sa_family
!= AF_INET6
||
349 !(ifap
->ifa_flags
& IFF_MULTICAST
))
351 ifindex
= if_nametoindex(ifap
->ifa_name
);
352 if (ifindex
== mreq6
.ipv6mr_interface
)
354 * Already did this one.
357 mreq6
.ipv6mr_interface
= ifindex
;
358 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq6
,
361 perror("setsockopt v6 multicast");
373 return (struct sockaddr
*)local_in4
;
376 return (struct sockaddr
*)local_in6
;