1 /* $NetBSD: in.c,v 1.76 2002/05/09 06:49:15 itojun 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 <sys/cdefs.h> 105 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.76 2002/05/09 06:49:15 itojun Exp $"); 106 107 #include "opt_inet.h" 108 #include "opt_inet_conf.h" 109 #include "opt_mrouting.h" 110 111 #include <sys/param.h> 112 #include <sys/ioctl.h> 113 #include <sys/errno.h> 114 #include <sys/malloc.h> 115 #include <sys/socket.h> 116 #include <sys/socketvar.h> 117 #include <sys/systm.h> 118 #include <sys/proc.h> 119 #include <sys/syslog.h> 120 121 #include <net/if.h> 122 #include <net/route.h> 123 124 #include <net/if_ether.h> 125 126 #include <netinet/in_systm.h> 127 #include <netinet/in.h> 128 #include <netinet/in_var.h> 129 #include <netinet/if_inarp.h> 130 #include <netinet/ip_mroute.h> 131 #include <netinet/igmp_var.h> 132 133 #ifdef INET 134 135 static int in_mask2len __P((struct in_addr *)); 136 static void in_len2mask __P((struct in_addr *, int)); 137 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 138 struct ifnet *, struct proc *)); 139 140 static int in_addprefix __P((struct in_ifaddr *, int)); 141 static int in_scrubprefix __P((struct in_ifaddr *)); 142 143 #ifndef SUBNETSARELOCAL 144 #define SUBNETSARELOCAL 1 145 #endif 146 147 #ifndef HOSTZEROBROADCAST 148 #define HOSTZEROBROADCAST 1 149 #endif 150 151 int subnetsarelocal = SUBNETSARELOCAL; 152 int hostzeroisbroadcast = HOSTZEROBROADCAST; 153 154 /* 155 * This list is used to keep track of in_multi chains which belong to 156 * deleted interface addresses. We use in_ifaddr so that a chain head 157 * won't be deallocated until all multicast address record are deleted. 158 */ 159 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk); 160 161 /* 162 * Return 1 if an internet address is for a ``local'' host 163 * (one to which we have a connection). If subnetsarelocal 164 * is true, this includes other subnets of the local net. 165 * Otherwise, it includes only the directly-connected (sub)nets. 166 */ 167 int 168 in_localaddr(in) 169 struct in_addr in; 170 { 171 struct in_ifaddr *ia; 172 173 if (subnetsarelocal) { 174 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) 175 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) 176 return (1); 177 } else { 178 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) 179 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) 180 return (1); 181 } 182 return (0); 183 } 184 185 /* 186 * Determine whether an IP address is in a reserved set of addresses 187 * that may not be forwarded, or whether datagrams to that destination 188 * may be forwarded. 189 */ 190 int 191 in_canforward(in) 192 struct in_addr in; 193 { 194 u_int32_t net; 195 196 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 197 return (0); 198 if (IN_CLASSA(in.s_addr)) { 199 net = in.s_addr & IN_CLASSA_NET; 200 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 201 return (0); 202 } 203 return (1); 204 } 205 206 /* 207 * Trim a mask in a sockaddr 208 */ 209 void 210 in_socktrim(ap) 211 struct sockaddr_in *ap; 212 { 213 char *cplim = (char *) &ap->sin_addr; 214 char *cp = (char *) (&ap->sin_addr + 1); 215 216 ap->sin_len = 0; 217 while (--cp >= cplim) 218 if (*cp) { 219 (ap)->sin_len = cp - (char *) (ap) + 1; 220 break; 221 } 222 } 223 224 /* 225 * Routine to take an Internet address and convert into a 226 * "dotted quad" representation for printing. 227 */ 228 const char * 229 in_fmtaddr(addr) 230 struct in_addr addr; 231 { 232 static char buf[sizeof("123.456.789.123")]; 233 234 addr.s_addr = ntohl(addr.s_addr); 235 236 sprintf(buf, "%d.%d.%d.%d", 237 (addr.s_addr >> 24) & 0xFF, 238 (addr.s_addr >> 16) & 0xFF, 239 (addr.s_addr >> 8) & 0xFF, 240 (addr.s_addr >> 0) & 0xFF); 241 return buf; 242 } 243 244 /* 245 * Maintain the "in_maxmtu" variable, which is the largest 246 * mtu for non-local interfaces with AF_INET addresses assigned 247 * to them that are up. 248 */ 249 unsigned long in_maxmtu; 250 251 void 252 in_setmaxmtu() 253 { 254 struct in_ifaddr *ia; 255 struct ifnet *ifp; 256 unsigned long maxmtu = 0; 257 258 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 259 if ((ifp = ia->ia_ifp) == 0) 260 continue; 261 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP) 262 continue; 263 if (ifp->if_mtu > maxmtu) 264 maxmtu = ifp->if_mtu; 265 } 266 if (maxmtu) 267 in_maxmtu = maxmtu; 268 } 269 270 static int 271 in_mask2len(mask) 272 struct in_addr *mask; 273 { 274 int x, y; 275 u_char *p; 276 277 p = (u_char *)mask; 278 for (x = 0; x < sizeof(*mask); x++) { 279 if (p[x] != 0xff) 280 break; 281 } 282 y = 0; 283 if (x < sizeof(*mask)) { 284 for (y = 0; y < 8; y++) { 285 if ((p[x] & (0x80 >> y)) == 0) 286 break; 287 } 288 } 289 return x * 8 + y; 290 } 291 292 static void 293 in_len2mask(mask, len) 294 struct in_addr *mask; 295 int len; 296 { 297 int i; 298 u_char *p; 299 300 p = (u_char *)mask; 301 bzero(mask, sizeof(*mask)); 302 for (i = 0; i < len / 8; i++) 303 p[i] = 0xff; 304 if (len % 8) 305 p[i] = (0xff00 >> (len % 8)) & 0xff; 306 } 307 308 /* 309 * Generic internet control operations (ioctl's). 310 * Ifp is 0 if not an interface-specific ioctl. 311 */ 312 /* ARGSUSED */ 313 int 314 in_control(so, cmd, data, ifp, p) 315 struct socket *so; 316 u_long cmd; 317 caddr_t data; 318 struct ifnet *ifp; 319 struct proc *p; 320 { 321 struct ifreq *ifr = (struct ifreq *)data; 322 struct in_ifaddr *ia = 0; 323 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 324 struct sockaddr_in oldaddr; 325 int error, hostIsNew, maskIsNew; 326 int newifaddr; 327 328 switch (cmd) { 329 case SIOCALIFADDR: 330 case SIOCDLIFADDR: 331 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 332 return(EPERM); 333 /*fall through*/ 334 case SIOCGLIFADDR: 335 if (!ifp) 336 return EINVAL; 337 return in_lifaddr_ioctl(so, cmd, data, ifp, p); 338 } 339 340 /* 341 * Find address for this interface, if it exists. 342 */ 343 if (ifp) 344 IFP_TO_IA(ifp, ia); 345 346 switch (cmd) { 347 348 case SIOCAIFADDR: 349 case SIOCDIFADDR: 350 case SIOCGIFALIAS: 351 if (ifra->ifra_addr.sin_family == AF_INET) 352 LIST_FOREACH(ia, 353 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr), 354 ia_hash) { 355 if (ia->ia_ifp == ifp && 356 in_hosteq(ia->ia_addr.sin_addr, 357 ifra->ifra_addr.sin_addr)) 358 break; 359 } 360 if (cmd == SIOCDIFADDR) { 361 if (ia == 0) 362 return (EADDRNOTAVAIL); 363 #if 1 /*def COMPAT_43*/ 364 if (ifra->ifra_addr.sin_family == AF_UNSPEC) 365 ifra->ifra_addr.sin_family = AF_INET; 366 #endif 367 } 368 /* FALLTHROUGH */ 369 case SIOCSIFADDR: 370 case SIOCSIFDSTADDR: 371 if (ifra->ifra_addr.sin_family != AF_INET) 372 return (EAFNOSUPPORT); 373 /* FALLTHROUGH */ 374 case SIOCSIFNETMASK: 375 if (ifp == 0) 376 panic("in_control"); 377 378 if (cmd == SIOCGIFALIAS) 379 break; 380 381 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 382 return (EPERM); 383 384 if (ia == 0) { 385 MALLOC(ia, struct in_ifaddr *, sizeof(*ia), 386 M_IFADDR, M_WAITOK); 387 if (ia == 0) 388 return (ENOBUFS); 389 bzero((caddr_t)ia, sizeof *ia); 390 TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list); 391 IFAREF(&ia->ia_ifa); 392 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, 393 ifa_list); 394 IFAREF(&ia->ia_ifa); 395 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 396 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 397 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 398 ia->ia_sockmask.sin_len = 8; 399 if (ifp->if_flags & IFF_BROADCAST) { 400 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 401 ia->ia_broadaddr.sin_family = AF_INET; 402 } 403 ia->ia_ifp = ifp; 404 LIST_INIT(&ia->ia_multiaddrs); 405 newifaddr = 1; 406 } else 407 newifaddr = 0; 408 break; 409 410 case SIOCSIFBRDADDR: 411 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 412 return (EPERM); 413 /* FALLTHROUGH */ 414 415 case SIOCGIFADDR: 416 case SIOCGIFNETMASK: 417 case SIOCGIFDSTADDR: 418 case SIOCGIFBRDADDR: 419 if (ia == 0) 420 return (EADDRNOTAVAIL); 421 break; 422 } 423 switch (cmd) { 424 425 case SIOCGIFADDR: 426 *satosin(&ifr->ifr_addr) = ia->ia_addr; 427 break; 428 429 case SIOCGIFBRDADDR: 430 if ((ifp->if_flags & IFF_BROADCAST) == 0) 431 return (EINVAL); 432 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr; 433 break; 434 435 case SIOCGIFDSTADDR: 436 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 437 return (EINVAL); 438 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr; 439 break; 440 441 case SIOCGIFNETMASK: 442 *satosin(&ifr->ifr_addr) = ia->ia_sockmask; 443 break; 444 445 case SIOCSIFDSTADDR: 446 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 447 return (EINVAL); 448 oldaddr = ia->ia_dstaddr; 449 ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr); 450 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 451 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 452 ia->ia_dstaddr = oldaddr; 453 return (error); 454 } 455 if (ia->ia_flags & IFA_ROUTE) { 456 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr); 457 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 458 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 459 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 460 } 461 break; 462 463 case SIOCSIFBRDADDR: 464 if ((ifp->if_flags & IFF_BROADCAST) == 0) 465 return (EINVAL); 466 ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr); 467 break; 468 469 case SIOCSIFADDR: 470 error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1); 471 return error; 472 473 case SIOCSIFNETMASK: 474 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr = 475 ifra->ifra_addr.sin_addr.s_addr; 476 break; 477 478 case SIOCAIFADDR: 479 maskIsNew = 0; 480 hostIsNew = 1; 481 error = 0; 482 if (ia->ia_addr.sin_family == AF_INET) { 483 if (ifra->ifra_addr.sin_len == 0) { 484 ifra->ifra_addr = ia->ia_addr; 485 hostIsNew = 0; 486 } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr)) 487 hostIsNew = 0; 488 } 489 if (ifra->ifra_mask.sin_len) { 490 in_ifscrub(ifp, ia); 491 ia->ia_sockmask = ifra->ifra_mask; 492 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 493 maskIsNew = 1; 494 } 495 if ((ifp->if_flags & IFF_POINTOPOINT) && 496 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 497 in_ifscrub(ifp, ia); 498 ia->ia_dstaddr = ifra->ifra_dstaddr; 499 maskIsNew = 1; /* We lie; but the effect's the same */ 500 } 501 if (ifra->ifra_addr.sin_family == AF_INET && 502 (hostIsNew || maskIsNew)) { 503 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 504 } 505 if ((ifp->if_flags & IFF_BROADCAST) && 506 (ifra->ifra_broadaddr.sin_family == AF_INET)) 507 ia->ia_broadaddr = ifra->ifra_broadaddr; 508 return (error); 509 510 case SIOCGIFALIAS: 511 ifra->ifra_mask = ia->ia_sockmask; 512 if ((ifp->if_flags & IFF_POINTOPOINT) && 513 (ia->ia_dstaddr.sin_family == AF_INET)) 514 ifra->ifra_dstaddr = ia->ia_dstaddr; 515 else if ((ifp->if_flags & IFF_BROADCAST) && 516 (ia->ia_broadaddr.sin_family == AF_INET)) 517 ifra->ifra_broadaddr = ia->ia_broadaddr; 518 else 519 bzero(&ifra->ifra_broadaddr, 520 sizeof(ifra->ifra_broadaddr)); 521 return 0; 522 523 case SIOCDIFADDR: 524 in_purgeaddr(&ia->ia_ifa, ifp); 525 break; 526 527 #ifdef MROUTING 528 case SIOCGETVIFCNT: 529 case SIOCGETSGCNT: 530 return (mrt_ioctl(so, cmd, data)); 531 #endif /* MROUTING */ 532 533 default: 534 if (ifp == 0 || ifp->if_ioctl == 0) 535 return (EOPNOTSUPP); 536 error = (*ifp->if_ioctl)(ifp, cmd, data); 537 in_setmaxmtu(); 538 return(error); 539 } 540 return (0); 541 } 542 543 void 544 in_purgeaddr(ifa, ifp) 545 struct ifaddr *ifa; 546 struct ifnet *ifp; 547 { 548 struct in_ifaddr *ia = (void *) ifa; 549 550 in_ifscrub(ifp, ia); 551 LIST_REMOVE(ia, ia_hash); 552 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 553 IFAFREE(&ia->ia_ifa); 554 TAILQ_REMOVE(&in_ifaddr, ia, ia_list); 555 if (ia->ia_allhosts != NULL) 556 in_delmulti(ia->ia_allhosts); 557 if (LIST_FIRST(&ia->ia_multiaddrs) != NULL && 558 /* 559 * If the interface is going away, don't bother to save 560 * the multicast entries. 561 */ 562 ifp->if_output != if_nulloutput) 563 in_savemkludge(ia); 564 IFAFREE(&ia->ia_ifa); 565 in_setmaxmtu(); 566 } 567 568 void 569 in_purgeif(ifp) 570 struct ifnet *ifp; 571 { 572 struct ifaddr *ifa, *nifa; 573 574 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 575 nifa = TAILQ_NEXT(ifa, ifa_list); 576 if (ifa->ifa_addr->sa_family != AF_INET) 577 continue; 578 in_purgeaddr(ifa, ifp); 579 } 580 in_purgemkludge(ifp); 581 } 582 583 /* 584 * SIOC[GAD]LIFADDR. 585 * SIOCGLIFADDR: get first address. (???) 586 * SIOCGLIFADDR with IFLR_PREFIX: 587 * get first address that matches the specified prefix. 588 * SIOCALIFADDR: add the specified address. 589 * SIOCALIFADDR with IFLR_PREFIX: 590 * EINVAL since we can't deduce hostid part of the address. 591 * SIOCDLIFADDR: delete the specified address. 592 * SIOCDLIFADDR with IFLR_PREFIX: 593 * delete the first address that matches the specified prefix. 594 * return values: 595 * EINVAL on invalid parameters 596 * EADDRNOTAVAIL on prefix match failed/specified address not found 597 * other values may be returned from in_ioctl() 598 */ 599 static int 600 in_lifaddr_ioctl(so, cmd, data, ifp, p) 601 struct socket *so; 602 u_long cmd; 603 caddr_t data; 604 struct ifnet *ifp; 605 struct proc *p; 606 { 607 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 608 struct ifaddr *ifa; 609 struct sockaddr *sa; 610 611 /* sanity checks */ 612 if (!data || !ifp) { 613 panic("invalid argument to in_lifaddr_ioctl"); 614 /*NOTRECHED*/ 615 } 616 617 switch (cmd) { 618 case SIOCGLIFADDR: 619 /* address must be specified on GET with IFLR_PREFIX */ 620 if ((iflr->flags & IFLR_PREFIX) == 0) 621 break; 622 /*FALLTHROUGH*/ 623 case SIOCALIFADDR: 624 case SIOCDLIFADDR: 625 /* address must be specified on ADD and DELETE */ 626 sa = (struct sockaddr *)&iflr->addr; 627 if (sa->sa_family != AF_INET) 628 return EINVAL; 629 if (sa->sa_len != sizeof(struct sockaddr_in)) 630 return EINVAL; 631 /* XXX need improvement */ 632 sa = (struct sockaddr *)&iflr->dstaddr; 633 if (sa->sa_family 634 && sa->sa_family != AF_INET) 635 return EINVAL; 636 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in)) 637 return EINVAL; 638 break; 639 default: /*shouldn't happen*/ 640 #if 0 641 panic("invalid cmd to in_lifaddr_ioctl"); 642 /*NOTREACHED*/ 643 #else 644 return EOPNOTSUPP; 645 #endif 646 } 647 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 648 return EINVAL; 649 650 switch (cmd) { 651 case SIOCALIFADDR: 652 { 653 struct in_aliasreq ifra; 654 655 if (iflr->flags & IFLR_PREFIX) 656 return EINVAL; 657 658 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 659 bzero(&ifra, sizeof(ifra)); 660 bcopy(iflr->iflr_name, ifra.ifra_name, 661 sizeof(ifra.ifra_name)); 662 663 bcopy(&iflr->addr, &ifra.ifra_addr, 664 ((struct sockaddr *)&iflr->addr)->sa_len); 665 666 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 667 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 668 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 669 } 670 671 ifra.ifra_mask.sin_family = AF_INET; 672 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 673 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 674 675 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p); 676 } 677 case SIOCGLIFADDR: 678 case SIOCDLIFADDR: 679 { 680 struct in_ifaddr *ia; 681 struct in_addr mask, candidate, match; 682 struct sockaddr_in *sin; 683 int cmp; 684 685 bzero(&mask, sizeof(mask)); 686 if (iflr->flags & IFLR_PREFIX) { 687 /* lookup a prefix rather than address. */ 688 in_len2mask(&mask, iflr->prefixlen); 689 690 sin = (struct sockaddr_in *)&iflr->addr; 691 match.s_addr = sin->sin_addr.s_addr; 692 match.s_addr &= mask.s_addr; 693 694 /* if you set extra bits, that's wrong */ 695 if (match.s_addr != sin->sin_addr.s_addr) 696 return EINVAL; 697 698 cmp = 1; 699 } else { 700 if (cmd == SIOCGLIFADDR) { 701 /* on getting an address, take the 1st match */ 702 cmp = 0; /*XXX*/ 703 } else { 704 /* on deleting an address, do exact match */ 705 in_len2mask(&mask, 32); 706 sin = (struct sockaddr_in *)&iflr->addr; 707 match.s_addr = sin->sin_addr.s_addr; 708 709 cmp = 1; 710 } 711 } 712 713 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 714 if (ifa->ifa_addr->sa_family != AF_INET6) 715 continue; 716 if (!cmp) 717 break; 718 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 719 candidate.s_addr &= mask.s_addr; 720 if (candidate.s_addr == match.s_addr) 721 break; 722 } 723 if (!ifa) 724 return EADDRNOTAVAIL; 725 ia = (struct in_ifaddr *)ifa; 726 727 if (cmd == SIOCGLIFADDR) { 728 /* fill in the if_laddrreq structure */ 729 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 730 731 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 732 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 733 ia->ia_dstaddr.sin_len); 734 } else 735 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 736 737 iflr->prefixlen = 738 in_mask2len(&ia->ia_sockmask.sin_addr); 739 740 iflr->flags = 0; /*XXX*/ 741 742 return 0; 743 } else { 744 struct in_aliasreq ifra; 745 746 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 747 bzero(&ifra, sizeof(ifra)); 748 bcopy(iflr->iflr_name, ifra.ifra_name, 749 sizeof(ifra.ifra_name)); 750 751 bcopy(&ia->ia_addr, &ifra.ifra_addr, 752 ia->ia_addr.sin_len); 753 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 754 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 755 ia->ia_dstaddr.sin_len); 756 } 757 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 758 ia->ia_sockmask.sin_len); 759 760 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 761 ifp, p); 762 } 763 } 764 } 765 766 return EOPNOTSUPP; /*just for safety*/ 767 } 768 769 /* 770 * Delete any existing route for an interface. 771 */ 772 void 773 in_ifscrub(ifp, ia) 774 struct ifnet *ifp; 775 struct in_ifaddr *ia; 776 { 777 778 in_scrubprefix(ia); 779 } 780 781 /* 782 * Initialize an interface's internet address 783 * and routing table entry. 784 */ 785 int 786 in_ifinit(ifp, ia, sin, scrub) 787 struct ifnet *ifp; 788 struct in_ifaddr *ia; 789 struct sockaddr_in *sin; 790 int scrub; 791 { 792 u_int32_t i = sin->sin_addr.s_addr; 793 struct sockaddr_in oldaddr; 794 int s = splnet(), flags = RTF_UP, error; 795 796 /* 797 * Set up new addresses. 798 */ 799 oldaddr = ia->ia_addr; 800 if (ia->ia_addr.sin_family == AF_INET) 801 LIST_REMOVE(ia, ia_hash); 802 ia->ia_addr = *sin; 803 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 804 805 /* 806 * Give the interface a chance to initialize 807 * if this is its first address, 808 * and to validate the address if necessary. 809 */ 810 if (ifp->if_ioctl && 811 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) 812 goto bad; 813 splx(s); 814 if (scrub) { 815 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 816 in_ifscrub(ifp, ia); 817 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 818 } 819 820 if (IN_CLASSA(i)) 821 ia->ia_netmask = IN_CLASSA_NET; 822 else if (IN_CLASSB(i)) 823 ia->ia_netmask = IN_CLASSB_NET; 824 else 825 ia->ia_netmask = IN_CLASSC_NET; 826 /* 827 * The subnet mask usually includes at least the standard network part, 828 * but may may be smaller in the case of supernetting. 829 * If it is set, we believe it. 830 */ 831 if (ia->ia_subnetmask == 0) { 832 ia->ia_subnetmask = ia->ia_netmask; 833 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 834 } else 835 ia->ia_netmask &= ia->ia_subnetmask; 836 837 ia->ia_net = i & ia->ia_netmask; 838 ia->ia_subnet = i & ia->ia_subnetmask; 839 in_socktrim(&ia->ia_sockmask); 840 /* re-calculate the "in_maxmtu" value */ 841 in_setmaxmtu(); 842 /* 843 * Add route for the network. 844 */ 845 ia->ia_ifa.ifa_metric = ifp->if_metric; 846 if (ifp->if_flags & IFF_BROADCAST) { 847 ia->ia_broadaddr.sin_addr.s_addr = 848 ia->ia_subnet | ~ia->ia_subnetmask; 849 ia->ia_netbroadcast.s_addr = 850 ia->ia_net | ~ia->ia_netmask; 851 } else if (ifp->if_flags & IFF_LOOPBACK) { 852 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 853 flags |= RTF_HOST; 854 } else if (ifp->if_flags & IFF_POINTOPOINT) { 855 if (ia->ia_dstaddr.sin_family != AF_INET) 856 return (0); 857 flags |= RTF_HOST; 858 } 859 error = in_addprefix(ia, flags); 860 /* 861 * recover multicast kludge entry, if there is. 862 */ 863 if (ifp->if_flags & IFF_MULTICAST) 864 in_restoremkludge(ia, ifp); 865 /* 866 * If the interface supports multicast, join the "all hosts" 867 * multicast group on that interface. 868 */ 869 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 870 struct in_addr addr; 871 872 addr.s_addr = INADDR_ALLHOSTS_GROUP; 873 ia->ia_allhosts = in_addmulti(&addr, ifp); 874 } 875 return (error); 876 bad: 877 splx(s); 878 LIST_REMOVE(ia, ia_hash); 879 ia->ia_addr = oldaddr; 880 if (ia->ia_addr.sin_family == AF_INET) 881 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), 882 ia, ia_hash); 883 return (error); 884 } 885 886 #define rtinitflags(x) \ 887 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 888 ? RTF_HOST : 0) 889 890 /* 891 * add a route to prefix ("connected route" in cisco terminology). 892 * does nothing if there's some interface address with the same prefix already. 893 */ 894 static int 895 in_addprefix(target, flags) 896 struct in_ifaddr *target; 897 int flags; 898 { 899 struct in_ifaddr *ia; 900 struct in_addr prefix, mask, p; 901 int error; 902 903 if ((flags & RTF_HOST) != 0) 904 prefix = target->ia_dstaddr.sin_addr; 905 else 906 prefix = target->ia_addr.sin_addr; 907 mask = target->ia_sockmask.sin_addr; 908 prefix.s_addr &= mask.s_addr; 909 910 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 911 /* easy one first */ 912 if (mask.s_addr != ia->ia_sockmask.sin_addr.s_addr) 913 continue; 914 915 if (rtinitflags(ia)) 916 p = ia->ia_dstaddr.sin_addr; 917 else 918 p = ia->ia_addr.sin_addr; 919 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 920 if (prefix.s_addr != p.s_addr) 921 continue; 922 923 /* 924 * if we got a matching prefix route inserted by other 925 * interface address, we don't need to bother 926 */ 927 if (ia->ia_flags & IFA_ROUTE) 928 return 0; 929 } 930 931 /* 932 * noone seem to have prefix route. insert it. 933 */ 934 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 935 if (!error) 936 target->ia_flags |= IFA_ROUTE; 937 return error; 938 } 939 940 /* 941 * remove a route to prefix ("connected route" in cisco terminology). 942 * re-installs the route by using another interface address, if there's one 943 * with the same prefix (otherwise we lose the route mistakenly). 944 */ 945 static int 946 in_scrubprefix(target) 947 struct in_ifaddr *target; 948 { 949 struct in_ifaddr *ia; 950 struct in_addr prefix, mask, p; 951 int error; 952 953 if ((target->ia_flags & IFA_ROUTE) == 0) 954 return 0; 955 956 if (rtinitflags(target)) 957 prefix = target->ia_dstaddr.sin_addr; 958 else 959 prefix = target->ia_addr.sin_addr; 960 mask = target->ia_sockmask.sin_addr; 961 prefix.s_addr &= mask.s_addr; 962 963 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 964 /* easy one first */ 965 if (mask.s_addr != ia->ia_sockmask.sin_addr.s_addr) 966 continue; 967 968 if (rtinitflags(ia)) 969 p = ia->ia_dstaddr.sin_addr; 970 else 971 p = ia->ia_addr.sin_addr; 972 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 973 if (prefix.s_addr != p.s_addr) 974 continue; 975 976 /* 977 * if we got a matching prefix route, move IFA_ROUTE to him 978 */ 979 if ((ia->ia_flags & IFA_ROUTE) == 0) { 980 rtinit(&(target->ia_ifa), (int)RTM_DELETE, 981 rtinitflags(target)); 982 target->ia_flags &= ~IFA_ROUTE; 983 984 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 985 rtinitflags(ia) | RTF_UP); 986 if (error == 0) 987 ia->ia_flags |= IFA_ROUTE; 988 return error; 989 } 990 } 991 992 /* 993 * noone seem to have prefix route. remove it. 994 */ 995 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 996 target->ia_flags &= ~IFA_ROUTE; 997 return 0; 998 } 999 1000 #undef rtinitflags 1001 1002 /* 1003 * Return 1 if the address might be a local broadcast address. 1004 */ 1005 int 1006 in_broadcast(in, ifp) 1007 struct in_addr in; 1008 struct ifnet *ifp; 1009 { 1010 struct ifaddr *ifa; 1011 1012 if (in.s_addr == INADDR_BROADCAST || 1013 in_nullhost(in)) 1014 return 1; 1015 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1016 return 0; 1017 /* 1018 * Look through the list of addresses for a match 1019 * with a broadcast address. 1020 */ 1021 #define ia (ifatoia(ifa)) 1022 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1023 if (ifa->ifa_addr->sa_family == AF_INET && 1024 !in_hosteq(in, ia->ia_addr.sin_addr) && 1025 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 1026 in_hosteq(in, ia->ia_netbroadcast) || 1027 (hostzeroisbroadcast && 1028 /* 1029 * Check for old-style (host 0) broadcast. 1030 */ 1031 (in.s_addr == ia->ia_subnet || 1032 in.s_addr == ia->ia_net)))) 1033 return 1; 1034 return (0); 1035 #undef ia 1036 } 1037 1038 /* 1039 * Multicast address kludge: 1040 * If there were any multicast addresses attached to this interface address, 1041 * either move them to another address on this interface, or save them until 1042 * such time as this interface is reconfigured for IPv4. 1043 */ 1044 void 1045 in_savemkludge(oia) 1046 struct in_ifaddr *oia; 1047 { 1048 struct in_ifaddr *ia; 1049 struct in_multi *inm, *next; 1050 1051 IFP_TO_IA(oia->ia_ifp, ia); 1052 if (ia) { /* there is another address */ 1053 for (inm = LIST_FIRST(&oia->ia_multiaddrs); inm; inm = next){ 1054 next = LIST_NEXT(inm, inm_list); 1055 LIST_REMOVE(inm, inm_list); 1056 IFAFREE(&inm->inm_ia->ia_ifa); 1057 IFAREF(&ia->ia_ifa); 1058 inm->inm_ia = ia; 1059 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list); 1060 } 1061 } else { /* last address on this if deleted, save */ 1062 TAILQ_INSERT_TAIL(&in_mk, oia, ia_list); 1063 IFAREF(&oia->ia_ifa); 1064 } 1065 } 1066 1067 /* 1068 * Continuation of multicast address hack: 1069 * If there was a multicast group list previously saved for this interface, 1070 * then we re-attach it to the first address configured on the i/f. 1071 */ 1072 void 1073 in_restoremkludge(ia, ifp) 1074 struct in_ifaddr *ia; 1075 struct ifnet *ifp; 1076 { 1077 struct in_ifaddr *oia; 1078 1079 for (oia = TAILQ_FIRST(&in_mk); oia != NULL; 1080 oia = TAILQ_NEXT(oia, ia_list)) { 1081 if (oia->ia_ifp == ifp) { 1082 struct in_multi *inm, *next; 1083 1084 for (inm = LIST_FIRST(&oia->ia_multiaddrs); 1085 inm != NULL; inm = next) { 1086 next = LIST_NEXT(inm, inm_list); 1087 LIST_REMOVE(inm, inm_list); 1088 IFAFREE(&inm->inm_ia->ia_ifa); 1089 IFAREF(&ia->ia_ifa); 1090 inm->inm_ia = ia; 1091 LIST_INSERT_HEAD(&ia->ia_multiaddrs, 1092 inm, inm_list); 1093 } 1094 TAILQ_REMOVE(&in_mk, oia, ia_list); 1095 IFAFREE(&oia->ia_ifa); 1096 break; 1097 } 1098 } 1099 } 1100 1101 void 1102 in_purgemkludge(ifp) 1103 struct ifnet *ifp; 1104 { 1105 struct in_ifaddr *oia; 1106 1107 for (oia = TAILQ_FIRST(&in_mk); oia != NULL; 1108 oia = TAILQ_NEXT(oia, ia_list)) { 1109 if (oia->ia_ifp != ifp) 1110 continue; 1111 1112 /* 1113 * Leaving from all multicast groups joined through 1114 * this interface is done via in_pcbpurgeif(). 1115 */ 1116 1117 TAILQ_REMOVE(&in_mk, oia, ia_list); 1118 IFAFREE(&oia->ia_ifa); 1119 break; 1120 } 1121 } 1122 1123 /* 1124 * Add an address to the list of IP multicast addresses for a given interface. 1125 */ 1126 struct in_multi * 1127 in_addmulti(ap, ifp) 1128 struct in_addr *ap; 1129 struct ifnet *ifp; 1130 { 1131 struct in_multi *inm; 1132 struct ifreq ifr; 1133 struct in_ifaddr *ia; 1134 int s = splsoftnet(); 1135 1136 /* 1137 * See if address already in list. 1138 */ 1139 IN_LOOKUP_MULTI(*ap, ifp, inm); 1140 if (inm != NULL) { 1141 /* 1142 * Found it; just increment the reference count. 1143 */ 1144 ++inm->inm_refcount; 1145 } else { 1146 /* 1147 * New address; allocate a new multicast record 1148 * and link it into the interface's multicast list. 1149 */ 1150 inm = (struct in_multi *)malloc(sizeof(*inm), 1151 M_IPMADDR, M_NOWAIT); 1152 if (inm == NULL) { 1153 splx(s); 1154 return (NULL); 1155 } 1156 inm->inm_addr = *ap; 1157 inm->inm_ifp = ifp; 1158 inm->inm_refcount = 1; 1159 IFP_TO_IA(ifp, ia); 1160 if (ia == NULL) { 1161 free(inm, M_IPMADDR); 1162 splx(s); 1163 return (NULL); 1164 } 1165 inm->inm_ia = ia; 1166 IFAREF(&inm->inm_ia->ia_ifa); 1167 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list); 1168 /* 1169 * Ask the network driver to update its multicast reception 1170 * filter appropriately for the new address. 1171 */ 1172 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in); 1173 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 1174 satosin(&ifr.ifr_addr)->sin_addr = *ap; 1175 if ((ifp->if_ioctl == NULL) || 1176 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 1177 LIST_REMOVE(inm, inm_list); 1178 free(inm, M_IPMADDR); 1179 splx(s); 1180 return (NULL); 1181 } 1182 /* 1183 * Let IGMP know that we have joined a new IP multicast group. 1184 */ 1185 igmp_joingroup(inm); 1186 } 1187 splx(s); 1188 return (inm); 1189 } 1190 1191 /* 1192 * Delete a multicast address record. 1193 */ 1194 void 1195 in_delmulti(inm) 1196 struct in_multi *inm; 1197 { 1198 struct ifreq ifr; 1199 int s = splsoftnet(); 1200 1201 if (--inm->inm_refcount == 0) { 1202 /* 1203 * No remaining claims to this record; let IGMP know that 1204 * we are leaving the multicast group. 1205 */ 1206 igmp_leavegroup(inm); 1207 /* 1208 * Unlink from list. 1209 */ 1210 LIST_REMOVE(inm, inm_list); 1211 IFAFREE(&inm->inm_ia->ia_ifa); 1212 /* 1213 * Notify the network driver to update its multicast reception 1214 * filter. 1215 */ 1216 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 1217 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr; 1218 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 1219 (caddr_t)&ifr); 1220 free(inm, M_IPMADDR); 1221 } 1222 splx(s); 1223 } 1224 #endif 1225