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)
1861 refcount_acquire(&ifa->ifa_refcnt);
1865 ifa_destroy(epoch_context_t ctx)
1869 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1870 counter_u64_free(ifa->ifa_opackets);
1871 counter_u64_free(ifa->ifa_ipackets);
1872 counter_u64_free(ifa->ifa_obytes);
1873 counter_u64_free(ifa->ifa_ibytes);
1874 free(ifa, M_IFADDR);
1878 ifa_free(struct ifaddr *ifa)
1881 if (refcount_release(&ifa->ifa_refcnt))
1882 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1886 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1887 * structs used to represent other address families, it is necessary
1888 * to perform a different comparison.
1891 #define sa_dl_equal(a1, a2) \
1892 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1893 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1894 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1895 CLLADDR((const struct sockaddr_dl *)(a2)), \
1896 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1899 * Locate an interface based on a complete address.
1903 ifa_ifwithaddr(const struct sockaddr *addr)
1910 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1911 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1912 if (ifa->ifa_addr->sa_family != addr->sa_family)
1914 if (sa_equal(addr, ifa->ifa_addr)) {
1917 /* IP6 doesn't have broadcast */
1918 if ((ifp->if_flags & IFF_BROADCAST) &&
1919 ifa->ifa_broadaddr &&
1920 ifa->ifa_broadaddr->sa_len != 0 &&
1921 sa_equal(ifa->ifa_broadaddr, addr)) {
1932 ifa_ifwithaddr_check(const struct sockaddr *addr)
1934 struct epoch_tracker et;
1937 NET_EPOCH_ENTER(et);
1938 rc = (ifa_ifwithaddr(addr) != NULL);
1944 * Locate an interface based on the broadcast address.
1948 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1954 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1955 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1957 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1958 if (ifa->ifa_addr->sa_family != addr->sa_family)
1960 if ((ifp->if_flags & IFF_BROADCAST) &&
1961 ifa->ifa_broadaddr &&
1962 ifa->ifa_broadaddr->sa_len != 0 &&
1963 sa_equal(ifa->ifa_broadaddr, addr)) {
1974 * Locate the point to point interface with a given destination address.
1978 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1984 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1985 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1987 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1989 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1990 if (ifa->ifa_addr->sa_family != addr->sa_family)
1992 if (ifa->ifa_dstaddr != NULL &&
1993 sa_equal(addr, ifa->ifa_dstaddr)) {
2004 * Find an interface on a specific network. If many, choice
2005 * is most specific found.
2008 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2012 struct ifaddr *ifa_maybe = NULL;
2013 u_int af = addr->sa_family;
2014 const char *addr_data = addr->sa_data, *cplim;
2018 * AF_LINK addresses can be looked up directly by their index number,
2019 * so do that if we can.
2021 if (af == AF_LINK) {
2022 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2023 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2024 return (ifaddr_byindex(sdl->sdl_index));
2028 * Scan though each interface, looking for ones that have addresses
2029 * in this address family and the requested fib.
2031 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2032 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2034 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2035 const char *cp, *cp2, *cp3;
2037 if (ifa->ifa_addr->sa_family != af)
2039 if (af == AF_INET &&
2040 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2042 * This is a bit broken as it doesn't
2043 * take into account that the remote end may
2044 * be a single node in the network we are
2046 * The trouble is that we don't know the
2047 * netmask for the remote end.
2049 if (ifa->ifa_dstaddr != NULL &&
2050 sa_equal(addr, ifa->ifa_dstaddr)) {
2055 * Scan all the bits in the ifa's address.
2056 * If a bit dissagrees with what we are
2057 * looking for, mask it with the netmask
2058 * to see if it really matters.
2059 * (A byte at a time)
2061 if (ifa->ifa_netmask == 0)
2064 cp2 = ifa->ifa_addr->sa_data;
2065 cp3 = ifa->ifa_netmask->sa_data;
2066 cplim = ifa->ifa_netmask->sa_len
2067 + (char *)ifa->ifa_netmask;
2069 if ((*cp++ ^ *cp2++) & *cp3++)
2070 goto next; /* next address! */
2072 * If the netmask of what we just found
2073 * is more specific than what we had before
2074 * (if we had one), or if the virtual status
2075 * of new prefix is better than of the old one,
2076 * then remember the new one before continuing
2077 * to search for an even better one.
2079 if (ifa_maybe == NULL ||
2080 ifa_preferred(ifa_maybe, ifa) ||
2081 rn_refines((caddr_t)ifa->ifa_netmask,
2082 (caddr_t)ifa_maybe->ifa_netmask)) {
2095 * Find an interface address specific to an interface best matching
2099 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2102 const char *cp, *cp2, *cp3;
2104 struct ifaddr *ifa_maybe = NULL;
2105 u_int af = addr->sa_family;
2111 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2112 if (ifa->ifa_addr->sa_family != af)
2114 if (ifa_maybe == NULL)
2116 if (ifa->ifa_netmask == 0) {
2117 if (sa_equal(addr, ifa->ifa_addr) ||
2118 (ifa->ifa_dstaddr &&
2119 sa_equal(addr, ifa->ifa_dstaddr)))
2123 if (ifp->if_flags & IFF_POINTOPOINT) {
2124 if (sa_equal(addr, ifa->ifa_dstaddr))
2128 cp2 = ifa->ifa_addr->sa_data;
2129 cp3 = ifa->ifa_netmask->sa_data;
2130 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2131 for (; cp3 < cplim; cp3++)
2132 if ((*cp++ ^ *cp2++) & *cp3)
2144 * See whether new ifa is better than current one:
2145 * 1) A non-virtual one is preferred over virtual.
2146 * 2) A virtual in master state preferred over any other state.
2148 * Used in several address selecting functions.
2151 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2154 return (cur->ifa_carp && (!next->ifa_carp ||
2155 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2158 struct sockaddr_dl *
2159 link_alloc_sdl(size_t size, int flags)
2162 return (malloc(size, M_TEMP, flags));
2166 link_free_sdl(struct sockaddr *sa)
2172 * Fills in given sdl with interface basic info.
2173 * Returns pointer to filled sdl.
2175 struct sockaddr_dl *
2176 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2178 struct sockaddr_dl *sdl;
2180 sdl = (struct sockaddr_dl *)paddr;
2181 memset(sdl, 0, sizeof(struct sockaddr_dl));
2182 sdl->sdl_len = sizeof(struct sockaddr_dl);
2183 sdl->sdl_family = AF_LINK;
2184 sdl->sdl_index = ifp->if_index;
2185 sdl->sdl_type = iftype;
2191 * Mark an interface down and notify protocols of
2195 if_unroute(struct ifnet *ifp, int flag, int fam)
2199 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2201 ifp->if_flags &= ~flag;
2202 getmicrotime(&ifp->if_lastchange);
2203 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2204 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2205 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2206 ifp->if_qflush(ifp);
2209 (*carp_linkstate_p)(ifp);
2214 * Mark an interface up and notify protocols of
2218 if_route(struct ifnet *ifp, int flag, int fam)
2222 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2224 ifp->if_flags |= flag;
2225 getmicrotime(&ifp->if_lastchange);
2226 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2227 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2228 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2230 (*carp_linkstate_p)(ifp);
2237 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2238 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2239 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2240 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2241 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2242 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2243 int (*vlan_setcookie_p)(struct ifnet *, void *);
2244 void *(*vlan_cookie_p)(struct ifnet *);
2247 * Handle a change in the interface link state. To avoid LORs
2248 * between driver lock and upper layer locks, as well as possible
2249 * recursions, we post event to taskqueue, and all job
2250 * is done in static do_link_state_change().
2253 if_link_state_change(struct ifnet *ifp, int link_state)
2255 /* Return if state hasn't changed. */
2256 if (ifp->if_link_state == link_state)
2259 ifp->if_link_state = link_state;
2261 /* XXXGL: reference ifp? */
2262 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2266 do_link_state_change(void *arg, int pending)
2272 link_state = ifp->if_link_state;
2274 CURVNET_SET(ifp->if_vnet);
2276 if (ifp->if_vlantrunk != NULL)
2277 (*vlan_link_state_p)(ifp);
2279 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2280 ifp->if_l2com != NULL)
2281 (*ng_ether_link_state_p)(ifp, link_state);
2283 (*carp_linkstate_p)(ifp);
2285 ifp->if_bridge_linkstate(ifp);
2287 (*lagg_linkstate_p)(ifp, link_state);
2289 if (IS_DEFAULT_VNET(curvnet))
2290 devctl_notify("IFNET", ifp->if_xname,
2291 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2294 if_printf(ifp, "%d link states coalesced\n", pending);
2295 if (log_link_state_change)
2296 if_printf(ifp, "link state changed to %s\n",
2297 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2298 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2303 * Mark an interface down and notify protocols of
2307 if_down(struct ifnet *ifp)
2310 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2311 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2315 * Mark an interface up and notify protocols of
2319 if_up(struct ifnet *ifp)
2322 if_route(ifp, IFF_UP, AF_UNSPEC);
2323 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2327 * Flush an interface queue.
2330 if_qflush(struct ifnet *ifp)
2338 if (ALTQ_IS_ENABLED(ifq))
2342 while ((m = n) != NULL) {
2353 * Map interface name to interface structure pointer, with or without
2354 * returning a reference.
2357 ifunit_ref(const char *name)
2359 struct epoch_tracker et;
2362 NET_EPOCH_ENTER(et);
2363 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2364 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2365 !(ifp->if_flags & IFF_DYING))
2375 ifunit(const char *name)
2377 struct epoch_tracker et;
2380 NET_EPOCH_ENTER(et);
2381 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2382 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2390 ifr_buffer_get_buffer(void *data)
2392 union ifreq_union *ifrup;
2395 #ifdef COMPAT_FREEBSD32
2396 if (SV_CURPROC_FLAG(SV_ILP32))
2397 return ((void *)(uintptr_t)
2398 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2400 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2404 ifr_buffer_set_buffer_null(void *data)
2406 union ifreq_union *ifrup;
2409 #ifdef COMPAT_FREEBSD32
2410 if (SV_CURPROC_FLAG(SV_ILP32))
2411 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2414 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2418 ifr_buffer_get_length(void *data)
2420 union ifreq_union *ifrup;
2423 #ifdef COMPAT_FREEBSD32
2424 if (SV_CURPROC_FLAG(SV_ILP32))
2425 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2427 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2431 ifr_buffer_set_length(void *data, size_t len)
2433 union ifreq_union *ifrup;
2436 #ifdef COMPAT_FREEBSD32
2437 if (SV_CURPROC_FLAG(SV_ILP32))
2438 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2441 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2445 ifr_data_get_ptr(void *ifrp)
2447 union ifreq_union *ifrup;
2450 #ifdef COMPAT_FREEBSD32
2451 if (SV_CURPROC_FLAG(SV_ILP32))
2452 return ((void *)(uintptr_t)
2453 ifrup->ifr32.ifr_ifru.ifru_data);
2455 return (ifrup->ifr.ifr_ifru.ifru_data);
2459 * Hardware specific interface ioctls.
2462 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2465 int error = 0, do_ifup = 0;
2466 int new_flags, temp_flags;
2467 size_t namelen, onamelen;
2469 char *descrbuf, *odescrbuf;
2470 char new_name[IFNAMSIZ];
2472 struct sockaddr_dl *sdl;
2474 ifr = (struct ifreq *)data;
2477 ifr->ifr_index = ifp->if_index;
2481 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2482 ifr->ifr_flags = temp_flags & 0xffff;
2483 ifr->ifr_flagshigh = temp_flags >> 16;
2487 ifr->ifr_reqcap = ifp->if_capabilities;
2488 ifr->ifr_curcap = ifp->if_capenable;
2495 /* Ensure uninitialised padding is not leaked. */
2496 memset(&ifd, 0, sizeof(ifd));
2498 if_data_copy(ifp, &ifd);
2499 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2505 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2510 ifr->ifr_metric = ifp->if_metric;
2514 ifr->ifr_mtu = ifp->if_mtu;
2518 /* XXXGL: did this ever worked? */
2524 sx_slock(&ifdescr_sx);
2525 if (ifp->if_description == NULL)
2528 /* space for terminating nul */
2529 descrlen = strlen(ifp->if_description) + 1;
2530 if (ifr_buffer_get_length(ifr) < descrlen)
2531 ifr_buffer_set_buffer_null(ifr);
2533 error = copyout(ifp->if_description,
2534 ifr_buffer_get_buffer(ifr), descrlen);
2535 ifr_buffer_set_length(ifr, descrlen);
2537 sx_sunlock(&ifdescr_sx);
2541 error = priv_check(td, PRIV_NET_SETIFDESCR);
2546 * Copy only (length-1) bytes to make sure that
2547 * if_description is always nul terminated. The
2548 * length parameter is supposed to count the
2549 * terminating nul in.
2551 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2552 return (ENAMETOOLONG);
2553 else if (ifr_buffer_get_length(ifr) == 0)
2556 descrbuf = malloc(ifr_buffer_get_length(ifr),
2557 M_IFDESCR, M_WAITOK | M_ZERO);
2558 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2559 ifr_buffer_get_length(ifr) - 1);
2561 free(descrbuf, M_IFDESCR);
2566 sx_xlock(&ifdescr_sx);
2567 odescrbuf = ifp->if_description;
2568 ifp->if_description = descrbuf;
2569 sx_xunlock(&ifdescr_sx);
2571 getmicrotime(&ifp->if_lastchange);
2572 free(odescrbuf, M_IFDESCR);
2576 ifr->ifr_fib = ifp->if_fib;
2580 error = priv_check(td, PRIV_NET_SETIFFIB);
2583 if (ifr->ifr_fib >= rt_numfibs)
2586 ifp->if_fib = ifr->ifr_fib;
2590 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2594 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2595 * check, so we don't need special handling here yet.
2597 new_flags = (ifr->ifr_flags & 0xffff) |
2598 (ifr->ifr_flagshigh << 16);
2599 if (ifp->if_flags & IFF_UP &&
2600 (new_flags & IFF_UP) == 0) {
2602 } else if (new_flags & IFF_UP &&
2603 (ifp->if_flags & IFF_UP) == 0) {
2606 /* See if permanently promiscuous mode bit is about to flip */
2607 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2608 if (new_flags & IFF_PPROMISC)
2609 ifp->if_flags |= IFF_PROMISC;
2610 else if (ifp->if_pcount == 0)
2611 ifp->if_flags &= ~IFF_PROMISC;
2612 if (log_promisc_mode_change)
2613 if_printf(ifp, "permanently promiscuous mode %s\n",
2614 ((new_flags & IFF_PPROMISC) ?
2615 "enabled" : "disabled"));
2617 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2618 (new_flags &~ IFF_CANTCHANGE);
2619 if (ifp->if_ioctl) {
2620 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2624 getmicrotime(&ifp->if_lastchange);
2628 error = priv_check(td, PRIV_NET_SETIFCAP);
2631 if (ifp->if_ioctl == NULL)
2632 return (EOPNOTSUPP);
2633 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2635 error = (*ifp->if_ioctl)(ifp, cmd, data);
2637 getmicrotime(&ifp->if_lastchange);
2642 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2647 error = priv_check(td, PRIV_NET_SETIFNAME);
2650 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2654 if (new_name[0] == '\0')
2656 if (new_name[IFNAMSIZ-1] != '\0') {
2657 new_name[IFNAMSIZ-1] = '\0';
2658 if (strlen(new_name) == IFNAMSIZ-1)
2661 if (strcmp(new_name, ifp->if_xname) == 0)
2663 if (ifunit(new_name) != NULL)
2667 * XXX: Locking. Nothing else seems to lock if_flags,
2668 * and there are numerous other races with the
2669 * ifunit() checks not being atomic with namespace
2670 * changes (renames, vmoves, if_attach, etc).
2672 ifp->if_flags |= IFF_RENAMING;
2674 /* Announce the departure of the interface. */
2675 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2676 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2678 if_printf(ifp, "changing name to '%s'\n", new_name);
2681 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2683 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2684 namelen = strlen(new_name);
2685 onamelen = sdl->sdl_nlen;
2687 * Move the address if needed. This is safe because we
2688 * allocate space for a name of length IFNAMSIZ when we
2689 * create this in if_attach().
2691 if (namelen != onamelen) {
2692 bcopy(sdl->sdl_data + onamelen,
2693 sdl->sdl_data + namelen, sdl->sdl_alen);
2695 bcopy(new_name, sdl->sdl_data, namelen);
2696 sdl->sdl_nlen = namelen;
2697 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2698 bzero(sdl->sdl_data, onamelen);
2699 while (namelen != 0)
2700 sdl->sdl_data[--namelen] = 0xff;
2701 IF_ADDR_WUNLOCK(ifp);
2703 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2704 /* Announce the return of the interface. */
2705 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2707 ifp->if_flags &= ~IFF_RENAMING;
2712 error = priv_check(td, PRIV_NET_SETIFVNET);
2715 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2720 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2723 ifp->if_metric = ifr->ifr_metric;
2724 getmicrotime(&ifp->if_lastchange);
2728 error = priv_check(td, PRIV_NET_SETIFPHYS);
2731 if (ifp->if_ioctl == NULL)
2732 return (EOPNOTSUPP);
2733 error = (*ifp->if_ioctl)(ifp, cmd, data);
2735 getmicrotime(&ifp->if_lastchange);
2740 u_long oldmtu = ifp->if_mtu;
2742 error = priv_check(td, PRIV_NET_SETIFMTU);
2745 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2747 if (ifp->if_ioctl == NULL)
2748 return (EOPNOTSUPP);
2749 error = (*ifp->if_ioctl)(ifp, cmd, data);
2751 getmicrotime(&ifp->if_lastchange);
2754 DEBUGNET_NOTIFY_MTU(ifp);
2758 * If the link MTU changed, do network layer specific procedure.
2760 if (ifp->if_mtu != oldmtu) {
2771 if (cmd == SIOCADDMULTI)
2772 error = priv_check(td, PRIV_NET_ADDMULTI);
2774 error = priv_check(td, PRIV_NET_DELMULTI);
2778 /* Don't allow group membership on non-multicast interfaces. */
2779 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2780 return (EOPNOTSUPP);
2782 /* Don't let users screw up protocols' entries. */
2783 if (ifr->ifr_addr.sa_family != AF_LINK)
2786 if (cmd == SIOCADDMULTI) {
2787 struct epoch_tracker et;
2788 struct ifmultiaddr *ifma;
2791 * Userland is only permitted to join groups once
2792 * via the if_addmulti() KPI, because it cannot hold
2793 * struct ifmultiaddr * between calls. It may also
2794 * lose a race while we check if the membership
2797 NET_EPOCH_ENTER(et);
2798 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2803 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2805 error = if_delmulti(ifp, &ifr->ifr_addr);
2808 getmicrotime(&ifp->if_lastchange);
2811 case SIOCSIFPHYADDR:
2812 case SIOCDIFPHYADDR:
2814 case SIOCSIFPHYADDR_IN6:
2817 case SIOCSIFGENERIC:
2818 error = priv_check(td, PRIV_NET_HWIOCTL);
2821 if (ifp->if_ioctl == NULL)
2822 return (EOPNOTSUPP);
2823 error = (*ifp->if_ioctl)(ifp, cmd, data);
2825 getmicrotime(&ifp->if_lastchange);
2829 case SIOCGIFPSRCADDR:
2830 case SIOCGIFPDSTADDR:
2833 case SIOCGIFGENERIC:
2835 case SIOCGIFRSSHASH:
2836 case SIOCGIFDOWNREASON:
2837 if (ifp->if_ioctl == NULL)
2838 return (EOPNOTSUPP);
2839 error = (*ifp->if_ioctl)(ifp, cmd, data);
2843 error = priv_check(td, PRIV_NET_SETLLADDR);
2846 error = if_setlladdr(ifp,
2847 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2851 error = if_gethwaddr(ifp, ifr);
2854 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2855 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2858 if ((error = if_addgroup(ifp,
2859 ifgr_group_get((struct ifgroupreq *)data))))
2863 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2865 struct epoch_tracker et;
2867 NET_EPOCH_ENTER(et);
2868 error = if_getgroup((struct ifgroupreq *)data, ifp);
2873 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2874 error = priv_check(td, PRIV_NET_DELIFGROUP);
2877 if ((error = if_delgroup(ifp,
2878 ifgr_group_get((struct ifgroupreq *)data))))
2889 #ifdef COMPAT_FREEBSD32
2897 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2900 #ifdef COMPAT_FREEBSD32
2902 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2904 struct ifmediareq32 *ifmr32;
2906 ifmr32 = (struct ifmediareq32 *)data;
2907 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2908 sizeof(ifmr->ifm_name));
2909 ifmr->ifm_current = ifmr32->ifm_current;
2910 ifmr->ifm_mask = ifmr32->ifm_mask;
2911 ifmr->ifm_status = ifmr32->ifm_status;
2912 ifmr->ifm_active = ifmr32->ifm_active;
2913 ifmr->ifm_count = ifmr32->ifm_count;
2914 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2918 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2920 struct ifmediareq32 *ifmr32;
2922 ifmr32 = (struct ifmediareq32 *)data;
2923 ifmr32->ifm_current = ifmr->ifm_current;
2924 ifmr32->ifm_mask = ifmr->ifm_mask;
2925 ifmr32->ifm_status = ifmr->ifm_status;
2926 ifmr32->ifm_active = ifmr->ifm_active;
2927 ifmr32->ifm_count = ifmr->ifm_count;
2935 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2937 #ifdef COMPAT_FREEBSD32
2938 caddr_t saved_data = NULL;
2939 struct ifmediareq ifmr;
2940 struct ifmediareq *ifmrp = NULL;
2950 CURVNET_SET(so->so_vnet);
2952 /* Make sure the VNET is stable. */
2953 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2962 error = ifconf(cmd, data);
2965 #ifdef COMPAT_FREEBSD32
2968 struct ifconf32 *ifc32;
2971 ifc32 = (struct ifconf32 *)data;
2972 ifc.ifc_len = ifc32->ifc_len;
2973 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2975 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2977 ifc32->ifc_len = ifc.ifc_len;
2983 #ifdef COMPAT_FREEBSD32
2985 case SIOCGIFMEDIA32:
2986 case SIOCGIFXMEDIA32:
2988 ifmr_init(ifmrp, data);
2989 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2991 data = (caddr_t)ifmrp;
2995 ifr = (struct ifreq *)data;
2999 error = priv_check(td, PRIV_NET_SETIFVNET);
3001 error = if_vmove_reclaim(td, ifr->ifr_name,
3007 error = priv_check(td, PRIV_NET_IFCREATE);
3009 error = if_clone_create(ifr->ifr_name,
3010 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3011 ifr_data_get_ptr(ifr) : NULL);
3014 error = priv_check(td, PRIV_NET_IFDESTROY);
3017 sx_xlock(&ifnet_detach_sxlock);
3018 error = if_clone_destroy(ifr->ifr_name);
3019 sx_xunlock(&ifnet_detach_sxlock);
3023 case SIOCIFGCLONERS:
3024 error = if_clone_list((struct if_clonereq *)data);
3027 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3028 error = if_getgroupmembers((struct ifgroupreq *)data);
3031 #if defined(INET) || defined(INET6)
3034 if (carp_ioctl_p == NULL)
3035 error = EPROTONOSUPPORT;
3037 error = (*carp_ioctl_p)(ifr, cmd, td);
3042 ifp = ifunit_ref(ifr->ifr_name);
3048 error = ifhwioctl(cmd, ifp, data, td);
3049 if (error != ENOIOCTL)
3052 oif_flags = ifp->if_flags;
3053 if (so->so_proto == NULL) {
3059 * Pass the request on to the socket control method, and if the
3060 * latter returns EOPNOTSUPP, directly to the interface.
3062 * Make an exception for the legacy SIOCSIF* requests. Drivers
3063 * trust SIOCSIFADDR et al to come from an already privileged
3064 * layer, and do not perform any credentials checks or input
3067 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3069 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3070 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3071 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3072 error = (*ifp->if_ioctl)(ifp, cmd, data);
3074 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3076 if (ifp->if_flags & IFF_UP)
3084 #ifdef COMPAT_FREEBSD32
3085 if (ifmrp != NULL) {
3086 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3087 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3090 ifmr_update(ifmrp, data);
3098 * The code common to handling reference counted flags,
3099 * e.g., in ifpromisc() and if_allmulti().
3100 * The "pflag" argument can specify a permanent mode flag to check,
3101 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3103 * Only to be used on stack-owned flags, not driver-owned flags.
3106 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3110 int oldflags, oldcount;
3112 /* Sanity checks to catch programming errors */
3113 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3114 ("%s: setting driver-owned flag %d", __func__, flag));
3117 KASSERT(*refcount >= 0,
3118 ("%s: increment negative refcount %d for flag %d",
3119 __func__, *refcount, flag));
3121 KASSERT(*refcount > 0,
3122 ("%s: decrement non-positive refcount %d for flag %d",
3123 __func__, *refcount, flag));
3125 /* In case this mode is permanent, just touch refcount */
3126 if (ifp->if_flags & pflag) {
3127 *refcount += onswitch ? 1 : -1;
3131 /* Save ifnet parameters for if_ioctl() may fail */
3132 oldcount = *refcount;
3133 oldflags = ifp->if_flags;
3136 * See if we aren't the only and touching refcount is enough.
3137 * Actually toggle interface flag if we are the first or last.
3142 ifp->if_flags |= flag;
3146 ifp->if_flags &= ~flag;
3149 /* Call down the driver since we've changed interface flags */
3150 if (ifp->if_ioctl == NULL) {
3154 ifr.ifr_flags = ifp->if_flags & 0xffff;
3155 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3156 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3159 /* Notify userland that interface flags have changed */
3164 /* Recover after driver error */
3165 *refcount = oldcount;
3166 ifp->if_flags = oldflags;
3171 * Set/clear promiscuous mode on interface ifp based on the truth value
3172 * of pswitch. The calls are reference counted so that only the first
3173 * "on" request actually has an effect, as does the final "off" request.
3174 * Results are undefined if the "off" and "on" requests are not matched.
3177 ifpromisc(struct ifnet *ifp, int pswitch)
3180 int oldflags = ifp->if_flags;
3182 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3183 &ifp->if_pcount, pswitch);
3184 /* If promiscuous mode status has changed, log a message */
3185 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3186 log_promisc_mode_change)
3187 if_printf(ifp, "promiscuous mode %s\n",
3188 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3193 * Return interface configuration
3194 * of system. List may be used
3195 * in later ioctl's (above) to get
3196 * other information.
3200 ifconf(u_long cmd, caddr_t data)
3202 struct ifconf *ifc = (struct ifconf *)data;
3207 int error, full = 0, valid_len, max_len;
3209 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3210 max_len = maxphys - 1;
3212 /* Prevent hostile input from being able to crash the system */
3213 if (ifc->ifc_len <= 0)
3217 if (ifc->ifc_len <= max_len) {
3218 max_len = ifc->ifc_len;
3221 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3226 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3227 struct epoch_tracker et;
3231 * Zero the ifr to make sure we don't disclose the contents
3234 memset(&ifr, 0, sizeof(ifr));
3236 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3237 >= sizeof(ifr.ifr_name)) {
3240 return (ENAMETOOLONG);
3244 NET_EPOCH_ENTER(et);
3245 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3246 struct sockaddr *sa = ifa->ifa_addr;
3248 if (prison_if(curthread->td_ucred, sa) != 0)
3251 if (sa->sa_len <= sizeof(*sa)) {
3252 if (sa->sa_len < sizeof(*sa)) {
3253 memset(&ifr.ifr_ifru.ifru_addr, 0,
3254 sizeof(ifr.ifr_ifru.ifru_addr));
3255 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3258 ifr.ifr_ifru.ifru_addr = *sa;
3259 sbuf_bcat(sb, &ifr, sizeof(ifr));
3260 max_len += sizeof(ifr);
3263 offsetof(struct ifreq, ifr_addr));
3264 max_len += offsetof(struct ifreq, ifr_addr);
3265 sbuf_bcat(sb, sa, sa->sa_len);
3266 max_len += sa->sa_len;
3269 if (sbuf_error(sb) == 0)
3270 valid_len = sbuf_len(sb);
3274 sbuf_bcat(sb, &ifr, sizeof(ifr));
3275 max_len += sizeof(ifr);
3277 if (sbuf_error(sb) == 0)
3278 valid_len = sbuf_len(sb);
3284 * If we didn't allocate enough space (uncommon), try again. If
3285 * we have already allocated as much space as we are allowed,
3286 * return what we've got.
3288 if (valid_len != max_len && !full) {
3293 ifc->ifc_len = valid_len;
3295 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3301 * Just like ifpromisc(), but for all-multicast-reception mode.
3304 if_allmulti(struct ifnet *ifp, int onswitch)
3307 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3310 struct ifmultiaddr *
3311 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3313 struct ifmultiaddr *ifma;
3315 IF_ADDR_LOCK_ASSERT(ifp);
3317 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3318 if (sa->sa_family == AF_LINK) {
3319 if (sa_dl_equal(ifma->ifma_addr, sa))
3322 if (sa_equal(ifma->ifma_addr, sa))
3331 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3332 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3333 * the ifnet multicast address list here, so the caller must do that and
3334 * other setup work (such as notifying the device driver). The reference
3335 * count is initialized to 1.
3337 static struct ifmultiaddr *
3338 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3341 struct ifmultiaddr *ifma;
3342 struct sockaddr *dupsa;
3344 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3349 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3350 if (dupsa == NULL) {
3351 free(ifma, M_IFMADDR);
3354 bcopy(sa, dupsa, sa->sa_len);
3355 ifma->ifma_addr = dupsa;
3357 ifma->ifma_ifp = ifp;
3358 ifma->ifma_refcount = 1;
3359 ifma->ifma_protospec = NULL;
3362 ifma->ifma_lladdr = NULL;
3366 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3367 if (dupsa == NULL) {
3368 free(ifma->ifma_addr, M_IFMADDR);
3369 free(ifma, M_IFMADDR);
3372 bcopy(llsa, dupsa, llsa->sa_len);
3373 ifma->ifma_lladdr = dupsa;
3379 * if_freemulti: free ifmultiaddr structure and possibly attached related
3380 * addresses. The caller is responsible for implementing reference
3381 * counting, notifying the driver, handling routing messages, and releasing
3382 * any dependent link layer state.
3384 #ifdef MCAST_VERBOSE
3385 extern void kdb_backtrace(void);
3388 if_freemulti_internal(struct ifmultiaddr *ifma)
3391 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3392 ifma->ifma_refcount));
3394 if (ifma->ifma_lladdr != NULL)
3395 free(ifma->ifma_lladdr, M_IFMADDR);
3396 #ifdef MCAST_VERBOSE
3398 printf("%s freeing ifma: %p\n", __func__, ifma);
3400 free(ifma->ifma_addr, M_IFMADDR);
3401 free(ifma, M_IFMADDR);
3405 if_destroymulti(epoch_context_t ctx)
3407 struct ifmultiaddr *ifma;
3409 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3410 if_freemulti_internal(ifma);
3414 if_freemulti(struct ifmultiaddr *ifma)
3416 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3417 ifma->ifma_refcount));
3419 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3423 * Register an additional multicast address with a network interface.
3425 * - If the address is already present, bump the reference count on the
3426 * address and return.
3427 * - If the address is not link-layer, look up a link layer address.
3428 * - Allocate address structures for one or both addresses, and attach to the
3429 * multicast address list on the interface. If automatically adding a link
3430 * layer address, the protocol address will own a reference to the link
3431 * layer address, to be freed when it is freed.
3432 * - Notify the network device driver of an addition to the multicast address
3435 * 'sa' points to caller-owned memory with the desired multicast address.
3437 * 'retifma' will be used to return a pointer to the resulting multicast
3438 * address reference, if desired.
3441 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3442 struct ifmultiaddr **retifma)
3444 struct ifmultiaddr *ifma, *ll_ifma;
3445 struct sockaddr *llsa;
3446 struct sockaddr_dl sdl;
3450 IN_MULTI_LIST_UNLOCK_ASSERT();
3453 IN6_MULTI_LIST_UNLOCK_ASSERT();
3456 * If the address is already present, return a new reference to it;
3457 * otherwise, allocate storage and set up a new address.
3460 ifma = if_findmulti(ifp, sa);
3462 ifma->ifma_refcount++;
3463 if (retifma != NULL)
3465 IF_ADDR_WUNLOCK(ifp);
3470 * The address isn't already present; resolve the protocol address
3471 * into a link layer address, and then look that up, bump its
3472 * refcount or allocate an ifma for that also.
3473 * Most link layer resolving functions returns address data which
3474 * fits inside default sockaddr_dl structure. However callback
3475 * can allocate another sockaddr structure, in that case we need to
3480 if (ifp->if_resolvemulti != NULL) {
3481 /* Provide called function with buffer size information */
3482 sdl.sdl_len = sizeof(sdl);
3483 llsa = (struct sockaddr *)&sdl;
3484 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3490 * Allocate the new address. Don't hook it up yet, as we may also
3491 * need to allocate a link layer multicast address.
3493 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3500 * If a link layer address is found, we'll need to see if it's
3501 * already present in the address list, or allocate is as well.
3502 * When this block finishes, the link layer address will be on the
3506 ll_ifma = if_findmulti(ifp, llsa);
3507 if (ll_ifma == NULL) {
3508 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3509 if (ll_ifma == NULL) {
3510 --ifma->ifma_refcount;
3515 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3516 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3519 ll_ifma->ifma_refcount++;
3520 ifma->ifma_llifma = ll_ifma;
3524 * We now have a new multicast address, ifma, and possibly a new or
3525 * referenced link layer address. Add the primary address to the
3526 * ifnet address list.
3528 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3529 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3531 if (retifma != NULL)
3535 * Must generate the message while holding the lock so that 'ifma'
3536 * pointer is still valid.
3538 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3539 IF_ADDR_WUNLOCK(ifp);
3542 * We are certain we have added something, so call down to the
3543 * interface to let them know about it.
3545 if (ifp->if_ioctl != NULL) {
3546 if (THREAD_CAN_SLEEP())
3547 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3549 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3552 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3553 link_free_sdl(llsa);
3558 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3559 link_free_sdl(llsa);
3562 IF_ADDR_WUNLOCK(ifp);
3567 if_siocaddmulti(void *arg, int pending)
3574 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3576 CURVNET_SET(ifp->if_vnet);
3577 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3582 * Delete a multicast group membership by network-layer group address.
3584 * Returns ENOENT if the entry could not be found. If ifp no longer
3585 * exists, results are undefined. This entry point should only be used
3586 * from subsystems which do appropriate locking to hold ifp for the
3587 * duration of the call.
3588 * Network-layer protocol domains must use if_delmulti_ifma().
3591 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3593 struct ifmultiaddr *ifma;
3596 KASSERT(ifp, ("%s: NULL ifp", __func__));
3600 ifma = if_findmulti(ifp, sa);
3602 lastref = if_delmulti_locked(ifp, ifma, 0);
3603 IF_ADDR_WUNLOCK(ifp);
3608 if (lastref && ifp->if_ioctl != NULL) {
3609 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3616 * Delete all multicast group membership for an interface.
3617 * Should be used to quickly flush all multicast filters.
3620 if_delallmulti(struct ifnet *ifp)
3622 struct ifmultiaddr *ifma;
3623 struct ifmultiaddr *next;
3626 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3627 if_delmulti_locked(ifp, ifma, 0);
3628 IF_ADDR_WUNLOCK(ifp);
3632 if_delmulti_ifma(struct ifmultiaddr *ifma)
3634 if_delmulti_ifma_flags(ifma, 0);
3638 * Delete a multicast group membership by group membership pointer.
3639 * Network-layer protocol domains must use this routine.
3641 * It is safe to call this routine if the ifp disappeared.
3644 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3648 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3650 IN_MULTI_LIST_UNLOCK_ASSERT();
3652 ifp = ifma->ifma_ifp;
3655 printf("%s: ifma_ifp seems to be detached\n", __func__);
3657 struct epoch_tracker et;
3660 NET_EPOCH_ENTER(et);
3661 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3670 * If and only if the ifnet instance exists: Acquire the address lock.
3675 lastref = if_delmulti_locked(ifp, ifma, flags);
3679 * If and only if the ifnet instance exists:
3680 * Release the address lock.
3681 * If the group was left: update the hardware hash filter.
3683 IF_ADDR_WUNLOCK(ifp);
3684 if (lastref && ifp->if_ioctl != NULL) {
3685 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3691 * Perform deletion of network-layer and/or link-layer multicast address.
3693 * Return 0 if the reference count was decremented.
3694 * Return 1 if the final reference was released, indicating that the
3695 * hardware hash filter should be reprogrammed.
3698 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3700 struct ifmultiaddr *ll_ifma;
3702 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3703 KASSERT(ifma->ifma_ifp == ifp,
3704 ("%s: inconsistent ifp %p", __func__, ifp));
3705 IF_ADDR_WLOCK_ASSERT(ifp);
3708 ifp = ifma->ifma_ifp;
3709 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3712 * If the ifnet is detaching, null out references to ifnet,
3713 * so that upper protocol layers will notice, and not attempt
3714 * to obtain locks for an ifnet which no longer exists. The
3715 * routing socket announcement must happen before the ifnet
3716 * instance is detached from the system.
3720 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3723 * ifp may already be nulled out if we are being reentered
3724 * to delete the ll_ifma.
3727 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3728 ifma->ifma_ifp = NULL;
3732 if (--ifma->ifma_refcount > 0)
3735 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3736 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3737 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3740 * If this ifma is a network-layer ifma, a link-layer ifma may
3741 * have been associated with it. Release it first if so.
3743 ll_ifma = ifma->ifma_llifma;
3744 if (ll_ifma != NULL) {
3745 KASSERT(ifma->ifma_lladdr != NULL,
3746 ("%s: llifma w/o lladdr", __func__));
3748 ll_ifma->ifma_ifp = NULL; /* XXX */
3749 if (--ll_ifma->ifma_refcount == 0) {
3751 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3752 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3754 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3757 if_freemulti(ll_ifma);
3762 struct ifmultiaddr *ifmatmp;
3764 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3765 MPASS(ifma != ifmatmp);
3770 * The last reference to this instance of struct ifmultiaddr
3771 * was released; the hardware should be notified of this change.
3777 * Set the link layer address on an interface.
3779 * At this time we only support certain types of interfaces,
3780 * and we don't allow the length of the address to change.
3782 * Set noinline to be dtrace-friendly
3785 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3787 struct sockaddr_dl *sdl;
3795 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3799 if (len != sdl->sdl_alen) /* don't allow length to change */
3802 switch (ifp->if_type) {
3807 case IFT_IEEE8023ADLAG:
3808 bcopy(lladdr, LLADDR(sdl), len);
3815 * If the interface is already up, we need
3816 * to re-init it in order to reprogram its
3819 if ((ifp->if_flags & IFF_UP) != 0) {
3820 if (ifp->if_ioctl) {
3821 ifp->if_flags &= ~IFF_UP;
3822 ifr.ifr_flags = ifp->if_flags & 0xffff;
3823 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3824 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3825 ifp->if_flags |= IFF_UP;
3826 ifr.ifr_flags = ifp->if_flags & 0xffff;
3827 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3828 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3831 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3837 * Compat function for handling basic encapsulation requests.
3838 * Not converted stacks (FDDI, IB, ..) supports traditional
3839 * output model: ARP (and other similar L2 protocols) are handled
3840 * inside output routine, arpresolve/nd6_resolve() returns MAC
3841 * address instead of full prepend.
3843 * This function creates calculated header==MAC for IPv4/IPv6 and
3844 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3848 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3851 if (req->rtype != IFENCAP_LL)
3852 return (EOPNOTSUPP);
3854 if (req->bufsize < req->lladdr_len)
3857 switch (req->family) {
3862 return (EAFNOSUPPORT);
3865 /* Copy lladdr to storage as is */
3866 memmove(req->buf, req->lladdr, req->lladdr_len);
3867 req->bufsize = req->lladdr_len;
3868 req->lladdr_off = 0;
3874 * Tunnel interfaces can nest, also they may cause infinite recursion
3875 * calls when misconfigured. We'll prevent this by detecting loops.
3876 * High nesting level may cause stack exhaustion. We'll prevent this
3877 * by introducing upper limit.
3879 * Return 0, if tunnel nesting count is equal or less than limit.
3882 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3890 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3891 if (*(struct ifnet **)(mtag + 1) == ifp) {
3892 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3897 if (count > limit) {
3899 "%s: if_output recursively called too many times(%d)\n",
3900 if_name(ifp), count);
3903 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3906 *(struct ifnet **)(mtag + 1) = ifp;
3907 m_tag_prepend(m, mtag);
3912 * Get the link layer address that was read from the hardware at attach.
3914 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3915 * their component interfaces as IFT_IEEE8023ADLAG.
3918 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3921 if (ifp->if_hw_addr == NULL)
3924 switch (ifp->if_type) {
3926 case IFT_IEEE8023ADLAG:
3927 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3935 * The name argument must be a pointer to storage which will last as
3936 * long as the interface does. For physical devices, the result of
3937 * device_get_name(dev) is a good choice and for pseudo-devices a
3938 * static string works well.
3941 if_initname(struct ifnet *ifp, const char *name, int unit)
3943 ifp->if_dname = name;
3944 ifp->if_dunit = unit;
3945 if (unit != IF_DUNIT_NONE)
3946 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3948 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3952 if_printf(struct ifnet *ifp, const char *fmt, ...)
3957 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3959 vlog(LOG_INFO, if_fmt, ap);
3965 if_start(struct ifnet *ifp)
3968 (*(ifp)->if_start)(ifp);
3972 * Backwards compatibility interface for drivers
3973 * that have not implemented it
3976 if_transmit(struct ifnet *ifp, struct mbuf *m)
3980 IFQ_HANDOFF(ifp, m, error);
3985 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3992 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3997 if (_IF_QFULL(ifq)) {
3999 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4004 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4005 if (m->m_flags & (M_BCAST|M_MCAST))
4006 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4007 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4009 _IF_ENQUEUE(ifq, m);
4011 if (ifp != NULL && !active)
4012 (*(ifp)->if_start)(ifp);
4017 if_register_com_alloc(u_char type,
4018 if_com_alloc_t *a, if_com_free_t *f)
4021 KASSERT(if_com_alloc[type] == NULL,
4022 ("if_register_com_alloc: %d already registered", type));
4023 KASSERT(if_com_free[type] == NULL,
4024 ("if_register_com_alloc: %d free already registered", type));
4026 if_com_alloc[type] = a;
4027 if_com_free[type] = f;
4031 if_deregister_com_alloc(u_char type)
4034 KASSERT(if_com_alloc[type] != NULL,
4035 ("if_deregister_com_alloc: %d not registered", type));
4036 KASSERT(if_com_free[type] != NULL,
4037 ("if_deregister_com_alloc: %d free not registered", type));
4038 if_com_alloc[type] = NULL;
4039 if_com_free[type] = NULL;
4042 /* API for driver access to network stack owned ifnet.*/
4044 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4048 oldbrate = ifp->if_baudrate;
4049 ifp->if_baudrate = baudrate;
4054 if_getbaudrate(if_t ifp)
4057 return (((struct ifnet *)ifp)->if_baudrate);
4061 if_setcapabilities(if_t ifp, int capabilities)
4063 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4068 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4070 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4071 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4077 if_getcapabilities(if_t ifp)
4079 return ((struct ifnet *)ifp)->if_capabilities;
4083 if_setcapenable(if_t ifp, int capabilities)
4085 ((struct ifnet *)ifp)->if_capenable = capabilities;
4090 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4093 ((struct ifnet *)ifp)->if_capenable |= setcap;
4095 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4101 if_getdname(if_t ifp)
4103 return ((struct ifnet *)ifp)->if_dname;
4107 if_togglecapenable(if_t ifp, int togglecap)
4109 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4114 if_getcapenable(if_t ifp)
4116 return ((struct ifnet *)ifp)->if_capenable;
4120 * This is largely undesirable because it ties ifnet to a device, but does
4121 * provide flexiblity for an embedded product vendor. Should be used with
4122 * the understanding that it violates the interface boundaries, and should be
4123 * a last resort only.
4126 if_setdev(if_t ifp, void *dev)
4132 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4134 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4135 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4141 if_getdrvflags(if_t ifp)
4143 return ((struct ifnet *)ifp)->if_drv_flags;
4147 if_setdrvflags(if_t ifp, int flags)
4149 ((struct ifnet *)ifp)->if_drv_flags = flags;
4154 if_setflags(if_t ifp, int flags)
4157 ifp->if_flags = flags;
4162 if_setflagbits(if_t ifp, int set, int clear)
4164 ((struct ifnet *)ifp)->if_flags |= set;
4165 ((struct ifnet *)ifp)->if_flags &= ~clear;
4171 if_getflags(if_t ifp)
4173 return ((struct ifnet *)ifp)->if_flags;
4177 if_clearhwassist(if_t ifp)
4179 ((struct ifnet *)ifp)->if_hwassist = 0;
4184 if_sethwassistbits(if_t ifp, int toset, int toclear)
4186 ((struct ifnet *)ifp)->if_hwassist |= toset;
4187 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4193 if_sethwassist(if_t ifp, int hwassist_bit)
4195 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4200 if_gethwassist(if_t ifp)
4202 return ((struct ifnet *)ifp)->if_hwassist;
4206 if_setmtu(if_t ifp, int mtu)
4208 ((struct ifnet *)ifp)->if_mtu = mtu;
4215 return ((struct ifnet *)ifp)->if_mtu;
4219 if_getmtu_family(if_t ifp, int family)
4223 for (dp = domains; dp; dp = dp->dom_next) {
4224 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4225 return (dp->dom_ifmtu((struct ifnet *)ifp));
4228 return (((struct ifnet *)ifp)->if_mtu);
4232 * Methods for drivers to access interface unicast and multicast
4233 * link level addresses. Driver shall not know 'struct ifaddr' neither
4234 * 'struct ifmultiaddr'.
4237 if_lladdr_count(if_t ifp)
4239 struct epoch_tracker et;
4244 NET_EPOCH_ENTER(et);
4245 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4246 if (ifa->ifa_addr->sa_family == AF_LINK)
4254 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4256 struct epoch_tracker et;
4263 NET_EPOCH_ENTER(et);
4264 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4265 if (ifa->ifa_addr->sa_family != AF_LINK)
4267 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4276 if_llmaddr_count(if_t ifp)
4278 struct epoch_tracker et;
4279 struct ifmultiaddr *ifma;
4283 NET_EPOCH_ENTER(et);
4284 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4285 if (ifma->ifma_addr->sa_family == AF_LINK)
4293 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4295 struct epoch_tracker et;
4296 struct ifmultiaddr *ifma;
4302 NET_EPOCH_ENTER(et);
4303 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4304 if (ifma->ifma_addr->sa_family != AF_LINK)
4306 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4315 if_setsoftc(if_t ifp, void *softc)
4317 ((struct ifnet *)ifp)->if_softc = softc;
4322 if_getsoftc(if_t ifp)
4324 return ((struct ifnet *)ifp)->if_softc;
4328 if_setrcvif(struct mbuf *m, if_t ifp)
4331 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4332 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4336 if_setvtag(struct mbuf *m, uint16_t tag)
4338 m->m_pkthdr.ether_vtag = tag;
4342 if_getvtag(struct mbuf *m)
4345 return (m->m_pkthdr.ether_vtag);
4349 if_sendq_empty(if_t ifp)
4351 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4355 if_getifaddr(if_t ifp)
4357 return ((struct ifnet *)ifp)->if_addr;
4361 if_getamcount(if_t ifp)
4363 return ((struct ifnet *)ifp)->if_amcount;
4367 if_setsendqready(if_t ifp)
4369 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4374 if_setsendqlen(if_t ifp, int tx_desc_count)
4376 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4377 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4383 if_vlantrunkinuse(if_t ifp)
4385 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4389 if_input(if_t ifp, struct mbuf* sendmp)
4391 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4397 if_dequeue(if_t ifp)
4400 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4406 if_sendq_prepend(if_t ifp, struct mbuf *m)
4408 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4413 if_setifheaderlen(if_t ifp, int len)
4415 ((struct ifnet *)ifp)->if_hdrlen = len;
4420 if_getlladdr(if_t ifp)
4422 return (IF_LLADDR((struct ifnet *)ifp));
4426 if_gethandle(u_char type)
4428 return (if_alloc(type));
4432 if_bpfmtap(if_t ifh, struct mbuf *m)
4434 struct ifnet *ifp = (struct ifnet *)ifh;
4440 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4442 struct ifnet *ifp = (struct ifnet *)ifh;
4444 ETHER_BPF_MTAP(ifp, m);
4448 if_vlancap(if_t ifh)
4450 struct ifnet *ifp = (struct ifnet *)ifh;
4451 VLAN_CAPABILITIES(ifp);
4455 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4458 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4463 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4466 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4471 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4474 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4479 if_gethwtsomax(if_t ifp)
4482 return (((struct ifnet *)ifp)->if_hw_tsomax);
4486 if_gethwtsomaxsegcount(if_t ifp)
4489 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4493 if_gethwtsomaxsegsize(if_t ifp)
4496 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4500 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4502 ((struct ifnet *)ifp)->if_init = init_fn;
4506 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4508 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4512 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4514 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4518 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4520 ((struct ifnet *)ifp)->if_transmit = start_fn;
4523 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4525 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4530 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4533 ifp->if_get_counter = fn;
4536 /* Revisit these - These are inline functions originally. */
4538 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4540 return drbr_inuse(ifh, br);
4544 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4546 return drbr_dequeue(ifh, br);
4550 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4552 return drbr_needs_enqueue(ifh, br);
4556 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4558 return drbr_enqueue(ifh, br, m);