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));
152 char ifd_name[IFNAMSIZ];
157 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
158 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
160 struct ifgroupreq32 {
161 char ifgr_name[IFNAMSIZ];
164 char ifgru_group[IFNAMSIZ];
165 uint32_t ifgru_groups;
169 struct ifmediareq32 {
170 char ifm_name[IFNAMSIZ];
176 uint32_t ifm_ulist; /* (int *) */
178 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
179 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
181 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
182 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
183 #else /* !COMPAT_FREEBSD32 */
184 #define _CASE_IOC_IFGROUPREQ_32(cmd)
185 #endif /* !COMPAT_FREEBSD32 */
187 #define CASE_IOC_IFGROUPREQ(cmd) \
188 _CASE_IOC_IFGROUPREQ_32(cmd) \
193 #ifdef COMPAT_FREEBSD32
194 struct ifreq32 ifr32;
198 union ifgroupreq_union {
199 struct ifgroupreq ifgr;
200 #ifdef COMPAT_FREEBSD32
201 struct ifgroupreq32 ifgr32;
205 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
207 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
208 "Generic link-management");
210 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
211 &ifqmaxlen, 0, "max send queue size");
213 /* Log link state change events */
214 static int log_link_state_change = 1;
216 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
217 &log_link_state_change, 0,
218 "log interface link state change events");
220 /* Log promiscuous mode change events */
221 static int log_promisc_mode_change = 1;
223 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
224 &log_promisc_mode_change, 1,
225 "log promiscuous mode change events");
227 /* Interface description */
228 static unsigned int ifdescr_maxlen = 1024;
229 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
231 "administrative maximum length for interface description");
233 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
235 /* global sx for non-critical path ifdescr */
236 static struct sx ifdescr_sx;
237 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
239 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
240 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
241 /* These are external hooks for CARP. */
242 void (*carp_linkstate_p)(struct ifnet *ifp);
243 void (*carp_demote_adj_p)(int, char *);
244 int (*carp_master_p)(struct ifaddr *);
245 #if defined(INET) || defined(INET6)
246 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
247 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
248 const struct sockaddr *sa);
249 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
250 int (*carp_attach_p)(struct ifaddr *, int);
251 void (*carp_detach_p)(struct ifaddr *, bool);
254 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
257 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
258 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
259 const struct in6_addr *taddr);
262 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
265 * XXX: Style; these should be sorted alphabetically, and unprototyped
266 * static functions should be prototyped. Currently they are sorted by
269 static void if_attachdomain(void *);
270 static void if_attachdomain1(struct ifnet *);
271 static int ifconf(u_long, caddr_t);
272 static void *if_grow(void);
273 static void if_input_default(struct ifnet *, struct mbuf *);
274 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
275 static void if_route(struct ifnet *, int flag, int fam);
276 static int if_setflag(struct ifnet *, int, int, int *, int);
277 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
278 static void if_unroute(struct ifnet *, int flag, int fam);
279 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
280 static void do_link_state_change(void *, int);
281 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
282 static int if_getgroupmembers(struct ifgroupreq *);
283 static void if_delgroups(struct ifnet *);
284 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
285 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
286 static void if_siocaddmulti(void *, int);
287 static void if_link_ifnet(struct ifnet *);
288 static bool if_unlink_ifnet(struct ifnet *, bool);
290 static int if_vmove(struct ifnet *, struct vnet *);
295 * XXX: declare here to avoid to include many inet6 related files..
296 * should be more generalized?
298 extern void nd6_setmtu(struct ifnet *);
301 /* ipsec helper hooks */
302 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
303 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
305 VNET_DEFINE(int, if_index);
306 int ifqmaxlen = IFQ_MAXLEN;
307 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
308 VNET_DEFINE(struct ifgrouphead, ifg_head);
310 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
312 /* Table of ifnet by index. */
313 VNET_DEFINE(struct ifnet **, ifindex_table);
315 #define V_if_indexlim VNET(if_indexlim)
316 #define V_ifindex_table VNET(ifindex_table)
319 * The global network interface list (V_ifnet) and related state (such as
320 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
321 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
323 struct sx ifnet_sxlock;
324 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
326 struct sx ifnet_detach_sxlock;
327 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
331 * The allocation of network interfaces is a rather non-atomic affair; we
332 * need to select an index before we are ready to expose the interface for
333 * use, so will use this pointer value to indicate reservation.
335 #define IFNET_HOLD (void *)(uintptr_t)(-1)
338 #define VNET_IS_SHUTTING_DOWN(_vnet) \
339 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
342 static if_com_alloc_t *if_com_alloc[256];
343 static if_com_free_t *if_com_free[256];
345 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
346 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
347 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
350 ifnet_byindex(u_short idx)
354 if (__predict_false(idx > V_if_index))
357 ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx);
358 return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp);
362 ifnet_byindex_ref(u_short idx)
368 ifp = ifnet_byindex(idx);
369 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
371 if (!if_try_ref(ifp))
377 * Allocate an ifindex array entry; return 0 on success or an error on
381 ifindex_alloc(void **old)
385 IFNET_WLOCK_ASSERT();
387 * Try to find an empty slot below V_if_index. If we fail, take the
390 for (idx = 1; idx <= V_if_index; idx++) {
391 if (V_ifindex_table[idx] == NULL)
395 /* Catch if_index overflow. */
396 if (idx >= V_if_indexlim) {
400 if (idx > V_if_index)
406 ifindex_free_locked(u_short idx)
409 IFNET_WLOCK_ASSERT();
411 V_ifindex_table[idx] = NULL;
412 while (V_if_index > 0 &&
413 V_ifindex_table[V_if_index] == NULL)
418 ifindex_free(u_short idx)
422 ifindex_free_locked(idx);
427 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
430 V_ifindex_table[idx] = ifp;
434 ifaddr_byindex(u_short idx)
437 struct ifaddr *ifa = NULL;
441 ifp = ifnet_byindex(idx);
442 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
448 * Network interface utility routines.
450 * Routines with ifa_ifwith* names take sockaddr *'s as
455 vnet_if_init(const void *unused __unused)
459 CK_STAILQ_INIT(&V_ifnet);
460 CK_STAILQ_INIT(&V_ifg_head);
462 old = if_grow(); /* create initial table */
464 epoch_wait_preempt(net_epoch_preempt);
466 vnet_if_clone_init();
468 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
473 vnet_if_uninit(const void *unused __unused)
476 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
477 "not empty", __func__, __LINE__, &V_ifnet));
478 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
479 "not empty", __func__, __LINE__, &V_ifg_head));
481 free((caddr_t)V_ifindex_table, M_IFNET);
483 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
484 vnet_if_uninit, NULL);
488 if_link_ifnet(struct ifnet *ifp)
492 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
494 curvnet->vnet_ifcnt++;
500 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
506 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
508 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
510 ifp->if_flags |= IFF_DYING;
515 curvnet->vnet_ifcnt--;
524 vnet_if_return(const void *unused __unused)
526 struct ifnet *ifp, *nifp;
527 struct ifnet **pending;
533 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
534 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
535 * if_detach_internal(), which waits for NET_EPOCH callbacks to
536 * complete. We can't do that from within NET_EPOCH.
538 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
539 * read/write lock. We cannot hold the lock as we call if_vmove()
540 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
545 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
546 M_IFNET, M_WAITOK | M_ZERO);
548 /* Return all inherited interfaces to their parent vnets. */
549 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
550 if (ifp->if_home_vnet != ifp->if_vnet) {
551 found = if_unlink_ifnet(ifp, true);
559 for (int j = 0; j < i; j++) {
560 sx_xlock(&ifnet_detach_sxlock);
561 if_vmove(pending[j], pending[j]->if_home_vnet);
562 sx_xunlock(&ifnet_detach_sxlock);
565 free(pending, M_IFNET);
567 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
568 vnet_if_return, NULL);
580 IFNET_WLOCK_ASSERT();
581 oldlim = V_if_indexlim;
583 n = (oldlim << 1) * sizeof(*e);
584 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
586 if (V_if_indexlim != oldlim) {
590 if (V_ifindex_table != NULL) {
591 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
592 old = V_ifindex_table;
600 * Allocate a struct ifnet and an index for an interface. A layer 2
601 * common structure will also be allocated if an allocation routine is
602 * registered for the passed type.
605 if_alloc_domain(u_char type, int numa_domain)
611 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
612 if (numa_domain == IF_NODOM)
613 ifp = malloc(sizeof(struct ifnet), M_IFNET,
616 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
617 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
620 idx = ifindex_alloc(&old);
621 if (__predict_false(idx == USHRT_MAX)) {
623 epoch_wait_preempt(net_epoch_preempt);
627 ifnet_setbyindex(idx, IFNET_HOLD);
631 ifp->if_alloctype = type;
632 ifp->if_numa_domain = numa_domain;
634 ifp->if_vnet = curvnet;
636 if (if_com_alloc[type] != NULL) {
637 ifp->if_l2com = if_com_alloc[type](type, ifp);
638 if (ifp->if_l2com == NULL) {
645 IF_ADDR_LOCK_INIT(ifp);
646 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
647 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
648 ifp->if_afdata_initialized = 0;
649 IF_AFDATA_LOCK_INIT(ifp);
650 CK_STAILQ_INIT(&ifp->if_addrhead);
651 CK_STAILQ_INIT(&ifp->if_multiaddrs);
652 CK_STAILQ_INIT(&ifp->if_groups);
656 ifq_init(&ifp->if_snd, ifp);
658 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
659 for (int i = 0; i < IFCOUNTERS; i++)
660 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
661 ifp->if_get_counter = if_get_counter_default;
662 ifp->if_pcp = IFNET_PCP_NONE;
663 ifnet_setbyindex(ifp->if_index, ifp);
668 if_alloc_dev(u_char type, device_t dev)
672 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
673 return (if_alloc_domain(type, IF_NODOM));
674 return (if_alloc_domain(type, numa_domain));
678 if_alloc(u_char type)
681 return (if_alloc_domain(type, IF_NODOM));
684 * Do the actual work of freeing a struct ifnet, and layer 2 common
685 * structure. This call is made when the last reference to an
686 * interface is released.
689 if_free_internal(struct ifnet *ifp)
692 KASSERT((ifp->if_flags & IFF_DYING),
693 ("if_free_internal: interface not dying"));
695 if (if_com_free[ifp->if_alloctype] != NULL)
696 if_com_free[ifp->if_alloctype](ifp->if_l2com,
700 mac_ifnet_destroy(ifp);
702 IF_AFDATA_DESTROY(ifp);
703 IF_ADDR_LOCK_DESTROY(ifp);
704 ifq_delete(&ifp->if_snd);
706 for (int i = 0; i < IFCOUNTERS; i++)
707 counter_u64_free(ifp->if_counters[i]);
709 free(ifp->if_description, M_IFDESCR);
710 free(ifp->if_hw_addr, M_IFADDR);
715 if_destroy(epoch_context_t ctx)
719 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
720 if_free_internal(ifp);
724 * Deregister an interface and free the associated storage.
727 if_free(struct ifnet *ifp)
730 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
732 CURVNET_SET_QUIET(ifp->if_vnet);
734 KASSERT(ifp == ifnet_byindex(ifp->if_index),
735 ("%s: freeing unallocated ifnet", ifp->if_xname));
737 ifindex_free_locked(ifp->if_index);
740 if (refcount_release(&ifp->if_refcount))
741 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
746 * Interfaces to keep an ifnet type-stable despite the possibility of the
747 * driver calling if_free(). If there are additional references, we defer
748 * freeing the underlying data structure.
751 if_ref(struct ifnet *ifp)
755 /* We don't assert the ifnet list lock here, but arguably should. */
756 old = refcount_acquire(&ifp->if_refcount);
757 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
761 if_try_ref(struct ifnet *ifp)
764 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
768 if_rele(struct ifnet *ifp)
771 if (!refcount_release(&ifp->if_refcount))
773 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
777 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
780 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
782 if (ifq->ifq_maxlen == 0)
783 ifq->ifq_maxlen = ifqmaxlen;
786 ifq->altq_disc = NULL;
787 ifq->altq_flags &= ALTQF_CANTCHANGE;
788 ifq->altq_tbr = NULL;
793 ifq_delete(struct ifaltq *ifq)
795 mtx_destroy(&ifq->ifq_mtx);
799 * Perform generic interface initialization tasks and attach the interface
800 * to the list of "active" interfaces. If vmove flag is set on entry
801 * to if_attach_internal(), perform only a limited subset of initialization
802 * tasks, given that we are moving from one vnet to another an ifnet which
803 * has already been fully initialized.
805 * Note that if_detach_internal() removes group membership unconditionally
806 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
807 * Thus, when if_vmove() is applied to a cloned interface, group membership
808 * is lost while a cloned one always joins a group whose name is
809 * ifc->ifc_name. To recover this after if_detach_internal() and
810 * if_attach_internal(), the cloner should be specified to
811 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
812 * attempts to join a group whose name is ifc->ifc_name.
815 * - The decision to return void and thus require this function to
816 * succeed is questionable.
817 * - We should probably do more sanity checking. For instance we don't
818 * do anything to insure if_xname is unique or non-empty.
821 if_attach(struct ifnet *ifp)
824 if_attach_internal(ifp, 0, NULL);
828 * Compute the least common TSO limit.
831 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
834 * 1) If there is no limit currently, take the limit from
835 * the network adapter.
837 * 2) If the network adapter has a limit below the current
840 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
841 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
842 pmax->tsomaxbytes = ifp->if_hw_tsomax;
844 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
845 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
846 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
848 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
849 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
850 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
855 * Update TSO limit of a network adapter.
857 * Returns zero if no change. Else non-zero.
860 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
863 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
864 ifp->if_hw_tsomax = pmax->tsomaxbytes;
867 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
868 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
871 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
872 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
879 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
881 unsigned socksize, ifasize;
882 int namelen, masklen;
883 struct sockaddr_dl *sdl;
886 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
887 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
891 ifp->if_vnet = curvnet;
892 if (ifp->if_home_vnet == NULL)
893 ifp->if_home_vnet = curvnet;
896 if_addgroup(ifp, IFG_ALL);
898 /* Restore group membership for cloned interfaces. */
899 if (vmove && ifc != NULL)
900 if_clone_addgroup(ifp, ifc);
902 getmicrotime(&ifp->if_lastchange);
903 ifp->if_epoch = time_uptime;
905 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
906 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
907 ("transmit and qflush must both either be set or both be NULL"));
908 if (ifp->if_transmit == NULL) {
909 ifp->if_transmit = if_transmit;
910 ifp->if_qflush = if_qflush;
912 if (ifp->if_input == NULL)
913 ifp->if_input = if_input_default;
915 if (ifp->if_requestencap == NULL)
916 ifp->if_requestencap = if_requestencap_default;
920 mac_ifnet_create(ifp);
924 * Create a Link Level name for this device.
926 namelen = strlen(ifp->if_xname);
928 * Always save enough space for any possiable name so we
929 * can do a rename in place later.
931 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
932 socksize = masklen + ifp->if_addrlen;
933 if (socksize < sizeof(*sdl))
934 socksize = sizeof(*sdl);
935 socksize = roundup2(socksize, sizeof(long));
936 ifasize = sizeof(*ifa) + 2 * socksize;
937 ifa = ifa_alloc(ifasize, M_WAITOK);
938 sdl = (struct sockaddr_dl *)(ifa + 1);
939 sdl->sdl_len = socksize;
940 sdl->sdl_family = AF_LINK;
941 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
942 sdl->sdl_nlen = namelen;
943 sdl->sdl_index = ifp->if_index;
944 sdl->sdl_type = ifp->if_type;
947 ifa->ifa_addr = (struct sockaddr *)sdl;
948 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
949 ifa->ifa_netmask = (struct sockaddr *)sdl;
950 sdl->sdl_len = masklen;
952 sdl->sdl_data[--namelen] = 0xff;
953 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
954 /* Reliably crash if used uninitialized. */
955 ifp->if_broadcastaddr = NULL;
957 if (ifp->if_type == IFT_ETHER) {
958 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
962 #if defined(INET) || defined(INET6)
963 /* Use defaults for TSO, if nothing is set */
964 if (ifp->if_hw_tsomax == 0 &&
965 ifp->if_hw_tsomaxsegcount == 0 &&
966 ifp->if_hw_tsomaxsegsize == 0) {
968 * The TSO defaults needs to be such that an
969 * NFS mbuf list of 35 mbufs totalling just
970 * below 64K works and that a chain of mbufs
971 * can be defragged into at most 32 segments:
973 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
974 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
975 ifp->if_hw_tsomaxsegcount = 35;
976 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
978 /* XXX some drivers set IFCAP_TSO after ethernet attach */
979 if (ifp->if_capabilities & IFCAP_TSO) {
980 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
982 ifp->if_hw_tsomaxsegcount,
983 ifp->if_hw_tsomaxsegsize);
991 * Update the interface index in the link layer address
994 for (ifa = ifp->if_addr; ifa != NULL;
995 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
996 if (ifa->ifa_addr->sa_family == AF_LINK) {
997 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
998 sdl->sdl_index = ifp->if_index;
1006 if (domain_init_status >= 2)
1007 if_attachdomain1(ifp);
1009 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1010 if (IS_DEFAULT_VNET(curvnet))
1011 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
1013 /* Announce the interface. */
1014 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1018 if_epochalloc(void *dummy __unused)
1021 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
1023 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
1026 if_attachdomain(void *dummy)
1030 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
1031 if_attachdomain1(ifp);
1033 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
1034 if_attachdomain, NULL);
1037 if_attachdomain1(struct ifnet *ifp)
1042 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1043 * cannot lock ifp->if_afdata initialization, entirely.
1045 IF_AFDATA_LOCK(ifp);
1046 if (ifp->if_afdata_initialized >= domain_init_status) {
1047 IF_AFDATA_UNLOCK(ifp);
1048 log(LOG_WARNING, "%s called more than once on %s\n",
1049 __func__, ifp->if_xname);
1052 ifp->if_afdata_initialized = domain_init_status;
1053 IF_AFDATA_UNLOCK(ifp);
1055 /* address family dependent data region */
1056 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1057 for (dp = domains; dp; dp = dp->dom_next) {
1058 if (dp->dom_ifattach)
1059 ifp->if_afdata[dp->dom_family] =
1060 (*dp->dom_ifattach)(ifp);
1065 * Remove any unicast or broadcast network addresses from an interface.
1068 if_purgeaddrs(struct ifnet *ifp)
1073 struct epoch_tracker et;
1075 NET_EPOCH_ENTER(et);
1076 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1077 if (ifa->ifa_addr->sa_family != AF_LINK)
1085 /* XXX: Ugly!! ad hoc just for INET */
1086 if (ifa->ifa_addr->sa_family == AF_INET) {
1087 struct ifaliasreq ifr;
1089 bzero(&ifr, sizeof(ifr));
1090 ifr.ifra_addr = *ifa->ifa_addr;
1091 if (ifa->ifa_dstaddr)
1092 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1093 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1099 if (ifa->ifa_addr->sa_family == AF_INET6) {
1100 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1101 /* ifp_addrhead is already updated */
1106 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1107 IF_ADDR_WUNLOCK(ifp);
1113 * Remove any multicast network addresses from an interface when an ifnet
1117 if_purgemaddrs(struct ifnet *ifp)
1119 struct ifmultiaddr *ifma;
1122 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1123 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1124 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1125 if_delmulti_locked(ifp, ifma, 1);
1127 IF_ADDR_WUNLOCK(ifp);
1131 * Detach an interface, removing it from the list of "active" interfaces.
1132 * If vmove flag is set on entry to if_detach_internal(), perform only a
1133 * limited subset of cleanup tasks, given that we are moving an ifnet from
1134 * one vnet to another, where it must be fully operational.
1136 * XXXRW: There are some significant questions about event ordering, and
1137 * how to prevent things from starting to use the interface during detach.
1140 if_detach(struct ifnet *ifp)
1144 CURVNET_SET_QUIET(ifp->if_vnet);
1145 found = if_unlink_ifnet(ifp, false);
1147 sx_xlock(&ifnet_detach_sxlock);
1148 if_detach_internal(ifp, 0, NULL);
1149 sx_xunlock(&ifnet_detach_sxlock);
1155 * The vmove flag, if set, indicates that we are called from a callpath
1156 * that is moving an interface to a different vnet instance.
1158 * The shutdown flag, if set, indicates that we are called in the
1159 * process of shutting down a vnet instance. Currently only the
1160 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1161 * on a vnet instance shutdown without this flag being set, e.g., when
1162 * the cloned interfaces are destoyed as first thing of teardown.
1165 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1173 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1177 * At this point we know the interface still was on the ifnet list
1178 * and we removed it so we are in a stable state.
1180 epoch_wait_preempt(net_epoch_preempt);
1183 * Ensure all pending EPOCH(9) callbacks have been executed. This
1184 * fixes issues about late destruction of multicast options
1185 * which lead to leave group calls, which in turn access the
1186 * belonging ifnet structure:
1188 epoch_drain_callbacks(net_epoch_preempt);
1191 * In any case (destroy or vmove) detach us from the groups
1192 * and remove/wait for pending events on the taskq.
1193 * XXX-BZ in theory an interface could still enqueue a taskq change?
1197 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1198 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1201 * Check if this is a cloned interface or not. Must do even if
1202 * shutting down as a if_vmove_reclaim() would move the ifp and
1203 * the if_clone_addgroup() will have a corrupted string overwise
1204 * from a gibberish pointer.
1206 if (vmove && ifcp != NULL)
1207 *ifcp = if_clone_findifc(ifp);
1213 * On VNET shutdown abort here as the stack teardown will do all
1214 * the work top-down for us.
1217 /* Give interface users the chance to clean up. */
1218 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1221 * In case of a vmove we are done here without error.
1222 * If we would signal an error it would lead to the same
1223 * abort as if we did not find the ifnet anymore.
1224 * if_detach() calls us in void context and does not care
1225 * about an early abort notification, so life is splendid :)
1227 goto finish_vnet_shutdown;
1232 * At this point we are not tearing down a VNET and are either
1233 * going to destroy or vmove the interface and have to cleanup
1238 * Remove routes and flush queues.
1241 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1242 altq_disable(&ifp->if_snd);
1243 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1244 altq_detach(&ifp->if_snd);
1255 * Remove all IPv6 kernel structs related to ifp. This should be done
1256 * before removing routing entries below, since IPv6 interface direct
1257 * routes are expected to be removed by the IPv6-specific kernel API.
1258 * Otherwise, the kernel will detect some inconsistency and bark it.
1262 if_purgemaddrs(ifp);
1264 /* Announce that the interface is gone. */
1265 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1266 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1267 if (IS_DEFAULT_VNET(curvnet))
1268 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1272 * Prevent further calls into the device driver via ifnet.
1277 * Clean up all addresses.
1280 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1281 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1282 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1283 IF_ADDR_WUNLOCK(ifp);
1286 IF_ADDR_WUNLOCK(ifp);
1289 rt_flushifroutes(ifp);
1292 finish_vnet_shutdown:
1295 * We cannot hold the lock over dom_ifdetach calls as they might
1296 * sleep, for example trying to drain a callout, thus open up the
1297 * theoretical race with re-attaching.
1299 IF_AFDATA_LOCK(ifp);
1300 i = ifp->if_afdata_initialized;
1301 ifp->if_afdata_initialized = 0;
1302 IF_AFDATA_UNLOCK(ifp);
1303 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1304 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1305 (*dp->dom_ifdetach)(ifp,
1306 ifp->if_afdata[dp->dom_family]);
1307 ifp->if_afdata[dp->dom_family] = NULL;
1316 * if_vmove() performs a limited version of if_detach() in current
1317 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1318 * An attempt is made to shrink if_index in current vnet, find an
1319 * unused if_index in target vnet and calls if_grow() if necessary,
1320 * and finally find an unused if_xname for the target vnet.
1323 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1325 struct if_clone *ifc;
1327 u_int bif_dlt, bif_hdrlen;
1334 * if_detach_internal() will call the eventhandler to notify
1335 * interface departure. That will detach if_bpf. We need to
1336 * safe the dlt and hdrlen so we can re-attach it later.
1338 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1342 * Detach from current vnet, but preserve LLADDR info, do not
1343 * mark as dead etc. so that the ifnet can be reattached later.
1344 * If we cannot find it, we lost the race to someone else.
1346 rc = if_detach_internal(ifp, 1, &ifc);
1351 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1352 * the if_index for that vnet if possible.
1354 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1355 * or we'd lock on one vnet and unlock on another.
1358 ifindex_free_locked(ifp->if_index);
1362 * Perform interface-specific reassignment tasks, if provided by
1365 if (ifp->if_reassign != NULL)
1366 ifp->if_reassign(ifp, new_vnet, NULL);
1369 * Switch to the context of the target vnet.
1371 CURVNET_SET_QUIET(new_vnet);
1374 ifp->if_index = ifindex_alloc(&old);
1375 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1377 epoch_wait_preempt(net_epoch_preempt);
1381 ifnet_setbyindex(ifp->if_index, ifp);
1384 if_attach_internal(ifp, 1, ifc);
1387 if (ifp->if_bpf == NULL)
1388 bpfattach(ifp, bif_dlt, bif_hdrlen);
1396 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1399 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1407 /* Try to find the prison within our visibility. */
1408 sx_slock(&allprison_lock);
1409 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1410 sx_sunlock(&allprison_lock);
1413 prison_hold_locked(pr);
1414 mtx_unlock(&pr->pr_mtx);
1416 /* Do not try to move the iface from and to the same prison. */
1417 if (pr->pr_vnet == ifp->if_vnet) {
1422 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1423 /* XXX Lock interfaces to avoid races. */
1424 CURVNET_SET_QUIET(pr->pr_vnet);
1425 difp = ifunit(ifname);
1431 sx_xlock(&ifnet_detach_sxlock);
1433 /* Make sure the VNET is stable. */
1434 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1436 sx_xunlock(&ifnet_detach_sxlock);
1443 found = if_unlink_ifnet(ifp, true);
1445 sx_xunlock(&ifnet_detach_sxlock);
1451 /* Move the interface into the child jail/vnet. */
1452 error = if_vmove(ifp, pr->pr_vnet);
1454 /* Report the new if_xname back to the userland on success. */
1456 sprintf(ifname, "%s", ifp->if_xname);
1458 sx_xunlock(&ifnet_detach_sxlock);
1465 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1468 struct vnet *vnet_dst;
1473 /* Try to find the prison within our visibility. */
1474 sx_slock(&allprison_lock);
1475 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1476 sx_sunlock(&allprison_lock);
1479 prison_hold_locked(pr);
1480 mtx_unlock(&pr->pr_mtx);
1482 /* Make sure the named iface exists in the source prison/vnet. */
1483 CURVNET_SET(pr->pr_vnet);
1484 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1491 /* Do not try to move the iface from and to the same prison. */
1492 vnet_dst = TD_TO_VNET(td);
1493 if (vnet_dst == ifp->if_vnet) {
1499 /* Make sure the VNET is stable. */
1500 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1507 /* Get interface back from child jail/vnet. */
1508 found = if_unlink_ifnet(ifp, true);
1510 sx_xlock(&ifnet_detach_sxlock);
1511 error = if_vmove(ifp, vnet_dst);
1512 sx_xunlock(&ifnet_detach_sxlock);
1515 /* Report the new if_xname back to the userland on success. */
1517 sprintf(ifname, "%s", ifp->if_xname);
1525 * Add a group to an interface
1528 if_addgroup(struct ifnet *ifp, const char *groupname)
1530 struct ifg_list *ifgl;
1531 struct ifg_group *ifg = NULL;
1532 struct ifg_member *ifgm;
1535 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1536 groupname[strlen(groupname) - 1] <= '9')
1540 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1541 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1546 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1551 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1557 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1558 if (!strcmp(ifg->ifg_group, groupname))
1562 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1568 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1569 ifg->ifg_refcnt = 0;
1570 CK_STAILQ_INIT(&ifg->ifg_members);
1571 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1576 ifgl->ifgl_group = ifg;
1577 ifgm->ifgm_ifp = ifp;
1580 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1581 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1582 IF_ADDR_WUNLOCK(ifp);
1587 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1588 EVENTHANDLER_INVOKE(group_change_event, groupname);
1594 * Helper function to remove a group out of an interface. Expects the global
1595 * ifnet lock to be write-locked, and drops it before returning.
1598 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1599 const char *groupname)
1601 struct ifg_member *ifgm;
1604 IFNET_WLOCK_ASSERT();
1607 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1608 IF_ADDR_WUNLOCK(ifp);
1610 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1611 if (ifgm->ifgm_ifp == ifp) {
1612 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1613 ifg_member, ifgm_next);
1618 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1619 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1627 epoch_wait_preempt(net_epoch_preempt);
1629 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1630 free(ifgl->ifgl_group, M_TEMP);
1635 EVENTHANDLER_INVOKE(group_change_event, groupname);
1639 * Remove a group from an interface
1642 if_delgroup(struct ifnet *ifp, const char *groupname)
1644 struct ifg_list *ifgl;
1647 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1648 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1655 _if_delgroup_locked(ifp, ifgl, groupname);
1661 * Remove an interface from all groups
1664 if_delgroups(struct ifnet *ifp)
1666 struct ifg_list *ifgl;
1667 char groupname[IFNAMSIZ];
1670 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1671 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1672 _if_delgroup_locked(ifp, ifgl, groupname);
1679 ifgr_group_get(void *ifgrp)
1681 union ifgroupreq_union *ifgrup;
1684 #ifdef COMPAT_FREEBSD32
1685 if (SV_CURPROC_FLAG(SV_ILP32))
1686 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1688 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1691 static struct ifg_req *
1692 ifgr_groups_get(void *ifgrp)
1694 union ifgroupreq_union *ifgrup;
1697 #ifdef COMPAT_FREEBSD32
1698 if (SV_CURPROC_FLAG(SV_ILP32))
1699 return ((struct ifg_req *)(uintptr_t)
1700 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1702 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1706 * Stores all groups from an interface in memory pointed to by ifgr.
1709 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1712 struct ifg_list *ifgl;
1713 struct ifg_req ifgrq, *ifgp;
1717 if (ifgr->ifgr_len == 0) {
1718 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1719 ifgr->ifgr_len += sizeof(struct ifg_req);
1723 len = ifgr->ifgr_len;
1724 ifgp = ifgr_groups_get(ifgr);
1726 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1727 if (len < sizeof(ifgrq))
1729 bzero(&ifgrq, sizeof ifgrq);
1730 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1731 sizeof(ifgrq.ifgrq_group));
1732 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1734 len -= sizeof(ifgrq);
1742 * Stores all members of a group in memory pointed to by igfr
1745 if_getgroupmembers(struct ifgroupreq *ifgr)
1747 struct ifg_group *ifg;
1748 struct ifg_member *ifgm;
1749 struct ifg_req ifgrq, *ifgp;
1753 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1754 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1761 if (ifgr->ifgr_len == 0) {
1762 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1763 ifgr->ifgr_len += sizeof(ifgrq);
1768 len = ifgr->ifgr_len;
1769 ifgp = ifgr_groups_get(ifgr);
1770 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1771 if (len < sizeof(ifgrq)) {
1775 bzero(&ifgrq, sizeof ifgrq);
1776 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1777 sizeof(ifgrq.ifgrq_member));
1778 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1782 len -= sizeof(ifgrq);
1791 * Return counter values from counter(9)s stored in ifnet.
1794 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1797 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1799 return (counter_u64_fetch(ifp->if_counters[cnt]));
1803 * Increase an ifnet counter. Usually used for counters shared
1804 * between the stack and a driver, but function supports them all.
1807 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1810 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1812 counter_u64_add(ifp->if_counters[cnt], inc);
1816 * Copy data from ifnet to userland API structure if_data.
1819 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1822 ifd->ifi_type = ifp->if_type;
1823 ifd->ifi_physical = 0;
1824 ifd->ifi_addrlen = ifp->if_addrlen;
1825 ifd->ifi_hdrlen = ifp->if_hdrlen;
1826 ifd->ifi_link_state = ifp->if_link_state;
1828 ifd->ifi_datalen = sizeof(struct if_data);
1829 ifd->ifi_mtu = ifp->if_mtu;
1830 ifd->ifi_metric = ifp->if_metric;
1831 ifd->ifi_baudrate = ifp->if_baudrate;
1832 ifd->ifi_hwassist = ifp->if_hwassist;
1833 ifd->ifi_epoch = ifp->if_epoch;
1834 ifd->ifi_lastchange = ifp->if_lastchange;
1836 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1837 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1838 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1839 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1840 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1841 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1842 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1843 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1844 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1845 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1846 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1847 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1851 * Initialization, destruction and refcounting functions for ifaddrs.
1854 ifa_alloc(size_t size, int flags)
1858 KASSERT(size >= sizeof(struct ifaddr),
1859 ("%s: invalid size %zu", __func__, size));
1861 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1865 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1867 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1869 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1871 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1874 refcount_init(&ifa->ifa_refcnt, 1);
1879 /* free(NULL) is okay */
1880 counter_u64_free(ifa->ifa_opackets);
1881 counter_u64_free(ifa->ifa_ipackets);
1882 counter_u64_free(ifa->ifa_obytes);
1883 counter_u64_free(ifa->ifa_ibytes);
1884 free(ifa, M_IFADDR);
1890 ifa_ref(struct ifaddr *ifa)
1894 old = refcount_acquire(&ifa->ifa_refcnt);
1895 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1899 ifa_try_ref(struct ifaddr *ifa)
1903 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1907 ifa_destroy(epoch_context_t ctx)
1911 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1912 counter_u64_free(ifa->ifa_opackets);
1913 counter_u64_free(ifa->ifa_ipackets);
1914 counter_u64_free(ifa->ifa_obytes);
1915 counter_u64_free(ifa->ifa_ibytes);
1916 free(ifa, M_IFADDR);
1920 ifa_free(struct ifaddr *ifa)
1923 if (refcount_release(&ifa->ifa_refcnt))
1924 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1928 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1929 * structs used to represent other address families, it is necessary
1930 * to perform a different comparison.
1933 #define sa_dl_equal(a1, a2) \
1934 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1935 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1936 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1937 CLLADDR((const struct sockaddr_dl *)(a2)), \
1938 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1941 * Locate an interface based on a complete address.
1945 ifa_ifwithaddr(const struct sockaddr *addr)
1952 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1953 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1954 if (ifa->ifa_addr->sa_family != addr->sa_family)
1956 if (sa_equal(addr, ifa->ifa_addr)) {
1959 /* IP6 doesn't have broadcast */
1960 if ((ifp->if_flags & IFF_BROADCAST) &&
1961 ifa->ifa_broadaddr &&
1962 ifa->ifa_broadaddr->sa_len != 0 &&
1963 sa_equal(ifa->ifa_broadaddr, addr)) {
1974 ifa_ifwithaddr_check(const struct sockaddr *addr)
1976 struct epoch_tracker et;
1979 NET_EPOCH_ENTER(et);
1980 rc = (ifa_ifwithaddr(addr) != NULL);
1986 * Locate an interface based on the broadcast address.
1990 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1996 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1997 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1999 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2000 if (ifa->ifa_addr->sa_family != addr->sa_family)
2002 if ((ifp->if_flags & IFF_BROADCAST) &&
2003 ifa->ifa_broadaddr &&
2004 ifa->ifa_broadaddr->sa_len != 0 &&
2005 sa_equal(ifa->ifa_broadaddr, addr)) {
2016 * Locate the point to point interface with a given destination address.
2020 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2026 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2027 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2029 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2031 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2032 if (ifa->ifa_addr->sa_family != addr->sa_family)
2034 if (ifa->ifa_dstaddr != NULL &&
2035 sa_equal(addr, ifa->ifa_dstaddr)) {
2046 * Find an interface on a specific network. If many, choice
2047 * is most specific found.
2050 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2054 struct ifaddr *ifa_maybe = NULL;
2055 u_int af = addr->sa_family;
2056 const char *addr_data = addr->sa_data, *cplim;
2057 const struct sockaddr_dl *sdl;
2061 * AF_LINK addresses can be looked up directly by their index number,
2062 * so do that if we can.
2064 if (af == AF_LINK) {
2065 sdl = (const struct sockaddr_dl *)addr;
2066 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) {
2067 ifp = ifnet_byindex(sdl->sdl_index);
2071 return (ifp->if_addr);
2076 * Scan though each interface, looking for ones that have addresses
2077 * in this address family and the requested fib.
2079 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2080 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2082 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2083 const char *cp, *cp2, *cp3;
2085 if (ifa->ifa_addr->sa_family != af)
2087 if (af == AF_INET &&
2088 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2090 * This is a bit broken as it doesn't
2091 * take into account that the remote end may
2092 * be a single node in the network we are
2094 * The trouble is that we don't know the
2095 * netmask for the remote end.
2097 if (ifa->ifa_dstaddr != NULL &&
2098 sa_equal(addr, ifa->ifa_dstaddr)) {
2103 * Scan all the bits in the ifa's address.
2104 * If a bit dissagrees with what we are
2105 * looking for, mask it with the netmask
2106 * to see if it really matters.
2107 * (A byte at a time)
2109 if (ifa->ifa_netmask == 0)
2112 cp2 = ifa->ifa_addr->sa_data;
2113 cp3 = ifa->ifa_netmask->sa_data;
2114 cplim = ifa->ifa_netmask->sa_len
2115 + (char *)ifa->ifa_netmask;
2117 if ((*cp++ ^ *cp2++) & *cp3++)
2118 goto next; /* next address! */
2120 * If the netmask of what we just found
2121 * is more specific than what we had before
2122 * (if we had one), or if the virtual status
2123 * of new prefix is better than of the old one,
2124 * then remember the new one before continuing
2125 * to search for an even better one.
2127 if (ifa_maybe == NULL ||
2128 ifa_preferred(ifa_maybe, ifa) ||
2129 rn_refines((caddr_t)ifa->ifa_netmask,
2130 (caddr_t)ifa_maybe->ifa_netmask)) {
2143 * Find an interface address specific to an interface best matching
2147 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2150 const char *cp, *cp2, *cp3;
2152 struct ifaddr *ifa_maybe = NULL;
2153 u_int af = addr->sa_family;
2159 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2160 if (ifa->ifa_addr->sa_family != af)
2162 if (ifa_maybe == NULL)
2164 if (ifa->ifa_netmask == 0) {
2165 if (sa_equal(addr, ifa->ifa_addr) ||
2166 (ifa->ifa_dstaddr &&
2167 sa_equal(addr, ifa->ifa_dstaddr)))
2171 if (ifp->if_flags & IFF_POINTOPOINT) {
2172 if (sa_equal(addr, ifa->ifa_dstaddr))
2176 cp2 = ifa->ifa_addr->sa_data;
2177 cp3 = ifa->ifa_netmask->sa_data;
2178 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2179 for (; cp3 < cplim; cp3++)
2180 if ((*cp++ ^ *cp2++) & *cp3)
2192 * See whether new ifa is better than current one:
2193 * 1) A non-virtual one is preferred over virtual.
2194 * 2) A virtual in master state preferred over any other state.
2196 * Used in several address selecting functions.
2199 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2202 return (cur->ifa_carp && (!next->ifa_carp ||
2203 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2206 struct sockaddr_dl *
2207 link_alloc_sdl(size_t size, int flags)
2210 return (malloc(size, M_TEMP, flags));
2214 link_free_sdl(struct sockaddr *sa)
2220 * Fills in given sdl with interface basic info.
2221 * Returns pointer to filled sdl.
2223 struct sockaddr_dl *
2224 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2226 struct sockaddr_dl *sdl;
2228 sdl = (struct sockaddr_dl *)paddr;
2229 memset(sdl, 0, sizeof(struct sockaddr_dl));
2230 sdl->sdl_len = sizeof(struct sockaddr_dl);
2231 sdl->sdl_family = AF_LINK;
2232 sdl->sdl_index = ifp->if_index;
2233 sdl->sdl_type = iftype;
2239 * Mark an interface down and notify protocols of
2243 if_unroute(struct ifnet *ifp, int flag, int fam)
2246 struct epoch_tracker et;
2248 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2250 ifp->if_flags &= ~flag;
2251 getmicrotime(&ifp->if_lastchange);
2252 NET_EPOCH_ENTER(et);
2253 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2254 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2255 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2257 ifp->if_qflush(ifp);
2260 (*carp_linkstate_p)(ifp);
2265 * Mark an interface up and notify protocols of
2269 if_route(struct ifnet *ifp, int flag, int fam)
2272 struct epoch_tracker et;
2274 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2276 ifp->if_flags |= flag;
2277 getmicrotime(&ifp->if_lastchange);
2278 NET_EPOCH_ENTER(et);
2279 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2280 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2281 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2284 (*carp_linkstate_p)(ifp);
2291 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2292 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2293 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2294 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2295 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2296 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2297 int (*vlan_setcookie_p)(struct ifnet *, void *);
2298 void *(*vlan_cookie_p)(struct ifnet *);
2301 * Handle a change in the interface link state. To avoid LORs
2302 * between driver lock and upper layer locks, as well as possible
2303 * recursions, we post event to taskqueue, and all job
2304 * is done in static do_link_state_change().
2307 if_link_state_change(struct ifnet *ifp, int link_state)
2309 /* Return if state hasn't changed. */
2310 if (ifp->if_link_state == link_state)
2313 ifp->if_link_state = link_state;
2315 /* XXXGL: reference ifp? */
2316 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2320 do_link_state_change(void *arg, int pending)
2326 link_state = ifp->if_link_state;
2328 CURVNET_SET(ifp->if_vnet);
2330 if (ifp->if_vlantrunk != NULL)
2331 (*vlan_link_state_p)(ifp);
2333 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2334 ifp->if_l2com != NULL)
2335 (*ng_ether_link_state_p)(ifp, link_state);
2337 (*carp_linkstate_p)(ifp);
2339 ifp->if_bridge_linkstate(ifp);
2341 (*lagg_linkstate_p)(ifp, link_state);
2343 if (IS_DEFAULT_VNET(curvnet))
2344 devctl_notify("IFNET", ifp->if_xname,
2345 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2348 if_printf(ifp, "%d link states coalesced\n", pending);
2349 if (log_link_state_change)
2350 if_printf(ifp, "link state changed to %s\n",
2351 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2352 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2357 * Mark an interface down and notify protocols of
2361 if_down(struct ifnet *ifp)
2364 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2365 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2369 * Mark an interface up and notify protocols of
2373 if_up(struct ifnet *ifp)
2376 if_route(ifp, IFF_UP, AF_UNSPEC);
2377 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2381 * Flush an interface queue.
2384 if_qflush(struct ifnet *ifp)
2392 if (ALTQ_IS_ENABLED(ifq))
2396 while ((m = n) != NULL) {
2407 * Map interface name to interface structure pointer, with or without
2408 * returning a reference.
2411 ifunit_ref(const char *name)
2413 struct epoch_tracker et;
2416 NET_EPOCH_ENTER(et);
2417 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2418 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2419 !(ifp->if_flags & IFF_DYING))
2431 ifunit(const char *name)
2433 struct epoch_tracker et;
2436 NET_EPOCH_ENTER(et);
2437 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2438 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2446 ifr_buffer_get_buffer(void *data)
2448 union ifreq_union *ifrup;
2451 #ifdef COMPAT_FREEBSD32
2452 if (SV_CURPROC_FLAG(SV_ILP32))
2453 return ((void *)(uintptr_t)
2454 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2456 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2460 ifr_buffer_set_buffer_null(void *data)
2462 union ifreq_union *ifrup;
2465 #ifdef COMPAT_FREEBSD32
2466 if (SV_CURPROC_FLAG(SV_ILP32))
2467 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2470 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2474 ifr_buffer_get_length(void *data)
2476 union ifreq_union *ifrup;
2479 #ifdef COMPAT_FREEBSD32
2480 if (SV_CURPROC_FLAG(SV_ILP32))
2481 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2483 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2487 ifr_buffer_set_length(void *data, size_t len)
2489 union ifreq_union *ifrup;
2492 #ifdef COMPAT_FREEBSD32
2493 if (SV_CURPROC_FLAG(SV_ILP32))
2494 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2497 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2501 ifr_data_get_ptr(void *ifrp)
2503 union ifreq_union *ifrup;
2506 #ifdef COMPAT_FREEBSD32
2507 if (SV_CURPROC_FLAG(SV_ILP32))
2508 return ((void *)(uintptr_t)
2509 ifrup->ifr32.ifr_ifru.ifru_data);
2511 return (ifrup->ifr.ifr_ifru.ifru_data);
2515 * Hardware specific interface ioctls.
2518 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2521 int error = 0, do_ifup = 0;
2522 int new_flags, temp_flags;
2523 size_t namelen, onamelen;
2525 char *descrbuf, *odescrbuf;
2526 char new_name[IFNAMSIZ];
2528 struct sockaddr_dl *sdl;
2530 ifr = (struct ifreq *)data;
2533 ifr->ifr_index = ifp->if_index;
2537 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2538 ifr->ifr_flags = temp_flags & 0xffff;
2539 ifr->ifr_flagshigh = temp_flags >> 16;
2543 ifr->ifr_reqcap = ifp->if_capabilities;
2544 ifr->ifr_curcap = ifp->if_capenable;
2551 /* Ensure uninitialised padding is not leaked. */
2552 memset(&ifd, 0, sizeof(ifd));
2554 if_data_copy(ifp, &ifd);
2555 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2561 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2566 ifr->ifr_metric = ifp->if_metric;
2570 ifr->ifr_mtu = ifp->if_mtu;
2574 /* XXXGL: did this ever worked? */
2580 sx_slock(&ifdescr_sx);
2581 if (ifp->if_description == NULL)
2584 /* space for terminating nul */
2585 descrlen = strlen(ifp->if_description) + 1;
2586 if (ifr_buffer_get_length(ifr) < descrlen)
2587 ifr_buffer_set_buffer_null(ifr);
2589 error = copyout(ifp->if_description,
2590 ifr_buffer_get_buffer(ifr), descrlen);
2591 ifr_buffer_set_length(ifr, descrlen);
2593 sx_sunlock(&ifdescr_sx);
2597 error = priv_check(td, PRIV_NET_SETIFDESCR);
2602 * Copy only (length-1) bytes to make sure that
2603 * if_description is always nul terminated. The
2604 * length parameter is supposed to count the
2605 * terminating nul in.
2607 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2608 return (ENAMETOOLONG);
2609 else if (ifr_buffer_get_length(ifr) == 0)
2612 descrbuf = malloc(ifr_buffer_get_length(ifr),
2613 M_IFDESCR, M_WAITOK | M_ZERO);
2614 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2615 ifr_buffer_get_length(ifr) - 1);
2617 free(descrbuf, M_IFDESCR);
2622 sx_xlock(&ifdescr_sx);
2623 odescrbuf = ifp->if_description;
2624 ifp->if_description = descrbuf;
2625 sx_xunlock(&ifdescr_sx);
2627 getmicrotime(&ifp->if_lastchange);
2628 free(odescrbuf, M_IFDESCR);
2632 ifr->ifr_fib = ifp->if_fib;
2636 error = priv_check(td, PRIV_NET_SETIFFIB);
2639 if (ifr->ifr_fib >= rt_numfibs)
2642 ifp->if_fib = ifr->ifr_fib;
2646 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2650 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2651 * check, so we don't need special handling here yet.
2653 new_flags = (ifr->ifr_flags & 0xffff) |
2654 (ifr->ifr_flagshigh << 16);
2655 if (ifp->if_flags & IFF_UP &&
2656 (new_flags & IFF_UP) == 0) {
2658 } else if (new_flags & IFF_UP &&
2659 (ifp->if_flags & IFF_UP) == 0) {
2662 /* See if permanently promiscuous mode bit is about to flip */
2663 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2664 if (new_flags & IFF_PPROMISC)
2665 ifp->if_flags |= IFF_PROMISC;
2666 else if (ifp->if_pcount == 0)
2667 ifp->if_flags &= ~IFF_PROMISC;
2668 if (log_promisc_mode_change)
2669 if_printf(ifp, "permanently promiscuous mode %s\n",
2670 ((new_flags & IFF_PPROMISC) ?
2671 "enabled" : "disabled"));
2673 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2674 (new_flags &~ IFF_CANTCHANGE);
2675 if (ifp->if_ioctl) {
2676 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2680 getmicrotime(&ifp->if_lastchange);
2684 error = priv_check(td, PRIV_NET_SETIFCAP);
2687 if (ifp->if_ioctl == NULL)
2688 return (EOPNOTSUPP);
2689 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2691 error = (*ifp->if_ioctl)(ifp, cmd, data);
2693 getmicrotime(&ifp->if_lastchange);
2698 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2703 error = priv_check(td, PRIV_NET_SETIFNAME);
2706 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2710 if (new_name[0] == '\0')
2712 if (strcmp(new_name, ifp->if_xname) == 0)
2714 if (ifunit(new_name) != NULL)
2718 * XXX: Locking. Nothing else seems to lock if_flags,
2719 * and there are numerous other races with the
2720 * ifunit() checks not being atomic with namespace
2721 * changes (renames, vmoves, if_attach, etc).
2723 ifp->if_flags |= IFF_RENAMING;
2725 /* Announce the departure of the interface. */
2726 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2727 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2729 if_printf(ifp, "changing name to '%s'\n", new_name);
2732 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2734 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2735 namelen = strlen(new_name);
2736 onamelen = sdl->sdl_nlen;
2738 * Move the address if needed. This is safe because we
2739 * allocate space for a name of length IFNAMSIZ when we
2740 * create this in if_attach().
2742 if (namelen != onamelen) {
2743 bcopy(sdl->sdl_data + onamelen,
2744 sdl->sdl_data + namelen, sdl->sdl_alen);
2746 bcopy(new_name, sdl->sdl_data, namelen);
2747 sdl->sdl_nlen = namelen;
2748 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2749 bzero(sdl->sdl_data, onamelen);
2750 while (namelen != 0)
2751 sdl->sdl_data[--namelen] = 0xff;
2752 IF_ADDR_WUNLOCK(ifp);
2754 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2755 /* Announce the return of the interface. */
2756 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2758 ifp->if_flags &= ~IFF_RENAMING;
2763 error = priv_check(td, PRIV_NET_SETIFVNET);
2766 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2771 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2774 ifp->if_metric = ifr->ifr_metric;
2775 getmicrotime(&ifp->if_lastchange);
2779 error = priv_check(td, PRIV_NET_SETIFPHYS);
2782 if (ifp->if_ioctl == NULL)
2783 return (EOPNOTSUPP);
2784 error = (*ifp->if_ioctl)(ifp, cmd, data);
2786 getmicrotime(&ifp->if_lastchange);
2791 u_long oldmtu = ifp->if_mtu;
2793 error = priv_check(td, PRIV_NET_SETIFMTU);
2796 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2798 if (ifp->if_ioctl == NULL)
2799 return (EOPNOTSUPP);
2800 error = (*ifp->if_ioctl)(ifp, cmd, data);
2802 getmicrotime(&ifp->if_lastchange);
2805 DEBUGNET_NOTIFY_MTU(ifp);
2809 * If the link MTU changed, do network layer specific procedure.
2811 if (ifp->if_mtu != oldmtu) {
2822 if (cmd == SIOCADDMULTI)
2823 error = priv_check(td, PRIV_NET_ADDMULTI);
2825 error = priv_check(td, PRIV_NET_DELMULTI);
2829 /* Don't allow group membership on non-multicast interfaces. */
2830 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2831 return (EOPNOTSUPP);
2833 /* Don't let users screw up protocols' entries. */
2834 if (ifr->ifr_addr.sa_family != AF_LINK)
2837 if (cmd == SIOCADDMULTI) {
2838 struct epoch_tracker et;
2839 struct ifmultiaddr *ifma;
2842 * Userland is only permitted to join groups once
2843 * via the if_addmulti() KPI, because it cannot hold
2844 * struct ifmultiaddr * between calls. It may also
2845 * lose a race while we check if the membership
2848 NET_EPOCH_ENTER(et);
2849 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2854 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2856 error = if_delmulti(ifp, &ifr->ifr_addr);
2859 getmicrotime(&ifp->if_lastchange);
2862 case SIOCSIFPHYADDR:
2863 case SIOCDIFPHYADDR:
2865 case SIOCSIFPHYADDR_IN6:
2868 case SIOCSIFGENERIC:
2869 error = priv_check(td, PRIV_NET_HWIOCTL);
2872 if (ifp->if_ioctl == NULL)
2873 return (EOPNOTSUPP);
2874 error = (*ifp->if_ioctl)(ifp, cmd, data);
2876 getmicrotime(&ifp->if_lastchange);
2880 case SIOCGIFPSRCADDR:
2881 case SIOCGIFPDSTADDR:
2884 case SIOCGIFGENERIC:
2886 case SIOCGIFRSSHASH:
2887 case SIOCGIFDOWNREASON:
2888 if (ifp->if_ioctl == NULL)
2889 return (EOPNOTSUPP);
2890 error = (*ifp->if_ioctl)(ifp, cmd, data);
2894 error = priv_check(td, PRIV_NET_SETLLADDR);
2897 error = if_setlladdr(ifp,
2898 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2902 error = if_gethwaddr(ifp, ifr);
2905 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2906 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2909 if ((error = if_addgroup(ifp,
2910 ifgr_group_get((struct ifgroupreq *)data))))
2914 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2916 struct epoch_tracker et;
2918 NET_EPOCH_ENTER(et);
2919 error = if_getgroup((struct ifgroupreq *)data, ifp);
2924 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2925 error = priv_check(td, PRIV_NET_DELIFGROUP);
2928 if ((error = if_delgroup(ifp,
2929 ifgr_group_get((struct ifgroupreq *)data))))
2940 #ifdef COMPAT_FREEBSD32
2948 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2954 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2956 #ifdef COMPAT_FREEBSD32
2960 struct ifmediareq ifmr;
2964 struct ifconf32 *ifc32;
2965 struct ifdrv32 *ifd32;
2966 struct ifmediareq32 *ifmr32;
2976 CURVNET_SET(so->so_vnet);
2978 /* Make sure the VNET is stable. */
2979 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2986 #ifdef COMPAT_FREEBSD32
2991 ifc32 = (struct ifconf32 *)data;
2992 thunk.ifc.ifc_len = ifc32->ifc_len;
2993 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2994 data = (caddr_t)&thunk.ifc;
2997 case SIOCGDRVSPEC32:
2998 case SIOCSDRVSPEC32:
2999 ifd32 = (struct ifdrv32 *)data;
3000 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
3001 sizeof(thunk.ifd.ifd_name));
3002 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
3003 thunk.ifd.ifd_len = ifd32->ifd_len;
3004 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
3005 data = (caddr_t)&thunk.ifd;
3006 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
3008 case SIOCGIFMEDIA32:
3009 case SIOCGIFXMEDIA32:
3010 ifmr32 = (struct ifmediareq32 *)data;
3011 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
3012 sizeof(thunk.ifmr.ifm_name));
3013 thunk.ifmr.ifm_current = ifmr32->ifm_current;
3014 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
3015 thunk.ifmr.ifm_status = ifmr32->ifm_status;
3016 thunk.ifmr.ifm_active = ifmr32->ifm_active;
3017 thunk.ifmr.ifm_count = ifmr32->ifm_count;
3018 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
3019 data = (caddr_t)&thunk.ifmr;
3020 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3027 error = ifconf(cmd, data);
3031 ifr = (struct ifreq *)data;
3035 error = priv_check(td, PRIV_NET_SETIFVNET);
3037 error = if_vmove_reclaim(td, ifr->ifr_name,
3043 error = priv_check(td, PRIV_NET_IFCREATE);
3045 error = if_clone_create(ifr->ifr_name,
3046 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3047 ifr_data_get_ptr(ifr) : NULL);
3050 error = priv_check(td, PRIV_NET_IFDESTROY);
3053 sx_xlock(&ifnet_detach_sxlock);
3054 error = if_clone_destroy(ifr->ifr_name);
3055 sx_xunlock(&ifnet_detach_sxlock);
3059 case SIOCIFGCLONERS:
3060 error = if_clone_list((struct if_clonereq *)data);
3063 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3064 error = if_getgroupmembers((struct ifgroupreq *)data);
3067 #if defined(INET) || defined(INET6)
3070 if (carp_ioctl_p == NULL)
3071 error = EPROTONOSUPPORT;
3073 error = (*carp_ioctl_p)(ifr, cmd, td);
3078 ifp = ifunit_ref(ifr->ifr_name);
3084 error = ifhwioctl(cmd, ifp, data, td);
3085 if (error != ENOIOCTL)
3088 oif_flags = ifp->if_flags;
3089 if (so->so_proto == NULL) {
3095 * Pass the request on to the socket control method, and if the
3096 * latter returns EOPNOTSUPP, directly to the interface.
3098 * Make an exception for the legacy SIOCSIF* requests. Drivers
3099 * trust SIOCSIFADDR et al to come from an already privileged
3100 * layer, and do not perform any credentials checks or input
3103 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3105 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3106 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3107 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3108 error = (*ifp->if_ioctl)(ifp, cmd, data);
3110 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3112 if (ifp->if_flags & IFF_UP)
3121 #ifdef COMPAT_FREEBSD32
3124 switch (saved_cmd) {
3126 ifc32->ifc_len = thunk.ifc.ifc_len;
3128 case SIOCGDRVSPEC32:
3130 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3131 * the struct so just assert that ifd_len (the only
3132 * field it might make sense to update) hasn't
3135 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3136 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3137 thunk.ifd.ifd_len));
3139 case SIOCGIFMEDIA32:
3140 case SIOCGIFXMEDIA32:
3141 ifmr32->ifm_current = thunk.ifmr.ifm_current;
3142 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3143 ifmr32->ifm_status = thunk.ifmr.ifm_status;
3144 ifmr32->ifm_active = thunk.ifmr.ifm_active;
3145 ifmr32->ifm_count = thunk.ifmr.ifm_count;
3153 * The code common to handling reference counted flags,
3154 * e.g., in ifpromisc() and if_allmulti().
3155 * The "pflag" argument can specify a permanent mode flag to check,
3156 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3158 * Only to be used on stack-owned flags, not driver-owned flags.
3161 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3165 int oldflags, oldcount;
3167 /* Sanity checks to catch programming errors */
3168 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3169 ("%s: setting driver-owned flag %d", __func__, flag));
3172 KASSERT(*refcount >= 0,
3173 ("%s: increment negative refcount %d for flag %d",
3174 __func__, *refcount, flag));
3176 KASSERT(*refcount > 0,
3177 ("%s: decrement non-positive refcount %d for flag %d",
3178 __func__, *refcount, flag));
3180 /* In case this mode is permanent, just touch refcount */
3181 if (ifp->if_flags & pflag) {
3182 *refcount += onswitch ? 1 : -1;
3186 /* Save ifnet parameters for if_ioctl() may fail */
3187 oldcount = *refcount;
3188 oldflags = ifp->if_flags;
3191 * See if we aren't the only and touching refcount is enough.
3192 * Actually toggle interface flag if we are the first or last.
3197 ifp->if_flags |= flag;
3201 ifp->if_flags &= ~flag;
3204 /* Call down the driver since we've changed interface flags */
3205 if (ifp->if_ioctl == NULL) {
3209 ifr.ifr_flags = ifp->if_flags & 0xffff;
3210 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3211 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3214 /* Notify userland that interface flags have changed */
3219 /* Recover after driver error */
3220 *refcount = oldcount;
3221 ifp->if_flags = oldflags;
3226 * Set/clear promiscuous mode on interface ifp based on the truth value
3227 * of pswitch. The calls are reference counted so that only the first
3228 * "on" request actually has an effect, as does the final "off" request.
3229 * Results are undefined if the "off" and "on" requests are not matched.
3232 ifpromisc(struct ifnet *ifp, int pswitch)
3235 int oldflags = ifp->if_flags;
3237 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3238 &ifp->if_pcount, pswitch);
3239 /* If promiscuous mode status has changed, log a message */
3240 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3241 log_promisc_mode_change)
3242 if_printf(ifp, "promiscuous mode %s\n",
3243 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3248 * Return interface configuration
3249 * of system. List may be used
3250 * in later ioctl's (above) to get
3251 * other information.
3255 ifconf(u_long cmd, caddr_t data)
3257 struct ifconf *ifc = (struct ifconf *)data;
3262 int error, full = 0, valid_len, max_len;
3264 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3265 max_len = maxphys - 1;
3267 /* Prevent hostile input from being able to crash the system */
3268 if (ifc->ifc_len <= 0)
3272 if (ifc->ifc_len <= max_len) {
3273 max_len = ifc->ifc_len;
3276 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3281 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3282 struct epoch_tracker et;
3286 * Zero the ifr to make sure we don't disclose the contents
3289 memset(&ifr, 0, sizeof(ifr));
3291 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3292 >= sizeof(ifr.ifr_name)) {
3295 return (ENAMETOOLONG);
3299 NET_EPOCH_ENTER(et);
3300 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3301 struct sockaddr *sa = ifa->ifa_addr;
3303 if (prison_if(curthread->td_ucred, sa) != 0)
3306 if (sa->sa_len <= sizeof(*sa)) {
3307 if (sa->sa_len < sizeof(*sa)) {
3308 memset(&ifr.ifr_ifru.ifru_addr, 0,
3309 sizeof(ifr.ifr_ifru.ifru_addr));
3310 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3313 ifr.ifr_ifru.ifru_addr = *sa;
3314 sbuf_bcat(sb, &ifr, sizeof(ifr));
3315 max_len += sizeof(ifr);
3318 offsetof(struct ifreq, ifr_addr));
3319 max_len += offsetof(struct ifreq, ifr_addr);
3320 sbuf_bcat(sb, sa, sa->sa_len);
3321 max_len += sa->sa_len;
3324 if (sbuf_error(sb) == 0)
3325 valid_len = sbuf_len(sb);
3329 sbuf_bcat(sb, &ifr, sizeof(ifr));
3330 max_len += sizeof(ifr);
3332 if (sbuf_error(sb) == 0)
3333 valid_len = sbuf_len(sb);
3339 * If we didn't allocate enough space (uncommon), try again. If
3340 * we have already allocated as much space as we are allowed,
3341 * return what we've got.
3343 if (valid_len != max_len && !full) {
3348 ifc->ifc_len = valid_len;
3350 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3356 * Just like ifpromisc(), but for all-multicast-reception mode.
3359 if_allmulti(struct ifnet *ifp, int onswitch)
3362 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3365 struct ifmultiaddr *
3366 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3368 struct ifmultiaddr *ifma;
3370 IF_ADDR_LOCK_ASSERT(ifp);
3372 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3373 if (sa->sa_family == AF_LINK) {
3374 if (sa_dl_equal(ifma->ifma_addr, sa))
3377 if (sa_equal(ifma->ifma_addr, sa))
3386 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3387 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3388 * the ifnet multicast address list here, so the caller must do that and
3389 * other setup work (such as notifying the device driver). The reference
3390 * count is initialized to 1.
3392 static struct ifmultiaddr *
3393 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3396 struct ifmultiaddr *ifma;
3397 struct sockaddr *dupsa;
3399 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3404 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3405 if (dupsa == NULL) {
3406 free(ifma, M_IFMADDR);
3409 bcopy(sa, dupsa, sa->sa_len);
3410 ifma->ifma_addr = dupsa;
3412 ifma->ifma_ifp = ifp;
3413 ifma->ifma_refcount = 1;
3414 ifma->ifma_protospec = NULL;
3417 ifma->ifma_lladdr = NULL;
3421 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3422 if (dupsa == NULL) {
3423 free(ifma->ifma_addr, M_IFMADDR);
3424 free(ifma, M_IFMADDR);
3427 bcopy(llsa, dupsa, llsa->sa_len);
3428 ifma->ifma_lladdr = dupsa;
3434 * if_freemulti: free ifmultiaddr structure and possibly attached related
3435 * addresses. The caller is responsible for implementing reference
3436 * counting, notifying the driver, handling routing messages, and releasing
3437 * any dependent link layer state.
3439 #ifdef MCAST_VERBOSE
3440 extern void kdb_backtrace(void);
3443 if_freemulti_internal(struct ifmultiaddr *ifma)
3446 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3447 ifma->ifma_refcount));
3449 if (ifma->ifma_lladdr != NULL)
3450 free(ifma->ifma_lladdr, M_IFMADDR);
3451 #ifdef MCAST_VERBOSE
3453 printf("%s freeing ifma: %p\n", __func__, ifma);
3455 free(ifma->ifma_addr, M_IFMADDR);
3456 free(ifma, M_IFMADDR);
3460 if_destroymulti(epoch_context_t ctx)
3462 struct ifmultiaddr *ifma;
3464 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3465 if_freemulti_internal(ifma);
3469 if_freemulti(struct ifmultiaddr *ifma)
3471 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3472 ifma->ifma_refcount));
3474 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3478 * Register an additional multicast address with a network interface.
3480 * - If the address is already present, bump the reference count on the
3481 * address and return.
3482 * - If the address is not link-layer, look up a link layer address.
3483 * - Allocate address structures for one or both addresses, and attach to the
3484 * multicast address list on the interface. If automatically adding a link
3485 * layer address, the protocol address will own a reference to the link
3486 * layer address, to be freed when it is freed.
3487 * - Notify the network device driver of an addition to the multicast address
3490 * 'sa' points to caller-owned memory with the desired multicast address.
3492 * 'retifma' will be used to return a pointer to the resulting multicast
3493 * address reference, if desired.
3496 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3497 struct ifmultiaddr **retifma)
3499 struct ifmultiaddr *ifma, *ll_ifma;
3500 struct sockaddr *llsa;
3501 struct sockaddr_dl sdl;
3505 IN_MULTI_LIST_UNLOCK_ASSERT();
3508 IN6_MULTI_LIST_UNLOCK_ASSERT();
3511 * If the address is already present, return a new reference to it;
3512 * otherwise, allocate storage and set up a new address.
3515 ifma = if_findmulti(ifp, sa);
3517 ifma->ifma_refcount++;
3518 if (retifma != NULL)
3520 IF_ADDR_WUNLOCK(ifp);
3525 * The address isn't already present; resolve the protocol address
3526 * into a link layer address, and then look that up, bump its
3527 * refcount or allocate an ifma for that also.
3528 * Most link layer resolving functions returns address data which
3529 * fits inside default sockaddr_dl structure. However callback
3530 * can allocate another sockaddr structure, in that case we need to
3535 if (ifp->if_resolvemulti != NULL) {
3536 /* Provide called function with buffer size information */
3537 sdl.sdl_len = sizeof(sdl);
3538 llsa = (struct sockaddr *)&sdl;
3539 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3545 * Allocate the new address. Don't hook it up yet, as we may also
3546 * need to allocate a link layer multicast address.
3548 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3555 * If a link layer address is found, we'll need to see if it's
3556 * already present in the address list, or allocate is as well.
3557 * When this block finishes, the link layer address will be on the
3561 ll_ifma = if_findmulti(ifp, llsa);
3562 if (ll_ifma == NULL) {
3563 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3564 if (ll_ifma == NULL) {
3565 --ifma->ifma_refcount;
3570 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3571 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3574 ll_ifma->ifma_refcount++;
3575 ifma->ifma_llifma = ll_ifma;
3579 * We now have a new multicast address, ifma, and possibly a new or
3580 * referenced link layer address. Add the primary address to the
3581 * ifnet address list.
3583 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3584 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3586 if (retifma != NULL)
3590 * Must generate the message while holding the lock so that 'ifma'
3591 * pointer is still valid.
3593 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3594 IF_ADDR_WUNLOCK(ifp);
3597 * We are certain we have added something, so call down to the
3598 * interface to let them know about it.
3600 if (ifp->if_ioctl != NULL) {
3601 if (THREAD_CAN_SLEEP())
3602 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3604 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3607 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3608 link_free_sdl(llsa);
3613 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3614 link_free_sdl(llsa);
3617 IF_ADDR_WUNLOCK(ifp);
3622 if_siocaddmulti(void *arg, int pending)
3629 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3631 CURVNET_SET(ifp->if_vnet);
3632 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3637 * Delete a multicast group membership by network-layer group address.
3639 * Returns ENOENT if the entry could not be found. If ifp no longer
3640 * exists, results are undefined. This entry point should only be used
3641 * from subsystems which do appropriate locking to hold ifp for the
3642 * duration of the call.
3643 * Network-layer protocol domains must use if_delmulti_ifma().
3646 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3648 struct ifmultiaddr *ifma;
3651 KASSERT(ifp, ("%s: NULL ifp", __func__));
3655 ifma = if_findmulti(ifp, sa);
3657 lastref = if_delmulti_locked(ifp, ifma, 0);
3658 IF_ADDR_WUNLOCK(ifp);
3663 if (lastref && ifp->if_ioctl != NULL) {
3664 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3671 * Delete all multicast group membership for an interface.
3672 * Should be used to quickly flush all multicast filters.
3675 if_delallmulti(struct ifnet *ifp)
3677 struct ifmultiaddr *ifma;
3678 struct ifmultiaddr *next;
3681 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3682 if_delmulti_locked(ifp, ifma, 0);
3683 IF_ADDR_WUNLOCK(ifp);
3687 if_delmulti_ifma(struct ifmultiaddr *ifma)
3689 if_delmulti_ifma_flags(ifma, 0);
3693 * Delete a multicast group membership by group membership pointer.
3694 * Network-layer protocol domains must use this routine.
3696 * It is safe to call this routine if the ifp disappeared.
3699 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3703 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3705 IN_MULTI_LIST_UNLOCK_ASSERT();
3707 ifp = ifma->ifma_ifp;
3710 printf("%s: ifma_ifp seems to be detached\n", __func__);
3712 struct epoch_tracker et;
3715 NET_EPOCH_ENTER(et);
3716 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3725 * If and only if the ifnet instance exists: Acquire the address lock.
3730 lastref = if_delmulti_locked(ifp, ifma, flags);
3734 * If and only if the ifnet instance exists:
3735 * Release the address lock.
3736 * If the group was left: update the hardware hash filter.
3738 IF_ADDR_WUNLOCK(ifp);
3739 if (lastref && ifp->if_ioctl != NULL) {
3740 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3746 * Perform deletion of network-layer and/or link-layer multicast address.
3748 * Return 0 if the reference count was decremented.
3749 * Return 1 if the final reference was released, indicating that the
3750 * hardware hash filter should be reprogrammed.
3753 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3755 struct ifmultiaddr *ll_ifma;
3757 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3758 KASSERT(ifma->ifma_ifp == ifp,
3759 ("%s: inconsistent ifp %p", __func__, ifp));
3760 IF_ADDR_WLOCK_ASSERT(ifp);
3763 ifp = ifma->ifma_ifp;
3764 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3767 * If the ifnet is detaching, null out references to ifnet,
3768 * so that upper protocol layers will notice, and not attempt
3769 * to obtain locks for an ifnet which no longer exists. The
3770 * routing socket announcement must happen before the ifnet
3771 * instance is detached from the system.
3775 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3778 * ifp may already be nulled out if we are being reentered
3779 * to delete the ll_ifma.
3782 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3783 ifma->ifma_ifp = NULL;
3787 if (--ifma->ifma_refcount > 0)
3790 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3791 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3792 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3795 * If this ifma is a network-layer ifma, a link-layer ifma may
3796 * have been associated with it. Release it first if so.
3798 ll_ifma = ifma->ifma_llifma;
3799 if (ll_ifma != NULL) {
3800 KASSERT(ifma->ifma_lladdr != NULL,
3801 ("%s: llifma w/o lladdr", __func__));
3803 ll_ifma->ifma_ifp = NULL; /* XXX */
3804 if (--ll_ifma->ifma_refcount == 0) {
3806 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3807 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3809 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3812 if_freemulti(ll_ifma);
3817 struct ifmultiaddr *ifmatmp;
3819 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3820 MPASS(ifma != ifmatmp);
3825 * The last reference to this instance of struct ifmultiaddr
3826 * was released; the hardware should be notified of this change.
3832 * Set the link layer address on an interface.
3834 * At this time we only support certain types of interfaces,
3835 * and we don't allow the length of the address to change.
3837 * Set noinline to be dtrace-friendly
3840 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3842 struct sockaddr_dl *sdl;
3850 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3854 if (len != sdl->sdl_alen) /* don't allow length to change */
3857 switch (ifp->if_type) {
3862 case IFT_IEEE8023ADLAG:
3863 bcopy(lladdr, LLADDR(sdl), len);
3870 * If the interface is already up, we need
3871 * to re-init it in order to reprogram its
3874 if ((ifp->if_flags & IFF_UP) != 0) {
3875 if (ifp->if_ioctl) {
3876 ifp->if_flags &= ~IFF_UP;
3877 ifr.ifr_flags = ifp->if_flags & 0xffff;
3878 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3879 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3880 ifp->if_flags |= IFF_UP;
3881 ifr.ifr_flags = ifp->if_flags & 0xffff;
3882 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3883 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3886 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3892 * Compat function for handling basic encapsulation requests.
3893 * Not converted stacks (FDDI, IB, ..) supports traditional
3894 * output model: ARP (and other similar L2 protocols) are handled
3895 * inside output routine, arpresolve/nd6_resolve() returns MAC
3896 * address instead of full prepend.
3898 * This function creates calculated header==MAC for IPv4/IPv6 and
3899 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3903 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3906 if (req->rtype != IFENCAP_LL)
3907 return (EOPNOTSUPP);
3909 if (req->bufsize < req->lladdr_len)
3912 switch (req->family) {
3917 return (EAFNOSUPPORT);
3920 /* Copy lladdr to storage as is */
3921 memmove(req->buf, req->lladdr, req->lladdr_len);
3922 req->bufsize = req->lladdr_len;
3923 req->lladdr_off = 0;
3929 * Tunnel interfaces can nest, also they may cause infinite recursion
3930 * calls when misconfigured. We'll prevent this by detecting loops.
3931 * High nesting level may cause stack exhaustion. We'll prevent this
3932 * by introducing upper limit.
3934 * Return 0, if tunnel nesting count is equal or less than limit.
3937 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3945 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3946 if (*(struct ifnet **)(mtag + 1) == ifp) {
3947 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3952 if (count > limit) {
3954 "%s: if_output recursively called too many times(%d)\n",
3955 if_name(ifp), count);
3958 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3961 *(struct ifnet **)(mtag + 1) = ifp;
3962 m_tag_prepend(m, mtag);
3967 * Get the link layer address that was read from the hardware at attach.
3969 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3970 * their component interfaces as IFT_IEEE8023ADLAG.
3973 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3976 if (ifp->if_hw_addr == NULL)
3979 switch (ifp->if_type) {
3981 case IFT_IEEE8023ADLAG:
3982 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3990 * The name argument must be a pointer to storage which will last as
3991 * long as the interface does. For physical devices, the result of
3992 * device_get_name(dev) is a good choice and for pseudo-devices a
3993 * static string works well.
3996 if_initname(struct ifnet *ifp, const char *name, int unit)
3998 ifp->if_dname = name;
3999 ifp->if_dunit = unit;
4000 if (unit != IF_DUNIT_NONE)
4001 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4003 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4007 if_printf(struct ifnet *ifp, const char *fmt, ...)
4012 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4014 vlog(LOG_INFO, if_fmt, ap);
4020 if_start(struct ifnet *ifp)
4023 (*(ifp)->if_start)(ifp);
4027 * Backwards compatibility interface for drivers
4028 * that have not implemented it
4031 if_transmit(struct ifnet *ifp, struct mbuf *m)
4035 IFQ_HANDOFF(ifp, m, error);
4040 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4047 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4052 if (_IF_QFULL(ifq)) {
4054 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4059 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4060 if (m->m_flags & (M_BCAST|M_MCAST))
4061 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4062 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4064 _IF_ENQUEUE(ifq, m);
4066 if (ifp != NULL && !active)
4067 (*(ifp)->if_start)(ifp);
4072 if_register_com_alloc(u_char type,
4073 if_com_alloc_t *a, if_com_free_t *f)
4076 KASSERT(if_com_alloc[type] == NULL,
4077 ("if_register_com_alloc: %d already registered", type));
4078 KASSERT(if_com_free[type] == NULL,
4079 ("if_register_com_alloc: %d free already registered", type));
4081 if_com_alloc[type] = a;
4082 if_com_free[type] = f;
4086 if_deregister_com_alloc(u_char type)
4089 KASSERT(if_com_alloc[type] != NULL,
4090 ("if_deregister_com_alloc: %d not registered", type));
4091 KASSERT(if_com_free[type] != NULL,
4092 ("if_deregister_com_alloc: %d free not registered", type));
4095 * Ensure all pending EPOCH(9) callbacks have been executed. This
4096 * fixes issues about late invocation of if_destroy(), which leads
4097 * to memory leak from if_com_alloc[type] allocated if_l2com.
4099 epoch_drain_callbacks(net_epoch_preempt);
4101 if_com_alloc[type] = NULL;
4102 if_com_free[type] = NULL;
4105 /* API for driver access to network stack owned ifnet.*/
4107 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4111 oldbrate = ifp->if_baudrate;
4112 ifp->if_baudrate = baudrate;
4117 if_getbaudrate(if_t ifp)
4120 return (((struct ifnet *)ifp)->if_baudrate);
4124 if_setcapabilities(if_t ifp, int capabilities)
4126 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4131 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4133 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4134 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4140 if_getcapabilities(if_t ifp)
4142 return ((struct ifnet *)ifp)->if_capabilities;
4146 if_setcapenable(if_t ifp, int capabilities)
4148 ((struct ifnet *)ifp)->if_capenable = capabilities;
4153 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4156 ((struct ifnet *)ifp)->if_capenable |= setcap;
4158 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4164 if_getdname(if_t ifp)
4166 return ((struct ifnet *)ifp)->if_dname;
4170 if_togglecapenable(if_t ifp, int togglecap)
4172 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4177 if_getcapenable(if_t ifp)
4179 return ((struct ifnet *)ifp)->if_capenable;
4183 * This is largely undesirable because it ties ifnet to a device, but does
4184 * provide flexiblity for an embedded product vendor. Should be used with
4185 * the understanding that it violates the interface boundaries, and should be
4186 * a last resort only.
4189 if_setdev(if_t ifp, void *dev)
4195 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4197 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4198 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4204 if_getdrvflags(if_t ifp)
4206 return ((struct ifnet *)ifp)->if_drv_flags;
4210 if_setdrvflags(if_t ifp, int flags)
4212 ((struct ifnet *)ifp)->if_drv_flags = flags;
4217 if_setflags(if_t ifp, int flags)
4220 ifp->if_flags = flags;
4225 if_setflagbits(if_t ifp, int set, int clear)
4227 ((struct ifnet *)ifp)->if_flags |= set;
4228 ((struct ifnet *)ifp)->if_flags &= ~clear;
4234 if_getflags(if_t ifp)
4236 return ((struct ifnet *)ifp)->if_flags;
4240 if_clearhwassist(if_t ifp)
4242 ((struct ifnet *)ifp)->if_hwassist = 0;
4247 if_sethwassistbits(if_t ifp, int toset, int toclear)
4249 ((struct ifnet *)ifp)->if_hwassist |= toset;
4250 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4256 if_sethwassist(if_t ifp, int hwassist_bit)
4258 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4263 if_gethwassist(if_t ifp)
4265 return ((struct ifnet *)ifp)->if_hwassist;
4269 if_setmtu(if_t ifp, int mtu)
4271 ((struct ifnet *)ifp)->if_mtu = mtu;
4278 return ((struct ifnet *)ifp)->if_mtu;
4282 if_getmtu_family(if_t ifp, int family)
4286 for (dp = domains; dp; dp = dp->dom_next) {
4287 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4288 return (dp->dom_ifmtu((struct ifnet *)ifp));
4291 return (((struct ifnet *)ifp)->if_mtu);
4295 * Methods for drivers to access interface unicast and multicast
4296 * link level addresses. Driver shall not know 'struct ifaddr' neither
4297 * 'struct ifmultiaddr'.
4300 if_lladdr_count(if_t ifp)
4302 struct epoch_tracker et;
4307 NET_EPOCH_ENTER(et);
4308 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4309 if (ifa->ifa_addr->sa_family == AF_LINK)
4317 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4319 struct epoch_tracker et;
4326 NET_EPOCH_ENTER(et);
4327 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4328 if (ifa->ifa_addr->sa_family != AF_LINK)
4330 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4339 if_llmaddr_count(if_t ifp)
4341 struct epoch_tracker et;
4342 struct ifmultiaddr *ifma;
4346 NET_EPOCH_ENTER(et);
4347 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4348 if (ifma->ifma_addr->sa_family == AF_LINK)
4356 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4358 struct epoch_tracker et;
4359 struct ifmultiaddr *ifma;
4365 NET_EPOCH_ENTER(et);
4366 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4367 if (ifma->ifma_addr->sa_family != AF_LINK)
4369 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4378 if_setsoftc(if_t ifp, void *softc)
4380 ((struct ifnet *)ifp)->if_softc = softc;
4385 if_getsoftc(if_t ifp)
4387 return ((struct ifnet *)ifp)->if_softc;
4391 if_setrcvif(struct mbuf *m, if_t ifp)
4394 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4395 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4399 if_setvtag(struct mbuf *m, uint16_t tag)
4401 m->m_pkthdr.ether_vtag = tag;
4405 if_getvtag(struct mbuf *m)
4408 return (m->m_pkthdr.ether_vtag);
4412 if_sendq_empty(if_t ifp)
4414 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4418 if_getifaddr(if_t ifp)
4420 return ((struct ifnet *)ifp)->if_addr;
4424 if_getamcount(if_t ifp)
4426 return ((struct ifnet *)ifp)->if_amcount;
4430 if_setsendqready(if_t ifp)
4432 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4437 if_setsendqlen(if_t ifp, int tx_desc_count)
4439 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4440 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4446 if_vlantrunkinuse(if_t ifp)
4448 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4452 if_input(if_t ifp, struct mbuf* sendmp)
4454 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4460 if_dequeue(if_t ifp)
4463 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4469 if_sendq_prepend(if_t ifp, struct mbuf *m)
4471 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4476 if_setifheaderlen(if_t ifp, int len)
4478 ((struct ifnet *)ifp)->if_hdrlen = len;
4483 if_getlladdr(if_t ifp)
4485 return (IF_LLADDR((struct ifnet *)ifp));
4489 if_gethandle(u_char type)
4491 return (if_alloc(type));
4495 if_bpfmtap(if_t ifh, struct mbuf *m)
4497 struct ifnet *ifp = (struct ifnet *)ifh;
4503 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4505 struct ifnet *ifp = (struct ifnet *)ifh;
4507 ETHER_BPF_MTAP(ifp, m);
4511 if_vlancap(if_t ifh)
4513 struct ifnet *ifp = (struct ifnet *)ifh;
4514 VLAN_CAPABILITIES(ifp);
4518 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4521 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4526 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4529 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4534 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4537 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4542 if_gethwtsomax(if_t ifp)
4545 return (((struct ifnet *)ifp)->if_hw_tsomax);
4549 if_gethwtsomaxsegcount(if_t ifp)
4552 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4556 if_gethwtsomaxsegsize(if_t ifp)
4559 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4563 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4565 ((struct ifnet *)ifp)->if_init = init_fn;
4569 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4571 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4575 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4577 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4581 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4583 ((struct ifnet *)ifp)->if_transmit = start_fn;
4586 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4588 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4593 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4596 ifp->if_get_counter = fn;
4599 /* Revisit these - These are inline functions originally. */
4601 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4603 return drbr_inuse(ifh, br);
4607 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4609 return drbr_dequeue(ifh, br);
4613 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4615 return drbr_needs_enqueue(ifh, br);
4619 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4621 return drbr_enqueue(ifh, br, m);