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
32 * @(#)if.c 8.5 (Berkeley) 1/9/95
37 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
46 #include <sys/domainset.h>
49 #include <sys/epoch.h>
51 #include <sys/systm.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/protosw.h>
57 #include <sys/kernel.h>
59 #include <sys/refcount.h>
60 #include <sys/module.h>
62 #include <sys/rwlock.h>
63 #include <sys/sockio.h>
64 #include <sys/syslog.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/taskqueue.h>
68 #include <sys/domain.h>
76 #include <machine/stdarg.h>
80 #include <net/ethernet.h>
82 #include <net/if_arp.h>
83 #include <net/if_clone.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
86 #include <net/if_var.h>
87 #include <net/if_media.h>
88 #include <net/if_mib.h>
89 #include <net/if_private.h>
90 #include <net/if_vlan_var.h>
91 #include <net/radix.h>
92 #include <net/route.h>
93 #include <net/route/route_ctl.h>
96 #if defined(INET) || defined(INET6)
97 #include <net/ethernet.h>
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip.h>
101 #include <netinet/ip_carp.h>
103 #include <net/debugnet.h>
104 #include <netinet/if_ether.h>
107 #include <netinet6/in6_var.h>
108 #include <netinet6/in6_ifattach.h>
110 #endif /* INET || INET6 */
112 #include <security/mac/mac_framework.h>
115 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
116 * and ifr_ifru when it is used in SIOCGIFCONF.
118 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
119 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
121 __read_mostly epoch_t net_epoch_preempt;
122 #ifdef COMPAT_FREEBSD32
123 #include <sys/mount.h>
124 #include <compat/freebsd32/freebsd32.h>
126 struct ifreq_buffer32 {
127 uint32_t length; /* (size_t) */
128 uint32_t buffer; /* (void *) */
132 * Interface request structure used for socket
133 * ioctl's. All interface ioctl's must have parameter
134 * definitions which begin with ifr_name. The
135 * remainder may be interface specific.
138 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
140 struct sockaddr ifru_addr;
141 struct sockaddr ifru_dstaddr;
142 struct sockaddr ifru_broadaddr;
143 struct ifreq_buffer32 ifru_buffer;
154 u_char ifru_vlan_pcp;
157 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
158 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
159 __offsetof(struct ifreq32, ifr_ifru));
168 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
171 char ifd_name[IFNAMSIZ];
176 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
177 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
179 struct ifgroupreq32 {
180 char ifgr_name[IFNAMSIZ];
183 char ifgru_group[IFNAMSIZ];
184 uint32_t ifgru_groups;
187 #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
188 #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
189 #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
190 #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
192 struct ifmediareq32 {
193 char ifm_name[IFNAMSIZ];
199 uint32_t ifm_ulist; /* (int *) */
201 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
202 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
203 #endif /* COMPAT_FREEBSD32 */
207 #ifdef COMPAT_FREEBSD32
208 struct ifreq32 ifr32;
212 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
214 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
215 "Generic link-management");
217 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
218 &ifqmaxlen, 0, "max send queue size");
220 /* Log link state change events */
221 static int log_link_state_change = 1;
223 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
224 &log_link_state_change, 0,
225 "log interface link state change events");
227 /* Log promiscuous mode change events */
228 static int log_promisc_mode_change = 1;
230 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
231 &log_promisc_mode_change, 1,
232 "log promiscuous mode change events");
234 /* Interface description */
235 static unsigned int ifdescr_maxlen = 1024;
236 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
238 "administrative maximum length for interface description");
240 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
242 /* global sx for non-critical path ifdescr */
243 static struct sx ifdescr_sx;
244 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
246 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
247 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
248 /* These are external hooks for CARP. */
249 void (*carp_linkstate_p)(struct ifnet *ifp);
250 void (*carp_demote_adj_p)(int, char *);
251 int (*carp_master_p)(struct ifaddr *);
252 #if defined(INET) || defined(INET6)
253 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
254 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
255 const struct sockaddr *sa);
256 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
257 int (*carp_attach_p)(struct ifaddr *, int);
258 void (*carp_detach_p)(struct ifaddr *, bool);
261 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
264 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
265 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
266 const struct in6_addr *taddr);
269 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
272 * XXX: Style; these should be sorted alphabetically, and unprototyped
273 * static functions should be prototyped. Currently they are sorted by
276 static void if_attachdomain(void *);
277 static void if_attachdomain1(struct ifnet *);
278 static int ifconf(u_long, caddr_t);
279 static void if_input_default(struct ifnet *, struct mbuf *);
280 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
281 static int if_setflag(struct ifnet *, int, int, int *, int);
282 static int if_transmit_default(struct ifnet *ifp, struct mbuf *m);
283 static void if_unroute(struct ifnet *, int flag, int fam);
284 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
285 static void do_link_state_change(void *, int);
286 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
287 static int if_getgroupmembers(struct ifgroupreq *);
288 static void if_delgroups(struct ifnet *);
289 static void if_attach_internal(struct ifnet *, bool);
290 static int if_detach_internal(struct ifnet *, bool);
291 static void if_siocaddmulti(void *, int);
292 static void if_link_ifnet(struct ifnet *);
293 static bool if_unlink_ifnet(struct ifnet *, bool);
295 static int if_vmove(struct ifnet *, struct vnet *);
300 * XXX: declare here to avoid to include many inet6 related files..
301 * should be more generalized?
303 extern void nd6_setmtu(struct ifnet *);
306 /* ipsec helper hooks */
307 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
308 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
310 int ifqmaxlen = IFQ_MAXLEN;
311 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
312 VNET_DEFINE(struct ifgrouphead, ifg_head);
314 /* Table of ifnet by index. */
316 static int if_indexlim = 8;
317 static struct ifindex_entry {
318 struct ifnet *ife_ifnet;
322 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
323 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
324 "Variables global to all interfaces");
326 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
331 for (int i = 1; i <= if_index; i++)
332 if (ifindex_table[i].ife_ifnet != NULL &&
333 ifindex_table[i].ife_ifnet->if_vnet == curvnet)
337 return (sysctl_handle_int(oidp, &rv, 0, req));
339 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
340 CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
341 "Maximum known interface index");
344 * The global network interface list (V_ifnet) and related state (such as
345 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
346 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
348 struct sx ifnet_sxlock;
349 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
351 struct sx ifnet_detach_sxlock;
352 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
356 #define VNET_IS_SHUTTING_DOWN(_vnet) \
357 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
360 static if_com_alloc_t *if_com_alloc[256];
361 static if_com_free_t *if_com_free[256];
363 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
364 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
365 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
368 ifnet_byindex(u_int idx)
374 if (__predict_false(idx > if_index))
377 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
379 if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
386 ifnet_byindex_ref(u_int idx)
390 ifp = ifnet_byindex(idx);
391 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
393 if (!if_try_ref(ifp))
399 ifnet_byindexgen(uint16_t idx, uint16_t gen)
405 if (__predict_false(idx > if_index))
408 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
410 if (ifindex_table[idx].ife_gencnt == gen)
417 * Network interface utility routines.
419 * Routines with ifa_ifwith* names take sockaddr *'s as
424 if_init_idxtable(void *arg __unused)
427 ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
428 M_IFNET, M_WAITOK | M_ZERO);
430 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
433 vnet_if_init(const void *unused __unused)
436 CK_STAILQ_INIT(&V_ifnet);
437 CK_STAILQ_INIT(&V_ifg_head);
438 vnet_if_clone_init();
440 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
444 if_link_ifnet(struct ifnet *ifp)
448 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
450 curvnet->vnet_ifcnt++;
456 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
462 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
464 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
466 ifp->if_flags |= IFF_DYING;
471 curvnet->vnet_ifcnt--;
480 vnet_if_return(const void *unused __unused)
482 struct ifnet *ifp, *nifp;
483 struct ifnet **pending;
484 int found __diagused;
490 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
491 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
492 * if_detach_internal(), which waits for NET_EPOCH callbacks to
493 * complete. We can't do that from within NET_EPOCH.
495 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
496 * read/write lock. We cannot hold the lock as we call if_vmove()
497 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
502 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
503 M_IFNET, M_WAITOK | M_ZERO);
505 /* Return all inherited interfaces to their parent vnets. */
506 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
507 if (ifp->if_home_vnet != ifp->if_vnet) {
508 found = if_unlink_ifnet(ifp, true);
516 for (int j = 0; j < i; j++) {
517 sx_xlock(&ifnet_detach_sxlock);
518 if_vmove(pending[j], pending[j]->if_home_vnet);
519 sx_xunlock(&ifnet_detach_sxlock);
522 free(pending, M_IFNET);
524 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
525 vnet_if_return, NULL);
529 * Allocate a struct ifnet and an index for an interface. A layer 2
530 * common structure will also be allocated if an allocation routine is
531 * registered for the passed type.
533 static struct ifnet *
534 if_alloc_domain(u_char type, int numa_domain)
539 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
540 if (numa_domain == IF_NODOM)
541 ifp = malloc(sizeof(struct ifnet), M_IFNET,
544 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
545 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
547 ifp->if_alloctype = type;
548 ifp->if_numa_domain = numa_domain;
550 ifp->if_vnet = curvnet;
552 if (if_com_alloc[type] != NULL) {
553 ifp->if_l2com = if_com_alloc[type](type, ifp);
554 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
558 IF_ADDR_LOCK_INIT(ifp);
559 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
560 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
561 ifp->if_afdata_initialized = 0;
562 IF_AFDATA_LOCK_INIT(ifp);
563 CK_STAILQ_INIT(&ifp->if_addrhead);
564 CK_STAILQ_INIT(&ifp->if_multiaddrs);
565 CK_STAILQ_INIT(&ifp->if_groups);
569 ifq_init(&ifp->if_snd, ifp);
571 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
572 for (int i = 0; i < IFCOUNTERS; i++)
573 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
574 ifp->if_get_counter = if_get_counter_default;
575 ifp->if_pcp = IFNET_PCP_NONE;
577 /* Allocate an ifindex array entry. */
580 * Try to find an empty slot below if_index. If we fail, take the
583 for (idx = 1; idx <= if_index; idx++) {
584 if (ifindex_table[idx].ife_ifnet == NULL)
588 /* Catch if_index overflow. */
589 if (idx >= if_indexlim) {
590 struct ifindex_entry *new, *old;
593 newlim = if_indexlim * 2;
594 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
595 memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
597 ck_pr_store_ptr(&ifindex_table, new);
598 if_indexlim = newlim;
599 epoch_wait_preempt(net_epoch_preempt);
606 ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
607 ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
614 if_alloc_dev(u_char type, device_t dev)
618 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
619 return (if_alloc_domain(type, IF_NODOM));
620 return (if_alloc_domain(type, numa_domain));
624 if_alloc(u_char type)
627 return (if_alloc_domain(type, IF_NODOM));
630 * Do the actual work of freeing a struct ifnet, and layer 2 common
631 * structure. This call is made when the network epoch guarantees
632 * us that nobody holds a pointer to the interface.
635 if_free_deferred(epoch_context_t ctx)
637 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
639 KASSERT((ifp->if_flags & IFF_DYING),
640 ("%s: interface not dying", __func__));
642 if (if_com_free[ifp->if_alloctype] != NULL)
643 if_com_free[ifp->if_alloctype](ifp->if_l2com,
647 mac_ifnet_destroy(ifp);
649 IF_AFDATA_DESTROY(ifp);
650 IF_ADDR_LOCK_DESTROY(ifp);
651 ifq_delete(&ifp->if_snd);
653 for (int i = 0; i < IFCOUNTERS; i++)
654 counter_u64_free(ifp->if_counters[i]);
656 if_freedescr(ifp->if_description);
657 free(ifp->if_hw_addr, M_IFADDR);
662 * Deregister an interface and free the associated storage.
665 if_free(struct ifnet *ifp)
668 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
671 * XXXGL: An interface index is really an alias to ifp pointer.
672 * Why would we clear the alias now, and not in the deferred
673 * context? Indeed there is nothing wrong with some network
674 * thread obtaining ifp via ifnet_byindex() inside the network
675 * epoch and then dereferencing ifp while we perform if_free(),
676 * and after if_free() finished, too.
678 * This early index freeing was important back when ifindex was
679 * virtualized and interface would outlive the vnet.
682 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
683 ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
684 ifindex_table[ifp->if_index].ife_gencnt++;
685 while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
689 if (refcount_release(&ifp->if_refcount))
690 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
694 * Interfaces to keep an ifnet type-stable despite the possibility of the
695 * driver calling if_free(). If there are additional references, we defer
696 * freeing the underlying data structure.
699 if_ref(struct ifnet *ifp)
701 u_int old __diagused;
703 /* We don't assert the ifnet list lock here, but arguably should. */
704 old = refcount_acquire(&ifp->if_refcount);
705 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
709 if_try_ref(struct ifnet *ifp)
712 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
716 if_rele(struct ifnet *ifp)
719 if (!refcount_release(&ifp->if_refcount))
721 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
725 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
728 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
730 if (ifq->ifq_maxlen == 0)
731 ifq->ifq_maxlen = ifqmaxlen;
734 ifq->altq_disc = NULL;
735 ifq->altq_flags &= ALTQF_CANTCHANGE;
736 ifq->altq_tbr = NULL;
741 ifq_delete(struct ifaltq *ifq)
743 mtx_destroy(&ifq->ifq_mtx);
747 * Perform generic interface initialization tasks and attach the interface
748 * to the list of "active" interfaces. If vmove flag is set on entry
749 * to if_attach_internal(), perform only a limited subset of initialization
750 * tasks, given that we are moving from one vnet to another an ifnet which
751 * has already been fully initialized.
753 * Note that if_detach_internal() removes group membership unconditionally
754 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
755 * Thus, when if_vmove() is applied to a cloned interface, group membership
756 * is lost while a cloned one always joins a group whose name is
757 * ifc->ifc_name. To recover this after if_detach_internal() and
758 * if_attach_internal(), the cloner should be specified to
759 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
760 * attempts to join a group whose name is ifc->ifc_name.
763 * - The decision to return void and thus require this function to
764 * succeed is questionable.
765 * - We should probably do more sanity checking. For instance we don't
766 * do anything to insure if_xname is unique or non-empty.
769 if_attach(struct ifnet *ifp)
772 if_attach_internal(ifp, false);
776 * Compute the least common TSO limit.
779 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
782 * 1) If there is no limit currently, take the limit from
783 * the network adapter.
785 * 2) If the network adapter has a limit below the current
788 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
789 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
790 pmax->tsomaxbytes = ifp->if_hw_tsomax;
792 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
793 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
794 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
796 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
797 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
798 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
803 * Update TSO limit of a network adapter.
805 * Returns zero if no change. Else non-zero.
808 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
811 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
812 ifp->if_hw_tsomax = pmax->tsomaxbytes;
815 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
816 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
819 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
820 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
827 if_attach_internal(struct ifnet *ifp, bool vmove)
829 unsigned socksize, ifasize;
830 int namelen, masklen;
831 struct sockaddr_dl *sdl;
834 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
837 ifp->if_vnet = curvnet;
838 if (ifp->if_home_vnet == NULL)
839 ifp->if_home_vnet = curvnet;
842 if_addgroup(ifp, IFG_ALL);
845 /* Restore group membership for cloned interface. */
847 if_clone_restoregroup(ifp);
850 getmicrotime(&ifp->if_lastchange);
851 ifp->if_epoch = time_uptime;
853 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
854 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
855 ("transmit and qflush must both either be set or both be NULL"));
856 if (ifp->if_transmit == NULL) {
857 ifp->if_transmit = if_transmit_default;
858 ifp->if_qflush = if_qflush;
860 if (ifp->if_input == NULL)
861 ifp->if_input = if_input_default;
863 if (ifp->if_requestencap == NULL)
864 ifp->if_requestencap = if_requestencap_default;
868 mac_ifnet_create(ifp);
872 * Create a Link Level name for this device.
874 namelen = strlen(ifp->if_xname);
876 * Always save enough space for any possiable name so we
877 * can do a rename in place later.
879 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
880 socksize = masklen + ifp->if_addrlen;
881 if (socksize < sizeof(*sdl))
882 socksize = sizeof(*sdl);
883 socksize = roundup2(socksize, sizeof(long));
884 ifasize = sizeof(*ifa) + 2 * socksize;
885 ifa = ifa_alloc(ifasize, M_WAITOK);
886 sdl = (struct sockaddr_dl *)(ifa + 1);
887 sdl->sdl_len = socksize;
888 sdl->sdl_family = AF_LINK;
889 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
890 sdl->sdl_nlen = namelen;
891 sdl->sdl_index = ifp->if_index;
892 sdl->sdl_type = ifp->if_type;
895 ifa->ifa_addr = (struct sockaddr *)sdl;
896 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
897 ifa->ifa_netmask = (struct sockaddr *)sdl;
898 sdl->sdl_len = masklen;
900 sdl->sdl_data[--namelen] = 0xff;
901 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
902 /* Reliably crash if used uninitialized. */
903 ifp->if_broadcastaddr = NULL;
905 if (ifp->if_type == IFT_ETHER) {
906 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
910 #if defined(INET) || defined(INET6)
911 /* Use defaults for TSO, if nothing is set */
912 if (ifp->if_hw_tsomax == 0 &&
913 ifp->if_hw_tsomaxsegcount == 0 &&
914 ifp->if_hw_tsomaxsegsize == 0) {
916 * The TSO defaults needs to be such that an
917 * NFS mbuf list of 35 mbufs totalling just
918 * below 64K works and that a chain of mbufs
919 * can be defragged into at most 32 segments:
921 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
922 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
923 ifp->if_hw_tsomaxsegcount = 35;
924 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
926 /* XXX some drivers set IFCAP_TSO after ethernet attach */
927 if (ifp->if_capabilities & IFCAP_TSO) {
928 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
930 ifp->if_hw_tsomaxsegcount,
931 ifp->if_hw_tsomaxsegsize);
939 * Update the interface index in the link layer address
942 for (ifa = ifp->if_addr; ifa != NULL;
943 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
944 if (ifa->ifa_addr->sa_family == AF_LINK) {
945 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
946 sdl->sdl_index = ifp->if_index;
954 if (domain_init_status >= 2)
955 if_attachdomain1(ifp);
957 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
958 if (IS_DEFAULT_VNET(curvnet))
959 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
963 if_epochalloc(void *dummy __unused)
966 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
968 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
971 if_attachdomain(void *dummy)
975 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
976 if_attachdomain1(ifp);
978 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
979 if_attachdomain, NULL);
982 if_attachdomain1(struct ifnet *ifp)
987 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
988 * cannot lock ifp->if_afdata initialization, entirely.
991 if (ifp->if_afdata_initialized >= domain_init_status) {
992 IF_AFDATA_UNLOCK(ifp);
993 log(LOG_WARNING, "%s called more than once on %s\n",
994 __func__, ifp->if_xname);
997 ifp->if_afdata_initialized = domain_init_status;
998 IF_AFDATA_UNLOCK(ifp);
1000 /* address family dependent data region */
1001 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1002 SLIST_FOREACH(dp, &domains, dom_next) {
1003 if (dp->dom_ifattach)
1004 ifp->if_afdata[dp->dom_family] =
1005 (*dp->dom_ifattach)(ifp);
1010 * Remove any unicast or broadcast network addresses from an interface.
1013 if_purgeaddrs(struct ifnet *ifp)
1019 * Need to leave multicast addresses of proxy NDP llentries
1020 * before in6_purgeifaddr() because the llentries are keys
1021 * for in6_multi objects of proxy NDP entries.
1022 * in6_purgeifaddr()s clean up llentries including proxy NDPs
1023 * then we would lose the keys if they are called earlier.
1025 in6_purge_proxy_ndp(ifp);
1028 struct epoch_tracker et;
1030 NET_EPOCH_ENTER(et);
1031 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1032 if (ifa->ifa_addr->sa_family != AF_LINK)
1040 /* XXX: Ugly!! ad hoc just for INET */
1041 if (ifa->ifa_addr->sa_family == AF_INET) {
1042 struct ifaliasreq ifr;
1044 bzero(&ifr, sizeof(ifr));
1045 ifr.ifra_addr = *ifa->ifa_addr;
1046 if (ifa->ifa_dstaddr)
1047 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1048 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1054 if (ifa->ifa_addr->sa_family == AF_INET6) {
1055 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1056 /* ifp_addrhead is already updated */
1061 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1062 IF_ADDR_WUNLOCK(ifp);
1068 * Remove any multicast network addresses from an interface when an ifnet
1072 if_purgemaddrs(struct ifnet *ifp)
1074 struct ifmultiaddr *ifma;
1077 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1078 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1079 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1080 if_delmulti_locked(ifp, ifma, 1);
1082 IF_ADDR_WUNLOCK(ifp);
1086 * Detach an interface, removing it from the list of "active" interfaces.
1087 * If vmove flag is set on entry to if_detach_internal(), perform only a
1088 * limited subset of cleanup tasks, given that we are moving an ifnet from
1089 * one vnet to another, where it must be fully operational.
1091 * XXXRW: There are some significant questions about event ordering, and
1092 * how to prevent things from starting to use the interface during detach.
1095 if_detach(struct ifnet *ifp)
1099 CURVNET_SET_QUIET(ifp->if_vnet);
1100 found = if_unlink_ifnet(ifp, false);
1102 sx_xlock(&ifnet_detach_sxlock);
1103 if_detach_internal(ifp, false);
1104 sx_xunlock(&ifnet_detach_sxlock);
1110 * The vmove flag, if set, indicates that we are called from a callpath
1111 * that is moving an interface to a different vnet instance.
1113 * The shutdown flag, if set, indicates that we are called in the
1114 * process of shutting down a vnet instance. Currently only the
1115 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1116 * on a vnet instance shutdown without this flag being set, e.g., when
1117 * the cloned interfaces are destoyed as first thing of teardown.
1120 if_detach_internal(struct ifnet *ifp, bool vmove)
1128 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
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_setdescr(if_t ifp, char *descrbuf)
4332 sx_xlock(&ifdescr_sx);
4333 char *odescrbuf = ifp->if_description;
4334 ifp->if_description = descrbuf;
4335 sx_xunlock(&ifdescr_sx);
4337 if_freedescr(odescrbuf);
4341 if_allocdescr(size_t sz, int malloc_flag)
4343 malloc_flag &= (M_WAITOK | M_NOWAIT);
4344 return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4348 if_freedescr(char *descrbuf)
4350 free(descrbuf, M_IFDESCR);
4354 if_getalloctype(const if_t ifp)
4356 return (ifp->if_alloctype);
4360 * This is largely undesirable because it ties ifnet to a device, but does
4361 * provide flexiblity for an embedded product vendor. Should be used with
4362 * the understanding that it violates the interface boundaries, and should be
4363 * a last resort only.
4366 if_setdev(if_t ifp, void *dev)
4372 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4374 ifp->if_drv_flags &= ~clear_flags;
4375 ifp->if_drv_flags |= set_flags;
4381 if_getdrvflags(const if_t ifp)
4383 return (ifp->if_drv_flags);
4387 if_setdrvflags(if_t ifp, int flags)
4389 ifp->if_drv_flags = flags;
4394 if_setflags(if_t ifp, int flags)
4396 ifp->if_flags = flags;
4401 if_setflagbits(if_t ifp, int set, int clear)
4403 ifp->if_flags &= ~clear;
4404 ifp->if_flags |= set;
4409 if_getflags(const if_t ifp)
4411 return (ifp->if_flags);
4415 if_clearhwassist(if_t ifp)
4417 ifp->if_hwassist = 0;
4422 if_sethwassistbits(if_t ifp, int toset, int toclear)
4424 ifp->if_hwassist &= ~toclear;
4425 ifp->if_hwassist |= toset;
4431 if_sethwassist(if_t ifp, int hwassist_bit)
4433 ifp->if_hwassist = hwassist_bit;
4438 if_gethwassist(const if_t ifp)
4440 return (ifp->if_hwassist);
4444 if_togglehwassist(if_t ifp, int toggle_bits)
4446 ifp->if_hwassist ^= toggle_bits;
4451 if_setmtu(if_t ifp, int mtu)
4458 if_notifymtu(if_t ifp)
4467 if_getmtu(const if_t ifp)
4469 return (ifp->if_mtu);
4473 if_getmtu_family(const if_t ifp, int family)
4477 SLIST_FOREACH(dp, &domains, dom_next) {
4478 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4479 return (dp->dom_ifmtu(ifp));
4482 return (ifp->if_mtu);
4486 * Methods for drivers to access interface unicast and multicast
4487 * link level addresses. Driver shall not know 'struct ifaddr' neither
4488 * 'struct ifmultiaddr'.
4491 if_lladdr_count(if_t ifp)
4493 struct epoch_tracker et;
4498 NET_EPOCH_ENTER(et);
4499 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4500 if (ifa->ifa_addr->sa_family == AF_LINK)
4508 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4517 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4518 error = cb(ifp, cb_arg);
4527 * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4528 * Stops iteration if @cb returns non-zero error code.
4529 * Returns the last error code from @cb.
4530 * @match_cb: optional match callback limiting the iteration to only matched interfaces
4531 * @match_arg: argument to pass to @match_cb
4532 * @cb: iteration callback
4533 * @cb_arg: argument to pass to @cb
4536 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4539 int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4540 struct ifnet **match_array = NULL;
4546 struct ifnet **new_array;
4547 int new_size = array_size;
4548 struct epoch_tracker et;
4551 while (new_size < match_count)
4553 new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4554 if (match_array != NULL)
4555 memcpy(new_array, match_array, array_size * sizeof(void *));
4556 free(match_array, M_TEMP);
4557 match_array = new_array;
4558 array_size = new_size;
4561 NET_EPOCH_ENTER(et);
4562 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4563 if (match_cb != NULL && !match_cb(ifp, match_arg))
4565 if (match_count < array_size) {
4566 if (if_try_ref(ifp))
4567 match_array[match_count++] = ifp;
4573 if (match_count > array_size) {
4574 for (int i = 0; i < array_size; i++)
4575 if_rele(match_array[i]);
4578 for (int i = 0; i < match_count; i++) {
4580 error = cb(match_array[i], cb_arg);
4581 if_rele(match_array[i]);
4583 free(match_array, M_TEMP);
4593 * Uses just 1 pointer of the 4 available in the public struct.
4596 if_iter_start(struct if_iter *iter)
4602 bzero(iter, sizeof(*iter));
4603 ifp = CK_STAILQ_FIRST(&V_ifnet);
4605 iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4607 iter->context[0] = NULL;
4612 if_iter_next(struct if_iter *iter)
4614 if_t cur_ifp = iter->context[0];
4616 if (cur_ifp != NULL)
4617 iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4622 if_iter_finish(struct if_iter *iter)
4624 /* Nothing to do here for now. */
4628 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4630 struct epoch_tracker et;
4637 NET_EPOCH_ENTER(et);
4638 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4639 if (ifa->ifa_addr->sa_family != AF_LINK)
4641 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4650 if_llmaddr_count(if_t ifp)
4652 struct epoch_tracker et;
4653 struct ifmultiaddr *ifma;
4657 NET_EPOCH_ENTER(et);
4658 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4659 if (ifma->ifma_addr->sa_family == AF_LINK)
4667 if_maddr_empty(if_t ifp)
4670 return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4674 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4676 struct epoch_tracker et;
4677 struct ifmultiaddr *ifma;
4683 NET_EPOCH_ENTER(et);
4684 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4685 if (ifma->ifma_addr->sa_family != AF_LINK)
4687 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4696 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4698 struct epoch_tracker et;
4705 NET_EPOCH_ENTER(et);
4706 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4707 if (ifa->ifa_addr->sa_family != type)
4709 count += (*cb)(cb_arg, ifa, count);
4717 if_setsoftc(if_t ifp, void *softc)
4719 ifp->if_softc = softc;
4724 if_getsoftc(const if_t ifp)
4726 return (ifp->if_softc);
4730 if_setrcvif(struct mbuf *m, if_t ifp)
4733 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4734 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4738 if_setvtag(struct mbuf *m, uint16_t tag)
4740 m->m_pkthdr.ether_vtag = tag;
4744 if_getvtag(struct mbuf *m)
4746 return (m->m_pkthdr.ether_vtag);
4750 if_sendq_empty(if_t ifp)
4752 return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4756 if_getifaddr(const if_t ifp)
4758 return (ifp->if_addr);
4762 if_getamcount(const if_t ifp)
4764 return (ifp->if_amcount);
4768 if_setsendqready(if_t ifp)
4770 IFQ_SET_READY(&ifp->if_snd);
4775 if_setsendqlen(if_t ifp, int tx_desc_count)
4777 IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4778 ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4783 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4785 ifp->if_netmap = na;
4788 struct netmap_adapter *
4789 if_getnetmapadapter(if_t ifp)
4791 return (ifp->if_netmap);
4795 if_vlantrunkinuse(if_t ifp)
4797 return (ifp->if_vlantrunk != NULL);
4801 if_init(if_t ifp, void *ctx)
4803 (*ifp->if_init)(ctx);
4807 if_input(if_t ifp, struct mbuf* sendmp)
4809 (*ifp->if_input)(ifp, sendmp);
4813 if_transmit(if_t ifp, struct mbuf *m)
4815 return ((*ifp->if_transmit)(ifp, m));
4819 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4821 if (ifp->if_resolvemulti == NULL)
4822 return (EOPNOTSUPP);
4824 return (ifp->if_resolvemulti(ifp, srcs, dst));
4828 if_dequeue(if_t ifp)
4832 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4837 if_sendq_prepend(if_t ifp, struct mbuf *m)
4839 IFQ_DRV_PREPEND(&ifp->if_snd, m);
4844 if_setifheaderlen(if_t ifp, int len)
4846 ifp->if_hdrlen = len;
4851 if_getlladdr(const if_t ifp)
4853 return (IF_LLADDR(ifp));
4857 if_gethandle(u_char type)
4859 return (if_alloc(type));
4863 if_bpfmtap(if_t ifp, struct mbuf *m)
4869 if_etherbpfmtap(if_t ifp, struct mbuf *m)
4871 ETHER_BPF_MTAP(ifp, m);
4875 if_vlancap(if_t ifp)
4877 VLAN_CAPABILITIES(ifp);
4881 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4883 ifp->if_hw_tsomax = if_hw_tsomax;
4888 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4890 ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4895 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4897 ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4902 if_gethwtsomax(const if_t ifp)
4904 return (ifp->if_hw_tsomax);
4908 if_gethwtsomaxsegcount(const if_t ifp)
4910 return (ifp->if_hw_tsomaxsegcount);
4914 if_gethwtsomaxsegsize(const if_t ifp)
4916 return (ifp->if_hw_tsomaxsegsize);
4920 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
4922 ifp->if_init = init_fn;
4926 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
4928 ifp->if_input = input_fn;
4932 if_getinputfn(if_t ifp)
4934 return (ifp->if_input);
4938 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
4940 ifp->if_ioctl = ioctl_fn;
4944 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
4946 ifp->if_output = output_fn;
4950 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
4952 ifp->if_start = start_fn;
4956 if_getstartfn(if_t ifp)
4958 return (ifp->if_start);
4962 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4964 ifp->if_transmit = start_fn;
4968 if_gettransmitfn(if_t ifp)
4970 return (ifp->if_transmit);
4974 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4976 ifp->if_qflush = flush_fn;
4980 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
4982 ifp->if_snd_tag_alloc = alloc_fn;
4986 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
4987 struct m_snd_tag **mstp)
4989 if (ifp->if_snd_tag_alloc == NULL)
4990 return (EOPNOTSUPP);
4991 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
4995 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4997 ifp->if_get_counter = fn;
5001 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
5003 ifp->if_reassign = fn;
5007 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
5009 ifp->if_ratelimit_query = fn;
5013 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
5015 ifp->if_debugnet_methods = m;
5019 if_getmaclabel(if_t ifp)
5021 return (ifp->if_label);
5025 if_setmaclabel(if_t ifp, struct label *label)
5027 ifp->if_label = label;
5031 if_gettype(if_t ifp)
5033 return (ifp->if_type);
5037 if_getllsoftc(if_t ifp)
5039 return (ifp->if_llsoftc);
5043 if_setllsoftc(if_t ifp, void *llsoftc)
5045 ifp->if_llsoftc = llsoftc;
5049 if_getlinkstate(if_t ifp)
5051 return (ifp->if_link_state);
5055 if_getbroadcastaddr(if_t ifp)
5057 return (ifp->if_broadcastaddr);
5061 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
5063 ifp->if_broadcastaddr = addr;
5067 if_getnumadomain(if_t ifp)
5069 return (ifp->if_numa_domain);
5073 if_getcounter(if_t ifp, ift_counter counter)
5075 return (ifp->if_get_counter(ifp, counter));
5079 if_altq_is_enabled(if_t ifp)
5081 return (ALTQ_IS_ENABLED(&ifp->if_snd));
5085 if_getvnet(if_t ifp)
5087 return (ifp->if_vnet);
5091 if_getafdata(if_t ifp, int af)
5093 return (ifp->if_afdata[af]);
5099 return (ifp->if_fib);
5103 if_getaddrlen(if_t ifp)
5105 return (ifp->if_addrlen);
5111 return (ifp->if_bpf);
5114 struct ifvlantrunk *
5115 if_getvlantrunk(if_t ifp)
5117 return (ifp->if_vlantrunk);
5123 return (ifp->if_pcp);
5127 if_getl2com(if_t ifp)
5129 return (ifp->if_l2com);
5134 if_show_ifnet(struct ifnet *ifp)
5138 db_printf("%s:\n", ifp->if_xname);
5139 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
5140 IF_DB_PRINTF("%s", if_dname);
5141 IF_DB_PRINTF("%d", if_dunit);
5142 IF_DB_PRINTF("%s", if_description);
5143 IF_DB_PRINTF("%u", if_index);
5144 IF_DB_PRINTF("%d", if_idxgen);
5145 IF_DB_PRINTF("%u", if_refcount);
5146 IF_DB_PRINTF("%p", if_softc);
5147 IF_DB_PRINTF("%p", if_l2com);
5148 IF_DB_PRINTF("%p", if_llsoftc);
5149 IF_DB_PRINTF("%d", if_amcount);
5150 IF_DB_PRINTF("%p", if_addr);
5151 IF_DB_PRINTF("%p", if_broadcastaddr);
5152 IF_DB_PRINTF("%p", if_afdata);
5153 IF_DB_PRINTF("%d", if_afdata_initialized);
5154 IF_DB_PRINTF("%u", if_fib);
5155 IF_DB_PRINTF("%p", if_vnet);
5156 IF_DB_PRINTF("%p", if_home_vnet);
5157 IF_DB_PRINTF("%p", if_vlantrunk);
5158 IF_DB_PRINTF("%p", if_bpf);
5159 IF_DB_PRINTF("%u", if_pcount);
5160 IF_DB_PRINTF("%p", if_bridge);
5161 IF_DB_PRINTF("%p", if_lagg);
5162 IF_DB_PRINTF("%p", if_pf_kif);
5163 IF_DB_PRINTF("%p", if_carp);
5164 IF_DB_PRINTF("%p", if_label);
5165 IF_DB_PRINTF("%p", if_netmap);
5166 IF_DB_PRINTF("0x%08x", if_flags);
5167 IF_DB_PRINTF("0x%08x", if_drv_flags);
5168 IF_DB_PRINTF("0x%08x", if_capabilities);
5169 IF_DB_PRINTF("0x%08x", if_capenable);
5170 IF_DB_PRINTF("%p", if_snd.ifq_head);
5171 IF_DB_PRINTF("%p", if_snd.ifq_tail);
5172 IF_DB_PRINTF("%d", if_snd.ifq_len);
5173 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5174 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5175 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5176 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5177 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5178 IF_DB_PRINTF("%d", if_snd.altq_type);
5179 IF_DB_PRINTF("%x", if_snd.altq_flags);
5183 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5186 db_printf("usage: show ifnet <struct ifnet *>\n");
5190 if_show_ifnet((struct ifnet *)addr);
5193 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5198 for (idx = 1; idx <= if_index; idx++) {
5199 ifp = ifindex_table[idx].ife_ifnet;
5202 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);