82#endif 83#ifdef DEV_CARP 84#include <netinet/ip_carp.h> 85#endif 86 87#include <security/mac/mac_framework.h> 88 89SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 90SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 91 92/* Log link state change events */ 93static int log_link_state_change = 1; 94 95SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 96 &log_link_state_change, 0, 97 "log interface link state change events"); 98 99void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 100void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 101void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 102 103struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 104 105/* 106 * XXX: Style; these should be sorted alphabetically, and unprototyped 107 * static functions should be prototyped. Currently they are sorted by 108 * declaration order. 109 */ 110static void if_attachdomain(void *); 111static void if_attachdomain1(struct ifnet *); 112static int ifconf(u_long, caddr_t); 113static void if_freemulti(struct ifmultiaddr *); 114static void if_grow(void); 115static void if_init(void *); 116static void if_qflush(struct ifnet *); 117static void if_route(struct ifnet *, int flag, int fam); 118static int if_setflag(struct ifnet *, int, int, int *, int); 119static void if_slowtimo(void *); 120static int if_transmit(struct ifnet *ifp, struct mbuf *m); 121static void if_unroute(struct ifnet *, int flag, int fam); 122static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 123static int if_rtdel(struct radix_node *, void *); 124static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 125static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 126static void if_start_deferred(void *context, int pending); 127static void do_link_state_change(void *, int); 128static int if_getgroup(struct ifgroupreq *, struct ifnet *); 129static int if_getgroupmembers(struct ifgroupreq *); 130 131#ifdef INET6 132/* 133 * XXX: declare here to avoid to include many inet6 related files.. 134 * should be more generalized? 135 */ 136extern void nd6_setmtu(struct ifnet *); 137#endif 138 139#ifdef VIMAGE_GLOBALS 140struct ifnethead ifnet; /* depend on static init XXX */ 141struct ifgrouphead ifg_head; 142int if_index; 143static int if_indexlim; 144/* Table of ifnet/cdev by index. Locked with ifnet_lock. */ 145static struct ifindex_entry *ifindex_table; 146static struct knlist ifklist; 147#endif 148 149int ifqmaxlen = IFQ_MAXLEN; 150struct mtx ifnet_lock; 151static if_com_alloc_t *if_com_alloc[256]; 152static if_com_free_t *if_com_free[256]; 153 154static void filt_netdetach(struct knote *kn); 155static int filt_netdev(struct knote *kn, long hint); 156 157static struct filterops netdev_filtops = 158 { 1, NULL, filt_netdetach, filt_netdev }; 159 160/* 161 * System initialization 162 */ 163SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 164SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_slowtimo, NULL); 165 166MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 167MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 168MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 169 170struct ifnet * 171ifnet_byindex(u_short idx) 172{ 173 INIT_VNET_NET(curvnet); 174 struct ifnet *ifp; 175 176 IFNET_RLOCK(); 177 ifp = V_ifindex_table[idx].ife_ifnet; 178 IFNET_RUNLOCK(); 179 return (ifp); 180} 181 182static void 183ifnet_setbyindex(u_short idx, struct ifnet *ifp) 184{ 185 INIT_VNET_NET(curvnet); 186 187 IFNET_WLOCK_ASSERT(); 188 189 V_ifindex_table[idx].ife_ifnet = ifp; 190} 191 192struct ifaddr * 193ifaddr_byindex(u_short idx) 194{ 195 struct ifaddr *ifa; 196 197 IFNET_RLOCK(); 198 ifa = ifnet_byindex(idx)->if_addr; 199 IFNET_RUNLOCK(); 200 return (ifa); 201} 202 203struct cdev * 204ifdev_byindex(u_short idx) 205{ 206 INIT_VNET_NET(curvnet); 207 struct cdev *cdev; 208 209 IFNET_RLOCK(); 210 cdev = V_ifindex_table[idx].ife_dev; 211 IFNET_RUNLOCK(); 212 return (cdev); 213} 214 215static void 216ifdev_setbyindex(u_short idx, struct cdev *cdev) 217{ 218 INIT_VNET_NET(curvnet); 219 220 IFNET_WLOCK(); 221 V_ifindex_table[idx].ife_dev = cdev; 222 IFNET_WUNLOCK(); 223} 224 225static d_open_t netopen; 226static d_close_t netclose; 227static d_ioctl_t netioctl; 228static d_kqfilter_t netkqfilter; 229 230static struct cdevsw net_cdevsw = { 231 .d_version = D_VERSION, 232 .d_flags = D_NEEDGIANT, 233 .d_open = netopen, 234 .d_close = netclose, 235 .d_ioctl = netioctl, 236 .d_name = "net", 237 .d_kqfilter = netkqfilter, 238}; 239 240static int 241netopen(struct cdev *dev, int flag, int mode, struct thread *td) 242{ 243 return (0); 244} 245 246static int 247netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 248{ 249 return (0); 250} 251 252static int 253netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 254{ 255 struct ifnet *ifp; 256 int error, idx; 257 258 /* only support interface specific ioctls */ 259 if (IOCGROUP(cmd) != 'i') 260 return (EOPNOTSUPP); 261 idx = dev2unit(dev); 262 if (idx == 0) { 263 /* 264 * special network device, not interface. 265 */ 266 if (cmd == SIOCGIFCONF) 267 return (ifconf(cmd, data)); /* XXX remove cmd */ 268#ifdef __amd64__ 269 if (cmd == SIOCGIFCONF32) 270 return (ifconf(cmd, data)); /* XXX remove cmd */ 271#endif 272 return (EOPNOTSUPP); 273 } 274 275 ifp = ifnet_byindex(idx); 276 if (ifp == NULL) 277 return (ENXIO); 278 279 error = ifhwioctl(cmd, ifp, data, td); 280 if (error == ENOIOCTL) 281 error = EOPNOTSUPP; 282 return (error); 283} 284 285static int 286netkqfilter(struct cdev *dev, struct knote *kn) 287{ 288 INIT_VNET_NET(curvnet); 289 struct knlist *klist; 290 struct ifnet *ifp; 291 int idx; 292 293 switch (kn->kn_filter) { 294 case EVFILT_NETDEV: 295 kn->kn_fop = &netdev_filtops; 296 break; 297 default: 298 return (EINVAL); 299 } 300 301 idx = dev2unit(dev); 302 if (idx == 0) { 303 klist = &V_ifklist; 304 } else { 305 ifp = ifnet_byindex(idx); 306 if (ifp == NULL) 307 return (1); 308 klist = &ifp->if_klist; 309 } 310 311 kn->kn_hook = (caddr_t)klist; 312 313 knlist_add(klist, kn, 0); 314 315 return (0); 316} 317 318static void 319filt_netdetach(struct knote *kn) 320{ 321 struct knlist *klist = (struct knlist *)kn->kn_hook; 322 323 knlist_remove(klist, kn, 0); 324} 325 326static int 327filt_netdev(struct knote *kn, long hint) 328{ 329 struct knlist *klist = (struct knlist *)kn->kn_hook; 330 331 /* 332 * Currently NOTE_EXIT is abused to indicate device detach. 333 */ 334 if (hint == NOTE_EXIT) { 335 kn->kn_data = NOTE_LINKINV; 336 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 337 knlist_remove_inevent(klist, kn); 338 return (1); 339 } 340 if (hint != 0) 341 kn->kn_data = hint; /* current status */ 342 if (kn->kn_sfflags & hint) 343 kn->kn_fflags |= hint; 344 return (kn->kn_fflags != 0); 345} 346 347/* 348 * Network interface utility routines. 349 * 350 * Routines with ifa_ifwith* names take sockaddr *'s as 351 * parameters. 352 */ 353 354/* ARGSUSED*/ 355static void 356if_init(void *dummy __unused) 357{ 358 INIT_VNET_NET(curvnet); 359 360 V_if_index = 0; 361 V_ifindex_table = NULL; 362 V_if_indexlim = 8; 363 364 IFNET_LOCK_INIT(); 365 TAILQ_INIT(&V_ifnet); 366 TAILQ_INIT(&V_ifg_head); 367 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL); 368 if_grow(); /* create initial table */ 369 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL, 370 0600, "network")); 371 if_clone_init(); 372} 373 374static void 375if_grow(void) 376{ 377 INIT_VNET_NET(curvnet); 378 u_int n; 379 struct ifindex_entry *e; 380 381 V_if_indexlim <<= 1; 382 n = V_if_indexlim * sizeof(*e); 383 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 384 if (V_ifindex_table != NULL) { 385 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 386 free((caddr_t)V_ifindex_table, M_IFNET); 387 } 388 V_ifindex_table = e; 389} 390 391/* 392 * Allocate a struct ifnet and an index for an interface. A layer 2 393 * common structure will also be allocated if an allocation routine is 394 * registered for the passed type. 395 */ 396struct ifnet* 397if_alloc(u_char type) 398{ 399 INIT_VNET_NET(curvnet); 400 struct ifnet *ifp; 401 402 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 403 404 /* 405 * Try to find an empty slot below if_index. If we fail, take 406 * the next slot. 407 * 408 * XXX: should be locked! 409 */ 410 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 411 if (ifnet_byindex(ifp->if_index) == NULL) 412 break; 413 } 414 /* Catch if_index overflow. */ 415 if (ifp->if_index < 1) { 416 free(ifp, M_IFNET); 417 return (NULL); 418 } 419 if (ifp->if_index > V_if_index) 420 V_if_index = ifp->if_index; 421 if (V_if_index >= V_if_indexlim) 422 if_grow(); 423 424 ifp->if_type = type; 425 426 if (if_com_alloc[type] != NULL) { 427 ifp->if_l2com = if_com_alloc[type](type, ifp); 428 if (ifp->if_l2com == NULL) { 429 free(ifp, M_IFNET); 430 return (NULL); 431 } 432 } 433 IFNET_WLOCK(); 434 ifnet_setbyindex(ifp->if_index, ifp); 435 IFNET_WUNLOCK(); 436 IF_ADDR_LOCK_INIT(ifp); 437 438 return (ifp); 439} 440 441/* 442 * Free the struct ifnet, the associated index, and the layer 2 common 443 * structure if needed. All the work is done in if_free_type(). 444 * 445 * Do not add code to this function! Add it to if_free_type(). 446 */ 447void 448if_free(struct ifnet *ifp) 449{ 450 451 if_free_type(ifp, ifp->if_type); 452} 453 454/* 455 * Do the actual work of freeing a struct ifnet, associated index, and 456 * layer 2 common structure. This version should only be called by 457 * intefaces that switch their type after calling if_alloc(). 458 */ 459void 460if_free_type(struct ifnet *ifp, u_char type) 461{ 462 INIT_VNET_NET(curvnet); /* ifp->if_vnet can be NULL here ! */ 463 464 if (ifp != ifnet_byindex(ifp->if_index)) { 465 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 466 __func__); 467 return; 468 } 469 470 IFNET_WLOCK(); 471 ifnet_setbyindex(ifp->if_index, NULL); 472 473 /* XXX: should be locked with if_findindex() */ 474 while (V_if_index > 0 && ifnet_byindex(V_if_index) == NULL) 475 V_if_index--; 476 IFNET_WUNLOCK(); 477 478 if (if_com_free[type] != NULL) 479 if_com_free[type](ifp->if_l2com, type); 480 481 IF_ADDR_LOCK_DESTROY(ifp); 482 free(ifp, M_IFNET); 483}; 484 485void 486ifq_attach(struct ifaltq *ifq, struct ifnet *ifp) 487{ 488 489 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 490 491 if (ifq->ifq_maxlen == 0) 492 ifq->ifq_maxlen = ifqmaxlen; 493 494 ifq->altq_type = 0; 495 ifq->altq_disc = NULL; 496 ifq->altq_flags &= ALTQF_CANTCHANGE; 497 ifq->altq_tbr = NULL; 498 ifq->altq_ifp = ifp; 499} 500 501void 502ifq_detach(struct ifaltq *ifq) 503{ 504 mtx_destroy(&ifq->ifq_mtx); 505} 506 507/* 508 * Perform generic interface initalization tasks and attach the interface 509 * to the list of "active" interfaces. 510 * 511 * XXX: 512 * - The decision to return void and thus require this function to 513 * succeed is questionable. 514 * - We do more initialization here then is probably a good idea. 515 * Some of this should probably move to if_alloc(). 516 * - We should probably do more sanity checking. For instance we don't 517 * do anything to insure if_xname is unique or non-empty. 518 */ 519void 520if_attach(struct ifnet *ifp) 521{ 522 INIT_VNET_NET(curvnet); 523 unsigned socksize, ifasize; 524 int namelen, masklen; 525 struct sockaddr_dl *sdl; 526 struct ifaddr *ifa; 527 528 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 529 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 530 ifp->if_xname); 531 532 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 533 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 534 IF_AFDATA_LOCK_INIT(ifp); 535 ifp->if_afdata_initialized = 0; 536 537 TAILQ_INIT(&ifp->if_addrhead); 538 TAILQ_INIT(&ifp->if_prefixhead); 539 TAILQ_INIT(&ifp->if_multiaddrs); 540 TAILQ_INIT(&ifp->if_groups); 541 542 if_addgroup(ifp, IFG_ALL); 543 544 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 545 getmicrotime(&ifp->if_lastchange); 546 ifp->if_data.ifi_epoch = time_uptime; 547 ifp->if_data.ifi_datalen = sizeof(struct if_data); 548 ifp->if_transmit = if_transmit; 549 ifp->if_qflush = if_qflush; 550#ifdef MAC 551 mac_ifnet_init(ifp); 552 mac_ifnet_create(ifp); 553#endif 554 555 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw, 556 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s", 557 net_cdevsw.d_name, ifp->if_xname)); 558 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 559 net_cdevsw.d_name, ifp->if_index); 560 561 ifq_attach(&ifp->if_snd, ifp); 562 563 /* 564 * create a Link Level name for this device 565 */ 566 namelen = strlen(ifp->if_xname); 567 /* 568 * Always save enough space for any possiable name so we can do 569 * a rename in place later. 570 */ 571 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 572 socksize = masklen + ifp->if_addrlen; 573 if (socksize < sizeof(*sdl)) 574 socksize = sizeof(*sdl); 575 socksize = roundup2(socksize, sizeof(long)); 576 ifasize = sizeof(*ifa) + 2 * socksize; 577 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 578 IFA_LOCK_INIT(ifa); 579 sdl = (struct sockaddr_dl *)(ifa + 1); 580 sdl->sdl_len = socksize; 581 sdl->sdl_family = AF_LINK; 582 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 583 sdl->sdl_nlen = namelen; 584 sdl->sdl_index = ifp->if_index; 585 sdl->sdl_type = ifp->if_type; 586 ifp->if_addr = ifa; 587 ifa->ifa_ifp = ifp; 588 ifa->ifa_rtrequest = link_rtrequest; 589 ifa->ifa_addr = (struct sockaddr *)sdl; 590 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 591 ifa->ifa_netmask = (struct sockaddr *)sdl; 592 sdl->sdl_len = masklen; 593 while (namelen != 0) 594 sdl->sdl_data[--namelen] = 0xff; 595 ifa->ifa_refcnt = 1; 596 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 597 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 598 599 600 IFNET_WLOCK(); 601 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 602 IFNET_WUNLOCK(); 603 604 if (domain_init_status >= 2) 605 if_attachdomain1(ifp); 606 607 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 608 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 609 610 /* Announce the interface. */ 611 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 612 613 if (ifp->if_watchdog != NULL) 614 if_printf(ifp, 615 "WARNING: using obsoleted if_watchdog interface\n"); 616 if (ifp->if_flags & IFF_NEEDSGIANT) 617 if_printf(ifp, 618 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n"); 619} 620 621static void 622if_attachdomain(void *dummy) 623{ 624 INIT_VNET_NET(curvnet); 625 struct ifnet *ifp; 626 int s; 627 628 s = splnet(); 629 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 630 if_attachdomain1(ifp); 631 splx(s); 632} 633SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 634 if_attachdomain, NULL); 635 636static void 637if_attachdomain1(struct ifnet *ifp) 638{ 639 struct domain *dp; 640 int s; 641 642 s = splnet(); 643 644 /* 645 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 646 * cannot lock ifp->if_afdata initialization, entirely. 647 */ 648 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 649 splx(s); 650 return; 651 } 652 if (ifp->if_afdata_initialized >= domain_init_status) { 653 IF_AFDATA_UNLOCK(ifp); 654 splx(s); 655 printf("if_attachdomain called more than once on %s\n", 656 ifp->if_xname); 657 return; 658 } 659 ifp->if_afdata_initialized = domain_init_status; 660 IF_AFDATA_UNLOCK(ifp); 661 662 /* address family dependent data region */ 663 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 664 for (dp = domains; dp; dp = dp->dom_next) { 665 if (dp->dom_ifattach) 666 ifp->if_afdata[dp->dom_family] = 667 (*dp->dom_ifattach)(ifp); 668 } 669 670 splx(s); 671} 672 673/* 674 * Remove any unicast or broadcast network addresses from an interface. 675 */ 676void 677if_purgeaddrs(struct ifnet *ifp) 678{ 679 struct ifaddr *ifa, *next; 680 681 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 682 if (ifa->ifa_addr->sa_family == AF_LINK) 683 continue; 684#ifdef INET 685 /* XXX: Ugly!! ad hoc just for INET */ 686 if (ifa->ifa_addr->sa_family == AF_INET) { 687 struct ifaliasreq ifr; 688 689 bzero(&ifr, sizeof(ifr)); 690 ifr.ifra_addr = *ifa->ifa_addr; 691 if (ifa->ifa_dstaddr) 692 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 693 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 694 NULL) == 0) 695 continue; 696 } 697#endif /* INET */ 698#ifdef INET6 699 if (ifa->ifa_addr->sa_family == AF_INET6) { 700 in6_purgeaddr(ifa); 701 /* ifp_addrhead is already updated */ 702 continue; 703 } 704#endif /* INET6 */ 705 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 706 IFAFREE(ifa); 707 } 708} 709 710/* 711 * Remove any multicast network addresses from an interface. 712 */ 713void 714if_purgemaddrs(struct ifnet *ifp) 715{ 716 struct ifmultiaddr *ifma; 717 struct ifmultiaddr *next; 718 719 IF_ADDR_LOCK(ifp); 720 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 721 if_delmulti_locked(ifp, ifma, 1); 722 IF_ADDR_UNLOCK(ifp); 723} 724 725/* 726 * Detach an interface, removing it from the 727 * list of "active" interfaces. 728 * 729 * XXXRW: There are some significant questions about event ordering, and 730 * how to prevent things from starting to use the interface during detach. 731 */ 732void 733if_detach(struct ifnet *ifp) 734{ 735 INIT_VNET_NET(ifp->if_vnet); 736 struct ifaddr *ifa; 737 struct radix_node_head *rnh; 738 int s, i, j; 739 struct domain *dp; 740 struct ifnet *iter; 741 int found = 0; 742 743 IFNET_WLOCK(); 744 TAILQ_FOREACH(iter, &V_ifnet, if_link) 745 if (iter == ifp) { 746 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 747 found = 1; 748 break; 749 } 750 IFNET_WUNLOCK(); 751 if (!found) 752 return; 753 754 /* 755 * Remove/wait for pending events. 756 */ 757 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 758 759 /* 760 * Remove routes and flush queues. 761 */ 762 s = splnet(); 763 if_down(ifp); 764#ifdef ALTQ 765 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 766 altq_disable(&ifp->if_snd); 767 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 768 altq_detach(&ifp->if_snd); 769#endif 770 771 if_purgeaddrs(ifp); 772 773#ifdef INET 774 in_ifdetach(ifp); 775#endif 776 777#ifdef INET6 778 /* 779 * Remove all IPv6 kernel structs related to ifp. This should be done 780 * before removing routing entries below, since IPv6 interface direct 781 * routes are expected to be removed by the IPv6-specific kernel API. 782 * Otherwise, the kernel will detect some inconsistency and bark it. 783 */ 784 in6_ifdetach(ifp); 785#endif 786 if_purgemaddrs(ifp); 787 788 /* 789 * Remove link ifaddr pointer and maybe decrement if_index. 790 * Clean up all addresses. 791 */ 792 ifp->if_addr = NULL; 793 destroy_dev(ifdev_byindex(ifp->if_index)); 794 ifdev_setbyindex(ifp->if_index, NULL); 795 796 /* We can now free link ifaddr. */ 797 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 798 ifa = TAILQ_FIRST(&ifp->if_addrhead); 799 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 800 IFAFREE(ifa); 801 } 802 803 /* 804 * Delete all remaining routes using this interface 805 * Unfortuneatly the only way to do this is to slog through 806 * the entire routing table looking for routes which point 807 * to this interface...oh well... 808 */ 809 for (i = 1; i <= AF_MAX; i++) { 810 for (j = 0; j < rt_numfibs; j++) { 811 if ((rnh = V_rt_tables[j][i]) == NULL) 812 continue; 813 RADIX_NODE_HEAD_LOCK(rnh); 814 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 815 RADIX_NODE_HEAD_UNLOCK(rnh); 816 } 817 } 818 819 /* Announce that the interface is gone. */ 820 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 821 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 822 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 823 824 IF_AFDATA_LOCK(ifp); 825 for (dp = domains; dp; dp = dp->dom_next) { 826 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 827 (*dp->dom_ifdetach)(ifp, 828 ifp->if_afdata[dp->dom_family]); 829 } 830 IF_AFDATA_UNLOCK(ifp); 831 832#ifdef MAC 833 mac_ifnet_destroy(ifp); 834#endif /* MAC */ 835 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 836 knlist_clear(&ifp->if_klist, 0); 837 knlist_destroy(&ifp->if_klist); 838 ifq_detach(&ifp->if_snd); 839 IF_AFDATA_DESTROY(ifp); 840 splx(s); 841} 842 843/* 844 * Add a group to an interface 845 */ 846int 847if_addgroup(struct ifnet *ifp, const char *groupname) 848{ 849 INIT_VNET_NET(ifp->if_vnet); 850 struct ifg_list *ifgl; 851 struct ifg_group *ifg = NULL; 852 struct ifg_member *ifgm; 853 854 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 855 groupname[strlen(groupname) - 1] <= '9') 856 return (EINVAL); 857 858 IFNET_WLOCK(); 859 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 860 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 861 IFNET_WUNLOCK(); 862 return (EEXIST); 863 } 864 865 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 866 M_NOWAIT)) == NULL) { 867 IFNET_WUNLOCK(); 868 return (ENOMEM); 869 } 870 871 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 872 M_TEMP, M_NOWAIT)) == NULL) { 873 free(ifgl, M_TEMP); 874 IFNET_WUNLOCK(); 875 return (ENOMEM); 876 } 877 878 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 879 if (!strcmp(ifg->ifg_group, groupname)) 880 break; 881 882 if (ifg == NULL) { 883 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 884 M_TEMP, M_NOWAIT)) == NULL) { 885 free(ifgl, M_TEMP); 886 free(ifgm, M_TEMP); 887 IFNET_WUNLOCK(); 888 return (ENOMEM); 889 } 890 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 891 ifg->ifg_refcnt = 0; 892 TAILQ_INIT(&ifg->ifg_members); 893 EVENTHANDLER_INVOKE(group_attach_event, ifg); 894 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 895 } 896 897 ifg->ifg_refcnt++; 898 ifgl->ifgl_group = ifg; 899 ifgm->ifgm_ifp = ifp; 900 901 IF_ADDR_LOCK(ifp); 902 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 903 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 904 IF_ADDR_UNLOCK(ifp); 905 906 IFNET_WUNLOCK(); 907 908 EVENTHANDLER_INVOKE(group_change_event, groupname); 909 910 return (0); 911} 912 913/* 914 * Remove a group from an interface 915 */ 916int 917if_delgroup(struct ifnet *ifp, const char *groupname) 918{ 919 INIT_VNET_NET(ifp->if_vnet); 920 struct ifg_list *ifgl; 921 struct ifg_member *ifgm; 922 923 IFNET_WLOCK(); 924 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 925 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 926 break; 927 if (ifgl == NULL) { 928 IFNET_WUNLOCK(); 929 return (ENOENT); 930 } 931 932 IF_ADDR_LOCK(ifp); 933 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 934 IF_ADDR_UNLOCK(ifp); 935 936 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 937 if (ifgm->ifgm_ifp == ifp) 938 break; 939 940 if (ifgm != NULL) { 941 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 942 free(ifgm, M_TEMP); 943 } 944 945 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 946 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 947 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 948 free(ifgl->ifgl_group, M_TEMP); 949 } 950 IFNET_WUNLOCK(); 951 952 free(ifgl, M_TEMP); 953 954 EVENTHANDLER_INVOKE(group_change_event, groupname); 955 956 return (0); 957} 958 959/* 960 * Stores all groups from an interface in memory pointed 961 * to by data 962 */ 963static int 964if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 965{ 966 int len, error; 967 struct ifg_list *ifgl; 968 struct ifg_req ifgrq, *ifgp; 969 struct ifgroupreq *ifgr = data; 970 971 if (ifgr->ifgr_len == 0) { 972 IF_ADDR_LOCK(ifp); 973 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 974 ifgr->ifgr_len += sizeof(struct ifg_req); 975 IF_ADDR_UNLOCK(ifp); 976 return (0); 977 } 978 979 len = ifgr->ifgr_len; 980 ifgp = ifgr->ifgr_groups; 981 /* XXX: wire */ 982 IF_ADDR_LOCK(ifp); 983 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 984 if (len < sizeof(ifgrq)) { 985 IF_ADDR_UNLOCK(ifp); 986 return (EINVAL); 987 } 988 bzero(&ifgrq, sizeof ifgrq); 989 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 990 sizeof(ifgrq.ifgrq_group)); 991 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 992 IF_ADDR_UNLOCK(ifp); 993 return (error); 994 } 995 len -= sizeof(ifgrq); 996 ifgp++; 997 } 998 IF_ADDR_UNLOCK(ifp); 999 1000 return (0); 1001} 1002 1003/* 1004 * Stores all members of a group in memory pointed to by data 1005 */ 1006static int 1007if_getgroupmembers(struct ifgroupreq *data) 1008{ 1009 INIT_VNET_NET(curvnet); 1010 struct ifgroupreq *ifgr = data; 1011 struct ifg_group *ifg; 1012 struct ifg_member *ifgm; 1013 struct ifg_req ifgrq, *ifgp; 1014 int len, error; 1015 1016 IFNET_RLOCK(); 1017 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1018 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1019 break; 1020 if (ifg == NULL) { 1021 IFNET_RUNLOCK(); 1022 return (ENOENT); 1023 } 1024 1025 if (ifgr->ifgr_len == 0) { 1026 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1027 ifgr->ifgr_len += sizeof(ifgrq); 1028 IFNET_RUNLOCK(); 1029 return (0); 1030 } 1031 1032 len = ifgr->ifgr_len; 1033 ifgp = ifgr->ifgr_groups; 1034 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1035 if (len < sizeof(ifgrq)) { 1036 IFNET_RUNLOCK(); 1037 return (EINVAL); 1038 } 1039 bzero(&ifgrq, sizeof ifgrq); 1040 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1041 sizeof(ifgrq.ifgrq_member)); 1042 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1043 IFNET_RUNLOCK(); 1044 return (error); 1045 } 1046 len -= sizeof(ifgrq); 1047 ifgp++; 1048 } 1049 IFNET_RUNLOCK(); 1050 1051 return (0); 1052} 1053 1054/* 1055 * Delete Routes for a Network Interface 1056 * 1057 * Called for each routing entry via the rnh->rnh_walktree() call above 1058 * to delete all route entries referencing a detaching network interface. 1059 * 1060 * Arguments: 1061 * rn pointer to node in the routing table 1062 * arg argument passed to rnh->rnh_walktree() - detaching interface 1063 * 1064 * Returns: 1065 * 0 successful 1066 * errno failed - reason indicated 1067 * 1068 */ 1069static int 1070if_rtdel(struct radix_node *rn, void *arg) 1071{ 1072 struct rtentry *rt = (struct rtentry *)rn; 1073 struct ifnet *ifp = arg; 1074 int err; 1075 1076 if (rt->rt_ifp == ifp) { 1077 1078 /* 1079 * Protect (sorta) against walktree recursion problems 1080 * with cloned routes 1081 */ 1082 if ((rt->rt_flags & RTF_UP) == 0) 1083 return (0); 1084 1085 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1086 rt_mask(rt), rt->rt_flags, 1087 (struct rtentry **) NULL, rt->rt_fibnum); 1088 if (err) { 1089 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1090 } 1091 } 1092 1093 return (0); 1094} 1095 1096/* 1097 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1098 * structs used to represent other address families, it is necessary 1099 * to perform a different comparison. 1100 */ 1101 1102#define sa_equal(a1, a2) \ 1103 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1104 1105#define sa_dl_equal(a1, a2) \ 1106 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1107 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1108 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1109 LLADDR((struct sockaddr_dl *)(a2)), \ 1110 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1111 1112/* 1113 * Locate an interface based on a complete address. 1114 */ 1115/*ARGSUSED*/ 1116struct ifaddr * 1117ifa_ifwithaddr(struct sockaddr *addr) 1118{ 1119 INIT_VNET_NET(curvnet); 1120 struct ifnet *ifp; 1121 struct ifaddr *ifa; 1122 1123 IFNET_RLOCK(); 1124 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1125 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1126 if (ifa->ifa_addr->sa_family != addr->sa_family) 1127 continue; 1128 if (sa_equal(addr, ifa->ifa_addr)) 1129 goto done; 1130 /* IP6 doesn't have broadcast */ 1131 if ((ifp->if_flags & IFF_BROADCAST) && 1132 ifa->ifa_broadaddr && 1133 ifa->ifa_broadaddr->sa_len != 0 && 1134 sa_equal(ifa->ifa_broadaddr, addr)) 1135 goto done; 1136 } 1137 ifa = NULL; 1138done: 1139 IFNET_RUNLOCK(); 1140 return (ifa); 1141} 1142 1143/* 1144 * Locate an interface based on the broadcast address. 1145 */ 1146/* ARGSUSED */ 1147struct ifaddr * 1148ifa_ifwithbroadaddr(struct sockaddr *addr) 1149{ 1150 INIT_VNET_NET(curvnet); 1151 struct ifnet *ifp; 1152 struct ifaddr *ifa; 1153 1154 IFNET_RLOCK(); 1155 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1156 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1157 if (ifa->ifa_addr->sa_family != addr->sa_family) 1158 continue; 1159 if ((ifp->if_flags & IFF_BROADCAST) && 1160 ifa->ifa_broadaddr && 1161 ifa->ifa_broadaddr->sa_len != 0 && 1162 sa_equal(ifa->ifa_broadaddr, addr)) 1163 goto done; 1164 } 1165 ifa = NULL; 1166done: 1167 IFNET_RUNLOCK(); 1168 return (ifa); 1169} 1170 1171/* 1172 * Locate the point to point interface with a given destination address. 1173 */ 1174/*ARGSUSED*/ 1175struct ifaddr * 1176ifa_ifwithdstaddr(struct sockaddr *addr) 1177{ 1178 INIT_VNET_NET(curvnet); 1179 struct ifnet *ifp; 1180 struct ifaddr *ifa; 1181 1182 IFNET_RLOCK(); 1183 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1184 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1185 continue; 1186 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1187 if (ifa->ifa_addr->sa_family != addr->sa_family) 1188 continue; 1189 if (ifa->ifa_dstaddr != NULL && 1190 sa_equal(addr, ifa->ifa_dstaddr)) 1191 goto done; 1192 } 1193 } 1194 ifa = NULL; 1195done: 1196 IFNET_RUNLOCK(); 1197 return (ifa); 1198} 1199 1200/* 1201 * Find an interface on a specific network. If many, choice 1202 * is most specific found. 1203 */ 1204struct ifaddr * 1205ifa_ifwithnet(struct sockaddr *addr) 1206{ 1207 INIT_VNET_NET(curvnet); 1208 struct ifnet *ifp; 1209 struct ifaddr *ifa; 1210 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1211 u_int af = addr->sa_family; 1212 char *addr_data = addr->sa_data, *cplim; 1213 1214 /* 1215 * AF_LINK addresses can be looked up directly by their index number, 1216 * so do that if we can. 1217 */ 1218 if (af == AF_LINK) { 1219 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1220 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1221 return (ifaddr_byindex(sdl->sdl_index)); 1222 } 1223 1224 /* 1225 * Scan though each interface, looking for ones that have 1226 * addresses in this address family. 1227 */ 1228 IFNET_RLOCK(); 1229 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1230 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1231 char *cp, *cp2, *cp3; 1232 1233 if (ifa->ifa_addr->sa_family != af) 1234next: continue; 1235 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1236 /* 1237 * This is a bit broken as it doesn't 1238 * take into account that the remote end may 1239 * be a single node in the network we are 1240 * looking for. 1241 * The trouble is that we don't know the 1242 * netmask for the remote end. 1243 */ 1244 if (ifa->ifa_dstaddr != NULL && 1245 sa_equal(addr, ifa->ifa_dstaddr)) 1246 goto done; 1247 } else { 1248 /* 1249 * if we have a special address handler, 1250 * then use it instead of the generic one. 1251 */ 1252 if (ifa->ifa_claim_addr) { 1253 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1254 goto done; 1255 continue; 1256 } 1257 1258 /* 1259 * Scan all the bits in the ifa's address. 1260 * If a bit dissagrees with what we are 1261 * looking for, mask it with the netmask 1262 * to see if it really matters. 1263 * (A byte at a time) 1264 */ 1265 if (ifa->ifa_netmask == 0) 1266 continue; 1267 cp = addr_data; 1268 cp2 = ifa->ifa_addr->sa_data; 1269 cp3 = ifa->ifa_netmask->sa_data; 1270 cplim = ifa->ifa_netmask->sa_len 1271 + (char *)ifa->ifa_netmask; 1272 while (cp3 < cplim) 1273 if ((*cp++ ^ *cp2++) & *cp3++) 1274 goto next; /* next address! */ 1275 /* 1276 * If the netmask of what we just found 1277 * is more specific than what we had before 1278 * (if we had one) then remember the new one 1279 * before continuing to search 1280 * for an even better one. 1281 */ 1282 if (ifa_maybe == 0 || 1283 rn_refines((caddr_t)ifa->ifa_netmask, 1284 (caddr_t)ifa_maybe->ifa_netmask)) 1285 ifa_maybe = ifa; 1286 } 1287 } 1288 } 1289 ifa = ifa_maybe; 1290done: 1291 IFNET_RUNLOCK(); 1292 return (ifa); 1293} 1294 1295/* 1296 * Find an interface address specific to an interface best matching 1297 * a given address. 1298 */ 1299struct ifaddr * 1300ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1301{ 1302 struct ifaddr *ifa; 1303 char *cp, *cp2, *cp3; 1304 char *cplim; 1305 struct ifaddr *ifa_maybe = 0; 1306 u_int af = addr->sa_family; 1307 1308 if (af >= AF_MAX) 1309 return (0); 1310 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1311 if (ifa->ifa_addr->sa_family != af) 1312 continue; 1313 if (ifa_maybe == 0) 1314 ifa_maybe = ifa; 1315 if (ifa->ifa_netmask == 0) { 1316 if (sa_equal(addr, ifa->ifa_addr) || 1317 (ifa->ifa_dstaddr && 1318 sa_equal(addr, ifa->ifa_dstaddr))) 1319 goto done; 1320 continue; 1321 } 1322 if (ifp->if_flags & IFF_POINTOPOINT) { 1323 if (sa_equal(addr, ifa->ifa_dstaddr)) 1324 goto done; 1325 } else { 1326 cp = addr->sa_data; 1327 cp2 = ifa->ifa_addr->sa_data; 1328 cp3 = ifa->ifa_netmask->sa_data; 1329 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1330 for (; cp3 < cplim; cp3++) 1331 if ((*cp++ ^ *cp2++) & *cp3) 1332 break; 1333 if (cp3 == cplim) 1334 goto done; 1335 } 1336 } 1337 ifa = ifa_maybe; 1338done: 1339 return (ifa); 1340} 1341 1342#include <net/route.h> 1343 1344/* 1345 * Default action when installing a route with a Link Level gateway. 1346 * Lookup an appropriate real ifa to point to. 1347 * This should be moved to /sys/net/link.c eventually. 1348 */ 1349static void 1350link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1351{ 1352 struct ifaddr *ifa, *oifa; 1353 struct sockaddr *dst; 1354 struct ifnet *ifp; 1355 1356 RT_LOCK_ASSERT(rt); 1357 1358 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1359 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1360 return; 1361 ifa = ifaof_ifpforaddr(dst, ifp); 1362 if (ifa) { 1363 IFAREF(ifa); /* XXX */ 1364 oifa = rt->rt_ifa; 1365 rt->rt_ifa = ifa; 1366 IFAFREE(oifa); 1367 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1368 ifa->ifa_rtrequest(cmd, rt, info); 1369 } 1370} 1371 1372/* 1373 * Mark an interface down and notify protocols of 1374 * the transition. 1375 * NOTE: must be called at splnet or eqivalent. 1376 */ 1377static void 1378if_unroute(struct ifnet *ifp, int flag, int fam) 1379{ 1380 struct ifaddr *ifa; 1381 1382 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1383 1384 ifp->if_flags &= ~flag; 1385 getmicrotime(&ifp->if_lastchange); 1386 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1387 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1388 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1389 ifp->if_qflush(ifp); 1390 1391#ifdef DEV_CARP 1392 if (ifp->if_carp) 1393 carp_carpdev_state(ifp->if_carp); 1394#endif 1395 rt_ifmsg(ifp); 1396} 1397 1398/* 1399 * Mark an interface up and notify protocols of 1400 * the transition. 1401 * NOTE: must be called at splnet or eqivalent. 1402 */ 1403static void 1404if_route(struct ifnet *ifp, int flag, int fam) 1405{ 1406 struct ifaddr *ifa; 1407 1408 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1409 1410 ifp->if_flags |= flag; 1411 getmicrotime(&ifp->if_lastchange); 1412 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1413 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1414 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1415#ifdef DEV_CARP 1416 if (ifp->if_carp) 1417 carp_carpdev_state(ifp->if_carp); 1418#endif 1419 rt_ifmsg(ifp); 1420#ifdef INET6 1421 in6_if_up(ifp); 1422#endif 1423} 1424 1425void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1426void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1427 1428/* 1429 * Handle a change in the interface link state. To avoid LORs 1430 * between driver lock and upper layer locks, as well as possible 1431 * recursions, we post event to taskqueue, and all job 1432 * is done in static do_link_state_change(). 1433 */ 1434void 1435if_link_state_change(struct ifnet *ifp, int link_state) 1436{ 1437 /* Return if state hasn't changed. */ 1438 if (ifp->if_link_state == link_state) 1439 return; 1440 1441 ifp->if_link_state = link_state; 1442 1443 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1444} 1445 1446static void 1447do_link_state_change(void *arg, int pending) 1448{ 1449 struct ifnet *ifp = (struct ifnet *)arg; 1450 int link_state = ifp->if_link_state; 1451 int link; 1452 CURVNET_SET(ifp->if_vnet); 1453 1454 /* Notify that the link state has changed. */ 1455 rt_ifmsg(ifp); 1456 if (link_state == LINK_STATE_UP) 1457 link = NOTE_LINKUP; 1458 else if (link_state == LINK_STATE_DOWN) 1459 link = NOTE_LINKDOWN; 1460 else 1461 link = NOTE_LINKINV; 1462 KNOTE_UNLOCKED(&ifp->if_klist, link); 1463 if (ifp->if_vlantrunk != NULL) 1464 (*vlan_link_state_p)(ifp, link); 1465 1466 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1467 IFP2AC(ifp)->ac_netgraph != NULL) 1468 (*ng_ether_link_state_p)(ifp, link_state); 1469#ifdef DEV_CARP 1470 if (ifp->if_carp) 1471 carp_carpdev_state(ifp->if_carp); 1472#endif 1473 if (ifp->if_bridge) { 1474 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1475 (*bstp_linkstate_p)(ifp, link_state); 1476 } 1477 if (ifp->if_lagg) { 1478 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1479 (*lagg_linkstate_p)(ifp, link_state); 1480 } 1481 1482 devctl_notify("IFNET", ifp->if_xname, 1483 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1484 if (pending > 1) 1485 if_printf(ifp, "%d link states coalesced\n", pending); 1486 if (log_link_state_change) 1487 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1488 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1489 CURVNET_RESTORE(); 1490} 1491 1492/* 1493 * Mark an interface down and notify protocols of 1494 * the transition. 1495 * NOTE: must be called at splnet or eqivalent. 1496 */ 1497void 1498if_down(struct ifnet *ifp) 1499{ 1500 1501 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1502} 1503 1504/* 1505 * Mark an interface up and notify protocols of 1506 * the transition. 1507 * NOTE: must be called at splnet or eqivalent. 1508 */ 1509void 1510if_up(struct ifnet *ifp) 1511{ 1512 1513 if_route(ifp, IFF_UP, AF_UNSPEC); 1514} 1515 1516/* 1517 * Flush an interface queue. 1518 */ 1519static void 1520if_qflush(struct ifnet *ifp) 1521{ 1522 struct mbuf *m, *n; 1523 struct ifaltq *ifq; 1524 1525 ifq = &ifp->if_snd; 1526 IFQ_LOCK(ifq); 1527#ifdef ALTQ 1528 if (ALTQ_IS_ENABLED(ifq)) 1529 ALTQ_PURGE(ifq); 1530#endif 1531 n = ifq->ifq_head; 1532 while ((m = n) != 0) { 1533 n = m->m_act; 1534 m_freem(m); 1535 } 1536 ifq->ifq_head = 0; 1537 ifq->ifq_tail = 0; 1538 ifq->ifq_len = 0; 1539 IFQ_UNLOCK(ifq); 1540} 1541 1542/* 1543 * Handle interface watchdog timer routines. Called 1544 * from softclock, we decrement timers (if set) and 1545 * call the appropriate interface routine on expiration. 1546 * 1547 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1548 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1549 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1550 */ 1551static void 1552if_slowtimo(void *arg) 1553{ 1554 VNET_ITERATOR_DECL(vnet_iter); 1555 struct ifnet *ifp; 1556 int s = splimp(); 1557 1558 IFNET_RLOCK(); 1559 VNET_LIST_RLOCK(); 1560 VNET_FOREACH(vnet_iter) { 1561 CURVNET_SET(vnet_iter); 1562 INIT_VNET_NET(vnet_iter); 1563 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1564 if (ifp->if_timer == 0 || --ifp->if_timer) 1565 continue; 1566 if (ifp->if_watchdog) 1567 (*ifp->if_watchdog)(ifp); 1568 } 1569 CURVNET_RESTORE(); 1570 } 1571 VNET_LIST_RUNLOCK(); 1572 IFNET_RUNLOCK(); 1573 splx(s); 1574 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1575} 1576 1577/* 1578 * Map interface name to 1579 * interface structure pointer. 1580 */ 1581struct ifnet * 1582ifunit(const char *name) 1583{ 1584 INIT_VNET_NET(curvnet); 1585 struct ifnet *ifp; 1586 1587 IFNET_RLOCK(); 1588 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1589 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1590 break; 1591 } 1592 IFNET_RUNLOCK(); 1593 return (ifp); 1594} 1595 1596/* 1597 * Hardware specific interface ioctls. 1598 */ 1599static int 1600ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1601{ 1602 struct ifreq *ifr; 1603 struct ifstat *ifs; 1604 int error = 0; 1605 int new_flags, temp_flags; 1606 size_t namelen, onamelen; 1607 char new_name[IFNAMSIZ]; 1608 struct ifaddr *ifa; 1609 struct sockaddr_dl *sdl; 1610 1611 ifr = (struct ifreq *)data; 1612 switch (cmd) { 1613 case SIOCGIFINDEX: 1614 ifr->ifr_index = ifp->if_index; 1615 break; 1616 1617 case SIOCGIFFLAGS: 1618 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1619 ifr->ifr_flags = temp_flags & 0xffff; 1620 ifr->ifr_flagshigh = temp_flags >> 16; 1621 break; 1622 1623 case SIOCGIFCAP: 1624 ifr->ifr_reqcap = ifp->if_capabilities; 1625 ifr->ifr_curcap = ifp->if_capenable; 1626 break; 1627 1628#ifdef MAC 1629 case SIOCGIFMAC: 1630 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 1631 break; 1632#endif 1633 1634 case SIOCGIFMETRIC: 1635 ifr->ifr_metric = ifp->if_metric; 1636 break; 1637 1638 case SIOCGIFMTU: 1639 ifr->ifr_mtu = ifp->if_mtu; 1640 break; 1641 1642 case SIOCGIFPHYS: 1643 ifr->ifr_phys = ifp->if_physical; 1644 break; 1645 1646 case SIOCSIFFLAGS: 1647 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1648 if (error) 1649 return (error); 1650 /* 1651 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1652 * check, so we don't need special handling here yet. 1653 */ 1654 new_flags = (ifr->ifr_flags & 0xffff) | 1655 (ifr->ifr_flagshigh << 16); 1656 if (ifp->if_flags & IFF_SMART) { 1657 /* Smart drivers twiddle their own routes */ 1658 } else if (ifp->if_flags & IFF_UP && 1659 (new_flags & IFF_UP) == 0) { 1660 int s = splimp(); 1661 if_down(ifp); 1662 splx(s); 1663 } else if (new_flags & IFF_UP && 1664 (ifp->if_flags & IFF_UP) == 0) { 1665 int s = splimp(); 1666 if_up(ifp); 1667 splx(s); 1668 } 1669 /* See if permanently promiscuous mode bit is about to flip */ 1670 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1671 if (new_flags & IFF_PPROMISC) 1672 ifp->if_flags |= IFF_PROMISC; 1673 else if (ifp->if_pcount == 0) 1674 ifp->if_flags &= ~IFF_PROMISC; 1675 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1676 ifp->if_xname, 1677 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1678 } 1679 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1680 (new_flags &~ IFF_CANTCHANGE); 1681 if (ifp->if_ioctl) { 1682 IFF_LOCKGIANT(ifp); 1683 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1684 IFF_UNLOCKGIANT(ifp); 1685 } 1686 getmicrotime(&ifp->if_lastchange); 1687 break; 1688 1689 case SIOCSIFCAP: 1690 error = priv_check(td, PRIV_NET_SETIFCAP); 1691 if (error) 1692 return (error); 1693 if (ifp->if_ioctl == NULL) 1694 return (EOPNOTSUPP); 1695 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1696 return (EINVAL); 1697 IFF_LOCKGIANT(ifp); 1698 error = (*ifp->if_ioctl)(ifp, cmd, data); 1699 IFF_UNLOCKGIANT(ifp); 1700 if (error == 0) 1701 getmicrotime(&ifp->if_lastchange); 1702 break; 1703 1704#ifdef MAC 1705 case SIOCSIFMAC: 1706 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 1707 break; 1708#endif 1709 1710 case SIOCSIFNAME: 1711 error = priv_check(td, PRIV_NET_SETIFNAME); 1712 if (error) 1713 return (error); 1714 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1715 if (error != 0) 1716 return (error); 1717 if (new_name[0] == '\0') 1718 return (EINVAL); 1719 if (ifunit(new_name) != NULL) 1720 return (EEXIST); 1721 1722 /* Announce the departure of the interface. */ 1723 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1724 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1725 1726 log(LOG_INFO, "%s: changing name to '%s'\n", 1727 ifp->if_xname, new_name); 1728 1729 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1730 ifa = ifp->if_addr; 1731 IFA_LOCK(ifa); 1732 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1733 namelen = strlen(new_name); 1734 onamelen = sdl->sdl_nlen; 1735 /* 1736 * Move the address if needed. This is safe because we 1737 * allocate space for a name of length IFNAMSIZ when we 1738 * create this in if_attach(). 1739 */ 1740 if (namelen != onamelen) { 1741 bcopy(sdl->sdl_data + onamelen, 1742 sdl->sdl_data + namelen, sdl->sdl_alen); 1743 } 1744 bcopy(new_name, sdl->sdl_data, namelen); 1745 sdl->sdl_nlen = namelen; 1746 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1747 bzero(sdl->sdl_data, onamelen); 1748 while (namelen != 0) 1749 sdl->sdl_data[--namelen] = 0xff; 1750 IFA_UNLOCK(ifa); 1751 1752 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1753 /* Announce the return of the interface. */ 1754 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1755 break; 1756 1757 case SIOCSIFMETRIC: 1758 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1759 if (error) 1760 return (error); 1761 ifp->if_metric = ifr->ifr_metric; 1762 getmicrotime(&ifp->if_lastchange); 1763 break; 1764 1765 case SIOCSIFPHYS: 1766 error = priv_check(td, PRIV_NET_SETIFPHYS); 1767 if (error) 1768 return (error); 1769 if (ifp->if_ioctl == NULL) 1770 return (EOPNOTSUPP); 1771 IFF_LOCKGIANT(ifp); 1772 error = (*ifp->if_ioctl)(ifp, cmd, data); 1773 IFF_UNLOCKGIANT(ifp); 1774 if (error == 0) 1775 getmicrotime(&ifp->if_lastchange); 1776 break; 1777 1778 case SIOCSIFMTU: 1779 { 1780 u_long oldmtu = ifp->if_mtu; 1781 1782 error = priv_check(td, PRIV_NET_SETIFMTU); 1783 if (error) 1784 return (error); 1785 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1786 return (EINVAL); 1787 if (ifp->if_ioctl == NULL) 1788 return (EOPNOTSUPP); 1789 IFF_LOCKGIANT(ifp); 1790 error = (*ifp->if_ioctl)(ifp, cmd, data); 1791 IFF_UNLOCKGIANT(ifp); 1792 if (error == 0) { 1793 getmicrotime(&ifp->if_lastchange); 1794 rt_ifmsg(ifp); 1795 } 1796 /* 1797 * If the link MTU changed, do network layer specific procedure. 1798 */ 1799 if (ifp->if_mtu != oldmtu) { 1800#ifdef INET6 1801 nd6_setmtu(ifp); 1802#endif 1803 } 1804 break; 1805 } 1806 1807 case SIOCADDMULTI: 1808 case SIOCDELMULTI: 1809 if (cmd == SIOCADDMULTI) 1810 error = priv_check(td, PRIV_NET_ADDMULTI); 1811 else 1812 error = priv_check(td, PRIV_NET_DELMULTI); 1813 if (error) 1814 return (error); 1815 1816 /* Don't allow group membership on non-multicast interfaces. */ 1817 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1818 return (EOPNOTSUPP); 1819 1820 /* Don't let users screw up protocols' entries. */ 1821 if (ifr->ifr_addr.sa_family != AF_LINK) 1822 return (EINVAL); 1823 1824 if (cmd == SIOCADDMULTI) { 1825 struct ifmultiaddr *ifma; 1826 1827 /* 1828 * Userland is only permitted to join groups once 1829 * via the if_addmulti() KPI, because it cannot hold 1830 * struct ifmultiaddr * between calls. It may also 1831 * lose a race while we check if the membership 1832 * already exists. 1833 */ 1834 IF_ADDR_LOCK(ifp); 1835 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1836 IF_ADDR_UNLOCK(ifp); 1837 if (ifma != NULL) 1838 error = EADDRINUSE; 1839 else 1840 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1841 } else { 1842 error = if_delmulti(ifp, &ifr->ifr_addr); 1843 } 1844 if (error == 0) 1845 getmicrotime(&ifp->if_lastchange); 1846 break; 1847 1848 case SIOCSIFPHYADDR: 1849 case SIOCDIFPHYADDR: 1850#ifdef INET6 1851 case SIOCSIFPHYADDR_IN6: 1852#endif 1853 case SIOCSLIFPHYADDR: 1854 case SIOCSIFMEDIA: 1855 case SIOCSIFGENERIC: 1856 error = priv_check(td, PRIV_NET_HWIOCTL); 1857 if (error) 1858 return (error); 1859 if (ifp->if_ioctl == NULL) 1860 return (EOPNOTSUPP); 1861 IFF_LOCKGIANT(ifp); 1862 error = (*ifp->if_ioctl)(ifp, cmd, data); 1863 IFF_UNLOCKGIANT(ifp); 1864 if (error == 0) 1865 getmicrotime(&ifp->if_lastchange); 1866 break; 1867 1868 case SIOCGIFSTATUS: 1869 ifs = (struct ifstat *)data; 1870 ifs->ascii[0] = '\0'; 1871 1872 case SIOCGIFPSRCADDR: 1873 case SIOCGIFPDSTADDR: 1874 case SIOCGLIFPHYADDR: 1875 case SIOCGIFMEDIA: 1876 case SIOCGIFGENERIC: 1877 if (ifp->if_ioctl == NULL) 1878 return (EOPNOTSUPP); 1879 IFF_LOCKGIANT(ifp); 1880 error = (*ifp->if_ioctl)(ifp, cmd, data); 1881 IFF_UNLOCKGIANT(ifp); 1882 break; 1883 1884 case SIOCSIFLLADDR: 1885 error = priv_check(td, PRIV_NET_SETLLADDR); 1886 if (error) 1887 return (error); 1888 error = if_setlladdr(ifp, 1889 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1890 break; 1891 1892 case SIOCAIFGROUP: 1893 { 1894 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1895 1896 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1897 if (error) 1898 return (error); 1899 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1900 return (error); 1901 break; 1902 } 1903 1904 case SIOCGIFGROUP: 1905 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1906 return (error); 1907 break; 1908 1909 case SIOCDIFGROUP: 1910 { 1911 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1912 1913 error = priv_check(td, PRIV_NET_DELIFGROUP); 1914 if (error) 1915 return (error); 1916 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1917 return (error); 1918 break; 1919 } 1920 1921 default: 1922 error = ENOIOCTL; 1923 break; 1924 } 1925 return (error); 1926} 1927 1928/* 1929 * Interface ioctls. 1930 */ 1931int 1932ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1933{ 1934 struct ifnet *ifp; 1935 struct ifreq *ifr; 1936 int error; 1937 int oif_flags; 1938 1939 switch (cmd) { 1940 case SIOCGIFCONF: 1941 case OSIOCGIFCONF: 1942#ifdef __amd64__ 1943 case SIOCGIFCONF32: 1944#endif 1945 return (ifconf(cmd, data)); 1946 } 1947 ifr = (struct ifreq *)data; 1948 1949 switch (cmd) { 1950 case SIOCIFCREATE: 1951 case SIOCIFCREATE2: 1952 error = priv_check(td, PRIV_NET_IFCREATE); 1953 if (error) 1954 return (error); 1955 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1956 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1957 case SIOCIFDESTROY: 1958 error = priv_check(td, PRIV_NET_IFDESTROY); 1959 if (error) 1960 return (error); 1961 return if_clone_destroy(ifr->ifr_name); 1962 1963 case SIOCIFGCLONERS: 1964 return (if_clone_list((struct if_clonereq *)data)); 1965 case SIOCGIFGMEMB: 1966 return (if_getgroupmembers((struct ifgroupreq *)data)); 1967 } 1968 1969 ifp = ifunit(ifr->ifr_name); 1970 if (ifp == 0) 1971 return (ENXIO); 1972 1973 error = ifhwioctl(cmd, ifp, data, td); 1974 if (error != ENOIOCTL) 1975 return (error); 1976 1977 oif_flags = ifp->if_flags; 1978 if (so->so_proto == 0) 1979 return (EOPNOTSUPP); 1980#ifndef COMPAT_43 1981 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1982 data, 1983 ifp, td)); 1984#else 1985 { 1986 int ocmd = cmd; 1987 1988 switch (cmd) { 1989 1990 case SIOCSIFDSTADDR: 1991 case SIOCSIFADDR: 1992 case SIOCSIFBRDADDR: 1993 case SIOCSIFNETMASK: 1994#if BYTE_ORDER != BIG_ENDIAN 1995 if (ifr->ifr_addr.sa_family == 0 && 1996 ifr->ifr_addr.sa_len < 16) { 1997 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1998 ifr->ifr_addr.sa_len = 16; 1999 } 2000#else 2001 if (ifr->ifr_addr.sa_len == 0) 2002 ifr->ifr_addr.sa_len = 16; 2003#endif 2004 break; 2005 2006 case OSIOCGIFADDR: 2007 cmd = SIOCGIFADDR; 2008 break; 2009 2010 case OSIOCGIFDSTADDR: 2011 cmd = SIOCGIFDSTADDR; 2012 break; 2013 2014 case OSIOCGIFBRDADDR: 2015 cmd = SIOCGIFBRDADDR; 2016 break; 2017 2018 case OSIOCGIFNETMASK: 2019 cmd = SIOCGIFNETMASK; 2020 } 2021 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 2022 cmd, 2023 data, 2024 ifp, td)); 2025 switch (ocmd) { 2026 2027 case OSIOCGIFADDR: 2028 case OSIOCGIFDSTADDR: 2029 case OSIOCGIFBRDADDR: 2030 case OSIOCGIFNETMASK: 2031 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 2032 2033 } 2034 } 2035#endif /* COMPAT_43 */ 2036 2037 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2038#ifdef INET6 2039 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 2040 if (ifp->if_flags & IFF_UP) { 2041 int s = splimp(); 2042 in6_if_up(ifp); 2043 splx(s); 2044 } 2045#endif 2046 } 2047 return (error); 2048} 2049 2050/* 2051 * The code common to handling reference counted flags, 2052 * e.g., in ifpromisc() and if_allmulti(). 2053 * The "pflag" argument can specify a permanent mode flag to check, 2054 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2055 * 2056 * Only to be used on stack-owned flags, not driver-owned flags. 2057 */ 2058static int 2059if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2060{ 2061 struct ifreq ifr; 2062 int error; 2063 int oldflags, oldcount; 2064 2065 /* Sanity checks to catch programming errors */ 2066 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2067 ("%s: setting driver-owned flag %d", __func__, flag)); 2068 2069 if (onswitch) 2070 KASSERT(*refcount >= 0, 2071 ("%s: increment negative refcount %d for flag %d", 2072 __func__, *refcount, flag)); 2073 else 2074 KASSERT(*refcount > 0, 2075 ("%s: decrement non-positive refcount %d for flag %d", 2076 __func__, *refcount, flag)); 2077 2078 /* In case this mode is permanent, just touch refcount */ 2079 if (ifp->if_flags & pflag) { 2080 *refcount += onswitch ? 1 : -1; 2081 return (0); 2082 } 2083 2084 /* Save ifnet parameters for if_ioctl() may fail */ 2085 oldcount = *refcount; 2086 oldflags = ifp->if_flags; 2087 2088 /* 2089 * See if we aren't the only and touching refcount is enough. 2090 * Actually toggle interface flag if we are the first or last. 2091 */ 2092 if (onswitch) { 2093 if ((*refcount)++) 2094 return (0); 2095 ifp->if_flags |= flag; 2096 } else { 2097 if (--(*refcount)) 2098 return (0); 2099 ifp->if_flags &= ~flag; 2100 } 2101 2102 /* Call down the driver since we've changed interface flags */ 2103 if (ifp->if_ioctl == NULL) { 2104 error = EOPNOTSUPP; 2105 goto recover; 2106 } 2107 ifr.ifr_flags = ifp->if_flags & 0xffff; 2108 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2109 IFF_LOCKGIANT(ifp); 2110 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2111 IFF_UNLOCKGIANT(ifp); 2112 if (error) 2113 goto recover; 2114 /* Notify userland that interface flags have changed */ 2115 rt_ifmsg(ifp); 2116 return (0); 2117 2118recover: 2119 /* Recover after driver error */ 2120 *refcount = oldcount; 2121 ifp->if_flags = oldflags; 2122 return (error); 2123} 2124 2125/* 2126 * Set/clear promiscuous mode on interface ifp based on the truth value 2127 * of pswitch. The calls are reference counted so that only the first 2128 * "on" request actually has an effect, as does the final "off" request. 2129 * Results are undefined if the "off" and "on" requests are not matched. 2130 */ 2131int 2132ifpromisc(struct ifnet *ifp, int pswitch) 2133{ 2134 int error; 2135 int oldflags = ifp->if_flags; 2136 2137 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2138 &ifp->if_pcount, pswitch); 2139 /* If promiscuous mode status has changed, log a message */ 2140 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2141 log(LOG_INFO, "%s: promiscuous mode %s\n", 2142 ifp->if_xname, 2143 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2144 return (error); 2145} 2146 2147/* 2148 * Return interface configuration 2149 * of system. List may be used 2150 * in later ioctl's (above) to get 2151 * other information. 2152 */ 2153/*ARGSUSED*/ 2154static int 2155ifconf(u_long cmd, caddr_t data) 2156{ 2157 INIT_VNET_NET(curvnet); 2158 struct ifconf *ifc = (struct ifconf *)data; 2159#ifdef __amd64__ 2160 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2161 struct ifconf ifc_swab; 2162#endif 2163 struct ifnet *ifp; 2164 struct ifaddr *ifa; 2165 struct ifreq ifr; 2166 struct sbuf *sb; 2167 int error, full = 0, valid_len, max_len; 2168 2169#ifdef __amd64__ 2170 if (cmd == SIOCGIFCONF32) { 2171 ifc_swab.ifc_len = ifc32->ifc_len; 2172 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2173 ifc = &ifc_swab; 2174 } 2175#endif 2176 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2177 max_len = MAXPHYS - 1; 2178 2179 /* Prevent hostile input from being able to crash the system */ 2180 if (ifc->ifc_len <= 0) 2181 return (EINVAL); 2182 2183again: 2184 if (ifc->ifc_len <= max_len) { 2185 max_len = ifc->ifc_len; 2186 full = 1; 2187 } 2188 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2189 max_len = 0; 2190 valid_len = 0; 2191 2192 IFNET_RLOCK(); /* could sleep XXX */ 2193 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2194 int addrs; 2195 2196 /* 2197 * Zero the ifr_name buffer to make sure we don't 2198 * disclose the contents of the stack. 2199 */ 2200 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2201 2202 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2203 >= sizeof(ifr.ifr_name)) { 2204 sbuf_delete(sb); 2205 IFNET_RUNLOCK(); 2206 return (ENAMETOOLONG); 2207 } 2208 2209 addrs = 0; 2210 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2211 struct sockaddr *sa = ifa->ifa_addr; 2212 2213 if (jailed(curthread->td_ucred) && 2214 !prison_if(curthread->td_ucred, sa)) 2215 continue; 2216 addrs++; 2217#ifdef COMPAT_43 2218 if (cmd == OSIOCGIFCONF) { 2219 struct osockaddr *osa = 2220 (struct osockaddr *)&ifr.ifr_addr; 2221 ifr.ifr_addr = *sa; 2222 osa->sa_family = sa->sa_family; 2223 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2224 max_len += sizeof(ifr); 2225 } else 2226#endif 2227 if (sa->sa_len <= sizeof(*sa)) { 2228 ifr.ifr_addr = *sa; 2229 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2230 max_len += sizeof(ifr); 2231 } else { 2232 sbuf_bcat(sb, &ifr, 2233 offsetof(struct ifreq, ifr_addr)); 2234 max_len += offsetof(struct ifreq, ifr_addr); 2235 sbuf_bcat(sb, sa, sa->sa_len); 2236 max_len += sa->sa_len; 2237 } 2238 2239 if (!sbuf_overflowed(sb)) 2240 valid_len = sbuf_len(sb); 2241 } 2242 if (addrs == 0) { 2243 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2244 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2245 max_len += sizeof(ifr); 2246 2247 if (!sbuf_overflowed(sb)) 2248 valid_len = sbuf_len(sb); 2249 } 2250 } 2251 IFNET_RUNLOCK(); 2252 2253 /* 2254 * If we didn't allocate enough space (uncommon), try again. If 2255 * we have already allocated as much space as we are allowed, 2256 * return what we've got. 2257 */ 2258 if (valid_len != max_len && !full) { 2259 sbuf_delete(sb); 2260 goto again; 2261 } 2262 2263 ifc->ifc_len = valid_len; 2264#ifdef __amd64__ 2265 if (cmd == SIOCGIFCONF32) 2266 ifc32->ifc_len = valid_len; 2267#endif 2268 sbuf_finish(sb); 2269 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2270 sbuf_delete(sb); 2271 return (error); 2272} 2273 2274/* 2275 * Just like ifpromisc(), but for all-multicast-reception mode. 2276 */ 2277int 2278if_allmulti(struct ifnet *ifp, int onswitch) 2279{ 2280 2281 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2282} 2283 2284struct ifmultiaddr * 2285if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2286{ 2287 struct ifmultiaddr *ifma; 2288 2289 IF_ADDR_LOCK_ASSERT(ifp); 2290 2291 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2292 if (sa->sa_family == AF_LINK) { 2293 if (sa_dl_equal(ifma->ifma_addr, sa)) 2294 break; 2295 } else { 2296 if (sa_equal(ifma->ifma_addr, sa)) 2297 break; 2298 } 2299 } 2300 2301 return ifma; 2302} 2303 2304/* 2305 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2306 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2307 * the ifnet multicast address list here, so the caller must do that and 2308 * other setup work (such as notifying the device driver). The reference 2309 * count is initialized to 1. 2310 */ 2311static struct ifmultiaddr * 2312if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2313 int mflags) 2314{ 2315 struct ifmultiaddr *ifma; 2316 struct sockaddr *dupsa; 2317 2318 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2319 M_ZERO); 2320 if (ifma == NULL) 2321 return (NULL); 2322 2323 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2324 if (dupsa == NULL) { 2325 free(ifma, M_IFMADDR); 2326 return (NULL); 2327 } 2328 bcopy(sa, dupsa, sa->sa_len); 2329 ifma->ifma_addr = dupsa; 2330 2331 ifma->ifma_ifp = ifp; 2332 ifma->ifma_refcount = 1; 2333 ifma->ifma_protospec = NULL; 2334 2335 if (llsa == NULL) { 2336 ifma->ifma_lladdr = NULL; 2337 return (ifma); 2338 } 2339 2340 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2341 if (dupsa == NULL) { 2342 free(ifma->ifma_addr, M_IFMADDR); 2343 free(ifma, M_IFMADDR); 2344 return (NULL); 2345 } 2346 bcopy(llsa, dupsa, llsa->sa_len); 2347 ifma->ifma_lladdr = dupsa; 2348 2349 return (ifma); 2350} 2351 2352/* 2353 * if_freemulti: free ifmultiaddr structure and possibly attached related 2354 * addresses. The caller is responsible for implementing reference 2355 * counting, notifying the driver, handling routing messages, and releasing 2356 * any dependent link layer state. 2357 */ 2358static void 2359if_freemulti(struct ifmultiaddr *ifma) 2360{ 2361 2362 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2363 ifma->ifma_refcount)); 2364 KASSERT(ifma->ifma_protospec == NULL, 2365 ("if_freemulti: protospec not NULL")); 2366 2367 if (ifma->ifma_lladdr != NULL) 2368 free(ifma->ifma_lladdr, M_IFMADDR); 2369 free(ifma->ifma_addr, M_IFMADDR); 2370 free(ifma, M_IFMADDR); 2371} 2372 2373/* 2374 * Register an additional multicast address with a network interface. 2375 * 2376 * - If the address is already present, bump the reference count on the 2377 * address and return. 2378 * - If the address is not link-layer, look up a link layer address. 2379 * - Allocate address structures for one or both addresses, and attach to the 2380 * multicast address list on the interface. If automatically adding a link 2381 * layer address, the protocol address will own a reference to the link 2382 * layer address, to be freed when it is freed. 2383 * - Notify the network device driver of an addition to the multicast address 2384 * list. 2385 * 2386 * 'sa' points to caller-owned memory with the desired multicast address. 2387 * 2388 * 'retifma' will be used to return a pointer to the resulting multicast 2389 * address reference, if desired. 2390 */ 2391int 2392if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2393 struct ifmultiaddr **retifma) 2394{ 2395 struct ifmultiaddr *ifma, *ll_ifma; 2396 struct sockaddr *llsa; 2397 int error; 2398 2399 /* 2400 * If the address is already present, return a new reference to it; 2401 * otherwise, allocate storage and set up a new address. 2402 */ 2403 IF_ADDR_LOCK(ifp); 2404 ifma = if_findmulti(ifp, sa); 2405 if (ifma != NULL) { 2406 ifma->ifma_refcount++; 2407 if (retifma != NULL) 2408 *retifma = ifma; 2409 IF_ADDR_UNLOCK(ifp); 2410 return (0); 2411 } 2412 2413 /* 2414 * The address isn't already present; resolve the protocol address 2415 * into a link layer address, and then look that up, bump its 2416 * refcount or allocate an ifma for that also. If 'llsa' was 2417 * returned, we will need to free it later. 2418 */ 2419 llsa = NULL; 2420 ll_ifma = NULL; 2421 if (ifp->if_resolvemulti != NULL) { 2422 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2423 if (error) 2424 goto unlock_out; 2425 } 2426 2427 /* 2428 * Allocate the new address. Don't hook it up yet, as we may also 2429 * need to allocate a link layer multicast address. 2430 */ 2431 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2432 if (ifma == NULL) { 2433 error = ENOMEM; 2434 goto free_llsa_out; 2435 } 2436 2437 /* 2438 * If a link layer address is found, we'll need to see if it's 2439 * already present in the address list, or allocate is as well. 2440 * When this block finishes, the link layer address will be on the 2441 * list. 2442 */ 2443 if (llsa != NULL) { 2444 ll_ifma = if_findmulti(ifp, llsa); 2445 if (ll_ifma == NULL) { 2446 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2447 if (ll_ifma == NULL) { 2448 --ifma->ifma_refcount; 2449 if_freemulti(ifma); 2450 error = ENOMEM; 2451 goto free_llsa_out; 2452 } 2453 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2454 ifma_link); 2455 } else 2456 ll_ifma->ifma_refcount++; 2457 ifma->ifma_llifma = ll_ifma; 2458 } 2459 2460 /* 2461 * We now have a new multicast address, ifma, and possibly a new or 2462 * referenced link layer address. Add the primary address to the 2463 * ifnet address list. 2464 */ 2465 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2466 2467 if (retifma != NULL) 2468 *retifma = ifma; 2469 2470 /* 2471 * Must generate the message while holding the lock so that 'ifma' 2472 * pointer is still valid. 2473 */ 2474 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2475 IF_ADDR_UNLOCK(ifp); 2476 2477 /* 2478 * We are certain we have added something, so call down to the 2479 * interface to let them know about it. 2480 */ 2481 if (ifp->if_ioctl != NULL) { 2482 IFF_LOCKGIANT(ifp); 2483 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2484 IFF_UNLOCKGIANT(ifp); 2485 } 2486 2487 if (llsa != NULL) 2488 free(llsa, M_IFMADDR); 2489 2490 return (0); 2491 2492free_llsa_out: 2493 if (llsa != NULL) 2494 free(llsa, M_IFMADDR); 2495 2496unlock_out: 2497 IF_ADDR_UNLOCK(ifp); 2498 return (error); 2499} 2500 2501/* 2502 * Delete a multicast group membership by network-layer group address. 2503 * 2504 * Returns ENOENT if the entry could not be found. If ifp no longer 2505 * exists, results are undefined. This entry point should only be used 2506 * from subsystems which do appropriate locking to hold ifp for the 2507 * duration of the call. 2508 * Network-layer protocol domains must use if_delmulti_ifma(). 2509 */ 2510int 2511if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2512{ 2513 struct ifmultiaddr *ifma; 2514 int lastref; 2515#ifdef INVARIANTS 2516 struct ifnet *oifp; 2517 INIT_VNET_NET(ifp->if_vnet); 2518 2519 IFNET_RLOCK(); 2520 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2521 if (ifp == oifp) 2522 break; 2523 if (ifp != oifp) 2524 ifp = NULL; 2525 IFNET_RUNLOCK(); 2526 2527 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2528#endif 2529 if (ifp == NULL) 2530 return (ENOENT); 2531 2532 IF_ADDR_LOCK(ifp); 2533 lastref = 0; 2534 ifma = if_findmulti(ifp, sa); 2535 if (ifma != NULL) 2536 lastref = if_delmulti_locked(ifp, ifma, 0); 2537 IF_ADDR_UNLOCK(ifp); 2538 2539 if (ifma == NULL) 2540 return (ENOENT); 2541 2542 if (lastref && ifp->if_ioctl != NULL) { 2543 IFF_LOCKGIANT(ifp); 2544 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2545 IFF_UNLOCKGIANT(ifp); 2546 } 2547 2548 return (0); 2549} 2550 2551/* 2552 * Delete a multicast group membership by group membership pointer. 2553 * Network-layer protocol domains must use this routine. 2554 * 2555 * It is safe to call this routine if the ifp disappeared. Callers should 2556 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2557 * reconfigured. 2558 */ 2559void 2560if_delmulti_ifma(struct ifmultiaddr *ifma) 2561{ 2562#ifdef DIAGNOSTIC 2563 INIT_VNET_NET(curvnet); 2564#endif 2565 struct ifnet *ifp; 2566 int lastref; 2567 2568 ifp = ifma->ifma_ifp; 2569#ifdef DIAGNOSTIC 2570 if (ifp == NULL) { 2571 printf("%s: ifma_ifp seems to be detached\n", __func__); 2572 } else { 2573 struct ifnet *oifp; 2574 2575 IFNET_RLOCK(); 2576 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2577 if (ifp == oifp) 2578 break; 2579 if (ifp != oifp) { 2580 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2581 ifp = NULL; 2582 } 2583 IFNET_RUNLOCK(); 2584 } 2585#endif 2586 /* 2587 * If and only if the ifnet instance exists: Acquire the address lock. 2588 */ 2589 if (ifp != NULL) 2590 IF_ADDR_LOCK(ifp); 2591 2592 lastref = if_delmulti_locked(ifp, ifma, 0); 2593 2594 if (ifp != NULL) { 2595 /* 2596 * If and only if the ifnet instance exists: 2597 * Release the address lock. 2598 * If the group was left: update the hardware hash filter. 2599 */ 2600 IF_ADDR_UNLOCK(ifp); 2601 if (lastref && ifp->if_ioctl != NULL) { 2602 IFF_LOCKGIANT(ifp); 2603 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2604 IFF_UNLOCKGIANT(ifp); 2605 } 2606 } 2607} 2608 2609/* 2610 * Perform deletion of network-layer and/or link-layer multicast address. 2611 * 2612 * Return 0 if the reference count was decremented. 2613 * Return 1 if the final reference was released, indicating that the 2614 * hardware hash filter should be reprogrammed. 2615 */ 2616static int 2617if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2618{ 2619 struct ifmultiaddr *ll_ifma; 2620 2621 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2622 KASSERT(ifma->ifma_ifp == ifp, 2623 ("%s: inconsistent ifp %p", __func__, ifp)); 2624 IF_ADDR_LOCK_ASSERT(ifp); 2625 } 2626 2627 ifp = ifma->ifma_ifp; 2628 2629 /* 2630 * If the ifnet is detaching, null out references to ifnet, 2631 * so that upper protocol layers will notice, and not attempt 2632 * to obtain locks for an ifnet which no longer exists. The 2633 * routing socket announcement must happen before the ifnet 2634 * instance is detached from the system. 2635 */ 2636 if (detaching) { 2637#ifdef DIAGNOSTIC 2638 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2639#endif 2640 /* 2641 * ifp may already be nulled out if we are being reentered 2642 * to delete the ll_ifma. 2643 */ 2644 if (ifp != NULL) { 2645 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2646 ifma->ifma_ifp = NULL; 2647 } 2648 } 2649 2650 if (--ifma->ifma_refcount > 0) 2651 return 0; 2652 2653 /* 2654 * If this ifma is a network-layer ifma, a link-layer ifma may 2655 * have been associated with it. Release it first if so. 2656 */ 2657 ll_ifma = ifma->ifma_llifma; 2658 if (ll_ifma != NULL) { 2659 KASSERT(ifma->ifma_lladdr != NULL, 2660 ("%s: llifma w/o lladdr", __func__)); 2661 if (detaching) 2662 ll_ifma->ifma_ifp = NULL; /* XXX */ 2663 if (--ll_ifma->ifma_refcount == 0) { 2664 if (ifp != NULL) { 2665 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2666 ifma_link); 2667 } 2668 if_freemulti(ll_ifma); 2669 } 2670 } 2671 2672 if (ifp != NULL) 2673 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2674 2675 if_freemulti(ifma); 2676 2677 /* 2678 * The last reference to this instance of struct ifmultiaddr 2679 * was released; the hardware should be notified of this change. 2680 */ 2681 return 1; 2682} 2683 2684/* 2685 * Set the link layer address on an interface. 2686 * 2687 * At this time we only support certain types of interfaces, 2688 * and we don't allow the length of the address to change. 2689 */ 2690int 2691if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2692{ 2693 struct sockaddr_dl *sdl; 2694 struct ifaddr *ifa; 2695 struct ifreq ifr; 2696 2697 ifa = ifp->if_addr; 2698 if (ifa == NULL) 2699 return (EINVAL); 2700 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2701 if (sdl == NULL) 2702 return (EINVAL); 2703 if (len != sdl->sdl_alen) /* don't allow length to change */ 2704 return (EINVAL); 2705 switch (ifp->if_type) { 2706 case IFT_ETHER: 2707 case IFT_FDDI: 2708 case IFT_XETHER: 2709 case IFT_ISO88025: 2710 case IFT_L2VLAN: 2711 case IFT_BRIDGE: 2712 case IFT_ARCNET: 2713 case IFT_IEEE8023ADLAG: 2714 bcopy(lladdr, LLADDR(sdl), len); 2715 break; 2716 default: 2717 return (ENODEV); 2718 } 2719 /* 2720 * If the interface is already up, we need 2721 * to re-init it in order to reprogram its 2722 * address filter. 2723 */ 2724 if ((ifp->if_flags & IFF_UP) != 0) { 2725 if (ifp->if_ioctl) { 2726 IFF_LOCKGIANT(ifp); 2727 ifp->if_flags &= ~IFF_UP; 2728 ifr.ifr_flags = ifp->if_flags & 0xffff; 2729 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2730 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2731 ifp->if_flags |= IFF_UP; 2732 ifr.ifr_flags = ifp->if_flags & 0xffff; 2733 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2734 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2735 IFF_UNLOCKGIANT(ifp); 2736 } 2737#ifdef INET 2738 /* 2739 * Also send gratuitous ARPs to notify other nodes about 2740 * the address change. 2741 */ 2742 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2743 if (ifa->ifa_addr->sa_family == AF_INET) 2744 arp_ifinit(ifp, ifa); 2745 } 2746#endif 2747 } 2748 return (0); 2749} 2750 2751/* 2752 * The name argument must be a pointer to storage which will last as 2753 * long as the interface does. For physical devices, the result of 2754 * device_get_name(dev) is a good choice and for pseudo-devices a 2755 * static string works well. 2756 */ 2757void 2758if_initname(struct ifnet *ifp, const char *name, int unit) 2759{ 2760 ifp->if_dname = name; 2761 ifp->if_dunit = unit; 2762 if (unit != IF_DUNIT_NONE) 2763 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2764 else 2765 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2766} 2767 2768int 2769if_printf(struct ifnet *ifp, const char * fmt, ...) 2770{ 2771 va_list ap; 2772 int retval; 2773 2774 retval = printf("%s: ", ifp->if_xname); 2775 va_start(ap, fmt); 2776 retval += vprintf(fmt, ap); 2777 va_end(ap); 2778 return (retval); 2779} 2780 2781/* 2782 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2783 * be called without Giant. However, we often can't acquire the Giant lock 2784 * at those points; instead, we run it via a task queue that holds Giant via 2785 * if_start_deferred. 2786 * 2787 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2788 * outstanding if_start_deferred() tasks that will run after the free. This 2789 * probably means waiting in if_detach(). 2790 */ 2791void 2792if_start(struct ifnet *ifp) 2793{ 2794 2795 if (ifp->if_flags & IFF_NEEDSGIANT) { 2796 if (mtx_owned(&Giant)) 2797 (*(ifp)->if_start)(ifp); 2798 else 2799 taskqueue_enqueue(taskqueue_swi_giant, 2800 &ifp->if_starttask); 2801 } else 2802 (*(ifp)->if_start)(ifp); 2803} 2804 2805static void 2806if_start_deferred(void *context, int pending) 2807{ 2808 struct ifnet *ifp; 2809 2810 GIANT_REQUIRED; 2811 2812 ifp = context; 2813 (ifp->if_start)(ifp); 2814} 2815 2816/* 2817 * Backwards compatibility interface for drivers 2818 * that have not implemented it 2819 */ 2820static int 2821if_transmit(struct ifnet *ifp, struct mbuf *m) 2822{ 2823 int error; 2824 2825 IFQ_HANDOFF(ifp, m, error); 2826 return (error); 2827} 2828 2829int 2830if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2831{ 2832 int active = 0; 2833 2834 IF_LOCK(ifq); 2835 if (_IF_QFULL(ifq)) { 2836 _IF_DROP(ifq); 2837 IF_UNLOCK(ifq); 2838 m_freem(m); 2839 return (0); 2840 } 2841 if (ifp != NULL) { 2842 ifp->if_obytes += m->m_pkthdr.len + adjust; 2843 if (m->m_flags & (M_BCAST|M_MCAST)) 2844 ifp->if_omcasts++; 2845 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2846 } 2847 _IF_ENQUEUE(ifq, m); 2848 IF_UNLOCK(ifq); 2849 if (ifp != NULL && !active) 2850 if_start(ifp); 2851 return (1); 2852} 2853 2854void 2855if_register_com_alloc(u_char type, 2856 if_com_alloc_t *a, if_com_free_t *f) 2857{ 2858 2859 KASSERT(if_com_alloc[type] == NULL, 2860 ("if_register_com_alloc: %d already registered", type)); 2861 KASSERT(if_com_free[type] == NULL, 2862 ("if_register_com_alloc: %d free already registered", type)); 2863 2864 if_com_alloc[type] = a; 2865 if_com_free[type] = f; 2866} 2867 2868void 2869if_deregister_com_alloc(u_char type) 2870{ 2871 2872 KASSERT(if_com_alloc[type] != NULL, 2873 ("if_deregister_com_alloc: %d not registered", type)); 2874 KASSERT(if_com_free[type] != NULL, 2875 ("if_deregister_com_alloc: %d free not registered", type)); 2876 if_com_alloc[type] = NULL; 2877 if_com_free[type] = NULL; 2878}
| 84#endif 85#ifdef DEV_CARP 86#include <netinet/ip_carp.h> 87#endif 88 89#include <security/mac/mac_framework.h> 90 91SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 92SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 93 94/* Log link state change events */ 95static int log_link_state_change = 1; 96 97SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 98 &log_link_state_change, 0, 99 "log interface link state change events"); 100 101void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 102void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 103void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 104 105struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 106 107/* 108 * XXX: Style; these should be sorted alphabetically, and unprototyped 109 * static functions should be prototyped. Currently they are sorted by 110 * declaration order. 111 */ 112static void if_attachdomain(void *); 113static void if_attachdomain1(struct ifnet *); 114static int ifconf(u_long, caddr_t); 115static void if_freemulti(struct ifmultiaddr *); 116static void if_grow(void); 117static void if_init(void *); 118static void if_qflush(struct ifnet *); 119static void if_route(struct ifnet *, int flag, int fam); 120static int if_setflag(struct ifnet *, int, int, int *, int); 121static void if_slowtimo(void *); 122static int if_transmit(struct ifnet *ifp, struct mbuf *m); 123static void if_unroute(struct ifnet *, int flag, int fam); 124static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 125static int if_rtdel(struct radix_node *, void *); 126static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 127static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 128static void if_start_deferred(void *context, int pending); 129static void do_link_state_change(void *, int); 130static int if_getgroup(struct ifgroupreq *, struct ifnet *); 131static int if_getgroupmembers(struct ifgroupreq *); 132 133#ifdef INET6 134/* 135 * XXX: declare here to avoid to include many inet6 related files.. 136 * should be more generalized? 137 */ 138extern void nd6_setmtu(struct ifnet *); 139#endif 140 141#ifdef VIMAGE_GLOBALS 142struct ifnethead ifnet; /* depend on static init XXX */ 143struct ifgrouphead ifg_head; 144int if_index; 145static int if_indexlim; 146/* Table of ifnet/cdev by index. Locked with ifnet_lock. */ 147static struct ifindex_entry *ifindex_table; 148static struct knlist ifklist; 149#endif 150 151int ifqmaxlen = IFQ_MAXLEN; 152struct mtx ifnet_lock; 153static if_com_alloc_t *if_com_alloc[256]; 154static if_com_free_t *if_com_free[256]; 155 156static void filt_netdetach(struct knote *kn); 157static int filt_netdev(struct knote *kn, long hint); 158 159static struct filterops netdev_filtops = 160 { 1, NULL, filt_netdetach, filt_netdev }; 161 162/* 163 * System initialization 164 */ 165SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 166SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_slowtimo, NULL); 167 168MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 169MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 170MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 171 172struct ifnet * 173ifnet_byindex(u_short idx) 174{ 175 INIT_VNET_NET(curvnet); 176 struct ifnet *ifp; 177 178 IFNET_RLOCK(); 179 ifp = V_ifindex_table[idx].ife_ifnet; 180 IFNET_RUNLOCK(); 181 return (ifp); 182} 183 184static void 185ifnet_setbyindex(u_short idx, struct ifnet *ifp) 186{ 187 INIT_VNET_NET(curvnet); 188 189 IFNET_WLOCK_ASSERT(); 190 191 V_ifindex_table[idx].ife_ifnet = ifp; 192} 193 194struct ifaddr * 195ifaddr_byindex(u_short idx) 196{ 197 struct ifaddr *ifa; 198 199 IFNET_RLOCK(); 200 ifa = ifnet_byindex(idx)->if_addr; 201 IFNET_RUNLOCK(); 202 return (ifa); 203} 204 205struct cdev * 206ifdev_byindex(u_short idx) 207{ 208 INIT_VNET_NET(curvnet); 209 struct cdev *cdev; 210 211 IFNET_RLOCK(); 212 cdev = V_ifindex_table[idx].ife_dev; 213 IFNET_RUNLOCK(); 214 return (cdev); 215} 216 217static void 218ifdev_setbyindex(u_short idx, struct cdev *cdev) 219{ 220 INIT_VNET_NET(curvnet); 221 222 IFNET_WLOCK(); 223 V_ifindex_table[idx].ife_dev = cdev; 224 IFNET_WUNLOCK(); 225} 226 227static d_open_t netopen; 228static d_close_t netclose; 229static d_ioctl_t netioctl; 230static d_kqfilter_t netkqfilter; 231 232static struct cdevsw net_cdevsw = { 233 .d_version = D_VERSION, 234 .d_flags = D_NEEDGIANT, 235 .d_open = netopen, 236 .d_close = netclose, 237 .d_ioctl = netioctl, 238 .d_name = "net", 239 .d_kqfilter = netkqfilter, 240}; 241 242static int 243netopen(struct cdev *dev, int flag, int mode, struct thread *td) 244{ 245 return (0); 246} 247 248static int 249netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 250{ 251 return (0); 252} 253 254static int 255netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 256{ 257 struct ifnet *ifp; 258 int error, idx; 259 260 /* only support interface specific ioctls */ 261 if (IOCGROUP(cmd) != 'i') 262 return (EOPNOTSUPP); 263 idx = dev2unit(dev); 264 if (idx == 0) { 265 /* 266 * special network device, not interface. 267 */ 268 if (cmd == SIOCGIFCONF) 269 return (ifconf(cmd, data)); /* XXX remove cmd */ 270#ifdef __amd64__ 271 if (cmd == SIOCGIFCONF32) 272 return (ifconf(cmd, data)); /* XXX remove cmd */ 273#endif 274 return (EOPNOTSUPP); 275 } 276 277 ifp = ifnet_byindex(idx); 278 if (ifp == NULL) 279 return (ENXIO); 280 281 error = ifhwioctl(cmd, ifp, data, td); 282 if (error == ENOIOCTL) 283 error = EOPNOTSUPP; 284 return (error); 285} 286 287static int 288netkqfilter(struct cdev *dev, struct knote *kn) 289{ 290 INIT_VNET_NET(curvnet); 291 struct knlist *klist; 292 struct ifnet *ifp; 293 int idx; 294 295 switch (kn->kn_filter) { 296 case EVFILT_NETDEV: 297 kn->kn_fop = &netdev_filtops; 298 break; 299 default: 300 return (EINVAL); 301 } 302 303 idx = dev2unit(dev); 304 if (idx == 0) { 305 klist = &V_ifklist; 306 } else { 307 ifp = ifnet_byindex(idx); 308 if (ifp == NULL) 309 return (1); 310 klist = &ifp->if_klist; 311 } 312 313 kn->kn_hook = (caddr_t)klist; 314 315 knlist_add(klist, kn, 0); 316 317 return (0); 318} 319 320static void 321filt_netdetach(struct knote *kn) 322{ 323 struct knlist *klist = (struct knlist *)kn->kn_hook; 324 325 knlist_remove(klist, kn, 0); 326} 327 328static int 329filt_netdev(struct knote *kn, long hint) 330{ 331 struct knlist *klist = (struct knlist *)kn->kn_hook; 332 333 /* 334 * Currently NOTE_EXIT is abused to indicate device detach. 335 */ 336 if (hint == NOTE_EXIT) { 337 kn->kn_data = NOTE_LINKINV; 338 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 339 knlist_remove_inevent(klist, kn); 340 return (1); 341 } 342 if (hint != 0) 343 kn->kn_data = hint; /* current status */ 344 if (kn->kn_sfflags & hint) 345 kn->kn_fflags |= hint; 346 return (kn->kn_fflags != 0); 347} 348 349/* 350 * Network interface utility routines. 351 * 352 * Routines with ifa_ifwith* names take sockaddr *'s as 353 * parameters. 354 */ 355 356/* ARGSUSED*/ 357static void 358if_init(void *dummy __unused) 359{ 360 INIT_VNET_NET(curvnet); 361 362 V_if_index = 0; 363 V_ifindex_table = NULL; 364 V_if_indexlim = 8; 365 366 IFNET_LOCK_INIT(); 367 TAILQ_INIT(&V_ifnet); 368 TAILQ_INIT(&V_ifg_head); 369 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL); 370 if_grow(); /* create initial table */ 371 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL, 372 0600, "network")); 373 if_clone_init(); 374} 375 376static void 377if_grow(void) 378{ 379 INIT_VNET_NET(curvnet); 380 u_int n; 381 struct ifindex_entry *e; 382 383 V_if_indexlim <<= 1; 384 n = V_if_indexlim * sizeof(*e); 385 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 386 if (V_ifindex_table != NULL) { 387 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 388 free((caddr_t)V_ifindex_table, M_IFNET); 389 } 390 V_ifindex_table = e; 391} 392 393/* 394 * Allocate a struct ifnet and an index for an interface. A layer 2 395 * common structure will also be allocated if an allocation routine is 396 * registered for the passed type. 397 */ 398struct ifnet* 399if_alloc(u_char type) 400{ 401 INIT_VNET_NET(curvnet); 402 struct ifnet *ifp; 403 404 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 405 406 /* 407 * Try to find an empty slot below if_index. If we fail, take 408 * the next slot. 409 * 410 * XXX: should be locked! 411 */ 412 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 413 if (ifnet_byindex(ifp->if_index) == NULL) 414 break; 415 } 416 /* Catch if_index overflow. */ 417 if (ifp->if_index < 1) { 418 free(ifp, M_IFNET); 419 return (NULL); 420 } 421 if (ifp->if_index > V_if_index) 422 V_if_index = ifp->if_index; 423 if (V_if_index >= V_if_indexlim) 424 if_grow(); 425 426 ifp->if_type = type; 427 428 if (if_com_alloc[type] != NULL) { 429 ifp->if_l2com = if_com_alloc[type](type, ifp); 430 if (ifp->if_l2com == NULL) { 431 free(ifp, M_IFNET); 432 return (NULL); 433 } 434 } 435 IFNET_WLOCK(); 436 ifnet_setbyindex(ifp->if_index, ifp); 437 IFNET_WUNLOCK(); 438 IF_ADDR_LOCK_INIT(ifp); 439 440 return (ifp); 441} 442 443/* 444 * Free the struct ifnet, the associated index, and the layer 2 common 445 * structure if needed. All the work is done in if_free_type(). 446 * 447 * Do not add code to this function! Add it to if_free_type(). 448 */ 449void 450if_free(struct ifnet *ifp) 451{ 452 453 if_free_type(ifp, ifp->if_type); 454} 455 456/* 457 * Do the actual work of freeing a struct ifnet, associated index, and 458 * layer 2 common structure. This version should only be called by 459 * intefaces that switch their type after calling if_alloc(). 460 */ 461void 462if_free_type(struct ifnet *ifp, u_char type) 463{ 464 INIT_VNET_NET(curvnet); /* ifp->if_vnet can be NULL here ! */ 465 466 if (ifp != ifnet_byindex(ifp->if_index)) { 467 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 468 __func__); 469 return; 470 } 471 472 IFNET_WLOCK(); 473 ifnet_setbyindex(ifp->if_index, NULL); 474 475 /* XXX: should be locked with if_findindex() */ 476 while (V_if_index > 0 && ifnet_byindex(V_if_index) == NULL) 477 V_if_index--; 478 IFNET_WUNLOCK(); 479 480 if (if_com_free[type] != NULL) 481 if_com_free[type](ifp->if_l2com, type); 482 483 IF_ADDR_LOCK_DESTROY(ifp); 484 free(ifp, M_IFNET); 485}; 486 487void 488ifq_attach(struct ifaltq *ifq, struct ifnet *ifp) 489{ 490 491 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 492 493 if (ifq->ifq_maxlen == 0) 494 ifq->ifq_maxlen = ifqmaxlen; 495 496 ifq->altq_type = 0; 497 ifq->altq_disc = NULL; 498 ifq->altq_flags &= ALTQF_CANTCHANGE; 499 ifq->altq_tbr = NULL; 500 ifq->altq_ifp = ifp; 501} 502 503void 504ifq_detach(struct ifaltq *ifq) 505{ 506 mtx_destroy(&ifq->ifq_mtx); 507} 508 509/* 510 * Perform generic interface initalization tasks and attach the interface 511 * to the list of "active" interfaces. 512 * 513 * XXX: 514 * - The decision to return void and thus require this function to 515 * succeed is questionable. 516 * - We do more initialization here then is probably a good idea. 517 * Some of this should probably move to if_alloc(). 518 * - We should probably do more sanity checking. For instance we don't 519 * do anything to insure if_xname is unique or non-empty. 520 */ 521void 522if_attach(struct ifnet *ifp) 523{ 524 INIT_VNET_NET(curvnet); 525 unsigned socksize, ifasize; 526 int namelen, masklen; 527 struct sockaddr_dl *sdl; 528 struct ifaddr *ifa; 529 530 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 531 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 532 ifp->if_xname); 533 534 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 535 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 536 IF_AFDATA_LOCK_INIT(ifp); 537 ifp->if_afdata_initialized = 0; 538 539 TAILQ_INIT(&ifp->if_addrhead); 540 TAILQ_INIT(&ifp->if_prefixhead); 541 TAILQ_INIT(&ifp->if_multiaddrs); 542 TAILQ_INIT(&ifp->if_groups); 543 544 if_addgroup(ifp, IFG_ALL); 545 546 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 547 getmicrotime(&ifp->if_lastchange); 548 ifp->if_data.ifi_epoch = time_uptime; 549 ifp->if_data.ifi_datalen = sizeof(struct if_data); 550 ifp->if_transmit = if_transmit; 551 ifp->if_qflush = if_qflush; 552#ifdef MAC 553 mac_ifnet_init(ifp); 554 mac_ifnet_create(ifp); 555#endif 556 557 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw, 558 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s", 559 net_cdevsw.d_name, ifp->if_xname)); 560 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 561 net_cdevsw.d_name, ifp->if_index); 562 563 ifq_attach(&ifp->if_snd, ifp); 564 565 /* 566 * create a Link Level name for this device 567 */ 568 namelen = strlen(ifp->if_xname); 569 /* 570 * Always save enough space for any possiable name so we can do 571 * a rename in place later. 572 */ 573 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 574 socksize = masklen + ifp->if_addrlen; 575 if (socksize < sizeof(*sdl)) 576 socksize = sizeof(*sdl); 577 socksize = roundup2(socksize, sizeof(long)); 578 ifasize = sizeof(*ifa) + 2 * socksize; 579 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 580 IFA_LOCK_INIT(ifa); 581 sdl = (struct sockaddr_dl *)(ifa + 1); 582 sdl->sdl_len = socksize; 583 sdl->sdl_family = AF_LINK; 584 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 585 sdl->sdl_nlen = namelen; 586 sdl->sdl_index = ifp->if_index; 587 sdl->sdl_type = ifp->if_type; 588 ifp->if_addr = ifa; 589 ifa->ifa_ifp = ifp; 590 ifa->ifa_rtrequest = link_rtrequest; 591 ifa->ifa_addr = (struct sockaddr *)sdl; 592 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 593 ifa->ifa_netmask = (struct sockaddr *)sdl; 594 sdl->sdl_len = masklen; 595 while (namelen != 0) 596 sdl->sdl_data[--namelen] = 0xff; 597 ifa->ifa_refcnt = 1; 598 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 599 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 600 601 602 IFNET_WLOCK(); 603 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 604 IFNET_WUNLOCK(); 605 606 if (domain_init_status >= 2) 607 if_attachdomain1(ifp); 608 609 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 610 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 611 612 /* Announce the interface. */ 613 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 614 615 if (ifp->if_watchdog != NULL) 616 if_printf(ifp, 617 "WARNING: using obsoleted if_watchdog interface\n"); 618 if (ifp->if_flags & IFF_NEEDSGIANT) 619 if_printf(ifp, 620 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n"); 621} 622 623static void 624if_attachdomain(void *dummy) 625{ 626 INIT_VNET_NET(curvnet); 627 struct ifnet *ifp; 628 int s; 629 630 s = splnet(); 631 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 632 if_attachdomain1(ifp); 633 splx(s); 634} 635SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 636 if_attachdomain, NULL); 637 638static void 639if_attachdomain1(struct ifnet *ifp) 640{ 641 struct domain *dp; 642 int s; 643 644 s = splnet(); 645 646 /* 647 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 648 * cannot lock ifp->if_afdata initialization, entirely. 649 */ 650 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 651 splx(s); 652 return; 653 } 654 if (ifp->if_afdata_initialized >= domain_init_status) { 655 IF_AFDATA_UNLOCK(ifp); 656 splx(s); 657 printf("if_attachdomain called more than once on %s\n", 658 ifp->if_xname); 659 return; 660 } 661 ifp->if_afdata_initialized = domain_init_status; 662 IF_AFDATA_UNLOCK(ifp); 663 664 /* address family dependent data region */ 665 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 666 for (dp = domains; dp; dp = dp->dom_next) { 667 if (dp->dom_ifattach) 668 ifp->if_afdata[dp->dom_family] = 669 (*dp->dom_ifattach)(ifp); 670 } 671 672 splx(s); 673} 674 675/* 676 * Remove any unicast or broadcast network addresses from an interface. 677 */ 678void 679if_purgeaddrs(struct ifnet *ifp) 680{ 681 struct ifaddr *ifa, *next; 682 683 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 684 if (ifa->ifa_addr->sa_family == AF_LINK) 685 continue; 686#ifdef INET 687 /* XXX: Ugly!! ad hoc just for INET */ 688 if (ifa->ifa_addr->sa_family == AF_INET) { 689 struct ifaliasreq ifr; 690 691 bzero(&ifr, sizeof(ifr)); 692 ifr.ifra_addr = *ifa->ifa_addr; 693 if (ifa->ifa_dstaddr) 694 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 695 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 696 NULL) == 0) 697 continue; 698 } 699#endif /* INET */ 700#ifdef INET6 701 if (ifa->ifa_addr->sa_family == AF_INET6) { 702 in6_purgeaddr(ifa); 703 /* ifp_addrhead is already updated */ 704 continue; 705 } 706#endif /* INET6 */ 707 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 708 IFAFREE(ifa); 709 } 710} 711 712/* 713 * Remove any multicast network addresses from an interface. 714 */ 715void 716if_purgemaddrs(struct ifnet *ifp) 717{ 718 struct ifmultiaddr *ifma; 719 struct ifmultiaddr *next; 720 721 IF_ADDR_LOCK(ifp); 722 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 723 if_delmulti_locked(ifp, ifma, 1); 724 IF_ADDR_UNLOCK(ifp); 725} 726 727/* 728 * Detach an interface, removing it from the 729 * list of "active" interfaces. 730 * 731 * XXXRW: There are some significant questions about event ordering, and 732 * how to prevent things from starting to use the interface during detach. 733 */ 734void 735if_detach(struct ifnet *ifp) 736{ 737 INIT_VNET_NET(ifp->if_vnet); 738 struct ifaddr *ifa; 739 struct radix_node_head *rnh; 740 int s, i, j; 741 struct domain *dp; 742 struct ifnet *iter; 743 int found = 0; 744 745 IFNET_WLOCK(); 746 TAILQ_FOREACH(iter, &V_ifnet, if_link) 747 if (iter == ifp) { 748 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 749 found = 1; 750 break; 751 } 752 IFNET_WUNLOCK(); 753 if (!found) 754 return; 755 756 /* 757 * Remove/wait for pending events. 758 */ 759 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 760 761 /* 762 * Remove routes and flush queues. 763 */ 764 s = splnet(); 765 if_down(ifp); 766#ifdef ALTQ 767 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 768 altq_disable(&ifp->if_snd); 769 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 770 altq_detach(&ifp->if_snd); 771#endif 772 773 if_purgeaddrs(ifp); 774 775#ifdef INET 776 in_ifdetach(ifp); 777#endif 778 779#ifdef INET6 780 /* 781 * Remove all IPv6 kernel structs related to ifp. This should be done 782 * before removing routing entries below, since IPv6 interface direct 783 * routes are expected to be removed by the IPv6-specific kernel API. 784 * Otherwise, the kernel will detect some inconsistency and bark it. 785 */ 786 in6_ifdetach(ifp); 787#endif 788 if_purgemaddrs(ifp); 789 790 /* 791 * Remove link ifaddr pointer and maybe decrement if_index. 792 * Clean up all addresses. 793 */ 794 ifp->if_addr = NULL; 795 destroy_dev(ifdev_byindex(ifp->if_index)); 796 ifdev_setbyindex(ifp->if_index, NULL); 797 798 /* We can now free link ifaddr. */ 799 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 800 ifa = TAILQ_FIRST(&ifp->if_addrhead); 801 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 802 IFAFREE(ifa); 803 } 804 805 /* 806 * Delete all remaining routes using this interface 807 * Unfortuneatly the only way to do this is to slog through 808 * the entire routing table looking for routes which point 809 * to this interface...oh well... 810 */ 811 for (i = 1; i <= AF_MAX; i++) { 812 for (j = 0; j < rt_numfibs; j++) { 813 if ((rnh = V_rt_tables[j][i]) == NULL) 814 continue; 815 RADIX_NODE_HEAD_LOCK(rnh); 816 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 817 RADIX_NODE_HEAD_UNLOCK(rnh); 818 } 819 } 820 821 /* Announce that the interface is gone. */ 822 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 823 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 824 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 825 826 IF_AFDATA_LOCK(ifp); 827 for (dp = domains; dp; dp = dp->dom_next) { 828 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 829 (*dp->dom_ifdetach)(ifp, 830 ifp->if_afdata[dp->dom_family]); 831 } 832 IF_AFDATA_UNLOCK(ifp); 833 834#ifdef MAC 835 mac_ifnet_destroy(ifp); 836#endif /* MAC */ 837 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 838 knlist_clear(&ifp->if_klist, 0); 839 knlist_destroy(&ifp->if_klist); 840 ifq_detach(&ifp->if_snd); 841 IF_AFDATA_DESTROY(ifp); 842 splx(s); 843} 844 845/* 846 * Add a group to an interface 847 */ 848int 849if_addgroup(struct ifnet *ifp, const char *groupname) 850{ 851 INIT_VNET_NET(ifp->if_vnet); 852 struct ifg_list *ifgl; 853 struct ifg_group *ifg = NULL; 854 struct ifg_member *ifgm; 855 856 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 857 groupname[strlen(groupname) - 1] <= '9') 858 return (EINVAL); 859 860 IFNET_WLOCK(); 861 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 862 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 863 IFNET_WUNLOCK(); 864 return (EEXIST); 865 } 866 867 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 868 M_NOWAIT)) == NULL) { 869 IFNET_WUNLOCK(); 870 return (ENOMEM); 871 } 872 873 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 874 M_TEMP, M_NOWAIT)) == NULL) { 875 free(ifgl, M_TEMP); 876 IFNET_WUNLOCK(); 877 return (ENOMEM); 878 } 879 880 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 881 if (!strcmp(ifg->ifg_group, groupname)) 882 break; 883 884 if (ifg == NULL) { 885 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 886 M_TEMP, M_NOWAIT)) == NULL) { 887 free(ifgl, M_TEMP); 888 free(ifgm, M_TEMP); 889 IFNET_WUNLOCK(); 890 return (ENOMEM); 891 } 892 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 893 ifg->ifg_refcnt = 0; 894 TAILQ_INIT(&ifg->ifg_members); 895 EVENTHANDLER_INVOKE(group_attach_event, ifg); 896 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 897 } 898 899 ifg->ifg_refcnt++; 900 ifgl->ifgl_group = ifg; 901 ifgm->ifgm_ifp = ifp; 902 903 IF_ADDR_LOCK(ifp); 904 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 905 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 906 IF_ADDR_UNLOCK(ifp); 907 908 IFNET_WUNLOCK(); 909 910 EVENTHANDLER_INVOKE(group_change_event, groupname); 911 912 return (0); 913} 914 915/* 916 * Remove a group from an interface 917 */ 918int 919if_delgroup(struct ifnet *ifp, const char *groupname) 920{ 921 INIT_VNET_NET(ifp->if_vnet); 922 struct ifg_list *ifgl; 923 struct ifg_member *ifgm; 924 925 IFNET_WLOCK(); 926 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 927 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 928 break; 929 if (ifgl == NULL) { 930 IFNET_WUNLOCK(); 931 return (ENOENT); 932 } 933 934 IF_ADDR_LOCK(ifp); 935 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 936 IF_ADDR_UNLOCK(ifp); 937 938 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 939 if (ifgm->ifgm_ifp == ifp) 940 break; 941 942 if (ifgm != NULL) { 943 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 944 free(ifgm, M_TEMP); 945 } 946 947 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 948 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 949 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 950 free(ifgl->ifgl_group, M_TEMP); 951 } 952 IFNET_WUNLOCK(); 953 954 free(ifgl, M_TEMP); 955 956 EVENTHANDLER_INVOKE(group_change_event, groupname); 957 958 return (0); 959} 960 961/* 962 * Stores all groups from an interface in memory pointed 963 * to by data 964 */ 965static int 966if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 967{ 968 int len, error; 969 struct ifg_list *ifgl; 970 struct ifg_req ifgrq, *ifgp; 971 struct ifgroupreq *ifgr = data; 972 973 if (ifgr->ifgr_len == 0) { 974 IF_ADDR_LOCK(ifp); 975 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 976 ifgr->ifgr_len += sizeof(struct ifg_req); 977 IF_ADDR_UNLOCK(ifp); 978 return (0); 979 } 980 981 len = ifgr->ifgr_len; 982 ifgp = ifgr->ifgr_groups; 983 /* XXX: wire */ 984 IF_ADDR_LOCK(ifp); 985 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 986 if (len < sizeof(ifgrq)) { 987 IF_ADDR_UNLOCK(ifp); 988 return (EINVAL); 989 } 990 bzero(&ifgrq, sizeof ifgrq); 991 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 992 sizeof(ifgrq.ifgrq_group)); 993 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 994 IF_ADDR_UNLOCK(ifp); 995 return (error); 996 } 997 len -= sizeof(ifgrq); 998 ifgp++; 999 } 1000 IF_ADDR_UNLOCK(ifp); 1001 1002 return (0); 1003} 1004 1005/* 1006 * Stores all members of a group in memory pointed to by data 1007 */ 1008static int 1009if_getgroupmembers(struct ifgroupreq *data) 1010{ 1011 INIT_VNET_NET(curvnet); 1012 struct ifgroupreq *ifgr = data; 1013 struct ifg_group *ifg; 1014 struct ifg_member *ifgm; 1015 struct ifg_req ifgrq, *ifgp; 1016 int len, error; 1017 1018 IFNET_RLOCK(); 1019 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1020 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1021 break; 1022 if (ifg == NULL) { 1023 IFNET_RUNLOCK(); 1024 return (ENOENT); 1025 } 1026 1027 if (ifgr->ifgr_len == 0) { 1028 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1029 ifgr->ifgr_len += sizeof(ifgrq); 1030 IFNET_RUNLOCK(); 1031 return (0); 1032 } 1033 1034 len = ifgr->ifgr_len; 1035 ifgp = ifgr->ifgr_groups; 1036 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1037 if (len < sizeof(ifgrq)) { 1038 IFNET_RUNLOCK(); 1039 return (EINVAL); 1040 } 1041 bzero(&ifgrq, sizeof ifgrq); 1042 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1043 sizeof(ifgrq.ifgrq_member)); 1044 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1045 IFNET_RUNLOCK(); 1046 return (error); 1047 } 1048 len -= sizeof(ifgrq); 1049 ifgp++; 1050 } 1051 IFNET_RUNLOCK(); 1052 1053 return (0); 1054} 1055 1056/* 1057 * Delete Routes for a Network Interface 1058 * 1059 * Called for each routing entry via the rnh->rnh_walktree() call above 1060 * to delete all route entries referencing a detaching network interface. 1061 * 1062 * Arguments: 1063 * rn pointer to node in the routing table 1064 * arg argument passed to rnh->rnh_walktree() - detaching interface 1065 * 1066 * Returns: 1067 * 0 successful 1068 * errno failed - reason indicated 1069 * 1070 */ 1071static int 1072if_rtdel(struct radix_node *rn, void *arg) 1073{ 1074 struct rtentry *rt = (struct rtentry *)rn; 1075 struct ifnet *ifp = arg; 1076 int err; 1077 1078 if (rt->rt_ifp == ifp) { 1079 1080 /* 1081 * Protect (sorta) against walktree recursion problems 1082 * with cloned routes 1083 */ 1084 if ((rt->rt_flags & RTF_UP) == 0) 1085 return (0); 1086 1087 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1088 rt_mask(rt), rt->rt_flags, 1089 (struct rtentry **) NULL, rt->rt_fibnum); 1090 if (err) { 1091 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1092 } 1093 } 1094 1095 return (0); 1096} 1097 1098/* 1099 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1100 * structs used to represent other address families, it is necessary 1101 * to perform a different comparison. 1102 */ 1103 1104#define sa_equal(a1, a2) \ 1105 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1106 1107#define sa_dl_equal(a1, a2) \ 1108 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1109 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1110 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1111 LLADDR((struct sockaddr_dl *)(a2)), \ 1112 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1113 1114/* 1115 * Locate an interface based on a complete address. 1116 */ 1117/*ARGSUSED*/ 1118struct ifaddr * 1119ifa_ifwithaddr(struct sockaddr *addr) 1120{ 1121 INIT_VNET_NET(curvnet); 1122 struct ifnet *ifp; 1123 struct ifaddr *ifa; 1124 1125 IFNET_RLOCK(); 1126 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1127 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1128 if (ifa->ifa_addr->sa_family != addr->sa_family) 1129 continue; 1130 if (sa_equal(addr, ifa->ifa_addr)) 1131 goto done; 1132 /* IP6 doesn't have broadcast */ 1133 if ((ifp->if_flags & IFF_BROADCAST) && 1134 ifa->ifa_broadaddr && 1135 ifa->ifa_broadaddr->sa_len != 0 && 1136 sa_equal(ifa->ifa_broadaddr, addr)) 1137 goto done; 1138 } 1139 ifa = NULL; 1140done: 1141 IFNET_RUNLOCK(); 1142 return (ifa); 1143} 1144 1145/* 1146 * Locate an interface based on the broadcast address. 1147 */ 1148/* ARGSUSED */ 1149struct ifaddr * 1150ifa_ifwithbroadaddr(struct sockaddr *addr) 1151{ 1152 INIT_VNET_NET(curvnet); 1153 struct ifnet *ifp; 1154 struct ifaddr *ifa; 1155 1156 IFNET_RLOCK(); 1157 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1158 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1159 if (ifa->ifa_addr->sa_family != addr->sa_family) 1160 continue; 1161 if ((ifp->if_flags & IFF_BROADCAST) && 1162 ifa->ifa_broadaddr && 1163 ifa->ifa_broadaddr->sa_len != 0 && 1164 sa_equal(ifa->ifa_broadaddr, addr)) 1165 goto done; 1166 } 1167 ifa = NULL; 1168done: 1169 IFNET_RUNLOCK(); 1170 return (ifa); 1171} 1172 1173/* 1174 * Locate the point to point interface with a given destination address. 1175 */ 1176/*ARGSUSED*/ 1177struct ifaddr * 1178ifa_ifwithdstaddr(struct sockaddr *addr) 1179{ 1180 INIT_VNET_NET(curvnet); 1181 struct ifnet *ifp; 1182 struct ifaddr *ifa; 1183 1184 IFNET_RLOCK(); 1185 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1186 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1187 continue; 1188 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1189 if (ifa->ifa_addr->sa_family != addr->sa_family) 1190 continue; 1191 if (ifa->ifa_dstaddr != NULL && 1192 sa_equal(addr, ifa->ifa_dstaddr)) 1193 goto done; 1194 } 1195 } 1196 ifa = NULL; 1197done: 1198 IFNET_RUNLOCK(); 1199 return (ifa); 1200} 1201 1202/* 1203 * Find an interface on a specific network. If many, choice 1204 * is most specific found. 1205 */ 1206struct ifaddr * 1207ifa_ifwithnet(struct sockaddr *addr) 1208{ 1209 INIT_VNET_NET(curvnet); 1210 struct ifnet *ifp; 1211 struct ifaddr *ifa; 1212 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1213 u_int af = addr->sa_family; 1214 char *addr_data = addr->sa_data, *cplim; 1215 1216 /* 1217 * AF_LINK addresses can be looked up directly by their index number, 1218 * so do that if we can. 1219 */ 1220 if (af == AF_LINK) { 1221 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1222 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1223 return (ifaddr_byindex(sdl->sdl_index)); 1224 } 1225 1226 /* 1227 * Scan though each interface, looking for ones that have 1228 * addresses in this address family. 1229 */ 1230 IFNET_RLOCK(); 1231 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1232 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1233 char *cp, *cp2, *cp3; 1234 1235 if (ifa->ifa_addr->sa_family != af) 1236next: continue; 1237 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1238 /* 1239 * This is a bit broken as it doesn't 1240 * take into account that the remote end may 1241 * be a single node in the network we are 1242 * looking for. 1243 * The trouble is that we don't know the 1244 * netmask for the remote end. 1245 */ 1246 if (ifa->ifa_dstaddr != NULL && 1247 sa_equal(addr, ifa->ifa_dstaddr)) 1248 goto done; 1249 } else { 1250 /* 1251 * if we have a special address handler, 1252 * then use it instead of the generic one. 1253 */ 1254 if (ifa->ifa_claim_addr) { 1255 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1256 goto done; 1257 continue; 1258 } 1259 1260 /* 1261 * Scan all the bits in the ifa's address. 1262 * If a bit dissagrees with what we are 1263 * looking for, mask it with the netmask 1264 * to see if it really matters. 1265 * (A byte at a time) 1266 */ 1267 if (ifa->ifa_netmask == 0) 1268 continue; 1269 cp = addr_data; 1270 cp2 = ifa->ifa_addr->sa_data; 1271 cp3 = ifa->ifa_netmask->sa_data; 1272 cplim = ifa->ifa_netmask->sa_len 1273 + (char *)ifa->ifa_netmask; 1274 while (cp3 < cplim) 1275 if ((*cp++ ^ *cp2++) & *cp3++) 1276 goto next; /* next address! */ 1277 /* 1278 * If the netmask of what we just found 1279 * is more specific than what we had before 1280 * (if we had one) then remember the new one 1281 * before continuing to search 1282 * for an even better one. 1283 */ 1284 if (ifa_maybe == 0 || 1285 rn_refines((caddr_t)ifa->ifa_netmask, 1286 (caddr_t)ifa_maybe->ifa_netmask)) 1287 ifa_maybe = ifa; 1288 } 1289 } 1290 } 1291 ifa = ifa_maybe; 1292done: 1293 IFNET_RUNLOCK(); 1294 return (ifa); 1295} 1296 1297/* 1298 * Find an interface address specific to an interface best matching 1299 * a given address. 1300 */ 1301struct ifaddr * 1302ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1303{ 1304 struct ifaddr *ifa; 1305 char *cp, *cp2, *cp3; 1306 char *cplim; 1307 struct ifaddr *ifa_maybe = 0; 1308 u_int af = addr->sa_family; 1309 1310 if (af >= AF_MAX) 1311 return (0); 1312 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1313 if (ifa->ifa_addr->sa_family != af) 1314 continue; 1315 if (ifa_maybe == 0) 1316 ifa_maybe = ifa; 1317 if (ifa->ifa_netmask == 0) { 1318 if (sa_equal(addr, ifa->ifa_addr) || 1319 (ifa->ifa_dstaddr && 1320 sa_equal(addr, ifa->ifa_dstaddr))) 1321 goto done; 1322 continue; 1323 } 1324 if (ifp->if_flags & IFF_POINTOPOINT) { 1325 if (sa_equal(addr, ifa->ifa_dstaddr)) 1326 goto done; 1327 } else { 1328 cp = addr->sa_data; 1329 cp2 = ifa->ifa_addr->sa_data; 1330 cp3 = ifa->ifa_netmask->sa_data; 1331 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1332 for (; cp3 < cplim; cp3++) 1333 if ((*cp++ ^ *cp2++) & *cp3) 1334 break; 1335 if (cp3 == cplim) 1336 goto done; 1337 } 1338 } 1339 ifa = ifa_maybe; 1340done: 1341 return (ifa); 1342} 1343 1344#include <net/route.h> 1345 1346/* 1347 * Default action when installing a route with a Link Level gateway. 1348 * Lookup an appropriate real ifa to point to. 1349 * This should be moved to /sys/net/link.c eventually. 1350 */ 1351static void 1352link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1353{ 1354 struct ifaddr *ifa, *oifa; 1355 struct sockaddr *dst; 1356 struct ifnet *ifp; 1357 1358 RT_LOCK_ASSERT(rt); 1359 1360 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1361 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1362 return; 1363 ifa = ifaof_ifpforaddr(dst, ifp); 1364 if (ifa) { 1365 IFAREF(ifa); /* XXX */ 1366 oifa = rt->rt_ifa; 1367 rt->rt_ifa = ifa; 1368 IFAFREE(oifa); 1369 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1370 ifa->ifa_rtrequest(cmd, rt, info); 1371 } 1372} 1373 1374/* 1375 * Mark an interface down and notify protocols of 1376 * the transition. 1377 * NOTE: must be called at splnet or eqivalent. 1378 */ 1379static void 1380if_unroute(struct ifnet *ifp, int flag, int fam) 1381{ 1382 struct ifaddr *ifa; 1383 1384 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1385 1386 ifp->if_flags &= ~flag; 1387 getmicrotime(&ifp->if_lastchange); 1388 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1389 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1390 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1391 ifp->if_qflush(ifp); 1392 1393#ifdef DEV_CARP 1394 if (ifp->if_carp) 1395 carp_carpdev_state(ifp->if_carp); 1396#endif 1397 rt_ifmsg(ifp); 1398} 1399 1400/* 1401 * Mark an interface up and notify protocols of 1402 * the transition. 1403 * NOTE: must be called at splnet or eqivalent. 1404 */ 1405static void 1406if_route(struct ifnet *ifp, int flag, int fam) 1407{ 1408 struct ifaddr *ifa; 1409 1410 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1411 1412 ifp->if_flags |= flag; 1413 getmicrotime(&ifp->if_lastchange); 1414 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1415 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1416 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1417#ifdef DEV_CARP 1418 if (ifp->if_carp) 1419 carp_carpdev_state(ifp->if_carp); 1420#endif 1421 rt_ifmsg(ifp); 1422#ifdef INET6 1423 in6_if_up(ifp); 1424#endif 1425} 1426 1427void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1428void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1429 1430/* 1431 * Handle a change in the interface link state. To avoid LORs 1432 * between driver lock and upper layer locks, as well as possible 1433 * recursions, we post event to taskqueue, and all job 1434 * is done in static do_link_state_change(). 1435 */ 1436void 1437if_link_state_change(struct ifnet *ifp, int link_state) 1438{ 1439 /* Return if state hasn't changed. */ 1440 if (ifp->if_link_state == link_state) 1441 return; 1442 1443 ifp->if_link_state = link_state; 1444 1445 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1446} 1447 1448static void 1449do_link_state_change(void *arg, int pending) 1450{ 1451 struct ifnet *ifp = (struct ifnet *)arg; 1452 int link_state = ifp->if_link_state; 1453 int link; 1454 CURVNET_SET(ifp->if_vnet); 1455 1456 /* Notify that the link state has changed. */ 1457 rt_ifmsg(ifp); 1458 if (link_state == LINK_STATE_UP) 1459 link = NOTE_LINKUP; 1460 else if (link_state == LINK_STATE_DOWN) 1461 link = NOTE_LINKDOWN; 1462 else 1463 link = NOTE_LINKINV; 1464 KNOTE_UNLOCKED(&ifp->if_klist, link); 1465 if (ifp->if_vlantrunk != NULL) 1466 (*vlan_link_state_p)(ifp, link); 1467 1468 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1469 IFP2AC(ifp)->ac_netgraph != NULL) 1470 (*ng_ether_link_state_p)(ifp, link_state); 1471#ifdef DEV_CARP 1472 if (ifp->if_carp) 1473 carp_carpdev_state(ifp->if_carp); 1474#endif 1475 if (ifp->if_bridge) { 1476 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1477 (*bstp_linkstate_p)(ifp, link_state); 1478 } 1479 if (ifp->if_lagg) { 1480 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1481 (*lagg_linkstate_p)(ifp, link_state); 1482 } 1483 1484 devctl_notify("IFNET", ifp->if_xname, 1485 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1486 if (pending > 1) 1487 if_printf(ifp, "%d link states coalesced\n", pending); 1488 if (log_link_state_change) 1489 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1490 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1491 CURVNET_RESTORE(); 1492} 1493 1494/* 1495 * Mark an interface down and notify protocols of 1496 * the transition. 1497 * NOTE: must be called at splnet or eqivalent. 1498 */ 1499void 1500if_down(struct ifnet *ifp) 1501{ 1502 1503 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1504} 1505 1506/* 1507 * Mark an interface up and notify protocols of 1508 * the transition. 1509 * NOTE: must be called at splnet or eqivalent. 1510 */ 1511void 1512if_up(struct ifnet *ifp) 1513{ 1514 1515 if_route(ifp, IFF_UP, AF_UNSPEC); 1516} 1517 1518/* 1519 * Flush an interface queue. 1520 */ 1521static void 1522if_qflush(struct ifnet *ifp) 1523{ 1524 struct mbuf *m, *n; 1525 struct ifaltq *ifq; 1526 1527 ifq = &ifp->if_snd; 1528 IFQ_LOCK(ifq); 1529#ifdef ALTQ 1530 if (ALTQ_IS_ENABLED(ifq)) 1531 ALTQ_PURGE(ifq); 1532#endif 1533 n = ifq->ifq_head; 1534 while ((m = n) != 0) { 1535 n = m->m_act; 1536 m_freem(m); 1537 } 1538 ifq->ifq_head = 0; 1539 ifq->ifq_tail = 0; 1540 ifq->ifq_len = 0; 1541 IFQ_UNLOCK(ifq); 1542} 1543 1544/* 1545 * Handle interface watchdog timer routines. Called 1546 * from softclock, we decrement timers (if set) and 1547 * call the appropriate interface routine on expiration. 1548 * 1549 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1550 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1551 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1552 */ 1553static void 1554if_slowtimo(void *arg) 1555{ 1556 VNET_ITERATOR_DECL(vnet_iter); 1557 struct ifnet *ifp; 1558 int s = splimp(); 1559 1560 IFNET_RLOCK(); 1561 VNET_LIST_RLOCK(); 1562 VNET_FOREACH(vnet_iter) { 1563 CURVNET_SET(vnet_iter); 1564 INIT_VNET_NET(vnet_iter); 1565 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1566 if (ifp->if_timer == 0 || --ifp->if_timer) 1567 continue; 1568 if (ifp->if_watchdog) 1569 (*ifp->if_watchdog)(ifp); 1570 } 1571 CURVNET_RESTORE(); 1572 } 1573 VNET_LIST_RUNLOCK(); 1574 IFNET_RUNLOCK(); 1575 splx(s); 1576 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1577} 1578 1579/* 1580 * Map interface name to 1581 * interface structure pointer. 1582 */ 1583struct ifnet * 1584ifunit(const char *name) 1585{ 1586 INIT_VNET_NET(curvnet); 1587 struct ifnet *ifp; 1588 1589 IFNET_RLOCK(); 1590 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1591 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1592 break; 1593 } 1594 IFNET_RUNLOCK(); 1595 return (ifp); 1596} 1597 1598/* 1599 * Hardware specific interface ioctls. 1600 */ 1601static int 1602ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1603{ 1604 struct ifreq *ifr; 1605 struct ifstat *ifs; 1606 int error = 0; 1607 int new_flags, temp_flags; 1608 size_t namelen, onamelen; 1609 char new_name[IFNAMSIZ]; 1610 struct ifaddr *ifa; 1611 struct sockaddr_dl *sdl; 1612 1613 ifr = (struct ifreq *)data; 1614 switch (cmd) { 1615 case SIOCGIFINDEX: 1616 ifr->ifr_index = ifp->if_index; 1617 break; 1618 1619 case SIOCGIFFLAGS: 1620 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1621 ifr->ifr_flags = temp_flags & 0xffff; 1622 ifr->ifr_flagshigh = temp_flags >> 16; 1623 break; 1624 1625 case SIOCGIFCAP: 1626 ifr->ifr_reqcap = ifp->if_capabilities; 1627 ifr->ifr_curcap = ifp->if_capenable; 1628 break; 1629 1630#ifdef MAC 1631 case SIOCGIFMAC: 1632 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 1633 break; 1634#endif 1635 1636 case SIOCGIFMETRIC: 1637 ifr->ifr_metric = ifp->if_metric; 1638 break; 1639 1640 case SIOCGIFMTU: 1641 ifr->ifr_mtu = ifp->if_mtu; 1642 break; 1643 1644 case SIOCGIFPHYS: 1645 ifr->ifr_phys = ifp->if_physical; 1646 break; 1647 1648 case SIOCSIFFLAGS: 1649 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1650 if (error) 1651 return (error); 1652 /* 1653 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1654 * check, so we don't need special handling here yet. 1655 */ 1656 new_flags = (ifr->ifr_flags & 0xffff) | 1657 (ifr->ifr_flagshigh << 16); 1658 if (ifp->if_flags & IFF_SMART) { 1659 /* Smart drivers twiddle their own routes */ 1660 } else if (ifp->if_flags & IFF_UP && 1661 (new_flags & IFF_UP) == 0) { 1662 int s = splimp(); 1663 if_down(ifp); 1664 splx(s); 1665 } else if (new_flags & IFF_UP && 1666 (ifp->if_flags & IFF_UP) == 0) { 1667 int s = splimp(); 1668 if_up(ifp); 1669 splx(s); 1670 } 1671 /* See if permanently promiscuous mode bit is about to flip */ 1672 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1673 if (new_flags & IFF_PPROMISC) 1674 ifp->if_flags |= IFF_PROMISC; 1675 else if (ifp->if_pcount == 0) 1676 ifp->if_flags &= ~IFF_PROMISC; 1677 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1678 ifp->if_xname, 1679 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1680 } 1681 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1682 (new_flags &~ IFF_CANTCHANGE); 1683 if (ifp->if_ioctl) { 1684 IFF_LOCKGIANT(ifp); 1685 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1686 IFF_UNLOCKGIANT(ifp); 1687 } 1688 getmicrotime(&ifp->if_lastchange); 1689 break; 1690 1691 case SIOCSIFCAP: 1692 error = priv_check(td, PRIV_NET_SETIFCAP); 1693 if (error) 1694 return (error); 1695 if (ifp->if_ioctl == NULL) 1696 return (EOPNOTSUPP); 1697 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1698 return (EINVAL); 1699 IFF_LOCKGIANT(ifp); 1700 error = (*ifp->if_ioctl)(ifp, cmd, data); 1701 IFF_UNLOCKGIANT(ifp); 1702 if (error == 0) 1703 getmicrotime(&ifp->if_lastchange); 1704 break; 1705 1706#ifdef MAC 1707 case SIOCSIFMAC: 1708 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 1709 break; 1710#endif 1711 1712 case SIOCSIFNAME: 1713 error = priv_check(td, PRIV_NET_SETIFNAME); 1714 if (error) 1715 return (error); 1716 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1717 if (error != 0) 1718 return (error); 1719 if (new_name[0] == '\0') 1720 return (EINVAL); 1721 if (ifunit(new_name) != NULL) 1722 return (EEXIST); 1723 1724 /* Announce the departure of the interface. */ 1725 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1726 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1727 1728 log(LOG_INFO, "%s: changing name to '%s'\n", 1729 ifp->if_xname, new_name); 1730 1731 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1732 ifa = ifp->if_addr; 1733 IFA_LOCK(ifa); 1734 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1735 namelen = strlen(new_name); 1736 onamelen = sdl->sdl_nlen; 1737 /* 1738 * Move the address if needed. This is safe because we 1739 * allocate space for a name of length IFNAMSIZ when we 1740 * create this in if_attach(). 1741 */ 1742 if (namelen != onamelen) { 1743 bcopy(sdl->sdl_data + onamelen, 1744 sdl->sdl_data + namelen, sdl->sdl_alen); 1745 } 1746 bcopy(new_name, sdl->sdl_data, namelen); 1747 sdl->sdl_nlen = namelen; 1748 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1749 bzero(sdl->sdl_data, onamelen); 1750 while (namelen != 0) 1751 sdl->sdl_data[--namelen] = 0xff; 1752 IFA_UNLOCK(ifa); 1753 1754 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1755 /* Announce the return of the interface. */ 1756 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1757 break; 1758 1759 case SIOCSIFMETRIC: 1760 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1761 if (error) 1762 return (error); 1763 ifp->if_metric = ifr->ifr_metric; 1764 getmicrotime(&ifp->if_lastchange); 1765 break; 1766 1767 case SIOCSIFPHYS: 1768 error = priv_check(td, PRIV_NET_SETIFPHYS); 1769 if (error) 1770 return (error); 1771 if (ifp->if_ioctl == NULL) 1772 return (EOPNOTSUPP); 1773 IFF_LOCKGIANT(ifp); 1774 error = (*ifp->if_ioctl)(ifp, cmd, data); 1775 IFF_UNLOCKGIANT(ifp); 1776 if (error == 0) 1777 getmicrotime(&ifp->if_lastchange); 1778 break; 1779 1780 case SIOCSIFMTU: 1781 { 1782 u_long oldmtu = ifp->if_mtu; 1783 1784 error = priv_check(td, PRIV_NET_SETIFMTU); 1785 if (error) 1786 return (error); 1787 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1788 return (EINVAL); 1789 if (ifp->if_ioctl == NULL) 1790 return (EOPNOTSUPP); 1791 IFF_LOCKGIANT(ifp); 1792 error = (*ifp->if_ioctl)(ifp, cmd, data); 1793 IFF_UNLOCKGIANT(ifp); 1794 if (error == 0) { 1795 getmicrotime(&ifp->if_lastchange); 1796 rt_ifmsg(ifp); 1797 } 1798 /* 1799 * If the link MTU changed, do network layer specific procedure. 1800 */ 1801 if (ifp->if_mtu != oldmtu) { 1802#ifdef INET6 1803 nd6_setmtu(ifp); 1804#endif 1805 } 1806 break; 1807 } 1808 1809 case SIOCADDMULTI: 1810 case SIOCDELMULTI: 1811 if (cmd == SIOCADDMULTI) 1812 error = priv_check(td, PRIV_NET_ADDMULTI); 1813 else 1814 error = priv_check(td, PRIV_NET_DELMULTI); 1815 if (error) 1816 return (error); 1817 1818 /* Don't allow group membership on non-multicast interfaces. */ 1819 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1820 return (EOPNOTSUPP); 1821 1822 /* Don't let users screw up protocols' entries. */ 1823 if (ifr->ifr_addr.sa_family != AF_LINK) 1824 return (EINVAL); 1825 1826 if (cmd == SIOCADDMULTI) { 1827 struct ifmultiaddr *ifma; 1828 1829 /* 1830 * Userland is only permitted to join groups once 1831 * via the if_addmulti() KPI, because it cannot hold 1832 * struct ifmultiaddr * between calls. It may also 1833 * lose a race while we check if the membership 1834 * already exists. 1835 */ 1836 IF_ADDR_LOCK(ifp); 1837 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1838 IF_ADDR_UNLOCK(ifp); 1839 if (ifma != NULL) 1840 error = EADDRINUSE; 1841 else 1842 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1843 } else { 1844 error = if_delmulti(ifp, &ifr->ifr_addr); 1845 } 1846 if (error == 0) 1847 getmicrotime(&ifp->if_lastchange); 1848 break; 1849 1850 case SIOCSIFPHYADDR: 1851 case SIOCDIFPHYADDR: 1852#ifdef INET6 1853 case SIOCSIFPHYADDR_IN6: 1854#endif 1855 case SIOCSLIFPHYADDR: 1856 case SIOCSIFMEDIA: 1857 case SIOCSIFGENERIC: 1858 error = priv_check(td, PRIV_NET_HWIOCTL); 1859 if (error) 1860 return (error); 1861 if (ifp->if_ioctl == NULL) 1862 return (EOPNOTSUPP); 1863 IFF_LOCKGIANT(ifp); 1864 error = (*ifp->if_ioctl)(ifp, cmd, data); 1865 IFF_UNLOCKGIANT(ifp); 1866 if (error == 0) 1867 getmicrotime(&ifp->if_lastchange); 1868 break; 1869 1870 case SIOCGIFSTATUS: 1871 ifs = (struct ifstat *)data; 1872 ifs->ascii[0] = '\0'; 1873 1874 case SIOCGIFPSRCADDR: 1875 case SIOCGIFPDSTADDR: 1876 case SIOCGLIFPHYADDR: 1877 case SIOCGIFMEDIA: 1878 case SIOCGIFGENERIC: 1879 if (ifp->if_ioctl == NULL) 1880 return (EOPNOTSUPP); 1881 IFF_LOCKGIANT(ifp); 1882 error = (*ifp->if_ioctl)(ifp, cmd, data); 1883 IFF_UNLOCKGIANT(ifp); 1884 break; 1885 1886 case SIOCSIFLLADDR: 1887 error = priv_check(td, PRIV_NET_SETLLADDR); 1888 if (error) 1889 return (error); 1890 error = if_setlladdr(ifp, 1891 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1892 break; 1893 1894 case SIOCAIFGROUP: 1895 { 1896 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1897 1898 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1899 if (error) 1900 return (error); 1901 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1902 return (error); 1903 break; 1904 } 1905 1906 case SIOCGIFGROUP: 1907 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1908 return (error); 1909 break; 1910 1911 case SIOCDIFGROUP: 1912 { 1913 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1914 1915 error = priv_check(td, PRIV_NET_DELIFGROUP); 1916 if (error) 1917 return (error); 1918 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1919 return (error); 1920 break; 1921 } 1922 1923 default: 1924 error = ENOIOCTL; 1925 break; 1926 } 1927 return (error); 1928} 1929 1930/* 1931 * Interface ioctls. 1932 */ 1933int 1934ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1935{ 1936 struct ifnet *ifp; 1937 struct ifreq *ifr; 1938 int error; 1939 int oif_flags; 1940 1941 switch (cmd) { 1942 case SIOCGIFCONF: 1943 case OSIOCGIFCONF: 1944#ifdef __amd64__ 1945 case SIOCGIFCONF32: 1946#endif 1947 return (ifconf(cmd, data)); 1948 } 1949 ifr = (struct ifreq *)data; 1950 1951 switch (cmd) { 1952 case SIOCIFCREATE: 1953 case SIOCIFCREATE2: 1954 error = priv_check(td, PRIV_NET_IFCREATE); 1955 if (error) 1956 return (error); 1957 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1958 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1959 case SIOCIFDESTROY: 1960 error = priv_check(td, PRIV_NET_IFDESTROY); 1961 if (error) 1962 return (error); 1963 return if_clone_destroy(ifr->ifr_name); 1964 1965 case SIOCIFGCLONERS: 1966 return (if_clone_list((struct if_clonereq *)data)); 1967 case SIOCGIFGMEMB: 1968 return (if_getgroupmembers((struct ifgroupreq *)data)); 1969 } 1970 1971 ifp = ifunit(ifr->ifr_name); 1972 if (ifp == 0) 1973 return (ENXIO); 1974 1975 error = ifhwioctl(cmd, ifp, data, td); 1976 if (error != ENOIOCTL) 1977 return (error); 1978 1979 oif_flags = ifp->if_flags; 1980 if (so->so_proto == 0) 1981 return (EOPNOTSUPP); 1982#ifndef COMPAT_43 1983 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1984 data, 1985 ifp, td)); 1986#else 1987 { 1988 int ocmd = cmd; 1989 1990 switch (cmd) { 1991 1992 case SIOCSIFDSTADDR: 1993 case SIOCSIFADDR: 1994 case SIOCSIFBRDADDR: 1995 case SIOCSIFNETMASK: 1996#if BYTE_ORDER != BIG_ENDIAN 1997 if (ifr->ifr_addr.sa_family == 0 && 1998 ifr->ifr_addr.sa_len < 16) { 1999 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2000 ifr->ifr_addr.sa_len = 16; 2001 } 2002#else 2003 if (ifr->ifr_addr.sa_len == 0) 2004 ifr->ifr_addr.sa_len = 16; 2005#endif 2006 break; 2007 2008 case OSIOCGIFADDR: 2009 cmd = SIOCGIFADDR; 2010 break; 2011 2012 case OSIOCGIFDSTADDR: 2013 cmd = SIOCGIFDSTADDR; 2014 break; 2015 2016 case OSIOCGIFBRDADDR: 2017 cmd = SIOCGIFBRDADDR; 2018 break; 2019 2020 case OSIOCGIFNETMASK: 2021 cmd = SIOCGIFNETMASK; 2022 } 2023 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 2024 cmd, 2025 data, 2026 ifp, td)); 2027 switch (ocmd) { 2028 2029 case OSIOCGIFADDR: 2030 case OSIOCGIFDSTADDR: 2031 case OSIOCGIFBRDADDR: 2032 case OSIOCGIFNETMASK: 2033 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 2034 2035 } 2036 } 2037#endif /* COMPAT_43 */ 2038 2039 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2040#ifdef INET6 2041 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 2042 if (ifp->if_flags & IFF_UP) { 2043 int s = splimp(); 2044 in6_if_up(ifp); 2045 splx(s); 2046 } 2047#endif 2048 } 2049 return (error); 2050} 2051 2052/* 2053 * The code common to handling reference counted flags, 2054 * e.g., in ifpromisc() and if_allmulti(). 2055 * The "pflag" argument can specify a permanent mode flag to check, 2056 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2057 * 2058 * Only to be used on stack-owned flags, not driver-owned flags. 2059 */ 2060static int 2061if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2062{ 2063 struct ifreq ifr; 2064 int error; 2065 int oldflags, oldcount; 2066 2067 /* Sanity checks to catch programming errors */ 2068 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2069 ("%s: setting driver-owned flag %d", __func__, flag)); 2070 2071 if (onswitch) 2072 KASSERT(*refcount >= 0, 2073 ("%s: increment negative refcount %d for flag %d", 2074 __func__, *refcount, flag)); 2075 else 2076 KASSERT(*refcount > 0, 2077 ("%s: decrement non-positive refcount %d for flag %d", 2078 __func__, *refcount, flag)); 2079 2080 /* In case this mode is permanent, just touch refcount */ 2081 if (ifp->if_flags & pflag) { 2082 *refcount += onswitch ? 1 : -1; 2083 return (0); 2084 } 2085 2086 /* Save ifnet parameters for if_ioctl() may fail */ 2087 oldcount = *refcount; 2088 oldflags = ifp->if_flags; 2089 2090 /* 2091 * See if we aren't the only and touching refcount is enough. 2092 * Actually toggle interface flag if we are the first or last. 2093 */ 2094 if (onswitch) { 2095 if ((*refcount)++) 2096 return (0); 2097 ifp->if_flags |= flag; 2098 } else { 2099 if (--(*refcount)) 2100 return (0); 2101 ifp->if_flags &= ~flag; 2102 } 2103 2104 /* Call down the driver since we've changed interface flags */ 2105 if (ifp->if_ioctl == NULL) { 2106 error = EOPNOTSUPP; 2107 goto recover; 2108 } 2109 ifr.ifr_flags = ifp->if_flags & 0xffff; 2110 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2111 IFF_LOCKGIANT(ifp); 2112 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2113 IFF_UNLOCKGIANT(ifp); 2114 if (error) 2115 goto recover; 2116 /* Notify userland that interface flags have changed */ 2117 rt_ifmsg(ifp); 2118 return (0); 2119 2120recover: 2121 /* Recover after driver error */ 2122 *refcount = oldcount; 2123 ifp->if_flags = oldflags; 2124 return (error); 2125} 2126 2127/* 2128 * Set/clear promiscuous mode on interface ifp based on the truth value 2129 * of pswitch. The calls are reference counted so that only the first 2130 * "on" request actually has an effect, as does the final "off" request. 2131 * Results are undefined if the "off" and "on" requests are not matched. 2132 */ 2133int 2134ifpromisc(struct ifnet *ifp, int pswitch) 2135{ 2136 int error; 2137 int oldflags = ifp->if_flags; 2138 2139 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2140 &ifp->if_pcount, pswitch); 2141 /* If promiscuous mode status has changed, log a message */ 2142 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2143 log(LOG_INFO, "%s: promiscuous mode %s\n", 2144 ifp->if_xname, 2145 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2146 return (error); 2147} 2148 2149/* 2150 * Return interface configuration 2151 * of system. List may be used 2152 * in later ioctl's (above) to get 2153 * other information. 2154 */ 2155/*ARGSUSED*/ 2156static int 2157ifconf(u_long cmd, caddr_t data) 2158{ 2159 INIT_VNET_NET(curvnet); 2160 struct ifconf *ifc = (struct ifconf *)data; 2161#ifdef __amd64__ 2162 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2163 struct ifconf ifc_swab; 2164#endif 2165 struct ifnet *ifp; 2166 struct ifaddr *ifa; 2167 struct ifreq ifr; 2168 struct sbuf *sb; 2169 int error, full = 0, valid_len, max_len; 2170 2171#ifdef __amd64__ 2172 if (cmd == SIOCGIFCONF32) { 2173 ifc_swab.ifc_len = ifc32->ifc_len; 2174 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2175 ifc = &ifc_swab; 2176 } 2177#endif 2178 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2179 max_len = MAXPHYS - 1; 2180 2181 /* Prevent hostile input from being able to crash the system */ 2182 if (ifc->ifc_len <= 0) 2183 return (EINVAL); 2184 2185again: 2186 if (ifc->ifc_len <= max_len) { 2187 max_len = ifc->ifc_len; 2188 full = 1; 2189 } 2190 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2191 max_len = 0; 2192 valid_len = 0; 2193 2194 IFNET_RLOCK(); /* could sleep XXX */ 2195 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2196 int addrs; 2197 2198 /* 2199 * Zero the ifr_name buffer to make sure we don't 2200 * disclose the contents of the stack. 2201 */ 2202 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2203 2204 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2205 >= sizeof(ifr.ifr_name)) { 2206 sbuf_delete(sb); 2207 IFNET_RUNLOCK(); 2208 return (ENAMETOOLONG); 2209 } 2210 2211 addrs = 0; 2212 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2213 struct sockaddr *sa = ifa->ifa_addr; 2214 2215 if (jailed(curthread->td_ucred) && 2216 !prison_if(curthread->td_ucred, sa)) 2217 continue; 2218 addrs++; 2219#ifdef COMPAT_43 2220 if (cmd == OSIOCGIFCONF) { 2221 struct osockaddr *osa = 2222 (struct osockaddr *)&ifr.ifr_addr; 2223 ifr.ifr_addr = *sa; 2224 osa->sa_family = sa->sa_family; 2225 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2226 max_len += sizeof(ifr); 2227 } else 2228#endif 2229 if (sa->sa_len <= sizeof(*sa)) { 2230 ifr.ifr_addr = *sa; 2231 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2232 max_len += sizeof(ifr); 2233 } else { 2234 sbuf_bcat(sb, &ifr, 2235 offsetof(struct ifreq, ifr_addr)); 2236 max_len += offsetof(struct ifreq, ifr_addr); 2237 sbuf_bcat(sb, sa, sa->sa_len); 2238 max_len += sa->sa_len; 2239 } 2240 2241 if (!sbuf_overflowed(sb)) 2242 valid_len = sbuf_len(sb); 2243 } 2244 if (addrs == 0) { 2245 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2246 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2247 max_len += sizeof(ifr); 2248 2249 if (!sbuf_overflowed(sb)) 2250 valid_len = sbuf_len(sb); 2251 } 2252 } 2253 IFNET_RUNLOCK(); 2254 2255 /* 2256 * If we didn't allocate enough space (uncommon), try again. If 2257 * we have already allocated as much space as we are allowed, 2258 * return what we've got. 2259 */ 2260 if (valid_len != max_len && !full) { 2261 sbuf_delete(sb); 2262 goto again; 2263 } 2264 2265 ifc->ifc_len = valid_len; 2266#ifdef __amd64__ 2267 if (cmd == SIOCGIFCONF32) 2268 ifc32->ifc_len = valid_len; 2269#endif 2270 sbuf_finish(sb); 2271 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2272 sbuf_delete(sb); 2273 return (error); 2274} 2275 2276/* 2277 * Just like ifpromisc(), but for all-multicast-reception mode. 2278 */ 2279int 2280if_allmulti(struct ifnet *ifp, int onswitch) 2281{ 2282 2283 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2284} 2285 2286struct ifmultiaddr * 2287if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2288{ 2289 struct ifmultiaddr *ifma; 2290 2291 IF_ADDR_LOCK_ASSERT(ifp); 2292 2293 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2294 if (sa->sa_family == AF_LINK) { 2295 if (sa_dl_equal(ifma->ifma_addr, sa)) 2296 break; 2297 } else { 2298 if (sa_equal(ifma->ifma_addr, sa)) 2299 break; 2300 } 2301 } 2302 2303 return ifma; 2304} 2305 2306/* 2307 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2308 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2309 * the ifnet multicast address list here, so the caller must do that and 2310 * other setup work (such as notifying the device driver). The reference 2311 * count is initialized to 1. 2312 */ 2313static struct ifmultiaddr * 2314if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2315 int mflags) 2316{ 2317 struct ifmultiaddr *ifma; 2318 struct sockaddr *dupsa; 2319 2320 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2321 M_ZERO); 2322 if (ifma == NULL) 2323 return (NULL); 2324 2325 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2326 if (dupsa == NULL) { 2327 free(ifma, M_IFMADDR); 2328 return (NULL); 2329 } 2330 bcopy(sa, dupsa, sa->sa_len); 2331 ifma->ifma_addr = dupsa; 2332 2333 ifma->ifma_ifp = ifp; 2334 ifma->ifma_refcount = 1; 2335 ifma->ifma_protospec = NULL; 2336 2337 if (llsa == NULL) { 2338 ifma->ifma_lladdr = NULL; 2339 return (ifma); 2340 } 2341 2342 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2343 if (dupsa == NULL) { 2344 free(ifma->ifma_addr, M_IFMADDR); 2345 free(ifma, M_IFMADDR); 2346 return (NULL); 2347 } 2348 bcopy(llsa, dupsa, llsa->sa_len); 2349 ifma->ifma_lladdr = dupsa; 2350 2351 return (ifma); 2352} 2353 2354/* 2355 * if_freemulti: free ifmultiaddr structure and possibly attached related 2356 * addresses. The caller is responsible for implementing reference 2357 * counting, notifying the driver, handling routing messages, and releasing 2358 * any dependent link layer state. 2359 */ 2360static void 2361if_freemulti(struct ifmultiaddr *ifma) 2362{ 2363 2364 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2365 ifma->ifma_refcount)); 2366 KASSERT(ifma->ifma_protospec == NULL, 2367 ("if_freemulti: protospec not NULL")); 2368 2369 if (ifma->ifma_lladdr != NULL) 2370 free(ifma->ifma_lladdr, M_IFMADDR); 2371 free(ifma->ifma_addr, M_IFMADDR); 2372 free(ifma, M_IFMADDR); 2373} 2374 2375/* 2376 * Register an additional multicast address with a network interface. 2377 * 2378 * - If the address is already present, bump the reference count on the 2379 * address and return. 2380 * - If the address is not link-layer, look up a link layer address. 2381 * - Allocate address structures for one or both addresses, and attach to the 2382 * multicast address list on the interface. If automatically adding a link 2383 * layer address, the protocol address will own a reference to the link 2384 * layer address, to be freed when it is freed. 2385 * - Notify the network device driver of an addition to the multicast address 2386 * list. 2387 * 2388 * 'sa' points to caller-owned memory with the desired multicast address. 2389 * 2390 * 'retifma' will be used to return a pointer to the resulting multicast 2391 * address reference, if desired. 2392 */ 2393int 2394if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2395 struct ifmultiaddr **retifma) 2396{ 2397 struct ifmultiaddr *ifma, *ll_ifma; 2398 struct sockaddr *llsa; 2399 int error; 2400 2401 /* 2402 * If the address is already present, return a new reference to it; 2403 * otherwise, allocate storage and set up a new address. 2404 */ 2405 IF_ADDR_LOCK(ifp); 2406 ifma = if_findmulti(ifp, sa); 2407 if (ifma != NULL) { 2408 ifma->ifma_refcount++; 2409 if (retifma != NULL) 2410 *retifma = ifma; 2411 IF_ADDR_UNLOCK(ifp); 2412 return (0); 2413 } 2414 2415 /* 2416 * The address isn't already present; resolve the protocol address 2417 * into a link layer address, and then look that up, bump its 2418 * refcount or allocate an ifma for that also. If 'llsa' was 2419 * returned, we will need to free it later. 2420 */ 2421 llsa = NULL; 2422 ll_ifma = NULL; 2423 if (ifp->if_resolvemulti != NULL) { 2424 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2425 if (error) 2426 goto unlock_out; 2427 } 2428 2429 /* 2430 * Allocate the new address. Don't hook it up yet, as we may also 2431 * need to allocate a link layer multicast address. 2432 */ 2433 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2434 if (ifma == NULL) { 2435 error = ENOMEM; 2436 goto free_llsa_out; 2437 } 2438 2439 /* 2440 * If a link layer address is found, we'll need to see if it's 2441 * already present in the address list, or allocate is as well. 2442 * When this block finishes, the link layer address will be on the 2443 * list. 2444 */ 2445 if (llsa != NULL) { 2446 ll_ifma = if_findmulti(ifp, llsa); 2447 if (ll_ifma == NULL) { 2448 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2449 if (ll_ifma == NULL) { 2450 --ifma->ifma_refcount; 2451 if_freemulti(ifma); 2452 error = ENOMEM; 2453 goto free_llsa_out; 2454 } 2455 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2456 ifma_link); 2457 } else 2458 ll_ifma->ifma_refcount++; 2459 ifma->ifma_llifma = ll_ifma; 2460 } 2461 2462 /* 2463 * We now have a new multicast address, ifma, and possibly a new or 2464 * referenced link layer address. Add the primary address to the 2465 * ifnet address list. 2466 */ 2467 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2468 2469 if (retifma != NULL) 2470 *retifma = ifma; 2471 2472 /* 2473 * Must generate the message while holding the lock so that 'ifma' 2474 * pointer is still valid. 2475 */ 2476 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2477 IF_ADDR_UNLOCK(ifp); 2478 2479 /* 2480 * We are certain we have added something, so call down to the 2481 * interface to let them know about it. 2482 */ 2483 if (ifp->if_ioctl != NULL) { 2484 IFF_LOCKGIANT(ifp); 2485 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2486 IFF_UNLOCKGIANT(ifp); 2487 } 2488 2489 if (llsa != NULL) 2490 free(llsa, M_IFMADDR); 2491 2492 return (0); 2493 2494free_llsa_out: 2495 if (llsa != NULL) 2496 free(llsa, M_IFMADDR); 2497 2498unlock_out: 2499 IF_ADDR_UNLOCK(ifp); 2500 return (error); 2501} 2502 2503/* 2504 * Delete a multicast group membership by network-layer group address. 2505 * 2506 * Returns ENOENT if the entry could not be found. If ifp no longer 2507 * exists, results are undefined. This entry point should only be used 2508 * from subsystems which do appropriate locking to hold ifp for the 2509 * duration of the call. 2510 * Network-layer protocol domains must use if_delmulti_ifma(). 2511 */ 2512int 2513if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2514{ 2515 struct ifmultiaddr *ifma; 2516 int lastref; 2517#ifdef INVARIANTS 2518 struct ifnet *oifp; 2519 INIT_VNET_NET(ifp->if_vnet); 2520 2521 IFNET_RLOCK(); 2522 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2523 if (ifp == oifp) 2524 break; 2525 if (ifp != oifp) 2526 ifp = NULL; 2527 IFNET_RUNLOCK(); 2528 2529 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2530#endif 2531 if (ifp == NULL) 2532 return (ENOENT); 2533 2534 IF_ADDR_LOCK(ifp); 2535 lastref = 0; 2536 ifma = if_findmulti(ifp, sa); 2537 if (ifma != NULL) 2538 lastref = if_delmulti_locked(ifp, ifma, 0); 2539 IF_ADDR_UNLOCK(ifp); 2540 2541 if (ifma == NULL) 2542 return (ENOENT); 2543 2544 if (lastref && ifp->if_ioctl != NULL) { 2545 IFF_LOCKGIANT(ifp); 2546 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2547 IFF_UNLOCKGIANT(ifp); 2548 } 2549 2550 return (0); 2551} 2552 2553/* 2554 * Delete a multicast group membership by group membership pointer. 2555 * Network-layer protocol domains must use this routine. 2556 * 2557 * It is safe to call this routine if the ifp disappeared. Callers should 2558 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2559 * reconfigured. 2560 */ 2561void 2562if_delmulti_ifma(struct ifmultiaddr *ifma) 2563{ 2564#ifdef DIAGNOSTIC 2565 INIT_VNET_NET(curvnet); 2566#endif 2567 struct ifnet *ifp; 2568 int lastref; 2569 2570 ifp = ifma->ifma_ifp; 2571#ifdef DIAGNOSTIC 2572 if (ifp == NULL) { 2573 printf("%s: ifma_ifp seems to be detached\n", __func__); 2574 } else { 2575 struct ifnet *oifp; 2576 2577 IFNET_RLOCK(); 2578 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2579 if (ifp == oifp) 2580 break; 2581 if (ifp != oifp) { 2582 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2583 ifp = NULL; 2584 } 2585 IFNET_RUNLOCK(); 2586 } 2587#endif 2588 /* 2589 * If and only if the ifnet instance exists: Acquire the address lock. 2590 */ 2591 if (ifp != NULL) 2592 IF_ADDR_LOCK(ifp); 2593 2594 lastref = if_delmulti_locked(ifp, ifma, 0); 2595 2596 if (ifp != NULL) { 2597 /* 2598 * If and only if the ifnet instance exists: 2599 * Release the address lock. 2600 * If the group was left: update the hardware hash filter. 2601 */ 2602 IF_ADDR_UNLOCK(ifp); 2603 if (lastref && ifp->if_ioctl != NULL) { 2604 IFF_LOCKGIANT(ifp); 2605 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2606 IFF_UNLOCKGIANT(ifp); 2607 } 2608 } 2609} 2610 2611/* 2612 * Perform deletion of network-layer and/or link-layer multicast address. 2613 * 2614 * Return 0 if the reference count was decremented. 2615 * Return 1 if the final reference was released, indicating that the 2616 * hardware hash filter should be reprogrammed. 2617 */ 2618static int 2619if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2620{ 2621 struct ifmultiaddr *ll_ifma; 2622 2623 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2624 KASSERT(ifma->ifma_ifp == ifp, 2625 ("%s: inconsistent ifp %p", __func__, ifp)); 2626 IF_ADDR_LOCK_ASSERT(ifp); 2627 } 2628 2629 ifp = ifma->ifma_ifp; 2630 2631 /* 2632 * If the ifnet is detaching, null out references to ifnet, 2633 * so that upper protocol layers will notice, and not attempt 2634 * to obtain locks for an ifnet which no longer exists. The 2635 * routing socket announcement must happen before the ifnet 2636 * instance is detached from the system. 2637 */ 2638 if (detaching) { 2639#ifdef DIAGNOSTIC 2640 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2641#endif 2642 /* 2643 * ifp may already be nulled out if we are being reentered 2644 * to delete the ll_ifma. 2645 */ 2646 if (ifp != NULL) { 2647 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2648 ifma->ifma_ifp = NULL; 2649 } 2650 } 2651 2652 if (--ifma->ifma_refcount > 0) 2653 return 0; 2654 2655 /* 2656 * If this ifma is a network-layer ifma, a link-layer ifma may 2657 * have been associated with it. Release it first if so. 2658 */ 2659 ll_ifma = ifma->ifma_llifma; 2660 if (ll_ifma != NULL) { 2661 KASSERT(ifma->ifma_lladdr != NULL, 2662 ("%s: llifma w/o lladdr", __func__)); 2663 if (detaching) 2664 ll_ifma->ifma_ifp = NULL; /* XXX */ 2665 if (--ll_ifma->ifma_refcount == 0) { 2666 if (ifp != NULL) { 2667 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2668 ifma_link); 2669 } 2670 if_freemulti(ll_ifma); 2671 } 2672 } 2673 2674 if (ifp != NULL) 2675 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2676 2677 if_freemulti(ifma); 2678 2679 /* 2680 * The last reference to this instance of struct ifmultiaddr 2681 * was released; the hardware should be notified of this change. 2682 */ 2683 return 1; 2684} 2685 2686/* 2687 * Set the link layer address on an interface. 2688 * 2689 * At this time we only support certain types of interfaces, 2690 * and we don't allow the length of the address to change. 2691 */ 2692int 2693if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2694{ 2695 struct sockaddr_dl *sdl; 2696 struct ifaddr *ifa; 2697 struct ifreq ifr; 2698 2699 ifa = ifp->if_addr; 2700 if (ifa == NULL) 2701 return (EINVAL); 2702 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2703 if (sdl == NULL) 2704 return (EINVAL); 2705 if (len != sdl->sdl_alen) /* don't allow length to change */ 2706 return (EINVAL); 2707 switch (ifp->if_type) { 2708 case IFT_ETHER: 2709 case IFT_FDDI: 2710 case IFT_XETHER: 2711 case IFT_ISO88025: 2712 case IFT_L2VLAN: 2713 case IFT_BRIDGE: 2714 case IFT_ARCNET: 2715 case IFT_IEEE8023ADLAG: 2716 bcopy(lladdr, LLADDR(sdl), len); 2717 break; 2718 default: 2719 return (ENODEV); 2720 } 2721 /* 2722 * If the interface is already up, we need 2723 * to re-init it in order to reprogram its 2724 * address filter. 2725 */ 2726 if ((ifp->if_flags & IFF_UP) != 0) { 2727 if (ifp->if_ioctl) { 2728 IFF_LOCKGIANT(ifp); 2729 ifp->if_flags &= ~IFF_UP; 2730 ifr.ifr_flags = ifp->if_flags & 0xffff; 2731 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2732 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2733 ifp->if_flags |= IFF_UP; 2734 ifr.ifr_flags = ifp->if_flags & 0xffff; 2735 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2736 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2737 IFF_UNLOCKGIANT(ifp); 2738 } 2739#ifdef INET 2740 /* 2741 * Also send gratuitous ARPs to notify other nodes about 2742 * the address change. 2743 */ 2744 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2745 if (ifa->ifa_addr->sa_family == AF_INET) 2746 arp_ifinit(ifp, ifa); 2747 } 2748#endif 2749 } 2750 return (0); 2751} 2752 2753/* 2754 * The name argument must be a pointer to storage which will last as 2755 * long as the interface does. For physical devices, the result of 2756 * device_get_name(dev) is a good choice and for pseudo-devices a 2757 * static string works well. 2758 */ 2759void 2760if_initname(struct ifnet *ifp, const char *name, int unit) 2761{ 2762 ifp->if_dname = name; 2763 ifp->if_dunit = unit; 2764 if (unit != IF_DUNIT_NONE) 2765 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2766 else 2767 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2768} 2769 2770int 2771if_printf(struct ifnet *ifp, const char * fmt, ...) 2772{ 2773 va_list ap; 2774 int retval; 2775 2776 retval = printf("%s: ", ifp->if_xname); 2777 va_start(ap, fmt); 2778 retval += vprintf(fmt, ap); 2779 va_end(ap); 2780 return (retval); 2781} 2782 2783/* 2784 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2785 * be called without Giant. However, we often can't acquire the Giant lock 2786 * at those points; instead, we run it via a task queue that holds Giant via 2787 * if_start_deferred. 2788 * 2789 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2790 * outstanding if_start_deferred() tasks that will run after the free. This 2791 * probably means waiting in if_detach(). 2792 */ 2793void 2794if_start(struct ifnet *ifp) 2795{ 2796 2797 if (ifp->if_flags & IFF_NEEDSGIANT) { 2798 if (mtx_owned(&Giant)) 2799 (*(ifp)->if_start)(ifp); 2800 else 2801 taskqueue_enqueue(taskqueue_swi_giant, 2802 &ifp->if_starttask); 2803 } else 2804 (*(ifp)->if_start)(ifp); 2805} 2806 2807static void 2808if_start_deferred(void *context, int pending) 2809{ 2810 struct ifnet *ifp; 2811 2812 GIANT_REQUIRED; 2813 2814 ifp = context; 2815 (ifp->if_start)(ifp); 2816} 2817 2818/* 2819 * Backwards compatibility interface for drivers 2820 * that have not implemented it 2821 */ 2822static int 2823if_transmit(struct ifnet *ifp, struct mbuf *m) 2824{ 2825 int error; 2826 2827 IFQ_HANDOFF(ifp, m, error); 2828 return (error); 2829} 2830 2831int 2832if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2833{ 2834 int active = 0; 2835 2836 IF_LOCK(ifq); 2837 if (_IF_QFULL(ifq)) { 2838 _IF_DROP(ifq); 2839 IF_UNLOCK(ifq); 2840 m_freem(m); 2841 return (0); 2842 } 2843 if (ifp != NULL) { 2844 ifp->if_obytes += m->m_pkthdr.len + adjust; 2845 if (m->m_flags & (M_BCAST|M_MCAST)) 2846 ifp->if_omcasts++; 2847 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2848 } 2849 _IF_ENQUEUE(ifq, m); 2850 IF_UNLOCK(ifq); 2851 if (ifp != NULL && !active) 2852 if_start(ifp); 2853 return (1); 2854} 2855 2856void 2857if_register_com_alloc(u_char type, 2858 if_com_alloc_t *a, if_com_free_t *f) 2859{ 2860 2861 KASSERT(if_com_alloc[type] == NULL, 2862 ("if_register_com_alloc: %d already registered", type)); 2863 KASSERT(if_com_free[type] == NULL, 2864 ("if_register_com_alloc: %d free already registered", type)); 2865 2866 if_com_alloc[type] = a; 2867 if_com_free[type] = f; 2868} 2869 2870void 2871if_deregister_com_alloc(u_char type) 2872{ 2873 2874 KASSERT(if_com_alloc[type] != NULL, 2875 ("if_deregister_com_alloc: %d not registered", type)); 2876 KASSERT(if_com_free[type] != NULL, 2877 ("if_deregister_com_alloc: %d free not registered", type)); 2878 if_com_alloc[type] = NULL; 2879 if_com_free[type] = NULL; 2880}
|