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