jcs's openbsd hax
openbsd
at jcs 1006 lines 24 kB view raw
1/* $OpenBSD: in.c,v 1.192 2026/01/03 14:10:04 bluhm Exp $ */ 2/* $NetBSD: in.c,v 1.26 1996/02/13 23:41:39 christos Exp $ */ 3 4/* 5 * Copyright (C) 2001 WIDE Project. 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) 1982, 1986, 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)in.c 8.2 (Berkeley) 11/15/93 61 */ 62 63#include <sys/param.h> 64#include <sys/systm.h> 65#include <sys/ioctl.h> 66#include <sys/malloc.h> 67#include <sys/socket.h> 68#include <sys/socketvar.h> 69 70#include <net/if.h> 71#include <net/if_var.h> 72#include <net/route.h> 73 74#include <netinet/in.h> 75#include <netinet/in_var.h> 76#include <netinet/igmp_var.h> 77 78#ifdef MROUTING 79#include <netinet/ip_mroute.h> 80#endif 81 82void in_socktrim(struct sockaddr_in *); 83 84int in_ioctl_set_ifaddr(u_long, caddr_t, struct ifnet *); 85int in_ioctl_change_ifaddr(u_long, caddr_t, struct ifnet *); 86int in_ioctl_get(u_long, caddr_t, struct ifnet *); 87void in_purgeaddr(struct ifaddr *); 88int in_addhost(struct in_ifaddr *, struct sockaddr_in *); 89int in_scrubhost(struct in_ifaddr *, struct sockaddr_in *); 90int in_insert_prefix(struct in_ifaddr *); 91void in_remove_prefix(struct in_ifaddr *); 92 93/* 94 * Determine whether an IP address is in a reserved set of addresses 95 * that may not be forwarded, or whether datagrams to that destination 96 * may be forwarded. 97 */ 98int 99in_canforward(struct in_addr in) 100{ 101 u_int32_t net; 102 103 if (IN_MULTICAST(in.s_addr)) 104 return (0); 105 if (IN_CLASSA(in.s_addr)) { 106 net = in.s_addr & IN_CLASSA_NET; 107 if (net == 0 || 108 net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 109 return (0); 110 } 111 return (1); 112} 113 114/* 115 * Trim a mask in a sockaddr 116 */ 117void 118in_socktrim(struct sockaddr_in *ap) 119{ 120 char *cplim = (char *) &ap->sin_addr; 121 char *cp = (char *) (&ap->sin_addr + 1); 122 123 ap->sin_len = 0; 124 while (--cp >= cplim) 125 if (*cp) { 126 (ap)->sin_len = cp - (char *) (ap) + 1; 127 break; 128 } 129} 130 131int 132in_mask2len(struct in_addr *mask) 133{ 134 int x, y; 135 u_char *p; 136 137 p = (u_char *)mask; 138 for (x = 0; x < sizeof(*mask); x++) { 139 if (p[x] != 0xff) 140 break; 141 } 142 y = 0; 143 if (x < sizeof(*mask)) { 144 for (y = 0; y < 8; y++) { 145 if ((p[x] & (0x80 >> y)) == 0) 146 break; 147 } 148 } 149 return x * 8 + y; 150} 151 152void 153in_len2mask(struct in_addr *mask, int len) 154{ 155 int i; 156 u_char *p; 157 158 p = (u_char *)mask; 159 bzero(mask, sizeof(*mask)); 160 for (i = 0; i < len / 8; i++) 161 p[i] = 0xff; 162 if (len % 8) 163 p[i] = (0xff00 >> (len % 8)) & 0xff; 164} 165 166int 167in_nam2sin(const struct mbuf *nam, struct sockaddr_in **sin) 168{ 169 struct sockaddr *sa = mtod(nam, struct sockaddr *); 170 171 if (nam->m_len < offsetof(struct sockaddr, sa_data)) 172 return EINVAL; 173 if (sa->sa_family != AF_INET) 174 return EAFNOSUPPORT; 175 if (sa->sa_len != nam->m_len) 176 return EINVAL; 177 if (sa->sa_len != sizeof(struct sockaddr_in)) 178 return EINVAL; 179 *sin = satosin(sa); 180 181 return 0; 182} 183 184int 185in_sa2sin(struct sockaddr *sa, struct sockaddr_in **sin) 186{ 187 if (sa->sa_family != AF_INET) 188 return EAFNOSUPPORT; 189 if (sa->sa_len != sizeof(struct sockaddr_in)) 190 return EINVAL; 191 *sin = satosin(sa); 192 193 return 0; 194} 195 196/* 197 * Find the internet address structure (in_ifaddr) corresponding 198 * to a given interface (ifnet structure). 199 */ 200struct in_ifaddr * 201in_ifp2ia(struct ifnet *ifp) 202{ 203 struct in_ifaddr *ia = NULL; 204 struct ifaddr *ifa; 205 206 NET_ASSERT_LOCKED(); 207 208 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 209 if (ifa->ifa_addr->sa_family != AF_INET) 210 continue; 211 ia = ifatoia(ifa); 212 break; 213 } 214 215 return (ia); 216} 217 218int 219in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp) 220{ 221 int privileged; 222 223 privileged = 0; 224 if ((so->so_state & SS_PRIV) != 0) 225 privileged++; 226 227 switch (cmd) { 228#ifdef MROUTING 229 case SIOCGETVIFCNT: 230 case SIOCGETSGCNT: 231 return mrt_ioctl(so, cmd, data); 232#endif /* MROUTING */ 233 default: 234 return in_ioctl(cmd, data, ifp, privileged); 235 } 236} 237 238int 239in_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged) 240{ 241 struct ifreq *ifr = (struct ifreq *)data; 242 struct ifaddr *ifa; 243 struct in_ifaddr *ia = NULL; 244 struct sockaddr_in *sin = NULL, oldaddr; 245 int error = 0; 246 247 if (ifp == NULL) 248 return (ENXIO); 249 250 switch (cmd) { 251 case SIOCGIFADDR: 252 case SIOCGIFNETMASK: 253 case SIOCGIFDSTADDR: 254 case SIOCGIFBRDADDR: 255 return in_ioctl_get(cmd, data, ifp); 256 case SIOCSIFADDR: 257 if (!privileged) 258 return (EPERM); 259 return in_ioctl_set_ifaddr(cmd, data, ifp); 260 case SIOCAIFADDR: 261 case SIOCDIFADDR: 262 if (!privileged) 263 return (EPERM); 264 return in_ioctl_change_ifaddr(cmd, data, ifp); 265 case SIOCSIFNETMASK: 266 case SIOCSIFDSTADDR: 267 case SIOCSIFBRDADDR: 268 break; 269 default: 270 return (EOPNOTSUPP); 271 } 272 273 if (!privileged) 274 return (EPERM); 275 276 if (ifr->ifr_addr.sa_family == AF_INET) { 277 error = in_sa2sin(&ifr->ifr_addr, &sin); 278 if (error) 279 return (error); 280 } 281 282 NET_LOCK(); 283 KERNEL_LOCK(); 284 285 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 286 if (ifa->ifa_addr->sa_family != AF_INET) 287 continue; 288 /* find first address or exact match */ 289 if (ia == NULL) 290 ia = ifatoia(ifa); 291 if (sin == NULL || sin->sin_addr.s_addr == INADDR_ANY) 292 break; 293 if (ifatoia(ifa)->ia_addr.sin_addr.s_addr == 294 sin->sin_addr.s_addr) { 295 ia = ifatoia(ifa); 296 break; 297 } 298 } 299 if (ia == NULL) { 300 error = EADDRNOTAVAIL; 301 goto err; 302 } 303 304 switch (cmd) { 305 case SIOCSIFDSTADDR: 306 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 307 error = EINVAL; 308 break; 309 } 310 error = in_sa2sin(&ifr->ifr_dstaddr, &sin); 311 if (error) 312 break; 313 oldaddr = ia->ia_dstaddr; 314 ia->ia_dstaddr = *sin; 315 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (caddr_t)ia); 316 if (error) { 317 ia->ia_dstaddr = oldaddr; 318 break; 319 } 320 in_scrubhost(ia, &oldaddr); 321 in_addhost(ia, &ia->ia_dstaddr); 322 break; 323 324 case SIOCSIFBRDADDR: 325 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 326 error = EINVAL; 327 break; 328 } 329 error = in_sa2sin(&ifr->ifr_broadaddr, &sin); 330 if (error) 331 break; 332 ifa_update_broadaddr(ifp, &ia->ia_ifa, sintosa(sin)); 333 break; 334 335 case SIOCSIFNETMASK: 336 if (ifr->ifr_addr.sa_len < 8) { 337 error = EINVAL; 338 break; 339 } 340 /* do not check inet family or strict len */ 341 sin = satosin(&ifr->ifr_addr); 342 if (ntohl(sin->sin_addr.s_addr) & 343 (~ntohl(sin->sin_addr.s_addr) >> 1)) { 344 /* non-contiguous netmask */ 345 error = EINVAL; 346 break; 347 } 348 ia->ia_netmask = ia->ia_sockmask.sin_addr.s_addr = 349 sin->sin_addr.s_addr; 350 break; 351 } 352err: 353 KERNEL_UNLOCK(); 354 NET_UNLOCK(); 355 return (error); 356} 357 358int 359in_ioctl_set_ifaddr(u_long cmd, caddr_t data, struct ifnet *ifp) 360{ 361 struct ifreq *ifr = (struct ifreq *)data; 362 struct ifaddr *ifa; 363 struct in_ifaddr *ia = NULL; 364 struct sockaddr_in *sin; 365 int error = 0; 366 int newifaddr; 367 368 if (cmd != SIOCSIFADDR) 369 panic("%s: invalid ioctl %lu", __func__, cmd); 370 371 error = in_sa2sin(&ifr->ifr_addr, &sin); 372 if (error) 373 return (error); 374 375 NET_LOCK(); 376 KERNEL_LOCK(); 377 378 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 379 if (ifa->ifa_addr->sa_family != AF_INET) 380 continue; 381 /* find first address */ 382 ia = ifatoia(ifa); 383 break; 384 } 385 if (ia == NULL) { 386 ia = malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 387 refcnt_init_trace(&ia->ia_ifa.ifa_refcnt, DT_REFCNT_IDX_IFADDR); 388 ia->ia_addr.sin_family = AF_INET; 389 ia->ia_addr.sin_len = sizeof(ia->ia_addr); 390 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 391 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 392 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 393 ia->ia_sockmask.sin_len = 8; 394 if (ifp->if_flags & IFF_BROADCAST) { 395 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 396 ia->ia_broadaddr.sin_family = AF_INET; 397 } 398 ia->ia_ifp = ifp; 399 400 newifaddr = 1; 401 } else 402 newifaddr = 0; 403 404 in_ifscrub(ifp, ia); 405 error = in_ifinit(ifp, ia, sin, newifaddr); 406 if (!error) 407 if_addrhooks_run(ifp); 408 409 KERNEL_UNLOCK(); 410 NET_UNLOCK(); 411 return error; 412} 413 414int 415in_ioctl_change_ifaddr(u_long cmd, caddr_t data, struct ifnet *ifp) 416{ 417 struct ifaddr *ifa; 418 struct in_ifaddr *ia = NULL; 419 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 420 struct sockaddr_in *sin = NULL, *dstsin = NULL, *broadsin = NULL; 421 struct sockaddr_in *masksin = NULL; 422 int error = 0; 423 int newifaddr; 424 425 if (ifra->ifra_addr.sin_family == AF_INET) { 426 error = in_sa2sin(sintosa(&ifra->ifra_addr), &sin); 427 if (error) 428 return (error); 429 } 430 431 NET_LOCK(); 432 KERNEL_LOCK(); 433 434 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 435 if (ifa->ifa_addr->sa_family != AF_INET) 436 continue; 437 /* find first address, if no exact match wanted */ 438 if (sin == NULL || sin->sin_addr.s_addr == 439 ifatoia(ifa)->ia_addr.sin_addr.s_addr) { 440 ia = ifatoia(ifa); 441 break; 442 } 443 } 444 445 switch (cmd) { 446 case SIOCAIFADDR: { 447 int needinit = 0; 448 449 if (ifra->ifra_mask.sin_len) { 450 if (ifra->ifra_mask.sin_len < 8) { 451 error = EINVAL; 452 break; 453 } 454 /* do not check inet family or strict len */ 455 masksin = &ifra->ifra_mask; 456 if (ntohl(masksin->sin_addr.s_addr) & 457 (~ntohl(masksin->sin_addr.s_addr) >> 1)) { 458 /* non-contiguous netmask */ 459 error = EINVAL; 460 break; 461 } 462 } 463 if ((ifp->if_flags & IFF_POINTOPOINT) && 464 ifra->ifra_dstaddr.sin_family == AF_INET) { 465 error = in_sa2sin(sintosa(&ifra->ifra_dstaddr), 466 &dstsin); 467 if (error) 468 break; 469 } 470 if ((ifp->if_flags & IFF_BROADCAST) && 471 ifra->ifra_broadaddr.sin_family == AF_INET) { 472 error = in_sa2sin(sintosa(&ifra->ifra_broadaddr), 473 &broadsin); 474 if (error) 475 break; 476 } 477 478 if (ia == NULL) { 479 ia = malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 480 refcnt_init_trace(&ia->ia_ifa.ifa_refcnt, 481 DT_REFCNT_IDX_IFADDR); 482 ia->ia_addr.sin_family = AF_INET; 483 ia->ia_addr.sin_len = sizeof(ia->ia_addr); 484 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 485 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 486 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 487 ia->ia_sockmask.sin_len = 8; 488 if (ifp->if_flags & IFF_BROADCAST) { 489 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 490 ia->ia_broadaddr.sin_family = AF_INET; 491 } 492 ia->ia_ifp = ifp; 493 494 newifaddr = 1; 495 } else 496 newifaddr = 0; 497 498 if (sin == NULL) { 499 sin = &ia->ia_addr; 500 } else if (newifaddr || 501 sin->sin_addr.s_addr != ia->ia_addr.sin_addr.s_addr) { 502 needinit = 1; 503 } 504 if (masksin != NULL) { 505 in_ifscrub(ifp, ia); 506 ia->ia_netmask = ia->ia_sockmask.sin_addr.s_addr = 507 masksin->sin_addr.s_addr; 508 needinit = 1; 509 } 510 if (dstsin != NULL) { 511 in_ifscrub(ifp, ia); 512 ia->ia_dstaddr = *dstsin; 513 needinit = 1; 514 } 515 if (broadsin != NULL) { 516 if (newifaddr) 517 ia->ia_broadaddr = *broadsin; 518 else 519 ifa_update_broadaddr(ifp, &ia->ia_ifa, 520 sintosa(broadsin)); 521 } 522 if (needinit) { 523 error = in_ifinit(ifp, ia, sin, newifaddr); 524 if (error) 525 break; 526 } 527 if_addrhooks_run(ifp); 528 break; 529 } 530 case SIOCDIFADDR: 531 if (ia == NULL) { 532 error = EADDRNOTAVAIL; 533 break; 534 } 535 /* 536 * Even if the individual steps were safe, shouldn't 537 * these kinds of changes happen atomically? What 538 * should happen to a packet that was routed after 539 * the scrub but before the other steps? 540 */ 541 in_purgeaddr(&ia->ia_ifa); 542 if_addrhooks_run(ifp); 543 break; 544 545 default: 546 panic("%s: invalid ioctl %lu", __func__, cmd); 547 } 548 549 KERNEL_UNLOCK(); 550 NET_UNLOCK(); 551 return (error); 552} 553 554int 555in_ioctl_get(u_long cmd, caddr_t data, struct ifnet *ifp) 556{ 557 struct ifreq *ifr = (struct ifreq *)data; 558 struct ifaddr *ifa; 559 struct in_ifaddr *ia = NULL; 560 struct sockaddr *sa; 561 struct sockaddr_in *sin = NULL; 562 int error = 0; 563 564 sa = &ifr->ifr_addr; 565 if (sa->sa_family == AF_INET) { 566 sa->sa_len = sizeof(struct sockaddr_in); 567 error = in_sa2sin(sa, &sin); 568 if (error) 569 return (error); 570 } 571 572 NET_LOCK_SHARED(); 573 574 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 575 if (ifa->ifa_addr->sa_family != AF_INET) 576 continue; 577 /* find first address or exact match */ 578 if (ia == NULL) 579 ia = ifatoia(ifa); 580 if (sin == NULL || sin->sin_addr.s_addr == INADDR_ANY) 581 break; 582 if (ifatoia(ifa)->ia_addr.sin_addr.s_addr == 583 sin->sin_addr.s_addr) { 584 ia = ifatoia(ifa); 585 break; 586 } 587 } 588 if (ia == NULL) { 589 error = EADDRNOTAVAIL; 590 goto err; 591 } 592 593 switch(cmd) { 594 case SIOCGIFADDR: 595 *satosin(&ifr->ifr_addr) = ia->ia_addr; 596 break; 597 598 case SIOCGIFBRDADDR: 599 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 600 error = EINVAL; 601 break; 602 } 603 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr; 604 break; 605 606 case SIOCGIFDSTADDR: 607 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 608 error = EINVAL; 609 break; 610 } 611 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr; 612 break; 613 614 case SIOCGIFNETMASK: 615 *satosin(&ifr->ifr_addr) = ia->ia_sockmask; 616 break; 617 618 default: 619 panic("%s: invalid ioctl %lu", __func__, cmd); 620 } 621 622err: 623 NET_UNLOCK_SHARED(); 624 return (error); 625} 626 627/* 628 * Delete any existing route for an interface. 629 */ 630void 631in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 632{ 633 if (ISSET(ifp->if_flags, IFF_POINTOPOINT)) 634 in_scrubhost(ia, &ia->ia_dstaddr); 635 else if (!ISSET(ifp->if_flags, IFF_LOOPBACK)) 636 in_remove_prefix(ia); 637} 638 639/* 640 * Initialize an interface's internet address 641 * and routing table entry. 642 */ 643int 644in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 645 int newaddr) 646{ 647 u_int32_t i = sin->sin_addr.s_addr; 648 struct sockaddr_in oldaddr; 649 int error = 0, rterror; 650 651 NET_ASSERT_LOCKED(); 652 653 /* 654 * Always remove the address from the tree to make sure its 655 * position gets updated in case the key changes. 656 */ 657 if (!newaddr) { 658 rt_ifa_dellocal(&ia->ia_ifa); 659 ifa_del(ifp, &ia->ia_ifa); 660 } 661 oldaddr = ia->ia_addr; 662 ia->ia_addr = *sin; 663 664 if (ia->ia_netmask == 0) { 665 if (IN_CLASSA(i)) 666 ia->ia_netmask = IN_CLASSA_NET; 667 else if (IN_CLASSB(i)) 668 ia->ia_netmask = IN_CLASSB_NET; 669 else 670 ia->ia_netmask = IN_CLASSC_NET; 671 ia->ia_sockmask.sin_addr.s_addr = ia->ia_netmask; 672 } 673 674 /* 675 * Give the interface a chance to initialize 676 * if this is its first address, 677 * and to validate the address if necessary. 678 */ 679 if ((error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 680 ia->ia_addr = oldaddr; 681 } 682 683 /* 684 * Add the address to the local list and the global tree. If an 685 * error occurred, put back the original address. 686 */ 687 ifa_add(ifp, &ia->ia_ifa); 688 rterror = rt_ifa_addlocal(&ia->ia_ifa); 689 690 if (rterror) { 691 if (!newaddr) 692 ifa_del(ifp, &ia->ia_ifa); 693 if (!error) 694 error = rterror; 695 goto out; 696 } 697 if (error) 698 goto out; 699 700 701 ia->ia_net = i & ia->ia_netmask; 702 in_socktrim(&ia->ia_sockmask); 703 /* 704 * Add route for the network. 705 */ 706 ia->ia_ifa.ifa_metric = ifp->if_metric; 707 if (ISSET(ifp->if_flags, IFF_BROADCAST)) { 708 if (IN_RFC3021_SUBNET(ia->ia_netmask)) 709 ia->ia_broadaddr.sin_addr.s_addr = 0; 710 else { 711 ia->ia_broadaddr.sin_addr.s_addr = 712 ia->ia_net | ~ia->ia_netmask; 713 } 714 } 715 716 if (ISSET(ifp->if_flags, IFF_POINTOPOINT)) { 717 /* XXX We should not even call in_ifinit() in this case. */ 718 if (ia->ia_dstaddr.sin_family != AF_INET) 719 goto out; 720 error = in_addhost(ia, &ia->ia_dstaddr); 721 } else if (!ISSET(ifp->if_flags, IFF_LOOPBACK)) { 722 error = in_insert_prefix(ia); 723 } 724 725 /* 726 * If the interface supports multicast, join the "all hosts" 727 * multicast group on that interface. 728 */ 729 if ((ifp->if_flags & IFF_MULTICAST) && ia->ia_allhosts == NULL) { 730 struct in_addr addr; 731 732 addr.s_addr = INADDR_ALLHOSTS_GROUP; 733 ia->ia_allhosts = in_addmulti(&addr, ifp); 734 } 735 736out: 737 if (error && newaddr) 738 in_purgeaddr(&ia->ia_ifa); 739 740 return (error); 741} 742 743void 744in_purgeaddr(struct ifaddr *ifa) 745{ 746 struct ifnet *ifp = ifa->ifa_ifp; 747 struct in_ifaddr *ia = ifatoia(ifa); 748 749 NET_ASSERT_LOCKED(); 750 751 in_ifscrub(ifp, ia); 752 753 rt_ifa_dellocal(&ia->ia_ifa); 754 rt_ifa_purge(&ia->ia_ifa); 755 ifa_del(ifp, &ia->ia_ifa); 756 757 if (ia->ia_allhosts != NULL) { 758 in_delmulti(ia->ia_allhosts); 759 ia->ia_allhosts = NULL; 760 } 761 762 ia->ia_ifp = NULL; 763 ifafree(&ia->ia_ifa); 764} 765 766int 767in_addhost(struct in_ifaddr *ia, struct sockaddr_in *dst) 768{ 769 return rt_ifa_add(&ia->ia_ifa, RTF_HOST | RTF_MPATH, 770 sintosa(dst), ia->ia_ifa.ifa_ifp->if_rdomain); 771} 772 773int 774in_scrubhost(struct in_ifaddr *ia, struct sockaddr_in *dst) 775{ 776 return rt_ifa_del(&ia->ia_ifa, RTF_HOST, 777 sintosa(dst), ia->ia_ifa.ifa_ifp->if_rdomain); 778} 779 780/* 781 * Insert the cloning and broadcast routes for this subnet. 782 */ 783int 784in_insert_prefix(struct in_ifaddr *ia) 785{ 786 struct ifaddr *ifa = &ia->ia_ifa; 787 int error; 788 789 error = rt_ifa_add(ifa, RTF_CLONING | RTF_CONNECTED | RTF_MPATH, 790 ifa->ifa_addr, ifa->ifa_ifp->if_rdomain); 791 if (error) 792 return (error); 793 794 if (ia->ia_broadaddr.sin_addr.s_addr != 0) { 795 error = rt_ifa_add(ifa, RTF_HOST | RTF_BROADCAST | RTF_MPATH, 796 ifa->ifa_broadaddr, ifa->ifa_ifp->if_rdomain); 797 } 798 799 return (error); 800} 801 802void 803in_remove_prefix(struct in_ifaddr *ia) 804{ 805 struct ifaddr *ifa = &ia->ia_ifa; 806 807 rt_ifa_del(ifa, RTF_CLONING | RTF_CONNECTED, 808 ifa->ifa_addr, ifa->ifa_ifp->if_rdomain); 809 810 if (ia->ia_broadaddr.sin_addr.s_addr != 0) { 811 rt_ifa_del(ifa, RTF_HOST | RTF_BROADCAST, 812 ifa->ifa_broadaddr, ifa->ifa_ifp->if_rdomain); 813 } 814} 815 816/* 817 * Return 1 if the address is a local broadcast address. 818 */ 819int 820in_broadcast(struct in_addr in, u_int rtableid) 821{ 822 struct ifnet *ifn; 823 struct ifaddr *ifa; 824 u_int rdomain; 825 826 NET_ASSERT_LOCKED(); 827 828 rdomain = rtable_l2(rtableid); 829 830#define ia (ifatoia(ifa)) 831 TAILQ_FOREACH(ifn, &ifnetlist, if_list) { 832 if (ifn->if_rdomain != rdomain) 833 continue; 834 if ((ifn->if_flags & IFF_BROADCAST) == 0) 835 continue; 836 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) 837 if (ifa->ifa_addr->sa_family == AF_INET && 838 in.s_addr != ia->ia_addr.sin_addr.s_addr && 839 in.s_addr == ia->ia_broadaddr.sin_addr.s_addr) 840 return 1; 841 } 842 return (0); 843#undef ia 844} 845 846/* 847 * Look up the in_multi record for a given IP multicast address 848 * on a given interface. Return the matching record if found or NULL. 849 */ 850struct in_multi * 851in_lookupmulti(const struct in_addr *addr, struct ifnet *ifp) 852{ 853 struct in_multi *inm = NULL; 854 struct ifmaddr *ifma; 855 856 NET_ASSERT_LOCKED(); 857 858 TAILQ_FOREACH(ifma, &ifp->if_maddrlist, ifma_list) { 859 if (ifma->ifma_addr->sa_family == AF_INET && 860 ifmatoinm(ifma)->inm_addr.s_addr == addr->s_addr) { 861 inm = ifmatoinm(ifma); 862 break; 863 } 864 } 865 return (inm); 866} 867 868/* 869 * Add an address to the list of IP multicast addresses for a given interface. 870 */ 871struct in_multi * 872in_addmulti(const struct in_addr *addr, struct ifnet *ifp) 873{ 874 struct in_multi *inm; 875 struct ifreq ifr; 876 877 /* 878 * See if address already in list. 879 */ 880 inm = in_lookupmulti(addr, ifp); 881 if (inm != NULL) { 882 /* 883 * Found it; just increment the reference count. 884 */ 885 refcnt_take(&inm->inm_refcnt); 886 } else { 887 /* 888 * New address; allocate a new multicast record 889 * and link it into the interface's multicast list. 890 */ 891 inm = malloc(sizeof(*inm), M_IPMADDR, M_WAITOK | M_ZERO); 892 inm->inm_sin.sin_len = sizeof(struct sockaddr_in); 893 inm->inm_sin.sin_family = AF_INET; 894 inm->inm_sin.sin_addr = *addr; 895 refcnt_init_trace(&inm->inm_refcnt, DT_REFCNT_IDX_IFMADDR); 896 inm->inm_ifidx = ifp->if_index; 897 inm->inm_ifma.ifma_addr = sintosa(&inm->inm_sin); 898 899 /* 900 * Ask the network driver to update its multicast reception 901 * filter appropriately for the new address. 902 */ 903 memset(&ifr, 0, sizeof(ifr)); 904 memcpy(&ifr.ifr_addr, &inm->inm_sin, sizeof(inm->inm_sin)); 905 KERNEL_LOCK(); 906 if ((*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 907 KERNEL_UNLOCK(); 908 free(inm, M_IPMADDR, sizeof(*inm)); 909 return (NULL); 910 } 911 KERNEL_UNLOCK(); 912 913 TAILQ_INSERT_HEAD(&ifp->if_maddrlist, &inm->inm_ifma, 914 ifma_list); 915 916 /* 917 * Let IGMP know that we have joined a new IP multicast group. 918 */ 919 igmp_joingroup(inm, ifp); 920 } 921 922 return (inm); 923} 924 925/* 926 * Delete a multicast address record. 927 */ 928void 929in_delmulti(struct in_multi *inm) 930{ 931 struct ifreq ifr; 932 struct ifnet *ifp; 933 934 NET_ASSERT_LOCKED(); 935 936 if (refcnt_rele(&inm->inm_refcnt) == 0) 937 return; 938 939 ifp = if_get(inm->inm_ifidx); 940 if (ifp != NULL) { 941 /* 942 * No remaining claims to this record; let IGMP know that 943 * we are leaving the multicast group. 944 */ 945 igmp_leavegroup(inm, ifp); 946 947 /* 948 * Notify the network driver to update its multicast 949 * reception filter. 950 */ 951 memset(&ifr, 0, sizeof(ifr)); 952 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in); 953 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 954 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr; 955 KERNEL_LOCK(); 956 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr); 957 KERNEL_UNLOCK(); 958 959 TAILQ_REMOVE(&ifp->if_maddrlist, &inm->inm_ifma, ifma_list); 960 } 961 if_put(ifp); 962 963 free(inm, M_IPMADDR, sizeof(*inm)); 964} 965 966/* 967 * Return 1 if the multicast group represented by ``addr'' has been 968 * joined by interface ``ifp'', 0 otherwise. 969 */ 970int 971in_hasmulti(const struct in_addr *addr, struct ifnet *ifp) 972{ 973 struct in_multi *inm; 974 int joined; 975 976 inm = in_lookupmulti(addr, ifp); 977 joined = (inm != NULL); 978 979 return (joined); 980} 981 982void 983in_ifdetach(struct ifnet *ifp) 984{ 985 struct ifaddr *ifa, *next; 986 987 /* nuke any of IPv4 addresses we have */ 988 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) { 989 if (ifa->ifa_addr->sa_family != AF_INET) 990 continue; 991 in_purgeaddr(ifa); 992 if_addrhooks_run(ifp); 993 } 994 995 if (ifp->if_xflags & IFXF_AUTOCONF4) 996 ifp->if_xflags &= ~IFXF_AUTOCONF4; 997} 998 999void 1000in_prefixlen2mask(struct in_addr *maskp, int plen) 1001{ 1002 if (plen == 0) 1003 maskp->s_addr = 0; 1004 else 1005 maskp->s_addr = htonl(0xffffffff << (32 - plen)); 1006}