in.c revision 227958
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 227958 2011-11-24 22:43:48Z glebius $"); 35 36#include "opt_mpath.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 50#include <net/if.h> 51#include <net/if_var.h> 52#include <net/if_arp.h> 53#include <net/if_dl.h> 54#include <net/if_llatbl.h> 55#include <net/if_types.h> 56#include <net/route.h> 57#include <net/vnet.h> 58 59#include <netinet/in.h> 60#include <netinet/in_var.h> 61#include <netinet/in_pcb.h> 62#include <netinet/ip_var.h> 63#include <netinet/igmp_var.h> 64#include <netinet/udp.h> 65#include <netinet/udp_var.h> 66 67static int in_mask2len(struct in_addr *); 68static void in_len2mask(struct in_addr *, int); 69static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 70 struct ifnet *, struct thread *); 71 72static int in_addprefix(struct in_ifaddr *, int); 73static int in_scrubprefix(struct in_ifaddr *, u_int); 74static void in_socktrim(struct sockaddr_in *); 75static int in_ifinit(struct ifnet *, 76 struct in_ifaddr *, struct sockaddr_in *, int); 77static void in_purgemaddrs(struct ifnet *); 78 79static VNET_DEFINE(int, sameprefixcarponly); 80#define V_sameprefixcarponly VNET(sameprefixcarponly) 81SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW, 82 &VNET_NAME(sameprefixcarponly), 0, 83 "Refuse to create same prefixes on different interfaces"); 84 85VNET_DECLARE(struct inpcbinfo, ripcbinfo); 86#define V_ripcbinfo VNET(ripcbinfo) 87 88VNET_DECLARE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */ 89#define V_arpstat VNET(arpstat) 90 91/* 92 * Return 1 if an internet address is for a ``local'' host 93 * (one to which we have a connection). 94 */ 95int 96in_localaddr(struct in_addr in) 97{ 98 register u_long i = ntohl(in.s_addr); 99 register struct in_ifaddr *ia; 100 101 IN_IFADDR_RLOCK(); 102 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 103 if ((i & ia->ia_subnetmask) == ia->ia_subnet) { 104 IN_IFADDR_RUNLOCK(); 105 return (1); 106 } 107 } 108 IN_IFADDR_RUNLOCK(); 109 return (0); 110} 111 112/* 113 * Return 1 if an internet address is for the local host and configured 114 * on one of its interfaces. 115 */ 116int 117in_localip(struct in_addr in) 118{ 119 struct in_ifaddr *ia; 120 121 IN_IFADDR_RLOCK(); 122 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 123 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) { 124 IN_IFADDR_RUNLOCK(); 125 return (1); 126 } 127 } 128 IN_IFADDR_RUNLOCK(); 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 register struct ifreq *ifr = (struct ifreq *)data; 217 register struct in_ifaddr *ia, *iap; 218 register struct ifaddr *ifa; 219 struct in_addr allhosts_addr; 220 struct in_addr dst; 221 struct in_ifinfo *ii; 222 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 223 struct sockaddr_in oldaddr; 224 int error, hostIsNew, iaIsNew, maskIsNew; 225 int iaIsFirst; 226 227 ia = NULL; 228 iaIsFirst = 0; 229 iaIsNew = 0; 230 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 231 232 /* 233 * Filter out ioctls we implement directly; forward the rest on to 234 * in_lifaddr_ioctl() and ifp->if_ioctl(). 235 */ 236 switch (cmd) { 237 case SIOCGIFADDR: 238 case SIOCGIFBRDADDR: 239 case SIOCGIFDSTADDR: 240 case SIOCGIFNETMASK: 241 case SIOCDIFADDR: 242 break; 243 case SIOCAIFADDR: 244 /* 245 * ifra_addr must be present and be of INET family. 246 * ifra_broadaddr and ifra_mask are optional. 247 */ 248 if (ifra->ifra_addr.sin_len != sizeof(struct sockaddr_in) || 249 ifra->ifra_addr.sin_family != AF_INET) 250 return (EINVAL); 251 if (ifra->ifra_broadaddr.sin_len != 0 && 252 (ifra->ifra_broadaddr.sin_len != 253 sizeof(struct sockaddr_in) || 254 ifra->ifra_broadaddr.sin_family != AF_INET)) 255 return (EINVAL); 256#if 0 257 /* 258 * ifconfig(8) historically doesn't set af_family for mask 259 * for unknown reason. 260 */ 261 if (ifra->ifra_mask.sin_len != 0 && 262 (ifra->ifra_mask.sin_len != sizeof(struct sockaddr_in) || 263 ifra->ifra_mask.sin_family != AF_INET)) 264 return (EINVAL); 265#endif 266 break; 267 case SIOCSIFADDR: 268 case SIOCSIFBRDADDR: 269 case SIOCSIFDSTADDR: 270 case SIOCSIFNETMASK: 271 if (ifr->ifr_addr.sa_family != AF_INET || 272 ifr->ifr_addr.sa_len != sizeof(struct sockaddr_in)) 273 return (EINVAL); 274 break; 275 276 case SIOCALIFADDR: 277 if (td != NULL) { 278 error = priv_check(td, PRIV_NET_ADDIFADDR); 279 if (error) 280 return (error); 281 } 282 if (ifp == NULL) 283 return (EINVAL); 284 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 285 286 case SIOCDLIFADDR: 287 if (td != NULL) { 288 error = priv_check(td, PRIV_NET_DELIFADDR); 289 if (error) 290 return (error); 291 } 292 if (ifp == NULL) 293 return (EINVAL); 294 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 295 296 case SIOCGLIFADDR: 297 if (ifp == NULL) 298 return (EINVAL); 299 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 300 301 default: 302 if (ifp == NULL || ifp->if_ioctl == NULL) 303 return (EOPNOTSUPP); 304 return ((*ifp->if_ioctl)(ifp, cmd, data)); 305 } 306 307 if (ifp == NULL) 308 return (EADDRNOTAVAIL); 309 310 /* 311 * Security checks before we get involved in any work. 312 */ 313 switch (cmd) { 314 case SIOCAIFADDR: 315 case SIOCSIFADDR: 316 case SIOCSIFBRDADDR: 317 case SIOCSIFNETMASK: 318 case SIOCSIFDSTADDR: 319 if (td != NULL) { 320 error = priv_check(td, PRIV_NET_ADDIFADDR); 321 if (error) 322 return (error); 323 } 324 break; 325 326 case SIOCDIFADDR: 327 if (td != NULL) { 328 error = priv_check(td, PRIV_NET_DELIFADDR); 329 if (error) 330 return (error); 331 } 332 break; 333 } 334 335 /* 336 * Find address for this interface, if it exists. 337 * 338 * If an alias address was specified, find that one instead of the 339 * first one on the interface, if possible. 340 */ 341 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 342 IN_IFADDR_RLOCK(); 343 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) { 344 if (iap->ia_ifp == ifp && 345 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 346 if (td == NULL || prison_check_ip4(td->td_ucred, 347 &dst) == 0) 348 ia = iap; 349 break; 350 } 351 } 352 if (ia != NULL) 353 ifa_ref(&ia->ia_ifa); 354 IN_IFADDR_RUNLOCK(); 355 if (ia == NULL) { 356 IF_ADDR_LOCK(ifp); 357 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 358 iap = ifatoia(ifa); 359 if (iap->ia_addr.sin_family == AF_INET) { 360 if (td != NULL && 361 prison_check_ip4(td->td_ucred, 362 &iap->ia_addr.sin_addr) != 0) 363 continue; 364 ia = iap; 365 break; 366 } 367 } 368 if (ia != NULL) 369 ifa_ref(&ia->ia_ifa); 370 IF_ADDR_UNLOCK(ifp); 371 } 372 if (ia == NULL) 373 iaIsFirst = 1; 374 375 error = 0; 376 switch (cmd) { 377 case SIOCAIFADDR: 378 case SIOCDIFADDR: 379 if (ifra->ifra_addr.sin_family == AF_INET) { 380 struct in_ifaddr *oia; 381 382 IN_IFADDR_RLOCK(); 383 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 384 if (ia->ia_ifp == ifp && 385 ia->ia_addr.sin_addr.s_addr == 386 ifra->ifra_addr.sin_addr.s_addr) 387 break; 388 } 389 if (ia != NULL && ia != oia) 390 ifa_ref(&ia->ia_ifa); 391 if (oia != NULL && ia != oia) 392 ifa_free(&oia->ia_ifa); 393 IN_IFADDR_RUNLOCK(); 394 if ((ifp->if_flags & IFF_POINTOPOINT) 395 && (cmd == SIOCAIFADDR) 396 && (ifra->ifra_dstaddr.sin_addr.s_addr 397 == INADDR_ANY)) { 398 error = EDESTADDRREQ; 399 goto out; 400 } 401 } 402 if (cmd == SIOCDIFADDR && ia == NULL) { 403 error = EADDRNOTAVAIL; 404 goto out; 405 } 406 /* FALLTHROUGH */ 407 case SIOCSIFADDR: 408 case SIOCSIFNETMASK: 409 case SIOCSIFDSTADDR: 410 if (ia == NULL) { 411 ia = (struct in_ifaddr *) 412 malloc(sizeof *ia, M_IFADDR, M_NOWAIT | 413 M_ZERO); 414 if (ia == NULL) { 415 error = ENOBUFS; 416 goto out; 417 } 418 419 ifa = &ia->ia_ifa; 420 ifa_init(ifa); 421 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 422 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 423 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 424 425 ia->ia_sockmask.sin_len = 8; 426 ia->ia_sockmask.sin_family = AF_INET; 427 if (ifp->if_flags & IFF_BROADCAST) { 428 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 429 ia->ia_broadaddr.sin_family = AF_INET; 430 } 431 ia->ia_ifp = ifp; 432 433 ifa_ref(ifa); /* if_addrhead */ 434 IF_ADDR_LOCK(ifp); 435 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 436 IF_ADDR_UNLOCK(ifp); 437 ifa_ref(ifa); /* in_ifaddrhead */ 438 IN_IFADDR_WLOCK(); 439 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 440 IN_IFADDR_WUNLOCK(); 441 iaIsNew = 1; 442 } 443 break; 444 445 case SIOCSIFBRDADDR: 446 case SIOCGIFADDR: 447 case SIOCGIFNETMASK: 448 case SIOCGIFDSTADDR: 449 case SIOCGIFBRDADDR: 450 if (ia == NULL) { 451 error = EADDRNOTAVAIL; 452 goto out; 453 } 454 break; 455 } 456 457 /* 458 * Most paths in this switch return directly or via out. Only paths 459 * that remove the address break in order to hit common removal code. 460 */ 461 switch (cmd) { 462 case SIOCGIFADDR: 463 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 464 goto out; 465 466 case SIOCGIFBRDADDR: 467 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 468 error = EINVAL; 469 goto out; 470 } 471 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 472 goto out; 473 474 case SIOCGIFDSTADDR: 475 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 476 error = EINVAL; 477 goto out; 478 } 479 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 480 goto out; 481 482 case SIOCGIFNETMASK: 483 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 484 goto out; 485 486 case SIOCSIFDSTADDR: 487 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 488 error = EINVAL; 489 goto out; 490 } 491 oldaddr = ia->ia_dstaddr; 492 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 493 if (ifp->if_ioctl != NULL) { 494 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 495 (caddr_t)ia); 496 if (error) { 497 ia->ia_dstaddr = oldaddr; 498 goto out; 499 } 500 } 501 if (ia->ia_flags & IFA_ROUTE) { 502 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 503 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 504 ia->ia_ifa.ifa_dstaddr = 505 (struct sockaddr *)&ia->ia_dstaddr; 506 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 507 } 508 goto out; 509 510 case SIOCSIFBRDADDR: 511 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 512 error = EINVAL; 513 goto out; 514 } 515 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 516 goto out; 517 518 case SIOCSIFADDR: 519 error = in_ifinit(ifp, ia, 520 (struct sockaddr_in *) &ifr->ifr_addr, 1); 521 if (error != 0 && iaIsNew) 522 break; 523 if (error == 0) { 524 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 525 if (iaIsFirst && 526 (ifp->if_flags & IFF_MULTICAST) != 0) { 527 error = in_joingroup(ifp, &allhosts_addr, 528 NULL, &ii->ii_allhosts); 529 } 530 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 531 } 532 error = 0; 533 goto out; 534 535 case SIOCSIFNETMASK: 536 ia->ia_sockmask = *(struct sockaddr_in *)&ifr->ifr_addr; 537 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 538 goto out; 539 540 case SIOCAIFADDR: 541 maskIsNew = 0; 542 hostIsNew = 1; 543 error = 0; 544 if (ifra->ifra_addr.sin_len == 0) { 545 ifra->ifra_addr = ia->ia_addr; 546 hostIsNew = 0; 547 } else if (ifra->ifra_addr.sin_addr.s_addr == 548 ia->ia_addr.sin_addr.s_addr) 549 hostIsNew = 0; 550 if (ifra->ifra_mask.sin_len) { 551 /* 552 * QL: XXX 553 * Need to scrub the prefix here in case 554 * the issued command is SIOCAIFADDR with 555 * the same address, but with a different 556 * prefix length. And if the prefix length 557 * is the same as before, then the call is 558 * un-necessarily executed here. 559 */ 560 in_ifscrub(ifp, ia, LLE_STATIC); 561 ia->ia_sockmask = ifra->ifra_mask; 562 ia->ia_sockmask.sin_family = AF_INET; 563 ia->ia_subnetmask = 564 ntohl(ia->ia_sockmask.sin_addr.s_addr); 565 maskIsNew = 1; 566 } 567 if ((ifp->if_flags & IFF_POINTOPOINT) && 568 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 569 in_ifscrub(ifp, ia, LLE_STATIC); 570 ia->ia_dstaddr = ifra->ifra_dstaddr; 571 maskIsNew = 1; /* We lie; but the effect's the same */ 572 } 573 if (hostIsNew || maskIsNew) 574 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 575 if (error != 0 && iaIsNew) 576 break; 577 578 if ((ifp->if_flags & IFF_BROADCAST) && 579 ifra->ifra_broadaddr.sin_len) 580 ia->ia_broadaddr = ifra->ifra_broadaddr; 581 if (error == 0) { 582 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 583 if (iaIsFirst && 584 (ifp->if_flags & IFF_MULTICAST) != 0) { 585 error = in_joingroup(ifp, &allhosts_addr, 586 NULL, &ii->ii_allhosts); 587 } 588 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 589 } 590 goto out; 591 592 case SIOCDIFADDR: 593 /* 594 * in_ifscrub kills the interface route. 595 */ 596 in_ifscrub(ifp, ia, LLE_STATIC); 597 598 /* 599 * in_ifadown gets rid of all the rest of 600 * the routes. This is not quite the right 601 * thing to do, but at least if we are running 602 * a routing process they will come back. 603 */ 604 in_ifadown(&ia->ia_ifa, 1); 605 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 606 error = 0; 607 break; 608 609 default: 610 panic("in_control: unsupported ioctl"); 611 } 612 613 IF_ADDR_LOCK(ifp); 614 /* Re-check that ia is still part of the list. */ 615 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 616 if (ifa == &ia->ia_ifa) 617 break; 618 } 619 if (ifa == NULL) { 620 /* 621 * If we lost the race with another thread, there is no need to 622 * try it again for the next loop as there is no other exit 623 * path between here and out. 624 */ 625 IF_ADDR_UNLOCK(ifp); 626 error = EADDRNOTAVAIL; 627 goto out; 628 } 629 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 630 IF_ADDR_UNLOCK(ifp); 631 ifa_free(&ia->ia_ifa); /* if_addrhead */ 632 633 IN_IFADDR_WLOCK(); 634 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 635 636 LIST_REMOVE(ia, ia_hash); 637 IN_IFADDR_WUNLOCK(); 638 /* 639 * If this is the last IPv4 address configured on this 640 * interface, leave the all-hosts group. 641 * No state-change report need be transmitted. 642 */ 643 IFP_TO_IA(ifp, iap); 644 if (iap == NULL) { 645 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 646 IN_MULTI_LOCK(); 647 if (ii->ii_allhosts) { 648 (void)in_leavegroup_locked(ii->ii_allhosts, NULL); 649 ii->ii_allhosts = NULL; 650 } 651 IN_MULTI_UNLOCK(); 652 } else 653 ifa_free(&iap->ia_ifa); 654 655 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 656out: 657 if (ia != NULL) 658 ifa_free(&ia->ia_ifa); 659 return (error); 660} 661 662/* 663 * SIOC[GAD]LIFADDR. 664 * SIOCGLIFADDR: get first address. (?!?) 665 * SIOCGLIFADDR with IFLR_PREFIX: 666 * get first address that matches the specified prefix. 667 * SIOCALIFADDR: add the specified address. 668 * SIOCALIFADDR with IFLR_PREFIX: 669 * EINVAL since we can't deduce hostid part of the address. 670 * SIOCDLIFADDR: delete the specified address. 671 * SIOCDLIFADDR with IFLR_PREFIX: 672 * delete the first address that matches the specified prefix. 673 * return values: 674 * EINVAL on invalid parameters 675 * EADDRNOTAVAIL on prefix match failed/specified address not found 676 * other values may be returned from in_ioctl() 677 */ 678static int 679in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 680 struct ifnet *ifp, struct thread *td) 681{ 682 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 683 struct ifaddr *ifa; 684 685 /* sanity checks */ 686 if (data == NULL || ifp == NULL) { 687 panic("invalid argument to in_lifaddr_ioctl"); 688 /*NOTRECHED*/ 689 } 690 691 switch (cmd) { 692 case SIOCGLIFADDR: 693 /* address must be specified on GET with IFLR_PREFIX */ 694 if ((iflr->flags & IFLR_PREFIX) == 0) 695 break; 696 /*FALLTHROUGH*/ 697 case SIOCALIFADDR: 698 case SIOCDLIFADDR: 699 /* address must be specified on ADD and DELETE */ 700 if (iflr->addr.ss_family != AF_INET) 701 return (EINVAL); 702 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 703 return (EINVAL); 704 /* XXX need improvement */ 705 if (iflr->dstaddr.ss_family 706 && iflr->dstaddr.ss_family != AF_INET) 707 return (EINVAL); 708 if (iflr->dstaddr.ss_family 709 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 710 return (EINVAL); 711 break; 712 default: /*shouldn't happen*/ 713 return (EOPNOTSUPP); 714 } 715 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 716 return (EINVAL); 717 718 switch (cmd) { 719 case SIOCALIFADDR: 720 { 721 struct in_aliasreq ifra; 722 723 if (iflr->flags & IFLR_PREFIX) 724 return (EINVAL); 725 726 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 727 bzero(&ifra, sizeof(ifra)); 728 bcopy(iflr->iflr_name, ifra.ifra_name, 729 sizeof(ifra.ifra_name)); 730 731 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 732 733 if (iflr->dstaddr.ss_family) { /*XXX*/ 734 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 735 iflr->dstaddr.ss_len); 736 } 737 738 ifra.ifra_mask.sin_family = AF_INET; 739 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 740 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 741 742 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 743 } 744 case SIOCGLIFADDR: 745 case SIOCDLIFADDR: 746 { 747 struct in_ifaddr *ia; 748 struct in_addr mask, candidate, match; 749 struct sockaddr_in *sin; 750 751 bzero(&mask, sizeof(mask)); 752 bzero(&match, sizeof(match)); 753 if (iflr->flags & IFLR_PREFIX) { 754 /* lookup a prefix rather than address. */ 755 in_len2mask(&mask, iflr->prefixlen); 756 757 sin = (struct sockaddr_in *)&iflr->addr; 758 match.s_addr = sin->sin_addr.s_addr; 759 match.s_addr &= mask.s_addr; 760 761 /* if you set extra bits, that's wrong */ 762 if (match.s_addr != sin->sin_addr.s_addr) 763 return (EINVAL); 764 765 } else { 766 /* on getting an address, take the 1st match */ 767 /* on deleting an address, do exact match */ 768 if (cmd != SIOCGLIFADDR) { 769 in_len2mask(&mask, 32); 770 sin = (struct sockaddr_in *)&iflr->addr; 771 match.s_addr = sin->sin_addr.s_addr; 772 } 773 } 774 775 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 776 if (ifa->ifa_addr->sa_family != AF_INET6) 777 continue; 778 if (match.s_addr == 0) 779 break; 780 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 781 candidate.s_addr &= mask.s_addr; 782 if (candidate.s_addr == match.s_addr) 783 break; 784 } 785 if (ifa == NULL) 786 return (EADDRNOTAVAIL); 787 ia = (struct in_ifaddr *)ifa; 788 789 if (cmd == SIOCGLIFADDR) { 790 /* fill in the if_laddrreq structure */ 791 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 792 793 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 794 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 795 ia->ia_dstaddr.sin_len); 796 } else 797 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 798 799 iflr->prefixlen = 800 in_mask2len(&ia->ia_sockmask.sin_addr); 801 802 iflr->flags = 0; /*XXX*/ 803 804 return (0); 805 } else { 806 struct in_aliasreq ifra; 807 808 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 809 bzero(&ifra, sizeof(ifra)); 810 bcopy(iflr->iflr_name, ifra.ifra_name, 811 sizeof(ifra.ifra_name)); 812 813 bcopy(&ia->ia_addr, &ifra.ifra_addr, 814 ia->ia_addr.sin_len); 815 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 816 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 817 ia->ia_dstaddr.sin_len); 818 } 819 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 820 ia->ia_sockmask.sin_len); 821 822 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 823 ifp, td)); 824 } 825 } 826 } 827 828 return (EOPNOTSUPP); /*just for safety*/ 829} 830 831/* 832 * Delete any existing route for an interface. 833 */ 834void 835in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags) 836{ 837 838 in_scrubprefix(ia, flags); 839} 840 841/* 842 * Initialize an interface's internet address 843 * and routing table entry. 844 */ 845static int 846in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 847 int scrub) 848{ 849 register u_long i = ntohl(sin->sin_addr.s_addr); 850 int flags = RTF_UP, error = 0; 851 852 if (scrub) 853 in_scrubprefix(ia, LLE_STATIC); 854 855 IN_IFADDR_WLOCK(); 856 if (ia->ia_addr.sin_family == AF_INET) 857 LIST_REMOVE(ia, ia_hash); 858 ia->ia_addr = *sin; 859 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 860 ia, ia_hash); 861 IN_IFADDR_WUNLOCK(); 862 863 /* 864 * Give the interface a chance to initialize 865 * if this is its first address, 866 * and to validate the address if necessary. 867 */ 868 if (ifp->if_ioctl != NULL && 869 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0) 870 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 871 return (error); 872 873 /* 874 * Be compatible with network classes, if netmask isn't supplied, 875 * guess it based on classes. 876 */ 877 if (ia->ia_subnetmask == 0) { 878 if (IN_CLASSA(i)) 879 ia->ia_subnetmask = IN_CLASSA_NET; 880 else if (IN_CLASSB(i)) 881 ia->ia_subnetmask = IN_CLASSB_NET; 882 else 883 ia->ia_subnetmask = IN_CLASSC_NET; 884 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 885 } 886 ia->ia_subnet = i & ia->ia_subnetmask; 887 in_socktrim(&ia->ia_sockmask); 888 /* 889 * XXX: carp(4) does not have interface route 890 */ 891 if (ifp->if_type == IFT_CARP) 892 return (0); 893 /* 894 * Add route for the network. 895 */ 896 ia->ia_ifa.ifa_metric = ifp->if_metric; 897 if (ifp->if_flags & IFF_BROADCAST) { 898 if (ia->ia_subnetmask == IN_RFC3021_MASK) 899 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 900 else 901 ia->ia_broadaddr.sin_addr.s_addr = 902 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 903 } else if (ifp->if_flags & IFF_LOOPBACK) { 904 ia->ia_dstaddr = ia->ia_addr; 905 flags |= RTF_HOST; 906 } else if (ifp->if_flags & IFF_POINTOPOINT) { 907 if (ia->ia_dstaddr.sin_family != AF_INET) 908 return (0); 909 flags |= RTF_HOST; 910 } 911 if ((error = in_addprefix(ia, flags)) != 0) 912 return (error); 913 914 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 915 return (0); 916 917 if (ifp->if_flags & IFF_POINTOPOINT && 918 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) 919 return (0); 920 921 /* 922 * add a loopback route to self 923 */ 924 if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) { 925 struct route ia_ro; 926 927 bzero(&ia_ro, sizeof(ia_ro)); 928 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr; 929 rtalloc_ign_fib(&ia_ro, 0, 0); 930 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) && 931 (ia_ro.ro_rt->rt_ifp == V_loif)) { 932 RT_LOCK(ia_ro.ro_rt); 933 RT_ADDREF(ia_ro.ro_rt); 934 RTFREE_LOCKED(ia_ro.ro_rt); 935 } else 936 error = ifa_add_loopback_route((struct ifaddr *)ia, 937 (struct sockaddr *)&ia->ia_addr); 938 if (error == 0) 939 ia->ia_flags |= IFA_RTSELF; 940 if (ia_ro.ro_rt != NULL) 941 RTFREE(ia_ro.ro_rt); 942 } 943 944 return (error); 945} 946 947#define rtinitflags(x) \ 948 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 949 ? RTF_HOST : 0) 950 951/* 952 * Generate a routing message when inserting or deleting 953 * an interface address alias. 954 */ 955static void in_addralias_rtmsg(int cmd, struct in_addr *prefix, 956 struct in_ifaddr *target) 957{ 958 struct route pfx_ro; 959 struct sockaddr_in *pfx_addr; 960 struct rtentry msg_rt; 961 962 /* QL: XXX 963 * This is a bit questionable because there is no 964 * additional route entry added/deleted for an address 965 * alias. Therefore this route report is inaccurate. 966 */ 967 bzero(&pfx_ro, sizeof(pfx_ro)); 968 pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst); 969 pfx_addr->sin_len = sizeof(*pfx_addr); 970 pfx_addr->sin_family = AF_INET; 971 pfx_addr->sin_addr = *prefix; 972 rtalloc_ign_fib(&pfx_ro, 0, 0); 973 if (pfx_ro.ro_rt != NULL) { 974 msg_rt = *pfx_ro.ro_rt; 975 976 /* QL: XXX 977 * Point the gateway to the new interface 978 * address as if a new prefix route entry has 979 * been added through the new address alias. 980 * All other parts of the rtentry is accurate, 981 * e.g., rt_key, rt_mask, rt_ifp etc. 982 */ 983 msg_rt.rt_gateway = 984 (struct sockaddr *)&target->ia_addr; 985 rt_newaddrmsg(cmd, 986 (struct ifaddr *)target, 987 0, &msg_rt); 988 RTFREE(pfx_ro.ro_rt); 989 } 990 return; 991} 992 993/* 994 * Check if we have a route for the given prefix already or add one accordingly. 995 */ 996static int 997in_addprefix(struct in_ifaddr *target, int flags) 998{ 999 struct in_ifaddr *ia; 1000 struct in_addr prefix, mask, p, m; 1001 int error; 1002 1003 if ((flags & RTF_HOST) != 0) { 1004 prefix = target->ia_dstaddr.sin_addr; 1005 mask.s_addr = 0; 1006 } else { 1007 prefix = target->ia_addr.sin_addr; 1008 mask = target->ia_sockmask.sin_addr; 1009 prefix.s_addr &= mask.s_addr; 1010 } 1011 1012 IN_IFADDR_RLOCK(); 1013 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1014 if (rtinitflags(ia)) { 1015 p = ia->ia_dstaddr.sin_addr; 1016 1017 if (prefix.s_addr != p.s_addr) 1018 continue; 1019 } else { 1020 p = ia->ia_addr.sin_addr; 1021 m = ia->ia_sockmask.sin_addr; 1022 p.s_addr &= m.s_addr; 1023 1024 if (prefix.s_addr != p.s_addr || 1025 mask.s_addr != m.s_addr) 1026 continue; 1027 } 1028 1029 /* 1030 * If we got a matching prefix route inserted by other 1031 * interface address, we are done here. 1032 */ 1033 if (ia->ia_flags & IFA_ROUTE) { 1034#ifdef RADIX_MPATH 1035 if (ia->ia_addr.sin_addr.s_addr == 1036 target->ia_addr.sin_addr.s_addr) { 1037 IN_IFADDR_RUNLOCK(); 1038 return (EEXIST); 1039 } else 1040 break; 1041#endif 1042 if (V_sameprefixcarponly && 1043 target->ia_ifp->if_type != IFT_CARP && 1044 ia->ia_ifp->if_type != IFT_CARP) { 1045 IN_IFADDR_RUNLOCK(); 1046 return (EEXIST); 1047 } else { 1048 in_addralias_rtmsg(RTM_ADD, &prefix, target); 1049 IN_IFADDR_RUNLOCK(); 1050 return (0); 1051 } 1052 } 1053 } 1054 IN_IFADDR_RUNLOCK(); 1055 1056 /* 1057 * No-one seem to have this prefix route, so we try to insert it. 1058 */ 1059 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 1060 if (!error) 1061 target->ia_flags |= IFA_ROUTE; 1062 return (error); 1063} 1064 1065extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 1066 1067/* 1068 * If there is no other address in the system that can serve a route to the 1069 * same prefix, remove the route. Hand over the route to the new address 1070 * otherwise. 1071 */ 1072static int 1073in_scrubprefix(struct in_ifaddr *target, u_int flags) 1074{ 1075 struct in_ifaddr *ia; 1076 struct in_addr prefix, mask, p; 1077 int error = 0; 1078 struct sockaddr_in prefix0, mask0; 1079 1080 /* 1081 * Remove the loopback route to the interface address. 1082 * The "useloopback" setting is not consulted because if the 1083 * user configures an interface address, turns off this 1084 * setting, and then tries to delete that interface address, 1085 * checking the current setting of "useloopback" would leave 1086 * that interface address loopback route untouched, which 1087 * would be wrong. Therefore the interface address loopback route 1088 * deletion is unconditional. 1089 */ 1090 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 1091 !(target->ia_ifp->if_flags & IFF_LOOPBACK) && 1092 (target->ia_flags & IFA_RTSELF)) { 1093 struct route ia_ro; 1094 int freeit = 0; 1095 1096 bzero(&ia_ro, sizeof(ia_ro)); 1097 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr; 1098 rtalloc_ign_fib(&ia_ro, 0, 0); 1099 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) && 1100 (ia_ro.ro_rt->rt_ifp == V_loif)) { 1101 RT_LOCK(ia_ro.ro_rt); 1102 if (ia_ro.ro_rt->rt_refcnt <= 1) 1103 freeit = 1; 1104 else if (flags & LLE_STATIC) { 1105 RT_REMREF(ia_ro.ro_rt); 1106 target->ia_flags &= ~IFA_RTSELF; 1107 } 1108 RTFREE_LOCKED(ia_ro.ro_rt); 1109 } 1110 if (freeit && (flags & LLE_STATIC)) { 1111 error = ifa_del_loopback_route((struct ifaddr *)target, 1112 (struct sockaddr *)&target->ia_addr); 1113 if (error == 0) 1114 target->ia_flags &= ~IFA_RTSELF; 1115 } 1116 if ((flags & LLE_STATIC) && 1117 !(target->ia_ifp->if_flags & IFF_NOARP)) 1118 /* remove arp cache */ 1119 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr); 1120 } 1121 1122 if (rtinitflags(target)) 1123 prefix = target->ia_dstaddr.sin_addr; 1124 else { 1125 prefix = target->ia_addr.sin_addr; 1126 mask = target->ia_sockmask.sin_addr; 1127 prefix.s_addr &= mask.s_addr; 1128 } 1129 1130 if ((target->ia_flags & IFA_ROUTE) == 0) { 1131 in_addralias_rtmsg(RTM_DELETE, &prefix, target); 1132 return (0); 1133 } 1134 1135 IN_IFADDR_RLOCK(); 1136 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1137 if (rtinitflags(ia)) 1138 p = ia->ia_dstaddr.sin_addr; 1139 else { 1140 p = ia->ia_addr.sin_addr; 1141 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1142 } 1143 1144 if ((prefix.s_addr != p.s_addr) || 1145 !(ia->ia_ifp->if_flags & IFF_UP)) 1146 continue; 1147 1148 /* 1149 * If we got a matching prefix address, move IFA_ROUTE and 1150 * the route itself to it. Make sure that routing daemons 1151 * get a heads-up. 1152 * 1153 * XXX: a special case for carp(4) interface - this should 1154 * be more generally specified as an interface that 1155 * doesn't support such action. 1156 */ 1157 if ((ia->ia_flags & IFA_ROUTE) == 0 1158 && (ia->ia_ifp->if_type != IFT_CARP)) { 1159 ifa_ref(&ia->ia_ifa); 1160 IN_IFADDR_RUNLOCK(); 1161 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, 1162 rtinitflags(target)); 1163 if (error == 0) 1164 target->ia_flags &= ~IFA_ROUTE; 1165 else 1166 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 1167 error); 1168 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 1169 rtinitflags(ia) | RTF_UP); 1170 if (error == 0) 1171 ia->ia_flags |= IFA_ROUTE; 1172 else 1173 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 1174 error); 1175 ifa_free(&ia->ia_ifa); 1176 return (error); 1177 } 1178 } 1179 IN_IFADDR_RUNLOCK(); 1180 1181 /* 1182 * remove all L2 entries on the given prefix 1183 */ 1184 bzero(&prefix0, sizeof(prefix0)); 1185 prefix0.sin_len = sizeof(prefix0); 1186 prefix0.sin_family = AF_INET; 1187 prefix0.sin_addr.s_addr = target->ia_subnet; 1188 bzero(&mask0, sizeof(mask0)); 1189 mask0.sin_len = sizeof(mask0); 1190 mask0.sin_family = AF_INET; 1191 mask0.sin_addr.s_addr = target->ia_subnetmask; 1192 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 1193 (struct sockaddr *)&mask0, flags); 1194 1195 /* 1196 * As no-one seem to have this prefix, we can remove the route. 1197 */ 1198 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 1199 if (error == 0) 1200 target->ia_flags &= ~IFA_ROUTE; 1201 else 1202 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 1203 return (error); 1204} 1205 1206#undef rtinitflags 1207 1208/* 1209 * Return 1 if the address might be a local broadcast address. 1210 */ 1211int 1212in_broadcast(struct in_addr in, struct ifnet *ifp) 1213{ 1214 register struct ifaddr *ifa; 1215 u_long t; 1216 1217 if (in.s_addr == INADDR_BROADCAST || 1218 in.s_addr == INADDR_ANY) 1219 return (1); 1220 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1221 return (0); 1222 t = ntohl(in.s_addr); 1223 /* 1224 * Look through the list of addresses for a match 1225 * with a broadcast address. 1226 */ 1227#define ia ((struct in_ifaddr *)ifa) 1228 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1229 if (ifa->ifa_addr->sa_family == AF_INET && 1230 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1231 /* 1232 * Check for old-style (host 0) broadcast, but 1233 * taking into account that RFC 3021 obsoletes it. 1234 */ 1235 (ia->ia_subnetmask != IN_RFC3021_MASK && 1236 t == ia->ia_subnet)) && 1237 /* 1238 * Check for an all one subnetmask. These 1239 * only exist when an interface gets a secondary 1240 * address. 1241 */ 1242 ia->ia_subnetmask != (u_long)0xffffffff) 1243 return (1); 1244 return (0); 1245#undef ia 1246} 1247 1248/* 1249 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1250 */ 1251void 1252in_ifdetach(struct ifnet *ifp) 1253{ 1254 1255 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1256 in_pcbpurgeif0(&V_udbinfo, ifp); 1257 in_purgemaddrs(ifp); 1258} 1259 1260/* 1261 * Delete all IPv4 multicast address records, and associated link-layer 1262 * multicast address records, associated with ifp. 1263 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1264 * XXX This should not race with ifma_protospec being set during 1265 * a new allocation, if it does, we have bigger problems. 1266 */ 1267static void 1268in_purgemaddrs(struct ifnet *ifp) 1269{ 1270 LIST_HEAD(,in_multi) purgeinms; 1271 struct in_multi *inm, *tinm; 1272 struct ifmultiaddr *ifma; 1273 1274 LIST_INIT(&purgeinms); 1275 IN_MULTI_LOCK(); 1276 1277 /* 1278 * Extract list of in_multi associated with the detaching ifp 1279 * which the PF_INET layer is about to release. 1280 * We need to do this as IF_ADDR_LOCK() may be re-acquired 1281 * by code further down. 1282 */ 1283 IF_ADDR_LOCK(ifp); 1284 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1285 if (ifma->ifma_addr->sa_family != AF_INET || 1286 ifma->ifma_protospec == NULL) 1287 continue; 1288#if 0 1289 KASSERT(ifma->ifma_protospec != NULL, 1290 ("%s: ifma_protospec is NULL", __func__)); 1291#endif 1292 inm = (struct in_multi *)ifma->ifma_protospec; 1293 LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1294 } 1295 IF_ADDR_UNLOCK(ifp); 1296 1297 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1298 LIST_REMOVE(inm, inm_link); 1299 inm_release_locked(inm); 1300 } 1301 igmp_ifdetach(ifp); 1302 1303 IN_MULTI_UNLOCK(); 1304} 1305 1306#include <net/if_dl.h> 1307#include <netinet/if_ether.h> 1308 1309struct in_llentry { 1310 struct llentry base; 1311 struct sockaddr_in l3_addr4; 1312}; 1313 1314static struct llentry * 1315in_lltable_new(const struct sockaddr *l3addr, u_int flags) 1316{ 1317 struct in_llentry *lle; 1318 1319 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO); 1320 if (lle == NULL) /* NB: caller generates msg */ 1321 return NULL; 1322 1323 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 1324 /* 1325 * For IPv4 this will trigger "arpresolve" to generate 1326 * an ARP request. 1327 */ 1328 lle->base.la_expire = time_uptime; /* mark expired */ 1329 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 1330 lle->base.lle_refcnt = 1; 1331 LLE_LOCK_INIT(&lle->base); 1332 return &lle->base; 1333} 1334 1335/* 1336 * Deletes an address from the address table. 1337 * This function is called by the timer functions 1338 * such as arptimer() and nd6_llinfo_timer(), and 1339 * the caller does the locking. 1340 */ 1341static void 1342in_lltable_free(struct lltable *llt, struct llentry *lle) 1343{ 1344 LLE_WUNLOCK(lle); 1345 LLE_LOCK_DESTROY(lle); 1346 free(lle, M_LLTABLE); 1347} 1348 1349 1350#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 1351 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 1352 1353static void 1354in_lltable_prefix_free(struct lltable *llt, 1355 const struct sockaddr *prefix, 1356 const struct sockaddr *mask, 1357 u_int flags) 1358{ 1359 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1360 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1361 struct llentry *lle, *next; 1362 register int i; 1363 size_t pkts_dropped; 1364 1365 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 1366 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 1367 1368 /* 1369 * (flags & LLE_STATIC) means deleting all entries 1370 * including static ARP entries 1371 */ 1372 if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle), 1373 pfx, msk) && 1374 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) { 1375 int canceled; 1376 1377 canceled = callout_drain(&lle->la_timer); 1378 LLE_WLOCK(lle); 1379 if (canceled) 1380 LLE_REMREF(lle); 1381 pkts_dropped = llentry_free(lle); 1382 ARPSTAT_ADD(dropped, pkts_dropped); 1383 } 1384 } 1385 } 1386} 1387 1388 1389static int 1390in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 1391{ 1392 struct rtentry *rt; 1393 1394 KASSERT(l3addr->sa_family == AF_INET, 1395 ("sin_family %d", l3addr->sa_family)); 1396 1397 /* XXX rtalloc1 should take a const param */ 1398 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 1399 1400 if (rt == NULL) 1401 return (EINVAL); 1402 1403 /* 1404 * If the gateway for an existing host route matches the target L3 1405 * address, which is a special route inserted by some implementation 1406 * such as MANET, and the interface is of the correct type, then 1407 * allow for ARP to proceed. 1408 */ 1409 if (rt->rt_flags & RTF_GATEWAY) { 1410 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 1411 rt->rt_ifp->if_type != IFT_ETHER || 1412 (rt->rt_ifp->if_flags & 1413 (IFF_NOARP | IFF_STATICARP)) != 0 || 1414 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 1415 sizeof(in_addr_t)) != 0) { 1416 RTFREE_LOCKED(rt); 1417 return (EINVAL); 1418 } 1419 } 1420 1421 /* 1422 * Make sure that at least the destination address is covered 1423 * by the route. This is for handling the case where 2 or more 1424 * interfaces have the same prefix. An incoming packet arrives 1425 * on one interface and the corresponding outgoing packet leaves 1426 * another interface. 1427 */ 1428 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 1429 const char *sa, *mask, *addr, *lim; 1430 int len; 1431 1432 mask = (const char *)rt_mask(rt); 1433 /* 1434 * Just being extra cautious to avoid some custom 1435 * code getting into trouble. 1436 */ 1437 if (mask == NULL) { 1438 RTFREE_LOCKED(rt); 1439 return (EINVAL); 1440 } 1441 1442 sa = (const char *)rt_key(rt); 1443 addr = (const char *)l3addr; 1444 len = ((const struct sockaddr_in *)l3addr)->sin_len; 1445 lim = addr + len; 1446 1447 for ( ; addr < lim; sa++, mask++, addr++) { 1448 if ((*sa ^ *addr) & *mask) { 1449#ifdef DIAGNOSTIC 1450 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1451 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1452#endif 1453 RTFREE_LOCKED(rt); 1454 return (EINVAL); 1455 } 1456 } 1457 } 1458 1459 RTFREE_LOCKED(rt); 1460 return (0); 1461} 1462 1463/* 1464 * Return NULL if not found or marked for deletion. 1465 * If found return lle read locked. 1466 */ 1467static struct llentry * 1468in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1469{ 1470 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1471 struct ifnet *ifp = llt->llt_ifp; 1472 struct llentry *lle; 1473 struct llentries *lleh; 1474 u_int hashkey; 1475 1476 IF_AFDATA_LOCK_ASSERT(ifp); 1477 KASSERT(l3addr->sa_family == AF_INET, 1478 ("sin_family %d", l3addr->sa_family)); 1479 1480 hashkey = sin->sin_addr.s_addr; 1481 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1482 LIST_FOREACH(lle, lleh, lle_next) { 1483 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle); 1484 if (lle->la_flags & LLE_DELETED) 1485 continue; 1486 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 1487 break; 1488 } 1489 if (lle == NULL) { 1490#ifdef DIAGNOSTIC 1491 if (flags & LLE_DELETE) 1492 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1493#endif 1494 if (!(flags & LLE_CREATE)) 1495 return (NULL); 1496 /* 1497 * A route that covers the given address must have 1498 * been installed 1st because we are doing a resolution, 1499 * verify this. 1500 */ 1501 if (!(flags & LLE_IFADDR) && 1502 in_lltable_rtcheck(ifp, flags, l3addr) != 0) 1503 goto done; 1504 1505 lle = in_lltable_new(l3addr, flags); 1506 if (lle == NULL) { 1507 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1508 goto done; 1509 } 1510 lle->la_flags = flags & ~LLE_CREATE; 1511 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1512 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1513 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1514 } 1515 1516 lle->lle_tbl = llt; 1517 lle->lle_head = lleh; 1518 LIST_INSERT_HEAD(lleh, lle, lle_next); 1519 } else if (flags & LLE_DELETE) { 1520 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1521 LLE_WLOCK(lle); 1522 lle->la_flags = LLE_DELETED; 1523 EVENTHANDLER_INVOKE(arp_update_event, lle); 1524 LLE_WUNLOCK(lle); 1525#ifdef DIAGNOSTIC 1526 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1527#endif 1528 } 1529 lle = (void *)-1; 1530 1531 } 1532 if (LLE_IS_VALID(lle)) { 1533 if (flags & LLE_EXCLUSIVE) 1534 LLE_WLOCK(lle); 1535 else 1536 LLE_RLOCK(lle); 1537 } 1538done: 1539 return (lle); 1540} 1541 1542static int 1543in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1544{ 1545#define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1546 struct ifnet *ifp = llt->llt_ifp; 1547 struct llentry *lle; 1548 /* XXX stack use */ 1549 struct { 1550 struct rt_msghdr rtm; 1551 struct sockaddr_inarp sin; 1552 struct sockaddr_dl sdl; 1553 } arpc; 1554 int error, i; 1555 1556 LLTABLE_LOCK_ASSERT(); 1557 1558 error = 0; 1559 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1560 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1561 struct sockaddr_dl *sdl; 1562 1563 /* skip deleted entries */ 1564 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 1565 continue; 1566 /* Skip if jailed and not a valid IP of the prison. */ 1567 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1568 continue; 1569 /* 1570 * produce a msg made of: 1571 * struct rt_msghdr; 1572 * struct sockaddr_inarp; (IPv4) 1573 * struct sockaddr_dl; 1574 */ 1575 bzero(&arpc, sizeof(arpc)); 1576 arpc.rtm.rtm_msglen = sizeof(arpc); 1577 arpc.rtm.rtm_version = RTM_VERSION; 1578 arpc.rtm.rtm_type = RTM_GET; 1579 arpc.rtm.rtm_flags = RTF_UP; 1580 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1581 arpc.sin.sin_family = AF_INET; 1582 arpc.sin.sin_len = sizeof(arpc.sin); 1583 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1584 1585 /* publish */ 1586 if (lle->la_flags & LLE_PUB) { 1587 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1588 /* proxy only */ 1589 if (lle->la_flags & LLE_PROXY) 1590 arpc.sin.sin_other = SIN_PROXY; 1591 } 1592 1593 sdl = &arpc.sdl; 1594 sdl->sdl_family = AF_LINK; 1595 sdl->sdl_len = sizeof(*sdl); 1596 sdl->sdl_index = ifp->if_index; 1597 sdl->sdl_type = ifp->if_type; 1598 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 1599 sdl->sdl_alen = ifp->if_addrlen; 1600 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1601 } else { 1602 sdl->sdl_alen = 0; 1603 bzero(LLADDR(sdl), ifp->if_addrlen); 1604 } 1605 1606 arpc.rtm.rtm_rmx.rmx_expire = 1607 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1608 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1609 if (lle->la_flags & LLE_STATIC) 1610 arpc.rtm.rtm_flags |= RTF_STATIC; 1611 arpc.rtm.rtm_index = ifp->if_index; 1612 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1613 if (error) 1614 break; 1615 } 1616 } 1617 return error; 1618#undef SIN 1619} 1620 1621void * 1622in_domifattach(struct ifnet *ifp) 1623{ 1624 struct in_ifinfo *ii; 1625 struct lltable *llt; 1626 1627 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1628 1629 llt = lltable_init(ifp, AF_INET); 1630 if (llt != NULL) { 1631 llt->llt_free = in_lltable_free; 1632 llt->llt_prefix_free = in_lltable_prefix_free; 1633 llt->llt_lookup = in_lltable_lookup; 1634 llt->llt_dump = in_lltable_dump; 1635 } 1636 ii->ii_llt = llt; 1637 1638 ii->ii_igmp = igmp_domifattach(ifp); 1639 1640 return ii; 1641} 1642 1643void 1644in_domifdetach(struct ifnet *ifp, void *aux) 1645{ 1646 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1647 1648 igmp_domifdetach(ifp); 1649 lltable_free(ii->ii_llt); 1650 free(ii, M_IFADDR); 1651} 1652