1 /* $NetBSD: util.c,v 1.19 2015/05/09 18:32:04 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/socket.h> 34 #include <sys/queue.h> 35 #include <net/if.h> 36 #include <netinet/in.h> 37 #include <assert.h> 38 #include <ifaddrs.h> 39 #include <poll.h> 40 #include <rpc/rpc.h> 41 #include <errno.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <netdb.h> 46 #include <netconfig.h> 47 #include <stdio.h> 48 #include <arpa/inet.h> 49 #include <err.h> 50 51 #include "rpcbind.h" 52 53 static struct sockaddr_in *local_in4; 54 #ifdef INET6 55 static struct sockaddr_in6 *local_in6; 56 #endif 57 58 static int bitmaskcmp(void *, void *, void *, int); 59 60 /* 61 * For all bits set in "mask", compare the corresponding bits in 62 * "dst" and "src", and see if they match. 63 */ 64 static int 65 bitmaskcmp(void *dst, void *src, void *mask, int bytelen) 66 { 67 int i, j; 68 u_int8_t *p1 = dst, *p2 = src, *netmask = mask; 69 u_int8_t bitmask; 70 71 for (i = 0; i < bytelen; i++) { 72 for (j = 0; j < 8; j++) { 73 bitmask = 1 << j; 74 if (!(netmask[i] & bitmask)) 75 continue; 76 if ((p1[i] & bitmask) != (p2[i] & bitmask)) 77 return 1; 78 } 79 } 80 81 return 0; 82 } 83 84 char * 85 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr, 86 char *netid) 87 { 88 struct ifaddrs *ifap, *ifp, *bestif; 89 #ifdef INET6 90 struct sockaddr_in6 *servsin6, *sin6mask, *clntsin6, *ifsin6, *realsin6; 91 struct sockaddr_in6 *newsin6; 92 #endif 93 struct sockaddr_in *servsin, *sinmask, *clntsin, *newsin, *ifsin; 94 struct netbuf *serv_nbp, *clnt_nbp = NULL, tbuf; 95 struct sockaddr *serv_sa; 96 struct sockaddr *clnt_sa; 97 struct sockaddr_storage ss; 98 struct netconfig *nconf; 99 struct sockaddr *clnt = caller->buf; 100 char *ret = NULL; 101 102 #ifdef INET6 103 servsin6 = ifsin6 = newsin6 = NULL; /* XXXGCC -Wuninitialized */ 104 #endif 105 servsin = newsin = NULL; /* XXXGCC -Wuninitialized */ 106 107 #ifdef RPCBIND_DEBUG 108 if (debugging) 109 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr, 110 clnt_uaddr, netid); 111 #endif 112 nconf = getnetconfigent(netid); 113 if (nconf == NULL) 114 return NULL; 115 116 /* 117 * Local merge, just return a duplicate. 118 */ 119 if (clnt_uaddr != NULL && strncmp(clnt_uaddr, "0.0.0.0.", 8) == 0) 120 return strdup(clnt_uaddr); 121 122 serv_nbp = uaddr2taddr(nconf, serv_uaddr); 123 if (serv_nbp == NULL) 124 return NULL; 125 126 serv_sa = (struct sockaddr *)serv_nbp->buf; 127 if (clnt_uaddr != NULL) { 128 clnt_nbp = uaddr2taddr(nconf, clnt_uaddr); 129 if (clnt_nbp == NULL) { 130 free(serv_nbp); 131 return NULL; 132 } 133 clnt_sa = (struct sockaddr *)clnt_nbp->buf; 134 if (clnt_sa->sa_family == AF_LOCAL) { 135 free(serv_nbp); 136 free(clnt_nbp); 137 free(clnt_sa); 138 return strdup(serv_uaddr); 139 } 140 } else { 141 clnt_sa = (struct sockaddr *) 142 malloc(sizeof (struct sockaddr_storage)); 143 memcpy(clnt_sa, clnt, clnt->sa_len); 144 } 145 146 if (getifaddrs(&ifp) < 0) { 147 free(serv_nbp); 148 free(clnt_sa); 149 if (clnt_nbp != NULL) 150 free(clnt_nbp); 151 return 0; 152 } 153 154 /* 155 * Loop through all interfaces. For each interface, see if the 156 * network portion of its address is equal to that of the client. 157 * If so, we have found the interface that we want to use. 158 */ 159 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { 160 if (ifap->ifa_addr->sa_family != clnt->sa_family || 161 !(ifap->ifa_flags & IFF_UP)) 162 continue; 163 164 switch (clnt->sa_family) { 165 case AF_INET: 166 /* 167 * realsin: address that recvfrom gave us. 168 * ifsin: address of interface being examined. 169 * clntsin: address that client want us to contact 170 * it on 171 * servsin: local address of RPC service. 172 * sinmask: netmask of this interface 173 * newsin: initially a copy of clntsin, eventually 174 * the merged address 175 */ 176 servsin = (struct sockaddr_in *)serv_sa; 177 clntsin = (struct sockaddr_in *)clnt_sa; 178 sinmask = (struct sockaddr_in *)ifap->ifa_netmask; 179 newsin = (struct sockaddr_in *)&ss; 180 ifsin = (struct sockaddr_in *)ifap->ifa_addr; 181 if (!bitmaskcmp(&ifsin->sin_addr, &clntsin->sin_addr, 182 &sinmask->sin_addr, sizeof (struct in_addr))) { 183 goto found; 184 } 185 break; 186 #ifdef INET6 187 case AF_INET6: 188 /* 189 * realsin6: address that recvfrom gave us. 190 * ifsin6: address of interface being examined. 191 * clntsin6: address that client want us to contact 192 * it on 193 * servsin6: local address of RPC service. 194 * sin6mask: netmask of this interface 195 * newsin6: initially a copy of clntsin, eventually 196 * the merged address 197 * 198 * For v6 link local addresses, if the client contacted 199 * us via a link-local address, and wants us to reply 200 * to one, use the scope id to see which one. 201 */ 202 realsin6 = (struct sockaddr_in6 *)clnt; 203 ifsin6 = (struct sockaddr_in6 *)ifap->ifa_addr; 204 inet6_getscopeid(ifsin6, 1); 205 clntsin6 = (struct sockaddr_in6 *)clnt_sa; 206 servsin6 = (struct sockaddr_in6 *)serv_sa; 207 sin6mask = (struct sockaddr_in6 *)ifap->ifa_netmask; 208 newsin6 = (struct sockaddr_in6 *)&ss; 209 if (IN6_IS_ADDR_LINKLOCAL(&ifsin6->sin6_addr) && 210 IN6_IS_ADDR_LINKLOCAL(&realsin6->sin6_addr) && 211 IN6_IS_ADDR_LINKLOCAL(&clntsin6->sin6_addr)) { 212 if (ifsin6->sin6_scope_id != 213 realsin6->sin6_scope_id) 214 continue; 215 goto found; 216 } 217 if (!bitmaskcmp(&ifsin6->sin6_addr, 218 &clntsin6->sin6_addr, &sin6mask->sin6_addr, 219 sizeof (struct in6_addr))) 220 goto found; 221 break; 222 #endif 223 default: 224 goto freeit; 225 } 226 } 227 /* 228 * Didn't find anything. Get the first possibly useful interface, 229 * preferring "normal" interfaces to point-to-point and loopback 230 * ones. 231 */ 232 bestif = NULL; 233 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { 234 if (ifap->ifa_addr->sa_family != clnt->sa_family || 235 !(ifap->ifa_flags & IFF_UP)) 236 continue; 237 if (!(ifap->ifa_flags & IFF_LOOPBACK) && 238 !(ifap->ifa_flags & IFF_POINTOPOINT)) { 239 bestif = ifap; 240 break; 241 } 242 if (bestif == NULL) 243 bestif = ifap; 244 else if ((bestif->ifa_flags & IFF_LOOPBACK) && 245 !(ifap->ifa_flags & IFF_LOOPBACK)) 246 bestif = ifap; 247 } 248 ifap = bestif; 249 found: 250 switch (clnt->sa_family) { 251 case AF_INET: 252 memcpy(newsin, ifap->ifa_addr, clnt_sa->sa_len); 253 newsin->sin_port = servsin->sin_port; 254 tbuf.len = clnt_sa->sa_len; 255 tbuf.maxlen = sizeof (struct sockaddr_storage); 256 tbuf.buf = newsin; 257 break; 258 #ifdef INET6 259 case AF_INET6: 260 assert(newsin6); 261 memcpy(newsin6, ifsin6, clnt_sa->sa_len); 262 newsin6->sin6_port = servsin6->sin6_port; 263 tbuf.maxlen = sizeof (struct sockaddr_storage); 264 tbuf.len = clnt_sa->sa_len; 265 tbuf.buf = newsin6; 266 break; 267 #endif 268 default: 269 goto freeit; 270 } 271 if (ifap != NULL) 272 ret = taddr2uaddr(nconf, &tbuf); 273 freeit: 274 freenetconfigent(nconf); 275 free(serv_sa); 276 free(serv_nbp); 277 if (clnt_sa != NULL) 278 free(clnt_sa); 279 if (clnt_nbp != NULL) 280 free(clnt_nbp); 281 freeifaddrs(ifp); 282 283 #ifdef RPCBIND_DEBUG 284 if (debugging) 285 fprintf(stderr, "addrmerge: returning %s\n", ret); 286 #endif 287 return ret; 288 } 289 290 void 291 network_init() 292 { 293 #ifdef INET6 294 struct ifaddrs *ifap, *ifp; 295 struct ipv6_mreq mreq6; 296 unsigned int ifindex; 297 int s; 298 #endif 299 int ecode; 300 struct addrinfo hints, *res; 301 302 memset(&hints, 0, sizeof hints); 303 hints.ai_family = AF_INET; 304 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) { 305 if (debugging) 306 fprintf(stderr, "can't get local ip4 address: %s\n", 307 gai_strerror(ecode)); 308 } else { 309 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4); 310 if (local_in4 == NULL) { 311 if (debugging) 312 fprintf(stderr, "can't alloc local ip4 addr\n"); 313 } 314 memcpy(local_in4, res->ai_addr, sizeof *local_in4); 315 } 316 317 #ifdef INET6 318 hints.ai_family = AF_INET6; 319 if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) { 320 if (debugging) 321 fprintf(stderr, "can't get local ip6 address: %s\n", 322 gai_strerror(ecode)); 323 } else { 324 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6); 325 if (local_in6 == NULL) { 326 if (debugging) 327 fprintf(stderr, "can't alloc local ip6 addr\n"); 328 } 329 memcpy(local_in6, res->ai_addr, sizeof *local_in6); 330 } 331 332 /* 333 * Now join the RPC ipv6 multicast group on all interfaces. 334 */ 335 if (getifaddrs(&ifp) < 0) 336 return; 337 338 mreq6.ipv6mr_interface = 0; 339 inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr); 340 341 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 342 343 /* 344 * Loop through all interfaces. For each interface, see if the 345 * network portion of its address is equal to that of the client. 346 * If so, we have found the interface that we want to use. 347 */ 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) 354 /* 355 * Already did this one. 356 */ 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 warn("setsockopt v6 multicast"); 363 } 364 freeifaddrs(ifp); 365 #endif 366 367 /* close(s); */ 368 } 369 370 struct sockaddr * 371 local_sa(int af) 372 { 373 switch (af) { 374 case AF_INET: 375 return (struct sockaddr *)local_in4; 376 #ifdef INET6 377 case AF_INET6: 378 return (struct sockaddr *)local_in6; 379 #endif 380 default: 381 return NULL; 382 } 383 } 384