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