1 /* $NetBSD: in.c,v 1.104 2005/02/26 22:45:12 perry 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.104 2005/02/26 22:45:12 perry Exp $"); 102 103 #include "opt_inet.h" 104 #include "opt_inet_conf.h" 105 #include "opt_mrouting.h" 106 #include "opt_pfil_hooks.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 #include <sys/syslog.h> 117 118 #include <net/if.h> 119 #include <net/route.h> 120 121 #include <net/if_ether.h> 122 123 #include <netinet/in_systm.h> 124 #include <netinet/in.h> 125 #include <netinet/in_var.h> 126 #include <netinet/ip.h> 127 #include <netinet/ip_var.h> 128 #include <netinet/in_pcb.h> 129 #include <netinet/if_inarp.h> 130 #include <netinet/ip_mroute.h> 131 #include <netinet/igmp_var.h> 132 133 #ifdef PFIL_HOOKS 134 #include <net/pfil.h> 135 #endif 136 137 #ifdef INET 138 static u_int in_mask2len(struct in_addr *); 139 static void in_len2mask(struct in_addr *, u_int); 140 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 141 struct ifnet *, struct proc *); 142 143 static int in_addprefix(struct in_ifaddr *, int); 144 static int in_scrubprefix(struct in_ifaddr *); 145 146 #ifndef SUBNETSARELOCAL 147 #define SUBNETSARELOCAL 1 148 #endif 149 150 #ifndef HOSTZEROBROADCAST 151 #define HOSTZEROBROADCAST 1 152 #endif 153 154 int subnetsarelocal = SUBNETSARELOCAL; 155 int hostzeroisbroadcast = HOSTZEROBROADCAST; 156 157 /* 158 * This list is used to keep track of in_multi chains which belong to 159 * deleted interface addresses. We use in_ifaddr so that a chain head 160 * won't be deallocated until all multicast address record are deleted. 161 */ 162 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk); 163 164 /* 165 * Return 1 if an internet address is for a ``local'' host 166 * (one to which we have a connection). If subnetsarelocal 167 * is true, this includes other subnets of the local net. 168 * Otherwise, it includes only the directly-connected (sub)nets. 169 */ 170 int 171 in_localaddr(struct in_addr in) 172 { 173 struct in_ifaddr *ia; 174 175 if (subnetsarelocal) { 176 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 177 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) 178 return (1); 179 } else { 180 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 181 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) 182 return (1); 183 } 184 return (0); 185 } 186 187 /* 188 * Determine whether an IP address is in a reserved set of addresses 189 * that may not be forwarded, or whether datagrams to that destination 190 * may be forwarded. 191 */ 192 int 193 in_canforward(struct in_addr in) 194 { 195 u_int32_t net; 196 197 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 198 return (0); 199 if (IN_CLASSA(in.s_addr)) { 200 net = in.s_addr & IN_CLASSA_NET; 201 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 202 return (0); 203 } 204 return (1); 205 } 206 207 /* 208 * Trim a mask in a sockaddr 209 */ 210 void 211 in_socktrim(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(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 snprintf(buf, sizeof(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(void) 252 { 253 struct in_ifaddr *ia; 254 struct ifnet *ifp; 255 unsigned long maxmtu = 0; 256 257 TAILQ_FOREACH(ia, &in_ifaddrhead, 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(struct in_addr *mask) 271 { 272 u_int x, y; 273 u_char *p; 274 275 p = (u_char *)mask; 276 for (x = 0; x < sizeof(*mask); x++) { 277 if (p[x] != 0xff) 278 break; 279 } 280 y = 0; 281 if (x < sizeof(*mask)) { 282 for (y = 0; y < 8; y++) { 283 if ((p[x] & (0x80 >> y)) == 0) 284 break; 285 } 286 } 287 return x * 8 + y; 288 } 289 290 static void 291 in_len2mask(struct in_addr *mask, u_int len) 292 { 293 u_int i; 294 u_char *p; 295 296 p = (u_char *)mask; 297 bzero(mask, sizeof(*mask)); 298 for (i = 0; i < len / 8; i++) 299 p[i] = 0xff; 300 if (len % 8) 301 p[i] = (0xff00 >> (len % 8)) & 0xff; 302 } 303 304 /* 305 * Generic internet control operations (ioctl's). 306 * Ifp is 0 if not an interface-specific ioctl. 307 */ 308 /* ARGSUSED */ 309 int 310 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 311 struct proc *p) 312 { 313 struct ifreq *ifr = (struct ifreq *)data; 314 struct in_ifaddr *ia = 0; 315 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 316 struct sockaddr_in oldaddr; 317 int error, hostIsNew, maskIsNew; 318 int newifaddr = 0; 319 320 switch (cmd) { 321 case SIOCALIFADDR: 322 case SIOCDLIFADDR: 323 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 324 return (EPERM); 325 /*fall through*/ 326 case SIOCGLIFADDR: 327 if (!ifp) 328 return EINVAL; 329 return in_lifaddr_ioctl(so, cmd, data, ifp, p); 330 } 331 332 /* 333 * Find address for this interface, if it exists. 334 */ 335 if (ifp) 336 IFP_TO_IA(ifp, ia); 337 338 switch (cmd) { 339 340 case SIOCAIFADDR: 341 case SIOCDIFADDR: 342 case SIOCGIFALIAS: 343 if (ifra->ifra_addr.sin_family == AF_INET) 344 LIST_FOREACH(ia, 345 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr), 346 ia_hash) { 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 (ia == NULL && 374 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) 375 return (EADDRNOTAVAIL); 376 377 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 378 return (EPERM); 379 380 if (ia == 0) { 381 MALLOC(ia, struct in_ifaddr *, sizeof(*ia), 382 M_IFADDR, M_WAITOK); 383 if (ia == 0) 384 return (ENOBUFS); 385 bzero((caddr_t)ia, sizeof *ia); 386 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list); 387 IFAREF(&ia->ia_ifa); 388 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, 389 ifa_list); 390 IFAREF(&ia->ia_ifa); 391 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 392 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 393 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 394 ia->ia_sockmask.sin_len = 8; 395 if (ifp->if_flags & IFF_BROADCAST) { 396 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 397 ia->ia_broadaddr.sin_family = AF_INET; 398 } 399 ia->ia_ifp = ifp; 400 LIST_INIT(&ia->ia_multiaddrs); 401 newifaddr = 1; 402 } 403 break; 404 405 case SIOCSIFBRDADDR: 406 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 407 return (EPERM); 408 /* FALLTHROUGH */ 409 410 case SIOCGIFADDR: 411 case SIOCGIFNETMASK: 412 case SIOCGIFDSTADDR: 413 case SIOCGIFBRDADDR: 414 if (ia == 0) 415 return (EADDRNOTAVAIL); 416 break; 417 } 418 error = 0; 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 #ifdef PFIL_HOOKS 468 if (!error) 469 (void)pfil_run_hooks(&if_pfil, 470 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR); 471 #endif 472 break; 473 474 case SIOCSIFNETMASK: 475 in_ifscrub(ifp, ia); 476 ia->ia_sockmask = *satosin(&ifr->ifr_addr); 477 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 478 error = in_ifinit(ifp, ia, NULL, 0); 479 break; 480 481 case SIOCAIFADDR: 482 maskIsNew = 0; 483 hostIsNew = 1; 484 if (ia->ia_addr.sin_family == AF_INET) { 485 if (ifra->ifra_addr.sin_len == 0) { 486 ifra->ifra_addr = ia->ia_addr; 487 hostIsNew = 0; 488 } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr)) 489 hostIsNew = 0; 490 } 491 if (ifra->ifra_mask.sin_len) { 492 in_ifscrub(ifp, ia); 493 ia->ia_sockmask = ifra->ifra_mask; 494 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 495 maskIsNew = 1; 496 } 497 if ((ifp->if_flags & IFF_POINTOPOINT) && 498 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 499 in_ifscrub(ifp, ia); 500 ia->ia_dstaddr = ifra->ifra_dstaddr; 501 maskIsNew = 1; /* We lie; but the effect's the same */ 502 } 503 if (ifra->ifra_addr.sin_family == AF_INET && 504 (hostIsNew || maskIsNew)) { 505 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 506 } 507 if ((ifp->if_flags & IFF_BROADCAST) && 508 (ifra->ifra_broadaddr.sin_family == AF_INET)) 509 ia->ia_broadaddr = ifra->ifra_broadaddr; 510 #ifdef PFIL_HOOKS 511 if (!error) 512 (void)pfil_run_hooks(&if_pfil, 513 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR); 514 #endif 515 break; 516 517 case SIOCGIFALIAS: 518 ifra->ifra_mask = ia->ia_sockmask; 519 if ((ifp->if_flags & IFF_POINTOPOINT) && 520 (ia->ia_dstaddr.sin_family == AF_INET)) 521 ifra->ifra_dstaddr = ia->ia_dstaddr; 522 else if ((ifp->if_flags & IFF_BROADCAST) && 523 (ia->ia_broadaddr.sin_family == AF_INET)) 524 ifra->ifra_broadaddr = ia->ia_broadaddr; 525 else 526 bzero(&ifra->ifra_broadaddr, 527 sizeof(ifra->ifra_broadaddr)); 528 break; 529 530 case SIOCDIFADDR: 531 in_purgeaddr(&ia->ia_ifa, ifp); 532 #ifdef PFIL_HOOKS 533 (void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR, 534 ifp, PFIL_IFADDR); 535 #endif 536 break; 537 538 #ifdef MROUTING 539 case SIOCGETVIFCNT: 540 case SIOCGETSGCNT: 541 error = mrt_ioctl(so, cmd, data); 542 break; 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 break; 551 } 552 553 if (error && newifaddr) { 554 KASSERT(ia != NULL); 555 in_purgeaddr(&ia->ia_ifa, ifp); 556 } 557 558 return error; 559 } 560 561 void 562 in_purgeaddr(struct ifaddr *ifa, struct ifnet *ifp) 563 { 564 struct in_ifaddr *ia = (void *) ifa; 565 566 in_ifscrub(ifp, ia); 567 LIST_REMOVE(ia, ia_hash); 568 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 569 IFAFREE(&ia->ia_ifa); 570 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list); 571 if (ia->ia_allhosts != NULL) 572 in_delmulti(ia->ia_allhosts); 573 IFAFREE(&ia->ia_ifa); 574 in_setmaxmtu(); 575 } 576 577 void 578 in_purgeif(struct ifnet *ifp) 579 { 580 struct ifaddr *ifa, *nifa; 581 582 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 583 nifa = TAILQ_NEXT(ifa, ifa_list); 584 if (ifa->ifa_addr->sa_family != AF_INET) 585 continue; 586 in_purgeaddr(ifa, ifp); 587 } 588 589 igmp_purgeif(ifp); 590 #ifdef MROUTING 591 ip_mrouter_detach(ifp); 592 #endif 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(struct socket *so, u_long cmd, caddr_t data, 613 struct ifnet *ifp, struct proc *p) 614 { 615 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 616 struct ifaddr *ifa; 617 struct sockaddr *sa; 618 619 /* sanity checks */ 620 if (!data || !ifp) { 621 panic("invalid argument to in_lifaddr_ioctl"); 622 /*NOTRECHED*/ 623 } 624 625 switch (cmd) { 626 case SIOCGLIFADDR: 627 /* address must be specified on GET with IFLR_PREFIX */ 628 if ((iflr->flags & IFLR_PREFIX) == 0) 629 break; 630 /*FALLTHROUGH*/ 631 case SIOCALIFADDR: 632 case SIOCDLIFADDR: 633 /* address must be specified on ADD and DELETE */ 634 sa = (struct sockaddr *)&iflr->addr; 635 if (sa->sa_family != AF_INET) 636 return EINVAL; 637 if (sa->sa_len != sizeof(struct sockaddr_in)) 638 return EINVAL; 639 /* XXX need improvement */ 640 sa = (struct sockaddr *)&iflr->dstaddr; 641 if (sa->sa_family 642 && sa->sa_family != AF_INET) 643 return EINVAL; 644 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in)) 645 return EINVAL; 646 break; 647 default: /*shouldn't happen*/ 648 #if 0 649 panic("invalid cmd to in_lifaddr_ioctl"); 650 /*NOTREACHED*/ 651 #else 652 return EOPNOTSUPP; 653 #endif 654 } 655 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 656 return EINVAL; 657 658 switch (cmd) { 659 case SIOCALIFADDR: 660 { 661 struct in_aliasreq ifra; 662 663 if (iflr->flags & IFLR_PREFIX) 664 return EINVAL; 665 666 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN). */ 667 bzero(&ifra, sizeof(ifra)); 668 bcopy(iflr->iflr_name, ifra.ifra_name, 669 sizeof(ifra.ifra_name)); 670 671 bcopy(&iflr->addr, &ifra.ifra_addr, 672 ((struct sockaddr *)&iflr->addr)->sa_len); 673 674 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 675 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 676 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 677 } 678 679 ifra.ifra_mask.sin_family = AF_INET; 680 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 681 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 682 683 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p); 684 } 685 case SIOCGLIFADDR: 686 case SIOCDLIFADDR: 687 { 688 struct in_ifaddr *ia; 689 struct in_addr mask, candidate, match; 690 struct sockaddr_in *sin; 691 int cmp; 692 693 bzero(&mask, sizeof(mask)); 694 if (iflr->flags & IFLR_PREFIX) { 695 /* lookup a prefix rather than address. */ 696 in_len2mask(&mask, iflr->prefixlen); 697 698 sin = (struct sockaddr_in *)&iflr->addr; 699 match.s_addr = sin->sin_addr.s_addr; 700 match.s_addr &= mask.s_addr; 701 702 /* if you set extra bits, that's wrong */ 703 if (match.s_addr != sin->sin_addr.s_addr) 704 return EINVAL; 705 706 cmp = 1; 707 } else { 708 if (cmd == SIOCGLIFADDR) { 709 /* on getting an address, take the 1st match */ 710 cmp = 0; /*XXX*/ 711 } else { 712 /* on deleting an address, do exact match */ 713 in_len2mask(&mask, 32); 714 sin = (struct sockaddr_in *)&iflr->addr; 715 match.s_addr = sin->sin_addr.s_addr; 716 717 cmp = 1; 718 } 719 } 720 721 IFADDR_FOREACH(ifa, ifp) { 722 if (ifa->ifa_addr->sa_family != AF_INET) 723 continue; 724 if (!cmp) 725 break; 726 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 727 candidate.s_addr &= mask.s_addr; 728 if (candidate.s_addr == match.s_addr) 729 break; 730 } 731 if (!ifa) 732 return EADDRNOTAVAIL; 733 ia = (struct in_ifaddr *)ifa; 734 735 if (cmd == SIOCGLIFADDR) { 736 /* fill in the if_laddrreq structure */ 737 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 738 739 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 740 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 741 ia->ia_dstaddr.sin_len); 742 } else 743 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 744 745 iflr->prefixlen = 746 in_mask2len(&ia->ia_sockmask.sin_addr); 747 748 iflr->flags = 0; /*XXX*/ 749 750 return 0; 751 } else { 752 struct in_aliasreq ifra; 753 754 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN) */ 755 bzero(&ifra, sizeof(ifra)); 756 bcopy(iflr->iflr_name, ifra.ifra_name, 757 sizeof(ifra.ifra_name)); 758 759 bcopy(&ia->ia_addr, &ifra.ifra_addr, 760 ia->ia_addr.sin_len); 761 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 762 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 763 ia->ia_dstaddr.sin_len); 764 } 765 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 766 ia->ia_sockmask.sin_len); 767 768 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 769 ifp, p); 770 } 771 } 772 } 773 774 return EOPNOTSUPP; /*just for safety*/ 775 } 776 777 /* 778 * Delete any existing route for an interface. 779 */ 780 void 781 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 782 { 783 784 in_scrubprefix(ia); 785 } 786 787 /* 788 * Initialize an interface's internet address 789 * and routing table entry. 790 */ 791 int 792 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, 793 struct sockaddr_in *sin, int scrub) 794 { 795 u_int32_t i; 796 struct sockaddr_in oldaddr; 797 int s = splnet(), flags = RTF_UP, error; 798 799 if (!sin) 800 sin = &ia->ia_addr; 801 802 /* 803 * Set up new addresses. 804 */ 805 oldaddr = ia->ia_addr; 806 if (ia->ia_addr.sin_family == AF_INET) 807 LIST_REMOVE(ia, ia_hash); 808 ia->ia_addr = *sin; 809 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 810 811 /* 812 * Give the interface a chance to initialize 813 * if this is its first address, 814 * and to validate the address if necessary. 815 */ 816 if (ifp->if_ioctl && 817 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) 818 goto bad; 819 splx(s); 820 if (scrub) { 821 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 822 in_ifscrub(ifp, ia); 823 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 824 } 825 826 i = ia->ia_addr.sin_addr.s_addr; 827 if (IN_CLASSA(i)) 828 ia->ia_netmask = IN_CLASSA_NET; 829 else if (IN_CLASSB(i)) 830 ia->ia_netmask = IN_CLASSB_NET; 831 else 832 ia->ia_netmask = IN_CLASSC_NET; 833 /* 834 * The subnet mask usually includes at least the standard network part, 835 * but may may be smaller in the case of supernetting. 836 * If it is set, we believe it. 837 */ 838 if (ia->ia_subnetmask == 0) { 839 ia->ia_subnetmask = ia->ia_netmask; 840 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 841 } else 842 ia->ia_netmask &= ia->ia_subnetmask; 843 844 ia->ia_net = i & ia->ia_netmask; 845 ia->ia_subnet = i & ia->ia_subnetmask; 846 in_socktrim(&ia->ia_sockmask); 847 /* re-calculate the "in_maxmtu" value */ 848 in_setmaxmtu(); 849 /* 850 * Add route for the network. 851 */ 852 ia->ia_ifa.ifa_metric = ifp->if_metric; 853 if (ifp->if_flags & IFF_BROADCAST) { 854 ia->ia_broadaddr.sin_addr.s_addr = 855 ia->ia_subnet | ~ia->ia_subnetmask; 856 ia->ia_netbroadcast.s_addr = 857 ia->ia_net | ~ia->ia_netmask; 858 } else if (ifp->if_flags & IFF_LOOPBACK) { 859 ia->ia_dstaddr = ia->ia_addr; 860 flags |= RTF_HOST; 861 } else if (ifp->if_flags & IFF_POINTOPOINT) { 862 if (ia->ia_dstaddr.sin_family != AF_INET) 863 return (0); 864 flags |= RTF_HOST; 865 } 866 error = in_addprefix(ia, flags); 867 /* 868 * If the interface supports multicast, join the "all hosts" 869 * multicast group on that interface. 870 */ 871 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 872 struct in_addr addr; 873 874 addr.s_addr = INADDR_ALLHOSTS_GROUP; 875 ia->ia_allhosts = in_addmulti(&addr, ifp); 876 } 877 return (error); 878 bad: 879 splx(s); 880 LIST_REMOVE(ia, ia_hash); 881 ia->ia_addr = oldaddr; 882 if (ia->ia_addr.sin_family == AF_INET) 883 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), 884 ia, ia_hash); 885 return (error); 886 } 887 888 #define rtinitflags(x) \ 889 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 890 ? RTF_HOST : 0) 891 892 /* 893 * add a route to prefix ("connected route" in cisco terminology). 894 * does nothing if there's some interface address with the same prefix already. 895 */ 896 static int 897 in_addprefix(struct in_ifaddr *target, 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 911 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 912 if (rtinitflags(ia)) 913 p = ia->ia_dstaddr.sin_addr; 914 else { 915 p = ia->ia_addr.sin_addr; 916 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 917 } 918 919 if (prefix.s_addr != p.s_addr) 920 continue; 921 922 /* 923 * if we got a matching prefix route inserted by other 924 * interface address, we don't need to bother 925 */ 926 if (ia->ia_flags & IFA_ROUTE) 927 return 0; 928 } 929 930 /* 931 * noone seem to have prefix route. insert it. 932 */ 933 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 934 if (!error) 935 target->ia_flags |= IFA_ROUTE; 936 return error; 937 } 938 939 /* 940 * remove a route to prefix ("connected route" in cisco terminology). 941 * re-installs the route by using another interface address, if there's one 942 * with the same prefix (otherwise we lose the route mistakenly). 943 */ 944 static int 945 in_scrubprefix(struct in_ifaddr *target) 946 { 947 struct in_ifaddr *ia; 948 struct in_addr prefix, mask, p; 949 int error; 950 951 if ((target->ia_flags & IFA_ROUTE) == 0) 952 return 0; 953 954 if (rtinitflags(target)) 955 prefix = target->ia_dstaddr.sin_addr; 956 else { 957 prefix = target->ia_addr.sin_addr; 958 mask = target->ia_sockmask.sin_addr; 959 prefix.s_addr &= mask.s_addr; 960 } 961 962 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 963 if (rtinitflags(ia)) 964 p = ia->ia_dstaddr.sin_addr; 965 else { 966 p = ia->ia_addr.sin_addr; 967 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 968 } 969 970 if (prefix.s_addr != p.s_addr) 971 continue; 972 973 /* 974 * if we got a matching prefix route, move IFA_ROUTE to him 975 */ 976 if ((ia->ia_flags & IFA_ROUTE) == 0) { 977 rtinit(&(target->ia_ifa), (int)RTM_DELETE, 978 rtinitflags(target)); 979 target->ia_flags &= ~IFA_ROUTE; 980 981 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 982 rtinitflags(ia) | RTF_UP); 983 if (error == 0) 984 ia->ia_flags |= IFA_ROUTE; 985 return error; 986 } 987 } 988 989 /* 990 * noone seem to have prefix route. remove it. 991 */ 992 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 993 target->ia_flags &= ~IFA_ROUTE; 994 return 0; 995 } 996 997 #undef rtinitflags 998 999 /* 1000 * Return 1 if the address might be a local broadcast address. 1001 */ 1002 int 1003 in_broadcast(struct in_addr in, struct ifnet *ifp) 1004 { 1005 struct ifaddr *ifa; 1006 1007 if (in.s_addr == INADDR_BROADCAST || 1008 in_nullhost(in)) 1009 return 1; 1010 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1011 return 0; 1012 /* 1013 * Look through the list of addresses for a match 1014 * with a broadcast address. 1015 */ 1016 #define ia (ifatoia(ifa)) 1017 IFADDR_FOREACH(ifa, ifp) 1018 if (ifa->ifa_addr->sa_family == AF_INET && 1019 !in_hosteq(in, ia->ia_addr.sin_addr) && 1020 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 1021 in_hosteq(in, ia->ia_netbroadcast) || 1022 (hostzeroisbroadcast && 1023 /* 1024 * Check for old-style (host 0) broadcast. 1025 */ 1026 (in.s_addr == ia->ia_subnet || 1027 in.s_addr == ia->ia_net)))) 1028 return 1; 1029 return (0); 1030 #undef ia 1031 } 1032 1033 /* 1034 * Add an address to the list of IP multicast addresses for a given interface. 1035 */ 1036 struct in_multi * 1037 in_addmulti(struct in_addr *ap, struct ifnet *ifp) 1038 { 1039 struct in_multi *inm; 1040 struct ifreq ifr; 1041 int s = splsoftnet(); 1042 1043 /* 1044 * See if address already in list. 1045 */ 1046 IN_LOOKUP_MULTI(*ap, ifp, inm); 1047 if (inm != NULL) { 1048 /* 1049 * Found it; just increment the reference count. 1050 */ 1051 ++inm->inm_refcount; 1052 } else { 1053 /* 1054 * New address; allocate a new multicast record 1055 * and link it into the interface's multicast list. 1056 */ 1057 inm = pool_get(&inmulti_pool, PR_NOWAIT); 1058 if (inm == NULL) { 1059 splx(s); 1060 return (NULL); 1061 } 1062 inm->inm_addr = *ap; 1063 inm->inm_ifp = ifp; 1064 inm->inm_refcount = 1; 1065 LIST_INSERT_HEAD( 1066 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp), 1067 inm, inm_list); 1068 /* 1069 * Ask the network driver to update its multicast reception 1070 * filter appropriately for the new address. 1071 */ 1072 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in); 1073 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 1074 satosin(&ifr.ifr_addr)->sin_addr = *ap; 1075 if ((ifp->if_ioctl == NULL) || 1076 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 1077 LIST_REMOVE(inm, inm_list); 1078 pool_put(&inmulti_pool, inm); 1079 splx(s); 1080 return (NULL); 1081 } 1082 /* 1083 * Let IGMP know that we have joined a new IP multicast group. 1084 */ 1085 if (igmp_joingroup(inm) != 0) { 1086 LIST_REMOVE(inm, inm_list); 1087 pool_put(&inmulti_pool, inm); 1088 splx(s); 1089 return (NULL); 1090 } 1091 in_multientries++; 1092 } 1093 splx(s); 1094 return (inm); 1095 } 1096 1097 /* 1098 * Delete a multicast address record. 1099 */ 1100 void 1101 in_delmulti(struct in_multi *inm) 1102 { 1103 struct ifreq ifr; 1104 int s = splsoftnet(); 1105 1106 if (--inm->inm_refcount == 0) { 1107 /* 1108 * No remaining claims to this record; let IGMP know that 1109 * we are leaving the multicast group. 1110 */ 1111 igmp_leavegroup(inm); 1112 /* 1113 * Unlink from list. 1114 */ 1115 LIST_REMOVE(inm, inm_list); 1116 in_multientries--; 1117 /* 1118 * Notify the network driver to update its multicast reception 1119 * filter. 1120 */ 1121 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 1122 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr; 1123 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 1124 (caddr_t)&ifr); 1125 pool_put(&inmulti_pool, inm); 1126 } 1127 splx(s); 1128 } 1129 #endif 1130