2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/capsicum.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>
59 #include <sys/rwlock.h>
60 #include <sys/sockio.h>
61 #include <sys/syslog.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/taskqueue.h>
65 #include <sys/domain.h>
73 #include <machine/stdarg.h>
77 #include <net/ethernet.h>
79 #include <net/if_arp.h>
80 #include <net/if_clone.h>
81 #include <net/if_dl.h>
82 #include <net/if_strings.h>
83 #include <net/if_types.h>
84 #include <net/if_var.h>
85 #include <net/if_media.h>
86 #include <net/if_mib.h>
87 #include <net/if_private.h>
88 #include <net/if_vlan_var.h>
89 #include <net/radix.h>
90 #include <net/route.h>
91 #include <net/route/route_ctl.h>
94 #if defined(INET) || defined(INET6)
95 #include <net/ethernet.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_carp.h>
101 #include <net/debugnet.h>
102 #include <netinet/if_ether.h>
105 #include <netinet6/in6_var.h>
106 #include <netinet6/in6_ifattach.h>
108 #endif /* INET || INET6 */
110 #include <security/mac/mac_framework.h>
113 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
114 * and ifr_ifru when it is used in SIOCGIFCONF.
116 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
117 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
119 __read_mostly epoch_t net_epoch_preempt;
120 #ifdef COMPAT_FREEBSD32
121 #include <sys/mount.h>
122 #include <compat/freebsd32/freebsd32.h>
124 struct ifreq_buffer32 {
125 uint32_t length; /* (size_t) */
126 uint32_t buffer; /* (void *) */
130 * Interface request structure used for socket
131 * ioctl's. All interface ioctl's must have parameter
132 * definitions which begin with ifr_name. The
133 * remainder may be interface specific.
136 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
138 struct sockaddr ifru_addr;
139 struct sockaddr ifru_dstaddr;
140 struct sockaddr ifru_broadaddr;
141 struct ifreq_buffer32 ifru_buffer;
152 u_char ifru_vlan_pcp;
155 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
156 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
157 __offsetof(struct ifreq32, ifr_ifru));
166 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
169 char ifd_name[IFNAMSIZ];
174 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
175 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
177 struct ifgroupreq32 {
178 char ifgr_name[IFNAMSIZ];
181 char ifgru_group[IFNAMSIZ];
182 uint32_t ifgru_groups;
185 #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
186 #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
187 #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
188 #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
190 struct ifmediareq32 {
191 char ifm_name[IFNAMSIZ];
197 uint32_t ifm_ulist; /* (int *) */
199 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
200 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
201 #endif /* COMPAT_FREEBSD32 */
205 #ifdef COMPAT_FREEBSD32
206 struct ifreq32 ifr32;
210 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
212 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
213 "Generic link-management");
215 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
216 &ifqmaxlen, 0, "max send queue size");
218 /* Log link state change events */
219 static int log_link_state_change = 1;
221 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
222 &log_link_state_change, 0,
223 "log interface link state change events");
225 /* Log promiscuous mode change events */
226 static int log_promisc_mode_change = 1;
228 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
229 &log_promisc_mode_change, 1,
230 "log promiscuous mode change events");
232 /* Interface description */
233 static unsigned int ifdescr_maxlen = 1024;
234 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
236 "administrative maximum length for interface description");
238 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
240 /* global sx for non-critical path ifdescr */
241 static struct sx ifdescr_sx;
242 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
244 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
245 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
246 /* These are external hooks for CARP. */
247 void (*carp_linkstate_p)(struct ifnet *ifp);
248 void (*carp_demote_adj_p)(int, char *);
249 int (*carp_master_p)(struct ifaddr *);
250 #if defined(INET) || defined(INET6)
251 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
252 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
253 const struct sockaddr *sa);
254 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
255 int (*carp_attach_p)(struct ifaddr *, int);
256 void (*carp_detach_p)(struct ifaddr *, bool);
259 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
262 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
263 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
264 const struct in6_addr *taddr);
267 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
270 * XXX: Style; these should be sorted alphabetically, and unprototyped
271 * static functions should be prototyped. Currently they are sorted by
274 static void if_attachdomain(void *);
275 static void if_attachdomain1(struct ifnet *);
276 static int ifconf(u_long, caddr_t);
277 static void if_input_default(struct ifnet *, struct mbuf *);
278 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
279 static int if_setflag(struct ifnet *, int, int, int *, int);
280 static int if_transmit_default(struct ifnet *ifp, struct mbuf *m);
281 static void if_unroute(struct ifnet *, int flag, int fam);
282 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
283 static void do_link_state_change(void *, int);
284 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
285 static int if_getgroupmembers(struct ifgroupreq *);
286 static void if_delgroups(struct ifnet *);
287 static void if_attach_internal(struct ifnet *, bool);
288 static int if_detach_internal(struct ifnet *, bool);
289 static void if_siocaddmulti(void *, int);
290 static void if_link_ifnet(struct ifnet *);
291 static bool if_unlink_ifnet(struct ifnet *, bool);
293 static int if_vmove(struct ifnet *, struct vnet *);
298 * XXX: declare here to avoid to include many inet6 related files..
299 * should be more generalized?
301 extern void nd6_setmtu(struct ifnet *);
304 /* ipsec helper hooks */
305 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
306 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
308 int ifqmaxlen = IFQ_MAXLEN;
309 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
310 VNET_DEFINE(struct ifgrouphead, ifg_head);
312 /* Table of ifnet by index. */
314 static int if_indexlim = 8;
315 static struct ifindex_entry {
316 struct ifnet *ife_ifnet;
320 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
321 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
322 "Variables global to all interfaces");
324 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
329 for (int i = 1; i <= if_index; i++)
330 if (ifindex_table[i].ife_ifnet != NULL &&
331 ifindex_table[i].ife_ifnet->if_vnet == curvnet)
335 return (sysctl_handle_int(oidp, &rv, 0, req));
337 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
338 CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
339 "Maximum known interface index");
342 * The global network interface list (V_ifnet) and related state (such as
343 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
344 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
346 struct sx ifnet_sxlock;
347 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
349 struct sx ifnet_detach_sxlock;
350 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
354 #define VNET_IS_SHUTTING_DOWN(_vnet) \
355 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
358 static if_com_alloc_t *if_com_alloc[256];
359 static if_com_free_t *if_com_free[256];
361 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
362 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
363 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
366 ifnet_byindex(u_int idx)
372 if (__predict_false(idx > if_index))
375 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
377 if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
384 ifnet_byindex_ref(u_int idx)
388 ifp = ifnet_byindex(idx);
389 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
391 if (!if_try_ref(ifp))
397 ifnet_byindexgen(uint16_t idx, uint16_t gen)
403 if (__predict_false(idx > if_index))
406 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
408 if (ifindex_table[idx].ife_gencnt == gen)
415 * Network interface utility routines.
417 * Routines with ifa_ifwith* names take sockaddr *'s as
422 if_init_idxtable(void *arg __unused)
425 ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
426 M_IFNET, M_WAITOK | M_ZERO);
428 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
431 vnet_if_init(const void *unused __unused)
434 CK_STAILQ_INIT(&V_ifnet);
435 CK_STAILQ_INIT(&V_ifg_head);
436 vnet_if_clone_init();
438 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
442 if_link_ifnet(struct ifnet *ifp)
446 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
448 curvnet->vnet_ifcnt++;
454 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
460 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
462 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
464 ifp->if_flags |= IFF_DYING;
469 curvnet->vnet_ifcnt--;
478 vnet_if_return(const void *unused __unused)
480 struct ifnet *ifp, *nifp;
481 struct ifnet **pending;
482 int found __diagused;
488 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
489 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
490 * if_detach_internal(), which waits for NET_EPOCH callbacks to
491 * complete. We can't do that from within NET_EPOCH.
493 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
494 * read/write lock. We cannot hold the lock as we call if_vmove()
495 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
500 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
501 M_IFNET, M_WAITOK | M_ZERO);
503 /* Return all inherited interfaces to their parent vnets. */
504 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
505 if (ifp->if_home_vnet != ifp->if_vnet) {
506 found = if_unlink_ifnet(ifp, true);
514 for (int j = 0; j < i; j++) {
515 sx_xlock(&ifnet_detach_sxlock);
516 if_vmove(pending[j], pending[j]->if_home_vnet);
517 sx_xunlock(&ifnet_detach_sxlock);
520 free(pending, M_IFNET);
522 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
523 vnet_if_return, NULL);
527 * Allocate a struct ifnet and an index for an interface. A layer 2
528 * common structure will also be allocated if an allocation routine is
529 * registered for the passed type.
531 static struct ifnet *
532 if_alloc_domain(u_char type, int numa_domain)
537 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
538 if (numa_domain == IF_NODOM)
539 ifp = malloc(sizeof(struct ifnet), M_IFNET,
542 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
543 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
545 ifp->if_alloctype = type;
546 ifp->if_numa_domain = numa_domain;
548 ifp->if_vnet = curvnet;
550 if (if_com_alloc[type] != NULL) {
551 ifp->if_l2com = if_com_alloc[type](type, ifp);
552 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
556 IF_ADDR_LOCK_INIT(ifp);
557 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
558 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
559 ifp->if_afdata_initialized = 0;
560 IF_AFDATA_LOCK_INIT(ifp);
561 CK_STAILQ_INIT(&ifp->if_addrhead);
562 CK_STAILQ_INIT(&ifp->if_multiaddrs);
563 CK_STAILQ_INIT(&ifp->if_groups);
567 ifq_init(&ifp->if_snd, ifp);
569 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
570 for (int i = 0; i < IFCOUNTERS; i++)
571 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
572 ifp->if_get_counter = if_get_counter_default;
573 ifp->if_pcp = IFNET_PCP_NONE;
575 /* Allocate an ifindex array entry. */
578 * Try to find an empty slot below if_index. If we fail, take the
581 for (idx = 1; idx <= if_index; idx++) {
582 if (ifindex_table[idx].ife_ifnet == NULL)
586 /* Catch if_index overflow. */
587 if (idx >= if_indexlim) {
588 struct ifindex_entry *new, *old;
591 newlim = if_indexlim * 2;
592 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
593 memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
595 ck_pr_store_ptr(&ifindex_table, new);
596 if_indexlim = newlim;
597 epoch_wait_preempt(net_epoch_preempt);
604 ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
605 ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
612 if_alloc_dev(u_char type, device_t dev)
616 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
617 return (if_alloc_domain(type, IF_NODOM));
618 return (if_alloc_domain(type, numa_domain));
622 if_alloc(u_char type)
625 return (if_alloc_domain(type, IF_NODOM));
628 * Do the actual work of freeing a struct ifnet, and layer 2 common
629 * structure. This call is made when the network epoch guarantees
630 * us that nobody holds a pointer to the interface.
633 if_free_deferred(epoch_context_t ctx)
635 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
637 KASSERT((ifp->if_flags & IFF_DYING),
638 ("%s: interface not dying", __func__));
640 if (if_com_free[ifp->if_alloctype] != NULL)
641 if_com_free[ifp->if_alloctype](ifp->if_l2com,
645 mac_ifnet_destroy(ifp);
647 IF_AFDATA_DESTROY(ifp);
648 IF_ADDR_LOCK_DESTROY(ifp);
649 ifq_delete(&ifp->if_snd);
651 for (int i = 0; i < IFCOUNTERS; i++)
652 counter_u64_free(ifp->if_counters[i]);
654 if_freedescr(ifp->if_description);
655 free(ifp->if_hw_addr, M_IFADDR);
660 * Deregister an interface and free the associated storage.
663 if_free(struct ifnet *ifp)
666 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
669 * XXXGL: An interface index is really an alias to ifp pointer.
670 * Why would we clear the alias now, and not in the deferred
671 * context? Indeed there is nothing wrong with some network
672 * thread obtaining ifp via ifnet_byindex() inside the network
673 * epoch and then dereferencing ifp while we perform if_free(),
674 * and after if_free() finished, too.
676 * This early index freeing was important back when ifindex was
677 * virtualized and interface would outlive the vnet.
680 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
681 ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
682 ifindex_table[ifp->if_index].ife_gencnt++;
683 while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
687 if (refcount_release(&ifp->if_refcount))
688 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
692 * Interfaces to keep an ifnet type-stable despite the possibility of the
693 * driver calling if_free(). If there are additional references, we defer
694 * freeing the underlying data structure.
697 if_ref(struct ifnet *ifp)
699 u_int old __diagused;
701 /* We don't assert the ifnet list lock here, but arguably should. */
702 old = refcount_acquire(&ifp->if_refcount);
703 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
707 if_try_ref(struct ifnet *ifp)
710 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
714 if_rele(struct ifnet *ifp)
717 if (!refcount_release(&ifp->if_refcount))
719 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
723 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
726 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
728 if (ifq->ifq_maxlen == 0)
729 ifq->ifq_maxlen = ifqmaxlen;
732 ifq->altq_disc = NULL;
733 ifq->altq_flags &= ALTQF_CANTCHANGE;
734 ifq->altq_tbr = NULL;
739 ifq_delete(struct ifaltq *ifq)
741 mtx_destroy(&ifq->ifq_mtx);
745 * Perform generic interface initialization tasks and attach the interface
746 * to the list of "active" interfaces. If vmove flag is set on entry
747 * to if_attach_internal(), perform only a limited subset of initialization
748 * tasks, given that we are moving from one vnet to another an ifnet which
749 * has already been fully initialized.
751 * Note that if_detach_internal() removes group membership unconditionally
752 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
753 * Thus, when if_vmove() is applied to a cloned interface, group membership
754 * is lost while a cloned one always joins a group whose name is
755 * ifc->ifc_name. To recover this after if_detach_internal() and
756 * if_attach_internal(), the cloner should be specified to
757 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
758 * attempts to join a group whose name is ifc->ifc_name.
761 * - The decision to return void and thus require this function to
762 * succeed is questionable.
763 * - We should probably do more sanity checking. For instance we don't
764 * do anything to insure if_xname is unique or non-empty.
767 if_attach(struct ifnet *ifp)
770 if_attach_internal(ifp, false);
774 * Compute the least common TSO limit.
777 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
780 * 1) If there is no limit currently, take the limit from
781 * the network adapter.
783 * 2) If the network adapter has a limit below the current
786 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
787 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
788 pmax->tsomaxbytes = ifp->if_hw_tsomax;
790 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
791 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
792 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
794 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
795 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
796 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
801 * Update TSO limit of a network adapter.
803 * Returns zero if no change. Else non-zero.
806 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
809 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
810 ifp->if_hw_tsomax = pmax->tsomaxbytes;
813 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
814 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
817 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
818 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
825 if_attach_internal(struct ifnet *ifp, bool vmove)
827 unsigned socksize, ifasize;
828 int namelen, masklen;
829 struct sockaddr_dl *sdl;
832 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
835 ifp->if_vnet = curvnet;
836 if (ifp->if_home_vnet == NULL)
837 ifp->if_home_vnet = curvnet;
840 if_addgroup(ifp, IFG_ALL);
843 /* Restore group membership for cloned interface. */
845 if_clone_restoregroup(ifp);
848 getmicrotime(&ifp->if_lastchange);
849 ifp->if_epoch = time_uptime;
851 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
852 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
853 ("transmit and qflush must both either be set or both be NULL"));
854 if (ifp->if_transmit == NULL) {
855 ifp->if_transmit = if_transmit_default;
856 ifp->if_qflush = if_qflush;
858 if (ifp->if_input == NULL)
859 ifp->if_input = if_input_default;
861 if (ifp->if_requestencap == NULL)
862 ifp->if_requestencap = if_requestencap_default;
866 mac_ifnet_create(ifp);
870 * Create a Link Level name for this device.
872 namelen = strlen(ifp->if_xname);
874 * Always save enough space for any possiable name so we
875 * can do a rename in place later.
877 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
878 socksize = masklen + ifp->if_addrlen;
879 if (socksize < sizeof(*sdl))
880 socksize = sizeof(*sdl);
881 socksize = roundup2(socksize, sizeof(long));
882 ifasize = sizeof(*ifa) + 2 * socksize;
883 ifa = ifa_alloc(ifasize, M_WAITOK);
884 sdl = (struct sockaddr_dl *)(ifa + 1);
885 sdl->sdl_len = socksize;
886 sdl->sdl_family = AF_LINK;
887 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
888 sdl->sdl_nlen = namelen;
889 sdl->sdl_index = ifp->if_index;
890 sdl->sdl_type = ifp->if_type;
893 ifa->ifa_addr = (struct sockaddr *)sdl;
894 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
895 ifa->ifa_netmask = (struct sockaddr *)sdl;
896 sdl->sdl_len = masklen;
898 sdl->sdl_data[--namelen] = 0xff;
899 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
900 /* Reliably crash if used uninitialized. */
901 ifp->if_broadcastaddr = NULL;
903 if (ifp->if_type == IFT_ETHER) {
904 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
908 #if defined(INET) || defined(INET6)
909 /* Use defaults for TSO, if nothing is set */
910 if (ifp->if_hw_tsomax == 0 &&
911 ifp->if_hw_tsomaxsegcount == 0 &&
912 ifp->if_hw_tsomaxsegsize == 0) {
914 * The TSO defaults needs to be such that an
915 * NFS mbuf list of 35 mbufs totalling just
916 * below 64K works and that a chain of mbufs
917 * can be defragged into at most 32 segments:
919 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
920 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
921 ifp->if_hw_tsomaxsegcount = 35;
922 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
924 /* XXX some drivers set IFCAP_TSO after ethernet attach */
925 if (ifp->if_capabilities & IFCAP_TSO) {
926 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
928 ifp->if_hw_tsomaxsegcount,
929 ifp->if_hw_tsomaxsegsize);
937 * Update the interface index in the link layer address
940 for (ifa = ifp->if_addr; ifa != NULL;
941 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
942 if (ifa->ifa_addr->sa_family == AF_LINK) {
943 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
944 sdl->sdl_index = ifp->if_index;
952 if (domain_init_status >= 2)
953 if_attachdomain1(ifp);
955 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
956 if (IS_DEFAULT_VNET(curvnet))
957 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
961 if_epochalloc(void *dummy __unused)
964 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
966 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
969 if_attachdomain(void *dummy)
973 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
974 if_attachdomain1(ifp);
976 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
977 if_attachdomain, NULL);
980 if_attachdomain1(struct ifnet *ifp)
985 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
986 * cannot lock ifp->if_afdata initialization, entirely.
989 if (ifp->if_afdata_initialized >= domain_init_status) {
990 IF_AFDATA_UNLOCK(ifp);
991 log(LOG_WARNING, "%s called more than once on %s\n",
992 __func__, ifp->if_xname);
995 ifp->if_afdata_initialized = domain_init_status;
996 IF_AFDATA_UNLOCK(ifp);
998 /* address family dependent data region */
999 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1000 SLIST_FOREACH(dp, &domains, dom_next) {
1001 if (dp->dom_ifattach)
1002 ifp->if_afdata[dp->dom_family] =
1003 (*dp->dom_ifattach)(ifp);
1008 * Remove any unicast or broadcast network addresses from an interface.
1011 if_purgeaddrs(struct ifnet *ifp)
1017 * Need to leave multicast addresses of proxy NDP llentries
1018 * before in6_purgeifaddr() because the llentries are keys
1019 * for in6_multi objects of proxy NDP entries.
1020 * in6_purgeifaddr()s clean up llentries including proxy NDPs
1021 * then we would lose the keys if they are called earlier.
1023 in6_purge_proxy_ndp(ifp);
1026 struct epoch_tracker et;
1028 NET_EPOCH_ENTER(et);
1029 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1030 if (ifa->ifa_addr->sa_family != AF_LINK)
1038 /* XXX: Ugly!! ad hoc just for INET */
1039 if (ifa->ifa_addr->sa_family == AF_INET) {
1040 struct ifaliasreq ifr;
1042 bzero(&ifr, sizeof(ifr));
1043 ifr.ifra_addr = *ifa->ifa_addr;
1044 if (ifa->ifa_dstaddr)
1045 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1046 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1052 if (ifa->ifa_addr->sa_family == AF_INET6) {
1053 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1054 /* ifp_addrhead is already updated */
1059 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1060 IF_ADDR_WUNLOCK(ifp);
1066 * Remove any multicast network addresses from an interface when an ifnet
1070 if_purgemaddrs(struct ifnet *ifp)
1072 struct ifmultiaddr *ifma;
1075 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1076 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1077 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1078 if_delmulti_locked(ifp, ifma, 1);
1080 IF_ADDR_WUNLOCK(ifp);
1084 * Detach an interface, removing it from the list of "active" interfaces.
1085 * If vmove flag is set on entry to if_detach_internal(), perform only a
1086 * limited subset of cleanup tasks, given that we are moving an ifnet from
1087 * one vnet to another, where it must be fully operational.
1089 * XXXRW: There are some significant questions about event ordering, and
1090 * how to prevent things from starting to use the interface during detach.
1093 if_detach(struct ifnet *ifp)
1097 CURVNET_SET_QUIET(ifp->if_vnet);
1098 found = if_unlink_ifnet(ifp, false);
1100 sx_xlock(&ifnet_detach_sxlock);
1101 if_detach_internal(ifp, false);
1102 sx_xunlock(&ifnet_detach_sxlock);
1108 * The vmove flag, if set, indicates that we are called from a callpath
1109 * that is moving an interface to a different vnet instance.
1111 * The shutdown flag, if set, indicates that we are called in the
1112 * process of shutting down a vnet instance. Currently only the
1113 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1114 * on a vnet instance shutdown without this flag being set, e.g., when
1115 * the cloned interfaces are destoyed as first thing of teardown.
1118 if_detach_internal(struct ifnet *ifp, bool vmove)
1126 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1129 sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
1132 * At this point we know the interface still was on the ifnet list
1133 * and we removed it so we are in a stable state.
1135 epoch_wait_preempt(net_epoch_preempt);
1138 * Ensure all pending EPOCH(9) callbacks have been executed. This
1139 * fixes issues about late destruction of multicast options
1140 * which lead to leave group calls, which in turn access the
1141 * belonging ifnet structure:
1143 NET_EPOCH_DRAIN_CALLBACKS();
1146 * In any case (destroy or vmove) detach us from the groups
1147 * and remove/wait for pending events on the taskq.
1148 * XXX-BZ in theory an interface could still enqueue a taskq change?
1152 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1153 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1159 * On VNET shutdown abort here as the stack teardown will do all
1160 * the work top-down for us.
1163 /* Give interface users the chance to clean up. */
1164 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1167 * In case of a vmove we are done here without error.
1168 * If we would signal an error it would lead to the same
1169 * abort as if we did not find the ifnet anymore.
1170 * if_detach() calls us in void context and does not care
1171 * about an early abort notification, so life is splendid :)
1173 goto finish_vnet_shutdown;
1178 * At this point we are not tearing down a VNET and are either
1179 * going to destroy or vmove the interface and have to cleanup
1184 * Remove routes and flush queues.
1187 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1188 altq_disable(&ifp->if_snd);
1189 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1190 altq_detach(&ifp->if_snd);
1201 * Remove all IPv6 kernel structs related to ifp. This should be done
1202 * before removing routing entries below, since IPv6 interface direct
1203 * routes are expected to be removed by the IPv6-specific kernel API.
1204 * Otherwise, the kernel will detect some inconsistency and bark it.
1208 if_purgemaddrs(ifp);
1210 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1211 if (IS_DEFAULT_VNET(curvnet))
1212 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1216 * Prevent further calls into the device driver via ifnet.
1221 * Clean up all addresses.
1224 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1225 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1226 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1227 IF_ADDR_WUNLOCK(ifp);
1230 IF_ADDR_WUNLOCK(ifp);
1233 rt_flushifroutes(ifp);
1236 finish_vnet_shutdown:
1239 * We cannot hold the lock over dom_ifdetach calls as they might
1240 * sleep, for example trying to drain a callout, thus open up the
1241 * theoretical race with re-attaching.
1243 IF_AFDATA_LOCK(ifp);
1244 i = ifp->if_afdata_initialized;
1245 ifp->if_afdata_initialized = 0;
1246 IF_AFDATA_UNLOCK(ifp);
1249 SLIST_FOREACH(dp, &domains, dom_next) {
1250 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1251 (*dp->dom_ifdetach)(ifp,
1252 ifp->if_afdata[dp->dom_family]);
1253 ifp->if_afdata[dp->dom_family] = NULL;
1262 * if_vmove() performs a limited version of if_detach() in current
1263 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1266 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1269 u_int bif_dlt, bif_hdrlen;
1275 * if_detach_internal() will call the eventhandler to notify
1276 * interface departure. That will detach if_bpf. We need to
1277 * safe the dlt and hdrlen so we can re-attach it later.
1279 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1283 * Detach from current vnet, but preserve LLADDR info, do not
1284 * mark as dead etc. so that the ifnet can be reattached later.
1285 * If we cannot find it, we lost the race to someone else.
1287 rc = if_detach_internal(ifp, true);
1292 * Perform interface-specific reassignment tasks, if provided by
1295 if (ifp->if_reassign != NULL)
1296 ifp->if_reassign(ifp, new_vnet, NULL);
1299 * Switch to the context of the target vnet.
1301 CURVNET_SET_QUIET(new_vnet);
1302 if_attach_internal(ifp, true);
1305 if (ifp->if_bpf == NULL)
1306 bpfattach(ifp, bif_dlt, bif_hdrlen);
1314 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1317 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1322 bool found __diagused;
1325 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
1327 /* Try to find the prison within our visibility. */
1328 sx_slock(&allprison_lock);
1329 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1330 sx_sunlock(&allprison_lock);
1333 prison_hold_locked(pr);
1334 mtx_unlock(&pr->pr_mtx);
1336 /* Do not try to move the iface from and to the same prison. */
1337 if (pr->pr_vnet == ifp->if_vnet) {
1342 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1343 /* XXX Lock interfaces to avoid races. */
1344 CURVNET_SET_QUIET(pr->pr_vnet);
1345 difp = ifunit(ifname);
1351 sx_xlock(&ifnet_detach_sxlock);
1353 /* Make sure the VNET is stable. */
1354 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1356 sx_xunlock(&ifnet_detach_sxlock);
1363 found = if_unlink_ifnet(ifp, true);
1365 sx_xunlock(&ifnet_detach_sxlock);
1371 /* Move the interface into the child jail/vnet. */
1372 error = if_vmove(ifp, pr->pr_vnet);
1374 /* Report the new if_xname back to the userland on success. */
1376 sprintf(ifname, "%s", ifp->if_xname);
1378 sx_xunlock(&ifnet_detach_sxlock);
1385 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1388 struct vnet *vnet_dst;
1390 int error, found __diagused;
1393 /* Try to find the prison within our visibility. */
1394 sx_slock(&allprison_lock);
1395 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1396 sx_sunlock(&allprison_lock);
1399 prison_hold_locked(pr);
1400 mtx_unlock(&pr->pr_mtx);
1402 /* Make sure the named iface exists in the source prison/vnet. */
1403 CURVNET_SET(pr->pr_vnet);
1404 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1411 /* Do not try to move the iface from and to the same prison. */
1412 vnet_dst = TD_TO_VNET(td);
1413 if (vnet_dst == ifp->if_vnet) {
1419 /* Make sure the VNET is stable. */
1420 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1427 /* Get interface back from child jail/vnet. */
1428 found = if_unlink_ifnet(ifp, true);
1430 sx_xlock(&ifnet_detach_sxlock);
1431 error = if_vmove(ifp, vnet_dst);
1432 sx_xunlock(&ifnet_detach_sxlock);
1435 /* Report the new if_xname back to the userland on success. */
1437 sprintf(ifname, "%s", ifp->if_xname);
1445 * Add a group to an interface
1448 if_addgroup(struct ifnet *ifp, const char *groupname)
1450 struct ifg_list *ifgl;
1451 struct ifg_group *ifg = NULL;
1452 struct ifg_member *ifgm;
1455 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1456 groupname[strlen(groupname) - 1] <= '9')
1460 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1461 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1466 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1471 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1477 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1478 if (!strcmp(ifg->ifg_group, groupname))
1482 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1488 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1489 ifg->ifg_refcnt = 0;
1490 CK_STAILQ_INIT(&ifg->ifg_members);
1491 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1496 ifgl->ifgl_group = ifg;
1497 ifgm->ifgm_ifp = ifp;
1500 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1501 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1502 IF_ADDR_WUNLOCK(ifp);
1507 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1508 EVENTHANDLER_INVOKE(group_change_event, groupname);
1514 * Helper function to remove a group out of an interface. Expects the global
1515 * ifnet lock to be write-locked, and drops it before returning.
1518 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1519 const char *groupname)
1521 struct ifg_member *ifgm;
1524 IFNET_WLOCK_ASSERT();
1527 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1528 IF_ADDR_WUNLOCK(ifp);
1530 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1531 if (ifgm->ifgm_ifp == ifp) {
1532 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1533 ifg_member, ifgm_next);
1538 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1539 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1547 epoch_wait_preempt(net_epoch_preempt);
1548 EVENTHANDLER_INVOKE(group_change_event, groupname);
1550 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1551 free(ifgl->ifgl_group, M_TEMP);
1558 * Remove a group from an interface
1561 if_delgroup(struct ifnet *ifp, const char *groupname)
1563 struct ifg_list *ifgl;
1566 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1567 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1574 _if_delgroup_locked(ifp, ifgl, groupname);
1580 * Remove an interface from all groups
1583 if_delgroups(struct ifnet *ifp)
1585 struct ifg_list *ifgl;
1586 char groupname[IFNAMSIZ];
1589 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1590 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1591 _if_delgroup_locked(ifp, ifgl, groupname);
1598 * Stores all groups from an interface in memory pointed to by ifgr.
1601 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1604 struct ifg_list *ifgl;
1605 struct ifg_req ifgrq, *ifgp;
1609 if (ifgr->ifgr_len == 0) {
1610 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1611 ifgr->ifgr_len += sizeof(struct ifg_req);
1615 len = ifgr->ifgr_len;
1616 ifgp = ifgr->ifgr_groups;
1618 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1619 if (len < sizeof(ifgrq))
1621 bzero(&ifgrq, sizeof ifgrq);
1622 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1623 sizeof(ifgrq.ifgrq_group));
1624 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1626 len -= sizeof(ifgrq);
1634 * Stores all members of a group in memory pointed to by igfr
1637 if_getgroupmembers(struct ifgroupreq *ifgr)
1639 struct ifg_group *ifg;
1640 struct ifg_member *ifgm;
1641 struct ifg_req ifgrq, *ifgp;
1645 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1646 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1653 if (ifgr->ifgr_len == 0) {
1654 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1655 ifgr->ifgr_len += sizeof(ifgrq);
1660 len = ifgr->ifgr_len;
1661 ifgp = ifgr->ifgr_groups;
1662 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1663 if (len < sizeof(ifgrq)) {
1667 bzero(&ifgrq, sizeof ifgrq);
1668 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1669 sizeof(ifgrq.ifgrq_member));
1670 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1674 len -= sizeof(ifgrq);
1683 * Return counter values from counter(9)s stored in ifnet.
1686 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1689 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1691 return (counter_u64_fetch(ifp->if_counters[cnt]));
1695 * Increase an ifnet counter. Usually used for counters shared
1696 * between the stack and a driver, but function supports them all.
1699 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1702 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1704 counter_u64_add(ifp->if_counters[cnt], inc);
1708 * Copy data from ifnet to userland API structure if_data.
1711 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1714 ifd->ifi_type = ifp->if_type;
1715 ifd->ifi_physical = 0;
1716 ifd->ifi_addrlen = ifp->if_addrlen;
1717 ifd->ifi_hdrlen = ifp->if_hdrlen;
1718 ifd->ifi_link_state = ifp->if_link_state;
1720 ifd->ifi_datalen = sizeof(struct if_data);
1721 ifd->ifi_mtu = ifp->if_mtu;
1722 ifd->ifi_metric = ifp->if_metric;
1723 ifd->ifi_baudrate = ifp->if_baudrate;
1724 ifd->ifi_hwassist = ifp->if_hwassist;
1725 ifd->ifi_epoch = ifp->if_epoch;
1726 ifd->ifi_lastchange = ifp->if_lastchange;
1728 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1729 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1730 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1731 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1732 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1733 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1734 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1735 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1736 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1737 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1738 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1739 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1743 * Initialization, destruction and refcounting functions for ifaddrs.
1746 ifa_alloc(size_t size, int flags)
1750 KASSERT(size >= sizeof(struct ifaddr),
1751 ("%s: invalid size %zu", __func__, size));
1753 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1757 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1759 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1761 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1763 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1766 refcount_init(&ifa->ifa_refcnt, 1);
1771 /* free(NULL) is okay */
1772 counter_u64_free(ifa->ifa_opackets);
1773 counter_u64_free(ifa->ifa_ipackets);
1774 counter_u64_free(ifa->ifa_obytes);
1775 counter_u64_free(ifa->ifa_ibytes);
1776 free(ifa, M_IFADDR);
1782 ifa_ref(struct ifaddr *ifa)
1784 u_int old __diagused;
1786 old = refcount_acquire(&ifa->ifa_refcnt);
1787 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1791 ifa_try_ref(struct ifaddr *ifa)
1795 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1799 ifa_destroy(epoch_context_t ctx)
1803 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1804 counter_u64_free(ifa->ifa_opackets);
1805 counter_u64_free(ifa->ifa_ipackets);
1806 counter_u64_free(ifa->ifa_obytes);
1807 counter_u64_free(ifa->ifa_ibytes);
1808 free(ifa, M_IFADDR);
1812 ifa_free(struct ifaddr *ifa)
1815 if (refcount_release(&ifa->ifa_refcnt))
1816 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1820 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1821 * structs used to represent other address families, it is necessary
1822 * to perform a different comparison.
1825 #define sa_dl_equal(a1, a2) \
1826 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1827 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1828 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1829 CLLADDR((const struct sockaddr_dl *)(a2)), \
1830 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1833 * Locate an interface based on a complete address.
1837 ifa_ifwithaddr(const struct sockaddr *addr)
1844 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1845 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1846 if (ifa->ifa_addr->sa_family != addr->sa_family)
1848 if (sa_equal(addr, ifa->ifa_addr)) {
1851 /* IP6 doesn't have broadcast */
1852 if ((ifp->if_flags & IFF_BROADCAST) &&
1853 ifa->ifa_broadaddr &&
1854 ifa->ifa_broadaddr->sa_len != 0 &&
1855 sa_equal(ifa->ifa_broadaddr, addr)) {
1866 ifa_ifwithaddr_check(const struct sockaddr *addr)
1868 struct epoch_tracker et;
1871 NET_EPOCH_ENTER(et);
1872 rc = (ifa_ifwithaddr(addr) != NULL);
1878 * Locate an interface based on the broadcast address.
1882 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1888 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1889 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1891 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1892 if (ifa->ifa_addr->sa_family != addr->sa_family)
1894 if ((ifp->if_flags & IFF_BROADCAST) &&
1895 ifa->ifa_broadaddr &&
1896 ifa->ifa_broadaddr->sa_len != 0 &&
1897 sa_equal(ifa->ifa_broadaddr, addr)) {
1908 * Locate the point to point interface with a given destination address.
1912 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1918 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1919 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1921 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1923 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1924 if (ifa->ifa_addr->sa_family != addr->sa_family)
1926 if (ifa->ifa_dstaddr != NULL &&
1927 sa_equal(addr, ifa->ifa_dstaddr)) {
1938 * Find an interface on a specific network. If many, choice
1939 * is most specific found.
1942 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1946 struct ifaddr *ifa_maybe = NULL;
1947 u_int af = addr->sa_family;
1948 const char *addr_data = addr->sa_data, *cplim;
1952 * AF_LINK addresses can be looked up directly by their index number,
1953 * so do that if we can.
1955 if (af == AF_LINK) {
1956 ifp = ifnet_byindex(
1957 ((const struct sockaddr_dl *)addr)->sdl_index);
1958 return (ifp ? ifp->if_addr : NULL);
1962 * Scan though each interface, looking for ones that have addresses
1963 * in this address family and the requested fib.
1965 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1966 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1968 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1969 const char *cp, *cp2, *cp3;
1971 if (ifa->ifa_addr->sa_family != af)
1973 if (af == AF_INET &&
1974 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1976 * This is a bit broken as it doesn't
1977 * take into account that the remote end may
1978 * be a single node in the network we are
1980 * The trouble is that we don't know the
1981 * netmask for the remote end.
1983 if (ifa->ifa_dstaddr != NULL &&
1984 sa_equal(addr, ifa->ifa_dstaddr)) {
1989 * Scan all the bits in the ifa's address.
1990 * If a bit dissagrees with what we are
1991 * looking for, mask it with the netmask
1992 * to see if it really matters.
1993 * (A byte at a time)
1995 if (ifa->ifa_netmask == 0)
1998 cp2 = ifa->ifa_addr->sa_data;
1999 cp3 = ifa->ifa_netmask->sa_data;
2000 cplim = ifa->ifa_netmask->sa_len
2001 + (char *)ifa->ifa_netmask;
2003 if ((*cp++ ^ *cp2++) & *cp3++)
2004 goto next; /* next address! */
2006 * If the netmask of what we just found
2007 * is more specific than what we had before
2008 * (if we had one), or if the virtual status
2009 * of new prefix is better than of the old one,
2010 * then remember the new one before continuing
2011 * to search for an even better one.
2013 if (ifa_maybe == NULL ||
2014 ifa_preferred(ifa_maybe, ifa) ||
2015 rn_refines((caddr_t)ifa->ifa_netmask,
2016 (caddr_t)ifa_maybe->ifa_netmask)) {
2029 * Find an interface address specific to an interface best matching
2033 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2036 const char *cp, *cp2, *cp3;
2038 struct ifaddr *ifa_maybe = NULL;
2039 u_int af = addr->sa_family;
2045 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2046 if (ifa->ifa_addr->sa_family != af)
2048 if (ifa_maybe == NULL)
2050 if (ifa->ifa_netmask == 0) {
2051 if (sa_equal(addr, ifa->ifa_addr) ||
2052 (ifa->ifa_dstaddr &&
2053 sa_equal(addr, ifa->ifa_dstaddr)))
2057 if (ifp->if_flags & IFF_POINTOPOINT) {
2058 if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
2062 cp2 = ifa->ifa_addr->sa_data;
2063 cp3 = ifa->ifa_netmask->sa_data;
2064 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2065 for (; cp3 < cplim; cp3++)
2066 if ((*cp++ ^ *cp2++) & *cp3)
2078 * See whether new ifa is better than current one:
2079 * 1) A non-virtual one is preferred over virtual.
2080 * 2) A virtual in master state preferred over any other state.
2082 * Used in several address selecting functions.
2085 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2088 return (cur->ifa_carp && (!next->ifa_carp ||
2089 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2092 struct sockaddr_dl *
2093 link_alloc_sdl(size_t size, int flags)
2096 return (malloc(size, M_TEMP, flags));
2100 link_free_sdl(struct sockaddr *sa)
2106 * Fills in given sdl with interface basic info.
2107 * Returns pointer to filled sdl.
2109 struct sockaddr_dl *
2110 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2112 struct sockaddr_dl *sdl;
2114 sdl = (struct sockaddr_dl *)paddr;
2115 memset(sdl, 0, sizeof(struct sockaddr_dl));
2116 sdl->sdl_len = sizeof(struct sockaddr_dl);
2117 sdl->sdl_family = AF_LINK;
2118 sdl->sdl_index = ifp->if_index;
2119 sdl->sdl_type = iftype;
2125 * Mark an interface down and notify protocols of
2129 if_unroute(struct ifnet *ifp, int flag, int fam)
2132 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2134 ifp->if_flags &= ~flag;
2135 getmicrotime(&ifp->if_lastchange);
2136 ifp->if_qflush(ifp);
2139 (*carp_linkstate_p)(ifp);
2140 rt_ifmsg(ifp, IFF_UP);
2143 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2144 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2145 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2146 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2147 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2148 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2149 int (*vlan_setcookie_p)(struct ifnet *, void *);
2150 void *(*vlan_cookie_p)(struct ifnet *);
2153 * Handle a change in the interface link state. To avoid LORs
2154 * between driver lock and upper layer locks, as well as possible
2155 * recursions, we post event to taskqueue, and all job
2156 * is done in static do_link_state_change().
2159 if_link_state_change(struct ifnet *ifp, int link_state)
2161 /* Return if state hasn't changed. */
2162 if (ifp->if_link_state == link_state)
2165 ifp->if_link_state = link_state;
2167 /* XXXGL: reference ifp? */
2168 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2172 do_link_state_change(void *arg, int pending)
2178 link_state = ifp->if_link_state;
2180 CURVNET_SET(ifp->if_vnet);
2182 if (ifp->if_vlantrunk != NULL)
2183 (*vlan_link_state_p)(ifp);
2185 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2186 ifp->if_l2com != NULL)
2187 (*ng_ether_link_state_p)(ifp, link_state);
2189 (*carp_linkstate_p)(ifp);
2191 ifp->if_bridge_linkstate(ifp);
2193 (*lagg_linkstate_p)(ifp, link_state);
2195 if (IS_DEFAULT_VNET(curvnet))
2196 devctl_notify("IFNET", ifp->if_xname,
2197 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2200 if_printf(ifp, "%d link states coalesced\n", pending);
2201 if (log_link_state_change)
2202 if_printf(ifp, "link state changed to %s\n",
2203 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2204 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2209 * Mark an interface down and notify protocols of
2213 if_down(struct ifnet *ifp)
2216 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2217 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2221 * Mark an interface up and notify protocols of
2225 if_up(struct ifnet *ifp)
2228 ifp->if_flags |= IFF_UP;
2229 getmicrotime(&ifp->if_lastchange);
2231 (*carp_linkstate_p)(ifp);
2232 rt_ifmsg(ifp, IFF_UP);
2233 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2237 * Flush an interface queue.
2240 if_qflush(struct ifnet *ifp)
2248 if (ALTQ_IS_ENABLED(ifq))
2252 while ((m = n) != NULL) {
2263 * Map interface name to interface structure pointer, with or without
2264 * returning a reference.
2267 ifunit_ref(const char *name)
2269 struct epoch_tracker et;
2272 NET_EPOCH_ENTER(et);
2273 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2274 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2275 !(ifp->if_flags & IFF_DYING))
2280 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2288 ifunit(const char *name)
2290 struct epoch_tracker et;
2293 NET_EPOCH_ENTER(et);
2294 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2295 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2303 ifr_buffer_get_buffer(void *data)
2305 union ifreq_union *ifrup;
2308 #ifdef COMPAT_FREEBSD32
2309 if (SV_CURPROC_FLAG(SV_ILP32))
2310 return ((void *)(uintptr_t)
2311 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2313 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2317 ifr_buffer_set_buffer_null(void *data)
2319 union ifreq_union *ifrup;
2322 #ifdef COMPAT_FREEBSD32
2323 if (SV_CURPROC_FLAG(SV_ILP32))
2324 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2327 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2331 ifr_buffer_get_length(void *data)
2333 union ifreq_union *ifrup;
2336 #ifdef COMPAT_FREEBSD32
2337 if (SV_CURPROC_FLAG(SV_ILP32))
2338 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2340 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2344 ifr_buffer_set_length(void *data, size_t len)
2346 union ifreq_union *ifrup;
2349 #ifdef COMPAT_FREEBSD32
2350 if (SV_CURPROC_FLAG(SV_ILP32))
2351 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2354 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2358 ifr_data_get_ptr(void *ifrp)
2360 union ifreq_union *ifrup;
2363 #ifdef COMPAT_FREEBSD32
2364 if (SV_CURPROC_FLAG(SV_ILP32))
2365 return ((void *)(uintptr_t)
2366 ifrup->ifr32.ifr_ifru.ifru_data);
2368 return (ifrup->ifr.ifr_ifru.ifru_data);
2371 struct ifcap_nv_bit_name {
2373 const char *cap_name;
2375 #define CAPNV(x) {.cap_bit = IFCAP_##x, \
2376 .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
2377 const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
2382 CAPNV(VLAN_HWTAGGING),
2394 CAPNV(VLAN_HWFILTER),
2406 CAPNV(VXLAN_HWCSUM),
2411 #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
2412 .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
2413 const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
2422 if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
2423 const struct ifcap_nv_bit_name *nn, bool all)
2428 for (i = 0; nn[i].cap_name != NULL; i++) {
2429 if (nvlist_exists_bool(nv, nn[i].cap_name)) {
2430 if (all || nvlist_get_bool(nv, nn[i].cap_name))
2431 res |= nn[i].cap_bit;
2433 res |= *old_cap & nn[i].cap_bit;
2440 if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
2441 int ifr_cap, int ifr_req)
2445 for (i = 0; nn[i].cap_name != NULL; i++) {
2446 if ((nn[i].cap_bit & ifr_cap) != 0) {
2447 nvlist_add_bool(nv, nn[i].cap_name,
2448 (nn[i].cap_bit & ifr_req) != 0);
2454 * Hardware specific interface ioctls.
2457 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2460 int error = 0, do_ifup = 0;
2461 int new_flags, temp_flags;
2462 size_t descrlen, nvbuflen;
2464 char new_name[IFNAMSIZ];
2467 struct siocsifcapnv_driver_data drv_ioctl_data;
2469 ifr = (struct ifreq *)data;
2472 ifr->ifr_index = ifp->if_index;
2476 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2477 ifr->ifr_flags = temp_flags & 0xffff;
2478 ifr->ifr_flagshigh = temp_flags >> 16;
2482 ifr->ifr_reqcap = ifp->if_capabilities;
2483 ifr->ifr_curcap = ifp->if_capenable;
2487 if ((ifp->if_capabilities & IFCAP_NV) == 0) {
2492 nvcap = nvlist_create(0);
2494 if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
2495 ifp->if_capabilities, ifp->if_capenable);
2496 if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
2497 ifp->if_capabilities2, ifp->if_capenable2);
2498 error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
2499 __DECONST(caddr_t, nvcap));
2502 "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
2506 buf = nvlist_pack(nvcap, &nvbuflen);
2508 error = nvlist_error(nvcap);
2513 if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
2514 ifr->ifr_cap_nv.length = nvbuflen;
2515 ifr->ifr_cap_nv.buffer = NULL;
2519 ifr->ifr_cap_nv.length = nvbuflen;
2520 error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
2523 free(buf, M_NVLIST);
2524 nvlist_destroy(nvcap);
2531 /* Ensure uninitialised padding is not leaked. */
2532 memset(&ifd, 0, sizeof(ifd));
2534 if_data_copy(ifp, &ifd);
2535 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2541 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2546 ifr->ifr_metric = ifp->if_metric;
2550 ifr->ifr_mtu = ifp->if_mtu;
2554 /* XXXGL: did this ever worked? */
2560 sx_slock(&ifdescr_sx);
2561 if (ifp->if_description == NULL)
2564 /* space for terminating nul */
2565 descrlen = strlen(ifp->if_description) + 1;
2566 if (ifr_buffer_get_length(ifr) < descrlen)
2567 ifr_buffer_set_buffer_null(ifr);
2569 error = copyout(ifp->if_description,
2570 ifr_buffer_get_buffer(ifr), descrlen);
2571 ifr_buffer_set_length(ifr, descrlen);
2573 sx_sunlock(&ifdescr_sx);
2577 error = priv_check(td, PRIV_NET_SETIFDESCR);
2582 * Copy only (length-1) bytes to make sure that
2583 * if_description is always nul terminated. The
2584 * length parameter is supposed to count the
2585 * terminating nul in.
2587 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2588 return (ENAMETOOLONG);
2589 else if (ifr_buffer_get_length(ifr) == 0)
2592 descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
2593 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2594 ifr_buffer_get_length(ifr) - 1);
2596 if_freedescr(descrbuf);
2601 if_setdescr(ifp, descrbuf);
2602 getmicrotime(&ifp->if_lastchange);
2606 ifr->ifr_fib = ifp->if_fib;
2610 error = priv_check(td, PRIV_NET_SETIFFIB);
2613 if (ifr->ifr_fib >= rt_numfibs)
2616 ifp->if_fib = ifr->ifr_fib;
2620 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2624 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2625 * check, so we don't need special handling here yet.
2627 new_flags = (ifr->ifr_flags & 0xffff) |
2628 (ifr->ifr_flagshigh << 16);
2629 if (ifp->if_flags & IFF_UP &&
2630 (new_flags & IFF_UP) == 0) {
2632 } else if (new_flags & IFF_UP &&
2633 (ifp->if_flags & IFF_UP) == 0) {
2636 /* See if permanently promiscuous mode bit is about to flip */
2637 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2638 if (new_flags & IFF_PPROMISC)
2639 ifp->if_flags |= IFF_PROMISC;
2640 else if (ifp->if_pcount == 0)
2641 ifp->if_flags &= ~IFF_PROMISC;
2642 if (log_promisc_mode_change)
2643 if_printf(ifp, "permanently promiscuous mode %s\n",
2644 ((new_flags & IFF_PPROMISC) ?
2645 "enabled" : "disabled"));
2647 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2648 (new_flags &~ IFF_CANTCHANGE);
2649 if (ifp->if_ioctl) {
2650 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2654 getmicrotime(&ifp->if_lastchange);
2658 error = priv_check(td, PRIV_NET_SETIFCAP);
2661 if (ifp->if_ioctl == NULL)
2662 return (EOPNOTSUPP);
2663 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2665 error = (*ifp->if_ioctl)(ifp, cmd, data);
2667 getmicrotime(&ifp->if_lastchange);
2671 error = priv_check(td, PRIV_NET_SETIFCAP);
2674 if (ifp->if_ioctl == NULL)
2675 return (EOPNOTSUPP);
2676 if ((ifp->if_capabilities & IFCAP_NV) == 0)
2678 if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
2681 buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
2683 error = copyin(ifr->ifr_cap_nv.buffer, buf,
2684 ifr->ifr_cap_nv.length);
2687 nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
2688 if (nvcap == NULL) {
2692 drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
2693 &ifp->if_capenable, ifcap_nv_bit_names, false);
2694 if ((drv_ioctl_data.reqcap &
2695 ~ifp->if_capabilities) != 0) {
2699 drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
2700 &ifp->if_capenable2, ifcap2_nv_bit_names, false);
2701 if ((drv_ioctl_data.reqcap2 &
2702 ~ifp->if_capabilities2) != 0) {
2706 drv_ioctl_data.nvcap = nvcap;
2707 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
2708 (caddr_t)&drv_ioctl_data);
2711 nvlist_destroy(nvcap);
2714 getmicrotime(&ifp->if_lastchange);
2719 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2724 error = priv_check(td, PRIV_NET_SETIFNAME);
2727 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2731 error = if_rename(ifp, new_name);
2736 error = priv_check(td, PRIV_NET_SETIFVNET);
2739 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2744 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2747 ifp->if_metric = ifr->ifr_metric;
2748 getmicrotime(&ifp->if_lastchange);
2752 error = priv_check(td, PRIV_NET_SETIFPHYS);
2755 if (ifp->if_ioctl == NULL)
2756 return (EOPNOTSUPP);
2757 error = (*ifp->if_ioctl)(ifp, cmd, data);
2759 getmicrotime(&ifp->if_lastchange);
2764 u_long oldmtu = ifp->if_mtu;
2766 error = priv_check(td, PRIV_NET_SETIFMTU);
2769 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2771 if (ifp->if_ioctl == NULL)
2772 return (EOPNOTSUPP);
2773 /* Disallow MTU changes on bridge member interfaces. */
2775 return (EOPNOTSUPP);
2776 error = (*ifp->if_ioctl)(ifp, cmd, data);
2778 getmicrotime(&ifp->if_lastchange);
2781 DEBUGNET_NOTIFY_MTU(ifp);
2785 * If the link MTU changed, do network layer specific procedure.
2787 if (ifp->if_mtu != oldmtu)
2794 if (cmd == SIOCADDMULTI)
2795 error = priv_check(td, PRIV_NET_ADDMULTI);
2797 error = priv_check(td, PRIV_NET_DELMULTI);
2801 /* Don't allow group membership on non-multicast interfaces. */
2802 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2803 return (EOPNOTSUPP);
2805 /* Don't let users screw up protocols' entries. */
2806 if (ifr->ifr_addr.sa_family != AF_LINK)
2809 if (cmd == SIOCADDMULTI) {
2810 struct epoch_tracker et;
2811 struct ifmultiaddr *ifma;
2814 * Userland is only permitted to join groups once
2815 * via the if_addmulti() KPI, because it cannot hold
2816 * struct ifmultiaddr * between calls. It may also
2817 * lose a race while we check if the membership
2820 NET_EPOCH_ENTER(et);
2821 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2826 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2828 error = if_delmulti(ifp, &ifr->ifr_addr);
2831 getmicrotime(&ifp->if_lastchange);
2834 case SIOCSIFPHYADDR:
2835 case SIOCDIFPHYADDR:
2837 case SIOCSIFPHYADDR_IN6:
2840 case SIOCSIFGENERIC:
2841 error = priv_check(td, PRIV_NET_HWIOCTL);
2844 if (ifp->if_ioctl == NULL)
2845 return (EOPNOTSUPP);
2846 error = (*ifp->if_ioctl)(ifp, cmd, data);
2848 getmicrotime(&ifp->if_lastchange);
2852 case SIOCGIFPSRCADDR:
2853 case SIOCGIFPDSTADDR:
2856 case SIOCGIFGENERIC:
2858 case SIOCGIFRSSHASH:
2859 case SIOCGIFDOWNREASON:
2860 if (ifp->if_ioctl == NULL)
2861 return (EOPNOTSUPP);
2862 error = (*ifp->if_ioctl)(ifp, cmd, data);
2866 error = priv_check(td, PRIV_NET_SETLLADDR);
2869 error = if_setlladdr(ifp,
2870 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2874 error = if_gethwaddr(ifp, ifr);
2878 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2881 error = if_addgroup(ifp,
2882 ((struct ifgroupreq *)data)->ifgr_group);
2889 struct epoch_tracker et;
2891 NET_EPOCH_ENTER(et);
2892 error = if_getgroup((struct ifgroupreq *)data, ifp);
2898 error = priv_check(td, PRIV_NET_DELIFGROUP);
2901 error = if_delgroup(ifp,
2902 ((struct ifgroupreq *)data)->ifgr_group);
2918 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2920 #ifdef COMPAT_FREEBSD32
2924 struct ifgroupreq ifgr;
2925 struct ifmediareq ifmr;
2928 struct ifconf32 *ifc32;
2929 struct ifdrv32 *ifd32;
2930 struct ifgroupreq32 *ifgr32;
2931 struct ifmediareq32 *ifmr32;
2941 CURVNET_SET(so->so_vnet);
2943 /* Make sure the VNET is stable. */
2944 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2951 #ifdef COMPAT_FREEBSD32
2955 ifc32 = (struct ifconf32 *)data;
2956 thunk.ifc.ifc_len = ifc32->ifc_len;
2957 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2958 data = (caddr_t)&thunk.ifc;
2961 case SIOCGDRVSPEC32:
2962 case SIOCSDRVSPEC32:
2963 ifd32 = (struct ifdrv32 *)data;
2964 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2965 sizeof(thunk.ifd.ifd_name));
2966 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2967 thunk.ifd.ifd_len = ifd32->ifd_len;
2968 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2969 data = (caddr_t)&thunk.ifd;
2970 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2972 case SIOCAIFGROUP32:
2973 case SIOCGIFGROUP32:
2974 case SIOCDIFGROUP32:
2975 case SIOCGIFGMEMB32:
2976 ifgr32 = (struct ifgroupreq32 *)data;
2977 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2978 sizeof(thunk.ifgr.ifgr_name));
2979 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2981 case SIOCAIFGROUP32:
2982 case SIOCDIFGROUP32:
2983 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2984 sizeof(thunk.ifgr.ifgr_group));
2986 case SIOCGIFGROUP32:
2987 case SIOCGIFGMEMB32:
2988 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2991 data = (caddr_t)&thunk.ifgr;
2992 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2994 case SIOCGIFMEDIA32:
2995 case SIOCGIFXMEDIA32:
2996 ifmr32 = (struct ifmediareq32 *)data;
2997 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2998 sizeof(thunk.ifmr.ifm_name));
2999 thunk.ifmr.ifm_current = ifmr32->ifm_current;
3000 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
3001 thunk.ifmr.ifm_status = ifmr32->ifm_status;
3002 thunk.ifmr.ifm_active = ifmr32->ifm_active;
3003 thunk.ifmr.ifm_count = ifmr32->ifm_count;
3004 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
3005 data = (caddr_t)&thunk.ifmr;
3006 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3013 error = ifconf(cmd, data);
3017 ifr = (struct ifreq *)data;
3021 error = priv_check(td, PRIV_NET_SETIFVNET);
3023 error = if_vmove_reclaim(td, ifr->ifr_name,
3029 error = priv_check(td, PRIV_NET_IFCREATE);
3031 error = if_clone_create(ifr->ifr_name,
3032 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3033 ifr_data_get_ptr(ifr) : NULL);
3036 error = priv_check(td, PRIV_NET_IFDESTROY);
3039 sx_xlock(&ifnet_detach_sxlock);
3040 error = if_clone_destroy(ifr->ifr_name);
3041 sx_xunlock(&ifnet_detach_sxlock);
3045 case SIOCIFGCLONERS:
3046 error = if_clone_list((struct if_clonereq *)data);
3050 error = if_getgroupmembers((struct ifgroupreq *)data);
3053 #if defined(INET) || defined(INET6)
3056 if (carp_ioctl_p == NULL)
3057 error = EPROTONOSUPPORT;
3059 error = (*carp_ioctl_p)(ifr, cmd, td);
3064 ifp = ifunit_ref(ifr->ifr_name);
3070 error = ifhwioctl(cmd, ifp, data, td);
3071 if (error != ENOIOCTL)
3074 oif_flags = ifp->if_flags;
3075 if (so->so_proto == NULL) {
3081 * Pass the request on to the socket control method, and if the
3082 * latter returns EOPNOTSUPP, directly to the interface.
3084 * Make an exception for the legacy SIOCSIF* requests. Drivers
3085 * trust SIOCSIFADDR et al to come from an already privileged
3086 * layer, and do not perform any credentials checks or input
3089 error = so->so_proto->pr_control(so, cmd, data, ifp, td);
3090 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3091 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3092 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3093 error = (*ifp->if_ioctl)(ifp, cmd, data);
3095 if (!(oif_flags & IFF_UP) && (ifp->if_flags & IFF_UP))
3101 #ifdef COMPAT_FREEBSD32
3104 switch (saved_cmd) {
3106 ifc32->ifc_len = thunk.ifc.ifc_len;
3108 case SIOCGDRVSPEC32:
3110 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3111 * the struct so just assert that ifd_len (the only
3112 * field it might make sense to update) hasn't
3115 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3116 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3117 thunk.ifd.ifd_len));
3119 case SIOCGIFGROUP32:
3120 case SIOCGIFGMEMB32:
3121 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3123 case SIOCGIFMEDIA32:
3124 case SIOCGIFXMEDIA32:
3125 ifmr32->ifm_current = thunk.ifmr.ifm_current;
3126 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3127 ifmr32->ifm_status = thunk.ifmr.ifm_status;
3128 ifmr32->ifm_active = thunk.ifmr.ifm_active;
3129 ifmr32->ifm_count = thunk.ifmr.ifm_count;
3137 if_rename(struct ifnet *ifp, char *new_name)
3140 struct sockaddr_dl *sdl;
3141 size_t namelen, onamelen;
3142 char old_name[IFNAMSIZ];
3143 char strbuf[IFNAMSIZ + 8];
3145 if (new_name[0] == '\0')
3147 if (strcmp(new_name, ifp->if_xname) == 0)
3149 if (ifunit(new_name) != NULL)
3153 * XXX: Locking. Nothing else seems to lock if_flags,
3154 * and there are numerous other races with the
3155 * ifunit() checks not being atomic with namespace
3156 * changes (renames, vmoves, if_attach, etc).
3158 ifp->if_flags |= IFF_RENAMING;
3160 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
3162 if_printf(ifp, "changing name to '%s'\n", new_name);
3165 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
3166 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
3168 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3169 namelen = strlen(new_name);
3170 onamelen = sdl->sdl_nlen;
3172 * Move the address if needed. This is safe because we
3173 * allocate space for a name of length IFNAMSIZ when we
3174 * create this in if_attach().
3176 if (namelen != onamelen) {
3177 bcopy(sdl->sdl_data + onamelen,
3178 sdl->sdl_data + namelen, sdl->sdl_alen);
3180 bcopy(new_name, sdl->sdl_data, namelen);
3181 sdl->sdl_nlen = namelen;
3182 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
3183 bzero(sdl->sdl_data, onamelen);
3184 while (namelen != 0)
3185 sdl->sdl_data[--namelen] = 0xff;
3186 IF_ADDR_WUNLOCK(ifp);
3188 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
3190 ifp->if_flags &= ~IFF_RENAMING;
3192 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
3193 devctl_notify("IFNET", old_name, "RENAME", strbuf);
3199 * The code common to handling reference counted flags,
3200 * e.g., in ifpromisc() and if_allmulti().
3201 * The "pflag" argument can specify a permanent mode flag to check,
3202 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3204 * Only to be used on stack-owned flags, not driver-owned flags.
3207 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3211 int oldflags, oldcount;
3213 /* Sanity checks to catch programming errors */
3214 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3215 ("%s: setting driver-owned flag %d", __func__, flag));
3218 KASSERT(*refcount >= 0,
3219 ("%s: increment negative refcount %d for flag %d",
3220 __func__, *refcount, flag));
3222 KASSERT(*refcount > 0,
3223 ("%s: decrement non-positive refcount %d for flag %d",
3224 __func__, *refcount, flag));
3226 /* In case this mode is permanent, just touch refcount */
3227 if (ifp->if_flags & pflag) {
3228 *refcount += onswitch ? 1 : -1;
3232 /* Save ifnet parameters for if_ioctl() may fail */
3233 oldcount = *refcount;
3234 oldflags = ifp->if_flags;
3237 * See if we aren't the only and touching refcount is enough.
3238 * Actually toggle interface flag if we are the first or last.
3243 ifp->if_flags |= flag;
3247 ifp->if_flags &= ~flag;
3250 /* Call down the driver since we've changed interface flags */
3251 if (ifp->if_ioctl == NULL) {
3255 ifr.ifr_flags = ifp->if_flags & 0xffff;
3256 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3257 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3260 /* Notify userland that interface flags have changed */
3261 rt_ifmsg(ifp, flag);
3265 /* Recover after driver error */
3266 *refcount = oldcount;
3267 ifp->if_flags = oldflags;
3272 * Set/clear promiscuous mode on interface ifp based on the truth value
3273 * of pswitch. The calls are reference counted so that only the first
3274 * "on" request actually has an effect, as does the final "off" request.
3275 * Results are undefined if the "off" and "on" requests are not matched.
3278 ifpromisc(struct ifnet *ifp, int pswitch)
3281 int oldflags = ifp->if_flags;
3283 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3284 &ifp->if_pcount, pswitch);
3285 /* If promiscuous mode status has changed, log a message */
3286 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3287 log_promisc_mode_change)
3288 if_printf(ifp, "promiscuous mode %s\n",
3289 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3294 * Return interface configuration
3295 * of system. List may be used
3296 * in later ioctl's (above) to get
3297 * other information.
3301 ifconf(u_long cmd, caddr_t data)
3303 struct ifconf *ifc = (struct ifconf *)data;
3308 int error, full = 0, valid_len, max_len;
3310 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3311 max_len = maxphys - 1;
3313 /* Prevent hostile input from being able to crash the system */
3314 if (ifc->ifc_len <= 0)
3318 if (ifc->ifc_len <= max_len) {
3319 max_len = ifc->ifc_len;
3322 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3327 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3328 struct epoch_tracker et;
3332 * Zero the ifr to make sure we don't disclose the contents
3335 memset(&ifr, 0, sizeof(ifr));
3337 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3338 >= sizeof(ifr.ifr_name)) {
3341 return (ENAMETOOLONG);
3345 NET_EPOCH_ENTER(et);
3346 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3347 struct sockaddr *sa = ifa->ifa_addr;
3349 if (prison_if(curthread->td_ucred, sa) != 0)
3352 if (sa->sa_len <= sizeof(*sa)) {
3353 if (sa->sa_len < sizeof(*sa)) {
3354 memset(&ifr.ifr_ifru.ifru_addr, 0,
3355 sizeof(ifr.ifr_ifru.ifru_addr));
3356 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3359 ifr.ifr_ifru.ifru_addr = *sa;
3360 sbuf_bcat(sb, &ifr, sizeof(ifr));
3361 max_len += sizeof(ifr);
3364 offsetof(struct ifreq, ifr_addr));
3365 max_len += offsetof(struct ifreq, ifr_addr);
3366 sbuf_bcat(sb, sa, sa->sa_len);
3367 max_len += sa->sa_len;
3370 if (sbuf_error(sb) == 0)
3371 valid_len = sbuf_len(sb);
3375 sbuf_bcat(sb, &ifr, sizeof(ifr));
3376 max_len += sizeof(ifr);
3378 if (sbuf_error(sb) == 0)
3379 valid_len = sbuf_len(sb);
3385 * If we didn't allocate enough space (uncommon), try again. If
3386 * we have already allocated as much space as we are allowed,
3387 * return what we've got.
3389 if (valid_len != max_len && !full) {
3394 ifc->ifc_len = valid_len;
3396 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3402 * Just like ifpromisc(), but for all-multicast-reception mode.
3405 if_allmulti(struct ifnet *ifp, int onswitch)
3408 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3411 struct ifmultiaddr *
3412 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3414 struct ifmultiaddr *ifma;
3416 IF_ADDR_LOCK_ASSERT(ifp);
3418 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3419 if (sa->sa_family == AF_LINK) {
3420 if (sa_dl_equal(ifma->ifma_addr, sa))
3423 if (sa_equal(ifma->ifma_addr, sa))
3432 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3433 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3434 * the ifnet multicast address list here, so the caller must do that and
3435 * other setup work (such as notifying the device driver). The reference
3436 * count is initialized to 1.
3438 static struct ifmultiaddr *
3439 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3442 struct ifmultiaddr *ifma;
3443 struct sockaddr *dupsa;
3445 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3450 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3451 if (dupsa == NULL) {
3452 free(ifma, M_IFMADDR);
3455 bcopy(sa, dupsa, sa->sa_len);
3456 ifma->ifma_addr = dupsa;
3458 ifma->ifma_ifp = ifp;
3459 ifma->ifma_refcount = 1;
3460 ifma->ifma_protospec = NULL;
3463 ifma->ifma_lladdr = NULL;
3467 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3468 if (dupsa == NULL) {
3469 free(ifma->ifma_addr, M_IFMADDR);
3470 free(ifma, M_IFMADDR);
3473 bcopy(llsa, dupsa, llsa->sa_len);
3474 ifma->ifma_lladdr = dupsa;
3480 * if_freemulti: free ifmultiaddr structure and possibly attached related
3481 * addresses. The caller is responsible for implementing reference
3482 * counting, notifying the driver, handling routing messages, and releasing
3483 * any dependent link layer state.
3485 #ifdef MCAST_VERBOSE
3486 extern void kdb_backtrace(void);
3489 if_freemulti_internal(struct ifmultiaddr *ifma)
3492 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3493 ifma->ifma_refcount));
3495 if (ifma->ifma_lladdr != NULL)
3496 free(ifma->ifma_lladdr, M_IFMADDR);
3497 #ifdef MCAST_VERBOSE
3499 printf("%s freeing ifma: %p\n", __func__, ifma);
3501 free(ifma->ifma_addr, M_IFMADDR);
3502 free(ifma, M_IFMADDR);
3506 if_destroymulti(epoch_context_t ctx)
3508 struct ifmultiaddr *ifma;
3510 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3511 if_freemulti_internal(ifma);
3515 if_freemulti(struct ifmultiaddr *ifma)
3517 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3518 ifma->ifma_refcount));
3520 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3524 * Register an additional multicast address with a network interface.
3526 * - If the address is already present, bump the reference count on the
3527 * address and return.
3528 * - If the address is not link-layer, look up a link layer address.
3529 * - Allocate address structures for one or both addresses, and attach to the
3530 * multicast address list on the interface. If automatically adding a link
3531 * layer address, the protocol address will own a reference to the link
3532 * layer address, to be freed when it is freed.
3533 * - Notify the network device driver of an addition to the multicast address
3536 * 'sa' points to caller-owned memory with the desired multicast address.
3538 * 'retifma' will be used to return a pointer to the resulting multicast
3539 * address reference, if desired.
3542 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3543 struct ifmultiaddr **retifma)
3545 struct ifmultiaddr *ifma, *ll_ifma;
3546 struct sockaddr *llsa;
3547 struct sockaddr_dl sdl;
3551 IN_MULTI_LIST_UNLOCK_ASSERT();
3554 IN6_MULTI_LIST_UNLOCK_ASSERT();
3557 * If the address is already present, return a new reference to it;
3558 * otherwise, allocate storage and set up a new address.
3561 ifma = if_findmulti(ifp, sa);
3563 ifma->ifma_refcount++;
3564 if (retifma != NULL)
3566 IF_ADDR_WUNLOCK(ifp);
3571 * The address isn't already present; resolve the protocol address
3572 * into a link layer address, and then look that up, bump its
3573 * refcount or allocate an ifma for that also.
3574 * Most link layer resolving functions returns address data which
3575 * fits inside default sockaddr_dl structure. However callback
3576 * can allocate another sockaddr structure, in that case we need to
3581 if (ifp->if_resolvemulti != NULL) {
3582 /* Provide called function with buffer size information */
3583 sdl.sdl_len = sizeof(sdl);
3584 llsa = (struct sockaddr *)&sdl;
3585 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3591 * Allocate the new address. Don't hook it up yet, as we may also
3592 * need to allocate a link layer multicast address.
3594 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3601 * If a link layer address is found, we'll need to see if it's
3602 * already present in the address list, or allocate is as well.
3603 * When this block finishes, the link layer address will be on the
3607 ll_ifma = if_findmulti(ifp, llsa);
3608 if (ll_ifma == NULL) {
3609 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3610 if (ll_ifma == NULL) {
3611 --ifma->ifma_refcount;
3616 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3617 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3620 ll_ifma->ifma_refcount++;
3621 ifma->ifma_llifma = ll_ifma;
3625 * We now have a new multicast address, ifma, and possibly a new or
3626 * referenced link layer address. Add the primary address to the
3627 * ifnet address list.
3629 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3630 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3632 if (retifma != NULL)
3636 * Must generate the message while holding the lock so that 'ifma'
3637 * pointer is still valid.
3639 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3640 IF_ADDR_WUNLOCK(ifp);
3643 * We are certain we have added something, so call down to the
3644 * interface to let them know about it.
3646 if (ifp->if_ioctl != NULL) {
3647 if (THREAD_CAN_SLEEP())
3648 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3650 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3653 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3654 link_free_sdl(llsa);
3659 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3660 link_free_sdl(llsa);
3663 IF_ADDR_WUNLOCK(ifp);
3668 if_siocaddmulti(void *arg, int pending)
3675 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3677 CURVNET_SET(ifp->if_vnet);
3678 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3683 * Delete a multicast group membership by network-layer group address.
3685 * Returns ENOENT if the entry could not be found. If ifp no longer
3686 * exists, results are undefined. This entry point should only be used
3687 * from subsystems which do appropriate locking to hold ifp for the
3688 * duration of the call.
3689 * Network-layer protocol domains must use if_delmulti_ifma().
3692 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3694 struct ifmultiaddr *ifma;
3697 KASSERT(ifp, ("%s: NULL ifp", __func__));
3701 ifma = if_findmulti(ifp, sa);
3703 lastref = if_delmulti_locked(ifp, ifma, 0);
3704 IF_ADDR_WUNLOCK(ifp);
3709 if (lastref && ifp->if_ioctl != NULL) {
3710 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3717 * Delete all multicast group membership for an interface.
3718 * Should be used to quickly flush all multicast filters.
3721 if_delallmulti(struct ifnet *ifp)
3723 struct ifmultiaddr *ifma;
3724 struct ifmultiaddr *next;
3727 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3728 if_delmulti_locked(ifp, ifma, 0);
3729 IF_ADDR_WUNLOCK(ifp);
3733 if_delmulti_ifma(struct ifmultiaddr *ifma)
3735 if_delmulti_ifma_flags(ifma, 0);
3739 * Delete a multicast group membership by group membership pointer.
3740 * Network-layer protocol domains must use this routine.
3742 * It is safe to call this routine if the ifp disappeared.
3745 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3749 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3751 IN_MULTI_LIST_UNLOCK_ASSERT();
3753 ifp = ifma->ifma_ifp;
3756 printf("%s: ifma_ifp seems to be detached\n", __func__);
3758 struct epoch_tracker et;
3761 NET_EPOCH_ENTER(et);
3762 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3771 * If and only if the ifnet instance exists: Acquire the address lock.
3776 lastref = if_delmulti_locked(ifp, ifma, flags);
3780 * If and only if the ifnet instance exists:
3781 * Release the address lock.
3782 * If the group was left: update the hardware hash filter.
3784 IF_ADDR_WUNLOCK(ifp);
3785 if (lastref && ifp->if_ioctl != NULL) {
3786 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3792 * Perform deletion of network-layer and/or link-layer multicast address.
3794 * Return 0 if the reference count was decremented.
3795 * Return 1 if the final reference was released, indicating that the
3796 * hardware hash filter should be reprogrammed.
3799 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3801 struct ifmultiaddr *ll_ifma;
3803 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3804 KASSERT(ifma->ifma_ifp == ifp,
3805 ("%s: inconsistent ifp %p", __func__, ifp));
3806 IF_ADDR_WLOCK_ASSERT(ifp);
3809 ifp = ifma->ifma_ifp;
3810 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3813 * If the ifnet is detaching, null out references to ifnet,
3814 * so that upper protocol layers will notice, and not attempt
3815 * to obtain locks for an ifnet which no longer exists. The
3816 * routing socket announcement must happen before the ifnet
3817 * instance is detached from the system.
3821 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3824 * ifp may already be nulled out if we are being reentered
3825 * to delete the ll_ifma.
3828 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3829 ifma->ifma_ifp = NULL;
3833 if (--ifma->ifma_refcount > 0)
3836 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3837 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3838 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3841 * If this ifma is a network-layer ifma, a link-layer ifma may
3842 * have been associated with it. Release it first if so.
3844 ll_ifma = ifma->ifma_llifma;
3845 if (ll_ifma != NULL) {
3846 KASSERT(ifma->ifma_lladdr != NULL,
3847 ("%s: llifma w/o lladdr", __func__));
3849 ll_ifma->ifma_ifp = NULL; /* XXX */
3850 if (--ll_ifma->ifma_refcount == 0) {
3852 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3853 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3855 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3858 if_freemulti(ll_ifma);
3863 struct ifmultiaddr *ifmatmp;
3865 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3866 MPASS(ifma != ifmatmp);
3871 * The last reference to this instance of struct ifmultiaddr
3872 * was released; the hardware should be notified of this change.
3878 * Set the link layer address on an interface.
3880 * At this time we only support certain types of interfaces,
3881 * and we don't allow the length of the address to change.
3883 * Set noinline to be dtrace-friendly
3886 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3888 struct sockaddr_dl *sdl;
3896 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3900 if (len != sdl->sdl_alen) /* don't allow length to change */
3903 switch (ifp->if_type) {
3908 case IFT_IEEE8023ADLAG:
3909 bcopy(lladdr, LLADDR(sdl), len);
3916 * If the interface is already up, we need
3917 * to re-init it in order to reprogram its
3920 if ((ifp->if_flags & IFF_UP) != 0) {
3921 if (ifp->if_ioctl) {
3922 ifp->if_flags &= ~IFF_UP;
3923 ifr.ifr_flags = ifp->if_flags & 0xffff;
3924 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3925 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3926 ifp->if_flags |= IFF_UP;
3927 ifr.ifr_flags = ifp->if_flags & 0xffff;
3928 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3929 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3932 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3938 * Compat function for handling basic encapsulation requests.
3939 * Not converted stacks (FDDI, IB, ..) supports traditional
3940 * output model: ARP (and other similar L2 protocols) are handled
3941 * inside output routine, arpresolve/nd6_resolve() returns MAC
3942 * address instead of full prepend.
3944 * This function creates calculated header==MAC for IPv4/IPv6 and
3945 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3949 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3951 if (req->rtype != IFENCAP_LL)
3952 return (EOPNOTSUPP);
3954 if (req->bufsize < req->lladdr_len)
3957 switch (req->family) {
3962 return (EAFNOSUPPORT);
3965 /* Copy lladdr to storage as is */
3966 memmove(req->buf, req->lladdr, req->lladdr_len);
3967 req->bufsize = req->lladdr_len;
3968 req->lladdr_off = 0;
3974 * Tunnel interfaces can nest, also they may cause infinite recursion
3975 * calls when misconfigured. We'll prevent this by detecting loops.
3976 * High nesting level may cause stack exhaustion. We'll prevent this
3977 * by introducing upper limit.
3979 * Return 0, if tunnel nesting count is equal or less than limit.
3982 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3990 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3991 if (*(struct ifnet **)(mtag + 1) == ifp) {
3992 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3997 if (count > limit) {
3999 "%s: if_output recursively called too many times(%d)\n",
4000 if_name(ifp), count);
4003 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
4006 *(struct ifnet **)(mtag + 1) = ifp;
4007 m_tag_prepend(m, mtag);
4012 * Get the link layer address that was read from the hardware at attach.
4014 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4015 * their component interfaces as IFT_IEEE8023ADLAG.
4018 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4020 if (ifp->if_hw_addr == NULL)
4023 switch (ifp->if_type) {
4025 case IFT_IEEE8023ADLAG:
4026 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4034 * The name argument must be a pointer to storage which will last as
4035 * long as the interface does. For physical devices, the result of
4036 * device_get_name(dev) is a good choice and for pseudo-devices a
4037 * static string works well.
4040 if_initname(struct ifnet *ifp, const char *name, int unit)
4042 ifp->if_dname = name;
4043 ifp->if_dunit = unit;
4044 if (unit != IF_DUNIT_NONE)
4045 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4047 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4051 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
4055 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4056 vlog(pri, if_fmt, ap);
4062 if_printf(struct ifnet *ifp, const char *fmt, ...)
4067 if_vlog(ifp, LOG_INFO, fmt, ap);
4073 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
4078 if_vlog(ifp, pri, fmt, ap);
4084 if_start(struct ifnet *ifp)
4087 (*(ifp)->if_start)(ifp);
4091 * Backwards compatibility interface for drivers
4092 * that have not implemented it
4095 if_transmit_default(struct ifnet *ifp, struct mbuf *m)
4099 IFQ_HANDOFF(ifp, m, error);
4104 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4110 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4115 if (_IF_QFULL(ifq)) {
4117 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4122 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4123 if (m->m_flags & (M_BCAST|M_MCAST))
4124 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4125 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4127 _IF_ENQUEUE(ifq, m);
4129 if (ifp != NULL && !active)
4130 (*(ifp)->if_start)(ifp);
4135 if_register_com_alloc(u_char type,
4136 if_com_alloc_t *a, if_com_free_t *f)
4139 KASSERT(if_com_alloc[type] == NULL,
4140 ("if_register_com_alloc: %d already registered", type));
4141 KASSERT(if_com_free[type] == NULL,
4142 ("if_register_com_alloc: %d free already registered", type));
4144 if_com_alloc[type] = a;
4145 if_com_free[type] = f;
4149 if_deregister_com_alloc(u_char type)
4152 KASSERT(if_com_alloc[type] != NULL,
4153 ("if_deregister_com_alloc: %d not registered", type));
4154 KASSERT(if_com_free[type] != NULL,
4155 ("if_deregister_com_alloc: %d free not registered", type));
4158 * Ensure all pending EPOCH(9) callbacks have been executed. This
4159 * fixes issues about late invocation of if_destroy(), which leads
4160 * to memory leak from if_com_alloc[type] allocated if_l2com.
4162 NET_EPOCH_DRAIN_CALLBACKS();
4164 if_com_alloc[type] = NULL;
4165 if_com_free[type] = NULL;
4168 /* API for driver access to network stack owned ifnet.*/
4170 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4174 oldbrate = ifp->if_baudrate;
4175 ifp->if_baudrate = baudrate;
4180 if_getbaudrate(const if_t ifp)
4182 return (ifp->if_baudrate);
4186 if_setcapabilities(if_t ifp, int capabilities)
4188 ifp->if_capabilities = capabilities;
4193 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4195 ifp->if_capabilities &= ~clearbit;
4196 ifp->if_capabilities |= setbit;
4201 if_getcapabilities(const if_t ifp)
4203 return (ifp->if_capabilities);
4207 if_setcapenable(if_t ifp, int capabilities)
4209 ifp->if_capenable = capabilities;
4214 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4216 ifp->if_capenable &= ~clearcap;
4217 ifp->if_capenable |= setcap;
4222 if_setcapabilities2(if_t ifp, int capabilities)
4224 ifp->if_capabilities2 = capabilities;
4229 if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
4231 ifp->if_capabilities2 &= ~clearbit;
4232 ifp->if_capabilities2 |= setbit;
4237 if_getcapabilities2(const if_t ifp)
4239 return (ifp->if_capabilities2);
4243 if_setcapenable2(if_t ifp, int capabilities2)
4245 ifp->if_capenable2 = capabilities2;
4250 if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
4252 ifp->if_capenable2 &= ~clearcap;
4253 ifp->if_capenable2 |= setcap;
4258 if_getdname(const if_t ifp)
4260 return (ifp->if_dname);
4264 if_setdname(if_t ifp, const char *dname)
4266 ifp->if_dname = dname;
4272 return (ifp->if_xname);
4276 if_setname(if_t ifp, const char *name)
4278 if (strlen(name) > sizeof(ifp->if_xname) - 1)
4279 return (ENAMETOOLONG);
4280 strcpy(ifp->if_xname, name);
4286 if_togglecapenable(if_t ifp, int togglecap)
4288 ifp->if_capenable ^= togglecap;
4293 if_getcapenable(const if_t ifp)
4295 return (ifp->if_capenable);
4299 if_togglecapenable2(if_t ifp, int togglecap)
4301 ifp->if_capenable2 ^= togglecap;
4306 if_getcapenable2(const if_t ifp)
4308 return (ifp->if_capenable2);
4312 if_getdunit(const if_t ifp)
4314 return (ifp->if_dunit);
4318 if_getindex(const if_t ifp)
4320 return (ifp->if_index);
4324 if_getidxgen(const if_t ifp)
4326 return (ifp->if_idxgen);
4330 if_getdescr(if_t ifp)
4332 return (ifp->if_description);
4336 if_setdescr(if_t ifp, char *descrbuf)
4338 sx_xlock(&ifdescr_sx);
4339 char *odescrbuf = ifp->if_description;
4340 ifp->if_description = descrbuf;
4341 sx_xunlock(&ifdescr_sx);
4343 if_freedescr(odescrbuf);
4347 if_allocdescr(size_t sz, int malloc_flag)
4349 malloc_flag &= (M_WAITOK | M_NOWAIT);
4350 return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4354 if_freedescr(char *descrbuf)
4356 free(descrbuf, M_IFDESCR);
4360 if_getalloctype(const if_t ifp)
4362 return (ifp->if_alloctype);
4366 if_setlastchange(if_t ifp)
4368 getmicrotime(&ifp->if_lastchange);
4372 * This is largely undesirable because it ties ifnet to a device, but does
4373 * provide flexiblity for an embedded product vendor. Should be used with
4374 * the understanding that it violates the interface boundaries, and should be
4375 * a last resort only.
4378 if_setdev(if_t ifp, void *dev)
4384 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4386 ifp->if_drv_flags &= ~clear_flags;
4387 ifp->if_drv_flags |= set_flags;
4393 if_getdrvflags(const if_t ifp)
4395 return (ifp->if_drv_flags);
4399 if_setdrvflags(if_t ifp, int flags)
4401 ifp->if_drv_flags = flags;
4406 if_setflags(if_t ifp, int flags)
4408 ifp->if_flags = flags;
4413 if_setflagbits(if_t ifp, int set, int clear)
4415 ifp->if_flags &= ~clear;
4416 ifp->if_flags |= set;
4421 if_getflags(const if_t ifp)
4423 return (ifp->if_flags);
4427 if_clearhwassist(if_t ifp)
4429 ifp->if_hwassist = 0;
4434 if_sethwassistbits(if_t ifp, int toset, int toclear)
4436 ifp->if_hwassist &= ~toclear;
4437 ifp->if_hwassist |= toset;
4443 if_sethwassist(if_t ifp, int hwassist_bit)
4445 ifp->if_hwassist = hwassist_bit;
4450 if_gethwassist(const if_t ifp)
4452 return (ifp->if_hwassist);
4456 if_togglehwassist(if_t ifp, int toggle_bits)
4458 ifp->if_hwassist ^= toggle_bits;
4463 if_setmtu(if_t ifp, int mtu)
4470 if_notifymtu(if_t ifp)
4479 if_getmtu(const if_t ifp)
4481 return (ifp->if_mtu);
4485 if_getmtu_family(const if_t ifp, int family)
4489 SLIST_FOREACH(dp, &domains, dom_next) {
4490 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4491 return (dp->dom_ifmtu(ifp));
4494 return (ifp->if_mtu);
4498 * Methods for drivers to access interface unicast and multicast
4499 * link level addresses. Driver shall not know 'struct ifaddr' neither
4500 * 'struct ifmultiaddr'.
4503 if_lladdr_count(if_t ifp)
4505 struct epoch_tracker et;
4510 NET_EPOCH_ENTER(et);
4511 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4512 if (ifa->ifa_addr->sa_family == AF_LINK)
4520 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4529 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4530 error = cb(ifp, cb_arg);
4539 * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4540 * Stops iteration if @cb returns non-zero error code.
4541 * Returns the last error code from @cb.
4542 * @match_cb: optional match callback limiting the iteration to only matched interfaces
4543 * @match_arg: argument to pass to @match_cb
4544 * @cb: iteration callback
4545 * @cb_arg: argument to pass to @cb
4548 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4551 int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4552 struct ifnet **match_array = NULL;
4558 struct ifnet **new_array;
4559 int new_size = array_size;
4560 struct epoch_tracker et;
4563 while (new_size < match_count)
4565 new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4566 if (match_array != NULL)
4567 memcpy(new_array, match_array, array_size * sizeof(void *));
4568 free(match_array, M_TEMP);
4569 match_array = new_array;
4570 array_size = new_size;
4573 NET_EPOCH_ENTER(et);
4574 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4575 if (match_cb != NULL && !match_cb(ifp, match_arg))
4577 if (match_count < array_size) {
4578 if (if_try_ref(ifp))
4579 match_array[match_count++] = ifp;
4585 if (match_count > array_size) {
4586 for (int i = 0; i < array_size; i++)
4587 if_rele(match_array[i]);
4590 for (int i = 0; i < match_count; i++) {
4592 error = cb(match_array[i], cb_arg);
4593 if_rele(match_array[i]);
4595 free(match_array, M_TEMP);
4605 * Uses just 1 pointer of the 4 available in the public struct.
4608 if_iter_start(struct if_iter *iter)
4614 bzero(iter, sizeof(*iter));
4615 ifp = CK_STAILQ_FIRST(&V_ifnet);
4617 iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4619 iter->context[0] = NULL;
4624 if_iter_next(struct if_iter *iter)
4626 if_t cur_ifp = iter->context[0];
4628 if (cur_ifp != NULL)
4629 iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4634 if_iter_finish(struct if_iter *iter)
4636 /* Nothing to do here for now. */
4640 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4642 struct epoch_tracker et;
4649 NET_EPOCH_ENTER(et);
4650 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4651 if (ifa->ifa_addr->sa_family != AF_LINK)
4653 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4662 if_llmaddr_count(if_t ifp)
4664 struct epoch_tracker et;
4665 struct ifmultiaddr *ifma;
4669 NET_EPOCH_ENTER(et);
4670 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4671 if (ifma->ifma_addr->sa_family == AF_LINK)
4679 if_maddr_empty(if_t ifp)
4682 return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4686 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4688 struct epoch_tracker et;
4689 struct ifmultiaddr *ifma;
4695 NET_EPOCH_ENTER(et);
4696 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4697 if (ifma->ifma_addr->sa_family != AF_LINK)
4699 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4708 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4710 struct epoch_tracker et;
4717 NET_EPOCH_ENTER(et);
4718 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4719 if (ifa->ifa_addr->sa_family != type)
4721 count += (*cb)(cb_arg, ifa, count);
4729 ifa_iter_start(if_t ifp, struct ifa_iter *iter)
4735 bzero(iter, sizeof(*iter));
4736 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
4738 iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4740 iter->context[0] = NULL;
4745 ifa_iter_next(struct ifa_iter *iter)
4747 struct ifaddr *ifa = iter->context[0];
4750 iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4755 ifa_iter_finish(struct ifa_iter *iter)
4757 /* Nothing to do here for now. */
4761 if_setsoftc(if_t ifp, void *softc)
4763 ifp->if_softc = softc;
4768 if_getsoftc(const if_t ifp)
4770 return (ifp->if_softc);
4774 if_setrcvif(struct mbuf *m, if_t ifp)
4777 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4778 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4782 if_setvtag(struct mbuf *m, uint16_t tag)
4784 m->m_pkthdr.ether_vtag = tag;
4788 if_getvtag(struct mbuf *m)
4790 return (m->m_pkthdr.ether_vtag);
4794 if_sendq_empty(if_t ifp)
4796 return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4800 if_getifaddr(const if_t ifp)
4802 return (ifp->if_addr);
4806 if_getamcount(const if_t ifp)
4808 return (ifp->if_amcount);
4812 if_setsendqready(if_t ifp)
4814 IFQ_SET_READY(&ifp->if_snd);
4819 if_setsendqlen(if_t ifp, int tx_desc_count)
4821 IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4822 ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4827 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4829 ifp->if_netmap = na;
4832 struct netmap_adapter *
4833 if_getnetmapadapter(if_t ifp)
4835 return (ifp->if_netmap);
4839 if_vlantrunkinuse(if_t ifp)
4841 return (ifp->if_vlantrunk != NULL);
4845 if_init(if_t ifp, void *ctx)
4847 (*ifp->if_init)(ctx);
4851 if_input(if_t ifp, struct mbuf* sendmp)
4853 (*ifp->if_input)(ifp, sendmp);
4857 if_transmit(if_t ifp, struct mbuf *m)
4859 return ((*ifp->if_transmit)(ifp, m));
4863 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4865 if (ifp->if_resolvemulti == NULL)
4866 return (EOPNOTSUPP);
4868 return (ifp->if_resolvemulti(ifp, srcs, dst));
4872 if_ioctl(if_t ifp, u_long cmd, void *data)
4874 return (ifp->if_ioctl(ifp, cmd, data));
4878 if_dequeue(if_t ifp)
4882 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4887 if_sendq_prepend(if_t ifp, struct mbuf *m)
4889 IFQ_DRV_PREPEND(&ifp->if_snd, m);
4894 if_setifheaderlen(if_t ifp, int len)
4896 ifp->if_hdrlen = len;
4901 if_getlladdr(const if_t ifp)
4903 return (IF_LLADDR(ifp));
4907 if_gethandle(u_char type)
4909 return (if_alloc(type));
4913 if_vlancap(if_t ifp)
4915 VLAN_CAPABILITIES(ifp);
4919 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4921 ifp->if_hw_tsomax = if_hw_tsomax;
4926 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4928 ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4933 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4935 ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4940 if_gethwtsomax(const if_t ifp)
4942 return (ifp->if_hw_tsomax);
4946 if_gethwtsomaxsegcount(const if_t ifp)
4948 return (ifp->if_hw_tsomaxsegcount);
4952 if_gethwtsomaxsegsize(const if_t ifp)
4954 return (ifp->if_hw_tsomaxsegsize);
4958 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
4960 ifp->if_init = init_fn;
4964 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
4966 ifp->if_input = input_fn;
4970 if_getinputfn(if_t ifp)
4972 return (ifp->if_input);
4976 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
4978 ifp->if_ioctl = ioctl_fn;
4982 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
4984 ifp->if_output = output_fn;
4988 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
4990 ifp->if_start = start_fn;
4994 if_getstartfn(if_t ifp)
4996 return (ifp->if_start);
5000 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
5002 ifp->if_transmit = start_fn;
5006 if_gettransmitfn(if_t ifp)
5008 return (ifp->if_transmit);
5012 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
5014 ifp->if_qflush = flush_fn;
5018 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
5020 ifp->if_snd_tag_alloc = alloc_fn;
5024 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
5025 struct m_snd_tag **mstp)
5027 if (ifp->if_snd_tag_alloc == NULL)
5028 return (EOPNOTSUPP);
5029 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
5033 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
5035 ifp->if_get_counter = fn;
5039 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
5041 ifp->if_reassign = fn;
5045 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
5047 ifp->if_ratelimit_query = fn;
5051 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
5053 ifp->if_debugnet_methods = m;
5057 if_getmaclabel(if_t ifp)
5059 return (ifp->if_label);
5063 if_setmaclabel(if_t ifp, struct label *label)
5065 ifp->if_label = label;
5069 if_gettype(if_t ifp)
5071 return (ifp->if_type);
5075 if_getllsoftc(if_t ifp)
5077 return (ifp->if_llsoftc);
5081 if_setllsoftc(if_t ifp, void *llsoftc)
5083 ifp->if_llsoftc = llsoftc;
5087 if_getlinkstate(if_t ifp)
5089 return (ifp->if_link_state);
5093 if_getbroadcastaddr(if_t ifp)
5095 return (ifp->if_broadcastaddr);
5099 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
5101 ifp->if_broadcastaddr = addr;
5105 if_getnumadomain(if_t ifp)
5107 return (ifp->if_numa_domain);
5111 if_getcounter(if_t ifp, ift_counter counter)
5113 return (ifp->if_get_counter(ifp, counter));
5117 if_altq_is_enabled(if_t ifp)
5119 return (ALTQ_IS_ENABLED(&ifp->if_snd));
5123 if_getvnet(if_t ifp)
5125 return (ifp->if_vnet);
5129 if_getafdata(if_t ifp, int af)
5131 return (ifp->if_afdata[af]);
5137 return (ifp->if_fib);
5141 if_getaddrlen(if_t ifp)
5143 return (ifp->if_addrlen);
5149 return (ifp->if_bpf);
5152 struct ifvlantrunk *
5153 if_getvlantrunk(if_t ifp)
5155 return (ifp->if_vlantrunk);
5161 return (ifp->if_pcp);
5165 if_getl2com(if_t ifp)
5167 return (ifp->if_l2com);
5172 if_show_ifnet(struct ifnet *ifp)
5176 db_printf("%s:\n", ifp->if_xname);
5177 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
5178 IF_DB_PRINTF("%s", if_dname);
5179 IF_DB_PRINTF("%d", if_dunit);
5180 IF_DB_PRINTF("%s", if_description);
5181 IF_DB_PRINTF("%u", if_index);
5182 IF_DB_PRINTF("%d", if_idxgen);
5183 IF_DB_PRINTF("%u", if_refcount);
5184 IF_DB_PRINTF("%p", if_softc);
5185 IF_DB_PRINTF("%p", if_l2com);
5186 IF_DB_PRINTF("%p", if_llsoftc);
5187 IF_DB_PRINTF("%d", if_amcount);
5188 IF_DB_PRINTF("%p", if_addr);
5189 IF_DB_PRINTF("%p", if_broadcastaddr);
5190 IF_DB_PRINTF("%p", if_afdata);
5191 IF_DB_PRINTF("%d", if_afdata_initialized);
5192 IF_DB_PRINTF("%u", if_fib);
5193 IF_DB_PRINTF("%p", if_vnet);
5194 IF_DB_PRINTF("%p", if_home_vnet);
5195 IF_DB_PRINTF("%p", if_vlantrunk);
5196 IF_DB_PRINTF("%p", if_bpf);
5197 IF_DB_PRINTF("%u", if_pcount);
5198 IF_DB_PRINTF("%p", if_bridge);
5199 IF_DB_PRINTF("%p", if_lagg);
5200 IF_DB_PRINTF("%p", if_pf_kif);
5201 IF_DB_PRINTF("%p", if_carp);
5202 IF_DB_PRINTF("%p", if_label);
5203 IF_DB_PRINTF("%p", if_netmap);
5204 IF_DB_PRINTF("0x%08x", if_flags);
5205 IF_DB_PRINTF("0x%08x", if_drv_flags);
5206 IF_DB_PRINTF("0x%08x", if_capabilities);
5207 IF_DB_PRINTF("0x%08x", if_capenable);
5208 IF_DB_PRINTF("%p", if_snd.ifq_head);
5209 IF_DB_PRINTF("%p", if_snd.ifq_tail);
5210 IF_DB_PRINTF("%d", if_snd.ifq_len);
5211 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5212 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5213 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5214 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5215 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5216 IF_DB_PRINTF("%d", if_snd.altq_type);
5217 IF_DB_PRINTF("%x", if_snd.altq_flags);
5221 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5224 db_printf("usage: show ifnet <struct ifnet *>\n");
5228 if_show_ifnet((struct ifnet *)addr);
5231 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5236 for (idx = 1; idx <= if_index; idx++) {
5237 ifp = ifindex_table[idx].ife_ifnet;
5240 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);