1 /* $NetBSD: in.c,v 1.241 2020/09/29 19:33:36 roy 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.241 2020/09/29 19:33:36 roy Exp $"); 95 96 #include "arp.h" 97 98 #ifdef _KERNEL_OPT 99 #include "opt_inet.h" 100 #include "opt_inet_conf.h" 101 #include "opt_mrouting.h" 102 #include "opt_net_mpsafe.h" 103 #endif 104 105 #include <sys/param.h> 106 #include <sys/ioctl.h> 107 #include <sys/errno.h> 108 #include <sys/kernel.h> 109 #include <sys/malloc.h> 110 #include <sys/socket.h> 111 #include <sys/socketvar.h> 112 #include <sys/sysctl.h> 113 #include <sys/systm.h> 114 #include <sys/proc.h> 115 #include <sys/syslog.h> 116 #include <sys/kauth.h> 117 #include <sys/kmem.h> 118 119 #include <sys/cprng.h> 120 121 #include <net/if.h> 122 #include <net/route.h> 123 #include <net/pfil.h> 124 125 #include <net/if_arp.h> 126 #include <net/if_ether.h> 127 #include <net/if_types.h> 128 #include <net/if_llatbl.h> 129 #include <net/if_dl.h> 130 131 #include <netinet/in_systm.h> 132 #include <netinet/in.h> 133 #include <netinet/in_var.h> 134 #include <netinet/ip.h> 135 #include <netinet/ip_var.h> 136 #include <netinet/in_ifattach.h> 137 #include <netinet/in_pcb.h> 138 #include <netinet/in_selsrc.h> 139 #include <netinet/if_inarp.h> 140 #include <netinet/ip_mroute.h> 141 #include <netinet/igmp_var.h> 142 143 #ifdef IPSELSRC 144 #include <netinet/in_selsrc.h> 145 #endif 146 147 static u_int in_mask2len(struct in_addr *); 148 static int in_lifaddr_ioctl(struct socket *, u_long, void *, 149 struct ifnet *); 150 151 static void in_addrhash_insert_locked(struct in_ifaddr *); 152 static void in_addrhash_remove_locked(struct in_ifaddr *); 153 154 static int in_addprefix(struct in_ifaddr *, int); 155 static void in_scrubaddr(struct in_ifaddr *); 156 static int in_scrubprefix(struct in_ifaddr *); 157 static void in_sysctl_init(struct sysctllog **); 158 159 #ifndef SUBNETSARELOCAL 160 #define SUBNETSARELOCAL 1 161 #endif 162 163 #ifndef HOSTZEROBROADCAST 164 #define HOSTZEROBROADCAST 0 165 #endif 166 167 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */ 168 #ifndef IN_MULTI_HASH_SIZE 169 #define IN_MULTI_HASH_SIZE 509 170 #endif 171 172 static int subnetsarelocal = SUBNETSARELOCAL; 173 static int hostzeroisbroadcast = HOSTZEROBROADCAST; 174 175 /* 176 * This list is used to keep track of in_multi chains which belong to 177 * deleted interface addresses. We use in_ifaddr so that a chain head 178 * won't be deallocated until all multicast address record are deleted. 179 */ 180 181 LIST_HEAD(in_multihashhead, in_multi); /* Type of the hash head */ 182 183 static struct pool inmulti_pool; 184 static u_int in_multientries; 185 static struct in_multihashhead *in_multihashtbl; 186 static u_long in_multihash; 187 static krwlock_t in_multilock; 188 189 #define IN_MULTI_HASH(x, ifp) \ 190 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE]) 191 192 /* XXX DEPRECATED. Keep them to avoid breaking kvm(3) users. */ 193 struct in_ifaddrhashhead * in_ifaddrhashtbl; 194 u_long in_ifaddrhash; 195 struct in_ifaddrhead in_ifaddrhead; 196 static kmutex_t in_ifaddr_lock; 197 198 pserialize_t in_ifaddrhash_psz; 199 struct pslist_head * in_ifaddrhashtbl_pslist; 200 u_long in_ifaddrhash_pslist; 201 struct pslist_head in_ifaddrhead_pslist; 202 203 void 204 in_init(void) 205 { 206 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", 207 NULL, IPL_SOFTNET); 208 TAILQ_INIT(&in_ifaddrhead); 209 PSLIST_INIT(&in_ifaddrhead_pslist); 210 211 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true, 212 &in_ifaddrhash); 213 214 in_ifaddrhash_psz = pserialize_create(); 215 in_ifaddrhashtbl_pslist = hashinit(IN_IFADDR_HASH_SIZE, HASH_PSLIST, 216 true, &in_ifaddrhash_pslist); 217 mutex_init(&in_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE); 218 219 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true, 220 &in_multihash); 221 rw_init(&in_multilock); 222 223 in_sysctl_init(NULL); 224 } 225 226 /* 227 * Return 1 if an internet address is for a ``local'' host 228 * (one to which we have a connection). If subnetsarelocal 229 * is true, this includes other subnets of the local net. 230 * Otherwise, it includes only the directly-connected (sub)nets. 231 */ 232 int 233 in_localaddr(struct in_addr in) 234 { 235 struct in_ifaddr *ia; 236 int localaddr = 0; 237 int s = pserialize_read_enter(); 238 239 if (subnetsarelocal) { 240 IN_ADDRLIST_READER_FOREACH(ia) { 241 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) { 242 localaddr = 1; 243 break; 244 } 245 } 246 } else { 247 IN_ADDRLIST_READER_FOREACH(ia) { 248 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) { 249 localaddr = 1; 250 break; 251 } 252 } 253 } 254 pserialize_read_exit(s); 255 256 return localaddr; 257 } 258 259 /* 260 * like in_localaddr() but can specify ifp. 261 */ 262 int 263 in_direct(struct in_addr in, struct ifnet *ifp) 264 { 265 struct ifaddr *ifa; 266 int localaddr = 0; 267 int s; 268 269 KASSERT(ifp != NULL); 270 271 #define ia (ifatoia(ifa)) 272 s = pserialize_read_enter(); 273 if (subnetsarelocal) { 274 IFADDR_READER_FOREACH(ifa, ifp) { 275 if (ifa->ifa_addr->sa_family == AF_INET && 276 ((in.s_addr & ia->ia_netmask) == ia->ia_net)) { 277 localaddr = 1; 278 break; 279 } 280 } 281 } else { 282 IFADDR_READER_FOREACH(ifa, ifp) { 283 if (ifa->ifa_addr->sa_family == AF_INET && 284 (in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) { 285 localaddr = 1; 286 break; 287 } 288 } 289 } 290 pserialize_read_exit(s); 291 292 return localaddr; 293 #undef ia 294 } 295 296 /* 297 * Determine whether an IP address is in a reserved set of addresses 298 * that may not be forwarded, or whether datagrams to that destination 299 * may be forwarded. 300 */ 301 int 302 in_canforward(struct in_addr in) 303 { 304 u_int32_t net; 305 306 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 307 return (0); 308 if (IN_CLASSA(in.s_addr)) { 309 net = in.s_addr & IN_CLASSA_NET; 310 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 311 return (0); 312 } 313 return (1); 314 } 315 316 /* 317 * Trim a mask in a sockaddr 318 */ 319 void 320 in_socktrim(struct sockaddr_in *ap) 321 { 322 char *cplim = (char *) &ap->sin_addr; 323 char *cp = (char *) (&ap->sin_addr + 1); 324 325 ap->sin_len = 0; 326 while (--cp >= cplim) 327 if (*cp) { 328 (ap)->sin_len = cp - (char *) (ap) + 1; 329 break; 330 } 331 } 332 333 /* 334 * Maintain the "in_maxmtu" variable, which is the largest 335 * mtu for non-local interfaces with AF_INET addresses assigned 336 * to them that are up. 337 */ 338 unsigned long in_maxmtu; 339 340 void 341 in_setmaxmtu(void) 342 { 343 struct in_ifaddr *ia; 344 struct ifnet *ifp; 345 unsigned long maxmtu = 0; 346 int s = pserialize_read_enter(); 347 348 IN_ADDRLIST_READER_FOREACH(ia) { 349 if ((ifp = ia->ia_ifp) == 0) 350 continue; 351 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP) 352 continue; 353 if (ifp->if_mtu > maxmtu) 354 maxmtu = ifp->if_mtu; 355 } 356 if (maxmtu) 357 in_maxmtu = maxmtu; 358 pserialize_read_exit(s); 359 } 360 361 static u_int 362 in_mask2len(struct in_addr *mask) 363 { 364 u_int x, y; 365 u_char *p; 366 367 p = (u_char *)mask; 368 for (x = 0; x < sizeof(*mask); x++) { 369 if (p[x] != 0xff) 370 break; 371 } 372 y = 0; 373 if (x < sizeof(*mask)) { 374 for (y = 0; y < NBBY; y++) { 375 if ((p[x] & (0x80 >> y)) == 0) 376 break; 377 } 378 } 379 return x * NBBY + y; 380 } 381 382 void 383 in_len2mask(struct in_addr *mask, u_int len) 384 { 385 u_int i; 386 u_char *p; 387 388 p = (u_char *)mask; 389 memset(mask, 0, sizeof(*mask)); 390 for (i = 0; i < len / NBBY; i++) 391 p[i] = 0xff; 392 if (len % NBBY) 393 p[i] = (0xff00 >> (len % NBBY)) & 0xff; 394 } 395 396 /* 397 * Generic internet control operations (ioctl's). 398 * Ifp is 0 if not an interface-specific ioctl. 399 */ 400 /* ARGSUSED */ 401 static int 402 in_control0(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 403 { 404 struct ifreq *ifr = (struct ifreq *)data; 405 struct in_ifaddr *ia = NULL; 406 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 407 struct sockaddr_in oldaddr, *new_dstaddr; 408 int error, hostIsNew, maskIsNew; 409 int newifaddr = 0; 410 bool run_hook = false; 411 bool need_reinsert = false; 412 struct psref psref; 413 int bound; 414 415 switch (cmd) { 416 case SIOCALIFADDR: 417 case SIOCDLIFADDR: 418 case SIOCGLIFADDR: 419 if (ifp == NULL) 420 return EINVAL; 421 return in_lifaddr_ioctl(so, cmd, data, ifp); 422 case SIOCGIFADDRPREF: 423 case SIOCSIFADDRPREF: 424 if (ifp == NULL) 425 return EINVAL; 426 return ifaddrpref_ioctl(so, cmd, data, ifp); 427 #if NARP > 0 428 case SIOCGNBRINFO: 429 { 430 struct in_nbrinfo *nbi = (struct in_nbrinfo *)data; 431 struct llentry *ln; 432 struct in_addr nb_addr = nbi->addr; /* make local for safety */ 433 434 ln = arplookup(ifp, &nb_addr, NULL, 0); 435 if (ln == NULL) 436 return EINVAL; 437 nbi->state = ln->ln_state; 438 nbi->asked = ln->ln_asked; 439 nbi->expire = ln->ln_expire ? 440 time_mono_to_wall(ln->ln_expire) : 0; 441 LLE_RUNLOCK(ln); 442 return 0; 443 } 444 #endif 445 } 446 447 bound = curlwp_bind(); 448 /* 449 * Find address for this interface, if it exists. 450 */ 451 if (ifp != NULL) 452 ia = in_get_ia_from_ifp_psref(ifp, &psref); 453 454 hostIsNew = 1; /* moved here to appease gcc */ 455 switch (cmd) { 456 case SIOCAIFADDR: 457 case SIOCDIFADDR: 458 case SIOCGIFALIAS: 459 case SIOCGIFAFLAG_IN: 460 if (ifra->ifra_addr.sin_family == AF_INET) { 461 int s; 462 463 if (ia != NULL) 464 ia4_release(ia, &psref); 465 s = pserialize_read_enter(); 466 IN_ADDRHASH_READER_FOREACH(ia, 467 ifra->ifra_addr.sin_addr.s_addr) { 468 if (ia->ia_ifp == ifp && 469 in_hosteq(ia->ia_addr.sin_addr, 470 ifra->ifra_addr.sin_addr)) 471 break; 472 } 473 if (ia != NULL) 474 ia4_acquire(ia, &psref); 475 pserialize_read_exit(s); 476 } 477 if ((cmd == SIOCDIFADDR || 478 cmd == SIOCGIFALIAS || 479 cmd == SIOCGIFAFLAG_IN) && 480 ia == NULL) { 481 error = EADDRNOTAVAIL; 482 goto out; 483 } 484 485 if (cmd == SIOCDIFADDR && 486 ifra->ifra_addr.sin_family == AF_UNSPEC) { 487 ifra->ifra_addr.sin_family = AF_INET; 488 } 489 /* FALLTHROUGH */ 490 case SIOCSIFADDR: 491 if (ia == NULL || ia->ia_addr.sin_family != AF_INET) 492 ; 493 else if (ifra->ifra_addr.sin_len == 0) { 494 ifra->ifra_addr = ia->ia_addr; 495 hostIsNew = 0; 496 } else if (in_hosteq(ia->ia_addr.sin_addr, 497 ifra->ifra_addr.sin_addr)) 498 hostIsNew = 0; 499 if (ifra->ifra_addr.sin_family != AF_INET) { 500 error = EAFNOSUPPORT; 501 goto out; 502 } 503 /* FALLTHROUGH */ 504 case SIOCSIFDSTADDR: 505 if (cmd == SIOCSIFDSTADDR && 506 ifreq_getaddr(cmd, ifr)->sa_family != AF_INET) { 507 error = EAFNOSUPPORT; 508 goto out; 509 } 510 /* FALLTHROUGH */ 511 case SIOCSIFNETMASK: 512 if (ifp == NULL) 513 panic("in_control"); 514 515 if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN) 516 break; 517 518 if (ia == NULL && 519 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) { 520 error = EADDRNOTAVAIL; 521 goto out; 522 } 523 524 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 525 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 526 NULL) != 0) { 527 error = EPERM; 528 goto out; 529 } 530 531 if (ia == NULL) { 532 ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO); 533 if (ia == NULL) { 534 error = ENOBUFS; 535 goto out; 536 } 537 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 538 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 539 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 540 #ifdef IPSELSRC 541 ia->ia_ifa.ifa_getifa = in_getifa; 542 #else /* IPSELSRC */ 543 ia->ia_ifa.ifa_getifa = NULL; 544 #endif /* IPSELSRC */ 545 ia->ia_sockmask.sin_len = 8; 546 ia->ia_sockmask.sin_family = AF_INET; 547 if (ifp->if_flags & IFF_BROADCAST) { 548 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 549 ia->ia_broadaddr.sin_family = AF_INET; 550 } 551 ia->ia_ifp = ifp; 552 ia->ia_idsalt = cprng_fast32() % 65535; 553 LIST_INIT(&ia->ia_multiaddrs); 554 IN_ADDRHASH_ENTRY_INIT(ia); 555 IN_ADDRLIST_ENTRY_INIT(ia); 556 ifa_psref_init(&ia->ia_ifa); 557 /* 558 * We need a reference to make ia survive over in_ifinit 559 * that does ifaref and ifafree. 560 */ 561 ifaref(&ia->ia_ifa); 562 563 newifaddr = 1; 564 } 565 break; 566 567 case SIOCSIFBRDADDR: 568 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 569 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 570 NULL) != 0) { 571 error = EPERM; 572 goto out; 573 } 574 /* FALLTHROUGH */ 575 576 case SIOCGIFADDR: 577 case SIOCGIFNETMASK: 578 case SIOCGIFDSTADDR: 579 case SIOCGIFBRDADDR: 580 if (ia == NULL) { 581 error = EADDRNOTAVAIL; 582 goto out; 583 } 584 break; 585 } 586 error = 0; 587 switch (cmd) { 588 589 case SIOCGIFADDR: 590 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr)); 591 break; 592 593 case SIOCGIFBRDADDR: 594 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 595 error = EINVAL; 596 goto out; 597 } 598 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr)); 599 break; 600 601 case SIOCGIFDSTADDR: 602 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 603 error = EINVAL; 604 goto out; 605 } 606 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr)); 607 break; 608 609 case SIOCGIFNETMASK: 610 /* 611 * We keep the number of trailing zero bytes the sin_len field 612 * of ia_sockmask, so we fix this before we pass it back to 613 * userland. 614 */ 615 oldaddr = ia->ia_sockmask; 616 oldaddr.sin_len = sizeof(struct sockaddr_in); 617 ifreq_setaddr(cmd, ifr, (const void *)&oldaddr); 618 break; 619 620 case SIOCSIFDSTADDR: 621 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 622 error = EINVAL; 623 goto out; 624 } 625 oldaddr = ia->ia_dstaddr; 626 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr)); 627 if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) { 628 ia->ia_dstaddr = oldaddr; 629 goto out; 630 } 631 if (ia->ia_flags & IFA_ROUTE) { 632 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr); 633 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST); 634 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 635 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP); 636 } 637 break; 638 639 case SIOCSIFBRDADDR: 640 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 641 error = EINVAL; 642 goto out; 643 } 644 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr)); 645 break; 646 647 case SIOCSIFADDR: 648 if (!newifaddr) { 649 in_addrhash_remove(ia); 650 need_reinsert = true; 651 } 652 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)), 653 NULL, 1); 654 655 run_hook = true; 656 break; 657 658 case SIOCSIFNETMASK: 659 in_scrubprefix(ia); 660 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr)); 661 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 662 if (!newifaddr) { 663 in_addrhash_remove(ia); 664 need_reinsert = true; 665 } 666 error = in_ifinit(ifp, ia, NULL, NULL, 0); 667 break; 668 669 case SIOCAIFADDR: 670 maskIsNew = 0; 671 if (ifra->ifra_mask.sin_len) { 672 in_scrubprefix(ia); 673 ia->ia_sockmask = ifra->ifra_mask; 674 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 675 maskIsNew = 1; 676 } 677 if ((ifp->if_flags & IFF_POINTOPOINT) && 678 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 679 new_dstaddr = &ifra->ifra_dstaddr; 680 maskIsNew = 1; /* We lie; but the effect's the same */ 681 } else 682 new_dstaddr = NULL; 683 if (ifra->ifra_addr.sin_family == AF_INET && 684 (hostIsNew || maskIsNew)) { 685 if (!newifaddr) { 686 in_addrhash_remove(ia); 687 need_reinsert = true; 688 } 689 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 690 new_dstaddr, 0); 691 } 692 if ((ifp->if_flags & IFF_BROADCAST) && 693 (ifra->ifra_broadaddr.sin_family == AF_INET)) 694 ia->ia_broadaddr = ifra->ifra_broadaddr; 695 run_hook = true; 696 break; 697 698 case SIOCGIFALIAS: 699 ifra->ifra_mask = ia->ia_sockmask; 700 if ((ifp->if_flags & IFF_POINTOPOINT) && 701 (ia->ia_dstaddr.sin_family == AF_INET)) 702 ifra->ifra_dstaddr = ia->ia_dstaddr; 703 else if ((ifp->if_flags & IFF_BROADCAST) && 704 (ia->ia_broadaddr.sin_family == AF_INET)) 705 ifra->ifra_broadaddr = ia->ia_broadaddr; 706 else 707 memset(&ifra->ifra_broadaddr, 0, 708 sizeof(ifra->ifra_broadaddr)); 709 break; 710 711 case SIOCGIFAFLAG_IN: 712 ifr->ifr_addrflags = ia->ia4_flags; 713 break; 714 715 case SIOCDIFADDR: 716 ia4_release(ia, &psref); 717 ifaref(&ia->ia_ifa); 718 in_purgeaddr(&ia->ia_ifa); 719 pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa); 720 ifafree(&ia->ia_ifa); 721 ia = NULL; 722 break; 723 724 #ifdef MROUTING 725 case SIOCGETVIFCNT: 726 case SIOCGETSGCNT: 727 error = mrt_ioctl(so, cmd, data); 728 break; 729 #endif /* MROUTING */ 730 731 default: 732 error = ENOTTY; 733 goto out; 734 } 735 736 /* 737 * XXX insert regardless of error to make in_purgeaddr below work. 738 * Need to improve. 739 */ 740 if (newifaddr) { 741 ifaref(&ia->ia_ifa); 742 ifa_insert(ifp, &ia->ia_ifa); 743 744 mutex_enter(&in_ifaddr_lock); 745 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list); 746 IN_ADDRLIST_WRITER_INSERT_TAIL(ia); 747 in_addrhash_insert_locked(ia); 748 /* Release a reference that is held just after creation. */ 749 ifafree(&ia->ia_ifa); 750 mutex_exit(&in_ifaddr_lock); 751 } else if (need_reinsert) { 752 in_addrhash_insert(ia); 753 } 754 755 if (error == 0) { 756 if (run_hook) 757 pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa); 758 } else if (newifaddr) { 759 KASSERT(ia != NULL); 760 in_purgeaddr(&ia->ia_ifa); 761 ia = NULL; 762 } 763 764 out: 765 if (!newifaddr && ia != NULL) 766 ia4_release(ia, &psref); 767 curlwp_bindx(bound); 768 return error; 769 } 770 771 int 772 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 773 { 774 int error; 775 776 #ifndef NET_MPSAFE 777 KASSERT(KERNEL_LOCKED_P()); 778 #endif 779 error = in_control0(so, cmd, data, ifp); 780 781 return error; 782 } 783 784 /* Add ownaddr as loopback rtentry. */ 785 static void 786 in_ifaddlocal(struct ifaddr *ifa) 787 { 788 struct in_ifaddr *ia; 789 790 ia = (struct in_ifaddr *)ifa; 791 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY || 792 (ia->ia_ifp->if_flags & IFF_POINTOPOINT && 793 in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr))) 794 { 795 rt_addrmsg(RTM_NEWADDR, ifa); 796 return; 797 } 798 799 rt_ifa_addlocal(ifa); 800 } 801 802 /* Remove loopback entry of ownaddr */ 803 static void 804 in_ifremlocal(struct ifaddr *ifa) 805 { 806 struct in_ifaddr *ia, *p; 807 struct ifaddr *alt_ifa = NULL; 808 int ia_count = 0; 809 int s; 810 struct psref psref; 811 int bound = curlwp_bind(); 812 813 ia = (struct in_ifaddr *)ifa; 814 /* Delete the entry if exactly one ifaddr matches the 815 * address, ifa->ifa_addr. */ 816 s = pserialize_read_enter(); 817 IN_ADDRLIST_READER_FOREACH(p) { 818 if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr)) 819 continue; 820 if (p->ia_ifp != ia->ia_ifp) 821 alt_ifa = &p->ia_ifa; 822 if (++ia_count > 1 && alt_ifa != NULL) 823 break; 824 } 825 if (alt_ifa != NULL && ia_count > 1) 826 ifa_acquire(alt_ifa, &psref); 827 pserialize_read_exit(s); 828 829 if (ia_count == 0) 830 goto out; 831 832 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa); 833 if (alt_ifa != NULL && ia_count > 1) 834 ifa_release(alt_ifa, &psref); 835 out: 836 curlwp_bindx(bound); 837 } 838 839 static void 840 in_scrubaddr(struct in_ifaddr *ia) 841 { 842 843 /* stop DAD processing */ 844 if (ia->ia_dad_stop != NULL) 845 ia->ia_dad_stop(&ia->ia_ifa); 846 847 in_scrubprefix(ia); 848 in_ifremlocal(&ia->ia_ifa); 849 850 mutex_enter(&in_ifaddr_lock); 851 if (ia->ia_allhosts != NULL) { 852 in_delmulti(ia->ia_allhosts); 853 ia->ia_allhosts = NULL; 854 } 855 mutex_exit(&in_ifaddr_lock); 856 } 857 858 /* 859 * Depends on it isn't called in concurrent. It should be guaranteed 860 * by ifa->ifa_ifp's ioctl lock. The possible callers are in_control 861 * and if_purgeaddrs; the former is called iva ifa->ifa_ifp's ioctl 862 * and the latter is called via ifa->ifa_ifp's if_detach. The functions 863 * never be executed in concurrent. 864 */ 865 void 866 in_purgeaddr(struct ifaddr *ifa) 867 { 868 struct in_ifaddr *ia = (void *) ifa; 869 struct ifnet *ifp = ifa->ifa_ifp; 870 871 /* KASSERT(!ifa_held(ifa)); XXX need ifa_not_held (psref_not_held) */ 872 873 ifa->ifa_flags |= IFA_DESTROYING; 874 in_scrubaddr(ia); 875 876 mutex_enter(&in_ifaddr_lock); 877 in_addrhash_remove_locked(ia); 878 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list); 879 IN_ADDRLIST_WRITER_REMOVE(ia); 880 ifa_remove(ifp, &ia->ia_ifa); 881 /* Assume ifa_remove called pserialize_perform and psref_destroy */ 882 mutex_exit(&in_ifaddr_lock); 883 IN_ADDRHASH_ENTRY_DESTROY(ia); 884 IN_ADDRLIST_ENTRY_DESTROY(ia); 885 ifafree(&ia->ia_ifa); 886 in_setmaxmtu(); 887 } 888 889 static void 890 in_addrhash_insert_locked(struct in_ifaddr *ia) 891 { 892 893 KASSERT(mutex_owned(&in_ifaddr_lock)); 894 895 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, 896 ia_hash); 897 IN_ADDRHASH_ENTRY_INIT(ia); 898 IN_ADDRHASH_WRITER_INSERT_HEAD(ia); 899 } 900 901 void 902 in_addrhash_insert(struct in_ifaddr *ia) 903 { 904 905 mutex_enter(&in_ifaddr_lock); 906 in_addrhash_insert_locked(ia); 907 mutex_exit(&in_ifaddr_lock); 908 } 909 910 static void 911 in_addrhash_remove_locked(struct in_ifaddr *ia) 912 { 913 914 KASSERT(mutex_owned(&in_ifaddr_lock)); 915 916 LIST_REMOVE(ia, ia_hash); 917 IN_ADDRHASH_WRITER_REMOVE(ia); 918 } 919 920 void 921 in_addrhash_remove(struct in_ifaddr *ia) 922 { 923 924 mutex_enter(&in_ifaddr_lock); 925 in_addrhash_remove_locked(ia); 926 #ifdef NET_MPSAFE 927 pserialize_perform(in_ifaddrhash_psz); 928 #endif 929 mutex_exit(&in_ifaddr_lock); 930 IN_ADDRHASH_ENTRY_DESTROY(ia); 931 } 932 933 void 934 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */ 935 { 936 937 IFNET_LOCK(ifp); 938 if_purgeaddrs(ifp, AF_INET, in_purgeaddr); 939 igmp_purgeif(ifp); /* manipulates pools */ 940 #ifdef MROUTING 941 ip_mrouter_detach(ifp); 942 #endif 943 IFNET_UNLOCK(ifp); 944 } 945 946 /* 947 * SIOC[GAD]LIFADDR. 948 * SIOCGLIFADDR: get first address. (???) 949 * SIOCGLIFADDR with IFLR_PREFIX: 950 * get first address that matches the specified prefix. 951 * SIOCALIFADDR: add the specified address. 952 * SIOCALIFADDR with IFLR_PREFIX: 953 * EINVAL since we can't deduce hostid part of the address. 954 * SIOCDLIFADDR: delete the specified address. 955 * SIOCDLIFADDR with IFLR_PREFIX: 956 * delete the first address that matches the specified prefix. 957 * return values: 958 * EINVAL on invalid parameters 959 * EADDRNOTAVAIL on prefix match failed/specified address not found 960 * other values may be returned from in_ioctl() 961 */ 962 static int 963 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data, 964 struct ifnet *ifp) 965 { 966 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 967 struct ifaddr *ifa; 968 struct sockaddr *sa; 969 970 /* sanity checks */ 971 if (data == NULL || ifp == NULL) { 972 panic("invalid argument to in_lifaddr_ioctl"); 973 /*NOTRECHED*/ 974 } 975 976 switch (cmd) { 977 case SIOCGLIFADDR: 978 /* address must be specified on GET with IFLR_PREFIX */ 979 if ((iflr->flags & IFLR_PREFIX) == 0) 980 break; 981 /*FALLTHROUGH*/ 982 case SIOCALIFADDR: 983 case SIOCDLIFADDR: 984 /* address must be specified on ADD and DELETE */ 985 sa = (struct sockaddr *)&iflr->addr; 986 if (sa->sa_family != AF_INET) 987 return EINVAL; 988 if (sa->sa_len != sizeof(struct sockaddr_in)) 989 return EINVAL; 990 /* XXX need improvement */ 991 sa = (struct sockaddr *)&iflr->dstaddr; 992 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET) 993 return EINVAL; 994 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in)) 995 return EINVAL; 996 break; 997 default: /*shouldn't happen*/ 998 #if 0 999 panic("invalid cmd to in_lifaddr_ioctl"); 1000 /*NOTREACHED*/ 1001 #else 1002 return EOPNOTSUPP; 1003 #endif 1004 } 1005 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen) 1006 return EINVAL; 1007 1008 switch (cmd) { 1009 case SIOCALIFADDR: 1010 { 1011 struct in_aliasreq ifra; 1012 1013 if (iflr->flags & IFLR_PREFIX) 1014 return EINVAL; 1015 1016 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 1017 memset(&ifra, 0, sizeof(ifra)); 1018 memcpy(ifra.ifra_name, iflr->iflr_name, 1019 sizeof(ifra.ifra_name)); 1020 1021 memcpy(&ifra.ifra_addr, &iflr->addr, 1022 ((struct sockaddr *)&iflr->addr)->sa_len); 1023 1024 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 1025 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr, 1026 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1027 } 1028 1029 ifra.ifra_mask.sin_family = AF_INET; 1030 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 1031 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 1032 1033 return in_control(so, SIOCAIFADDR, &ifra, ifp); 1034 } 1035 case SIOCGLIFADDR: 1036 case SIOCDLIFADDR: 1037 { 1038 struct in_ifaddr *ia; 1039 struct in_addr mask, candidate, match; 1040 struct sockaddr_in *sin; 1041 int cmp, s; 1042 1043 memset(&mask, 0, sizeof(mask)); 1044 memset(&match, 0, sizeof(match)); /* XXX gcc */ 1045 if (iflr->flags & IFLR_PREFIX) { 1046 /* lookup a prefix rather than address. */ 1047 in_len2mask(&mask, iflr->prefixlen); 1048 1049 sin = (struct sockaddr_in *)&iflr->addr; 1050 match.s_addr = sin->sin_addr.s_addr; 1051 match.s_addr &= mask.s_addr; 1052 1053 /* if you set extra bits, that's wrong */ 1054 if (match.s_addr != sin->sin_addr.s_addr) 1055 return EINVAL; 1056 1057 cmp = 1; 1058 } else { 1059 if (cmd == SIOCGLIFADDR) { 1060 /* on getting an address, take the 1st match */ 1061 cmp = 0; /*XXX*/ 1062 } else { 1063 /* on deleting an address, do exact match */ 1064 in_len2mask(&mask, 32); 1065 sin = (struct sockaddr_in *)&iflr->addr; 1066 match.s_addr = sin->sin_addr.s_addr; 1067 1068 cmp = 1; 1069 } 1070 } 1071 1072 s = pserialize_read_enter(); 1073 IFADDR_READER_FOREACH(ifa, ifp) { 1074 if (ifa->ifa_addr->sa_family != AF_INET) 1075 continue; 1076 if (cmp == 0) 1077 break; 1078 candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; 1079 candidate.s_addr &= mask.s_addr; 1080 if (candidate.s_addr == match.s_addr) 1081 break; 1082 } 1083 if (ifa == NULL) { 1084 pserialize_read_exit(s); 1085 return EADDRNOTAVAIL; 1086 } 1087 ia = (struct in_ifaddr *)ifa; 1088 1089 if (cmd == SIOCGLIFADDR) { 1090 /* fill in the if_laddrreq structure */ 1091 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len); 1092 1093 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1094 memcpy(&iflr->dstaddr, &ia->ia_dstaddr, 1095 ia->ia_dstaddr.sin_len); 1096 } else 1097 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr)); 1098 1099 iflr->prefixlen = 1100 in_mask2len(&ia->ia_sockmask.sin_addr); 1101 1102 iflr->flags = 0; /*XXX*/ 1103 pserialize_read_exit(s); 1104 1105 return 0; 1106 } else { 1107 struct in_aliasreq ifra; 1108 1109 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 1110 memset(&ifra, 0, sizeof(ifra)); 1111 memcpy(ifra.ifra_name, iflr->iflr_name, 1112 sizeof(ifra.ifra_name)); 1113 1114 memcpy(&ifra.ifra_addr, &ia->ia_addr, 1115 ia->ia_addr.sin_len); 1116 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1117 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr, 1118 ia->ia_dstaddr.sin_len); 1119 } 1120 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask, 1121 ia->ia_sockmask.sin_len); 1122 pserialize_read_exit(s); 1123 1124 return in_control(so, SIOCDIFADDR, &ifra, ifp); 1125 } 1126 } 1127 } 1128 1129 return EOPNOTSUPP; /*just for safety*/ 1130 } 1131 1132 /* 1133 * Initialize an interface's internet address 1134 * and routing table entry. 1135 */ 1136 int 1137 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, 1138 const struct sockaddr_in *sin, const struct sockaddr_in *dst, int scrub) 1139 { 1140 u_int32_t i; 1141 struct sockaddr_in oldaddr, olddst; 1142 int s, oldflags, flags = RTF_UP, error, hostIsNew; 1143 1144 if (sin == NULL) 1145 sin = &ia->ia_addr; 1146 if (dst == NULL) 1147 dst = &ia->ia_dstaddr; 1148 1149 /* 1150 * Set up new addresses. 1151 */ 1152 oldaddr = ia->ia_addr; 1153 olddst = ia->ia_dstaddr; 1154 oldflags = ia->ia4_flags; 1155 ia->ia_addr = *sin; 1156 ia->ia_dstaddr = *dst; 1157 hostIsNew = oldaddr.sin_family != AF_INET || 1158 !in_hosteq(ia->ia_addr.sin_addr, oldaddr.sin_addr); 1159 if (!scrub) 1160 scrub = oldaddr.sin_family != ia->ia_dstaddr.sin_family || 1161 !in_hosteq(ia->ia_dstaddr.sin_addr, olddst.sin_addr); 1162 1163 /* 1164 * Configure address flags. 1165 * We need to do this early because they may be adjusted 1166 * by if_addr_init depending on the address. 1167 */ 1168 if (ia->ia4_flags & IN_IFF_DUPLICATED) { 1169 ia->ia4_flags &= ~IN_IFF_DUPLICATED; 1170 hostIsNew = 1; 1171 } 1172 if (ifp->if_link_state == LINK_STATE_DOWN) { 1173 ia->ia4_flags |= IN_IFF_DETACHED; 1174 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1175 } else if (hostIsNew && if_do_dad(ifp) && ip_dad_enabled()) 1176 ia->ia4_flags |= IN_IFF_TRYTENTATIVE; 1177 1178 /* 1179 * Give the interface a chance to initialize 1180 * if this is its first address, 1181 * and to validate the address if necessary. 1182 */ 1183 s = splsoftnet(); 1184 error = if_addr_init(ifp, &ia->ia_ifa, true); 1185 splx(s); 1186 /* Now clear the try tentative flag, its job is done. */ 1187 ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE; 1188 if (error != 0) { 1189 ia->ia_addr = oldaddr; 1190 ia->ia_dstaddr = olddst; 1191 ia->ia4_flags = oldflags; 1192 return error; 1193 } 1194 1195 if (scrub || hostIsNew) { 1196 int newflags = ia->ia4_flags; 1197 1198 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 1199 ia->ia_ifa.ifa_dstaddr = sintosa(&olddst); 1200 ia->ia4_flags = oldflags; 1201 if (hostIsNew) 1202 in_scrubaddr(ia); 1203 else if (scrub) 1204 in_scrubprefix(ia); 1205 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 1206 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 1207 ia->ia4_flags = newflags; 1208 } 1209 1210 i = ia->ia_addr.sin_addr.s_addr; 1211 if (ifp->if_flags & IFF_POINTOPOINT) 1212 ia->ia_netmask = INADDR_BROADCAST; /* default to /32 */ 1213 else if (IN_CLASSA(i)) 1214 ia->ia_netmask = IN_CLASSA_NET; 1215 else if (IN_CLASSB(i)) 1216 ia->ia_netmask = IN_CLASSB_NET; 1217 else 1218 ia->ia_netmask = IN_CLASSC_NET; 1219 /* 1220 * The subnet mask usually includes at least the standard network part, 1221 * but may may be smaller in the case of supernetting. 1222 * If it is set, we believe it. 1223 */ 1224 if (ia->ia_subnetmask == 0) { 1225 ia->ia_subnetmask = ia->ia_netmask; 1226 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 1227 } else 1228 ia->ia_netmask &= ia->ia_subnetmask; 1229 1230 ia->ia_net = i & ia->ia_netmask; 1231 ia->ia_subnet = i & ia->ia_subnetmask; 1232 in_socktrim(&ia->ia_sockmask); 1233 1234 /* re-calculate the "in_maxmtu" value */ 1235 in_setmaxmtu(); 1236 1237 ia->ia_ifa.ifa_metric = ifp->if_metric; 1238 if (ifp->if_flags & IFF_BROADCAST) { 1239 if (ia->ia_subnetmask == IN_RFC3021_MASK) { 1240 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 1241 ia->ia_netbroadcast.s_addr = INADDR_BROADCAST; 1242 } else { 1243 ia->ia_broadaddr.sin_addr.s_addr = 1244 ia->ia_subnet | ~ia->ia_subnetmask; 1245 ia->ia_netbroadcast.s_addr = 1246 ia->ia_net | ~ia->ia_netmask; 1247 } 1248 } else if (ifp->if_flags & IFF_LOOPBACK) { 1249 ia->ia_dstaddr = ia->ia_addr; 1250 flags |= RTF_HOST; 1251 } else if (ifp->if_flags & IFF_POINTOPOINT) { 1252 if (ia->ia_dstaddr.sin_family != AF_INET) 1253 return (0); 1254 flags |= RTF_HOST; 1255 } 1256 1257 /* Add the local route to the address */ 1258 in_ifaddlocal(&ia->ia_ifa); 1259 1260 /* Add the prefix route for the address */ 1261 error = in_addprefix(ia, flags); 1262 1263 /* 1264 * If the interface supports multicast, join the "all hosts" 1265 * multicast group on that interface. 1266 */ 1267 mutex_enter(&in_ifaddr_lock); 1268 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 1269 struct in_addr addr; 1270 1271 addr.s_addr = INADDR_ALLHOSTS_GROUP; 1272 ia->ia_allhosts = in_addmulti(&addr, ifp); 1273 } 1274 mutex_exit(&in_ifaddr_lock); 1275 1276 if (hostIsNew && 1277 ia->ia4_flags & IN_IFF_TENTATIVE && 1278 if_do_dad(ifp)) 1279 ia->ia_dad_start((struct ifaddr *)ia); 1280 1281 return error; 1282 } 1283 1284 #define rtinitflags(x) \ 1285 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 1286 ? RTF_HOST : 0) 1287 1288 /* 1289 * add a route to prefix ("connected route" in cisco terminology). 1290 * does nothing if there's some interface address with the same prefix already. 1291 */ 1292 static int 1293 in_addprefix(struct in_ifaddr *target, int flags) 1294 { 1295 struct in_ifaddr *ia; 1296 struct in_addr prefix, mask, p; 1297 int error; 1298 int s; 1299 1300 if ((flags & RTF_HOST) != 0) 1301 prefix = target->ia_dstaddr.sin_addr; 1302 else { 1303 prefix = target->ia_addr.sin_addr; 1304 mask = target->ia_sockmask.sin_addr; 1305 prefix.s_addr &= mask.s_addr; 1306 } 1307 1308 s = pserialize_read_enter(); 1309 IN_ADDRLIST_READER_FOREACH(ia) { 1310 if (rtinitflags(ia)) 1311 p = ia->ia_dstaddr.sin_addr; 1312 else { 1313 p = ia->ia_addr.sin_addr; 1314 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1315 } 1316 1317 if (prefix.s_addr != p.s_addr) 1318 continue; 1319 1320 /* 1321 * if we got a matching prefix route inserted by other 1322 * interface address, we don't need to bother 1323 * 1324 * XXX RADIX_MPATH implications here? -dyoung 1325 */ 1326 if (ia->ia_flags & IFA_ROUTE) { 1327 pserialize_read_exit(s); 1328 return 0; 1329 } 1330 } 1331 pserialize_read_exit(s); 1332 1333 /* 1334 * noone seem to have prefix route. insert it. 1335 */ 1336 error = rtinit(&target->ia_ifa, RTM_ADD, flags); 1337 if (error == 0) 1338 target->ia_flags |= IFA_ROUTE; 1339 else if (error == EEXIST) { 1340 /* 1341 * the fact the route already exists is not an error. 1342 */ 1343 error = 0; 1344 } 1345 return error; 1346 } 1347 1348 /* 1349 * remove a route to prefix ("connected route" in cisco terminology). 1350 * re-installs the route by using another interface address, if there's one 1351 * with the same prefix (otherwise we lose the route mistakenly). 1352 */ 1353 static int 1354 in_scrubprefix(struct in_ifaddr *target) 1355 { 1356 struct in_ifaddr *ia; 1357 struct in_addr prefix, mask, p; 1358 int error; 1359 int s; 1360 1361 /* If we don't have IFA_ROUTE we have nothing to do */ 1362 if ((target->ia_flags & IFA_ROUTE) == 0) 1363 return 0; 1364 1365 if (rtinitflags(target)) 1366 prefix = target->ia_dstaddr.sin_addr; 1367 else { 1368 prefix = target->ia_addr.sin_addr; 1369 mask = target->ia_sockmask.sin_addr; 1370 prefix.s_addr &= mask.s_addr; 1371 } 1372 1373 s = pserialize_read_enter(); 1374 IN_ADDRLIST_READER_FOREACH(ia) { 1375 if (rtinitflags(ia)) 1376 p = ia->ia_dstaddr.sin_addr; 1377 else { 1378 p = ia->ia_addr.sin_addr; 1379 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1380 } 1381 1382 if (prefix.s_addr != p.s_addr) 1383 continue; 1384 1385 /* 1386 * if we got a matching prefix route, move IFA_ROUTE to him 1387 */ 1388 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1389 struct psref psref; 1390 int bound = curlwp_bind(); 1391 1392 ia4_acquire(ia, &psref); 1393 pserialize_read_exit(s); 1394 1395 rtinit(&target->ia_ifa, RTM_DELETE, 1396 rtinitflags(target)); 1397 target->ia_flags &= ~IFA_ROUTE; 1398 1399 error = rtinit(&ia->ia_ifa, RTM_ADD, 1400 rtinitflags(ia) | RTF_UP); 1401 if (error == 0) 1402 ia->ia_flags |= IFA_ROUTE; 1403 1404 ia4_release(ia, &psref); 1405 curlwp_bindx(bound); 1406 1407 return error; 1408 } 1409 } 1410 pserialize_read_exit(s); 1411 1412 /* 1413 * noone seem to have prefix route. remove it. 1414 */ 1415 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target)); 1416 target->ia_flags &= ~IFA_ROUTE; 1417 return 0; 1418 } 1419 1420 #undef rtinitflags 1421 1422 /* 1423 * Return 1 if the address might be a local broadcast address. 1424 */ 1425 int 1426 in_broadcast(struct in_addr in, struct ifnet *ifp) 1427 { 1428 struct ifaddr *ifa; 1429 int s; 1430 1431 KASSERT(ifp != NULL); 1432 1433 if (in.s_addr == INADDR_BROADCAST || 1434 in_nullhost(in)) 1435 return 1; 1436 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1437 return 0; 1438 /* 1439 * Look through the list of addresses for a match 1440 * with a broadcast address. 1441 */ 1442 #define ia (ifatoia(ifa)) 1443 s = pserialize_read_enter(); 1444 IFADDR_READER_FOREACH(ifa, ifp) { 1445 if (ifa->ifa_addr->sa_family == AF_INET && 1446 !in_hosteq(in, ia->ia_addr.sin_addr) && 1447 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 1448 in_hosteq(in, ia->ia_netbroadcast) || 1449 (hostzeroisbroadcast && 1450 /* 1451 * Check for old-style (host 0) broadcast, but 1452 * taking into account that RFC 3021 obsoletes it. 1453 */ 1454 ia->ia_subnetmask != IN_RFC3021_MASK && 1455 (in.s_addr == ia->ia_subnet || 1456 in.s_addr == ia->ia_net)))) { 1457 pserialize_read_exit(s); 1458 return 1; 1459 } 1460 } 1461 pserialize_read_exit(s); 1462 return (0); 1463 #undef ia 1464 } 1465 1466 /* 1467 * perform DAD when interface becomes IFF_UP. 1468 */ 1469 void 1470 in_if_link_up(struct ifnet *ifp) 1471 { 1472 struct ifaddr *ifa; 1473 struct in_ifaddr *ia; 1474 int s, bound; 1475 1476 /* Ensure it's sane to run DAD */ 1477 if (ifp->if_link_state == LINK_STATE_DOWN) 1478 return; 1479 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 1480 return; 1481 1482 bound = curlwp_bind(); 1483 s = pserialize_read_enter(); 1484 IFADDR_READER_FOREACH(ifa, ifp) { 1485 struct psref psref; 1486 1487 if (ifa->ifa_addr->sa_family != AF_INET) 1488 continue; 1489 ifa_acquire(ifa, &psref); 1490 pserialize_read_exit(s); 1491 1492 ia = (struct in_ifaddr *)ifa; 1493 1494 /* If detached then mark as tentative */ 1495 if (ia->ia4_flags & IN_IFF_DETACHED) { 1496 ia->ia4_flags &= ~IN_IFF_DETACHED; 1497 if (ip_dad_enabled() && if_do_dad(ifp) && 1498 ia->ia_dad_start != NULL) 1499 ia->ia4_flags |= IN_IFF_TENTATIVE; 1500 else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0) 1501 rt_addrmsg(RTM_NEWADDR, ifa); 1502 } 1503 1504 if (ia->ia4_flags & IN_IFF_TENTATIVE) { 1505 /* Clear the duplicated flag as we're starting DAD. */ 1506 ia->ia4_flags &= ~IN_IFF_DUPLICATED; 1507 ia->ia_dad_start(ifa); 1508 } 1509 1510 s = pserialize_read_enter(); 1511 ifa_release(ifa, &psref); 1512 } 1513 pserialize_read_exit(s); 1514 curlwp_bindx(bound); 1515 } 1516 1517 void 1518 in_if_up(struct ifnet *ifp) 1519 { 1520 1521 /* interface may not support link state, so bring it up also */ 1522 in_if_link_up(ifp); 1523 } 1524 1525 /* 1526 * Mark all addresses as detached. 1527 */ 1528 void 1529 in_if_link_down(struct ifnet *ifp) 1530 { 1531 struct ifaddr *ifa; 1532 struct in_ifaddr *ia; 1533 int s, bound; 1534 1535 bound = curlwp_bind(); 1536 s = pserialize_read_enter(); 1537 IFADDR_READER_FOREACH(ifa, ifp) { 1538 struct psref psref; 1539 1540 if (ifa->ifa_addr->sa_family != AF_INET) 1541 continue; 1542 ifa_acquire(ifa, &psref); 1543 pserialize_read_exit(s); 1544 1545 ia = (struct in_ifaddr *)ifa; 1546 1547 /* Stop DAD processing */ 1548 if (ia->ia_dad_stop != NULL) 1549 ia->ia_dad_stop(ifa); 1550 1551 /* 1552 * Mark the address as detached. 1553 */ 1554 if (!(ia->ia4_flags & IN_IFF_DETACHED)) { 1555 ia->ia4_flags |= IN_IFF_DETACHED; 1556 ia->ia4_flags &= 1557 ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED); 1558 rt_addrmsg(RTM_NEWADDR, ifa); 1559 } 1560 1561 s = pserialize_read_enter(); 1562 ifa_release(ifa, &psref); 1563 } 1564 pserialize_read_exit(s); 1565 curlwp_bindx(bound); 1566 } 1567 1568 void 1569 in_if_down(struct ifnet *ifp) 1570 { 1571 1572 in_if_link_down(ifp); 1573 #if NARP > 0 1574 lltable_purge_entries(LLTABLE(ifp)); 1575 #endif 1576 } 1577 1578 void 1579 in_if_link_state_change(struct ifnet *ifp, int link_state) 1580 { 1581 1582 /* 1583 * Treat LINK_STATE_UNKNOWN as UP. 1584 * LINK_STATE_UNKNOWN transitions to LINK_STATE_DOWN when 1585 * if_link_state_change() transitions to LINK_STATE_UP. 1586 */ 1587 if (link_state == LINK_STATE_DOWN) 1588 in_if_link_down(ifp); 1589 else 1590 in_if_link_up(ifp); 1591 } 1592 1593 /* 1594 * in_lookup_multi: look up the in_multi record for a given IP 1595 * multicast address on a given interface. If no matching record is 1596 * found, return NULL. 1597 */ 1598 struct in_multi * 1599 in_lookup_multi(struct in_addr addr, ifnet_t *ifp) 1600 { 1601 struct in_multi *inm; 1602 1603 KASSERT(rw_lock_held(&in_multilock)); 1604 1605 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) { 1606 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp) 1607 break; 1608 } 1609 return inm; 1610 } 1611 1612 /* 1613 * in_multi_group: check whether the address belongs to an IP multicast 1614 * group we are joined on this interface. Returns true or false. 1615 */ 1616 bool 1617 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags) 1618 { 1619 bool ingroup; 1620 1621 if (__predict_true(flags & IP_IGMP_MCAST) == 0) { 1622 rw_enter(&in_multilock, RW_READER); 1623 ingroup = in_lookup_multi(addr, ifp) != NULL; 1624 rw_exit(&in_multilock); 1625 } else { 1626 /* XXX Recursive call from ip_output(). */ 1627 KASSERT(rw_lock_held(&in_multilock)); 1628 ingroup = in_lookup_multi(addr, ifp) != NULL; 1629 } 1630 return ingroup; 1631 } 1632 1633 /* 1634 * Add an address to the list of IP multicast addresses for a given interface. 1635 */ 1636 struct in_multi * 1637 in_addmulti(struct in_addr *ap, ifnet_t *ifp) 1638 { 1639 struct sockaddr_in sin; 1640 struct in_multi *inm; 1641 1642 /* 1643 * See if address already in list. 1644 */ 1645 rw_enter(&in_multilock, RW_WRITER); 1646 inm = in_lookup_multi(*ap, ifp); 1647 if (inm != NULL) { 1648 /* 1649 * Found it; just increment the reference count. 1650 */ 1651 inm->inm_refcount++; 1652 rw_exit(&in_multilock); 1653 return inm; 1654 } 1655 1656 /* 1657 * New address; allocate a new multicast record. 1658 */ 1659 inm = pool_get(&inmulti_pool, PR_NOWAIT); 1660 if (inm == NULL) { 1661 rw_exit(&in_multilock); 1662 return NULL; 1663 } 1664 inm->inm_addr = *ap; 1665 inm->inm_ifp = ifp; 1666 inm->inm_refcount = 1; 1667 1668 /* 1669 * Ask the network driver to update its multicast reception 1670 * filter appropriately for the new address. 1671 */ 1672 sockaddr_in_init(&sin, ap, 0); 1673 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) { 1674 rw_exit(&in_multilock); 1675 pool_put(&inmulti_pool, inm); 1676 return NULL; 1677 } 1678 1679 /* 1680 * Let IGMP know that we have joined a new IP multicast group. 1681 */ 1682 if (igmp_joingroup(inm) != 0) { 1683 rw_exit(&in_multilock); 1684 pool_put(&inmulti_pool, inm); 1685 return NULL; 1686 } 1687 LIST_INSERT_HEAD( 1688 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp), 1689 inm, inm_list); 1690 in_multientries++; 1691 rw_exit(&in_multilock); 1692 1693 return inm; 1694 } 1695 1696 /* 1697 * Delete a multicast address record. 1698 */ 1699 void 1700 in_delmulti(struct in_multi *inm) 1701 { 1702 struct sockaddr_in sin; 1703 1704 rw_enter(&in_multilock, RW_WRITER); 1705 if (--inm->inm_refcount > 0) { 1706 rw_exit(&in_multilock); 1707 return; 1708 } 1709 1710 /* 1711 * No remaining claims to this record; let IGMP know that 1712 * we are leaving the multicast group. 1713 */ 1714 igmp_leavegroup(inm); 1715 1716 /* 1717 * Notify the network driver to update its multicast reception 1718 * filter. 1719 */ 1720 sockaddr_in_init(&sin, &inm->inm_addr, 0); 1721 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin)); 1722 1723 /* 1724 * Unlink from list. 1725 */ 1726 LIST_REMOVE(inm, inm_list); 1727 in_multientries--; 1728 rw_exit(&in_multilock); 1729 1730 pool_put(&inmulti_pool, inm); 1731 } 1732 1733 /* 1734 * in_next_multi: step through all of the in_multi records, one at a time. 1735 * The current position is remembered in "step", which the caller must 1736 * provide. in_first_multi(), below, must be called to initialize "step" 1737 * and get the first record. Both macros return a NULL "inm" when there 1738 * are no remaining records. 1739 */ 1740 struct in_multi * 1741 in_next_multi(struct in_multistep *step) 1742 { 1743 struct in_multi *inm; 1744 1745 KASSERT(rw_lock_held(&in_multilock)); 1746 1747 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) { 1748 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]); 1749 } 1750 if ((inm = step->i_inm) != NULL) { 1751 step->i_inm = LIST_NEXT(inm, inm_list); 1752 } 1753 return inm; 1754 } 1755 1756 struct in_multi * 1757 in_first_multi(struct in_multistep *step) 1758 { 1759 KASSERT(rw_lock_held(&in_multilock)); 1760 1761 step->i_n = 0; 1762 step->i_inm = LIST_FIRST(&in_multihashtbl[0]); 1763 return in_next_multi(step); 1764 } 1765 1766 void 1767 in_multi_lock(int op) 1768 { 1769 rw_enter(&in_multilock, op); 1770 } 1771 1772 void 1773 in_multi_unlock(void) 1774 { 1775 rw_exit(&in_multilock); 1776 } 1777 1778 int 1779 in_multi_lock_held(void) 1780 { 1781 return rw_lock_held(&in_multilock); 1782 } 1783 1784 struct in_ifaddr * 1785 in_selectsrc(struct sockaddr_in *sin, struct route *ro, 1786 int soopts, struct ip_moptions *mopts, int *errorp, struct psref *psref) 1787 { 1788 struct rtentry *rt = NULL; 1789 struct in_ifaddr *ia = NULL; 1790 1791 KASSERT(ISSET(curlwp->l_pflag, LP_BOUND)); 1792 /* 1793 * If route is known or can be allocated now, take the 1794 * source address from the interface. Otherwise, punt. 1795 */ 1796 if ((soopts & SO_DONTROUTE) != 0) 1797 rtcache_free(ro); 1798 else { 1799 union { 1800 struct sockaddr dst; 1801 struct sockaddr_in dst4; 1802 } u; 1803 1804 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0); 1805 rt = rtcache_lookup(ro, &u.dst); 1806 } 1807 /* 1808 * If we found a route, use the address 1809 * corresponding to the outgoing interface 1810 * unless it is the loopback (in case a route 1811 * to our address on another net goes to loopback). 1812 * 1813 * XXX Is this still true? Do we care? 1814 */ 1815 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 1816 int s; 1817 struct ifaddr *ifa; 1818 /* 1819 * Just in case. May not need to do this workaround. 1820 * Revisit when working on rtentry MP-ification. 1821 */ 1822 s = pserialize_read_enter(); 1823 IFADDR_READER_FOREACH(ifa, rt->rt_ifp) { 1824 if (ifa == rt->rt_ifa) 1825 break; 1826 } 1827 if (ifa != NULL) 1828 ifa_acquire(ifa, psref); 1829 pserialize_read_exit(s); 1830 1831 ia = ifatoia(ifa); 1832 } 1833 if (ia == NULL) { 1834 u_int16_t fport = sin->sin_port; 1835 struct ifaddr *ifa; 1836 int s; 1837 1838 sin->sin_port = 0; 1839 ifa = ifa_ifwithladdr_psref(sintosa(sin), psref); 1840 sin->sin_port = fport; 1841 if (ifa == NULL) { 1842 /* Find 1st non-loopback AF_INET address */ 1843 s = pserialize_read_enter(); 1844 IN_ADDRLIST_READER_FOREACH(ia) { 1845 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) 1846 break; 1847 } 1848 if (ia != NULL) 1849 ia4_acquire(ia, psref); 1850 pserialize_read_exit(s); 1851 } else { 1852 /* ia is already referenced by psref */ 1853 ia = ifatoia(ifa); 1854 } 1855 if (ia == NULL) { 1856 *errorp = EADDRNOTAVAIL; 1857 goto out; 1858 } 1859 } 1860 /* 1861 * If the destination address is multicast and an outgoing 1862 * interface has been set as a multicast option, use the 1863 * address of that interface as our source address. 1864 */ 1865 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 1866 struct ip_moptions *imo; 1867 1868 imo = mopts; 1869 if (imo->imo_multicast_if_index != 0) { 1870 struct ifnet *ifp; 1871 int s; 1872 1873 if (ia != NULL) 1874 ia4_release(ia, psref); 1875 s = pserialize_read_enter(); 1876 ifp = if_byindex(imo->imo_multicast_if_index); 1877 if (ifp != NULL) { 1878 /* XXX */ 1879 ia = in_get_ia_from_ifp_psref(ifp, psref); 1880 } else 1881 ia = NULL; 1882 if (ia == NULL || ia->ia4_flags & IN_IFF_NOTREADY) { 1883 pserialize_read_exit(s); 1884 if (ia != NULL) 1885 ia4_release(ia, psref); 1886 *errorp = EADDRNOTAVAIL; 1887 ia = NULL; 1888 goto out; 1889 } 1890 pserialize_read_exit(s); 1891 } 1892 } 1893 if (ia->ia_ifa.ifa_getifa != NULL) { 1894 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa, 1895 sintosa(sin))); 1896 if (ia == NULL) { 1897 *errorp = EADDRNOTAVAIL; 1898 goto out; 1899 } 1900 /* FIXME NOMPSAFE */ 1901 ia4_acquire(ia, psref); 1902 } 1903 #ifdef GETIFA_DEBUG 1904 else 1905 printf("%s: missing ifa_getifa\n", __func__); 1906 #endif 1907 out: 1908 rtcache_unref(rt, ro); 1909 return ia; 1910 } 1911 1912 int 1913 in_tunnel_validate(const struct ip *ip, struct in_addr src, struct in_addr dst) 1914 { 1915 struct in_ifaddr *ia4; 1916 int s; 1917 1918 /* check for address match */ 1919 if (src.s_addr != ip->ip_dst.s_addr || 1920 dst.s_addr != ip->ip_src.s_addr) 1921 return 0; 1922 1923 /* martian filters on outer source - NOT done in ip_input! */ 1924 if (IN_MULTICAST(ip->ip_src.s_addr)) 1925 return 0; 1926 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) { 1927 case 0: 1928 case 127: 1929 case 255: 1930 return 0; 1931 } 1932 /* reject packets with broadcast on source */ 1933 s = pserialize_read_enter(); 1934 IN_ADDRLIST_READER_FOREACH(ia4) { 1935 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 1936 continue; 1937 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) { 1938 pserialize_read_exit(s); 1939 return 0; 1940 } 1941 } 1942 pserialize_read_exit(s); 1943 1944 /* NOTE: packet may dropped by uRPF */ 1945 1946 /* return valid bytes length */ 1947 return sizeof(src) + sizeof(dst); 1948 } 1949 1950 #if NARP > 0 1951 1952 #define IN_LLTBL_DEFAULT_HSIZE 32 1953 #define IN_LLTBL_HASH(k, h) \ 1954 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 1955 1956 /* 1957 * Do actual deallocation of @lle. 1958 * Called by LLE_FREE_LOCKED when number of references 1959 * drops to zero. 1960 */ 1961 static void 1962 in_lltable_destroy_lle(struct llentry *lle) 1963 { 1964 1965 KASSERTMSG(lle->la_numheld == 0, "la_numheld=%d", lle->la_numheld); 1966 1967 LLE_WUNLOCK(lle); 1968 LLE_LOCK_DESTROY(lle); 1969 llentry_pool_put(lle); 1970 } 1971 1972 static struct llentry * 1973 in_lltable_new(struct in_addr addr4, u_int flags) 1974 { 1975 struct llentry *lle; 1976 1977 lle = llentry_pool_get(PR_NOWAIT); 1978 if (lle == NULL) /* NB: caller generates msg */ 1979 return NULL; 1980 1981 lle->r_l3addr.addr4 = addr4; 1982 lle->lle_refcnt = 1; 1983 lle->lle_free = in_lltable_destroy_lle; 1984 LLE_LOCK_INIT(lle); 1985 callout_init(&lle->la_timer, CALLOUT_MPSAFE); 1986 1987 return lle; 1988 } 1989 1990 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 1991 (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 1992 1993 static int 1994 in_lltable_match_prefix(const struct sockaddr *prefix, 1995 const struct sockaddr *mask, u_int flags, struct llentry *lle) 1996 { 1997 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1998 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1999 struct in_addr lle_addr; 2000 2001 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr); 2002 2003 /* 2004 * (flags & LLE_STATIC) means deleting all entries 2005 * including static ARP entries. 2006 */ 2007 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, pfx, msk) && 2008 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) 2009 return (1); 2010 2011 return (0); 2012 } 2013 2014 static void 2015 in_lltable_free_entry(struct lltable *llt, struct llentry *lle) 2016 { 2017 size_t pkts_dropped; 2018 2019 LLE_WLOCK_ASSERT(lle); 2020 KASSERT(llt != NULL); 2021 2022 pkts_dropped = llentry_free(lle); 2023 arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped); 2024 } 2025 2026 static int 2027 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr, 2028 const struct rtentry *rt) 2029 { 2030 int error = EINVAL; 2031 2032 if (rt == NULL) 2033 return error; 2034 2035 /* 2036 * If the gateway for an existing host route matches the target L3 2037 * address, which is a special route inserted by some implementation 2038 * such as MANET, and the interface is of the correct type, then 2039 * allow for ARP to proceed. 2040 */ 2041 if (rt->rt_flags & RTF_GATEWAY) { 2042 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 2043 rt->rt_ifp->if_type != IFT_ETHER || 2044 (rt->rt_ifp->if_flags & IFF_NOARP) != 0 || 2045 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 2046 sizeof(in_addr_t)) != 0) { 2047 goto error; 2048 } 2049 } 2050 2051 /* 2052 * Make sure that at least the destination address is covered 2053 * by the route. This is for handling the case where 2 or more 2054 * interfaces have the same prefix. An incoming packet arrives 2055 * on one interface and the corresponding outgoing packet leaves 2056 * another interface. 2057 */ 2058 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 2059 const char *sa, *mask, *addr, *lim; 2060 int len; 2061 2062 mask = (const char *)rt_mask(rt); 2063 /* 2064 * Just being extra cautious to avoid some custom 2065 * code getting into trouble. 2066 */ 2067 if (mask == NULL) 2068 goto error; 2069 2070 sa = (const char *)rt_getkey(rt); 2071 addr = (const char *)l3addr; 2072 len = ((const struct sockaddr_in *)l3addr)->sin_len; 2073 lim = addr + len; 2074 2075 for ( ; addr < lim; sa++, mask++, addr++) { 2076 if ((*sa ^ *addr) & *mask) { 2077 #ifdef DIAGNOSTIC 2078 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 2079 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 2080 #endif 2081 goto error; 2082 } 2083 } 2084 } 2085 2086 error = 0; 2087 error: 2088 return error; 2089 } 2090 2091 static inline uint32_t 2092 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize) 2093 { 2094 2095 return (IN_LLTBL_HASH(dst.s_addr, hsize)); 2096 } 2097 2098 static uint32_t 2099 in_lltable_hash(const struct llentry *lle, uint32_t hsize) 2100 { 2101 2102 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize)); 2103 } 2104 2105 static void 2106 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 2107 { 2108 struct sockaddr_in *sin; 2109 2110 sin = (struct sockaddr_in *)sa; 2111 memset(sin, 0, sizeof(*sin)); 2112 sin->sin_family = AF_INET; 2113 sin->sin_len = sizeof(*sin); 2114 sin->sin_addr = lle->r_l3addr.addr4; 2115 } 2116 2117 static inline struct llentry * 2118 in_lltable_find_dst(struct lltable *llt, struct in_addr dst) 2119 { 2120 struct llentry *lle; 2121 struct llentries *lleh; 2122 u_int hashidx; 2123 2124 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize); 2125 lleh = &llt->lle_head[hashidx]; 2126 LIST_FOREACH(lle, lleh, lle_next) { 2127 if (lle->la_flags & LLE_DELETED) 2128 continue; 2129 if (lle->r_l3addr.addr4.s_addr == dst.s_addr) 2130 break; 2131 } 2132 2133 return (lle); 2134 } 2135 2136 static int 2137 in_lltable_delete(struct lltable *llt, u_int flags, 2138 const struct sockaddr *l3addr) 2139 { 2140 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 2141 struct ifnet *ifp __diagused = llt->llt_ifp; 2142 struct llentry *lle; 2143 2144 IF_AFDATA_WLOCK_ASSERT(ifp); 2145 KASSERTMSG(l3addr->sa_family == AF_INET, 2146 "sin_family %d", l3addr->sa_family); 2147 2148 lle = in_lltable_find_dst(llt, sin->sin_addr); 2149 if (lle == NULL) { 2150 #ifdef LLTABLE_DEBUG 2151 char buf[64]; 2152 sockaddr_format(l3addr, buf, sizeof(buf)); 2153 log(LOG_INFO, "%s: cache for %s is not found\n", 2154 __func__, buf); 2155 #endif 2156 return (ENOENT); 2157 } 2158 2159 LLE_WLOCK(lle); 2160 #ifdef LLTABLE_DEBUG 2161 { 2162 char buf[64]; 2163 sockaddr_format(l3addr, buf, sizeof(buf)); 2164 log(LOG_INFO, "%s: cache for %s (%p) is deleted\n", 2165 __func__, buf, lle); 2166 } 2167 #endif 2168 llentry_free(lle); 2169 2170 return (0); 2171 } 2172 2173 static struct llentry * 2174 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr, 2175 const struct rtentry *rt) 2176 { 2177 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 2178 struct ifnet *ifp = llt->llt_ifp; 2179 struct llentry *lle; 2180 2181 IF_AFDATA_WLOCK_ASSERT(ifp); 2182 KASSERTMSG(l3addr->sa_family == AF_INET, 2183 "sin_family %d", l3addr->sa_family); 2184 2185 lle = in_lltable_find_dst(llt, sin->sin_addr); 2186 2187 if (lle != NULL) { 2188 LLE_WLOCK(lle); 2189 return (lle); 2190 } 2191 2192 /* no existing record, we need to create new one */ 2193 2194 /* 2195 * A route that covers the given address must have 2196 * been installed 1st because we are doing a resolution, 2197 * verify this. 2198 */ 2199 if (!(flags & LLE_IFADDR) && 2200 in_lltable_rtcheck(ifp, flags, l3addr, rt) != 0) 2201 return (NULL); 2202 2203 lle = in_lltable_new(sin->sin_addr, flags); 2204 if (lle == NULL) { 2205 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2206 return (NULL); 2207 } 2208 lle->la_flags = flags; 2209 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 2210 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen); 2211 lle->la_flags |= (LLE_VALID | LLE_STATIC); 2212 } 2213 2214 lltable_link_entry(llt, lle); 2215 LLE_WLOCK(lle); 2216 2217 return (lle); 2218 } 2219 2220 /* 2221 * Return NULL if not found or marked for deletion. 2222 * If found return lle read locked. 2223 */ 2224 static struct llentry * 2225 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 2226 { 2227 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 2228 struct llentry *lle; 2229 2230 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); 2231 KASSERTMSG(l3addr->sa_family == AF_INET, 2232 "sin_family %d", l3addr->sa_family); 2233 2234 lle = in_lltable_find_dst(llt, sin->sin_addr); 2235 2236 if (lle == NULL) 2237 return NULL; 2238 2239 if (flags & LLE_EXCLUSIVE) 2240 LLE_WLOCK(lle); 2241 else 2242 LLE_RLOCK(lle); 2243 2244 return lle; 2245 } 2246 2247 static int 2248 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle, 2249 struct rt_walkarg *w) 2250 { 2251 struct sockaddr_in sin; 2252 2253 LLTABLE_LOCK_ASSERT(); 2254 2255 /* skip deleted entries */ 2256 if (lle->la_flags & LLE_DELETED) 2257 return 0; 2258 2259 sockaddr_in_init(&sin, &lle->r_l3addr.addr4, 0); 2260 2261 return lltable_dump_entry(llt, lle, w, sintosa(&sin)); 2262 } 2263 2264 #endif /* NARP > 0 */ 2265 2266 static int 2267 in_multicast_sysctl(SYSCTLFN_ARGS) 2268 { 2269 struct ifnet *ifp; 2270 struct ifaddr *ifa; 2271 struct in_ifaddr *ifa4; 2272 struct in_multi *inm; 2273 uint32_t tmp; 2274 int error; 2275 size_t written; 2276 struct psref psref; 2277 int bound; 2278 2279 if (namelen != 1) 2280 return EINVAL; 2281 2282 bound = curlwp_bind(); 2283 ifp = if_get_byindex(name[0], &psref); 2284 if (ifp == NULL) { 2285 curlwp_bindx(bound); 2286 return ENODEV; 2287 } 2288 2289 if (oldp == NULL) { 2290 *oldlenp = 0; 2291 IFADDR_FOREACH(ifa, ifp) { 2292 if (ifa->ifa_addr->sa_family != AF_INET) 2293 continue; 2294 ifa4 = (void *)ifa; 2295 LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) { 2296 *oldlenp += 2 * sizeof(struct in_addr) + 2297 sizeof(uint32_t); 2298 } 2299 } 2300 if_put(ifp, &psref); 2301 curlwp_bindx(bound); 2302 return 0; 2303 } 2304 2305 error = 0; 2306 written = 0; 2307 IFADDR_FOREACH(ifa, ifp) { 2308 if (ifa->ifa_addr->sa_family != AF_INET) 2309 continue; 2310 ifa4 = (void *)ifa; 2311 LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) { 2312 if (written + 2 * sizeof(struct in_addr) + 2313 sizeof(uint32_t) > *oldlenp) 2314 goto done; 2315 error = sysctl_copyout(l, &ifa4->ia_addr.sin_addr, 2316 oldp, sizeof(struct in_addr)); 2317 if (error) 2318 goto done; 2319 oldp = (char *)oldp + sizeof(struct in_addr); 2320 written += sizeof(struct in_addr); 2321 error = sysctl_copyout(l, &inm->inm_addr, 2322 oldp, sizeof(struct in_addr)); 2323 if (error) 2324 goto done; 2325 oldp = (char *)oldp + sizeof(struct in_addr); 2326 written += sizeof(struct in_addr); 2327 tmp = inm->inm_refcount; 2328 error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp)); 2329 if (error) 2330 goto done; 2331 oldp = (char *)oldp + sizeof(tmp); 2332 written += sizeof(tmp); 2333 } 2334 } 2335 done: 2336 if_put(ifp, &psref); 2337 curlwp_bindx(bound); 2338 *oldlenp = written; 2339 return error; 2340 } 2341 2342 static void 2343 in_sysctl_init(struct sysctllog **clog) 2344 { 2345 sysctl_createv(clog, 0, NULL, NULL, 2346 CTLFLAG_PERMANENT, 2347 CTLTYPE_NODE, "inet", 2348 SYSCTL_DESCR("PF_INET related settings"), 2349 NULL, 0, NULL, 0, 2350 CTL_NET, PF_INET, CTL_EOL); 2351 sysctl_createv(clog, 0, NULL, NULL, 2352 CTLFLAG_PERMANENT, 2353 CTLTYPE_NODE, "multicast", 2354 SYSCTL_DESCR("Multicast information"), 2355 in_multicast_sysctl, 0, NULL, 0, 2356 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL); 2357 sysctl_createv(clog, 0, NULL, NULL, 2358 CTLFLAG_PERMANENT, 2359 CTLTYPE_NODE, "ip", 2360 SYSCTL_DESCR("IPv4 related settings"), 2361 NULL, 0, NULL, 0, 2362 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL); 2363 2364 sysctl_createv(clog, 0, NULL, NULL, 2365 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2366 CTLTYPE_INT, "subnetsarelocal", 2367 SYSCTL_DESCR("Whether logical subnets are considered " 2368 "local"), 2369 NULL, 0, &subnetsarelocal, 0, 2370 CTL_NET, PF_INET, IPPROTO_IP, 2371 IPCTL_SUBNETSARELOCAL, CTL_EOL); 2372 sysctl_createv(clog, 0, NULL, NULL, 2373 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2374 CTLTYPE_INT, "hostzerobroadcast", 2375 SYSCTL_DESCR("All zeroes address is broadcast address"), 2376 NULL, 0, &hostzeroisbroadcast, 0, 2377 CTL_NET, PF_INET, IPPROTO_IP, 2378 IPCTL_HOSTZEROBROADCAST, CTL_EOL); 2379 } 2380 2381 #if NARP > 0 2382 2383 static struct lltable * 2384 in_lltattach(struct ifnet *ifp) 2385 { 2386 struct lltable *llt; 2387 2388 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE); 2389 llt->llt_af = AF_INET; 2390 llt->llt_ifp = ifp; 2391 2392 llt->llt_lookup = in_lltable_lookup; 2393 llt->llt_create = in_lltable_create; 2394 llt->llt_delete = in_lltable_delete; 2395 llt->llt_dump_entry = in_lltable_dump_entry; 2396 llt->llt_hash = in_lltable_hash; 2397 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry; 2398 llt->llt_free_entry = in_lltable_free_entry; 2399 llt->llt_match_prefix = in_lltable_match_prefix; 2400 lltable_link(llt); 2401 2402 return (llt); 2403 } 2404 2405 #endif /* NARP > 0 */ 2406 2407 void * 2408 in_domifattach(struct ifnet *ifp) 2409 { 2410 struct in_ifinfo *ii; 2411 2412 ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP); 2413 2414 #if NARP > 0 2415 ii->ii_llt = in_lltattach(ifp); 2416 #endif 2417 2418 #ifdef IPSELSRC 2419 ii->ii_selsrc = in_selsrc_domifattach(ifp); 2420 KASSERT(ii->ii_selsrc != NULL); 2421 #endif 2422 2423 return ii; 2424 } 2425 2426 void 2427 in_domifdetach(struct ifnet *ifp, void *aux) 2428 { 2429 struct in_ifinfo *ii = aux; 2430 2431 #ifdef IPSELSRC 2432 in_selsrc_domifdetach(ifp, ii->ii_selsrc); 2433 #endif 2434 #if NARP > 0 2435 lltable_free(ii->ii_llt); 2436 #endif 2437 kmem_free(ii, sizeof(struct in_ifinfo)); 2438 } 2439