2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
36 #include "opt_inet6.h"
39 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
46 #include <sys/epoch.h>
48 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/rwlock.h>
59 #include <sys/sockio.h>
60 #include <sys/syslog.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/taskqueue.h>
64 #include <sys/domain.h>
68 #include <machine/stdarg.h>
72 #include <net/ethernet.h>
74 #include <net/if_arp.h>
75 #include <net/if_clone.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/if_media.h>
80 #include <net/if_vlan_var.h>
81 #include <net/radix.h>
82 #include <net/route.h>
83 #include <net/route/route_ctl.h>
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>
93 #include <net/debugnet.h>
94 #include <netinet/if_ether.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet6/in6_ifattach.h>
100 #endif /* INET || INET6 */
102 #include <security/mac/mac_framework.h>
105 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
106 * and ifr_ifru when it is used in SIOCGIFCONF.
108 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
109 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
111 __read_mostly epoch_t net_epoch_preempt;
112 #ifdef COMPAT_FREEBSD32
113 #include <sys/mount.h>
114 #include <compat/freebsd32/freebsd32.h>
116 struct ifreq_buffer32 {
117 uint32_t length; /* (size_t) */
118 uint32_t buffer; /* (void *) */
122 * Interface request structure used for socket
123 * ioctl's. All interface ioctl's must have parameter
124 * definitions which begin with ifr_name. The
125 * remainder may be interface specific.
128 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
130 struct sockaddr ifru_addr;
131 struct sockaddr ifru_dstaddr;
132 struct sockaddr ifru_broadaddr;
133 struct ifreq_buffer32 ifru_buffer;
144 u_char ifru_vlan_pcp;
147 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
148 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
149 __offsetof(struct ifreq32, ifr_ifru));
151 struct ifgroupreq32 {
152 char ifgr_name[IFNAMSIZ];
155 char ifgru_group[IFNAMSIZ];
156 uint32_t ifgru_groups;
160 struct ifmediareq32 {
161 char ifm_name[IFNAMSIZ];
167 uint32_t ifm_ulist; /* (int *) */
169 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
170 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
172 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
173 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
174 #else /* !COMPAT_FREEBSD32 */
175 #define _CASE_IOC_IFGROUPREQ_32(cmd)
176 #endif /* !COMPAT_FREEBSD32 */
178 #define CASE_IOC_IFGROUPREQ(cmd) \
179 _CASE_IOC_IFGROUPREQ_32(cmd) \
184 #ifdef COMPAT_FREEBSD32
185 struct ifreq32 ifr32;
189 union ifgroupreq_union {
190 struct ifgroupreq ifgr;
191 #ifdef COMPAT_FREEBSD32
192 struct ifgroupreq32 ifgr32;
196 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
198 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
199 "Generic link-management");
201 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
202 &ifqmaxlen, 0, "max send queue size");
204 /* Log link state change events */
205 static int log_link_state_change = 1;
207 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
208 &log_link_state_change, 0,
209 "log interface link state change events");
211 /* Log promiscuous mode change events */
212 static int log_promisc_mode_change = 1;
214 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
215 &log_promisc_mode_change, 1,
216 "log promiscuous mode change events");
218 /* Interface description */
219 static unsigned int ifdescr_maxlen = 1024;
220 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
222 "administrative maximum length for interface description");
224 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
226 /* global sx for non-critical path ifdescr */
227 static struct sx ifdescr_sx;
228 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
230 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
231 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
232 /* These are external hooks for CARP. */
233 void (*carp_linkstate_p)(struct ifnet *ifp);
234 void (*carp_demote_adj_p)(int, char *);
235 int (*carp_master_p)(struct ifaddr *);
236 #if defined(INET) || defined(INET6)
237 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
238 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
239 const struct sockaddr *sa);
240 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
241 int (*carp_attach_p)(struct ifaddr *, int);
242 void (*carp_detach_p)(struct ifaddr *, bool);
245 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
248 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
249 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
250 const struct in6_addr *taddr);
253 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
256 * XXX: Style; these should be sorted alphabetically, and unprototyped
257 * static functions should be prototyped. Currently they are sorted by
260 static void if_attachdomain(void *);
261 static void if_attachdomain1(struct ifnet *);
262 static int ifconf(u_long, caddr_t);
263 static void *if_grow(void);
264 static void if_input_default(struct ifnet *, struct mbuf *);
265 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
266 static void if_route(struct ifnet *, int flag, int fam);
267 static int if_setflag(struct ifnet *, int, int, int *, int);
268 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
269 static void if_unroute(struct ifnet *, int flag, int fam);
270 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
271 static void do_link_state_change(void *, int);
272 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
273 static int if_getgroupmembers(struct ifgroupreq *);
274 static void if_delgroups(struct ifnet *);
275 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
276 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
277 static void if_siocaddmulti(void *, int);
278 static void if_link_ifnet(struct ifnet *);
279 static bool if_unlink_ifnet(struct ifnet *, bool);
281 static int if_vmove(struct ifnet *, struct vnet *);
286 * XXX: declare here to avoid to include many inet6 related files..
287 * should be more generalized?
289 extern void nd6_setmtu(struct ifnet *);
292 /* ipsec helper hooks */
293 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
294 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
296 VNET_DEFINE(int, if_index);
297 int ifqmaxlen = IFQ_MAXLEN;
298 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
299 VNET_DEFINE(struct ifgrouphead, ifg_head);
301 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
303 /* Table of ifnet by index. */
304 VNET_DEFINE(struct ifnet **, ifindex_table);
306 #define V_if_indexlim VNET(if_indexlim)
307 #define V_ifindex_table VNET(ifindex_table)
310 * The global network interface list (V_ifnet) and related state (such as
311 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
312 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
314 struct sx ifnet_sxlock;
315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
317 struct sx ifnet_detach_sxlock;
318 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
322 * The allocation of network interfaces is a rather non-atomic affair; we
323 * need to select an index before we are ready to expose the interface for
324 * use, so will use this pointer value to indicate reservation.
326 #define IFNET_HOLD (void *)(uintptr_t)(-1)
329 #define VNET_IS_SHUTTING_DOWN(_vnet) \
330 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
333 static if_com_alloc_t *if_com_alloc[256];
334 static if_com_free_t *if_com_free[256];
336 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
337 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
338 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
341 ifnet_byindex(u_short idx)
345 if (__predict_false(idx > V_if_index))
348 ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx);
349 return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp);
353 ifnet_byindex_ref(u_short idx)
359 ifp = ifnet_byindex(idx);
360 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
367 * Allocate an ifindex array entry; return 0 on success or an error on
371 ifindex_alloc(void **old)
375 IFNET_WLOCK_ASSERT();
377 * Try to find an empty slot below V_if_index. If we fail, take the
380 for (idx = 1; idx <= V_if_index; idx++) {
381 if (V_ifindex_table[idx] == NULL)
385 /* Catch if_index overflow. */
386 if (idx >= V_if_indexlim) {
390 if (idx > V_if_index)
396 ifindex_free_locked(u_short idx)
399 IFNET_WLOCK_ASSERT();
401 V_ifindex_table[idx] = NULL;
402 while (V_if_index > 0 &&
403 V_ifindex_table[V_if_index] == NULL)
408 ifindex_free(u_short idx)
412 ifindex_free_locked(idx);
417 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
420 V_ifindex_table[idx] = ifp;
424 ifaddr_byindex(u_short idx)
427 struct ifaddr *ifa = NULL;
431 ifp = ifnet_byindex(idx);
432 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
438 * Network interface utility routines.
440 * Routines with ifa_ifwith* names take sockaddr *'s as
445 vnet_if_init(const void *unused __unused)
449 CK_STAILQ_INIT(&V_ifnet);
450 CK_STAILQ_INIT(&V_ifg_head);
452 old = if_grow(); /* create initial table */
454 epoch_wait_preempt(net_epoch_preempt);
456 vnet_if_clone_init();
458 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
463 vnet_if_uninit(const void *unused __unused)
466 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
467 "not empty", __func__, __LINE__, &V_ifnet));
468 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
469 "not empty", __func__, __LINE__, &V_ifg_head));
471 free((caddr_t)V_ifindex_table, M_IFNET);
473 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
474 vnet_if_uninit, NULL);
478 if_link_ifnet(struct ifnet *ifp)
482 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
484 curvnet->vnet_ifcnt++;
490 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
496 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
498 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
500 ifp->if_flags |= IFF_DYING;
505 curvnet->vnet_ifcnt--;
514 vnet_if_return(const void *unused __unused)
516 struct ifnet *ifp, *nifp;
517 struct ifnet **pending;
523 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
524 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
525 * if_detach_internal(), which waits for NET_EPOCH callbacks to
526 * complete. We can't do that from within NET_EPOCH.
528 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
529 * read/write lock. We cannot hold the lock as we call if_vmove()
530 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
535 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
536 M_IFNET, M_WAITOK | M_ZERO);
538 /* Return all inherited interfaces to their parent vnets. */
539 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
540 if (ifp->if_home_vnet != ifp->if_vnet) {
541 found = if_unlink_ifnet(ifp, true);
549 for (int j = 0; j < i; j++) {
550 if_vmove(pending[j], pending[j]->if_home_vnet);
553 free(pending, M_IFNET);
555 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
556 vnet_if_return, NULL);
568 IFNET_WLOCK_ASSERT();
569 oldlim = V_if_indexlim;
571 n = (oldlim << 1) * sizeof(*e);
572 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
574 if (V_if_indexlim != oldlim) {
578 if (V_ifindex_table != NULL) {
579 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
580 old = V_ifindex_table;
588 * Allocate a struct ifnet and an index for an interface. A layer 2
589 * common structure will also be allocated if an allocation routine is
590 * registered for the passed type.
593 if_alloc_domain(u_char type, int numa_domain)
599 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
600 if (numa_domain == IF_NODOM)
601 ifp = malloc(sizeof(struct ifnet), M_IFNET,
604 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
605 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
608 idx = ifindex_alloc(&old);
609 if (__predict_false(idx == USHRT_MAX)) {
611 epoch_wait_preempt(net_epoch_preempt);
615 ifnet_setbyindex(idx, IFNET_HOLD);
619 ifp->if_alloctype = type;
620 ifp->if_numa_domain = numa_domain;
622 ifp->if_vnet = curvnet;
624 if (if_com_alloc[type] != NULL) {
625 ifp->if_l2com = if_com_alloc[type](type, ifp);
626 if (ifp->if_l2com == NULL) {
633 IF_ADDR_LOCK_INIT(ifp);
634 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
635 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
636 ifp->if_afdata_initialized = 0;
637 IF_AFDATA_LOCK_INIT(ifp);
638 CK_STAILQ_INIT(&ifp->if_addrhead);
639 CK_STAILQ_INIT(&ifp->if_multiaddrs);
640 CK_STAILQ_INIT(&ifp->if_groups);
644 ifq_init(&ifp->if_snd, ifp);
646 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
647 for (int i = 0; i < IFCOUNTERS; i++)
648 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
649 ifp->if_get_counter = if_get_counter_default;
650 ifp->if_pcp = IFNET_PCP_NONE;
651 ifnet_setbyindex(ifp->if_index, ifp);
656 if_alloc_dev(u_char type, device_t dev)
660 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
661 return (if_alloc_domain(type, IF_NODOM));
662 return (if_alloc_domain(type, numa_domain));
666 if_alloc(u_char type)
669 return (if_alloc_domain(type, IF_NODOM));
672 * Do the actual work of freeing a struct ifnet, and layer 2 common
673 * structure. This call is made when the last reference to an
674 * interface is released.
677 if_free_internal(struct ifnet *ifp)
680 KASSERT((ifp->if_flags & IFF_DYING),
681 ("if_free_internal: interface not dying"));
683 if (if_com_free[ifp->if_alloctype] != NULL)
684 if_com_free[ifp->if_alloctype](ifp->if_l2com,
688 mac_ifnet_destroy(ifp);
690 IF_AFDATA_DESTROY(ifp);
691 IF_ADDR_LOCK_DESTROY(ifp);
692 ifq_delete(&ifp->if_snd);
694 for (int i = 0; i < IFCOUNTERS; i++)
695 counter_u64_free(ifp->if_counters[i]);
697 free(ifp->if_description, M_IFDESCR);
698 free(ifp->if_hw_addr, M_IFADDR);
703 if_destroy(epoch_context_t ctx)
707 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
708 if_free_internal(ifp);
712 * Deregister an interface and free the associated storage.
715 if_free(struct ifnet *ifp)
718 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
720 CURVNET_SET_QUIET(ifp->if_vnet);
722 KASSERT(ifp == ifnet_byindex(ifp->if_index),
723 ("%s: freeing unallocated ifnet", ifp->if_xname));
725 ifindex_free_locked(ifp->if_index);
728 if (refcount_release(&ifp->if_refcount))
729 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
734 * Interfaces to keep an ifnet type-stable despite the possibility of the
735 * driver calling if_free(). If there are additional references, we defer
736 * freeing the underlying data structure.
739 if_ref(struct ifnet *ifp)
742 /* We don't assert the ifnet list lock here, but arguably should. */
743 refcount_acquire(&ifp->if_refcount);
747 if_rele(struct ifnet *ifp)
750 if (!refcount_release(&ifp->if_refcount))
752 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
756 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
759 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
761 if (ifq->ifq_maxlen == 0)
762 ifq->ifq_maxlen = ifqmaxlen;
765 ifq->altq_disc = NULL;
766 ifq->altq_flags &= ALTQF_CANTCHANGE;
767 ifq->altq_tbr = NULL;
772 ifq_delete(struct ifaltq *ifq)
774 mtx_destroy(&ifq->ifq_mtx);
778 * Perform generic interface initialization tasks and attach the interface
779 * to the list of "active" interfaces. If vmove flag is set on entry
780 * to if_attach_internal(), perform only a limited subset of initialization
781 * tasks, given that we are moving from one vnet to another an ifnet which
782 * has already been fully initialized.
784 * Note that if_detach_internal() removes group membership unconditionally
785 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
786 * Thus, when if_vmove() is applied to a cloned interface, group membership
787 * is lost while a cloned one always joins a group whose name is
788 * ifc->ifc_name. To recover this after if_detach_internal() and
789 * if_attach_internal(), the cloner should be specified to
790 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
791 * attempts to join a group whose name is ifc->ifc_name.
794 * - The decision to return void and thus require this function to
795 * succeed is questionable.
796 * - We should probably do more sanity checking. For instance we don't
797 * do anything to insure if_xname is unique or non-empty.
800 if_attach(struct ifnet *ifp)
803 if_attach_internal(ifp, 0, NULL);
807 * Compute the least common TSO limit.
810 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
813 * 1) If there is no limit currently, take the limit from
814 * the network adapter.
816 * 2) If the network adapter has a limit below the current
819 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
820 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
821 pmax->tsomaxbytes = ifp->if_hw_tsomax;
823 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
824 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
825 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
827 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
828 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
829 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
834 * Update TSO limit of a network adapter.
836 * Returns zero if no change. Else non-zero.
839 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
842 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
843 ifp->if_hw_tsomax = pmax->tsomaxbytes;
846 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
847 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
850 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
851 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
858 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
860 unsigned socksize, ifasize;
861 int namelen, masklen;
862 struct sockaddr_dl *sdl;
865 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
866 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
870 ifp->if_vnet = curvnet;
871 if (ifp->if_home_vnet == NULL)
872 ifp->if_home_vnet = curvnet;
875 if_addgroup(ifp, IFG_ALL);
877 /* Restore group membership for cloned interfaces. */
878 if (vmove && ifc != NULL)
879 if_clone_addgroup(ifp, ifc);
881 getmicrotime(&ifp->if_lastchange);
882 ifp->if_epoch = time_uptime;
884 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
885 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
886 ("transmit and qflush must both either be set or both be NULL"));
887 if (ifp->if_transmit == NULL) {
888 ifp->if_transmit = if_transmit;
889 ifp->if_qflush = if_qflush;
891 if (ifp->if_input == NULL)
892 ifp->if_input = if_input_default;
894 if (ifp->if_requestencap == NULL)
895 ifp->if_requestencap = if_requestencap_default;
899 mac_ifnet_create(ifp);
903 * Create a Link Level name for this device.
905 namelen = strlen(ifp->if_xname);
907 * Always save enough space for any possiable name so we
908 * can do a rename in place later.
910 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
911 socksize = masklen + ifp->if_addrlen;
912 if (socksize < sizeof(*sdl))
913 socksize = sizeof(*sdl);
914 socksize = roundup2(socksize, sizeof(long));
915 ifasize = sizeof(*ifa) + 2 * socksize;
916 ifa = ifa_alloc(ifasize, M_WAITOK);
917 sdl = (struct sockaddr_dl *)(ifa + 1);
918 sdl->sdl_len = socksize;
919 sdl->sdl_family = AF_LINK;
920 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
921 sdl->sdl_nlen = namelen;
922 sdl->sdl_index = ifp->if_index;
923 sdl->sdl_type = ifp->if_type;
926 ifa->ifa_addr = (struct sockaddr *)sdl;
927 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
928 ifa->ifa_netmask = (struct sockaddr *)sdl;
929 sdl->sdl_len = masklen;
931 sdl->sdl_data[--namelen] = 0xff;
932 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
933 /* Reliably crash if used uninitialized. */
934 ifp->if_broadcastaddr = NULL;
936 if (ifp->if_type == IFT_ETHER) {
937 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
941 #if defined(INET) || defined(INET6)
942 /* Use defaults for TSO, if nothing is set */
943 if (ifp->if_hw_tsomax == 0 &&
944 ifp->if_hw_tsomaxsegcount == 0 &&
945 ifp->if_hw_tsomaxsegsize == 0) {
947 * The TSO defaults needs to be such that an
948 * NFS mbuf list of 35 mbufs totalling just
949 * below 64K works and that a chain of mbufs
950 * can be defragged into at most 32 segments:
952 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
953 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
954 ifp->if_hw_tsomaxsegcount = 35;
955 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
957 /* XXX some drivers set IFCAP_TSO after ethernet attach */
958 if (ifp->if_capabilities & IFCAP_TSO) {
959 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
961 ifp->if_hw_tsomaxsegcount,
962 ifp->if_hw_tsomaxsegsize);
970 * Update the interface index in the link layer address
973 for (ifa = ifp->if_addr; ifa != NULL;
974 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
975 if (ifa->ifa_addr->sa_family == AF_LINK) {
976 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
977 sdl->sdl_index = ifp->if_index;
985 if (domain_init_status >= 2)
986 if_attachdomain1(ifp);
988 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
989 if (IS_DEFAULT_VNET(curvnet))
990 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
992 /* Announce the interface. */
993 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
997 if_epochalloc(void *dummy __unused)
1000 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
1002 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
1005 if_attachdomain(void *dummy)
1009 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
1010 if_attachdomain1(ifp);
1012 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
1013 if_attachdomain, NULL);
1016 if_attachdomain1(struct ifnet *ifp)
1021 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1022 * cannot lock ifp->if_afdata initialization, entirely.
1024 IF_AFDATA_LOCK(ifp);
1025 if (ifp->if_afdata_initialized >= domain_init_status) {
1026 IF_AFDATA_UNLOCK(ifp);
1027 log(LOG_WARNING, "%s called more than once on %s\n",
1028 __func__, ifp->if_xname);
1031 ifp->if_afdata_initialized = domain_init_status;
1032 IF_AFDATA_UNLOCK(ifp);
1034 /* address family dependent data region */
1035 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1036 for (dp = domains; dp; dp = dp->dom_next) {
1037 if (dp->dom_ifattach)
1038 ifp->if_afdata[dp->dom_family] =
1039 (*dp->dom_ifattach)(ifp);
1044 * Remove any unicast or broadcast network addresses from an interface.
1047 if_purgeaddrs(struct ifnet *ifp)
1052 struct epoch_tracker et;
1054 NET_EPOCH_ENTER(et);
1055 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1056 if (ifa->ifa_addr->sa_family != AF_LINK)
1064 /* XXX: Ugly!! ad hoc just for INET */
1065 if (ifa->ifa_addr->sa_family == AF_INET) {
1066 struct ifaliasreq ifr;
1068 bzero(&ifr, sizeof(ifr));
1069 ifr.ifra_addr = *ifa->ifa_addr;
1070 if (ifa->ifa_dstaddr)
1071 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1072 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1078 if (ifa->ifa_addr->sa_family == AF_INET6) {
1079 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1080 /* ifp_addrhead is already updated */
1085 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1086 IF_ADDR_WUNLOCK(ifp);
1092 * Remove any multicast network addresses from an interface when an ifnet
1096 if_purgemaddrs(struct ifnet *ifp)
1098 struct ifmultiaddr *ifma;
1101 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1102 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1103 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1104 if_delmulti_locked(ifp, ifma, 1);
1106 IF_ADDR_WUNLOCK(ifp);
1110 * Detach an interface, removing it from the list of "active" interfaces.
1111 * If vmove flag is set on entry to if_detach_internal(), perform only a
1112 * limited subset of cleanup tasks, given that we are moving an ifnet from
1113 * one vnet to another, where it must be fully operational.
1115 * XXXRW: There are some significant questions about event ordering, and
1116 * how to prevent things from starting to use the interface during detach.
1119 if_detach(struct ifnet *ifp)
1123 CURVNET_SET_QUIET(ifp->if_vnet);
1124 found = if_unlink_ifnet(ifp, false);
1126 sx_xlock(&ifnet_detach_sxlock);
1127 if_detach_internal(ifp, 0, NULL);
1128 sx_xunlock(&ifnet_detach_sxlock);
1134 * The vmove flag, if set, indicates that we are called from a callpath
1135 * that is moving an interface to a different vnet instance.
1137 * The shutdown flag, if set, indicates that we are called in the
1138 * process of shutting down a vnet instance. Currently only the
1139 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1140 * on a vnet instance shutdown without this flag being set, e.g., when
1141 * the cloned interfaces are destoyed as first thing of teardown.
1144 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1152 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1156 * At this point we know the interface still was on the ifnet list
1157 * and we removed it so we are in a stable state.
1159 epoch_wait_preempt(net_epoch_preempt);
1162 * Ensure all pending EPOCH(9) callbacks have been executed. This
1163 * fixes issues about late destruction of multicast options
1164 * which lead to leave group calls, which in turn access the
1165 * belonging ifnet structure:
1167 epoch_drain_callbacks(net_epoch_preempt);
1170 * In any case (destroy or vmove) detach us from the groups
1171 * and remove/wait for pending events on the taskq.
1172 * XXX-BZ in theory an interface could still enqueue a taskq change?
1176 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1177 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1180 * Check if this is a cloned interface or not. Must do even if
1181 * shutting down as a if_vmove_reclaim() would move the ifp and
1182 * the if_clone_addgroup() will have a corrupted string overwise
1183 * from a gibberish pointer.
1185 if (vmove && ifcp != NULL)
1186 *ifcp = if_clone_findifc(ifp);
1192 * On VNET shutdown abort here as the stack teardown will do all
1193 * the work top-down for us.
1196 /* Give interface users the chance to clean up. */
1197 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1200 * In case of a vmove we are done here without error.
1201 * If we would signal an error it would lead to the same
1202 * abort as if we did not find the ifnet anymore.
1203 * if_detach() calls us in void context and does not care
1204 * about an early abort notification, so life is splendid :)
1206 goto finish_vnet_shutdown;
1211 * At this point we are not tearing down a VNET and are either
1212 * going to destroy or vmove the interface and have to cleanup
1217 * Remove routes and flush queues.
1220 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1221 altq_disable(&ifp->if_snd);
1222 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1223 altq_detach(&ifp->if_snd);
1234 * Remove all IPv6 kernel structs related to ifp. This should be done
1235 * before removing routing entries below, since IPv6 interface direct
1236 * routes are expected to be removed by the IPv6-specific kernel API.
1237 * Otherwise, the kernel will detect some inconsistency and bark it.
1241 if_purgemaddrs(ifp);
1243 /* Announce that the interface is gone. */
1244 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1245 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1246 if (IS_DEFAULT_VNET(curvnet))
1247 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1251 * Prevent further calls into the device driver via ifnet.
1256 * Clean up all addresses.
1259 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1260 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1261 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1262 IF_ADDR_WUNLOCK(ifp);
1265 IF_ADDR_WUNLOCK(ifp);
1268 rt_flushifroutes(ifp);
1271 finish_vnet_shutdown:
1274 * We cannot hold the lock over dom_ifdetach calls as they might
1275 * sleep, for example trying to drain a callout, thus open up the
1276 * theoretical race with re-attaching.
1278 IF_AFDATA_LOCK(ifp);
1279 i = ifp->if_afdata_initialized;
1280 ifp->if_afdata_initialized = 0;
1281 IF_AFDATA_UNLOCK(ifp);
1282 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1283 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1284 (*dp->dom_ifdetach)(ifp,
1285 ifp->if_afdata[dp->dom_family]);
1286 ifp->if_afdata[dp->dom_family] = NULL;
1295 * if_vmove() performs a limited version of if_detach() in current
1296 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1297 * An attempt is made to shrink if_index in current vnet, find an
1298 * unused if_index in target vnet and calls if_grow() if necessary,
1299 * and finally find an unused if_xname for the target vnet.
1302 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1304 struct if_clone *ifc;
1306 u_int bif_dlt, bif_hdrlen;
1313 * if_detach_internal() will call the eventhandler to notify
1314 * interface departure. That will detach if_bpf. We need to
1315 * safe the dlt and hdrlen so we can re-attach it later.
1317 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1321 * Detach from current vnet, but preserve LLADDR info, do not
1322 * mark as dead etc. so that the ifnet can be reattached later.
1323 * If we cannot find it, we lost the race to someone else.
1325 rc = if_detach_internal(ifp, 1, &ifc);
1330 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1331 * the if_index for that vnet if possible.
1333 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1334 * or we'd lock on one vnet and unlock on another.
1337 ifindex_free_locked(ifp->if_index);
1341 * Perform interface-specific reassignment tasks, if provided by
1344 if (ifp->if_reassign != NULL)
1345 ifp->if_reassign(ifp, new_vnet, NULL);
1348 * Switch to the context of the target vnet.
1350 CURVNET_SET_QUIET(new_vnet);
1353 ifp->if_index = ifindex_alloc(&old);
1354 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1356 epoch_wait_preempt(net_epoch_preempt);
1360 ifnet_setbyindex(ifp->if_index, ifp);
1363 if_attach_internal(ifp, 1, ifc);
1366 if (ifp->if_bpf == NULL)
1367 bpfattach(ifp, bif_dlt, bif_hdrlen);
1375 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1378 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1386 /* Try to find the prison within our visibility. */
1387 sx_slock(&allprison_lock);
1388 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1389 sx_sunlock(&allprison_lock);
1392 prison_hold_locked(pr);
1393 mtx_unlock(&pr->pr_mtx);
1395 /* Do not try to move the iface from and to the same prison. */
1396 if (pr->pr_vnet == ifp->if_vnet) {
1401 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1402 /* XXX Lock interfaces to avoid races. */
1403 CURVNET_SET_QUIET(pr->pr_vnet);
1404 difp = ifunit(ifname);
1411 /* Make sure the VNET is stable. */
1412 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1420 found = if_unlink_ifnet(ifp, true);
1423 /* Move the interface into the child jail/vnet. */
1424 error = if_vmove(ifp, pr->pr_vnet);
1426 /* Report the new if_xname back to the userland on success. */
1428 sprintf(ifname, "%s", ifp->if_xname);
1435 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1438 struct vnet *vnet_dst;
1443 /* Try to find the prison within our visibility. */
1444 sx_slock(&allprison_lock);
1445 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1446 sx_sunlock(&allprison_lock);
1449 prison_hold_locked(pr);
1450 mtx_unlock(&pr->pr_mtx);
1452 /* Make sure the named iface exists in the source prison/vnet. */
1453 CURVNET_SET(pr->pr_vnet);
1454 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1461 /* Do not try to move the iface from and to the same prison. */
1462 vnet_dst = TD_TO_VNET(td);
1463 if (vnet_dst == ifp->if_vnet) {
1469 /* Make sure the VNET is stable. */
1470 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1477 /* Get interface back from child jail/vnet. */
1478 found = if_unlink_ifnet(ifp, true);
1480 error = if_vmove(ifp, vnet_dst);
1483 /* Report the new if_xname back to the userland on success. */
1485 sprintf(ifname, "%s", ifp->if_xname);
1493 * Add a group to an interface
1496 if_addgroup(struct ifnet *ifp, const char *groupname)
1498 struct ifg_list *ifgl;
1499 struct ifg_group *ifg = NULL;
1500 struct ifg_member *ifgm;
1503 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1504 groupname[strlen(groupname) - 1] <= '9')
1508 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1509 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1514 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1519 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1525 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1526 if (!strcmp(ifg->ifg_group, groupname))
1530 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1536 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1537 ifg->ifg_refcnt = 0;
1538 CK_STAILQ_INIT(&ifg->ifg_members);
1539 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1544 ifgl->ifgl_group = ifg;
1545 ifgm->ifgm_ifp = ifp;
1548 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1549 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1550 IF_ADDR_WUNLOCK(ifp);
1555 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1556 EVENTHANDLER_INVOKE(group_change_event, groupname);
1562 * Helper function to remove a group out of an interface. Expects the global
1563 * ifnet lock to be write-locked, and drops it before returning.
1566 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1567 const char *groupname)
1569 struct ifg_member *ifgm;
1572 IFNET_WLOCK_ASSERT();
1575 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1576 IF_ADDR_WUNLOCK(ifp);
1578 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1579 if (ifgm->ifgm_ifp == ifp) {
1580 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1581 ifg_member, ifgm_next);
1586 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1587 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1595 epoch_wait_preempt(net_epoch_preempt);
1597 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1598 free(ifgl->ifgl_group, M_TEMP);
1603 EVENTHANDLER_INVOKE(group_change_event, groupname);
1607 * Remove a group from an interface
1610 if_delgroup(struct ifnet *ifp, const char *groupname)
1612 struct ifg_list *ifgl;
1615 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1616 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1623 _if_delgroup_locked(ifp, ifgl, groupname);
1629 * Remove an interface from all groups
1632 if_delgroups(struct ifnet *ifp)
1634 struct ifg_list *ifgl;
1635 char groupname[IFNAMSIZ];
1638 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1639 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1640 _if_delgroup_locked(ifp, ifgl, groupname);
1647 ifgr_group_get(void *ifgrp)
1649 union ifgroupreq_union *ifgrup;
1652 #ifdef COMPAT_FREEBSD32
1653 if (SV_CURPROC_FLAG(SV_ILP32))
1654 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1656 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1659 static struct ifg_req *
1660 ifgr_groups_get(void *ifgrp)
1662 union ifgroupreq_union *ifgrup;
1665 #ifdef COMPAT_FREEBSD32
1666 if (SV_CURPROC_FLAG(SV_ILP32))
1667 return ((struct ifg_req *)(uintptr_t)
1668 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1670 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1674 * Stores all groups from an interface in memory pointed to by ifgr.
1677 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1680 struct ifg_list *ifgl;
1681 struct ifg_req ifgrq, *ifgp;
1685 if (ifgr->ifgr_len == 0) {
1686 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1687 ifgr->ifgr_len += sizeof(struct ifg_req);
1691 len = ifgr->ifgr_len;
1692 ifgp = ifgr_groups_get(ifgr);
1694 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1695 if (len < sizeof(ifgrq))
1697 bzero(&ifgrq, sizeof ifgrq);
1698 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1699 sizeof(ifgrq.ifgrq_group));
1700 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1702 len -= sizeof(ifgrq);
1710 * Stores all members of a group in memory pointed to by igfr
1713 if_getgroupmembers(struct ifgroupreq *ifgr)
1715 struct ifg_group *ifg;
1716 struct ifg_member *ifgm;
1717 struct ifg_req ifgrq, *ifgp;
1721 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1722 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1729 if (ifgr->ifgr_len == 0) {
1730 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1731 ifgr->ifgr_len += sizeof(ifgrq);
1736 len = ifgr->ifgr_len;
1737 ifgp = ifgr_groups_get(ifgr);
1738 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1739 if (len < sizeof(ifgrq)) {
1743 bzero(&ifgrq, sizeof ifgrq);
1744 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1745 sizeof(ifgrq.ifgrq_member));
1746 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1750 len -= sizeof(ifgrq);
1759 * Return counter values from counter(9)s stored in ifnet.
1762 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1765 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1767 return (counter_u64_fetch(ifp->if_counters[cnt]));
1771 * Increase an ifnet counter. Usually used for counters shared
1772 * between the stack and a driver, but function supports them all.
1775 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1778 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1780 counter_u64_add(ifp->if_counters[cnt], inc);
1784 * Copy data from ifnet to userland API structure if_data.
1787 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1790 ifd->ifi_type = ifp->if_type;
1791 ifd->ifi_physical = 0;
1792 ifd->ifi_addrlen = ifp->if_addrlen;
1793 ifd->ifi_hdrlen = ifp->if_hdrlen;
1794 ifd->ifi_link_state = ifp->if_link_state;
1796 ifd->ifi_datalen = sizeof(struct if_data);
1797 ifd->ifi_mtu = ifp->if_mtu;
1798 ifd->ifi_metric = ifp->if_metric;
1799 ifd->ifi_baudrate = ifp->if_baudrate;
1800 ifd->ifi_hwassist = ifp->if_hwassist;
1801 ifd->ifi_epoch = ifp->if_epoch;
1802 ifd->ifi_lastchange = ifp->if_lastchange;
1804 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1805 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1806 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1807 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1808 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1809 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1810 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1811 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1812 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1813 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1814 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1815 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1819 * Initialization, destruction and refcounting functions for ifaddrs.
1822 ifa_alloc(size_t size, int flags)
1826 KASSERT(size >= sizeof(struct ifaddr),
1827 ("%s: invalid size %zu", __func__, size));
1829 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1833 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1835 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1837 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1839 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1842 refcount_init(&ifa->ifa_refcnt, 1);
1847 /* free(NULL) is okay */
1848 counter_u64_free(ifa->ifa_opackets);
1849 counter_u64_free(ifa->ifa_ipackets);
1850 counter_u64_free(ifa->ifa_obytes);
1851 counter_u64_free(ifa->ifa_ibytes);
1852 free(ifa, M_IFADDR);
1858 ifa_ref(struct ifaddr *ifa)
1862 old = refcount_acquire(&ifa->ifa_refcnt);
1863 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1867 ifa_try_ref(struct ifaddr *ifa)
1871 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1875 ifa_destroy(epoch_context_t ctx)
1879 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1880 counter_u64_free(ifa->ifa_opackets);
1881 counter_u64_free(ifa->ifa_ipackets);
1882 counter_u64_free(ifa->ifa_obytes);
1883 counter_u64_free(ifa->ifa_ibytes);
1884 free(ifa, M_IFADDR);
1888 ifa_free(struct ifaddr *ifa)
1891 if (refcount_release(&ifa->ifa_refcnt))
1892 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1896 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1897 * structs used to represent other address families, it is necessary
1898 * to perform a different comparison.
1901 #define sa_dl_equal(a1, a2) \
1902 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1903 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1904 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1905 CLLADDR((const struct sockaddr_dl *)(a2)), \
1906 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1909 * Locate an interface based on a complete address.
1913 ifa_ifwithaddr(const struct sockaddr *addr)
1920 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1921 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1922 if (ifa->ifa_addr->sa_family != addr->sa_family)
1924 if (sa_equal(addr, ifa->ifa_addr)) {
1927 /* IP6 doesn't have broadcast */
1928 if ((ifp->if_flags & IFF_BROADCAST) &&
1929 ifa->ifa_broadaddr &&
1930 ifa->ifa_broadaddr->sa_len != 0 &&
1931 sa_equal(ifa->ifa_broadaddr, addr)) {
1942 ifa_ifwithaddr_check(const struct sockaddr *addr)
1944 struct epoch_tracker et;
1947 NET_EPOCH_ENTER(et);
1948 rc = (ifa_ifwithaddr(addr) != NULL);
1954 * Locate an interface based on the broadcast address.
1958 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1964 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1965 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1967 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1968 if (ifa->ifa_addr->sa_family != addr->sa_family)
1970 if ((ifp->if_flags & IFF_BROADCAST) &&
1971 ifa->ifa_broadaddr &&
1972 ifa->ifa_broadaddr->sa_len != 0 &&
1973 sa_equal(ifa->ifa_broadaddr, addr)) {
1984 * Locate the point to point interface with a given destination address.
1988 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1994 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1995 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1997 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1999 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2000 if (ifa->ifa_addr->sa_family != addr->sa_family)
2002 if (ifa->ifa_dstaddr != NULL &&
2003 sa_equal(addr, ifa->ifa_dstaddr)) {
2014 * Find an interface on a specific network. If many, choice
2015 * is most specific found.
2018 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2022 struct ifaddr *ifa_maybe = NULL;
2023 u_int af = addr->sa_family;
2024 const char *addr_data = addr->sa_data, *cplim;
2028 * AF_LINK addresses can be looked up directly by their index number,
2029 * so do that if we can.
2031 if (af == AF_LINK) {
2032 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2033 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2034 return (ifaddr_byindex(sdl->sdl_index));
2038 * Scan though each interface, looking for ones that have addresses
2039 * in this address family and the requested fib.
2041 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2042 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2044 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2045 const char *cp, *cp2, *cp3;
2047 if (ifa->ifa_addr->sa_family != af)
2049 if (af == AF_INET &&
2050 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2052 * This is a bit broken as it doesn't
2053 * take into account that the remote end may
2054 * be a single node in the network we are
2056 * The trouble is that we don't know the
2057 * netmask for the remote end.
2059 if (ifa->ifa_dstaddr != NULL &&
2060 sa_equal(addr, ifa->ifa_dstaddr)) {
2065 * Scan all the bits in the ifa's address.
2066 * If a bit dissagrees with what we are
2067 * looking for, mask it with the netmask
2068 * to see if it really matters.
2069 * (A byte at a time)
2071 if (ifa->ifa_netmask == 0)
2074 cp2 = ifa->ifa_addr->sa_data;
2075 cp3 = ifa->ifa_netmask->sa_data;
2076 cplim = ifa->ifa_netmask->sa_len
2077 + (char *)ifa->ifa_netmask;
2079 if ((*cp++ ^ *cp2++) & *cp3++)
2080 goto next; /* next address! */
2082 * If the netmask of what we just found
2083 * is more specific than what we had before
2084 * (if we had one), or if the virtual status
2085 * of new prefix is better than of the old one,
2086 * then remember the new one before continuing
2087 * to search for an even better one.
2089 if (ifa_maybe == NULL ||
2090 ifa_preferred(ifa_maybe, ifa) ||
2091 rn_refines((caddr_t)ifa->ifa_netmask,
2092 (caddr_t)ifa_maybe->ifa_netmask)) {
2105 * Find an interface address specific to an interface best matching
2109 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2112 const char *cp, *cp2, *cp3;
2114 struct ifaddr *ifa_maybe = NULL;
2115 u_int af = addr->sa_family;
2121 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2122 if (ifa->ifa_addr->sa_family != af)
2124 if (ifa_maybe == NULL)
2126 if (ifa->ifa_netmask == 0) {
2127 if (sa_equal(addr, ifa->ifa_addr) ||
2128 (ifa->ifa_dstaddr &&
2129 sa_equal(addr, ifa->ifa_dstaddr)))
2133 if (ifp->if_flags & IFF_POINTOPOINT) {
2134 if (sa_equal(addr, ifa->ifa_dstaddr))
2138 cp2 = ifa->ifa_addr->sa_data;
2139 cp3 = ifa->ifa_netmask->sa_data;
2140 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2141 for (; cp3 < cplim; cp3++)
2142 if ((*cp++ ^ *cp2++) & *cp3)
2154 * See whether new ifa is better than current one:
2155 * 1) A non-virtual one is preferred over virtual.
2156 * 2) A virtual in master state preferred over any other state.
2158 * Used in several address selecting functions.
2161 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2164 return (cur->ifa_carp && (!next->ifa_carp ||
2165 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2168 struct sockaddr_dl *
2169 link_alloc_sdl(size_t size, int flags)
2172 return (malloc(size, M_TEMP, flags));
2176 link_free_sdl(struct sockaddr *sa)
2182 * Fills in given sdl with interface basic info.
2183 * Returns pointer to filled sdl.
2185 struct sockaddr_dl *
2186 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2188 struct sockaddr_dl *sdl;
2190 sdl = (struct sockaddr_dl *)paddr;
2191 memset(sdl, 0, sizeof(struct sockaddr_dl));
2192 sdl->sdl_len = sizeof(struct sockaddr_dl);
2193 sdl->sdl_family = AF_LINK;
2194 sdl->sdl_index = ifp->if_index;
2195 sdl->sdl_type = iftype;
2201 * Mark an interface down and notify protocols of
2205 if_unroute(struct ifnet *ifp, int flag, int fam)
2209 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2211 ifp->if_flags &= ~flag;
2212 getmicrotime(&ifp->if_lastchange);
2213 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2214 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2215 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2216 ifp->if_qflush(ifp);
2219 (*carp_linkstate_p)(ifp);
2224 * Mark an interface up and notify protocols of
2228 if_route(struct ifnet *ifp, int flag, int fam)
2232 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2234 ifp->if_flags |= flag;
2235 getmicrotime(&ifp->if_lastchange);
2236 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2237 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2238 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2240 (*carp_linkstate_p)(ifp);
2247 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2248 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2249 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2250 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2251 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2252 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2253 int (*vlan_setcookie_p)(struct ifnet *, void *);
2254 void *(*vlan_cookie_p)(struct ifnet *);
2257 * Handle a change in the interface link state. To avoid LORs
2258 * between driver lock and upper layer locks, as well as possible
2259 * recursions, we post event to taskqueue, and all job
2260 * is done in static do_link_state_change().
2263 if_link_state_change(struct ifnet *ifp, int link_state)
2265 /* Return if state hasn't changed. */
2266 if (ifp->if_link_state == link_state)
2269 ifp->if_link_state = link_state;
2271 /* XXXGL: reference ifp? */
2272 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2276 do_link_state_change(void *arg, int pending)
2282 link_state = ifp->if_link_state;
2284 CURVNET_SET(ifp->if_vnet);
2286 if (ifp->if_vlantrunk != NULL)
2287 (*vlan_link_state_p)(ifp);
2289 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2290 ifp->if_l2com != NULL)
2291 (*ng_ether_link_state_p)(ifp, link_state);
2293 (*carp_linkstate_p)(ifp);
2295 ifp->if_bridge_linkstate(ifp);
2297 (*lagg_linkstate_p)(ifp, link_state);
2299 if (IS_DEFAULT_VNET(curvnet))
2300 devctl_notify("IFNET", ifp->if_xname,
2301 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2304 if_printf(ifp, "%d link states coalesced\n", pending);
2305 if (log_link_state_change)
2306 if_printf(ifp, "link state changed to %s\n",
2307 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2308 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2313 * Mark an interface down and notify protocols of
2317 if_down(struct ifnet *ifp)
2320 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2321 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2325 * Mark an interface up and notify protocols of
2329 if_up(struct ifnet *ifp)
2332 if_route(ifp, IFF_UP, AF_UNSPEC);
2333 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2337 * Flush an interface queue.
2340 if_qflush(struct ifnet *ifp)
2348 if (ALTQ_IS_ENABLED(ifq))
2352 while ((m = n) != NULL) {
2363 * Map interface name to interface structure pointer, with or without
2364 * returning a reference.
2367 ifunit_ref(const char *name)
2369 struct epoch_tracker et;
2372 NET_EPOCH_ENTER(et);
2373 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2374 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2375 !(ifp->if_flags & IFF_DYING))
2385 ifunit(const char *name)
2387 struct epoch_tracker et;
2390 NET_EPOCH_ENTER(et);
2391 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2392 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2400 ifr_buffer_get_buffer(void *data)
2402 union ifreq_union *ifrup;
2405 #ifdef COMPAT_FREEBSD32
2406 if (SV_CURPROC_FLAG(SV_ILP32))
2407 return ((void *)(uintptr_t)
2408 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2410 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2414 ifr_buffer_set_buffer_null(void *data)
2416 union ifreq_union *ifrup;
2419 #ifdef COMPAT_FREEBSD32
2420 if (SV_CURPROC_FLAG(SV_ILP32))
2421 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2424 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2428 ifr_buffer_get_length(void *data)
2430 union ifreq_union *ifrup;
2433 #ifdef COMPAT_FREEBSD32
2434 if (SV_CURPROC_FLAG(SV_ILP32))
2435 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2437 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2441 ifr_buffer_set_length(void *data, size_t len)
2443 union ifreq_union *ifrup;
2446 #ifdef COMPAT_FREEBSD32
2447 if (SV_CURPROC_FLAG(SV_ILP32))
2448 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2451 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2455 ifr_data_get_ptr(void *ifrp)
2457 union ifreq_union *ifrup;
2460 #ifdef COMPAT_FREEBSD32
2461 if (SV_CURPROC_FLAG(SV_ILP32))
2462 return ((void *)(uintptr_t)
2463 ifrup->ifr32.ifr_ifru.ifru_data);
2465 return (ifrup->ifr.ifr_ifru.ifru_data);
2469 * Hardware specific interface ioctls.
2472 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2475 int error = 0, do_ifup = 0;
2476 int new_flags, temp_flags;
2477 size_t namelen, onamelen;
2479 char *descrbuf, *odescrbuf;
2480 char new_name[IFNAMSIZ];
2482 struct sockaddr_dl *sdl;
2484 ifr = (struct ifreq *)data;
2487 ifr->ifr_index = ifp->if_index;
2491 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2492 ifr->ifr_flags = temp_flags & 0xffff;
2493 ifr->ifr_flagshigh = temp_flags >> 16;
2497 ifr->ifr_reqcap = ifp->if_capabilities;
2498 ifr->ifr_curcap = ifp->if_capenable;
2505 /* Ensure uninitialised padding is not leaked. */
2506 memset(&ifd, 0, sizeof(ifd));
2508 if_data_copy(ifp, &ifd);
2509 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2515 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2520 ifr->ifr_metric = ifp->if_metric;
2524 ifr->ifr_mtu = ifp->if_mtu;
2528 /* XXXGL: did this ever worked? */
2534 sx_slock(&ifdescr_sx);
2535 if (ifp->if_description == NULL)
2538 /* space for terminating nul */
2539 descrlen = strlen(ifp->if_description) + 1;
2540 if (ifr_buffer_get_length(ifr) < descrlen)
2541 ifr_buffer_set_buffer_null(ifr);
2543 error = copyout(ifp->if_description,
2544 ifr_buffer_get_buffer(ifr), descrlen);
2545 ifr_buffer_set_length(ifr, descrlen);
2547 sx_sunlock(&ifdescr_sx);
2551 error = priv_check(td, PRIV_NET_SETIFDESCR);
2556 * Copy only (length-1) bytes to make sure that
2557 * if_description is always nul terminated. The
2558 * length parameter is supposed to count the
2559 * terminating nul in.
2561 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2562 return (ENAMETOOLONG);
2563 else if (ifr_buffer_get_length(ifr) == 0)
2566 descrbuf = malloc(ifr_buffer_get_length(ifr),
2567 M_IFDESCR, M_WAITOK | M_ZERO);
2568 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2569 ifr_buffer_get_length(ifr) - 1);
2571 free(descrbuf, M_IFDESCR);
2576 sx_xlock(&ifdescr_sx);
2577 odescrbuf = ifp->if_description;
2578 ifp->if_description = descrbuf;
2579 sx_xunlock(&ifdescr_sx);
2581 getmicrotime(&ifp->if_lastchange);
2582 free(odescrbuf, M_IFDESCR);
2586 ifr->ifr_fib = ifp->if_fib;
2590 error = priv_check(td, PRIV_NET_SETIFFIB);
2593 if (ifr->ifr_fib >= rt_numfibs)
2596 ifp->if_fib = ifr->ifr_fib;
2600 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2604 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2605 * check, so we don't need special handling here yet.
2607 new_flags = (ifr->ifr_flags & 0xffff) |
2608 (ifr->ifr_flagshigh << 16);
2609 if (ifp->if_flags & IFF_UP &&
2610 (new_flags & IFF_UP) == 0) {
2612 } else if (new_flags & IFF_UP &&
2613 (ifp->if_flags & IFF_UP) == 0) {
2616 /* See if permanently promiscuous mode bit is about to flip */
2617 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2618 if (new_flags & IFF_PPROMISC)
2619 ifp->if_flags |= IFF_PROMISC;
2620 else if (ifp->if_pcount == 0)
2621 ifp->if_flags &= ~IFF_PROMISC;
2622 if (log_promisc_mode_change)
2623 if_printf(ifp, "permanently promiscuous mode %s\n",
2624 ((new_flags & IFF_PPROMISC) ?
2625 "enabled" : "disabled"));
2627 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2628 (new_flags &~ IFF_CANTCHANGE);
2629 if (ifp->if_ioctl) {
2630 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2634 getmicrotime(&ifp->if_lastchange);
2638 error = priv_check(td, PRIV_NET_SETIFCAP);
2641 if (ifp->if_ioctl == NULL)
2642 return (EOPNOTSUPP);
2643 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2645 error = (*ifp->if_ioctl)(ifp, cmd, data);
2647 getmicrotime(&ifp->if_lastchange);
2652 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2657 error = priv_check(td, PRIV_NET_SETIFNAME);
2660 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2664 if (new_name[0] == '\0')
2666 if (new_name[IFNAMSIZ-1] != '\0') {
2667 new_name[IFNAMSIZ-1] = '\0';
2668 if (strlen(new_name) == IFNAMSIZ-1)
2671 if (strcmp(new_name, ifp->if_xname) == 0)
2673 if (ifunit(new_name) != NULL)
2677 * XXX: Locking. Nothing else seems to lock if_flags,
2678 * and there are numerous other races with the
2679 * ifunit() checks not being atomic with namespace
2680 * changes (renames, vmoves, if_attach, etc).
2682 ifp->if_flags |= IFF_RENAMING;
2684 /* Announce the departure of the interface. */
2685 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2686 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2688 if_printf(ifp, "changing name to '%s'\n", new_name);
2691 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2693 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2694 namelen = strlen(new_name);
2695 onamelen = sdl->sdl_nlen;
2697 * Move the address if needed. This is safe because we
2698 * allocate space for a name of length IFNAMSIZ when we
2699 * create this in if_attach().
2701 if (namelen != onamelen) {
2702 bcopy(sdl->sdl_data + onamelen,
2703 sdl->sdl_data + namelen, sdl->sdl_alen);
2705 bcopy(new_name, sdl->sdl_data, namelen);
2706 sdl->sdl_nlen = namelen;
2707 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2708 bzero(sdl->sdl_data, onamelen);
2709 while (namelen != 0)
2710 sdl->sdl_data[--namelen] = 0xff;
2711 IF_ADDR_WUNLOCK(ifp);
2713 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2714 /* Announce the return of the interface. */
2715 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2717 ifp->if_flags &= ~IFF_RENAMING;
2722 error = priv_check(td, PRIV_NET_SETIFVNET);
2725 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2730 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2733 ifp->if_metric = ifr->ifr_metric;
2734 getmicrotime(&ifp->if_lastchange);
2738 error = priv_check(td, PRIV_NET_SETIFPHYS);
2741 if (ifp->if_ioctl == NULL)
2742 return (EOPNOTSUPP);
2743 error = (*ifp->if_ioctl)(ifp, cmd, data);
2745 getmicrotime(&ifp->if_lastchange);
2750 u_long oldmtu = ifp->if_mtu;
2752 error = priv_check(td, PRIV_NET_SETIFMTU);
2755 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2757 if (ifp->if_ioctl == NULL)
2758 return (EOPNOTSUPP);
2759 error = (*ifp->if_ioctl)(ifp, cmd, data);
2761 getmicrotime(&ifp->if_lastchange);
2764 DEBUGNET_NOTIFY_MTU(ifp);
2768 * If the link MTU changed, do network layer specific procedure.
2770 if (ifp->if_mtu != oldmtu) {
2781 if (cmd == SIOCADDMULTI)
2782 error = priv_check(td, PRIV_NET_ADDMULTI);
2784 error = priv_check(td, PRIV_NET_DELMULTI);
2788 /* Don't allow group membership on non-multicast interfaces. */
2789 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2790 return (EOPNOTSUPP);
2792 /* Don't let users screw up protocols' entries. */
2793 if (ifr->ifr_addr.sa_family != AF_LINK)
2796 if (cmd == SIOCADDMULTI) {
2797 struct epoch_tracker et;
2798 struct ifmultiaddr *ifma;
2801 * Userland is only permitted to join groups once
2802 * via the if_addmulti() KPI, because it cannot hold
2803 * struct ifmultiaddr * between calls. It may also
2804 * lose a race while we check if the membership
2807 NET_EPOCH_ENTER(et);
2808 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2813 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2815 error = if_delmulti(ifp, &ifr->ifr_addr);
2818 getmicrotime(&ifp->if_lastchange);
2821 case SIOCSIFPHYADDR:
2822 case SIOCDIFPHYADDR:
2824 case SIOCSIFPHYADDR_IN6:
2827 case SIOCSIFGENERIC:
2828 error = priv_check(td, PRIV_NET_HWIOCTL);
2831 if (ifp->if_ioctl == NULL)
2832 return (EOPNOTSUPP);
2833 error = (*ifp->if_ioctl)(ifp, cmd, data);
2835 getmicrotime(&ifp->if_lastchange);
2839 case SIOCGIFPSRCADDR:
2840 case SIOCGIFPDSTADDR:
2843 case SIOCGIFGENERIC:
2845 case SIOCGIFRSSHASH:
2846 case SIOCGIFDOWNREASON:
2847 if (ifp->if_ioctl == NULL)
2848 return (EOPNOTSUPP);
2849 error = (*ifp->if_ioctl)(ifp, cmd, data);
2853 error = priv_check(td, PRIV_NET_SETLLADDR);
2856 error = if_setlladdr(ifp,
2857 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2861 error = if_gethwaddr(ifp, ifr);
2864 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2865 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2868 if ((error = if_addgroup(ifp,
2869 ifgr_group_get((struct ifgroupreq *)data))))
2873 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2875 struct epoch_tracker et;
2877 NET_EPOCH_ENTER(et);
2878 error = if_getgroup((struct ifgroupreq *)data, ifp);
2883 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2884 error = priv_check(td, PRIV_NET_DELIFGROUP);
2887 if ((error = if_delgroup(ifp,
2888 ifgr_group_get((struct ifgroupreq *)data))))
2899 #ifdef COMPAT_FREEBSD32
2907 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2910 #ifdef COMPAT_FREEBSD32
2912 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2914 struct ifmediareq32 *ifmr32;
2916 ifmr32 = (struct ifmediareq32 *)data;
2917 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2918 sizeof(ifmr->ifm_name));
2919 ifmr->ifm_current = ifmr32->ifm_current;
2920 ifmr->ifm_mask = ifmr32->ifm_mask;
2921 ifmr->ifm_status = ifmr32->ifm_status;
2922 ifmr->ifm_active = ifmr32->ifm_active;
2923 ifmr->ifm_count = ifmr32->ifm_count;
2924 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2928 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2930 struct ifmediareq32 *ifmr32;
2932 ifmr32 = (struct ifmediareq32 *)data;
2933 ifmr32->ifm_current = ifmr->ifm_current;
2934 ifmr32->ifm_mask = ifmr->ifm_mask;
2935 ifmr32->ifm_status = ifmr->ifm_status;
2936 ifmr32->ifm_active = ifmr->ifm_active;
2937 ifmr32->ifm_count = ifmr->ifm_count;
2945 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2947 #ifdef COMPAT_FREEBSD32
2948 caddr_t saved_data = NULL;
2949 struct ifmediareq ifmr;
2950 struct ifmediareq *ifmrp = NULL;
2960 CURVNET_SET(so->so_vnet);
2962 /* Make sure the VNET is stable. */
2963 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2972 error = ifconf(cmd, data);
2975 #ifdef COMPAT_FREEBSD32
2978 struct ifconf32 *ifc32;
2981 ifc32 = (struct ifconf32 *)data;
2982 ifc.ifc_len = ifc32->ifc_len;
2983 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2985 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2987 ifc32->ifc_len = ifc.ifc_len;
2993 #ifdef COMPAT_FREEBSD32
2995 case SIOCGIFMEDIA32:
2996 case SIOCGIFXMEDIA32:
2998 ifmr_init(ifmrp, data);
2999 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3001 data = (caddr_t)ifmrp;
3005 ifr = (struct ifreq *)data;
3009 error = priv_check(td, PRIV_NET_SETIFVNET);
3011 error = if_vmove_reclaim(td, ifr->ifr_name,
3017 error = priv_check(td, PRIV_NET_IFCREATE);
3019 error = if_clone_create(ifr->ifr_name,
3020 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3021 ifr_data_get_ptr(ifr) : NULL);
3024 error = priv_check(td, PRIV_NET_IFDESTROY);
3027 sx_xlock(&ifnet_detach_sxlock);
3028 error = if_clone_destroy(ifr->ifr_name);
3029 sx_xunlock(&ifnet_detach_sxlock);
3033 case SIOCIFGCLONERS:
3034 error = if_clone_list((struct if_clonereq *)data);
3037 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3038 error = if_getgroupmembers((struct ifgroupreq *)data);
3041 #if defined(INET) || defined(INET6)
3044 if (carp_ioctl_p == NULL)
3045 error = EPROTONOSUPPORT;
3047 error = (*carp_ioctl_p)(ifr, cmd, td);
3052 ifp = ifunit_ref(ifr->ifr_name);
3058 error = ifhwioctl(cmd, ifp, data, td);
3059 if (error != ENOIOCTL)
3062 oif_flags = ifp->if_flags;
3063 if (so->so_proto == NULL) {
3069 * Pass the request on to the socket control method, and if the
3070 * latter returns EOPNOTSUPP, directly to the interface.
3072 * Make an exception for the legacy SIOCSIF* requests. Drivers
3073 * trust SIOCSIFADDR et al to come from an already privileged
3074 * layer, and do not perform any credentials checks or input
3077 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3079 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3080 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3081 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3082 error = (*ifp->if_ioctl)(ifp, cmd, data);
3084 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3086 if (ifp->if_flags & IFF_UP)
3094 #ifdef COMPAT_FREEBSD32
3095 if (ifmrp != NULL) {
3096 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3097 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3100 ifmr_update(ifmrp, data);
3108 * The code common to handling reference counted flags,
3109 * e.g., in ifpromisc() and if_allmulti().
3110 * The "pflag" argument can specify a permanent mode flag to check,
3111 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3113 * Only to be used on stack-owned flags, not driver-owned flags.
3116 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3120 int oldflags, oldcount;
3122 /* Sanity checks to catch programming errors */
3123 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3124 ("%s: setting driver-owned flag %d", __func__, flag));
3127 KASSERT(*refcount >= 0,
3128 ("%s: increment negative refcount %d for flag %d",
3129 __func__, *refcount, flag));
3131 KASSERT(*refcount > 0,
3132 ("%s: decrement non-positive refcount %d for flag %d",
3133 __func__, *refcount, flag));
3135 /* In case this mode is permanent, just touch refcount */
3136 if (ifp->if_flags & pflag) {
3137 *refcount += onswitch ? 1 : -1;
3141 /* Save ifnet parameters for if_ioctl() may fail */
3142 oldcount = *refcount;
3143 oldflags = ifp->if_flags;
3146 * See if we aren't the only and touching refcount is enough.
3147 * Actually toggle interface flag if we are the first or last.
3152 ifp->if_flags |= flag;
3156 ifp->if_flags &= ~flag;
3159 /* Call down the driver since we've changed interface flags */
3160 if (ifp->if_ioctl == NULL) {
3164 ifr.ifr_flags = ifp->if_flags & 0xffff;
3165 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3166 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3169 /* Notify userland that interface flags have changed */
3174 /* Recover after driver error */
3175 *refcount = oldcount;
3176 ifp->if_flags = oldflags;
3181 * Set/clear promiscuous mode on interface ifp based on the truth value
3182 * of pswitch. The calls are reference counted so that only the first
3183 * "on" request actually has an effect, as does the final "off" request.
3184 * Results are undefined if the "off" and "on" requests are not matched.
3187 ifpromisc(struct ifnet *ifp, int pswitch)
3190 int oldflags = ifp->if_flags;
3192 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3193 &ifp->if_pcount, pswitch);
3194 /* If promiscuous mode status has changed, log a message */
3195 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3196 log_promisc_mode_change)
3197 if_printf(ifp, "promiscuous mode %s\n",
3198 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3203 * Return interface configuration
3204 * of system. List may be used
3205 * in later ioctl's (above) to get
3206 * other information.
3210 ifconf(u_long cmd, caddr_t data)
3212 struct ifconf *ifc = (struct ifconf *)data;
3217 int error, full = 0, valid_len, max_len;
3219 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3220 max_len = maxphys - 1;
3222 /* Prevent hostile input from being able to crash the system */
3223 if (ifc->ifc_len <= 0)
3227 if (ifc->ifc_len <= max_len) {
3228 max_len = ifc->ifc_len;
3231 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3236 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3237 struct epoch_tracker et;
3241 * Zero the ifr to make sure we don't disclose the contents
3244 memset(&ifr, 0, sizeof(ifr));
3246 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3247 >= sizeof(ifr.ifr_name)) {
3250 return (ENAMETOOLONG);
3254 NET_EPOCH_ENTER(et);
3255 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3256 struct sockaddr *sa = ifa->ifa_addr;
3258 if (prison_if(curthread->td_ucred, sa) != 0)
3261 if (sa->sa_len <= sizeof(*sa)) {
3262 if (sa->sa_len < sizeof(*sa)) {
3263 memset(&ifr.ifr_ifru.ifru_addr, 0,
3264 sizeof(ifr.ifr_ifru.ifru_addr));
3265 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3268 ifr.ifr_ifru.ifru_addr = *sa;
3269 sbuf_bcat(sb, &ifr, sizeof(ifr));
3270 max_len += sizeof(ifr);
3273 offsetof(struct ifreq, ifr_addr));
3274 max_len += offsetof(struct ifreq, ifr_addr);
3275 sbuf_bcat(sb, sa, sa->sa_len);
3276 max_len += sa->sa_len;
3279 if (sbuf_error(sb) == 0)
3280 valid_len = sbuf_len(sb);
3284 sbuf_bcat(sb, &ifr, sizeof(ifr));
3285 max_len += sizeof(ifr);
3287 if (sbuf_error(sb) == 0)
3288 valid_len = sbuf_len(sb);
3294 * If we didn't allocate enough space (uncommon), try again. If
3295 * we have already allocated as much space as we are allowed,
3296 * return what we've got.
3298 if (valid_len != max_len && !full) {
3303 ifc->ifc_len = valid_len;
3305 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3311 * Just like ifpromisc(), but for all-multicast-reception mode.
3314 if_allmulti(struct ifnet *ifp, int onswitch)
3317 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3320 struct ifmultiaddr *
3321 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3323 struct ifmultiaddr *ifma;
3325 IF_ADDR_LOCK_ASSERT(ifp);
3327 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3328 if (sa->sa_family == AF_LINK) {
3329 if (sa_dl_equal(ifma->ifma_addr, sa))
3332 if (sa_equal(ifma->ifma_addr, sa))
3341 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3342 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3343 * the ifnet multicast address list here, so the caller must do that and
3344 * other setup work (such as notifying the device driver). The reference
3345 * count is initialized to 1.
3347 static struct ifmultiaddr *
3348 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3351 struct ifmultiaddr *ifma;
3352 struct sockaddr *dupsa;
3354 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3359 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3360 if (dupsa == NULL) {
3361 free(ifma, M_IFMADDR);
3364 bcopy(sa, dupsa, sa->sa_len);
3365 ifma->ifma_addr = dupsa;
3367 ifma->ifma_ifp = ifp;
3368 ifma->ifma_refcount = 1;
3369 ifma->ifma_protospec = NULL;
3372 ifma->ifma_lladdr = NULL;
3376 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3377 if (dupsa == NULL) {
3378 free(ifma->ifma_addr, M_IFMADDR);
3379 free(ifma, M_IFMADDR);
3382 bcopy(llsa, dupsa, llsa->sa_len);
3383 ifma->ifma_lladdr = dupsa;
3389 * if_freemulti: free ifmultiaddr structure and possibly attached related
3390 * addresses. The caller is responsible for implementing reference
3391 * counting, notifying the driver, handling routing messages, and releasing
3392 * any dependent link layer state.
3394 #ifdef MCAST_VERBOSE
3395 extern void kdb_backtrace(void);
3398 if_freemulti_internal(struct ifmultiaddr *ifma)
3401 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3402 ifma->ifma_refcount));
3404 if (ifma->ifma_lladdr != NULL)
3405 free(ifma->ifma_lladdr, M_IFMADDR);
3406 #ifdef MCAST_VERBOSE
3408 printf("%s freeing ifma: %p\n", __func__, ifma);
3410 free(ifma->ifma_addr, M_IFMADDR);
3411 free(ifma, M_IFMADDR);
3415 if_destroymulti(epoch_context_t ctx)
3417 struct ifmultiaddr *ifma;
3419 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3420 if_freemulti_internal(ifma);
3424 if_freemulti(struct ifmultiaddr *ifma)
3426 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3427 ifma->ifma_refcount));
3429 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3433 * Register an additional multicast address with a network interface.
3435 * - If the address is already present, bump the reference count on the
3436 * address and return.
3437 * - If the address is not link-layer, look up a link layer address.
3438 * - Allocate address structures for one or both addresses, and attach to the
3439 * multicast address list on the interface. If automatically adding a link
3440 * layer address, the protocol address will own a reference to the link
3441 * layer address, to be freed when it is freed.
3442 * - Notify the network device driver of an addition to the multicast address
3445 * 'sa' points to caller-owned memory with the desired multicast address.
3447 * 'retifma' will be used to return a pointer to the resulting multicast
3448 * address reference, if desired.
3451 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3452 struct ifmultiaddr **retifma)
3454 struct ifmultiaddr *ifma, *ll_ifma;
3455 struct sockaddr *llsa;
3456 struct sockaddr_dl sdl;
3460 IN_MULTI_LIST_UNLOCK_ASSERT();
3463 IN6_MULTI_LIST_UNLOCK_ASSERT();
3466 * If the address is already present, return a new reference to it;
3467 * otherwise, allocate storage and set up a new address.
3470 ifma = if_findmulti(ifp, sa);
3472 ifma->ifma_refcount++;
3473 if (retifma != NULL)
3475 IF_ADDR_WUNLOCK(ifp);
3480 * The address isn't already present; resolve the protocol address
3481 * into a link layer address, and then look that up, bump its
3482 * refcount or allocate an ifma for that also.
3483 * Most link layer resolving functions returns address data which
3484 * fits inside default sockaddr_dl structure. However callback
3485 * can allocate another sockaddr structure, in that case we need to
3490 if (ifp->if_resolvemulti != NULL) {
3491 /* Provide called function with buffer size information */
3492 sdl.sdl_len = sizeof(sdl);
3493 llsa = (struct sockaddr *)&sdl;
3494 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3500 * Allocate the new address. Don't hook it up yet, as we may also
3501 * need to allocate a link layer multicast address.
3503 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3510 * If a link layer address is found, we'll need to see if it's
3511 * already present in the address list, or allocate is as well.
3512 * When this block finishes, the link layer address will be on the
3516 ll_ifma = if_findmulti(ifp, llsa);
3517 if (ll_ifma == NULL) {
3518 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3519 if (ll_ifma == NULL) {
3520 --ifma->ifma_refcount;
3525 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3526 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3529 ll_ifma->ifma_refcount++;
3530 ifma->ifma_llifma = ll_ifma;
3534 * We now have a new multicast address, ifma, and possibly a new or
3535 * referenced link layer address. Add the primary address to the
3536 * ifnet address list.
3538 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3539 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3541 if (retifma != NULL)
3545 * Must generate the message while holding the lock so that 'ifma'
3546 * pointer is still valid.
3548 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3549 IF_ADDR_WUNLOCK(ifp);
3552 * We are certain we have added something, so call down to the
3553 * interface to let them know about it.
3555 if (ifp->if_ioctl != NULL) {
3556 if (THREAD_CAN_SLEEP())
3557 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3559 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3562 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3563 link_free_sdl(llsa);
3568 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3569 link_free_sdl(llsa);
3572 IF_ADDR_WUNLOCK(ifp);
3577 if_siocaddmulti(void *arg, int pending)
3584 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3586 CURVNET_SET(ifp->if_vnet);
3587 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3592 * Delete a multicast group membership by network-layer group address.
3594 * Returns ENOENT if the entry could not be found. If ifp no longer
3595 * exists, results are undefined. This entry point should only be used
3596 * from subsystems which do appropriate locking to hold ifp for the
3597 * duration of the call.
3598 * Network-layer protocol domains must use if_delmulti_ifma().
3601 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3603 struct ifmultiaddr *ifma;
3606 KASSERT(ifp, ("%s: NULL ifp", __func__));
3610 ifma = if_findmulti(ifp, sa);
3612 lastref = if_delmulti_locked(ifp, ifma, 0);
3613 IF_ADDR_WUNLOCK(ifp);
3618 if (lastref && ifp->if_ioctl != NULL) {
3619 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3626 * Delete all multicast group membership for an interface.
3627 * Should be used to quickly flush all multicast filters.
3630 if_delallmulti(struct ifnet *ifp)
3632 struct ifmultiaddr *ifma;
3633 struct ifmultiaddr *next;
3636 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3637 if_delmulti_locked(ifp, ifma, 0);
3638 IF_ADDR_WUNLOCK(ifp);
3642 if_delmulti_ifma(struct ifmultiaddr *ifma)
3644 if_delmulti_ifma_flags(ifma, 0);
3648 * Delete a multicast group membership by group membership pointer.
3649 * Network-layer protocol domains must use this routine.
3651 * It is safe to call this routine if the ifp disappeared.
3654 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3658 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3660 IN_MULTI_LIST_UNLOCK_ASSERT();
3662 ifp = ifma->ifma_ifp;
3665 printf("%s: ifma_ifp seems to be detached\n", __func__);
3667 struct epoch_tracker et;
3670 NET_EPOCH_ENTER(et);
3671 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3680 * If and only if the ifnet instance exists: Acquire the address lock.
3685 lastref = if_delmulti_locked(ifp, ifma, flags);
3689 * If and only if the ifnet instance exists:
3690 * Release the address lock.
3691 * If the group was left: update the hardware hash filter.
3693 IF_ADDR_WUNLOCK(ifp);
3694 if (lastref && ifp->if_ioctl != NULL) {
3695 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3701 * Perform deletion of network-layer and/or link-layer multicast address.
3703 * Return 0 if the reference count was decremented.
3704 * Return 1 if the final reference was released, indicating that the
3705 * hardware hash filter should be reprogrammed.
3708 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3710 struct ifmultiaddr *ll_ifma;
3712 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3713 KASSERT(ifma->ifma_ifp == ifp,
3714 ("%s: inconsistent ifp %p", __func__, ifp));
3715 IF_ADDR_WLOCK_ASSERT(ifp);
3718 ifp = ifma->ifma_ifp;
3719 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3722 * If the ifnet is detaching, null out references to ifnet,
3723 * so that upper protocol layers will notice, and not attempt
3724 * to obtain locks for an ifnet which no longer exists. The
3725 * routing socket announcement must happen before the ifnet
3726 * instance is detached from the system.
3730 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3733 * ifp may already be nulled out if we are being reentered
3734 * to delete the ll_ifma.
3737 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3738 ifma->ifma_ifp = NULL;
3742 if (--ifma->ifma_refcount > 0)
3745 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3746 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3747 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3750 * If this ifma is a network-layer ifma, a link-layer ifma may
3751 * have been associated with it. Release it first if so.
3753 ll_ifma = ifma->ifma_llifma;
3754 if (ll_ifma != NULL) {
3755 KASSERT(ifma->ifma_lladdr != NULL,
3756 ("%s: llifma w/o lladdr", __func__));
3758 ll_ifma->ifma_ifp = NULL; /* XXX */
3759 if (--ll_ifma->ifma_refcount == 0) {
3761 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3762 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3764 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3767 if_freemulti(ll_ifma);
3772 struct ifmultiaddr *ifmatmp;
3774 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3775 MPASS(ifma != ifmatmp);
3780 * The last reference to this instance of struct ifmultiaddr
3781 * was released; the hardware should be notified of this change.
3787 * Set the link layer address on an interface.
3789 * At this time we only support certain types of interfaces,
3790 * and we don't allow the length of the address to change.
3792 * Set noinline to be dtrace-friendly
3795 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3797 struct sockaddr_dl *sdl;
3805 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3809 if (len != sdl->sdl_alen) /* don't allow length to change */
3812 switch (ifp->if_type) {
3817 case IFT_IEEE8023ADLAG:
3818 bcopy(lladdr, LLADDR(sdl), len);
3825 * If the interface is already up, we need
3826 * to re-init it in order to reprogram its
3829 if ((ifp->if_flags & IFF_UP) != 0) {
3830 if (ifp->if_ioctl) {
3831 ifp->if_flags &= ~IFF_UP;
3832 ifr.ifr_flags = ifp->if_flags & 0xffff;
3833 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3834 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3835 ifp->if_flags |= IFF_UP;
3836 ifr.ifr_flags = ifp->if_flags & 0xffff;
3837 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3838 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3841 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3847 * Compat function for handling basic encapsulation requests.
3848 * Not converted stacks (FDDI, IB, ..) supports traditional
3849 * output model: ARP (and other similar L2 protocols) are handled
3850 * inside output routine, arpresolve/nd6_resolve() returns MAC
3851 * address instead of full prepend.
3853 * This function creates calculated header==MAC for IPv4/IPv6 and
3854 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3858 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3861 if (req->rtype != IFENCAP_LL)
3862 return (EOPNOTSUPP);
3864 if (req->bufsize < req->lladdr_len)
3867 switch (req->family) {
3872 return (EAFNOSUPPORT);
3875 /* Copy lladdr to storage as is */
3876 memmove(req->buf, req->lladdr, req->lladdr_len);
3877 req->bufsize = req->lladdr_len;
3878 req->lladdr_off = 0;
3884 * Tunnel interfaces can nest, also they may cause infinite recursion
3885 * calls when misconfigured. We'll prevent this by detecting loops.
3886 * High nesting level may cause stack exhaustion. We'll prevent this
3887 * by introducing upper limit.
3889 * Return 0, if tunnel nesting count is equal or less than limit.
3892 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3900 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3901 if (*(struct ifnet **)(mtag + 1) == ifp) {
3902 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3907 if (count > limit) {
3909 "%s: if_output recursively called too many times(%d)\n",
3910 if_name(ifp), count);
3913 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3916 *(struct ifnet **)(mtag + 1) = ifp;
3917 m_tag_prepend(m, mtag);
3922 * Get the link layer address that was read from the hardware at attach.
3924 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3925 * their component interfaces as IFT_IEEE8023ADLAG.
3928 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3931 if (ifp->if_hw_addr == NULL)
3934 switch (ifp->if_type) {
3936 case IFT_IEEE8023ADLAG:
3937 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3945 * The name argument must be a pointer to storage which will last as
3946 * long as the interface does. For physical devices, the result of
3947 * device_get_name(dev) is a good choice and for pseudo-devices a
3948 * static string works well.
3951 if_initname(struct ifnet *ifp, const char *name, int unit)
3953 ifp->if_dname = name;
3954 ifp->if_dunit = unit;
3955 if (unit != IF_DUNIT_NONE)
3956 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3958 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3962 if_printf(struct ifnet *ifp, const char *fmt, ...)
3967 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3969 vlog(LOG_INFO, if_fmt, ap);
3975 if_start(struct ifnet *ifp)
3978 (*(ifp)->if_start)(ifp);
3982 * Backwards compatibility interface for drivers
3983 * that have not implemented it
3986 if_transmit(struct ifnet *ifp, struct mbuf *m)
3990 IFQ_HANDOFF(ifp, m, error);
3995 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4002 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4007 if (_IF_QFULL(ifq)) {
4009 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4014 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4015 if (m->m_flags & (M_BCAST|M_MCAST))
4016 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4017 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4019 _IF_ENQUEUE(ifq, m);
4021 if (ifp != NULL && !active)
4022 (*(ifp)->if_start)(ifp);
4027 if_register_com_alloc(u_char type,
4028 if_com_alloc_t *a, if_com_free_t *f)
4031 KASSERT(if_com_alloc[type] == NULL,
4032 ("if_register_com_alloc: %d already registered", type));
4033 KASSERT(if_com_free[type] == NULL,
4034 ("if_register_com_alloc: %d free already registered", type));
4036 if_com_alloc[type] = a;
4037 if_com_free[type] = f;
4041 if_deregister_com_alloc(u_char type)
4044 KASSERT(if_com_alloc[type] != NULL,
4045 ("if_deregister_com_alloc: %d not registered", type));
4046 KASSERT(if_com_free[type] != NULL,
4047 ("if_deregister_com_alloc: %d free not registered", type));
4050 * Ensure all pending EPOCH(9) callbacks have been executed. This
4051 * fixes issues about late invocation of if_destroy(), which leads
4052 * to memory leak from if_com_alloc[type] allocated if_l2com.
4054 epoch_drain_callbacks(net_epoch_preempt);
4056 if_com_alloc[type] = NULL;
4057 if_com_free[type] = NULL;
4060 /* API for driver access to network stack owned ifnet.*/
4062 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4066 oldbrate = ifp->if_baudrate;
4067 ifp->if_baudrate = baudrate;
4072 if_getbaudrate(if_t ifp)
4075 return (((struct ifnet *)ifp)->if_baudrate);
4079 if_setcapabilities(if_t ifp, int capabilities)
4081 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4086 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4088 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4089 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4095 if_getcapabilities(if_t ifp)
4097 return ((struct ifnet *)ifp)->if_capabilities;
4101 if_setcapenable(if_t ifp, int capabilities)
4103 ((struct ifnet *)ifp)->if_capenable = capabilities;
4108 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4111 ((struct ifnet *)ifp)->if_capenable |= setcap;
4113 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4119 if_getdname(if_t ifp)
4121 return ((struct ifnet *)ifp)->if_dname;
4125 if_togglecapenable(if_t ifp, int togglecap)
4127 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4132 if_getcapenable(if_t ifp)
4134 return ((struct ifnet *)ifp)->if_capenable;
4138 * This is largely undesirable because it ties ifnet to a device, but does
4139 * provide flexiblity for an embedded product vendor. Should be used with
4140 * the understanding that it violates the interface boundaries, and should be
4141 * a last resort only.
4144 if_setdev(if_t ifp, void *dev)
4150 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4152 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4153 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4159 if_getdrvflags(if_t ifp)
4161 return ((struct ifnet *)ifp)->if_drv_flags;
4165 if_setdrvflags(if_t ifp, int flags)
4167 ((struct ifnet *)ifp)->if_drv_flags = flags;
4172 if_setflags(if_t ifp, int flags)
4175 ifp->if_flags = flags;
4180 if_setflagbits(if_t ifp, int set, int clear)
4182 ((struct ifnet *)ifp)->if_flags |= set;
4183 ((struct ifnet *)ifp)->if_flags &= ~clear;
4189 if_getflags(if_t ifp)
4191 return ((struct ifnet *)ifp)->if_flags;
4195 if_clearhwassist(if_t ifp)
4197 ((struct ifnet *)ifp)->if_hwassist = 0;
4202 if_sethwassistbits(if_t ifp, int toset, int toclear)
4204 ((struct ifnet *)ifp)->if_hwassist |= toset;
4205 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4211 if_sethwassist(if_t ifp, int hwassist_bit)
4213 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4218 if_gethwassist(if_t ifp)
4220 return ((struct ifnet *)ifp)->if_hwassist;
4224 if_setmtu(if_t ifp, int mtu)
4226 ((struct ifnet *)ifp)->if_mtu = mtu;
4233 return ((struct ifnet *)ifp)->if_mtu;
4237 if_getmtu_family(if_t ifp, int family)
4241 for (dp = domains; dp; dp = dp->dom_next) {
4242 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4243 return (dp->dom_ifmtu((struct ifnet *)ifp));
4246 return (((struct ifnet *)ifp)->if_mtu);
4250 * Methods for drivers to access interface unicast and multicast
4251 * link level addresses. Driver shall not know 'struct ifaddr' neither
4252 * 'struct ifmultiaddr'.
4255 if_lladdr_count(if_t ifp)
4257 struct epoch_tracker et;
4262 NET_EPOCH_ENTER(et);
4263 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4264 if (ifa->ifa_addr->sa_family == AF_LINK)
4272 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4274 struct epoch_tracker et;
4281 NET_EPOCH_ENTER(et);
4282 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4283 if (ifa->ifa_addr->sa_family != AF_LINK)
4285 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4294 if_llmaddr_count(if_t ifp)
4296 struct epoch_tracker et;
4297 struct ifmultiaddr *ifma;
4301 NET_EPOCH_ENTER(et);
4302 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4303 if (ifma->ifma_addr->sa_family == AF_LINK)
4311 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4313 struct epoch_tracker et;
4314 struct ifmultiaddr *ifma;
4320 NET_EPOCH_ENTER(et);
4321 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4322 if (ifma->ifma_addr->sa_family != AF_LINK)
4324 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4333 if_setsoftc(if_t ifp, void *softc)
4335 ((struct ifnet *)ifp)->if_softc = softc;
4340 if_getsoftc(if_t ifp)
4342 return ((struct ifnet *)ifp)->if_softc;
4346 if_setrcvif(struct mbuf *m, if_t ifp)
4349 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4350 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4354 if_setvtag(struct mbuf *m, uint16_t tag)
4356 m->m_pkthdr.ether_vtag = tag;
4360 if_getvtag(struct mbuf *m)
4363 return (m->m_pkthdr.ether_vtag);
4367 if_sendq_empty(if_t ifp)
4369 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4373 if_getifaddr(if_t ifp)
4375 return ((struct ifnet *)ifp)->if_addr;
4379 if_getamcount(if_t ifp)
4381 return ((struct ifnet *)ifp)->if_amcount;
4385 if_setsendqready(if_t ifp)
4387 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4392 if_setsendqlen(if_t ifp, int tx_desc_count)
4394 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4395 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4401 if_vlantrunkinuse(if_t ifp)
4403 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4407 if_input(if_t ifp, struct mbuf* sendmp)
4409 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4415 if_dequeue(if_t ifp)
4418 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4424 if_sendq_prepend(if_t ifp, struct mbuf *m)
4426 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4431 if_setifheaderlen(if_t ifp, int len)
4433 ((struct ifnet *)ifp)->if_hdrlen = len;
4438 if_getlladdr(if_t ifp)
4440 return (IF_LLADDR((struct ifnet *)ifp));
4444 if_gethandle(u_char type)
4446 return (if_alloc(type));
4450 if_bpfmtap(if_t ifh, struct mbuf *m)
4452 struct ifnet *ifp = (struct ifnet *)ifh;
4458 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4460 struct ifnet *ifp = (struct ifnet *)ifh;
4462 ETHER_BPF_MTAP(ifp, m);
4466 if_vlancap(if_t ifh)
4468 struct ifnet *ifp = (struct ifnet *)ifh;
4469 VLAN_CAPABILITIES(ifp);
4473 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4476 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4481 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4484 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4489 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4492 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4497 if_gethwtsomax(if_t ifp)
4500 return (((struct ifnet *)ifp)->if_hw_tsomax);
4504 if_gethwtsomaxsegcount(if_t ifp)
4507 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4511 if_gethwtsomaxsegsize(if_t ifp)
4514 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4518 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4520 ((struct ifnet *)ifp)->if_init = init_fn;
4524 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4526 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4530 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4532 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4536 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4538 ((struct ifnet *)ifp)->if_transmit = start_fn;
4541 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4543 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4548 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4551 ifp->if_get_counter = fn;
4554 /* Revisit these - These are inline functions originally. */
4556 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4558 return drbr_inuse(ifh, br);
4562 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4564 return drbr_dequeue(ifh, br);
4568 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4570 return drbr_needs_enqueue(ifh, br);
4574 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4576 return drbr_enqueue(ifh, br, m);