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 * $Id: in.c,v 1.34 1997/04/27 20:01:03 wollman Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/sockio.h> 40 #include <sys/malloc.h> 41 #include <sys/proc.h> 42 #include <sys/socket.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 46 #include <net/if.h> 47 #include <net/route.h> 48 49 #include <netinet/in.h> 50 #include <netinet/in_var.h> 51 52 #include <netinet/igmp_var.h> 53 54 static void in_socktrim __P((struct sockaddr_in *)); 55 static int in_ifinit __P((struct ifnet *, 56 struct in_ifaddr *, struct sockaddr_in *, int)); 57 58 static int subnetsarelocal = 0; 59 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 60 &subnetsarelocal, 0, ""); 61 62 struct in_multihead in_multihead; /* XXX BSS initialization */ 63 64 /* 65 * Return 1 if an internet address is for a ``local'' host 66 * (one to which we have a connection). If subnetsarelocal 67 * is true, this includes other subnets of the local net. 68 * Otherwise, it includes only the directly-connected (sub)nets. 69 */ 70 int 71 in_localaddr(in) 72 struct in_addr in; 73 { 74 register u_long i = ntohl(in.s_addr); 75 register struct in_ifaddr *ia; 76 77 if (subnetsarelocal) { 78 for (ia = in_ifaddrhead.tqh_first; ia; 79 ia = ia->ia_link.tqe_next) 80 if ((i & ia->ia_netmask) == ia->ia_net) 81 return (1); 82 } else { 83 for (ia = in_ifaddrhead.tqh_first; ia; 84 ia = ia->ia_link.tqe_next) 85 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 86 return (1); 87 } 88 return (0); 89 } 90 91 /* 92 * Determine whether an IP address is in a reserved set of addresses 93 * that may not be forwarded, or whether datagrams to that destination 94 * may be forwarded. 95 */ 96 int 97 in_canforward(in) 98 struct in_addr in; 99 { 100 register u_long i = ntohl(in.s_addr); 101 register u_long net; 102 103 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 104 return (0); 105 if (IN_CLASSA(i)) { 106 net = i & IN_CLASSA_NET; 107 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 108 return (0); 109 } 110 return (1); 111 } 112 113 /* 114 * Trim a mask in a sockaddr 115 */ 116 static void 117 in_socktrim(ap) 118 struct sockaddr_in *ap; 119 { 120 register char *cplim = (char *) &ap->sin_addr; 121 register 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 131 static int in_interfaces; /* number of external internet interfaces */ 132 133 /* 134 * Generic internet control operations (ioctl's). 135 * Ifp is 0 if not an interface-specific ioctl. 136 */ 137 /* ARGSUSED */ 138 int 139 in_control(so, cmd, data, ifp, p) 140 struct socket *so; 141 int cmd; 142 caddr_t data; 143 register struct ifnet *ifp; 144 struct proc *p; 145 { 146 register struct ifreq *ifr = (struct ifreq *)data; 147 register struct in_ifaddr *ia = 0, *iap; 148 register struct ifaddr *ifa; 149 struct in_ifaddr *oia; 150 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 151 struct sockaddr_in oldaddr; 152 int error, hostIsNew, maskIsNew, s; 153 u_long i; 154 155 /* 156 * Find address for this interface, if it exists. 157 * 158 * If an alias address was specified, find that one instead of 159 * the first one on the interface. 160 */ 161 if (ifp) 162 for (iap = in_ifaddrhead.tqh_first; iap; 163 iap = iap->ia_link.tqe_next) 164 if (iap->ia_ifp == ifp) { 165 if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr == 166 iap->ia_addr.sin_addr.s_addr) { 167 ia = iap; 168 break; 169 } else if (ia == NULL) { 170 ia = iap; 171 if (ifr->ifr_addr.sa_family != AF_INET) 172 break; 173 } 174 } 175 176 switch (cmd) { 177 178 case SIOCAIFADDR: 179 case SIOCDIFADDR: 180 if (ifra->ifra_addr.sin_family == AF_INET) { 181 for (oia = ia; ia; ia = ia->ia_link.tqe_next) { 182 if (ia->ia_ifp == ifp && 183 ia->ia_addr.sin_addr.s_addr == 184 ifra->ifra_addr.sin_addr.s_addr) 185 break; 186 } 187 if ((ifp->if_flags & IFF_POINTOPOINT) 188 && (cmd == SIOCAIFADDR) 189 && (ifra->ifra_dstaddr.sin_addr.s_addr 190 == INADDR_ANY)) { 191 return EDESTADDRREQ; 192 } 193 } 194 if (cmd == SIOCDIFADDR && ia == 0) 195 return (EADDRNOTAVAIL); 196 /* FALLTHROUGH */ 197 case SIOCSIFADDR: 198 case SIOCSIFNETMASK: 199 case SIOCSIFDSTADDR: 200 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 201 return error; 202 203 if (ifp == 0) 204 panic("in_control"); 205 if (ia == (struct in_ifaddr *)0) { 206 ia = (struct in_ifaddr *) 207 malloc(sizeof *ia, M_IFADDR, M_WAITOK); 208 if (ia == (struct in_ifaddr *)NULL) 209 return (ENOBUFS); 210 bzero((caddr_t)ia, sizeof *ia); 211 /* 212 * Protect from ipintr() traversing address list 213 * while we're modifying it. 214 */ 215 s = splnet(); 216 217 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link); 218 ifa = &ia->ia_ifa; 219 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 220 221 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 222 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 223 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 224 ia->ia_sockmask.sin_len = 8; 225 if (ifp->if_flags & IFF_BROADCAST) { 226 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 227 ia->ia_broadaddr.sin_family = AF_INET; 228 } 229 ia->ia_ifp = ifp; 230 if (!(ifp->if_flags & IFF_LOOPBACK)) 231 in_interfaces++; 232 splx(s); 233 } 234 break; 235 236 case SIOCSIFBRDADDR: 237 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 238 return error; 239 /* FALLTHROUGH */ 240 241 case SIOCGIFADDR: 242 case SIOCGIFNETMASK: 243 case SIOCGIFDSTADDR: 244 case SIOCGIFBRDADDR: 245 if (ia == (struct in_ifaddr *)0) 246 return (EADDRNOTAVAIL); 247 break; 248 } 249 switch (cmd) { 250 251 case SIOCGIFADDR: 252 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 253 break; 254 255 case SIOCGIFBRDADDR: 256 if ((ifp->if_flags & IFF_BROADCAST) == 0) 257 return (EINVAL); 258 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 259 break; 260 261 case SIOCGIFDSTADDR: 262 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 263 return (EINVAL); 264 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 265 break; 266 267 case SIOCGIFNETMASK: 268 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 269 break; 270 271 case SIOCSIFDSTADDR: 272 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 273 return (EINVAL); 274 oldaddr = ia->ia_dstaddr; 275 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 276 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 277 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 278 ia->ia_dstaddr = oldaddr; 279 return (error); 280 } 281 if (ia->ia_flags & IFA_ROUTE) { 282 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 283 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 284 ia->ia_ifa.ifa_dstaddr = 285 (struct sockaddr *)&ia->ia_dstaddr; 286 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 287 } 288 break; 289 290 case SIOCSIFBRDADDR: 291 if ((ifp->if_flags & IFF_BROADCAST) == 0) 292 return (EINVAL); 293 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 294 break; 295 296 case SIOCSIFADDR: 297 return (in_ifinit(ifp, ia, 298 (struct sockaddr_in *) &ifr->ifr_addr, 1)); 299 300 case SIOCSIFNETMASK: 301 i = ifra->ifra_addr.sin_addr.s_addr; 302 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i); 303 break; 304 305 case SIOCAIFADDR: 306 maskIsNew = 0; 307 hostIsNew = 1; 308 error = 0; 309 if (ia->ia_addr.sin_family == AF_INET) { 310 if (ifra->ifra_addr.sin_len == 0) { 311 ifra->ifra_addr = ia->ia_addr; 312 hostIsNew = 0; 313 } else if (ifra->ifra_addr.sin_addr.s_addr == 314 ia->ia_addr.sin_addr.s_addr) 315 hostIsNew = 0; 316 } 317 if (ifra->ifra_mask.sin_len) { 318 in_ifscrub(ifp, ia); 319 ia->ia_sockmask = ifra->ifra_mask; 320 ia->ia_subnetmask = 321 ntohl(ia->ia_sockmask.sin_addr.s_addr); 322 maskIsNew = 1; 323 } 324 if ((ifp->if_flags & IFF_POINTOPOINT) && 325 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 326 in_ifscrub(ifp, ia); 327 ia->ia_dstaddr = ifra->ifra_dstaddr; 328 maskIsNew = 1; /* We lie; but the effect's the same */ 329 } 330 if (ifra->ifra_addr.sin_family == AF_INET && 331 (hostIsNew || maskIsNew)) 332 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 333 if ((ifp->if_flags & IFF_BROADCAST) && 334 (ifra->ifra_broadaddr.sin_family == AF_INET)) 335 ia->ia_broadaddr = ifra->ifra_broadaddr; 336 return (error); 337 338 case SIOCDIFADDR: 339 in_ifscrub(ifp, ia); 340 /* 341 * Protect from ipintr() traversing address list 342 * while we're modifying it. 343 */ 344 s = splnet(); 345 346 ifa = &ia->ia_ifa; 347 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 348 oia = ia; 349 TAILQ_REMOVE(&in_ifaddrhead, oia, ia_link); 350 IFAFREE(&oia->ia_ifa); 351 splx(s); 352 break; 353 354 default: 355 if (ifp == 0 || ifp->if_ioctl == 0) 356 return (EOPNOTSUPP); 357 return ((*ifp->if_ioctl)(ifp, cmd, data)); 358 } 359 return (0); 360 } 361 362 /* 363 * Delete any existing route for an interface. 364 */ 365 void 366 in_ifscrub(ifp, ia) 367 register struct ifnet *ifp; 368 register struct in_ifaddr *ia; 369 { 370 371 if ((ia->ia_flags & IFA_ROUTE) == 0) 372 return; 373 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 374 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 375 else 376 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 377 ia->ia_flags &= ~IFA_ROUTE; 378 } 379 380 /* 381 * Initialize an interface's internet address 382 * and routing table entry. 383 */ 384 static int 385 in_ifinit(ifp, ia, sin, scrub) 386 register struct ifnet *ifp; 387 register struct in_ifaddr *ia; 388 struct sockaddr_in *sin; 389 int scrub; 390 { 391 register u_long i = ntohl(sin->sin_addr.s_addr); 392 struct sockaddr_in oldaddr; 393 int s = splimp(), flags = RTF_UP, error; 394 395 oldaddr = ia->ia_addr; 396 ia->ia_addr = *sin; 397 /* 398 * Give the interface a chance to initialize 399 * if this is its first address, 400 * and to validate the address if necessary. 401 */ 402 if (ifp->if_ioctl && 403 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 404 splx(s); 405 ia->ia_addr = oldaddr; 406 return (error); 407 } 408 splx(s); 409 if (scrub) { 410 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 411 in_ifscrub(ifp, ia); 412 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 413 } 414 if (IN_CLASSA(i)) 415 ia->ia_netmask = IN_CLASSA_NET; 416 else if (IN_CLASSB(i)) 417 ia->ia_netmask = IN_CLASSB_NET; 418 else 419 ia->ia_netmask = IN_CLASSC_NET; 420 /* 421 * The subnet mask usually includes at least the standard network part, 422 * but may may be smaller in the case of supernetting. 423 * If it is set, we believe it. 424 */ 425 if (ia->ia_subnetmask == 0) { 426 ia->ia_subnetmask = ia->ia_netmask; 427 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 428 } else 429 ia->ia_netmask &= ia->ia_subnetmask; 430 ia->ia_net = i & ia->ia_netmask; 431 ia->ia_subnet = i & ia->ia_subnetmask; 432 in_socktrim(&ia->ia_sockmask); 433 /* 434 * Add route for the network. 435 */ 436 ia->ia_ifa.ifa_metric = ifp->if_metric; 437 if (ifp->if_flags & IFF_BROADCAST) { 438 ia->ia_broadaddr.sin_addr.s_addr = 439 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 440 ia->ia_netbroadcast.s_addr = 441 htonl(ia->ia_net | ~ ia->ia_netmask); 442 } else if (ifp->if_flags & IFF_LOOPBACK) { 443 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 444 flags |= RTF_HOST; 445 } else if (ifp->if_flags & IFF_POINTOPOINT) { 446 if (ia->ia_dstaddr.sin_family != AF_INET) 447 return (0); 448 flags |= RTF_HOST; 449 } 450 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 451 ia->ia_flags |= IFA_ROUTE; 452 453 /* 454 * If the interface supports multicast, join the "all hosts" 455 * multicast group on that interface. 456 */ 457 if (ifp->if_flags & IFF_MULTICAST) { 458 struct in_addr addr; 459 460 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 461 in_addmulti(&addr, ifp); 462 } 463 return (error); 464 } 465 466 467 /* 468 * Return 1 if the address might be a local broadcast address. 469 */ 470 int 471 in_broadcast(in, ifp) 472 struct in_addr in; 473 struct ifnet *ifp; 474 { 475 register struct ifaddr *ifa; 476 u_long t; 477 478 if (in.s_addr == INADDR_BROADCAST || 479 in.s_addr == INADDR_ANY) 480 return 1; 481 if ((ifp->if_flags & IFF_BROADCAST) == 0) 482 return 0; 483 t = ntohl(in.s_addr); 484 /* 485 * Look through the list of addresses for a match 486 * with a broadcast address. 487 */ 488 #define ia ((struct in_ifaddr *)ifa) 489 for (ifa = ifp->if_addrhead.tqh_first; ifa; 490 ifa = ifa->ifa_link.tqe_next) 491 if (ifa->ifa_addr->sa_family == AF_INET && 492 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 493 in.s_addr == ia->ia_netbroadcast.s_addr || 494 /* 495 * Check for old-style (host 0) broadcast. 496 */ 497 t == ia->ia_subnet || t == ia->ia_net) && 498 /* 499 * Check for an all one subnetmask. These 500 * only exist when an interface gets a secondary 501 * address. 502 */ 503 ia->ia_subnetmask != (u_long)0xffffffff) 504 return 1; 505 return (0); 506 #undef ia 507 } 508 /* 509 * Add an address to the list of IP multicast addresses for a given interface. 510 */ 511 struct in_multi * 512 in_addmulti(ap, ifp) 513 register struct in_addr *ap; 514 register struct ifnet *ifp; 515 { 516 register struct in_multi *inm; 517 int error; 518 struct sockaddr_in sin; 519 struct ifmultiaddr *ifma; 520 int s = splnet(); 521 522 /* 523 * Call generic routine to add membership or increment 524 * refcount. It wants addresses in the form of a sockaddr, 525 * so we build one here (being careful to zero the unused bytes). 526 */ 527 bzero(&sin, sizeof sin); 528 sin.sin_family = AF_INET; 529 sin.sin_len = sizeof sin; 530 sin.sin_addr = *ap; 531 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 532 if (error) { 533 splx(s); 534 return 0; 535 } 536 537 /* 538 * If ifma->ifma_protospec is null, then if_addmulti() created 539 * a new record. Otherwise, we are done. 540 */ 541 if (ifma->ifma_protospec != 0) 542 return ifma->ifma_protospec; 543 544 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 545 at interrupt time? If so, need to fix if_addmulti. XXX */ 546 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT); 547 if (inm == NULL) { 548 splx(s); 549 return (NULL); 550 } 551 552 bzero(inm, sizeof *inm); 553 inm->inm_addr = *ap; 554 inm->inm_ifp = ifp; 555 inm->inm_ifma = ifma; 556 ifma->ifma_protospec = inm; 557 LIST_INSERT_HEAD(&in_multihead, inm, inm_link); 558 559 /* 560 * Let IGMP know that we have joined a new IP multicast group. 561 */ 562 igmp_joingroup(inm); 563 splx(s); 564 return (inm); 565 } 566 567 /* 568 * Delete a multicast address record. 569 */ 570 void 571 in_delmulti(inm) 572 register struct in_multi *inm; 573 { 574 struct ifmultiaddr *ifma = inm->inm_ifma; 575 int s = splnet(); 576 577 if (ifma->ifma_refcount == 1) { 578 /* 579 * No remaining claims to this record; let IGMP know that 580 * we are leaving the multicast group. 581 */ 582 igmp_leavegroup(inm); 583 ifma->ifma_protospec = 0; 584 LIST_REMOVE(inm, inm_link); 585 free(inm, M_IPMADDR); 586 } 587 /* XXX - should be separate API for when we have an ifma? */ 588 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 589 splx(s); 590 } 591