1 /* $OpenBSD: in.c,v 1.135 2017/02/16 10:15:12 mpi 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 82 #include "ether.h" 83 84 85 void in_socktrim(struct sockaddr_in *); 86 int in_lifaddr_ioctl(u_long, caddr_t, struct ifnet *, int); 87 88 void in_purgeaddr(struct ifaddr *); 89 int in_addhost(struct in_ifaddr *, struct sockaddr_in *); 90 int in_scrubhost(struct in_ifaddr *, struct sockaddr_in *); 91 int in_insert_prefix(struct in_ifaddr *); 92 void in_remove_prefix(struct in_ifaddr *); 93 94 /* 95 * Determine whether an IP address is in a reserved set of addresses 96 * that may not be forwarded, or whether datagrams to that destination 97 * may be forwarded. 98 */ 99 int 100 in_canforward(struct in_addr in) 101 { 102 u_int32_t net; 103 104 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 105 return (0); 106 if (IN_CLASSA(in.s_addr)) { 107 net = in.s_addr & IN_CLASSA_NET; 108 if (net == 0 || 109 net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 110 return (0); 111 } 112 return (1); 113 } 114 115 /* 116 * Trim a mask in a sockaddr 117 */ 118 void 119 in_socktrim(struct sockaddr_in *ap) 120 { 121 char *cplim = (char *) &ap->sin_addr; 122 char *cp = (char *) (&ap->sin_addr + 1); 123 124 ap->sin_len = 0; 125 while (--cp >= cplim) 126 if (*cp) { 127 (ap)->sin_len = cp - (char *) (ap) + 1; 128 break; 129 } 130 } 131 132 int 133 in_mask2len(struct in_addr *mask) 134 { 135 int x, y; 136 u_char *p; 137 138 p = (u_char *)mask; 139 for (x = 0; x < sizeof(*mask); x++) { 140 if (p[x] != 0xff) 141 break; 142 } 143 y = 0; 144 if (x < sizeof(*mask)) { 145 for (y = 0; y < 8; y++) { 146 if ((p[x] & (0x80 >> y)) == 0) 147 break; 148 } 149 } 150 return x * 8 + y; 151 } 152 153 void 154 in_len2mask(struct in_addr *mask, int len) 155 { 156 int i; 157 u_char *p; 158 159 p = (u_char *)mask; 160 bzero(mask, sizeof(*mask)); 161 for (i = 0; i < len / 8; i++) 162 p[i] = 0xff; 163 if (len % 8) 164 p[i] = (0xff00 >> (len % 8)) & 0xff; 165 } 166 167 /* 168 * Generic internet control operations (ioctl's). 169 */ 170 int 171 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp) 172 { 173 int privileged; 174 175 privileged = 0; 176 if ((so->so_state & SS_PRIV) != 0) 177 privileged++; 178 179 switch (cmd) { 180 #ifdef MROUTING 181 case SIOCGETVIFCNT: 182 case SIOCGETSGCNT: 183 return (mrt_ioctl(so, cmd, data)); 184 #endif /* MROUTING */ 185 case SIOCALIFADDR: 186 case SIOCDLIFADDR: 187 if (!privileged) 188 return (EPERM); 189 /* FALLTHROUGH */ 190 case SIOCGLIFADDR: 191 if (ifp == NULL) 192 return (EINVAL); 193 return in_lifaddr_ioctl(cmd, data, ifp, privileged); 194 default: 195 if (ifp == NULL) 196 return (EOPNOTSUPP); 197 } 198 199 return (in_ioctl(cmd, data, ifp, privileged)); 200 } 201 202 int 203 in_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged) 204 { 205 struct ifreq *ifr = (struct ifreq *)data; 206 struct ifaddr *ifa; 207 struct in_ifaddr *ia = NULL; 208 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 209 struct sockaddr_in oldaddr; 210 int error; 211 int newifaddr; 212 213 splsoftassert(IPL_SOFTNET); 214 215 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 216 if (ifa->ifa_addr->sa_family == AF_INET) { 217 ia = ifatoia(ifa); 218 break; 219 } 220 } 221 222 switch (cmd) { 223 224 case SIOCAIFADDR: 225 case SIOCDIFADDR: 226 if (ifra->ifra_addr.sin_family == AF_INET) { 227 for (; ifa != NULL; ifa = TAILQ_NEXT(ifa, ifa_list)) { 228 if ((ifa->ifa_addr->sa_family == AF_INET) && 229 ifatoia(ifa)->ia_addr.sin_addr.s_addr == 230 ifra->ifra_addr.sin_addr.s_addr) 231 break; 232 } 233 ia = ifatoia(ifa); 234 } 235 if (cmd == SIOCDIFADDR && ia == NULL) 236 return (EADDRNOTAVAIL); 237 /* FALLTHROUGH */ 238 case SIOCSIFADDR: 239 if (!privileged) 240 return (EPERM); 241 242 if (ia == NULL) { 243 ia = malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 244 ia->ia_addr.sin_family = AF_INET; 245 ia->ia_addr.sin_len = sizeof(ia->ia_addr); 246 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 247 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 248 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 249 ia->ia_sockmask.sin_len = 8; 250 if (ifp->if_flags & IFF_BROADCAST) { 251 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 252 ia->ia_broadaddr.sin_family = AF_INET; 253 } 254 ia->ia_ifp = ifp; 255 256 newifaddr = 1; 257 } else 258 newifaddr = 0; 259 break; 260 261 case SIOCSIFNETMASK: 262 case SIOCSIFDSTADDR: 263 case SIOCSIFBRDADDR: 264 if (!privileged) 265 return (EPERM); 266 /* FALLTHROUGH */ 267 268 case SIOCGIFADDR: 269 case SIOCGIFNETMASK: 270 case SIOCGIFDSTADDR: 271 case SIOCGIFBRDADDR: 272 if (ia && satosin(&ifr->ifr_addr)->sin_addr.s_addr) { 273 for (; ifa != NULL; ifa = TAILQ_NEXT(ifa, ifa_list)) { 274 if ((ifa->ifa_addr->sa_family == AF_INET) && 275 ifatoia(ifa)->ia_addr.sin_addr.s_addr == 276 satosin(&ifr->ifr_addr)->sin_addr.s_addr) { 277 ia = ifatoia(ifa); 278 break; 279 } 280 } 281 } 282 if (ia == NULL) 283 return (EADDRNOTAVAIL); 284 break; 285 } 286 switch (cmd) { 287 288 case SIOCGIFADDR: 289 *satosin(&ifr->ifr_addr) = ia->ia_addr; 290 break; 291 292 case SIOCGIFBRDADDR: 293 if ((ifp->if_flags & IFF_BROADCAST) == 0) 294 return (EINVAL); 295 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr; 296 break; 297 298 case SIOCGIFDSTADDR: 299 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 300 return (EINVAL); 301 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr; 302 break; 303 304 case SIOCGIFNETMASK: 305 *satosin(&ifr->ifr_addr) = ia->ia_sockmask; 306 break; 307 308 case SIOCSIFDSTADDR: 309 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 310 return (EINVAL); 311 oldaddr = ia->ia_dstaddr; 312 ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr); 313 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 314 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 315 ia->ia_dstaddr = oldaddr; 316 return (error); 317 } 318 in_scrubhost(ia, &oldaddr); 319 in_addhost(ia, &ia->ia_dstaddr); 320 break; 321 322 case SIOCSIFBRDADDR: 323 if ((ifp->if_flags & IFF_BROADCAST) == 0) 324 return (EINVAL); 325 ifa_update_broadaddr(ifp, &ia->ia_ifa, &ifr->ifr_broadaddr); 326 break; 327 328 case SIOCSIFADDR: 329 in_ifscrub(ifp, ia); 330 error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), newifaddr); 331 if (!error) 332 dohooks(ifp->if_addrhooks, 0); 333 return (error); 334 335 case SIOCSIFNETMASK: 336 ia->ia_netmask = ia->ia_sockmask.sin_addr.s_addr = 337 ifra->ifra_addr.sin_addr.s_addr; 338 break; 339 340 case SIOCAIFADDR: { 341 int needinit = 0; 342 343 error = 0; 344 345 if (ia->ia_addr.sin_family == AF_INET) { 346 if (ifra->ifra_addr.sin_len == 0) 347 ifra->ifra_addr = ia->ia_addr; 348 else if (ifra->ifra_addr.sin_addr.s_addr != 349 ia->ia_addr.sin_addr.s_addr || newifaddr) 350 needinit = 1; 351 } 352 if (ifra->ifra_mask.sin_len) { 353 in_ifscrub(ifp, ia); 354 ia->ia_sockmask = ifra->ifra_mask; 355 ia->ia_netmask = ia->ia_sockmask.sin_addr.s_addr; 356 needinit = 1; 357 } 358 if ((ifp->if_flags & IFF_POINTOPOINT) && 359 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 360 in_ifscrub(ifp, ia); 361 ia->ia_dstaddr = ifra->ifra_dstaddr; 362 needinit = 1; 363 } 364 if ((ifp->if_flags & IFF_BROADCAST) && 365 (ifra->ifra_broadaddr.sin_family == AF_INET)) { 366 if (newifaddr) 367 ia->ia_broadaddr = ifra->ifra_broadaddr; 368 else 369 ifa_update_broadaddr(ifp, &ia->ia_ifa, 370 sintosa(&ifra->ifra_broadaddr)); 371 } 372 if (ifra->ifra_addr.sin_family == AF_INET && needinit) { 373 error = in_ifinit(ifp, ia, &ifra->ifra_addr, newifaddr); 374 } 375 if (!error) 376 dohooks(ifp->if_addrhooks, 0); 377 return (error); 378 } 379 case SIOCDIFADDR: 380 /* 381 * Even if the individual steps were safe, shouldn't 382 * these kinds of changes happen atomically? What 383 * should happen to a packet that was routed after 384 * the scrub but before the other steps? 385 */ 386 in_purgeaddr(&ia->ia_ifa); 387 dohooks(ifp->if_addrhooks, 0); 388 break; 389 390 default: 391 if (ifp->if_ioctl == NULL) 392 return (EOPNOTSUPP); 393 error = ((*ifp->if_ioctl)(ifp, cmd, data)); 394 return (error); 395 } 396 return (0); 397 } 398 399 /* 400 * SIOC[GAD]LIFADDR. 401 * SIOCGLIFADDR: get first address. (???) 402 * SIOCGLIFADDR with IFLR_PREFIX: 403 * get first address that matches the specified prefix. 404 * SIOCALIFADDR: add the specified address. 405 * SIOCALIFADDR with IFLR_PREFIX: 406 * EINVAL since we can't deduce hostid part of the address. 407 * SIOCDLIFADDR: delete the specified address. 408 * SIOCDLIFADDR with IFLR_PREFIX: 409 * delete the first address that matches the specified prefix. 410 * return values: 411 * EINVAL on invalid parameters 412 * EADDRNOTAVAIL on prefix match failed/specified address not found 413 * other values may be returned from in_ioctl() 414 */ 415 int 416 in_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged) 417 { 418 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 419 struct ifaddr *ifa; 420 struct sockaddr *sa; 421 422 /* sanity checks */ 423 if (!data || !ifp) { 424 panic("invalid argument to in_lifaddr_ioctl"); 425 /*NOTRECHED*/ 426 } 427 428 switch (cmd) { 429 case SIOCGLIFADDR: 430 /* address must be specified on GET with IFLR_PREFIX */ 431 if ((iflr->flags & IFLR_PREFIX) == 0) 432 break; 433 /*FALLTHROUGH*/ 434 case SIOCALIFADDR: 435 case SIOCDLIFADDR: 436 /* address must be specified on ADD and DELETE */ 437 sa = (struct sockaddr *)&iflr->addr; 438 if (sa->sa_family != AF_INET) 439 return EINVAL; 440 if (sa->sa_len != sizeof(struct sockaddr_in)) 441 return EINVAL; 442 /* XXX need improvement */ 443 sa = (struct sockaddr *)&iflr->dstaddr; 444 if (sa->sa_family 445 && sa->sa_family != AF_INET) 446 return EINVAL; 447 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in)) 448 return EINVAL; 449 break; 450 default: /*shouldn't happen*/ 451 #if 0 452 panic("invalid cmd to in_lifaddr_ioctl"); 453 /*NOTREACHED*/ 454 #else 455 return EOPNOTSUPP; 456 #endif 457 } 458 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 459 return EINVAL; 460 461 switch (cmd) { 462 case SIOCALIFADDR: 463 { 464 struct in_aliasreq ifra; 465 466 if (iflr->flags & IFLR_PREFIX) 467 return EINVAL; 468 469 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 470 bzero(&ifra, sizeof(ifra)); 471 memcpy(ifra.ifra_name, iflr->iflr_name, 472 sizeof(ifra.ifra_name)); 473 474 memcpy(&ifra.ifra_addr, &iflr->addr, 475 ((struct sockaddr *)&iflr->addr)->sa_len); 476 477 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 478 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr, 479 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 480 } 481 482 ifra.ifra_mask.sin_family = AF_INET; 483 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 484 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 485 486 return in_ioctl(SIOCAIFADDR, (caddr_t)&ifra, ifp, privileged); 487 } 488 case SIOCGLIFADDR: 489 case SIOCDLIFADDR: 490 { 491 struct in_ifaddr *ia; 492 struct in_addr mask, candidate, match; 493 struct sockaddr_in *sin; 494 int cmp; 495 496 bzero(&mask, sizeof(mask)); 497 if (iflr->flags & IFLR_PREFIX) { 498 /* lookup a prefix rather than address. */ 499 in_len2mask(&mask, iflr->prefixlen); 500 501 sin = (struct sockaddr_in *)&iflr->addr; 502 match.s_addr = sin->sin_addr.s_addr; 503 match.s_addr &= mask.s_addr; 504 505 /* if you set extra bits, that's wrong */ 506 if (match.s_addr != sin->sin_addr.s_addr) 507 return EINVAL; 508 509 cmp = 1; 510 } else { 511 if (cmd == SIOCGLIFADDR) { 512 /* on getting an address, take the 1st match */ 513 cmp = 0; /*XXX*/ 514 } else { 515 /* on deleting an address, do exact match */ 516 in_len2mask(&mask, 32); 517 sin = (struct sockaddr_in *)&iflr->addr; 518 match.s_addr = sin->sin_addr.s_addr; 519 520 cmp = 1; 521 } 522 } 523 524 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 525 if (ifa->ifa_addr->sa_family != AF_INET) 526 continue; 527 if (!cmp) 528 break; 529 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 530 candidate.s_addr &= mask.s_addr; 531 if (candidate.s_addr == match.s_addr) 532 break; 533 } 534 if (!ifa) 535 return EADDRNOTAVAIL; 536 ia = ifatoia(ifa); 537 538 if (cmd == SIOCGLIFADDR) { 539 /* fill in the if_laddrreq structure */ 540 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len); 541 542 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 543 memcpy(&iflr->dstaddr, &ia->ia_dstaddr, 544 ia->ia_dstaddr.sin_len); 545 } else 546 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 547 548 iflr->prefixlen = 549 in_mask2len(&ia->ia_sockmask.sin_addr); 550 551 iflr->flags = 0; /*XXX*/ 552 553 return 0; 554 } else { 555 struct in_aliasreq ifra; 556 557 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 558 bzero(&ifra, sizeof(ifra)); 559 memcpy(ifra.ifra_name, iflr->iflr_name, 560 sizeof(ifra.ifra_name)); 561 562 memcpy(&ifra.ifra_addr, &ia->ia_addr, 563 ia->ia_addr.sin_len); 564 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 565 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr, 566 ia->ia_dstaddr.sin_len); 567 } 568 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask, 569 ia->ia_sockmask.sin_len); 570 571 return in_ioctl(SIOCDIFADDR, (caddr_t)&ifra, ifp, 572 privileged); 573 } 574 } 575 } 576 577 return EOPNOTSUPP; /*just for safety*/ 578 } 579 580 /* 581 * Delete any existing route for an interface. 582 */ 583 void 584 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 585 { 586 if (ISSET(ifp->if_flags, IFF_POINTOPOINT)) 587 in_scrubhost(ia, &ia->ia_dstaddr); 588 else if (!ISSET(ifp->if_flags, IFF_LOOPBACK)) 589 in_remove_prefix(ia); 590 } 591 592 /* 593 * Initialize an interface's internet address 594 * and routing table entry. 595 */ 596 int 597 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 598 int newaddr) 599 { 600 u_int32_t i = sin->sin_addr.s_addr; 601 struct sockaddr_in oldaddr; 602 int error = 0, rterror; 603 604 splsoftassert(IPL_SOFTNET); 605 606 /* 607 * Always remove the address from the tree to make sure its 608 * position gets updated in case the key changes. 609 */ 610 if (!newaddr) { 611 rt_ifa_dellocal(&ia->ia_ifa); 612 ifa_del(ifp, &ia->ia_ifa); 613 } 614 oldaddr = ia->ia_addr; 615 ia->ia_addr = *sin; 616 617 /* 618 * Give the interface a chance to initialize 619 * if this is its first address, 620 * and to validate the address if necessary. 621 */ 622 if (ifp->if_ioctl && 623 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 624 ia->ia_addr = oldaddr; 625 } 626 627 /* 628 * Add the address to the local list and the global tree. If an 629 * error occured, put back the original address. 630 */ 631 ifa_add(ifp, &ia->ia_ifa); 632 rterror = rt_ifa_addlocal(&ia->ia_ifa); 633 634 if (rterror) { 635 if (!newaddr) 636 ifa_del(ifp, &ia->ia_ifa); 637 if (!error) 638 error = rterror; 639 goto out; 640 } 641 if (error) 642 goto out; 643 644 if (ia->ia_netmask == 0) { 645 if (IN_CLASSA(i)) 646 ia->ia_netmask = IN_CLASSA_NET; 647 else if (IN_CLASSB(i)) 648 ia->ia_netmask = IN_CLASSB_NET; 649 else 650 ia->ia_netmask = IN_CLASSC_NET; 651 ia->ia_sockmask.sin_addr.s_addr = ia->ia_netmask; 652 } 653 654 ia->ia_net = i & ia->ia_netmask; 655 in_socktrim(&ia->ia_sockmask); 656 /* 657 * Add route for the network. 658 */ 659 ia->ia_ifa.ifa_metric = ifp->if_metric; 660 if (ISSET(ifp->if_flags, IFF_BROADCAST)) { 661 if (IN_RFC3021_SUBNET(ia->ia_netmask)) 662 ia->ia_broadaddr.sin_addr.s_addr = 0; 663 else { 664 ia->ia_broadaddr.sin_addr.s_addr = 665 ia->ia_net | ~ia->ia_netmask; 666 } 667 } 668 669 if (ISSET(ifp->if_flags, IFF_POINTOPOINT)) { 670 /* XXX We should not even call in_ifinit() in this case. */ 671 if (ia->ia_dstaddr.sin_family != AF_INET) 672 goto out; 673 error = in_addhost(ia, &ia->ia_dstaddr); 674 } else if (!ISSET(ifp->if_flags, IFF_LOOPBACK)) { 675 error = in_insert_prefix(ia); 676 } 677 678 /* 679 * If the interface supports multicast, join the "all hosts" 680 * multicast group on that interface. 681 */ 682 if ((ifp->if_flags & IFF_MULTICAST) && ia->ia_allhosts == NULL) { 683 struct in_addr addr; 684 685 addr.s_addr = INADDR_ALLHOSTS_GROUP; 686 ia->ia_allhosts = in_addmulti(&addr, ifp); 687 } 688 689 out: 690 if (error && newaddr) 691 in_purgeaddr(&ia->ia_ifa); 692 693 return (error); 694 } 695 696 void 697 in_purgeaddr(struct ifaddr *ifa) 698 { 699 struct ifnet *ifp = ifa->ifa_ifp; 700 struct in_ifaddr *ia = ifatoia(ifa); 701 extern int ifatrash; 702 703 splsoftassert(IPL_SOFTNET); 704 705 in_ifscrub(ifp, ia); 706 707 rt_ifa_dellocal(&ia->ia_ifa); 708 rt_ifa_purge(&ia->ia_ifa); 709 ifa_del(ifp, &ia->ia_ifa); 710 711 if (ia->ia_allhosts != NULL) { 712 in_delmulti(ia->ia_allhosts); 713 ia->ia_allhosts = NULL; 714 } 715 716 ifatrash++; 717 ia->ia_ifp = NULL; 718 ifafree(&ia->ia_ifa); 719 } 720 721 int 722 in_addhost(struct in_ifaddr *ia, struct sockaddr_in *dst) 723 { 724 return rt_ifa_add(&ia->ia_ifa, RTF_HOST, sintosa(dst)); 725 } 726 727 int 728 in_scrubhost(struct in_ifaddr *ia, struct sockaddr_in *dst) 729 { 730 return rt_ifa_del(&ia->ia_ifa, RTF_HOST, sintosa(dst)); 731 } 732 733 /* 734 * Insert the cloning and broadcast routes for this subnet. 735 */ 736 int 737 in_insert_prefix(struct in_ifaddr *ia) 738 { 739 struct ifaddr *ifa = &ia->ia_ifa; 740 int error; 741 742 error = rt_ifa_add(ifa, RTF_CLONING | RTF_CONNECTED, ifa->ifa_addr); 743 if (error) 744 return (error); 745 746 if (ia->ia_broadaddr.sin_addr.s_addr != 0) 747 error = rt_ifa_add(ifa, RTF_HOST | RTF_BROADCAST, 748 ifa->ifa_broadaddr); 749 750 return (error); 751 } 752 753 void 754 in_remove_prefix(struct in_ifaddr *ia) 755 { 756 struct ifaddr *ifa = &ia->ia_ifa; 757 758 rt_ifa_del(ifa, RTF_CLONING | RTF_CONNECTED, ifa->ifa_addr); 759 760 if (ia->ia_broadaddr.sin_addr.s_addr != 0) 761 rt_ifa_del(ifa, RTF_HOST | RTF_BROADCAST, ifa->ifa_broadaddr); 762 } 763 764 /* 765 * Return 1 if the address is a local broadcast address. 766 */ 767 int 768 in_broadcast(struct in_addr in, u_int rtableid) 769 { 770 struct ifnet *ifn; 771 struct ifaddr *ifa; 772 u_int rdomain; 773 774 rdomain = rtable_l2(rtableid); 775 776 #define ia (ifatoia(ifa)) 777 TAILQ_FOREACH(ifn, &ifnet, if_list) { 778 if (ifn->if_rdomain != rdomain) 779 continue; 780 if ((ifn->if_flags & IFF_BROADCAST) == 0) 781 continue; 782 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) 783 if (ifa->ifa_addr->sa_family == AF_INET && 784 in.s_addr != ia->ia_addr.sin_addr.s_addr && 785 in.s_addr == ia->ia_broadaddr.sin_addr.s_addr) 786 return 1; 787 } 788 return (0); 789 #undef ia 790 } 791 792 /* 793 * Add an address to the list of IP multicast addresses for a given interface. 794 */ 795 struct in_multi * 796 in_addmulti(struct in_addr *ap, struct ifnet *ifp) 797 { 798 struct in_multi *inm; 799 struct ifreq ifr; 800 801 splsoftassert(IPL_SOFTNET); 802 803 /* 804 * See if address already in list. 805 */ 806 IN_LOOKUP_MULTI(*ap, ifp, inm); 807 if (inm != NULL) { 808 /* 809 * Found it; just increment the reference count. 810 */ 811 ++inm->inm_refcnt; 812 } else { 813 if (ifp->if_ioctl == NULL) 814 return (NULL); 815 816 /* 817 * New address; allocate a new multicast record 818 * and link it into the interface's multicast list. 819 */ 820 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO); 821 if (inm == NULL) 822 return (NULL); 823 824 inm->inm_sin.sin_len = sizeof(struct sockaddr_in); 825 inm->inm_sin.sin_family = AF_INET; 826 inm->inm_sin.sin_addr = *ap; 827 inm->inm_refcnt = 1; 828 inm->inm_ifidx = ifp->if_index; 829 inm->inm_ifma.ifma_addr = sintosa(&inm->inm_sin); 830 831 /* 832 * Ask the network driver to update its multicast reception 833 * filter appropriately for the new address. 834 */ 835 memset(&ifr, 0, sizeof(ifr)); 836 memcpy(&ifr.ifr_addr, &inm->inm_sin, sizeof(inm->inm_sin)); 837 if ((*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 838 free(inm, M_IPMADDR, sizeof(*inm)); 839 return (NULL); 840 } 841 842 TAILQ_INSERT_HEAD(&ifp->if_maddrlist, &inm->inm_ifma, 843 ifma_list); 844 845 /* 846 * Let IGMP know that we have joined a new IP multicast group. 847 */ 848 igmp_joingroup(inm); 849 } 850 851 return (inm); 852 } 853 854 /* 855 * Delete a multicast address record. 856 */ 857 void 858 in_delmulti(struct in_multi *inm) 859 { 860 struct ifreq ifr; 861 struct ifnet *ifp; 862 863 splsoftassert(IPL_SOFTNET); 864 865 if (--inm->inm_refcnt == 0) { 866 /* 867 * No remaining claims to this record; let IGMP know that 868 * we are leaving the multicast group. 869 */ 870 igmp_leavegroup(inm); 871 ifp = if_get(inm->inm_ifidx); 872 873 /* 874 * Notify the network driver to update its multicast 875 * reception filter. 876 */ 877 if (ifp != NULL) { 878 memset(&ifr, 0, sizeof(ifr)); 879 satosin(&ifr.ifr_addr)->sin_len = 880 sizeof(struct sockaddr_in); 881 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 882 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr; 883 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr); 884 885 TAILQ_REMOVE(&ifp->if_maddrlist, &inm->inm_ifma, 886 ifma_list); 887 } 888 if_put(ifp); 889 890 free(inm, M_IPMADDR, sizeof(*inm)); 891 } 892 } 893 894 /* 895 * Return 1 if the multicast group represented by ``ap'' has been 896 * joined by interface ``ifp'', 0 otherwise. 897 */ 898 int 899 in_hasmulti(struct in_addr *ap, struct ifnet *ifp) 900 { 901 struct in_multi *inm; 902 int joined; 903 904 KERNEL_LOCK(); 905 IN_LOOKUP_MULTI(*ap, ifp, inm); 906 joined = (inm != NULL); 907 KERNEL_UNLOCK(); 908 909 return (joined); 910 } 911 912 void 913 in_ifdetach(struct ifnet *ifp) 914 { 915 struct ifaddr *ifa, *next; 916 917 /* nuke any of IPv4 addresses we have */ 918 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) { 919 if (ifa->ifa_addr->sa_family != AF_INET) 920 continue; 921 in_purgeaddr(ifa); 922 dohooks(ifp->if_addrhooks, 0); 923 } 924 } 925 926 void 927 in_prefixlen2mask(struct in_addr *maskp, int plen) 928 { 929 if (plen == 0) 930 maskp->s_addr = 0; 931 else 932 maskp->s_addr = htonl(0xffffffff << (32 - plen)); 933 } 934