in.c revision 192262
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: head/sys/netinet/in.c 192262 2009-05-17 20:53:10Z bz $"); 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/jail.h> 45#include <sys/kernel.h> 46#include <sys/proc.h> 47#include <sys/sysctl.h> 48#include <sys/syslog.h> 49#include <sys/vimage.h> 50 51#include <net/if.h> 52#include <net/if_dl.h> 53#include <net/if_llatbl.h> 54#include <net/if_types.h> 55#include <net/route.h> 56#include <net/vnet.h> 57 58#include <netinet/in.h> 59#include <netinet/in_var.h> 60#include <netinet/in_pcb.h> 61#include <netinet/ip_var.h> 62#include <netinet/vinet.h> 63#include <netinet/igmp_var.h> 64 65static int in_mask2len(struct in_addr *); 66static void in_len2mask(struct in_addr *, int); 67static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 68 struct ifnet *, struct thread *); 69 70static int in_addprefix(struct in_ifaddr *, int); 71static int in_scrubprefix(struct in_ifaddr *); 72static void in_socktrim(struct sockaddr_in *); 73static int in_ifinit(struct ifnet *, 74 struct in_ifaddr *, struct sockaddr_in *, int); 75static void in_purgemaddrs(struct ifnet *); 76 77#ifdef VIMAGE_GLOBALS 78static int subnetsarelocal; 79static int sameprefixcarponly; 80extern struct inpcbinfo ripcbinfo; 81#endif 82 83SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local, 84 CTLFLAG_RW, subnetsarelocal, 0, 85 "Treat all subnets as directly connected"); 86SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only, 87 CTLFLAG_RW, sameprefixcarponly, 0, 88 "Refuse to create same prefixes on different interfaces"); 89 90/* 91 * Return 1 if an internet address is for a ``local'' host 92 * (one to which we have a connection). If subnetsarelocal 93 * is true, this includes other subnets of the local net. 94 * Otherwise, it includes only the directly-connected (sub)nets. 95 */ 96int 97in_localaddr(struct in_addr in) 98{ 99 INIT_VNET_INET(curvnet); 100 register u_long i = ntohl(in.s_addr); 101 register struct in_ifaddr *ia; 102 103 if (V_subnetsarelocal) { 104 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 105 if ((i & ia->ia_netmask) == ia->ia_net) 106 return (1); 107 } else { 108 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 109 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 110 return (1); 111 } 112 return (0); 113} 114 115/* 116 * Return 1 if an internet address is for the local host and configured 117 * on one of its interfaces. 118 */ 119int 120in_localip(struct in_addr in) 121{ 122 INIT_VNET_INET(curvnet); 123 struct in_ifaddr *ia; 124 125 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 126 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 127 return (1); 128 } 129 return (0); 130} 131 132/* 133 * Determine whether an IP address is in a reserved set of addresses 134 * that may not be forwarded, or whether datagrams to that destination 135 * may be forwarded. 136 */ 137int 138in_canforward(struct in_addr in) 139{ 140 register u_long i = ntohl(in.s_addr); 141 register u_long net; 142 143 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 144 return (0); 145 if (IN_CLASSA(i)) { 146 net = i & IN_CLASSA_NET; 147 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 148 return (0); 149 } 150 return (1); 151} 152 153/* 154 * Trim a mask in a sockaddr 155 */ 156static void 157in_socktrim(struct sockaddr_in *ap) 158{ 159 register char *cplim = (char *) &ap->sin_addr; 160 register char *cp = (char *) (&ap->sin_addr + 1); 161 162 ap->sin_len = 0; 163 while (--cp >= cplim) 164 if (*cp) { 165 (ap)->sin_len = cp - (char *) (ap) + 1; 166 break; 167 } 168} 169 170static int 171in_mask2len(mask) 172 struct in_addr *mask; 173{ 174 int x, y; 175 u_char *p; 176 177 p = (u_char *)mask; 178 for (x = 0; x < sizeof(*mask); x++) { 179 if (p[x] != 0xff) 180 break; 181 } 182 y = 0; 183 if (x < sizeof(*mask)) { 184 for (y = 0; y < 8; y++) { 185 if ((p[x] & (0x80 >> y)) == 0) 186 break; 187 } 188 } 189 return (x * 8 + y); 190} 191 192static void 193in_len2mask(struct in_addr *mask, int len) 194{ 195 int i; 196 u_char *p; 197 198 p = (u_char *)mask; 199 bzero(mask, sizeof(*mask)); 200 for (i = 0; i < len / 8; i++) 201 p[i] = 0xff; 202 if (len % 8) 203 p[i] = (0xff00 >> (len % 8)) & 0xff; 204} 205 206/* 207 * Generic internet control operations (ioctl's). 208 * 209 * ifp is NULL if not an interface-specific ioctl. 210 */ 211/* ARGSUSED */ 212int 213in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 214 struct thread *td) 215{ 216 INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */ 217 register struct ifreq *ifr = (struct ifreq *)data; 218 register struct in_ifaddr *ia, *iap; 219 register struct ifaddr *ifa; 220 struct in_addr allhosts_addr; 221 struct in_addr dst; 222 struct in_ifaddr *oia; 223 struct in_ifinfo *ii; 224 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 225 struct sockaddr_in oldaddr; 226 int error, hostIsNew, iaIsNew, maskIsNew, s; 227 int iaIsFirst; 228 229 ia = NULL; 230 iaIsFirst = 0; 231 iaIsNew = 0; 232 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 233 234 /* 235 * Filter out ioctls we implement directly; forward the rest on to 236 * in_lifaddr_ioctl() and ifp->if_ioctl(). 237 */ 238 switch (cmd) { 239 case SIOCAIFADDR: 240 case SIOCDIFADDR: 241 case SIOCGIFADDR: 242 case SIOCGIFBRDADDR: 243 case SIOCGIFDSTADDR: 244 case SIOCGIFNETMASK: 245 case SIOCSIFADDR: 246 case SIOCSIFBRDADDR: 247 case SIOCSIFDSTADDR: 248 case SIOCSIFNETMASK: 249 break; 250 251 case SIOCALIFADDR: 252 if (td != NULL) { 253 error = priv_check(td, PRIV_NET_ADDIFADDR); 254 if (error) 255 return (error); 256 } 257 if (ifp == NULL) 258 return (EINVAL); 259 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 260 261 case SIOCDLIFADDR: 262 if (td != NULL) { 263 error = priv_check(td, PRIV_NET_DELIFADDR); 264 if (error) 265 return (error); 266 } 267 if (ifp == NULL) 268 return (EINVAL); 269 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 270 271 case SIOCGLIFADDR: 272 if (ifp == NULL) 273 return (EINVAL); 274 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 275 276 default: 277 if (ifp == NULL || ifp->if_ioctl == NULL) 278 return (EOPNOTSUPP); 279 return ((*ifp->if_ioctl)(ifp, cmd, data)); 280 } 281 282 if (ifp == NULL) 283 return (EADDRNOTAVAIL); 284 285 /* 286 * Security checks before we get involved in any work. 287 */ 288 switch (cmd) { 289 case SIOCAIFADDR: 290 case SIOCSIFADDR: 291 case SIOCSIFBRDADDR: 292 case SIOCSIFNETMASK: 293 case SIOCSIFDSTADDR: 294 if (td != NULL) { 295 error = priv_check(td, PRIV_NET_ADDIFADDR); 296 if (error) 297 return (error); 298 } 299 break; 300 301 case SIOCDIFADDR: 302 if (td != NULL) { 303 error = priv_check(td, PRIV_NET_DELIFADDR); 304 if (error) 305 return (error); 306 } 307 break; 308 } 309 310 /* 311 * Find address for this interface, if it exists. 312 * 313 * If an alias address was specified, find that one instead of the 314 * first one on the interface, if possible. 315 */ 316 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 317 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) { 318 if (iap->ia_ifp == ifp && 319 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 320 if (td == NULL || prison_check_ip4(td->td_ucred, 321 &dst) == 0) 322 ia = iap; 323 break; 324 } 325 } 326 IF_ADDR_LOCK(ifp); 327 if (ia == NULL) { 328 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 329 iap = ifatoia(ifa); 330 if (iap->ia_addr.sin_family == AF_INET) { 331 if (td != NULL && 332 prison_check_ip4(td->td_ucred, 333 &iap->ia_addr.sin_addr) != 0) 334 continue; 335 ia = iap; 336 break; 337 } 338 } 339 } 340 if (ia == NULL) 341 iaIsFirst = 1; 342 343 error = 0; 344 switch (cmd) { 345 case SIOCAIFADDR: 346 case SIOCDIFADDR: 347 if (ifra->ifra_addr.sin_family == AF_INET) { 348 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 349 if (ia->ia_ifp == ifp && 350 ia->ia_addr.sin_addr.s_addr == 351 ifra->ifra_addr.sin_addr.s_addr) 352 break; 353 } 354 if ((ifp->if_flags & IFF_POINTOPOINT) 355 && (cmd == SIOCAIFADDR) 356 && (ifra->ifra_dstaddr.sin_addr.s_addr 357 == INADDR_ANY)) { 358 error = EDESTADDRREQ; 359 goto out_unlock; 360 } 361 } 362 if (cmd == SIOCDIFADDR && ia == NULL) { 363 error = EADDRNOTAVAIL; 364 goto out_unlock; 365 } 366 /* FALLTHROUGH */ 367 case SIOCSIFADDR: 368 case SIOCSIFNETMASK: 369 case SIOCSIFDSTADDR: 370 if (ia == NULL) { 371 ia = (struct in_ifaddr *) 372 malloc(sizeof *ia, M_IFADDR, M_NOWAIT | 373 M_ZERO); 374 if (ia == NULL) { 375 error = ENOBUFS; 376 goto out_unlock; 377 } 378 379 ifa = &ia->ia_ifa; 380 IFA_LOCK_INIT(ifa); 381 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 382 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 383 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 384 ifa->ifa_refcnt = 1; 385 386 ia->ia_sockmask.sin_len = 8; 387 ia->ia_sockmask.sin_family = AF_INET; 388 if (ifp->if_flags & IFF_BROADCAST) { 389 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 390 ia->ia_broadaddr.sin_family = AF_INET; 391 } 392 ia->ia_ifp = ifp; 393 394 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 395 s = splnet(); 396 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 397 splx(s); 398 iaIsNew = 1; 399 } 400 break; 401 402 case SIOCSIFBRDADDR: 403 case SIOCGIFADDR: 404 case SIOCGIFNETMASK: 405 case SIOCGIFDSTADDR: 406 case SIOCGIFBRDADDR: 407 if (ia == NULL) { 408 error = EADDRNOTAVAIL; 409 goto out_unlock; 410 } 411 break; 412 } 413 414 /* 415 * Most paths in this switch return directly or via out_unlock. Only 416 * paths that remove the address break in order to hit common removal 417 * code. 418 * 419 * XXXRW: We enter the switch with IF_ADDR_LOCK() held, but leave 420 * without it. This is a bug. 421 */ 422 IF_ADDR_LOCK_ASSERT(ifp); 423 switch (cmd) { 424 case SIOCGIFADDR: 425 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 426 goto out_unlock; 427 428 case SIOCGIFBRDADDR: 429 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 430 error = EINVAL; 431 goto out_unlock; 432 } 433 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 434 goto out_unlock; 435 436 case SIOCGIFDSTADDR: 437 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 438 error = EINVAL; 439 goto out_unlock; 440 } 441 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 442 goto out_unlock; 443 444 case SIOCGIFNETMASK: 445 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 446 goto out_unlock; 447 448 case SIOCSIFDSTADDR: 449 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 450 error = EINVAL; 451 goto out_unlock; 452 } 453 oldaddr = ia->ia_dstaddr; 454 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 455 IF_ADDR_UNLOCK(ifp); 456 457 /* 458 * XXXRW: Locks dropped for if_ioctl and rtinit, but ia is 459 * still being used. 460 */ 461 if (ifp->if_ioctl != NULL) { 462 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 463 (caddr_t)ia); 464 if (error) { 465 ia->ia_dstaddr = oldaddr; 466 return (error); 467 } 468 } 469 if (ia->ia_flags & IFA_ROUTE) { 470 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 471 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 472 ia->ia_ifa.ifa_dstaddr = 473 (struct sockaddr *)&ia->ia_dstaddr; 474 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 475 } 476 return (0); 477 478 case SIOCSIFBRDADDR: 479 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 480 error = EINVAL; 481 goto out_unlock; 482 } 483 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 484 goto out_unlock; 485 486 case SIOCSIFADDR: 487 IF_ADDR_UNLOCK(ifp); 488 489 /* 490 * XXXRW: Locks dropped for in_ifinit and in_joingroup, but ia 491 * is still being used. 492 */ 493 error = in_ifinit(ifp, ia, 494 (struct sockaddr_in *) &ifr->ifr_addr, 1); 495 if (error != 0 && iaIsNew) 496 break; 497 if (error == 0) { 498 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 499 if (iaIsFirst && 500 (ifp->if_flags & IFF_MULTICAST) != 0) { 501 error = in_joingroup(ifp, &allhosts_addr, 502 NULL, &ii->ii_allhosts); 503 } 504 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 505 } 506 return (0); 507 508 case SIOCSIFNETMASK: 509 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 510 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 511 goto out_unlock; 512 513 case SIOCAIFADDR: 514 maskIsNew = 0; 515 hostIsNew = 1; 516 error = 0; 517 if (ia->ia_addr.sin_family == AF_INET) { 518 if (ifra->ifra_addr.sin_len == 0) { 519 ifra->ifra_addr = ia->ia_addr; 520 hostIsNew = 0; 521 } else if (ifra->ifra_addr.sin_addr.s_addr == 522 ia->ia_addr.sin_addr.s_addr) 523 hostIsNew = 0; 524 } 525 IF_ADDR_UNLOCK(ifp); 526 527 /* 528 * XXXRW: Locks dropped for in_ifscrub and in_ifinit, but ia 529 * is still being used. 530 */ 531 if (ifra->ifra_mask.sin_len) { 532 in_ifscrub(ifp, ia); 533 ia->ia_sockmask = ifra->ifra_mask; 534 ia->ia_sockmask.sin_family = AF_INET; 535 ia->ia_subnetmask = 536 ntohl(ia->ia_sockmask.sin_addr.s_addr); 537 maskIsNew = 1; 538 } 539 if ((ifp->if_flags & IFF_POINTOPOINT) && 540 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 541 in_ifscrub(ifp, ia); 542 ia->ia_dstaddr = ifra->ifra_dstaddr; 543 maskIsNew = 1; /* We lie; but the effect's the same */ 544 } 545 if (ifra->ifra_addr.sin_family == AF_INET && 546 (hostIsNew || maskIsNew)) 547 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 548 if (error != 0 && iaIsNew) 549 break; 550 551 if ((ifp->if_flags & IFF_BROADCAST) && 552 (ifra->ifra_broadaddr.sin_family == AF_INET)) 553 ia->ia_broadaddr = ifra->ifra_broadaddr; 554 if (error == 0) { 555 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 556 if (iaIsFirst && 557 (ifp->if_flags & IFF_MULTICAST) != 0) { 558 error = in_joingroup(ifp, &allhosts_addr, 559 NULL, &ii->ii_allhosts); 560 } 561 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 562 } 563 return (error); 564 565 case SIOCDIFADDR: 566 IF_ADDR_UNLOCK(ifp); 567 568 /* 569 * XXXRW: Locks dropped for in_ifscrub and in_ifadown, but ia 570 * is still being used. 571 * 572 * in_ifscrub kills the interface route. 573 */ 574 in_ifscrub(ifp, ia); 575 576 /* 577 * in_ifadown gets rid of all the rest of 578 * the routes. This is not quite the right 579 * thing to do, but at least if we are running 580 * a routing process they will come back. 581 */ 582 in_ifadown(&ia->ia_ifa, 1); 583 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 584 error = 0; 585 break; 586 587 default: 588 panic("in_control: unsupported ioctl"); 589 } 590 591 /* 592 * XXXRW: In a more ideal world, we would still be holding 593 * IF_ADDR_LOCK here. 594 */ 595 IF_ADDR_LOCK(ifp); 596 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 597 IF_ADDR_UNLOCK(ifp); 598 s = splnet(); 599 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 600 if (ia->ia_addr.sin_family == AF_INET) { 601 LIST_REMOVE(ia, ia_hash); 602 /* 603 * If this is the last IPv4 address configured on this 604 * interface, leave the all-hosts group. 605 * No state-change report need be transmitted. 606 */ 607 oia = NULL; 608 IFP_TO_IA(ifp, oia); 609 if (oia == NULL) { 610 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 611 IN_MULTI_LOCK(); 612 if (ii->ii_allhosts) { 613 (void)in_leavegroup_locked(ii->ii_allhosts, 614 NULL); 615 ii->ii_allhosts = NULL; 616 } 617 IN_MULTI_UNLOCK(); 618 } 619 } 620 IFAFREE(&ia->ia_ifa); 621 splx(s); 622 623 return (error); 624 625out_unlock: 626 IF_ADDR_UNLOCK(ifp); 627 return (error); 628} 629 630/* 631 * SIOC[GAD]LIFADDR. 632 * SIOCGLIFADDR: get first address. (?!?) 633 * SIOCGLIFADDR with IFLR_PREFIX: 634 * get first address that matches the specified prefix. 635 * SIOCALIFADDR: add the specified address. 636 * SIOCALIFADDR with IFLR_PREFIX: 637 * EINVAL since we can't deduce hostid part of the address. 638 * SIOCDLIFADDR: delete the specified address. 639 * SIOCDLIFADDR with IFLR_PREFIX: 640 * delete the first address that matches the specified prefix. 641 * return values: 642 * EINVAL on invalid parameters 643 * EADDRNOTAVAIL on prefix match failed/specified address not found 644 * other values may be returned from in_ioctl() 645 */ 646static int 647in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 648 struct ifnet *ifp, struct thread *td) 649{ 650 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 651 struct ifaddr *ifa; 652 653 /* sanity checks */ 654 if (data == NULL || ifp == NULL) { 655 panic("invalid argument to in_lifaddr_ioctl"); 656 /*NOTRECHED*/ 657 } 658 659 switch (cmd) { 660 case SIOCGLIFADDR: 661 /* address must be specified on GET with IFLR_PREFIX */ 662 if ((iflr->flags & IFLR_PREFIX) == 0) 663 break; 664 /*FALLTHROUGH*/ 665 case SIOCALIFADDR: 666 case SIOCDLIFADDR: 667 /* address must be specified on ADD and DELETE */ 668 if (iflr->addr.ss_family != AF_INET) 669 return (EINVAL); 670 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 671 return (EINVAL); 672 /* XXX need improvement */ 673 if (iflr->dstaddr.ss_family 674 && iflr->dstaddr.ss_family != AF_INET) 675 return (EINVAL); 676 if (iflr->dstaddr.ss_family 677 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 678 return (EINVAL); 679 break; 680 default: /*shouldn't happen*/ 681 return (EOPNOTSUPP); 682 } 683 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 684 return (EINVAL); 685 686 switch (cmd) { 687 case SIOCALIFADDR: 688 { 689 struct in_aliasreq ifra; 690 691 if (iflr->flags & IFLR_PREFIX) 692 return (EINVAL); 693 694 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 695 bzero(&ifra, sizeof(ifra)); 696 bcopy(iflr->iflr_name, ifra.ifra_name, 697 sizeof(ifra.ifra_name)); 698 699 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 700 701 if (iflr->dstaddr.ss_family) { /*XXX*/ 702 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 703 iflr->dstaddr.ss_len); 704 } 705 706 ifra.ifra_mask.sin_family = AF_INET; 707 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 708 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 709 710 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 711 } 712 case SIOCGLIFADDR: 713 case SIOCDLIFADDR: 714 { 715 struct in_ifaddr *ia; 716 struct in_addr mask, candidate, match; 717 struct sockaddr_in *sin; 718 719 bzero(&mask, sizeof(mask)); 720 bzero(&match, sizeof(match)); 721 if (iflr->flags & IFLR_PREFIX) { 722 /* lookup a prefix rather than address. */ 723 in_len2mask(&mask, iflr->prefixlen); 724 725 sin = (struct sockaddr_in *)&iflr->addr; 726 match.s_addr = sin->sin_addr.s_addr; 727 match.s_addr &= mask.s_addr; 728 729 /* if you set extra bits, that's wrong */ 730 if (match.s_addr != sin->sin_addr.s_addr) 731 return (EINVAL); 732 733 } else { 734 /* on getting an address, take the 1st match */ 735 /* on deleting an address, do exact match */ 736 if (cmd != SIOCGLIFADDR) { 737 in_len2mask(&mask, 32); 738 sin = (struct sockaddr_in *)&iflr->addr; 739 match.s_addr = sin->sin_addr.s_addr; 740 } 741 } 742 743 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 744 if (ifa->ifa_addr->sa_family != AF_INET6) 745 continue; 746 if (match.s_addr == 0) 747 break; 748 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 749 candidate.s_addr &= mask.s_addr; 750 if (candidate.s_addr == match.s_addr) 751 break; 752 } 753 if (ifa == NULL) 754 return (EADDRNOTAVAIL); 755 ia = (struct in_ifaddr *)ifa; 756 757 if (cmd == SIOCGLIFADDR) { 758 /* fill in the if_laddrreq structure */ 759 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 760 761 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 762 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 763 ia->ia_dstaddr.sin_len); 764 } else 765 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 766 767 iflr->prefixlen = 768 in_mask2len(&ia->ia_sockmask.sin_addr); 769 770 iflr->flags = 0; /*XXX*/ 771 772 return (0); 773 } else { 774 struct in_aliasreq ifra; 775 776 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 777 bzero(&ifra, sizeof(ifra)); 778 bcopy(iflr->iflr_name, ifra.ifra_name, 779 sizeof(ifra.ifra_name)); 780 781 bcopy(&ia->ia_addr, &ifra.ifra_addr, 782 ia->ia_addr.sin_len); 783 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 784 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 785 ia->ia_dstaddr.sin_len); 786 } 787 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 788 ia->ia_sockmask.sin_len); 789 790 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 791 ifp, td)); 792 } 793 } 794 } 795 796 return (EOPNOTSUPP); /*just for safety*/ 797} 798 799/* 800 * Delete any existing route for an interface. 801 */ 802void 803in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 804{ 805 806 in_scrubprefix(ia); 807} 808 809/* 810 * Initialize an interface's internet address 811 * and routing table entry. 812 */ 813static int 814in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 815 int scrub) 816{ 817 INIT_VNET_NET(ifp->if_vnet); 818 INIT_VNET_INET(ifp->if_vnet); 819 register u_long i = ntohl(sin->sin_addr.s_addr); 820 struct sockaddr_in oldaddr; 821 struct rtentry *rt = NULL; 822 struct rt_addrinfo info; 823 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 824 int s = splimp(), flags = RTF_UP, error = 0; 825 826 oldaddr = ia->ia_addr; 827 if (oldaddr.sin_family == AF_INET) 828 LIST_REMOVE(ia, ia_hash); 829 ia->ia_addr = *sin; 830 if (ia->ia_addr.sin_family == AF_INET) 831 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 832 ia, ia_hash); 833 /* 834 * Give the interface a chance to initialize 835 * if this is its first address, 836 * and to validate the address if necessary. 837 */ 838 if (ifp->if_ioctl != NULL) { 839 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 840 if (error) { 841 splx(s); 842 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 843 ia->ia_addr = oldaddr; 844 if (ia->ia_addr.sin_family == AF_INET) 845 LIST_INSERT_HEAD(INADDR_HASH( 846 ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 847 else 848 /* 849 * If oldaddr family is not AF_INET (e.g. 850 * interface has been just created) in_control 851 * does not call LIST_REMOVE, and we end up 852 * with bogus ia entries in hash 853 */ 854 LIST_REMOVE(ia, ia_hash); 855 return (error); 856 } 857 } 858 splx(s); 859 if (scrub) { 860 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 861 in_ifscrub(ifp, ia); 862 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 863 } 864 if (IN_CLASSA(i)) 865 ia->ia_netmask = IN_CLASSA_NET; 866 else if (IN_CLASSB(i)) 867 ia->ia_netmask = IN_CLASSB_NET; 868 else 869 ia->ia_netmask = IN_CLASSC_NET; 870 /* 871 * The subnet mask usually includes at least the standard network part, 872 * but may may be smaller in the case of supernetting. 873 * If it is set, we believe it. 874 */ 875 if (ia->ia_subnetmask == 0) { 876 ia->ia_subnetmask = ia->ia_netmask; 877 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 878 } else 879 ia->ia_netmask &= ia->ia_subnetmask; 880 ia->ia_net = i & ia->ia_netmask; 881 ia->ia_subnet = i & ia->ia_subnetmask; 882 in_socktrim(&ia->ia_sockmask); 883#ifdef DEV_CARP 884 /* 885 * XXX: carp(4) does not have interface route 886 */ 887 if (ifp->if_type == IFT_CARP) 888 return (0); 889#endif 890 /* 891 * Add route for the network. 892 */ 893 ia->ia_ifa.ifa_metric = ifp->if_metric; 894 if (ifp->if_flags & IFF_BROADCAST) { 895 ia->ia_broadaddr.sin_addr.s_addr = 896 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 897 ia->ia_netbroadcast.s_addr = 898 htonl(ia->ia_net | ~ ia->ia_netmask); 899 } else if (ifp->if_flags & IFF_LOOPBACK) { 900 ia->ia_dstaddr = ia->ia_addr; 901 flags |= RTF_HOST; 902 } else if (ifp->if_flags & IFF_POINTOPOINT) { 903 if (ia->ia_dstaddr.sin_family != AF_INET) 904 return (0); 905 flags |= RTF_HOST; 906 } 907 if ((error = in_addprefix(ia, flags)) != 0) 908 return (error); 909 910 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 911 return (0); 912 913 /* 914 * add a loopback route to self 915 */ 916 if (!(ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 917 bzero(&info, sizeof(info)); 918 info.rti_ifp = V_loif; 919 info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC; 920 info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr; 921 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 922 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); 923 924 if (error == 0 && rt != NULL) { 925 RT_LOCK(rt); 926 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 927 rt->rt_ifp->if_type; 928 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 929 rt->rt_ifp->if_index; 930 RT_REMREF(rt); 931 RT_UNLOCK(rt); 932 } else if (error != 0) 933 log(LOG_INFO, "in_ifinit: insertion failed\n"); 934 } 935 936 return (error); 937} 938 939#define rtinitflags(x) \ 940 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 941 ? RTF_HOST : 0) 942/* 943 * Check if we have a route for the given prefix already or add one accordingly. 944 */ 945static int 946in_addprefix(struct in_ifaddr *target, int flags) 947{ 948 INIT_VNET_INET(curvnet); 949 struct in_ifaddr *ia; 950 struct in_addr prefix, mask, p, m; 951 int error; 952 953 if ((flags & RTF_HOST) != 0) { 954 prefix = target->ia_dstaddr.sin_addr; 955 mask.s_addr = 0; 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, &V_in_ifaddrhead, ia_link) { 963 if (rtinitflags(ia)) { 964 p = ia->ia_addr.sin_addr; 965 966 if (prefix.s_addr != p.s_addr) 967 continue; 968 } else { 969 p = ia->ia_addr.sin_addr; 970 m = ia->ia_sockmask.sin_addr; 971 p.s_addr &= m.s_addr; 972 973 if (prefix.s_addr != p.s_addr || 974 mask.s_addr != m.s_addr) 975 continue; 976 } 977 978 /* 979 * If we got a matching prefix route inserted by other 980 * interface address, we are done here. 981 */ 982 if (ia->ia_flags & IFA_ROUTE) { 983 if (V_sameprefixcarponly && 984 target->ia_ifp->if_type != IFT_CARP && 985 ia->ia_ifp->if_type != IFT_CARP) 986 return (EEXIST); 987 else 988 return (0); 989 } 990 } 991 992 /* 993 * No-one seem to have this prefix route, so we try to insert it. 994 */ 995 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 996 if (!error) 997 target->ia_flags |= IFA_ROUTE; 998 return (error); 999} 1000 1001extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 1002 1003/* 1004 * If there is no other address in the system that can serve a route to the 1005 * same prefix, remove the route. Hand over the route to the new address 1006 * otherwise. 1007 */ 1008static int 1009in_scrubprefix(struct in_ifaddr *target) 1010{ 1011 INIT_VNET_NET(curvnet); 1012 INIT_VNET_INET(curvnet); 1013 struct in_ifaddr *ia; 1014 struct in_addr prefix, mask, p; 1015 int error; 1016 struct rt_addrinfo info; 1017 struct sockaddr_dl null_sdl; 1018 1019 if ((target->ia_flags & IFA_ROUTE) == 0) 1020 return (0); 1021 1022 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 1023 !(target->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 1024 bzero(&null_sdl, sizeof(null_sdl)); 1025 null_sdl.sdl_len = sizeof(null_sdl); 1026 null_sdl.sdl_family = AF_LINK; 1027 null_sdl.sdl_type = V_loif->if_type; 1028 null_sdl.sdl_index = V_loif->if_index; 1029 bzero(&info, sizeof(info)); 1030 info.rti_flags = target->ia_flags | RTF_HOST | RTF_STATIC; 1031 info.rti_info[RTAX_DST] = (struct sockaddr *)&target->ia_addr; 1032 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1033 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0); 1034 1035 if (error != 0) 1036 log(LOG_INFO, "in_scrubprefix: deletion failed\n"); 1037 } 1038 1039 if (rtinitflags(target)) 1040 prefix = target->ia_dstaddr.sin_addr; 1041 else { 1042 prefix = target->ia_addr.sin_addr; 1043 mask = target->ia_sockmask.sin_addr; 1044 prefix.s_addr &= mask.s_addr; 1045 /* remove arp cache */ 1046 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr); 1047 } 1048 1049 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1050 if (rtinitflags(ia)) 1051 p = ia->ia_dstaddr.sin_addr; 1052 else { 1053 p = ia->ia_addr.sin_addr; 1054 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1055 } 1056 1057 if (prefix.s_addr != p.s_addr) 1058 continue; 1059 1060 /* 1061 * If we got a matching prefix address, move IFA_ROUTE and 1062 * the route itself to it. Make sure that routing daemons 1063 * get a heads-up. 1064 * 1065 * XXX: a special case for carp(4) interface 1066 */ 1067 if ((ia->ia_flags & IFA_ROUTE) == 0 1068#ifdef DEV_CARP 1069 && (ia->ia_ifp->if_type != IFT_CARP) 1070#endif 1071 ) { 1072 rtinit(&(target->ia_ifa), (int)RTM_DELETE, 1073 rtinitflags(target)); 1074 target->ia_flags &= ~IFA_ROUTE; 1075 1076 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 1077 rtinitflags(ia) | RTF_UP); 1078 if (error == 0) 1079 ia->ia_flags |= IFA_ROUTE; 1080 return (error); 1081 } 1082 } 1083 1084 /* 1085 * As no-one seem to have this prefix, we can remove the route. 1086 */ 1087 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 1088 target->ia_flags &= ~IFA_ROUTE; 1089 return (0); 1090} 1091 1092#undef rtinitflags 1093 1094/* 1095 * Return 1 if the address might be a local broadcast address. 1096 */ 1097int 1098in_broadcast(struct in_addr in, struct ifnet *ifp) 1099{ 1100 register struct ifaddr *ifa; 1101 u_long t; 1102 1103 if (in.s_addr == INADDR_BROADCAST || 1104 in.s_addr == INADDR_ANY) 1105 return (1); 1106 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1107 return (0); 1108 t = ntohl(in.s_addr); 1109 /* 1110 * Look through the list of addresses for a match 1111 * with a broadcast address. 1112 */ 1113#define ia ((struct in_ifaddr *)ifa) 1114 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1115 if (ifa->ifa_addr->sa_family == AF_INET && 1116 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1117 in.s_addr == ia->ia_netbroadcast.s_addr || 1118 /* 1119 * Check for old-style (host 0) broadcast. 1120 */ 1121 t == ia->ia_subnet || t == ia->ia_net) && 1122 /* 1123 * Check for an all one subnetmask. These 1124 * only exist when an interface gets a secondary 1125 * address. 1126 */ 1127 ia->ia_subnetmask != (u_long)0xffffffff) 1128 return (1); 1129 return (0); 1130#undef ia 1131} 1132 1133/* 1134 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1135 */ 1136void 1137in_ifdetach(struct ifnet *ifp) 1138{ 1139 INIT_VNET_INET(ifp->if_vnet); 1140 1141 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1142 in_pcbpurgeif0(&V_udbinfo, ifp); 1143 in_purgemaddrs(ifp); 1144} 1145 1146/* 1147 * Delete all IPv4 multicast address records, and associated link-layer 1148 * multicast address records, associated with ifp. 1149 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1150 * XXX This should not race with ifma_protospec being set during 1151 * a new allocation, if it does, we have bigger problems. 1152 */ 1153static void 1154in_purgemaddrs(struct ifnet *ifp) 1155{ 1156 LIST_HEAD(,in_multi) purgeinms; 1157 struct in_multi *inm, *tinm; 1158 struct ifmultiaddr *ifma; 1159 1160 LIST_INIT(&purgeinms); 1161 IN_MULTI_LOCK(); 1162 1163 /* 1164 * Extract list of in_multi associated with the detaching ifp 1165 * which the PF_INET layer is about to release. 1166 * We need to do this as IF_ADDR_LOCK() may be re-acquired 1167 * by code further down. 1168 */ 1169 IF_ADDR_LOCK(ifp); 1170 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1171 if (ifma->ifma_addr->sa_family != AF_INET || 1172 ifma->ifma_protospec == NULL) 1173 continue; 1174#if 0 1175 KASSERT(ifma->ifma_protospec != NULL, 1176 ("%s: ifma_protospec is NULL", __func__)); 1177#endif 1178 inm = (struct in_multi *)ifma->ifma_protospec; 1179 LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1180 } 1181 IF_ADDR_UNLOCK(ifp); 1182 1183 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1184 LIST_REMOVE(inm, inm_link); 1185 inm_release_locked(inm); 1186 } 1187 igmp_ifdetach(ifp); 1188 1189 IN_MULTI_UNLOCK(); 1190} 1191 1192#include <net/if_dl.h> 1193#include <netinet/if_ether.h> 1194 1195struct in_llentry { 1196 struct llentry base; 1197 struct sockaddr_in l3_addr4; 1198}; 1199 1200static struct llentry * 1201in_lltable_new(const struct sockaddr *l3addr, u_int flags) 1202{ 1203 struct in_llentry *lle; 1204 1205 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO); 1206 if (lle == NULL) /* NB: caller generates msg */ 1207 return NULL; 1208 1209 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 1210 /* 1211 * For IPv4 this will trigger "arpresolve" to generate 1212 * an ARP request. 1213 */ 1214 lle->base.la_expire = time_second; /* mark expired */ 1215 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 1216 lle->base.lle_refcnt = 1; 1217 LLE_LOCK_INIT(&lle->base); 1218 return &lle->base; 1219} 1220 1221/* 1222 * Deletes an address from the address table. 1223 * This function is called by the timer functions 1224 * such as arptimer() and nd6_llinfo_timer(), and 1225 * the caller does the locking. 1226 */ 1227static void 1228in_lltable_free(struct lltable *llt, struct llentry *lle) 1229{ 1230 LLE_WUNLOCK(lle); 1231 LLE_LOCK_DESTROY(lle); 1232 free(lle, M_LLTABLE); 1233} 1234 1235static int 1236in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 1237{ 1238 struct rtentry *rt; 1239 1240 KASSERT(l3addr->sa_family == AF_INET, 1241 ("sin_family %d", l3addr->sa_family)); 1242 1243 /* XXX rtalloc1 should take a const param */ 1244 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 1245 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 1246 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1247 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1248 if (rt != NULL) 1249 RTFREE_LOCKED(rt); 1250 return (EINVAL); 1251 } 1252 RTFREE_LOCKED(rt); 1253 return 0; 1254} 1255 1256/* 1257 * Return NULL if not found or marked for deletion. 1258 * If found return lle read locked. 1259 */ 1260static struct llentry * 1261in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1262{ 1263 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1264 struct ifnet *ifp = llt->llt_ifp; 1265 struct llentry *lle; 1266 struct llentries *lleh; 1267 u_int hashkey; 1268 1269 IF_AFDATA_LOCK_ASSERT(ifp); 1270 KASSERT(l3addr->sa_family == AF_INET, 1271 ("sin_family %d", l3addr->sa_family)); 1272 1273 hashkey = sin->sin_addr.s_addr; 1274 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1275 LIST_FOREACH(lle, lleh, lle_next) { 1276 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle); 1277 if (lle->la_flags & LLE_DELETED) 1278 continue; 1279 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 1280 break; 1281 } 1282 if (lle == NULL) { 1283#ifdef DIAGNOSTICS 1284 if (flags & LLE_DELETE) 1285 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1286#endif 1287 if (!(flags & LLE_CREATE)) 1288 return (NULL); 1289 /* 1290 * A route that covers the given address must have 1291 * been installed 1st because we are doing a resolution, 1292 * verify this. 1293 */ 1294 if (!(flags & LLE_IFADDR) && 1295 in_lltable_rtcheck(ifp, l3addr) != 0) 1296 goto done; 1297 1298 lle = in_lltable_new(l3addr, flags); 1299 if (lle == NULL) { 1300 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1301 goto done; 1302 } 1303 lle->la_flags = flags & ~LLE_CREATE; 1304 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1305 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1306 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1307 } 1308 1309 lle->lle_tbl = llt; 1310 lle->lle_head = lleh; 1311 LIST_INSERT_HEAD(lleh, lle, lle_next); 1312 } else if (flags & LLE_DELETE) { 1313 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1314 LLE_WLOCK(lle); 1315 lle->la_flags = LLE_DELETED; 1316 LLE_WUNLOCK(lle); 1317#ifdef DIAGNOSTICS 1318 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1319#endif 1320 } 1321 lle = (void *)-1; 1322 1323 } 1324 if (LLE_IS_VALID(lle)) { 1325 if (flags & LLE_EXCLUSIVE) 1326 LLE_WLOCK(lle); 1327 else 1328 LLE_RLOCK(lle); 1329 } 1330done: 1331 return (lle); 1332} 1333 1334static int 1335in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1336{ 1337#define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1338 struct ifnet *ifp = llt->llt_ifp; 1339 struct llentry *lle; 1340 /* XXX stack use */ 1341 struct { 1342 struct rt_msghdr rtm; 1343 struct sockaddr_inarp sin; 1344 struct sockaddr_dl sdl; 1345 } arpc; 1346 int error, i; 1347 1348 /* XXXXX 1349 * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 1350 * so it is okay to use this ASSERT, change it when 1351 * IFNET lock is finalized 1352 */ 1353 IFNET_WLOCK_ASSERT(); 1354 1355 error = 0; 1356 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1357 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1358 struct sockaddr_dl *sdl; 1359 1360 /* skip deleted entries */ 1361 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 1362 continue; 1363 /* Skip if jailed and not a valid IP of the prison. */ 1364 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1365 continue; 1366 /* 1367 * produce a msg made of: 1368 * struct rt_msghdr; 1369 * struct sockaddr_inarp; (IPv4) 1370 * struct sockaddr_dl; 1371 */ 1372 bzero(&arpc, sizeof(arpc)); 1373 arpc.rtm.rtm_msglen = sizeof(arpc); 1374 arpc.rtm.rtm_version = RTM_VERSION; 1375 arpc.rtm.rtm_type = RTM_GET; 1376 arpc.rtm.rtm_flags = RTF_UP; 1377 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1378 arpc.sin.sin_family = AF_INET; 1379 arpc.sin.sin_len = sizeof(arpc.sin); 1380 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1381 1382 /* publish */ 1383 if (lle->la_flags & LLE_PUB) { 1384 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1385 /* proxy only */ 1386 if (lle->la_flags & LLE_PROXY) 1387 arpc.sin.sin_other = SIN_PROXY; 1388 } 1389 1390 sdl = &arpc.sdl; 1391 sdl->sdl_family = AF_LINK; 1392 sdl->sdl_len = sizeof(*sdl); 1393 sdl->sdl_alen = ifp->if_addrlen; 1394 sdl->sdl_index = ifp->if_index; 1395 sdl->sdl_type = ifp->if_type; 1396 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1397 1398 arpc.rtm.rtm_rmx.rmx_expire = 1399 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1400 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1401 if (lle->la_flags & LLE_STATIC) 1402 arpc.rtm.rtm_flags |= RTF_STATIC; 1403 arpc.rtm.rtm_index = ifp->if_index; 1404 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1405 if (error) 1406 break; 1407 } 1408 } 1409 return error; 1410#undef SIN 1411} 1412 1413void * 1414in_domifattach(struct ifnet *ifp) 1415{ 1416 struct in_ifinfo *ii; 1417 struct lltable *llt; 1418 1419 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1420 1421 llt = lltable_init(ifp, AF_INET); 1422 if (llt != NULL) { 1423 llt->llt_new = in_lltable_new; 1424 llt->llt_free = in_lltable_free; 1425 llt->llt_rtcheck = in_lltable_rtcheck; 1426 llt->llt_lookup = in_lltable_lookup; 1427 llt->llt_dump = in_lltable_dump; 1428 } 1429 ii->ii_llt = llt; 1430 1431 ii->ii_igmp = igmp_domifattach(ifp); 1432 1433 return ii; 1434} 1435 1436void 1437in_domifdetach(struct ifnet *ifp, void *aux) 1438{ 1439 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1440 1441 igmp_domifdetach(ifp); 1442 lltable_free(ii->ii_llt); 1443 free(ii, M_IFADDR); 1444} 1445