1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (C) 2001 WIDE Project. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)in.c 8.4 (Berkeley) 1/9/95 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_carp.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/sockio.h> 41 #include <sys/malloc.h> 42 #include <sys/priv.h> 43 #include <sys/socket.h> 44 #include <sys/kernel.h> 45 #include <sys/sysctl.h> 46 #include <sys/vimage.h> 47 48 #include <net/if.h> 49 #include <net/if_llatbl.h> 50 #include <net/if_types.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_var.h> 55 #include <netinet/in_pcb.h> 56 #include <netinet/ip_var.h> 57 #include <netinet/vinet.h> 58 59 static int in_mask2len(struct in_addr *); 60 static void in_len2mask(struct in_addr *, int); 61 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 62 struct ifnet *, struct thread *); 63 64 static int in_addprefix(struct in_ifaddr *, int); 65 static int in_scrubprefix(struct in_ifaddr *); 66 static void in_socktrim(struct sockaddr_in *); 67 static int in_ifinit(struct ifnet *, 68 struct in_ifaddr *, struct sockaddr_in *, int); 69 static void in_purgemaddrs(struct ifnet *); 70 71 #ifdef VIMAGE_GLOBALS 72 static int subnetsarelocal; 73 static int sameprefixcarponly; 74 extern struct inpcbinfo ripcbinfo; 75 #endif 76 77 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local, 78 CTLFLAG_RW, subnetsarelocal, 0, 79 "Treat all subnets as directly connected"); 80 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only, 81 CTLFLAG_RW, sameprefixcarponly, 0, 82 "Refuse to create same prefixes on different interfaces"); 83 84 /* 85 * Return 1 if an internet address is for a ``local'' host 86 * (one to which we have a connection). If subnetsarelocal 87 * is true, this includes other subnets of the local net. 88 * Otherwise, it includes only the directly-connected (sub)nets. 89 */ 90 int 91 in_localaddr(struct in_addr in) 92 { 93 INIT_VNET_INET(curvnet); 94 register u_long i = ntohl(in.s_addr); 95 register struct in_ifaddr *ia; 96 97 if (V_subnetsarelocal) { 98 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 99 if ((i & ia->ia_netmask) == ia->ia_net) 100 return (1); 101 } else { 102 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 103 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 104 return (1); 105 } 106 return (0); 107 } 108 109 /* 110 * Return 1 if an internet address is for the local host and configured 111 * on one of its interfaces. 112 */ 113 int 114 in_localip(struct in_addr in) 115 { 116 INIT_VNET_INET(curvnet); 117 struct in_ifaddr *ia; 118 119 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 120 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 121 return (1); 122 } 123 return (0); 124 } 125 126 /* 127 * Determine whether an IP address is in a reserved set of addresses 128 * that may not be forwarded, or whether datagrams to that destination 129 * may be forwarded. 130 */ 131 int 132 in_canforward(struct in_addr in) 133 { 134 register u_long i = ntohl(in.s_addr); 135 register u_long net; 136 137 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 138 return (0); 139 if (IN_CLASSA(i)) { 140 net = i & IN_CLASSA_NET; 141 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 142 return (0); 143 } 144 return (1); 145 } 146 147 /* 148 * Trim a mask in a sockaddr 149 */ 150 static void 151 in_socktrim(struct sockaddr_in *ap) 152 { 153 register char *cplim = (char *) &ap->sin_addr; 154 register char *cp = (char *) (&ap->sin_addr + 1); 155 156 ap->sin_len = 0; 157 while (--cp >= cplim) 158 if (*cp) { 159 (ap)->sin_len = cp - (char *) (ap) + 1; 160 break; 161 } 162 } 163 164 static int 165 in_mask2len(mask) 166 struct in_addr *mask; 167 { 168 int x, y; 169 u_char *p; 170 171 p = (u_char *)mask; 172 for (x = 0; x < sizeof(*mask); x++) { 173 if (p[x] != 0xff) 174 break; 175 } 176 y = 0; 177 if (x < sizeof(*mask)) { 178 for (y = 0; y < 8; y++) { 179 if ((p[x] & (0x80 >> y)) == 0) 180 break; 181 } 182 } 183 return (x * 8 + y); 184 } 185 186 static void 187 in_len2mask(struct in_addr *mask, int len) 188 { 189 int i; 190 u_char *p; 191 192 p = (u_char *)mask; 193 bzero(mask, sizeof(*mask)); 194 for (i = 0; i < len / 8; i++) 195 p[i] = 0xff; 196 if (len % 8) 197 p[i] = (0xff00 >> (len % 8)) & 0xff; 198 } 199 200 /* 201 * Generic internet control operations (ioctl's). 202 * Ifp is 0 if not an interface-specific ioctl. 203 */ 204 /* ARGSUSED */ 205 int 206 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 207 struct thread *td) 208 { 209 INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */ 210 register struct ifreq *ifr = (struct ifreq *)data; 211 register struct in_ifaddr *ia, *iap; 212 register struct ifaddr *ifa; 213 struct in_addr allhosts_addr; 214 struct in_addr dst; 215 struct in_ifaddr *oia; 216 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 217 struct sockaddr_in oldaddr; 218 int error, hostIsNew, iaIsNew, maskIsNew, s; 219 int iaIsFirst; 220 221 ia = NULL; 222 iaIsFirst = 0; 223 iaIsNew = 0; 224 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 225 226 switch (cmd) { 227 case SIOCALIFADDR: 228 if (td != NULL) { 229 error = priv_check(td, PRIV_NET_ADDIFADDR); 230 if (error) 231 return (error); 232 } 233 if (ifp == NULL) 234 return (EINVAL); 235 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 236 237 case SIOCDLIFADDR: 238 if (td != NULL) { 239 error = priv_check(td, PRIV_NET_DELIFADDR); 240 if (error) 241 return (error); 242 } 243 if (ifp == NULL) 244 return (EINVAL); 245 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 246 247 case SIOCGLIFADDR: 248 if (ifp == NULL) 249 return (EINVAL); 250 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 251 } 252 253 /* 254 * Find address for this interface, if it exists. 255 * 256 * If an alias address was specified, find that one instead of 257 * the first one on the interface, if possible. 258 */ 259 if (ifp != NULL) { 260 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 261 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) 262 if (iap->ia_ifp == ifp && 263 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 264 ia = iap; 265 break; 266 } 267 if (ia == NULL) 268 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 269 iap = ifatoia(ifa); 270 if (iap->ia_addr.sin_family == AF_INET) { 271 ia = iap; 272 break; 273 } 274 } 275 if (ia == NULL) 276 iaIsFirst = 1; 277 } 278 279 switch (cmd) { 280 281 case SIOCAIFADDR: 282 case SIOCDIFADDR: 283 if (ifp == NULL) 284 return (EADDRNOTAVAIL); 285 if (ifra->ifra_addr.sin_family == AF_INET) { 286 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 287 if (ia->ia_ifp == ifp && 288 ia->ia_addr.sin_addr.s_addr == 289 ifra->ifra_addr.sin_addr.s_addr) 290 break; 291 } 292 if ((ifp->if_flags & IFF_POINTOPOINT) 293 && (cmd == SIOCAIFADDR) 294 && (ifra->ifra_dstaddr.sin_addr.s_addr 295 == INADDR_ANY)) { 296 return (EDESTADDRREQ); 297 } 298 } 299 if (cmd == SIOCDIFADDR && ia == NULL) 300 return (EADDRNOTAVAIL); 301 /* FALLTHROUGH */ 302 case SIOCSIFADDR: 303 case SIOCSIFNETMASK: 304 case SIOCSIFDSTADDR: 305 if (td != NULL) { 306 error = priv_check(td, (cmd == SIOCDIFADDR) ? 307 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 308 if (error) 309 return (error); 310 } 311 312 if (ifp == NULL) 313 return (EADDRNOTAVAIL); 314 if (ia == NULL) { 315 ia = (struct in_ifaddr *) 316 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 317 if (ia == NULL) 318 return (ENOBUFS); 319 /* 320 * Protect from ipintr() traversing address list 321 * while we're modifying it. 322 */ 323 s = splnet(); 324 ifa = &ia->ia_ifa; 325 IFA_LOCK_INIT(ifa); 326 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 327 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 328 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 329 ifa->ifa_refcnt = 1; 330 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 331 332 ia->ia_sockmask.sin_len = 8; 333 ia->ia_sockmask.sin_family = AF_INET; 334 if (ifp->if_flags & IFF_BROADCAST) { 335 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 336 ia->ia_broadaddr.sin_family = AF_INET; 337 } 338 ia->ia_ifp = ifp; 339 340 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 341 splx(s); 342 iaIsNew = 1; 343 } 344 break; 345 346 case SIOCSIFBRDADDR: 347 if (td != NULL) { 348 error = priv_check(td, PRIV_NET_ADDIFADDR); 349 if (error) 350 return (error); 351 } 352 /* FALLTHROUGH */ 353 354 case SIOCGIFADDR: 355 case SIOCGIFNETMASK: 356 case SIOCGIFDSTADDR: 357 case SIOCGIFBRDADDR: 358 if (ia == NULL) 359 return (EADDRNOTAVAIL); 360 break; 361 } 362 switch (cmd) { 363 364 case SIOCGIFADDR: 365 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 366 return (0); 367 368 case SIOCGIFBRDADDR: 369 if ((ifp->if_flags & IFF_BROADCAST) == 0) 370 return (EINVAL); 371 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 372 return (0); 373 374 case SIOCGIFDSTADDR: 375 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 376 return (EINVAL); 377 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 378 return (0); 379 380 case SIOCGIFNETMASK: 381 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 382 return (0); 383 384 case SIOCSIFDSTADDR: 385 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 386 return (EINVAL); 387 oldaddr = ia->ia_dstaddr; 388 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 389 if (ifp->if_ioctl != NULL) { 390 IFF_LOCKGIANT(ifp); 391 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 392 (caddr_t)ia); 393 IFF_UNLOCKGIANT(ifp); 394 if (error) { 395 ia->ia_dstaddr = oldaddr; 396 return (error); 397 } 398 } 399 if (ia->ia_flags & IFA_ROUTE) { 400 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 401 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 402 ia->ia_ifa.ifa_dstaddr = 403 (struct sockaddr *)&ia->ia_dstaddr; 404 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 405 } 406 return (0); 407 408 case SIOCSIFBRDADDR: 409 if ((ifp->if_flags & IFF_BROADCAST) == 0) 410 return (EINVAL); 411 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 412 return (0); 413 414 case SIOCSIFADDR: 415 error = in_ifinit(ifp, ia, 416 (struct sockaddr_in *) &ifr->ifr_addr, 1); 417 if (error != 0 && iaIsNew) 418 break; 419 if (error == 0) { 420 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0) 421 in_addmulti(&allhosts_addr, ifp); 422 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 423 } 424 return (0); 425 426 case SIOCSIFNETMASK: 427 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 428 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 429 return (0); 430 431 case SIOCAIFADDR: 432 maskIsNew = 0; 433 hostIsNew = 1; 434 error = 0; 435 if (ia->ia_addr.sin_family == AF_INET) { 436 if (ifra->ifra_addr.sin_len == 0) { 437 ifra->ifra_addr = ia->ia_addr; 438 hostIsNew = 0; 439 } else if (ifra->ifra_addr.sin_addr.s_addr == 440 ia->ia_addr.sin_addr.s_addr) 441 hostIsNew = 0; 442 } 443 if (ifra->ifra_mask.sin_len) { 444 in_ifscrub(ifp, ia); 445 ia->ia_sockmask = ifra->ifra_mask; 446 ia->ia_sockmask.sin_family = AF_INET; 447 ia->ia_subnetmask = 448 ntohl(ia->ia_sockmask.sin_addr.s_addr); 449 maskIsNew = 1; 450 } 451 if ((ifp->if_flags & IFF_POINTOPOINT) && 452 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 453 in_ifscrub(ifp, ia); 454 ia->ia_dstaddr = ifra->ifra_dstaddr; 455 maskIsNew = 1; /* We lie; but the effect's the same */ 456 } 457 if (ifra->ifra_addr.sin_family == AF_INET && 458 (hostIsNew || maskIsNew)) 459 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 460 if (error != 0 && iaIsNew) 461 break; 462 463 if ((ifp->if_flags & IFF_BROADCAST) && 464 (ifra->ifra_broadaddr.sin_family == AF_INET)) 465 ia->ia_broadaddr = ifra->ifra_broadaddr; 466 if (error == 0) { 467 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0) 468 in_addmulti(&allhosts_addr, ifp); 469 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 470 } 471 return (error); 472 473 case SIOCDIFADDR: 474 /* 475 * in_ifscrub kills the interface route. 476 */ 477 in_ifscrub(ifp, ia); 478 /* 479 * in_ifadown gets rid of all the rest of 480 * the routes. This is not quite the right 481 * thing to do, but at least if we are running 482 * a routing process they will come back. 483 */ 484 in_ifadown(&ia->ia_ifa, 1); 485 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 486 error = 0; 487 break; 488 489 default: 490 if (ifp == NULL || ifp->if_ioctl == NULL) 491 return (EOPNOTSUPP); 492 IFF_LOCKGIANT(ifp); 493 error = (*ifp->if_ioctl)(ifp, cmd, data); 494 IFF_UNLOCKGIANT(ifp); 495 return (error); 496 } 497 498 /* 499 * Protect from ipintr() traversing address list while we're modifying 500 * it. 501 */ 502 s = splnet(); 503 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 504 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 505 if (ia->ia_addr.sin_family == AF_INET) { 506 LIST_REMOVE(ia, ia_hash); 507 /* 508 * If this is the last IPv4 address configured on this 509 * interface, leave the all-hosts group. 510 * XXX: This is quite ugly because of locking and structure. 511 */ 512 oia = NULL; 513 IFP_TO_IA(ifp, oia); 514 if (oia == NULL) { 515 struct in_multi *inm; 516 517 IFF_LOCKGIANT(ifp); 518 IN_MULTI_LOCK(); 519 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm); 520 if (inm != NULL) 521 in_delmulti_locked(inm); 522 IN_MULTI_UNLOCK(); 523 IFF_UNLOCKGIANT(ifp); 524 } 525 } 526 IFAFREE(&ia->ia_ifa); 527 splx(s); 528 529 return (error); 530 } 531 532 /* 533 * SIOC[GAD]LIFADDR. 534 * SIOCGLIFADDR: get first address. (?!?) 535 * SIOCGLIFADDR with IFLR_PREFIX: 536 * get first address that matches the specified prefix. 537 * SIOCALIFADDR: add the specified address. 538 * SIOCALIFADDR with IFLR_PREFIX: 539 * EINVAL since we can't deduce hostid part of the address. 540 * SIOCDLIFADDR: delete the specified address. 541 * SIOCDLIFADDR with IFLR_PREFIX: 542 * delete the first address that matches the specified prefix. 543 * return values: 544 * EINVAL on invalid parameters 545 * EADDRNOTAVAIL on prefix match failed/specified address not found 546 * other values may be returned from in_ioctl() 547 */ 548 static int 549 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 550 struct ifnet *ifp, struct thread *td) 551 { 552 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 553 struct ifaddr *ifa; 554 555 /* sanity checks */ 556 if (data == NULL || ifp == NULL) { 557 panic("invalid argument to in_lifaddr_ioctl"); 558 /*NOTRECHED*/ 559 } 560 561 switch (cmd) { 562 case SIOCGLIFADDR: 563 /* address must be specified on GET with IFLR_PREFIX */ 564 if ((iflr->flags & IFLR_PREFIX) == 0) 565 break; 566 /*FALLTHROUGH*/ 567 case SIOCALIFADDR: 568 case SIOCDLIFADDR: 569 /* address must be specified on ADD and DELETE */ 570 if (iflr->addr.ss_family != AF_INET) 571 return (EINVAL); 572 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 573 return (EINVAL); 574 /* XXX need improvement */ 575 if (iflr->dstaddr.ss_family 576 && iflr->dstaddr.ss_family != AF_INET) 577 return (EINVAL); 578 if (iflr->dstaddr.ss_family 579 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 580 return (EINVAL); 581 break; 582 default: /*shouldn't happen*/ 583 return (EOPNOTSUPP); 584 } 585 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 586 return (EINVAL); 587 588 switch (cmd) { 589 case SIOCALIFADDR: 590 { 591 struct in_aliasreq ifra; 592 593 if (iflr->flags & IFLR_PREFIX) 594 return (EINVAL); 595 596 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 597 bzero(&ifra, sizeof(ifra)); 598 bcopy(iflr->iflr_name, ifra.ifra_name, 599 sizeof(ifra.ifra_name)); 600 601 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 602 603 if (iflr->dstaddr.ss_family) { /*XXX*/ 604 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 605 iflr->dstaddr.ss_len); 606 } 607 608 ifra.ifra_mask.sin_family = AF_INET; 609 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 610 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 611 612 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 613 } 614 case SIOCGLIFADDR: 615 case SIOCDLIFADDR: 616 { 617 struct in_ifaddr *ia; 618 struct in_addr mask, candidate, match; 619 struct sockaddr_in *sin; 620 621 bzero(&mask, sizeof(mask)); 622 bzero(&match, sizeof(match)); 623 if (iflr->flags & IFLR_PREFIX) { 624 /* lookup a prefix rather than address. */ 625 in_len2mask(&mask, iflr->prefixlen); 626 627 sin = (struct sockaddr_in *)&iflr->addr; 628 match.s_addr = sin->sin_addr.s_addr; 629 match.s_addr &= mask.s_addr; 630 631 /* if you set extra bits, that's wrong */ 632 if (match.s_addr != sin->sin_addr.s_addr) 633 return (EINVAL); 634 635 } else { 636 /* on getting an address, take the 1st match */ 637 /* on deleting an address, do exact match */ 638 if (cmd != SIOCGLIFADDR) { 639 in_len2mask(&mask, 32); 640 sin = (struct sockaddr_in *)&iflr->addr; 641 match.s_addr = sin->sin_addr.s_addr; 642 } 643 } 644 645 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 646 if (ifa->ifa_addr->sa_family != AF_INET6) 647 continue; 648 if (match.s_addr == 0) 649 break; 650 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 651 candidate.s_addr &= mask.s_addr; 652 if (candidate.s_addr == match.s_addr) 653 break; 654 } 655 if (ifa == NULL) 656 return (EADDRNOTAVAIL); 657 ia = (struct in_ifaddr *)ifa; 658 659 if (cmd == SIOCGLIFADDR) { 660 /* fill in the if_laddrreq structure */ 661 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 662 663 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 664 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 665 ia->ia_dstaddr.sin_len); 666 } else 667 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 668 669 iflr->prefixlen = 670 in_mask2len(&ia->ia_sockmask.sin_addr); 671 672 iflr->flags = 0; /*XXX*/ 673 674 return (0); 675 } else { 676 struct in_aliasreq ifra; 677 678 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 679 bzero(&ifra, sizeof(ifra)); 680 bcopy(iflr->iflr_name, ifra.ifra_name, 681 sizeof(ifra.ifra_name)); 682 683 bcopy(&ia->ia_addr, &ifra.ifra_addr, 684 ia->ia_addr.sin_len); 685 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 686 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 687 ia->ia_dstaddr.sin_len); 688 } 689 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 690 ia->ia_sockmask.sin_len); 691 692 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 693 ifp, td)); 694 } 695 } 696 } 697 698 return (EOPNOTSUPP); /*just for safety*/ 699 } 700 701 /* 702 * Delete any existing route for an interface. 703 */ 704 void 705 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 706 { 707 708 in_scrubprefix(ia); 709 } 710 711 /* 712 * Initialize an interface's internet address 713 * and routing table entry. 714 */ 715 static int 716 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 717 int scrub) 718 { 719 INIT_VNET_INET(ifp->if_vnet); 720 register u_long i = ntohl(sin->sin_addr.s_addr); 721 struct sockaddr_in oldaddr; 722 int s = splimp(), flags = RTF_UP, error = 0; 723 724 oldaddr = ia->ia_addr; 725 if (oldaddr.sin_family == AF_INET) 726 LIST_REMOVE(ia, ia_hash); 727 ia->ia_addr = *sin; 728 if (ia->ia_addr.sin_family == AF_INET) 729 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 730 ia, ia_hash); 731 /* 732 * Give the interface a chance to initialize 733 * if this is its first address, 734 * and to validate the address if necessary. 735 */ 736 if (ifp->if_ioctl != NULL) { 737 IFF_LOCKGIANT(ifp); 738 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 739 IFF_UNLOCKGIANT(ifp); 740 if (error) { 741 splx(s); 742 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 743 ia->ia_addr = oldaddr; 744 if (ia->ia_addr.sin_family == AF_INET) 745 LIST_INSERT_HEAD(INADDR_HASH( 746 ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 747 else 748 /* 749 * If oldaddr family is not AF_INET (e.g. 750 * interface has been just created) in_control 751 * does not call LIST_REMOVE, and we end up 752 * with bogus ia entries in hash 753 */ 754 LIST_REMOVE(ia, ia_hash); 755 return (error); 756 } 757 } 758 splx(s); 759 if (scrub) { 760 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 761 in_ifscrub(ifp, ia); 762 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 763 } 764 if (IN_CLASSA(i)) 765 ia->ia_netmask = IN_CLASSA_NET; 766 else if (IN_CLASSB(i)) 767 ia->ia_netmask = IN_CLASSB_NET; 768 else 769 ia->ia_netmask = IN_CLASSC_NET; 770 /* 771 * The subnet mask usually includes at least the standard network part, 772 * but may may be smaller in the case of supernetting. 773 * If it is set, we believe it. 774 */ 775 if (ia->ia_subnetmask == 0) { 776 ia->ia_subnetmask = ia->ia_netmask; 777 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 778 } else 779 ia->ia_netmask &= ia->ia_subnetmask; 780 ia->ia_net = i & ia->ia_netmask; 781 ia->ia_subnet = i & ia->ia_subnetmask; 782 in_socktrim(&ia->ia_sockmask); 783 #ifdef DEV_CARP 784 /* 785 * XXX: carp(4) does not have interface route 786 */ 787 if (ifp->if_type == IFT_CARP) 788 return (0); 789 #endif 790 /* 791 * Add route for the network. 792 */ 793 ia->ia_ifa.ifa_metric = ifp->if_metric; 794 if (ifp->if_flags & IFF_BROADCAST) { 795 ia->ia_broadaddr.sin_addr.s_addr = 796 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 797 ia->ia_netbroadcast.s_addr = 798 htonl(ia->ia_net | ~ ia->ia_netmask); 799 } else if (ifp->if_flags & IFF_LOOPBACK) { 800 ia->ia_dstaddr = ia->ia_addr; 801 flags |= RTF_HOST; 802 } else if (ifp->if_flags & IFF_POINTOPOINT) { 803 if (ia->ia_dstaddr.sin_family != AF_INET) 804 return (0); 805 flags |= RTF_HOST; 806 } 807 if ((error = in_addprefix(ia, flags)) != 0) 808 return (error); 809 810 return (error); 811 } 812 813 #define rtinitflags(x) \ 814 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 815 ? RTF_HOST : 0) 816 /* 817 * Check if we have a route for the given prefix already or add one accordingly. 818 */ 819 static int 820 in_addprefix(struct in_ifaddr *target, int flags) 821 { 822 INIT_VNET_INET(curvnet); 823 struct in_ifaddr *ia; 824 struct in_addr prefix, mask, p, m; 825 int error; 826 827 if ((flags & RTF_HOST) != 0) { 828 prefix = target->ia_dstaddr.sin_addr; 829 mask.s_addr = 0; 830 } else { 831 prefix = target->ia_addr.sin_addr; 832 mask = target->ia_sockmask.sin_addr; 833 prefix.s_addr &= mask.s_addr; 834 } 835 836 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 837 if (rtinitflags(ia)) { 838 p = ia->ia_addr.sin_addr; 839 840 if (prefix.s_addr != p.s_addr) 841 continue; 842 } else { 843 p = ia->ia_addr.sin_addr; 844 m = ia->ia_sockmask.sin_addr; 845 p.s_addr &= m.s_addr; 846 847 if (prefix.s_addr != p.s_addr || 848 mask.s_addr != m.s_addr) 849 continue; 850 } 851 852 /* 853 * If we got a matching prefix route inserted by other 854 * interface address, we are done here. 855 */ 856 if (ia->ia_flags & IFA_ROUTE) { 857 if (V_sameprefixcarponly && 858 target->ia_ifp->if_type != IFT_CARP && 859 ia->ia_ifp->if_type != IFT_CARP) 860 return (EEXIST); 861 else 862 return (0); 863 } 864 } 865 866 /* 867 * No-one seem to have this prefix route, so we try to insert it. 868 */ 869 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 870 if (!error) 871 target->ia_flags |= IFA_ROUTE; 872 return (error); 873 } 874 875 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 876 877 /* 878 * If there is no other address in the system that can serve a route to the 879 * same prefix, remove the route. Hand over the route to the new address 880 * otherwise. 881 */ 882 static int 883 in_scrubprefix(struct in_ifaddr *target) 884 { 885 INIT_VNET_INET(curvnet); 886 struct in_ifaddr *ia; 887 struct in_addr prefix, mask, p; 888 int error; 889 890 if ((target->ia_flags & IFA_ROUTE) == 0) 891 return (0); 892 893 if (rtinitflags(target)) 894 prefix = target->ia_dstaddr.sin_addr; 895 else { 896 prefix = target->ia_addr.sin_addr; 897 mask = target->ia_sockmask.sin_addr; 898 prefix.s_addr &= mask.s_addr; 899 /* remove arp cache */ 900 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr); 901 } 902 903 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 904 if (rtinitflags(ia)) 905 p = ia->ia_dstaddr.sin_addr; 906 else { 907 p = ia->ia_addr.sin_addr; 908 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 909 } 910 911 if (prefix.s_addr != p.s_addr) 912 continue; 913 914 /* 915 * If we got a matching prefix address, move IFA_ROUTE and 916 * the route itself to it. Make sure that routing daemons 917 * get a heads-up. 918 * 919 * XXX: a special case for carp(4) interface 920 */ 921 if ((ia->ia_flags & IFA_ROUTE) == 0 922 #ifdef DEV_CARP 923 && (ia->ia_ifp->if_type != IFT_CARP) 924 #endif 925 ) { 926 rtinit(&(target->ia_ifa), (int)RTM_DELETE, 927 rtinitflags(target)); 928 target->ia_flags &= ~IFA_ROUTE; 929 930 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 931 rtinitflags(ia) | RTF_UP); 932 if (error == 0) 933 ia->ia_flags |= IFA_ROUTE; 934 return (error); 935 } 936 } 937 938 /* 939 * As no-one seem to have this prefix, we can remove the route. 940 */ 941 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 942 target->ia_flags &= ~IFA_ROUTE; 943 return (0); 944 } 945 946 #undef rtinitflags 947 948 /* 949 * Return 1 if the address might be a local broadcast address. 950 */ 951 int 952 in_broadcast(struct in_addr in, struct ifnet *ifp) 953 { 954 register struct ifaddr *ifa; 955 u_long t; 956 957 if (in.s_addr == INADDR_BROADCAST || 958 in.s_addr == INADDR_ANY) 959 return (1); 960 if ((ifp->if_flags & IFF_BROADCAST) == 0) 961 return (0); 962 t = ntohl(in.s_addr); 963 /* 964 * Look through the list of addresses for a match 965 * with a broadcast address. 966 */ 967 #define ia ((struct in_ifaddr *)ifa) 968 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 969 if (ifa->ifa_addr->sa_family == AF_INET && 970 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 971 in.s_addr == ia->ia_netbroadcast.s_addr || 972 /* 973 * Check for old-style (host 0) broadcast. 974 */ 975 t == ia->ia_subnet || t == ia->ia_net) && 976 /* 977 * Check for an all one subnetmask. These 978 * only exist when an interface gets a secondary 979 * address. 980 */ 981 ia->ia_subnetmask != (u_long)0xffffffff) 982 return (1); 983 return (0); 984 #undef ia 985 } 986 987 /* 988 * Delete all IPv4 multicast address records, and associated link-layer 989 * multicast address records, associated with ifp. 990 */ 991 static void 992 in_purgemaddrs(struct ifnet *ifp) 993 { 994 INIT_VNET_INET(ifp->if_vnet); 995 struct in_multi *inm; 996 struct in_multi *oinm; 997 998 #ifdef DIAGNOSTIC 999 printf("%s: purging ifp %p\n", __func__, ifp); 1000 #endif 1001 IFF_LOCKGIANT(ifp); 1002 IN_MULTI_LOCK(); 1003 LIST_FOREACH_SAFE(inm, &V_in_multihead, inm_link, oinm) { 1004 if (inm->inm_ifp == ifp) 1005 in_delmulti_locked(inm); 1006 } 1007 IN_MULTI_UNLOCK(); 1008 IFF_UNLOCKGIANT(ifp); 1009 } 1010 1011 /* 1012 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1013 */ 1014 void 1015 in_ifdetach(struct ifnet *ifp) 1016 { 1017 INIT_VNET_INET(ifp->if_vnet); 1018 1019 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1020 in_pcbpurgeif0(&V_udbinfo, ifp); 1021 in_purgemaddrs(ifp); 1022 } 1023 1024 #include <sys/syslog.h> 1025 #include <net/if_dl.h> 1026 #include <netinet/if_ether.h> 1027 1028 struct in_llentry { 1029 struct llentry base; 1030 struct sockaddr_in l3_addr4; 1031 }; 1032 1033 static struct llentry * 1034 in_lltable_new(const struct sockaddr *l3addr, u_int flags) 1035 { 1036 struct in_llentry *lle; 1037 1038 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO); 1039 if (lle == NULL) /* NB: caller generates msg */ 1040 return NULL; 1041 1042 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 1043 /* 1044 * For IPv4 this will trigger "arpresolve" to generate 1045 * an ARP request. 1046 */ 1047 lle->base.la_expire = time_second; /* mark expired */ 1048 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 1049 lle->base.lle_refcnt = 1; 1050 LLE_LOCK_INIT(&lle->base); 1051 return &lle->base; 1052 } 1053 1054 /* 1055 * Deletes an address from the address table. 1056 * This function is called by the timer functions 1057 * such as arptimer() and nd6_llinfo_timer(), and 1058 * the caller does the locking. 1059 */ 1060 static void 1061 in_lltable_free(struct lltable *llt, struct llentry *lle) 1062 { 1063 LLE_WUNLOCK(lle); 1064 LLE_LOCK_DESTROY(lle); 1065 free(lle, M_LLTABLE); 1066 } 1067 1068 static int 1069 in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 1070 { 1071 struct rtentry *rt; 1072 1073 KASSERT(l3addr->sa_family == AF_INET, 1074 ("sin_family %d", l3addr->sa_family)); 1075 1076 /* XXX rtalloc1 should take a const param */ 1077 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 1078 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 1079 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1080 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1081 if (rt != NULL) 1082 RTFREE_LOCKED(rt); 1083 return (EINVAL); 1084 } 1085 RTFREE_LOCKED(rt); 1086 return 0; 1087 } 1088 1089 /* 1090 * Return NULL if not found or marked for deletion. 1091 * If found return lle read locked. 1092 */ 1093 static struct llentry * 1094 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1095 { 1096 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1097 struct ifnet *ifp = llt->llt_ifp; 1098 struct llentry *lle; 1099 struct llentries *lleh; 1100 u_int hashkey; 1101 1102 IF_AFDATA_LOCK_ASSERT(ifp); 1103 KASSERT(l3addr->sa_family == AF_INET, 1104 ("sin_family %d", l3addr->sa_family)); 1105 1106 hashkey = sin->sin_addr.s_addr; 1107 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1108 LIST_FOREACH(lle, lleh, lle_next) { 1109 if (lle->la_flags & LLE_DELETED) 1110 continue; 1111 if (bcmp(L3_ADDR(lle), l3addr, sizeof(struct sockaddr_in)) == 0) 1112 break; 1113 } 1114 if (lle == NULL) { 1115 #ifdef DIAGNOSTICS 1116 if (flags & LLE_DELETE) 1117 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1118 #endif 1119 if (!(flags & LLE_CREATE)) 1120 return (NULL); 1121 /* 1122 * A route that covers the given address must have 1123 * been installed 1st because we are doing a resolution, 1124 * verify this. 1125 */ 1126 if (!(flags & LLE_IFADDR) && 1127 in_lltable_rtcheck(ifp, l3addr) != 0) 1128 goto done; 1129 1130 lle = in_lltable_new(l3addr, flags); 1131 if (lle == NULL) { 1132 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1133 goto done; 1134 } 1135 lle->la_flags = flags & ~LLE_CREATE; 1136 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1137 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1138 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1139 } 1140 1141 lle->lle_tbl = llt; 1142 lle->lle_head = lleh; 1143 LIST_INSERT_HEAD(lleh, lle, lle_next); 1144 } else if (flags & LLE_DELETE) { 1145 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1146 LLE_WLOCK(lle); 1147 lle->la_flags = LLE_DELETED; 1148 LLE_WUNLOCK(lle); 1149 #ifdef DIAGNOSTICS 1150 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1151 #endif 1152 } 1153 lle = (void *)-1; 1154 1155 } 1156 if (LLE_IS_VALID(lle)) { 1157 if (flags & LLE_EXCLUSIVE) 1158 LLE_WLOCK(lle); 1159 else 1160 LLE_RLOCK(lle); 1161 } 1162 done: 1163 return (lle); 1164 } 1165 1166 static int 1167 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1168 { 1169 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1170 struct ifnet *ifp = llt->llt_ifp; 1171 struct llentry *lle; 1172 /* XXX stack use */ 1173 struct { 1174 struct rt_msghdr rtm; 1175 struct sockaddr_inarp sin; 1176 struct sockaddr_dl sdl; 1177 } arpc; 1178 int error, i; 1179 1180 /* XXXXX 1181 * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 1182 * so it is okay to use this ASSERT, change it when 1183 * IFNET lock is finalized 1184 */ 1185 IFNET_WLOCK_ASSERT(); 1186 1187 error = 0; 1188 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1189 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1190 struct sockaddr_dl *sdl; 1191 1192 /* skip deleted entries */ 1193 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 1194 continue; 1195 /* 1196 * produce a msg made of: 1197 * struct rt_msghdr; 1198 * struct sockaddr_inarp; (IPv4) 1199 * struct sockaddr_dl; 1200 */ 1201 bzero(&arpc, sizeof(arpc)); 1202 arpc.rtm.rtm_msglen = sizeof(arpc); 1203 arpc.sin.sin_family = AF_INET; 1204 arpc.sin.sin_len = sizeof(arpc.sin); 1205 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1206 1207 /* publish */ 1208 if (lle->la_flags & LLE_PUB) { 1209 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1210 /* proxy only */ 1211 if (lle->la_flags & LLE_PROXY) 1212 arpc.sin.sin_other = SIN_PROXY; 1213 } 1214 1215 sdl = &arpc.sdl; 1216 sdl->sdl_family = AF_LINK; 1217 sdl->sdl_len = sizeof(*sdl); 1218 sdl->sdl_alen = ifp->if_addrlen; 1219 sdl->sdl_index = ifp->if_index; 1220 sdl->sdl_type = ifp->if_type; 1221 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1222 1223 arpc.rtm.rtm_rmx.rmx_expire = 1224 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1225 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1226 if (lle->la_flags & LLE_STATIC) 1227 arpc.rtm.rtm_flags |= RTF_STATIC; 1228 arpc.rtm.rtm_index = ifp->if_index; 1229 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1230 if (error) 1231 break; 1232 } 1233 } 1234 return error; 1235 #undef SIN 1236 } 1237 1238 void * 1239 in_domifattach(struct ifnet *ifp) 1240 { 1241 struct lltable *llt = lltable_init(ifp, AF_INET); 1242 1243 if (llt != NULL) { 1244 llt->llt_new = in_lltable_new; 1245 llt->llt_free = in_lltable_free; 1246 llt->llt_rtcheck = in_lltable_rtcheck; 1247 llt->llt_lookup = in_lltable_lookup; 1248 llt->llt_dump = in_lltable_dump; 1249 } 1250 return (llt); 1251 } 1252 1253 void 1254 in_domifdetach(struct ifnet *ifp __unused, void *aux) 1255 { 1256 struct lltable *llt = (struct lltable *)aux; 1257 1258 lltable_free(llt); 1259 } 1260