1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (C) 2001 WIDE Project. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)in.c 8.4 (Berkeley) 1/9/95 31 * $FreeBSD$ 32 */ 33 34 #include "opt_carp.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/sockio.h> 39 #include <sys/malloc.h> 40 #include <sys/socket.h> 41 #include <sys/kernel.h> 42 #include <sys/sysctl.h> 43 44 #include <net/if.h> 45 #include <net/if_types.h> 46 #include <net/route.h> 47 48 #include <netinet/in.h> 49 #include <netinet/in_var.h> 50 #include <netinet/in_pcb.h> 51 52 #include <netinet/igmp_var.h> 53 54 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address"); 55 56 static int in_mask2len(struct in_addr *); 57 static void in_len2mask(struct in_addr *, int); 58 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 59 struct ifnet *, struct thread *); 60 61 static int in_addprefix(struct in_ifaddr *, int); 62 static int in_scrubprefix(struct in_ifaddr *); 63 static void in_socktrim(struct sockaddr_in *); 64 static int in_ifinit(struct ifnet *, 65 struct in_ifaddr *, struct sockaddr_in *, int); 66 67 static int subnetsarelocal = 0; 68 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 69 &subnetsarelocal, 0, "Treat all subnets as directly connected"); 70 71 struct in_multihead in_multihead; /* XXX BSS initialization */ 72 73 extern struct inpcbinfo ripcbinfo; 74 extern struct inpcbinfo udbinfo; 75 76 /* 77 * Return 1 if an internet address is for a ``local'' host 78 * (one to which we have a connection). If subnetsarelocal 79 * is true, this includes other subnets of the local net. 80 * Otherwise, it includes only the directly-connected (sub)nets. 81 */ 82 int 83 in_localaddr(in) 84 struct in_addr in; 85 { 86 register u_long i = ntohl(in.s_addr); 87 register struct in_ifaddr *ia; 88 89 if (subnetsarelocal) { 90 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 91 if ((i & ia->ia_netmask) == ia->ia_net) 92 return (1); 93 } else { 94 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 95 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 96 return (1); 97 } 98 return (0); 99 } 100 101 /* 102 * Return 1 if an internet address is for the local host and configured 103 * on one of its interfaces. 104 */ 105 int 106 in_localip(in) 107 struct in_addr in; 108 { 109 struct in_ifaddr *ia; 110 111 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 112 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 113 return 1; 114 } 115 return 0; 116 } 117 118 /* 119 * Determine whether an IP address is in a reserved set of addresses 120 * that may not be forwarded, or whether datagrams to that destination 121 * may be forwarded. 122 */ 123 int 124 in_canforward(in) 125 struct in_addr in; 126 { 127 register u_long i = ntohl(in.s_addr); 128 register u_long net; 129 130 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 131 return (0); 132 if (IN_CLASSA(i)) { 133 net = i & IN_CLASSA_NET; 134 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 135 return (0); 136 } 137 return (1); 138 } 139 140 /* 141 * Trim a mask in a sockaddr 142 */ 143 static void 144 in_socktrim(ap) 145 struct sockaddr_in *ap; 146 { 147 register char *cplim = (char *) &ap->sin_addr; 148 register char *cp = (char *) (&ap->sin_addr + 1); 149 150 ap->sin_len = 0; 151 while (--cp >= cplim) 152 if (*cp) { 153 (ap)->sin_len = cp - (char *) (ap) + 1; 154 break; 155 } 156 } 157 158 static int 159 in_mask2len(mask) 160 struct in_addr *mask; 161 { 162 int x, y; 163 u_char *p; 164 165 p = (u_char *)mask; 166 for (x = 0; x < sizeof(*mask); x++) { 167 if (p[x] != 0xff) 168 break; 169 } 170 y = 0; 171 if (x < sizeof(*mask)) { 172 for (y = 0; y < 8; y++) { 173 if ((p[x] & (0x80 >> y)) == 0) 174 break; 175 } 176 } 177 return x * 8 + y; 178 } 179 180 static void 181 in_len2mask(mask, len) 182 struct in_addr *mask; 183 int len; 184 { 185 int i; 186 u_char *p; 187 188 p = (u_char *)mask; 189 bzero(mask, sizeof(*mask)); 190 for (i = 0; i < len / 8; i++) 191 p[i] = 0xff; 192 if (len % 8) 193 p[i] = (0xff00 >> (len % 8)) & 0xff; 194 } 195 196 /* 197 * Generic internet control operations (ioctl's). 198 * Ifp is 0 if not an interface-specific ioctl. 199 */ 200 /* ARGSUSED */ 201 int 202 in_control(so, cmd, data, ifp, td) 203 struct socket *so; 204 u_long cmd; 205 caddr_t data; 206 register struct ifnet *ifp; 207 struct thread *td; 208 { 209 register struct ifreq *ifr = (struct ifreq *)data; 210 register struct in_ifaddr *ia = 0, *iap; 211 register struct ifaddr *ifa; 212 struct in_addr dst; 213 struct in_ifaddr *oia; 214 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 215 struct sockaddr_in oldaddr; 216 int error, hostIsNew, iaIsNew, maskIsNew, s; 217 218 iaIsNew = 0; 219 220 switch (cmd) { 221 case SIOCALIFADDR: 222 case SIOCDLIFADDR: 223 if (td && (error = suser(td)) != 0) 224 return error; 225 /*fall through*/ 226 case SIOCGLIFADDR: 227 if (!ifp) 228 return EINVAL; 229 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 230 } 231 232 /* 233 * Find address for this interface, if it exists. 234 * 235 * If an alias address was specified, find that one instead of 236 * the first one on the interface, if possible. 237 */ 238 if (ifp) { 239 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 240 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) 241 if (iap->ia_ifp == ifp && 242 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 243 ia = iap; 244 break; 245 } 246 if (ia == NULL) 247 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 248 iap = ifatoia(ifa); 249 if (iap->ia_addr.sin_family == AF_INET) { 250 ia = iap; 251 break; 252 } 253 } 254 } 255 256 switch (cmd) { 257 258 case SIOCAIFADDR: 259 case SIOCDIFADDR: 260 if (ifp == 0) 261 return (EADDRNOTAVAIL); 262 if (ifra->ifra_addr.sin_family == AF_INET) { 263 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 264 if (ia->ia_ifp == ifp && 265 ia->ia_addr.sin_addr.s_addr == 266 ifra->ifra_addr.sin_addr.s_addr) 267 break; 268 } 269 if ((ifp->if_flags & IFF_POINTOPOINT) 270 && (cmd == SIOCAIFADDR) 271 && (ifra->ifra_dstaddr.sin_addr.s_addr 272 == INADDR_ANY)) { 273 return EDESTADDRREQ; 274 } 275 } 276 if (cmd == SIOCDIFADDR && ia == 0) 277 return (EADDRNOTAVAIL); 278 /* FALLTHROUGH */ 279 case SIOCSIFADDR: 280 case SIOCSIFNETMASK: 281 case SIOCSIFDSTADDR: 282 if (td && (error = suser(td)) != 0) 283 return error; 284 285 if (ifp == 0) 286 return (EADDRNOTAVAIL); 287 if (ia == (struct in_ifaddr *)0) { 288 ia = (struct in_ifaddr *) 289 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 290 if (ia == (struct in_ifaddr *)NULL) 291 return (ENOBUFS); 292 /* 293 * Protect from ipintr() traversing address list 294 * while we're modifying it. 295 */ 296 s = splnet(); 297 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link); 298 299 ifa = &ia->ia_ifa; 300 IFA_LOCK_INIT(ifa); 301 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 302 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 303 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 304 ifa->ifa_refcnt = 1; 305 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 306 307 ia->ia_sockmask.sin_len = 8; 308 ia->ia_sockmask.sin_family = AF_INET; 309 if (ifp->if_flags & IFF_BROADCAST) { 310 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 311 ia->ia_broadaddr.sin_family = AF_INET; 312 } 313 ia->ia_ifp = ifp; 314 splx(s); 315 iaIsNew = 1; 316 } 317 break; 318 319 case SIOCSIFBRDADDR: 320 if (td && (error = suser(td)) != 0) 321 return error; 322 /* FALLTHROUGH */ 323 324 case SIOCGIFADDR: 325 case SIOCGIFNETMASK: 326 case SIOCGIFDSTADDR: 327 case SIOCGIFBRDADDR: 328 if (ia == (struct in_ifaddr *)0) 329 return (EADDRNOTAVAIL); 330 break; 331 } 332 switch (cmd) { 333 334 case SIOCGIFADDR: 335 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 336 return (0); 337 338 case SIOCGIFBRDADDR: 339 if ((ifp->if_flags & IFF_BROADCAST) == 0) 340 return (EINVAL); 341 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 342 return (0); 343 344 case SIOCGIFDSTADDR: 345 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 346 return (EINVAL); 347 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 348 return (0); 349 350 case SIOCGIFNETMASK: 351 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 352 return (0); 353 354 case SIOCSIFDSTADDR: 355 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 356 return (EINVAL); 357 oldaddr = ia->ia_dstaddr; 358 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 359 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 360 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 361 ia->ia_dstaddr = oldaddr; 362 return (error); 363 } 364 if (ia->ia_flags & IFA_ROUTE) { 365 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 366 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 367 ia->ia_ifa.ifa_dstaddr = 368 (struct sockaddr *)&ia->ia_dstaddr; 369 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 370 } 371 return (0); 372 373 case SIOCSIFBRDADDR: 374 if ((ifp->if_flags & IFF_BROADCAST) == 0) 375 return (EINVAL); 376 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 377 return (0); 378 379 case SIOCSIFADDR: 380 error = in_ifinit(ifp, ia, 381 (struct sockaddr_in *) &ifr->ifr_addr, 1); 382 if (error != 0 && iaIsNew) 383 break; 384 if (error == 0) 385 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 386 return (0); 387 388 case SIOCSIFNETMASK: 389 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 390 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 391 return (0); 392 393 case SIOCAIFADDR: 394 maskIsNew = 0; 395 hostIsNew = 1; 396 error = 0; 397 if (ia->ia_addr.sin_family == AF_INET) { 398 if (ifra->ifra_addr.sin_len == 0) { 399 ifra->ifra_addr = ia->ia_addr; 400 hostIsNew = 0; 401 } else if (ifra->ifra_addr.sin_addr.s_addr == 402 ia->ia_addr.sin_addr.s_addr) 403 hostIsNew = 0; 404 } 405 if (ifra->ifra_mask.sin_len) { 406 in_ifscrub(ifp, ia); 407 ia->ia_sockmask = ifra->ifra_mask; 408 ia->ia_sockmask.sin_family = AF_INET; 409 ia->ia_subnetmask = 410 ntohl(ia->ia_sockmask.sin_addr.s_addr); 411 maskIsNew = 1; 412 } 413 if ((ifp->if_flags & IFF_POINTOPOINT) && 414 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 415 in_ifscrub(ifp, ia); 416 ia->ia_dstaddr = ifra->ifra_dstaddr; 417 maskIsNew = 1; /* We lie; but the effect's the same */ 418 } 419 if (ifra->ifra_addr.sin_family == AF_INET && 420 (hostIsNew || maskIsNew)) 421 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 422 if (error != 0 && iaIsNew) 423 break; 424 425 if ((ifp->if_flags & IFF_BROADCAST) && 426 (ifra->ifra_broadaddr.sin_family == AF_INET)) 427 ia->ia_broadaddr = ifra->ifra_broadaddr; 428 if (error == 0) 429 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 430 return (error); 431 432 case SIOCDIFADDR: 433 /* 434 * in_ifscrub kills the interface route. 435 */ 436 in_ifscrub(ifp, ia); 437 /* 438 * in_ifadown gets rid of all the rest of 439 * the routes. This is not quite the right 440 * thing to do, but at least if we are running 441 * a routing process they will come back. 442 */ 443 in_ifadown(&ia->ia_ifa, 1); 444 /* 445 * XXX horrible hack to detect that we are being called 446 * from if_detach() 447 */ 448 if (ifaddr_byindex(ifp->if_index) == NULL) { 449 in_pcbpurgeif0(&ripcbinfo, ifp); 450 in_pcbpurgeif0(&udbinfo, ifp); 451 } 452 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 453 error = 0; 454 break; 455 456 default: 457 if (ifp == 0 || ifp->if_ioctl == 0) 458 return (EOPNOTSUPP); 459 return ((*ifp->if_ioctl)(ifp, cmd, data)); 460 } 461 462 /* 463 * Protect from ipintr() traversing address list while we're modifying 464 * it. 465 */ 466 s = splnet(); 467 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 468 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link); 469 LIST_REMOVE(ia, ia_hash); 470 IFAFREE(&ia->ia_ifa); 471 splx(s); 472 473 return (error); 474 } 475 476 /* 477 * SIOC[GAD]LIFADDR. 478 * SIOCGLIFADDR: get first address. (?!?) 479 * SIOCGLIFADDR with IFLR_PREFIX: 480 * get first address that matches the specified prefix. 481 * SIOCALIFADDR: add the specified address. 482 * SIOCALIFADDR with IFLR_PREFIX: 483 * EINVAL since we can't deduce hostid part of the address. 484 * SIOCDLIFADDR: delete the specified address. 485 * SIOCDLIFADDR with IFLR_PREFIX: 486 * delete the first address that matches the specified prefix. 487 * return values: 488 * EINVAL on invalid parameters 489 * EADDRNOTAVAIL on prefix match failed/specified address not found 490 * other values may be returned from in_ioctl() 491 */ 492 static int 493 in_lifaddr_ioctl(so, cmd, data, ifp, td) 494 struct socket *so; 495 u_long cmd; 496 caddr_t data; 497 struct ifnet *ifp; 498 struct thread *td; 499 { 500 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 501 struct ifaddr *ifa; 502 503 /* sanity checks */ 504 if (!data || !ifp) { 505 panic("invalid argument to in_lifaddr_ioctl"); 506 /*NOTRECHED*/ 507 } 508 509 switch (cmd) { 510 case SIOCGLIFADDR: 511 /* address must be specified on GET with IFLR_PREFIX */ 512 if ((iflr->flags & IFLR_PREFIX) == 0) 513 break; 514 /*FALLTHROUGH*/ 515 case SIOCALIFADDR: 516 case SIOCDLIFADDR: 517 /* address must be specified on ADD and DELETE */ 518 if (iflr->addr.ss_family != AF_INET) 519 return EINVAL; 520 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 521 return EINVAL; 522 /* XXX need improvement */ 523 if (iflr->dstaddr.ss_family 524 && iflr->dstaddr.ss_family != AF_INET) 525 return EINVAL; 526 if (iflr->dstaddr.ss_family 527 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 528 return EINVAL; 529 break; 530 default: /*shouldn't happen*/ 531 return EOPNOTSUPP; 532 } 533 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 534 return EINVAL; 535 536 switch (cmd) { 537 case SIOCALIFADDR: 538 { 539 struct in_aliasreq ifra; 540 541 if (iflr->flags & IFLR_PREFIX) 542 return EINVAL; 543 544 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 545 bzero(&ifra, sizeof(ifra)); 546 bcopy(iflr->iflr_name, ifra.ifra_name, 547 sizeof(ifra.ifra_name)); 548 549 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 550 551 if (iflr->dstaddr.ss_family) { /*XXX*/ 552 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 553 iflr->dstaddr.ss_len); 554 } 555 556 ifra.ifra_mask.sin_family = AF_INET; 557 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 558 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 559 560 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td); 561 } 562 case SIOCGLIFADDR: 563 case SIOCDLIFADDR: 564 { 565 struct in_ifaddr *ia; 566 struct in_addr mask, candidate, match; 567 struct sockaddr_in *sin; 568 int cmp; 569 570 bzero(&mask, sizeof(mask)); 571 if (iflr->flags & IFLR_PREFIX) { 572 /* lookup a prefix rather than address. */ 573 in_len2mask(&mask, iflr->prefixlen); 574 575 sin = (struct sockaddr_in *)&iflr->addr; 576 match.s_addr = sin->sin_addr.s_addr; 577 match.s_addr &= mask.s_addr; 578 579 /* if you set extra bits, that's wrong */ 580 if (match.s_addr != sin->sin_addr.s_addr) 581 return EINVAL; 582 583 cmp = 1; 584 } else { 585 if (cmd == SIOCGLIFADDR) { 586 /* on getting an address, take the 1st match */ 587 cmp = 0; /*XXX*/ 588 } else { 589 /* on deleting an address, do exact match */ 590 in_len2mask(&mask, 32); 591 sin = (struct sockaddr_in *)&iflr->addr; 592 match.s_addr = sin->sin_addr.s_addr; 593 594 cmp = 1; 595 } 596 } 597 598 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 599 if (ifa->ifa_addr->sa_family != AF_INET6) 600 continue; 601 if (!cmp) 602 break; 603 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 604 candidate.s_addr &= mask.s_addr; 605 if (candidate.s_addr == match.s_addr) 606 break; 607 } 608 if (!ifa) 609 return EADDRNOTAVAIL; 610 ia = (struct in_ifaddr *)ifa; 611 612 if (cmd == SIOCGLIFADDR) { 613 /* fill in the if_laddrreq structure */ 614 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 615 616 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 617 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 618 ia->ia_dstaddr.sin_len); 619 } else 620 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 621 622 iflr->prefixlen = 623 in_mask2len(&ia->ia_sockmask.sin_addr); 624 625 iflr->flags = 0; /*XXX*/ 626 627 return 0; 628 } else { 629 struct in_aliasreq ifra; 630 631 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 632 bzero(&ifra, sizeof(ifra)); 633 bcopy(iflr->iflr_name, ifra.ifra_name, 634 sizeof(ifra.ifra_name)); 635 636 bcopy(&ia->ia_addr, &ifra.ifra_addr, 637 ia->ia_addr.sin_len); 638 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 639 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 640 ia->ia_dstaddr.sin_len); 641 } 642 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 643 ia->ia_sockmask.sin_len); 644 645 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 646 ifp, td); 647 } 648 } 649 } 650 651 return EOPNOTSUPP; /*just for safety*/ 652 } 653 654 /* 655 * Delete any existing route for an interface. 656 */ 657 void 658 in_ifscrub(ifp, ia) 659 register struct ifnet *ifp; 660 register struct in_ifaddr *ia; 661 { 662 in_scrubprefix(ia); 663 } 664 665 /* 666 * Initialize an interface's internet address 667 * and routing table entry. 668 */ 669 static int 670 in_ifinit(ifp, ia, sin, scrub) 671 register struct ifnet *ifp; 672 register struct in_ifaddr *ia; 673 struct sockaddr_in *sin; 674 int scrub; 675 { 676 register u_long i = ntohl(sin->sin_addr.s_addr); 677 struct sockaddr_in oldaddr; 678 int s = splimp(), flags = RTF_UP, error = 0; 679 680 oldaddr = ia->ia_addr; 681 if (oldaddr.sin_family == AF_INET) 682 LIST_REMOVE(ia, ia_hash); 683 ia->ia_addr = *sin; 684 if (ia->ia_addr.sin_family == AF_INET) 685 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 686 ia, ia_hash); 687 /* 688 * Give the interface a chance to initialize 689 * if this is its first address, 690 * and to validate the address if necessary. 691 */ 692 if (ifp->if_ioctl && 693 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 694 splx(s); 695 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 696 ia->ia_addr = oldaddr; 697 if (ia->ia_addr.sin_family == AF_INET) 698 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 699 ia, ia_hash); 700 return (error); 701 } 702 splx(s); 703 if (scrub) { 704 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 705 in_ifscrub(ifp, ia); 706 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 707 } 708 if (IN_CLASSA(i)) 709 ia->ia_netmask = IN_CLASSA_NET; 710 else if (IN_CLASSB(i)) 711 ia->ia_netmask = IN_CLASSB_NET; 712 else 713 ia->ia_netmask = IN_CLASSC_NET; 714 /* 715 * The subnet mask usually includes at least the standard network part, 716 * but may may be smaller in the case of supernetting. 717 * If it is set, we believe it. 718 */ 719 if (ia->ia_subnetmask == 0) { 720 ia->ia_subnetmask = ia->ia_netmask; 721 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 722 } else 723 ia->ia_netmask &= ia->ia_subnetmask; 724 ia->ia_net = i & ia->ia_netmask; 725 ia->ia_subnet = i & ia->ia_subnetmask; 726 in_socktrim(&ia->ia_sockmask); 727 #ifdef DEV_CARP 728 /* 729 * XXX: carp(4) does not have interface route 730 */ 731 if (ifp->if_type == IFT_CARP) 732 return (0); 733 #endif 734 /* 735 * Add route for the network. 736 */ 737 ia->ia_ifa.ifa_metric = ifp->if_metric; 738 if (ifp->if_flags & IFF_BROADCAST) { 739 ia->ia_broadaddr.sin_addr.s_addr = 740 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 741 ia->ia_netbroadcast.s_addr = 742 htonl(ia->ia_net | ~ ia->ia_netmask); 743 } else if (ifp->if_flags & IFF_LOOPBACK) { 744 ia->ia_dstaddr = ia->ia_addr; 745 flags |= RTF_HOST; 746 } else if (ifp->if_flags & IFF_POINTOPOINT) { 747 if (ia->ia_dstaddr.sin_family != AF_INET) 748 return (0); 749 flags |= RTF_HOST; 750 } 751 if ((error = in_addprefix(ia, flags)) != 0) 752 return (error); 753 754 /* 755 * If the interface supports multicast, join the "all hosts" 756 * multicast group on that interface. 757 */ 758 if (ifp->if_flags & IFF_MULTICAST) { 759 struct in_addr addr; 760 761 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 762 in_addmulti(&addr, ifp); 763 } 764 return (error); 765 } 766 767 #define rtinitflags(x) \ 768 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 769 ? RTF_HOST : 0) 770 /* 771 * Check if we have a route for the given prefix already or add a one 772 * accordingly. 773 */ 774 static int 775 in_addprefix(target, flags) 776 struct in_ifaddr *target; 777 int flags; 778 { 779 struct in_ifaddr *ia; 780 struct in_addr prefix, mask, p; 781 int error; 782 783 if ((flags & RTF_HOST) != 0) 784 prefix = target->ia_dstaddr.sin_addr; 785 else { 786 prefix = target->ia_addr.sin_addr; 787 mask = target->ia_sockmask.sin_addr; 788 prefix.s_addr &= mask.s_addr; 789 } 790 791 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 792 if (rtinitflags(ia)) 793 p = ia->ia_dstaddr.sin_addr; 794 else { 795 p = ia->ia_addr.sin_addr; 796 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 797 } 798 799 if (prefix.s_addr != p.s_addr) 800 continue; 801 802 /* 803 * If we got a matching prefix route inserted by other 804 * interface address, we are done here. 805 */ 806 if (ia->ia_flags & IFA_ROUTE) 807 return 0; 808 } 809 810 /* 811 * No-one seem to have this prefix route, so we try to insert it. 812 */ 813 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 814 if (!error) 815 target->ia_flags |= IFA_ROUTE; 816 return error; 817 } 818 819 /* 820 * If there is no other address in the system that can serve a route to the 821 * same prefix, remove the route. Hand over the route to the new address 822 * otherwise. 823 */ 824 static int 825 in_scrubprefix(target) 826 struct in_ifaddr *target; 827 { 828 struct in_ifaddr *ia; 829 struct in_addr prefix, mask, p; 830 int error; 831 832 if ((target->ia_flags & IFA_ROUTE) == 0) 833 return 0; 834 835 if (rtinitflags(target)) 836 prefix = target->ia_dstaddr.sin_addr; 837 else { 838 prefix = target->ia_addr.sin_addr; 839 mask = target->ia_sockmask.sin_addr; 840 prefix.s_addr &= mask.s_addr; 841 } 842 843 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 844 if (rtinitflags(ia)) 845 p = ia->ia_dstaddr.sin_addr; 846 else { 847 p = ia->ia_addr.sin_addr; 848 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 849 } 850 851 if (prefix.s_addr != p.s_addr) 852 continue; 853 854 /* 855 * If we got a matching prefix address, move IFA_ROUTE and 856 * the route itself to it. Make sure that routing daemons 857 * get a heads-up. 858 * 859 * XXX: a special case for carp(4) interface 860 */ 861 if ((ia->ia_flags & IFA_ROUTE) == 0 862 #ifdef DEV_CARP 863 && (ia->ia_ifp->if_type != IFT_CARP) 864 #endif 865 ) { 866 rtinit(&(target->ia_ifa), (int)RTM_DELETE, 867 rtinitflags(target)); 868 target->ia_flags &= ~IFA_ROUTE; 869 870 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 871 rtinitflags(ia) | RTF_UP); 872 if (error == 0) 873 ia->ia_flags |= IFA_ROUTE; 874 return error; 875 } 876 } 877 878 /* 879 * As no-one seem to have this prefix, we can remove the route. 880 */ 881 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 882 target->ia_flags &= ~IFA_ROUTE; 883 return 0; 884 } 885 886 #undef rtinitflags 887 888 /* 889 * Return 1 if the address might be a local broadcast address. 890 */ 891 int 892 in_broadcast(in, ifp) 893 struct in_addr in; 894 struct ifnet *ifp; 895 { 896 register struct ifaddr *ifa; 897 u_long t; 898 899 if (in.s_addr == INADDR_BROADCAST || 900 in.s_addr == INADDR_ANY) 901 return 1; 902 if ((ifp->if_flags & IFF_BROADCAST) == 0) 903 return 0; 904 t = ntohl(in.s_addr); 905 /* 906 * Look through the list of addresses for a match 907 * with a broadcast address. 908 */ 909 #define ia ((struct in_ifaddr *)ifa) 910 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 911 if (ifa->ifa_addr->sa_family == AF_INET && 912 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 913 in.s_addr == ia->ia_netbroadcast.s_addr || 914 /* 915 * Check for old-style (host 0) broadcast. 916 */ 917 t == ia->ia_subnet || t == ia->ia_net) && 918 /* 919 * Check for an all one subnetmask. These 920 * only exist when an interface gets a secondary 921 * address. 922 */ 923 ia->ia_subnetmask != (u_long)0xffffffff) 924 return 1; 925 return (0); 926 #undef ia 927 } 928 /* 929 * Add an address to the list of IP multicast addresses for a given interface. 930 */ 931 struct in_multi * 932 in_addmulti(ap, ifp) 933 register struct in_addr *ap; 934 register struct ifnet *ifp; 935 { 936 register struct in_multi *inm; 937 int error; 938 struct sockaddr_in sin; 939 struct ifmultiaddr *ifma; 940 int s = splnet(); 941 942 /* 943 * Call generic routine to add membership or increment 944 * refcount. It wants addresses in the form of a sockaddr, 945 * so we build one here (being careful to zero the unused bytes). 946 */ 947 bzero(&sin, sizeof sin); 948 sin.sin_family = AF_INET; 949 sin.sin_len = sizeof sin; 950 sin.sin_addr = *ap; 951 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 952 if (error) { 953 splx(s); 954 return 0; 955 } 956 957 /* 958 * If ifma->ifma_protospec is null, then if_addmulti() created 959 * a new record. Otherwise, we are done. 960 */ 961 if (ifma->ifma_protospec != NULL) { 962 splx(s); 963 return ifma->ifma_protospec; 964 } 965 966 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 967 at interrupt time? If so, need to fix if_addmulti. XXX */ 968 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, 969 M_NOWAIT | M_ZERO); 970 if (inm == NULL) { 971 splx(s); 972 return (NULL); 973 } 974 975 inm->inm_addr = *ap; 976 inm->inm_ifp = ifp; 977 inm->inm_ifma = ifma; 978 ifma->ifma_protospec = inm; 979 LIST_INSERT_HEAD(&in_multihead, inm, inm_link); 980 981 /* 982 * Let IGMP know that we have joined a new IP multicast group. 983 */ 984 igmp_joingroup(inm); 985 splx(s); 986 return (inm); 987 } 988 989 /* 990 * Delete a multicast address record. 991 */ 992 void 993 in_delmulti(inm) 994 register struct in_multi *inm; 995 { 996 struct ifmultiaddr *ifma = inm->inm_ifma; 997 struct in_multi my_inm; 998 int s = splnet(); 999 1000 my_inm.inm_ifp = NULL ; /* don't send the leave msg */ 1001 if (ifma->ifma_refcount == 1) { 1002 /* 1003 * No remaining claims to this record; let IGMP know that 1004 * we are leaving the multicast group. 1005 * But do it after the if_delmulti() which might reset 1006 * the interface and nuke the packet. 1007 */ 1008 my_inm = *inm ; 1009 ifma->ifma_protospec = NULL; 1010 LIST_REMOVE(inm, inm_link); 1011 free(inm, M_IPMADDR); 1012 } 1013 /* XXX - should be separate API for when we have an ifma? */ 1014 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 1015 if (my_inm.inm_ifp != NULL) 1016 igmp_leavegroup(&my_inm); 1017 splx(s); 1018 } 1019