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