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