1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (C) 2001 WIDE Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 #include "opt_inet.h" 35 36 #define IN_HISTORICAL_NETS /* include class masks */ 37 38 #include <sys/param.h> 39 #include <sys/eventhandler.h> 40 #include <sys/systm.h> 41 #include <sys/sockio.h> 42 #include <sys/malloc.h> 43 #include <sys/priv.h> 44 #include <sys/socket.h> 45 #include <sys/jail.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/proc.h> 49 #include <sys/sysctl.h> 50 #include <sys/syslog.h> 51 #include <sys/sx.h> 52 53 #include <net/if.h> 54 #include <net/if_var.h> 55 #include <net/if_arp.h> 56 #include <net/if_dl.h> 57 #include <net/if_llatbl.h> 58 #include <net/if_private.h> 59 #include <net/if_types.h> 60 #include <net/if_bridgevar.h> 61 #include <net/route.h> 62 #include <net/route/nhop.h> 63 #include <net/route/route_ctl.h> 64 #include <net/vnet.h> 65 66 #include <netinet/if_ether.h> 67 #include <netinet/in.h> 68 #include <netinet/in_fib.h> 69 #include <netinet/in_var.h> 70 #include <netinet/in_pcb.h> 71 #include <netinet/ip_var.h> 72 #include <netinet/ip_carp.h> 73 #include <netinet/igmp_var.h> 74 #include <netinet/udp.h> 75 #include <netinet/udp_var.h> 76 77 #ifdef MAC 78 #include <security/mac/mac_framework.h> 79 #endif 80 81 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *); 82 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *); 83 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *); 84 85 static void in_socktrim(struct sockaddr_in *); 86 static void in_purgemaddrs(struct ifnet *); 87 88 static bool ia_need_loopback_route(const struct in_ifaddr *); 89 90 VNET_DEFINE_STATIC(int, nosameprefix); 91 #define V_nosameprefix VNET(nosameprefix) 92 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW, 93 &VNET_NAME(nosameprefix), 0, 94 "Refuse to create same prefixes on different interfaces"); 95 96 VNET_DEFINE_STATIC(bool, broadcast_lowest); 97 #define V_broadcast_lowest VNET(broadcast_lowest) 98 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW, 99 &VNET_NAME(broadcast_lowest), 0, 100 "Treat lowest address on a subnet (host 0) as broadcast"); 101 102 VNET_DEFINE(bool, ip_allow_net240) = false; 103 #define V_ip_allow_net240 VNET(ip_allow_net240) 104 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240, 105 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0, 106 "Allow forwarding of and ICMP response to Experimental addresses, aka Class E (240/4)"); 107 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */ 108 109 VNET_DEFINE(bool, ip_allow_net0) = false; 110 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0, 111 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0, 112 "Allow forwarding of and ICMP response to addresses in network 0/8"); 113 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */ 114 115 VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT; 116 #define V_in_loopback_mask VNET(in_loopback_mask) 117 static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS); 118 SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen, 119 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 120 NULL, 0, sysctl_loopback_prefixlen, "I", 121 "Prefix length of address space reserved for loopback"); 122 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */ 123 124 VNET_DECLARE(struct inpcbinfo, ripcbinfo); 125 #define V_ripcbinfo VNET(ripcbinfo) 126 127 static struct sx in_control_sx; 128 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control"); 129 130 /* 131 * Return true if an internet address is for a ``local'' host 132 * (one to which we have a connection). 133 */ 134 bool 135 in_localaddr(struct in_addr in) 136 { 137 u_long i = ntohl(in.s_addr); 138 struct in_ifaddr *ia; 139 140 NET_EPOCH_ASSERT(); 141 142 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 143 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 144 return (true); 145 } 146 147 return (false); 148 } 149 150 /* 151 * Return true if an internet address is for the local host and configured 152 * on one of its interfaces. 153 */ 154 bool 155 in_localip(struct in_addr in) 156 { 157 struct in_ifaddr *ia; 158 159 NET_EPOCH_ASSERT(); 160 161 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) 162 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 163 return (true); 164 165 return (false); 166 } 167 168 /* 169 * Like in_localip(), but FIB-aware and carp(4)-aware. 170 */ 171 bool 172 in_localip_fib(struct in_addr in, uint16_t fib) 173 { 174 struct in_ifaddr *ia; 175 176 NET_EPOCH_ASSERT(); 177 178 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) 179 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr && 180 (ia->ia_ifa.ifa_carp == NULL || 181 carp_master_p(&ia->ia_ifa)) && 182 ia->ia_ifa.ifa_ifp->if_fib == fib) 183 return (true); 184 185 return (false); 186 } 187 188 /* 189 * Return true if an internet address is configured on an interface. 190 */ 191 bool 192 in_ifhasaddr(struct ifnet *ifp, struct in_addr in) 193 { 194 struct ifaddr *ifa; 195 struct in_ifaddr *ia; 196 197 NET_EPOCH_ASSERT(); 198 199 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 200 if (ifa->ifa_addr->sa_family != AF_INET) 201 continue; 202 ia = (struct in_ifaddr *)ifa; 203 if (ia->ia_addr.sin_addr.s_addr == in.s_addr) 204 return (true); 205 } 206 207 return (false); 208 } 209 210 /* 211 * Return a reference to the interface address which is different to 212 * the supplied one but with same IP address value. 213 */ 214 static struct in_ifaddr * 215 in_localip_more(struct in_ifaddr *original_ia) 216 { 217 struct epoch_tracker et; 218 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr; 219 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib; 220 struct in_ifaddr *ia; 221 222 NET_EPOCH_ENTER(et); 223 CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) { 224 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr; 225 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib; 226 if (!V_rt_add_addr_allfibs && (original_fib != fib)) 227 continue; 228 if ((original_ia != ia) && (original_addr == addr)) { 229 ifa_ref(&ia->ia_ifa); 230 NET_EPOCH_EXIT(et); 231 return (ia); 232 } 233 } 234 NET_EPOCH_EXIT(et); 235 236 return (NULL); 237 } 238 239 /* 240 * Tries to find first IPv4 address in the provided fib. 241 * Prefers non-loopback addresses and return loopback IFF 242 * @loopback_ok is set. 243 * 244 * Returns ifa or NULL. 245 */ 246 struct in_ifaddr * 247 in_findlocal(uint32_t fibnum, bool loopback_ok) 248 { 249 struct in_ifaddr *ia = NULL, *ia_lo = NULL; 250 251 NET_EPOCH_ASSERT(); 252 253 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 254 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib; 255 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib)) 256 continue; 257 258 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr))) 259 break; 260 if (loopback_ok) 261 ia_lo = ia; 262 } 263 264 if (ia == NULL) 265 ia = ia_lo; 266 267 return (ia); 268 } 269 270 /* 271 * Determine whether an IP address is in a reserved set of addresses 272 * that may not be forwarded, or whether datagrams to that destination 273 * may be forwarded. 274 */ 275 bool 276 in_canforward(struct in_addr in) 277 { 278 u_long i = ntohl(in.s_addr); 279 280 if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i) || 281 in_nullhost(in)) 282 return (false); 283 if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240) 284 return (false); 285 if (IN_ZERONET(i) && !V_ip_allow_net0) 286 return (false); 287 return (true); 288 } 289 290 /* 291 * Sysctl to manage prefix of reserved loopback network; translate 292 * to/from mask. The mask is always contiguous high-order 1 bits 293 * followed by all 0 bits. 294 */ 295 static int 296 sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS) 297 { 298 int error, preflen; 299 300 /* ffs is 1-based; compensate. */ 301 preflen = 33 - ffs(V_in_loopback_mask); 302 error = sysctl_handle_int(oidp, &preflen, 0, req); 303 if (error || !req->newptr) 304 return (error); 305 if (preflen < 8 || preflen > 31) 306 return (EINVAL); 307 V_in_loopback_mask = 0xffffffff << (32 - preflen); 308 return (0); 309 } 310 311 /* 312 * Trim a mask in a sockaddr 313 */ 314 static void 315 in_socktrim(struct sockaddr_in *ap) 316 { 317 char *cplim = (char *) &ap->sin_addr; 318 char *cp = (char *) (&ap->sin_addr + 1); 319 320 ap->sin_len = 0; 321 while (--cp >= cplim) 322 if (*cp) { 323 (ap)->sin_len = cp - (char *) (ap) + 1; 324 break; 325 } 326 } 327 328 /* 329 * Generic internet control operations (ioctl's). 330 */ 331 int 332 in_control_ioctl(u_long cmd, void *data, struct ifnet *ifp, 333 struct ucred *cred) 334 { 335 struct ifreq *ifr = (struct ifreq *)data; 336 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr; 337 struct epoch_tracker et; 338 struct ifaddr *ifa; 339 struct in_ifaddr *ia; 340 int error; 341 342 if (ifp == NULL) 343 return (EADDRNOTAVAIL); 344 345 /* 346 * Filter out 4 ioctls we implement directly. Forward the rest 347 * to specific functions and ifp->if_ioctl(). 348 */ 349 switch (cmd) { 350 case SIOCGIFADDR: 351 case SIOCGIFBRDADDR: 352 case SIOCGIFDSTADDR: 353 case SIOCGIFNETMASK: 354 break; 355 case SIOCGIFALIAS: 356 sx_xlock(&in_control_sx); 357 error = in_gifaddr_ioctl(cmd, data, ifp, cred); 358 sx_xunlock(&in_control_sx); 359 return (error); 360 case SIOCDIFADDR: 361 sx_xlock(&in_control_sx); 362 error = in_difaddr_ioctl(cmd, data, ifp, cred); 363 sx_xunlock(&in_control_sx); 364 return (error); 365 case OSIOCAIFADDR: /* 9.x compat */ 366 case SIOCAIFADDR: 367 sx_xlock(&in_control_sx); 368 error = in_aifaddr_ioctl(cmd, data, ifp, cred); 369 sx_xunlock(&in_control_sx); 370 return (error); 371 case SIOCSIFADDR: 372 case SIOCSIFBRDADDR: 373 case SIOCSIFDSTADDR: 374 case SIOCSIFNETMASK: 375 /* We no longer support that old commands. */ 376 return (EINVAL); 377 default: 378 if (ifp->if_ioctl == NULL) 379 return (EOPNOTSUPP); 380 return ((*ifp->if_ioctl)(ifp, cmd, data)); 381 } 382 383 if (addr->sin_addr.s_addr != INADDR_ANY && 384 prison_check_ip4(cred, &addr->sin_addr) != 0) 385 return (EADDRNOTAVAIL); 386 387 /* 388 * Find address for this interface, if it exists. If an 389 * address was specified, find that one instead of the 390 * first one on the interface, if possible. 391 */ 392 NET_EPOCH_ENTER(et); 393 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 394 if (ifa->ifa_addr->sa_family != AF_INET) 395 continue; 396 ia = (struct in_ifaddr *)ifa; 397 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr) 398 break; 399 } 400 if (ifa == NULL) 401 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 402 if (ifa->ifa_addr->sa_family == AF_INET) { 403 ia = (struct in_ifaddr *)ifa; 404 if (prison_check_ip4(cred, 405 &ia->ia_addr.sin_addr) == 0) 406 break; 407 } 408 409 if (ifa == NULL) { 410 NET_EPOCH_EXIT(et); 411 return (EADDRNOTAVAIL); 412 } 413 414 error = 0; 415 switch (cmd) { 416 case SIOCGIFADDR: 417 *addr = ia->ia_addr; 418 break; 419 420 case SIOCGIFBRDADDR: 421 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 422 error = EINVAL; 423 break; 424 } 425 *addr = ia->ia_broadaddr; 426 break; 427 428 case SIOCGIFDSTADDR: 429 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 430 error = EINVAL; 431 break; 432 } 433 *addr = ia->ia_dstaddr; 434 break; 435 436 case SIOCGIFNETMASK: 437 *addr = ia->ia_sockmask; 438 break; 439 } 440 441 NET_EPOCH_EXIT(et); 442 443 return (error); 444 } 445 446 int 447 in_mask2len(struct in_addr *mask) 448 { 449 int x, y; 450 u_char *p; 451 452 p = (u_char *)mask; 453 for (x = 0; x < sizeof(*mask); x++) { 454 if (p[x] != 0xff) 455 break; 456 } 457 y = 0; 458 if (x < sizeof(*mask)) { 459 for (y = 0; y < 8; y++) { 460 if ((p[x] & (0x80 >> y)) == 0) 461 break; 462 } 463 } 464 return (x * 8 + y); 465 } 466 467 int 468 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp, 469 struct thread *td) 470 { 471 return (in_control_ioctl(cmd, data, ifp, td ? td->td_ucred : NULL)); 472 } 473 474 static int 475 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred) 476 { 477 const struct in_aliasreq *ifra = (struct in_aliasreq *)data; 478 const struct sockaddr_in *addr = &ifra->ifra_addr; 479 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr; 480 const struct sockaddr_in *mask = &ifra->ifra_mask; 481 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr; 482 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0; 483 struct epoch_tracker et; 484 struct ifaddr *ifa; 485 struct in_ifaddr *ia; 486 bool iaIsFirst; 487 int error = 0; 488 489 error = priv_check_cred(cred, PRIV_NET_ADDIFADDR); 490 if (error) 491 return (error); 492 493 /* 494 * ifra_addr must be present and be of INET family. 495 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional. 496 */ 497 if (addr->sin_len != sizeof(struct sockaddr_in) || 498 addr->sin_family != AF_INET) 499 return (EINVAL); 500 if (broadaddr->sin_len != 0 && 501 (broadaddr->sin_len != sizeof(struct sockaddr_in) || 502 broadaddr->sin_family != AF_INET)) 503 return (EINVAL); 504 if (mask->sin_len != 0 && 505 (mask->sin_len != sizeof(struct sockaddr_in) || 506 mask->sin_family != AF_INET)) 507 return (EINVAL); 508 if ((ifp->if_flags & IFF_POINTOPOINT) && 509 (dstaddr->sin_len != sizeof(struct sockaddr_in) || 510 dstaddr->sin_addr.s_addr == INADDR_ANY)) 511 return (EDESTADDRREQ); 512 if (vhid != 0 && carp_attach_p == NULL) 513 return (EPROTONOSUPPORT); 514 515 #ifdef MAC 516 /* Check if a MAC policy disallows setting the IPv4 address. */ 517 error = mac_inet_check_add_addr(cred, &addr->sin_addr, ifp); 518 if (error != 0) 519 return (error); 520 #endif 521 522 /* 523 * Check if bridge wants to allow adding addrs to member interfaces. 524 */ 525 if (ifp->if_bridge && bridge_member_ifaddrs_p && 526 !bridge_member_ifaddrs_p()) 527 return (EINVAL); 528 529 /* 530 * See whether address already exist. 531 */ 532 iaIsFirst = true; 533 ia = NULL; 534 NET_EPOCH_ENTER(et); 535 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 536 struct in_ifaddr *it; 537 538 if (ifa->ifa_addr->sa_family != AF_INET) 539 continue; 540 541 it = (struct in_ifaddr *)ifa; 542 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 543 prison_check_ip4(cred, &addr->sin_addr) == 0) 544 ia = it; 545 else 546 iaIsFirst = false; 547 } 548 NET_EPOCH_EXIT(et); 549 550 if (ia != NULL) 551 (void )in_difaddr_ioctl(cmd, data, ifp, cred); 552 553 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 554 ia = (struct in_ifaddr *)ifa; 555 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 556 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 557 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 558 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock, 559 CALLOUT_RETURNUNLOCKED); 560 561 ia->ia_ifp = ifp; 562 ia->ia_addr = *addr; 563 if (mask->sin_len != 0) { 564 ia->ia_sockmask = *mask; 565 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 566 } else { 567 in_addr_t i = ntohl(addr->sin_addr.s_addr); 568 569 /* 570 * If netmask isn't supplied, use historical default. 571 * This is deprecated for interfaces other than loopback 572 * or point-to-point; warn in other cases. In the future 573 * we should return an error rather than warning. 574 */ 575 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) 576 printf("%s: set address: WARNING: network mask " 577 "should be specified; using historical default\n", 578 ifp->if_xname); 579 if (IN_CLASSA(i)) 580 ia->ia_subnetmask = IN_CLASSA_NET; 581 else if (IN_CLASSB(i)) 582 ia->ia_subnetmask = IN_CLASSB_NET; 583 else 584 ia->ia_subnetmask = IN_CLASSC_NET; 585 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 586 } 587 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask; 588 in_socktrim(&ia->ia_sockmask); 589 590 if (ifp->if_flags & IFF_BROADCAST) { 591 if (broadaddr->sin_len != 0) { 592 ia->ia_broadaddr = *broadaddr; 593 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) { 594 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 595 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 596 ia->ia_broadaddr.sin_family = AF_INET; 597 } else { 598 ia->ia_broadaddr.sin_addr.s_addr = 599 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 600 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 601 ia->ia_broadaddr.sin_family = AF_INET; 602 } 603 } 604 605 if (ifp->if_flags & IFF_POINTOPOINT) 606 ia->ia_dstaddr = *dstaddr; 607 608 if (vhid != 0) { 609 error = (*carp_attach_p)(&ia->ia_ifa, vhid); 610 if (error) 611 return (error); 612 } 613 614 /* if_addrhead is already referenced by ifa_alloc() */ 615 IF_ADDR_WLOCK(ifp); 616 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 617 IF_ADDR_WUNLOCK(ifp); 618 619 ifa_ref(ifa); /* in_ifaddrhead */ 620 sx_assert(&in_control_sx, SA_XLOCKED); 621 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 622 CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, 623 ia_hash); 624 625 /* 626 * Give the interface a chance to initialize 627 * if this is its first address, 628 * and to validate the address if necessary. 629 */ 630 if (ifp->if_ioctl != NULL) { 631 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 632 if (error) 633 goto fail1; 634 } 635 636 /* 637 * Add route for the network. 638 */ 639 if (vhid == 0) { 640 error = in_addprefix(ia); 641 if (error) 642 goto fail1; 643 } 644 645 /* 646 * Add a loopback route to self. 647 */ 648 if (vhid == 0 && ia_need_loopback_route(ia)) { 649 struct in_ifaddr *eia; 650 651 eia = in_localip_more(ia); 652 653 if (eia == NULL) { 654 error = ifa_add_loopback_route((struct ifaddr *)ia, 655 (struct sockaddr *)&ia->ia_addr); 656 if (error) 657 goto fail2; 658 } else 659 ifa_free(&eia->ia_ifa); 660 } 661 662 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) { 663 struct in_addr allhosts_addr; 664 struct in_ifinfo *ii; 665 666 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 667 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 668 669 error = in_joingroup(ifp, &allhosts_addr, NULL, 670 &ii->ii_allhosts); 671 } 672 673 /* 674 * Note: we don't need extra reference for ifa, since we called 675 * with sx lock held, and ifaddr can not be deleted in concurrent 676 * thread. 677 */ 678 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD); 679 680 return (error); 681 682 fail2: 683 if (vhid == 0) 684 (void )in_scrubprefix(ia, LLE_STATIC); 685 686 fail1: 687 if (ia->ia_ifa.ifa_carp) 688 (*carp_detach_p)(&ia->ia_ifa, false); 689 690 IF_ADDR_WLOCK(ifp); 691 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link); 692 IF_ADDR_WUNLOCK(ifp); 693 ifa_free(&ia->ia_ifa); /* if_addrhead */ 694 695 sx_assert(&in_control_sx, SA_XLOCKED); 696 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link); 697 CK_LIST_REMOVE(ia, ia_hash); 698 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 699 700 return (error); 701 } 702 703 static int 704 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred) 705 { 706 const struct ifreq *ifr = (struct ifreq *)data; 707 const struct sockaddr_in *addr = (const struct sockaddr_in *) 708 &ifr->ifr_addr; 709 struct ifaddr *ifa; 710 struct in_ifaddr *ia; 711 bool deleteAny, iaIsLast; 712 int error; 713 714 if (cred != NULL) { 715 error = priv_check_cred(cred, PRIV_NET_DELIFADDR); 716 if (error) 717 return (error); 718 } 719 720 if (addr->sin_len != sizeof(struct sockaddr_in) || 721 addr->sin_family != AF_INET) 722 deleteAny = true; 723 else 724 deleteAny = false; 725 726 iaIsLast = true; 727 ia = NULL; 728 IF_ADDR_WLOCK(ifp); 729 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 730 struct in_ifaddr *it; 731 732 if (ifa->ifa_addr->sa_family != AF_INET) 733 continue; 734 735 it = (struct in_ifaddr *)ifa; 736 if (deleteAny && ia == NULL && (cred == NULL || 737 prison_check_ip4(cred, &it->ia_addr.sin_addr) == 0)) 738 ia = it; 739 740 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 741 (cred == NULL || prison_check_ip4(cred, 742 &addr->sin_addr) == 0)) 743 ia = it; 744 745 if (it != ia) 746 iaIsLast = false; 747 } 748 749 if (ia == NULL) { 750 IF_ADDR_WUNLOCK(ifp); 751 return (EADDRNOTAVAIL); 752 } 753 754 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link); 755 IF_ADDR_WUNLOCK(ifp); 756 ifa_free(&ia->ia_ifa); /* if_addrhead */ 757 758 sx_assert(&in_control_sx, SA_XLOCKED); 759 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link); 760 CK_LIST_REMOVE(ia, ia_hash); 761 762 /* 763 * in_scrubprefix() kills the interface route. 764 */ 765 in_scrubprefix(ia, LLE_STATIC); 766 767 /* 768 * in_ifadown gets rid of all the rest of 769 * the routes. This is not quite the right 770 * thing to do, but at least if we are running 771 * a routing process they will come back. 772 */ 773 in_ifadown(&ia->ia_ifa, 1); 774 775 if (ia->ia_ifa.ifa_carp) 776 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR); 777 778 /* 779 * If this is the last IPv4 address configured on this 780 * interface, leave the all-hosts group. 781 * No state-change report need be transmitted. 782 */ 783 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) { 784 struct in_ifinfo *ii; 785 786 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 787 if (ii->ii_allhosts) { 788 (void)in_leavegroup(ii->ii_allhosts, NULL); 789 ii->ii_allhosts = NULL; 790 } 791 } 792 793 IF_ADDR_WLOCK(ifp); 794 if (callout_stop(&ia->ia_garp_timer) == 1) { 795 ifa_free(&ia->ia_ifa); 796 } 797 IF_ADDR_WUNLOCK(ifp); 798 799 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa, 800 IFADDR_EVENT_DEL); 801 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 802 803 return (0); 804 } 805 806 static int 807 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred) 808 { 809 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 810 const struct sockaddr_in *addr = &ifra->ifra_addr; 811 struct epoch_tracker et; 812 struct ifaddr *ifa; 813 struct in_ifaddr *ia; 814 815 /* 816 * ifra_addr must be present and be of INET family. 817 */ 818 if (addr->sin_len != sizeof(struct sockaddr_in) || 819 addr->sin_family != AF_INET) 820 return (EINVAL); 821 822 /* 823 * See whether address exist. 824 */ 825 ia = NULL; 826 NET_EPOCH_ENTER(et); 827 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 828 struct in_ifaddr *it; 829 830 if (ifa->ifa_addr->sa_family != AF_INET) 831 continue; 832 833 it = (struct in_ifaddr *)ifa; 834 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 835 prison_check_ip4(cred, &addr->sin_addr) == 0) { 836 ia = it; 837 break; 838 } 839 } 840 if (ia == NULL) { 841 NET_EPOCH_EXIT(et); 842 return (EADDRNOTAVAIL); 843 } 844 845 ifra->ifra_mask = ia->ia_sockmask; 846 if ((ifp->if_flags & IFF_POINTOPOINT) && 847 ia->ia_dstaddr.sin_family == AF_INET) 848 ifra->ifra_dstaddr = ia->ia_dstaddr; 849 else if ((ifp->if_flags & IFF_BROADCAST) && 850 ia->ia_broadaddr.sin_family == AF_INET) 851 ifra->ifra_broadaddr = ia->ia_broadaddr; 852 else 853 memset(&ifra->ifra_broadaddr, 0, 854 sizeof(ifra->ifra_broadaddr)); 855 856 NET_EPOCH_EXIT(et); 857 return (0); 858 } 859 860 static int 861 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg) 862 { 863 864 if (nh->nh_ifa == (struct ifaddr *)arg) 865 return (1); 866 867 return (0); 868 } 869 870 static int 871 in_handle_prefix_route(uint32_t fibnum, int cmd, 872 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa, 873 struct ifnet *ifp) 874 { 875 876 NET_EPOCH_ASSERT(); 877 878 /* Prepare gateway */ 879 struct sockaddr_dl_short sdl = { 880 .sdl_family = AF_LINK, 881 .sdl_len = sizeof(struct sockaddr_dl_short), 882 .sdl_type = ifa->ifa_ifp->if_type, 883 .sdl_index = ifa->ifa_ifp->if_index, 884 }; 885 886 struct rt_addrinfo info = { 887 .rti_ifa = ifa, 888 .rti_ifp = ifp, 889 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST), 890 .rti_info = { 891 [RTAX_DST] = (struct sockaddr *)dst, 892 [RTAX_NETMASK] = (struct sockaddr *)netmask, 893 [RTAX_GATEWAY] = (struct sockaddr *)&sdl, 894 }, 895 /* Ensure we delete the prefix IFF prefix ifa matches */ 896 .rti_filter = in_match_ifaddr, 897 .rti_filterdata = ifa, 898 }; 899 900 return (rib_handle_ifaddr_info(fibnum, cmd, &info)); 901 } 902 903 /* 904 * Routing table interaction with interface addresses. 905 * 906 * In general, two types of routes needs to be installed: 907 * a) "interface" or "prefix" route, telling user that the addresses 908 * behind the ifa prefix are reached directly. 909 * b) "loopback" route installed for the ifa address, telling user that 910 * the address belongs to local system. 911 * 912 * Handling for (a) and (b) differs in multi-fib aspects, hence they 913 * are implemented in different functions below. 914 * 915 * The cases above may intersect - /32 interface aliases results in 916 * the same prefix produced by (a) and (b). This blurs the definition 917 * of the "loopback" route and complicate interactions. The interaction 918 * table is defined below. The case numbers are used in the multiple 919 * functions below to refer to the particular test case. 920 * 921 * There can be multiple options: 922 * 1) Adding address with prefix on non-p2p/non-loopback interface. 923 * Example: 192.0.2.1/24. Action: 924 * * add "prefix" route towards 192.0.2.0/24 via @ia interface, 925 * using @ia as an address source. 926 * * add "loopback" route towards 192.0.2.1 via V_loif, saving 927 * @ia ifp in the gateway and using @ia as an address source. 928 * 929 * 2) Adding address with /32 mask to non-p2p/non-loopback interface. 930 * Example: 192.0.2.2/32. Action: 931 * * add "prefix" host route via V_loif, using @ia as an address source. 932 * 933 * 3) Adding address with or without prefix to p2p interface. 934 * Example: 10.0.0.1/24->10.0.0.2. Action: 935 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia 936 * as an address source. Note: no sense in installing full /24 as the interface 937 * is point-to-point. 938 * * add "loopback" route towards 10.0.9.1 via V_loif, saving 939 * @ia ifp in the gateway and using @ia as an address source. 940 * 941 * 4) Adding address with or without prefix to loopback interface. 942 * Example: 192.0.2.1/24. Action: 943 * * add "prefix" host route via @ia interface, using @ia as an address source. 944 * Note: Skip installing /24 prefix as it would introduce TTL loop 945 * for the traffic destined to these addresses. 946 */ 947 948 /* 949 * Checks if @ia needs to install loopback route to @ia address via 950 * ifa_maintain_loopback_route(). 951 * 952 * Return true on success. 953 */ 954 static bool 955 ia_need_loopback_route(const struct in_ifaddr *ia) 956 { 957 struct ifnet *ifp = ia->ia_ifp; 958 959 /* Case 4: Skip loopback interfaces */ 960 if ((ifp->if_flags & IFF_LOOPBACK) || 961 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)) 962 return (false); 963 964 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */ 965 if ((ifp->if_flags & IFF_POINTOPOINT) && 966 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) 967 return (false); 968 969 /* Case 2: skip /32 prefixes */ 970 if (!(ifp->if_flags & IFF_POINTOPOINT) && 971 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST)) 972 return (false); 973 974 return (true); 975 } 976 977 /* 978 * Calculate "prefix" route corresponding to @ia. 979 */ 980 static void 981 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask) 982 { 983 984 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) { 985 /* Case 3: return host route for dstaddr */ 986 *prefix = ia->ia_dstaddr.sin_addr; 987 mask->s_addr = INADDR_BROADCAST; 988 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) { 989 /* Case 4: return host route for ifaddr */ 990 *prefix = ia->ia_addr.sin_addr; 991 mask->s_addr = INADDR_BROADCAST; 992 } else { 993 /* Cases 1,2: return actual ia prefix */ 994 *prefix = ia->ia_addr.sin_addr; 995 *mask = ia->ia_sockmask.sin_addr; 996 prefix->s_addr &= mask->s_addr; 997 } 998 } 999 1000 /* 1001 * Adds or delete interface "prefix" route corresponding to @ifa. 1002 * Returns 0 on success or errno. 1003 */ 1004 static int 1005 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia) 1006 { 1007 struct ifaddr *ifa = &ia->ia_ifa; 1008 struct in_addr daddr, maddr; 1009 struct sockaddr_in *pmask; 1010 struct epoch_tracker et; 1011 int error; 1012 1013 ia_getrtprefix(ia, &daddr, &maddr); 1014 1015 struct sockaddr_in mask = { 1016 .sin_family = AF_INET, 1017 .sin_len = sizeof(struct sockaddr_in), 1018 .sin_addr = maddr, 1019 }; 1020 1021 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL; 1022 1023 struct sockaddr_in dst = { 1024 .sin_family = AF_INET, 1025 .sin_len = sizeof(struct sockaddr_in), 1026 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr, 1027 }; 1028 1029 struct ifnet *ifp = ia->ia_ifp; 1030 1031 if ((maddr.s_addr == INADDR_BROADCAST) && 1032 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) { 1033 /* Case 2: host route on broadcast interface */ 1034 ifp = V_loif; 1035 } 1036 1037 uint32_t fibnum = ifa->ifa_ifp->if_fib; 1038 NET_EPOCH_ENTER(et); 1039 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp); 1040 NET_EPOCH_EXIT(et); 1041 1042 return (error); 1043 } 1044 1045 /* 1046 * Check if we have a route for the given prefix already. 1047 */ 1048 static bool 1049 in_hasrtprefix(struct in_ifaddr *target) 1050 { 1051 struct epoch_tracker et; 1052 struct in_ifaddr *ia; 1053 struct in_addr prefix, mask, p, m; 1054 bool result = false; 1055 1056 ia_getrtprefix(target, &prefix, &mask); 1057 1058 /* Look for an existing address with the same prefix, mask, and fib */ 1059 NET_EPOCH_ENTER(et); 1060 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1061 ia_getrtprefix(ia, &p, &m); 1062 1063 if (prefix.s_addr != p.s_addr || 1064 mask.s_addr != m.s_addr) 1065 continue; 1066 1067 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib) 1068 continue; 1069 1070 /* 1071 * If we got a matching prefix route inserted by other 1072 * interface address, we are done here. 1073 */ 1074 if (ia->ia_flags & IFA_ROUTE) { 1075 result = true; 1076 break; 1077 } 1078 } 1079 NET_EPOCH_EXIT(et); 1080 1081 return (result); 1082 } 1083 1084 int 1085 in_addprefix(struct in_ifaddr *target) 1086 { 1087 int error; 1088 1089 if (in_hasrtprefix(target)) { 1090 if (V_nosameprefix) 1091 return (EEXIST); 1092 else { 1093 rt_addrmsg(RTM_ADD, &target->ia_ifa, 1094 target->ia_ifp->if_fib); 1095 return (0); 1096 } 1097 } 1098 1099 /* 1100 * No-one seem to have this prefix route, so we try to insert it. 1101 */ 1102 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib); 1103 error = in_handle_ifaddr_route(RTM_ADD, target); 1104 if (!error) 1105 target->ia_flags |= IFA_ROUTE; 1106 return (error); 1107 } 1108 1109 /* 1110 * Removes either all lle entries for given @ia, or lle 1111 * corresponding to @ia address. 1112 */ 1113 static void 1114 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags) 1115 { 1116 struct sockaddr_in addr, mask; 1117 struct sockaddr *saddr, *smask; 1118 struct ifnet *ifp; 1119 1120 saddr = (struct sockaddr *)&addr; 1121 bzero(&addr, sizeof(addr)); 1122 addr.sin_len = sizeof(addr); 1123 addr.sin_family = AF_INET; 1124 smask = (struct sockaddr *)&mask; 1125 bzero(&mask, sizeof(mask)); 1126 mask.sin_len = sizeof(mask); 1127 mask.sin_family = AF_INET; 1128 mask.sin_addr.s_addr = ia->ia_subnetmask; 1129 ifp = ia->ia_ifp; 1130 1131 if (all) { 1132 /* 1133 * Remove all L2 entries matching given prefix. 1134 * Convert address to host representation to avoid 1135 * doing this on every callback. ia_subnetmask is already 1136 * stored in host representation. 1137 */ 1138 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr); 1139 lltable_prefix_free(AF_INET, saddr, smask, flags); 1140 } else { 1141 /* Remove interface address only */ 1142 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr; 1143 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr); 1144 } 1145 } 1146 1147 /* 1148 * If there is no other address in the system that can serve a route to the 1149 * same prefix, remove the route. Hand over the route to the new address 1150 * otherwise. 1151 */ 1152 int 1153 in_scrubprefix(struct in_ifaddr *target, u_int flags) 1154 { 1155 struct epoch_tracker et; 1156 struct in_ifaddr *ia; 1157 struct in_addr prefix, mask, p, m; 1158 int error = 0; 1159 1160 /* 1161 * Remove the loopback route to the interface address. 1162 */ 1163 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) { 1164 struct in_ifaddr *eia; 1165 1166 eia = in_localip_more(target); 1167 1168 if (eia != NULL) { 1169 error = ifa_switch_loopback_route((struct ifaddr *)eia, 1170 (struct sockaddr *)&target->ia_addr); 1171 ifa_free(&eia->ia_ifa); 1172 } else { 1173 error = ifa_del_loopback_route((struct ifaddr *)target, 1174 (struct sockaddr *)&target->ia_addr); 1175 } 1176 } 1177 1178 ia_getrtprefix(target, &prefix, &mask); 1179 1180 if ((target->ia_flags & IFA_ROUTE) == 0) { 1181 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib); 1182 1183 /* 1184 * Removing address from !IFF_UP interface or 1185 * prefix which exists on other interface (along with route). 1186 * No entries should exist here except target addr. 1187 * Given that, delete this entry only. 1188 */ 1189 in_scrubprefixlle(target, 0, flags); 1190 return (0); 1191 } 1192 1193 NET_EPOCH_ENTER(et); 1194 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1195 ia_getrtprefix(ia, &p, &m); 1196 1197 if (prefix.s_addr != p.s_addr || 1198 mask.s_addr != m.s_addr) 1199 continue; 1200 1201 if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 1202 continue; 1203 1204 /* 1205 * If we got a matching prefix address, move IFA_ROUTE and 1206 * the route itself to it. Make sure that routing daemons 1207 * get a heads-up. 1208 */ 1209 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1210 ifa_ref(&ia->ia_ifa); 1211 NET_EPOCH_EXIT(et); 1212 error = in_handle_ifaddr_route(RTM_DELETE, target); 1213 if (error == 0) 1214 target->ia_flags &= ~IFA_ROUTE; 1215 else 1216 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 1217 error); 1218 /* Scrub all entries IFF interface is different */ 1219 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp, 1220 flags); 1221 error = in_handle_ifaddr_route(RTM_ADD, ia); 1222 if (error == 0) 1223 ia->ia_flags |= IFA_ROUTE; 1224 else 1225 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 1226 error); 1227 ifa_free(&ia->ia_ifa); 1228 return (error); 1229 } 1230 } 1231 NET_EPOCH_EXIT(et); 1232 1233 /* 1234 * remove all L2 entries on the given prefix 1235 */ 1236 in_scrubprefixlle(target, 1, flags); 1237 1238 /* 1239 * As no-one seem to have this prefix, we can remove the route. 1240 */ 1241 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib); 1242 error = in_handle_ifaddr_route(RTM_DELETE, target); 1243 if (error == 0) 1244 target->ia_flags &= ~IFA_ROUTE; 1245 else 1246 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 1247 return (error); 1248 } 1249 1250 void 1251 in_ifscrub_all(void) 1252 { 1253 struct ifnet *ifp; 1254 struct ifaddr *ifa, *nifa; 1255 struct ifreq ifr; 1256 1257 IFNET_RLOCK(); 1258 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1259 /* Cannot lock here - lock recursion. */ 1260 /* NET_EPOCH_ENTER(et); */ 1261 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 1262 if (ifa->ifa_addr->sa_family != AF_INET) 1263 continue; 1264 1265 /* 1266 * This is ugly but the only way for legacy IP to 1267 * cleanly remove addresses and everything attached. 1268 */ 1269 bzero(&ifr, sizeof(ifr)); 1270 ifr.ifr_addr = *ifa->ifa_addr; 1271 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, 1272 ifp, NULL); 1273 } 1274 /* NET_EPOCH_EXIT(et); */ 1275 in_purgemaddrs(ifp); 1276 igmp_domifdetach(ifp); 1277 } 1278 IFNET_RUNLOCK(); 1279 } 1280 1281 bool 1282 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia) 1283 { 1284 1285 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1286 /* 1287 * Optionally check for old-style (host 0) broadcast, but 1288 * taking into account that RFC 3021 obsoletes it. 1289 */ 1290 __predict_false(V_broadcast_lowest && 1291 ia->ia_subnetmask != IN_RFC3021_MASK && 1292 ntohl(in.s_addr) == ia->ia_subnet)) && 1293 /* 1294 * Check for an all one subnetmask. These 1295 * only exist when an interface gets a secondary 1296 * address. 1297 */ 1298 ia->ia_subnetmask != (u_long)0xffffffff); 1299 } 1300 1301 /* 1302 * Return true if the address might be a local broadcast address. 1303 */ 1304 bool 1305 in_ifnet_broadcast(struct in_addr in, struct ifnet *ifp) 1306 { 1307 struct ifaddr *ifa; 1308 1309 NET_EPOCH_ASSERT(); 1310 1311 if (in_broadcast(in)) 1312 return (true); 1313 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1314 return (false); 1315 /* 1316 * Look through the list of addresses for a match 1317 * with a broadcast address. 1318 */ 1319 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1320 if (ifa->ifa_addr->sa_family == AF_INET && 1321 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) 1322 return (true); 1323 return (false); 1324 } 1325 1326 /* 1327 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1328 */ 1329 void 1330 in_ifdetach(struct ifnet *ifp) 1331 { 1332 IN_MULTI_LOCK(); 1333 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1334 in_pcbpurgeif0(&V_udbinfo, ifp); 1335 in_pcbpurgeif0(&V_ulitecbinfo, ifp); 1336 in_purgemaddrs(ifp); 1337 IN_MULTI_UNLOCK(); 1338 1339 /* 1340 * Make sure all multicast deletions invoking if_ioctl() are 1341 * completed before returning. Else we risk accessing a freed 1342 * ifnet structure pointer. 1343 */ 1344 inm_release_wait(NULL); 1345 } 1346 1347 static void 1348 in_ifnet_event(void *arg __unused, struct ifnet *ifp, int event) 1349 { 1350 struct epoch_tracker et; 1351 struct ifaddr *ifa; 1352 struct in_ifaddr *ia; 1353 int error; 1354 1355 NET_EPOCH_ENTER(et); 1356 switch (event) { 1357 case IFNET_EVENT_DOWN: 1358 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1359 if (ifa->ifa_addr->sa_family != AF_INET) 1360 continue; 1361 ia = (struct in_ifaddr *)ifa; 1362 if ((ia->ia_flags & IFA_ROUTE) == 0) 1363 continue; 1364 ifa_ref(ifa); 1365 /* 1366 * in_scrubprefix() kills the interface route. 1367 */ 1368 in_scrubprefix(ia, 0); 1369 /* 1370 * in_ifadown gets rid of all the rest of the 1371 * routes. This is not quite the right thing 1372 * to do, but at least if we are running a 1373 * routing process they will come back. 1374 */ 1375 in_ifadown(ifa, 0); 1376 ifa_free(ifa); 1377 } 1378 break; 1379 1380 case IFNET_EVENT_UP: 1381 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1382 if (ifa->ifa_addr->sa_family != AF_INET) 1383 continue; 1384 ia = (struct in_ifaddr *)ifa; 1385 if (ia->ia_flags & IFA_ROUTE) 1386 continue; 1387 ifa_ref(ifa); 1388 error = ifa_del_loopback_route(ifa, ifa->ifa_addr); 1389 rt_addrmsg(RTM_ADD, ifa, ifa->ifa_ifp->if_fib); 1390 error = in_handle_ifaddr_route(RTM_ADD, ia); 1391 if (error == 0) 1392 ia->ia_flags |= IFA_ROUTE; 1393 error = ifa_add_loopback_route(ifa, ifa->ifa_addr); 1394 ifa_free(ifa); 1395 } 1396 break; 1397 } 1398 NET_EPOCH_EXIT(et); 1399 } 1400 EVENTHANDLER_DEFINE(ifnet_event, in_ifnet_event, NULL, EVENTHANDLER_PRI_ANY); 1401 1402 /* 1403 * Delete all IPv4 multicast address records, and associated link-layer 1404 * multicast address records, associated with ifp. 1405 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1406 * XXX This should not race with ifma_protospec being set during 1407 * a new allocation, if it does, we have bigger problems. 1408 */ 1409 static void 1410 in_purgemaddrs(struct ifnet *ifp) 1411 { 1412 struct epoch_tracker et; 1413 struct in_multi_head purgeinms; 1414 struct in_multi *inm; 1415 struct ifmultiaddr *ifma; 1416 1417 SLIST_INIT(&purgeinms); 1418 IN_MULTI_LIST_LOCK(); 1419 1420 /* 1421 * Extract list of in_multi associated with the detaching ifp 1422 * which the PF_INET layer is about to release. 1423 * We need to do this as IF_ADDR_LOCK() may be re-acquired 1424 * by code further down. 1425 */ 1426 IF_ADDR_WLOCK(ifp); 1427 NET_EPOCH_ENTER(et); 1428 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1429 inm = inm_ifmultiaddr_get_inm(ifma); 1430 if (inm == NULL) 1431 continue; 1432 inm_rele_locked(&purgeinms, inm); 1433 } 1434 NET_EPOCH_EXIT(et); 1435 IF_ADDR_WUNLOCK(ifp); 1436 1437 inm_release_list_deferred(&purgeinms); 1438 igmp_ifdetach(ifp); 1439 IN_MULTI_LIST_UNLOCK(); 1440 } 1441 1442 struct in_llentry { 1443 struct llentry base; 1444 }; 1445 1446 #define IN_LLTBL_DEFAULT_HSIZE 32 1447 #define IN_LLTBL_HASH(k, h) \ 1448 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 1449 1450 /* 1451 * Do actual deallocation of @lle. 1452 */ 1453 static void 1454 in_lltable_destroy_lle_unlocked(epoch_context_t ctx) 1455 { 1456 struct llentry *lle; 1457 1458 lle = __containerof(ctx, struct llentry, lle_epoch_ctx); 1459 LLE_LOCK_DESTROY(lle); 1460 LLE_REQ_DESTROY(lle); 1461 free(lle, M_LLTABLE); 1462 } 1463 1464 /* 1465 * Called by LLE_FREE_LOCKED when number of references 1466 * drops to zero. 1467 */ 1468 static void 1469 in_lltable_destroy_lle(struct llentry *lle) 1470 { 1471 1472 LLE_WUNLOCK(lle); 1473 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx); 1474 } 1475 1476 static struct llentry * 1477 in_lltable_new(struct in_addr addr4, u_int flags) 1478 { 1479 struct in_llentry *lle; 1480 1481 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 1482 if (lle == NULL) /* NB: caller generates msg */ 1483 return NULL; 1484 1485 /* 1486 * For IPv4 this will trigger "arpresolve" to generate 1487 * an ARP request. 1488 */ 1489 lle->base.la_expire = time_uptime; /* mark expired */ 1490 lle->base.r_l3addr.addr4 = addr4; 1491 lle->base.lle_refcnt = 1; 1492 lle->base.lle_free = in_lltable_destroy_lle; 1493 LLE_LOCK_INIT(&lle->base); 1494 LLE_REQ_INIT(&lle->base); 1495 callout_init(&lle->base.lle_timer, 1); 1496 1497 return (&lle->base); 1498 } 1499 1500 static int 1501 in_lltable_match_prefix(const struct sockaddr *saddr, 1502 const struct sockaddr *smask, u_int flags, struct llentry *lle) 1503 { 1504 struct in_addr addr, mask, lle_addr; 1505 1506 addr = ((const struct sockaddr_in *)saddr)->sin_addr; 1507 mask = ((const struct sockaddr_in *)smask)->sin_addr; 1508 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr); 1509 1510 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0) 1511 return (0); 1512 1513 if (lle->la_flags & LLE_IFADDR) { 1514 /* 1515 * Delete LLE_IFADDR records IFF address & flag matches. 1516 * Note that addr is the interface address within prefix 1517 * being matched. 1518 * Note also we should handle 'ifdown' cases without removing 1519 * ifaddr macs. 1520 */ 1521 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0) 1522 return (1); 1523 return (0); 1524 } 1525 1526 /* flags & LLE_STATIC means deleting both dynamic and static entries */ 1527 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)) 1528 return (1); 1529 1530 return (0); 1531 } 1532 1533 static void 1534 in_lltable_free_entry(struct lltable *llt, struct llentry *lle) 1535 { 1536 size_t pkts_dropped; 1537 1538 LLE_WLOCK_ASSERT(lle); 1539 KASSERT(llt != NULL, ("lltable is NULL")); 1540 1541 /* Unlink entry from table if not already */ 1542 if ((lle->la_flags & LLE_LINKED) != 0) { 1543 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp); 1544 lltable_unlink_entry(llt, lle); 1545 } 1546 1547 /* Drop hold queue */ 1548 pkts_dropped = llentry_free(lle); 1549 ARPSTAT_ADD(dropped, pkts_dropped); 1550 } 1551 1552 static int 1553 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 1554 { 1555 struct nhop_object *nh; 1556 struct in_addr addr; 1557 1558 KASSERT(l3addr->sa_family == AF_INET, 1559 ("sin_family %d", l3addr->sa_family)); 1560 1561 addr = ((const struct sockaddr_in *)l3addr)->sin_addr; 1562 1563 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0); 1564 if (nh == NULL) 1565 return (EINVAL); 1566 1567 /* 1568 * If the gateway for an existing host route matches the target L3 1569 * address, which is a special route inserted by some implementation 1570 * such as MANET, and the interface is of the correct type, then 1571 * allow for ARP to proceed. 1572 */ 1573 if (nh->nh_flags & NHF_GATEWAY) { 1574 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER || 1575 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 1576 memcmp(nh->gw_sa.sa_data, l3addr->sa_data, 1577 sizeof(in_addr_t)) != 0) { 1578 return (EINVAL); 1579 } 1580 } 1581 1582 /* 1583 * Make sure that at least the destination address is covered 1584 * by the route. This is for handling the case where 2 or more 1585 * interfaces have the same prefix. An incoming packet arrives 1586 * on one interface and the corresponding outgoing packet leaves 1587 * another interface. 1588 */ 1589 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) { 1590 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp); 1591 struct in_addr dst_addr, mask_addr; 1592 1593 if (ia == NULL) 1594 return (EINVAL); 1595 1596 /* 1597 * ifaof_ifpforaddr() returns _best matching_ IFA. 1598 * It is possible that ifa prefix does not cover our address. 1599 * Explicitly verify and fail if that's the case. 1600 */ 1601 dst_addr = IA_SIN(ia)->sin_addr; 1602 mask_addr.s_addr = htonl(ia->ia_subnetmask); 1603 1604 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr)) 1605 return (EINVAL); 1606 } 1607 1608 return (0); 1609 } 1610 1611 static inline uint32_t 1612 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize) 1613 { 1614 1615 return (IN_LLTBL_HASH(dst.s_addr, hsize)); 1616 } 1617 1618 static uint32_t 1619 in_lltable_hash(const struct llentry *lle, uint32_t hsize) 1620 { 1621 1622 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize)); 1623 } 1624 1625 static void 1626 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 1627 { 1628 struct sockaddr_in *sin; 1629 1630 sin = (struct sockaddr_in *)sa; 1631 bzero(sin, sizeof(*sin)); 1632 sin->sin_family = AF_INET; 1633 sin->sin_len = sizeof(*sin); 1634 sin->sin_addr = lle->r_l3addr.addr4; 1635 } 1636 1637 static inline struct llentry * 1638 in_lltable_find_dst(struct lltable *llt, struct in_addr dst) 1639 { 1640 struct llentry *lle; 1641 struct llentries *lleh; 1642 u_int hashidx; 1643 1644 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize); 1645 lleh = &llt->lle_head[hashidx]; 1646 CK_LIST_FOREACH(lle, lleh, lle_next) { 1647 if (lle->la_flags & LLE_DELETED) 1648 continue; 1649 if (lle->r_l3addr.addr4.s_addr == dst.s_addr) 1650 break; 1651 } 1652 1653 return (lle); 1654 } 1655 1656 static void 1657 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle) 1658 { 1659 1660 lle->la_flags |= LLE_DELETED; 1661 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 1662 #ifdef DIAGNOSTIC 1663 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1664 #endif 1665 llentry_free(lle); 1666 } 1667 1668 static struct llentry * 1669 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1670 { 1671 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1672 struct ifnet *ifp = llt->llt_ifp; 1673 struct llentry *lle; 1674 char linkhdr[LLE_MAX_LINKHDR]; 1675 size_t linkhdrsize; 1676 int lladdr_off; 1677 1678 KASSERT(l3addr->sa_family == AF_INET, 1679 ("sin_family %d", l3addr->sa_family)); 1680 1681 /* 1682 * A route that covers the given address must have 1683 * been installed 1st because we are doing a resolution, 1684 * verify this. 1685 */ 1686 if (!(flags & LLE_IFADDR) && 1687 in_lltable_rtcheck(ifp, flags, l3addr) != 0) 1688 return (NULL); 1689 1690 lle = in_lltable_new(sin->sin_addr, flags); 1691 if (lle == NULL) { 1692 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1693 return (NULL); 1694 } 1695 lle->la_flags = flags; 1696 if (flags & LLE_STATIC) 1697 lle->r_flags |= RLLE_VALID; 1698 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 1699 linkhdrsize = LLE_MAX_LINKHDR; 1700 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp), 1701 linkhdr, &linkhdrsize, &lladdr_off) != 0) { 1702 in_lltable_free_entry(llt, lle); 1703 return (NULL); 1704 } 1705 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, 1706 lladdr_off); 1707 lle->la_flags |= LLE_STATIC; 1708 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR); 1709 lle->la_expire = 0; 1710 } 1711 1712 return (lle); 1713 } 1714 1715 /* 1716 * Return NULL if not found or marked for deletion. 1717 * If found return lle read locked. 1718 */ 1719 static struct llentry * 1720 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1721 { 1722 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1723 struct llentry *lle; 1724 1725 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); 1726 KASSERT(l3addr->sa_family == AF_INET, 1727 ("sin_family %d", l3addr->sa_family)); 1728 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) != 1729 (LLE_UNLOCKED | LLE_EXCLUSIVE), 1730 ("wrong lle request flags: %#x", flags)); 1731 1732 lle = in_lltable_find_dst(llt, sin->sin_addr); 1733 if (lle == NULL) 1734 return (NULL); 1735 if (flags & LLE_UNLOCKED) 1736 return (lle); 1737 1738 if (flags & LLE_EXCLUSIVE) 1739 LLE_WLOCK(lle); 1740 else 1741 LLE_RLOCK(lle); 1742 1743 /* 1744 * If the afdata lock is not held, the LLE may have been unlinked while 1745 * we were blocked on the LLE lock. Check for this case. 1746 */ 1747 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) { 1748 if (flags & LLE_EXCLUSIVE) 1749 LLE_WUNLOCK(lle); 1750 else 1751 LLE_RUNLOCK(lle); 1752 return (NULL); 1753 } 1754 return (lle); 1755 } 1756 1757 static int 1758 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle, 1759 struct sysctl_req *wr) 1760 { 1761 struct ifnet *ifp = llt->llt_ifp; 1762 /* XXX stack use */ 1763 struct { 1764 struct rt_msghdr rtm; 1765 struct sockaddr_in sin; 1766 struct sockaddr_dl sdl; 1767 } arpc; 1768 struct sockaddr_dl *sdl; 1769 int error; 1770 1771 bzero(&arpc, sizeof(arpc)); 1772 /* skip deleted entries */ 1773 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 1774 return (0); 1775 /* Skip if jailed and not a valid IP of the prison. */ 1776 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin); 1777 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0) 1778 return (0); 1779 /* 1780 * produce a msg made of: 1781 * struct rt_msghdr; 1782 * struct sockaddr_in; (IPv4) 1783 * struct sockaddr_dl; 1784 */ 1785 arpc.rtm.rtm_msglen = sizeof(arpc); 1786 arpc.rtm.rtm_version = RTM_VERSION; 1787 arpc.rtm.rtm_type = RTM_GET; 1788 arpc.rtm.rtm_flags = RTF_UP; 1789 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1790 1791 /* publish */ 1792 if (lle->la_flags & LLE_PUB) 1793 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1794 1795 sdl = &arpc.sdl; 1796 sdl->sdl_family = AF_LINK; 1797 sdl->sdl_len = sizeof(*sdl); 1798 sdl->sdl_index = ifp->if_index; 1799 sdl->sdl_type = ifp->if_type; 1800 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 1801 sdl->sdl_alen = ifp->if_addrlen; 1802 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1803 } else { 1804 sdl->sdl_alen = 0; 1805 bzero(LLADDR(sdl), ifp->if_addrlen); 1806 } 1807 1808 arpc.rtm.rtm_rmx.rmx_expire = 1809 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1810 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1811 if (lle->la_flags & LLE_STATIC) 1812 arpc.rtm.rtm_flags |= RTF_STATIC; 1813 if (lle->la_flags & LLE_IFADDR) 1814 arpc.rtm.rtm_flags |= RTF_PINNED; 1815 arpc.rtm.rtm_index = ifp->if_index; 1816 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1817 1818 return (error); 1819 } 1820 1821 static void 1822 in_lltable_post_resolved(struct lltable *llt, struct llentry *lle) 1823 { 1824 struct ifnet *ifp = llt->llt_ifp; 1825 1826 /* gratuitous ARP */ 1827 if ((lle->la_flags & LLE_PUB) != 0) 1828 arprequest(ifp, &lle->r_l3addr.addr4, &lle->r_l3addr.addr4, 1829 lle->ll_addr); 1830 } 1831 1832 static struct lltable * 1833 in_lltattach(struct ifnet *ifp) 1834 { 1835 struct lltable *llt; 1836 1837 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE); 1838 llt->llt_af = AF_INET; 1839 llt->llt_ifp = ifp; 1840 1841 llt->llt_lookup = in_lltable_lookup; 1842 llt->llt_alloc_entry = in_lltable_alloc; 1843 llt->llt_delete_entry = in_lltable_delete_entry; 1844 llt->llt_dump_entry = in_lltable_dump_entry; 1845 llt->llt_hash = in_lltable_hash; 1846 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry; 1847 llt->llt_free_entry = in_lltable_free_entry; 1848 llt->llt_match_prefix = in_lltable_match_prefix; 1849 llt->llt_mark_used = llentry_mark_used; 1850 llt->llt_post_resolved = in_lltable_post_resolved; 1851 lltable_link(llt); 1852 1853 return (llt); 1854 } 1855 1856 struct lltable * 1857 in_lltable_get(struct ifnet *ifp) 1858 { 1859 struct lltable *llt = NULL; 1860 1861 void *afdata_ptr = ifp->if_afdata[AF_INET]; 1862 if (afdata_ptr != NULL) 1863 llt = ((struct in_ifinfo *)afdata_ptr)->ii_llt; 1864 return (llt); 1865 } 1866 1867 void * 1868 in_domifattach(struct ifnet *ifp) 1869 { 1870 struct in_ifinfo *ii; 1871 1872 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1873 1874 ii->ii_llt = in_lltattach(ifp); 1875 ii->ii_igmp = igmp_domifattach(ifp); 1876 1877 return (ii); 1878 } 1879 1880 void 1881 in_domifdetach(struct ifnet *ifp, void *aux) 1882 { 1883 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1884 1885 igmp_domifdetach(ifp); 1886 lltable_free(ii->ii_llt); 1887 free(ii, M_IFADDR); 1888 } 1889