in.c revision 194602
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 194602 2009-06-21 19:30:33Z rwatson $"); 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_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 385 ia->ia_sockmask.sin_len = 8; 386 ia->ia_sockmask.sin_family = AF_INET; 387 if (ifp->if_flags & IFF_BROADCAST) { 388 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 389 ia->ia_broadaddr.sin_family = AF_INET; 390 } 391 ia->ia_ifp = ifp; 392 393 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 394 s = splnet(); 395 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 396 splx(s); 397 iaIsNew = 1; 398 } 399 break; 400 401 case SIOCSIFBRDADDR: 402 case SIOCGIFADDR: 403 case SIOCGIFNETMASK: 404 case SIOCGIFDSTADDR: 405 case SIOCGIFBRDADDR: 406 if (ia == NULL) { 407 error = EADDRNOTAVAIL; 408 goto out_unlock; 409 } 410 break; 411 } 412 413 /* 414 * Most paths in this switch return directly or via out_unlock. Only 415 * paths that remove the address break in order to hit common removal 416 * code. 417 * 418 * XXXRW: We enter the switch with IF_ADDR_LOCK() held, but leave 419 * without it. This is a bug. 420 */ 421 IF_ADDR_LOCK_ASSERT(ifp); 422 switch (cmd) { 423 case SIOCGIFADDR: 424 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 425 goto out_unlock; 426 427 case SIOCGIFBRDADDR: 428 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 429 error = EINVAL; 430 goto out_unlock; 431 } 432 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 433 goto out_unlock; 434 435 case SIOCGIFDSTADDR: 436 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 437 error = EINVAL; 438 goto out_unlock; 439 } 440 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 441 goto out_unlock; 442 443 case SIOCGIFNETMASK: 444 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 445 goto out_unlock; 446 447 case SIOCSIFDSTADDR: 448 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 449 error = EINVAL; 450 goto out_unlock; 451 } 452 oldaddr = ia->ia_dstaddr; 453 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 454 IF_ADDR_UNLOCK(ifp); 455 456 /* 457 * XXXRW: Locks dropped for if_ioctl and rtinit, but ia is 458 * still being used. 459 */ 460 if (ifp->if_ioctl != NULL) { 461 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 462 (caddr_t)ia); 463 if (error) { 464 ia->ia_dstaddr = oldaddr; 465 return (error); 466 } 467 } 468 if (ia->ia_flags & IFA_ROUTE) { 469 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 470 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 471 ia->ia_ifa.ifa_dstaddr = 472 (struct sockaddr *)&ia->ia_dstaddr; 473 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 474 } 475 return (0); 476 477 case SIOCSIFBRDADDR: 478 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 479 error = EINVAL; 480 goto out_unlock; 481 } 482 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 483 goto out_unlock; 484 485 case SIOCSIFADDR: 486 IF_ADDR_UNLOCK(ifp); 487 488 /* 489 * XXXRW: Locks dropped for in_ifinit and in_joingroup, but ia 490 * is still being used. 491 */ 492 error = in_ifinit(ifp, ia, 493 (struct sockaddr_in *) &ifr->ifr_addr, 1); 494 if (error != 0 && iaIsNew) 495 break; 496 if (error == 0) { 497 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 498 if (iaIsFirst && 499 (ifp->if_flags & IFF_MULTICAST) != 0) { 500 error = in_joingroup(ifp, &allhosts_addr, 501 NULL, &ii->ii_allhosts); 502 } 503 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 504 } 505 return (0); 506 507 case SIOCSIFNETMASK: 508 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 509 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 510 goto out_unlock; 511 512 case SIOCAIFADDR: 513 maskIsNew = 0; 514 hostIsNew = 1; 515 error = 0; 516 if (ia->ia_addr.sin_family == AF_INET) { 517 if (ifra->ifra_addr.sin_len == 0) { 518 ifra->ifra_addr = ia->ia_addr; 519 hostIsNew = 0; 520 } else if (ifra->ifra_addr.sin_addr.s_addr == 521 ia->ia_addr.sin_addr.s_addr) 522 hostIsNew = 0; 523 } 524 IF_ADDR_UNLOCK(ifp); 525 526 /* 527 * XXXRW: Locks dropped for in_ifscrub and in_ifinit, but ia 528 * is still being used. 529 */ 530 if (ifra->ifra_mask.sin_len) { 531 in_ifscrub(ifp, ia); 532 ia->ia_sockmask = ifra->ifra_mask; 533 ia->ia_sockmask.sin_family = AF_INET; 534 ia->ia_subnetmask = 535 ntohl(ia->ia_sockmask.sin_addr.s_addr); 536 maskIsNew = 1; 537 } 538 if ((ifp->if_flags & IFF_POINTOPOINT) && 539 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 540 in_ifscrub(ifp, ia); 541 ia->ia_dstaddr = ifra->ifra_dstaddr; 542 maskIsNew = 1; /* We lie; but the effect's the same */ 543 } 544 if (ifra->ifra_addr.sin_family == AF_INET && 545 (hostIsNew || maskIsNew)) 546 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 547 if (error != 0 && iaIsNew) 548 break; 549 550 if ((ifp->if_flags & IFF_BROADCAST) && 551 (ifra->ifra_broadaddr.sin_family == AF_INET)) 552 ia->ia_broadaddr = ifra->ifra_broadaddr; 553 if (error == 0) { 554 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 555 if (iaIsFirst && 556 (ifp->if_flags & IFF_MULTICAST) != 0) { 557 error = in_joingroup(ifp, &allhosts_addr, 558 NULL, &ii->ii_allhosts); 559 } 560 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 561 } 562 return (error); 563 564 case SIOCDIFADDR: 565 IF_ADDR_UNLOCK(ifp); 566 567 /* 568 * XXXRW: Locks dropped for in_ifscrub and in_ifadown, but ia 569 * is still being used. 570 * 571 * in_ifscrub kills the interface route. 572 */ 573 in_ifscrub(ifp, ia); 574 575 /* 576 * in_ifadown gets rid of all the rest of 577 * the routes. This is not quite the right 578 * thing to do, but at least if we are running 579 * a routing process they will come back. 580 */ 581 in_ifadown(&ia->ia_ifa, 1); 582 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 583 error = 0; 584 break; 585 586 default: 587 panic("in_control: unsupported ioctl"); 588 } 589 590 /* 591 * XXXRW: In a more ideal world, we would still be holding 592 * IF_ADDR_LOCK here. 593 */ 594 IF_ADDR_LOCK(ifp); 595 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 596 IF_ADDR_UNLOCK(ifp); 597 s = splnet(); 598 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 599 if (ia->ia_addr.sin_family == AF_INET) { 600 LIST_REMOVE(ia, ia_hash); 601 /* 602 * If this is the last IPv4 address configured on this 603 * interface, leave the all-hosts group. 604 * No state-change report need be transmitted. 605 */ 606 oia = NULL; 607 IFP_TO_IA(ifp, oia); 608 if (oia == NULL) { 609 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 610 IN_MULTI_LOCK(); 611 if (ii->ii_allhosts) { 612 (void)in_leavegroup_locked(ii->ii_allhosts, 613 NULL); 614 ii->ii_allhosts = NULL; 615 } 616 IN_MULTI_UNLOCK(); 617 } 618 } 619 ifa_free(&ia->ia_ifa); 620 splx(s); 621 622 return (error); 623 624out_unlock: 625 IF_ADDR_UNLOCK(ifp); 626 return (error); 627} 628 629/* 630 * SIOC[GAD]LIFADDR. 631 * SIOCGLIFADDR: get first address. (?!?) 632 * SIOCGLIFADDR with IFLR_PREFIX: 633 * get first address that matches the specified prefix. 634 * SIOCALIFADDR: add the specified address. 635 * SIOCALIFADDR with IFLR_PREFIX: 636 * EINVAL since we can't deduce hostid part of the address. 637 * SIOCDLIFADDR: delete the specified address. 638 * SIOCDLIFADDR with IFLR_PREFIX: 639 * delete the first address that matches the specified prefix. 640 * return values: 641 * EINVAL on invalid parameters 642 * EADDRNOTAVAIL on prefix match failed/specified address not found 643 * other values may be returned from in_ioctl() 644 */ 645static int 646in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 647 struct ifnet *ifp, struct thread *td) 648{ 649 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 650 struct ifaddr *ifa; 651 652 /* sanity checks */ 653 if (data == NULL || ifp == NULL) { 654 panic("invalid argument to in_lifaddr_ioctl"); 655 /*NOTRECHED*/ 656 } 657 658 switch (cmd) { 659 case SIOCGLIFADDR: 660 /* address must be specified on GET with IFLR_PREFIX */ 661 if ((iflr->flags & IFLR_PREFIX) == 0) 662 break; 663 /*FALLTHROUGH*/ 664 case SIOCALIFADDR: 665 case SIOCDLIFADDR: 666 /* address must be specified on ADD and DELETE */ 667 if (iflr->addr.ss_family != AF_INET) 668 return (EINVAL); 669 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 670 return (EINVAL); 671 /* XXX need improvement */ 672 if (iflr->dstaddr.ss_family 673 && iflr->dstaddr.ss_family != AF_INET) 674 return (EINVAL); 675 if (iflr->dstaddr.ss_family 676 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 677 return (EINVAL); 678 break; 679 default: /*shouldn't happen*/ 680 return (EOPNOTSUPP); 681 } 682 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 683 return (EINVAL); 684 685 switch (cmd) { 686 case SIOCALIFADDR: 687 { 688 struct in_aliasreq ifra; 689 690 if (iflr->flags & IFLR_PREFIX) 691 return (EINVAL); 692 693 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 694 bzero(&ifra, sizeof(ifra)); 695 bcopy(iflr->iflr_name, ifra.ifra_name, 696 sizeof(ifra.ifra_name)); 697 698 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 699 700 if (iflr->dstaddr.ss_family) { /*XXX*/ 701 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 702 iflr->dstaddr.ss_len); 703 } 704 705 ifra.ifra_mask.sin_family = AF_INET; 706 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 707 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 708 709 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 710 } 711 case SIOCGLIFADDR: 712 case SIOCDLIFADDR: 713 { 714 struct in_ifaddr *ia; 715 struct in_addr mask, candidate, match; 716 struct sockaddr_in *sin; 717 718 bzero(&mask, sizeof(mask)); 719 bzero(&match, sizeof(match)); 720 if (iflr->flags & IFLR_PREFIX) { 721 /* lookup a prefix rather than address. */ 722 in_len2mask(&mask, iflr->prefixlen); 723 724 sin = (struct sockaddr_in *)&iflr->addr; 725 match.s_addr = sin->sin_addr.s_addr; 726 match.s_addr &= mask.s_addr; 727 728 /* if you set extra bits, that's wrong */ 729 if (match.s_addr != sin->sin_addr.s_addr) 730 return (EINVAL); 731 732 } else { 733 /* on getting an address, take the 1st match */ 734 /* on deleting an address, do exact match */ 735 if (cmd != SIOCGLIFADDR) { 736 in_len2mask(&mask, 32); 737 sin = (struct sockaddr_in *)&iflr->addr; 738 match.s_addr = sin->sin_addr.s_addr; 739 } 740 } 741 742 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 743 if (ifa->ifa_addr->sa_family != AF_INET6) 744 continue; 745 if (match.s_addr == 0) 746 break; 747 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 748 candidate.s_addr &= mask.s_addr; 749 if (candidate.s_addr == match.s_addr) 750 break; 751 } 752 if (ifa == NULL) 753 return (EADDRNOTAVAIL); 754 ia = (struct in_ifaddr *)ifa; 755 756 if (cmd == SIOCGLIFADDR) { 757 /* fill in the if_laddrreq structure */ 758 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 759 760 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 761 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 762 ia->ia_dstaddr.sin_len); 763 } else 764 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 765 766 iflr->prefixlen = 767 in_mask2len(&ia->ia_sockmask.sin_addr); 768 769 iflr->flags = 0; /*XXX*/ 770 771 return (0); 772 } else { 773 struct in_aliasreq ifra; 774 775 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 776 bzero(&ifra, sizeof(ifra)); 777 bcopy(iflr->iflr_name, ifra.ifra_name, 778 sizeof(ifra.ifra_name)); 779 780 bcopy(&ia->ia_addr, &ifra.ifra_addr, 781 ia->ia_addr.sin_len); 782 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 783 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 784 ia->ia_dstaddr.sin_len); 785 } 786 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 787 ia->ia_sockmask.sin_len); 788 789 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 790 ifp, td)); 791 } 792 } 793 } 794 795 return (EOPNOTSUPP); /*just for safety*/ 796} 797 798/* 799 * Delete any existing route for an interface. 800 */ 801void 802in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 803{ 804 805 in_scrubprefix(ia); 806} 807 808/* 809 * Initialize an interface's internet address 810 * and routing table entry. 811 */ 812static int 813in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 814 int scrub) 815{ 816 INIT_VNET_NET(ifp->if_vnet); 817 INIT_VNET_INET(ifp->if_vnet); 818 register u_long i = ntohl(sin->sin_addr.s_addr); 819 struct sockaddr_in oldaddr; 820 struct rtentry *rt = NULL; 821 struct rt_addrinfo info; 822 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 823 int s = splimp(), flags = RTF_UP, error = 0; 824 825 oldaddr = ia->ia_addr; 826 if (oldaddr.sin_family == AF_INET) 827 LIST_REMOVE(ia, ia_hash); 828 ia->ia_addr = *sin; 829 if (ia->ia_addr.sin_family == AF_INET) 830 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 831 ia, ia_hash); 832 /* 833 * Give the interface a chance to initialize 834 * if this is its first address, 835 * and to validate the address if necessary. 836 */ 837 if (ifp->if_ioctl != NULL) { 838 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 839 if (error) { 840 splx(s); 841 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 842 ia->ia_addr = oldaddr; 843 if (ia->ia_addr.sin_family == AF_INET) 844 LIST_INSERT_HEAD(INADDR_HASH( 845 ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 846 else 847 /* 848 * If oldaddr family is not AF_INET (e.g. 849 * interface has been just created) in_control 850 * does not call LIST_REMOVE, and we end up 851 * with bogus ia entries in hash 852 */ 853 LIST_REMOVE(ia, ia_hash); 854 return (error); 855 } 856 } 857 splx(s); 858 if (scrub) { 859 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 860 in_ifscrub(ifp, ia); 861 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 862 } 863 if (IN_CLASSA(i)) 864 ia->ia_netmask = IN_CLASSA_NET; 865 else if (IN_CLASSB(i)) 866 ia->ia_netmask = IN_CLASSB_NET; 867 else 868 ia->ia_netmask = IN_CLASSC_NET; 869 /* 870 * The subnet mask usually includes at least the standard network part, 871 * but may may be smaller in the case of supernetting. 872 * If it is set, we believe it. 873 */ 874 if (ia->ia_subnetmask == 0) { 875 ia->ia_subnetmask = ia->ia_netmask; 876 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 877 } else 878 ia->ia_netmask &= ia->ia_subnetmask; 879 ia->ia_net = i & ia->ia_netmask; 880 ia->ia_subnet = i & ia->ia_subnetmask; 881 in_socktrim(&ia->ia_sockmask); 882#ifdef DEV_CARP 883 /* 884 * XXX: carp(4) does not have interface route 885 */ 886 if (ifp->if_type == IFT_CARP) 887 return (0); 888#endif 889 /* 890 * Add route for the network. 891 */ 892 ia->ia_ifa.ifa_metric = ifp->if_metric; 893 if (ifp->if_flags & IFF_BROADCAST) { 894 ia->ia_broadaddr.sin_addr.s_addr = 895 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 896 ia->ia_netbroadcast.s_addr = 897 htonl(ia->ia_net | ~ ia->ia_netmask); 898 } else if (ifp->if_flags & IFF_LOOPBACK) { 899 ia->ia_dstaddr = ia->ia_addr; 900 flags |= RTF_HOST; 901 } else if (ifp->if_flags & IFF_POINTOPOINT) { 902 if (ia->ia_dstaddr.sin_family != AF_INET) 903 return (0); 904 flags |= RTF_HOST; 905 } 906 if ((error = in_addprefix(ia, flags)) != 0) 907 return (error); 908 909 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 910 return (0); 911 912 /* 913 * add a loopback route to self 914 */ 915 if (!(ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 916 bzero(&info, sizeof(info)); 917 info.rti_ifp = V_loif; 918 info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC; 919 info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr; 920 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 921 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); 922 923 if (error == 0 && rt != NULL) { 924 RT_LOCK(rt); 925 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 926 rt->rt_ifp->if_type; 927 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 928 rt->rt_ifp->if_index; 929 RT_REMREF(rt); 930 RT_UNLOCK(rt); 931 } else if (error != 0) 932 log(LOG_INFO, "in_ifinit: insertion failed\n"); 933 } 934 935 return (error); 936} 937 938#define rtinitflags(x) \ 939 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 940 ? RTF_HOST : 0) 941/* 942 * Check if we have a route for the given prefix already or add one accordingly. 943 */ 944static int 945in_addprefix(struct in_ifaddr *target, int flags) 946{ 947 INIT_VNET_INET(curvnet); 948 struct in_ifaddr *ia; 949 struct in_addr prefix, mask, p, m; 950 int error; 951 952 if ((flags & RTF_HOST) != 0) { 953 prefix = target->ia_dstaddr.sin_addr; 954 mask.s_addr = 0; 955 } else { 956 prefix = target->ia_addr.sin_addr; 957 mask = target->ia_sockmask.sin_addr; 958 prefix.s_addr &= mask.s_addr; 959 } 960 961 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 962 if (rtinitflags(ia)) { 963 p = ia->ia_addr.sin_addr; 964 965 if (prefix.s_addr != p.s_addr) 966 continue; 967 } else { 968 p = ia->ia_addr.sin_addr; 969 m = ia->ia_sockmask.sin_addr; 970 p.s_addr &= m.s_addr; 971 972 if (prefix.s_addr != p.s_addr || 973 mask.s_addr != m.s_addr) 974 continue; 975 } 976 977 /* 978 * If we got a matching prefix route inserted by other 979 * interface address, we are done here. 980 */ 981 if (ia->ia_flags & IFA_ROUTE) { 982 if (V_sameprefixcarponly && 983 target->ia_ifp->if_type != IFT_CARP && 984 ia->ia_ifp->if_type != IFT_CARP) 985 return (EEXIST); 986 else 987 return (0); 988 } 989 } 990 991 /* 992 * No-one seem to have this prefix route, so we try to insert it. 993 */ 994 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 995 if (!error) 996 target->ia_flags |= IFA_ROUTE; 997 return (error); 998} 999 1000extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 1001 1002/* 1003 * If there is no other address in the system that can serve a route to the 1004 * same prefix, remove the route. Hand over the route to the new address 1005 * otherwise. 1006 */ 1007static int 1008in_scrubprefix(struct in_ifaddr *target) 1009{ 1010 INIT_VNET_NET(curvnet); 1011 INIT_VNET_INET(curvnet); 1012 struct in_ifaddr *ia; 1013 struct in_addr prefix, mask, p; 1014 int error; 1015 struct sockaddr_in prefix0, mask0; 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 * remove all L2 entries on the given prefix 1086 */ 1087 bzero(&prefix0, sizeof(prefix0)); 1088 prefix0.sin_len = sizeof(prefix0); 1089 prefix0.sin_family = AF_INET; 1090 prefix0.sin_addr.s_addr = target->ia_subnet; 1091 bzero(&mask0, sizeof(mask0)); 1092 mask0.sin_len = sizeof(mask0); 1093 mask0.sin_family = AF_INET; 1094 mask0.sin_addr.s_addr = target->ia_subnetmask; 1095 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 1096 (struct sockaddr *)&mask0); 1097 1098 /* 1099 * As no-one seem to have this prefix, we can remove the route. 1100 */ 1101 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 1102 target->ia_flags &= ~IFA_ROUTE; 1103 return (0); 1104} 1105 1106#undef rtinitflags 1107 1108/* 1109 * Return 1 if the address might be a local broadcast address. 1110 */ 1111int 1112in_broadcast(struct in_addr in, struct ifnet *ifp) 1113{ 1114 register struct ifaddr *ifa; 1115 u_long t; 1116 1117 if (in.s_addr == INADDR_BROADCAST || 1118 in.s_addr == INADDR_ANY) 1119 return (1); 1120 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1121 return (0); 1122 t = ntohl(in.s_addr); 1123 /* 1124 * Look through the list of addresses for a match 1125 * with a broadcast address. 1126 */ 1127#define ia ((struct in_ifaddr *)ifa) 1128 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1129 if (ifa->ifa_addr->sa_family == AF_INET && 1130 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1131 in.s_addr == ia->ia_netbroadcast.s_addr || 1132 /* 1133 * Check for old-style (host 0) broadcast. 1134 */ 1135 t == ia->ia_subnet || t == ia->ia_net) && 1136 /* 1137 * Check for an all one subnetmask. These 1138 * only exist when an interface gets a secondary 1139 * address. 1140 */ 1141 ia->ia_subnetmask != (u_long)0xffffffff) 1142 return (1); 1143 return (0); 1144#undef ia 1145} 1146 1147/* 1148 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1149 */ 1150void 1151in_ifdetach(struct ifnet *ifp) 1152{ 1153 INIT_VNET_INET(ifp->if_vnet); 1154 1155 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1156 in_pcbpurgeif0(&V_udbinfo, ifp); 1157 in_purgemaddrs(ifp); 1158} 1159 1160/* 1161 * Delete all IPv4 multicast address records, and associated link-layer 1162 * multicast address records, associated with ifp. 1163 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1164 * XXX This should not race with ifma_protospec being set during 1165 * a new allocation, if it does, we have bigger problems. 1166 */ 1167static void 1168in_purgemaddrs(struct ifnet *ifp) 1169{ 1170 LIST_HEAD(,in_multi) purgeinms; 1171 struct in_multi *inm, *tinm; 1172 struct ifmultiaddr *ifma; 1173 1174 LIST_INIT(&purgeinms); 1175 IN_MULTI_LOCK(); 1176 1177 /* 1178 * Extract list of in_multi associated with the detaching ifp 1179 * which the PF_INET layer is about to release. 1180 * We need to do this as IF_ADDR_LOCK() may be re-acquired 1181 * by code further down. 1182 */ 1183 IF_ADDR_LOCK(ifp); 1184 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1185 if (ifma->ifma_addr->sa_family != AF_INET || 1186 ifma->ifma_protospec == NULL) 1187 continue; 1188#if 0 1189 KASSERT(ifma->ifma_protospec != NULL, 1190 ("%s: ifma_protospec is NULL", __func__)); 1191#endif 1192 inm = (struct in_multi *)ifma->ifma_protospec; 1193 LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1194 } 1195 IF_ADDR_UNLOCK(ifp); 1196 1197 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1198 LIST_REMOVE(inm, inm_link); 1199 inm_release_locked(inm); 1200 } 1201 igmp_ifdetach(ifp); 1202 1203 IN_MULTI_UNLOCK(); 1204} 1205 1206#include <net/if_dl.h> 1207#include <netinet/if_ether.h> 1208 1209struct in_llentry { 1210 struct llentry base; 1211 struct sockaddr_in l3_addr4; 1212}; 1213 1214static struct llentry * 1215in_lltable_new(const struct sockaddr *l3addr, u_int flags) 1216{ 1217 struct in_llentry *lle; 1218 1219 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO); 1220 if (lle == NULL) /* NB: caller generates msg */ 1221 return NULL; 1222 1223 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 1224 /* 1225 * For IPv4 this will trigger "arpresolve" to generate 1226 * an ARP request. 1227 */ 1228 lle->base.la_expire = time_second; /* mark expired */ 1229 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 1230 lle->base.lle_refcnt = 1; 1231 LLE_LOCK_INIT(&lle->base); 1232 return &lle->base; 1233} 1234 1235/* 1236 * Deletes an address from the address table. 1237 * This function is called by the timer functions 1238 * such as arptimer() and nd6_llinfo_timer(), and 1239 * the caller does the locking. 1240 */ 1241static void 1242in_lltable_free(struct lltable *llt, struct llentry *lle) 1243{ 1244 LLE_WUNLOCK(lle); 1245 LLE_LOCK_DESTROY(lle); 1246 free(lle, M_LLTABLE); 1247} 1248 1249 1250#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 1251 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 1252 1253static void 1254in_lltable_prefix_free(struct lltable *llt, 1255 const struct sockaddr *prefix, 1256 const struct sockaddr *mask) 1257{ 1258 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1259 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1260 struct llentry *lle, *next; 1261 register int i; 1262 1263 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 1264 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 1265 1266 if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle), 1267 pfx, msk)) { 1268 callout_drain(&lle->la_timer); 1269 LLE_WLOCK(lle); 1270 llentry_free(lle); 1271 } 1272 } 1273 } 1274} 1275 1276 1277static int 1278in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 1279{ 1280 struct rtentry *rt; 1281 1282 KASSERT(l3addr->sa_family == AF_INET, 1283 ("sin_family %d", l3addr->sa_family)); 1284 1285 /* XXX rtalloc1 should take a const param */ 1286 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 1287 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 1288 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1289 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1290 if (rt != NULL) 1291 RTFREE_LOCKED(rt); 1292 return (EINVAL); 1293 } 1294 RTFREE_LOCKED(rt); 1295 return 0; 1296} 1297 1298/* 1299 * Return NULL if not found or marked for deletion. 1300 * If found return lle read locked. 1301 */ 1302static struct llentry * 1303in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1304{ 1305 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1306 struct ifnet *ifp = llt->llt_ifp; 1307 struct llentry *lle; 1308 struct llentries *lleh; 1309 u_int hashkey; 1310 1311 IF_AFDATA_LOCK_ASSERT(ifp); 1312 KASSERT(l3addr->sa_family == AF_INET, 1313 ("sin_family %d", l3addr->sa_family)); 1314 1315 hashkey = sin->sin_addr.s_addr; 1316 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1317 LIST_FOREACH(lle, lleh, lle_next) { 1318 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle); 1319 if (lle->la_flags & LLE_DELETED) 1320 continue; 1321 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 1322 break; 1323 } 1324 if (lle == NULL) { 1325#ifdef DIAGNOSTICS 1326 if (flags & LLE_DELETE) 1327 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1328#endif 1329 if (!(flags & LLE_CREATE)) 1330 return (NULL); 1331 /* 1332 * A route that covers the given address must have 1333 * been installed 1st because we are doing a resolution, 1334 * verify this. 1335 */ 1336 if (!(flags & LLE_IFADDR) && 1337 in_lltable_rtcheck(ifp, l3addr) != 0) 1338 goto done; 1339 1340 lle = in_lltable_new(l3addr, flags); 1341 if (lle == NULL) { 1342 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1343 goto done; 1344 } 1345 lle->la_flags = flags & ~LLE_CREATE; 1346 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1347 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1348 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1349 } 1350 1351 lle->lle_tbl = llt; 1352 lle->lle_head = lleh; 1353 LIST_INSERT_HEAD(lleh, lle, lle_next); 1354 } else if (flags & LLE_DELETE) { 1355 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1356 LLE_WLOCK(lle); 1357 lle->la_flags = LLE_DELETED; 1358 LLE_WUNLOCK(lle); 1359#ifdef DIAGNOSTICS 1360 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1361#endif 1362 } 1363 lle = (void *)-1; 1364 1365 } 1366 if (LLE_IS_VALID(lle)) { 1367 if (flags & LLE_EXCLUSIVE) 1368 LLE_WLOCK(lle); 1369 else 1370 LLE_RLOCK(lle); 1371 } 1372done: 1373 return (lle); 1374} 1375 1376static int 1377in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1378{ 1379#define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1380 struct ifnet *ifp = llt->llt_ifp; 1381 struct llentry *lle; 1382 /* XXX stack use */ 1383 struct { 1384 struct rt_msghdr rtm; 1385 struct sockaddr_inarp sin; 1386 struct sockaddr_dl sdl; 1387 } arpc; 1388 int error, i; 1389 1390 /* XXXXX 1391 * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 1392 * so it is okay to use this ASSERT, change it when 1393 * IFNET lock is finalized 1394 */ 1395 IFNET_WLOCK_ASSERT(); 1396 1397 error = 0; 1398 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1399 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1400 struct sockaddr_dl *sdl; 1401 1402 /* skip deleted entries */ 1403 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 1404 continue; 1405 /* Skip if jailed and not a valid IP of the prison. */ 1406 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1407 continue; 1408 /* 1409 * produce a msg made of: 1410 * struct rt_msghdr; 1411 * struct sockaddr_inarp; (IPv4) 1412 * struct sockaddr_dl; 1413 */ 1414 bzero(&arpc, sizeof(arpc)); 1415 arpc.rtm.rtm_msglen = sizeof(arpc); 1416 arpc.rtm.rtm_version = RTM_VERSION; 1417 arpc.rtm.rtm_type = RTM_GET; 1418 arpc.rtm.rtm_flags = RTF_UP; 1419 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1420 arpc.sin.sin_family = AF_INET; 1421 arpc.sin.sin_len = sizeof(arpc.sin); 1422 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1423 1424 /* publish */ 1425 if (lle->la_flags & LLE_PUB) { 1426 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1427 /* proxy only */ 1428 if (lle->la_flags & LLE_PROXY) 1429 arpc.sin.sin_other = SIN_PROXY; 1430 } 1431 1432 sdl = &arpc.sdl; 1433 sdl->sdl_family = AF_LINK; 1434 sdl->sdl_len = sizeof(*sdl); 1435 sdl->sdl_alen = ifp->if_addrlen; 1436 sdl->sdl_index = ifp->if_index; 1437 sdl->sdl_type = ifp->if_type; 1438 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1439 1440 arpc.rtm.rtm_rmx.rmx_expire = 1441 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1442 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1443 if (lle->la_flags & LLE_STATIC) 1444 arpc.rtm.rtm_flags |= RTF_STATIC; 1445 arpc.rtm.rtm_index = ifp->if_index; 1446 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1447 if (error) 1448 break; 1449 } 1450 } 1451 return error; 1452#undef SIN 1453} 1454 1455void * 1456in_domifattach(struct ifnet *ifp) 1457{ 1458 struct in_ifinfo *ii; 1459 struct lltable *llt; 1460 1461 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1462 1463 llt = lltable_init(ifp, AF_INET); 1464 if (llt != NULL) { 1465 llt->llt_new = in_lltable_new; 1466 llt->llt_free = in_lltable_free; 1467 llt->llt_prefix_free = in_lltable_prefix_free; 1468 llt->llt_rtcheck = in_lltable_rtcheck; 1469 llt->llt_lookup = in_lltable_lookup; 1470 llt->llt_dump = in_lltable_dump; 1471 } 1472 ii->ii_llt = llt; 1473 1474 ii->ii_igmp = igmp_domifattach(ifp); 1475 1476 return ii; 1477} 1478 1479void 1480in_domifdetach(struct ifnet *ifp, void *aux) 1481{ 1482 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1483 1484 igmp_domifdetach(ifp); 1485 lltable_free(ii->ii_llt); 1486 free(ii, M_IFADDR); 1487} 1488