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