1/* 2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
| 1/* 2 * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
|
3 * $FreeBSD: head/usr.sbin/rpcbind/util.c 243187 2012-11-17 20:19:00Z hrs $
| 3 * $FreeBSD: head/usr.sbin/rpcbind/util.c 293229 2016-01-06 00:00:11Z asomers $
|
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 54static struct sockaddr_in *local_in4; 55#ifdef INET6 56static struct sockaddr_in6 *local_in6; 57#endif 58
| 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 54static struct sockaddr_in *local_in4; 55#ifdef INET6 56static struct sockaddr_in6 *local_in6; 57#endif 58
|
59static int bitmaskcmp(void *, void *, void *, int);
| 59static int bitmaskcmp(struct sockaddr *, struct sockaddr *, struct sockaddr *);
|
60 61/* 62 * For all bits set in "mask", compare the corresponding bits in 63 * "dst" and "src", and see if they match. Returns 0 if the addresses 64 * match. 65 */ 66static int
| 60 61/* 62 * For all bits set in "mask", compare the corresponding bits in 63 * "dst" and "src", and see if they match. Returns 0 if the addresses 64 * match. 65 */ 66static int
|
67bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
| 67bitmaskcmp(struct sockaddr *dst, struct sockaddr *src, struct sockaddr *mask)
|
68{ 69 int i;
| 68{ 69 int i;
|
70 u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
| 70 u_int8_t *p1, *p2, *netmask; 71 int bytelen;
|
71
| 72
|
| 73 if (dst->sa_family != src->sa_family || 74 dst->sa_family != mask->sa_family) 75 return (1); 76 77 switch (dst->sa_family) { 78 case AF_INET: 79 p1 = (uint8_t*) &SA2SINADDR(dst); 80 p2 = (uint8_t*) &SA2SINADDR(src); 81 netmask = (uint8_t*) &SA2SINADDR(mask); 82 bytelen = sizeof(struct in_addr); 83 break; 84#ifdef INET6 85 case AF_INET6: 86 p1 = (uint8_t*) &SA2SIN6ADDR(dst); 87 p2 = (uint8_t*) &SA2SIN6ADDR(src); 88 netmask = (uint8_t*) &SA2SIN6ADDR(mask); 89 bytelen = sizeof(struct in6_addr); 90 break; 91#endif 92 default: 93 return (1); 94 } 95
|
72 for (i = 0; i < bytelen; i++) 73 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i])) 74 return (1); 75 return (0); 76} 77 78/* 79 * Find a server address that can be used by `caller' to contact 80 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is 81 * non-NULL, it is used instead of `caller' as a hint suggesting 82 * the best address (e.g. the `r_addr' field of an rpc, which 83 * contains the rpcbind server address that the caller used). 84 * 85 * Returns the best server address as a malloc'd "universal address" 86 * string which should be freed by the caller. On error, returns NULL. 87 */ 88char *
| 96 for (i = 0; i < bytelen; i++) 97 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i])) 98 return (1); 99 return (0); 100} 101 102/* 103 * Find a server address that can be used by `caller' to contact 104 * the local service specified by `serv_uaddr'. If `clnt_uaddr' is 105 * non-NULL, it is used instead of `caller' as a hint suggesting 106 * the best address (e.g. the `r_addr' field of an rpc, which 107 * contains the rpcbind server address that the caller used). 108 * 109 * Returns the best server address as a malloc'd "universal address" 110 * string which should be freed by the caller. On error, returns NULL. 111 */ 112char *
|
89addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr, 90 char *netid)
| 113addrmerge(struct netbuf *caller, const char *serv_uaddr, const char *clnt_uaddr, 114 const char *netid)
|
91{ 92 struct ifaddrs *ifap, *ifp = NULL, *bestif; 93 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf; 94 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa; 95 struct sockaddr_storage ss; 96 struct netconfig *nconf;
| 115{ 116 struct ifaddrs *ifap, *ifp = NULL, *bestif; 117 struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf; 118 struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa; 119 struct sockaddr_storage ss; 120 struct netconfig *nconf;
|
97 char *caller_uaddr = NULL, *hint_uaddr = NULL;
| 121 char *caller_uaddr = NULL; 122 const char *hint_uaddr = NULL;
|
98 char *ret = NULL;
| 123 char *ret = NULL;
|
| 124 int bestif_goodness;
|
99 100#ifdef ND_DEBUG 101 if (debugging) 102 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr, 103 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid); 104#endif 105 caller_sa = caller->buf; 106 if ((nconf = rpcbind_get_conf(netid)) == NULL) 107 goto freeit; 108 if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL) 109 goto freeit; 110 111 /* 112 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its 113 * address family is different from that of the caller. 114 */ 115 hint_sa = NULL; 116 if (clnt_uaddr != NULL) { 117 hint_uaddr = clnt_uaddr; 118 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL) 119 goto freeit; 120 hint_sa = hint_nbp->buf; 121 } 122 if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) { 123 hint_uaddr = caller_uaddr; 124 hint_sa = caller->buf; 125 } 126 127#ifdef ND_DEBUG 128 if (debugging) 129 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr); 130#endif 131 /* Local caller, just return the server address. */ 132 if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 || 133 strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') { 134 ret = strdup(serv_uaddr); 135 goto freeit; 136 } 137 138 if (getifaddrs(&ifp) < 0) 139 goto freeit; 140 141 /*
| 125 126#ifdef ND_DEBUG 127 if (debugging) 128 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr, 129 clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid); 130#endif 131 caller_sa = caller->buf; 132 if ((nconf = rpcbind_get_conf(netid)) == NULL) 133 goto freeit; 134 if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL) 135 goto freeit; 136 137 /* 138 * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its 139 * address family is different from that of the caller. 140 */ 141 hint_sa = NULL; 142 if (clnt_uaddr != NULL) { 143 hint_uaddr = clnt_uaddr; 144 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL) 145 goto freeit; 146 hint_sa = hint_nbp->buf; 147 } 148 if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) { 149 hint_uaddr = caller_uaddr; 150 hint_sa = caller->buf; 151 } 152 153#ifdef ND_DEBUG 154 if (debugging) 155 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr); 156#endif 157 /* Local caller, just return the server address. */ 158 if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 || 159 strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') { 160 ret = strdup(serv_uaddr); 161 goto freeit; 162 } 163 164 if (getifaddrs(&ifp) < 0) 165 goto freeit; 166 167 /*
|
142 * Loop through all interfaces. For each interface, see if it 143 * is either the loopback interface (which we always listen 144 * on) or is one of the addresses the program bound to (the 145 * wildcard by default, or a subset if -h is specified) and 146 * the network portion of its address is equal to that of the 147 * client. If so, we have found the interface that we want to 148 * use.
| 168 * Loop through all interface addresses. We are listening to an address 169 * if any of the following are true: 170 * a) It's a loopback address 171 * b) It was specified with the -h command line option 172 * c) There were no -h command line options. 173 * 174 * Among addresses on which we are listening, choose in order of 175 * preference an address that is: 176 * 177 * a) Equal to the hint 178 * b) A link local address with the same scope ID as the client's 179 * address, if the client's address is also link local 180 * c) An address on the same subnet as the client's address 181 * d) A non-localhost, non-p2p address 182 * e) Any usable address
|
149 */ 150 bestif = NULL;
| 183 */ 184 bestif = NULL;
|
| 185 bestif_goodness = 0;
|
151 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { 152 ifsa = ifap->ifa_addr; 153 ifmasksa = ifap->ifa_netmask; 154
| 186 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { 187 ifsa = ifap->ifa_addr; 188 ifmasksa = ifap->ifa_netmask; 189
|
| 190 /* Skip addresses where we don't listen */
|
155 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family || 156 !(ifap->ifa_flags & IFF_UP)) 157 continue; 158 159 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa)) 160 continue; 161
| 191 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family || 192 !(ifap->ifa_flags & IFF_UP)) 193 continue; 194 195 if (!(ifap->ifa_flags & IFF_LOOPBACK) && !listen_addr(ifsa)) 196 continue; 197
|
162 switch (hint_sa->sa_family) { 163 case AF_INET: 164 /* 165 * If the hint address matches this interface 166 * address/netmask, then we're done. 167 */ 168 if (!bitmaskcmp(&SA2SINADDR(ifsa), 169 &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa), 170 sizeof(struct in_addr))) { 171 bestif = ifap; 172 goto found; 173 } 174 break;
| 198 if ((hint_sa->sa_family == AF_INET) && 199 ((((struct sockaddr_in*)hint_sa)->sin_addr.s_addr == 200 ((struct sockaddr_in*)ifsa)->sin_addr.s_addr))) { 201 const int goodness = 4; 202 203 bestif_goodness = goodness; 204 bestif = ifap; 205 goto found; 206 }
|
175#ifdef INET6
| 207#ifdef INET6
|
176 case AF_INET6:
| 208 if ((hint_sa->sa_family == AF_INET6) && 209 (0 == memcmp(&((struct sockaddr_in6*)hint_sa)->sin6_addr, 210 &((struct sockaddr_in6*)ifsa)->sin6_addr, 211 sizeof(struct in6_addr))) && 212 (((struct sockaddr_in6*)hint_sa)->sin6_scope_id == 213 (((struct sockaddr_in6*)ifsa)->sin6_scope_id))) { 214 const int goodness = 4; 215 216 bestif_goodness = goodness; 217 bestif = ifap; 218 goto found; 219 } 220 if (hint_sa->sa_family == AF_INET6) {
|
177 /* 178 * For v6 link local addresses, if the caller is on 179 * a link-local address then use the scope id to see 180 * which one. 181 */ 182 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) && 183 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) && 184 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) { 185 if (SA2SIN6(ifsa)->sin6_scope_id == 186 SA2SIN6(caller_sa)->sin6_scope_id) {
| 221 /* 222 * For v6 link local addresses, if the caller is on 223 * a link-local address then use the scope id to see 224 * which one. 225 */ 226 if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) && 227 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) && 228 IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) { 229 if (SA2SIN6(ifsa)->sin6_scope_id == 230 SA2SIN6(caller_sa)->sin6_scope_id) {
|
187 bestif = ifap; 188 goto found;
| 231 const int goodness = 3; 232 233 if (bestif_goodness < goodness) { 234 bestif = ifap; 235 bestif_goodness = goodness; 236 }
|
189 }
| 237 }
|
190 } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa), 191 &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa), 192 sizeof(struct in6_addr))) {
| 238 } 239 } 240#endif /* INET6 */ 241 if (0 == bitmaskcmp(hint_sa, ifsa, ifmasksa)) { 242 const int goodness = 2; 243 244 if (bestif_goodness < goodness) {
|
193 bestif = ifap;
| 245 bestif = ifap;
|
194 goto found;
| 246 bestif_goodness = goodness;
|
195 }
| 247 }
|
196 break; 197#endif 198 default: 199 continue;
| |
200 }
| 248 }
|
| 249 if (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 250 const int goodness = 1;
|
201
| 251
|
202 /* 203 * Remember the first possibly useful interface, preferring 204 * "normal" to point-to-point and loopback ones. 205 */ 206 if (bestif == NULL || 207 (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) && 208 (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
| 252 if (bestif_goodness < goodness) { 253 bestif = ifap; 254 bestif_goodness = goodness; 255 } 256 } 257 if (bestif == NULL)
|
209 bestif = ifap; 210 } 211 if (bestif == NULL) 212 goto freeit; 213 214found: 215 /* 216 * Construct the new address using the address from 217 * `bestif', and the port number from `serv_uaddr'. 218 */ 219 serv_nbp = uaddr2taddr(nconf, serv_uaddr); 220 if (serv_nbp == NULL) 221 goto freeit; 222 serv_sa = serv_nbp->buf; 223 224 memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len); 225 switch (ss.ss_family) { 226 case AF_INET: 227 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port; 228 break; 229#ifdef INET6 230 case AF_INET6: 231 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port; 232 break; 233#endif 234 } 235 tbuf.len = ss.ss_len; 236 tbuf.maxlen = sizeof(ss); 237 tbuf.buf = &ss; 238 ret = taddr2uaddr(nconf, &tbuf); 239 240freeit: 241 if (caller_uaddr != NULL) 242 free(caller_uaddr); 243 if (hint_nbp != NULL) { 244 free(hint_nbp->buf); 245 free(hint_nbp); 246 } 247 if (serv_nbp != NULL) { 248 free(serv_nbp->buf); 249 free(serv_nbp); 250 } 251 if (ifp != NULL) 252 freeifaddrs(ifp); 253 254#ifdef ND_DEBUG 255 if (debugging) 256 fprintf(stderr, "addrmerge: returning %s\n", ret); 257#endif 258 return ret; 259} 260 261void 262network_init(void) 263{ 264#ifdef INET6 265 struct ifaddrs *ifap, *ifp; 266 struct ipv6_mreq mreq6; 267 unsigned int ifindex; 268 int s; 269#endif 270 int ecode; 271 struct addrinfo hints, *res; 272 273 memset(&hints, 0, sizeof hints); 274 hints.ai_family = AF_INET; 275 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) { 276 if (debugging) 277 fprintf(stderr, "can't get local ip4 address: %s\n", 278 gai_strerror(ecode)); 279 } else { 280 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4); 281 if (local_in4 == NULL) { 282 if (debugging) 283 fprintf(stderr, "can't alloc local ip4 addr\n"); 284 } 285 memcpy(local_in4, res->ai_addr, sizeof *local_in4); 286 } 287 288#ifdef INET6 289 hints.ai_family = AF_INET6; 290 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) { 291 if (debugging) 292 fprintf(stderr, "can't get local ip6 address: %s\n", 293 gai_strerror(ecode)); 294 } else { 295 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6); 296 if (local_in6 == NULL) { 297 if (debugging) 298 fprintf(stderr, "can't alloc local ip6 addr\n"); 299 } 300 memcpy(local_in6, res->ai_addr, sizeof *local_in6); 301 } 302 303 /* 304 * Now join the RPC ipv6 multicast group on all interfaces. 305 */ 306 if (getifaddrs(&ifp) < 0) 307 return; 308 309 mreq6.ipv6mr_interface = 0; 310 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr); 311 312 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 313 314 /* 315 * Loop through all interfaces. For each IPv6 multicast-capable 316 * interface, join the RPC multicast group on that interface. 317 */ 318 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { 319 if (ifap->ifa_addr->sa_family != AF_INET6 || 320 !(ifap->ifa_flags & IFF_MULTICAST)) 321 continue; 322 ifindex = if_nametoindex(ifap->ifa_name); 323 if (ifindex == mreq6.ipv6mr_interface) 324 /* 325 * Already did this one. 326 */ 327 continue; 328 mreq6.ipv6mr_interface = ifindex; 329 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6, 330 sizeof mreq6) < 0) 331 if (debugging) 332 perror("setsockopt v6 multicast"); 333 } 334#endif 335 336 /* close(s); */ 337} 338 339struct sockaddr * 340local_sa(int af) 341{ 342 switch (af) { 343 case AF_INET: 344 return (struct sockaddr *)local_in4; 345#ifdef INET6 346 case AF_INET6: 347 return (struct sockaddr *)local_in6; 348#endif 349 default: 350 return NULL; 351 } 352}
| 258 bestif = ifap; 259 } 260 if (bestif == NULL) 261 goto freeit; 262 263found: 264 /* 265 * Construct the new address using the address from 266 * `bestif', and the port number from `serv_uaddr'. 267 */ 268 serv_nbp = uaddr2taddr(nconf, serv_uaddr); 269 if (serv_nbp == NULL) 270 goto freeit; 271 serv_sa = serv_nbp->buf; 272 273 memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len); 274 switch (ss.ss_family) { 275 case AF_INET: 276 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port; 277 break; 278#ifdef INET6 279 case AF_INET6: 280 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port; 281 break; 282#endif 283 } 284 tbuf.len = ss.ss_len; 285 tbuf.maxlen = sizeof(ss); 286 tbuf.buf = &ss; 287 ret = taddr2uaddr(nconf, &tbuf); 288 289freeit: 290 if (caller_uaddr != NULL) 291 free(caller_uaddr); 292 if (hint_nbp != NULL) { 293 free(hint_nbp->buf); 294 free(hint_nbp); 295 } 296 if (serv_nbp != NULL) { 297 free(serv_nbp->buf); 298 free(serv_nbp); 299 } 300 if (ifp != NULL) 301 freeifaddrs(ifp); 302 303#ifdef ND_DEBUG 304 if (debugging) 305 fprintf(stderr, "addrmerge: returning %s\n", ret); 306#endif 307 return ret; 308} 309 310void 311network_init(void) 312{ 313#ifdef INET6 314 struct ifaddrs *ifap, *ifp; 315 struct ipv6_mreq mreq6; 316 unsigned int ifindex; 317 int s; 318#endif 319 int ecode; 320 struct addrinfo hints, *res; 321 322 memset(&hints, 0, sizeof hints); 323 hints.ai_family = AF_INET; 324 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) { 325 if (debugging) 326 fprintf(stderr, "can't get local ip4 address: %s\n", 327 gai_strerror(ecode)); 328 } else { 329 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4); 330 if (local_in4 == NULL) { 331 if (debugging) 332 fprintf(stderr, "can't alloc local ip4 addr\n"); 333 } 334 memcpy(local_in4, res->ai_addr, sizeof *local_in4); 335 } 336 337#ifdef INET6 338 hints.ai_family = AF_INET6; 339 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) { 340 if (debugging) 341 fprintf(stderr, "can't get local ip6 address: %s\n", 342 gai_strerror(ecode)); 343 } else { 344 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6); 345 if (local_in6 == NULL) { 346 if (debugging) 347 fprintf(stderr, "can't alloc local ip6 addr\n"); 348 } 349 memcpy(local_in6, res->ai_addr, sizeof *local_in6); 350 } 351 352 /* 353 * Now join the RPC ipv6 multicast group on all interfaces. 354 */ 355 if (getifaddrs(&ifp) < 0) 356 return; 357 358 mreq6.ipv6mr_interface = 0; 359 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr); 360 361 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 362 363 /* 364 * Loop through all interfaces. For each IPv6 multicast-capable 365 * interface, join the RPC multicast group on that interface. 366 */ 367 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { 368 if (ifap->ifa_addr->sa_family != AF_INET6 || 369 !(ifap->ifa_flags & IFF_MULTICAST)) 370 continue; 371 ifindex = if_nametoindex(ifap->ifa_name); 372 if (ifindex == mreq6.ipv6mr_interface) 373 /* 374 * Already did this one. 375 */ 376 continue; 377 mreq6.ipv6mr_interface = ifindex; 378 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6, 379 sizeof mreq6) < 0) 380 if (debugging) 381 perror("setsockopt v6 multicast"); 382 } 383#endif 384 385 /* close(s); */ 386} 387 388struct sockaddr * 389local_sa(int af) 390{ 391 switch (af) { 392 case AF_INET: 393 return (struct sockaddr *)local_in4; 394#ifdef INET6 395 case AF_INET6: 396 return (struct sockaddr *)local_in6; 397#endif 398 default: 399 return NULL; 400 } 401}
|