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