1 /* $NetBSD: in.c,v 1.66 2001/04/13 23:30:22 thorpej Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * Copyright (c) 1982, 1986, 1991, 1993 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. All advertising materials mentioning features or use of this software 82 * must display the following acknowledgement: 83 * This product includes software developed by the University of 84 * California, Berkeley and its contributors. 85 * 4. Neither the name of the University nor the names of its contributors 86 * may be used to endorse or promote products derived from this software 87 * without specific prior written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 99 * SUCH DAMAGE. 100 * 101 * @(#)in.c 8.4 (Berkeley) 1/9/95 102 */ 103 104 #include "opt_inet.h" 105 #include "opt_inet_conf.h" 106 #include "opt_mrouting.h" 107 108 #include <sys/param.h> 109 #include <sys/ioctl.h> 110 #include <sys/errno.h> 111 #include <sys/malloc.h> 112 #include <sys/socket.h> 113 #include <sys/socketvar.h> 114 #include <sys/systm.h> 115 #include <sys/proc.h> 116 117 #include <net/if.h> 118 #include <net/route.h> 119 120 #include <net/if_ether.h> 121 122 #include <netinet/in_systm.h> 123 #include <netinet/in.h> 124 #include <netinet/in_var.h> 125 #include <netinet/if_inarp.h> 126 #include <netinet/ip_mroute.h> 127 #include <netinet/igmp_var.h> 128 129 #ifdef INET 130 131 static int in_mask2len __P((struct in_addr *)); 132 static void in_len2mask __P((struct in_addr *, int)); 133 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 134 struct ifnet *, struct proc *)); 135 136 #ifndef SUBNETSARELOCAL 137 #define SUBNETSARELOCAL 1 138 #endif 139 140 #ifndef HOSTZEROBROADCAST 141 #define HOSTZEROBROADCAST 1 142 #endif 143 144 int subnetsarelocal = SUBNETSARELOCAL; 145 int hostzeroisbroadcast = HOSTZEROBROADCAST; 146 147 /* 148 * This list is used to keep track of in_multi chains which belong to 149 * deleted interface addresses. We use in_ifaddr so that a chain head 150 * won't be deallocated until all multicast address record are deleted. 151 */ 152 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk); 153 154 /* 155 * Return 1 if an internet address is for a ``local'' host 156 * (one to which we have a connection). If subnetsarelocal 157 * is true, this includes other subnets of the local net. 158 * Otherwise, it includes only the directly-connected (sub)nets. 159 */ 160 int 161 in_localaddr(in) 162 struct in_addr in; 163 { 164 struct in_ifaddr *ia; 165 166 if (subnetsarelocal) { 167 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) 168 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) 169 return (1); 170 } else { 171 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) 172 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) 173 return (1); 174 } 175 return (0); 176 } 177 178 /* 179 * Determine whether an IP address is in a reserved set of addresses 180 * that may not be forwarded, or whether datagrams to that destination 181 * may be forwarded. 182 */ 183 int 184 in_canforward(in) 185 struct in_addr in; 186 { 187 u_int32_t net; 188 189 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 190 return (0); 191 if (IN_CLASSA(in.s_addr)) { 192 net = in.s_addr & IN_CLASSA_NET; 193 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 194 return (0); 195 } 196 return (1); 197 } 198 199 /* 200 * Trim a mask in a sockaddr 201 */ 202 void 203 in_socktrim(ap) 204 struct sockaddr_in *ap; 205 { 206 char *cplim = (char *) &ap->sin_addr; 207 char *cp = (char *) (&ap->sin_addr + 1); 208 209 ap->sin_len = 0; 210 while (--cp >= cplim) 211 if (*cp) { 212 (ap)->sin_len = cp - (char *) (ap) + 1; 213 break; 214 } 215 } 216 217 /* 218 * Routine to take an Internet address and convert into a 219 * "dotted quad" representation for printing. 220 */ 221 const char * 222 in_fmtaddr(addr) 223 struct in_addr addr; 224 { 225 static char buf[sizeof("123.456.789.123")]; 226 227 addr.s_addr = ntohl(addr.s_addr); 228 229 sprintf(buf, "%d.%d.%d.%d", 230 (addr.s_addr >> 24) & 0xFF, 231 (addr.s_addr >> 16) & 0xFF, 232 (addr.s_addr >> 8) & 0xFF, 233 (addr.s_addr >> 0) & 0xFF); 234 return buf; 235 } 236 237 /* 238 * Maintain the "in_maxmtu" variable, which is the largest 239 * mtu for non-local interfaces with AF_INET addresses assigned 240 * to them that are up. 241 */ 242 unsigned long in_maxmtu; 243 244 void 245 in_setmaxmtu() 246 { 247 struct in_ifaddr *ia; 248 struct ifnet *ifp; 249 unsigned long maxmtu = 0; 250 251 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) { 252 if ((ifp = ia->ia_ifp) == 0) 253 continue; 254 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP) 255 continue; 256 if (ifp->if_mtu > maxmtu) 257 maxmtu = ifp->if_mtu; 258 } 259 if (maxmtu) 260 in_maxmtu = maxmtu; 261 } 262 263 static int 264 in_mask2len(mask) 265 struct in_addr *mask; 266 { 267 int x, y; 268 u_char *p; 269 270 p = (u_char *)mask; 271 for (x = 0; x < sizeof(*mask); x++) { 272 if (p[x] != 0xff) 273 break; 274 } 275 y = 0; 276 if (x < sizeof(*mask)) { 277 for (y = 0; y < 8; y++) { 278 if ((p[x] & (0x80 >> y)) == 0) 279 break; 280 } 281 } 282 return x * 8 + y; 283 } 284 285 static void 286 in_len2mask(mask, len) 287 struct in_addr *mask; 288 int len; 289 { 290 int i; 291 u_char *p; 292 293 p = (u_char *)mask; 294 bzero(mask, sizeof(*mask)); 295 for (i = 0; i < len / 8; i++) 296 p[i] = 0xff; 297 if (len % 8) 298 p[i] = (0xff00 >> (len % 8)) & 0xff; 299 } 300 301 /* 302 * Generic internet control operations (ioctl's). 303 * Ifp is 0 if not an interface-specific ioctl. 304 */ 305 /* ARGSUSED */ 306 int 307 in_control(so, cmd, data, ifp, p) 308 struct socket *so; 309 u_long cmd; 310 caddr_t data; 311 struct ifnet *ifp; 312 struct proc *p; 313 { 314 struct ifreq *ifr = (struct ifreq *)data; 315 struct in_ifaddr *ia = 0; 316 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 317 struct sockaddr_in oldaddr; 318 int error, hostIsNew, maskIsNew; 319 int newifaddr; 320 321 switch (cmd) { 322 case SIOCALIFADDR: 323 case SIOCDLIFADDR: 324 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 325 return(EPERM); 326 /*fall through*/ 327 case SIOCGLIFADDR: 328 if (!ifp) 329 return EINVAL; 330 return in_lifaddr_ioctl(so, cmd, data, ifp, p); 331 } 332 333 /* 334 * Find address for this interface, if it exists. 335 */ 336 if (ifp) 337 IFP_TO_IA(ifp, ia); 338 339 switch (cmd) { 340 341 case SIOCAIFADDR: 342 case SIOCDIFADDR: 343 case SIOCGIFALIAS: 344 if (ifra->ifra_addr.sin_family == AF_INET) 345 for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first; 346 ia != 0; ia = ia->ia_hash.le_next) { 347 if (ia->ia_ifp == ifp && 348 in_hosteq(ia->ia_addr.sin_addr, 349 ifra->ifra_addr.sin_addr)) 350 break; 351 } 352 if (cmd == SIOCDIFADDR) { 353 if (ia == 0) 354 return (EADDRNOTAVAIL); 355 #if 1 /*def COMPAT_43*/ 356 if (ifra->ifra_addr.sin_family == AF_UNSPEC) 357 ifra->ifra_addr.sin_family = AF_INET; 358 #endif 359 } 360 /* FALLTHROUGH */ 361 case SIOCSIFADDR: 362 case SIOCSIFDSTADDR: 363 if (ifra->ifra_addr.sin_family != AF_INET) 364 return (EAFNOSUPPORT); 365 /* FALLTHROUGH */ 366 case SIOCSIFNETMASK: 367 if (ifp == 0) 368 panic("in_control"); 369 370 if (cmd == SIOCGIFALIAS) 371 break; 372 373 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 374 return (EPERM); 375 376 if (ia == 0) { 377 MALLOC(ia, struct in_ifaddr *, sizeof(*ia), 378 M_IFADDR, M_WAITOK); 379 if (ia == 0) 380 return (ENOBUFS); 381 bzero((caddr_t)ia, sizeof *ia); 382 TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list); 383 IFAREF(&ia->ia_ifa); 384 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, 385 ifa_list); 386 IFAREF(&ia->ia_ifa); 387 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 388 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 389 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 390 ia->ia_sockmask.sin_len = 8; 391 if (ifp->if_flags & IFF_BROADCAST) { 392 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 393 ia->ia_broadaddr.sin_family = AF_INET; 394 } 395 ia->ia_ifp = ifp; 396 LIST_INIT(&ia->ia_multiaddrs); 397 newifaddr = 1; 398 } else 399 newifaddr = 0; 400 break; 401 402 case SIOCSIFBRDADDR: 403 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 404 return (EPERM); 405 /* FALLTHROUGH */ 406 407 case SIOCGIFADDR: 408 case SIOCGIFNETMASK: 409 case SIOCGIFDSTADDR: 410 case SIOCGIFBRDADDR: 411 if (ia == 0) 412 return (EADDRNOTAVAIL); 413 break; 414 } 415 switch (cmd) { 416 417 case SIOCGIFADDR: 418 *satosin(&ifr->ifr_addr) = ia->ia_addr; 419 break; 420 421 case SIOCGIFBRDADDR: 422 if ((ifp->if_flags & IFF_BROADCAST) == 0) 423 return (EINVAL); 424 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr; 425 break; 426 427 case SIOCGIFDSTADDR: 428 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 429 return (EINVAL); 430 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr; 431 break; 432 433 case SIOCGIFNETMASK: 434 *satosin(&ifr->ifr_addr) = ia->ia_sockmask; 435 break; 436 437 case SIOCSIFDSTADDR: 438 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 439 return (EINVAL); 440 oldaddr = ia->ia_dstaddr; 441 ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr); 442 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 443 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 444 ia->ia_dstaddr = oldaddr; 445 return (error); 446 } 447 if (ia->ia_flags & IFA_ROUTE) { 448 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr); 449 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 450 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 451 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 452 } 453 break; 454 455 case SIOCSIFBRDADDR: 456 if ((ifp->if_flags & IFF_BROADCAST) == 0) 457 return (EINVAL); 458 ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr); 459 break; 460 461 case SIOCSIFADDR: 462 error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1); 463 #if 0 464 /* 465 * the code chokes if we are to assign multiple addresses with 466 * the same address prefix (rtinit() will return EEXIST, which 467 * is not fatal actually). we will get memory leak if we 468 * don't do it. 469 * -> we may want to hide EEXIST from rtinit(). 470 */ 471 undo: 472 if (error && newifaddr) { 473 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 474 IFAFREE(&ia->ia_ifa); 475 TAILQ_REMOVE(&in_ifaddr, ia, ia_list); 476 IFAFREE(&ia->ia_ifa); 477 } 478 #endif 479 return error; 480 481 case SIOCSIFNETMASK: 482 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr = 483 ifra->ifra_addr.sin_addr.s_addr; 484 break; 485 486 case SIOCAIFADDR: 487 maskIsNew = 0; 488 hostIsNew = 1; 489 error = 0; 490 if (ia->ia_addr.sin_family == AF_INET) { 491 if (ifra->ifra_addr.sin_len == 0) { 492 ifra->ifra_addr = ia->ia_addr; 493 hostIsNew = 0; 494 } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr)) 495 hostIsNew = 0; 496 } 497 if (ifra->ifra_mask.sin_len) { 498 in_ifscrub(ifp, ia); 499 ia->ia_sockmask = ifra->ifra_mask; 500 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 501 maskIsNew = 1; 502 } 503 if ((ifp->if_flags & IFF_POINTOPOINT) && 504 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 505 in_ifscrub(ifp, ia); 506 ia->ia_dstaddr = ifra->ifra_dstaddr; 507 maskIsNew = 1; /* We lie; but the effect's the same */ 508 } 509 if (ifra->ifra_addr.sin_family == AF_INET && 510 (hostIsNew || maskIsNew)) { 511 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 512 #if 0 513 if (error) 514 goto undo; 515 #endif 516 } 517 if ((ifp->if_flags & IFF_BROADCAST) && 518 (ifra->ifra_broadaddr.sin_family == AF_INET)) 519 ia->ia_broadaddr = ifra->ifra_broadaddr; 520 return (error); 521 522 case SIOCGIFALIAS: 523 ifra->ifra_mask = ia->ia_sockmask; 524 if ((ifp->if_flags & IFF_POINTOPOINT) && 525 (ia->ia_dstaddr.sin_family == AF_INET)) 526 ifra->ifra_dstaddr = ia->ia_dstaddr; 527 else if ((ifp->if_flags & IFF_BROADCAST) && 528 (ia->ia_broadaddr.sin_family == AF_INET)) 529 ifra->ifra_broadaddr = ia->ia_broadaddr; 530 else 531 bzero(&ifra->ifra_broadaddr, 532 sizeof(ifra->ifra_broadaddr)); 533 return 0; 534 535 case SIOCDIFADDR: 536 in_purgeaddr(&ia->ia_ifa, ifp); 537 break; 538 539 #ifdef MROUTING 540 case SIOCGETVIFCNT: 541 case SIOCGETSGCNT: 542 return (mrt_ioctl(so, cmd, data)); 543 #endif /* MROUTING */ 544 545 default: 546 if (ifp == 0 || ifp->if_ioctl == 0) 547 return (EOPNOTSUPP); 548 error = (*ifp->if_ioctl)(ifp, cmd, data); 549 in_setmaxmtu(); 550 return(error); 551 } 552 return (0); 553 } 554 555 void 556 in_purgeaddr(ifa, ifp) 557 struct ifaddr *ifa; 558 struct ifnet *ifp; 559 { 560 struct in_ifaddr *ia = (void *) ifa; 561 562 in_ifscrub(ifp, ia); 563 LIST_REMOVE(ia, ia_hash); 564 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 565 IFAFREE(&ia->ia_ifa); 566 TAILQ_REMOVE(&in_ifaddr, ia, ia_list); 567 if (ia->ia_allhosts != NULL) 568 in_delmulti(ia->ia_allhosts); 569 if (LIST_FIRST(&ia->ia_multiaddrs) != NULL && 570 /* 571 * If the interface is going away, don't bother to save 572 * the multicast entries. 573 */ 574 ifp->if_output != if_nulloutput) 575 in_savemkludge(ia); 576 IFAFREE(&ia->ia_ifa); 577 in_setmaxmtu(); 578 } 579 580 void 581 in_purgeif(ifp) 582 struct ifnet *ifp; 583 { 584 struct ifaddr *ifa, *nifa; 585 586 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 587 nifa = TAILQ_NEXT(ifa, ifa_list); 588 if (ifa->ifa_addr->sa_family != AF_INET) 589 continue; 590 in_purgeaddr(ifa, ifp); 591 } 592 in_purgemkludge(ifp); 593 } 594 595 /* 596 * SIOC[GAD]LIFADDR. 597 * SIOCGLIFADDR: get first address. (???) 598 * SIOCGLIFADDR with IFLR_PREFIX: 599 * get first address that matches the specified prefix. 600 * SIOCALIFADDR: add the specified address. 601 * SIOCALIFADDR with IFLR_PREFIX: 602 * EINVAL since we can't deduce hostid part of the address. 603 * SIOCDLIFADDR: delete the specified address. 604 * SIOCDLIFADDR with IFLR_PREFIX: 605 * delete the first address that matches the specified prefix. 606 * return values: 607 * EINVAL on invalid parameters 608 * EADDRNOTAVAIL on prefix match failed/specified address not found 609 * other values may be returned from in_ioctl() 610 */ 611 static int 612 in_lifaddr_ioctl(so, cmd, data, ifp, p) 613 struct socket *so; 614 u_long cmd; 615 caddr_t data; 616 struct ifnet *ifp; 617 struct proc *p; 618 { 619 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 620 struct ifaddr *ifa; 621 struct sockaddr *sa; 622 623 /* sanity checks */ 624 if (!data || !ifp) { 625 panic("invalid argument to in_lifaddr_ioctl"); 626 /*NOTRECHED*/ 627 } 628 629 switch (cmd) { 630 case SIOCGLIFADDR: 631 /* address must be specified on GET with IFLR_PREFIX */ 632 if ((iflr->flags & IFLR_PREFIX) == 0) 633 break; 634 /*FALLTHROUGH*/ 635 case SIOCALIFADDR: 636 case SIOCDLIFADDR: 637 /* address must be specified on ADD and DELETE */ 638 sa = (struct sockaddr *)&iflr->addr; 639 if (sa->sa_family != AF_INET) 640 return EINVAL; 641 if (sa->sa_len != sizeof(struct sockaddr_in)) 642 return EINVAL; 643 /* XXX need improvement */ 644 sa = (struct sockaddr *)&iflr->dstaddr; 645 if (sa->sa_family 646 && sa->sa_family != AF_INET) 647 return EINVAL; 648 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in)) 649 return EINVAL; 650 break; 651 default: /*shouldn't happen*/ 652 #if 0 653 panic("invalid cmd to in_lifaddr_ioctl"); 654 /*NOTREACHED*/ 655 #else 656 return EOPNOTSUPP; 657 #endif 658 } 659 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 660 return EINVAL; 661 662 switch (cmd) { 663 case SIOCALIFADDR: 664 { 665 struct in_aliasreq ifra; 666 667 if (iflr->flags & IFLR_PREFIX) 668 return EINVAL; 669 670 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 671 bzero(&ifra, sizeof(ifra)); 672 bcopy(iflr->iflr_name, ifra.ifra_name, 673 sizeof(ifra.ifra_name)); 674 675 bcopy(&iflr->addr, &ifra.ifra_addr, 676 ((struct sockaddr *)&iflr->addr)->sa_len); 677 678 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 679 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 680 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 681 } 682 683 ifra.ifra_mask.sin_family = AF_INET; 684 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 685 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 686 687 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p); 688 } 689 case SIOCGLIFADDR: 690 case SIOCDLIFADDR: 691 { 692 struct in_ifaddr *ia; 693 struct in_addr mask, candidate, match; 694 struct sockaddr_in *sin; 695 int cmp; 696 697 bzero(&mask, sizeof(mask)); 698 if (iflr->flags & IFLR_PREFIX) { 699 /* lookup a prefix rather than address. */ 700 in_len2mask(&mask, iflr->prefixlen); 701 702 sin = (struct sockaddr_in *)&iflr->addr; 703 match.s_addr = sin->sin_addr.s_addr; 704 match.s_addr &= mask.s_addr; 705 706 /* if you set extra bits, that's wrong */ 707 if (match.s_addr != sin->sin_addr.s_addr) 708 return EINVAL; 709 710 cmp = 1; 711 } else { 712 if (cmd == SIOCGLIFADDR) { 713 /* on getting an address, take the 1st match */ 714 cmp = 0; /*XXX*/ 715 } else { 716 /* on deleting an address, do exact match */ 717 in_len2mask(&mask, 32); 718 sin = (struct sockaddr_in *)&iflr->addr; 719 match.s_addr = sin->sin_addr.s_addr; 720 721 cmp = 1; 722 } 723 } 724 725 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) { 726 if (ifa->ifa_addr->sa_family != AF_INET6) 727 continue; 728 if (!cmp) 729 break; 730 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 731 candidate.s_addr &= mask.s_addr; 732 if (candidate.s_addr == match.s_addr) 733 break; 734 } 735 if (!ifa) 736 return EADDRNOTAVAIL; 737 ia = (struct in_ifaddr *)ifa; 738 739 if (cmd == SIOCGLIFADDR) { 740 /* fill in the if_laddrreq structure */ 741 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 742 743 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 744 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 745 ia->ia_dstaddr.sin_len); 746 } else 747 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 748 749 iflr->prefixlen = 750 in_mask2len(&ia->ia_sockmask.sin_addr); 751 752 iflr->flags = 0; /*XXX*/ 753 754 return 0; 755 } else { 756 struct in_aliasreq ifra; 757 758 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 759 bzero(&ifra, sizeof(ifra)); 760 bcopy(iflr->iflr_name, ifra.ifra_name, 761 sizeof(ifra.ifra_name)); 762 763 bcopy(&ia->ia_addr, &ifra.ifra_addr, 764 ia->ia_addr.sin_len); 765 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 766 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 767 ia->ia_dstaddr.sin_len); 768 } 769 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 770 ia->ia_sockmask.sin_len); 771 772 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 773 ifp, p); 774 } 775 } 776 } 777 778 return EOPNOTSUPP; /*just for safety*/ 779 } 780 781 /* 782 * Delete any existing route for an interface. 783 */ 784 void 785 in_ifscrub(ifp, ia) 786 struct ifnet *ifp; 787 struct in_ifaddr *ia; 788 { 789 790 if ((ia->ia_flags & IFA_ROUTE) == 0) 791 return; 792 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 793 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 794 else 795 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 796 ia->ia_flags &= ~IFA_ROUTE; 797 } 798 799 /* 800 * Initialize an interface's internet address 801 * and routing table entry. 802 */ 803 int 804 in_ifinit(ifp, ia, sin, scrub) 805 struct ifnet *ifp; 806 struct in_ifaddr *ia; 807 struct sockaddr_in *sin; 808 int scrub; 809 { 810 u_int32_t i = sin->sin_addr.s_addr; 811 struct sockaddr_in oldaddr; 812 int s = splnet(), flags = RTF_UP, error; 813 814 /* 815 * Set up new addresses. 816 */ 817 oldaddr = ia->ia_addr; 818 if (ia->ia_addr.sin_family == AF_INET) 819 LIST_REMOVE(ia, ia_hash); 820 ia->ia_addr = *sin; 821 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 822 823 /* 824 * Give the interface a chance to initialize 825 * if this is its first address, 826 * and to validate the address if necessary. 827 */ 828 if (ifp->if_ioctl && 829 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) 830 goto bad; 831 splx(s); 832 if (scrub) { 833 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 834 in_ifscrub(ifp, ia); 835 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 836 } 837 838 if (IN_CLASSA(i)) 839 ia->ia_netmask = IN_CLASSA_NET; 840 else if (IN_CLASSB(i)) 841 ia->ia_netmask = IN_CLASSB_NET; 842 else 843 ia->ia_netmask = IN_CLASSC_NET; 844 /* 845 * The subnet mask usually includes at least the standard network part, 846 * but may may be smaller in the case of supernetting. 847 * If it is set, we believe it. 848 */ 849 if (ia->ia_subnetmask == 0) { 850 ia->ia_subnetmask = ia->ia_netmask; 851 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 852 } else 853 ia->ia_netmask &= ia->ia_subnetmask; 854 855 ia->ia_net = i & ia->ia_netmask; 856 ia->ia_subnet = i & ia->ia_subnetmask; 857 in_socktrim(&ia->ia_sockmask); 858 /* re-calculate the "in_maxmtu" value */ 859 in_setmaxmtu(); 860 /* 861 * Add route for the network. 862 */ 863 ia->ia_ifa.ifa_metric = ifp->if_metric; 864 if (ifp->if_flags & IFF_BROADCAST) { 865 ia->ia_broadaddr.sin_addr.s_addr = 866 ia->ia_subnet | ~ia->ia_subnetmask; 867 ia->ia_netbroadcast.s_addr = 868 ia->ia_net | ~ia->ia_netmask; 869 } else if (ifp->if_flags & IFF_LOOPBACK) { 870 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 871 flags |= RTF_HOST; 872 } else if (ifp->if_flags & IFF_POINTOPOINT) { 873 if (ia->ia_dstaddr.sin_family != AF_INET) 874 return (0); 875 flags |= RTF_HOST; 876 } 877 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags); 878 if (!error) 879 ia->ia_flags |= IFA_ROUTE; 880 /* XXX check if the subnet route points to the same interface */ 881 if (error == EEXIST) 882 error = 0; 883 /* 884 * recover multicast kludge entry, if there is. 885 */ 886 if (ifp->if_flags & IFF_MULTICAST) 887 in_restoremkludge(ia, ifp); 888 /* 889 * If the interface supports multicast, join the "all hosts" 890 * multicast group on that interface. 891 */ 892 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 893 struct in_addr addr; 894 895 addr.s_addr = INADDR_ALLHOSTS_GROUP; 896 ia->ia_allhosts = in_addmulti(&addr, ifp); 897 } 898 return (error); 899 bad: 900 splx(s); 901 LIST_REMOVE(ia, ia_hash); 902 ia->ia_addr = oldaddr; 903 if (ia->ia_addr.sin_family == AF_INET) 904 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), 905 ia, ia_hash); 906 return (error); 907 } 908 909 /* 910 * Return 1 if the address might be a local broadcast address. 911 */ 912 int 913 in_broadcast(in, ifp) 914 struct in_addr in; 915 struct ifnet *ifp; 916 { 917 struct ifaddr *ifa; 918 919 if (in.s_addr == INADDR_BROADCAST || 920 in_nullhost(in)) 921 return 1; 922 if ((ifp->if_flags & IFF_BROADCAST) == 0) 923 return 0; 924 /* 925 * Look through the list of addresses for a match 926 * with a broadcast address. 927 */ 928 #define ia (ifatoia(ifa)) 929 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 930 if (ifa->ifa_addr->sa_family == AF_INET && 931 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 932 in_hosteq(in, ia->ia_netbroadcast) || 933 (hostzeroisbroadcast && 934 /* 935 * Check for old-style (host 0) broadcast. 936 */ 937 (in.s_addr == ia->ia_subnet || 938 in.s_addr == ia->ia_net)))) 939 return 1; 940 return (0); 941 #undef ia 942 } 943 944 /* 945 * Multicast address kludge: 946 * If there were any multicast addresses attached to this interface address, 947 * either move them to another address on this interface, or save them until 948 * such time as this interface is reconfigured for IPv4. 949 */ 950 void 951 in_savemkludge(oia) 952 struct in_ifaddr *oia; 953 { 954 struct in_ifaddr *ia; 955 struct in_multi *inm, *next; 956 957 IFP_TO_IA(oia->ia_ifp, ia); 958 if (ia) { /* there is another address */ 959 for (inm = oia->ia_multiaddrs.lh_first; inm; inm = next){ 960 next = inm->inm_list.le_next; 961 IFAFREE(&inm->inm_ia->ia_ifa); 962 IFAREF(&ia->ia_ifa); 963 inm->inm_ia = ia; 964 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list); 965 } 966 } else { /* last address on this if deleted, save */ 967 TAILQ_INSERT_TAIL(&in_mk, oia, ia_list); 968 IFAREF(&oia->ia_ifa); 969 } 970 } 971 972 /* 973 * Continuation of multicast address hack: 974 * If there was a multicast group list previously saved for this interface, 975 * then we re-attach it to the first address configured on the i/f. 976 */ 977 void 978 in_restoremkludge(ia, ifp) 979 struct in_ifaddr *ia; 980 struct ifnet *ifp; 981 { 982 struct in_ifaddr *oia; 983 984 for (oia = TAILQ_FIRST(&in_mk); oia != NULL; 985 oia = TAILQ_NEXT(oia, ia_list)) { 986 if (oia->ia_ifp == ifp) { 987 struct in_multi *inm, *next; 988 989 for (inm = LIST_FIRST(&oia->ia_multiaddrs); 990 inm != NULL; inm = next) { 991 next = LIST_NEXT(inm, inm_list); 992 IFAFREE(&inm->inm_ia->ia_ifa); 993 IFAREF(&ia->ia_ifa); 994 inm->inm_ia = ia; 995 LIST_INSERT_HEAD(&ia->ia_multiaddrs, 996 inm, inm_list); 997 } 998 TAILQ_REMOVE(&in_mk, oia, ia_list); 999 IFAFREE(&oia->ia_ifa); 1000 break; 1001 } 1002 } 1003 } 1004 1005 void 1006 in_purgemkludge(ifp) 1007 struct ifnet *ifp; 1008 { 1009 struct in_ifaddr *oia; 1010 1011 for (oia = TAILQ_FIRST(&in_mk); oia != NULL; 1012 oia = TAILQ_NEXT(oia, ia_list)) { 1013 if (oia->ia_ifp != ifp) 1014 continue; 1015 1016 /* 1017 * Leaving from all multicast groups joined through 1018 * this interface is done via in_pcbpurgeif(). 1019 */ 1020 1021 TAILQ_REMOVE(&in_mk, oia, ia_list); 1022 IFAFREE(&oia->ia_ifa); 1023 break; 1024 } 1025 } 1026 1027 /* 1028 * Add an address to the list of IP multicast addresses for a given interface. 1029 */ 1030 struct in_multi * 1031 in_addmulti(ap, ifp) 1032 struct in_addr *ap; 1033 struct ifnet *ifp; 1034 { 1035 struct in_multi *inm; 1036 struct ifreq ifr; 1037 struct in_ifaddr *ia; 1038 int s = splsoftnet(); 1039 1040 /* 1041 * See if address already in list. 1042 */ 1043 IN_LOOKUP_MULTI(*ap, ifp, inm); 1044 if (inm != NULL) { 1045 /* 1046 * Found it; just increment the reference count. 1047 */ 1048 ++inm->inm_refcount; 1049 } else { 1050 /* 1051 * New address; allocate a new multicast record 1052 * and link it into the interface's multicast list. 1053 */ 1054 inm = (struct in_multi *)malloc(sizeof(*inm), 1055 M_IPMADDR, M_NOWAIT); 1056 if (inm == NULL) { 1057 splx(s); 1058 return (NULL); 1059 } 1060 inm->inm_addr = *ap; 1061 inm->inm_ifp = ifp; 1062 inm->inm_refcount = 1; 1063 IFP_TO_IA(ifp, ia); 1064 if (ia == NULL) { 1065 free(inm, M_IPMADDR); 1066 splx(s); 1067 return (NULL); 1068 } 1069 inm->inm_ia = ia; 1070 IFAREF(&inm->inm_ia->ia_ifa); 1071 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list); 1072 /* 1073 * Ask the network driver to update its multicast reception 1074 * filter appropriately for the new address. 1075 */ 1076 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in); 1077 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 1078 satosin(&ifr.ifr_addr)->sin_addr = *ap; 1079 if ((ifp->if_ioctl == NULL) || 1080 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 1081 LIST_REMOVE(inm, inm_list); 1082 free(inm, M_IPMADDR); 1083 splx(s); 1084 return (NULL); 1085 } 1086 /* 1087 * Let IGMP know that we have joined a new IP multicast group. 1088 */ 1089 igmp_joingroup(inm); 1090 } 1091 splx(s); 1092 return (inm); 1093 } 1094 1095 /* 1096 * Delete a multicast address record. 1097 */ 1098 void 1099 in_delmulti(inm) 1100 struct in_multi *inm; 1101 { 1102 struct ifreq ifr; 1103 int s = splsoftnet(); 1104 1105 if (--inm->inm_refcount == 0) { 1106 /* 1107 * No remaining claims to this record; let IGMP know that 1108 * we are leaving the multicast group. 1109 */ 1110 igmp_leavegroup(inm); 1111 /* 1112 * Unlink from list. 1113 */ 1114 LIST_REMOVE(inm, inm_list); 1115 IFAFREE(&inm->inm_ia->ia_ifa); 1116 /* 1117 * Notify the network driver to update its multicast reception 1118 * filter. 1119 */ 1120 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 1121 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr; 1122 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 1123 (caddr_t)&ifr); 1124 free(inm, M_IPMADDR); 1125 } 1126 splx(s); 1127 } 1128 #endif 1129