1 /* $NetBSD: in.c,v 1.148 2014/09/09 20:16:12 rmind Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * Copyright (c) 1982, 1986, 1991, 1993 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)in.c 8.4 (Berkeley) 1/9/95 91 */ 92 93 #include <sys/cdefs.h> 94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.148 2014/09/09 20:16:12 rmind Exp $"); 95 96 #include "opt_inet.h" 97 #include "opt_inet_conf.h" 98 #include "opt_mrouting.h" 99 100 #include <sys/param.h> 101 #include <sys/ioctl.h> 102 #include <sys/errno.h> 103 #include <sys/malloc.h> 104 #include <sys/socket.h> 105 #include <sys/socketvar.h> 106 #include <sys/sysctl.h> 107 #include <sys/systm.h> 108 #include <sys/proc.h> 109 #include <sys/syslog.h> 110 #include <sys/kauth.h> 111 112 #include <sys/cprng.h> 113 114 #include <net/if.h> 115 #include <net/route.h> 116 #include <net/pfil.h> 117 118 #include <net/if_ether.h> 119 120 #include <netinet/in_systm.h> 121 #include <netinet/in.h> 122 #include <netinet/in_var.h> 123 #include <netinet/ip.h> 124 #include <netinet/ip_var.h> 125 #include <netinet/in_ifattach.h> 126 #include <netinet/in_pcb.h> 127 #include <netinet/if_inarp.h> 128 #include <netinet/ip_mroute.h> 129 #include <netinet/igmp_var.h> 130 131 #ifdef IPSELSRC 132 #include <netinet/in_selsrc.h> 133 #endif 134 135 static u_int in_mask2len(struct in_addr *); 136 static void in_len2mask(struct in_addr *, u_int); 137 static int in_lifaddr_ioctl(struct socket *, u_long, void *, 138 struct ifnet *); 139 140 static int in_addprefix(struct in_ifaddr *, int); 141 static int in_scrubprefix(struct in_ifaddr *); 142 static void in_sysctl_init(struct sysctllog **); 143 144 #ifndef SUBNETSARELOCAL 145 #define SUBNETSARELOCAL 1 146 #endif 147 148 #ifndef HOSTZEROBROADCAST 149 #define HOSTZEROBROADCAST 1 150 #endif 151 152 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */ 153 #ifndef IN_MULTI_HASH_SIZE 154 #define IN_MULTI_HASH_SIZE 509 155 #endif 156 157 static int subnetsarelocal = SUBNETSARELOCAL; 158 static int hostzeroisbroadcast = HOSTZEROBROADCAST; 159 160 /* 161 * This list is used to keep track of in_multi chains which belong to 162 * deleted interface addresses. We use in_ifaddr so that a chain head 163 * won't be deallocated until all multicast address record are deleted. 164 */ 165 166 LIST_HEAD(in_multihashhead, in_multi); /* Type of the hash head */ 167 168 static struct pool inmulti_pool; 169 static u_int in_multientries; 170 static struct in_multihashhead *in_multihashtbl; 171 static u_long in_multihash; 172 static krwlock_t in_multilock; 173 174 #define IN_MULTI_HASH(x, ifp) \ 175 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE]) 176 177 struct in_ifaddrhashhead * in_ifaddrhashtbl; 178 u_long in_ifaddrhash; 179 struct in_ifaddrhead in_ifaddrhead; 180 181 void 182 in_init(void) 183 { 184 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", 185 NULL, IPL_SOFTNET); 186 TAILQ_INIT(&in_ifaddrhead); 187 188 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true, 189 &in_ifaddrhash); 190 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true, 191 &in_multihash); 192 rw_init(&in_multilock); 193 194 in_sysctl_init(NULL); 195 } 196 197 /* 198 * Return 1 if an internet address is for a ``local'' host 199 * (one to which we have a connection). If subnetsarelocal 200 * is true, this includes other subnets of the local net. 201 * Otherwise, it includes only the directly-connected (sub)nets. 202 */ 203 int 204 in_localaddr(struct in_addr in) 205 { 206 struct in_ifaddr *ia; 207 208 if (subnetsarelocal) { 209 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 210 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) 211 return (1); 212 } else { 213 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 214 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) 215 return (1); 216 } 217 return (0); 218 } 219 220 /* 221 * Determine whether an IP address is in a reserved set of addresses 222 * that may not be forwarded, or whether datagrams to that destination 223 * may be forwarded. 224 */ 225 int 226 in_canforward(struct in_addr in) 227 { 228 u_int32_t net; 229 230 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 231 return (0); 232 if (IN_CLASSA(in.s_addr)) { 233 net = in.s_addr & IN_CLASSA_NET; 234 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 235 return (0); 236 } 237 return (1); 238 } 239 240 /* 241 * Trim a mask in a sockaddr 242 */ 243 void 244 in_socktrim(struct sockaddr_in *ap) 245 { 246 char *cplim = (char *) &ap->sin_addr; 247 char *cp = (char *) (&ap->sin_addr + 1); 248 249 ap->sin_len = 0; 250 while (--cp >= cplim) 251 if (*cp) { 252 (ap)->sin_len = cp - (char *) (ap) + 1; 253 break; 254 } 255 } 256 257 /* 258 * Routine to take an Internet address and convert into a 259 * "dotted quad" representation for printing. 260 */ 261 const char * 262 in_fmtaddr(struct in_addr addr) 263 { 264 static char buf[sizeof("123.456.789.123")]; 265 266 addr.s_addr = ntohl(addr.s_addr); 267 268 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", 269 (addr.s_addr >> 24) & 0xFF, 270 (addr.s_addr >> 16) & 0xFF, 271 (addr.s_addr >> 8) & 0xFF, 272 (addr.s_addr >> 0) & 0xFF); 273 return buf; 274 } 275 276 /* 277 * Maintain the "in_maxmtu" variable, which is the largest 278 * mtu for non-local interfaces with AF_INET addresses assigned 279 * to them that are up. 280 */ 281 unsigned long in_maxmtu; 282 283 void 284 in_setmaxmtu(void) 285 { 286 struct in_ifaddr *ia; 287 struct ifnet *ifp; 288 unsigned long maxmtu = 0; 289 290 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 291 if ((ifp = ia->ia_ifp) == 0) 292 continue; 293 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP) 294 continue; 295 if (ifp->if_mtu > maxmtu) 296 maxmtu = ifp->if_mtu; 297 } 298 if (maxmtu) 299 in_maxmtu = maxmtu; 300 } 301 302 static u_int 303 in_mask2len(struct in_addr *mask) 304 { 305 u_int x, y; 306 u_char *p; 307 308 p = (u_char *)mask; 309 for (x = 0; x < sizeof(*mask); x++) { 310 if (p[x] != 0xff) 311 break; 312 } 313 y = 0; 314 if (x < sizeof(*mask)) { 315 for (y = 0; y < NBBY; y++) { 316 if ((p[x] & (0x80 >> y)) == 0) 317 break; 318 } 319 } 320 return x * NBBY + y; 321 } 322 323 static void 324 in_len2mask(struct in_addr *mask, u_int len) 325 { 326 u_int i; 327 u_char *p; 328 329 p = (u_char *)mask; 330 memset(mask, 0, sizeof(*mask)); 331 for (i = 0; i < len / NBBY; i++) 332 p[i] = 0xff; 333 if (len % NBBY) 334 p[i] = (0xff00 >> (len % NBBY)) & 0xff; 335 } 336 337 /* 338 * Generic internet control operations (ioctl's). 339 * Ifp is 0 if not an interface-specific ioctl. 340 */ 341 /* ARGSUSED */ 342 int 343 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 344 { 345 struct ifreq *ifr = (struct ifreq *)data; 346 struct in_ifaddr *ia = NULL; 347 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 348 struct sockaddr_in oldaddr; 349 int error, hostIsNew, maskIsNew; 350 int newifaddr = 0; 351 352 switch (cmd) { 353 case SIOCALIFADDR: 354 case SIOCDLIFADDR: 355 case SIOCGLIFADDR: 356 if (ifp == NULL) 357 return EINVAL; 358 return in_lifaddr_ioctl(so, cmd, data, ifp); 359 case SIOCGIFADDRPREF: 360 case SIOCSIFADDRPREF: 361 if (ifp == NULL) 362 return EINVAL; 363 return ifaddrpref_ioctl(so, cmd, data, ifp); 364 } 365 366 /* 367 * Find address for this interface, if it exists. 368 */ 369 if (ifp != NULL) 370 IFP_TO_IA(ifp, ia); 371 372 switch (cmd) { 373 case SIOCAIFADDR: 374 case SIOCDIFADDR: 375 case SIOCGIFALIAS: 376 if (ifra->ifra_addr.sin_family == AF_INET) 377 LIST_FOREACH(ia, 378 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr), 379 ia_hash) { 380 if (ia->ia_ifp == ifp && 381 in_hosteq(ia->ia_addr.sin_addr, 382 ifra->ifra_addr.sin_addr)) 383 break; 384 } 385 if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL) 386 return (EADDRNOTAVAIL); 387 388 if (cmd == SIOCDIFADDR && 389 ifra->ifra_addr.sin_family == AF_UNSPEC) { 390 ifra->ifra_addr.sin_family = AF_INET; 391 } 392 /* FALLTHROUGH */ 393 case SIOCSIFADDR: 394 case SIOCSIFDSTADDR: 395 if (ifra->ifra_addr.sin_family != AF_INET) 396 return (EAFNOSUPPORT); 397 /* FALLTHROUGH */ 398 case SIOCSIFNETMASK: 399 if (ifp == NULL) 400 panic("in_control"); 401 402 if (cmd == SIOCGIFALIAS) 403 break; 404 405 if (ia == NULL && 406 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) 407 return (EADDRNOTAVAIL); 408 409 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 410 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 411 NULL) != 0) 412 return (EPERM); 413 414 if (ia == NULL) { 415 ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO); 416 if (ia == NULL) 417 return (ENOBUFS); 418 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list); 419 ifaref(&ia->ia_ifa); 420 ifa_insert(ifp, &ia->ia_ifa); 421 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 422 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 423 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 424 #ifdef IPSELSRC 425 ia->ia_ifa.ifa_getifa = in_getifa; 426 #else /* IPSELSRC */ 427 ia->ia_ifa.ifa_getifa = NULL; 428 #endif /* IPSELSRC */ 429 ia->ia_sockmask.sin_len = 8; 430 if (ifp->if_flags & IFF_BROADCAST) { 431 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 432 ia->ia_broadaddr.sin_family = AF_INET; 433 } 434 ia->ia_ifp = ifp; 435 ia->ia_idsalt = cprng_fast32() % 65535; 436 LIST_INIT(&ia->ia_multiaddrs); 437 newifaddr = 1; 438 } 439 break; 440 441 case SIOCSIFBRDADDR: 442 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 443 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 444 NULL) != 0) 445 return (EPERM); 446 /* FALLTHROUGH */ 447 448 case SIOCGIFADDR: 449 case SIOCGIFNETMASK: 450 case SIOCGIFDSTADDR: 451 case SIOCGIFBRDADDR: 452 if (ia == NULL) 453 return (EADDRNOTAVAIL); 454 break; 455 } 456 error = 0; 457 switch (cmd) { 458 459 case SIOCGIFADDR: 460 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr)); 461 break; 462 463 case SIOCGIFBRDADDR: 464 if ((ifp->if_flags & IFF_BROADCAST) == 0) 465 return (EINVAL); 466 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr)); 467 break; 468 469 case SIOCGIFDSTADDR: 470 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 471 return (EINVAL); 472 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr)); 473 break; 474 475 case SIOCGIFNETMASK: 476 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask)); 477 break; 478 479 case SIOCSIFDSTADDR: 480 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 481 return (EINVAL); 482 oldaddr = ia->ia_dstaddr; 483 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr)); 484 if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) { 485 ia->ia_dstaddr = oldaddr; 486 return error; 487 } 488 if (ia->ia_flags & IFA_ROUTE) { 489 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr); 490 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST); 491 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 492 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP); 493 } 494 break; 495 496 case SIOCSIFBRDADDR: 497 if ((ifp->if_flags & IFF_BROADCAST) == 0) 498 return EINVAL; 499 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr)); 500 break; 501 502 case SIOCSIFADDR: 503 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)), 504 1); 505 if (error == 0) { 506 (void)pfil_run_hooks(if_pfil, 507 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR); 508 } 509 break; 510 511 case SIOCSIFNETMASK: 512 in_ifscrub(ifp, ia); 513 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr)); 514 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 515 error = in_ifinit(ifp, ia, NULL, 0); 516 break; 517 518 case SIOCAIFADDR: 519 maskIsNew = 0; 520 hostIsNew = 1; 521 if (ia->ia_addr.sin_family != AF_INET) 522 ; 523 else if (ifra->ifra_addr.sin_len == 0) { 524 ifra->ifra_addr = ia->ia_addr; 525 hostIsNew = 0; 526 } else if (in_hosteq(ia->ia_addr.sin_addr, 527 ifra->ifra_addr.sin_addr)) 528 hostIsNew = 0; 529 if (ifra->ifra_mask.sin_len) { 530 /* Only scrub if we control the prefix route, 531 * otherwise userland gets a bogus message */ 532 if ((ia->ia_flags & IFA_ROUTE)) 533 in_ifscrub(ifp, ia); 534 ia->ia_sockmask = ifra->ifra_mask; 535 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 536 maskIsNew = 1; 537 } 538 if ((ifp->if_flags & IFF_POINTOPOINT) && 539 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 540 /* Only scrub if we control the prefix route, 541 * otherwise userland gets a bogus message */ 542 if ((ia->ia_flags & IFA_ROUTE)) 543 in_ifscrub(ifp, ia); 544 ia->ia_dstaddr = ifra->ifra_dstaddr; 545 maskIsNew = 1; /* We lie; but the effect's the same */ 546 } 547 if (ifra->ifra_addr.sin_family == AF_INET && 548 (hostIsNew || maskIsNew)) { 549 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 550 } 551 if ((ifp->if_flags & IFF_BROADCAST) && 552 (ifra->ifra_broadaddr.sin_family == AF_INET)) 553 ia->ia_broadaddr = ifra->ifra_broadaddr; 554 if (error == 0) 555 (void)pfil_run_hooks(if_pfil, 556 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR); 557 break; 558 559 case SIOCGIFALIAS: 560 ifra->ifra_mask = ia->ia_sockmask; 561 if ((ifp->if_flags & IFF_POINTOPOINT) && 562 (ia->ia_dstaddr.sin_family == AF_INET)) 563 ifra->ifra_dstaddr = ia->ia_dstaddr; 564 else if ((ifp->if_flags & IFF_BROADCAST) && 565 (ia->ia_broadaddr.sin_family == AF_INET)) 566 ifra->ifra_broadaddr = ia->ia_broadaddr; 567 else 568 memset(&ifra->ifra_broadaddr, 0, 569 sizeof(ifra->ifra_broadaddr)); 570 break; 571 572 case SIOCDIFADDR: 573 in_purgeaddr(&ia->ia_ifa); 574 (void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR, 575 ifp, PFIL_IFADDR); 576 break; 577 578 #ifdef MROUTING 579 case SIOCGETVIFCNT: 580 case SIOCGETSGCNT: 581 error = mrt_ioctl(so, cmd, data); 582 break; 583 #endif /* MROUTING */ 584 585 default: 586 return ENOTTY; 587 } 588 589 if (error != 0 && newifaddr) { 590 KASSERT(ia != NULL); 591 in_purgeaddr(&ia->ia_ifa); 592 } 593 594 return error; 595 } 596 597 void 598 in_purgeaddr(struct ifaddr *ifa) 599 { 600 struct ifnet *ifp = ifa->ifa_ifp; 601 struct in_ifaddr *ia = (void *) ifa; 602 603 in_ifscrub(ifp, ia); 604 LIST_REMOVE(ia, ia_hash); 605 ifa_remove(ifp, &ia->ia_ifa); 606 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list); 607 if (ia->ia_allhosts != NULL) 608 in_delmulti(ia->ia_allhosts); 609 ifafree(&ia->ia_ifa); 610 in_setmaxmtu(); 611 } 612 613 void 614 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */ 615 { 616 if_purgeaddrs(ifp, AF_INET, in_purgeaddr); 617 igmp_purgeif(ifp); /* manipulates pools */ 618 #ifdef MROUTING 619 ip_mrouter_detach(ifp); 620 #endif 621 } 622 623 /* 624 * SIOC[GAD]LIFADDR. 625 * SIOCGLIFADDR: get first address. (???) 626 * SIOCGLIFADDR with IFLR_PREFIX: 627 * get first address that matches the specified prefix. 628 * SIOCALIFADDR: add the specified address. 629 * SIOCALIFADDR with IFLR_PREFIX: 630 * EINVAL since we can't deduce hostid part of the address. 631 * SIOCDLIFADDR: delete the specified address. 632 * SIOCDLIFADDR with IFLR_PREFIX: 633 * delete the first address that matches the specified prefix. 634 * return values: 635 * EINVAL on invalid parameters 636 * EADDRNOTAVAIL on prefix match failed/specified address not found 637 * other values may be returned from in_ioctl() 638 */ 639 static int 640 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data, 641 struct ifnet *ifp) 642 { 643 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 644 struct ifaddr *ifa; 645 struct sockaddr *sa; 646 647 /* sanity checks */ 648 if (data == NULL || ifp == NULL) { 649 panic("invalid argument to in_lifaddr_ioctl"); 650 /*NOTRECHED*/ 651 } 652 653 switch (cmd) { 654 case SIOCGLIFADDR: 655 /* address must be specified on GET with IFLR_PREFIX */ 656 if ((iflr->flags & IFLR_PREFIX) == 0) 657 break; 658 /*FALLTHROUGH*/ 659 case SIOCALIFADDR: 660 case SIOCDLIFADDR: 661 /* address must be specified on ADD and DELETE */ 662 sa = (struct sockaddr *)&iflr->addr; 663 if (sa->sa_family != AF_INET) 664 return EINVAL; 665 if (sa->sa_len != sizeof(struct sockaddr_in)) 666 return EINVAL; 667 /* XXX need improvement */ 668 sa = (struct sockaddr *)&iflr->dstaddr; 669 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET) 670 return EINVAL; 671 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in)) 672 return EINVAL; 673 break; 674 default: /*shouldn't happen*/ 675 #if 0 676 panic("invalid cmd to in_lifaddr_ioctl"); 677 /*NOTREACHED*/ 678 #else 679 return EOPNOTSUPP; 680 #endif 681 } 682 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen) 683 return EINVAL; 684 685 switch (cmd) { 686 case SIOCALIFADDR: 687 { 688 struct in_aliasreq ifra; 689 690 if (iflr->flags & IFLR_PREFIX) 691 return EINVAL; 692 693 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 694 memset(&ifra, 0, sizeof(ifra)); 695 memcpy(ifra.ifra_name, iflr->iflr_name, 696 sizeof(ifra.ifra_name)); 697 698 memcpy(&ifra.ifra_addr, &iflr->addr, 699 ((struct sockaddr *)&iflr->addr)->sa_len); 700 701 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 702 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr, 703 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 704 } 705 706 ifra.ifra_mask.sin_family = AF_INET; 707 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 708 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 709 710 return in_control(so, SIOCAIFADDR, &ifra, ifp); 711 } 712 case SIOCGLIFADDR: 713 case SIOCDLIFADDR: 714 { 715 struct in_ifaddr *ia; 716 struct in_addr mask, candidate, match; 717 struct sockaddr_in *sin; 718 int cmp; 719 720 memset(&mask, 0, sizeof(mask)); 721 memset(&match, 0, sizeof(match)); /* XXX gcc */ 722 if (iflr->flags & IFLR_PREFIX) { 723 /* lookup a prefix rather than address. */ 724 in_len2mask(&mask, iflr->prefixlen); 725 726 sin = (struct sockaddr_in *)&iflr->addr; 727 match.s_addr = sin->sin_addr.s_addr; 728 match.s_addr &= mask.s_addr; 729 730 /* if you set extra bits, that's wrong */ 731 if (match.s_addr != sin->sin_addr.s_addr) 732 return EINVAL; 733 734 cmp = 1; 735 } else { 736 if (cmd == SIOCGLIFADDR) { 737 /* on getting an address, take the 1st match */ 738 cmp = 0; /*XXX*/ 739 } else { 740 /* on deleting an address, do exact match */ 741 in_len2mask(&mask, 32); 742 sin = (struct sockaddr_in *)&iflr->addr; 743 match.s_addr = sin->sin_addr.s_addr; 744 745 cmp = 1; 746 } 747 } 748 749 IFADDR_FOREACH(ifa, ifp) { 750 if (ifa->ifa_addr->sa_family != AF_INET) 751 continue; 752 if (cmp == 0) 753 break; 754 candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; 755 candidate.s_addr &= mask.s_addr; 756 if (candidate.s_addr == match.s_addr) 757 break; 758 } 759 if (ifa == NULL) 760 return EADDRNOTAVAIL; 761 ia = (struct in_ifaddr *)ifa; 762 763 if (cmd == SIOCGLIFADDR) { 764 /* fill in the if_laddrreq structure */ 765 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len); 766 767 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 768 memcpy(&iflr->dstaddr, &ia->ia_dstaddr, 769 ia->ia_dstaddr.sin_len); 770 } else 771 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr)); 772 773 iflr->prefixlen = 774 in_mask2len(&ia->ia_sockmask.sin_addr); 775 776 iflr->flags = 0; /*XXX*/ 777 778 return 0; 779 } else { 780 struct in_aliasreq ifra; 781 782 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 783 memset(&ifra, 0, sizeof(ifra)); 784 memcpy(ifra.ifra_name, iflr->iflr_name, 785 sizeof(ifra.ifra_name)); 786 787 memcpy(&ifra.ifra_addr, &ia->ia_addr, 788 ia->ia_addr.sin_len); 789 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 790 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr, 791 ia->ia_dstaddr.sin_len); 792 } 793 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask, 794 ia->ia_sockmask.sin_len); 795 796 return in_control(so, SIOCDIFADDR, &ifra, ifp); 797 } 798 } 799 } 800 801 return EOPNOTSUPP; /*just for safety*/ 802 } 803 804 /* 805 * Delete any existing route for an interface. 806 */ 807 void 808 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 809 { 810 811 in_scrubprefix(ia); 812 } 813 814 /* 815 * Initialize an interface's internet address 816 * and routing table entry. 817 */ 818 int 819 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, 820 const struct sockaddr_in *sin, int scrub) 821 { 822 u_int32_t i; 823 struct sockaddr_in oldaddr; 824 int s = splnet(), flags = RTF_UP, error; 825 826 if (sin == NULL) 827 sin = &ia->ia_addr; 828 829 /* 830 * Set up new addresses. 831 */ 832 oldaddr = ia->ia_addr; 833 if (ia->ia_addr.sin_family == AF_INET) 834 LIST_REMOVE(ia, ia_hash); 835 ia->ia_addr = *sin; 836 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 837 838 /* 839 * Give the interface a chance to initialize 840 * if this is its first address, 841 * and to validate the address if necessary. 842 */ 843 if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0) 844 goto bad; 845 splx(s); 846 if (scrub) { 847 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 848 in_ifscrub(ifp, ia); 849 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 850 } 851 852 i = ia->ia_addr.sin_addr.s_addr; 853 if (IN_CLASSA(i)) 854 ia->ia_netmask = IN_CLASSA_NET; 855 else if (IN_CLASSB(i)) 856 ia->ia_netmask = IN_CLASSB_NET; 857 else 858 ia->ia_netmask = IN_CLASSC_NET; 859 /* 860 * The subnet mask usually includes at least the standard network part, 861 * but may may be smaller in the case of supernetting. 862 * If it is set, we believe it. 863 */ 864 if (ia->ia_subnetmask == 0) { 865 ia->ia_subnetmask = ia->ia_netmask; 866 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 867 } else 868 ia->ia_netmask &= ia->ia_subnetmask; 869 870 ia->ia_net = i & ia->ia_netmask; 871 ia->ia_subnet = i & ia->ia_subnetmask; 872 in_socktrim(&ia->ia_sockmask); 873 /* re-calculate the "in_maxmtu" value */ 874 in_setmaxmtu(); 875 /* 876 * Add route for the network. 877 */ 878 ia->ia_ifa.ifa_metric = ifp->if_metric; 879 if (ifp->if_flags & IFF_BROADCAST) { 880 ia->ia_broadaddr.sin_addr.s_addr = 881 ia->ia_subnet | ~ia->ia_subnetmask; 882 ia->ia_netbroadcast.s_addr = 883 ia->ia_net | ~ia->ia_netmask; 884 } else if (ifp->if_flags & IFF_LOOPBACK) { 885 ia->ia_dstaddr = ia->ia_addr; 886 flags |= RTF_HOST; 887 } else if (ifp->if_flags & IFF_POINTOPOINT) { 888 if (ia->ia_dstaddr.sin_family != AF_INET) 889 return (0); 890 flags |= RTF_HOST; 891 } 892 error = in_addprefix(ia, flags); 893 /* 894 * If the interface supports multicast, join the "all hosts" 895 * multicast group on that interface. 896 */ 897 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 898 struct in_addr addr; 899 900 addr.s_addr = INADDR_ALLHOSTS_GROUP; 901 ia->ia_allhosts = in_addmulti(&addr, ifp); 902 } 903 return (error); 904 bad: 905 splx(s); 906 LIST_REMOVE(ia, ia_hash); 907 ia->ia_addr = oldaddr; 908 if (ia->ia_addr.sin_family == AF_INET) 909 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), 910 ia, ia_hash); 911 return (error); 912 } 913 914 #define rtinitflags(x) \ 915 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 916 ? RTF_HOST : 0) 917 918 /* 919 * add a route to prefix ("connected route" in cisco terminology). 920 * does nothing if there's some interface address with the same prefix already. 921 */ 922 static int 923 in_addprefix(struct in_ifaddr *target, int flags) 924 { 925 struct in_ifaddr *ia; 926 struct in_addr prefix, mask, p; 927 int error; 928 929 if ((flags & RTF_HOST) != 0) 930 prefix = target->ia_dstaddr.sin_addr; 931 else { 932 prefix = target->ia_addr.sin_addr; 933 mask = target->ia_sockmask.sin_addr; 934 prefix.s_addr &= mask.s_addr; 935 } 936 937 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 938 if (rtinitflags(ia)) 939 p = ia->ia_dstaddr.sin_addr; 940 else { 941 p = ia->ia_addr.sin_addr; 942 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 943 } 944 945 if (prefix.s_addr != p.s_addr) 946 continue; 947 948 /* 949 * if we got a matching prefix route inserted by other 950 * interface address, we don't need to bother 951 * 952 * XXX RADIX_MPATH implications here? -dyoung 953 * 954 * But we should still notify userland of the new address 955 */ 956 if (ia->ia_flags & IFA_ROUTE) { 957 rt_newaddrmsg(RTM_NEWADDR, &target->ia_ifa, 0, NULL); 958 return 0; 959 } 960 } 961 962 /* 963 * noone seem to have prefix route. insert it. 964 */ 965 error = rtinit(&target->ia_ifa, RTM_ADD, flags); 966 if (error == 0) 967 target->ia_flags |= IFA_ROUTE; 968 else if (error == EEXIST) { 969 /* 970 * the fact the route already exists is not an error. 971 */ 972 error = 0; 973 } 974 return error; 975 } 976 977 /* 978 * remove a route to prefix ("connected route" in cisco terminology). 979 * re-installs the route by using another interface address, if there's one 980 * with the same prefix (otherwise we lose the route mistakenly). 981 */ 982 static int 983 in_scrubprefix(struct in_ifaddr *target) 984 { 985 struct in_ifaddr *ia; 986 struct in_addr prefix, mask, p; 987 int error; 988 989 /* If we don't have IFA_ROUTE we should still inform userland */ 990 if ((target->ia_flags & IFA_ROUTE) == 0) { 991 rt_newaddrmsg(RTM_DELADDR, &target->ia_ifa, 0, NULL); 992 return 0; 993 } 994 995 if (rtinitflags(target)) 996 prefix = target->ia_dstaddr.sin_addr; 997 else { 998 prefix = target->ia_addr.sin_addr; 999 mask = target->ia_sockmask.sin_addr; 1000 prefix.s_addr &= mask.s_addr; 1001 } 1002 1003 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 1004 if (rtinitflags(ia)) 1005 p = ia->ia_dstaddr.sin_addr; 1006 else { 1007 p = ia->ia_addr.sin_addr; 1008 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1009 } 1010 1011 if (prefix.s_addr != p.s_addr) 1012 continue; 1013 1014 /* 1015 * if we got a matching prefix route, move IFA_ROUTE to him 1016 */ 1017 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1018 rtinit(&target->ia_ifa, RTM_DELETE, 1019 rtinitflags(target)); 1020 target->ia_flags &= ~IFA_ROUTE; 1021 1022 error = rtinit(&ia->ia_ifa, RTM_ADD, 1023 rtinitflags(ia) | RTF_UP); 1024 if (error == 0) 1025 ia->ia_flags |= IFA_ROUTE; 1026 return error; 1027 } 1028 } 1029 1030 /* 1031 * noone seem to have prefix route. remove it. 1032 */ 1033 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target)); 1034 target->ia_flags &= ~IFA_ROUTE; 1035 return 0; 1036 } 1037 1038 #undef rtinitflags 1039 1040 /* 1041 * Return 1 if the address might be a local broadcast address. 1042 */ 1043 int 1044 in_broadcast(struct in_addr in, struct ifnet *ifp) 1045 { 1046 struct ifaddr *ifa; 1047 1048 if (in.s_addr == INADDR_BROADCAST || 1049 in_nullhost(in)) 1050 return 1; 1051 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1052 return 0; 1053 /* 1054 * Look through the list of addresses for a match 1055 * with a broadcast address. 1056 */ 1057 #define ia (ifatoia(ifa)) 1058 IFADDR_FOREACH(ifa, ifp) 1059 if (ifa->ifa_addr->sa_family == AF_INET && 1060 !in_hosteq(in, ia->ia_addr.sin_addr) && 1061 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 1062 in_hosteq(in, ia->ia_netbroadcast) || 1063 (hostzeroisbroadcast && 1064 /* 1065 * Check for old-style (host 0) broadcast. 1066 */ 1067 (in.s_addr == ia->ia_subnet || 1068 in.s_addr == ia->ia_net)))) 1069 return 1; 1070 return (0); 1071 #undef ia 1072 } 1073 1074 /* 1075 * in_lookup_multi: look up the in_multi record for a given IP 1076 * multicast address on a given interface. If no matching record is 1077 * found, return NULL. 1078 */ 1079 struct in_multi * 1080 in_lookup_multi(struct in_addr addr, ifnet_t *ifp) 1081 { 1082 struct in_multi *inm; 1083 1084 KASSERT(rw_lock_held(&in_multilock)); 1085 1086 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) { 1087 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp) 1088 break; 1089 } 1090 return inm; 1091 } 1092 1093 /* 1094 * in_multi_group: check whether the address belongs to an IP multicast 1095 * group we are joined on this interface. Returns true or false. 1096 */ 1097 bool 1098 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags) 1099 { 1100 bool ingroup; 1101 1102 if (__predict_true(flags & IP_IGMP_MCAST) == 0) { 1103 rw_enter(&in_multilock, RW_READER); 1104 ingroup = in_lookup_multi(addr, ifp) != NULL; 1105 rw_exit(&in_multilock); 1106 } else { 1107 /* XXX Recursive call from ip_output(). */ 1108 KASSERT(rw_lock_held(&in_multilock)); 1109 ingroup = in_lookup_multi(addr, ifp) != NULL; 1110 } 1111 return ingroup; 1112 } 1113 1114 /* 1115 * Add an address to the list of IP multicast addresses for a given interface. 1116 */ 1117 struct in_multi * 1118 in_addmulti(struct in_addr *ap, ifnet_t *ifp) 1119 { 1120 struct sockaddr_in sin; 1121 struct in_multi *inm; 1122 1123 /* 1124 * See if address already in list. 1125 */ 1126 rw_enter(&in_multilock, RW_WRITER); 1127 inm = in_lookup_multi(*ap, ifp); 1128 if (inm != NULL) { 1129 /* 1130 * Found it; just increment the reference count. 1131 */ 1132 inm->inm_refcount++; 1133 rw_exit(&in_multilock); 1134 return inm; 1135 } 1136 1137 /* 1138 * New address; allocate a new multicast record. 1139 */ 1140 inm = pool_get(&inmulti_pool, PR_NOWAIT); 1141 if (inm == NULL) { 1142 rw_exit(&in_multilock); 1143 return NULL; 1144 } 1145 inm->inm_addr = *ap; 1146 inm->inm_ifp = ifp; 1147 inm->inm_refcount = 1; 1148 1149 /* 1150 * Ask the network driver to update its multicast reception 1151 * filter appropriately for the new address. 1152 */ 1153 sockaddr_in_init(&sin, ap, 0); 1154 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) { 1155 rw_exit(&in_multilock); 1156 pool_put(&inmulti_pool, inm); 1157 return NULL; 1158 } 1159 1160 /* 1161 * Let IGMP know that we have joined a new IP multicast group. 1162 */ 1163 if (igmp_joingroup(inm) != 0) { 1164 rw_exit(&in_multilock); 1165 pool_put(&inmulti_pool, inm); 1166 return NULL; 1167 } 1168 LIST_INSERT_HEAD( 1169 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp), 1170 inm, inm_list); 1171 in_multientries++; 1172 rw_exit(&in_multilock); 1173 1174 return inm; 1175 } 1176 1177 /* 1178 * Delete a multicast address record. 1179 */ 1180 void 1181 in_delmulti(struct in_multi *inm) 1182 { 1183 struct sockaddr_in sin; 1184 1185 rw_enter(&in_multilock, RW_WRITER); 1186 if (--inm->inm_refcount > 0) { 1187 rw_exit(&in_multilock); 1188 return; 1189 } 1190 1191 /* 1192 * No remaining claims to this record; let IGMP know that 1193 * we are leaving the multicast group. 1194 */ 1195 igmp_leavegroup(inm); 1196 1197 /* 1198 * Notify the network driver to update its multicast reception 1199 * filter. 1200 */ 1201 sockaddr_in_init(&sin, &inm->inm_addr, 0); 1202 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin)); 1203 1204 /* 1205 * Unlink from list. 1206 */ 1207 LIST_REMOVE(inm, inm_list); 1208 in_multientries--; 1209 rw_exit(&in_multilock); 1210 1211 pool_put(&inmulti_pool, inm); 1212 } 1213 1214 /* 1215 * in_next_multi: step through all of the in_multi records, one at a time. 1216 * The current position is remembered in "step", which the caller must 1217 * provide. in_first_multi(), below, must be called to initialize "step" 1218 * and get the first record. Both macros return a NULL "inm" when there 1219 * are no remaining records. 1220 */ 1221 struct in_multi * 1222 in_next_multi(struct in_multistep *step) 1223 { 1224 struct in_multi *inm; 1225 1226 KASSERT(rw_lock_held(&in_multilock)); 1227 1228 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) { 1229 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]); 1230 } 1231 if ((inm = step->i_inm) != NULL) { 1232 step->i_inm = LIST_NEXT(inm, inm_list); 1233 } 1234 return inm; 1235 } 1236 1237 struct in_multi * 1238 in_first_multi(struct in_multistep *step) 1239 { 1240 KASSERT(rw_lock_held(&in_multilock)); 1241 1242 step->i_n = 0; 1243 step->i_inm = LIST_FIRST(&in_multihashtbl[0]); 1244 return in_next_multi(step); 1245 } 1246 1247 void 1248 in_multi_lock(int op) 1249 { 1250 rw_enter(&in_multilock, op); 1251 } 1252 1253 void 1254 in_multi_unlock(void) 1255 { 1256 rw_exit(&in_multilock); 1257 } 1258 1259 int 1260 in_multi_lock_held(void) 1261 { 1262 return rw_lock_held(&in_multilock); 1263 } 1264 1265 struct sockaddr_in * 1266 in_selectsrc(struct sockaddr_in *sin, struct route *ro, 1267 int soopts, struct ip_moptions *mopts, int *errorp) 1268 { 1269 struct rtentry *rt = NULL; 1270 struct in_ifaddr *ia = NULL; 1271 1272 /* 1273 * If route is known or can be allocated now, take the 1274 * source address from the interface. Otherwise, punt. 1275 */ 1276 if ((soopts & SO_DONTROUTE) != 0) 1277 rtcache_free(ro); 1278 else { 1279 union { 1280 struct sockaddr dst; 1281 struct sockaddr_in dst4; 1282 } u; 1283 1284 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0); 1285 rt = rtcache_lookup(ro, &u.dst); 1286 } 1287 /* 1288 * If we found a route, use the address 1289 * corresponding to the outgoing interface 1290 * unless it is the loopback (in case a route 1291 * to our address on another net goes to loopback). 1292 * 1293 * XXX Is this still true? Do we care? 1294 */ 1295 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) 1296 ia = ifatoia(rt->rt_ifa); 1297 if (ia == NULL) { 1298 u_int16_t fport = sin->sin_port; 1299 1300 sin->sin_port = 0; 1301 ia = ifatoia(ifa_ifwithladdr(sintosa(sin))); 1302 sin->sin_port = fport; 1303 if (ia == NULL) { 1304 /* Find 1st non-loopback AF_INET address */ 1305 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 1306 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) 1307 break; 1308 } 1309 } 1310 if (ia == NULL) { 1311 *errorp = EADDRNOTAVAIL; 1312 return NULL; 1313 } 1314 } 1315 /* 1316 * If the destination address is multicast and an outgoing 1317 * interface has been set as a multicast option, use the 1318 * address of that interface as our source address. 1319 */ 1320 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 1321 struct ip_moptions *imo; 1322 struct ifnet *ifp; 1323 1324 imo = mopts; 1325 if (imo->imo_multicast_ifp != NULL) { 1326 ifp = imo->imo_multicast_ifp; 1327 IFP_TO_IA(ifp, ia); /* XXX */ 1328 if (ia == 0) { 1329 *errorp = EADDRNOTAVAIL; 1330 return NULL; 1331 } 1332 } 1333 } 1334 if (ia->ia_ifa.ifa_getifa != NULL) { 1335 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa, 1336 sintosa(sin))); 1337 } 1338 #ifdef GETIFA_DEBUG 1339 else 1340 printf("%s: missing ifa_getifa\n", __func__); 1341 #endif 1342 return satosin(&ia->ia_addr); 1343 } 1344 1345 static void 1346 in_sysctl_init(struct sysctllog **clog) 1347 { 1348 sysctl_createv(clog, 0, NULL, NULL, 1349 CTLFLAG_PERMANENT, 1350 CTLTYPE_NODE, "inet", 1351 SYSCTL_DESCR("PF_INET related settings"), 1352 NULL, 0, NULL, 0, 1353 CTL_NET, PF_INET, CTL_EOL); 1354 sysctl_createv(clog, 0, NULL, NULL, 1355 CTLFLAG_PERMANENT, 1356 CTLTYPE_NODE, "ip", 1357 SYSCTL_DESCR("IPv4 related settings"), 1358 NULL, 0, NULL, 0, 1359 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL); 1360 1361 sysctl_createv(clog, 0, NULL, NULL, 1362 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1363 CTLTYPE_INT, "subnetsarelocal", 1364 SYSCTL_DESCR("Whether logical subnets are considered " 1365 "local"), 1366 NULL, 0, &subnetsarelocal, 0, 1367 CTL_NET, PF_INET, IPPROTO_IP, 1368 IPCTL_SUBNETSARELOCAL, CTL_EOL); 1369 sysctl_createv(clog, 0, NULL, NULL, 1370 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1371 CTLTYPE_INT, "hostzerobroadcast", 1372 SYSCTL_DESCR("All zeroes address is broadcast address"), 1373 NULL, 0, &hostzeroisbroadcast, 0, 1374 CTL_NET, PF_INET, IPPROTO_IP, 1375 IPCTL_HOSTZEROBROADCAST, CTL_EOL); 1376 } 1377