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