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