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 void if_route(struct ifnet *, int flag, int fam);
282 static int if_setflag(struct ifnet *, int, int, int *, int);
283 static int if_transmit_default(struct ifnet *ifp, struct mbuf *m);
284 static void if_unroute(struct ifnet *, int flag, int fam);
285 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
286 static void do_link_state_change(void *, int);
287 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
288 static int if_getgroupmembers(struct ifgroupreq *);
289 static void if_delgroups(struct ifnet *);
290 static void if_attach_internal(struct ifnet *, bool);
291 static int if_detach_internal(struct ifnet *, bool);
292 static void if_siocaddmulti(void *, int);
293 static void if_link_ifnet(struct ifnet *);
294 static bool if_unlink_ifnet(struct ifnet *, bool);
296 static int if_vmove(struct ifnet *, struct vnet *);
301 * XXX: declare here to avoid to include many inet6 related files..
302 * should be more generalized?
304 extern void nd6_setmtu(struct ifnet *);
307 /* ipsec helper hooks */
308 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
309 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
311 int ifqmaxlen = IFQ_MAXLEN;
312 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
313 VNET_DEFINE(struct ifgrouphead, ifg_head);
315 /* Table of ifnet by index. */
317 static int if_indexlim = 8;
318 static struct ifindex_entry {
319 struct ifnet *ife_ifnet;
323 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
324 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
325 "Variables global to all interfaces");
327 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
332 for (int i = 1; i <= if_index; i++)
333 if (ifindex_table[i].ife_ifnet != NULL &&
334 ifindex_table[i].ife_ifnet->if_vnet == curvnet)
338 return (sysctl_handle_int(oidp, &rv, 0, req));
340 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
341 CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
342 "Maximum known interface index");
345 * The global network interface list (V_ifnet) and related state (such as
346 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
347 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
349 struct sx ifnet_sxlock;
350 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
352 struct sx ifnet_detach_sxlock;
353 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
357 #define VNET_IS_SHUTTING_DOWN(_vnet) \
358 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
361 static if_com_alloc_t *if_com_alloc[256];
362 static if_com_free_t *if_com_free[256];
364 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
365 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
366 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
369 ifnet_byindex(u_int idx)
375 if (__predict_false(idx > if_index))
378 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
380 if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
387 ifnet_byindex_ref(u_int idx)
391 ifp = ifnet_byindex(idx);
392 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
394 if (!if_try_ref(ifp))
400 ifnet_byindexgen(uint16_t idx, uint16_t gen)
406 if (__predict_false(idx > if_index))
409 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
411 if (ifindex_table[idx].ife_gencnt == gen)
418 * Network interface utility routines.
420 * Routines with ifa_ifwith* names take sockaddr *'s as
425 if_init_idxtable(void *arg __unused)
428 ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
429 M_IFNET, M_WAITOK | M_ZERO);
431 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
434 vnet_if_init(const void *unused __unused)
437 CK_STAILQ_INIT(&V_ifnet);
438 CK_STAILQ_INIT(&V_ifg_head);
439 vnet_if_clone_init();
441 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
445 if_link_ifnet(struct ifnet *ifp)
449 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
451 curvnet->vnet_ifcnt++;
457 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
463 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
465 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
467 ifp->if_flags |= IFF_DYING;
472 curvnet->vnet_ifcnt--;
481 vnet_if_return(const void *unused __unused)
483 struct ifnet *ifp, *nifp;
484 struct ifnet **pending;
485 int found __diagused;
491 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
492 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
493 * if_detach_internal(), which waits for NET_EPOCH callbacks to
494 * complete. We can't do that from within NET_EPOCH.
496 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
497 * read/write lock. We cannot hold the lock as we call if_vmove()
498 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
503 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
504 M_IFNET, M_WAITOK | M_ZERO);
506 /* Return all inherited interfaces to their parent vnets. */
507 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
508 if (ifp->if_home_vnet != ifp->if_vnet) {
509 found = if_unlink_ifnet(ifp, true);
517 for (int j = 0; j < i; j++) {
518 sx_xlock(&ifnet_detach_sxlock);
519 if_vmove(pending[j], pending[j]->if_home_vnet);
520 sx_xunlock(&ifnet_detach_sxlock);
523 free(pending, M_IFNET);
525 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
526 vnet_if_return, NULL);
530 * Allocate a struct ifnet and an index for an interface. A layer 2
531 * common structure will also be allocated if an allocation routine is
532 * registered for the passed type.
534 static struct ifnet *
535 if_alloc_domain(u_char type, int numa_domain)
540 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
541 if (numa_domain == IF_NODOM)
542 ifp = malloc(sizeof(struct ifnet), M_IFNET,
545 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
546 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
548 ifp->if_alloctype = type;
549 ifp->if_numa_domain = numa_domain;
551 ifp->if_vnet = curvnet;
553 if (if_com_alloc[type] != NULL) {
554 ifp->if_l2com = if_com_alloc[type](type, ifp);
555 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
559 IF_ADDR_LOCK_INIT(ifp);
560 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
561 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
562 ifp->if_afdata_initialized = 0;
563 IF_AFDATA_LOCK_INIT(ifp);
564 CK_STAILQ_INIT(&ifp->if_addrhead);
565 CK_STAILQ_INIT(&ifp->if_multiaddrs);
566 CK_STAILQ_INIT(&ifp->if_groups);
570 ifq_init(&ifp->if_snd, ifp);
572 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
573 for (int i = 0; i < IFCOUNTERS; i++)
574 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
575 ifp->if_get_counter = if_get_counter_default;
576 ifp->if_pcp = IFNET_PCP_NONE;
578 /* Allocate an ifindex array entry. */
581 * Try to find an empty slot below if_index. If we fail, take the
584 for (idx = 1; idx <= if_index; idx++) {
585 if (ifindex_table[idx].ife_ifnet == NULL)
589 /* Catch if_index overflow. */
590 if (idx >= if_indexlim) {
591 struct ifindex_entry *new, *old;
594 newlim = if_indexlim * 2;
595 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
596 memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
598 ck_pr_store_ptr(&ifindex_table, new);
599 if_indexlim = newlim;
600 epoch_wait_preempt(net_epoch_preempt);
607 ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
608 ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
615 if_alloc_dev(u_char type, device_t dev)
619 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
620 return (if_alloc_domain(type, IF_NODOM));
621 return (if_alloc_domain(type, numa_domain));
625 if_alloc(u_char type)
628 return (if_alloc_domain(type, IF_NODOM));
631 * Do the actual work of freeing a struct ifnet, and layer 2 common
632 * structure. This call is made when the network epoch guarantees
633 * us that nobody holds a pointer to the interface.
636 if_free_deferred(epoch_context_t ctx)
638 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
640 KASSERT((ifp->if_flags & IFF_DYING),
641 ("%s: interface not dying", __func__));
643 if (if_com_free[ifp->if_alloctype] != NULL)
644 if_com_free[ifp->if_alloctype](ifp->if_l2com,
648 mac_ifnet_destroy(ifp);
650 IF_AFDATA_DESTROY(ifp);
651 IF_ADDR_LOCK_DESTROY(ifp);
652 ifq_delete(&ifp->if_snd);
654 for (int i = 0; i < IFCOUNTERS; i++)
655 counter_u64_free(ifp->if_counters[i]);
657 if_freedescr(ifp->if_description);
658 free(ifp->if_hw_addr, M_IFADDR);
663 * Deregister an interface and free the associated storage.
666 if_free(struct ifnet *ifp)
669 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
672 * XXXGL: An interface index is really an alias to ifp pointer.
673 * Why would we clear the alias now, and not in the deferred
674 * context? Indeed there is nothing wrong with some network
675 * thread obtaining ifp via ifnet_byindex() inside the network
676 * epoch and then dereferencing ifp while we perform if_free(),
677 * and after if_free() finished, too.
679 * This early index freeing was important back when ifindex was
680 * virtualized and interface would outlive the vnet.
683 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
684 ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
685 ifindex_table[ifp->if_index].ife_gencnt++;
686 while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
690 if (refcount_release(&ifp->if_refcount))
691 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
695 * Interfaces to keep an ifnet type-stable despite the possibility of the
696 * driver calling if_free(). If there are additional references, we defer
697 * freeing the underlying data structure.
700 if_ref(struct ifnet *ifp)
702 u_int old __diagused;
704 /* We don't assert the ifnet list lock here, but arguably should. */
705 old = refcount_acquire(&ifp->if_refcount);
706 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
710 if_try_ref(struct ifnet *ifp)
713 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
717 if_rele(struct ifnet *ifp)
720 if (!refcount_release(&ifp->if_refcount))
722 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
726 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
729 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
731 if (ifq->ifq_maxlen == 0)
732 ifq->ifq_maxlen = ifqmaxlen;
735 ifq->altq_disc = NULL;
736 ifq->altq_flags &= ALTQF_CANTCHANGE;
737 ifq->altq_tbr = NULL;
742 ifq_delete(struct ifaltq *ifq)
744 mtx_destroy(&ifq->ifq_mtx);
748 * Perform generic interface initialization tasks and attach the interface
749 * to the list of "active" interfaces. If vmove flag is set on entry
750 * to if_attach_internal(), perform only a limited subset of initialization
751 * tasks, given that we are moving from one vnet to another an ifnet which
752 * has already been fully initialized.
754 * Note that if_detach_internal() removes group membership unconditionally
755 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
756 * Thus, when if_vmove() is applied to a cloned interface, group membership
757 * is lost while a cloned one always joins a group whose name is
758 * ifc->ifc_name. To recover this after if_detach_internal() and
759 * if_attach_internal(), the cloner should be specified to
760 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
761 * attempts to join a group whose name is ifc->ifc_name.
764 * - The decision to return void and thus require this function to
765 * succeed is questionable.
766 * - We should probably do more sanity checking. For instance we don't
767 * do anything to insure if_xname is unique or non-empty.
770 if_attach(struct ifnet *ifp)
773 if_attach_internal(ifp, false);
777 * Compute the least common TSO limit.
780 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
783 * 1) If there is no limit currently, take the limit from
784 * the network adapter.
786 * 2) If the network adapter has a limit below the current
789 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
790 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
791 pmax->tsomaxbytes = ifp->if_hw_tsomax;
793 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
794 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
795 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
797 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
798 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
799 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
804 * Update TSO limit of a network adapter.
806 * Returns zero if no change. Else non-zero.
809 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
812 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
813 ifp->if_hw_tsomax = pmax->tsomaxbytes;
816 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
817 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
820 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
821 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
828 if_attach_internal(struct ifnet *ifp, bool vmove)
830 unsigned socksize, ifasize;
831 int namelen, masklen;
832 struct sockaddr_dl *sdl;
835 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
838 ifp->if_vnet = curvnet;
839 if (ifp->if_home_vnet == NULL)
840 ifp->if_home_vnet = curvnet;
843 if_addgroup(ifp, IFG_ALL);
846 /* Restore group membership for cloned interface. */
848 if_clone_restoregroup(ifp);
851 getmicrotime(&ifp->if_lastchange);
852 ifp->if_epoch = time_uptime;
854 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
855 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
856 ("transmit and qflush must both either be set or both be NULL"));
857 if (ifp->if_transmit == NULL) {
858 ifp->if_transmit = if_transmit_default;
859 ifp->if_qflush = if_qflush;
861 if (ifp->if_input == NULL)
862 ifp->if_input = if_input_default;
864 if (ifp->if_requestencap == NULL)
865 ifp->if_requestencap = if_requestencap_default;
869 mac_ifnet_create(ifp);
873 * Create a Link Level name for this device.
875 namelen = strlen(ifp->if_xname);
877 * Always save enough space for any possiable name so we
878 * can do a rename in place later.
880 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
881 socksize = masklen + ifp->if_addrlen;
882 if (socksize < sizeof(*sdl))
883 socksize = sizeof(*sdl);
884 socksize = roundup2(socksize, sizeof(long));
885 ifasize = sizeof(*ifa) + 2 * socksize;
886 ifa = ifa_alloc(ifasize, M_WAITOK);
887 sdl = (struct sockaddr_dl *)(ifa + 1);
888 sdl->sdl_len = socksize;
889 sdl->sdl_family = AF_LINK;
890 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
891 sdl->sdl_nlen = namelen;
892 sdl->sdl_index = ifp->if_index;
893 sdl->sdl_type = ifp->if_type;
896 ifa->ifa_addr = (struct sockaddr *)sdl;
897 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
898 ifa->ifa_netmask = (struct sockaddr *)sdl;
899 sdl->sdl_len = masklen;
901 sdl->sdl_data[--namelen] = 0xff;
902 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
903 /* Reliably crash if used uninitialized. */
904 ifp->if_broadcastaddr = NULL;
906 if (ifp->if_type == IFT_ETHER) {
907 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
911 #if defined(INET) || defined(INET6)
912 /* Use defaults for TSO, if nothing is set */
913 if (ifp->if_hw_tsomax == 0 &&
914 ifp->if_hw_tsomaxsegcount == 0 &&
915 ifp->if_hw_tsomaxsegsize == 0) {
917 * The TSO defaults needs to be such that an
918 * NFS mbuf list of 35 mbufs totalling just
919 * below 64K works and that a chain of mbufs
920 * can be defragged into at most 32 segments:
922 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
923 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
924 ifp->if_hw_tsomaxsegcount = 35;
925 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
927 /* XXX some drivers set IFCAP_TSO after ethernet attach */
928 if (ifp->if_capabilities & IFCAP_TSO) {
929 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
931 ifp->if_hw_tsomaxsegcount,
932 ifp->if_hw_tsomaxsegsize);
940 * Update the interface index in the link layer address
943 for (ifa = ifp->if_addr; ifa != NULL;
944 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
945 if (ifa->ifa_addr->sa_family == AF_LINK) {
946 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
947 sdl->sdl_index = ifp->if_index;
955 if (domain_init_status >= 2)
956 if_attachdomain1(ifp);
958 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
959 if (IS_DEFAULT_VNET(curvnet))
960 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
964 if_epochalloc(void *dummy __unused)
967 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
969 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
972 if_attachdomain(void *dummy)
976 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
977 if_attachdomain1(ifp);
979 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
980 if_attachdomain, NULL);
983 if_attachdomain1(struct ifnet *ifp)
988 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
989 * cannot lock ifp->if_afdata initialization, entirely.
992 if (ifp->if_afdata_initialized >= domain_init_status) {
993 IF_AFDATA_UNLOCK(ifp);
994 log(LOG_WARNING, "%s called more than once on %s\n",
995 __func__, ifp->if_xname);
998 ifp->if_afdata_initialized = domain_init_status;
999 IF_AFDATA_UNLOCK(ifp);
1001 /* address family dependent data region */
1002 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1003 SLIST_FOREACH(dp, &domains, dom_next) {
1004 if (dp->dom_ifattach)
1005 ifp->if_afdata[dp->dom_family] =
1006 (*dp->dom_ifattach)(ifp);
1011 * Remove any unicast or broadcast network addresses from an interface.
1014 if_purgeaddrs(struct ifnet *ifp)
1020 * Need to leave multicast addresses of proxy NDP llentries
1021 * before in6_purgeifaddr() because the llentries are keys
1022 * for in6_multi objects of proxy NDP entries.
1023 * in6_purgeifaddr()s clean up llentries including proxy NDPs
1024 * then we would lose the keys if they are called earlier.
1026 in6_purge_proxy_ndp(ifp);
1029 struct epoch_tracker et;
1031 NET_EPOCH_ENTER(et);
1032 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1033 if (ifa->ifa_addr->sa_family != AF_LINK)
1041 /* XXX: Ugly!! ad hoc just for INET */
1042 if (ifa->ifa_addr->sa_family == AF_INET) {
1043 struct ifaliasreq ifr;
1045 bzero(&ifr, sizeof(ifr));
1046 ifr.ifra_addr = *ifa->ifa_addr;
1047 if (ifa->ifa_dstaddr)
1048 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1049 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1055 if (ifa->ifa_addr->sa_family == AF_INET6) {
1056 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1057 /* ifp_addrhead is already updated */
1062 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1063 IF_ADDR_WUNLOCK(ifp);
1069 * Remove any multicast network addresses from an interface when an ifnet
1073 if_purgemaddrs(struct ifnet *ifp)
1075 struct ifmultiaddr *ifma;
1078 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1079 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1080 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1081 if_delmulti_locked(ifp, ifma, 1);
1083 IF_ADDR_WUNLOCK(ifp);
1087 * Detach an interface, removing it from the list of "active" interfaces.
1088 * If vmove flag is set on entry to if_detach_internal(), perform only a
1089 * limited subset of cleanup tasks, given that we are moving an ifnet from
1090 * one vnet to another, where it must be fully operational.
1092 * XXXRW: There are some significant questions about event ordering, and
1093 * how to prevent things from starting to use the interface during detach.
1096 if_detach(struct ifnet *ifp)
1100 CURVNET_SET_QUIET(ifp->if_vnet);
1101 found = if_unlink_ifnet(ifp, false);
1103 sx_xlock(&ifnet_detach_sxlock);
1104 if_detach_internal(ifp, false);
1105 sx_xunlock(&ifnet_detach_sxlock);
1111 * The vmove flag, if set, indicates that we are called from a callpath
1112 * that is moving an interface to a different vnet instance.
1114 * The shutdown flag, if set, indicates that we are called in the
1115 * process of shutting down a vnet instance. Currently only the
1116 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1117 * on a vnet instance shutdown without this flag being set, e.g., when
1118 * the cloned interfaces are destoyed as first thing of teardown.
1121 if_detach_internal(struct ifnet *ifp, bool vmove)
1129 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1133 * At this point we know the interface still was on the ifnet list
1134 * and we removed it so we are in a stable state.
1136 epoch_wait_preempt(net_epoch_preempt);
1139 * Ensure all pending EPOCH(9) callbacks have been executed. This
1140 * fixes issues about late destruction of multicast options
1141 * which lead to leave group calls, which in turn access the
1142 * belonging ifnet structure:
1144 NET_EPOCH_DRAIN_CALLBACKS();
1147 * In any case (destroy or vmove) detach us from the groups
1148 * and remove/wait for pending events on the taskq.
1149 * XXX-BZ in theory an interface could still enqueue a taskq change?
1153 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1154 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1160 * On VNET shutdown abort here as the stack teardown will do all
1161 * the work top-down for us.
1164 /* Give interface users the chance to clean up. */
1165 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1168 * In case of a vmove we are done here without error.
1169 * If we would signal an error it would lead to the same
1170 * abort as if we did not find the ifnet anymore.
1171 * if_detach() calls us in void context and does not care
1172 * about an early abort notification, so life is splendid :)
1174 goto finish_vnet_shutdown;
1179 * At this point we are not tearing down a VNET and are either
1180 * going to destroy or vmove the interface and have to cleanup
1185 * Remove routes and flush queues.
1188 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1189 altq_disable(&ifp->if_snd);
1190 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1191 altq_detach(&ifp->if_snd);
1202 * Remove all IPv6 kernel structs related to ifp. This should be done
1203 * before removing routing entries below, since IPv6 interface direct
1204 * routes are expected to be removed by the IPv6-specific kernel API.
1205 * Otherwise, the kernel will detect some inconsistency and bark it.
1209 if_purgemaddrs(ifp);
1211 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1212 if (IS_DEFAULT_VNET(curvnet))
1213 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1217 * Prevent further calls into the device driver via ifnet.
1222 * Clean up all addresses.
1225 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1226 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1227 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1228 IF_ADDR_WUNLOCK(ifp);
1231 IF_ADDR_WUNLOCK(ifp);
1234 rt_flushifroutes(ifp);
1237 finish_vnet_shutdown:
1240 * We cannot hold the lock over dom_ifdetach calls as they might
1241 * sleep, for example trying to drain a callout, thus open up the
1242 * theoretical race with re-attaching.
1244 IF_AFDATA_LOCK(ifp);
1245 i = ifp->if_afdata_initialized;
1246 ifp->if_afdata_initialized = 0;
1247 IF_AFDATA_UNLOCK(ifp);
1250 SLIST_FOREACH(dp, &domains, dom_next) {
1251 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1252 (*dp->dom_ifdetach)(ifp,
1253 ifp->if_afdata[dp->dom_family]);
1254 ifp->if_afdata[dp->dom_family] = NULL;
1263 * if_vmove() performs a limited version of if_detach() in current
1264 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1267 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1270 u_int bif_dlt, bif_hdrlen;
1276 * if_detach_internal() will call the eventhandler to notify
1277 * interface departure. That will detach if_bpf. We need to
1278 * safe the dlt and hdrlen so we can re-attach it later.
1280 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1284 * Detach from current vnet, but preserve LLADDR info, do not
1285 * mark as dead etc. so that the ifnet can be reattached later.
1286 * If we cannot find it, we lost the race to someone else.
1288 rc = if_detach_internal(ifp, true);
1293 * Perform interface-specific reassignment tasks, if provided by
1296 if (ifp->if_reassign != NULL)
1297 ifp->if_reassign(ifp, new_vnet, NULL);
1300 * Switch to the context of the target vnet.
1302 CURVNET_SET_QUIET(new_vnet);
1303 if_attach_internal(ifp, true);
1306 if (ifp->if_bpf == NULL)
1307 bpfattach(ifp, bif_dlt, bif_hdrlen);
1315 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1318 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1323 bool found __diagused;
1326 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
1328 /* Try to find the prison within our visibility. */
1329 sx_slock(&allprison_lock);
1330 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1331 sx_sunlock(&allprison_lock);
1334 prison_hold_locked(pr);
1335 mtx_unlock(&pr->pr_mtx);
1337 /* Do not try to move the iface from and to the same prison. */
1338 if (pr->pr_vnet == ifp->if_vnet) {
1343 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1344 /* XXX Lock interfaces to avoid races. */
1345 CURVNET_SET_QUIET(pr->pr_vnet);
1346 difp = ifunit(ifname);
1352 sx_xlock(&ifnet_detach_sxlock);
1354 /* Make sure the VNET is stable. */
1355 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1357 sx_xunlock(&ifnet_detach_sxlock);
1364 found = if_unlink_ifnet(ifp, true);
1366 sx_xunlock(&ifnet_detach_sxlock);
1372 /* Move the interface into the child jail/vnet. */
1373 error = if_vmove(ifp, pr->pr_vnet);
1375 /* Report the new if_xname back to the userland on success. */
1377 sprintf(ifname, "%s", ifp->if_xname);
1379 sx_xunlock(&ifnet_detach_sxlock);
1386 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1389 struct vnet *vnet_dst;
1391 int error, found __diagused;
1394 /* Try to find the prison within our visibility. */
1395 sx_slock(&allprison_lock);
1396 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1397 sx_sunlock(&allprison_lock);
1400 prison_hold_locked(pr);
1401 mtx_unlock(&pr->pr_mtx);
1403 /* Make sure the named iface exists in the source prison/vnet. */
1404 CURVNET_SET(pr->pr_vnet);
1405 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1412 /* Do not try to move the iface from and to the same prison. */
1413 vnet_dst = TD_TO_VNET(td);
1414 if (vnet_dst == ifp->if_vnet) {
1420 /* Make sure the VNET is stable. */
1421 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1428 /* Get interface back from child jail/vnet. */
1429 found = if_unlink_ifnet(ifp, true);
1431 sx_xlock(&ifnet_detach_sxlock);
1432 error = if_vmove(ifp, vnet_dst);
1433 sx_xunlock(&ifnet_detach_sxlock);
1436 /* Report the new if_xname back to the userland on success. */
1438 sprintf(ifname, "%s", ifp->if_xname);
1446 * Add a group to an interface
1449 if_addgroup(struct ifnet *ifp, const char *groupname)
1451 struct ifg_list *ifgl;
1452 struct ifg_group *ifg = NULL;
1453 struct ifg_member *ifgm;
1456 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1457 groupname[strlen(groupname) - 1] <= '9')
1461 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1462 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1467 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1472 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1478 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1479 if (!strcmp(ifg->ifg_group, groupname))
1483 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1489 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1490 ifg->ifg_refcnt = 0;
1491 CK_STAILQ_INIT(&ifg->ifg_members);
1492 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1497 ifgl->ifgl_group = ifg;
1498 ifgm->ifgm_ifp = ifp;
1501 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1502 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1503 IF_ADDR_WUNLOCK(ifp);
1508 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1509 EVENTHANDLER_INVOKE(group_change_event, groupname);
1515 * Helper function to remove a group out of an interface. Expects the global
1516 * ifnet lock to be write-locked, and drops it before returning.
1519 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1520 const char *groupname)
1522 struct ifg_member *ifgm;
1525 IFNET_WLOCK_ASSERT();
1528 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1529 IF_ADDR_WUNLOCK(ifp);
1531 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1532 if (ifgm->ifgm_ifp == ifp) {
1533 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1534 ifg_member, ifgm_next);
1539 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1540 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1548 epoch_wait_preempt(net_epoch_preempt);
1549 EVENTHANDLER_INVOKE(group_change_event, groupname);
1551 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1552 free(ifgl->ifgl_group, M_TEMP);
1559 * Remove a group from an interface
1562 if_delgroup(struct ifnet *ifp, const char *groupname)
1564 struct ifg_list *ifgl;
1567 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1568 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1575 _if_delgroup_locked(ifp, ifgl, groupname);
1581 * Remove an interface from all groups
1584 if_delgroups(struct ifnet *ifp)
1586 struct ifg_list *ifgl;
1587 char groupname[IFNAMSIZ];
1590 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1591 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1592 _if_delgroup_locked(ifp, ifgl, groupname);
1599 * Stores all groups from an interface in memory pointed to by ifgr.
1602 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1605 struct ifg_list *ifgl;
1606 struct ifg_req ifgrq, *ifgp;
1610 if (ifgr->ifgr_len == 0) {
1611 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1612 ifgr->ifgr_len += sizeof(struct ifg_req);
1616 len = ifgr->ifgr_len;
1617 ifgp = ifgr->ifgr_groups;
1619 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1620 if (len < sizeof(ifgrq))
1622 bzero(&ifgrq, sizeof ifgrq);
1623 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1624 sizeof(ifgrq.ifgrq_group));
1625 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1627 len -= sizeof(ifgrq);
1635 * Stores all members of a group in memory pointed to by igfr
1638 if_getgroupmembers(struct ifgroupreq *ifgr)
1640 struct ifg_group *ifg;
1641 struct ifg_member *ifgm;
1642 struct ifg_req ifgrq, *ifgp;
1646 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1647 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1654 if (ifgr->ifgr_len == 0) {
1655 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1656 ifgr->ifgr_len += sizeof(ifgrq);
1661 len = ifgr->ifgr_len;
1662 ifgp = ifgr->ifgr_groups;
1663 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1664 if (len < sizeof(ifgrq)) {
1668 bzero(&ifgrq, sizeof ifgrq);
1669 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1670 sizeof(ifgrq.ifgrq_member));
1671 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1675 len -= sizeof(ifgrq);
1684 * Return counter values from counter(9)s stored in ifnet.
1687 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1690 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1692 return (counter_u64_fetch(ifp->if_counters[cnt]));
1696 * Increase an ifnet counter. Usually used for counters shared
1697 * between the stack and a driver, but function supports them all.
1700 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1703 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1705 counter_u64_add(ifp->if_counters[cnt], inc);
1709 * Copy data from ifnet to userland API structure if_data.
1712 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1715 ifd->ifi_type = ifp->if_type;
1716 ifd->ifi_physical = 0;
1717 ifd->ifi_addrlen = ifp->if_addrlen;
1718 ifd->ifi_hdrlen = ifp->if_hdrlen;
1719 ifd->ifi_link_state = ifp->if_link_state;
1721 ifd->ifi_datalen = sizeof(struct if_data);
1722 ifd->ifi_mtu = ifp->if_mtu;
1723 ifd->ifi_metric = ifp->if_metric;
1724 ifd->ifi_baudrate = ifp->if_baudrate;
1725 ifd->ifi_hwassist = ifp->if_hwassist;
1726 ifd->ifi_epoch = ifp->if_epoch;
1727 ifd->ifi_lastchange = ifp->if_lastchange;
1729 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1730 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1731 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1732 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1733 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1734 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1735 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1736 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1737 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1738 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1739 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1740 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1744 * Initialization, destruction and refcounting functions for ifaddrs.
1747 ifa_alloc(size_t size, int flags)
1751 KASSERT(size >= sizeof(struct ifaddr),
1752 ("%s: invalid size %zu", __func__, size));
1754 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1758 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1760 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1762 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1764 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1767 refcount_init(&ifa->ifa_refcnt, 1);
1772 /* free(NULL) is okay */
1773 counter_u64_free(ifa->ifa_opackets);
1774 counter_u64_free(ifa->ifa_ipackets);
1775 counter_u64_free(ifa->ifa_obytes);
1776 counter_u64_free(ifa->ifa_ibytes);
1777 free(ifa, M_IFADDR);
1783 ifa_ref(struct ifaddr *ifa)
1785 u_int old __diagused;
1787 old = refcount_acquire(&ifa->ifa_refcnt);
1788 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1792 ifa_try_ref(struct ifaddr *ifa)
1796 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1800 ifa_destroy(epoch_context_t ctx)
1804 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1805 counter_u64_free(ifa->ifa_opackets);
1806 counter_u64_free(ifa->ifa_ipackets);
1807 counter_u64_free(ifa->ifa_obytes);
1808 counter_u64_free(ifa->ifa_ibytes);
1809 free(ifa, M_IFADDR);
1813 ifa_free(struct ifaddr *ifa)
1816 if (refcount_release(&ifa->ifa_refcnt))
1817 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1821 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1822 * structs used to represent other address families, it is necessary
1823 * to perform a different comparison.
1826 #define sa_dl_equal(a1, a2) \
1827 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1828 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1829 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1830 CLLADDR((const struct sockaddr_dl *)(a2)), \
1831 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1834 * Locate an interface based on a complete address.
1838 ifa_ifwithaddr(const struct sockaddr *addr)
1845 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1846 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1847 if (ifa->ifa_addr->sa_family != addr->sa_family)
1849 if (sa_equal(addr, ifa->ifa_addr)) {
1852 /* IP6 doesn't have broadcast */
1853 if ((ifp->if_flags & IFF_BROADCAST) &&
1854 ifa->ifa_broadaddr &&
1855 ifa->ifa_broadaddr->sa_len != 0 &&
1856 sa_equal(ifa->ifa_broadaddr, addr)) {
1867 ifa_ifwithaddr_check(const struct sockaddr *addr)
1869 struct epoch_tracker et;
1872 NET_EPOCH_ENTER(et);
1873 rc = (ifa_ifwithaddr(addr) != NULL);
1879 * Locate an interface based on the broadcast address.
1883 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1889 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1890 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1892 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1893 if (ifa->ifa_addr->sa_family != addr->sa_family)
1895 if ((ifp->if_flags & IFF_BROADCAST) &&
1896 ifa->ifa_broadaddr &&
1897 ifa->ifa_broadaddr->sa_len != 0 &&
1898 sa_equal(ifa->ifa_broadaddr, addr)) {
1909 * Locate the point to point interface with a given destination address.
1913 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1919 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1920 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1922 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1924 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1925 if (ifa->ifa_addr->sa_family != addr->sa_family)
1927 if (ifa->ifa_dstaddr != NULL &&
1928 sa_equal(addr, ifa->ifa_dstaddr)) {
1939 * Find an interface on a specific network. If many, choice
1940 * is most specific found.
1943 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1947 struct ifaddr *ifa_maybe = NULL;
1948 u_int af = addr->sa_family;
1949 const char *addr_data = addr->sa_data, *cplim;
1953 * AF_LINK addresses can be looked up directly by their index number,
1954 * so do that if we can.
1956 if (af == AF_LINK) {
1957 ifp = ifnet_byindex(
1958 ((const struct sockaddr_dl *)addr)->sdl_index);
1959 return (ifp ? ifp->if_addr : NULL);
1963 * Scan though each interface, looking for ones that have addresses
1964 * in this address family and the requested fib.
1966 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1967 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1969 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1970 const char *cp, *cp2, *cp3;
1972 if (ifa->ifa_addr->sa_family != af)
1974 if (af == AF_INET &&
1975 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1977 * This is a bit broken as it doesn't
1978 * take into account that the remote end may
1979 * be a single node in the network we are
1981 * The trouble is that we don't know the
1982 * netmask for the remote end.
1984 if (ifa->ifa_dstaddr != NULL &&
1985 sa_equal(addr, ifa->ifa_dstaddr)) {
1990 * Scan all the bits in the ifa's address.
1991 * If a bit dissagrees with what we are
1992 * looking for, mask it with the netmask
1993 * to see if it really matters.
1994 * (A byte at a time)
1996 if (ifa->ifa_netmask == 0)
1999 cp2 = ifa->ifa_addr->sa_data;
2000 cp3 = ifa->ifa_netmask->sa_data;
2001 cplim = ifa->ifa_netmask->sa_len
2002 + (char *)ifa->ifa_netmask;
2004 if ((*cp++ ^ *cp2++) & *cp3++)
2005 goto next; /* next address! */
2007 * If the netmask of what we just found
2008 * is more specific than what we had before
2009 * (if we had one), or if the virtual status
2010 * of new prefix is better than of the old one,
2011 * then remember the new one before continuing
2012 * to search for an even better one.
2014 if (ifa_maybe == NULL ||
2015 ifa_preferred(ifa_maybe, ifa) ||
2016 rn_refines((caddr_t)ifa->ifa_netmask,
2017 (caddr_t)ifa_maybe->ifa_netmask)) {
2030 * Find an interface address specific to an interface best matching
2034 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2037 const char *cp, *cp2, *cp3;
2039 struct ifaddr *ifa_maybe = NULL;
2040 u_int af = addr->sa_family;
2046 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2047 if (ifa->ifa_addr->sa_family != af)
2049 if (ifa_maybe == NULL)
2051 if (ifa->ifa_netmask == 0) {
2052 if (sa_equal(addr, ifa->ifa_addr) ||
2053 (ifa->ifa_dstaddr &&
2054 sa_equal(addr, ifa->ifa_dstaddr)))
2058 if (ifp->if_flags & IFF_POINTOPOINT) {
2059 if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
2063 cp2 = ifa->ifa_addr->sa_data;
2064 cp3 = ifa->ifa_netmask->sa_data;
2065 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2066 for (; cp3 < cplim; cp3++)
2067 if ((*cp++ ^ *cp2++) & *cp3)
2079 * See whether new ifa is better than current one:
2080 * 1) A non-virtual one is preferred over virtual.
2081 * 2) A virtual in master state preferred over any other state.
2083 * Used in several address selecting functions.
2086 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2089 return (cur->ifa_carp && (!next->ifa_carp ||
2090 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2093 struct sockaddr_dl *
2094 link_alloc_sdl(size_t size, int flags)
2097 return (malloc(size, M_TEMP, flags));
2101 link_free_sdl(struct sockaddr *sa)
2107 * Fills in given sdl with interface basic info.
2108 * Returns pointer to filled sdl.
2110 struct sockaddr_dl *
2111 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2113 struct sockaddr_dl *sdl;
2115 sdl = (struct sockaddr_dl *)paddr;
2116 memset(sdl, 0, sizeof(struct sockaddr_dl));
2117 sdl->sdl_len = sizeof(struct sockaddr_dl);
2118 sdl->sdl_family = AF_LINK;
2119 sdl->sdl_index = ifp->if_index;
2120 sdl->sdl_type = iftype;
2126 * Mark an interface down and notify protocols of
2130 if_unroute(struct ifnet *ifp, int flag, int fam)
2133 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2135 ifp->if_flags &= ~flag;
2136 getmicrotime(&ifp->if_lastchange);
2137 ifp->if_qflush(ifp);
2140 (*carp_linkstate_p)(ifp);
2141 rt_ifmsg(ifp, IFF_UP);
2145 * Mark an interface up and notify protocols of
2149 if_route(struct ifnet *ifp, int flag, int fam)
2152 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2154 ifp->if_flags |= flag;
2155 getmicrotime(&ifp->if_lastchange);
2157 (*carp_linkstate_p)(ifp);
2158 rt_ifmsg(ifp, IFF_UP);
2164 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2165 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2166 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2167 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2168 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2169 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2170 int (*vlan_setcookie_p)(struct ifnet *, void *);
2171 void *(*vlan_cookie_p)(struct ifnet *);
2174 * Handle a change in the interface link state. To avoid LORs
2175 * between driver lock and upper layer locks, as well as possible
2176 * recursions, we post event to taskqueue, and all job
2177 * is done in static do_link_state_change().
2180 if_link_state_change(struct ifnet *ifp, int link_state)
2182 /* Return if state hasn't changed. */
2183 if (ifp->if_link_state == link_state)
2186 ifp->if_link_state = link_state;
2188 /* XXXGL: reference ifp? */
2189 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2193 do_link_state_change(void *arg, int pending)
2199 link_state = ifp->if_link_state;
2201 CURVNET_SET(ifp->if_vnet);
2203 if (ifp->if_vlantrunk != NULL)
2204 (*vlan_link_state_p)(ifp);
2206 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2207 ifp->if_l2com != NULL)
2208 (*ng_ether_link_state_p)(ifp, link_state);
2210 (*carp_linkstate_p)(ifp);
2212 ifp->if_bridge_linkstate(ifp);
2214 (*lagg_linkstate_p)(ifp, link_state);
2216 if (IS_DEFAULT_VNET(curvnet))
2217 devctl_notify("IFNET", ifp->if_xname,
2218 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2221 if_printf(ifp, "%d link states coalesced\n", pending);
2222 if (log_link_state_change)
2223 if_printf(ifp, "link state changed to %s\n",
2224 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2225 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2230 * Mark an interface down and notify protocols of
2234 if_down(struct ifnet *ifp)
2237 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2238 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2242 * Mark an interface up and notify protocols of
2246 if_up(struct ifnet *ifp)
2249 if_route(ifp, IFF_UP, AF_UNSPEC);
2250 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2254 * Flush an interface queue.
2257 if_qflush(struct ifnet *ifp)
2265 if (ALTQ_IS_ENABLED(ifq))
2269 while ((m = n) != NULL) {
2280 * Map interface name to interface structure pointer, with or without
2281 * returning a reference.
2284 ifunit_ref(const char *name)
2286 struct epoch_tracker et;
2289 NET_EPOCH_ENTER(et);
2290 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2291 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2292 !(ifp->if_flags & IFF_DYING))
2297 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2305 ifunit(const char *name)
2307 struct epoch_tracker et;
2310 NET_EPOCH_ENTER(et);
2311 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2312 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2320 ifr_buffer_get_buffer(void *data)
2322 union ifreq_union *ifrup;
2325 #ifdef COMPAT_FREEBSD32
2326 if (SV_CURPROC_FLAG(SV_ILP32))
2327 return ((void *)(uintptr_t)
2328 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2330 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2334 ifr_buffer_set_buffer_null(void *data)
2336 union ifreq_union *ifrup;
2339 #ifdef COMPAT_FREEBSD32
2340 if (SV_CURPROC_FLAG(SV_ILP32))
2341 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2344 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2348 ifr_buffer_get_length(void *data)
2350 union ifreq_union *ifrup;
2353 #ifdef COMPAT_FREEBSD32
2354 if (SV_CURPROC_FLAG(SV_ILP32))
2355 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2357 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2361 ifr_buffer_set_length(void *data, size_t len)
2363 union ifreq_union *ifrup;
2366 #ifdef COMPAT_FREEBSD32
2367 if (SV_CURPROC_FLAG(SV_ILP32))
2368 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2371 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2375 ifr_data_get_ptr(void *ifrp)
2377 union ifreq_union *ifrup;
2380 #ifdef COMPAT_FREEBSD32
2381 if (SV_CURPROC_FLAG(SV_ILP32))
2382 return ((void *)(uintptr_t)
2383 ifrup->ifr32.ifr_ifru.ifru_data);
2385 return (ifrup->ifr.ifr_ifru.ifru_data);
2388 struct ifcap_nv_bit_name {
2390 const char *cap_name;
2392 #define CAPNV(x) {.cap_bit = IFCAP_##x, \
2393 .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
2394 const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
2399 CAPNV(VLAN_HWTAGGING),
2411 CAPNV(VLAN_HWFILTER),
2423 CAPNV(VXLAN_HWCSUM),
2428 #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
2429 .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
2430 const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
2439 if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
2440 const struct ifcap_nv_bit_name *nn, bool all)
2445 for (i = 0; nn[i].cap_name != NULL; i++) {
2446 if (nvlist_exists_bool(nv, nn[i].cap_name)) {
2447 if (all || nvlist_get_bool(nv, nn[i].cap_name))
2448 res |= nn[i].cap_bit;
2450 res |= *old_cap & nn[i].cap_bit;
2457 if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
2458 int ifr_cap, int ifr_req)
2462 for (i = 0; nn[i].cap_name != NULL; i++) {
2463 if ((nn[i].cap_bit & ifr_cap) != 0) {
2464 nvlist_add_bool(nv, nn[i].cap_name,
2465 (nn[i].cap_bit & ifr_req) != 0);
2471 * Hardware specific interface ioctls.
2474 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2477 int error = 0, do_ifup = 0;
2478 int new_flags, temp_flags;
2479 size_t descrlen, nvbuflen;
2481 char new_name[IFNAMSIZ];
2484 struct siocsifcapnv_driver_data drv_ioctl_data;
2486 ifr = (struct ifreq *)data;
2489 ifr->ifr_index = ifp->if_index;
2493 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2494 ifr->ifr_flags = temp_flags & 0xffff;
2495 ifr->ifr_flagshigh = temp_flags >> 16;
2499 ifr->ifr_reqcap = ifp->if_capabilities;
2500 ifr->ifr_curcap = ifp->if_capenable;
2504 if ((ifp->if_capabilities & IFCAP_NV) == 0) {
2509 nvcap = nvlist_create(0);
2511 if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
2512 ifp->if_capabilities, ifp->if_capenable);
2513 if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
2514 ifp->if_capabilities2, ifp->if_capenable2);
2515 error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
2516 __DECONST(caddr_t, nvcap));
2519 "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
2523 buf = nvlist_pack(nvcap, &nvbuflen);
2525 error = nvlist_error(nvcap);
2530 if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
2531 ifr->ifr_cap_nv.length = nvbuflen;
2532 ifr->ifr_cap_nv.buffer = NULL;
2536 ifr->ifr_cap_nv.length = nvbuflen;
2537 error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
2540 free(buf, M_NVLIST);
2541 nvlist_destroy(nvcap);
2548 /* Ensure uninitialised padding is not leaked. */
2549 memset(&ifd, 0, sizeof(ifd));
2551 if_data_copy(ifp, &ifd);
2552 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2558 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2563 ifr->ifr_metric = ifp->if_metric;
2567 ifr->ifr_mtu = ifp->if_mtu;
2571 /* XXXGL: did this ever worked? */
2577 sx_slock(&ifdescr_sx);
2578 if (ifp->if_description == NULL)
2581 /* space for terminating nul */
2582 descrlen = strlen(ifp->if_description) + 1;
2583 if (ifr_buffer_get_length(ifr) < descrlen)
2584 ifr_buffer_set_buffer_null(ifr);
2586 error = copyout(ifp->if_description,
2587 ifr_buffer_get_buffer(ifr), descrlen);
2588 ifr_buffer_set_length(ifr, descrlen);
2590 sx_sunlock(&ifdescr_sx);
2594 error = priv_check(td, PRIV_NET_SETIFDESCR);
2599 * Copy only (length-1) bytes to make sure that
2600 * if_description is always nul terminated. The
2601 * length parameter is supposed to count the
2602 * terminating nul in.
2604 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2605 return (ENAMETOOLONG);
2606 else if (ifr_buffer_get_length(ifr) == 0)
2609 descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
2610 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2611 ifr_buffer_get_length(ifr) - 1);
2613 if_freedescr(descrbuf);
2618 if_setdescr(ifp, descrbuf);
2619 getmicrotime(&ifp->if_lastchange);
2623 ifr->ifr_fib = ifp->if_fib;
2627 error = priv_check(td, PRIV_NET_SETIFFIB);
2630 if (ifr->ifr_fib >= rt_numfibs)
2633 ifp->if_fib = ifr->ifr_fib;
2637 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2641 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2642 * check, so we don't need special handling here yet.
2644 new_flags = (ifr->ifr_flags & 0xffff) |
2645 (ifr->ifr_flagshigh << 16);
2646 if (ifp->if_flags & IFF_UP &&
2647 (new_flags & IFF_UP) == 0) {
2649 } else if (new_flags & IFF_UP &&
2650 (ifp->if_flags & IFF_UP) == 0) {
2653 /* See if permanently promiscuous mode bit is about to flip */
2654 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2655 if (new_flags & IFF_PPROMISC)
2656 ifp->if_flags |= IFF_PROMISC;
2657 else if (ifp->if_pcount == 0)
2658 ifp->if_flags &= ~IFF_PROMISC;
2659 if (log_promisc_mode_change)
2660 if_printf(ifp, "permanently promiscuous mode %s\n",
2661 ((new_flags & IFF_PPROMISC) ?
2662 "enabled" : "disabled"));
2664 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2665 (new_flags &~ IFF_CANTCHANGE);
2666 if (ifp->if_ioctl) {
2667 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2671 getmicrotime(&ifp->if_lastchange);
2675 error = priv_check(td, PRIV_NET_SETIFCAP);
2678 if (ifp->if_ioctl == NULL)
2679 return (EOPNOTSUPP);
2680 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2682 error = (*ifp->if_ioctl)(ifp, cmd, data);
2684 getmicrotime(&ifp->if_lastchange);
2688 error = priv_check(td, PRIV_NET_SETIFCAP);
2691 if (ifp->if_ioctl == NULL)
2692 return (EOPNOTSUPP);
2693 if ((ifp->if_capabilities & IFCAP_NV) == 0)
2695 if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
2698 buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
2700 error = copyin(ifr->ifr_cap_nv.buffer, buf,
2701 ifr->ifr_cap_nv.length);
2704 nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
2705 if (nvcap == NULL) {
2709 drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
2710 &ifp->if_capenable, ifcap_nv_bit_names, false);
2711 if ((drv_ioctl_data.reqcap &
2712 ~ifp->if_capabilities) != 0) {
2716 drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
2717 &ifp->if_capenable2, ifcap2_nv_bit_names, false);
2718 if ((drv_ioctl_data.reqcap2 &
2719 ~ifp->if_capabilities2) != 0) {
2723 drv_ioctl_data.nvcap = nvcap;
2724 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
2725 (caddr_t)&drv_ioctl_data);
2728 nvlist_destroy(nvcap);
2731 getmicrotime(&ifp->if_lastchange);
2736 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2741 error = priv_check(td, PRIV_NET_SETIFNAME);
2744 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2748 error = if_rename(ifp, new_name);
2753 error = priv_check(td, PRIV_NET_SETIFVNET);
2756 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2761 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2764 ifp->if_metric = ifr->ifr_metric;
2765 getmicrotime(&ifp->if_lastchange);
2769 error = priv_check(td, PRIV_NET_SETIFPHYS);
2772 if (ifp->if_ioctl == NULL)
2773 return (EOPNOTSUPP);
2774 error = (*ifp->if_ioctl)(ifp, cmd, data);
2776 getmicrotime(&ifp->if_lastchange);
2781 u_long oldmtu = ifp->if_mtu;
2783 error = priv_check(td, PRIV_NET_SETIFMTU);
2786 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2788 if (ifp->if_ioctl == NULL)
2789 return (EOPNOTSUPP);
2790 /* Disallow MTU changes on bridge member interfaces. */
2792 return (EOPNOTSUPP);
2793 error = (*ifp->if_ioctl)(ifp, cmd, data);
2795 getmicrotime(&ifp->if_lastchange);
2798 DEBUGNET_NOTIFY_MTU(ifp);
2802 * If the link MTU changed, do network layer specific procedure.
2804 if (ifp->if_mtu != oldmtu)
2811 if (cmd == SIOCADDMULTI)
2812 error = priv_check(td, PRIV_NET_ADDMULTI);
2814 error = priv_check(td, PRIV_NET_DELMULTI);
2818 /* Don't allow group membership on non-multicast interfaces. */
2819 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2820 return (EOPNOTSUPP);
2822 /* Don't let users screw up protocols' entries. */
2823 if (ifr->ifr_addr.sa_family != AF_LINK)
2826 if (cmd == SIOCADDMULTI) {
2827 struct epoch_tracker et;
2828 struct ifmultiaddr *ifma;
2831 * Userland is only permitted to join groups once
2832 * via the if_addmulti() KPI, because it cannot hold
2833 * struct ifmultiaddr * between calls. It may also
2834 * lose a race while we check if the membership
2837 NET_EPOCH_ENTER(et);
2838 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2843 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2845 error = if_delmulti(ifp, &ifr->ifr_addr);
2848 getmicrotime(&ifp->if_lastchange);
2851 case SIOCSIFPHYADDR:
2852 case SIOCDIFPHYADDR:
2854 case SIOCSIFPHYADDR_IN6:
2857 case SIOCSIFGENERIC:
2858 error = priv_check(td, PRIV_NET_HWIOCTL);
2861 if (ifp->if_ioctl == NULL)
2862 return (EOPNOTSUPP);
2863 error = (*ifp->if_ioctl)(ifp, cmd, data);
2865 getmicrotime(&ifp->if_lastchange);
2869 case SIOCGIFPSRCADDR:
2870 case SIOCGIFPDSTADDR:
2873 case SIOCGIFGENERIC:
2875 case SIOCGIFRSSHASH:
2876 case SIOCGIFDOWNREASON:
2877 if (ifp->if_ioctl == NULL)
2878 return (EOPNOTSUPP);
2879 error = (*ifp->if_ioctl)(ifp, cmd, data);
2883 error = priv_check(td, PRIV_NET_SETLLADDR);
2886 error = if_setlladdr(ifp,
2887 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2891 error = if_gethwaddr(ifp, ifr);
2895 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2898 error = if_addgroup(ifp,
2899 ((struct ifgroupreq *)data)->ifgr_group);
2906 struct epoch_tracker et;
2908 NET_EPOCH_ENTER(et);
2909 error = if_getgroup((struct ifgroupreq *)data, ifp);
2915 error = priv_check(td, PRIV_NET_DELIFGROUP);
2918 error = if_delgroup(ifp,
2919 ((struct ifgroupreq *)data)->ifgr_group);
2935 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2937 #ifdef COMPAT_FREEBSD32
2941 struct ifgroupreq ifgr;
2942 struct ifmediareq ifmr;
2945 struct ifconf32 *ifc32;
2946 struct ifdrv32 *ifd32;
2947 struct ifgroupreq32 *ifgr32;
2948 struct ifmediareq32 *ifmr32;
2958 CURVNET_SET(so->so_vnet);
2960 /* Make sure the VNET is stable. */
2961 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2968 #ifdef COMPAT_FREEBSD32
2972 ifc32 = (struct ifconf32 *)data;
2973 thunk.ifc.ifc_len = ifc32->ifc_len;
2974 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2975 data = (caddr_t)&thunk.ifc;
2978 case SIOCGDRVSPEC32:
2979 case SIOCSDRVSPEC32:
2980 ifd32 = (struct ifdrv32 *)data;
2981 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2982 sizeof(thunk.ifd.ifd_name));
2983 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2984 thunk.ifd.ifd_len = ifd32->ifd_len;
2985 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2986 data = (caddr_t)&thunk.ifd;
2987 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2989 case SIOCAIFGROUP32:
2990 case SIOCGIFGROUP32:
2991 case SIOCDIFGROUP32:
2992 case SIOCGIFGMEMB32:
2993 ifgr32 = (struct ifgroupreq32 *)data;
2994 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2995 sizeof(thunk.ifgr.ifgr_name));
2996 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2998 case SIOCAIFGROUP32:
2999 case SIOCDIFGROUP32:
3000 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
3001 sizeof(thunk.ifgr.ifgr_group));
3003 case SIOCGIFGROUP32:
3004 case SIOCGIFGMEMB32:
3005 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
3008 data = (caddr_t)&thunk.ifgr;
3009 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
3011 case SIOCGIFMEDIA32:
3012 case SIOCGIFXMEDIA32:
3013 ifmr32 = (struct ifmediareq32 *)data;
3014 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
3015 sizeof(thunk.ifmr.ifm_name));
3016 thunk.ifmr.ifm_current = ifmr32->ifm_current;
3017 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
3018 thunk.ifmr.ifm_status = ifmr32->ifm_status;
3019 thunk.ifmr.ifm_active = ifmr32->ifm_active;
3020 thunk.ifmr.ifm_count = ifmr32->ifm_count;
3021 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
3022 data = (caddr_t)&thunk.ifmr;
3023 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3030 error = ifconf(cmd, data);
3034 ifr = (struct ifreq *)data;
3038 error = priv_check(td, PRIV_NET_SETIFVNET);
3040 error = if_vmove_reclaim(td, ifr->ifr_name,
3046 error = priv_check(td, PRIV_NET_IFCREATE);
3048 error = if_clone_create(ifr->ifr_name,
3049 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3050 ifr_data_get_ptr(ifr) : NULL);
3053 error = priv_check(td, PRIV_NET_IFDESTROY);
3056 sx_xlock(&ifnet_detach_sxlock);
3057 error = if_clone_destroy(ifr->ifr_name);
3058 sx_xunlock(&ifnet_detach_sxlock);
3062 case SIOCIFGCLONERS:
3063 error = if_clone_list((struct if_clonereq *)data);
3067 error = if_getgroupmembers((struct ifgroupreq *)data);
3070 #if defined(INET) || defined(INET6)
3073 if (carp_ioctl_p == NULL)
3074 error = EPROTONOSUPPORT;
3076 error = (*carp_ioctl_p)(ifr, cmd, td);
3081 ifp = ifunit_ref(ifr->ifr_name);
3087 error = ifhwioctl(cmd, ifp, data, td);
3088 if (error != ENOIOCTL)
3091 oif_flags = ifp->if_flags;
3092 if (so->so_proto == NULL) {
3098 * Pass the request on to the socket control method, and if the
3099 * latter returns EOPNOTSUPP, directly to the interface.
3101 * Make an exception for the legacy SIOCSIF* requests. Drivers
3102 * trust SIOCSIFADDR et al to come from an already privileged
3103 * layer, and do not perform any credentials checks or input
3106 error = so->so_proto->pr_control(so, cmd, data, ifp, td);
3107 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3108 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3109 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3110 error = (*ifp->if_ioctl)(ifp, cmd, data);
3112 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3114 if (ifp->if_flags & IFF_UP)
3123 #ifdef COMPAT_FREEBSD32
3126 switch (saved_cmd) {
3128 ifc32->ifc_len = thunk.ifc.ifc_len;
3130 case SIOCGDRVSPEC32:
3132 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3133 * the struct so just assert that ifd_len (the only
3134 * field it might make sense to update) hasn't
3137 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3138 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3139 thunk.ifd.ifd_len));
3141 case SIOCGIFGROUP32:
3142 case SIOCGIFGMEMB32:
3143 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3145 case SIOCGIFMEDIA32:
3146 case SIOCGIFXMEDIA32:
3147 ifmr32->ifm_current = thunk.ifmr.ifm_current;
3148 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3149 ifmr32->ifm_status = thunk.ifmr.ifm_status;
3150 ifmr32->ifm_active = thunk.ifmr.ifm_active;
3151 ifmr32->ifm_count = thunk.ifmr.ifm_count;
3159 if_rename(struct ifnet *ifp, char *new_name)
3162 struct sockaddr_dl *sdl;
3163 size_t namelen, onamelen;
3164 char old_name[IFNAMSIZ];
3165 char strbuf[IFNAMSIZ + 8];
3167 if (new_name[0] == '\0')
3169 if (strcmp(new_name, ifp->if_xname) == 0)
3171 if (ifunit(new_name) != NULL)
3175 * XXX: Locking. Nothing else seems to lock if_flags,
3176 * and there are numerous other races with the
3177 * ifunit() checks not being atomic with namespace
3178 * changes (renames, vmoves, if_attach, etc).
3180 ifp->if_flags |= IFF_RENAMING;
3182 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
3184 if_printf(ifp, "changing name to '%s'\n", new_name);
3187 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
3188 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
3190 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3191 namelen = strlen(new_name);
3192 onamelen = sdl->sdl_nlen;
3194 * Move the address if needed. This is safe because we
3195 * allocate space for a name of length IFNAMSIZ when we
3196 * create this in if_attach().
3198 if (namelen != onamelen) {
3199 bcopy(sdl->sdl_data + onamelen,
3200 sdl->sdl_data + namelen, sdl->sdl_alen);
3202 bcopy(new_name, sdl->sdl_data, namelen);
3203 sdl->sdl_nlen = namelen;
3204 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
3205 bzero(sdl->sdl_data, onamelen);
3206 while (namelen != 0)
3207 sdl->sdl_data[--namelen] = 0xff;
3208 IF_ADDR_WUNLOCK(ifp);
3210 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
3212 ifp->if_flags &= ~IFF_RENAMING;
3214 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
3215 devctl_notify("IFNET", old_name, "RENAME", strbuf);
3221 * The code common to handling reference counted flags,
3222 * e.g., in ifpromisc() and if_allmulti().
3223 * The "pflag" argument can specify a permanent mode flag to check,
3224 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3226 * Only to be used on stack-owned flags, not driver-owned flags.
3229 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3233 int oldflags, oldcount;
3235 /* Sanity checks to catch programming errors */
3236 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3237 ("%s: setting driver-owned flag %d", __func__, flag));
3240 KASSERT(*refcount >= 0,
3241 ("%s: increment negative refcount %d for flag %d",
3242 __func__, *refcount, flag));
3244 KASSERT(*refcount > 0,
3245 ("%s: decrement non-positive refcount %d for flag %d",
3246 __func__, *refcount, flag));
3248 /* In case this mode is permanent, just touch refcount */
3249 if (ifp->if_flags & pflag) {
3250 *refcount += onswitch ? 1 : -1;
3254 /* Save ifnet parameters for if_ioctl() may fail */
3255 oldcount = *refcount;
3256 oldflags = ifp->if_flags;
3259 * See if we aren't the only and touching refcount is enough.
3260 * Actually toggle interface flag if we are the first or last.
3265 ifp->if_flags |= flag;
3269 ifp->if_flags &= ~flag;
3272 /* Call down the driver since we've changed interface flags */
3273 if (ifp->if_ioctl == NULL) {
3277 ifr.ifr_flags = ifp->if_flags & 0xffff;
3278 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3279 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3282 /* Notify userland that interface flags have changed */
3283 rt_ifmsg(ifp, flag);
3287 /* Recover after driver error */
3288 *refcount = oldcount;
3289 ifp->if_flags = oldflags;
3294 * Set/clear promiscuous mode on interface ifp based on the truth value
3295 * of pswitch. The calls are reference counted so that only the first
3296 * "on" request actually has an effect, as does the final "off" request.
3297 * Results are undefined if the "off" and "on" requests are not matched.
3300 ifpromisc(struct ifnet *ifp, int pswitch)
3303 int oldflags = ifp->if_flags;
3305 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3306 &ifp->if_pcount, pswitch);
3307 /* If promiscuous mode status has changed, log a message */
3308 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3309 log_promisc_mode_change)
3310 if_printf(ifp, "promiscuous mode %s\n",
3311 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3316 * Return interface configuration
3317 * of system. List may be used
3318 * in later ioctl's (above) to get
3319 * other information.
3323 ifconf(u_long cmd, caddr_t data)
3325 struct ifconf *ifc = (struct ifconf *)data;
3330 int error, full = 0, valid_len, max_len;
3332 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3333 max_len = maxphys - 1;
3335 /* Prevent hostile input from being able to crash the system */
3336 if (ifc->ifc_len <= 0)
3340 if (ifc->ifc_len <= max_len) {
3341 max_len = ifc->ifc_len;
3344 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3349 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3350 struct epoch_tracker et;
3354 * Zero the ifr to make sure we don't disclose the contents
3357 memset(&ifr, 0, sizeof(ifr));
3359 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3360 >= sizeof(ifr.ifr_name)) {
3363 return (ENAMETOOLONG);
3367 NET_EPOCH_ENTER(et);
3368 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3369 struct sockaddr *sa = ifa->ifa_addr;
3371 if (prison_if(curthread->td_ucred, sa) != 0)
3374 if (sa->sa_len <= sizeof(*sa)) {
3375 if (sa->sa_len < sizeof(*sa)) {
3376 memset(&ifr.ifr_ifru.ifru_addr, 0,
3377 sizeof(ifr.ifr_ifru.ifru_addr));
3378 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3381 ifr.ifr_ifru.ifru_addr = *sa;
3382 sbuf_bcat(sb, &ifr, sizeof(ifr));
3383 max_len += sizeof(ifr);
3386 offsetof(struct ifreq, ifr_addr));
3387 max_len += offsetof(struct ifreq, ifr_addr);
3388 sbuf_bcat(sb, sa, sa->sa_len);
3389 max_len += sa->sa_len;
3392 if (sbuf_error(sb) == 0)
3393 valid_len = sbuf_len(sb);
3397 sbuf_bcat(sb, &ifr, sizeof(ifr));
3398 max_len += sizeof(ifr);
3400 if (sbuf_error(sb) == 0)
3401 valid_len = sbuf_len(sb);
3407 * If we didn't allocate enough space (uncommon), try again. If
3408 * we have already allocated as much space as we are allowed,
3409 * return what we've got.
3411 if (valid_len != max_len && !full) {
3416 ifc->ifc_len = valid_len;
3418 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3424 * Just like ifpromisc(), but for all-multicast-reception mode.
3427 if_allmulti(struct ifnet *ifp, int onswitch)
3430 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3433 struct ifmultiaddr *
3434 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3436 struct ifmultiaddr *ifma;
3438 IF_ADDR_LOCK_ASSERT(ifp);
3440 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3441 if (sa->sa_family == AF_LINK) {
3442 if (sa_dl_equal(ifma->ifma_addr, sa))
3445 if (sa_equal(ifma->ifma_addr, sa))
3454 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3455 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3456 * the ifnet multicast address list here, so the caller must do that and
3457 * other setup work (such as notifying the device driver). The reference
3458 * count is initialized to 1.
3460 static struct ifmultiaddr *
3461 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3464 struct ifmultiaddr *ifma;
3465 struct sockaddr *dupsa;
3467 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3472 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3473 if (dupsa == NULL) {
3474 free(ifma, M_IFMADDR);
3477 bcopy(sa, dupsa, sa->sa_len);
3478 ifma->ifma_addr = dupsa;
3480 ifma->ifma_ifp = ifp;
3481 ifma->ifma_refcount = 1;
3482 ifma->ifma_protospec = NULL;
3485 ifma->ifma_lladdr = NULL;
3489 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3490 if (dupsa == NULL) {
3491 free(ifma->ifma_addr, M_IFMADDR);
3492 free(ifma, M_IFMADDR);
3495 bcopy(llsa, dupsa, llsa->sa_len);
3496 ifma->ifma_lladdr = dupsa;
3502 * if_freemulti: free ifmultiaddr structure and possibly attached related
3503 * addresses. The caller is responsible for implementing reference
3504 * counting, notifying the driver, handling routing messages, and releasing
3505 * any dependent link layer state.
3507 #ifdef MCAST_VERBOSE
3508 extern void kdb_backtrace(void);
3511 if_freemulti_internal(struct ifmultiaddr *ifma)
3514 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3515 ifma->ifma_refcount));
3517 if (ifma->ifma_lladdr != NULL)
3518 free(ifma->ifma_lladdr, M_IFMADDR);
3519 #ifdef MCAST_VERBOSE
3521 printf("%s freeing ifma: %p\n", __func__, ifma);
3523 free(ifma->ifma_addr, M_IFMADDR);
3524 free(ifma, M_IFMADDR);
3528 if_destroymulti(epoch_context_t ctx)
3530 struct ifmultiaddr *ifma;
3532 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3533 if_freemulti_internal(ifma);
3537 if_freemulti(struct ifmultiaddr *ifma)
3539 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3540 ifma->ifma_refcount));
3542 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3546 * Register an additional multicast address with a network interface.
3548 * - If the address is already present, bump the reference count on the
3549 * address and return.
3550 * - If the address is not link-layer, look up a link layer address.
3551 * - Allocate address structures for one or both addresses, and attach to the
3552 * multicast address list on the interface. If automatically adding a link
3553 * layer address, the protocol address will own a reference to the link
3554 * layer address, to be freed when it is freed.
3555 * - Notify the network device driver of an addition to the multicast address
3558 * 'sa' points to caller-owned memory with the desired multicast address.
3560 * 'retifma' will be used to return a pointer to the resulting multicast
3561 * address reference, if desired.
3564 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3565 struct ifmultiaddr **retifma)
3567 struct ifmultiaddr *ifma, *ll_ifma;
3568 struct sockaddr *llsa;
3569 struct sockaddr_dl sdl;
3573 IN_MULTI_LIST_UNLOCK_ASSERT();
3576 IN6_MULTI_LIST_UNLOCK_ASSERT();
3579 * If the address is already present, return a new reference to it;
3580 * otherwise, allocate storage and set up a new address.
3583 ifma = if_findmulti(ifp, sa);
3585 ifma->ifma_refcount++;
3586 if (retifma != NULL)
3588 IF_ADDR_WUNLOCK(ifp);
3593 * The address isn't already present; resolve the protocol address
3594 * into a link layer address, and then look that up, bump its
3595 * refcount or allocate an ifma for that also.
3596 * Most link layer resolving functions returns address data which
3597 * fits inside default sockaddr_dl structure. However callback
3598 * can allocate another sockaddr structure, in that case we need to
3603 if (ifp->if_resolvemulti != NULL) {
3604 /* Provide called function with buffer size information */
3605 sdl.sdl_len = sizeof(sdl);
3606 llsa = (struct sockaddr *)&sdl;
3607 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3613 * Allocate the new address. Don't hook it up yet, as we may also
3614 * need to allocate a link layer multicast address.
3616 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3623 * If a link layer address is found, we'll need to see if it's
3624 * already present in the address list, or allocate is as well.
3625 * When this block finishes, the link layer address will be on the
3629 ll_ifma = if_findmulti(ifp, llsa);
3630 if (ll_ifma == NULL) {
3631 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3632 if (ll_ifma == NULL) {
3633 --ifma->ifma_refcount;
3638 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3639 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3642 ll_ifma->ifma_refcount++;
3643 ifma->ifma_llifma = ll_ifma;
3647 * We now have a new multicast address, ifma, and possibly a new or
3648 * referenced link layer address. Add the primary address to the
3649 * ifnet address list.
3651 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3652 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3654 if (retifma != NULL)
3658 * Must generate the message while holding the lock so that 'ifma'
3659 * pointer is still valid.
3661 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3662 IF_ADDR_WUNLOCK(ifp);
3665 * We are certain we have added something, so call down to the
3666 * interface to let them know about it.
3668 if (ifp->if_ioctl != NULL) {
3669 if (THREAD_CAN_SLEEP())
3670 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3672 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3675 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3676 link_free_sdl(llsa);
3681 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3682 link_free_sdl(llsa);
3685 IF_ADDR_WUNLOCK(ifp);
3690 if_siocaddmulti(void *arg, int pending)
3697 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3699 CURVNET_SET(ifp->if_vnet);
3700 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3705 * Delete a multicast group membership by network-layer group address.
3707 * Returns ENOENT if the entry could not be found. If ifp no longer
3708 * exists, results are undefined. This entry point should only be used
3709 * from subsystems which do appropriate locking to hold ifp for the
3710 * duration of the call.
3711 * Network-layer protocol domains must use if_delmulti_ifma().
3714 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3716 struct ifmultiaddr *ifma;
3719 KASSERT(ifp, ("%s: NULL ifp", __func__));
3723 ifma = if_findmulti(ifp, sa);
3725 lastref = if_delmulti_locked(ifp, ifma, 0);
3726 IF_ADDR_WUNLOCK(ifp);
3731 if (lastref && ifp->if_ioctl != NULL) {
3732 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3739 * Delete all multicast group membership for an interface.
3740 * Should be used to quickly flush all multicast filters.
3743 if_delallmulti(struct ifnet *ifp)
3745 struct ifmultiaddr *ifma;
3746 struct ifmultiaddr *next;
3749 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3750 if_delmulti_locked(ifp, ifma, 0);
3751 IF_ADDR_WUNLOCK(ifp);
3755 if_delmulti_ifma(struct ifmultiaddr *ifma)
3757 if_delmulti_ifma_flags(ifma, 0);
3761 * Delete a multicast group membership by group membership pointer.
3762 * Network-layer protocol domains must use this routine.
3764 * It is safe to call this routine if the ifp disappeared.
3767 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3771 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3773 IN_MULTI_LIST_UNLOCK_ASSERT();
3775 ifp = ifma->ifma_ifp;
3778 printf("%s: ifma_ifp seems to be detached\n", __func__);
3780 struct epoch_tracker et;
3783 NET_EPOCH_ENTER(et);
3784 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3793 * If and only if the ifnet instance exists: Acquire the address lock.
3798 lastref = if_delmulti_locked(ifp, ifma, flags);
3802 * If and only if the ifnet instance exists:
3803 * Release the address lock.
3804 * If the group was left: update the hardware hash filter.
3806 IF_ADDR_WUNLOCK(ifp);
3807 if (lastref && ifp->if_ioctl != NULL) {
3808 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3814 * Perform deletion of network-layer and/or link-layer multicast address.
3816 * Return 0 if the reference count was decremented.
3817 * Return 1 if the final reference was released, indicating that the
3818 * hardware hash filter should be reprogrammed.
3821 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3823 struct ifmultiaddr *ll_ifma;
3825 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3826 KASSERT(ifma->ifma_ifp == ifp,
3827 ("%s: inconsistent ifp %p", __func__, ifp));
3828 IF_ADDR_WLOCK_ASSERT(ifp);
3831 ifp = ifma->ifma_ifp;
3832 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3835 * If the ifnet is detaching, null out references to ifnet,
3836 * so that upper protocol layers will notice, and not attempt
3837 * to obtain locks for an ifnet which no longer exists. The
3838 * routing socket announcement must happen before the ifnet
3839 * instance is detached from the system.
3843 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3846 * ifp may already be nulled out if we are being reentered
3847 * to delete the ll_ifma.
3850 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3851 ifma->ifma_ifp = NULL;
3855 if (--ifma->ifma_refcount > 0)
3858 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3859 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3860 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3863 * If this ifma is a network-layer ifma, a link-layer ifma may
3864 * have been associated with it. Release it first if so.
3866 ll_ifma = ifma->ifma_llifma;
3867 if (ll_ifma != NULL) {
3868 KASSERT(ifma->ifma_lladdr != NULL,
3869 ("%s: llifma w/o lladdr", __func__));
3871 ll_ifma->ifma_ifp = NULL; /* XXX */
3872 if (--ll_ifma->ifma_refcount == 0) {
3874 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3875 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3877 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3880 if_freemulti(ll_ifma);
3885 struct ifmultiaddr *ifmatmp;
3887 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3888 MPASS(ifma != ifmatmp);
3893 * The last reference to this instance of struct ifmultiaddr
3894 * was released; the hardware should be notified of this change.
3900 * Set the link layer address on an interface.
3902 * At this time we only support certain types of interfaces,
3903 * and we don't allow the length of the address to change.
3905 * Set noinline to be dtrace-friendly
3908 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3910 struct sockaddr_dl *sdl;
3918 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3922 if (len != sdl->sdl_alen) /* don't allow length to change */
3925 switch (ifp->if_type) {
3930 case IFT_IEEE8023ADLAG:
3931 bcopy(lladdr, LLADDR(sdl), len);
3938 * If the interface is already up, we need
3939 * to re-init it in order to reprogram its
3942 if ((ifp->if_flags & IFF_UP) != 0) {
3943 if (ifp->if_ioctl) {
3944 ifp->if_flags &= ~IFF_UP;
3945 ifr.ifr_flags = ifp->if_flags & 0xffff;
3946 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3947 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3948 ifp->if_flags |= IFF_UP;
3949 ifr.ifr_flags = ifp->if_flags & 0xffff;
3950 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3951 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3954 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3960 * Compat function for handling basic encapsulation requests.
3961 * Not converted stacks (FDDI, IB, ..) supports traditional
3962 * output model: ARP (and other similar L2 protocols) are handled
3963 * inside output routine, arpresolve/nd6_resolve() returns MAC
3964 * address instead of full prepend.
3966 * This function creates calculated header==MAC for IPv4/IPv6 and
3967 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3971 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3973 if (req->rtype != IFENCAP_LL)
3974 return (EOPNOTSUPP);
3976 if (req->bufsize < req->lladdr_len)
3979 switch (req->family) {
3984 return (EAFNOSUPPORT);
3987 /* Copy lladdr to storage as is */
3988 memmove(req->buf, req->lladdr, req->lladdr_len);
3989 req->bufsize = req->lladdr_len;
3990 req->lladdr_off = 0;
3996 * Tunnel interfaces can nest, also they may cause infinite recursion
3997 * calls when misconfigured. We'll prevent this by detecting loops.
3998 * High nesting level may cause stack exhaustion. We'll prevent this
3999 * by introducing upper limit.
4001 * Return 0, if tunnel nesting count is equal or less than limit.
4004 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
4012 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
4013 if (*(struct ifnet **)(mtag + 1) == ifp) {
4014 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
4019 if (count > limit) {
4021 "%s: if_output recursively called too many times(%d)\n",
4022 if_name(ifp), count);
4025 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
4028 *(struct ifnet **)(mtag + 1) = ifp;
4029 m_tag_prepend(m, mtag);
4034 * Get the link layer address that was read from the hardware at attach.
4036 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4037 * their component interfaces as IFT_IEEE8023ADLAG.
4040 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4042 if (ifp->if_hw_addr == NULL)
4045 switch (ifp->if_type) {
4047 case IFT_IEEE8023ADLAG:
4048 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4056 * The name argument must be a pointer to storage which will last as
4057 * long as the interface does. For physical devices, the result of
4058 * device_get_name(dev) is a good choice and for pseudo-devices a
4059 * static string works well.
4062 if_initname(struct ifnet *ifp, const char *name, int unit)
4064 ifp->if_dname = name;
4065 ifp->if_dunit = unit;
4066 if (unit != IF_DUNIT_NONE)
4067 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4069 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4073 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
4077 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4078 vlog(pri, if_fmt, ap);
4084 if_printf(struct ifnet *ifp, const char *fmt, ...)
4089 if_vlog(ifp, LOG_INFO, fmt, ap);
4095 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
4100 if_vlog(ifp, pri, fmt, ap);
4106 if_start(struct ifnet *ifp)
4109 (*(ifp)->if_start)(ifp);
4113 * Backwards compatibility interface for drivers
4114 * that have not implemented it
4117 if_transmit_default(struct ifnet *ifp, struct mbuf *m)
4121 IFQ_HANDOFF(ifp, m, error);
4126 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4132 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4137 if (_IF_QFULL(ifq)) {
4139 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4144 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4145 if (m->m_flags & (M_BCAST|M_MCAST))
4146 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4147 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4149 _IF_ENQUEUE(ifq, m);
4151 if (ifp != NULL && !active)
4152 (*(ifp)->if_start)(ifp);
4157 if_register_com_alloc(u_char type,
4158 if_com_alloc_t *a, if_com_free_t *f)
4161 KASSERT(if_com_alloc[type] == NULL,
4162 ("if_register_com_alloc: %d already registered", type));
4163 KASSERT(if_com_free[type] == NULL,
4164 ("if_register_com_alloc: %d free already registered", type));
4166 if_com_alloc[type] = a;
4167 if_com_free[type] = f;
4171 if_deregister_com_alloc(u_char type)
4174 KASSERT(if_com_alloc[type] != NULL,
4175 ("if_deregister_com_alloc: %d not registered", type));
4176 KASSERT(if_com_free[type] != NULL,
4177 ("if_deregister_com_alloc: %d free not registered", type));
4180 * Ensure all pending EPOCH(9) callbacks have been executed. This
4181 * fixes issues about late invocation of if_destroy(), which leads
4182 * to memory leak from if_com_alloc[type] allocated if_l2com.
4184 NET_EPOCH_DRAIN_CALLBACKS();
4186 if_com_alloc[type] = NULL;
4187 if_com_free[type] = NULL;
4190 /* API for driver access to network stack owned ifnet.*/
4192 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4196 oldbrate = ifp->if_baudrate;
4197 ifp->if_baudrate = baudrate;
4202 if_getbaudrate(const if_t ifp)
4204 return (ifp->if_baudrate);
4208 if_setcapabilities(if_t ifp, int capabilities)
4210 ifp->if_capabilities = capabilities;
4215 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4217 ifp->if_capabilities &= ~clearbit;
4218 ifp->if_capabilities |= setbit;
4223 if_getcapabilities(const if_t ifp)
4225 return (ifp->if_capabilities);
4229 if_setcapenable(if_t ifp, int capabilities)
4231 ifp->if_capenable = capabilities;
4236 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4238 ifp->if_capenable &= ~clearcap;
4239 ifp->if_capenable |= setcap;
4244 if_setcapabilities2(if_t ifp, int capabilities)
4246 ifp->if_capabilities2 = capabilities;
4251 if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
4253 ifp->if_capabilities2 &= ~clearbit;
4254 ifp->if_capabilities2 |= setbit;
4259 if_getcapabilities2(const if_t ifp)
4261 return (ifp->if_capabilities2);
4265 if_setcapenable2(if_t ifp, int capabilities2)
4267 ifp->if_capenable2 = capabilities2;
4272 if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
4274 ifp->if_capenable2 &= ~clearcap;
4275 ifp->if_capenable2 |= setcap;
4280 if_getdname(const if_t ifp)
4282 return (ifp->if_dname);
4286 if_setdname(if_t ifp, const char *dname)
4288 ifp->if_dname = dname;
4294 return (ifp->if_xname);
4298 if_setname(if_t ifp, const char *name)
4300 if (strlen(name) > sizeof(ifp->if_xname) - 1)
4301 return (ENAMETOOLONG);
4302 strcpy(ifp->if_xname, name);
4308 if_togglecapenable(if_t ifp, int togglecap)
4310 ifp->if_capenable ^= togglecap;
4315 if_getcapenable(const if_t ifp)
4317 return (ifp->if_capenable);
4321 if_togglecapenable2(if_t ifp, int togglecap)
4323 ifp->if_capenable2 ^= togglecap;
4328 if_getcapenable2(const if_t ifp)
4330 return (ifp->if_capenable2);
4334 if_getdunit(const if_t ifp)
4336 return (ifp->if_dunit);
4340 if_getindex(const if_t ifp)
4342 return (ifp->if_index);
4346 if_getidxgen(const if_t ifp)
4348 return (ifp->if_idxgen);
4352 if_setdescr(if_t ifp, char *descrbuf)
4354 sx_xlock(&ifdescr_sx);
4355 char *odescrbuf = ifp->if_description;
4356 ifp->if_description = descrbuf;
4357 sx_xunlock(&ifdescr_sx);
4359 if_freedescr(odescrbuf);
4363 if_allocdescr(size_t sz, int malloc_flag)
4365 malloc_flag &= (M_WAITOK | M_NOWAIT);
4366 return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4370 if_freedescr(char *descrbuf)
4372 free(descrbuf, M_IFDESCR);
4376 if_getalloctype(const if_t ifp)
4378 return (ifp->if_alloctype);
4382 * This is largely undesirable because it ties ifnet to a device, but does
4383 * provide flexiblity for an embedded product vendor. Should be used with
4384 * the understanding that it violates the interface boundaries, and should be
4385 * a last resort only.
4388 if_setdev(if_t ifp, void *dev)
4394 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4396 ifp->if_drv_flags &= ~clear_flags;
4397 ifp->if_drv_flags |= set_flags;
4403 if_getdrvflags(const if_t ifp)
4405 return (ifp->if_drv_flags);
4409 if_setdrvflags(if_t ifp, int flags)
4411 ifp->if_drv_flags = flags;
4416 if_setflags(if_t ifp, int flags)
4418 ifp->if_flags = flags;
4423 if_setflagbits(if_t ifp, int set, int clear)
4425 ifp->if_flags &= ~clear;
4426 ifp->if_flags |= set;
4431 if_getflags(const if_t ifp)
4433 return (ifp->if_flags);
4437 if_clearhwassist(if_t ifp)
4439 ifp->if_hwassist = 0;
4444 if_sethwassistbits(if_t ifp, int toset, int toclear)
4446 ifp->if_hwassist &= ~toclear;
4447 ifp->if_hwassist |= toset;
4453 if_sethwassist(if_t ifp, int hwassist_bit)
4455 ifp->if_hwassist = hwassist_bit;
4460 if_gethwassist(const if_t ifp)
4462 return (ifp->if_hwassist);
4466 if_togglehwassist(if_t ifp, int toggle_bits)
4468 ifp->if_hwassist ^= toggle_bits;
4473 if_setmtu(if_t ifp, int mtu)
4480 if_notifymtu(if_t ifp)
4489 if_getmtu(const if_t ifp)
4491 return (ifp->if_mtu);
4495 if_getmtu_family(const if_t ifp, int family)
4499 SLIST_FOREACH(dp, &domains, dom_next) {
4500 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4501 return (dp->dom_ifmtu(ifp));
4504 return (ifp->if_mtu);
4508 * Methods for drivers to access interface unicast and multicast
4509 * link level addresses. Driver shall not know 'struct ifaddr' neither
4510 * 'struct ifmultiaddr'.
4513 if_lladdr_count(if_t ifp)
4515 struct epoch_tracker et;
4520 NET_EPOCH_ENTER(et);
4521 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4522 if (ifa->ifa_addr->sa_family == AF_LINK)
4530 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4539 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4540 error = cb(ifp, cb_arg);
4549 * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4550 * Stops iteration if @cb returns non-zero error code.
4551 * Returns the last error code from @cb.
4552 * @match_cb: optional match callback limiting the iteration to only matched interfaces
4553 * @match_arg: argument to pass to @match_cb
4554 * @cb: iteration callback
4555 * @cb_arg: argument to pass to @cb
4558 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4561 int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4562 struct ifnet **match_array = NULL;
4568 struct ifnet **new_array;
4569 int new_size = array_size;
4570 struct epoch_tracker et;
4573 while (new_size < match_count)
4575 new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4576 if (match_array != NULL)
4577 memcpy(new_array, match_array, array_size * sizeof(void *));
4578 free(match_array, M_TEMP);
4579 match_array = new_array;
4580 array_size = new_size;
4583 NET_EPOCH_ENTER(et);
4584 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4585 if (match_cb != NULL && !match_cb(ifp, match_arg))
4587 if (match_count < array_size) {
4588 if (if_try_ref(ifp))
4589 match_array[match_count++] = ifp;
4595 if (match_count > array_size) {
4596 for (int i = 0; i < array_size; i++)
4597 if_rele(match_array[i]);
4600 for (int i = 0; i < match_count; i++) {
4602 error = cb(match_array[i], cb_arg);
4603 if_rele(match_array[i]);
4605 free(match_array, M_TEMP);
4615 * Uses just 1 pointer of the 4 available in the public struct.
4618 if_iter_start(struct if_iter *iter)
4624 bzero(iter, sizeof(*iter));
4625 ifp = CK_STAILQ_FIRST(&V_ifnet);
4627 iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4629 iter->context[0] = NULL;
4634 if_iter_next(struct if_iter *iter)
4636 if_t cur_ifp = iter->context[0];
4638 if (cur_ifp != NULL)
4639 iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4644 if_iter_finish(struct if_iter *iter)
4646 /* Nothing to do here for now. */
4650 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4652 struct epoch_tracker et;
4659 NET_EPOCH_ENTER(et);
4660 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4661 if (ifa->ifa_addr->sa_family != AF_LINK)
4663 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4672 if_llmaddr_count(if_t ifp)
4674 struct epoch_tracker et;
4675 struct ifmultiaddr *ifma;
4679 NET_EPOCH_ENTER(et);
4680 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4681 if (ifma->ifma_addr->sa_family == AF_LINK)
4689 if_maddr_empty(if_t ifp)
4692 return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4696 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4698 struct epoch_tracker et;
4699 struct ifmultiaddr *ifma;
4705 NET_EPOCH_ENTER(et);
4706 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4707 if (ifma->ifma_addr->sa_family != AF_LINK)
4709 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4718 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4720 struct epoch_tracker et;
4727 NET_EPOCH_ENTER(et);
4728 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4729 if (ifa->ifa_addr->sa_family != type)
4731 count += (*cb)(cb_arg, ifa, count);
4739 if_setsoftc(if_t ifp, void *softc)
4741 ifp->if_softc = softc;
4746 if_getsoftc(const if_t ifp)
4748 return (ifp->if_softc);
4752 if_setrcvif(struct mbuf *m, if_t ifp)
4755 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4756 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4760 if_setvtag(struct mbuf *m, uint16_t tag)
4762 m->m_pkthdr.ether_vtag = tag;
4766 if_getvtag(struct mbuf *m)
4768 return (m->m_pkthdr.ether_vtag);
4772 if_sendq_empty(if_t ifp)
4774 return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4778 if_getifaddr(const if_t ifp)
4780 return (ifp->if_addr);
4784 if_getamcount(const if_t ifp)
4786 return (ifp->if_amcount);
4790 if_setsendqready(if_t ifp)
4792 IFQ_SET_READY(&ifp->if_snd);
4797 if_setsendqlen(if_t ifp, int tx_desc_count)
4799 IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4800 ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4805 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4807 ifp->if_netmap = na;
4810 struct netmap_adapter *
4811 if_getnetmapadapter(if_t ifp)
4813 return (ifp->if_netmap);
4817 if_vlantrunkinuse(if_t ifp)
4819 return (ifp->if_vlantrunk != NULL);
4823 if_init(if_t ifp, void *ctx)
4825 (*ifp->if_init)(ctx);
4829 if_input(if_t ifp, struct mbuf* sendmp)
4831 (*ifp->if_input)(ifp, sendmp);
4835 if_transmit(if_t ifp, struct mbuf *m)
4837 return ((*ifp->if_transmit)(ifp, m));
4841 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4843 if (ifp->if_resolvemulti == NULL)
4844 return (EOPNOTSUPP);
4846 return (ifp->if_resolvemulti(ifp, srcs, dst));
4850 if_dequeue(if_t ifp)
4854 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4859 if_sendq_prepend(if_t ifp, struct mbuf *m)
4861 IFQ_DRV_PREPEND(&ifp->if_snd, m);
4866 if_setifheaderlen(if_t ifp, int len)
4868 ifp->if_hdrlen = len;
4873 if_getlladdr(const if_t ifp)
4875 return (IF_LLADDR(ifp));
4879 if_gethandle(u_char type)
4881 return (if_alloc(type));
4885 if_bpfmtap(if_t ifp, struct mbuf *m)
4891 if_etherbpfmtap(if_t ifp, struct mbuf *m)
4893 ETHER_BPF_MTAP(ifp, m);
4897 if_vlancap(if_t ifp)
4899 VLAN_CAPABILITIES(ifp);
4903 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4905 ifp->if_hw_tsomax = if_hw_tsomax;
4910 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4912 ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4917 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4919 ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4924 if_gethwtsomax(const if_t ifp)
4926 return (ifp->if_hw_tsomax);
4930 if_gethwtsomaxsegcount(const if_t ifp)
4932 return (ifp->if_hw_tsomaxsegcount);
4936 if_gethwtsomaxsegsize(const if_t ifp)
4938 return (ifp->if_hw_tsomaxsegsize);
4942 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
4944 ifp->if_init = init_fn;
4948 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
4950 ifp->if_input = input_fn;
4954 if_getinputfn(if_t ifp)
4956 return (ifp->if_input);
4960 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
4962 ifp->if_ioctl = ioctl_fn;
4966 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
4968 ifp->if_output = output_fn;
4972 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
4974 ifp->if_start = start_fn;
4978 if_getstartfn(if_t ifp)
4980 return (ifp->if_start);
4984 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4986 ifp->if_transmit = start_fn;
4990 if_gettransmitfn(if_t ifp)
4992 return (ifp->if_transmit);
4996 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4998 ifp->if_qflush = flush_fn;
5002 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
5004 ifp->if_snd_tag_alloc = alloc_fn;
5008 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
5009 struct m_snd_tag **mstp)
5011 if (ifp->if_snd_tag_alloc == NULL)
5012 return (EOPNOTSUPP);
5013 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
5017 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
5019 ifp->if_get_counter = fn;
5023 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
5025 ifp->if_reassign = fn;
5029 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
5031 ifp->if_ratelimit_query = fn;
5035 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
5037 ifp->if_debugnet_methods = m;
5041 if_getmaclabel(if_t ifp)
5043 return (ifp->if_label);
5047 if_setmaclabel(if_t ifp, struct label *label)
5049 ifp->if_label = label;
5053 if_gettype(if_t ifp)
5055 return (ifp->if_type);
5059 if_getllsoftc(if_t ifp)
5061 return (ifp->if_llsoftc);
5065 if_setllsoftc(if_t ifp, void *llsoftc)
5067 ifp->if_llsoftc = llsoftc;
5071 if_getlinkstate(if_t ifp)
5073 return (ifp->if_link_state);
5077 if_getbroadcastaddr(if_t ifp)
5079 return (ifp->if_broadcastaddr);
5083 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
5085 ifp->if_broadcastaddr = addr;
5089 if_getnumadomain(if_t ifp)
5091 return (ifp->if_numa_domain);
5095 if_getcounter(if_t ifp, ift_counter counter)
5097 return (ifp->if_get_counter(ifp, counter));
5101 if_altq_is_enabled(if_t ifp)
5103 return (ALTQ_IS_ENABLED(&ifp->if_snd));
5107 if_getvnet(if_t ifp)
5109 return (ifp->if_vnet);
5113 if_getafdata(if_t ifp, int af)
5115 return (ifp->if_afdata[af]);
5121 return (ifp->if_fib);
5125 if_getaddrlen(if_t ifp)
5127 return (ifp->if_addrlen);
5133 return (ifp->if_bpf);
5136 struct ifvlantrunk *
5137 if_getvlantrunk(if_t ifp)
5139 return (ifp->if_vlantrunk);
5145 return (ifp->if_pcp);
5149 if_getl2com(if_t ifp)
5151 return (ifp->if_l2com);
5156 if_show_ifnet(struct ifnet *ifp)
5160 db_printf("%s:\n", ifp->if_xname);
5161 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
5162 IF_DB_PRINTF("%s", if_dname);
5163 IF_DB_PRINTF("%d", if_dunit);
5164 IF_DB_PRINTF("%s", if_description);
5165 IF_DB_PRINTF("%u", if_index);
5166 IF_DB_PRINTF("%d", if_idxgen);
5167 IF_DB_PRINTF("%u", if_refcount);
5168 IF_DB_PRINTF("%p", if_softc);
5169 IF_DB_PRINTF("%p", if_l2com);
5170 IF_DB_PRINTF("%p", if_llsoftc);
5171 IF_DB_PRINTF("%d", if_amcount);
5172 IF_DB_PRINTF("%p", if_addr);
5173 IF_DB_PRINTF("%p", if_broadcastaddr);
5174 IF_DB_PRINTF("%p", if_afdata);
5175 IF_DB_PRINTF("%d", if_afdata_initialized);
5176 IF_DB_PRINTF("%u", if_fib);
5177 IF_DB_PRINTF("%p", if_vnet);
5178 IF_DB_PRINTF("%p", if_home_vnet);
5179 IF_DB_PRINTF("%p", if_vlantrunk);
5180 IF_DB_PRINTF("%p", if_bpf);
5181 IF_DB_PRINTF("%u", if_pcount);
5182 IF_DB_PRINTF("%p", if_bridge);
5183 IF_DB_PRINTF("%p", if_lagg);
5184 IF_DB_PRINTF("%p", if_pf_kif);
5185 IF_DB_PRINTF("%p", if_carp);
5186 IF_DB_PRINTF("%p", if_label);
5187 IF_DB_PRINTF("%p", if_netmap);
5188 IF_DB_PRINTF("0x%08x", if_flags);
5189 IF_DB_PRINTF("0x%08x", if_drv_flags);
5190 IF_DB_PRINTF("0x%08x", if_capabilities);
5191 IF_DB_PRINTF("0x%08x", if_capenable);
5192 IF_DB_PRINTF("%p", if_snd.ifq_head);
5193 IF_DB_PRINTF("%p", if_snd.ifq_tail);
5194 IF_DB_PRINTF("%d", if_snd.ifq_len);
5195 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5196 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5197 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5198 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5199 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5200 IF_DB_PRINTF("%d", if_snd.altq_type);
5201 IF_DB_PRINTF("%x", if_snd.altq_flags);
5205 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5208 db_printf("usage: show ifnet <struct ifnet *>\n");
5212 if_show_ifnet((struct ifnet *)addr);
5215 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5220 for (idx = 1; idx <= if_index; idx++) {
5221 ifp = ifindex_table[idx].ife_ifnet;
5224 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);