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}
|