1 /* $NetBSD: in.c,v 1.161 2015/08/31 16:46:14 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * Copyright (c) 1982, 1986, 1991, 1993 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)in.c 8.4 (Berkeley) 1/9/95 91 */ 92 93 #include <sys/cdefs.h> 94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.161 2015/08/31 16:46:14 ozaki-r Exp $"); 95 96 #include "arp.h" 97 #include "ether.h" 98 99 #ifdef _KERNEL_OPT 100 #include "opt_inet.h" 101 #include "opt_inet_conf.h" 102 #include "opt_mrouting.h" 103 #endif 104 105 #include <sys/param.h> 106 #include <sys/ioctl.h> 107 #include <sys/errno.h> 108 #include <sys/kernel.h> 109 #include <sys/malloc.h> 110 #include <sys/socket.h> 111 #include <sys/socketvar.h> 112 #include <sys/sysctl.h> 113 #include <sys/systm.h> 114 #include <sys/proc.h> 115 #include <sys/syslog.h> 116 #include <sys/kauth.h> 117 #include <sys/kmem.h> 118 119 #include <sys/cprng.h> 120 121 #include <net/if.h> 122 #include <net/route.h> 123 #include <net/pfil.h> 124 125 #include <net/if_arp.h> 126 #include <net/if_ether.h> 127 #include <net/if_types.h> 128 #include <net/if_llatbl.h> 129 #include <net/if_dl.h> 130 131 #include <netinet/in_systm.h> 132 #include <netinet/in.h> 133 #include <netinet/in_var.h> 134 #include <netinet/ip.h> 135 #include <netinet/ip_var.h> 136 #include <netinet/in_ifattach.h> 137 #include <netinet/in_pcb.h> 138 #include <netinet/in_selsrc.h> 139 #include <netinet/if_inarp.h> 140 #include <netinet/ip_mroute.h> 141 #include <netinet/igmp_var.h> 142 143 #ifdef IPSELSRC 144 #include <netinet/in_selsrc.h> 145 #endif 146 147 static u_int in_mask2len(struct in_addr *); 148 static void in_len2mask(struct in_addr *, u_int); 149 static int in_lifaddr_ioctl(struct socket *, u_long, void *, 150 struct ifnet *); 151 152 static int in_addprefix(struct in_ifaddr *, int); 153 static int in_scrubprefix(struct in_ifaddr *); 154 static void in_sysctl_init(struct sysctllog **); 155 156 #ifndef SUBNETSARELOCAL 157 #define SUBNETSARELOCAL 1 158 #endif 159 160 #ifndef HOSTZEROBROADCAST 161 #define HOSTZEROBROADCAST 1 162 #endif 163 164 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */ 165 #ifndef IN_MULTI_HASH_SIZE 166 #define IN_MULTI_HASH_SIZE 509 167 #endif 168 169 static int subnetsarelocal = SUBNETSARELOCAL; 170 static int hostzeroisbroadcast = HOSTZEROBROADCAST; 171 172 /* 173 * This list is used to keep track of in_multi chains which belong to 174 * deleted interface addresses. We use in_ifaddr so that a chain head 175 * won't be deallocated until all multicast address record are deleted. 176 */ 177 178 LIST_HEAD(in_multihashhead, in_multi); /* Type of the hash head */ 179 180 static struct pool inmulti_pool; 181 static u_int in_multientries; 182 static struct in_multihashhead *in_multihashtbl; 183 static u_long in_multihash; 184 static krwlock_t in_multilock; 185 186 #define IN_MULTI_HASH(x, ifp) \ 187 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE]) 188 189 struct in_ifaddrhashhead * in_ifaddrhashtbl; 190 u_long in_ifaddrhash; 191 struct in_ifaddrhead in_ifaddrhead; 192 193 void 194 in_init(void) 195 { 196 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", 197 NULL, IPL_SOFTNET); 198 TAILQ_INIT(&in_ifaddrhead); 199 200 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true, 201 &in_ifaddrhash); 202 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true, 203 &in_multihash); 204 rw_init(&in_multilock); 205 206 in_sysctl_init(NULL); 207 } 208 209 /* 210 * Return 1 if an internet address is for a ``local'' host 211 * (one to which we have a connection). If subnetsarelocal 212 * is true, this includes other subnets of the local net. 213 * Otherwise, it includes only the directly-connected (sub)nets. 214 */ 215 int 216 in_localaddr(struct in_addr in) 217 { 218 struct in_ifaddr *ia; 219 220 if (subnetsarelocal) { 221 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 222 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) 223 return (1); 224 } else { 225 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 226 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) 227 return (1); 228 } 229 return (0); 230 } 231 232 /* 233 * Determine whether an IP address is in a reserved set of addresses 234 * that may not be forwarded, or whether datagrams to that destination 235 * may be forwarded. 236 */ 237 int 238 in_canforward(struct in_addr in) 239 { 240 u_int32_t net; 241 242 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 243 return (0); 244 if (IN_CLASSA(in.s_addr)) { 245 net = in.s_addr & IN_CLASSA_NET; 246 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 247 return (0); 248 } 249 return (1); 250 } 251 252 /* 253 * Trim a mask in a sockaddr 254 */ 255 void 256 in_socktrim(struct sockaddr_in *ap) 257 { 258 char *cplim = (char *) &ap->sin_addr; 259 char *cp = (char *) (&ap->sin_addr + 1); 260 261 ap->sin_len = 0; 262 while (--cp >= cplim) 263 if (*cp) { 264 (ap)->sin_len = cp - (char *) (ap) + 1; 265 break; 266 } 267 } 268 269 /* 270 * Routine to take an Internet address and convert into a 271 * "dotted quad" representation for printing. 272 */ 273 const char * 274 in_fmtaddr(struct in_addr addr) 275 { 276 static char buf[sizeof("123.456.789.123")]; 277 278 addr.s_addr = ntohl(addr.s_addr); 279 280 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", 281 (addr.s_addr >> 24) & 0xFF, 282 (addr.s_addr >> 16) & 0xFF, 283 (addr.s_addr >> 8) & 0xFF, 284 (addr.s_addr >> 0) & 0xFF); 285 return buf; 286 } 287 288 /* 289 * Maintain the "in_maxmtu" variable, which is the largest 290 * mtu for non-local interfaces with AF_INET addresses assigned 291 * to them that are up. 292 */ 293 unsigned long in_maxmtu; 294 295 void 296 in_setmaxmtu(void) 297 { 298 struct in_ifaddr *ia; 299 struct ifnet *ifp; 300 unsigned long maxmtu = 0; 301 302 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 303 if ((ifp = ia->ia_ifp) == 0) 304 continue; 305 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP) 306 continue; 307 if (ifp->if_mtu > maxmtu) 308 maxmtu = ifp->if_mtu; 309 } 310 if (maxmtu) 311 in_maxmtu = maxmtu; 312 } 313 314 static u_int 315 in_mask2len(struct in_addr *mask) 316 { 317 u_int x, y; 318 u_char *p; 319 320 p = (u_char *)mask; 321 for (x = 0; x < sizeof(*mask); x++) { 322 if (p[x] != 0xff) 323 break; 324 } 325 y = 0; 326 if (x < sizeof(*mask)) { 327 for (y = 0; y < NBBY; y++) { 328 if ((p[x] & (0x80 >> y)) == 0) 329 break; 330 } 331 } 332 return x * NBBY + y; 333 } 334 335 static void 336 in_len2mask(struct in_addr *mask, u_int len) 337 { 338 u_int i; 339 u_char *p; 340 341 p = (u_char *)mask; 342 memset(mask, 0, sizeof(*mask)); 343 for (i = 0; i < len / NBBY; i++) 344 p[i] = 0xff; 345 if (len % NBBY) 346 p[i] = (0xff00 >> (len % NBBY)) & 0xff; 347 } 348 349 /* 350 * Generic internet control operations (ioctl's). 351 * Ifp is 0 if not an interface-specific ioctl. 352 */ 353 /* ARGSUSED */ 354 int 355 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 356 { 357 struct ifreq *ifr = (struct ifreq *)data; 358 struct in_ifaddr *ia = NULL; 359 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 360 struct sockaddr_in oldaddr; 361 int error, hostIsNew, maskIsNew; 362 int newifaddr = 0; 363 364 switch (cmd) { 365 case SIOCALIFADDR: 366 case SIOCDLIFADDR: 367 case SIOCGLIFADDR: 368 if (ifp == NULL) 369 return EINVAL; 370 return in_lifaddr_ioctl(so, cmd, data, ifp); 371 case SIOCGIFADDRPREF: 372 case SIOCSIFADDRPREF: 373 if (ifp == NULL) 374 return EINVAL; 375 return ifaddrpref_ioctl(so, cmd, data, ifp); 376 } 377 378 /* 379 * Find address for this interface, if it exists. 380 */ 381 if (ifp != NULL) 382 IFP_TO_IA(ifp, ia); 383 384 hostIsNew = 1; /* moved here to appease gcc */ 385 switch (cmd) { 386 case SIOCAIFADDR: 387 case SIOCDIFADDR: 388 case SIOCGIFALIAS: 389 case SIOCGIFAFLAG_IN: 390 if (ifra->ifra_addr.sin_family == AF_INET) 391 LIST_FOREACH(ia, 392 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr), 393 ia_hash) { 394 if (ia->ia_ifp == ifp && 395 in_hosteq(ia->ia_addr.sin_addr, 396 ifra->ifra_addr.sin_addr)) 397 break; 398 } 399 if ((cmd == SIOCDIFADDR || 400 cmd == SIOCGIFALIAS || 401 cmd == SIOCGIFAFLAG_IN) && 402 ia == NULL) 403 return (EADDRNOTAVAIL); 404 405 if (cmd == SIOCDIFADDR && 406 ifra->ifra_addr.sin_family == AF_UNSPEC) { 407 ifra->ifra_addr.sin_family = AF_INET; 408 } 409 /* FALLTHROUGH */ 410 case SIOCSIFADDR: 411 if (ia == NULL || ia->ia_addr.sin_family != AF_INET) 412 ; 413 else if (ifra->ifra_addr.sin_len == 0) { 414 ifra->ifra_addr = ia->ia_addr; 415 hostIsNew = 0; 416 } else if (in_hosteq(ia->ia_addr.sin_addr, 417 ifra->ifra_addr.sin_addr)) 418 hostIsNew = 0; 419 /* FALLTHROUGH */ 420 case SIOCSIFDSTADDR: 421 if (ifra->ifra_addr.sin_family != AF_INET) 422 return (EAFNOSUPPORT); 423 /* FALLTHROUGH */ 424 case SIOCSIFNETMASK: 425 if (ifp == NULL) 426 panic("in_control"); 427 428 if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN) 429 break; 430 431 if (ia == NULL && 432 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) 433 return (EADDRNOTAVAIL); 434 435 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 436 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 437 NULL) != 0) 438 return (EPERM); 439 440 if (ia == NULL) { 441 ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO); 442 if (ia == NULL) 443 return (ENOBUFS); 444 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list); 445 ifaref(&ia->ia_ifa); 446 ifa_insert(ifp, &ia->ia_ifa); 447 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 448 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 449 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 450 #ifdef IPSELSRC 451 ia->ia_ifa.ifa_getifa = in_getifa; 452 #else /* IPSELSRC */ 453 ia->ia_ifa.ifa_getifa = NULL; 454 #endif /* IPSELSRC */ 455 ia->ia_sockmask.sin_len = 8; 456 ia->ia_sockmask.sin_family = AF_INET; 457 if (ifp->if_flags & IFF_BROADCAST) { 458 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 459 ia->ia_broadaddr.sin_family = AF_INET; 460 } 461 ia->ia_ifp = ifp; 462 ia->ia_idsalt = cprng_fast32() % 65535; 463 LIST_INIT(&ia->ia_multiaddrs); 464 newifaddr = 1; 465 } 466 break; 467 468 case SIOCSIFBRDADDR: 469 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 470 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 471 NULL) != 0) 472 return (EPERM); 473 /* FALLTHROUGH */ 474 475 case SIOCGIFADDR: 476 case SIOCGIFNETMASK: 477 case SIOCGIFDSTADDR: 478 case SIOCGIFBRDADDR: 479 if (ia == NULL) 480 return (EADDRNOTAVAIL); 481 break; 482 } 483 error = 0; 484 switch (cmd) { 485 486 case SIOCGIFADDR: 487 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr)); 488 break; 489 490 case SIOCGIFBRDADDR: 491 if ((ifp->if_flags & IFF_BROADCAST) == 0) 492 return (EINVAL); 493 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr)); 494 break; 495 496 case SIOCGIFDSTADDR: 497 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 498 return (EINVAL); 499 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr)); 500 break; 501 502 case SIOCGIFNETMASK: 503 /* 504 * We keep the number of trailing zero bytes the sin_len field 505 * of ia_sockmask, so we fix this before we pass it back to 506 * userland. 507 */ 508 oldaddr = ia->ia_sockmask; 509 oldaddr.sin_len = sizeof(struct sockaddr_in); 510 ifreq_setaddr(cmd, ifr, (const void *)&oldaddr); 511 break; 512 513 case SIOCSIFDSTADDR: 514 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 515 return (EINVAL); 516 oldaddr = ia->ia_dstaddr; 517 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr)); 518 if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) { 519 ia->ia_dstaddr = oldaddr; 520 return error; 521 } 522 if (ia->ia_flags & IFA_ROUTE) { 523 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr); 524 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST); 525 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 526 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP); 527 } 528 break; 529 530 case SIOCSIFBRDADDR: 531 if ((ifp->if_flags & IFF_BROADCAST) == 0) 532 return EINVAL; 533 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr)); 534 break; 535 536 case SIOCSIFADDR: 537 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)), 538 1, hostIsNew); 539 if (error == 0) { 540 (void)pfil_run_hooks(if_pfil, 541 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR); 542 } 543 break; 544 545 case SIOCSIFNETMASK: 546 in_ifscrub(ifp, ia); 547 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr)); 548 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 549 error = in_ifinit(ifp, ia, NULL, 0, 0); 550 break; 551 552 case SIOCAIFADDR: 553 maskIsNew = 0; 554 if (ifra->ifra_mask.sin_len) { 555 /* Only scrub if we control the prefix route, 556 * otherwise userland gets a bogus message */ 557 if ((ia->ia_flags & IFA_ROUTE)) 558 in_ifscrub(ifp, ia); 559 ia->ia_sockmask = ifra->ifra_mask; 560 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 561 maskIsNew = 1; 562 } 563 if ((ifp->if_flags & IFF_POINTOPOINT) && 564 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 565 /* Only scrub if we control the prefix route, 566 * otherwise userland gets a bogus message */ 567 if ((ia->ia_flags & IFA_ROUTE)) 568 in_ifscrub(ifp, ia); 569 ia->ia_dstaddr = ifra->ifra_dstaddr; 570 maskIsNew = 1; /* We lie; but the effect's the same */ 571 } 572 if (ifra->ifra_addr.sin_family == AF_INET && 573 (hostIsNew || maskIsNew)) { 574 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0, 575 hostIsNew); 576 } 577 if ((ifp->if_flags & IFF_BROADCAST) && 578 (ifra->ifra_broadaddr.sin_family == AF_INET)) 579 ia->ia_broadaddr = ifra->ifra_broadaddr; 580 if (error == 0) 581 (void)pfil_run_hooks(if_pfil, 582 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR); 583 break; 584 585 case SIOCGIFALIAS: 586 ifra->ifra_mask = ia->ia_sockmask; 587 if ((ifp->if_flags & IFF_POINTOPOINT) && 588 (ia->ia_dstaddr.sin_family == AF_INET)) 589 ifra->ifra_dstaddr = ia->ia_dstaddr; 590 else if ((ifp->if_flags & IFF_BROADCAST) && 591 (ia->ia_broadaddr.sin_family == AF_INET)) 592 ifra->ifra_broadaddr = ia->ia_broadaddr; 593 else 594 memset(&ifra->ifra_broadaddr, 0, 595 sizeof(ifra->ifra_broadaddr)); 596 break; 597 598 case SIOCGIFAFLAG_IN: 599 ifr->ifr_addrflags = ia->ia4_flags; 600 break; 601 602 case SIOCDIFADDR: 603 in_purgeaddr(&ia->ia_ifa); 604 (void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR, 605 ifp, PFIL_IFADDR); 606 break; 607 608 #ifdef MROUTING 609 case SIOCGETVIFCNT: 610 case SIOCGETSGCNT: 611 error = mrt_ioctl(so, cmd, data); 612 break; 613 #endif /* MROUTING */ 614 615 default: 616 return ENOTTY; 617 } 618 619 if (error != 0 && newifaddr) { 620 KASSERT(ia != NULL); 621 in_purgeaddr(&ia->ia_ifa); 622 } 623 624 return error; 625 } 626 627 /* Add ownaddr as loopback rtentry. */ 628 static void 629 in_ifaddlocal(struct ifaddr *ifa) 630 { 631 struct in_ifaddr *ia; 632 633 ia = (struct in_ifaddr *)ifa; 634 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY || 635 (ia->ia_ifp->if_flags & IFF_POINTOPOINT && 636 in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr))) 637 { 638 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 639 return; 640 } 641 642 rt_ifa_addlocal(ifa); 643 } 644 645 /* Rempve loopback entry of ownaddr */ 646 static void 647 in_ifremlocal(struct ifaddr *ifa) 648 { 649 struct in_ifaddr *ia, *p; 650 struct ifaddr *alt_ifa = NULL; 651 int ia_count = 0; 652 653 ia = (struct in_ifaddr *)ifa; 654 /* Delete the entry if exactly one ifaddr matches the 655 * address, ifa->ifa_addr. */ 656 TAILQ_FOREACH(p, &in_ifaddrhead, ia_list) { 657 if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr)) 658 continue; 659 if (p->ia_ifp != ia->ia_ifp) 660 alt_ifa = &p->ia_ifa; 661 if (++ia_count > 1 && alt_ifa != NULL) 662 break; 663 } 664 665 if (ia_count == 0) 666 return; 667 668 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa); 669 } 670 671 void 672 in_purgeaddr(struct ifaddr *ifa) 673 { 674 struct ifnet *ifp = ifa->ifa_ifp; 675 struct in_ifaddr *ia = (void *) ifa; 676 677 /* stop DAD processing */ 678 if (ia->ia_dad_stop != NULL) 679 ia->ia_dad_stop(ifa); 680 681 in_ifscrub(ifp, ia); 682 in_ifremlocal(ifa); 683 LIST_REMOVE(ia, ia_hash); 684 ifa_remove(ifp, &ia->ia_ifa); 685 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list); 686 if (ia->ia_allhosts != NULL) 687 in_delmulti(ia->ia_allhosts); 688 ifafree(&ia->ia_ifa); 689 in_setmaxmtu(); 690 } 691 692 void 693 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */ 694 { 695 if_purgeaddrs(ifp, AF_INET, in_purgeaddr); 696 igmp_purgeif(ifp); /* manipulates pools */ 697 #ifdef MROUTING 698 ip_mrouter_detach(ifp); 699 #endif 700 } 701 702 /* 703 * SIOC[GAD]LIFADDR. 704 * SIOCGLIFADDR: get first address. (???) 705 * SIOCGLIFADDR with IFLR_PREFIX: 706 * get first address that matches the specified prefix. 707 * SIOCALIFADDR: add the specified address. 708 * SIOCALIFADDR with IFLR_PREFIX: 709 * EINVAL since we can't deduce hostid part of the address. 710 * SIOCDLIFADDR: delete the specified address. 711 * SIOCDLIFADDR with IFLR_PREFIX: 712 * delete the first address that matches the specified prefix. 713 * return values: 714 * EINVAL on invalid parameters 715 * EADDRNOTAVAIL on prefix match failed/specified address not found 716 * other values may be returned from in_ioctl() 717 */ 718 static int 719 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data, 720 struct ifnet *ifp) 721 { 722 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 723 struct ifaddr *ifa; 724 struct sockaddr *sa; 725 726 /* sanity checks */ 727 if (data == NULL || ifp == NULL) { 728 panic("invalid argument to in_lifaddr_ioctl"); 729 /*NOTRECHED*/ 730 } 731 732 switch (cmd) { 733 case SIOCGLIFADDR: 734 /* address must be specified on GET with IFLR_PREFIX */ 735 if ((iflr->flags & IFLR_PREFIX) == 0) 736 break; 737 /*FALLTHROUGH*/ 738 case SIOCALIFADDR: 739 case SIOCDLIFADDR: 740 /* address must be specified on ADD and DELETE */ 741 sa = (struct sockaddr *)&iflr->addr; 742 if (sa->sa_family != AF_INET) 743 return EINVAL; 744 if (sa->sa_len != sizeof(struct sockaddr_in)) 745 return EINVAL; 746 /* XXX need improvement */ 747 sa = (struct sockaddr *)&iflr->dstaddr; 748 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET) 749 return EINVAL; 750 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in)) 751 return EINVAL; 752 break; 753 default: /*shouldn't happen*/ 754 #if 0 755 panic("invalid cmd to in_lifaddr_ioctl"); 756 /*NOTREACHED*/ 757 #else 758 return EOPNOTSUPP; 759 #endif 760 } 761 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen) 762 return EINVAL; 763 764 switch (cmd) { 765 case SIOCALIFADDR: 766 { 767 struct in_aliasreq ifra; 768 769 if (iflr->flags & IFLR_PREFIX) 770 return EINVAL; 771 772 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 773 memset(&ifra, 0, sizeof(ifra)); 774 memcpy(ifra.ifra_name, iflr->iflr_name, 775 sizeof(ifra.ifra_name)); 776 777 memcpy(&ifra.ifra_addr, &iflr->addr, 778 ((struct sockaddr *)&iflr->addr)->sa_len); 779 780 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 781 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr, 782 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 783 } 784 785 ifra.ifra_mask.sin_family = AF_INET; 786 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 787 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 788 789 return in_control(so, SIOCAIFADDR, &ifra, ifp); 790 } 791 case SIOCGLIFADDR: 792 case SIOCDLIFADDR: 793 { 794 struct in_ifaddr *ia; 795 struct in_addr mask, candidate, match; 796 struct sockaddr_in *sin; 797 int cmp; 798 799 memset(&mask, 0, sizeof(mask)); 800 memset(&match, 0, sizeof(match)); /* XXX gcc */ 801 if (iflr->flags & IFLR_PREFIX) { 802 /* lookup a prefix rather than address. */ 803 in_len2mask(&mask, iflr->prefixlen); 804 805 sin = (struct sockaddr_in *)&iflr->addr; 806 match.s_addr = sin->sin_addr.s_addr; 807 match.s_addr &= mask.s_addr; 808 809 /* if you set extra bits, that's wrong */ 810 if (match.s_addr != sin->sin_addr.s_addr) 811 return EINVAL; 812 813 cmp = 1; 814 } else { 815 if (cmd == SIOCGLIFADDR) { 816 /* on getting an address, take the 1st match */ 817 cmp = 0; /*XXX*/ 818 } else { 819 /* on deleting an address, do exact match */ 820 in_len2mask(&mask, 32); 821 sin = (struct sockaddr_in *)&iflr->addr; 822 match.s_addr = sin->sin_addr.s_addr; 823 824 cmp = 1; 825 } 826 } 827 828 IFADDR_FOREACH(ifa, ifp) { 829 if (ifa->ifa_addr->sa_family != AF_INET) 830 continue; 831 if (cmp == 0) 832 break; 833 candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; 834 candidate.s_addr &= mask.s_addr; 835 if (candidate.s_addr == match.s_addr) 836 break; 837 } 838 if (ifa == NULL) 839 return EADDRNOTAVAIL; 840 ia = (struct in_ifaddr *)ifa; 841 842 if (cmd == SIOCGLIFADDR) { 843 /* fill in the if_laddrreq structure */ 844 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len); 845 846 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 847 memcpy(&iflr->dstaddr, &ia->ia_dstaddr, 848 ia->ia_dstaddr.sin_len); 849 } else 850 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr)); 851 852 iflr->prefixlen = 853 in_mask2len(&ia->ia_sockmask.sin_addr); 854 855 iflr->flags = 0; /*XXX*/ 856 857 return 0; 858 } else { 859 struct in_aliasreq ifra; 860 861 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 862 memset(&ifra, 0, sizeof(ifra)); 863 memcpy(ifra.ifra_name, iflr->iflr_name, 864 sizeof(ifra.ifra_name)); 865 866 memcpy(&ifra.ifra_addr, &ia->ia_addr, 867 ia->ia_addr.sin_len); 868 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 869 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr, 870 ia->ia_dstaddr.sin_len); 871 } 872 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask, 873 ia->ia_sockmask.sin_len); 874 875 return in_control(so, SIOCDIFADDR, &ifra, ifp); 876 } 877 } 878 } 879 880 return EOPNOTSUPP; /*just for safety*/ 881 } 882 883 /* 884 * Delete any existing route for an interface. 885 */ 886 void 887 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 888 { 889 890 in_scrubprefix(ia); 891 } 892 893 /* 894 * Initialize an interface's internet address 895 * and routing table entry. 896 */ 897 int 898 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, 899 const struct sockaddr_in *sin, int scrub, int hostIsNew) 900 { 901 u_int32_t i; 902 struct sockaddr_in oldaddr; 903 int s = splnet(), flags = RTF_UP, error; 904 905 if (sin == NULL) 906 sin = &ia->ia_addr; 907 908 /* 909 * Set up new addresses. 910 */ 911 oldaddr = ia->ia_addr; 912 if (ia->ia_addr.sin_family == AF_INET) 913 LIST_REMOVE(ia, ia_hash); 914 ia->ia_addr = *sin; 915 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 916 917 /* Set IN_IFF flags early for if_addr_init() */ 918 if (hostIsNew && if_do_dad(ifp) && !in_nullhost(ia->ia_addr.sin_addr)) { 919 if (ifp->if_link_state == LINK_STATE_DOWN) 920 ia->ia4_flags |= IN_IFF_DETACHED; 921 else 922 /* State the intent to try DAD if possible */ 923 ia->ia4_flags |= IN_IFF_TRYTENTATIVE; 924 } 925 926 /* 927 * Give the interface a chance to initialize 928 * if this is its first address, 929 * and to validate the address if necessary. 930 */ 931 if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0) 932 goto bad; 933 /* Now clear the try tentative flag, it's job is done. */ 934 ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE; 935 splx(s); 936 937 if (scrub) { 938 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 939 in_ifscrub(ifp, ia); 940 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 941 } 942 943 /* Add the local route to the address */ 944 in_ifaddlocal(&ia->ia_ifa); 945 946 i = ia->ia_addr.sin_addr.s_addr; 947 if (IN_CLASSA(i)) 948 ia->ia_netmask = IN_CLASSA_NET; 949 else if (IN_CLASSB(i)) 950 ia->ia_netmask = IN_CLASSB_NET; 951 else 952 ia->ia_netmask = IN_CLASSC_NET; 953 /* 954 * The subnet mask usually includes at least the standard network part, 955 * but may may be smaller in the case of supernetting. 956 * If it is set, we believe it. 957 */ 958 if (ia->ia_subnetmask == 0) { 959 ia->ia_subnetmask = ia->ia_netmask; 960 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 961 } else 962 ia->ia_netmask &= ia->ia_subnetmask; 963 964 ia->ia_net = i & ia->ia_netmask; 965 ia->ia_subnet = i & ia->ia_subnetmask; 966 in_socktrim(&ia->ia_sockmask); 967 /* re-calculate the "in_maxmtu" value */ 968 in_setmaxmtu(); 969 /* 970 * Add route for the network. 971 */ 972 ia->ia_ifa.ifa_metric = ifp->if_metric; 973 if (ifp->if_flags & IFF_BROADCAST) { 974 ia->ia_broadaddr.sin_addr.s_addr = 975 ia->ia_subnet | ~ia->ia_subnetmask; 976 ia->ia_netbroadcast.s_addr = 977 ia->ia_net | ~ia->ia_netmask; 978 } else if (ifp->if_flags & IFF_LOOPBACK) { 979 ia->ia_dstaddr = ia->ia_addr; 980 flags |= RTF_HOST; 981 } else if (ifp->if_flags & IFF_POINTOPOINT) { 982 if (ia->ia_dstaddr.sin_family != AF_INET) 983 return (0); 984 flags |= RTF_HOST; 985 } 986 error = in_addprefix(ia, flags); 987 /* 988 * If the interface supports multicast, join the "all hosts" 989 * multicast group on that interface. 990 */ 991 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 992 struct in_addr addr; 993 994 addr.s_addr = INADDR_ALLHOSTS_GROUP; 995 ia->ia_allhosts = in_addmulti(&addr, ifp); 996 } 997 998 if (hostIsNew && if_do_dad(ifp) && 999 !in_nullhost(ia->ia_addr.sin_addr) && 1000 ia->ia4_flags & IN_IFF_TENTATIVE) 1001 ia->ia_dad_start((struct ifaddr *)ia); 1002 1003 return (error); 1004 bad: 1005 splx(s); 1006 LIST_REMOVE(ia, ia_hash); 1007 ia->ia_addr = oldaddr; 1008 if (ia->ia_addr.sin_family == AF_INET) 1009 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), 1010 ia, ia_hash); 1011 return (error); 1012 } 1013 1014 #define rtinitflags(x) \ 1015 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 1016 ? RTF_HOST : 0) 1017 1018 /* 1019 * add a route to prefix ("connected route" in cisco terminology). 1020 * does nothing if there's some interface address with the same prefix already. 1021 */ 1022 static int 1023 in_addprefix(struct in_ifaddr *target, int flags) 1024 { 1025 struct in_ifaddr *ia; 1026 struct in_addr prefix, mask, p; 1027 int error; 1028 1029 if ((flags & RTF_HOST) != 0) 1030 prefix = target->ia_dstaddr.sin_addr; 1031 else { 1032 prefix = target->ia_addr.sin_addr; 1033 mask = target->ia_sockmask.sin_addr; 1034 prefix.s_addr &= mask.s_addr; 1035 } 1036 1037 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 1038 if (rtinitflags(ia)) 1039 p = ia->ia_dstaddr.sin_addr; 1040 else { 1041 p = ia->ia_addr.sin_addr; 1042 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1043 } 1044 1045 if (prefix.s_addr != p.s_addr) 1046 continue; 1047 1048 /* 1049 * if we got a matching prefix route inserted by other 1050 * interface address, we don't need to bother 1051 * 1052 * XXX RADIX_MPATH implications here? -dyoung 1053 */ 1054 if (ia->ia_flags & IFA_ROUTE) 1055 return 0; 1056 } 1057 1058 /* 1059 * noone seem to have prefix route. insert it. 1060 */ 1061 error = rtinit(&target->ia_ifa, RTM_ADD, flags); 1062 if (error == 0) 1063 target->ia_flags |= IFA_ROUTE; 1064 else if (error == EEXIST) { 1065 /* 1066 * the fact the route already exists is not an error. 1067 */ 1068 error = 0; 1069 } 1070 return error; 1071 } 1072 1073 /* 1074 * remove a route to prefix ("connected route" in cisco terminology). 1075 * re-installs the route by using another interface address, if there's one 1076 * with the same prefix (otherwise we lose the route mistakenly). 1077 */ 1078 static int 1079 in_scrubprefix(struct in_ifaddr *target) 1080 { 1081 struct in_ifaddr *ia; 1082 struct in_addr prefix, mask, p; 1083 int error; 1084 1085 /* If we don't have IFA_ROUTE we should still inform userland */ 1086 if ((target->ia_flags & IFA_ROUTE) == 0) 1087 return 0; 1088 1089 if (rtinitflags(target)) 1090 prefix = target->ia_dstaddr.sin_addr; 1091 else { 1092 prefix = target->ia_addr.sin_addr; 1093 mask = target->ia_sockmask.sin_addr; 1094 prefix.s_addr &= mask.s_addr; 1095 } 1096 1097 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 1098 if (rtinitflags(ia)) 1099 p = ia->ia_dstaddr.sin_addr; 1100 else { 1101 p = ia->ia_addr.sin_addr; 1102 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1103 } 1104 1105 if (prefix.s_addr != p.s_addr) 1106 continue; 1107 1108 /* 1109 * if we got a matching prefix route, move IFA_ROUTE to him 1110 */ 1111 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1112 rtinit(&target->ia_ifa, RTM_DELETE, 1113 rtinitflags(target)); 1114 target->ia_flags &= ~IFA_ROUTE; 1115 1116 error = rtinit(&ia->ia_ifa, RTM_ADD, 1117 rtinitflags(ia) | RTF_UP); 1118 if (error == 0) 1119 ia->ia_flags |= IFA_ROUTE; 1120 return error; 1121 } 1122 } 1123 1124 /* 1125 * noone seem to have prefix route. remove it. 1126 */ 1127 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target)); 1128 target->ia_flags &= ~IFA_ROUTE; 1129 return 0; 1130 } 1131 1132 #undef rtinitflags 1133 1134 /* 1135 * Return 1 if the address might be a local broadcast address. 1136 */ 1137 int 1138 in_broadcast(struct in_addr in, struct ifnet *ifp) 1139 { 1140 struct ifaddr *ifa; 1141 1142 if (in.s_addr == INADDR_BROADCAST || 1143 in_nullhost(in)) 1144 return 1; 1145 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1146 return 0; 1147 /* 1148 * Look through the list of addresses for a match 1149 * with a broadcast address. 1150 */ 1151 #define ia (ifatoia(ifa)) 1152 IFADDR_FOREACH(ifa, ifp) 1153 if (ifa->ifa_addr->sa_family == AF_INET && 1154 !in_hosteq(in, ia->ia_addr.sin_addr) && 1155 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 1156 in_hosteq(in, ia->ia_netbroadcast) || 1157 (hostzeroisbroadcast && 1158 /* 1159 * Check for old-style (host 0) broadcast. 1160 */ 1161 (in.s_addr == ia->ia_subnet || 1162 in.s_addr == ia->ia_net)))) 1163 return 1; 1164 return (0); 1165 #undef ia 1166 } 1167 1168 /* 1169 * perform DAD when interface becomes IFF_UP. 1170 */ 1171 void 1172 in_if_link_up(struct ifnet *ifp) 1173 { 1174 struct ifaddr *ifa; 1175 struct in_ifaddr *ia; 1176 1177 /* Ensure it's sane to run DAD */ 1178 if (ifp->if_link_state == LINK_STATE_DOWN) 1179 return; 1180 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 1181 return; 1182 1183 IFADDR_FOREACH(ifa, ifp) { 1184 if (ifa->ifa_addr->sa_family != AF_INET) 1185 continue; 1186 ia = (struct in_ifaddr *)ifa; 1187 1188 /* If detached then mark as tentative */ 1189 if (ia->ia4_flags & IN_IFF_DETACHED) { 1190 ia->ia4_flags &= ~IN_IFF_DETACHED; 1191 if (if_do_dad(ifp) && ia->ia_dad_start != NULL) 1192 ia->ia4_flags |= IN_IFF_TENTATIVE; 1193 else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0) 1194 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 1195 } 1196 1197 if (ia->ia4_flags & IN_IFF_TENTATIVE) { 1198 /* Clear the duplicated flag as we're starting DAD. */ 1199 ia->ia4_flags &= ~IN_IFF_DUPLICATED; 1200 ia->ia_dad_start(ifa); 1201 } 1202 } 1203 } 1204 1205 void 1206 in_if_up(struct ifnet *ifp) 1207 { 1208 1209 /* interface may not support link state, so bring it up also */ 1210 in_if_link_up(ifp); 1211 } 1212 1213 /* 1214 * Mark all addresses as detached. 1215 */ 1216 void 1217 in_if_link_down(struct ifnet *ifp) 1218 { 1219 struct ifaddr *ifa; 1220 struct in_ifaddr *ia; 1221 1222 IFADDR_FOREACH(ifa, ifp) { 1223 if (ifa->ifa_addr->sa_family != AF_INET) 1224 continue; 1225 ia = (struct in_ifaddr *)ifa; 1226 1227 /* Stop DAD processing */ 1228 if (ia->ia_dad_stop != NULL) 1229 ia->ia_dad_stop(ifa); 1230 1231 /* 1232 * Mark the address as detached. 1233 */ 1234 if (!(ia->ia4_flags & IN_IFF_DETACHED)) { 1235 ia->ia4_flags |= IN_IFF_DETACHED; 1236 ia->ia4_flags &= 1237 ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED); 1238 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 1239 } 1240 } 1241 } 1242 1243 void 1244 in_if_down(struct ifnet *ifp) 1245 { 1246 1247 in_if_link_down(ifp); 1248 } 1249 1250 void 1251 in_if_link_state_change(struct ifnet *ifp, int link_state) 1252 { 1253 1254 switch (link_state) { 1255 case LINK_STATE_DOWN: 1256 in_if_link_down(ifp); 1257 break; 1258 case LINK_STATE_UP: 1259 in_if_link_up(ifp); 1260 break; 1261 } 1262 } 1263 1264 /* 1265 * in_lookup_multi: look up the in_multi record for a given IP 1266 * multicast address on a given interface. If no matching record is 1267 * found, return NULL. 1268 */ 1269 struct in_multi * 1270 in_lookup_multi(struct in_addr addr, ifnet_t *ifp) 1271 { 1272 struct in_multi *inm; 1273 1274 KASSERT(rw_lock_held(&in_multilock)); 1275 1276 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) { 1277 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp) 1278 break; 1279 } 1280 return inm; 1281 } 1282 1283 /* 1284 * in_multi_group: check whether the address belongs to an IP multicast 1285 * group we are joined on this interface. Returns true or false. 1286 */ 1287 bool 1288 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags) 1289 { 1290 bool ingroup; 1291 1292 if (__predict_true(flags & IP_IGMP_MCAST) == 0) { 1293 rw_enter(&in_multilock, RW_READER); 1294 ingroup = in_lookup_multi(addr, ifp) != NULL; 1295 rw_exit(&in_multilock); 1296 } else { 1297 /* XXX Recursive call from ip_output(). */ 1298 KASSERT(rw_lock_held(&in_multilock)); 1299 ingroup = in_lookup_multi(addr, ifp) != NULL; 1300 } 1301 return ingroup; 1302 } 1303 1304 /* 1305 * Add an address to the list of IP multicast addresses for a given interface. 1306 */ 1307 struct in_multi * 1308 in_addmulti(struct in_addr *ap, ifnet_t *ifp) 1309 { 1310 struct sockaddr_in sin; 1311 struct in_multi *inm; 1312 1313 /* 1314 * See if address already in list. 1315 */ 1316 rw_enter(&in_multilock, RW_WRITER); 1317 inm = in_lookup_multi(*ap, ifp); 1318 if (inm != NULL) { 1319 /* 1320 * Found it; just increment the reference count. 1321 */ 1322 inm->inm_refcount++; 1323 rw_exit(&in_multilock); 1324 return inm; 1325 } 1326 1327 /* 1328 * New address; allocate a new multicast record. 1329 */ 1330 inm = pool_get(&inmulti_pool, PR_NOWAIT); 1331 if (inm == NULL) { 1332 rw_exit(&in_multilock); 1333 return NULL; 1334 } 1335 inm->inm_addr = *ap; 1336 inm->inm_ifp = ifp; 1337 inm->inm_refcount = 1; 1338 1339 /* 1340 * Ask the network driver to update its multicast reception 1341 * filter appropriately for the new address. 1342 */ 1343 sockaddr_in_init(&sin, ap, 0); 1344 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) { 1345 rw_exit(&in_multilock); 1346 pool_put(&inmulti_pool, inm); 1347 return NULL; 1348 } 1349 1350 /* 1351 * Let IGMP know that we have joined a new IP multicast group. 1352 */ 1353 if (igmp_joingroup(inm) != 0) { 1354 rw_exit(&in_multilock); 1355 pool_put(&inmulti_pool, inm); 1356 return NULL; 1357 } 1358 LIST_INSERT_HEAD( 1359 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp), 1360 inm, inm_list); 1361 in_multientries++; 1362 rw_exit(&in_multilock); 1363 1364 return inm; 1365 } 1366 1367 /* 1368 * Delete a multicast address record. 1369 */ 1370 void 1371 in_delmulti(struct in_multi *inm) 1372 { 1373 struct sockaddr_in sin; 1374 1375 rw_enter(&in_multilock, RW_WRITER); 1376 if (--inm->inm_refcount > 0) { 1377 rw_exit(&in_multilock); 1378 return; 1379 } 1380 1381 /* 1382 * No remaining claims to this record; let IGMP know that 1383 * we are leaving the multicast group. 1384 */ 1385 igmp_leavegroup(inm); 1386 1387 /* 1388 * Notify the network driver to update its multicast reception 1389 * filter. 1390 */ 1391 sockaddr_in_init(&sin, &inm->inm_addr, 0); 1392 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin)); 1393 1394 /* 1395 * Unlink from list. 1396 */ 1397 LIST_REMOVE(inm, inm_list); 1398 in_multientries--; 1399 rw_exit(&in_multilock); 1400 1401 pool_put(&inmulti_pool, inm); 1402 } 1403 1404 /* 1405 * in_next_multi: step through all of the in_multi records, one at a time. 1406 * The current position is remembered in "step", which the caller must 1407 * provide. in_first_multi(), below, must be called to initialize "step" 1408 * and get the first record. Both macros return a NULL "inm" when there 1409 * are no remaining records. 1410 */ 1411 struct in_multi * 1412 in_next_multi(struct in_multistep *step) 1413 { 1414 struct in_multi *inm; 1415 1416 KASSERT(rw_lock_held(&in_multilock)); 1417 1418 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) { 1419 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]); 1420 } 1421 if ((inm = step->i_inm) != NULL) { 1422 step->i_inm = LIST_NEXT(inm, inm_list); 1423 } 1424 return inm; 1425 } 1426 1427 struct in_multi * 1428 in_first_multi(struct in_multistep *step) 1429 { 1430 KASSERT(rw_lock_held(&in_multilock)); 1431 1432 step->i_n = 0; 1433 step->i_inm = LIST_FIRST(&in_multihashtbl[0]); 1434 return in_next_multi(step); 1435 } 1436 1437 void 1438 in_multi_lock(int op) 1439 { 1440 rw_enter(&in_multilock, op); 1441 } 1442 1443 void 1444 in_multi_unlock(void) 1445 { 1446 rw_exit(&in_multilock); 1447 } 1448 1449 int 1450 in_multi_lock_held(void) 1451 { 1452 return rw_lock_held(&in_multilock); 1453 } 1454 1455 struct sockaddr_in * 1456 in_selectsrc(struct sockaddr_in *sin, struct route *ro, 1457 int soopts, struct ip_moptions *mopts, int *errorp) 1458 { 1459 struct rtentry *rt = NULL; 1460 struct in_ifaddr *ia = NULL; 1461 1462 /* 1463 * If route is known or can be allocated now, take the 1464 * source address from the interface. Otherwise, punt. 1465 */ 1466 if ((soopts & SO_DONTROUTE) != 0) 1467 rtcache_free(ro); 1468 else { 1469 union { 1470 struct sockaddr dst; 1471 struct sockaddr_in dst4; 1472 } u; 1473 1474 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0); 1475 rt = rtcache_lookup(ro, &u.dst); 1476 } 1477 /* 1478 * If we found a route, use the address 1479 * corresponding to the outgoing interface 1480 * unless it is the loopback (in case a route 1481 * to our address on another net goes to loopback). 1482 * 1483 * XXX Is this still true? Do we care? 1484 */ 1485 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) 1486 ia = ifatoia(rt->rt_ifa); 1487 if (ia == NULL) { 1488 u_int16_t fport = sin->sin_port; 1489 1490 sin->sin_port = 0; 1491 ia = ifatoia(ifa_ifwithladdr(sintosa(sin))); 1492 sin->sin_port = fport; 1493 if (ia == NULL) { 1494 /* Find 1st non-loopback AF_INET address */ 1495 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 1496 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) 1497 break; 1498 } 1499 } 1500 if (ia == NULL) { 1501 *errorp = EADDRNOTAVAIL; 1502 return NULL; 1503 } 1504 } 1505 /* 1506 * If the destination address is multicast and an outgoing 1507 * interface has been set as a multicast option, use the 1508 * address of that interface as our source address. 1509 */ 1510 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 1511 struct ip_moptions *imo; 1512 struct ifnet *ifp; 1513 1514 imo = mopts; 1515 if (imo->imo_multicast_ifp != NULL) { 1516 ifp = imo->imo_multicast_ifp; 1517 IFP_TO_IA(ifp, ia); /* XXX */ 1518 if (ia == 0 || ia->ia4_flags & IN_IFF_NOTREADY) { 1519 *errorp = EADDRNOTAVAIL; 1520 return NULL; 1521 } 1522 } 1523 } 1524 if (ia->ia_ifa.ifa_getifa != NULL) { 1525 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa, 1526 sintosa(sin))); 1527 if (ia == NULL) { 1528 *errorp = EADDRNOTAVAIL; 1529 return NULL; 1530 } 1531 } 1532 #ifdef GETIFA_DEBUG 1533 else 1534 printf("%s: missing ifa_getifa\n", __func__); 1535 #endif 1536 return satosin(&ia->ia_addr); 1537 } 1538 1539 #if NETHER > 0 1540 1541 struct in_llentry { 1542 struct llentry base; 1543 }; 1544 1545 #define IN_LLTBL_DEFAULT_HSIZE 32 1546 #define IN_LLTBL_HASH(k, h) \ 1547 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 1548 1549 /* 1550 * Do actual deallocation of @lle. 1551 * Called by LLE_FREE_LOCKED when number of references 1552 * drops to zero. 1553 */ 1554 static void 1555 in_lltable_destroy_lle(struct llentry *lle) 1556 { 1557 1558 LLE_WUNLOCK(lle); 1559 LLE_LOCK_DESTROY(lle); 1560 kmem_intr_free(lle, sizeof(*lle)); 1561 } 1562 1563 static struct llentry * 1564 in_lltable_new(struct in_addr addr4, u_int flags) 1565 { 1566 struct in_llentry *lle; 1567 1568 lle = kmem_intr_zalloc(sizeof(*lle), KM_NOSLEEP); 1569 if (lle == NULL) /* NB: caller generates msg */ 1570 return NULL; 1571 1572 /* 1573 * For IPv4 this will trigger "arpresolve" to generate 1574 * an ARP request. 1575 */ 1576 lle->base.la_expire = time_uptime; /* mark expired */ 1577 lle->base.r_l3addr.addr4 = addr4; 1578 lle->base.lle_refcnt = 1; 1579 lle->base.lle_free = in_lltable_destroy_lle; 1580 LLE_LOCK_INIT(&lle->base); 1581 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 1582 1583 return (&lle->base); 1584 } 1585 1586 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 1587 (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 1588 1589 static int 1590 in_lltable_match_prefix(const struct sockaddr *prefix, 1591 const struct sockaddr *mask, u_int flags, struct llentry *lle) 1592 { 1593 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1594 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1595 1596 /* 1597 * (flags & LLE_STATIC) means deleting all entries 1598 * including static ARP entries. 1599 */ 1600 if (IN_ARE_MASKED_ADDR_EQUAL(lle->r_l3addr.addr4, pfx, msk) && 1601 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) 1602 return (1); 1603 1604 return (0); 1605 } 1606 1607 static void 1608 in_lltable_free_entry(struct lltable *llt, struct llentry *lle) 1609 { 1610 struct ifnet *ifp __diagused; 1611 size_t pkts_dropped; 1612 1613 LLE_WLOCK_ASSERT(lle); 1614 KASSERT(llt != NULL); 1615 1616 /* Unlink entry from table if not already */ 1617 if ((lle->la_flags & LLE_LINKED) != 0) { 1618 ifp = llt->llt_ifp; 1619 IF_AFDATA_WLOCK_ASSERT(ifp); 1620 lltable_unlink_entry(llt, lle); 1621 } 1622 1623 /* cancel timer */ 1624 if (callout_stop(&lle->lle_timer)) 1625 LLE_REMREF(lle); 1626 1627 /* Drop hold queue */ 1628 pkts_dropped = llentry_free(lle); 1629 arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped); 1630 } 1631 1632 static int 1633 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 1634 { 1635 struct rtentry *rt; 1636 int error = EINVAL; 1637 1638 KASSERTMSG(l3addr->sa_family == AF_INET, 1639 "sin_family %d", l3addr->sa_family); 1640 1641 rt = rtalloc1(l3addr, 0); 1642 if (rt == NULL) 1643 return error; 1644 1645 /* 1646 * If the gateway for an existing host route matches the target L3 1647 * address, which is a special route inserted by some implementation 1648 * such as MANET, and the interface is of the correct type, then 1649 * allow for ARP to proceed. 1650 */ 1651 if (rt->rt_flags & RTF_GATEWAY) { 1652 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 1653 rt->rt_ifp->if_type != IFT_ETHER || 1654 #ifdef __FreeBSD__ 1655 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 1656 #else 1657 (rt->rt_ifp->if_flags & IFF_NOARP) != 0 || 1658 #endif 1659 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 1660 sizeof(in_addr_t)) != 0) { 1661 goto error; 1662 } 1663 } 1664 1665 /* 1666 * Make sure that at least the destination address is covered 1667 * by the route. This is for handling the case where 2 or more 1668 * interfaces have the same prefix. An incoming packet arrives 1669 * on one interface and the corresponding outgoing packet leaves 1670 * another interface. 1671 */ 1672 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 1673 const char *sa, *mask, *addr, *lim; 1674 int len; 1675 1676 mask = (const char *)rt_mask(rt); 1677 /* 1678 * Just being extra cautious to avoid some custom 1679 * code getting into trouble. 1680 */ 1681 if (mask == NULL) 1682 goto error; 1683 1684 sa = (const char *)rt_getkey(rt); 1685 addr = (const char *)l3addr; 1686 len = ((const struct sockaddr_in *)l3addr)->sin_len; 1687 lim = addr + len; 1688 1689 for ( ; addr < lim; sa++, mask++, addr++) { 1690 if ((*sa ^ *addr) & *mask) { 1691 #ifdef DIAGNOSTIC 1692 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1693 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1694 #endif 1695 goto error; 1696 } 1697 } 1698 } 1699 1700 error = 0; 1701 error: 1702 return error; 1703 } 1704 1705 static inline uint32_t 1706 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize) 1707 { 1708 1709 return (IN_LLTBL_HASH(dst.s_addr, hsize)); 1710 } 1711 1712 static uint32_t 1713 in_lltable_hash(const struct llentry *lle, uint32_t hsize) 1714 { 1715 1716 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize)); 1717 } 1718 1719 static void 1720 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 1721 { 1722 struct sockaddr_in *sin; 1723 1724 sin = (struct sockaddr_in *)sa; 1725 memset(sin, 0, sizeof(*sin)); 1726 sin->sin_family = AF_INET; 1727 sin->sin_len = sizeof(*sin); 1728 sin->sin_addr = lle->r_l3addr.addr4; 1729 } 1730 1731 static inline struct llentry * 1732 in_lltable_find_dst(struct lltable *llt, struct in_addr dst) 1733 { 1734 struct llentry *lle; 1735 struct llentries *lleh; 1736 u_int hashidx; 1737 1738 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize); 1739 lleh = &llt->lle_head[hashidx]; 1740 LIST_FOREACH(lle, lleh, lle_next) { 1741 if (lle->la_flags & LLE_DELETED) 1742 continue; 1743 if (lle->r_l3addr.addr4.s_addr == dst.s_addr) 1744 break; 1745 } 1746 1747 return (lle); 1748 } 1749 1750 static int 1751 in_lltable_delete(struct lltable *llt, u_int flags, 1752 const struct sockaddr *l3addr) 1753 { 1754 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1755 struct ifnet *ifp __diagused = llt->llt_ifp; 1756 struct llentry *lle; 1757 1758 IF_AFDATA_WLOCK_ASSERT(ifp); 1759 KASSERTMSG(l3addr->sa_family == AF_INET, 1760 "sin_family %d", l3addr->sa_family); 1761 1762 lle = in_lltable_find_dst(llt, sin->sin_addr); 1763 if (lle == NULL) { 1764 #ifdef DIAGNOSTIC 1765 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1766 #endif 1767 return (ENOENT); 1768 } 1769 1770 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1771 LLE_WLOCK(lle); 1772 lle->la_flags |= LLE_DELETED; 1773 #ifdef DIAGNOSTIC 1774 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1775 #endif 1776 if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 1777 llentry_free(lle); 1778 else 1779 LLE_WUNLOCK(lle); 1780 } 1781 1782 return (0); 1783 } 1784 1785 static struct llentry * 1786 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1787 { 1788 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1789 struct ifnet *ifp = llt->llt_ifp; 1790 struct llentry *lle; 1791 1792 IF_AFDATA_WLOCK_ASSERT(ifp); 1793 KASSERTMSG(l3addr->sa_family == AF_INET, 1794 "sin_family %d", l3addr->sa_family); 1795 1796 lle = in_lltable_find_dst(llt, sin->sin_addr); 1797 1798 if (lle != NULL) { 1799 LLE_WLOCK(lle); 1800 return (lle); 1801 } 1802 1803 /* no existing record, we need to create new one */ 1804 1805 /* 1806 * A route that covers the given address must have 1807 * been installed 1st because we are doing a resolution, 1808 * verify this. 1809 */ 1810 if (!(flags & LLE_IFADDR) && 1811 in_lltable_rtcheck(ifp, flags, l3addr) != 0) 1812 return (NULL); 1813 1814 lle = in_lltable_new(sin->sin_addr, flags); 1815 if (lle == NULL) { 1816 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1817 return (NULL); 1818 } 1819 lle->la_flags = flags; 1820 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 1821 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen); 1822 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1823 } 1824 1825 lltable_link_entry(llt, lle); 1826 LLE_WLOCK(lle); 1827 1828 return (lle); 1829 } 1830 1831 /* 1832 * Return NULL if not found or marked for deletion. 1833 * If found return lle read locked. 1834 */ 1835 static struct llentry * 1836 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1837 { 1838 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1839 struct llentry *lle; 1840 1841 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); 1842 KASSERTMSG(l3addr->sa_family == AF_INET, 1843 "sin_family %d", l3addr->sa_family); 1844 1845 lle = in_lltable_find_dst(llt, sin->sin_addr); 1846 1847 if (lle == NULL) 1848 return NULL; 1849 1850 if (flags & LLE_EXCLUSIVE) 1851 LLE_WLOCK(lle); 1852 else 1853 LLE_RLOCK(lle); 1854 1855 return lle; 1856 } 1857 1858 #endif /* NETHER > 0 */ 1859 1860 static void 1861 in_sysctl_init(struct sysctllog **clog) 1862 { 1863 sysctl_createv(clog, 0, NULL, NULL, 1864 CTLFLAG_PERMANENT, 1865 CTLTYPE_NODE, "inet", 1866 SYSCTL_DESCR("PF_INET related settings"), 1867 NULL, 0, NULL, 0, 1868 CTL_NET, PF_INET, CTL_EOL); 1869 sysctl_createv(clog, 0, NULL, NULL, 1870 CTLFLAG_PERMANENT, 1871 CTLTYPE_NODE, "ip", 1872 SYSCTL_DESCR("IPv4 related settings"), 1873 NULL, 0, NULL, 0, 1874 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL); 1875 1876 sysctl_createv(clog, 0, NULL, NULL, 1877 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1878 CTLTYPE_INT, "subnetsarelocal", 1879 SYSCTL_DESCR("Whether logical subnets are considered " 1880 "local"), 1881 NULL, 0, &subnetsarelocal, 0, 1882 CTL_NET, PF_INET, IPPROTO_IP, 1883 IPCTL_SUBNETSARELOCAL, CTL_EOL); 1884 sysctl_createv(clog, 0, NULL, NULL, 1885 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1886 CTLTYPE_INT, "hostzerobroadcast", 1887 SYSCTL_DESCR("All zeroes address is broadcast address"), 1888 NULL, 0, &hostzeroisbroadcast, 0, 1889 CTL_NET, PF_INET, IPPROTO_IP, 1890 IPCTL_HOSTZEROBROADCAST, CTL_EOL); 1891 } 1892 1893 #if NETHER > 0 1894 static struct lltable * 1895 in_lltattach(struct ifnet *ifp) 1896 { 1897 struct lltable *llt; 1898 1899 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE); 1900 llt->llt_af = AF_INET; 1901 llt->llt_ifp = ifp; 1902 1903 llt->llt_lookup = in_lltable_lookup; 1904 llt->llt_create = in_lltable_create; 1905 llt->llt_delete = in_lltable_delete; 1906 #if 0 1907 llt->llt_dump_entry = in_lltable_dump_entry; 1908 #endif 1909 llt->llt_hash = in_lltable_hash; 1910 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry; 1911 llt->llt_free_entry = in_lltable_free_entry; 1912 llt->llt_match_prefix = in_lltable_match_prefix; 1913 lltable_link(llt); 1914 1915 return (llt); 1916 } 1917 #endif /* NETHER > 0 */ 1918 1919 void * 1920 in_domifattach(struct ifnet *ifp) 1921 { 1922 struct in_ifinfo *ii; 1923 1924 ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP); 1925 KASSERT(ii != NULL); 1926 1927 #if NETHER > 0 1928 ii->ii_llt = in_lltattach(ifp); 1929 #endif 1930 1931 #ifdef IPSELSRC 1932 ii->ii_selsrc = in_selsrc_domifattach(ifp); 1933 KASSERT(ii->ii_selsrc != NULL); 1934 #endif 1935 1936 return ii; 1937 } 1938 1939 void 1940 in_domifdetach(struct ifnet *ifp, void *aux) 1941 { 1942 struct in_ifinfo *ii = aux; 1943 1944 #ifdef IPSELSRC 1945 in_selsrc_domifdetach(ifp, ii->ii_selsrc); 1946 #endif 1947 #if NETHER > 0 1948 lltable_free(ii->ii_llt); 1949 #endif 1950 kmem_free(ii, sizeof(struct in_ifinfo)); 1951 } 1952