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>
61 #include <sys/rwlock.h>
62 #include <sys/sockio.h>
63 #include <sys/syslog.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/taskqueue.h>
67 #include <sys/domain.h>
75 #include <machine/stdarg.h>
79 #include <net/ethernet.h>
81 #include <net/if_arp.h>
82 #include <net/if_clone.h>
83 #include <net/if_dl.h>
84 #include <net/if_types.h>
85 #include <net/if_var.h>
86 #include <net/if_media.h>
87 #include <net/if_mib.h>
88 #include <net/if_vlan_var.h>
89 #include <net/radix.h>
90 #include <net/route.h>
91 #include <net/route/route_ctl.h>
94 #if defined(INET) || defined(INET6)
95 #include <net/ethernet.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_carp.h>
101 #include <net/debugnet.h>
102 #include <netinet/if_ether.h>
105 #include <netinet6/in6_var.h>
106 #include <netinet6/in6_ifattach.h>
108 #endif /* INET || INET6 */
110 #include <security/mac/mac_framework.h>
113 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
114 * and ifr_ifru when it is used in SIOCGIFCONF.
116 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
117 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
119 __read_mostly epoch_t net_epoch_preempt;
120 #ifdef COMPAT_FREEBSD32
121 #include <sys/mount.h>
122 #include <compat/freebsd32/freebsd32.h>
124 struct ifreq_buffer32 {
125 uint32_t length; /* (size_t) */
126 uint32_t buffer; /* (void *) */
130 * Interface request structure used for socket
131 * ioctl's. All interface ioctl's must have parameter
132 * definitions which begin with ifr_name. The
133 * remainder may be interface specific.
136 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
138 struct sockaddr ifru_addr;
139 struct sockaddr ifru_dstaddr;
140 struct sockaddr ifru_broadaddr;
141 struct ifreq_buffer32 ifru_buffer;
152 u_char ifru_vlan_pcp;
155 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
156 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
157 __offsetof(struct ifreq32, ifr_ifru));
166 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
169 char ifd_name[IFNAMSIZ];
174 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
175 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
177 struct ifgroupreq32 {
178 char ifgr_name[IFNAMSIZ];
181 char ifgru_group[IFNAMSIZ];
182 uint32_t ifgru_groups;
185 #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
186 #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
187 #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
188 #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
190 struct ifmediareq32 {
191 char ifm_name[IFNAMSIZ];
197 uint32_t ifm_ulist; /* (int *) */
199 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
200 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
201 #endif /* COMPAT_FREEBSD32 */
205 #ifdef COMPAT_FREEBSD32
206 struct ifreq32 ifr32;
210 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
212 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
213 "Generic link-management");
215 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
216 &ifqmaxlen, 0, "max send queue size");
218 /* Log link state change events */
219 static int log_link_state_change = 1;
221 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
222 &log_link_state_change, 0,
223 "log interface link state change events");
225 /* Log promiscuous mode change events */
226 static int log_promisc_mode_change = 1;
228 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
229 &log_promisc_mode_change, 1,
230 "log promiscuous mode change events");
232 /* Interface description */
233 static unsigned int ifdescr_maxlen = 1024;
234 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
236 "administrative maximum length for interface description");
238 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
240 /* global sx for non-critical path ifdescr */
241 static struct sx ifdescr_sx;
242 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
244 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
245 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
246 /* These are external hooks for CARP. */
247 void (*carp_linkstate_p)(struct ifnet *ifp);
248 void (*carp_demote_adj_p)(int, char *);
249 int (*carp_master_p)(struct ifaddr *);
250 #if defined(INET) || defined(INET6)
251 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
252 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
253 const struct sockaddr *sa);
254 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
255 int (*carp_attach_p)(struct ifaddr *, int);
256 void (*carp_detach_p)(struct ifaddr *, bool);
259 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
262 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
263 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
264 const struct in6_addr *taddr);
267 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
270 * XXX: Style; these should be sorted alphabetically, and unprototyped
271 * static functions should be prototyped. Currently they are sorted by
274 static void if_attachdomain(void *);
275 static void if_attachdomain1(struct ifnet *);
276 static int ifconf(u_long, caddr_t);
277 static void if_input_default(struct ifnet *, struct mbuf *);
278 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
279 static void if_route(struct ifnet *, int flag, int fam);
280 static int if_setflag(struct ifnet *, int, int, int *, int);
281 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
282 static void if_unroute(struct ifnet *, int flag, int fam);
283 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
284 static void do_link_state_change(void *, int);
285 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
286 static int if_getgroupmembers(struct ifgroupreq *);
287 static void if_delgroups(struct ifnet *);
288 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
289 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
290 static void if_siocaddmulti(void *, int);
291 static void if_link_ifnet(struct ifnet *);
292 static bool if_unlink_ifnet(struct ifnet *, bool);
294 static int if_vmove(struct ifnet *, struct vnet *);
299 * XXX: declare here to avoid to include many inet6 related files..
300 * should be more generalized?
302 extern void nd6_setmtu(struct ifnet *);
305 /* ipsec helper hooks */
306 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
307 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
309 int ifqmaxlen = IFQ_MAXLEN;
310 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
311 VNET_DEFINE(struct ifgrouphead, ifg_head);
313 /* Table of ifnet by index. */
314 VNET_DEFINE_STATIC(int, if_index);
315 #define V_if_index VNET(if_index)
316 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
317 #define V_if_indexlim VNET(if_indexlim)
318 VNET_DEFINE_STATIC(struct ifnet **, ifindex_table);
319 #define V_ifindex_table VNET(ifindex_table)
321 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
322 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
323 "Variables global to all interfaces");
324 SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
325 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(if_index), 0,
326 "Number of configured interfaces");
329 * The global network interface list (V_ifnet) and related state (such as
330 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
331 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
333 struct sx ifnet_sxlock;
334 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
336 struct sx ifnet_detach_sxlock;
337 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
341 #define VNET_IS_SHUTTING_DOWN(_vnet) \
342 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
345 static if_com_alloc_t *if_com_alloc[256];
346 static if_com_free_t *if_com_free[256];
348 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
349 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
350 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
353 ifnet_byindex(u_int idx)
358 if (__predict_false(idx > V_if_index))
361 return (ck_pr_load_ptr(&V_ifindex_table[idx]));
365 ifnet_byindex_ref(u_int idx)
369 ifp = ifnet_byindex(idx);
370 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
372 if (!if_try_ref(ifp))
378 * Allocate an ifindex array entry.
381 ifindex_alloc(struct ifnet *ifp)
387 * Try to find an empty slot below V_if_index. If we fail, take the
390 for (idx = 1; idx <= V_if_index; idx++) {
391 if (V_ifindex_table[idx] == NULL)
395 /* Catch if_index overflow. */
396 if (idx >= V_if_indexlim) {
397 struct ifnet **new, **old;
400 newlim = V_if_indexlim * 2;
401 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
402 memcpy(new, V_ifindex_table, V_if_indexlim * sizeof(*new));
403 old = V_ifindex_table;
404 ck_pr_store_ptr(&V_ifindex_table, new);
405 V_if_indexlim = newlim;
406 epoch_wait_preempt(net_epoch_preempt);
409 if (idx > V_if_index)
413 ck_pr_store_ptr(&V_ifindex_table[idx], ifp);
418 ifindex_free(u_short idx)
421 IFNET_WLOCK_ASSERT();
423 ck_pr_store_ptr(&V_ifindex_table[idx], NULL);
424 while (V_if_index > 0 &&
425 V_ifindex_table[V_if_index] == NULL)
430 ifaddr_byindex(u_short idx)
433 struct ifaddr *ifa = NULL;
437 ifp = ifnet_byindex(idx);
438 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
444 * Network interface utility routines.
446 * Routines with ifa_ifwith* names take sockaddr *'s as
451 vnet_if_init(const void *unused __unused)
454 CK_STAILQ_INIT(&V_ifnet);
455 CK_STAILQ_INIT(&V_ifg_head);
456 V_ifindex_table = malloc(V_if_indexlim * sizeof(*V_ifindex_table),
457 M_IFNET, M_WAITOK | M_ZERO);
458 vnet_if_clone_init();
460 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
465 vnet_if_uninit(const void *unused __unused)
468 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
469 "not empty", __func__, __LINE__, &V_ifnet));
470 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
471 "not empty", __func__, __LINE__, &V_ifg_head));
473 free((caddr_t)V_ifindex_table, M_IFNET);
475 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
476 vnet_if_uninit, NULL);
480 if_link_ifnet(struct ifnet *ifp)
484 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
486 curvnet->vnet_ifcnt++;
492 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
498 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
500 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
502 ifp->if_flags |= IFF_DYING;
507 curvnet->vnet_ifcnt--;
516 vnet_if_return(const void *unused __unused)
518 struct ifnet *ifp, *nifp;
519 struct ifnet **pending;
520 int found __diagused;
526 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
527 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
528 * if_detach_internal(), which waits for NET_EPOCH callbacks to
529 * complete. We can't do that from within NET_EPOCH.
531 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
532 * read/write lock. We cannot hold the lock as we call if_vmove()
533 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
538 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
539 M_IFNET, M_WAITOK | M_ZERO);
541 /* Return all inherited interfaces to their parent vnets. */
542 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
543 if (ifp->if_home_vnet != ifp->if_vnet) {
544 found = if_unlink_ifnet(ifp, true);
552 for (int j = 0; j < i; j++) {
553 if_vmove(pending[j], pending[j]->if_home_vnet);
556 free(pending, M_IFNET);
558 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
559 vnet_if_return, NULL);
563 * Allocate a struct ifnet and an index for an interface. A layer 2
564 * common structure will also be allocated if an allocation routine is
565 * registered for the passed type.
567 static struct ifnet *
568 if_alloc_domain(u_char type, int numa_domain)
572 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
573 if (numa_domain == IF_NODOM)
574 ifp = malloc(sizeof(struct ifnet), M_IFNET,
577 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
578 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
580 ifp->if_alloctype = type;
581 ifp->if_numa_domain = numa_domain;
583 ifp->if_vnet = curvnet;
585 if (if_com_alloc[type] != NULL) {
586 ifp->if_l2com = if_com_alloc[type](type, ifp);
587 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
591 IF_ADDR_LOCK_INIT(ifp);
592 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
593 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
594 ifp->if_afdata_initialized = 0;
595 IF_AFDATA_LOCK_INIT(ifp);
596 CK_STAILQ_INIT(&ifp->if_addrhead);
597 CK_STAILQ_INIT(&ifp->if_multiaddrs);
598 CK_STAILQ_INIT(&ifp->if_groups);
602 ifq_init(&ifp->if_snd, ifp);
604 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
605 for (int i = 0; i < IFCOUNTERS; i++)
606 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
607 ifp->if_get_counter = if_get_counter_default;
608 ifp->if_pcp = IFNET_PCP_NONE;
616 if_alloc_dev(u_char type, device_t dev)
620 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
621 return (if_alloc_domain(type, IF_NODOM));
622 return (if_alloc_domain(type, numa_domain));
626 if_alloc(u_char type)
629 return (if_alloc_domain(type, IF_NODOM));
632 * Do the actual work of freeing a struct ifnet, and layer 2 common
633 * structure. This call is made when the network epoch guarantees
634 * us that nobody holds a pointer to the interface.
637 if_free_deferred(epoch_context_t ctx)
639 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
641 KASSERT((ifp->if_flags & IFF_DYING),
642 ("%s: interface not dying", __func__));
644 if (if_com_free[ifp->if_alloctype] != NULL)
645 if_com_free[ifp->if_alloctype](ifp->if_l2com,
649 mac_ifnet_destroy(ifp);
651 IF_AFDATA_DESTROY(ifp);
652 IF_ADDR_LOCK_DESTROY(ifp);
653 ifq_delete(&ifp->if_snd);
655 for (int i = 0; i < IFCOUNTERS; i++)
656 counter_u64_free(ifp->if_counters[i]);
658 free(ifp->if_description, M_IFDESCR);
659 free(ifp->if_hw_addr, M_IFADDR);
664 * Deregister an interface and free the associated storage.
667 if_free(struct ifnet *ifp)
670 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
673 * XXXGL: An interface index is really an alias to ifp pointer.
674 * Why would we clear the alias now, and not in the deferred
675 * context? Indeed there is nothing wrong with some network
676 * thread obtaining ifp via ifnet_byindex() inside the network
677 * epoch and then dereferencing ifp while we peform if_free(),
678 * and after if_free() finished, too.
680 * The reason is the VIMAGE. For some reason it was designed
681 * to require all sockets drained before destroying, but not all
682 * ifnets. A vnet destruction calls if_vmove() on ifnet, which
683 * causes ID change. But ID change and a possible misidentification
684 * of an ifnet later is a lesser problem, as it doesn't crash kernel.
685 * A worse problem is that removed interface may outlive the vnet it
686 * belongs too! The if_free_deferred() would see ifp->if_vnet freed.
688 CURVNET_SET_QUIET(ifp->if_vnet);
690 MPASS(V_ifindex_table[ifp->if_index] == ifp);
691 ifindex_free(ifp->if_index);
694 if (refcount_release(&ifp->if_refcount))
695 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
700 * Interfaces to keep an ifnet type-stable despite the possibility of the
701 * driver calling if_free(). If there are additional references, we defer
702 * freeing the underlying data structure.
705 if_ref(struct ifnet *ifp)
707 u_int old __diagused;
709 /* We don't assert the ifnet list lock here, but arguably should. */
710 old = refcount_acquire(&ifp->if_refcount);
711 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
715 if_try_ref(struct ifnet *ifp)
718 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
722 if_rele(struct ifnet *ifp)
725 if (!refcount_release(&ifp->if_refcount))
727 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
731 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
734 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
736 if (ifq->ifq_maxlen == 0)
737 ifq->ifq_maxlen = ifqmaxlen;
740 ifq->altq_disc = NULL;
741 ifq->altq_flags &= ALTQF_CANTCHANGE;
742 ifq->altq_tbr = NULL;
747 ifq_delete(struct ifaltq *ifq)
749 mtx_destroy(&ifq->ifq_mtx);
753 * Perform generic interface initialization tasks and attach the interface
754 * to the list of "active" interfaces. If vmove flag is set on entry
755 * to if_attach_internal(), perform only a limited subset of initialization
756 * tasks, given that we are moving from one vnet to another an ifnet which
757 * has already been fully initialized.
759 * Note that if_detach_internal() removes group membership unconditionally
760 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
761 * Thus, when if_vmove() is applied to a cloned interface, group membership
762 * is lost while a cloned one always joins a group whose name is
763 * ifc->ifc_name. To recover this after if_detach_internal() and
764 * if_attach_internal(), the cloner should be specified to
765 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
766 * attempts to join a group whose name is ifc->ifc_name.
769 * - The decision to return void and thus require this function to
770 * succeed is questionable.
771 * - We should probably do more sanity checking. For instance we don't
772 * do anything to insure if_xname is unique or non-empty.
775 if_attach(struct ifnet *ifp)
778 if_attach_internal(ifp, 0, NULL);
782 * Compute the least common TSO limit.
785 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
788 * 1) If there is no limit currently, take the limit from
789 * the network adapter.
791 * 2) If the network adapter has a limit below the current
794 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
795 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
796 pmax->tsomaxbytes = ifp->if_hw_tsomax;
798 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
799 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
800 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
802 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
803 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
804 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
809 * Update TSO limit of a network adapter.
811 * Returns zero if no change. Else non-zero.
814 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
817 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
818 ifp->if_hw_tsomax = pmax->tsomaxbytes;
821 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
822 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
825 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
826 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
833 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
835 unsigned socksize, ifasize;
836 int namelen, masklen;
837 struct sockaddr_dl *sdl;
840 MPASS(V_ifindex_table[ifp->if_index] == ifp);
843 ifp->if_vnet = curvnet;
844 if (ifp->if_home_vnet == NULL)
845 ifp->if_home_vnet = curvnet;
848 if_addgroup(ifp, IFG_ALL);
850 /* Restore group membership for cloned interfaces. */
851 if (vmove && ifc != NULL)
852 if_clone_addgroup(ifp, ifc);
854 getmicrotime(&ifp->if_lastchange);
855 ifp->if_epoch = time_uptime;
857 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
858 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
859 ("transmit and qflush must both either be set or both be NULL"));
860 if (ifp->if_transmit == NULL) {
861 ifp->if_transmit = if_transmit;
862 ifp->if_qflush = if_qflush;
864 if (ifp->if_input == NULL)
865 ifp->if_input = if_input_default;
867 if (ifp->if_requestencap == NULL)
868 ifp->if_requestencap = if_requestencap_default;
872 mac_ifnet_create(ifp);
876 * Create a Link Level name for this device.
878 namelen = strlen(ifp->if_xname);
880 * Always save enough space for any possiable name so we
881 * can do a rename in place later.
883 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
884 socksize = masklen + ifp->if_addrlen;
885 if (socksize < sizeof(*sdl))
886 socksize = sizeof(*sdl);
887 socksize = roundup2(socksize, sizeof(long));
888 ifasize = sizeof(*ifa) + 2 * socksize;
889 ifa = ifa_alloc(ifasize, M_WAITOK);
890 sdl = (struct sockaddr_dl *)(ifa + 1);
891 sdl->sdl_len = socksize;
892 sdl->sdl_family = AF_LINK;
893 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
894 sdl->sdl_nlen = namelen;
895 sdl->sdl_index = ifp->if_index;
896 sdl->sdl_type = ifp->if_type;
899 ifa->ifa_addr = (struct sockaddr *)sdl;
900 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
901 ifa->ifa_netmask = (struct sockaddr *)sdl;
902 sdl->sdl_len = masklen;
904 sdl->sdl_data[--namelen] = 0xff;
905 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
906 /* Reliably crash if used uninitialized. */
907 ifp->if_broadcastaddr = NULL;
909 if (ifp->if_type == IFT_ETHER) {
910 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
914 #if defined(INET) || defined(INET6)
915 /* Use defaults for TSO, if nothing is set */
916 if (ifp->if_hw_tsomax == 0 &&
917 ifp->if_hw_tsomaxsegcount == 0 &&
918 ifp->if_hw_tsomaxsegsize == 0) {
920 * The TSO defaults needs to be such that an
921 * NFS mbuf list of 35 mbufs totalling just
922 * below 64K works and that a chain of mbufs
923 * can be defragged into at most 32 segments:
925 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
926 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
927 ifp->if_hw_tsomaxsegcount = 35;
928 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
930 /* XXX some drivers set IFCAP_TSO after ethernet attach */
931 if (ifp->if_capabilities & IFCAP_TSO) {
932 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
934 ifp->if_hw_tsomaxsegcount,
935 ifp->if_hw_tsomaxsegsize);
943 * Update the interface index in the link layer address
946 for (ifa = ifp->if_addr; ifa != NULL;
947 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
948 if (ifa->ifa_addr->sa_family == AF_LINK) {
949 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
950 sdl->sdl_index = ifp->if_index;
958 if (domain_init_status >= 2)
959 if_attachdomain1(ifp);
961 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
962 if (IS_DEFAULT_VNET(curvnet))
963 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
965 /* Announce the interface. */
966 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
970 if_epochalloc(void *dummy __unused)
973 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
975 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
978 if_attachdomain(void *dummy)
982 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
983 if_attachdomain1(ifp);
985 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
986 if_attachdomain, NULL);
989 if_attachdomain1(struct ifnet *ifp)
994 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
995 * cannot lock ifp->if_afdata initialization, entirely.
998 if (ifp->if_afdata_initialized >= domain_init_status) {
999 IF_AFDATA_UNLOCK(ifp);
1000 log(LOG_WARNING, "%s called more than once on %s\n",
1001 __func__, ifp->if_xname);
1004 ifp->if_afdata_initialized = domain_init_status;
1005 IF_AFDATA_UNLOCK(ifp);
1007 /* address family dependent data region */
1008 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1009 for (dp = domains; dp; dp = dp->dom_next) {
1010 if (dp->dom_ifattach)
1011 ifp->if_afdata[dp->dom_family] =
1012 (*dp->dom_ifattach)(ifp);
1017 * Remove any unicast or broadcast network addresses from an interface.
1020 if_purgeaddrs(struct ifnet *ifp)
1025 struct epoch_tracker et;
1027 NET_EPOCH_ENTER(et);
1028 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1029 if (ifa->ifa_addr->sa_family != AF_LINK)
1037 /* XXX: Ugly!! ad hoc just for INET */
1038 if (ifa->ifa_addr->sa_family == AF_INET) {
1039 struct ifaliasreq ifr;
1041 bzero(&ifr, sizeof(ifr));
1042 ifr.ifra_addr = *ifa->ifa_addr;
1043 if (ifa->ifa_dstaddr)
1044 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1045 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1051 if (ifa->ifa_addr->sa_family == AF_INET6) {
1052 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1053 /* ifp_addrhead is already updated */
1058 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1059 IF_ADDR_WUNLOCK(ifp);
1065 * Remove any multicast network addresses from an interface when an ifnet
1069 if_purgemaddrs(struct ifnet *ifp)
1071 struct ifmultiaddr *ifma;
1074 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1075 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1076 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1077 if_delmulti_locked(ifp, ifma, 1);
1079 IF_ADDR_WUNLOCK(ifp);
1083 * Detach an interface, removing it from the list of "active" interfaces.
1084 * If vmove flag is set on entry to if_detach_internal(), perform only a
1085 * limited subset of cleanup tasks, given that we are moving an ifnet from
1086 * one vnet to another, where it must be fully operational.
1088 * XXXRW: There are some significant questions about event ordering, and
1089 * how to prevent things from starting to use the interface during detach.
1092 if_detach(struct ifnet *ifp)
1096 CURVNET_SET_QUIET(ifp->if_vnet);
1097 found = if_unlink_ifnet(ifp, false);
1099 sx_xlock(&ifnet_detach_sxlock);
1100 if_detach_internal(ifp, 0, NULL);
1101 sx_xunlock(&ifnet_detach_sxlock);
1107 * The vmove flag, if set, indicates that we are called from a callpath
1108 * that is moving an interface to a different vnet instance.
1110 * The shutdown flag, if set, indicates that we are called in the
1111 * process of shutting down a vnet instance. Currently only the
1112 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1113 * on a vnet instance shutdown without this flag being set, e.g., when
1114 * the cloned interfaces are destoyed as first thing of teardown.
1117 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1125 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1129 * At this point we know the interface still was on the ifnet list
1130 * and we removed it so we are in a stable state.
1132 epoch_wait_preempt(net_epoch_preempt);
1135 * Ensure all pending EPOCH(9) callbacks have been executed. This
1136 * fixes issues about late destruction of multicast options
1137 * which lead to leave group calls, which in turn access the
1138 * belonging ifnet structure:
1140 epoch_drain_callbacks(net_epoch_preempt);
1143 * In any case (destroy or vmove) detach us from the groups
1144 * and remove/wait for pending events on the taskq.
1145 * XXX-BZ in theory an interface could still enqueue a taskq change?
1149 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1150 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1153 * Check if this is a cloned interface or not. Must do even if
1154 * shutting down as a if_vmove_reclaim() would move the ifp and
1155 * the if_clone_addgroup() will have a corrupted string overwise
1156 * from a gibberish pointer.
1158 if (vmove && ifcp != NULL)
1159 *ifcp = if_clone_findifc(ifp);
1165 * On VNET shutdown abort here as the stack teardown will do all
1166 * the work top-down for us.
1169 /* Give interface users the chance to clean up. */
1170 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1173 * In case of a vmove we are done here without error.
1174 * If we would signal an error it would lead to the same
1175 * abort as if we did not find the ifnet anymore.
1176 * if_detach() calls us in void context and does not care
1177 * about an early abort notification, so life is splendid :)
1179 goto finish_vnet_shutdown;
1184 * At this point we are not tearing down a VNET and are either
1185 * going to destroy or vmove the interface and have to cleanup
1190 * Remove routes and flush queues.
1193 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1194 altq_disable(&ifp->if_snd);
1195 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1196 altq_detach(&ifp->if_snd);
1207 * Remove all IPv6 kernel structs related to ifp. This should be done
1208 * before removing routing entries below, since IPv6 interface direct
1209 * routes are expected to be removed by the IPv6-specific kernel API.
1210 * Otherwise, the kernel will detect some inconsistency and bark it.
1214 if_purgemaddrs(ifp);
1216 /* Announce that the interface is gone. */
1217 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1218 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1219 if (IS_DEFAULT_VNET(curvnet))
1220 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1224 * Prevent further calls into the device driver via ifnet.
1229 * Clean up all addresses.
1232 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1233 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1234 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1235 IF_ADDR_WUNLOCK(ifp);
1238 IF_ADDR_WUNLOCK(ifp);
1241 rt_flushifroutes(ifp);
1244 finish_vnet_shutdown:
1247 * We cannot hold the lock over dom_ifdetach calls as they might
1248 * sleep, for example trying to drain a callout, thus open up the
1249 * theoretical race with re-attaching.
1251 IF_AFDATA_LOCK(ifp);
1252 i = ifp->if_afdata_initialized;
1253 ifp->if_afdata_initialized = 0;
1254 IF_AFDATA_UNLOCK(ifp);
1255 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1256 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1257 (*dp->dom_ifdetach)(ifp,
1258 ifp->if_afdata[dp->dom_family]);
1259 ifp->if_afdata[dp->dom_family] = NULL;
1268 * if_vmove() performs a limited version of if_detach() in current
1269 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1272 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1274 struct if_clone *ifc;
1276 u_int bif_dlt, bif_hdrlen;
1282 * if_detach_internal() will call the eventhandler to notify
1283 * interface departure. That will detach if_bpf. We need to
1284 * safe the dlt and hdrlen so we can re-attach it later.
1286 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1290 * Detach from current vnet, but preserve LLADDR info, do not
1291 * mark as dead etc. so that the ifnet can be reattached later.
1292 * If we cannot find it, we lost the race to someone else.
1294 rc = if_detach_internal(ifp, 1, &ifc);
1299 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1300 * the if_index for that vnet if possible.
1302 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1303 * or we'd lock on one vnet and unlock on another.
1306 ifindex_free(ifp->if_index);
1310 * Perform interface-specific reassignment tasks, if provided by
1313 if (ifp->if_reassign != NULL)
1314 ifp->if_reassign(ifp, new_vnet, NULL);
1317 * Switch to the context of the target vnet.
1319 CURVNET_SET_QUIET(new_vnet);
1321 if_attach_internal(ifp, 1, ifc);
1324 if (ifp->if_bpf == NULL)
1325 bpfattach(ifp, bif_dlt, bif_hdrlen);
1333 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1336 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1341 bool found __diagused;
1344 /* Try to find the prison within our visibility. */
1345 sx_slock(&allprison_lock);
1346 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1347 sx_sunlock(&allprison_lock);
1350 prison_hold_locked(pr);
1351 mtx_unlock(&pr->pr_mtx);
1353 /* Do not try to move the iface from and to the same prison. */
1354 if (pr->pr_vnet == ifp->if_vnet) {
1359 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1360 /* XXX Lock interfaces to avoid races. */
1361 CURVNET_SET_QUIET(pr->pr_vnet);
1362 difp = ifunit(ifname);
1369 /* Make sure the VNET is stable. */
1370 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1378 found = if_unlink_ifnet(ifp, true);
1381 /* Move the interface into the child jail/vnet. */
1382 error = if_vmove(ifp, pr->pr_vnet);
1384 /* Report the new if_xname back to the userland on success. */
1386 sprintf(ifname, "%s", ifp->if_xname);
1393 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1396 struct vnet *vnet_dst;
1398 int error, found __diagused;
1401 /* Try to find the prison within our visibility. */
1402 sx_slock(&allprison_lock);
1403 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1404 sx_sunlock(&allprison_lock);
1407 prison_hold_locked(pr);
1408 mtx_unlock(&pr->pr_mtx);
1410 /* Make sure the named iface exists in the source prison/vnet. */
1411 CURVNET_SET(pr->pr_vnet);
1412 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1419 /* Do not try to move the iface from and to the same prison. */
1420 vnet_dst = TD_TO_VNET(td);
1421 if (vnet_dst == ifp->if_vnet) {
1427 /* Make sure the VNET is stable. */
1428 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1435 /* Get interface back from child jail/vnet. */
1436 found = if_unlink_ifnet(ifp, true);
1438 error = if_vmove(ifp, vnet_dst);
1441 /* Report the new if_xname back to the userland on success. */
1443 sprintf(ifname, "%s", ifp->if_xname);
1451 * Add a group to an interface
1454 if_addgroup(struct ifnet *ifp, const char *groupname)
1456 struct ifg_list *ifgl;
1457 struct ifg_group *ifg = NULL;
1458 struct ifg_member *ifgm;
1461 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1462 groupname[strlen(groupname) - 1] <= '9')
1466 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1467 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1472 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1477 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1483 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1484 if (!strcmp(ifg->ifg_group, groupname))
1488 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1494 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1495 ifg->ifg_refcnt = 0;
1496 CK_STAILQ_INIT(&ifg->ifg_members);
1497 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1502 ifgl->ifgl_group = ifg;
1503 ifgm->ifgm_ifp = ifp;
1506 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1507 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1508 IF_ADDR_WUNLOCK(ifp);
1513 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1514 EVENTHANDLER_INVOKE(group_change_event, groupname);
1520 * Helper function to remove a group out of an interface. Expects the global
1521 * ifnet lock to be write-locked, and drops it before returning.
1524 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1525 const char *groupname)
1527 struct ifg_member *ifgm;
1530 IFNET_WLOCK_ASSERT();
1533 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1534 IF_ADDR_WUNLOCK(ifp);
1536 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1537 if (ifgm->ifgm_ifp == ifp) {
1538 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1539 ifg_member, ifgm_next);
1544 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1545 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1553 epoch_wait_preempt(net_epoch_preempt);
1555 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1556 free(ifgl->ifgl_group, M_TEMP);
1561 EVENTHANDLER_INVOKE(group_change_event, groupname);
1565 * Remove a group from an interface
1568 if_delgroup(struct ifnet *ifp, const char *groupname)
1570 struct ifg_list *ifgl;
1573 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1574 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1581 _if_delgroup_locked(ifp, ifgl, groupname);
1587 * Remove an interface from all groups
1590 if_delgroups(struct ifnet *ifp)
1592 struct ifg_list *ifgl;
1593 char groupname[IFNAMSIZ];
1596 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1597 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1598 _if_delgroup_locked(ifp, ifgl, groupname);
1605 * Stores all groups from an interface in memory pointed to by ifgr.
1608 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1611 struct ifg_list *ifgl;
1612 struct ifg_req ifgrq, *ifgp;
1616 if (ifgr->ifgr_len == 0) {
1617 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1618 ifgr->ifgr_len += sizeof(struct ifg_req);
1622 len = ifgr->ifgr_len;
1623 ifgp = ifgr->ifgr_groups;
1625 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1626 if (len < sizeof(ifgrq))
1628 bzero(&ifgrq, sizeof ifgrq);
1629 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1630 sizeof(ifgrq.ifgrq_group));
1631 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1633 len -= sizeof(ifgrq);
1641 * Stores all members of a group in memory pointed to by igfr
1644 if_getgroupmembers(struct ifgroupreq *ifgr)
1646 struct ifg_group *ifg;
1647 struct ifg_member *ifgm;
1648 struct ifg_req ifgrq, *ifgp;
1652 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1653 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1660 if (ifgr->ifgr_len == 0) {
1661 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1662 ifgr->ifgr_len += sizeof(ifgrq);
1667 len = ifgr->ifgr_len;
1668 ifgp = ifgr->ifgr_groups;
1669 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1670 if (len < sizeof(ifgrq)) {
1674 bzero(&ifgrq, sizeof ifgrq);
1675 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1676 sizeof(ifgrq.ifgrq_member));
1677 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1681 len -= sizeof(ifgrq);
1690 * Return counter values from counter(9)s stored in ifnet.
1693 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1696 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1698 return (counter_u64_fetch(ifp->if_counters[cnt]));
1702 * Increase an ifnet counter. Usually used for counters shared
1703 * between the stack and a driver, but function supports them all.
1706 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1709 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1711 counter_u64_add(ifp->if_counters[cnt], inc);
1715 * Copy data from ifnet to userland API structure if_data.
1718 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1721 ifd->ifi_type = ifp->if_type;
1722 ifd->ifi_physical = 0;
1723 ifd->ifi_addrlen = ifp->if_addrlen;
1724 ifd->ifi_hdrlen = ifp->if_hdrlen;
1725 ifd->ifi_link_state = ifp->if_link_state;
1727 ifd->ifi_datalen = sizeof(struct if_data);
1728 ifd->ifi_mtu = ifp->if_mtu;
1729 ifd->ifi_metric = ifp->if_metric;
1730 ifd->ifi_baudrate = ifp->if_baudrate;
1731 ifd->ifi_hwassist = ifp->if_hwassist;
1732 ifd->ifi_epoch = ifp->if_epoch;
1733 ifd->ifi_lastchange = ifp->if_lastchange;
1735 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1736 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1737 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1738 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1739 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1740 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1741 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1742 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1743 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1744 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1745 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1746 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1750 * Initialization, destruction and refcounting functions for ifaddrs.
1753 ifa_alloc(size_t size, int flags)
1757 KASSERT(size >= sizeof(struct ifaddr),
1758 ("%s: invalid size %zu", __func__, size));
1760 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1764 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1766 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1768 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1770 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1773 refcount_init(&ifa->ifa_refcnt, 1);
1778 /* free(NULL) is okay */
1779 counter_u64_free(ifa->ifa_opackets);
1780 counter_u64_free(ifa->ifa_ipackets);
1781 counter_u64_free(ifa->ifa_obytes);
1782 counter_u64_free(ifa->ifa_ibytes);
1783 free(ifa, M_IFADDR);
1789 ifa_ref(struct ifaddr *ifa)
1791 u_int old __diagused;
1793 old = refcount_acquire(&ifa->ifa_refcnt);
1794 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1798 ifa_try_ref(struct ifaddr *ifa)
1802 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1806 ifa_destroy(epoch_context_t ctx)
1810 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1811 counter_u64_free(ifa->ifa_opackets);
1812 counter_u64_free(ifa->ifa_ipackets);
1813 counter_u64_free(ifa->ifa_obytes);
1814 counter_u64_free(ifa->ifa_ibytes);
1815 free(ifa, M_IFADDR);
1819 ifa_free(struct ifaddr *ifa)
1822 if (refcount_release(&ifa->ifa_refcnt))
1823 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1827 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1828 * structs used to represent other address families, it is necessary
1829 * to perform a different comparison.
1832 #define sa_dl_equal(a1, a2) \
1833 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1834 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1835 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1836 CLLADDR((const struct sockaddr_dl *)(a2)), \
1837 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1840 * Locate an interface based on a complete address.
1844 ifa_ifwithaddr(const struct sockaddr *addr)
1851 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1852 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1853 if (ifa->ifa_addr->sa_family != addr->sa_family)
1855 if (sa_equal(addr, ifa->ifa_addr)) {
1858 /* IP6 doesn't have broadcast */
1859 if ((ifp->if_flags & IFF_BROADCAST) &&
1860 ifa->ifa_broadaddr &&
1861 ifa->ifa_broadaddr->sa_len != 0 &&
1862 sa_equal(ifa->ifa_broadaddr, addr)) {
1873 ifa_ifwithaddr_check(const struct sockaddr *addr)
1875 struct epoch_tracker et;
1878 NET_EPOCH_ENTER(et);
1879 rc = (ifa_ifwithaddr(addr) != NULL);
1885 * Locate an interface based on the broadcast address.
1889 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1895 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1896 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1898 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1899 if (ifa->ifa_addr->sa_family != addr->sa_family)
1901 if ((ifp->if_flags & IFF_BROADCAST) &&
1902 ifa->ifa_broadaddr &&
1903 ifa->ifa_broadaddr->sa_len != 0 &&
1904 sa_equal(ifa->ifa_broadaddr, addr)) {
1915 * Locate the point to point interface with a given destination address.
1919 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1925 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1926 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1928 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1930 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1931 if (ifa->ifa_addr->sa_family != addr->sa_family)
1933 if (ifa->ifa_dstaddr != NULL &&
1934 sa_equal(addr, ifa->ifa_dstaddr)) {
1945 * Find an interface on a specific network. If many, choice
1946 * is most specific found.
1949 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1953 struct ifaddr *ifa_maybe = NULL;
1954 u_int af = addr->sa_family;
1955 const char *addr_data = addr->sa_data, *cplim;
1956 const struct sockaddr_dl *sdl;
1960 * AF_LINK addresses can be looked up directly by their index number,
1961 * so do that if we can.
1963 if (af == AF_LINK) {
1964 sdl = (const struct sockaddr_dl *)addr;
1965 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) {
1966 ifp = ifnet_byindex(sdl->sdl_index);
1970 return (ifp->if_addr);
1975 * Scan though each interface, looking for ones that have addresses
1976 * in this address family and the requested fib.
1978 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1979 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1981 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1982 const char *cp, *cp2, *cp3;
1984 if (ifa->ifa_addr->sa_family != af)
1986 if (af == AF_INET &&
1987 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1989 * This is a bit broken as it doesn't
1990 * take into account that the remote end may
1991 * be a single node in the network we are
1993 * The trouble is that we don't know the
1994 * netmask for the remote end.
1996 if (ifa->ifa_dstaddr != NULL &&
1997 sa_equal(addr, ifa->ifa_dstaddr)) {
2002 * Scan all the bits in the ifa's address.
2003 * If a bit dissagrees with what we are
2004 * looking for, mask it with the netmask
2005 * to see if it really matters.
2006 * (A byte at a time)
2008 if (ifa->ifa_netmask == 0)
2011 cp2 = ifa->ifa_addr->sa_data;
2012 cp3 = ifa->ifa_netmask->sa_data;
2013 cplim = ifa->ifa_netmask->sa_len
2014 + (char *)ifa->ifa_netmask;
2016 if ((*cp++ ^ *cp2++) & *cp3++)
2017 goto next; /* next address! */
2019 * If the netmask of what we just found
2020 * is more specific than what we had before
2021 * (if we had one), or if the virtual status
2022 * of new prefix is better than of the old one,
2023 * then remember the new one before continuing
2024 * to search for an even better one.
2026 if (ifa_maybe == NULL ||
2027 ifa_preferred(ifa_maybe, ifa) ||
2028 rn_refines((caddr_t)ifa->ifa_netmask,
2029 (caddr_t)ifa_maybe->ifa_netmask)) {
2042 * Find an interface address specific to an interface best matching
2046 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2049 const char *cp, *cp2, *cp3;
2051 struct ifaddr *ifa_maybe = NULL;
2052 u_int af = addr->sa_family;
2058 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2059 if (ifa->ifa_addr->sa_family != af)
2061 if (ifa_maybe == NULL)
2063 if (ifa->ifa_netmask == 0) {
2064 if (sa_equal(addr, ifa->ifa_addr) ||
2065 (ifa->ifa_dstaddr &&
2066 sa_equal(addr, ifa->ifa_dstaddr)))
2070 if (ifp->if_flags & IFF_POINTOPOINT) {
2071 if (sa_equal(addr, ifa->ifa_dstaddr))
2075 cp2 = ifa->ifa_addr->sa_data;
2076 cp3 = ifa->ifa_netmask->sa_data;
2077 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2078 for (; cp3 < cplim; cp3++)
2079 if ((*cp++ ^ *cp2++) & *cp3)
2091 * See whether new ifa is better than current one:
2092 * 1) A non-virtual one is preferred over virtual.
2093 * 2) A virtual in master state preferred over any other state.
2095 * Used in several address selecting functions.
2098 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2101 return (cur->ifa_carp && (!next->ifa_carp ||
2102 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2105 struct sockaddr_dl *
2106 link_alloc_sdl(size_t size, int flags)
2109 return (malloc(size, M_TEMP, flags));
2113 link_free_sdl(struct sockaddr *sa)
2119 * Fills in given sdl with interface basic info.
2120 * Returns pointer to filled sdl.
2122 struct sockaddr_dl *
2123 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2125 struct sockaddr_dl *sdl;
2127 sdl = (struct sockaddr_dl *)paddr;
2128 memset(sdl, 0, sizeof(struct sockaddr_dl));
2129 sdl->sdl_len = sizeof(struct sockaddr_dl);
2130 sdl->sdl_family = AF_LINK;
2131 sdl->sdl_index = ifp->if_index;
2132 sdl->sdl_type = iftype;
2138 * Mark an interface down and notify protocols of
2142 if_unroute(struct ifnet *ifp, int flag, int fam)
2145 struct epoch_tracker et;
2147 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2149 ifp->if_flags &= ~flag;
2150 getmicrotime(&ifp->if_lastchange);
2151 NET_EPOCH_ENTER(et);
2152 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2153 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2154 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2156 ifp->if_qflush(ifp);
2159 (*carp_linkstate_p)(ifp);
2164 * Mark an interface up and notify protocols of
2168 if_route(struct ifnet *ifp, int flag, int fam)
2171 struct epoch_tracker et;
2173 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2175 ifp->if_flags |= flag;
2176 getmicrotime(&ifp->if_lastchange);
2177 NET_EPOCH_ENTER(et);
2178 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2179 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2180 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2183 (*carp_linkstate_p)(ifp);
2190 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2191 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2192 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2193 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2194 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2195 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2196 int (*vlan_setcookie_p)(struct ifnet *, void *);
2197 void *(*vlan_cookie_p)(struct ifnet *);
2200 * Handle a change in the interface link state. To avoid LORs
2201 * between driver lock and upper layer locks, as well as possible
2202 * recursions, we post event to taskqueue, and all job
2203 * is done in static do_link_state_change().
2206 if_link_state_change(struct ifnet *ifp, int link_state)
2208 /* Return if state hasn't changed. */
2209 if (ifp->if_link_state == link_state)
2212 ifp->if_link_state = link_state;
2214 /* XXXGL: reference ifp? */
2215 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2219 do_link_state_change(void *arg, int pending)
2225 link_state = ifp->if_link_state;
2227 CURVNET_SET(ifp->if_vnet);
2229 if (ifp->if_vlantrunk != NULL)
2230 (*vlan_link_state_p)(ifp);
2232 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2233 ifp->if_l2com != NULL)
2234 (*ng_ether_link_state_p)(ifp, link_state);
2236 (*carp_linkstate_p)(ifp);
2238 ifp->if_bridge_linkstate(ifp);
2240 (*lagg_linkstate_p)(ifp, link_state);
2242 if (IS_DEFAULT_VNET(curvnet))
2243 devctl_notify("IFNET", ifp->if_xname,
2244 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2247 if_printf(ifp, "%d link states coalesced\n", pending);
2248 if (log_link_state_change)
2249 if_printf(ifp, "link state changed to %s\n",
2250 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2251 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2256 * Mark an interface down and notify protocols of
2260 if_down(struct ifnet *ifp)
2263 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2264 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2268 * Mark an interface up and notify protocols of
2272 if_up(struct ifnet *ifp)
2275 if_route(ifp, IFF_UP, AF_UNSPEC);
2276 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2280 * Flush an interface queue.
2283 if_qflush(struct ifnet *ifp)
2291 if (ALTQ_IS_ENABLED(ifq))
2295 while ((m = n) != NULL) {
2306 * Map interface name to interface structure pointer, with or without
2307 * returning a reference.
2310 ifunit_ref(const char *name)
2312 struct epoch_tracker et;
2315 NET_EPOCH_ENTER(et);
2316 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2317 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2318 !(ifp->if_flags & IFF_DYING))
2328 ifunit(const char *name)
2330 struct epoch_tracker et;
2333 NET_EPOCH_ENTER(et);
2334 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2335 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2343 ifr_buffer_get_buffer(void *data)
2345 union ifreq_union *ifrup;
2348 #ifdef COMPAT_FREEBSD32
2349 if (SV_CURPROC_FLAG(SV_ILP32))
2350 return ((void *)(uintptr_t)
2351 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2353 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2357 ifr_buffer_set_buffer_null(void *data)
2359 union ifreq_union *ifrup;
2362 #ifdef COMPAT_FREEBSD32
2363 if (SV_CURPROC_FLAG(SV_ILP32))
2364 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2367 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2371 ifr_buffer_get_length(void *data)
2373 union ifreq_union *ifrup;
2376 #ifdef COMPAT_FREEBSD32
2377 if (SV_CURPROC_FLAG(SV_ILP32))
2378 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2380 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2384 ifr_buffer_set_length(void *data, size_t len)
2386 union ifreq_union *ifrup;
2389 #ifdef COMPAT_FREEBSD32
2390 if (SV_CURPROC_FLAG(SV_ILP32))
2391 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2394 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2398 ifr_data_get_ptr(void *ifrp)
2400 union ifreq_union *ifrup;
2403 #ifdef COMPAT_FREEBSD32
2404 if (SV_CURPROC_FLAG(SV_ILP32))
2405 return ((void *)(uintptr_t)
2406 ifrup->ifr32.ifr_ifru.ifru_data);
2408 return (ifrup->ifr.ifr_ifru.ifru_data);
2412 * Hardware specific interface ioctls.
2415 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2418 int error = 0, do_ifup = 0;
2419 int new_flags, temp_flags;
2420 size_t namelen, onamelen;
2422 char *descrbuf, *odescrbuf;
2423 char new_name[IFNAMSIZ];
2424 char old_name[IFNAMSIZ], strbuf[IFNAMSIZ + 8];
2426 struct sockaddr_dl *sdl;
2428 ifr = (struct ifreq *)data;
2431 ifr->ifr_index = ifp->if_index;
2435 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2436 ifr->ifr_flags = temp_flags & 0xffff;
2437 ifr->ifr_flagshigh = temp_flags >> 16;
2441 ifr->ifr_reqcap = ifp->if_capabilities;
2442 ifr->ifr_curcap = ifp->if_capenable;
2449 /* Ensure uninitialised padding is not leaked. */
2450 memset(&ifd, 0, sizeof(ifd));
2452 if_data_copy(ifp, &ifd);
2453 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2459 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2464 ifr->ifr_metric = ifp->if_metric;
2468 ifr->ifr_mtu = ifp->if_mtu;
2472 /* XXXGL: did this ever worked? */
2478 sx_slock(&ifdescr_sx);
2479 if (ifp->if_description == NULL)
2482 /* space for terminating nul */
2483 descrlen = strlen(ifp->if_description) + 1;
2484 if (ifr_buffer_get_length(ifr) < descrlen)
2485 ifr_buffer_set_buffer_null(ifr);
2487 error = copyout(ifp->if_description,
2488 ifr_buffer_get_buffer(ifr), descrlen);
2489 ifr_buffer_set_length(ifr, descrlen);
2491 sx_sunlock(&ifdescr_sx);
2495 error = priv_check(td, PRIV_NET_SETIFDESCR);
2500 * Copy only (length-1) bytes to make sure that
2501 * if_description is always nul terminated. The
2502 * length parameter is supposed to count the
2503 * terminating nul in.
2505 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2506 return (ENAMETOOLONG);
2507 else if (ifr_buffer_get_length(ifr) == 0)
2510 descrbuf = malloc(ifr_buffer_get_length(ifr),
2511 M_IFDESCR, M_WAITOK | M_ZERO);
2512 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2513 ifr_buffer_get_length(ifr) - 1);
2515 free(descrbuf, M_IFDESCR);
2520 sx_xlock(&ifdescr_sx);
2521 odescrbuf = ifp->if_description;
2522 ifp->if_description = descrbuf;
2523 sx_xunlock(&ifdescr_sx);
2525 getmicrotime(&ifp->if_lastchange);
2526 free(odescrbuf, M_IFDESCR);
2530 ifr->ifr_fib = ifp->if_fib;
2534 error = priv_check(td, PRIV_NET_SETIFFIB);
2537 if (ifr->ifr_fib >= rt_numfibs)
2540 ifp->if_fib = ifr->ifr_fib;
2544 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2548 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2549 * check, so we don't need special handling here yet.
2551 new_flags = (ifr->ifr_flags & 0xffff) |
2552 (ifr->ifr_flagshigh << 16);
2553 if (ifp->if_flags & IFF_UP &&
2554 (new_flags & IFF_UP) == 0) {
2556 } else if (new_flags & IFF_UP &&
2557 (ifp->if_flags & IFF_UP) == 0) {
2560 /* See if permanently promiscuous mode bit is about to flip */
2561 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2562 if (new_flags & IFF_PPROMISC)
2563 ifp->if_flags |= IFF_PROMISC;
2564 else if (ifp->if_pcount == 0)
2565 ifp->if_flags &= ~IFF_PROMISC;
2566 if (log_promisc_mode_change)
2567 if_printf(ifp, "permanently promiscuous mode %s\n",
2568 ((new_flags & IFF_PPROMISC) ?
2569 "enabled" : "disabled"));
2571 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2572 (new_flags &~ IFF_CANTCHANGE);
2573 if (ifp->if_ioctl) {
2574 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2578 getmicrotime(&ifp->if_lastchange);
2582 error = priv_check(td, PRIV_NET_SETIFCAP);
2585 if (ifp->if_ioctl == NULL)
2586 return (EOPNOTSUPP);
2587 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2589 error = (*ifp->if_ioctl)(ifp, cmd, data);
2591 getmicrotime(&ifp->if_lastchange);
2596 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2601 error = priv_check(td, PRIV_NET_SETIFNAME);
2604 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2608 if (new_name[0] == '\0')
2610 if (strcmp(new_name, ifp->if_xname) == 0)
2612 if (ifunit(new_name) != NULL)
2616 * XXX: Locking. Nothing else seems to lock if_flags,
2617 * and there are numerous other races with the
2618 * ifunit() checks not being atomic with namespace
2619 * changes (renames, vmoves, if_attach, etc).
2621 ifp->if_flags |= IFF_RENAMING;
2623 /* Announce the departure of the interface. */
2624 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2625 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2627 if_printf(ifp, "changing name to '%s'\n", new_name);
2630 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
2631 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2633 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2634 namelen = strlen(new_name);
2635 onamelen = sdl->sdl_nlen;
2637 * Move the address if needed. This is safe because we
2638 * allocate space for a name of length IFNAMSIZ when we
2639 * create this in if_attach().
2641 if (namelen != onamelen) {
2642 bcopy(sdl->sdl_data + onamelen,
2643 sdl->sdl_data + namelen, sdl->sdl_alen);
2645 bcopy(new_name, sdl->sdl_data, namelen);
2646 sdl->sdl_nlen = namelen;
2647 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2648 bzero(sdl->sdl_data, onamelen);
2649 while (namelen != 0)
2650 sdl->sdl_data[--namelen] = 0xff;
2651 IF_ADDR_WUNLOCK(ifp);
2653 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2654 /* Announce the return of the interface. */
2655 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2657 ifp->if_flags &= ~IFF_RENAMING;
2659 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
2660 devctl_notify("IFNET", old_name, "RENAME", strbuf);
2665 error = priv_check(td, PRIV_NET_SETIFVNET);
2668 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2673 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2676 ifp->if_metric = ifr->ifr_metric;
2677 getmicrotime(&ifp->if_lastchange);
2681 error = priv_check(td, PRIV_NET_SETIFPHYS);
2684 if (ifp->if_ioctl == NULL)
2685 return (EOPNOTSUPP);
2686 error = (*ifp->if_ioctl)(ifp, cmd, data);
2688 getmicrotime(&ifp->if_lastchange);
2693 u_long oldmtu = ifp->if_mtu;
2695 error = priv_check(td, PRIV_NET_SETIFMTU);
2698 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2700 if (ifp->if_ioctl == NULL)
2701 return (EOPNOTSUPP);
2702 /* Disallow MTU changes on bridge member interfaces. */
2704 return (EOPNOTSUPP);
2705 error = (*ifp->if_ioctl)(ifp, cmd, data);
2707 getmicrotime(&ifp->if_lastchange);
2710 DEBUGNET_NOTIFY_MTU(ifp);
2714 * If the link MTU changed, do network layer specific procedure.
2716 if (ifp->if_mtu != oldmtu) {
2727 if (cmd == SIOCADDMULTI)
2728 error = priv_check(td, PRIV_NET_ADDMULTI);
2730 error = priv_check(td, PRIV_NET_DELMULTI);
2734 /* Don't allow group membership on non-multicast interfaces. */
2735 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2736 return (EOPNOTSUPP);
2738 /* Don't let users screw up protocols' entries. */
2739 if (ifr->ifr_addr.sa_family != AF_LINK)
2742 if (cmd == SIOCADDMULTI) {
2743 struct epoch_tracker et;
2744 struct ifmultiaddr *ifma;
2747 * Userland is only permitted to join groups once
2748 * via the if_addmulti() KPI, because it cannot hold
2749 * struct ifmultiaddr * between calls. It may also
2750 * lose a race while we check if the membership
2753 NET_EPOCH_ENTER(et);
2754 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2759 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2761 error = if_delmulti(ifp, &ifr->ifr_addr);
2764 getmicrotime(&ifp->if_lastchange);
2767 case SIOCSIFPHYADDR:
2768 case SIOCDIFPHYADDR:
2770 case SIOCSIFPHYADDR_IN6:
2773 case SIOCSIFGENERIC:
2774 error = priv_check(td, PRIV_NET_HWIOCTL);
2777 if (ifp->if_ioctl == NULL)
2778 return (EOPNOTSUPP);
2779 error = (*ifp->if_ioctl)(ifp, cmd, data);
2781 getmicrotime(&ifp->if_lastchange);
2785 case SIOCGIFPSRCADDR:
2786 case SIOCGIFPDSTADDR:
2789 case SIOCGIFGENERIC:
2791 case SIOCGIFRSSHASH:
2792 case SIOCGIFDOWNREASON:
2793 if (ifp->if_ioctl == NULL)
2794 return (EOPNOTSUPP);
2795 error = (*ifp->if_ioctl)(ifp, cmd, data);
2799 error = priv_check(td, PRIV_NET_SETLLADDR);
2802 error = if_setlladdr(ifp,
2803 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2807 error = if_gethwaddr(ifp, ifr);
2811 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2814 error = if_addgroup(ifp,
2815 ((struct ifgroupreq *)data)->ifgr_group);
2822 struct epoch_tracker et;
2824 NET_EPOCH_ENTER(et);
2825 error = if_getgroup((struct ifgroupreq *)data, ifp);
2831 error = priv_check(td, PRIV_NET_DELIFGROUP);
2834 error = if_delgroup(ifp,
2835 ((struct ifgroupreq *)data)->ifgr_group);
2851 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2853 #ifdef COMPAT_FREEBSD32
2857 struct ifgroupreq ifgr;
2858 struct ifmediareq ifmr;
2861 struct ifconf32 *ifc32;
2862 struct ifdrv32 *ifd32;
2863 struct ifgroupreq32 *ifgr32;
2864 struct ifmediareq32 *ifmr32;
2874 CURVNET_SET(so->so_vnet);
2876 /* Make sure the VNET is stable. */
2877 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2884 #ifdef COMPAT_FREEBSD32
2888 ifc32 = (struct ifconf32 *)data;
2889 thunk.ifc.ifc_len = ifc32->ifc_len;
2890 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2891 data = (caddr_t)&thunk.ifc;
2894 case SIOCGDRVSPEC32:
2895 case SIOCSDRVSPEC32:
2896 ifd32 = (struct ifdrv32 *)data;
2897 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2898 sizeof(thunk.ifd.ifd_name));
2899 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2900 thunk.ifd.ifd_len = ifd32->ifd_len;
2901 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2902 data = (caddr_t)&thunk.ifd;
2903 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2905 case SIOCAIFGROUP32:
2906 case SIOCGIFGROUP32:
2907 case SIOCDIFGROUP32:
2908 case SIOCGIFGMEMB32:
2909 ifgr32 = (struct ifgroupreq32 *)data;
2910 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2911 sizeof(thunk.ifgr.ifgr_name));
2912 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2914 case SIOCAIFGROUP32:
2915 case SIOCDIFGROUP32:
2916 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2917 sizeof(thunk.ifgr.ifgr_group));
2919 case SIOCGIFGROUP32:
2920 case SIOCGIFGMEMB32:
2921 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2924 data = (caddr_t)&thunk.ifgr;
2925 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2927 case SIOCGIFMEDIA32:
2928 case SIOCGIFXMEDIA32:
2929 ifmr32 = (struct ifmediareq32 *)data;
2930 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2931 sizeof(thunk.ifmr.ifm_name));
2932 thunk.ifmr.ifm_current = ifmr32->ifm_current;
2933 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
2934 thunk.ifmr.ifm_status = ifmr32->ifm_status;
2935 thunk.ifmr.ifm_active = ifmr32->ifm_active;
2936 thunk.ifmr.ifm_count = ifmr32->ifm_count;
2937 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
2938 data = (caddr_t)&thunk.ifmr;
2939 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2946 error = ifconf(cmd, data);
2950 ifr = (struct ifreq *)data;
2954 error = priv_check(td, PRIV_NET_SETIFVNET);
2956 error = if_vmove_reclaim(td, ifr->ifr_name,
2962 error = priv_check(td, PRIV_NET_IFCREATE);
2964 error = if_clone_create(ifr->ifr_name,
2965 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
2966 ifr_data_get_ptr(ifr) : NULL);
2969 error = priv_check(td, PRIV_NET_IFDESTROY);
2972 sx_xlock(&ifnet_detach_sxlock);
2973 error = if_clone_destroy(ifr->ifr_name);
2974 sx_xunlock(&ifnet_detach_sxlock);
2978 case SIOCIFGCLONERS:
2979 error = if_clone_list((struct if_clonereq *)data);
2983 error = if_getgroupmembers((struct ifgroupreq *)data);
2986 #if defined(INET) || defined(INET6)
2989 if (carp_ioctl_p == NULL)
2990 error = EPROTONOSUPPORT;
2992 error = (*carp_ioctl_p)(ifr, cmd, td);
2997 ifp = ifunit_ref(ifr->ifr_name);
3003 error = ifhwioctl(cmd, ifp, data, td);
3004 if (error != ENOIOCTL)
3007 oif_flags = ifp->if_flags;
3008 if (so->so_proto == NULL) {
3014 * Pass the request on to the socket control method, and if the
3015 * latter returns EOPNOTSUPP, directly to the interface.
3017 * Make an exception for the legacy SIOCSIF* requests. Drivers
3018 * trust SIOCSIFADDR et al to come from an already privileged
3019 * layer, and do not perform any credentials checks or input
3022 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3024 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3025 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3026 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3027 error = (*ifp->if_ioctl)(ifp, cmd, data);
3029 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3031 if (ifp->if_flags & IFF_UP)
3040 #ifdef COMPAT_FREEBSD32
3043 switch (saved_cmd) {
3045 ifc32->ifc_len = thunk.ifc.ifc_len;
3047 case SIOCGDRVSPEC32:
3049 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3050 * the struct so just assert that ifd_len (the only
3051 * field it might make sense to update) hasn't
3054 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3055 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3056 thunk.ifd.ifd_len));
3058 case SIOCGIFGROUP32:
3059 case SIOCGIFGMEMB32:
3060 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3062 case SIOCGIFMEDIA32:
3063 case SIOCGIFXMEDIA32:
3064 ifmr32->ifm_current = thunk.ifmr.ifm_current;
3065 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3066 ifmr32->ifm_status = thunk.ifmr.ifm_status;
3067 ifmr32->ifm_active = thunk.ifmr.ifm_active;
3068 ifmr32->ifm_count = thunk.ifmr.ifm_count;
3076 * The code common to handling reference counted flags,
3077 * e.g., in ifpromisc() and if_allmulti().
3078 * The "pflag" argument can specify a permanent mode flag to check,
3079 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3081 * Only to be used on stack-owned flags, not driver-owned flags.
3084 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3088 int oldflags, oldcount;
3090 /* Sanity checks to catch programming errors */
3091 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3092 ("%s: setting driver-owned flag %d", __func__, flag));
3095 KASSERT(*refcount >= 0,
3096 ("%s: increment negative refcount %d for flag %d",
3097 __func__, *refcount, flag));
3099 KASSERT(*refcount > 0,
3100 ("%s: decrement non-positive refcount %d for flag %d",
3101 __func__, *refcount, flag));
3103 /* In case this mode is permanent, just touch refcount */
3104 if (ifp->if_flags & pflag) {
3105 *refcount += onswitch ? 1 : -1;
3109 /* Save ifnet parameters for if_ioctl() may fail */
3110 oldcount = *refcount;
3111 oldflags = ifp->if_flags;
3114 * See if we aren't the only and touching refcount is enough.
3115 * Actually toggle interface flag if we are the first or last.
3120 ifp->if_flags |= flag;
3124 ifp->if_flags &= ~flag;
3127 /* Call down the driver since we've changed interface flags */
3128 if (ifp->if_ioctl == NULL) {
3132 ifr.ifr_flags = ifp->if_flags & 0xffff;
3133 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3134 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3137 /* Notify userland that interface flags have changed */
3142 /* Recover after driver error */
3143 *refcount = oldcount;
3144 ifp->if_flags = oldflags;
3149 * Set/clear promiscuous mode on interface ifp based on the truth value
3150 * of pswitch. The calls are reference counted so that only the first
3151 * "on" request actually has an effect, as does the final "off" request.
3152 * Results are undefined if the "off" and "on" requests are not matched.
3155 ifpromisc(struct ifnet *ifp, int pswitch)
3158 int oldflags = ifp->if_flags;
3160 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3161 &ifp->if_pcount, pswitch);
3162 /* If promiscuous mode status has changed, log a message */
3163 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3164 log_promisc_mode_change)
3165 if_printf(ifp, "promiscuous mode %s\n",
3166 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3171 * Return interface configuration
3172 * of system. List may be used
3173 * in later ioctl's (above) to get
3174 * other information.
3178 ifconf(u_long cmd, caddr_t data)
3180 struct ifconf *ifc = (struct ifconf *)data;
3185 int error, full = 0, valid_len, max_len;
3187 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3188 max_len = maxphys - 1;
3190 /* Prevent hostile input from being able to crash the system */
3191 if (ifc->ifc_len <= 0)
3195 if (ifc->ifc_len <= max_len) {
3196 max_len = ifc->ifc_len;
3199 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3204 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3205 struct epoch_tracker et;
3209 * Zero the ifr to make sure we don't disclose the contents
3212 memset(&ifr, 0, sizeof(ifr));
3214 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3215 >= sizeof(ifr.ifr_name)) {
3218 return (ENAMETOOLONG);
3222 NET_EPOCH_ENTER(et);
3223 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3224 struct sockaddr *sa = ifa->ifa_addr;
3226 if (prison_if(curthread->td_ucred, sa) != 0)
3229 if (sa->sa_len <= sizeof(*sa)) {
3230 if (sa->sa_len < sizeof(*sa)) {
3231 memset(&ifr.ifr_ifru.ifru_addr, 0,
3232 sizeof(ifr.ifr_ifru.ifru_addr));
3233 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3236 ifr.ifr_ifru.ifru_addr = *sa;
3237 sbuf_bcat(sb, &ifr, sizeof(ifr));
3238 max_len += sizeof(ifr);
3241 offsetof(struct ifreq, ifr_addr));
3242 max_len += offsetof(struct ifreq, ifr_addr);
3243 sbuf_bcat(sb, sa, sa->sa_len);
3244 max_len += sa->sa_len;
3247 if (sbuf_error(sb) == 0)
3248 valid_len = sbuf_len(sb);
3252 sbuf_bcat(sb, &ifr, sizeof(ifr));
3253 max_len += sizeof(ifr);
3255 if (sbuf_error(sb) == 0)
3256 valid_len = sbuf_len(sb);
3262 * If we didn't allocate enough space (uncommon), try again. If
3263 * we have already allocated as much space as we are allowed,
3264 * return what we've got.
3266 if (valid_len != max_len && !full) {
3271 ifc->ifc_len = valid_len;
3273 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3279 * Just like ifpromisc(), but for all-multicast-reception mode.
3282 if_allmulti(struct ifnet *ifp, int onswitch)
3285 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3288 struct ifmultiaddr *
3289 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3291 struct ifmultiaddr *ifma;
3293 IF_ADDR_LOCK_ASSERT(ifp);
3295 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3296 if (sa->sa_family == AF_LINK) {
3297 if (sa_dl_equal(ifma->ifma_addr, sa))
3300 if (sa_equal(ifma->ifma_addr, sa))
3309 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3310 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3311 * the ifnet multicast address list here, so the caller must do that and
3312 * other setup work (such as notifying the device driver). The reference
3313 * count is initialized to 1.
3315 static struct ifmultiaddr *
3316 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3319 struct ifmultiaddr *ifma;
3320 struct sockaddr *dupsa;
3322 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3327 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3328 if (dupsa == NULL) {
3329 free(ifma, M_IFMADDR);
3332 bcopy(sa, dupsa, sa->sa_len);
3333 ifma->ifma_addr = dupsa;
3335 ifma->ifma_ifp = ifp;
3336 ifma->ifma_refcount = 1;
3337 ifma->ifma_protospec = NULL;
3340 ifma->ifma_lladdr = NULL;
3344 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3345 if (dupsa == NULL) {
3346 free(ifma->ifma_addr, M_IFMADDR);
3347 free(ifma, M_IFMADDR);
3350 bcopy(llsa, dupsa, llsa->sa_len);
3351 ifma->ifma_lladdr = dupsa;
3357 * if_freemulti: free ifmultiaddr structure and possibly attached related
3358 * addresses. The caller is responsible for implementing reference
3359 * counting, notifying the driver, handling routing messages, and releasing
3360 * any dependent link layer state.
3362 #ifdef MCAST_VERBOSE
3363 extern void kdb_backtrace(void);
3366 if_freemulti_internal(struct ifmultiaddr *ifma)
3369 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3370 ifma->ifma_refcount));
3372 if (ifma->ifma_lladdr != NULL)
3373 free(ifma->ifma_lladdr, M_IFMADDR);
3374 #ifdef MCAST_VERBOSE
3376 printf("%s freeing ifma: %p\n", __func__, ifma);
3378 free(ifma->ifma_addr, M_IFMADDR);
3379 free(ifma, M_IFMADDR);
3383 if_destroymulti(epoch_context_t ctx)
3385 struct ifmultiaddr *ifma;
3387 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3388 if_freemulti_internal(ifma);
3392 if_freemulti(struct ifmultiaddr *ifma)
3394 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3395 ifma->ifma_refcount));
3397 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3401 * Register an additional multicast address with a network interface.
3403 * - If the address is already present, bump the reference count on the
3404 * address and return.
3405 * - If the address is not link-layer, look up a link layer address.
3406 * - Allocate address structures for one or both addresses, and attach to the
3407 * multicast address list on the interface. If automatically adding a link
3408 * layer address, the protocol address will own a reference to the link
3409 * layer address, to be freed when it is freed.
3410 * - Notify the network device driver of an addition to the multicast address
3413 * 'sa' points to caller-owned memory with the desired multicast address.
3415 * 'retifma' will be used to return a pointer to the resulting multicast
3416 * address reference, if desired.
3419 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3420 struct ifmultiaddr **retifma)
3422 struct ifmultiaddr *ifma, *ll_ifma;
3423 struct sockaddr *llsa;
3424 struct sockaddr_dl sdl;
3428 IN_MULTI_LIST_UNLOCK_ASSERT();
3431 IN6_MULTI_LIST_UNLOCK_ASSERT();
3434 * If the address is already present, return a new reference to it;
3435 * otherwise, allocate storage and set up a new address.
3438 ifma = if_findmulti(ifp, sa);
3440 ifma->ifma_refcount++;
3441 if (retifma != NULL)
3443 IF_ADDR_WUNLOCK(ifp);
3448 * The address isn't already present; resolve the protocol address
3449 * into a link layer address, and then look that up, bump its
3450 * refcount or allocate an ifma for that also.
3451 * Most link layer resolving functions returns address data which
3452 * fits inside default sockaddr_dl structure. However callback
3453 * can allocate another sockaddr structure, in that case we need to
3458 if (ifp->if_resolvemulti != NULL) {
3459 /* Provide called function with buffer size information */
3460 sdl.sdl_len = sizeof(sdl);
3461 llsa = (struct sockaddr *)&sdl;
3462 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3468 * Allocate the new address. Don't hook it up yet, as we may also
3469 * need to allocate a link layer multicast address.
3471 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3478 * If a link layer address is found, we'll need to see if it's
3479 * already present in the address list, or allocate is as well.
3480 * When this block finishes, the link layer address will be on the
3484 ll_ifma = if_findmulti(ifp, llsa);
3485 if (ll_ifma == NULL) {
3486 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3487 if (ll_ifma == NULL) {
3488 --ifma->ifma_refcount;
3493 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3494 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3497 ll_ifma->ifma_refcount++;
3498 ifma->ifma_llifma = ll_ifma;
3502 * We now have a new multicast address, ifma, and possibly a new or
3503 * referenced link layer address. Add the primary address to the
3504 * ifnet address list.
3506 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3507 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3509 if (retifma != NULL)
3513 * Must generate the message while holding the lock so that 'ifma'
3514 * pointer is still valid.
3516 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3517 IF_ADDR_WUNLOCK(ifp);
3520 * We are certain we have added something, so call down to the
3521 * interface to let them know about it.
3523 if (ifp->if_ioctl != NULL) {
3524 if (THREAD_CAN_SLEEP())
3525 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3527 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3530 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3531 link_free_sdl(llsa);
3536 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3537 link_free_sdl(llsa);
3540 IF_ADDR_WUNLOCK(ifp);
3545 if_siocaddmulti(void *arg, int pending)
3552 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3554 CURVNET_SET(ifp->if_vnet);
3555 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3560 * Delete a multicast group membership by network-layer group address.
3562 * Returns ENOENT if the entry could not be found. If ifp no longer
3563 * exists, results are undefined. This entry point should only be used
3564 * from subsystems which do appropriate locking to hold ifp for the
3565 * duration of the call.
3566 * Network-layer protocol domains must use if_delmulti_ifma().
3569 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3571 struct ifmultiaddr *ifma;
3574 KASSERT(ifp, ("%s: NULL ifp", __func__));
3578 ifma = if_findmulti(ifp, sa);
3580 lastref = if_delmulti_locked(ifp, ifma, 0);
3581 IF_ADDR_WUNLOCK(ifp);
3586 if (lastref && ifp->if_ioctl != NULL) {
3587 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3594 * Delete all multicast group membership for an interface.
3595 * Should be used to quickly flush all multicast filters.
3598 if_delallmulti(struct ifnet *ifp)
3600 struct ifmultiaddr *ifma;
3601 struct ifmultiaddr *next;
3604 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3605 if_delmulti_locked(ifp, ifma, 0);
3606 IF_ADDR_WUNLOCK(ifp);
3610 if_delmulti_ifma(struct ifmultiaddr *ifma)
3612 if_delmulti_ifma_flags(ifma, 0);
3616 * Delete a multicast group membership by group membership pointer.
3617 * Network-layer protocol domains must use this routine.
3619 * It is safe to call this routine if the ifp disappeared.
3622 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3626 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3628 IN_MULTI_LIST_UNLOCK_ASSERT();
3630 ifp = ifma->ifma_ifp;
3633 printf("%s: ifma_ifp seems to be detached\n", __func__);
3635 struct epoch_tracker et;
3638 NET_EPOCH_ENTER(et);
3639 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3648 * If and only if the ifnet instance exists: Acquire the address lock.
3653 lastref = if_delmulti_locked(ifp, ifma, flags);
3657 * If and only if the ifnet instance exists:
3658 * Release the address lock.
3659 * If the group was left: update the hardware hash filter.
3661 IF_ADDR_WUNLOCK(ifp);
3662 if (lastref && ifp->if_ioctl != NULL) {
3663 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3669 * Perform deletion of network-layer and/or link-layer multicast address.
3671 * Return 0 if the reference count was decremented.
3672 * Return 1 if the final reference was released, indicating that the
3673 * hardware hash filter should be reprogrammed.
3676 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3678 struct ifmultiaddr *ll_ifma;
3680 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3681 KASSERT(ifma->ifma_ifp == ifp,
3682 ("%s: inconsistent ifp %p", __func__, ifp));
3683 IF_ADDR_WLOCK_ASSERT(ifp);
3686 ifp = ifma->ifma_ifp;
3687 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3690 * If the ifnet is detaching, null out references to ifnet,
3691 * so that upper protocol layers will notice, and not attempt
3692 * to obtain locks for an ifnet which no longer exists. The
3693 * routing socket announcement must happen before the ifnet
3694 * instance is detached from the system.
3698 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3701 * ifp may already be nulled out if we are being reentered
3702 * to delete the ll_ifma.
3705 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3706 ifma->ifma_ifp = NULL;
3710 if (--ifma->ifma_refcount > 0)
3713 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3714 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3715 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3718 * If this ifma is a network-layer ifma, a link-layer ifma may
3719 * have been associated with it. Release it first if so.
3721 ll_ifma = ifma->ifma_llifma;
3722 if (ll_ifma != NULL) {
3723 KASSERT(ifma->ifma_lladdr != NULL,
3724 ("%s: llifma w/o lladdr", __func__));
3726 ll_ifma->ifma_ifp = NULL; /* XXX */
3727 if (--ll_ifma->ifma_refcount == 0) {
3729 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3730 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3732 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3735 if_freemulti(ll_ifma);
3740 struct ifmultiaddr *ifmatmp;
3742 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3743 MPASS(ifma != ifmatmp);
3748 * The last reference to this instance of struct ifmultiaddr
3749 * was released; the hardware should be notified of this change.
3755 * Set the link layer address on an interface.
3757 * At this time we only support certain types of interfaces,
3758 * and we don't allow the length of the address to change.
3760 * Set noinline to be dtrace-friendly
3763 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3765 struct sockaddr_dl *sdl;
3773 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3777 if (len != sdl->sdl_alen) /* don't allow length to change */
3780 switch (ifp->if_type) {
3785 case IFT_IEEE8023ADLAG:
3786 bcopy(lladdr, LLADDR(sdl), len);
3793 * If the interface is already up, we need
3794 * to re-init it in order to reprogram its
3797 if ((ifp->if_flags & IFF_UP) != 0) {
3798 if (ifp->if_ioctl) {
3799 ifp->if_flags &= ~IFF_UP;
3800 ifr.ifr_flags = ifp->if_flags & 0xffff;
3801 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3802 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3803 ifp->if_flags |= IFF_UP;
3804 ifr.ifr_flags = ifp->if_flags & 0xffff;
3805 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3806 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3809 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3815 * Compat function for handling basic encapsulation requests.
3816 * Not converted stacks (FDDI, IB, ..) supports traditional
3817 * output model: ARP (and other similar L2 protocols) are handled
3818 * inside output routine, arpresolve/nd6_resolve() returns MAC
3819 * address instead of full prepend.
3821 * This function creates calculated header==MAC for IPv4/IPv6 and
3822 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3826 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3829 if (req->rtype != IFENCAP_LL)
3830 return (EOPNOTSUPP);
3832 if (req->bufsize < req->lladdr_len)
3835 switch (req->family) {
3840 return (EAFNOSUPPORT);
3843 /* Copy lladdr to storage as is */
3844 memmove(req->buf, req->lladdr, req->lladdr_len);
3845 req->bufsize = req->lladdr_len;
3846 req->lladdr_off = 0;
3852 * Tunnel interfaces can nest, also they may cause infinite recursion
3853 * calls when misconfigured. We'll prevent this by detecting loops.
3854 * High nesting level may cause stack exhaustion. We'll prevent this
3855 * by introducing upper limit.
3857 * Return 0, if tunnel nesting count is equal or less than limit.
3860 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3868 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3869 if (*(struct ifnet **)(mtag + 1) == ifp) {
3870 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3875 if (count > limit) {
3877 "%s: if_output recursively called too many times(%d)\n",
3878 if_name(ifp), count);
3881 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3884 *(struct ifnet **)(mtag + 1) = ifp;
3885 m_tag_prepend(m, mtag);
3890 * Get the link layer address that was read from the hardware at attach.
3892 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3893 * their component interfaces as IFT_IEEE8023ADLAG.
3896 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3899 if (ifp->if_hw_addr == NULL)
3902 switch (ifp->if_type) {
3904 case IFT_IEEE8023ADLAG:
3905 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3913 * The name argument must be a pointer to storage which will last as
3914 * long as the interface does. For physical devices, the result of
3915 * device_get_name(dev) is a good choice and for pseudo-devices a
3916 * static string works well.
3919 if_initname(struct ifnet *ifp, const char *name, int unit)
3921 ifp->if_dname = name;
3922 ifp->if_dunit = unit;
3923 if (unit != IF_DUNIT_NONE)
3924 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3926 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3930 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
3934 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3935 vlog(pri, if_fmt, ap);
3941 if_printf(struct ifnet *ifp, const char *fmt, ...)
3946 if_vlog(ifp, LOG_INFO, fmt, ap);
3952 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
3957 if_vlog(ifp, pri, fmt, ap);
3963 if_start(struct ifnet *ifp)
3966 (*(ifp)->if_start)(ifp);
3970 * Backwards compatibility interface for drivers
3971 * that have not implemented it
3974 if_transmit(struct ifnet *ifp, struct mbuf *m)
3978 IFQ_HANDOFF(ifp, m, error);
3983 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3990 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3995 if (_IF_QFULL(ifq)) {
3997 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4002 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4003 if (m->m_flags & (M_BCAST|M_MCAST))
4004 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4005 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4007 _IF_ENQUEUE(ifq, m);
4009 if (ifp != NULL && !active)
4010 (*(ifp)->if_start)(ifp);
4015 if_register_com_alloc(u_char type,
4016 if_com_alloc_t *a, if_com_free_t *f)
4019 KASSERT(if_com_alloc[type] == NULL,
4020 ("if_register_com_alloc: %d already registered", type));
4021 KASSERT(if_com_free[type] == NULL,
4022 ("if_register_com_alloc: %d free already registered", type));
4024 if_com_alloc[type] = a;
4025 if_com_free[type] = f;
4029 if_deregister_com_alloc(u_char type)
4032 KASSERT(if_com_alloc[type] != NULL,
4033 ("if_deregister_com_alloc: %d not registered", type));
4034 KASSERT(if_com_free[type] != NULL,
4035 ("if_deregister_com_alloc: %d free not registered", type));
4038 * Ensure all pending EPOCH(9) callbacks have been executed. This
4039 * fixes issues about late invocation of if_destroy(), which leads
4040 * to memory leak from if_com_alloc[type] allocated if_l2com.
4042 epoch_drain_callbacks(net_epoch_preempt);
4044 if_com_alloc[type] = NULL;
4045 if_com_free[type] = NULL;
4048 /* API for driver access to network stack owned ifnet.*/
4050 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4054 oldbrate = ifp->if_baudrate;
4055 ifp->if_baudrate = baudrate;
4060 if_getbaudrate(if_t ifp)
4063 return (((struct ifnet *)ifp)->if_baudrate);
4067 if_setcapabilities(if_t ifp, int capabilities)
4069 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4074 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4076 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4077 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4083 if_getcapabilities(if_t ifp)
4085 return ((struct ifnet *)ifp)->if_capabilities;
4089 if_setcapenable(if_t ifp, int capabilities)
4091 ((struct ifnet *)ifp)->if_capenable = capabilities;
4096 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4099 ((struct ifnet *)ifp)->if_capenable |= setcap;
4101 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4107 if_getdname(if_t ifp)
4109 return ((struct ifnet *)ifp)->if_dname;
4113 if_togglecapenable(if_t ifp, int togglecap)
4115 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4120 if_getcapenable(if_t ifp)
4122 return ((struct ifnet *)ifp)->if_capenable;
4126 * This is largely undesirable because it ties ifnet to a device, but does
4127 * provide flexiblity for an embedded product vendor. Should be used with
4128 * the understanding that it violates the interface boundaries, and should be
4129 * a last resort only.
4132 if_setdev(if_t ifp, void *dev)
4138 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4140 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4141 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4147 if_getdrvflags(if_t ifp)
4149 return ((struct ifnet *)ifp)->if_drv_flags;
4153 if_setdrvflags(if_t ifp, int flags)
4155 ((struct ifnet *)ifp)->if_drv_flags = flags;
4160 if_setflags(if_t ifp, int flags)
4163 ifp->if_flags = flags;
4168 if_setflagbits(if_t ifp, int set, int clear)
4170 ((struct ifnet *)ifp)->if_flags |= set;
4171 ((struct ifnet *)ifp)->if_flags &= ~clear;
4177 if_getflags(if_t ifp)
4179 return ((struct ifnet *)ifp)->if_flags;
4183 if_clearhwassist(if_t ifp)
4185 ((struct ifnet *)ifp)->if_hwassist = 0;
4190 if_sethwassistbits(if_t ifp, int toset, int toclear)
4192 ((struct ifnet *)ifp)->if_hwassist |= toset;
4193 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4199 if_sethwassist(if_t ifp, int hwassist_bit)
4201 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4206 if_gethwassist(if_t ifp)
4208 return ((struct ifnet *)ifp)->if_hwassist;
4212 if_setmtu(if_t ifp, int mtu)
4214 ((struct ifnet *)ifp)->if_mtu = mtu;
4221 return ((struct ifnet *)ifp)->if_mtu;
4225 if_getmtu_family(if_t ifp, int family)
4229 for (dp = domains; dp; dp = dp->dom_next) {
4230 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4231 return (dp->dom_ifmtu((struct ifnet *)ifp));
4234 return (((struct ifnet *)ifp)->if_mtu);
4238 * Methods for drivers to access interface unicast and multicast
4239 * link level addresses. Driver shall not know 'struct ifaddr' neither
4240 * 'struct ifmultiaddr'.
4243 if_lladdr_count(if_t ifp)
4245 struct epoch_tracker et;
4250 NET_EPOCH_ENTER(et);
4251 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4252 if (ifa->ifa_addr->sa_family == AF_LINK)
4260 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4262 struct epoch_tracker et;
4269 NET_EPOCH_ENTER(et);
4270 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4271 if (ifa->ifa_addr->sa_family != AF_LINK)
4273 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4282 if_llmaddr_count(if_t ifp)
4284 struct epoch_tracker et;
4285 struct ifmultiaddr *ifma;
4289 NET_EPOCH_ENTER(et);
4290 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4291 if (ifma->ifma_addr->sa_family == AF_LINK)
4299 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4301 struct epoch_tracker et;
4302 struct ifmultiaddr *ifma;
4308 NET_EPOCH_ENTER(et);
4309 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4310 if (ifma->ifma_addr->sa_family != AF_LINK)
4312 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4321 if_setsoftc(if_t ifp, void *softc)
4323 ((struct ifnet *)ifp)->if_softc = softc;
4328 if_getsoftc(if_t ifp)
4330 return ((struct ifnet *)ifp)->if_softc;
4334 if_setrcvif(struct mbuf *m, if_t ifp)
4337 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4338 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4342 if_setvtag(struct mbuf *m, uint16_t tag)
4344 m->m_pkthdr.ether_vtag = tag;
4348 if_getvtag(struct mbuf *m)
4351 return (m->m_pkthdr.ether_vtag);
4355 if_sendq_empty(if_t ifp)
4357 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4361 if_getifaddr(if_t ifp)
4363 return ((struct ifnet *)ifp)->if_addr;
4367 if_getamcount(if_t ifp)
4369 return ((struct ifnet *)ifp)->if_amcount;
4373 if_setsendqready(if_t ifp)
4375 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4380 if_setsendqlen(if_t ifp, int tx_desc_count)
4382 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4383 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4389 if_vlantrunkinuse(if_t ifp)
4391 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4395 if_input(if_t ifp, struct mbuf* sendmp)
4397 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4403 if_dequeue(if_t ifp)
4406 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4412 if_sendq_prepend(if_t ifp, struct mbuf *m)
4414 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4419 if_setifheaderlen(if_t ifp, int len)
4421 ((struct ifnet *)ifp)->if_hdrlen = len;
4426 if_getlladdr(if_t ifp)
4428 return (IF_LLADDR((struct ifnet *)ifp));
4432 if_gethandle(u_char type)
4434 return (if_alloc(type));
4438 if_bpfmtap(if_t ifh, struct mbuf *m)
4440 struct ifnet *ifp = (struct ifnet *)ifh;
4446 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4448 struct ifnet *ifp = (struct ifnet *)ifh;
4450 ETHER_BPF_MTAP(ifp, m);
4454 if_vlancap(if_t ifh)
4456 struct ifnet *ifp = (struct ifnet *)ifh;
4457 VLAN_CAPABILITIES(ifp);
4461 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4464 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4469 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4472 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4477 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4480 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4485 if_gethwtsomax(if_t ifp)
4488 return (((struct ifnet *)ifp)->if_hw_tsomax);
4492 if_gethwtsomaxsegcount(if_t ifp)
4495 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4499 if_gethwtsomaxsegsize(if_t ifp)
4502 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4506 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4508 ((struct ifnet *)ifp)->if_init = init_fn;
4512 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4514 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4518 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4520 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4524 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4526 ((struct ifnet *)ifp)->if_transmit = start_fn;
4529 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4531 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4536 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4539 ifp->if_get_counter = fn;
4544 if_show_ifnet(struct ifnet *ifp)
4549 db_printf("%s:\n", ifp->if_xname);
4550 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
4551 IF_DB_PRINTF("%s", if_dname);
4552 IF_DB_PRINTF("%d", if_dunit);
4553 IF_DB_PRINTF("%s", if_description);
4554 IF_DB_PRINTF("%u", if_index);
4555 IF_DB_PRINTF("%u", if_refcount);
4556 IF_DB_PRINTF("%d", if_index_reserved);
4557 IF_DB_PRINTF("%p", if_softc);
4558 IF_DB_PRINTF("%p", if_l2com);
4559 IF_DB_PRINTF("%p", if_llsoftc);
4560 IF_DB_PRINTF("%d", if_amcount);
4561 IF_DB_PRINTF("%p", if_addr);
4562 IF_DB_PRINTF("%p", if_broadcastaddr);
4563 IF_DB_PRINTF("%p", if_afdata);
4564 IF_DB_PRINTF("%d", if_afdata_initialized);
4565 IF_DB_PRINTF("%u", if_fib);
4566 IF_DB_PRINTF("%p", if_vnet);
4567 IF_DB_PRINTF("%p", if_home_vnet);
4568 IF_DB_PRINTF("%p", if_vlantrunk);
4569 IF_DB_PRINTF("%p", if_bpf);
4570 IF_DB_PRINTF("%u", if_pcount);
4571 IF_DB_PRINTF("%p", if_bridge);
4572 IF_DB_PRINTF("%p", if_lagg);
4573 IF_DB_PRINTF("%p", if_pf_kif);
4574 IF_DB_PRINTF("%p", if_carp);
4575 IF_DB_PRINTF("%p", if_label);
4576 IF_DB_PRINTF("%p", if_netmap);
4577 IF_DB_PRINTF("0x%08x", if_flags);
4578 IF_DB_PRINTF("0x%08x", if_drv_flags);
4579 IF_DB_PRINTF("0x%08x", if_capabilities);
4580 IF_DB_PRINTF("0x%08x", if_capenable);
4581 IF_DB_PRINTF("%p", if_snd.ifq_head);
4582 IF_DB_PRINTF("%p", if_snd.ifq_tail);
4583 IF_DB_PRINTF("%d", if_snd.ifq_len);
4584 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
4585 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
4586 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
4587 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
4588 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
4589 IF_DB_PRINTF("%d", if_snd.altq_type);
4590 IF_DB_PRINTF("%x", if_snd.altq_flags);
4594 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
4598 db_printf("usage: show ifnet <struct ifnet *>\n");
4602 if_show_ifnet((struct ifnet *)addr);
4605 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
4607 VNET_ITERATOR_DECL(vnet_iter);
4611 VNET_FOREACH(vnet_iter) {
4612 CURVNET_SET_QUIET(vnet_iter);
4614 db_printf("vnet=%p\n", curvnet);
4616 for (idx = 1; idx <= V_if_index; idx++) {
4617 ifp = V_ifindex_table[idx];
4620 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);