1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)if.c 8.5 (Berkeley) 1/9/95 32 * $FreeBSD$ 33 */ 34 35#include "opt_bpf.h" 36#include "opt_inet6.h" 37#include "opt_inet.h" 38 39#include <sys/param.h> 40#include <sys/types.h> 41#include <sys/conf.h> 42#include <sys/malloc.h> 43#include <sys/sbuf.h> 44#include <sys/bus.h> 45#include <sys/epoch.h> 46#include <sys/mbuf.h> 47#include <sys/systm.h> 48#include <sys/priv.h> 49#include <sys/proc.h> 50#include <sys/socket.h> 51#include <sys/socketvar.h> 52#include <sys/protosw.h> 53#include <sys/kernel.h> 54#include <sys/lock.h> 55#include <sys/refcount.h> 56#include <sys/module.h> 57#include <sys/rwlock.h> 58#include <sys/sockio.h> 59#include <sys/syslog.h> 60#include <sys/sysctl.h> 61#include <sys/sysent.h> 62#include <sys/taskqueue.h> 63#include <sys/domain.h> 64#include <sys/jail.h> 65#include <sys/priv.h> 66#include <sys/sched.h> 67#include <sys/smp.h> 68 69#include <machine/stdarg.h> 70#include <vm/uma.h> 71 72#include <net/bpf.h> 73#include <net/ethernet.h> 74#include <net/if.h> 75#include <net/if_arp.h> 76#include <net/if_clone.h> 77#include <net/if_dl.h> 78#include <net/if_types.h> 79#include <net/if_var.h> 80#include <net/if_media.h> 81#include <net/if_vlan_var.h> 82#include <net/radix.h> 83#include <net/route.h> 84#include <net/vnet.h> 85 86#if defined(INET) || defined(INET6) 87#include <net/ethernet.h> 88#include <netinet/in.h> 89#include <netinet/in_var.h> 90#include <netinet/ip.h> 91#include <netinet/ip_carp.h> 92#ifdef INET 93#include <netinet/if_ether.h> 94#include <netinet/netdump/netdump.h> 95#endif /* INET */ 96#ifdef INET6 97#include <netinet6/in6_var.h> 98#include <netinet6/in6_ifattach.h> 99#endif /* INET6 */ 100#endif /* INET || INET6 */ 101 102#include <security/mac/mac_framework.h> 103 104/* 105 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name 106 * and ifr_ifru when it is used in SIOCGIFCONF. 107 */ 108_Static_assert(sizeof(((struct ifreq *)0)->ifr_name) == 109 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru"); 110 111__read_mostly epoch_t net_epoch_preempt; 112__read_mostly epoch_t net_epoch; 113#ifdef COMPAT_FREEBSD32 114#include <sys/mount.h> 115#include <compat/freebsd32/freebsd32.h> 116 117struct ifreq_buffer32 { 118 uint32_t length; /* (size_t) */ 119 uint32_t buffer; /* (void *) */ 120}; 121 122/* 123 * Interface request structure used for socket 124 * ioctl's. All interface ioctl's must have parameter 125 * definitions which begin with ifr_name. The 126 * remainder may be interface specific. 127 */ 128struct ifreq32 { 129 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 130 union { 131 struct sockaddr ifru_addr; 132 struct sockaddr ifru_dstaddr; 133 struct sockaddr ifru_broadaddr; 134 struct ifreq_buffer32 ifru_buffer; 135 short ifru_flags[2]; 136 short ifru_index; 137 int ifru_jid; 138 int ifru_metric; 139 int ifru_mtu; 140 int ifru_phys; 141 int ifru_media; 142 uint32_t ifru_data; 143 int ifru_cap[2]; 144 u_int ifru_fib; 145 u_char ifru_vlan_pcp; 146 } ifr_ifru; 147}; 148CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32)); 149CTASSERT(__offsetof(struct ifreq, ifr_ifru) == 150 __offsetof(struct ifreq32, ifr_ifru)); 151 152struct ifgroupreq32 { 153 char ifgr_name[IFNAMSIZ]; 154 u_int ifgr_len; 155 union { 156 char ifgru_group[IFNAMSIZ]; 157 uint32_t ifgru_groups; 158 } ifgr_ifgru; 159}; 160 161struct ifmediareq32 { 162 char ifm_name[IFNAMSIZ]; 163 int ifm_current; 164 int ifm_mask; 165 int ifm_status; 166 int ifm_active; 167 int ifm_count; 168 uint32_t ifm_ulist; /* (int *) */ 169}; 170#define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32) 171#define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32) 172 173#define _CASE_IOC_IFGROUPREQ_32(cmd) \ 174 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case 175#else /* !COMPAT_FREEBSD32 */ 176#define _CASE_IOC_IFGROUPREQ_32(cmd) 177#endif /* !COMPAT_FREEBSD32 */ 178 179#define CASE_IOC_IFGROUPREQ(cmd) \ 180 _CASE_IOC_IFGROUPREQ_32(cmd) \ 181 (cmd) 182 183union ifreq_union { 184 struct ifreq ifr; 185#ifdef COMPAT_FREEBSD32 186 struct ifreq32 ifr32; 187#endif 188}; 189 190union ifgroupreq_union { 191 struct ifgroupreq ifgr; 192#ifdef COMPAT_FREEBSD32 193 struct ifgroupreq32 ifgr32; 194#endif 195}; 196 197SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 198SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 199 200SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, 201 &ifqmaxlen, 0, "max send queue size"); 202 203/* Log link state change events */ 204static int log_link_state_change = 1; 205 206SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 207 &log_link_state_change, 0, 208 "log interface link state change events"); 209 210/* Log promiscuous mode change events */ 211static int log_promisc_mode_change = 1; 212 213SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN, 214 &log_promisc_mode_change, 1, 215 "log promiscuous mode change events"); 216 217/* Interface description */ 218static unsigned int ifdescr_maxlen = 1024; 219SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, 220 &ifdescr_maxlen, 0, 221 "administrative maximum length for interface description"); 222 223static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); 224 225/* global sx for non-critical path ifdescr */ 226static struct sx ifdescr_sx; 227SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); 228 229void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 230void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 231/* These are external hooks for CARP. */ 232void (*carp_linkstate_p)(struct ifnet *ifp); 233void (*carp_demote_adj_p)(int, char *); 234int (*carp_master_p)(struct ifaddr *); 235#if defined(INET) || defined(INET6) 236int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost); 237int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, 238 const struct sockaddr *sa); 239int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *); 240int (*carp_attach_p)(struct ifaddr *, int); 241void (*carp_detach_p)(struct ifaddr *, bool); 242#endif 243#ifdef INET 244int (*carp_iamatch_p)(struct ifaddr *, uint8_t **); 245#endif 246#ifdef INET6 247struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); 248caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, 249 const struct in6_addr *taddr); 250#endif 251 252struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 253 254/* 255 * XXX: Style; these should be sorted alphabetically, and unprototyped 256 * static functions should be prototyped. Currently they are sorted by 257 * declaration order. 258 */ 259static void if_attachdomain(void *); 260static void if_attachdomain1(struct ifnet *); 261static int ifconf(u_long, caddr_t); 262static void *if_grow(void); 263static void if_input_default(struct ifnet *, struct mbuf *); 264static int if_requestencap_default(struct ifnet *, struct if_encap_req *); 265static void if_route(struct ifnet *, int flag, int fam); 266static int if_setflag(struct ifnet *, int, int, int *, int); 267static int if_transmit(struct ifnet *ifp, struct mbuf *m); 268static void if_unroute(struct ifnet *, int flag, int fam); 269static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 270static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 271static void do_link_state_change(void *, int); 272static int if_getgroup(struct ifgroupreq *, struct ifnet *); 273static int if_getgroupmembers(struct ifgroupreq *); 274static void if_delgroups(struct ifnet *); 275static void if_attach_internal(struct ifnet *, int, struct if_clone *); 276static int if_detach_internal(struct ifnet *, int, struct if_clone **); 277static void if_link_ifnet(struct ifnet *); 278static bool if_unlink_ifnet(struct ifnet *, bool); 279#ifdef VIMAGE 280static void if_vmove(struct ifnet *, struct vnet *); 281#endif 282 283#ifdef INET6 284/* 285 * XXX: declare here to avoid to include many inet6 related files.. 286 * should be more generalized? 287 */ 288extern void nd6_setmtu(struct ifnet *); 289#endif 290 291/* ipsec helper hooks */ 292VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]); 293VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]); 294 295VNET_DEFINE(int, if_index); 296int ifqmaxlen = IFQ_MAXLEN; 297VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 298VNET_DEFINE(struct ifgrouphead, ifg_head); 299 300VNET_DEFINE_STATIC(int, if_indexlim) = 8; 301 302/* Table of ifnet by index. */ 303VNET_DEFINE(struct ifnet **, ifindex_table); 304 305#define V_if_indexlim VNET(if_indexlim) 306#define V_ifindex_table VNET(ifindex_table) 307 308/* 309 * The global network interface list (V_ifnet) and related state (such as 310 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock. 311 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH. 312 */ 313struct rwlock ifnet_rwlock; 314RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE); 315struct sx ifnet_sxlock; 316SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE); 317 318struct sx ifnet_detach_sxlock; 319SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx", 320 SX_RECURSE); 321 322/* 323 * The allocation of network interfaces is a rather non-atomic affair; we 324 * need to select an index before we are ready to expose the interface for 325 * use, so will use this pointer value to indicate reservation. 326 */ 327#define IFNET_HOLD (void *)(uintptr_t)(-1) 328 329static if_com_alloc_t *if_com_alloc[256]; 330static if_com_free_t *if_com_free[256]; 331 332static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 333MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 334MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 335 336struct ifnet * 337ifnet_byindex_locked(u_short idx) 338{ 339 340 if (idx > V_if_index) 341 return (NULL); 342 if (V_ifindex_table[idx] == IFNET_HOLD) 343 return (NULL); 344 return (V_ifindex_table[idx]); 345} 346 347struct ifnet * 348ifnet_byindex(u_short idx) 349{ 350 struct ifnet *ifp; 351 352 ifp = ifnet_byindex_locked(idx); 353 return (ifp); 354} 355 356struct ifnet * 357ifnet_byindex_ref(u_short idx) 358{ 359 struct ifnet *ifp; 360 361 IFNET_RLOCK_NOSLEEP(); 362 ifp = ifnet_byindex_locked(idx); 363 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 364 IFNET_RUNLOCK_NOSLEEP(); 365 return (NULL); 366 } 367 if_ref(ifp); 368 IFNET_RUNLOCK_NOSLEEP(); 369 return (ifp); 370} 371 372/* 373 * Allocate an ifindex array entry; return 0 on success or an error on 374 * failure. 375 */ 376static u_short 377ifindex_alloc(void **old) 378{ 379 u_short idx; 380 381 IFNET_WLOCK_ASSERT(); 382 /* 383 * Try to find an empty slot below V_if_index. If we fail, take the 384 * next slot. 385 */ 386 for (idx = 1; idx <= V_if_index; idx++) { 387 if (V_ifindex_table[idx] == NULL) 388 break; 389 } 390 391 /* Catch if_index overflow. */ 392 if (idx >= V_if_indexlim) { 393 *old = if_grow(); 394 return (USHRT_MAX); 395 } 396 if (idx > V_if_index) 397 V_if_index = idx; 398 return (idx); 399} 400 401static void 402ifindex_free_locked(u_short idx) 403{ 404 405 IFNET_WLOCK_ASSERT(); 406 407 V_ifindex_table[idx] = NULL; 408 while (V_if_index > 0 && 409 V_ifindex_table[V_if_index] == NULL) 410 V_if_index--; 411} 412 413static void 414ifindex_free(u_short idx) 415{ 416 417 IFNET_WLOCK(); 418 ifindex_free_locked(idx); 419 IFNET_WUNLOCK(); 420} 421 422static void 423ifnet_setbyindex(u_short idx, struct ifnet *ifp) 424{ 425 426 V_ifindex_table[idx] = ifp; 427} 428 429struct ifaddr * 430ifaddr_byindex(u_short idx) 431{ 432 struct ifnet *ifp; 433 struct ifaddr *ifa = NULL; 434 435 IFNET_RLOCK_NOSLEEP(); 436 ifp = ifnet_byindex_locked(idx); 437 if (ifp != NULL && (ifa = ifp->if_addr) != NULL) 438 ifa_ref(ifa); 439 IFNET_RUNLOCK_NOSLEEP(); 440 return (ifa); 441} 442 443/* 444 * Network interface utility routines. 445 * 446 * Routines with ifa_ifwith* names take sockaddr *'s as 447 * parameters. 448 */ 449 450static void 451vnet_if_init(const void *unused __unused) 452{ 453 void *old; 454 455 CK_STAILQ_INIT(&V_ifnet); 456 CK_STAILQ_INIT(&V_ifg_head); 457 IFNET_WLOCK(); 458 old = if_grow(); /* create initial table */ 459 IFNET_WUNLOCK(); 460 epoch_wait_preempt(net_epoch_preempt); 461 free(old, M_IFNET); 462 vnet_if_clone_init(); 463} 464VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, 465 NULL); 466 467#ifdef VIMAGE 468static void 469vnet_if_uninit(const void *unused __unused) 470{ 471 472 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " 473 "not empty", __func__, __LINE__, &V_ifnet)); 474 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " 475 "not empty", __func__, __LINE__, &V_ifg_head)); 476 477 free((caddr_t)V_ifindex_table, M_IFNET); 478} 479VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 480 vnet_if_uninit, NULL); 481#endif 482 483static void 484if_link_ifnet(struct ifnet *ifp) 485{ 486 487 IFNET_WLOCK(); 488 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 489#ifdef VIMAGE 490 curvnet->vnet_ifcnt++; 491#endif 492 IFNET_WUNLOCK(); 493} 494 495static bool 496if_unlink_ifnet(struct ifnet *ifp, bool vmove) 497{ 498 struct ifnet *iter; 499 int found = 0; 500 501 IFNET_WLOCK(); 502 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link) 503 if (iter == ifp) { 504 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link); 505 if (!vmove) 506 ifp->if_flags |= IFF_DYING; 507 found = 1; 508 break; 509 } 510#ifdef VIMAGE 511 curvnet->vnet_ifcnt--; 512#endif 513 IFNET_WUNLOCK(); 514 515 return (found); 516} 517 518#ifdef VIMAGE 519static void 520vnet_if_return(const void *unused __unused) 521{ 522 struct ifnet *ifp, *nifp; 523 struct ifnet **pending; 524 int found, i; 525 526 i = 0; 527 528 /* 529 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd 530 * enter NET_EPOCH, but that's not possible, because if_vmove() calls 531 * if_detach_internal(), which waits for NET_EPOCH callbacks to 532 * complete. We can't do that from within NET_EPOCH. 533 * 534 * However, we can also use the IFNET_xLOCK, which is the V_ifnet 535 * read/write lock. We cannot hold the lock as we call if_vmove() 536 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib 537 * ctx lock. 538 */ 539 IFNET_WLOCK(); 540 541 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt, 542 M_IFNET, M_WAITOK | M_ZERO); 543 544 /* Return all inherited interfaces to their parent vnets. */ 545 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) { 546 if (ifp->if_home_vnet != ifp->if_vnet) { 547 found = if_unlink_ifnet(ifp, true); 548 MPASS(found); 549 550 pending[i++] = ifp; 551 } 552 } 553 IFNET_WUNLOCK(); 554 555 for (int j = 0; j < i; j++) { 556 if_vmove(pending[j], pending[j]->if_home_vnet); 557 } 558 559 free(pending, M_IFNET); 560} 561VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY, 562 vnet_if_return, NULL); 563#endif 564 565 566static void * 567if_grow(void) 568{ 569 int oldlim; 570 u_int n; 571 struct ifnet **e; 572 void *old; 573 574 old = NULL; 575 IFNET_WLOCK_ASSERT(); 576 oldlim = V_if_indexlim; 577 IFNET_WUNLOCK(); 578 n = (oldlim << 1) * sizeof(*e); 579 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 580 IFNET_WLOCK(); 581 if (V_if_indexlim != oldlim) { 582 free(e, M_IFNET); 583 return (NULL); 584 } 585 if (V_ifindex_table != NULL) { 586 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 587 old = V_ifindex_table; 588 } 589 V_if_indexlim <<= 1; 590 V_ifindex_table = e; 591 return (old); 592} 593 594/* 595 * Allocate a struct ifnet and an index for an interface. A layer 2 596 * common structure will also be allocated if an allocation routine is 597 * registered for the passed type. 598 */ 599struct ifnet * 600if_alloc(u_char type) 601{ 602 struct ifnet *ifp; 603 u_short idx; 604 void *old; 605 606 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 607 restart: 608 IFNET_WLOCK(); 609 idx = ifindex_alloc(&old); 610 if (__predict_false(idx == USHRT_MAX)) { 611 IFNET_WUNLOCK(); 612 epoch_wait_preempt(net_epoch_preempt); 613 free(old, M_IFNET); 614 goto restart; 615 } 616 ifnet_setbyindex(idx, IFNET_HOLD); 617 IFNET_WUNLOCK(); 618 ifp->if_index = idx; 619 ifp->if_type = type; 620 ifp->if_alloctype = type; 621#ifdef VIMAGE 622 ifp->if_vnet = curvnet; 623#endif 624 if (if_com_alloc[type] != NULL) { 625 ifp->if_l2com = if_com_alloc[type](type, ifp); 626 if (ifp->if_l2com == NULL) { 627 free(ifp, M_IFNET); 628 ifindex_free(idx); 629 return (NULL); 630 } 631 } 632 633 IF_ADDR_LOCK_INIT(ifp); 634 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 635 ifp->if_afdata_initialized = 0; 636 IF_AFDATA_LOCK_INIT(ifp); 637 CK_STAILQ_INIT(&ifp->if_addrhead); 638 CK_STAILQ_INIT(&ifp->if_multiaddrs); 639 CK_STAILQ_INIT(&ifp->if_groups); 640#ifdef MAC 641 mac_ifnet_init(ifp); 642#endif 643 ifq_init(&ifp->if_snd, ifp); 644 645 refcount_init(&ifp->if_refcount, 1); /* Index reference. */ 646 for (int i = 0; i < IFCOUNTERS; i++) 647 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK); 648 ifp->if_get_counter = if_get_counter_default; 649 ifp->if_pcp = IFNET_PCP_NONE; 650 ifnet_setbyindex(ifp->if_index, ifp); 651 return (ifp); 652} 653 654/* 655 * Do the actual work of freeing a struct ifnet, and layer 2 common 656 * structure. This call is made when the last reference to an 657 * interface is released. 658 */ 659static void 660if_free_internal(struct ifnet *ifp) 661{ 662 663 KASSERT((ifp->if_flags & IFF_DYING), 664 ("if_free_internal: interface not dying")); 665 666 if (if_com_free[ifp->if_alloctype] != NULL) 667 if_com_free[ifp->if_alloctype](ifp->if_l2com, 668 ifp->if_alloctype); 669 670#ifdef MAC 671 mac_ifnet_destroy(ifp); 672#endif /* MAC */ 673 IF_AFDATA_DESTROY(ifp); 674 IF_ADDR_LOCK_DESTROY(ifp); 675 ifq_delete(&ifp->if_snd); 676 677 for (int i = 0; i < IFCOUNTERS; i++) 678 counter_u64_free(ifp->if_counters[i]); 679 680 free(ifp->if_description, M_IFDESCR); 681 free(ifp->if_hw_addr, M_IFADDR); 682 free(ifp, M_IFNET); 683} 684 685static void 686if_destroy(epoch_context_t ctx) 687{ 688 struct ifnet *ifp; 689 690 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx); 691 if_free_internal(ifp); 692} 693 694/* 695 * Deregister an interface and free the associated storage. 696 */ 697void 698if_free(struct ifnet *ifp) 699{ 700 701 ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 702 703 CURVNET_SET_QUIET(ifp->if_vnet); 704 IFNET_WLOCK(); 705 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 706 ("%s: freeing unallocated ifnet", ifp->if_xname)); 707 708 ifindex_free_locked(ifp->if_index); 709 IFNET_WUNLOCK(); 710 711 if (refcount_release(&ifp->if_refcount)) 712 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy); 713 CURVNET_RESTORE(); 714} 715 716/* 717 * Interfaces to keep an ifnet type-stable despite the possibility of the 718 * driver calling if_free(). If there are additional references, we defer 719 * freeing the underlying data structure. 720 */ 721void 722if_ref(struct ifnet *ifp) 723{ 724 725 /* We don't assert the ifnet list lock here, but arguably should. */ 726 refcount_acquire(&ifp->if_refcount); 727} 728 729void 730if_rele(struct ifnet *ifp) 731{ 732 733 if (!refcount_release(&ifp->if_refcount)) 734 return; 735 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy); 736} 737 738void 739ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 740{ 741 742 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 743 744 if (ifq->ifq_maxlen == 0) 745 ifq->ifq_maxlen = ifqmaxlen; 746 747 ifq->altq_type = 0; 748 ifq->altq_disc = NULL; 749 ifq->altq_flags &= ALTQF_CANTCHANGE; 750 ifq->altq_tbr = NULL; 751 ifq->altq_ifp = ifp; 752} 753 754void 755ifq_delete(struct ifaltq *ifq) 756{ 757 mtx_destroy(&ifq->ifq_mtx); 758} 759 760/* 761 * Perform generic interface initialization tasks and attach the interface 762 * to the list of "active" interfaces. If vmove flag is set on entry 763 * to if_attach_internal(), perform only a limited subset of initialization 764 * tasks, given that we are moving from one vnet to another an ifnet which 765 * has already been fully initialized. 766 * 767 * Note that if_detach_internal() removes group membership unconditionally 768 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL. 769 * Thus, when if_vmove() is applied to a cloned interface, group membership 770 * is lost while a cloned one always joins a group whose name is 771 * ifc->ifc_name. To recover this after if_detach_internal() and 772 * if_attach_internal(), the cloner should be specified to 773 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal() 774 * attempts to join a group whose name is ifc->ifc_name. 775 * 776 * XXX: 777 * - The decision to return void and thus require this function to 778 * succeed is questionable. 779 * - We should probably do more sanity checking. For instance we don't 780 * do anything to insure if_xname is unique or non-empty. 781 */ 782void 783if_attach(struct ifnet *ifp) 784{ 785 786 if_attach_internal(ifp, 0, NULL); 787} 788 789/* 790 * Compute the least common TSO limit. 791 */ 792void 793if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax) 794{ 795 /* 796 * 1) If there is no limit currently, take the limit from 797 * the network adapter. 798 * 799 * 2) If the network adapter has a limit below the current 800 * limit, apply it. 801 */ 802 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 && 803 ifp->if_hw_tsomax < pmax->tsomaxbytes)) { 804 pmax->tsomaxbytes = ifp->if_hw_tsomax; 805 } 806 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 && 807 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) { 808 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount; 809 } 810 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 && 811 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) { 812 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize; 813 } 814} 815 816/* 817 * Update TSO limit of a network adapter. 818 * 819 * Returns zero if no change. Else non-zero. 820 */ 821int 822if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax) 823{ 824 int retval = 0; 825 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) { 826 ifp->if_hw_tsomax = pmax->tsomaxbytes; 827 retval++; 828 } 829 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) { 830 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize; 831 retval++; 832 } 833 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) { 834 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount; 835 retval++; 836 } 837 return (retval); 838} 839 840static void 841if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc) 842{ 843 unsigned socksize, ifasize; 844 int namelen, masklen; 845 struct sockaddr_dl *sdl; 846 struct ifaddr *ifa; 847 848 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 849 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 850 ifp->if_xname); 851 852#ifdef VIMAGE 853 ifp->if_vnet = curvnet; 854 if (ifp->if_home_vnet == NULL) 855 ifp->if_home_vnet = curvnet; 856#endif 857 858 if_addgroup(ifp, IFG_ALL); 859 860 /* Restore group membership for cloned interfaces. */ 861 if (vmove && ifc != NULL) 862 if_clone_addgroup(ifp, ifc); 863 864 getmicrotime(&ifp->if_lastchange); 865 ifp->if_epoch = time_uptime; 866 867 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 868 (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 869 ("transmit and qflush must both either be set or both be NULL")); 870 if (ifp->if_transmit == NULL) { 871 ifp->if_transmit = if_transmit; 872 ifp->if_qflush = if_qflush; 873 } 874 if (ifp->if_input == NULL) 875 ifp->if_input = if_input_default; 876 877 if (ifp->if_requestencap == NULL) 878 ifp->if_requestencap = if_requestencap_default; 879 880 if (!vmove) { 881#ifdef MAC 882 mac_ifnet_create(ifp); 883#endif 884 885 /* 886 * Create a Link Level name for this device. 887 */ 888 namelen = strlen(ifp->if_xname); 889 /* 890 * Always save enough space for any possiable name so we 891 * can do a rename in place later. 892 */ 893 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 894 socksize = masklen + ifp->if_addrlen; 895 if (socksize < sizeof(*sdl)) 896 socksize = sizeof(*sdl); 897 socksize = roundup2(socksize, sizeof(long)); 898 ifasize = sizeof(*ifa) + 2 * socksize; 899 ifa = ifa_alloc(ifasize, M_WAITOK); 900 sdl = (struct sockaddr_dl *)(ifa + 1); 901 sdl->sdl_len = socksize; 902 sdl->sdl_family = AF_LINK; 903 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 904 sdl->sdl_nlen = namelen; 905 sdl->sdl_index = ifp->if_index; 906 sdl->sdl_type = ifp->if_type; 907 ifp->if_addr = ifa; 908 ifa->ifa_ifp = ifp; 909 ifa->ifa_rtrequest = link_rtrequest; 910 ifa->ifa_addr = (struct sockaddr *)sdl; 911 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 912 ifa->ifa_netmask = (struct sockaddr *)sdl; 913 sdl->sdl_len = masklen; 914 while (namelen != 0) 915 sdl->sdl_data[--namelen] = 0xff; 916 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 917 /* Reliably crash if used uninitialized. */ 918 ifp->if_broadcastaddr = NULL; 919 920 if (ifp->if_type == IFT_ETHER) { 921 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR, 922 M_WAITOK | M_ZERO); 923 } 924 925#if defined(INET) || defined(INET6) 926 /* Use defaults for TSO, if nothing is set */ 927 if (ifp->if_hw_tsomax == 0 && 928 ifp->if_hw_tsomaxsegcount == 0 && 929 ifp->if_hw_tsomaxsegsize == 0) { 930 /* 931 * The TSO defaults needs to be such that an 932 * NFS mbuf list of 35 mbufs totalling just 933 * below 64K works and that a chain of mbufs 934 * can be defragged into at most 32 segments: 935 */ 936 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) - 937 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 938 ifp->if_hw_tsomaxsegcount = 35; 939 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */ 940 941 /* XXX some drivers set IFCAP_TSO after ethernet attach */ 942 if (ifp->if_capabilities & IFCAP_TSO) { 943 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n", 944 ifp->if_hw_tsomax, 945 ifp->if_hw_tsomaxsegcount, 946 ifp->if_hw_tsomaxsegsize); 947 } 948 } 949#endif 950 } 951#ifdef VIMAGE 952 else { 953 /* 954 * Update the interface index in the link layer address 955 * of the interface. 956 */ 957 for (ifa = ifp->if_addr; ifa != NULL; 958 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) { 959 if (ifa->ifa_addr->sa_family == AF_LINK) { 960 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 961 sdl->sdl_index = ifp->if_index; 962 } 963 } 964 } 965#endif 966 967 if_link_ifnet(ifp); 968 969 if (domain_init_status >= 2) 970 if_attachdomain1(ifp); 971 972 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 973 if (IS_DEFAULT_VNET(curvnet)) 974 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 975 976 /* Announce the interface. */ 977 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 978} 979 980static void 981if_epochalloc(void *dummy __unused) 982{ 983 984 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT); 985 net_epoch = epoch_alloc(0); 986} 987SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY, 988 if_epochalloc, NULL); 989 990static void 991if_attachdomain(void *dummy) 992{ 993 struct ifnet *ifp; 994 995 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) 996 if_attachdomain1(ifp); 997} 998SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 999 if_attachdomain, NULL); 1000 1001static void 1002if_attachdomain1(struct ifnet *ifp) 1003{ 1004 struct domain *dp; 1005 1006 /* 1007 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 1008 * cannot lock ifp->if_afdata initialization, entirely. 1009 */ 1010 IF_AFDATA_LOCK(ifp); 1011 if (ifp->if_afdata_initialized >= domain_init_status) { 1012 IF_AFDATA_UNLOCK(ifp); 1013 log(LOG_WARNING, "%s called more than once on %s\n", 1014 __func__, ifp->if_xname); 1015 return; 1016 } 1017 ifp->if_afdata_initialized = domain_init_status; 1018 IF_AFDATA_UNLOCK(ifp); 1019 1020 /* address family dependent data region */ 1021 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 1022 for (dp = domains; dp; dp = dp->dom_next) { 1023 if (dp->dom_ifattach) 1024 ifp->if_afdata[dp->dom_family] = 1025 (*dp->dom_ifattach)(ifp); 1026 } 1027} 1028 1029/* 1030 * Remove any unicast or broadcast network addresses from an interface. 1031 */ 1032void 1033if_purgeaddrs(struct ifnet *ifp) 1034{ 1035 struct ifaddr *ifa; 1036 1037 while (1) { 1038 NET_EPOCH_ENTER(); 1039 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1040 if (ifa->ifa_addr->sa_family != AF_LINK) 1041 break; 1042 } 1043 NET_EPOCH_EXIT(); 1044 1045 if (ifa == NULL) 1046 break; 1047#ifdef INET 1048 /* XXX: Ugly!! ad hoc just for INET */ 1049 if (ifa->ifa_addr->sa_family == AF_INET) { 1050 struct ifaliasreq ifr; 1051 1052 bzero(&ifr, sizeof(ifr)); 1053 ifr.ifra_addr = *ifa->ifa_addr; 1054 if (ifa->ifa_dstaddr) 1055 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 1056 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 1057 NULL) == 0) 1058 continue; 1059 } 1060#endif /* INET */ 1061#ifdef INET6 1062 if (ifa->ifa_addr->sa_family == AF_INET6) { 1063 in6_purgeaddr(ifa); 1064 /* ifp_addrhead is already updated */ 1065 continue; 1066 } 1067#endif /* INET6 */ 1068 IF_ADDR_WLOCK(ifp); 1069 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link); 1070 IF_ADDR_WUNLOCK(ifp); 1071 ifa_free(ifa); 1072 } 1073} 1074 1075/* 1076 * Remove any multicast network addresses from an interface when an ifnet 1077 * is going away. 1078 */ 1079static void 1080if_purgemaddrs(struct ifnet *ifp) 1081{ 1082 struct ifmultiaddr *ifma; 1083 1084 IF_ADDR_WLOCK(ifp); 1085 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) { 1086 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs); 1087 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 1088 if_delmulti_locked(ifp, ifma, 1); 1089 } 1090 IF_ADDR_WUNLOCK(ifp); 1091} 1092 1093/* 1094 * Detach an interface, removing it from the list of "active" interfaces. 1095 * If vmove flag is set on entry to if_detach_internal(), perform only a 1096 * limited subset of cleanup tasks, given that we are moving an ifnet from 1097 * one vnet to another, where it must be fully operational. 1098 * 1099 * XXXRW: There are some significant questions about event ordering, and 1100 * how to prevent things from starting to use the interface during detach. 1101 */ 1102void 1103if_detach(struct ifnet *ifp) 1104{ 1105 bool found; 1106 1107 CURVNET_SET_QUIET(ifp->if_vnet); 1108 found = if_unlink_ifnet(ifp, false); 1109 if (found) { 1110 sx_xlock(&ifnet_detach_sxlock); 1111 if_detach_internal(ifp, 0, NULL); 1112 sx_xunlock(&ifnet_detach_sxlock); 1113 } 1114 CURVNET_RESTORE(); 1115} 1116 1117/* 1118 * The vmove flag, if set, indicates that we are called from a callpath 1119 * that is moving an interface to a different vnet instance. 1120 * 1121 * The shutdown flag, if set, indicates that we are called in the 1122 * process of shutting down a vnet instance. Currently only the 1123 * vnet_if_return SYSUNINIT function sets it. Note: we can be called 1124 * on a vnet instance shutdown without this flag being set, e.g., when 1125 * the cloned interfaces are destoyed as first thing of teardown. 1126 */ 1127static int 1128if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp) 1129{ 1130 struct ifaddr *ifa; 1131 int i; 1132 struct domain *dp; 1133#ifdef VIMAGE 1134 int shutdown; 1135 1136 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && 1137 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 1138#endif 1139 1140 /* 1141 * At this point we know the interface still was on the ifnet list 1142 * and we removed it so we are in a stable state. 1143 */ 1144 epoch_wait_preempt(net_epoch_preempt); 1145 1146 /* 1147 * Ensure all pending EPOCH(9) callbacks have been executed. This 1148 * fixes issues about late destruction of multicast options 1149 * which lead to leave group calls, which in turn access the 1150 * belonging ifnet structure: 1151 */ 1152 epoch_drain_callbacks(net_epoch_preempt); 1153 1154 /* 1155 * In any case (destroy or vmove) detach us from the groups 1156 * and remove/wait for pending events on the taskq. 1157 * XXX-BZ in theory an interface could still enqueue a taskq change? 1158 */ 1159 if_delgroups(ifp); 1160 1161 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 1162 1163 /* 1164 * Check if this is a cloned interface or not. Must do even if 1165 * shutting down as a if_vmove_reclaim() would move the ifp and 1166 * the if_clone_addgroup() will have a corrupted string overwise 1167 * from a gibberish pointer. 1168 */ 1169 if (vmove && ifcp != NULL) 1170 *ifcp = if_clone_findifc(ifp); 1171 1172 if_down(ifp); 1173 1174#ifdef VIMAGE 1175 /* 1176 * On VNET shutdown abort here as the stack teardown will do all 1177 * the work top-down for us. 1178 */ 1179 if (shutdown) { 1180 /* Give interface users the chance to clean up. */ 1181 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1182 1183 /* 1184 * In case of a vmove we are done here without error. 1185 * If we would signal an error it would lead to the same 1186 * abort as if we did not find the ifnet anymore. 1187 * if_detach() calls us in void context and does not care 1188 * about an early abort notification, so life is splendid :) 1189 */ 1190 goto finish_vnet_shutdown; 1191 } 1192#endif 1193 1194 /* 1195 * At this point we are not tearing down a VNET and are either 1196 * going to destroy or vmove the interface and have to cleanup 1197 * accordingly. 1198 */ 1199 1200 /* 1201 * Remove routes and flush queues. 1202 */ 1203#ifdef ALTQ 1204 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 1205 altq_disable(&ifp->if_snd); 1206 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 1207 altq_detach(&ifp->if_snd); 1208#endif 1209 1210 if_purgeaddrs(ifp); 1211 1212#ifdef INET 1213 in_ifdetach(ifp); 1214#endif 1215 1216#ifdef INET6 1217 /* 1218 * Remove all IPv6 kernel structs related to ifp. This should be done 1219 * before removing routing entries below, since IPv6 interface direct 1220 * routes are expected to be removed by the IPv6-specific kernel API. 1221 * Otherwise, the kernel will detect some inconsistency and bark it. 1222 */ 1223 in6_ifdetach(ifp); 1224#endif 1225 if_purgemaddrs(ifp); 1226 1227 /* Announce that the interface is gone. */ 1228 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1229 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1230 if (IS_DEFAULT_VNET(curvnet)) 1231 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 1232 1233 if (!vmove) { 1234 /* 1235 * Prevent further calls into the device driver via ifnet. 1236 */ 1237 if_dead(ifp); 1238 1239 /* 1240 * Clean up all addresses. 1241 */ 1242 IF_ADDR_WLOCK(ifp); 1243 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) { 1244 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead); 1245 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link); 1246 IF_ADDR_WUNLOCK(ifp); 1247 ifa_free(ifa); 1248 } else 1249 IF_ADDR_WUNLOCK(ifp); 1250 } 1251 1252 rt_flushifroutes(ifp); 1253 1254#ifdef VIMAGE 1255finish_vnet_shutdown: 1256#endif 1257 /* 1258 * We cannot hold the lock over dom_ifdetach calls as they might 1259 * sleep, for example trying to drain a callout, thus open up the 1260 * theoretical race with re-attaching. 1261 */ 1262 IF_AFDATA_LOCK(ifp); 1263 i = ifp->if_afdata_initialized; 1264 ifp->if_afdata_initialized = 0; 1265 IF_AFDATA_UNLOCK(ifp); 1266 for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 1267 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) { 1268 (*dp->dom_ifdetach)(ifp, 1269 ifp->if_afdata[dp->dom_family]); 1270 ifp->if_afdata[dp->dom_family] = NULL; 1271 } 1272 } 1273 1274 return (0); 1275} 1276 1277#ifdef VIMAGE 1278/* 1279 * if_vmove() performs a limited version of if_detach() in current 1280 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 1281 * An attempt is made to shrink if_index in current vnet, find an 1282 * unused if_index in target vnet and calls if_grow() if necessary, 1283 * and finally find an unused if_xname for the target vnet. 1284 */ 1285static void 1286if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 1287{ 1288 struct if_clone *ifc; 1289#ifdef DEV_BPF 1290 u_int bif_dlt, bif_hdrlen; 1291#endif 1292 void *old; 1293 int rc; 1294 1295#ifdef DEV_BPF 1296 /* 1297 * if_detach_internal() will call the eventhandler to notify 1298 * interface departure. That will detach if_bpf. We need to 1299 * safe the dlt and hdrlen so we can re-attach it later. 1300 */ 1301 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen); 1302#endif 1303 1304 /* 1305 * Detach from current vnet, but preserve LLADDR info, do not 1306 * mark as dead etc. so that the ifnet can be reattached later. 1307 * If we cannot find it, we lost the race to someone else. 1308 */ 1309 rc = if_detach_internal(ifp, 1, &ifc); 1310 if (rc != 0) 1311 return; 1312 1313 /* 1314 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 1315 * the if_index for that vnet if possible. 1316 * 1317 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 1318 * or we'd lock on one vnet and unlock on another. 1319 */ 1320 IFNET_WLOCK(); 1321 ifindex_free_locked(ifp->if_index); 1322 IFNET_WUNLOCK(); 1323 1324 /* 1325 * Perform interface-specific reassignment tasks, if provided by 1326 * the driver. 1327 */ 1328 if (ifp->if_reassign != NULL) 1329 ifp->if_reassign(ifp, new_vnet, NULL); 1330 1331 /* 1332 * Switch to the context of the target vnet. 1333 */ 1334 CURVNET_SET_QUIET(new_vnet); 1335 restart: 1336 IFNET_WLOCK(); 1337 ifp->if_index = ifindex_alloc(&old); 1338 if (__predict_false(ifp->if_index == USHRT_MAX)) { 1339 IFNET_WUNLOCK(); 1340 epoch_wait_preempt(net_epoch_preempt); 1341 free(old, M_IFNET); 1342 goto restart; 1343 } 1344 ifnet_setbyindex(ifp->if_index, ifp); 1345 IFNET_WUNLOCK(); 1346 1347 if_attach_internal(ifp, 1, ifc); 1348 1349#ifdef DEV_BPF 1350 if (ifp->if_bpf == NULL) 1351 bpfattach(ifp, bif_dlt, bif_hdrlen); 1352#endif 1353 1354 CURVNET_RESTORE(); 1355} 1356 1357/* 1358 * Move an ifnet to or from another child prison/vnet, specified by the jail id. 1359 */ 1360static int 1361if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 1362{ 1363 struct prison *pr; 1364 struct ifnet *difp; 1365 int shutdown; 1366 bool found; 1367 1368 /* Try to find the prison within our visibility. */ 1369 sx_slock(&allprison_lock); 1370 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1371 sx_sunlock(&allprison_lock); 1372 if (pr == NULL) 1373 return (ENXIO); 1374 prison_hold_locked(pr); 1375 mtx_unlock(&pr->pr_mtx); 1376 1377 /* Do not try to move the iface from and to the same prison. */ 1378 if (pr->pr_vnet == ifp->if_vnet) { 1379 prison_free(pr); 1380 return (EEXIST); 1381 } 1382 1383 /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1384 /* XXX Lock interfaces to avoid races. */ 1385 CURVNET_SET_QUIET(pr->pr_vnet); 1386 difp = ifunit(ifname); 1387 if (difp != NULL) { 1388 CURVNET_RESTORE(); 1389 prison_free(pr); 1390 return (EEXIST); 1391 } 1392 1393 /* Make sure the VNET is stable. */ 1394 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && 1395 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 1396 if (shutdown) { 1397 CURVNET_RESTORE(); 1398 prison_free(pr); 1399 return (EBUSY); 1400 } 1401 CURVNET_RESTORE(); 1402 1403 found = if_unlink_ifnet(ifp, true); 1404 MPASS(found); 1405 1406 /* Move the interface into the child jail/vnet. */ 1407 if_vmove(ifp, pr->pr_vnet); 1408 1409 /* Report the new if_xname back to the userland. */ 1410 sprintf(ifname, "%s", ifp->if_xname); 1411 1412 prison_free(pr); 1413 return (0); 1414} 1415 1416static int 1417if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1418{ 1419 struct prison *pr; 1420 struct vnet *vnet_dst; 1421 struct ifnet *ifp; 1422 int shutdown; 1423 bool found; 1424 1425 /* Try to find the prison within our visibility. */ 1426 sx_slock(&allprison_lock); 1427 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1428 sx_sunlock(&allprison_lock); 1429 if (pr == NULL) 1430 return (ENXIO); 1431 prison_hold_locked(pr); 1432 mtx_unlock(&pr->pr_mtx); 1433 1434 /* Make sure the named iface exists in the source prison/vnet. */ 1435 CURVNET_SET(pr->pr_vnet); 1436 ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1437 if (ifp == NULL) { 1438 CURVNET_RESTORE(); 1439 prison_free(pr); 1440 return (ENXIO); 1441 } 1442 1443 /* Do not try to move the iface from and to the same prison. */ 1444 vnet_dst = TD_TO_VNET(td); 1445 if (vnet_dst == ifp->if_vnet) { 1446 CURVNET_RESTORE(); 1447 prison_free(pr); 1448 return (EEXIST); 1449 } 1450 1451 /* Make sure the VNET is stable. */ 1452 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && 1453 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 1454 if (shutdown) { 1455 CURVNET_RESTORE(); 1456 prison_free(pr); 1457 return (EBUSY); 1458 } 1459 1460 /* Get interface back from child jail/vnet. */ 1461 found = if_unlink_ifnet(ifp, true); 1462 MPASS(found); 1463 if_vmove(ifp, vnet_dst); 1464 CURVNET_RESTORE(); 1465 1466 /* Report the new if_xname back to the userland. */ 1467 sprintf(ifname, "%s", ifp->if_xname); 1468 1469 prison_free(pr); 1470 return (0); 1471} 1472#endif /* VIMAGE */ 1473 1474/* 1475 * Add a group to an interface 1476 */ 1477int 1478if_addgroup(struct ifnet *ifp, const char *groupname) 1479{ 1480 struct ifg_list *ifgl; 1481 struct ifg_group *ifg = NULL; 1482 struct ifg_member *ifgm; 1483 int new = 0; 1484 1485 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 1486 groupname[strlen(groupname) - 1] <= '9') 1487 return (EINVAL); 1488 1489 IFNET_WLOCK(); 1490 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1491 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 1492 IFNET_WUNLOCK(); 1493 return (EEXIST); 1494 } 1495 1496 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) { 1497 IFNET_WUNLOCK(); 1498 return (ENOMEM); 1499 } 1500 1501 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) { 1502 free(ifgl, M_TEMP); 1503 IFNET_WUNLOCK(); 1504 return (ENOMEM); 1505 } 1506 1507 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1508 if (!strcmp(ifg->ifg_group, groupname)) 1509 break; 1510 1511 if (ifg == NULL) { 1512 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) { 1513 free(ifgl, M_TEMP); 1514 free(ifgm, M_TEMP); 1515 IFNET_WUNLOCK(); 1516 return (ENOMEM); 1517 } 1518 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 1519 ifg->ifg_refcnt = 0; 1520 CK_STAILQ_INIT(&ifg->ifg_members); 1521 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 1522 new = 1; 1523 } 1524 1525 ifg->ifg_refcnt++; 1526 ifgl->ifgl_group = ifg; 1527 ifgm->ifgm_ifp = ifp; 1528 1529 IF_ADDR_WLOCK(ifp); 1530 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 1531 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 1532 IF_ADDR_WUNLOCK(ifp); 1533 1534 IFNET_WUNLOCK(); 1535 1536 if (new) 1537 EVENTHANDLER_INVOKE(group_attach_event, ifg); 1538 EVENTHANDLER_INVOKE(group_change_event, groupname); 1539 1540 return (0); 1541} 1542 1543/* 1544 * Helper function to remove a group out of an interface. Expects the global 1545 * ifnet lock to be write-locked, and drops it before returning. 1546 */ 1547static void 1548_if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl, 1549 const char *groupname) 1550{ 1551 struct ifg_member *ifgm; 1552 bool freeifgl; 1553 1554 IFNET_WLOCK_ASSERT(); 1555 1556 IF_ADDR_WLOCK(ifp); 1557 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next); 1558 IF_ADDR_WUNLOCK(ifp); 1559 1560 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) { 1561 if (ifgm->ifgm_ifp == ifp) { 1562 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 1563 ifg_member, ifgm_next); 1564 break; 1565 } 1566 } 1567 1568 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1569 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, 1570 ifg_next); 1571 freeifgl = true; 1572 } else { 1573 freeifgl = false; 1574 } 1575 IFNET_WUNLOCK(); 1576 1577 epoch_wait_preempt(net_epoch_preempt); 1578 if (freeifgl) { 1579 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 1580 free(ifgl->ifgl_group, M_TEMP); 1581 } 1582 free(ifgm, M_TEMP); 1583 free(ifgl, M_TEMP); 1584 1585 EVENTHANDLER_INVOKE(group_change_event, groupname); 1586} 1587 1588/* 1589 * Remove a group from an interface 1590 */ 1591int 1592if_delgroup(struct ifnet *ifp, const char *groupname) 1593{ 1594 struct ifg_list *ifgl; 1595 1596 IFNET_WLOCK(); 1597 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1598 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0) 1599 break; 1600 if (ifgl == NULL) { 1601 IFNET_WUNLOCK(); 1602 return (ENOENT); 1603 } 1604 1605 _if_delgroup_locked(ifp, ifgl, groupname); 1606 1607 return (0); 1608} 1609 1610/* 1611 * Remove an interface from all groups 1612 */ 1613static void 1614if_delgroups(struct ifnet *ifp) 1615{ 1616 struct ifg_list *ifgl; 1617 char groupname[IFNAMSIZ]; 1618 1619 IFNET_WLOCK(); 1620 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) { 1621 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 1622 _if_delgroup_locked(ifp, ifgl, groupname); 1623 IFNET_WLOCK(); 1624 } 1625 IFNET_WUNLOCK(); 1626} 1627 1628static char * 1629ifgr_group_get(void *ifgrp) 1630{ 1631 union ifgroupreq_union *ifgrup; 1632 1633 ifgrup = ifgrp; 1634#ifdef COMPAT_FREEBSD32 1635 if (SV_CURPROC_FLAG(SV_ILP32)) 1636 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]); 1637#endif 1638 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]); 1639} 1640 1641static struct ifg_req * 1642ifgr_groups_get(void *ifgrp) 1643{ 1644 union ifgroupreq_union *ifgrup; 1645 1646 ifgrup = ifgrp; 1647#ifdef COMPAT_FREEBSD32 1648 if (SV_CURPROC_FLAG(SV_ILP32)) 1649 return ((struct ifg_req *)(uintptr_t) 1650 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups); 1651#endif 1652 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups); 1653} 1654 1655/* 1656 * Stores all groups from an interface in memory pointed to by ifgr. 1657 */ 1658static int 1659if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp) 1660{ 1661 int len, error; 1662 struct ifg_list *ifgl; 1663 struct ifg_req ifgrq, *ifgp; 1664 1665 if (ifgr->ifgr_len == 0) { 1666 IF_ADDR_RLOCK(ifp); 1667 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1668 ifgr->ifgr_len += sizeof(struct ifg_req); 1669 IF_ADDR_RUNLOCK(ifp); 1670 return (0); 1671 } 1672 1673 len = ifgr->ifgr_len; 1674 ifgp = ifgr_groups_get(ifgr); 1675 /* XXX: wire */ 1676 IF_ADDR_RLOCK(ifp); 1677 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1678 if (len < sizeof(ifgrq)) { 1679 IF_ADDR_RUNLOCK(ifp); 1680 return (EINVAL); 1681 } 1682 bzero(&ifgrq, sizeof ifgrq); 1683 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1684 sizeof(ifgrq.ifgrq_group)); 1685 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1686 IF_ADDR_RUNLOCK(ifp); 1687 return (error); 1688 } 1689 len -= sizeof(ifgrq); 1690 ifgp++; 1691 } 1692 IF_ADDR_RUNLOCK(ifp); 1693 1694 return (0); 1695} 1696 1697/* 1698 * Stores all members of a group in memory pointed to by igfr 1699 */ 1700static int 1701if_getgroupmembers(struct ifgroupreq *ifgr) 1702{ 1703 struct ifg_group *ifg; 1704 struct ifg_member *ifgm; 1705 struct ifg_req ifgrq, *ifgp; 1706 int len, error; 1707 1708 IFNET_RLOCK(); 1709 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1710 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0) 1711 break; 1712 if (ifg == NULL) { 1713 IFNET_RUNLOCK(); 1714 return (ENOENT); 1715 } 1716 1717 if (ifgr->ifgr_len == 0) { 1718 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1719 ifgr->ifgr_len += sizeof(ifgrq); 1720 IFNET_RUNLOCK(); 1721 return (0); 1722 } 1723 1724 len = ifgr->ifgr_len; 1725 ifgp = ifgr_groups_get(ifgr); 1726 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1727 if (len < sizeof(ifgrq)) { 1728 IFNET_RUNLOCK(); 1729 return (EINVAL); 1730 } 1731 bzero(&ifgrq, sizeof ifgrq); 1732 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1733 sizeof(ifgrq.ifgrq_member)); 1734 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1735 IFNET_RUNLOCK(); 1736 return (error); 1737 } 1738 len -= sizeof(ifgrq); 1739 ifgp++; 1740 } 1741 IFNET_RUNLOCK(); 1742 1743 return (0); 1744} 1745 1746/* 1747 * Return counter values from counter(9)s stored in ifnet. 1748 */ 1749uint64_t 1750if_get_counter_default(struct ifnet *ifp, ift_counter cnt) 1751{ 1752 1753 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1754 1755 return (counter_u64_fetch(ifp->if_counters[cnt])); 1756} 1757 1758/* 1759 * Increase an ifnet counter. Usually used for counters shared 1760 * between the stack and a driver, but function supports them all. 1761 */ 1762void 1763if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc) 1764{ 1765 1766 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1767 1768 counter_u64_add(ifp->if_counters[cnt], inc); 1769} 1770 1771/* 1772 * Copy data from ifnet to userland API structure if_data. 1773 */ 1774void 1775if_data_copy(struct ifnet *ifp, struct if_data *ifd) 1776{ 1777 1778 ifd->ifi_type = ifp->if_type; 1779 ifd->ifi_physical = 0; 1780 ifd->ifi_addrlen = ifp->if_addrlen; 1781 ifd->ifi_hdrlen = ifp->if_hdrlen; 1782 ifd->ifi_link_state = ifp->if_link_state; 1783 ifd->ifi_vhid = 0; 1784 ifd->ifi_datalen = sizeof(struct if_data); 1785 ifd->ifi_mtu = ifp->if_mtu; 1786 ifd->ifi_metric = ifp->if_metric; 1787 ifd->ifi_baudrate = ifp->if_baudrate; 1788 ifd->ifi_hwassist = ifp->if_hwassist; 1789 ifd->ifi_epoch = ifp->if_epoch; 1790 ifd->ifi_lastchange = ifp->if_lastchange; 1791 1792 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS); 1793 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS); 1794 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS); 1795 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS); 1796 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS); 1797 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES); 1798 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES); 1799 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS); 1800 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS); 1801 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS); 1802 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS); 1803 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO); 1804} 1805 1806struct ifnet_read_lock { 1807 struct mtx mtx; /* lock protecting tracker below */ 1808 struct epoch_tracker et; 1809}; 1810 1811DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_addr_read_lock); 1812DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_maddr_read_lock); 1813 1814static void 1815ifnet_read_lock_init(void __unused *arg) 1816{ 1817 struct ifnet_read_lock *pifrl; 1818 int cpu; 1819 1820 CPU_FOREACH(cpu) { 1821 pifrl = DPCPU_ID_PTR(cpu, ifnet_addr_read_lock); 1822 mtx_init(&pifrl->mtx, "ifnet_addr_read_lock", NULL, MTX_DEF); 1823 1824 pifrl = DPCPU_ID_PTR(cpu, ifnet_maddr_read_lock); 1825 mtx_init(&pifrl->mtx, "ifnet_maddr_read_lock", NULL, MTX_DEF); 1826 } 1827} 1828SYSINIT(ifnet_read_lock_init, SI_SUB_CPU + 1, SI_ORDER_FIRST, &ifnet_read_lock_init, NULL); 1829 1830/* 1831 * Wrapper functions for struct ifnet address list locking macros. These are 1832 * used by kernel modules to avoid encoding programming interface or binary 1833 * interface assumptions that may be violated when kernel-internal locking 1834 * approaches change. 1835 */ 1836void 1837if_addr_rlock(struct ifnet *ifp) 1838{ 1839 struct ifnet_read_lock *pifrl; 1840 1841 sched_pin(); 1842 pifrl = DPCPU_PTR(ifnet_addr_read_lock); 1843 mtx_lock(&pifrl->mtx); 1844 epoch_enter_preempt(net_epoch_preempt, &pifrl->et); 1845} 1846 1847void 1848if_addr_runlock(struct ifnet *ifp) 1849{ 1850 struct ifnet_read_lock *pifrl; 1851 1852 pifrl = DPCPU_PTR(ifnet_addr_read_lock); 1853 1854 epoch_exit_preempt(net_epoch_preempt, &pifrl->et); 1855 mtx_unlock(&pifrl->mtx); 1856 sched_unpin(); 1857} 1858 1859void 1860if_maddr_rlock(if_t ifp) 1861{ 1862 struct ifnet_read_lock *pifrl; 1863 1864 sched_pin(); 1865 pifrl = DPCPU_PTR(ifnet_maddr_read_lock); 1866 mtx_lock(&pifrl->mtx); 1867 epoch_enter_preempt(net_epoch_preempt, &pifrl->et); 1868} 1869 1870void 1871if_maddr_runlock(if_t ifp) 1872{ 1873 struct ifnet_read_lock *pifrl; 1874 1875 pifrl = DPCPU_PTR(ifnet_maddr_read_lock); 1876 1877 epoch_exit_preempt(net_epoch_preempt, &pifrl->et); 1878 mtx_unlock(&pifrl->mtx); 1879 sched_unpin(); 1880} 1881 1882/* 1883 * Initialization, destruction and refcounting functions for ifaddrs. 1884 */ 1885struct ifaddr * 1886ifa_alloc(size_t size, int flags) 1887{ 1888 struct ifaddr *ifa; 1889 1890 KASSERT(size >= sizeof(struct ifaddr), 1891 ("%s: invalid size %zu", __func__, size)); 1892 1893 ifa = malloc(size, M_IFADDR, M_ZERO | flags); 1894 if (ifa == NULL) 1895 return (NULL); 1896 1897 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL) 1898 goto fail; 1899 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL) 1900 goto fail; 1901 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL) 1902 goto fail; 1903 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL) 1904 goto fail; 1905 1906 refcount_init(&ifa->ifa_refcnt, 1); 1907 1908 return (ifa); 1909 1910fail: 1911 /* free(NULL) is okay */ 1912 counter_u64_free(ifa->ifa_opackets); 1913 counter_u64_free(ifa->ifa_ipackets); 1914 counter_u64_free(ifa->ifa_obytes); 1915 counter_u64_free(ifa->ifa_ibytes); 1916 free(ifa, M_IFADDR); 1917 1918 return (NULL); 1919} 1920 1921void 1922ifa_ref(struct ifaddr *ifa) 1923{ 1924 1925 refcount_acquire(&ifa->ifa_refcnt); 1926} 1927 1928static void 1929ifa_destroy(epoch_context_t ctx) 1930{ 1931 struct ifaddr *ifa; 1932 1933 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx); 1934 counter_u64_free(ifa->ifa_opackets); 1935 counter_u64_free(ifa->ifa_ipackets); 1936 counter_u64_free(ifa->ifa_obytes); 1937 counter_u64_free(ifa->ifa_ibytes); 1938 free(ifa, M_IFADDR); 1939} 1940 1941void 1942ifa_free(struct ifaddr *ifa) 1943{ 1944 1945 if (refcount_release(&ifa->ifa_refcnt)) 1946 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy); 1947} 1948 1949 1950static int 1951ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa, 1952 struct sockaddr *ia) 1953{ 1954 int error; 1955 struct rt_addrinfo info; 1956 struct sockaddr_dl null_sdl; 1957 struct ifnet *ifp; 1958 1959 ifp = ifa->ifa_ifp; 1960 1961 bzero(&info, sizeof(info)); 1962 if (cmd != RTM_DELETE) 1963 info.rti_ifp = V_loif; 1964 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED; 1965 info.rti_info[RTAX_DST] = ia; 1966 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1967 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type); 1968 1969 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib); 1970 1971 if (error == 0 || 1972 (cmd == RTM_ADD && error == EEXIST) || 1973 (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH))) 1974 return (error); 1975 1976 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n", 1977 __func__, otype, if_name(ifp), error); 1978 1979 return (error); 1980} 1981 1982int 1983ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1984{ 1985 1986 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia)); 1987} 1988 1989int 1990ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1991{ 1992 1993 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia)); 1994} 1995 1996int 1997ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1998{ 1999 2000 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia)); 2001} 2002 2003/* 2004 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 2005 * structs used to represent other address families, it is necessary 2006 * to perform a different comparison. 2007 */ 2008 2009#define sa_dl_equal(a1, a2) \ 2010 ((((const struct sockaddr_dl *)(a1))->sdl_len == \ 2011 ((const struct sockaddr_dl *)(a2))->sdl_len) && \ 2012 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \ 2013 CLLADDR((const struct sockaddr_dl *)(a2)), \ 2014 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 2015 2016/* 2017 * Locate an interface based on a complete address. 2018 */ 2019/*ARGSUSED*/ 2020struct ifaddr * 2021ifa_ifwithaddr(const struct sockaddr *addr) 2022{ 2023 struct ifnet *ifp; 2024 struct ifaddr *ifa; 2025 2026 MPASS(in_epoch(net_epoch_preempt)); 2027 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2028 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2029 if (ifa->ifa_addr->sa_family != addr->sa_family) 2030 continue; 2031 if (sa_equal(addr, ifa->ifa_addr)) { 2032 goto done; 2033 } 2034 /* IP6 doesn't have broadcast */ 2035 if ((ifp->if_flags & IFF_BROADCAST) && 2036 ifa->ifa_broadaddr && 2037 ifa->ifa_broadaddr->sa_len != 0 && 2038 sa_equal(ifa->ifa_broadaddr, addr)) { 2039 goto done; 2040 } 2041 } 2042 } 2043 ifa = NULL; 2044done: 2045 return (ifa); 2046} 2047 2048int 2049ifa_ifwithaddr_check(const struct sockaddr *addr) 2050{ 2051 int rc; 2052 2053 NET_EPOCH_ENTER(); 2054 rc = (ifa_ifwithaddr(addr) != NULL); 2055 NET_EPOCH_EXIT(); 2056 return (rc); 2057} 2058 2059/* 2060 * Locate an interface based on the broadcast address. 2061 */ 2062/* ARGSUSED */ 2063struct ifaddr * 2064ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum) 2065{ 2066 struct ifnet *ifp; 2067 struct ifaddr *ifa; 2068 2069 MPASS(in_epoch(net_epoch_preempt)); 2070 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2071 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 2072 continue; 2073 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2074 if (ifa->ifa_addr->sa_family != addr->sa_family) 2075 continue; 2076 if ((ifp->if_flags & IFF_BROADCAST) && 2077 ifa->ifa_broadaddr && 2078 ifa->ifa_broadaddr->sa_len != 0 && 2079 sa_equal(ifa->ifa_broadaddr, addr)) { 2080 goto done; 2081 } 2082 } 2083 } 2084 ifa = NULL; 2085done: 2086 return (ifa); 2087} 2088 2089/* 2090 * Locate the point to point interface with a given destination address. 2091 */ 2092/*ARGSUSED*/ 2093struct ifaddr * 2094ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum) 2095{ 2096 struct ifnet *ifp; 2097 struct ifaddr *ifa; 2098 2099 MPASS(in_epoch(net_epoch_preempt)); 2100 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2101 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 2102 continue; 2103 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 2104 continue; 2105 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2106 if (ifa->ifa_addr->sa_family != addr->sa_family) 2107 continue; 2108 if (ifa->ifa_dstaddr != NULL && 2109 sa_equal(addr, ifa->ifa_dstaddr)) { 2110 goto done; 2111 } 2112 } 2113 } 2114 ifa = NULL; 2115done: 2116 return (ifa); 2117} 2118 2119/* 2120 * Find an interface on a specific network. If many, choice 2121 * is most specific found. 2122 */ 2123struct ifaddr * 2124ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum) 2125{ 2126 struct ifnet *ifp; 2127 struct ifaddr *ifa; 2128 struct ifaddr *ifa_maybe = NULL; 2129 u_int af = addr->sa_family; 2130 const char *addr_data = addr->sa_data, *cplim; 2131 2132 MPASS(in_epoch(net_epoch_preempt)); 2133 /* 2134 * AF_LINK addresses can be looked up directly by their index number, 2135 * so do that if we can. 2136 */ 2137 if (af == AF_LINK) { 2138 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr; 2139 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 2140 return (ifaddr_byindex(sdl->sdl_index)); 2141 } 2142 2143 /* 2144 * Scan though each interface, looking for ones that have addresses 2145 * in this address family and the requested fib. Maintain a reference 2146 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that 2147 * kept it stable when we move onto the next interface. 2148 */ 2149 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2150 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 2151 continue; 2152 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2153 const char *cp, *cp2, *cp3; 2154 2155 if (ifa->ifa_addr->sa_family != af) 2156next: continue; 2157 if (af == AF_INET && 2158 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 2159 /* 2160 * This is a bit broken as it doesn't 2161 * take into account that the remote end may 2162 * be a single node in the network we are 2163 * looking for. 2164 * The trouble is that we don't know the 2165 * netmask for the remote end. 2166 */ 2167 if (ifa->ifa_dstaddr != NULL && 2168 sa_equal(addr, ifa->ifa_dstaddr)) { 2169 goto done; 2170 } 2171 } else { 2172 /* 2173 * Scan all the bits in the ifa's address. 2174 * If a bit dissagrees with what we are 2175 * looking for, mask it with the netmask 2176 * to see if it really matters. 2177 * (A byte at a time) 2178 */ 2179 if (ifa->ifa_netmask == 0) 2180 continue; 2181 cp = addr_data; 2182 cp2 = ifa->ifa_addr->sa_data; 2183 cp3 = ifa->ifa_netmask->sa_data; 2184 cplim = ifa->ifa_netmask->sa_len 2185 + (char *)ifa->ifa_netmask; 2186 while (cp3 < cplim) 2187 if ((*cp++ ^ *cp2++) & *cp3++) 2188 goto next; /* next address! */ 2189 /* 2190 * If the netmask of what we just found 2191 * is more specific than what we had before 2192 * (if we had one), or if the virtual status 2193 * of new prefix is better than of the old one, 2194 * then remember the new one before continuing 2195 * to search for an even better one. 2196 */ 2197 if (ifa_maybe == NULL || 2198 ifa_preferred(ifa_maybe, ifa) || 2199 rn_refines((caddr_t)ifa->ifa_netmask, 2200 (caddr_t)ifa_maybe->ifa_netmask)) { 2201 ifa_maybe = ifa; 2202 } 2203 } 2204 } 2205 } 2206 ifa = ifa_maybe; 2207 ifa_maybe = NULL; 2208done: 2209 return (ifa); 2210} 2211 2212/* 2213 * Find an interface address specific to an interface best matching 2214 * a given address. 2215 */ 2216struct ifaddr * 2217ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 2218{ 2219 struct ifaddr *ifa; 2220 const char *cp, *cp2, *cp3; 2221 char *cplim; 2222 struct ifaddr *ifa_maybe = NULL; 2223 u_int af = addr->sa_family; 2224 2225 if (af >= AF_MAX) 2226 return (NULL); 2227 2228 MPASS(in_epoch(net_epoch_preempt)); 2229 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2230 if (ifa->ifa_addr->sa_family != af) 2231 continue; 2232 if (ifa_maybe == NULL) 2233 ifa_maybe = ifa; 2234 if (ifa->ifa_netmask == 0) { 2235 if (sa_equal(addr, ifa->ifa_addr) || 2236 (ifa->ifa_dstaddr && 2237 sa_equal(addr, ifa->ifa_dstaddr))) 2238 goto done; 2239 continue; 2240 } 2241 if (ifp->if_flags & IFF_POINTOPOINT) { 2242 if (sa_equal(addr, ifa->ifa_dstaddr)) 2243 goto done; 2244 } else { 2245 cp = addr->sa_data; 2246 cp2 = ifa->ifa_addr->sa_data; 2247 cp3 = ifa->ifa_netmask->sa_data; 2248 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2249 for (; cp3 < cplim; cp3++) 2250 if ((*cp++ ^ *cp2++) & *cp3) 2251 break; 2252 if (cp3 == cplim) 2253 goto done; 2254 } 2255 } 2256 ifa = ifa_maybe; 2257done: 2258 return (ifa); 2259} 2260 2261/* 2262 * See whether new ifa is better than current one: 2263 * 1) A non-virtual one is preferred over virtual. 2264 * 2) A virtual in master state preferred over any other state. 2265 * 2266 * Used in several address selecting functions. 2267 */ 2268int 2269ifa_preferred(struct ifaddr *cur, struct ifaddr *next) 2270{ 2271 2272 return (cur->ifa_carp && (!next->ifa_carp || 2273 ((*carp_master_p)(next) && !(*carp_master_p)(cur)))); 2274} 2275 2276#include <net/if_llatbl.h> 2277 2278/* 2279 * Default action when installing a route with a Link Level gateway. 2280 * Lookup an appropriate real ifa to point to. 2281 * This should be moved to /sys/net/link.c eventually. 2282 */ 2283static void 2284link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 2285{ 2286 struct ifaddr *ifa, *oifa; 2287 struct sockaddr *dst; 2288 struct ifnet *ifp; 2289 2290 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) || 2291 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL)) 2292 return; 2293 NET_EPOCH_ENTER(); 2294 ifa = ifaof_ifpforaddr(dst, ifp); 2295 if (ifa) { 2296 oifa = rt->rt_ifa; 2297 if (oifa != ifa) { 2298 ifa_free(oifa); 2299 ifa_ref(ifa); 2300 } 2301 rt->rt_ifa = ifa; 2302 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 2303 ifa->ifa_rtrequest(cmd, rt, info); 2304 } 2305 NET_EPOCH_EXIT(); 2306} 2307 2308struct sockaddr_dl * 2309link_alloc_sdl(size_t size, int flags) 2310{ 2311 2312 return (malloc(size, M_TEMP, flags)); 2313} 2314 2315void 2316link_free_sdl(struct sockaddr *sa) 2317{ 2318 free(sa, M_TEMP); 2319} 2320 2321/* 2322 * Fills in given sdl with interface basic info. 2323 * Returns pointer to filled sdl. 2324 */ 2325struct sockaddr_dl * 2326link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype) 2327{ 2328 struct sockaddr_dl *sdl; 2329 2330 sdl = (struct sockaddr_dl *)paddr; 2331 memset(sdl, 0, sizeof(struct sockaddr_dl)); 2332 sdl->sdl_len = sizeof(struct sockaddr_dl); 2333 sdl->sdl_family = AF_LINK; 2334 sdl->sdl_index = ifp->if_index; 2335 sdl->sdl_type = iftype; 2336 2337 return (sdl); 2338} 2339 2340/* 2341 * Mark an interface down and notify protocols of 2342 * the transition. 2343 */ 2344static void 2345if_unroute(struct ifnet *ifp, int flag, int fam) 2346{ 2347 struct ifaddr *ifa; 2348 2349 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 2350 2351 ifp->if_flags &= ~flag; 2352 getmicrotime(&ifp->if_lastchange); 2353 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2354 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2355 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2356 ifp->if_qflush(ifp); 2357 2358 if (ifp->if_carp) 2359 (*carp_linkstate_p)(ifp); 2360 rt_ifmsg(ifp); 2361} 2362 2363/* 2364 * Mark an interface up and notify protocols of 2365 * the transition. 2366 */ 2367static void 2368if_route(struct ifnet *ifp, int flag, int fam) 2369{ 2370 struct ifaddr *ifa; 2371 2372 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 2373 2374 ifp->if_flags |= flag; 2375 getmicrotime(&ifp->if_lastchange); 2376 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2377 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2378 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2379 if (ifp->if_carp) 2380 (*carp_linkstate_p)(ifp); 2381 rt_ifmsg(ifp); 2382#ifdef INET6 2383 in6_if_up(ifp); 2384#endif 2385} 2386 2387void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 2388void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 2389struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 2390struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 2391int (*vlan_tag_p)(struct ifnet *, uint16_t *); 2392int (*vlan_pcp_p)(struct ifnet *, uint16_t *); 2393int (*vlan_setcookie_p)(struct ifnet *, void *); 2394void *(*vlan_cookie_p)(struct ifnet *); 2395 2396/* 2397 * Handle a change in the interface link state. To avoid LORs 2398 * between driver lock and upper layer locks, as well as possible 2399 * recursions, we post event to taskqueue, and all job 2400 * is done in static do_link_state_change(). 2401 */ 2402void 2403if_link_state_change(struct ifnet *ifp, int link_state) 2404{ 2405 /* Return if state hasn't changed. */ 2406 if (ifp->if_link_state == link_state) 2407 return; 2408 2409 ifp->if_link_state = link_state; 2410 2411 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 2412} 2413 2414static void 2415do_link_state_change(void *arg, int pending) 2416{ 2417 struct ifnet *ifp = (struct ifnet *)arg; 2418 int link_state = ifp->if_link_state; 2419 CURVNET_SET(ifp->if_vnet); 2420 2421 /* Notify that the link state has changed. */ 2422 rt_ifmsg(ifp); 2423 if (ifp->if_vlantrunk != NULL) 2424 (*vlan_link_state_p)(ifp); 2425 2426 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 2427 ifp->if_l2com != NULL) 2428 (*ng_ether_link_state_p)(ifp, link_state); 2429 if (ifp->if_carp) 2430 (*carp_linkstate_p)(ifp); 2431 if (ifp->if_bridge) 2432 ifp->if_bridge_linkstate(ifp); 2433 if (ifp->if_lagg) 2434 (*lagg_linkstate_p)(ifp, link_state); 2435 2436 if (IS_DEFAULT_VNET(curvnet)) 2437 devctl_notify("IFNET", ifp->if_xname, 2438 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 2439 NULL); 2440 if (pending > 1) 2441 if_printf(ifp, "%d link states coalesced\n", pending); 2442 if (log_link_state_change) 2443 if_printf(ifp, "link state changed to %s\n", 2444 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 2445 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state); 2446 CURVNET_RESTORE(); 2447} 2448 2449/* 2450 * Mark an interface down and notify protocols of 2451 * the transition. 2452 */ 2453void 2454if_down(struct ifnet *ifp) 2455{ 2456 2457 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN); 2458 if_unroute(ifp, IFF_UP, AF_UNSPEC); 2459} 2460 2461/* 2462 * Mark an interface up and notify protocols of 2463 * the transition. 2464 */ 2465void 2466if_up(struct ifnet *ifp) 2467{ 2468 2469 if_route(ifp, IFF_UP, AF_UNSPEC); 2470 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP); 2471} 2472 2473/* 2474 * Flush an interface queue. 2475 */ 2476void 2477if_qflush(struct ifnet *ifp) 2478{ 2479 struct mbuf *m, *n; 2480 struct ifaltq *ifq; 2481 2482 ifq = &ifp->if_snd; 2483 IFQ_LOCK(ifq); 2484#ifdef ALTQ 2485 if (ALTQ_IS_ENABLED(ifq)) 2486 ALTQ_PURGE(ifq); 2487#endif 2488 n = ifq->ifq_head; 2489 while ((m = n) != NULL) { 2490 n = m->m_nextpkt; 2491 m_freem(m); 2492 } 2493 ifq->ifq_head = 0; 2494 ifq->ifq_tail = 0; 2495 ifq->ifq_len = 0; 2496 IFQ_UNLOCK(ifq); 2497} 2498 2499/* 2500 * Map interface name to interface structure pointer, with or without 2501 * returning a reference. 2502 */ 2503struct ifnet * 2504ifunit_ref(const char *name) 2505{ 2506 struct ifnet *ifp; 2507 2508 IFNET_RLOCK_NOSLEEP(); 2509 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2510 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 2511 !(ifp->if_flags & IFF_DYING)) 2512 break; 2513 } 2514 if (ifp != NULL) 2515 if_ref(ifp); 2516 IFNET_RUNLOCK_NOSLEEP(); 2517 return (ifp); 2518} 2519 2520struct ifnet * 2521ifunit(const char *name) 2522{ 2523 struct ifnet *ifp; 2524 2525 IFNET_RLOCK_NOSLEEP(); 2526 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2527 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2528 break; 2529 } 2530 IFNET_RUNLOCK_NOSLEEP(); 2531 return (ifp); 2532} 2533 2534void * 2535ifr_buffer_get_buffer(void *data) 2536{ 2537 union ifreq_union *ifrup; 2538 2539 ifrup = data; 2540#ifdef COMPAT_FREEBSD32 2541 if (SV_CURPROC_FLAG(SV_ILP32)) 2542 return ((void *)(uintptr_t) 2543 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer); 2544#endif 2545 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer); 2546} 2547 2548static void 2549ifr_buffer_set_buffer_null(void *data) 2550{ 2551 union ifreq_union *ifrup; 2552 2553 ifrup = data; 2554#ifdef COMPAT_FREEBSD32 2555 if (SV_CURPROC_FLAG(SV_ILP32)) 2556 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0; 2557 else 2558#endif 2559 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL; 2560} 2561 2562size_t 2563ifr_buffer_get_length(void *data) 2564{ 2565 union ifreq_union *ifrup; 2566 2567 ifrup = data; 2568#ifdef COMPAT_FREEBSD32 2569 if (SV_CURPROC_FLAG(SV_ILP32)) 2570 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length); 2571#endif 2572 return (ifrup->ifr.ifr_ifru.ifru_buffer.length); 2573} 2574 2575static void 2576ifr_buffer_set_length(void *data, size_t len) 2577{ 2578 union ifreq_union *ifrup; 2579 2580 ifrup = data; 2581#ifdef COMPAT_FREEBSD32 2582 if (SV_CURPROC_FLAG(SV_ILP32)) 2583 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len; 2584 else 2585#endif 2586 ifrup->ifr.ifr_ifru.ifru_buffer.length = len; 2587} 2588 2589void * 2590ifr_data_get_ptr(void *ifrp) 2591{ 2592 union ifreq_union *ifrup; 2593 2594 ifrup = ifrp; 2595#ifdef COMPAT_FREEBSD32 2596 if (SV_CURPROC_FLAG(SV_ILP32)) 2597 return ((void *)(uintptr_t) 2598 ifrup->ifr32.ifr_ifru.ifru_data); 2599#endif 2600 return (ifrup->ifr.ifr_ifru.ifru_data); 2601} 2602 2603/* 2604 * Hardware specific interface ioctls. 2605 */ 2606int 2607ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2608{ 2609 struct ifreq *ifr; 2610 int error = 0, do_ifup = 0; 2611 int new_flags, temp_flags; 2612 size_t namelen, onamelen; 2613 size_t descrlen; 2614 char *descrbuf, *odescrbuf; 2615 char new_name[IFNAMSIZ]; 2616 struct ifaddr *ifa; 2617 struct sockaddr_dl *sdl; 2618 2619 ifr = (struct ifreq *)data; 2620 switch (cmd) { 2621 case SIOCGIFINDEX: 2622 ifr->ifr_index = ifp->if_index; 2623 break; 2624 2625 case SIOCGIFFLAGS: 2626 temp_flags = ifp->if_flags | ifp->if_drv_flags; 2627 ifr->ifr_flags = temp_flags & 0xffff; 2628 ifr->ifr_flagshigh = temp_flags >> 16; 2629 break; 2630 2631 case SIOCGIFCAP: 2632 ifr->ifr_reqcap = ifp->if_capabilities; 2633 ifr->ifr_curcap = ifp->if_capenable; 2634 break; 2635 2636#ifdef MAC 2637 case SIOCGIFMAC: 2638 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 2639 break; 2640#endif 2641 2642 case SIOCGIFMETRIC: 2643 ifr->ifr_metric = ifp->if_metric; 2644 break; 2645 2646 case SIOCGIFMTU: 2647 ifr->ifr_mtu = ifp->if_mtu; 2648 break; 2649 2650 case SIOCGIFPHYS: 2651 /* XXXGL: did this ever worked? */ 2652 ifr->ifr_phys = 0; 2653 break; 2654 2655 case SIOCGIFDESCR: 2656 error = 0; 2657 sx_slock(&ifdescr_sx); 2658 if (ifp->if_description == NULL) 2659 error = ENOMSG; 2660 else { 2661 /* space for terminating nul */ 2662 descrlen = strlen(ifp->if_description) + 1; 2663 if (ifr_buffer_get_length(ifr) < descrlen) 2664 ifr_buffer_set_buffer_null(ifr); 2665 else 2666 error = copyout(ifp->if_description, 2667 ifr_buffer_get_buffer(ifr), descrlen); 2668 ifr_buffer_set_length(ifr, descrlen); 2669 } 2670 sx_sunlock(&ifdescr_sx); 2671 break; 2672 2673 case SIOCSIFDESCR: 2674 error = priv_check(td, PRIV_NET_SETIFDESCR); 2675 if (error) 2676 return (error); 2677 2678 /* 2679 * Copy only (length-1) bytes to make sure that 2680 * if_description is always nul terminated. The 2681 * length parameter is supposed to count the 2682 * terminating nul in. 2683 */ 2684 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen) 2685 return (ENAMETOOLONG); 2686 else if (ifr_buffer_get_length(ifr) == 0) 2687 descrbuf = NULL; 2688 else { 2689 descrbuf = malloc(ifr_buffer_get_length(ifr), 2690 M_IFDESCR, M_WAITOK | M_ZERO); 2691 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf, 2692 ifr_buffer_get_length(ifr) - 1); 2693 if (error) { 2694 free(descrbuf, M_IFDESCR); 2695 break; 2696 } 2697 } 2698 2699 sx_xlock(&ifdescr_sx); 2700 odescrbuf = ifp->if_description; 2701 ifp->if_description = descrbuf; 2702 sx_xunlock(&ifdescr_sx); 2703 2704 getmicrotime(&ifp->if_lastchange); 2705 free(odescrbuf, M_IFDESCR); 2706 break; 2707 2708 case SIOCGIFFIB: 2709 ifr->ifr_fib = ifp->if_fib; 2710 break; 2711 2712 case SIOCSIFFIB: 2713 error = priv_check(td, PRIV_NET_SETIFFIB); 2714 if (error) 2715 return (error); 2716 if (ifr->ifr_fib >= rt_numfibs) 2717 return (EINVAL); 2718 2719 ifp->if_fib = ifr->ifr_fib; 2720 break; 2721 2722 case SIOCSIFFLAGS: 2723 error = priv_check(td, PRIV_NET_SETIFFLAGS); 2724 if (error) 2725 return (error); 2726 /* 2727 * Currently, no driver owned flags pass the IFF_CANTCHANGE 2728 * check, so we don't need special handling here yet. 2729 */ 2730 new_flags = (ifr->ifr_flags & 0xffff) | 2731 (ifr->ifr_flagshigh << 16); 2732 if (ifp->if_flags & IFF_UP && 2733 (new_flags & IFF_UP) == 0) { 2734 if_down(ifp); 2735 } else if (new_flags & IFF_UP && 2736 (ifp->if_flags & IFF_UP) == 0) { 2737 do_ifup = 1; 2738 } 2739 /* See if permanently promiscuous mode bit is about to flip */ 2740 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 2741 if (new_flags & IFF_PPROMISC) 2742 ifp->if_flags |= IFF_PROMISC; 2743 else if (ifp->if_pcount == 0) 2744 ifp->if_flags &= ~IFF_PROMISC; 2745 if (log_promisc_mode_change) 2746 if_printf(ifp, "permanently promiscuous mode %s\n", 2747 ((new_flags & IFF_PPROMISC) ? 2748 "enabled" : "disabled")); 2749 } 2750 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2751 (new_flags &~ IFF_CANTCHANGE); 2752 if (ifp->if_ioctl) { 2753 (void) (*ifp->if_ioctl)(ifp, cmd, data); 2754 } 2755 if (do_ifup) 2756 if_up(ifp); 2757 getmicrotime(&ifp->if_lastchange); 2758 break; 2759 2760 case SIOCSIFCAP: 2761 error = priv_check(td, PRIV_NET_SETIFCAP); 2762 if (error) 2763 return (error); 2764 if (ifp->if_ioctl == NULL) 2765 return (EOPNOTSUPP); 2766 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2767 return (EINVAL); 2768 error = (*ifp->if_ioctl)(ifp, cmd, data); 2769 if (error == 0) 2770 getmicrotime(&ifp->if_lastchange); 2771 break; 2772 2773#ifdef MAC 2774 case SIOCSIFMAC: 2775 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 2776 break; 2777#endif 2778 2779 case SIOCSIFNAME: 2780 error = priv_check(td, PRIV_NET_SETIFNAME); 2781 if (error) 2782 return (error); 2783 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ, 2784 NULL); 2785 if (error != 0) 2786 return (error); 2787 if (new_name[0] == '\0') 2788 return (EINVAL); 2789 if (new_name[IFNAMSIZ-1] != '\0') { 2790 new_name[IFNAMSIZ-1] = '\0'; 2791 if (strlen(new_name) == IFNAMSIZ-1) 2792 return (EINVAL); 2793 } 2794 if (strcmp(new_name, ifp->if_xname) == 0) 2795 break; 2796 if (ifunit(new_name) != NULL) 2797 return (EEXIST); 2798 2799 /* 2800 * XXX: Locking. Nothing else seems to lock if_flags, 2801 * and there are numerous other races with the 2802 * ifunit() checks not being atomic with namespace 2803 * changes (renames, vmoves, if_attach, etc). 2804 */ 2805 ifp->if_flags |= IFF_RENAMING; 2806 2807 /* Announce the departure of the interface. */ 2808 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 2809 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 2810 2811 if_printf(ifp, "changing name to '%s'\n", new_name); 2812 2813 IF_ADDR_WLOCK(ifp); 2814 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 2815 ifa = ifp->if_addr; 2816 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2817 namelen = strlen(new_name); 2818 onamelen = sdl->sdl_nlen; 2819 /* 2820 * Move the address if needed. This is safe because we 2821 * allocate space for a name of length IFNAMSIZ when we 2822 * create this in if_attach(). 2823 */ 2824 if (namelen != onamelen) { 2825 bcopy(sdl->sdl_data + onamelen, 2826 sdl->sdl_data + namelen, sdl->sdl_alen); 2827 } 2828 bcopy(new_name, sdl->sdl_data, namelen); 2829 sdl->sdl_nlen = namelen; 2830 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 2831 bzero(sdl->sdl_data, onamelen); 2832 while (namelen != 0) 2833 sdl->sdl_data[--namelen] = 0xff; 2834 IF_ADDR_WUNLOCK(ifp); 2835 2836 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 2837 /* Announce the return of the interface. */ 2838 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 2839 2840 ifp->if_flags &= ~IFF_RENAMING; 2841 break; 2842 2843#ifdef VIMAGE 2844 case SIOCSIFVNET: 2845 error = priv_check(td, PRIV_NET_SETIFVNET); 2846 if (error) 2847 return (error); 2848 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2849 break; 2850#endif 2851 2852 case SIOCSIFMETRIC: 2853 error = priv_check(td, PRIV_NET_SETIFMETRIC); 2854 if (error) 2855 return (error); 2856 ifp->if_metric = ifr->ifr_metric; 2857 getmicrotime(&ifp->if_lastchange); 2858 break; 2859 2860 case SIOCSIFPHYS: 2861 error = priv_check(td, PRIV_NET_SETIFPHYS); 2862 if (error) 2863 return (error); 2864 if (ifp->if_ioctl == NULL) 2865 return (EOPNOTSUPP); 2866 error = (*ifp->if_ioctl)(ifp, cmd, data); 2867 if (error == 0) 2868 getmicrotime(&ifp->if_lastchange); 2869 break; 2870 2871 case SIOCSIFMTU: 2872 { 2873 u_long oldmtu = ifp->if_mtu; 2874 2875 error = priv_check(td, PRIV_NET_SETIFMTU); 2876 if (error) 2877 return (error); 2878 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 2879 return (EINVAL); 2880 if (ifp->if_ioctl == NULL) 2881 return (EOPNOTSUPP); 2882 error = (*ifp->if_ioctl)(ifp, cmd, data); 2883 if (error == 0) { 2884 getmicrotime(&ifp->if_lastchange); 2885 rt_ifmsg(ifp); 2886#ifdef INET 2887 NETDUMP_REINIT(ifp); 2888#endif 2889 } 2890 /* 2891 * If the link MTU changed, do network layer specific procedure. 2892 */ 2893 if (ifp->if_mtu != oldmtu) { 2894#ifdef INET6 2895 nd6_setmtu(ifp); 2896#endif 2897 rt_updatemtu(ifp); 2898 } 2899 break; 2900 } 2901 2902 case SIOCADDMULTI: 2903 case SIOCDELMULTI: 2904 if (cmd == SIOCADDMULTI) 2905 error = priv_check(td, PRIV_NET_ADDMULTI); 2906 else 2907 error = priv_check(td, PRIV_NET_DELMULTI); 2908 if (error) 2909 return (error); 2910 2911 /* Don't allow group membership on non-multicast interfaces. */ 2912 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2913 return (EOPNOTSUPP); 2914 2915 /* Don't let users screw up protocols' entries. */ 2916 if (ifr->ifr_addr.sa_family != AF_LINK) 2917 return (EINVAL); 2918 2919 if (cmd == SIOCADDMULTI) { 2920 struct ifmultiaddr *ifma; 2921 2922 /* 2923 * Userland is only permitted to join groups once 2924 * via the if_addmulti() KPI, because it cannot hold 2925 * struct ifmultiaddr * between calls. It may also 2926 * lose a race while we check if the membership 2927 * already exists. 2928 */ 2929 IF_ADDR_RLOCK(ifp); 2930 ifma = if_findmulti(ifp, &ifr->ifr_addr); 2931 IF_ADDR_RUNLOCK(ifp); 2932 if (ifma != NULL) 2933 error = EADDRINUSE; 2934 else 2935 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2936 } else { 2937 error = if_delmulti(ifp, &ifr->ifr_addr); 2938 } 2939 if (error == 0) 2940 getmicrotime(&ifp->if_lastchange); 2941 break; 2942 2943 case SIOCSIFPHYADDR: 2944 case SIOCDIFPHYADDR: 2945#ifdef INET6 2946 case SIOCSIFPHYADDR_IN6: 2947#endif 2948 case SIOCSIFMEDIA: 2949 case SIOCSIFGENERIC: 2950 error = priv_check(td, PRIV_NET_HWIOCTL); 2951 if (error) 2952 return (error); 2953 if (ifp->if_ioctl == NULL) 2954 return (EOPNOTSUPP); 2955 error = (*ifp->if_ioctl)(ifp, cmd, data); 2956 if (error == 0) 2957 getmicrotime(&ifp->if_lastchange); 2958 break; 2959 2960 case SIOCGIFSTATUS: 2961 case SIOCGIFPSRCADDR: 2962 case SIOCGIFPDSTADDR: 2963 case SIOCGIFMEDIA: 2964 case SIOCGIFXMEDIA: 2965 case SIOCGIFGENERIC: 2966 case SIOCGIFRSSKEY: 2967 case SIOCGIFRSSHASH: 2968 case SIOCGIFDOWNREASON: 2969 if (ifp->if_ioctl == NULL) 2970 return (EOPNOTSUPP); 2971 error = (*ifp->if_ioctl)(ifp, cmd, data); 2972 break; 2973 2974 case SIOCSIFLLADDR: 2975 error = priv_check(td, PRIV_NET_SETLLADDR); 2976 if (error) 2977 return (error); 2978 error = if_setlladdr(ifp, 2979 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2980 break; 2981 2982 case SIOCGHWADDR: 2983 error = if_gethwaddr(ifp, ifr); 2984 break; 2985 2986 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP): 2987 error = priv_check(td, PRIV_NET_ADDIFGROUP); 2988 if (error) 2989 return (error); 2990 if ((error = if_addgroup(ifp, 2991 ifgr_group_get((struct ifgroupreq *)data)))) 2992 return (error); 2993 break; 2994 2995 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP): 2996 if ((error = if_getgroup((struct ifgroupreq *)data, ifp))) 2997 return (error); 2998 break; 2999 3000 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP): 3001 error = priv_check(td, PRIV_NET_DELIFGROUP); 3002 if (error) 3003 return (error); 3004 if ((error = if_delgroup(ifp, 3005 ifgr_group_get((struct ifgroupreq *)data)))) 3006 return (error); 3007 break; 3008 3009 default: 3010 error = ENOIOCTL; 3011 break; 3012 } 3013 return (error); 3014} 3015 3016#ifdef COMPAT_FREEBSD32 3017struct ifconf32 { 3018 int32_t ifc_len; 3019 union { 3020 uint32_t ifcu_buf; 3021 uint32_t ifcu_req; 3022 } ifc_ifcu; 3023}; 3024#define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) 3025#endif 3026 3027#ifdef COMPAT_FREEBSD32 3028static void 3029ifmr_init(struct ifmediareq *ifmr, caddr_t data) 3030{ 3031 struct ifmediareq32 *ifmr32; 3032 3033 ifmr32 = (struct ifmediareq32 *)data; 3034 memcpy(ifmr->ifm_name, ifmr32->ifm_name, 3035 sizeof(ifmr->ifm_name)); 3036 ifmr->ifm_current = ifmr32->ifm_current; 3037 ifmr->ifm_mask = ifmr32->ifm_mask; 3038 ifmr->ifm_status = ifmr32->ifm_status; 3039 ifmr->ifm_active = ifmr32->ifm_active; 3040 ifmr->ifm_count = ifmr32->ifm_count; 3041 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist; 3042} 3043 3044static void 3045ifmr_update(const struct ifmediareq *ifmr, caddr_t data) 3046{ 3047 struct ifmediareq32 *ifmr32; 3048 3049 ifmr32 = (struct ifmediareq32 *)data; 3050 ifmr32->ifm_current = ifmr->ifm_current; 3051 ifmr32->ifm_mask = ifmr->ifm_mask; 3052 ifmr32->ifm_status = ifmr->ifm_status; 3053 ifmr32->ifm_active = ifmr->ifm_active; 3054 ifmr32->ifm_count = ifmr->ifm_count; 3055} 3056#endif 3057 3058/* 3059 * Interface ioctls. 3060 */ 3061int 3062ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 3063{ 3064#ifdef COMPAT_FREEBSD32 3065 caddr_t saved_data = NULL; 3066 struct ifmediareq ifmr; 3067 struct ifmediareq *ifmrp; 3068#endif 3069 struct ifnet *ifp; 3070 struct ifreq *ifr; 3071 int error; 3072 int oif_flags; 3073#ifdef VIMAGE 3074 int shutdown; 3075#endif 3076 3077 CURVNET_SET(so->so_vnet); 3078#ifdef VIMAGE 3079 /* Make sure the VNET is stable. */ 3080 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET && 3081 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; 3082 if (shutdown) { 3083 CURVNET_RESTORE(); 3084 return (EBUSY); 3085 } 3086#endif 3087 3088 3089 switch (cmd) { 3090 case SIOCGIFCONF: 3091 error = ifconf(cmd, data); 3092 CURVNET_RESTORE(); 3093 return (error); 3094 3095#ifdef COMPAT_FREEBSD32 3096 case SIOCGIFCONF32: 3097 { 3098 struct ifconf32 *ifc32; 3099 struct ifconf ifc; 3100 3101 ifc32 = (struct ifconf32 *)data; 3102 ifc.ifc_len = ifc32->ifc_len; 3103 ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 3104 3105 error = ifconf(SIOCGIFCONF, (void *)&ifc); 3106 CURVNET_RESTORE(); 3107 if (error == 0) 3108 ifc32->ifc_len = ifc.ifc_len; 3109 return (error); 3110 } 3111#endif 3112 } 3113 3114#ifdef COMPAT_FREEBSD32 3115 ifmrp = NULL; 3116 switch (cmd) { 3117 case SIOCGIFMEDIA32: 3118 case SIOCGIFXMEDIA32: 3119 ifmrp = &ifmr; 3120 ifmr_init(ifmrp, data); 3121 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq); 3122 saved_data = data; 3123 data = (caddr_t)ifmrp; 3124 } 3125#endif 3126 3127 ifr = (struct ifreq *)data; 3128 switch (cmd) { 3129#ifdef VIMAGE 3130 case SIOCSIFRVNET: 3131 error = priv_check(td, PRIV_NET_SETIFVNET); 3132 if (error == 0) 3133 error = if_vmove_reclaim(td, ifr->ifr_name, 3134 ifr->ifr_jid); 3135 goto out_noref; 3136#endif 3137 case SIOCIFCREATE: 3138 case SIOCIFCREATE2: 3139 error = priv_check(td, PRIV_NET_IFCREATE); 3140 if (error == 0) 3141 error = if_clone_create(ifr->ifr_name, 3142 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ? 3143 ifr_data_get_ptr(ifr) : NULL); 3144 goto out_noref; 3145 case SIOCIFDESTROY: 3146 error = priv_check(td, PRIV_NET_IFDESTROY); 3147 3148 if (error == 0) { 3149 sx_xlock(&ifnet_detach_sxlock); 3150 error = if_clone_destroy(ifr->ifr_name); 3151 sx_xunlock(&ifnet_detach_sxlock); 3152 } 3153 goto out_noref; 3154 3155 case SIOCIFGCLONERS: 3156 error = if_clone_list((struct if_clonereq *)data); 3157 goto out_noref; 3158 3159 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB): 3160 error = if_getgroupmembers((struct ifgroupreq *)data); 3161 goto out_noref; 3162 3163#if defined(INET) || defined(INET6) 3164 case SIOCSVH: 3165 case SIOCGVH: 3166 if (carp_ioctl_p == NULL) 3167 error = EPROTONOSUPPORT; 3168 else 3169 error = (*carp_ioctl_p)(ifr, cmd, td); 3170 goto out_noref; 3171#endif 3172 } 3173 3174 ifp = ifunit_ref(ifr->ifr_name); 3175 if (ifp == NULL) { 3176 error = ENXIO; 3177 goto out_noref; 3178 } 3179 3180 error = ifhwioctl(cmd, ifp, data, td); 3181 if (error != ENOIOCTL) 3182 goto out_ref; 3183 3184 oif_flags = ifp->if_flags; 3185 if (so->so_proto == NULL) { 3186 error = EOPNOTSUPP; 3187 goto out_ref; 3188 } 3189 3190 /* 3191 * Pass the request on to the socket control method, and if the 3192 * latter returns EOPNOTSUPP, directly to the interface. 3193 * 3194 * Make an exception for the legacy SIOCSIF* requests. Drivers 3195 * trust SIOCSIFADDR et al to come from an already privileged 3196 * layer, and do not perform any credentials checks or input 3197 * validation. 3198 */ 3199 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 3200 ifp, td)); 3201 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 3202 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 3203 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 3204 error = (*ifp->if_ioctl)(ifp, cmd, data); 3205 3206 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 3207#ifdef INET6 3208 if (ifp->if_flags & IFF_UP) 3209 in6_if_up(ifp); 3210#endif 3211 } 3212 3213out_ref: 3214 if_rele(ifp); 3215out_noref: 3216#ifdef COMPAT_FREEBSD32 3217 if (ifmrp != NULL) { 3218 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA), 3219 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx", 3220 cmd)); 3221 data = saved_data; 3222 ifmr_update(ifmrp, data); 3223 } 3224#endif 3225 CURVNET_RESTORE(); 3226 return (error); 3227} 3228 3229/* 3230 * The code common to handling reference counted flags, 3231 * e.g., in ifpromisc() and if_allmulti(). 3232 * The "pflag" argument can specify a permanent mode flag to check, 3233 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 3234 * 3235 * Only to be used on stack-owned flags, not driver-owned flags. 3236 */ 3237static int 3238if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 3239{ 3240 struct ifreq ifr; 3241 int error; 3242 int oldflags, oldcount; 3243 3244 /* Sanity checks to catch programming errors */ 3245 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 3246 ("%s: setting driver-owned flag %d", __func__, flag)); 3247 3248 if (onswitch) 3249 KASSERT(*refcount >= 0, 3250 ("%s: increment negative refcount %d for flag %d", 3251 __func__, *refcount, flag)); 3252 else 3253 KASSERT(*refcount > 0, 3254 ("%s: decrement non-positive refcount %d for flag %d", 3255 __func__, *refcount, flag)); 3256 3257 /* In case this mode is permanent, just touch refcount */ 3258 if (ifp->if_flags & pflag) { 3259 *refcount += onswitch ? 1 : -1; 3260 return (0); 3261 } 3262 3263 /* Save ifnet parameters for if_ioctl() may fail */ 3264 oldcount = *refcount; 3265 oldflags = ifp->if_flags; 3266 3267 /* 3268 * See if we aren't the only and touching refcount is enough. 3269 * Actually toggle interface flag if we are the first or last. 3270 */ 3271 if (onswitch) { 3272 if ((*refcount)++) 3273 return (0); 3274 ifp->if_flags |= flag; 3275 } else { 3276 if (--(*refcount)) 3277 return (0); 3278 ifp->if_flags &= ~flag; 3279 } 3280 3281 /* Call down the driver since we've changed interface flags */ 3282 if (ifp->if_ioctl == NULL) { 3283 error = EOPNOTSUPP; 3284 goto recover; 3285 } 3286 ifr.ifr_flags = ifp->if_flags & 0xffff; 3287 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3288 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3289 if (error) 3290 goto recover; 3291 /* Notify userland that interface flags have changed */ 3292 rt_ifmsg(ifp); 3293 return (0); 3294 3295recover: 3296 /* Recover after driver error */ 3297 *refcount = oldcount; 3298 ifp->if_flags = oldflags; 3299 return (error); 3300} 3301 3302/* 3303 * Set/clear promiscuous mode on interface ifp based on the truth value 3304 * of pswitch. The calls are reference counted so that only the first 3305 * "on" request actually has an effect, as does the final "off" request. 3306 * Results are undefined if the "off" and "on" requests are not matched. 3307 */ 3308int 3309ifpromisc(struct ifnet *ifp, int pswitch) 3310{ 3311 int error; 3312 int oldflags = ifp->if_flags; 3313 3314 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 3315 &ifp->if_pcount, pswitch); 3316 /* If promiscuous mode status has changed, log a message */ 3317 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) && 3318 log_promisc_mode_change) 3319 if_printf(ifp, "promiscuous mode %s\n", 3320 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 3321 return (error); 3322} 3323 3324/* 3325 * Return interface configuration 3326 * of system. List may be used 3327 * in later ioctl's (above) to get 3328 * other information. 3329 */ 3330/*ARGSUSED*/ 3331static int 3332ifconf(u_long cmd, caddr_t data) 3333{ 3334 struct ifconf *ifc = (struct ifconf *)data; 3335 struct ifnet *ifp; 3336 struct ifaddr *ifa; 3337 struct ifreq ifr; 3338 struct sbuf *sb; 3339 int error, full = 0, valid_len, max_len; 3340 3341 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 3342 max_len = MAXPHYS - 1; 3343 3344 /* Prevent hostile input from being able to crash the system */ 3345 if (ifc->ifc_len <= 0) 3346 return (EINVAL); 3347 3348again: 3349 if (ifc->ifc_len <= max_len) { 3350 max_len = ifc->ifc_len; 3351 full = 1; 3352 } 3353 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 3354 max_len = 0; 3355 valid_len = 0; 3356 3357 IFNET_RLOCK(); 3358 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 3359 int addrs; 3360 3361 /* 3362 * Zero the ifr to make sure we don't disclose the contents 3363 * of the stack. 3364 */ 3365 memset(&ifr, 0, sizeof(ifr)); 3366 3367 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 3368 >= sizeof(ifr.ifr_name)) { 3369 sbuf_delete(sb); 3370 IFNET_RUNLOCK(); 3371 return (ENAMETOOLONG); 3372 } 3373 3374 addrs = 0; 3375 IF_ADDR_RLOCK(ifp); 3376 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3377 struct sockaddr *sa = ifa->ifa_addr; 3378 3379 if (prison_if(curthread->td_ucred, sa) != 0) 3380 continue; 3381 addrs++; 3382 if (sa->sa_len <= sizeof(*sa)) { 3383 if (sa->sa_len < sizeof(*sa)) { 3384 memset(&ifr.ifr_ifru.ifru_addr, 0, 3385 sizeof(ifr.ifr_ifru.ifru_addr)); 3386 memcpy(&ifr.ifr_ifru.ifru_addr, sa, 3387 sa->sa_len); 3388 } else 3389 ifr.ifr_ifru.ifru_addr = *sa; 3390 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3391 max_len += sizeof(ifr); 3392 } else { 3393 sbuf_bcat(sb, &ifr, 3394 offsetof(struct ifreq, ifr_addr)); 3395 max_len += offsetof(struct ifreq, ifr_addr); 3396 sbuf_bcat(sb, sa, sa->sa_len); 3397 max_len += sa->sa_len; 3398 } 3399 3400 if (sbuf_error(sb) == 0) 3401 valid_len = sbuf_len(sb); 3402 } 3403 IF_ADDR_RUNLOCK(ifp); 3404 if (addrs == 0) { 3405 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3406 max_len += sizeof(ifr); 3407 3408 if (sbuf_error(sb) == 0) 3409 valid_len = sbuf_len(sb); 3410 } 3411 } 3412 IFNET_RUNLOCK(); 3413 3414 /* 3415 * If we didn't allocate enough space (uncommon), try again. If 3416 * we have already allocated as much space as we are allowed, 3417 * return what we've got. 3418 */ 3419 if (valid_len != max_len && !full) { 3420 sbuf_delete(sb); 3421 goto again; 3422 } 3423 3424 ifc->ifc_len = valid_len; 3425 sbuf_finish(sb); 3426 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 3427 sbuf_delete(sb); 3428 return (error); 3429} 3430 3431/* 3432 * Just like ifpromisc(), but for all-multicast-reception mode. 3433 */ 3434int 3435if_allmulti(struct ifnet *ifp, int onswitch) 3436{ 3437 3438 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 3439} 3440 3441struct ifmultiaddr * 3442if_findmulti(struct ifnet *ifp, const struct sockaddr *sa) 3443{ 3444 struct ifmultiaddr *ifma; 3445 3446 IF_ADDR_LOCK_ASSERT(ifp); 3447 3448 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3449 if (sa->sa_family == AF_LINK) { 3450 if (sa_dl_equal(ifma->ifma_addr, sa)) 3451 break; 3452 } else { 3453 if (sa_equal(ifma->ifma_addr, sa)) 3454 break; 3455 } 3456 } 3457 3458 return ifma; 3459} 3460 3461/* 3462 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 3463 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 3464 * the ifnet multicast address list here, so the caller must do that and 3465 * other setup work (such as notifying the device driver). The reference 3466 * count is initialized to 1. 3467 */ 3468static struct ifmultiaddr * 3469if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 3470 int mflags) 3471{ 3472 struct ifmultiaddr *ifma; 3473 struct sockaddr *dupsa; 3474 3475 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 3476 M_ZERO); 3477 if (ifma == NULL) 3478 return (NULL); 3479 3480 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 3481 if (dupsa == NULL) { 3482 free(ifma, M_IFMADDR); 3483 return (NULL); 3484 } 3485 bcopy(sa, dupsa, sa->sa_len); 3486 ifma->ifma_addr = dupsa; 3487 3488 ifma->ifma_ifp = ifp; 3489 ifma->ifma_refcount = 1; 3490 ifma->ifma_protospec = NULL; 3491 3492 if (llsa == NULL) { 3493 ifma->ifma_lladdr = NULL; 3494 return (ifma); 3495 } 3496 3497 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 3498 if (dupsa == NULL) { 3499 free(ifma->ifma_addr, M_IFMADDR); 3500 free(ifma, M_IFMADDR); 3501 return (NULL); 3502 } 3503 bcopy(llsa, dupsa, llsa->sa_len); 3504 ifma->ifma_lladdr = dupsa; 3505 3506 return (ifma); 3507} 3508 3509/* 3510 * if_freemulti: free ifmultiaddr structure and possibly attached related 3511 * addresses. The caller is responsible for implementing reference 3512 * counting, notifying the driver, handling routing messages, and releasing 3513 * any dependent link layer state. 3514 */ 3515#ifdef MCAST_VERBOSE 3516extern void kdb_backtrace(void); 3517#endif 3518static void 3519if_freemulti_internal(struct ifmultiaddr *ifma) 3520{ 3521 3522 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 3523 ifma->ifma_refcount)); 3524 3525 if (ifma->ifma_lladdr != NULL) 3526 free(ifma->ifma_lladdr, M_IFMADDR); 3527#ifdef MCAST_VERBOSE 3528 kdb_backtrace(); 3529 printf("%s freeing ifma: %p\n", __func__, ifma); 3530#endif 3531 free(ifma->ifma_addr, M_IFMADDR); 3532 free(ifma, M_IFMADDR); 3533} 3534 3535static void 3536if_destroymulti(epoch_context_t ctx) 3537{ 3538 struct ifmultiaddr *ifma; 3539 3540 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx); 3541 if_freemulti_internal(ifma); 3542} 3543 3544void 3545if_freemulti(struct ifmultiaddr *ifma) 3546{ 3547 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d", 3548 ifma->ifma_refcount)); 3549 3550 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti); 3551} 3552 3553 3554/* 3555 * Register an additional multicast address with a network interface. 3556 * 3557 * - If the address is already present, bump the reference count on the 3558 * address and return. 3559 * - If the address is not link-layer, look up a link layer address. 3560 * - Allocate address structures for one or both addresses, and attach to the 3561 * multicast address list on the interface. If automatically adding a link 3562 * layer address, the protocol address will own a reference to the link 3563 * layer address, to be freed when it is freed. 3564 * - Notify the network device driver of an addition to the multicast address 3565 * list. 3566 * 3567 * 'sa' points to caller-owned memory with the desired multicast address. 3568 * 3569 * 'retifma' will be used to return a pointer to the resulting multicast 3570 * address reference, if desired. 3571 */ 3572int 3573if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 3574 struct ifmultiaddr **retifma) 3575{ 3576 struct ifmultiaddr *ifma, *ll_ifma; 3577 struct sockaddr *llsa; 3578 struct sockaddr_dl sdl; 3579 int error; 3580 3581#ifdef INET 3582 IN_MULTI_LIST_UNLOCK_ASSERT(); 3583#endif 3584#ifdef INET6 3585 IN6_MULTI_LIST_UNLOCK_ASSERT(); 3586#endif 3587 /* 3588 * If the address is already present, return a new reference to it; 3589 * otherwise, allocate storage and set up a new address. 3590 */ 3591 IF_ADDR_WLOCK(ifp); 3592 ifma = if_findmulti(ifp, sa); 3593 if (ifma != NULL) { 3594 ifma->ifma_refcount++; 3595 if (retifma != NULL) 3596 *retifma = ifma; 3597 IF_ADDR_WUNLOCK(ifp); 3598 return (0); 3599 } 3600 3601 /* 3602 * The address isn't already present; resolve the protocol address 3603 * into a link layer address, and then look that up, bump its 3604 * refcount or allocate an ifma for that also. 3605 * Most link layer resolving functions returns address data which 3606 * fits inside default sockaddr_dl structure. However callback 3607 * can allocate another sockaddr structure, in that case we need to 3608 * free it later. 3609 */ 3610 llsa = NULL; 3611 ll_ifma = NULL; 3612 if (ifp->if_resolvemulti != NULL) { 3613 /* Provide called function with buffer size information */ 3614 sdl.sdl_len = sizeof(sdl); 3615 llsa = (struct sockaddr *)&sdl; 3616 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3617 if (error) 3618 goto unlock_out; 3619 } 3620 3621 /* 3622 * Allocate the new address. Don't hook it up yet, as we may also 3623 * need to allocate a link layer multicast address. 3624 */ 3625 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3626 if (ifma == NULL) { 3627 error = ENOMEM; 3628 goto free_llsa_out; 3629 } 3630 3631 /* 3632 * If a link layer address is found, we'll need to see if it's 3633 * already present in the address list, or allocate is as well. 3634 * When this block finishes, the link layer address will be on the 3635 * list. 3636 */ 3637 if (llsa != NULL) { 3638 ll_ifma = if_findmulti(ifp, llsa); 3639 if (ll_ifma == NULL) { 3640 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3641 if (ll_ifma == NULL) { 3642 --ifma->ifma_refcount; 3643 if_freemulti(ifma); 3644 error = ENOMEM; 3645 goto free_llsa_out; 3646 } 3647 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED; 3648 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3649 ifma_link); 3650 } else 3651 ll_ifma->ifma_refcount++; 3652 ifma->ifma_llifma = ll_ifma; 3653 } 3654 3655 /* 3656 * We now have a new multicast address, ifma, and possibly a new or 3657 * referenced link layer address. Add the primary address to the 3658 * ifnet address list. 3659 */ 3660 ifma->ifma_flags |= IFMA_F_ENQUEUED; 3661 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3662 3663 if (retifma != NULL) 3664 *retifma = ifma; 3665 3666 /* 3667 * Must generate the message while holding the lock so that 'ifma' 3668 * pointer is still valid. 3669 */ 3670 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3671 IF_ADDR_WUNLOCK(ifp); 3672 3673 /* 3674 * We are certain we have added something, so call down to the 3675 * interface to let them know about it. 3676 */ 3677 if (ifp->if_ioctl != NULL) { 3678 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3679 } 3680 3681 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3682 link_free_sdl(llsa); 3683 3684 return (0); 3685 3686free_llsa_out: 3687 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3688 link_free_sdl(llsa); 3689 3690unlock_out: 3691 IF_ADDR_WUNLOCK(ifp); 3692 return (error); 3693} 3694 3695/* 3696 * Delete a multicast group membership by network-layer group address. 3697 * 3698 * Returns ENOENT if the entry could not be found. If ifp no longer 3699 * exists, results are undefined. This entry point should only be used 3700 * from subsystems which do appropriate locking to hold ifp for the 3701 * duration of the call. 3702 * Network-layer protocol domains must use if_delmulti_ifma(). 3703 */ 3704int 3705if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3706{ 3707 struct ifmultiaddr *ifma; 3708 int lastref; 3709#ifdef INVARIANTS 3710 struct ifnet *oifp; 3711 3712 IFNET_RLOCK_NOSLEEP(); 3713 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3714 if (ifp == oifp) 3715 break; 3716 if (ifp != oifp) 3717 ifp = NULL; 3718 IFNET_RUNLOCK_NOSLEEP(); 3719 3720 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3721#endif 3722 if (ifp == NULL) 3723 return (ENOENT); 3724 3725 IF_ADDR_WLOCK(ifp); 3726 lastref = 0; 3727 ifma = if_findmulti(ifp, sa); 3728 if (ifma != NULL) 3729 lastref = if_delmulti_locked(ifp, ifma, 0); 3730 IF_ADDR_WUNLOCK(ifp); 3731 3732 if (ifma == NULL) 3733 return (ENOENT); 3734 3735 if (lastref && ifp->if_ioctl != NULL) { 3736 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3737 } 3738 3739 return (0); 3740} 3741 3742/* 3743 * Delete all multicast group membership for an interface. 3744 * Should be used to quickly flush all multicast filters. 3745 */ 3746void 3747if_delallmulti(struct ifnet *ifp) 3748{ 3749 struct ifmultiaddr *ifma; 3750 struct ifmultiaddr *next; 3751 3752 IF_ADDR_WLOCK(ifp); 3753 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3754 if_delmulti_locked(ifp, ifma, 0); 3755 IF_ADDR_WUNLOCK(ifp); 3756} 3757 3758void 3759if_delmulti_ifma(struct ifmultiaddr *ifma) 3760{ 3761 if_delmulti_ifma_flags(ifma, 0); 3762} 3763 3764/* 3765 * Delete a multicast group membership by group membership pointer. 3766 * Network-layer protocol domains must use this routine. 3767 * 3768 * It is safe to call this routine if the ifp disappeared. 3769 */ 3770void 3771if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags) 3772{ 3773 struct ifnet *ifp; 3774 int lastref; 3775 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma); 3776#ifdef INET 3777 IN_MULTI_LIST_UNLOCK_ASSERT(); 3778#endif 3779 ifp = ifma->ifma_ifp; 3780#ifdef DIAGNOSTIC 3781 if (ifp == NULL) { 3782 printf("%s: ifma_ifp seems to be detached\n", __func__); 3783 } else { 3784 struct ifnet *oifp; 3785 3786 IFNET_RLOCK_NOSLEEP(); 3787 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3788 if (ifp == oifp) 3789 break; 3790 if (ifp != oifp) 3791 ifp = NULL; 3792 IFNET_RUNLOCK_NOSLEEP(); 3793 } 3794#endif 3795 /* 3796 * If and only if the ifnet instance exists: Acquire the address lock. 3797 */ 3798 if (ifp != NULL) 3799 IF_ADDR_WLOCK(ifp); 3800 3801 lastref = if_delmulti_locked(ifp, ifma, flags); 3802 3803 if (ifp != NULL) { 3804 /* 3805 * If and only if the ifnet instance exists: 3806 * Release the address lock. 3807 * If the group was left: update the hardware hash filter. 3808 */ 3809 IF_ADDR_WUNLOCK(ifp); 3810 if (lastref && ifp->if_ioctl != NULL) { 3811 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3812 } 3813 } 3814} 3815 3816/* 3817 * Perform deletion of network-layer and/or link-layer multicast address. 3818 * 3819 * Return 0 if the reference count was decremented. 3820 * Return 1 if the final reference was released, indicating that the 3821 * hardware hash filter should be reprogrammed. 3822 */ 3823static int 3824if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3825{ 3826 struct ifmultiaddr *ll_ifma; 3827 3828 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3829 KASSERT(ifma->ifma_ifp == ifp, 3830 ("%s: inconsistent ifp %p", __func__, ifp)); 3831 IF_ADDR_WLOCK_ASSERT(ifp); 3832 } 3833 3834 ifp = ifma->ifma_ifp; 3835 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : ""); 3836 3837 /* 3838 * If the ifnet is detaching, null out references to ifnet, 3839 * so that upper protocol layers will notice, and not attempt 3840 * to obtain locks for an ifnet which no longer exists. The 3841 * routing socket announcement must happen before the ifnet 3842 * instance is detached from the system. 3843 */ 3844 if (detaching) { 3845#ifdef DIAGNOSTIC 3846 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3847#endif 3848 /* 3849 * ifp may already be nulled out if we are being reentered 3850 * to delete the ll_ifma. 3851 */ 3852 if (ifp != NULL) { 3853 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3854 ifma->ifma_ifp = NULL; 3855 } 3856 } 3857 3858 if (--ifma->ifma_refcount > 0) 3859 return 0; 3860 3861 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) { 3862 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 3863 ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3864 } 3865 /* 3866 * If this ifma is a network-layer ifma, a link-layer ifma may 3867 * have been associated with it. Release it first if so. 3868 */ 3869 ll_ifma = ifma->ifma_llifma; 3870 if (ll_ifma != NULL) { 3871 KASSERT(ifma->ifma_lladdr != NULL, 3872 ("%s: llifma w/o lladdr", __func__)); 3873 if (detaching) 3874 ll_ifma->ifma_ifp = NULL; /* XXX */ 3875 if (--ll_ifma->ifma_refcount == 0) { 3876 if (ifp != NULL) { 3877 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) { 3878 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, 3879 ifma_link); 3880 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3881 } 3882 } 3883 if_freemulti(ll_ifma); 3884 } 3885 } 3886#ifdef INVARIANTS 3887 if (ifp) { 3888 struct ifmultiaddr *ifmatmp; 3889 3890 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link) 3891 MPASS(ifma != ifmatmp); 3892 } 3893#endif 3894 if_freemulti(ifma); 3895 /* 3896 * The last reference to this instance of struct ifmultiaddr 3897 * was released; the hardware should be notified of this change. 3898 */ 3899 return 1; 3900} 3901 3902/* 3903 * Set the link layer address on an interface. 3904 * 3905 * At this time we only support certain types of interfaces, 3906 * and we don't allow the length of the address to change. 3907 * 3908 * Set noinline to be dtrace-friendly 3909 */ 3910__noinline int 3911if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3912{ 3913 struct sockaddr_dl *sdl; 3914 struct ifaddr *ifa; 3915 struct ifreq ifr; 3916 int rc; 3917 3918 rc = 0; 3919 NET_EPOCH_ENTER(); 3920 ifa = ifp->if_addr; 3921 if (ifa == NULL) { 3922 rc = EINVAL; 3923 goto out; 3924 } 3925 3926 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3927 if (sdl == NULL) { 3928 rc = EINVAL; 3929 goto out; 3930 } 3931 if (len != sdl->sdl_alen) { /* don't allow length to change */ 3932 rc = EINVAL; 3933 goto out; 3934 } 3935 switch (ifp->if_type) { 3936 case IFT_ETHER: 3937 case IFT_XETHER: 3938 case IFT_L2VLAN: 3939 case IFT_BRIDGE: 3940 case IFT_IEEE8023ADLAG: 3941 bcopy(lladdr, LLADDR(sdl), len); 3942 break; 3943 default: 3944 rc = ENODEV; 3945 goto out; 3946 } 3947 3948 /* 3949 * If the interface is already up, we need 3950 * to re-init it in order to reprogram its 3951 * address filter. 3952 */ 3953 NET_EPOCH_EXIT(); 3954 if ((ifp->if_flags & IFF_UP) != 0) { 3955 if (ifp->if_ioctl) { 3956 ifp->if_flags &= ~IFF_UP; 3957 ifr.ifr_flags = ifp->if_flags & 0xffff; 3958 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3959 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3960 ifp->if_flags |= IFF_UP; 3961 ifr.ifr_flags = ifp->if_flags & 0xffff; 3962 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3963 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3964 } 3965 } 3966 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 3967 return (0); 3968 out: 3969 NET_EPOCH_EXIT(); 3970 return (rc); 3971} 3972 3973/* 3974 * Compat function for handling basic encapsulation requests. 3975 * Not converted stacks (FDDI, IB, ..) supports traditional 3976 * output model: ARP (and other similar L2 protocols) are handled 3977 * inside output routine, arpresolve/nd6_resolve() returns MAC 3978 * address instead of full prepend. 3979 * 3980 * This function creates calculated header==MAC for IPv4/IPv6 and 3981 * returns EAFNOSUPPORT (which is then handled in ARP code) for other 3982 * address families. 3983 */ 3984static int 3985if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req) 3986{ 3987 3988 if (req->rtype != IFENCAP_LL) 3989 return (EOPNOTSUPP); 3990 3991 if (req->bufsize < req->lladdr_len) 3992 return (ENOMEM); 3993 3994 switch (req->family) { 3995 case AF_INET: 3996 case AF_INET6: 3997 break; 3998 default: 3999 return (EAFNOSUPPORT); 4000 } 4001 4002 /* Copy lladdr to storage as is */ 4003 memmove(req->buf, req->lladdr, req->lladdr_len); 4004 req->bufsize = req->lladdr_len; 4005 req->lladdr_off = 0; 4006 4007 return (0); 4008} 4009 4010/* 4011 * Tunnel interfaces can nest, also they may cause infinite recursion 4012 * calls when misconfigured. We'll prevent this by detecting loops. 4013 * High nesting level may cause stack exhaustion. We'll prevent this 4014 * by introducing upper limit. 4015 * 4016 * Return 0, if tunnel nesting count is equal or less than limit. 4017 */ 4018int 4019if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie, 4020 int limit) 4021{ 4022 struct m_tag *mtag; 4023 int count; 4024 4025 count = 1; 4026 mtag = NULL; 4027 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) { 4028 if (*(struct ifnet **)(mtag + 1) == ifp) { 4029 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp)); 4030 return (EIO); 4031 } 4032 count++; 4033 } 4034 if (count > limit) { 4035 log(LOG_NOTICE, 4036 "%s: if_output recursively called too many times(%d)\n", 4037 if_name(ifp), count); 4038 return (EIO); 4039 } 4040 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT); 4041 if (mtag == NULL) 4042 return (ENOMEM); 4043 *(struct ifnet **)(mtag + 1) = ifp; 4044 m_tag_prepend(m, mtag); 4045 return (0); 4046} 4047 4048/* 4049 * Get the link layer address that was read from the hardware at attach. 4050 * 4051 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type 4052 * their component interfaces as IFT_IEEE8023ADLAG. 4053 */ 4054int 4055if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr) 4056{ 4057 4058 if (ifp->if_hw_addr == NULL) 4059 return (ENODEV); 4060 4061 switch (ifp->if_type) { 4062 case IFT_ETHER: 4063 case IFT_IEEE8023ADLAG: 4064 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen); 4065 return (0); 4066 default: 4067 return (ENODEV); 4068 } 4069} 4070 4071/* 4072 * The name argument must be a pointer to storage which will last as 4073 * long as the interface does. For physical devices, the result of 4074 * device_get_name(dev) is a good choice and for pseudo-devices a 4075 * static string works well. 4076 */ 4077void 4078if_initname(struct ifnet *ifp, const char *name, int unit) 4079{ 4080 ifp->if_dname = name; 4081 ifp->if_dunit = unit; 4082 if (unit != IF_DUNIT_NONE) 4083 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 4084 else 4085 strlcpy(ifp->if_xname, name, IFNAMSIZ); 4086} 4087 4088int 4089if_printf(struct ifnet *ifp, const char *fmt, ...) 4090{ 4091 char if_fmt[256]; 4092 va_list ap; 4093 4094 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt); 4095 va_start(ap, fmt); 4096 vlog(LOG_INFO, if_fmt, ap); 4097 va_end(ap); 4098 return (0); 4099} 4100 4101void 4102if_start(struct ifnet *ifp) 4103{ 4104 4105 (*(ifp)->if_start)(ifp); 4106} 4107 4108/* 4109 * Backwards compatibility interface for drivers 4110 * that have not implemented it 4111 */ 4112static int 4113if_transmit(struct ifnet *ifp, struct mbuf *m) 4114{ 4115 int error; 4116 4117 IFQ_HANDOFF(ifp, m, error); 4118 return (error); 4119} 4120 4121static void 4122if_input_default(struct ifnet *ifp __unused, struct mbuf *m) 4123{ 4124 4125 m_freem(m); 4126} 4127 4128int 4129if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 4130{ 4131 int active = 0; 4132 4133 IF_LOCK(ifq); 4134 if (_IF_QFULL(ifq)) { 4135 IF_UNLOCK(ifq); 4136 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 4137 m_freem(m); 4138 return (0); 4139 } 4140 if (ifp != NULL) { 4141 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust); 4142 if (m->m_flags & (M_BCAST|M_MCAST)) 4143 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 4144 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 4145 } 4146 _IF_ENQUEUE(ifq, m); 4147 IF_UNLOCK(ifq); 4148 if (ifp != NULL && !active) 4149 (*(ifp)->if_start)(ifp); 4150 return (1); 4151} 4152 4153void 4154if_register_com_alloc(u_char type, 4155 if_com_alloc_t *a, if_com_free_t *f) 4156{ 4157 4158 KASSERT(if_com_alloc[type] == NULL, 4159 ("if_register_com_alloc: %d already registered", type)); 4160 KASSERT(if_com_free[type] == NULL, 4161 ("if_register_com_alloc: %d free already registered", type)); 4162 4163 if_com_alloc[type] = a; 4164 if_com_free[type] = f; 4165} 4166 4167void 4168if_deregister_com_alloc(u_char type) 4169{ 4170 4171 KASSERT(if_com_alloc[type] != NULL, 4172 ("if_deregister_com_alloc: %d not registered", type)); 4173 KASSERT(if_com_free[type] != NULL, 4174 ("if_deregister_com_alloc: %d free not registered", type)); 4175 4176 /* 4177 * Ensure all pending EPOCH(9) callbacks have been executed. This 4178 * fixes issues about late invocation of if_destroy(), which leads 4179 * to memory leak from if_com_alloc[type] allocated if_l2com. 4180 */ 4181 epoch_drain_callbacks(net_epoch_preempt); 4182 4183 if_com_alloc[type] = NULL; 4184 if_com_free[type] = NULL; 4185} 4186 4187/* API for driver access to network stack owned ifnet.*/ 4188uint64_t 4189if_setbaudrate(struct ifnet *ifp, uint64_t baudrate) 4190{ 4191 uint64_t oldbrate; 4192 4193 oldbrate = ifp->if_baudrate; 4194 ifp->if_baudrate = baudrate; 4195 return (oldbrate); 4196} 4197 4198uint64_t 4199if_getbaudrate(if_t ifp) 4200{ 4201 4202 return (((struct ifnet *)ifp)->if_baudrate); 4203} 4204 4205int 4206if_setcapabilities(if_t ifp, int capabilities) 4207{ 4208 ((struct ifnet *)ifp)->if_capabilities = capabilities; 4209 return (0); 4210} 4211 4212int 4213if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) 4214{ 4215 ((struct ifnet *)ifp)->if_capabilities |= setbit; 4216 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; 4217 4218 return (0); 4219} 4220 4221int 4222if_getcapabilities(if_t ifp) 4223{ 4224 return ((struct ifnet *)ifp)->if_capabilities; 4225} 4226 4227int 4228if_setcapenable(if_t ifp, int capabilities) 4229{ 4230 ((struct ifnet *)ifp)->if_capenable = capabilities; 4231 return (0); 4232} 4233 4234int 4235if_setcapenablebit(if_t ifp, int setcap, int clearcap) 4236{ 4237 if(setcap) 4238 ((struct ifnet *)ifp)->if_capenable |= setcap; 4239 if(clearcap) 4240 ((struct ifnet *)ifp)->if_capenable &= ~clearcap; 4241 4242 return (0); 4243} 4244 4245const char * 4246if_getdname(if_t ifp) 4247{ 4248 return ((struct ifnet *)ifp)->if_dname; 4249} 4250 4251int 4252if_togglecapenable(if_t ifp, int togglecap) 4253{ 4254 ((struct ifnet *)ifp)->if_capenable ^= togglecap; 4255 return (0); 4256} 4257 4258int 4259if_getcapenable(if_t ifp) 4260{ 4261 return ((struct ifnet *)ifp)->if_capenable; 4262} 4263 4264/* 4265 * This is largely undesirable because it ties ifnet to a device, but does 4266 * provide flexiblity for an embedded product vendor. Should be used with 4267 * the understanding that it violates the interface boundaries, and should be 4268 * a last resort only. 4269 */ 4270int 4271if_setdev(if_t ifp, void *dev) 4272{ 4273 return (0); 4274} 4275 4276int 4277if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) 4278{ 4279 ((struct ifnet *)ifp)->if_drv_flags |= set_flags; 4280 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; 4281 4282 return (0); 4283} 4284 4285int 4286if_getdrvflags(if_t ifp) 4287{ 4288 return ((struct ifnet *)ifp)->if_drv_flags; 4289} 4290 4291int 4292if_setdrvflags(if_t ifp, int flags) 4293{ 4294 ((struct ifnet *)ifp)->if_drv_flags = flags; 4295 return (0); 4296} 4297 4298 4299int 4300if_setflags(if_t ifp, int flags) 4301{ 4302 ((struct ifnet *)ifp)->if_flags = flags; 4303 return (0); 4304} 4305 4306int 4307if_setflagbits(if_t ifp, int set, int clear) 4308{ 4309 ((struct ifnet *)ifp)->if_flags |= set; 4310 ((struct ifnet *)ifp)->if_flags &= ~clear; 4311 4312 return (0); 4313} 4314 4315int 4316if_getflags(if_t ifp) 4317{ 4318 return ((struct ifnet *)ifp)->if_flags; 4319} 4320 4321int 4322if_clearhwassist(if_t ifp) 4323{ 4324 ((struct ifnet *)ifp)->if_hwassist = 0; 4325 return (0); 4326} 4327 4328int 4329if_sethwassistbits(if_t ifp, int toset, int toclear) 4330{ 4331 ((struct ifnet *)ifp)->if_hwassist |= toset; 4332 ((struct ifnet *)ifp)->if_hwassist &= ~toclear; 4333 4334 return (0); 4335} 4336 4337int 4338if_sethwassist(if_t ifp, int hwassist_bit) 4339{ 4340 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; 4341 return (0); 4342} 4343 4344int 4345if_gethwassist(if_t ifp) 4346{ 4347 return ((struct ifnet *)ifp)->if_hwassist; 4348} 4349 4350int 4351if_setmtu(if_t ifp, int mtu) 4352{ 4353 ((struct ifnet *)ifp)->if_mtu = mtu; 4354 return (0); 4355} 4356 4357int 4358if_getmtu(if_t ifp) 4359{ 4360 return ((struct ifnet *)ifp)->if_mtu; 4361} 4362 4363int 4364if_getmtu_family(if_t ifp, int family) 4365{ 4366 struct domain *dp; 4367 4368 for (dp = domains; dp; dp = dp->dom_next) { 4369 if (dp->dom_family == family && dp->dom_ifmtu != NULL) 4370 return (dp->dom_ifmtu((struct ifnet *)ifp)); 4371 } 4372 4373 return (((struct ifnet *)ifp)->if_mtu); 4374} 4375 4376/* 4377 * Methods for drivers to access interface unicast and multicast 4378 * link level addresses. Driver shall not know 'struct ifaddr' neither 4379 * 'struct ifmultiaddr'. 4380 */ 4381u_int 4382if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4383{ 4384 struct ifaddr *ifa; 4385 u_int count; 4386 4387 MPASS(cb); 4388 4389 count = 0; 4390 IF_ADDR_RLOCK(ifp); 4391 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 4392 if (ifa->ifa_addr->sa_family != AF_LINK) 4393 continue; 4394 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr, 4395 count); 4396 } 4397 IF_ADDR_RUNLOCK(ifp); 4398 4399 return (count); 4400} 4401 4402u_int 4403if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4404{ 4405 struct ifmultiaddr *ifma; 4406 u_int count; 4407 4408 MPASS(cb); 4409 4410 count = 0; 4411 IF_ADDR_RLOCK(ifp); 4412 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 4413 if (ifma->ifma_addr->sa_family != AF_LINK) 4414 continue; 4415 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr, 4416 count); 4417 } 4418 IF_ADDR_RUNLOCK(ifp); 4419 4420 return (count); 4421} 4422 4423int 4424if_setsoftc(if_t ifp, void *softc) 4425{ 4426 ((struct ifnet *)ifp)->if_softc = softc; 4427 return (0); 4428} 4429 4430void * 4431if_getsoftc(if_t ifp) 4432{ 4433 return ((struct ifnet *)ifp)->if_softc; 4434} 4435 4436void 4437if_setrcvif(struct mbuf *m, if_t ifp) 4438{ 4439 m->m_pkthdr.rcvif = (struct ifnet *)ifp; 4440} 4441 4442void 4443if_setvtag(struct mbuf *m, uint16_t tag) 4444{ 4445 m->m_pkthdr.ether_vtag = tag; 4446} 4447 4448uint16_t 4449if_getvtag(struct mbuf *m) 4450{ 4451 4452 return (m->m_pkthdr.ether_vtag); 4453} 4454 4455int 4456if_sendq_empty(if_t ifp) 4457{ 4458 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); 4459} 4460 4461struct ifaddr * 4462if_getifaddr(if_t ifp) 4463{ 4464 return ((struct ifnet *)ifp)->if_addr; 4465} 4466 4467int 4468if_getamcount(if_t ifp) 4469{ 4470 return ((struct ifnet *)ifp)->if_amcount; 4471} 4472 4473 4474int 4475if_setsendqready(if_t ifp) 4476{ 4477 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); 4478 return (0); 4479} 4480 4481int 4482if_setsendqlen(if_t ifp, int tx_desc_count) 4483{ 4484 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); 4485 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; 4486 4487 return (0); 4488} 4489 4490int 4491if_vlantrunkinuse(if_t ifp) 4492{ 4493 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; 4494} 4495 4496int 4497if_input(if_t ifp, struct mbuf* sendmp) 4498{ 4499 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); 4500 return (0); 4501 4502} 4503 4504/* XXX */ 4505#ifndef ETH_ADDR_LEN 4506#define ETH_ADDR_LEN 6 4507#endif 4508 4509int 4510if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max) 4511{ 4512 struct ifmultiaddr *ifma; 4513 uint8_t *lmta = (uint8_t *)mta; 4514 int mcnt = 0; 4515 4516 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { 4517 if (ifma->ifma_addr->sa_family != AF_LINK) 4518 continue; 4519 4520 if (mcnt == max) 4521 break; 4522 4523 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 4524 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN); 4525 mcnt++; 4526 } 4527 *cnt = mcnt; 4528 4529 return (0); 4530} 4531 4532int 4533if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max) 4534{ 4535 int error; 4536 4537 if_maddr_rlock(ifp); 4538 error = if_setupmultiaddr(ifp, mta, cnt, max); 4539 if_maddr_runlock(ifp); 4540 return (error); 4541} 4542 4543int 4544if_multiaddr_count(if_t ifp, int max) 4545{ 4546 struct ifmultiaddr *ifma; 4547 int count; 4548 4549 count = 0; 4550 if_maddr_rlock(ifp); 4551 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { 4552 if (ifma->ifma_addr->sa_family != AF_LINK) 4553 continue; 4554 count++; 4555 if (count == max) 4556 break; 4557 } 4558 if_maddr_runlock(ifp); 4559 return (count); 4560} 4561 4562int 4563if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg) 4564{ 4565 struct ifmultiaddr *ifma; 4566 int cnt = 0; 4567 4568 if_maddr_rlock(ifp); 4569 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 4570 cnt += filter(arg, ifma, cnt); 4571 if_maddr_runlock(ifp); 4572 return (cnt); 4573} 4574 4575struct mbuf * 4576if_dequeue(if_t ifp) 4577{ 4578 struct mbuf *m; 4579 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); 4580 4581 return (m); 4582} 4583 4584int 4585if_sendq_prepend(if_t ifp, struct mbuf *m) 4586{ 4587 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); 4588 return (0); 4589} 4590 4591int 4592if_setifheaderlen(if_t ifp, int len) 4593{ 4594 ((struct ifnet *)ifp)->if_hdrlen = len; 4595 return (0); 4596} 4597 4598caddr_t 4599if_getlladdr(if_t ifp) 4600{ 4601 return (IF_LLADDR((struct ifnet *)ifp)); 4602} 4603 4604void * 4605if_gethandle(u_char type) 4606{ 4607 return (if_alloc(type)); 4608} 4609 4610void 4611if_bpfmtap(if_t ifh, struct mbuf *m) 4612{ 4613 struct ifnet *ifp = (struct ifnet *)ifh; 4614 4615 BPF_MTAP(ifp, m); 4616} 4617 4618void 4619if_etherbpfmtap(if_t ifh, struct mbuf *m) 4620{ 4621 struct ifnet *ifp = (struct ifnet *)ifh; 4622 4623 ETHER_BPF_MTAP(ifp, m); 4624} 4625 4626void 4627if_vlancap(if_t ifh) 4628{ 4629 struct ifnet *ifp = (struct ifnet *)ifh; 4630 VLAN_CAPABILITIES(ifp); 4631} 4632 4633int 4634if_sethwtsomax(if_t ifp, u_int if_hw_tsomax) 4635{ 4636 4637 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax; 4638 return (0); 4639} 4640 4641int 4642if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount) 4643{ 4644 4645 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount; 4646 return (0); 4647} 4648 4649int 4650if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize) 4651{ 4652 4653 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize; 4654 return (0); 4655} 4656 4657u_int 4658if_gethwtsomax(if_t ifp) 4659{ 4660 4661 return (((struct ifnet *)ifp)->if_hw_tsomax); 4662} 4663 4664u_int 4665if_gethwtsomaxsegcount(if_t ifp) 4666{ 4667 4668 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount); 4669} 4670 4671u_int 4672if_gethwtsomaxsegsize(if_t ifp) 4673{ 4674 4675 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize); 4676} 4677 4678void 4679if_setinitfn(if_t ifp, void (*init_fn)(void *)) 4680{ 4681 ((struct ifnet *)ifp)->if_init = init_fn; 4682} 4683 4684void 4685if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t)) 4686{ 4687 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; 4688} 4689 4690void 4691if_setstartfn(if_t ifp, void (*start_fn)(if_t)) 4692{ 4693 ((struct ifnet *)ifp)->if_start = (void *)start_fn; 4694} 4695 4696void 4697if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) 4698{ 4699 ((struct ifnet *)ifp)->if_transmit = start_fn; 4700} 4701 4702void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) 4703{ 4704 ((struct ifnet *)ifp)->if_qflush = flush_fn; 4705 4706} 4707 4708void 4709if_setgetcounterfn(if_t ifp, if_get_counter_t fn) 4710{ 4711 4712 ifp->if_get_counter = fn; 4713} 4714 4715/* Revisit these - These are inline functions originally. */ 4716int 4717drbr_inuse_drv(if_t ifh, struct buf_ring *br) 4718{ 4719 return drbr_inuse(ifh, br); 4720} 4721 4722struct mbuf* 4723drbr_dequeue_drv(if_t ifh, struct buf_ring *br) 4724{ 4725 return drbr_dequeue(ifh, br); 4726} 4727 4728int 4729drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br) 4730{ 4731 return drbr_needs_enqueue(ifh, br); 4732} 4733 4734int 4735drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m) 4736{ 4737 return drbr_enqueue(ifh, br, m); 4738 4739} 4740