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