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);
318 * The allocation of network interfaces is a rather non-atomic affair; we
319 * need to select an index before we are ready to expose the interface for
320 * use, so will use this pointer value to indicate reservation.
322 #define IFNET_HOLD (void *)(uintptr_t)(-1)
325 #define VNET_IS_SHUTTING_DOWN(_vnet) \
326 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
329 static if_com_alloc_t *if_com_alloc[256];
330 static if_com_free_t *if_com_free[256];
332 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
333 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
334 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
337 ifnet_byindex(u_short idx)
341 if (__predict_false(idx > V_if_index))
344 ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx);
345 return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp);
349 ifnet_byindex_ref(u_short idx)
355 ifp = ifnet_byindex(idx);
356 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
363 * Allocate an ifindex array entry; return 0 on success or an error on
367 ifindex_alloc(void **old)
371 IFNET_WLOCK_ASSERT();
373 * Try to find an empty slot below V_if_index. If we fail, take the
376 for (idx = 1; idx <= V_if_index; idx++) {
377 if (V_ifindex_table[idx] == NULL)
381 /* Catch if_index overflow. */
382 if (idx >= V_if_indexlim) {
386 if (idx > V_if_index)
392 ifindex_free_locked(u_short idx)
395 IFNET_WLOCK_ASSERT();
397 V_ifindex_table[idx] = NULL;
398 while (V_if_index > 0 &&
399 V_ifindex_table[V_if_index] == NULL)
404 ifindex_free(u_short idx)
408 ifindex_free_locked(idx);
413 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
416 V_ifindex_table[idx] = ifp;
420 ifaddr_byindex(u_short idx)
423 struct ifaddr *ifa = NULL;
427 ifp = ifnet_byindex(idx);
428 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
434 * Network interface utility routines.
436 * Routines with ifa_ifwith* names take sockaddr *'s as
441 vnet_if_init(const void *unused __unused)
445 CK_STAILQ_INIT(&V_ifnet);
446 CK_STAILQ_INIT(&V_ifg_head);
448 old = if_grow(); /* create initial table */
450 epoch_wait_preempt(net_epoch_preempt);
452 vnet_if_clone_init();
454 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
459 vnet_if_uninit(const void *unused __unused)
462 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
463 "not empty", __func__, __LINE__, &V_ifnet));
464 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
465 "not empty", __func__, __LINE__, &V_ifg_head));
467 free((caddr_t)V_ifindex_table, M_IFNET);
469 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
470 vnet_if_uninit, NULL);
474 if_link_ifnet(struct ifnet *ifp)
478 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
480 curvnet->vnet_ifcnt++;
486 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
492 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
494 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
496 ifp->if_flags |= IFF_DYING;
501 curvnet->vnet_ifcnt--;
510 vnet_if_return(const void *unused __unused)
512 struct ifnet *ifp, *nifp;
513 struct ifnet **pending;
519 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
520 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
521 * if_detach_internal(), which waits for NET_EPOCH callbacks to
522 * complete. We can't do that from within NET_EPOCH.
524 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
525 * read/write lock. We cannot hold the lock as we call if_vmove()
526 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
531 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
532 M_IFNET, M_WAITOK | M_ZERO);
534 /* Return all inherited interfaces to their parent vnets. */
535 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
536 if (ifp->if_home_vnet != ifp->if_vnet) {
537 found = if_unlink_ifnet(ifp, true);
545 for (int j = 0; j < i; j++) {
546 if_vmove(pending[j], pending[j]->if_home_vnet);
549 free(pending, M_IFNET);
551 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
552 vnet_if_return, NULL);
564 IFNET_WLOCK_ASSERT();
565 oldlim = V_if_indexlim;
567 n = (oldlim << 1) * sizeof(*e);
568 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
570 if (V_if_indexlim != oldlim) {
574 if (V_ifindex_table != NULL) {
575 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
576 old = V_ifindex_table;
584 * Allocate a struct ifnet and an index for an interface. A layer 2
585 * common structure will also be allocated if an allocation routine is
586 * registered for the passed type.
589 if_alloc_domain(u_char type, int numa_domain)
595 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
596 if (numa_domain == IF_NODOM)
597 ifp = malloc(sizeof(struct ifnet), M_IFNET,
600 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
601 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
604 idx = ifindex_alloc(&old);
605 if (__predict_false(idx == USHRT_MAX)) {
607 epoch_wait_preempt(net_epoch_preempt);
611 ifnet_setbyindex(idx, IFNET_HOLD);
615 ifp->if_alloctype = type;
616 ifp->if_numa_domain = numa_domain;
618 ifp->if_vnet = curvnet;
620 if (if_com_alloc[type] != NULL) {
621 ifp->if_l2com = if_com_alloc[type](type, ifp);
622 if (ifp->if_l2com == NULL) {
629 IF_ADDR_LOCK_INIT(ifp);
630 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
631 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
632 ifp->if_afdata_initialized = 0;
633 IF_AFDATA_LOCK_INIT(ifp);
634 CK_STAILQ_INIT(&ifp->if_addrhead);
635 CK_STAILQ_INIT(&ifp->if_multiaddrs);
636 CK_STAILQ_INIT(&ifp->if_groups);
640 ifq_init(&ifp->if_snd, ifp);
642 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
643 for (int i = 0; i < IFCOUNTERS; i++)
644 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
645 ifp->if_get_counter = if_get_counter_default;
646 ifp->if_pcp = IFNET_PCP_NONE;
647 ifnet_setbyindex(ifp->if_index, ifp);
652 if_alloc_dev(u_char type, device_t dev)
656 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
657 return (if_alloc_domain(type, IF_NODOM));
658 return (if_alloc_domain(type, numa_domain));
662 if_alloc(u_char type)
665 return (if_alloc_domain(type, IF_NODOM));
668 * Do the actual work of freeing a struct ifnet, and layer 2 common
669 * structure. This call is made when the last reference to an
670 * interface is released.
673 if_free_internal(struct ifnet *ifp)
676 KASSERT((ifp->if_flags & IFF_DYING),
677 ("if_free_internal: interface not dying"));
679 if (if_com_free[ifp->if_alloctype] != NULL)
680 if_com_free[ifp->if_alloctype](ifp->if_l2com,
684 mac_ifnet_destroy(ifp);
686 IF_AFDATA_DESTROY(ifp);
687 IF_ADDR_LOCK_DESTROY(ifp);
688 ifq_delete(&ifp->if_snd);
690 for (int i = 0; i < IFCOUNTERS; i++)
691 counter_u64_free(ifp->if_counters[i]);
693 free(ifp->if_description, M_IFDESCR);
694 free(ifp->if_hw_addr, M_IFADDR);
699 if_destroy(epoch_context_t ctx)
703 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
704 if_free_internal(ifp);
708 * Deregister an interface and free the associated storage.
711 if_free(struct ifnet *ifp)
714 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
716 CURVNET_SET_QUIET(ifp->if_vnet);
718 KASSERT(ifp == ifnet_byindex(ifp->if_index),
719 ("%s: freeing unallocated ifnet", ifp->if_xname));
721 ifindex_free_locked(ifp->if_index);
724 if (refcount_release(&ifp->if_refcount))
725 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
730 * Interfaces to keep an ifnet type-stable despite the possibility of the
731 * driver calling if_free(). If there are additional references, we defer
732 * freeing the underlying data structure.
735 if_ref(struct ifnet *ifp)
738 /* We don't assert the ifnet list lock here, but arguably should. */
739 refcount_acquire(&ifp->if_refcount);
743 if_rele(struct ifnet *ifp)
746 if (!refcount_release(&ifp->if_refcount))
748 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
752 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
755 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
757 if (ifq->ifq_maxlen == 0)
758 ifq->ifq_maxlen = ifqmaxlen;
761 ifq->altq_disc = NULL;
762 ifq->altq_flags &= ALTQF_CANTCHANGE;
763 ifq->altq_tbr = NULL;
768 ifq_delete(struct ifaltq *ifq)
770 mtx_destroy(&ifq->ifq_mtx);
774 * Perform generic interface initialization tasks and attach the interface
775 * to the list of "active" interfaces. If vmove flag is set on entry
776 * to if_attach_internal(), perform only a limited subset of initialization
777 * tasks, given that we are moving from one vnet to another an ifnet which
778 * has already been fully initialized.
780 * Note that if_detach_internal() removes group membership unconditionally
781 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
782 * Thus, when if_vmove() is applied to a cloned interface, group membership
783 * is lost while a cloned one always joins a group whose name is
784 * ifc->ifc_name. To recover this after if_detach_internal() and
785 * if_attach_internal(), the cloner should be specified to
786 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
787 * attempts to join a group whose name is ifc->ifc_name.
790 * - The decision to return void and thus require this function to
791 * succeed is questionable.
792 * - We should probably do more sanity checking. For instance we don't
793 * do anything to insure if_xname is unique or non-empty.
796 if_attach(struct ifnet *ifp)
799 if_attach_internal(ifp, 0, NULL);
803 * Compute the least common TSO limit.
806 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
809 * 1) If there is no limit currently, take the limit from
810 * the network adapter.
812 * 2) If the network adapter has a limit below the current
815 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
816 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
817 pmax->tsomaxbytes = ifp->if_hw_tsomax;
819 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
820 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
821 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
823 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
824 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
825 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
830 * Update TSO limit of a network adapter.
832 * Returns zero if no change. Else non-zero.
835 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
838 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
839 ifp->if_hw_tsomax = pmax->tsomaxbytes;
842 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
843 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
846 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
847 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
854 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
856 unsigned socksize, ifasize;
857 int namelen, masklen;
858 struct sockaddr_dl *sdl;
861 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
862 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
866 ifp->if_vnet = curvnet;
867 if (ifp->if_home_vnet == NULL)
868 ifp->if_home_vnet = curvnet;
871 if_addgroup(ifp, IFG_ALL);
873 /* Restore group membership for cloned interfaces. */
874 if (vmove && ifc != NULL)
875 if_clone_addgroup(ifp, ifc);
877 getmicrotime(&ifp->if_lastchange);
878 ifp->if_epoch = time_uptime;
880 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
881 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
882 ("transmit and qflush must both either be set or both be NULL"));
883 if (ifp->if_transmit == NULL) {
884 ifp->if_transmit = if_transmit;
885 ifp->if_qflush = if_qflush;
887 if (ifp->if_input == NULL)
888 ifp->if_input = if_input_default;
890 if (ifp->if_requestencap == NULL)
891 ifp->if_requestencap = if_requestencap_default;
895 mac_ifnet_create(ifp);
899 * Create a Link Level name for this device.
901 namelen = strlen(ifp->if_xname);
903 * Always save enough space for any possiable name so we
904 * can do a rename in place later.
906 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
907 socksize = masklen + ifp->if_addrlen;
908 if (socksize < sizeof(*sdl))
909 socksize = sizeof(*sdl);
910 socksize = roundup2(socksize, sizeof(long));
911 ifasize = sizeof(*ifa) + 2 * socksize;
912 ifa = ifa_alloc(ifasize, M_WAITOK);
913 sdl = (struct sockaddr_dl *)(ifa + 1);
914 sdl->sdl_len = socksize;
915 sdl->sdl_family = AF_LINK;
916 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
917 sdl->sdl_nlen = namelen;
918 sdl->sdl_index = ifp->if_index;
919 sdl->sdl_type = ifp->if_type;
922 ifa->ifa_addr = (struct sockaddr *)sdl;
923 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
924 ifa->ifa_netmask = (struct sockaddr *)sdl;
925 sdl->sdl_len = masklen;
927 sdl->sdl_data[--namelen] = 0xff;
928 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
929 /* Reliably crash if used uninitialized. */
930 ifp->if_broadcastaddr = NULL;
932 if (ifp->if_type == IFT_ETHER) {
933 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
937 #if defined(INET) || defined(INET6)
938 /* Use defaults for TSO, if nothing is set */
939 if (ifp->if_hw_tsomax == 0 &&
940 ifp->if_hw_tsomaxsegcount == 0 &&
941 ifp->if_hw_tsomaxsegsize == 0) {
943 * The TSO defaults needs to be such that an
944 * NFS mbuf list of 35 mbufs totalling just
945 * below 64K works and that a chain of mbufs
946 * can be defragged into at most 32 segments:
948 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
949 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
950 ifp->if_hw_tsomaxsegcount = 35;
951 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
953 /* XXX some drivers set IFCAP_TSO after ethernet attach */
954 if (ifp->if_capabilities & IFCAP_TSO) {
955 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
957 ifp->if_hw_tsomaxsegcount,
958 ifp->if_hw_tsomaxsegsize);
966 * Update the interface index in the link layer address
969 for (ifa = ifp->if_addr; ifa != NULL;
970 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
971 if (ifa->ifa_addr->sa_family == AF_LINK) {
972 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
973 sdl->sdl_index = ifp->if_index;
981 if (domain_init_status >= 2)
982 if_attachdomain1(ifp);
984 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
985 if (IS_DEFAULT_VNET(curvnet))
986 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
988 /* Announce the interface. */
989 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
993 if_epochalloc(void *dummy __unused)
996 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
998 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
1001 if_attachdomain(void *dummy)
1005 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
1006 if_attachdomain1(ifp);
1008 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
1009 if_attachdomain, NULL);
1012 if_attachdomain1(struct ifnet *ifp)
1017 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1018 * cannot lock ifp->if_afdata initialization, entirely.
1020 IF_AFDATA_LOCK(ifp);
1021 if (ifp->if_afdata_initialized >= domain_init_status) {
1022 IF_AFDATA_UNLOCK(ifp);
1023 log(LOG_WARNING, "%s called more than once on %s\n",
1024 __func__, ifp->if_xname);
1027 ifp->if_afdata_initialized = domain_init_status;
1028 IF_AFDATA_UNLOCK(ifp);
1030 /* address family dependent data region */
1031 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1032 for (dp = domains; dp; dp = dp->dom_next) {
1033 if (dp->dom_ifattach)
1034 ifp->if_afdata[dp->dom_family] =
1035 (*dp->dom_ifattach)(ifp);
1040 * Remove any unicast or broadcast network addresses from an interface.
1043 if_purgeaddrs(struct ifnet *ifp)
1048 struct epoch_tracker et;
1050 NET_EPOCH_ENTER(et);
1051 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1052 if (ifa->ifa_addr->sa_family != AF_LINK)
1060 /* XXX: Ugly!! ad hoc just for INET */
1061 if (ifa->ifa_addr->sa_family == AF_INET) {
1062 struct ifaliasreq ifr;
1064 bzero(&ifr, sizeof(ifr));
1065 ifr.ifra_addr = *ifa->ifa_addr;
1066 if (ifa->ifa_dstaddr)
1067 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1068 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1074 if (ifa->ifa_addr->sa_family == AF_INET6) {
1076 /* ifp_addrhead is already updated */
1081 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1082 IF_ADDR_WUNLOCK(ifp);
1088 * Remove any multicast network addresses from an interface when an ifnet
1092 if_purgemaddrs(struct ifnet *ifp)
1094 struct ifmultiaddr *ifma;
1097 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1098 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1099 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1100 if_delmulti_locked(ifp, ifma, 1);
1102 IF_ADDR_WUNLOCK(ifp);
1106 * Detach an interface, removing it from the list of "active" interfaces.
1107 * If vmove flag is set on entry to if_detach_internal(), perform only a
1108 * limited subset of cleanup tasks, given that we are moving an ifnet from
1109 * one vnet to another, where it must be fully operational.
1111 * XXXRW: There are some significant questions about event ordering, and
1112 * how to prevent things from starting to use the interface during detach.
1115 if_detach(struct ifnet *ifp)
1119 CURVNET_SET_QUIET(ifp->if_vnet);
1120 found = if_unlink_ifnet(ifp, false);
1122 if_detach_internal(ifp, 0, NULL);
1127 * The vmove flag, if set, indicates that we are called from a callpath
1128 * that is moving an interface to a different vnet instance.
1130 * The shutdown flag, if set, indicates that we are called in the
1131 * process of shutting down a vnet instance. Currently only the
1132 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1133 * on a vnet instance shutdown without this flag being set, e.g., when
1134 * the cloned interfaces are destoyed as first thing of teardown.
1137 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1145 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1149 * At this point we know the interface still was on the ifnet list
1150 * and we removed it so we are in a stable state.
1152 epoch_wait_preempt(net_epoch_preempt);
1155 * Ensure all pending EPOCH(9) callbacks have been executed. This
1156 * fixes issues about late destruction of multicast options
1157 * which lead to leave group calls, which in turn access the
1158 * belonging ifnet structure:
1160 epoch_drain_callbacks(net_epoch_preempt);
1163 * In any case (destroy or vmove) detach us from the groups
1164 * and remove/wait for pending events on the taskq.
1165 * XXX-BZ in theory an interface could still enqueue a taskq change?
1169 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1170 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1173 * Check if this is a cloned interface or not. Must do even if
1174 * shutting down as a if_vmove_reclaim() would move the ifp and
1175 * the if_clone_addgroup() will have a corrupted string overwise
1176 * from a gibberish pointer.
1178 if (vmove && ifcp != NULL)
1179 *ifcp = if_clone_findifc(ifp);
1185 * On VNET shutdown abort here as the stack teardown will do all
1186 * the work top-down for us.
1189 /* Give interface users the chance to clean up. */
1190 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1193 * In case of a vmove we are done here without error.
1194 * If we would signal an error it would lead to the same
1195 * abort as if we did not find the ifnet anymore.
1196 * if_detach() calls us in void context and does not care
1197 * about an early abort notification, so life is splendid :)
1199 goto finish_vnet_shutdown;
1204 * At this point we are not tearing down a VNET and are either
1205 * going to destroy or vmove the interface and have to cleanup
1210 * Remove routes and flush queues.
1213 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1214 altq_disable(&ifp->if_snd);
1215 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1216 altq_detach(&ifp->if_snd);
1227 * Remove all IPv6 kernel structs related to ifp. This should be done
1228 * before removing routing entries below, since IPv6 interface direct
1229 * routes are expected to be removed by the IPv6-specific kernel API.
1230 * Otherwise, the kernel will detect some inconsistency and bark it.
1234 if_purgemaddrs(ifp);
1236 /* Announce that the interface is gone. */
1237 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1238 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1239 if (IS_DEFAULT_VNET(curvnet))
1240 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1244 * Prevent further calls into the device driver via ifnet.
1249 * Clean up all addresses.
1252 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1253 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1254 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1255 IF_ADDR_WUNLOCK(ifp);
1258 IF_ADDR_WUNLOCK(ifp);
1261 rt_flushifroutes(ifp);
1264 finish_vnet_shutdown:
1267 * We cannot hold the lock over dom_ifdetach calls as they might
1268 * sleep, for example trying to drain a callout, thus open up the
1269 * theoretical race with re-attaching.
1271 IF_AFDATA_LOCK(ifp);
1272 i = ifp->if_afdata_initialized;
1273 ifp->if_afdata_initialized = 0;
1274 IF_AFDATA_UNLOCK(ifp);
1275 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1276 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1277 (*dp->dom_ifdetach)(ifp,
1278 ifp->if_afdata[dp->dom_family]);
1279 ifp->if_afdata[dp->dom_family] = NULL;
1288 * if_vmove() performs a limited version of if_detach() in current
1289 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1290 * An attempt is made to shrink if_index in current vnet, find an
1291 * unused if_index in target vnet and calls if_grow() if necessary,
1292 * and finally find an unused if_xname for the target vnet.
1295 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1297 struct if_clone *ifc;
1299 u_int bif_dlt, bif_hdrlen;
1306 * if_detach_internal() will call the eventhandler to notify
1307 * interface departure. That will detach if_bpf. We need to
1308 * safe the dlt and hdrlen so we can re-attach it later.
1310 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1314 * Detach from current vnet, but preserve LLADDR info, do not
1315 * mark as dead etc. so that the ifnet can be reattached later.
1316 * If we cannot find it, we lost the race to someone else.
1318 rc = if_detach_internal(ifp, 1, &ifc);
1323 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1324 * the if_index for that vnet if possible.
1326 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1327 * or we'd lock on one vnet and unlock on another.
1330 ifindex_free_locked(ifp->if_index);
1334 /* Don't re-attach DYING interfaces. */
1335 if (ifp->if_flags & IFF_DYING)
1339 * Perform interface-specific reassignment tasks, if provided by
1342 if (ifp->if_reassign != NULL)
1343 ifp->if_reassign(ifp, new_vnet, NULL);
1346 * Switch to the context of the target vnet.
1348 CURVNET_SET_QUIET(new_vnet);
1351 ifp->if_index = ifindex_alloc(&old);
1352 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1354 epoch_wait_preempt(net_epoch_preempt);
1358 ifnet_setbyindex(ifp->if_index, ifp);
1361 if_attach_internal(ifp, 1, ifc);
1364 if (ifp->if_bpf == NULL)
1365 bpfattach(ifp, bif_dlt, bif_hdrlen);
1373 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1376 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1384 /* Try to find the prison within our visibility. */
1385 sx_slock(&allprison_lock);
1386 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1387 sx_sunlock(&allprison_lock);
1390 prison_hold_locked(pr);
1391 mtx_unlock(&pr->pr_mtx);
1393 /* Do not try to move the iface from and to the same prison. */
1394 if (pr->pr_vnet == ifp->if_vnet) {
1399 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1400 /* XXX Lock interfaces to avoid races. */
1401 CURVNET_SET_QUIET(pr->pr_vnet);
1402 difp = ifunit(ifname);
1409 /* Make sure the VNET is stable. */
1410 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1418 found = if_unlink_ifnet(ifp, true);
1421 /* Move the interface into the child jail/vnet. */
1422 error = if_vmove(ifp, pr->pr_vnet);
1424 /* Report the new if_xname back to the userland on success. */
1426 sprintf(ifname, "%s", ifp->if_xname);
1433 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1436 struct vnet *vnet_dst;
1441 /* Try to find the prison within our visibility. */
1442 sx_slock(&allprison_lock);
1443 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1444 sx_sunlock(&allprison_lock);
1447 prison_hold_locked(pr);
1448 mtx_unlock(&pr->pr_mtx);
1450 /* Make sure the named iface exists in the source prison/vnet. */
1451 CURVNET_SET(pr->pr_vnet);
1452 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1459 /* Do not try to move the iface from and to the same prison. */
1460 vnet_dst = TD_TO_VNET(td);
1461 if (vnet_dst == ifp->if_vnet) {
1467 /* Make sure the VNET is stable. */
1468 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1475 /* Get interface back from child jail/vnet. */
1476 found = if_unlink_ifnet(ifp, true);
1478 error = if_vmove(ifp, vnet_dst);
1481 /* Report the new if_xname back to the userland on success. */
1483 sprintf(ifname, "%s", ifp->if_xname);
1491 * Add a group to an interface
1494 if_addgroup(struct ifnet *ifp, const char *groupname)
1496 struct ifg_list *ifgl;
1497 struct ifg_group *ifg = NULL;
1498 struct ifg_member *ifgm;
1501 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1502 groupname[strlen(groupname) - 1] <= '9')
1506 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1507 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1512 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1517 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1523 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1524 if (!strcmp(ifg->ifg_group, groupname))
1528 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1534 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1535 ifg->ifg_refcnt = 0;
1536 CK_STAILQ_INIT(&ifg->ifg_members);
1537 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1542 ifgl->ifgl_group = ifg;
1543 ifgm->ifgm_ifp = ifp;
1546 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1547 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1548 IF_ADDR_WUNLOCK(ifp);
1553 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1554 EVENTHANDLER_INVOKE(group_change_event, groupname);
1560 * Helper function to remove a group out of an interface. Expects the global
1561 * ifnet lock to be write-locked, and drops it before returning.
1564 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1565 const char *groupname)
1567 struct ifg_member *ifgm;
1570 IFNET_WLOCK_ASSERT();
1573 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1574 IF_ADDR_WUNLOCK(ifp);
1576 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1577 if (ifgm->ifgm_ifp == ifp) {
1578 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1579 ifg_member, ifgm_next);
1584 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1585 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1593 epoch_wait_preempt(net_epoch_preempt);
1595 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1596 free(ifgl->ifgl_group, M_TEMP);
1601 EVENTHANDLER_INVOKE(group_change_event, groupname);
1605 * Remove a group from an interface
1608 if_delgroup(struct ifnet *ifp, const char *groupname)
1610 struct ifg_list *ifgl;
1613 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1614 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1621 _if_delgroup_locked(ifp, ifgl, groupname);
1627 * Remove an interface from all groups
1630 if_delgroups(struct ifnet *ifp)
1632 struct ifg_list *ifgl;
1633 char groupname[IFNAMSIZ];
1636 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1637 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1638 _if_delgroup_locked(ifp, ifgl, groupname);
1645 ifgr_group_get(void *ifgrp)
1647 union ifgroupreq_union *ifgrup;
1650 #ifdef COMPAT_FREEBSD32
1651 if (SV_CURPROC_FLAG(SV_ILP32))
1652 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1654 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1657 static struct ifg_req *
1658 ifgr_groups_get(void *ifgrp)
1660 union ifgroupreq_union *ifgrup;
1663 #ifdef COMPAT_FREEBSD32
1664 if (SV_CURPROC_FLAG(SV_ILP32))
1665 return ((struct ifg_req *)(uintptr_t)
1666 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1668 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1672 * Stores all groups from an interface in memory pointed to by ifgr.
1675 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1678 struct ifg_list *ifgl;
1679 struct ifg_req ifgrq, *ifgp;
1683 if (ifgr->ifgr_len == 0) {
1684 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1685 ifgr->ifgr_len += sizeof(struct ifg_req);
1689 len = ifgr->ifgr_len;
1690 ifgp = ifgr_groups_get(ifgr);
1692 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1693 if (len < sizeof(ifgrq))
1695 bzero(&ifgrq, sizeof ifgrq);
1696 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1697 sizeof(ifgrq.ifgrq_group));
1698 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1700 len -= sizeof(ifgrq);
1708 * Stores all members of a group in memory pointed to by igfr
1711 if_getgroupmembers(struct ifgroupreq *ifgr)
1713 struct ifg_group *ifg;
1714 struct ifg_member *ifgm;
1715 struct ifg_req ifgrq, *ifgp;
1719 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1720 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1727 if (ifgr->ifgr_len == 0) {
1728 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1729 ifgr->ifgr_len += sizeof(ifgrq);
1734 len = ifgr->ifgr_len;
1735 ifgp = ifgr_groups_get(ifgr);
1736 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1737 if (len < sizeof(ifgrq)) {
1741 bzero(&ifgrq, sizeof ifgrq);
1742 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1743 sizeof(ifgrq.ifgrq_member));
1744 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1748 len -= sizeof(ifgrq);
1757 * Return counter values from counter(9)s stored in ifnet.
1760 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1763 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1765 return (counter_u64_fetch(ifp->if_counters[cnt]));
1769 * Increase an ifnet counter. Usually used for counters shared
1770 * between the stack and a driver, but function supports them all.
1773 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1776 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1778 counter_u64_add(ifp->if_counters[cnt], inc);
1782 * Copy data from ifnet to userland API structure if_data.
1785 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1788 ifd->ifi_type = ifp->if_type;
1789 ifd->ifi_physical = 0;
1790 ifd->ifi_addrlen = ifp->if_addrlen;
1791 ifd->ifi_hdrlen = ifp->if_hdrlen;
1792 ifd->ifi_link_state = ifp->if_link_state;
1794 ifd->ifi_datalen = sizeof(struct if_data);
1795 ifd->ifi_mtu = ifp->if_mtu;
1796 ifd->ifi_metric = ifp->if_metric;
1797 ifd->ifi_baudrate = ifp->if_baudrate;
1798 ifd->ifi_hwassist = ifp->if_hwassist;
1799 ifd->ifi_epoch = ifp->if_epoch;
1800 ifd->ifi_lastchange = ifp->if_lastchange;
1802 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1803 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1804 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1805 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1806 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1807 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1808 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1809 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1810 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1811 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1812 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1813 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1817 * Initialization, destruction and refcounting functions for ifaddrs.
1820 ifa_alloc(size_t size, int flags)
1824 KASSERT(size >= sizeof(struct ifaddr),
1825 ("%s: invalid size %zu", __func__, size));
1827 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1831 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1833 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1835 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1837 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1840 refcount_init(&ifa->ifa_refcnt, 1);
1845 /* free(NULL) is okay */
1846 counter_u64_free(ifa->ifa_opackets);
1847 counter_u64_free(ifa->ifa_ipackets);
1848 counter_u64_free(ifa->ifa_obytes);
1849 counter_u64_free(ifa->ifa_ibytes);
1850 free(ifa, M_IFADDR);
1856 ifa_ref(struct ifaddr *ifa)
1859 refcount_acquire(&ifa->ifa_refcnt);
1863 ifa_destroy(epoch_context_t ctx)
1867 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1868 counter_u64_free(ifa->ifa_opackets);
1869 counter_u64_free(ifa->ifa_ipackets);
1870 counter_u64_free(ifa->ifa_obytes);
1871 counter_u64_free(ifa->ifa_ibytes);
1872 free(ifa, M_IFADDR);
1876 ifa_free(struct ifaddr *ifa)
1879 if (refcount_release(&ifa->ifa_refcnt))
1880 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1884 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1885 * structs used to represent other address families, it is necessary
1886 * to perform a different comparison.
1889 #define sa_dl_equal(a1, a2) \
1890 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1891 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1892 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1893 CLLADDR((const struct sockaddr_dl *)(a2)), \
1894 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1897 * Locate an interface based on a complete address.
1901 ifa_ifwithaddr(const struct sockaddr *addr)
1908 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1909 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1910 if (ifa->ifa_addr->sa_family != addr->sa_family)
1912 if (sa_equal(addr, ifa->ifa_addr)) {
1915 /* IP6 doesn't have broadcast */
1916 if ((ifp->if_flags & IFF_BROADCAST) &&
1917 ifa->ifa_broadaddr &&
1918 ifa->ifa_broadaddr->sa_len != 0 &&
1919 sa_equal(ifa->ifa_broadaddr, addr)) {
1930 ifa_ifwithaddr_check(const struct sockaddr *addr)
1932 struct epoch_tracker et;
1935 NET_EPOCH_ENTER(et);
1936 rc = (ifa_ifwithaddr(addr) != NULL);
1942 * Locate an interface based on the broadcast address.
1946 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1952 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1953 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1955 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1956 if (ifa->ifa_addr->sa_family != addr->sa_family)
1958 if ((ifp->if_flags & IFF_BROADCAST) &&
1959 ifa->ifa_broadaddr &&
1960 ifa->ifa_broadaddr->sa_len != 0 &&
1961 sa_equal(ifa->ifa_broadaddr, addr)) {
1972 * Locate the point to point interface with a given destination address.
1976 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1982 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1983 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1985 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1987 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1988 if (ifa->ifa_addr->sa_family != addr->sa_family)
1990 if (ifa->ifa_dstaddr != NULL &&
1991 sa_equal(addr, ifa->ifa_dstaddr)) {
2002 * Find an interface on a specific network. If many, choice
2003 * is most specific found.
2006 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2010 struct ifaddr *ifa_maybe = NULL;
2011 u_int af = addr->sa_family;
2012 const char *addr_data = addr->sa_data, *cplim;
2016 * AF_LINK addresses can be looked up directly by their index number,
2017 * so do that if we can.
2019 if (af == AF_LINK) {
2020 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2021 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2022 return (ifaddr_byindex(sdl->sdl_index));
2026 * Scan though each interface, looking for ones that have addresses
2027 * in this address family and the requested fib.
2029 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2030 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2032 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2033 const char *cp, *cp2, *cp3;
2035 if (ifa->ifa_addr->sa_family != af)
2037 if (af == AF_INET &&
2038 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2040 * This is a bit broken as it doesn't
2041 * take into account that the remote end may
2042 * be a single node in the network we are
2044 * The trouble is that we don't know the
2045 * netmask for the remote end.
2047 if (ifa->ifa_dstaddr != NULL &&
2048 sa_equal(addr, ifa->ifa_dstaddr)) {
2053 * Scan all the bits in the ifa's address.
2054 * If a bit dissagrees with what we are
2055 * looking for, mask it with the netmask
2056 * to see if it really matters.
2057 * (A byte at a time)
2059 if (ifa->ifa_netmask == 0)
2062 cp2 = ifa->ifa_addr->sa_data;
2063 cp3 = ifa->ifa_netmask->sa_data;
2064 cplim = ifa->ifa_netmask->sa_len
2065 + (char *)ifa->ifa_netmask;
2067 if ((*cp++ ^ *cp2++) & *cp3++)
2068 goto next; /* next address! */
2070 * If the netmask of what we just found
2071 * is more specific than what we had before
2072 * (if we had one), or if the virtual status
2073 * of new prefix is better than of the old one,
2074 * then remember the new one before continuing
2075 * to search for an even better one.
2077 if (ifa_maybe == NULL ||
2078 ifa_preferred(ifa_maybe, ifa) ||
2079 rn_refines((caddr_t)ifa->ifa_netmask,
2080 (caddr_t)ifa_maybe->ifa_netmask)) {
2093 * Find an interface address specific to an interface best matching
2097 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2100 const char *cp, *cp2, *cp3;
2102 struct ifaddr *ifa_maybe = NULL;
2103 u_int af = addr->sa_family;
2109 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2110 if (ifa->ifa_addr->sa_family != af)
2112 if (ifa_maybe == NULL)
2114 if (ifa->ifa_netmask == 0) {
2115 if (sa_equal(addr, ifa->ifa_addr) ||
2116 (ifa->ifa_dstaddr &&
2117 sa_equal(addr, ifa->ifa_dstaddr)))
2121 if (ifp->if_flags & IFF_POINTOPOINT) {
2122 if (sa_equal(addr, ifa->ifa_dstaddr))
2126 cp2 = ifa->ifa_addr->sa_data;
2127 cp3 = ifa->ifa_netmask->sa_data;
2128 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2129 for (; cp3 < cplim; cp3++)
2130 if ((*cp++ ^ *cp2++) & *cp3)
2142 * See whether new ifa is better than current one:
2143 * 1) A non-virtual one is preferred over virtual.
2144 * 2) A virtual in master state preferred over any other state.
2146 * Used in several address selecting functions.
2149 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2152 return (cur->ifa_carp && (!next->ifa_carp ||
2153 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2156 struct sockaddr_dl *
2157 link_alloc_sdl(size_t size, int flags)
2160 return (malloc(size, M_TEMP, flags));
2164 link_free_sdl(struct sockaddr *sa)
2170 * Fills in given sdl with interface basic info.
2171 * Returns pointer to filled sdl.
2173 struct sockaddr_dl *
2174 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2176 struct sockaddr_dl *sdl;
2178 sdl = (struct sockaddr_dl *)paddr;
2179 memset(sdl, 0, sizeof(struct sockaddr_dl));
2180 sdl->sdl_len = sizeof(struct sockaddr_dl);
2181 sdl->sdl_family = AF_LINK;
2182 sdl->sdl_index = ifp->if_index;
2183 sdl->sdl_type = iftype;
2189 * Mark an interface down and notify protocols of
2193 if_unroute(struct ifnet *ifp, int flag, int fam)
2197 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2199 ifp->if_flags &= ~flag;
2200 getmicrotime(&ifp->if_lastchange);
2201 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2202 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2203 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2204 ifp->if_qflush(ifp);
2207 (*carp_linkstate_p)(ifp);
2212 * Mark an interface up and notify protocols of
2216 if_route(struct ifnet *ifp, int flag, int fam)
2220 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2222 ifp->if_flags |= flag;
2223 getmicrotime(&ifp->if_lastchange);
2224 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2225 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2226 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2228 (*carp_linkstate_p)(ifp);
2235 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2236 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2237 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2238 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2239 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2240 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2241 int (*vlan_setcookie_p)(struct ifnet *, void *);
2242 void *(*vlan_cookie_p)(struct ifnet *);
2245 * Handle a change in the interface link state. To avoid LORs
2246 * between driver lock and upper layer locks, as well as possible
2247 * recursions, we post event to taskqueue, and all job
2248 * is done in static do_link_state_change().
2251 if_link_state_change(struct ifnet *ifp, int link_state)
2253 /* Return if state hasn't changed. */
2254 if (ifp->if_link_state == link_state)
2257 ifp->if_link_state = link_state;
2259 /* XXXGL: reference ifp? */
2260 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2264 do_link_state_change(void *arg, int pending)
2270 link_state = ifp->if_link_state;
2272 CURVNET_SET(ifp->if_vnet);
2274 if (ifp->if_vlantrunk != NULL)
2275 (*vlan_link_state_p)(ifp);
2277 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2278 ifp->if_l2com != NULL)
2279 (*ng_ether_link_state_p)(ifp, link_state);
2281 (*carp_linkstate_p)(ifp);
2283 ifp->if_bridge_linkstate(ifp);
2285 (*lagg_linkstate_p)(ifp, link_state);
2287 if (IS_DEFAULT_VNET(curvnet))
2288 devctl_notify("IFNET", ifp->if_xname,
2289 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2292 if_printf(ifp, "%d link states coalesced\n", pending);
2293 if (log_link_state_change)
2294 if_printf(ifp, "link state changed to %s\n",
2295 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2296 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2301 * Mark an interface down and notify protocols of
2305 if_down(struct ifnet *ifp)
2308 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2309 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2313 * Mark an interface up and notify protocols of
2317 if_up(struct ifnet *ifp)
2320 if_route(ifp, IFF_UP, AF_UNSPEC);
2321 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2325 * Flush an interface queue.
2328 if_qflush(struct ifnet *ifp)
2336 if (ALTQ_IS_ENABLED(ifq))
2340 while ((m = n) != NULL) {
2351 * Map interface name to interface structure pointer, with or without
2352 * returning a reference.
2355 ifunit_ref(const char *name)
2357 struct epoch_tracker et;
2360 NET_EPOCH_ENTER(et);
2361 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2362 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2363 !(ifp->if_flags & IFF_DYING))
2373 ifunit(const char *name)
2375 struct epoch_tracker et;
2378 NET_EPOCH_ENTER(et);
2379 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2380 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2388 ifr_buffer_get_buffer(void *data)
2390 union ifreq_union *ifrup;
2393 #ifdef COMPAT_FREEBSD32
2394 if (SV_CURPROC_FLAG(SV_ILP32))
2395 return ((void *)(uintptr_t)
2396 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2398 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2402 ifr_buffer_set_buffer_null(void *data)
2404 union ifreq_union *ifrup;
2407 #ifdef COMPAT_FREEBSD32
2408 if (SV_CURPROC_FLAG(SV_ILP32))
2409 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2412 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2416 ifr_buffer_get_length(void *data)
2418 union ifreq_union *ifrup;
2421 #ifdef COMPAT_FREEBSD32
2422 if (SV_CURPROC_FLAG(SV_ILP32))
2423 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2425 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2429 ifr_buffer_set_length(void *data, size_t len)
2431 union ifreq_union *ifrup;
2434 #ifdef COMPAT_FREEBSD32
2435 if (SV_CURPROC_FLAG(SV_ILP32))
2436 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2439 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2443 ifr_data_get_ptr(void *ifrp)
2445 union ifreq_union *ifrup;
2448 #ifdef COMPAT_FREEBSD32
2449 if (SV_CURPROC_FLAG(SV_ILP32))
2450 return ((void *)(uintptr_t)
2451 ifrup->ifr32.ifr_ifru.ifru_data);
2453 return (ifrup->ifr.ifr_ifru.ifru_data);
2457 * Hardware specific interface ioctls.
2460 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2463 int error = 0, do_ifup = 0;
2464 int new_flags, temp_flags;
2465 size_t namelen, onamelen;
2467 char *descrbuf, *odescrbuf;
2468 char new_name[IFNAMSIZ];
2470 struct sockaddr_dl *sdl;
2472 ifr = (struct ifreq *)data;
2475 ifr->ifr_index = ifp->if_index;
2479 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2480 ifr->ifr_flags = temp_flags & 0xffff;
2481 ifr->ifr_flagshigh = temp_flags >> 16;
2485 ifr->ifr_reqcap = ifp->if_capabilities;
2486 ifr->ifr_curcap = ifp->if_capenable;
2493 /* Ensure uninitialised padding is not leaked. */
2494 memset(&ifd, 0, sizeof(ifd));
2496 if_data_copy(ifp, &ifd);
2497 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2503 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2508 ifr->ifr_metric = ifp->if_metric;
2512 ifr->ifr_mtu = ifp->if_mtu;
2516 /* XXXGL: did this ever worked? */
2522 sx_slock(&ifdescr_sx);
2523 if (ifp->if_description == NULL)
2526 /* space for terminating nul */
2527 descrlen = strlen(ifp->if_description) + 1;
2528 if (ifr_buffer_get_length(ifr) < descrlen)
2529 ifr_buffer_set_buffer_null(ifr);
2531 error = copyout(ifp->if_description,
2532 ifr_buffer_get_buffer(ifr), descrlen);
2533 ifr_buffer_set_length(ifr, descrlen);
2535 sx_sunlock(&ifdescr_sx);
2539 error = priv_check(td, PRIV_NET_SETIFDESCR);
2544 * Copy only (length-1) bytes to make sure that
2545 * if_description is always nul terminated. The
2546 * length parameter is supposed to count the
2547 * terminating nul in.
2549 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2550 return (ENAMETOOLONG);
2551 else if (ifr_buffer_get_length(ifr) == 0)
2554 descrbuf = malloc(ifr_buffer_get_length(ifr),
2555 M_IFDESCR, M_WAITOK | M_ZERO);
2556 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2557 ifr_buffer_get_length(ifr) - 1);
2559 free(descrbuf, M_IFDESCR);
2564 sx_xlock(&ifdescr_sx);
2565 odescrbuf = ifp->if_description;
2566 ifp->if_description = descrbuf;
2567 sx_xunlock(&ifdescr_sx);
2569 getmicrotime(&ifp->if_lastchange);
2570 free(odescrbuf, M_IFDESCR);
2574 ifr->ifr_fib = ifp->if_fib;
2578 error = priv_check(td, PRIV_NET_SETIFFIB);
2581 if (ifr->ifr_fib >= rt_numfibs)
2584 ifp->if_fib = ifr->ifr_fib;
2588 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2592 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2593 * check, so we don't need special handling here yet.
2595 new_flags = (ifr->ifr_flags & 0xffff) |
2596 (ifr->ifr_flagshigh << 16);
2597 if (ifp->if_flags & IFF_UP &&
2598 (new_flags & IFF_UP) == 0) {
2600 } else if (new_flags & IFF_UP &&
2601 (ifp->if_flags & IFF_UP) == 0) {
2604 /* See if permanently promiscuous mode bit is about to flip */
2605 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2606 if (new_flags & IFF_PPROMISC)
2607 ifp->if_flags |= IFF_PROMISC;
2608 else if (ifp->if_pcount == 0)
2609 ifp->if_flags &= ~IFF_PROMISC;
2610 if (log_promisc_mode_change)
2611 if_printf(ifp, "permanently promiscuous mode %s\n",
2612 ((new_flags & IFF_PPROMISC) ?
2613 "enabled" : "disabled"));
2615 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2616 (new_flags &~ IFF_CANTCHANGE);
2617 if (ifp->if_ioctl) {
2618 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2622 getmicrotime(&ifp->if_lastchange);
2626 error = priv_check(td, PRIV_NET_SETIFCAP);
2629 if (ifp->if_ioctl == NULL)
2630 return (EOPNOTSUPP);
2631 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2633 error = (*ifp->if_ioctl)(ifp, cmd, data);
2635 getmicrotime(&ifp->if_lastchange);
2640 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2645 error = priv_check(td, PRIV_NET_SETIFNAME);
2648 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2652 if (new_name[0] == '\0')
2654 if (new_name[IFNAMSIZ-1] != '\0') {
2655 new_name[IFNAMSIZ-1] = '\0';
2656 if (strlen(new_name) == IFNAMSIZ-1)
2659 if (strcmp(new_name, ifp->if_xname) == 0)
2661 if (ifunit(new_name) != NULL)
2665 * XXX: Locking. Nothing else seems to lock if_flags,
2666 * and there are numerous other races with the
2667 * ifunit() checks not being atomic with namespace
2668 * changes (renames, vmoves, if_attach, etc).
2670 ifp->if_flags |= IFF_RENAMING;
2672 /* Announce the departure of the interface. */
2673 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2674 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2676 if_printf(ifp, "changing name to '%s'\n", new_name);
2679 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2681 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2682 namelen = strlen(new_name);
2683 onamelen = sdl->sdl_nlen;
2685 * Move the address if needed. This is safe because we
2686 * allocate space for a name of length IFNAMSIZ when we
2687 * create this in if_attach().
2689 if (namelen != onamelen) {
2690 bcopy(sdl->sdl_data + onamelen,
2691 sdl->sdl_data + namelen, sdl->sdl_alen);
2693 bcopy(new_name, sdl->sdl_data, namelen);
2694 sdl->sdl_nlen = namelen;
2695 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2696 bzero(sdl->sdl_data, onamelen);
2697 while (namelen != 0)
2698 sdl->sdl_data[--namelen] = 0xff;
2699 IF_ADDR_WUNLOCK(ifp);
2701 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2702 /* Announce the return of the interface. */
2703 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2705 ifp->if_flags &= ~IFF_RENAMING;
2710 error = priv_check(td, PRIV_NET_SETIFVNET);
2713 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2718 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2721 ifp->if_metric = ifr->ifr_metric;
2722 getmicrotime(&ifp->if_lastchange);
2726 error = priv_check(td, PRIV_NET_SETIFPHYS);
2729 if (ifp->if_ioctl == NULL)
2730 return (EOPNOTSUPP);
2731 error = (*ifp->if_ioctl)(ifp, cmd, data);
2733 getmicrotime(&ifp->if_lastchange);
2738 u_long oldmtu = ifp->if_mtu;
2740 error = priv_check(td, PRIV_NET_SETIFMTU);
2743 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2745 if (ifp->if_ioctl == NULL)
2746 return (EOPNOTSUPP);
2747 error = (*ifp->if_ioctl)(ifp, cmd, data);
2749 getmicrotime(&ifp->if_lastchange);
2752 DEBUGNET_NOTIFY_MTU(ifp);
2756 * If the link MTU changed, do network layer specific procedure.
2758 if (ifp->if_mtu != oldmtu) {
2769 if (cmd == SIOCADDMULTI)
2770 error = priv_check(td, PRIV_NET_ADDMULTI);
2772 error = priv_check(td, PRIV_NET_DELMULTI);
2776 /* Don't allow group membership on non-multicast interfaces. */
2777 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2778 return (EOPNOTSUPP);
2780 /* Don't let users screw up protocols' entries. */
2781 if (ifr->ifr_addr.sa_family != AF_LINK)
2784 if (cmd == SIOCADDMULTI) {
2785 struct epoch_tracker et;
2786 struct ifmultiaddr *ifma;
2789 * Userland is only permitted to join groups once
2790 * via the if_addmulti() KPI, because it cannot hold
2791 * struct ifmultiaddr * between calls. It may also
2792 * lose a race while we check if the membership
2795 NET_EPOCH_ENTER(et);
2796 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2801 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2803 error = if_delmulti(ifp, &ifr->ifr_addr);
2806 getmicrotime(&ifp->if_lastchange);
2809 case SIOCSIFPHYADDR:
2810 case SIOCDIFPHYADDR:
2812 case SIOCSIFPHYADDR_IN6:
2815 case SIOCSIFGENERIC:
2816 error = priv_check(td, PRIV_NET_HWIOCTL);
2819 if (ifp->if_ioctl == NULL)
2820 return (EOPNOTSUPP);
2821 error = (*ifp->if_ioctl)(ifp, cmd, data);
2823 getmicrotime(&ifp->if_lastchange);
2827 case SIOCGIFPSRCADDR:
2828 case SIOCGIFPDSTADDR:
2831 case SIOCGIFGENERIC:
2833 case SIOCGIFRSSHASH:
2834 case SIOCGIFDOWNREASON:
2835 if (ifp->if_ioctl == NULL)
2836 return (EOPNOTSUPP);
2837 error = (*ifp->if_ioctl)(ifp, cmd, data);
2841 error = priv_check(td, PRIV_NET_SETLLADDR);
2844 error = if_setlladdr(ifp,
2845 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2849 error = if_gethwaddr(ifp, ifr);
2852 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2853 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2856 if ((error = if_addgroup(ifp,
2857 ifgr_group_get((struct ifgroupreq *)data))))
2861 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2863 struct epoch_tracker et;
2865 NET_EPOCH_ENTER(et);
2866 error = if_getgroup((struct ifgroupreq *)data, ifp);
2871 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2872 error = priv_check(td, PRIV_NET_DELIFGROUP);
2875 if ((error = if_delgroup(ifp,
2876 ifgr_group_get((struct ifgroupreq *)data))))
2887 #ifdef COMPAT_FREEBSD32
2895 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2898 #ifdef COMPAT_FREEBSD32
2900 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2902 struct ifmediareq32 *ifmr32;
2904 ifmr32 = (struct ifmediareq32 *)data;
2905 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2906 sizeof(ifmr->ifm_name));
2907 ifmr->ifm_current = ifmr32->ifm_current;
2908 ifmr->ifm_mask = ifmr32->ifm_mask;
2909 ifmr->ifm_status = ifmr32->ifm_status;
2910 ifmr->ifm_active = ifmr32->ifm_active;
2911 ifmr->ifm_count = ifmr32->ifm_count;
2912 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2916 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2918 struct ifmediareq32 *ifmr32;
2920 ifmr32 = (struct ifmediareq32 *)data;
2921 ifmr32->ifm_current = ifmr->ifm_current;
2922 ifmr32->ifm_mask = ifmr->ifm_mask;
2923 ifmr32->ifm_status = ifmr->ifm_status;
2924 ifmr32->ifm_active = ifmr->ifm_active;
2925 ifmr32->ifm_count = ifmr->ifm_count;
2933 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2935 #ifdef COMPAT_FREEBSD32
2936 caddr_t saved_data = NULL;
2937 struct ifmediareq ifmr;
2938 struct ifmediareq *ifmrp = NULL;
2948 CURVNET_SET(so->so_vnet);
2950 /* Make sure the VNET is stable. */
2951 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2960 error = ifconf(cmd, data);
2963 #ifdef COMPAT_FREEBSD32
2966 struct ifconf32 *ifc32;
2969 ifc32 = (struct ifconf32 *)data;
2970 ifc.ifc_len = ifc32->ifc_len;
2971 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2973 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2975 ifc32->ifc_len = ifc.ifc_len;
2981 #ifdef COMPAT_FREEBSD32
2983 case SIOCGIFMEDIA32:
2984 case SIOCGIFXMEDIA32:
2986 ifmr_init(ifmrp, data);
2987 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2989 data = (caddr_t)ifmrp;
2993 ifr = (struct ifreq *)data;
2997 error = priv_check(td, PRIV_NET_SETIFVNET);
2999 error = if_vmove_reclaim(td, ifr->ifr_name,
3005 error = priv_check(td, PRIV_NET_IFCREATE);
3007 error = if_clone_create(ifr->ifr_name,
3008 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3009 ifr_data_get_ptr(ifr) : NULL);
3012 error = priv_check(td, PRIV_NET_IFDESTROY);
3014 error = if_clone_destroy(ifr->ifr_name);
3017 case SIOCIFGCLONERS:
3018 error = if_clone_list((struct if_clonereq *)data);
3021 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3022 error = if_getgroupmembers((struct ifgroupreq *)data);
3025 #if defined(INET) || defined(INET6)
3028 if (carp_ioctl_p == NULL)
3029 error = EPROTONOSUPPORT;
3031 error = (*carp_ioctl_p)(ifr, cmd, td);
3036 ifp = ifunit_ref(ifr->ifr_name);
3042 error = ifhwioctl(cmd, ifp, data, td);
3043 if (error != ENOIOCTL)
3046 oif_flags = ifp->if_flags;
3047 if (so->so_proto == NULL) {
3053 * Pass the request on to the socket control method, and if the
3054 * latter returns EOPNOTSUPP, directly to the interface.
3056 * Make an exception for the legacy SIOCSIF* requests. Drivers
3057 * trust SIOCSIFADDR et al to come from an already privileged
3058 * layer, and do not perform any credentials checks or input
3061 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3063 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3064 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3065 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3066 error = (*ifp->if_ioctl)(ifp, cmd, data);
3068 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3070 if (ifp->if_flags & IFF_UP)
3078 #ifdef COMPAT_FREEBSD32
3079 if (ifmrp != NULL) {
3080 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3081 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3084 ifmr_update(ifmrp, data);
3092 * The code common to handling reference counted flags,
3093 * e.g., in ifpromisc() and if_allmulti().
3094 * The "pflag" argument can specify a permanent mode flag to check,
3095 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3097 * Only to be used on stack-owned flags, not driver-owned flags.
3100 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3104 int oldflags, oldcount;
3106 /* Sanity checks to catch programming errors */
3107 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3108 ("%s: setting driver-owned flag %d", __func__, flag));
3111 KASSERT(*refcount >= 0,
3112 ("%s: increment negative refcount %d for flag %d",
3113 __func__, *refcount, flag));
3115 KASSERT(*refcount > 0,
3116 ("%s: decrement non-positive refcount %d for flag %d",
3117 __func__, *refcount, flag));
3119 /* In case this mode is permanent, just touch refcount */
3120 if (ifp->if_flags & pflag) {
3121 *refcount += onswitch ? 1 : -1;
3125 /* Save ifnet parameters for if_ioctl() may fail */
3126 oldcount = *refcount;
3127 oldflags = ifp->if_flags;
3130 * See if we aren't the only and touching refcount is enough.
3131 * Actually toggle interface flag if we are the first or last.
3136 ifp->if_flags |= flag;
3140 ifp->if_flags &= ~flag;
3143 /* Call down the driver since we've changed interface flags */
3144 if (ifp->if_ioctl == NULL) {
3148 ifr.ifr_flags = ifp->if_flags & 0xffff;
3149 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3150 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3153 /* Notify userland that interface flags have changed */
3158 /* Recover after driver error */
3159 *refcount = oldcount;
3160 ifp->if_flags = oldflags;
3165 * Set/clear promiscuous mode on interface ifp based on the truth value
3166 * of pswitch. The calls are reference counted so that only the first
3167 * "on" request actually has an effect, as does the final "off" request.
3168 * Results are undefined if the "off" and "on" requests are not matched.
3171 ifpromisc(struct ifnet *ifp, int pswitch)
3174 int oldflags = ifp->if_flags;
3176 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3177 &ifp->if_pcount, pswitch);
3178 /* If promiscuous mode status has changed, log a message */
3179 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3180 log_promisc_mode_change)
3181 if_printf(ifp, "promiscuous mode %s\n",
3182 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3187 * Return interface configuration
3188 * of system. List may be used
3189 * in later ioctl's (above) to get
3190 * other information.
3194 ifconf(u_long cmd, caddr_t data)
3196 struct ifconf *ifc = (struct ifconf *)data;
3201 int error, full = 0, valid_len, max_len;
3203 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3204 max_len = MAXPHYS - 1;
3206 /* Prevent hostile input from being able to crash the system */
3207 if (ifc->ifc_len <= 0)
3211 if (ifc->ifc_len <= max_len) {
3212 max_len = ifc->ifc_len;
3215 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3220 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3221 struct epoch_tracker et;
3225 * Zero the ifr to make sure we don't disclose the contents
3228 memset(&ifr, 0, sizeof(ifr));
3230 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3231 >= sizeof(ifr.ifr_name)) {
3234 return (ENAMETOOLONG);
3238 NET_EPOCH_ENTER(et);
3239 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3240 struct sockaddr *sa = ifa->ifa_addr;
3242 if (prison_if(curthread->td_ucred, sa) != 0)
3245 if (sa->sa_len <= sizeof(*sa)) {
3246 if (sa->sa_len < sizeof(*sa)) {
3247 memset(&ifr.ifr_ifru.ifru_addr, 0,
3248 sizeof(ifr.ifr_ifru.ifru_addr));
3249 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3252 ifr.ifr_ifru.ifru_addr = *sa;
3253 sbuf_bcat(sb, &ifr, sizeof(ifr));
3254 max_len += sizeof(ifr);
3257 offsetof(struct ifreq, ifr_addr));
3258 max_len += offsetof(struct ifreq, ifr_addr);
3259 sbuf_bcat(sb, sa, sa->sa_len);
3260 max_len += sa->sa_len;
3263 if (sbuf_error(sb) == 0)
3264 valid_len = sbuf_len(sb);
3268 sbuf_bcat(sb, &ifr, sizeof(ifr));
3269 max_len += sizeof(ifr);
3271 if (sbuf_error(sb) == 0)
3272 valid_len = sbuf_len(sb);
3278 * If we didn't allocate enough space (uncommon), try again. If
3279 * we have already allocated as much space as we are allowed,
3280 * return what we've got.
3282 if (valid_len != max_len && !full) {
3287 ifc->ifc_len = valid_len;
3289 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3295 * Just like ifpromisc(), but for all-multicast-reception mode.
3298 if_allmulti(struct ifnet *ifp, int onswitch)
3301 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3304 struct ifmultiaddr *
3305 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3307 struct ifmultiaddr *ifma;
3309 IF_ADDR_LOCK_ASSERT(ifp);
3311 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3312 if (sa->sa_family == AF_LINK) {
3313 if (sa_dl_equal(ifma->ifma_addr, sa))
3316 if (sa_equal(ifma->ifma_addr, sa))
3325 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3326 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3327 * the ifnet multicast address list here, so the caller must do that and
3328 * other setup work (such as notifying the device driver). The reference
3329 * count is initialized to 1.
3331 static struct ifmultiaddr *
3332 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3335 struct ifmultiaddr *ifma;
3336 struct sockaddr *dupsa;
3338 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3343 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3344 if (dupsa == NULL) {
3345 free(ifma, M_IFMADDR);
3348 bcopy(sa, dupsa, sa->sa_len);
3349 ifma->ifma_addr = dupsa;
3351 ifma->ifma_ifp = ifp;
3352 ifma->ifma_refcount = 1;
3353 ifma->ifma_protospec = NULL;
3356 ifma->ifma_lladdr = NULL;
3360 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3361 if (dupsa == NULL) {
3362 free(ifma->ifma_addr, M_IFMADDR);
3363 free(ifma, M_IFMADDR);
3366 bcopy(llsa, dupsa, llsa->sa_len);
3367 ifma->ifma_lladdr = dupsa;
3373 * if_freemulti: free ifmultiaddr structure and possibly attached related
3374 * addresses. The caller is responsible for implementing reference
3375 * counting, notifying the driver, handling routing messages, and releasing
3376 * any dependent link layer state.
3378 #ifdef MCAST_VERBOSE
3379 extern void kdb_backtrace(void);
3382 if_freemulti_internal(struct ifmultiaddr *ifma)
3385 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3386 ifma->ifma_refcount));
3388 if (ifma->ifma_lladdr != NULL)
3389 free(ifma->ifma_lladdr, M_IFMADDR);
3390 #ifdef MCAST_VERBOSE
3392 printf("%s freeing ifma: %p\n", __func__, ifma);
3394 free(ifma->ifma_addr, M_IFMADDR);
3395 free(ifma, M_IFMADDR);
3399 if_destroymulti(epoch_context_t ctx)
3401 struct ifmultiaddr *ifma;
3403 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3404 if_freemulti_internal(ifma);
3408 if_freemulti(struct ifmultiaddr *ifma)
3410 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3411 ifma->ifma_refcount));
3413 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3417 * Register an additional multicast address with a network interface.
3419 * - If the address is already present, bump the reference count on the
3420 * address and return.
3421 * - If the address is not link-layer, look up a link layer address.
3422 * - Allocate address structures for one or both addresses, and attach to the
3423 * multicast address list on the interface. If automatically adding a link
3424 * layer address, the protocol address will own a reference to the link
3425 * layer address, to be freed when it is freed.
3426 * - Notify the network device driver of an addition to the multicast address
3429 * 'sa' points to caller-owned memory with the desired multicast address.
3431 * 'retifma' will be used to return a pointer to the resulting multicast
3432 * address reference, if desired.
3435 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3436 struct ifmultiaddr **retifma)
3438 struct ifmultiaddr *ifma, *ll_ifma;
3439 struct sockaddr *llsa;
3440 struct sockaddr_dl sdl;
3444 IN_MULTI_LIST_UNLOCK_ASSERT();
3447 IN6_MULTI_LIST_UNLOCK_ASSERT();
3450 * If the address is already present, return a new reference to it;
3451 * otherwise, allocate storage and set up a new address.
3454 ifma = if_findmulti(ifp, sa);
3456 ifma->ifma_refcount++;
3457 if (retifma != NULL)
3459 IF_ADDR_WUNLOCK(ifp);
3464 * The address isn't already present; resolve the protocol address
3465 * into a link layer address, and then look that up, bump its
3466 * refcount or allocate an ifma for that also.
3467 * Most link layer resolving functions returns address data which
3468 * fits inside default sockaddr_dl structure. However callback
3469 * can allocate another sockaddr structure, in that case we need to
3474 if (ifp->if_resolvemulti != NULL) {
3475 /* Provide called function with buffer size information */
3476 sdl.sdl_len = sizeof(sdl);
3477 llsa = (struct sockaddr *)&sdl;
3478 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3484 * Allocate the new address. Don't hook it up yet, as we may also
3485 * need to allocate a link layer multicast address.
3487 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3494 * If a link layer address is found, we'll need to see if it's
3495 * already present in the address list, or allocate is as well.
3496 * When this block finishes, the link layer address will be on the
3500 ll_ifma = if_findmulti(ifp, llsa);
3501 if (ll_ifma == NULL) {
3502 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3503 if (ll_ifma == NULL) {
3504 --ifma->ifma_refcount;
3509 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3510 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3513 ll_ifma->ifma_refcount++;
3514 ifma->ifma_llifma = ll_ifma;
3518 * We now have a new multicast address, ifma, and possibly a new or
3519 * referenced link layer address. Add the primary address to the
3520 * ifnet address list.
3522 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3523 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3525 if (retifma != NULL)
3529 * Must generate the message while holding the lock so that 'ifma'
3530 * pointer is still valid.
3532 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3533 IF_ADDR_WUNLOCK(ifp);
3536 * We are certain we have added something, so call down to the
3537 * interface to let them know about it.
3539 if (ifp->if_ioctl != NULL) {
3540 if (THREAD_CAN_SLEEP())
3541 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3543 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3546 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3547 link_free_sdl(llsa);
3552 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3553 link_free_sdl(llsa);
3556 IF_ADDR_WUNLOCK(ifp);
3561 if_siocaddmulti(void *arg, int pending)
3568 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3570 CURVNET_SET(ifp->if_vnet);
3571 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3576 * Delete a multicast group membership by network-layer group address.
3578 * Returns ENOENT if the entry could not be found. If ifp no longer
3579 * exists, results are undefined. This entry point should only be used
3580 * from subsystems which do appropriate locking to hold ifp for the
3581 * duration of the call.
3582 * Network-layer protocol domains must use if_delmulti_ifma().
3585 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3587 struct ifmultiaddr *ifma;
3590 KASSERT(ifp, ("%s: NULL ifp", __func__));
3594 ifma = if_findmulti(ifp, sa);
3596 lastref = if_delmulti_locked(ifp, ifma, 0);
3597 IF_ADDR_WUNLOCK(ifp);
3602 if (lastref && ifp->if_ioctl != NULL) {
3603 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3610 * Delete all multicast group membership for an interface.
3611 * Should be used to quickly flush all multicast filters.
3614 if_delallmulti(struct ifnet *ifp)
3616 struct ifmultiaddr *ifma;
3617 struct ifmultiaddr *next;
3620 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3621 if_delmulti_locked(ifp, ifma, 0);
3622 IF_ADDR_WUNLOCK(ifp);
3626 if_delmulti_ifma(struct ifmultiaddr *ifma)
3628 if_delmulti_ifma_flags(ifma, 0);
3632 * Delete a multicast group membership by group membership pointer.
3633 * Network-layer protocol domains must use this routine.
3635 * It is safe to call this routine if the ifp disappeared.
3638 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3642 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3644 IN_MULTI_LIST_UNLOCK_ASSERT();
3646 ifp = ifma->ifma_ifp;
3649 printf("%s: ifma_ifp seems to be detached\n", __func__);
3651 struct epoch_tracker et;
3654 NET_EPOCH_ENTER(et);
3655 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3664 * If and only if the ifnet instance exists: Acquire the address lock.
3669 lastref = if_delmulti_locked(ifp, ifma, flags);
3673 * If and only if the ifnet instance exists:
3674 * Release the address lock.
3675 * If the group was left: update the hardware hash filter.
3677 IF_ADDR_WUNLOCK(ifp);
3678 if (lastref && ifp->if_ioctl != NULL) {
3679 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3685 * Perform deletion of network-layer and/or link-layer multicast address.
3687 * Return 0 if the reference count was decremented.
3688 * Return 1 if the final reference was released, indicating that the
3689 * hardware hash filter should be reprogrammed.
3692 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3694 struct ifmultiaddr *ll_ifma;
3696 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3697 KASSERT(ifma->ifma_ifp == ifp,
3698 ("%s: inconsistent ifp %p", __func__, ifp));
3699 IF_ADDR_WLOCK_ASSERT(ifp);
3702 ifp = ifma->ifma_ifp;
3703 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3706 * If the ifnet is detaching, null out references to ifnet,
3707 * so that upper protocol layers will notice, and not attempt
3708 * to obtain locks for an ifnet which no longer exists. The
3709 * routing socket announcement must happen before the ifnet
3710 * instance is detached from the system.
3714 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3717 * ifp may already be nulled out if we are being reentered
3718 * to delete the ll_ifma.
3721 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3722 ifma->ifma_ifp = NULL;
3726 if (--ifma->ifma_refcount > 0)
3729 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3730 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3731 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3734 * If this ifma is a network-layer ifma, a link-layer ifma may
3735 * have been associated with it. Release it first if so.
3737 ll_ifma = ifma->ifma_llifma;
3738 if (ll_ifma != NULL) {
3739 KASSERT(ifma->ifma_lladdr != NULL,
3740 ("%s: llifma w/o lladdr", __func__));
3742 ll_ifma->ifma_ifp = NULL; /* XXX */
3743 if (--ll_ifma->ifma_refcount == 0) {
3745 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3746 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3748 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3751 if_freemulti(ll_ifma);
3756 struct ifmultiaddr *ifmatmp;
3758 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3759 MPASS(ifma != ifmatmp);
3764 * The last reference to this instance of struct ifmultiaddr
3765 * was released; the hardware should be notified of this change.
3771 * Set the link layer address on an interface.
3773 * At this time we only support certain types of interfaces,
3774 * and we don't allow the length of the address to change.
3776 * Set noinline to be dtrace-friendly
3779 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3781 struct sockaddr_dl *sdl;
3789 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3793 if (len != sdl->sdl_alen) /* don't allow length to change */
3796 switch (ifp->if_type) {
3801 case IFT_IEEE8023ADLAG:
3802 bcopy(lladdr, LLADDR(sdl), len);
3809 * If the interface is already up, we need
3810 * to re-init it in order to reprogram its
3813 if ((ifp->if_flags & IFF_UP) != 0) {
3814 if (ifp->if_ioctl) {
3815 ifp->if_flags &= ~IFF_UP;
3816 ifr.ifr_flags = ifp->if_flags & 0xffff;
3817 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3818 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3819 ifp->if_flags |= IFF_UP;
3820 ifr.ifr_flags = ifp->if_flags & 0xffff;
3821 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3822 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3825 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3831 * Compat function for handling basic encapsulation requests.
3832 * Not converted stacks (FDDI, IB, ..) supports traditional
3833 * output model: ARP (and other similar L2 protocols) are handled
3834 * inside output routine, arpresolve/nd6_resolve() returns MAC
3835 * address instead of full prepend.
3837 * This function creates calculated header==MAC for IPv4/IPv6 and
3838 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3842 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3845 if (req->rtype != IFENCAP_LL)
3846 return (EOPNOTSUPP);
3848 if (req->bufsize < req->lladdr_len)
3851 switch (req->family) {
3856 return (EAFNOSUPPORT);
3859 /* Copy lladdr to storage as is */
3860 memmove(req->buf, req->lladdr, req->lladdr_len);
3861 req->bufsize = req->lladdr_len;
3862 req->lladdr_off = 0;
3868 * Tunnel interfaces can nest, also they may cause infinite recursion
3869 * calls when misconfigured. We'll prevent this by detecting loops.
3870 * High nesting level may cause stack exhaustion. We'll prevent this
3871 * by introducing upper limit.
3873 * Return 0, if tunnel nesting count is equal or less than limit.
3876 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3884 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3885 if (*(struct ifnet **)(mtag + 1) == ifp) {
3886 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3891 if (count > limit) {
3893 "%s: if_output recursively called too many times(%d)\n",
3894 if_name(ifp), count);
3897 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3900 *(struct ifnet **)(mtag + 1) = ifp;
3901 m_tag_prepend(m, mtag);
3906 * Get the link layer address that was read from the hardware at attach.
3908 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3909 * their component interfaces as IFT_IEEE8023ADLAG.
3912 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3915 if (ifp->if_hw_addr == NULL)
3918 switch (ifp->if_type) {
3920 case IFT_IEEE8023ADLAG:
3921 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3929 * The name argument must be a pointer to storage which will last as
3930 * long as the interface does. For physical devices, the result of
3931 * device_get_name(dev) is a good choice and for pseudo-devices a
3932 * static string works well.
3935 if_initname(struct ifnet *ifp, const char *name, int unit)
3937 ifp->if_dname = name;
3938 ifp->if_dunit = unit;
3939 if (unit != IF_DUNIT_NONE)
3940 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3942 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3946 if_printf(struct ifnet *ifp, const char *fmt, ...)
3951 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3953 vlog(LOG_INFO, if_fmt, ap);
3959 if_start(struct ifnet *ifp)
3962 (*(ifp)->if_start)(ifp);
3966 * Backwards compatibility interface for drivers
3967 * that have not implemented it
3970 if_transmit(struct ifnet *ifp, struct mbuf *m)
3974 IFQ_HANDOFF(ifp, m, error);
3979 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3986 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3991 if (_IF_QFULL(ifq)) {
3993 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3998 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3999 if (m->m_flags & (M_BCAST|M_MCAST))
4000 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4001 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4003 _IF_ENQUEUE(ifq, m);
4005 if (ifp != NULL && !active)
4006 (*(ifp)->if_start)(ifp);
4011 if_register_com_alloc(u_char type,
4012 if_com_alloc_t *a, if_com_free_t *f)
4015 KASSERT(if_com_alloc[type] == NULL,
4016 ("if_register_com_alloc: %d already registered", type));
4017 KASSERT(if_com_free[type] == NULL,
4018 ("if_register_com_alloc: %d free already registered", type));
4020 if_com_alloc[type] = a;
4021 if_com_free[type] = f;
4025 if_deregister_com_alloc(u_char type)
4028 KASSERT(if_com_alloc[type] != NULL,
4029 ("if_deregister_com_alloc: %d not registered", type));
4030 KASSERT(if_com_free[type] != NULL,
4031 ("if_deregister_com_alloc: %d free not registered", type));
4032 if_com_alloc[type] = NULL;
4033 if_com_free[type] = NULL;
4036 /* API for driver access to network stack owned ifnet.*/
4038 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4042 oldbrate = ifp->if_baudrate;
4043 ifp->if_baudrate = baudrate;
4048 if_getbaudrate(if_t ifp)
4051 return (((struct ifnet *)ifp)->if_baudrate);
4055 if_setcapabilities(if_t ifp, int capabilities)
4057 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4062 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4064 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4065 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4071 if_getcapabilities(if_t ifp)
4073 return ((struct ifnet *)ifp)->if_capabilities;
4077 if_setcapenable(if_t ifp, int capabilities)
4079 ((struct ifnet *)ifp)->if_capenable = capabilities;
4084 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4087 ((struct ifnet *)ifp)->if_capenable |= setcap;
4089 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4095 if_getdname(if_t ifp)
4097 return ((struct ifnet *)ifp)->if_dname;
4101 if_togglecapenable(if_t ifp, int togglecap)
4103 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4108 if_getcapenable(if_t ifp)
4110 return ((struct ifnet *)ifp)->if_capenable;
4114 * This is largely undesirable because it ties ifnet to a device, but does
4115 * provide flexiblity for an embedded product vendor. Should be used with
4116 * the understanding that it violates the interface boundaries, and should be
4117 * a last resort only.
4120 if_setdev(if_t ifp, void *dev)
4126 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4128 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4129 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4135 if_getdrvflags(if_t ifp)
4137 return ((struct ifnet *)ifp)->if_drv_flags;
4141 if_setdrvflags(if_t ifp, int flags)
4143 ((struct ifnet *)ifp)->if_drv_flags = flags;
4148 if_setflags(if_t ifp, int flags)
4151 ifp->if_flags = flags;
4156 if_setflagbits(if_t ifp, int set, int clear)
4158 ((struct ifnet *)ifp)->if_flags |= set;
4159 ((struct ifnet *)ifp)->if_flags &= ~clear;
4165 if_getflags(if_t ifp)
4167 return ((struct ifnet *)ifp)->if_flags;
4171 if_clearhwassist(if_t ifp)
4173 ((struct ifnet *)ifp)->if_hwassist = 0;
4178 if_sethwassistbits(if_t ifp, int toset, int toclear)
4180 ((struct ifnet *)ifp)->if_hwassist |= toset;
4181 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4187 if_sethwassist(if_t ifp, int hwassist_bit)
4189 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4194 if_gethwassist(if_t ifp)
4196 return ((struct ifnet *)ifp)->if_hwassist;
4200 if_setmtu(if_t ifp, int mtu)
4202 ((struct ifnet *)ifp)->if_mtu = mtu;
4209 return ((struct ifnet *)ifp)->if_mtu;
4213 if_getmtu_family(if_t ifp, int family)
4217 for (dp = domains; dp; dp = dp->dom_next) {
4218 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4219 return (dp->dom_ifmtu((struct ifnet *)ifp));
4222 return (((struct ifnet *)ifp)->if_mtu);
4226 * Methods for drivers to access interface unicast and multicast
4227 * link level addresses. Driver shall not know 'struct ifaddr' neither
4228 * 'struct ifmultiaddr'.
4231 if_lladdr_count(if_t ifp)
4233 struct epoch_tracker et;
4238 NET_EPOCH_ENTER(et);
4239 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4240 if (ifa->ifa_addr->sa_family == AF_LINK)
4248 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4250 struct epoch_tracker et;
4257 NET_EPOCH_ENTER(et);
4258 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4259 if (ifa->ifa_addr->sa_family != AF_LINK)
4261 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4270 if_llmaddr_count(if_t ifp)
4272 struct epoch_tracker et;
4273 struct ifmultiaddr *ifma;
4277 NET_EPOCH_ENTER(et);
4278 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4279 if (ifma->ifma_addr->sa_family == AF_LINK)
4287 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4289 struct epoch_tracker et;
4290 struct ifmultiaddr *ifma;
4296 NET_EPOCH_ENTER(et);
4297 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4298 if (ifma->ifma_addr->sa_family != AF_LINK)
4300 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4309 if_setsoftc(if_t ifp, void *softc)
4311 ((struct ifnet *)ifp)->if_softc = softc;
4316 if_getsoftc(if_t ifp)
4318 return ((struct ifnet *)ifp)->if_softc;
4322 if_setrcvif(struct mbuf *m, if_t ifp)
4325 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4326 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4330 if_setvtag(struct mbuf *m, uint16_t tag)
4332 m->m_pkthdr.ether_vtag = tag;
4336 if_getvtag(struct mbuf *m)
4339 return (m->m_pkthdr.ether_vtag);
4343 if_sendq_empty(if_t ifp)
4345 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4349 if_getifaddr(if_t ifp)
4351 return ((struct ifnet *)ifp)->if_addr;
4355 if_getamcount(if_t ifp)
4357 return ((struct ifnet *)ifp)->if_amcount;
4361 if_setsendqready(if_t ifp)
4363 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4368 if_setsendqlen(if_t ifp, int tx_desc_count)
4370 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4371 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4377 if_vlantrunkinuse(if_t ifp)
4379 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4383 if_input(if_t ifp, struct mbuf* sendmp)
4385 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4391 if_dequeue(if_t ifp)
4394 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4400 if_sendq_prepend(if_t ifp, struct mbuf *m)
4402 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4407 if_setifheaderlen(if_t ifp, int len)
4409 ((struct ifnet *)ifp)->if_hdrlen = len;
4414 if_getlladdr(if_t ifp)
4416 return (IF_LLADDR((struct ifnet *)ifp));
4420 if_gethandle(u_char type)
4422 return (if_alloc(type));
4426 if_bpfmtap(if_t ifh, struct mbuf *m)
4428 struct ifnet *ifp = (struct ifnet *)ifh;
4434 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4436 struct ifnet *ifp = (struct ifnet *)ifh;
4438 ETHER_BPF_MTAP(ifp, m);
4442 if_vlancap(if_t ifh)
4444 struct ifnet *ifp = (struct ifnet *)ifh;
4445 VLAN_CAPABILITIES(ifp);
4449 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4452 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4457 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4460 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4465 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4468 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4473 if_gethwtsomax(if_t ifp)
4476 return (((struct ifnet *)ifp)->if_hw_tsomax);
4480 if_gethwtsomaxsegcount(if_t ifp)
4483 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4487 if_gethwtsomaxsegsize(if_t ifp)
4490 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4494 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4496 ((struct ifnet *)ifp)->if_init = init_fn;
4500 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4502 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4506 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4508 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4512 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4514 ((struct ifnet *)ifp)->if_transmit = start_fn;
4517 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4519 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4524 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4527 ifp->if_get_counter = fn;
4530 /* Revisit these - These are inline functions originally. */
4532 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4534 return drbr_inuse(ifh, br);
4538 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4540 return drbr_dequeue(ifh, br);
4544 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4546 return drbr_needs_enqueue(ifh, br);
4550 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4552 return drbr_enqueue(ifh, br, m);