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