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 *, bool);
289 static int if_detach_internal(struct ifnet *, bool);
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. */
315 static int if_indexlim = 8;
316 static struct ifindex_entry {
317 struct ifnet *ife_ifnet;
321 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
322 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
323 "Variables global to all interfaces");
325 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
330 for (int i = 1; i <= if_index; i++)
331 if (ifindex_table[i].ife_ifnet != NULL &&
332 ifindex_table[i].ife_ifnet->if_vnet == curvnet)
336 return (sysctl_handle_int(oidp, &rv, 0, req));
338 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
339 CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
340 "Maximum known interface index");
343 * The global network interface list (V_ifnet) and related state (such as
344 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
345 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
347 struct sx ifnet_sxlock;
348 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
350 struct sx ifnet_detach_sxlock;
351 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
355 #define VNET_IS_SHUTTING_DOWN(_vnet) \
356 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
359 static if_com_alloc_t *if_com_alloc[256];
360 static if_com_free_t *if_com_free[256];
362 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
363 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
364 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
367 ifnet_byindex(u_int idx)
373 if (__predict_false(idx > if_index))
376 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
378 if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
385 ifnet_byindex_ref(u_int idx)
389 ifp = ifnet_byindex(idx);
390 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
392 if (!if_try_ref(ifp))
398 ifnet_byindexgen(uint16_t idx, uint16_t gen)
404 if (__predict_false(idx > if_index))
407 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
409 if (ifindex_table[idx].ife_gencnt == gen)
416 * Network interface utility routines.
418 * Routines with ifa_ifwith* names take sockaddr *'s as
423 if_init(void *arg __unused)
426 ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
427 M_IFNET, M_WAITOK | M_ZERO);
429 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
432 vnet_if_init(const void *unused __unused)
435 CK_STAILQ_INIT(&V_ifnet);
436 CK_STAILQ_INIT(&V_ifg_head);
437 vnet_if_clone_init();
439 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
443 if_link_ifnet(struct ifnet *ifp)
447 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
449 curvnet->vnet_ifcnt++;
455 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
461 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
463 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
465 ifp->if_flags |= IFF_DYING;
470 curvnet->vnet_ifcnt--;
479 vnet_if_return(const void *unused __unused)
481 struct ifnet *ifp, *nifp;
482 struct ifnet **pending;
483 int found __diagused;
489 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
490 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
491 * if_detach_internal(), which waits for NET_EPOCH callbacks to
492 * complete. We can't do that from within NET_EPOCH.
494 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
495 * read/write lock. We cannot hold the lock as we call if_vmove()
496 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
501 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
502 M_IFNET, M_WAITOK | M_ZERO);
504 /* Return all inherited interfaces to their parent vnets. */
505 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
506 if (ifp->if_home_vnet != ifp->if_vnet) {
507 found = if_unlink_ifnet(ifp, true);
515 for (int j = 0; j < i; j++) {
516 if_vmove(pending[j], pending[j]->if_home_vnet);
519 free(pending, M_IFNET);
521 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
522 vnet_if_return, NULL);
526 * Allocate a struct ifnet and an index for an interface. A layer 2
527 * common structure will also be allocated if an allocation routine is
528 * registered for the passed type.
530 static struct ifnet *
531 if_alloc_domain(u_char type, int numa_domain)
536 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
537 if (numa_domain == IF_NODOM)
538 ifp = malloc(sizeof(struct ifnet), M_IFNET,
541 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
542 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
544 ifp->if_alloctype = type;
545 ifp->if_numa_domain = numa_domain;
547 ifp->if_vnet = curvnet;
549 if (if_com_alloc[type] != NULL) {
550 ifp->if_l2com = if_com_alloc[type](type, ifp);
551 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
555 IF_ADDR_LOCK_INIT(ifp);
556 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
557 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
558 ifp->if_afdata_initialized = 0;
559 IF_AFDATA_LOCK_INIT(ifp);
560 CK_STAILQ_INIT(&ifp->if_addrhead);
561 CK_STAILQ_INIT(&ifp->if_multiaddrs);
562 CK_STAILQ_INIT(&ifp->if_groups);
566 ifq_init(&ifp->if_snd, ifp);
568 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
569 for (int i = 0; i < IFCOUNTERS; i++)
570 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
571 ifp->if_get_counter = if_get_counter_default;
572 ifp->if_pcp = IFNET_PCP_NONE;
574 /* Allocate an ifindex array entry. */
577 * Try to find an empty slot below if_index. If we fail, take the
580 for (idx = 1; idx <= if_index; idx++) {
581 if (ifindex_table[idx].ife_ifnet == NULL)
585 /* Catch if_index overflow. */
586 if (idx >= if_indexlim) {
587 struct ifindex_entry *new, *old;
590 newlim = if_indexlim * 2;
591 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
592 memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
594 ck_pr_store_ptr(&ifindex_table, new);
595 if_indexlim = newlim;
596 epoch_wait_preempt(net_epoch_preempt);
603 ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
604 ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
611 if_alloc_dev(u_char type, device_t dev)
615 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
616 return (if_alloc_domain(type, IF_NODOM));
617 return (if_alloc_domain(type, numa_domain));
621 if_alloc(u_char type)
624 return (if_alloc_domain(type, IF_NODOM));
627 * Do the actual work of freeing a struct ifnet, and layer 2 common
628 * structure. This call is made when the network epoch guarantees
629 * us that nobody holds a pointer to the interface.
632 if_free_deferred(epoch_context_t ctx)
634 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
636 KASSERT((ifp->if_flags & IFF_DYING),
637 ("%s: interface not dying", __func__));
639 if (if_com_free[ifp->if_alloctype] != NULL)
640 if_com_free[ifp->if_alloctype](ifp->if_l2com,
644 mac_ifnet_destroy(ifp);
646 IF_AFDATA_DESTROY(ifp);
647 IF_ADDR_LOCK_DESTROY(ifp);
648 ifq_delete(&ifp->if_snd);
650 for (int i = 0; i < IFCOUNTERS; i++)
651 counter_u64_free(ifp->if_counters[i]);
653 free(ifp->if_description, M_IFDESCR);
654 free(ifp->if_hw_addr, M_IFADDR);
659 * Deregister an interface and free the associated storage.
662 if_free(struct ifnet *ifp)
665 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
668 * XXXGL: An interface index is really an alias to ifp pointer.
669 * Why would we clear the alias now, and not in the deferred
670 * context? Indeed there is nothing wrong with some network
671 * thread obtaining ifp via ifnet_byindex() inside the network
672 * epoch and then dereferencing ifp while we peform if_free(),
673 * and after if_free() finished, too.
675 * This early index freeing was important back when ifindex was
676 * virtualized and interface would outlive the vnet.
679 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
680 ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
681 ifindex_table[ifp->if_index].ife_gencnt++;
682 while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
686 if (refcount_release(&ifp->if_refcount))
687 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
691 * Interfaces to keep an ifnet type-stable despite the possibility of the
692 * driver calling if_free(). If there are additional references, we defer
693 * freeing the underlying data structure.
696 if_ref(struct ifnet *ifp)
698 u_int old __diagused;
700 /* We don't assert the ifnet list lock here, but arguably should. */
701 old = refcount_acquire(&ifp->if_refcount);
702 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
706 if_try_ref(struct ifnet *ifp)
709 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
713 if_rele(struct ifnet *ifp)
716 if (!refcount_release(&ifp->if_refcount))
718 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
722 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
725 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
727 if (ifq->ifq_maxlen == 0)
728 ifq->ifq_maxlen = ifqmaxlen;
731 ifq->altq_disc = NULL;
732 ifq->altq_flags &= ALTQF_CANTCHANGE;
733 ifq->altq_tbr = NULL;
738 ifq_delete(struct ifaltq *ifq)
740 mtx_destroy(&ifq->ifq_mtx);
744 * Perform generic interface initialization tasks and attach the interface
745 * to the list of "active" interfaces. If vmove flag is set on entry
746 * to if_attach_internal(), perform only a limited subset of initialization
747 * tasks, given that we are moving from one vnet to another an ifnet which
748 * has already been fully initialized.
750 * Note that if_detach_internal() removes group membership unconditionally
751 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
752 * Thus, when if_vmove() is applied to a cloned interface, group membership
753 * is lost while a cloned one always joins a group whose name is
754 * ifc->ifc_name. To recover this after if_detach_internal() and
755 * if_attach_internal(), the cloner should be specified to
756 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
757 * attempts to join a group whose name is ifc->ifc_name.
760 * - The decision to return void and thus require this function to
761 * succeed is questionable.
762 * - We should probably do more sanity checking. For instance we don't
763 * do anything to insure if_xname is unique or non-empty.
766 if_attach(struct ifnet *ifp)
769 if_attach_internal(ifp, false);
773 * Compute the least common TSO limit.
776 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
779 * 1) If there is no limit currently, take the limit from
780 * the network adapter.
782 * 2) If the network adapter has a limit below the current
785 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
786 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
787 pmax->tsomaxbytes = ifp->if_hw_tsomax;
789 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
790 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
791 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
793 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
794 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
795 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
800 * Update TSO limit of a network adapter.
802 * Returns zero if no change. Else non-zero.
805 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
808 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
809 ifp->if_hw_tsomax = pmax->tsomaxbytes;
812 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
813 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
816 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
817 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
824 if_attach_internal(struct ifnet *ifp, bool vmove)
826 unsigned socksize, ifasize;
827 int namelen, masklen;
828 struct sockaddr_dl *sdl;
831 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
834 ifp->if_vnet = curvnet;
835 if (ifp->if_home_vnet == NULL)
836 ifp->if_home_vnet = curvnet;
839 if_addgroup(ifp, IFG_ALL);
842 /* Restore group membership for cloned interface. */
844 if_clone_restoregroup(ifp);
847 getmicrotime(&ifp->if_lastchange);
848 ifp->if_epoch = time_uptime;
850 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
851 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
852 ("transmit and qflush must both either be set or both be NULL"));
853 if (ifp->if_transmit == NULL) {
854 ifp->if_transmit = if_transmit;
855 ifp->if_qflush = if_qflush;
857 if (ifp->if_input == NULL)
858 ifp->if_input = if_input_default;
860 if (ifp->if_requestencap == NULL)
861 ifp->if_requestencap = if_requestencap_default;
865 mac_ifnet_create(ifp);
869 * Create a Link Level name for this device.
871 namelen = strlen(ifp->if_xname);
873 * Always save enough space for any possiable name so we
874 * can do a rename in place later.
876 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
877 socksize = masklen + ifp->if_addrlen;
878 if (socksize < sizeof(*sdl))
879 socksize = sizeof(*sdl);
880 socksize = roundup2(socksize, sizeof(long));
881 ifasize = sizeof(*ifa) + 2 * socksize;
882 ifa = ifa_alloc(ifasize, M_WAITOK);
883 sdl = (struct sockaddr_dl *)(ifa + 1);
884 sdl->sdl_len = socksize;
885 sdl->sdl_family = AF_LINK;
886 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
887 sdl->sdl_nlen = namelen;
888 sdl->sdl_index = ifp->if_index;
889 sdl->sdl_type = ifp->if_type;
892 ifa->ifa_addr = (struct sockaddr *)sdl;
893 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
894 ifa->ifa_netmask = (struct sockaddr *)sdl;
895 sdl->sdl_len = masklen;
897 sdl->sdl_data[--namelen] = 0xff;
898 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
899 /* Reliably crash if used uninitialized. */
900 ifp->if_broadcastaddr = NULL;
902 if (ifp->if_type == IFT_ETHER) {
903 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
907 #if defined(INET) || defined(INET6)
908 /* Use defaults for TSO, if nothing is set */
909 if (ifp->if_hw_tsomax == 0 &&
910 ifp->if_hw_tsomaxsegcount == 0 &&
911 ifp->if_hw_tsomaxsegsize == 0) {
913 * The TSO defaults needs to be such that an
914 * NFS mbuf list of 35 mbufs totalling just
915 * below 64K works and that a chain of mbufs
916 * can be defragged into at most 32 segments:
918 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
919 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
920 ifp->if_hw_tsomaxsegcount = 35;
921 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
923 /* XXX some drivers set IFCAP_TSO after ethernet attach */
924 if (ifp->if_capabilities & IFCAP_TSO) {
925 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
927 ifp->if_hw_tsomaxsegcount,
928 ifp->if_hw_tsomaxsegsize);
936 * Update the interface index in the link layer address
939 for (ifa = ifp->if_addr; ifa != NULL;
940 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
941 if (ifa->ifa_addr->sa_family == AF_LINK) {
942 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
943 sdl->sdl_index = ifp->if_index;
951 if (domain_init_status >= 2)
952 if_attachdomain1(ifp);
954 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
955 if (IS_DEFAULT_VNET(curvnet))
956 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
958 /* Announce the interface. */
959 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
963 if_epochalloc(void *dummy __unused)
966 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
968 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
971 if_attachdomain(void *dummy)
975 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
976 if_attachdomain1(ifp);
978 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
979 if_attachdomain, NULL);
982 if_attachdomain1(struct ifnet *ifp)
987 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
988 * cannot lock ifp->if_afdata initialization, entirely.
991 if (ifp->if_afdata_initialized >= domain_init_status) {
992 IF_AFDATA_UNLOCK(ifp);
993 log(LOG_WARNING, "%s called more than once on %s\n",
994 __func__, ifp->if_xname);
997 ifp->if_afdata_initialized = domain_init_status;
998 IF_AFDATA_UNLOCK(ifp);
1000 /* address family dependent data region */
1001 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1002 for (dp = domains; dp; dp = dp->dom_next) {
1003 if (dp->dom_ifattach)
1004 ifp->if_afdata[dp->dom_family] =
1005 (*dp->dom_ifattach)(ifp);
1010 * Remove any unicast or broadcast network addresses from an interface.
1013 if_purgeaddrs(struct ifnet *ifp)
1018 struct epoch_tracker et;
1020 NET_EPOCH_ENTER(et);
1021 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1022 if (ifa->ifa_addr->sa_family != AF_LINK)
1030 /* XXX: Ugly!! ad hoc just for INET */
1031 if (ifa->ifa_addr->sa_family == AF_INET) {
1032 struct ifaliasreq ifr;
1034 bzero(&ifr, sizeof(ifr));
1035 ifr.ifra_addr = *ifa->ifa_addr;
1036 if (ifa->ifa_dstaddr)
1037 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1038 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1044 if (ifa->ifa_addr->sa_family == AF_INET6) {
1045 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1046 /* ifp_addrhead is already updated */
1051 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1052 IF_ADDR_WUNLOCK(ifp);
1058 * Remove any multicast network addresses from an interface when an ifnet
1062 if_purgemaddrs(struct ifnet *ifp)
1064 struct ifmultiaddr *ifma;
1067 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1068 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1069 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1070 if_delmulti_locked(ifp, ifma, 1);
1072 IF_ADDR_WUNLOCK(ifp);
1076 * Detach an interface, removing it from the list of "active" interfaces.
1077 * If vmove flag is set on entry to if_detach_internal(), perform only a
1078 * limited subset of cleanup tasks, given that we are moving an ifnet from
1079 * one vnet to another, where it must be fully operational.
1081 * XXXRW: There are some significant questions about event ordering, and
1082 * how to prevent things from starting to use the interface during detach.
1085 if_detach(struct ifnet *ifp)
1089 CURVNET_SET_QUIET(ifp->if_vnet);
1090 found = if_unlink_ifnet(ifp, false);
1092 sx_xlock(&ifnet_detach_sxlock);
1093 if_detach_internal(ifp, false);
1094 sx_xunlock(&ifnet_detach_sxlock);
1100 * The vmove flag, if set, indicates that we are called from a callpath
1101 * that is moving an interface to a different vnet instance.
1103 * The shutdown flag, if set, indicates that we are called in the
1104 * process of shutting down a vnet instance. Currently only the
1105 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1106 * on a vnet instance shutdown without this flag being set, e.g., when
1107 * the cloned interfaces are destoyed as first thing of teardown.
1110 if_detach_internal(struct ifnet *ifp, bool vmove)
1118 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1122 * At this point we know the interface still was on the ifnet list
1123 * and we removed it so we are in a stable state.
1125 epoch_wait_preempt(net_epoch_preempt);
1128 * Ensure all pending EPOCH(9) callbacks have been executed. This
1129 * fixes issues about late destruction of multicast options
1130 * which lead to leave group calls, which in turn access the
1131 * belonging ifnet structure:
1133 epoch_drain_callbacks(net_epoch_preempt);
1136 * In any case (destroy or vmove) detach us from the groups
1137 * and remove/wait for pending events on the taskq.
1138 * XXX-BZ in theory an interface could still enqueue a taskq change?
1142 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1143 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1149 * On VNET shutdown abort here as the stack teardown will do all
1150 * the work top-down for us.
1153 /* Give interface users the chance to clean up. */
1154 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1157 * In case of a vmove we are done here without error.
1158 * If we would signal an error it would lead to the same
1159 * abort as if we did not find the ifnet anymore.
1160 * if_detach() calls us in void context and does not care
1161 * about an early abort notification, so life is splendid :)
1163 goto finish_vnet_shutdown;
1168 * At this point we are not tearing down a VNET and are either
1169 * going to destroy or vmove the interface and have to cleanup
1174 * Remove routes and flush queues.
1177 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1178 altq_disable(&ifp->if_snd);
1179 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1180 altq_detach(&ifp->if_snd);
1191 * Remove all IPv6 kernel structs related to ifp. This should be done
1192 * before removing routing entries below, since IPv6 interface direct
1193 * routes are expected to be removed by the IPv6-specific kernel API.
1194 * Otherwise, the kernel will detect some inconsistency and bark it.
1198 if_purgemaddrs(ifp);
1200 /* Announce that the interface is gone. */
1201 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1202 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1203 if (IS_DEFAULT_VNET(curvnet))
1204 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1208 * Prevent further calls into the device driver via ifnet.
1213 * Clean up all addresses.
1216 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1217 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1218 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1219 IF_ADDR_WUNLOCK(ifp);
1222 IF_ADDR_WUNLOCK(ifp);
1225 rt_flushifroutes(ifp);
1228 finish_vnet_shutdown:
1231 * We cannot hold the lock over dom_ifdetach calls as they might
1232 * sleep, for example trying to drain a callout, thus open up the
1233 * theoretical race with re-attaching.
1235 IF_AFDATA_LOCK(ifp);
1236 i = ifp->if_afdata_initialized;
1237 ifp->if_afdata_initialized = 0;
1238 IF_AFDATA_UNLOCK(ifp);
1239 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1240 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1241 (*dp->dom_ifdetach)(ifp,
1242 ifp->if_afdata[dp->dom_family]);
1243 ifp->if_afdata[dp->dom_family] = NULL;
1252 * if_vmove() performs a limited version of if_detach() in current
1253 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1256 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1259 u_int bif_dlt, bif_hdrlen;
1265 * if_detach_internal() will call the eventhandler to notify
1266 * interface departure. That will detach if_bpf. We need to
1267 * safe the dlt and hdrlen so we can re-attach it later.
1269 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1273 * Detach from current vnet, but preserve LLADDR info, do not
1274 * mark as dead etc. so that the ifnet can be reattached later.
1275 * If we cannot find it, we lost the race to someone else.
1277 rc = if_detach_internal(ifp, true);
1282 * Perform interface-specific reassignment tasks, if provided by
1285 if (ifp->if_reassign != NULL)
1286 ifp->if_reassign(ifp, new_vnet, NULL);
1289 * Switch to the context of the target vnet.
1291 CURVNET_SET_QUIET(new_vnet);
1292 if_attach_internal(ifp, true);
1295 if (ifp->if_bpf == NULL)
1296 bpfattach(ifp, bif_dlt, bif_hdrlen);
1304 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1307 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1312 bool found __diagused;
1315 /* Try to find the prison within our visibility. */
1316 sx_slock(&allprison_lock);
1317 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1318 sx_sunlock(&allprison_lock);
1321 prison_hold_locked(pr);
1322 mtx_unlock(&pr->pr_mtx);
1324 /* Do not try to move the iface from and to the same prison. */
1325 if (pr->pr_vnet == ifp->if_vnet) {
1330 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1331 /* XXX Lock interfaces to avoid races. */
1332 CURVNET_SET_QUIET(pr->pr_vnet);
1333 difp = ifunit(ifname);
1340 /* Make sure the VNET is stable. */
1341 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1349 found = if_unlink_ifnet(ifp, true);
1352 /* Move the interface into the child jail/vnet. */
1353 error = if_vmove(ifp, pr->pr_vnet);
1355 /* Report the new if_xname back to the userland on success. */
1357 sprintf(ifname, "%s", ifp->if_xname);
1364 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1367 struct vnet *vnet_dst;
1369 int error, found __diagused;
1372 /* Try to find the prison within our visibility. */
1373 sx_slock(&allprison_lock);
1374 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1375 sx_sunlock(&allprison_lock);
1378 prison_hold_locked(pr);
1379 mtx_unlock(&pr->pr_mtx);
1381 /* Make sure the named iface exists in the source prison/vnet. */
1382 CURVNET_SET(pr->pr_vnet);
1383 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1390 /* Do not try to move the iface from and to the same prison. */
1391 vnet_dst = TD_TO_VNET(td);
1392 if (vnet_dst == ifp->if_vnet) {
1398 /* Make sure the VNET is stable. */
1399 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1406 /* Get interface back from child jail/vnet. */
1407 found = if_unlink_ifnet(ifp, true);
1409 error = if_vmove(ifp, vnet_dst);
1412 /* Report the new if_xname back to the userland on success. */
1414 sprintf(ifname, "%s", ifp->if_xname);
1422 * Add a group to an interface
1425 if_addgroup(struct ifnet *ifp, const char *groupname)
1427 struct ifg_list *ifgl;
1428 struct ifg_group *ifg = NULL;
1429 struct ifg_member *ifgm;
1432 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1433 groupname[strlen(groupname) - 1] <= '9')
1437 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1438 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1443 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1448 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1454 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1455 if (!strcmp(ifg->ifg_group, groupname))
1459 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1465 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1466 ifg->ifg_refcnt = 0;
1467 CK_STAILQ_INIT(&ifg->ifg_members);
1468 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1473 ifgl->ifgl_group = ifg;
1474 ifgm->ifgm_ifp = ifp;
1477 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1478 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1479 IF_ADDR_WUNLOCK(ifp);
1484 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1485 EVENTHANDLER_INVOKE(group_change_event, groupname);
1491 * Helper function to remove a group out of an interface. Expects the global
1492 * ifnet lock to be write-locked, and drops it before returning.
1495 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1496 const char *groupname)
1498 struct ifg_member *ifgm;
1501 IFNET_WLOCK_ASSERT();
1504 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1505 IF_ADDR_WUNLOCK(ifp);
1507 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1508 if (ifgm->ifgm_ifp == ifp) {
1509 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1510 ifg_member, ifgm_next);
1515 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1516 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1524 epoch_wait_preempt(net_epoch_preempt);
1526 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1527 free(ifgl->ifgl_group, M_TEMP);
1532 EVENTHANDLER_INVOKE(group_change_event, groupname);
1536 * Remove a group from an interface
1539 if_delgroup(struct ifnet *ifp, const char *groupname)
1541 struct ifg_list *ifgl;
1544 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1545 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1552 _if_delgroup_locked(ifp, ifgl, groupname);
1558 * Remove an interface from all groups
1561 if_delgroups(struct ifnet *ifp)
1563 struct ifg_list *ifgl;
1564 char groupname[IFNAMSIZ];
1567 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1568 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1569 _if_delgroup_locked(ifp, ifgl, groupname);
1576 * Stores all groups from an interface in memory pointed to by ifgr.
1579 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1582 struct ifg_list *ifgl;
1583 struct ifg_req ifgrq, *ifgp;
1587 if (ifgr->ifgr_len == 0) {
1588 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1589 ifgr->ifgr_len += sizeof(struct ifg_req);
1593 len = ifgr->ifgr_len;
1594 ifgp = ifgr->ifgr_groups;
1596 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1597 if (len < sizeof(ifgrq))
1599 bzero(&ifgrq, sizeof ifgrq);
1600 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1601 sizeof(ifgrq.ifgrq_group));
1602 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1604 len -= sizeof(ifgrq);
1612 * Stores all members of a group in memory pointed to by igfr
1615 if_getgroupmembers(struct ifgroupreq *ifgr)
1617 struct ifg_group *ifg;
1618 struct ifg_member *ifgm;
1619 struct ifg_req ifgrq, *ifgp;
1623 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1624 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1631 if (ifgr->ifgr_len == 0) {
1632 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1633 ifgr->ifgr_len += sizeof(ifgrq);
1638 len = ifgr->ifgr_len;
1639 ifgp = ifgr->ifgr_groups;
1640 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1641 if (len < sizeof(ifgrq)) {
1645 bzero(&ifgrq, sizeof ifgrq);
1646 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1647 sizeof(ifgrq.ifgrq_member));
1648 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1652 len -= sizeof(ifgrq);
1661 * Return counter values from counter(9)s stored in ifnet.
1664 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1667 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1669 return (counter_u64_fetch(ifp->if_counters[cnt]));
1673 * Increase an ifnet counter. Usually used for counters shared
1674 * between the stack and a driver, but function supports them all.
1677 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1680 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1682 counter_u64_add(ifp->if_counters[cnt], inc);
1686 * Copy data from ifnet to userland API structure if_data.
1689 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1692 ifd->ifi_type = ifp->if_type;
1693 ifd->ifi_physical = 0;
1694 ifd->ifi_addrlen = ifp->if_addrlen;
1695 ifd->ifi_hdrlen = ifp->if_hdrlen;
1696 ifd->ifi_link_state = ifp->if_link_state;
1698 ifd->ifi_datalen = sizeof(struct if_data);
1699 ifd->ifi_mtu = ifp->if_mtu;
1700 ifd->ifi_metric = ifp->if_metric;
1701 ifd->ifi_baudrate = ifp->if_baudrate;
1702 ifd->ifi_hwassist = ifp->if_hwassist;
1703 ifd->ifi_epoch = ifp->if_epoch;
1704 ifd->ifi_lastchange = ifp->if_lastchange;
1706 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1707 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1708 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1709 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1710 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1711 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1712 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1713 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1714 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1715 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1716 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1717 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1721 * Initialization, destruction and refcounting functions for ifaddrs.
1724 ifa_alloc(size_t size, int flags)
1728 KASSERT(size >= sizeof(struct ifaddr),
1729 ("%s: invalid size %zu", __func__, size));
1731 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1735 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1737 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1739 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1741 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1744 refcount_init(&ifa->ifa_refcnt, 1);
1749 /* free(NULL) is okay */
1750 counter_u64_free(ifa->ifa_opackets);
1751 counter_u64_free(ifa->ifa_ipackets);
1752 counter_u64_free(ifa->ifa_obytes);
1753 counter_u64_free(ifa->ifa_ibytes);
1754 free(ifa, M_IFADDR);
1760 ifa_ref(struct ifaddr *ifa)
1762 u_int old __diagused;
1764 old = refcount_acquire(&ifa->ifa_refcnt);
1765 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1769 ifa_try_ref(struct ifaddr *ifa)
1773 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1777 ifa_destroy(epoch_context_t ctx)
1781 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1782 counter_u64_free(ifa->ifa_opackets);
1783 counter_u64_free(ifa->ifa_ipackets);
1784 counter_u64_free(ifa->ifa_obytes);
1785 counter_u64_free(ifa->ifa_ibytes);
1786 free(ifa, M_IFADDR);
1790 ifa_free(struct ifaddr *ifa)
1793 if (refcount_release(&ifa->ifa_refcnt))
1794 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1798 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1799 * structs used to represent other address families, it is necessary
1800 * to perform a different comparison.
1803 #define sa_dl_equal(a1, a2) \
1804 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1805 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1806 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1807 CLLADDR((const struct sockaddr_dl *)(a2)), \
1808 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1811 * Locate an interface based on a complete address.
1815 ifa_ifwithaddr(const struct sockaddr *addr)
1822 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1823 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1824 if (ifa->ifa_addr->sa_family != addr->sa_family)
1826 if (sa_equal(addr, ifa->ifa_addr)) {
1829 /* IP6 doesn't have broadcast */
1830 if ((ifp->if_flags & IFF_BROADCAST) &&
1831 ifa->ifa_broadaddr &&
1832 ifa->ifa_broadaddr->sa_len != 0 &&
1833 sa_equal(ifa->ifa_broadaddr, addr)) {
1844 ifa_ifwithaddr_check(const struct sockaddr *addr)
1846 struct epoch_tracker et;
1849 NET_EPOCH_ENTER(et);
1850 rc = (ifa_ifwithaddr(addr) != NULL);
1856 * Locate an interface based on the broadcast address.
1860 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1866 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1867 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1869 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1870 if (ifa->ifa_addr->sa_family != addr->sa_family)
1872 if ((ifp->if_flags & IFF_BROADCAST) &&
1873 ifa->ifa_broadaddr &&
1874 ifa->ifa_broadaddr->sa_len != 0 &&
1875 sa_equal(ifa->ifa_broadaddr, addr)) {
1886 * Locate the point to point interface with a given destination address.
1890 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1896 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1897 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1899 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1901 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1902 if (ifa->ifa_addr->sa_family != addr->sa_family)
1904 if (ifa->ifa_dstaddr != NULL &&
1905 sa_equal(addr, ifa->ifa_dstaddr)) {
1916 * Find an interface on a specific network. If many, choice
1917 * is most specific found.
1920 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1924 struct ifaddr *ifa_maybe = NULL;
1925 u_int af = addr->sa_family;
1926 const char *addr_data = addr->sa_data, *cplim;
1930 * AF_LINK addresses can be looked up directly by their index number,
1931 * so do that if we can.
1933 if (af == AF_LINK) {
1934 ifp = ifnet_byindex(
1935 ((const struct sockaddr_dl *)addr)->sdl_index);
1936 return (ifp ? ifp->if_addr : NULL);
1940 * Scan though each interface, looking for ones that have addresses
1941 * in this address family and the requested fib.
1943 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1944 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1946 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1947 const char *cp, *cp2, *cp3;
1949 if (ifa->ifa_addr->sa_family != af)
1951 if (af == AF_INET &&
1952 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1954 * This is a bit broken as it doesn't
1955 * take into account that the remote end may
1956 * be a single node in the network we are
1958 * The trouble is that we don't know the
1959 * netmask for the remote end.
1961 if (ifa->ifa_dstaddr != NULL &&
1962 sa_equal(addr, ifa->ifa_dstaddr)) {
1967 * Scan all the bits in the ifa's address.
1968 * If a bit dissagrees with what we are
1969 * looking for, mask it with the netmask
1970 * to see if it really matters.
1971 * (A byte at a time)
1973 if (ifa->ifa_netmask == 0)
1976 cp2 = ifa->ifa_addr->sa_data;
1977 cp3 = ifa->ifa_netmask->sa_data;
1978 cplim = ifa->ifa_netmask->sa_len
1979 + (char *)ifa->ifa_netmask;
1981 if ((*cp++ ^ *cp2++) & *cp3++)
1982 goto next; /* next address! */
1984 * If the netmask of what we just found
1985 * is more specific than what we had before
1986 * (if we had one), or if the virtual status
1987 * of new prefix is better than of the old one,
1988 * then remember the new one before continuing
1989 * to search for an even better one.
1991 if (ifa_maybe == NULL ||
1992 ifa_preferred(ifa_maybe, ifa) ||
1993 rn_refines((caddr_t)ifa->ifa_netmask,
1994 (caddr_t)ifa_maybe->ifa_netmask)) {
2007 * Find an interface address specific to an interface best matching
2011 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2014 const char *cp, *cp2, *cp3;
2016 struct ifaddr *ifa_maybe = NULL;
2017 u_int af = addr->sa_family;
2023 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2024 if (ifa->ifa_addr->sa_family != af)
2026 if (ifa_maybe == NULL)
2028 if (ifa->ifa_netmask == 0) {
2029 if (sa_equal(addr, ifa->ifa_addr) ||
2030 (ifa->ifa_dstaddr &&
2031 sa_equal(addr, ifa->ifa_dstaddr)))
2035 if (ifp->if_flags & IFF_POINTOPOINT) {
2036 if (sa_equal(addr, ifa->ifa_dstaddr))
2040 cp2 = ifa->ifa_addr->sa_data;
2041 cp3 = ifa->ifa_netmask->sa_data;
2042 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2043 for (; cp3 < cplim; cp3++)
2044 if ((*cp++ ^ *cp2++) & *cp3)
2056 * See whether new ifa is better than current one:
2057 * 1) A non-virtual one is preferred over virtual.
2058 * 2) A virtual in master state preferred over any other state.
2060 * Used in several address selecting functions.
2063 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2066 return (cur->ifa_carp && (!next->ifa_carp ||
2067 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2070 struct sockaddr_dl *
2071 link_alloc_sdl(size_t size, int flags)
2074 return (malloc(size, M_TEMP, flags));
2078 link_free_sdl(struct sockaddr *sa)
2084 * Fills in given sdl with interface basic info.
2085 * Returns pointer to filled sdl.
2087 struct sockaddr_dl *
2088 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2090 struct sockaddr_dl *sdl;
2092 sdl = (struct sockaddr_dl *)paddr;
2093 memset(sdl, 0, sizeof(struct sockaddr_dl));
2094 sdl->sdl_len = sizeof(struct sockaddr_dl);
2095 sdl->sdl_family = AF_LINK;
2096 sdl->sdl_index = ifp->if_index;
2097 sdl->sdl_type = iftype;
2103 * Mark an interface down and notify protocols of
2107 if_unroute(struct ifnet *ifp, int flag, int fam)
2110 struct epoch_tracker et;
2112 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2114 ifp->if_flags &= ~flag;
2115 getmicrotime(&ifp->if_lastchange);
2116 NET_EPOCH_ENTER(et);
2117 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2118 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2119 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2121 ifp->if_qflush(ifp);
2124 (*carp_linkstate_p)(ifp);
2129 * Mark an interface up and notify protocols of
2133 if_route(struct ifnet *ifp, int flag, int fam)
2136 struct epoch_tracker et;
2138 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2140 ifp->if_flags |= flag;
2141 getmicrotime(&ifp->if_lastchange);
2142 NET_EPOCH_ENTER(et);
2143 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2144 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2145 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2148 (*carp_linkstate_p)(ifp);
2155 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2156 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2157 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2158 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2159 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2160 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2161 int (*vlan_setcookie_p)(struct ifnet *, void *);
2162 void *(*vlan_cookie_p)(struct ifnet *);
2165 * Handle a change in the interface link state. To avoid LORs
2166 * between driver lock and upper layer locks, as well as possible
2167 * recursions, we post event to taskqueue, and all job
2168 * is done in static do_link_state_change().
2171 if_link_state_change(struct ifnet *ifp, int link_state)
2173 /* Return if state hasn't changed. */
2174 if (ifp->if_link_state == link_state)
2177 ifp->if_link_state = link_state;
2179 /* XXXGL: reference ifp? */
2180 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2184 do_link_state_change(void *arg, int pending)
2190 link_state = ifp->if_link_state;
2192 CURVNET_SET(ifp->if_vnet);
2194 if (ifp->if_vlantrunk != NULL)
2195 (*vlan_link_state_p)(ifp);
2197 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2198 ifp->if_l2com != NULL)
2199 (*ng_ether_link_state_p)(ifp, link_state);
2201 (*carp_linkstate_p)(ifp);
2203 ifp->if_bridge_linkstate(ifp);
2205 (*lagg_linkstate_p)(ifp, link_state);
2207 if (IS_DEFAULT_VNET(curvnet))
2208 devctl_notify("IFNET", ifp->if_xname,
2209 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2212 if_printf(ifp, "%d link states coalesced\n", pending);
2213 if (log_link_state_change)
2214 if_printf(ifp, "link state changed to %s\n",
2215 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2216 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2221 * Mark an interface down and notify protocols of
2225 if_down(struct ifnet *ifp)
2228 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2229 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2233 * Mark an interface up and notify protocols of
2237 if_up(struct ifnet *ifp)
2240 if_route(ifp, IFF_UP, AF_UNSPEC);
2241 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2245 * Flush an interface queue.
2248 if_qflush(struct ifnet *ifp)
2256 if (ALTQ_IS_ENABLED(ifq))
2260 while ((m = n) != NULL) {
2271 * Map interface name to interface structure pointer, with or without
2272 * returning a reference.
2275 ifunit_ref(const char *name)
2277 struct epoch_tracker et;
2280 NET_EPOCH_ENTER(et);
2281 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2282 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2283 !(ifp->if_flags & IFF_DYING))
2293 ifunit(const char *name)
2295 struct epoch_tracker et;
2298 NET_EPOCH_ENTER(et);
2299 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2300 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2308 ifr_buffer_get_buffer(void *data)
2310 union ifreq_union *ifrup;
2313 #ifdef COMPAT_FREEBSD32
2314 if (SV_CURPROC_FLAG(SV_ILP32))
2315 return ((void *)(uintptr_t)
2316 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2318 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2322 ifr_buffer_set_buffer_null(void *data)
2324 union ifreq_union *ifrup;
2327 #ifdef COMPAT_FREEBSD32
2328 if (SV_CURPROC_FLAG(SV_ILP32))
2329 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2332 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2336 ifr_buffer_get_length(void *data)
2338 union ifreq_union *ifrup;
2341 #ifdef COMPAT_FREEBSD32
2342 if (SV_CURPROC_FLAG(SV_ILP32))
2343 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2345 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2349 ifr_buffer_set_length(void *data, size_t len)
2351 union ifreq_union *ifrup;
2354 #ifdef COMPAT_FREEBSD32
2355 if (SV_CURPROC_FLAG(SV_ILP32))
2356 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2359 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2363 ifr_data_get_ptr(void *ifrp)
2365 union ifreq_union *ifrup;
2368 #ifdef COMPAT_FREEBSD32
2369 if (SV_CURPROC_FLAG(SV_ILP32))
2370 return ((void *)(uintptr_t)
2371 ifrup->ifr32.ifr_ifru.ifru_data);
2373 return (ifrup->ifr.ifr_ifru.ifru_data);
2377 * Hardware specific interface ioctls.
2380 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2383 int error = 0, do_ifup = 0;
2384 int new_flags, temp_flags;
2385 size_t namelen, onamelen;
2387 char *descrbuf, *odescrbuf;
2388 char new_name[IFNAMSIZ];
2389 char old_name[IFNAMSIZ], strbuf[IFNAMSIZ + 8];
2391 struct sockaddr_dl *sdl;
2393 ifr = (struct ifreq *)data;
2396 ifr->ifr_index = ifp->if_index;
2400 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2401 ifr->ifr_flags = temp_flags & 0xffff;
2402 ifr->ifr_flagshigh = temp_flags >> 16;
2406 ifr->ifr_reqcap = ifp->if_capabilities;
2407 ifr->ifr_curcap = ifp->if_capenable;
2414 /* Ensure uninitialised padding is not leaked. */
2415 memset(&ifd, 0, sizeof(ifd));
2417 if_data_copy(ifp, &ifd);
2418 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2424 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2429 ifr->ifr_metric = ifp->if_metric;
2433 ifr->ifr_mtu = ifp->if_mtu;
2437 /* XXXGL: did this ever worked? */
2443 sx_slock(&ifdescr_sx);
2444 if (ifp->if_description == NULL)
2447 /* space for terminating nul */
2448 descrlen = strlen(ifp->if_description) + 1;
2449 if (ifr_buffer_get_length(ifr) < descrlen)
2450 ifr_buffer_set_buffer_null(ifr);
2452 error = copyout(ifp->if_description,
2453 ifr_buffer_get_buffer(ifr), descrlen);
2454 ifr_buffer_set_length(ifr, descrlen);
2456 sx_sunlock(&ifdescr_sx);
2460 error = priv_check(td, PRIV_NET_SETIFDESCR);
2465 * Copy only (length-1) bytes to make sure that
2466 * if_description is always nul terminated. The
2467 * length parameter is supposed to count the
2468 * terminating nul in.
2470 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2471 return (ENAMETOOLONG);
2472 else if (ifr_buffer_get_length(ifr) == 0)
2475 descrbuf = malloc(ifr_buffer_get_length(ifr),
2476 M_IFDESCR, M_WAITOK | M_ZERO);
2477 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2478 ifr_buffer_get_length(ifr) - 1);
2480 free(descrbuf, M_IFDESCR);
2485 sx_xlock(&ifdescr_sx);
2486 odescrbuf = ifp->if_description;
2487 ifp->if_description = descrbuf;
2488 sx_xunlock(&ifdescr_sx);
2490 getmicrotime(&ifp->if_lastchange);
2491 free(odescrbuf, M_IFDESCR);
2495 ifr->ifr_fib = ifp->if_fib;
2499 error = priv_check(td, PRIV_NET_SETIFFIB);
2502 if (ifr->ifr_fib >= rt_numfibs)
2505 ifp->if_fib = ifr->ifr_fib;
2509 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2513 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2514 * check, so we don't need special handling here yet.
2516 new_flags = (ifr->ifr_flags & 0xffff) |
2517 (ifr->ifr_flagshigh << 16);
2518 if (ifp->if_flags & IFF_UP &&
2519 (new_flags & IFF_UP) == 0) {
2521 } else if (new_flags & IFF_UP &&
2522 (ifp->if_flags & IFF_UP) == 0) {
2525 /* See if permanently promiscuous mode bit is about to flip */
2526 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2527 if (new_flags & IFF_PPROMISC)
2528 ifp->if_flags |= IFF_PROMISC;
2529 else if (ifp->if_pcount == 0)
2530 ifp->if_flags &= ~IFF_PROMISC;
2531 if (log_promisc_mode_change)
2532 if_printf(ifp, "permanently promiscuous mode %s\n",
2533 ((new_flags & IFF_PPROMISC) ?
2534 "enabled" : "disabled"));
2536 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2537 (new_flags &~ IFF_CANTCHANGE);
2538 if (ifp->if_ioctl) {
2539 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2543 getmicrotime(&ifp->if_lastchange);
2547 error = priv_check(td, PRIV_NET_SETIFCAP);
2550 if (ifp->if_ioctl == NULL)
2551 return (EOPNOTSUPP);
2552 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2554 error = (*ifp->if_ioctl)(ifp, cmd, data);
2556 getmicrotime(&ifp->if_lastchange);
2561 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2566 error = priv_check(td, PRIV_NET_SETIFNAME);
2569 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2573 if (new_name[0] == '\0')
2575 if (strcmp(new_name, ifp->if_xname) == 0)
2577 if (ifunit(new_name) != NULL)
2581 * XXX: Locking. Nothing else seems to lock if_flags,
2582 * and there are numerous other races with the
2583 * ifunit() checks not being atomic with namespace
2584 * changes (renames, vmoves, if_attach, etc).
2586 ifp->if_flags |= IFF_RENAMING;
2588 /* Announce the departure of the interface. */
2589 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2590 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2592 if_printf(ifp, "changing name to '%s'\n", new_name);
2595 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
2596 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2598 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2599 namelen = strlen(new_name);
2600 onamelen = sdl->sdl_nlen;
2602 * Move the address if needed. This is safe because we
2603 * allocate space for a name of length IFNAMSIZ when we
2604 * create this in if_attach().
2606 if (namelen != onamelen) {
2607 bcopy(sdl->sdl_data + onamelen,
2608 sdl->sdl_data + namelen, sdl->sdl_alen);
2610 bcopy(new_name, sdl->sdl_data, namelen);
2611 sdl->sdl_nlen = namelen;
2612 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2613 bzero(sdl->sdl_data, onamelen);
2614 while (namelen != 0)
2615 sdl->sdl_data[--namelen] = 0xff;
2616 IF_ADDR_WUNLOCK(ifp);
2618 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2619 /* Announce the return of the interface. */
2620 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2622 ifp->if_flags &= ~IFF_RENAMING;
2624 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
2625 devctl_notify("IFNET", old_name, "RENAME", strbuf);
2630 error = priv_check(td, PRIV_NET_SETIFVNET);
2633 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2638 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2641 ifp->if_metric = ifr->ifr_metric;
2642 getmicrotime(&ifp->if_lastchange);
2646 error = priv_check(td, PRIV_NET_SETIFPHYS);
2649 if (ifp->if_ioctl == NULL)
2650 return (EOPNOTSUPP);
2651 error = (*ifp->if_ioctl)(ifp, cmd, data);
2653 getmicrotime(&ifp->if_lastchange);
2658 u_long oldmtu = ifp->if_mtu;
2660 error = priv_check(td, PRIV_NET_SETIFMTU);
2663 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2665 if (ifp->if_ioctl == NULL)
2666 return (EOPNOTSUPP);
2667 /* Disallow MTU changes on bridge member interfaces. */
2669 return (EOPNOTSUPP);
2670 error = (*ifp->if_ioctl)(ifp, cmd, data);
2672 getmicrotime(&ifp->if_lastchange);
2675 DEBUGNET_NOTIFY_MTU(ifp);
2679 * If the link MTU changed, do network layer specific procedure.
2681 if (ifp->if_mtu != oldmtu) {
2692 if (cmd == SIOCADDMULTI)
2693 error = priv_check(td, PRIV_NET_ADDMULTI);
2695 error = priv_check(td, PRIV_NET_DELMULTI);
2699 /* Don't allow group membership on non-multicast interfaces. */
2700 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2701 return (EOPNOTSUPP);
2703 /* Don't let users screw up protocols' entries. */
2704 if (ifr->ifr_addr.sa_family != AF_LINK)
2707 if (cmd == SIOCADDMULTI) {
2708 struct epoch_tracker et;
2709 struct ifmultiaddr *ifma;
2712 * Userland is only permitted to join groups once
2713 * via the if_addmulti() KPI, because it cannot hold
2714 * struct ifmultiaddr * between calls. It may also
2715 * lose a race while we check if the membership
2718 NET_EPOCH_ENTER(et);
2719 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2724 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2726 error = if_delmulti(ifp, &ifr->ifr_addr);
2729 getmicrotime(&ifp->if_lastchange);
2732 case SIOCSIFPHYADDR:
2733 case SIOCDIFPHYADDR:
2735 case SIOCSIFPHYADDR_IN6:
2738 case SIOCSIFGENERIC:
2739 error = priv_check(td, PRIV_NET_HWIOCTL);
2742 if (ifp->if_ioctl == NULL)
2743 return (EOPNOTSUPP);
2744 error = (*ifp->if_ioctl)(ifp, cmd, data);
2746 getmicrotime(&ifp->if_lastchange);
2750 case SIOCGIFPSRCADDR:
2751 case SIOCGIFPDSTADDR:
2754 case SIOCGIFGENERIC:
2756 case SIOCGIFRSSHASH:
2757 case SIOCGIFDOWNREASON:
2758 if (ifp->if_ioctl == NULL)
2759 return (EOPNOTSUPP);
2760 error = (*ifp->if_ioctl)(ifp, cmd, data);
2764 error = priv_check(td, PRIV_NET_SETLLADDR);
2767 error = if_setlladdr(ifp,
2768 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2772 error = if_gethwaddr(ifp, ifr);
2776 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2779 error = if_addgroup(ifp,
2780 ((struct ifgroupreq *)data)->ifgr_group);
2787 struct epoch_tracker et;
2789 NET_EPOCH_ENTER(et);
2790 error = if_getgroup((struct ifgroupreq *)data, ifp);
2796 error = priv_check(td, PRIV_NET_DELIFGROUP);
2799 error = if_delgroup(ifp,
2800 ((struct ifgroupreq *)data)->ifgr_group);
2816 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2818 #ifdef COMPAT_FREEBSD32
2822 struct ifgroupreq ifgr;
2823 struct ifmediareq ifmr;
2826 struct ifconf32 *ifc32;
2827 struct ifdrv32 *ifd32;
2828 struct ifgroupreq32 *ifgr32;
2829 struct ifmediareq32 *ifmr32;
2839 CURVNET_SET(so->so_vnet);
2841 /* Make sure the VNET is stable. */
2842 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2849 #ifdef COMPAT_FREEBSD32
2853 ifc32 = (struct ifconf32 *)data;
2854 thunk.ifc.ifc_len = ifc32->ifc_len;
2855 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2856 data = (caddr_t)&thunk.ifc;
2859 case SIOCGDRVSPEC32:
2860 case SIOCSDRVSPEC32:
2861 ifd32 = (struct ifdrv32 *)data;
2862 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2863 sizeof(thunk.ifd.ifd_name));
2864 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2865 thunk.ifd.ifd_len = ifd32->ifd_len;
2866 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2867 data = (caddr_t)&thunk.ifd;
2868 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2870 case SIOCAIFGROUP32:
2871 case SIOCGIFGROUP32:
2872 case SIOCDIFGROUP32:
2873 case SIOCGIFGMEMB32:
2874 ifgr32 = (struct ifgroupreq32 *)data;
2875 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2876 sizeof(thunk.ifgr.ifgr_name));
2877 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2879 case SIOCAIFGROUP32:
2880 case SIOCDIFGROUP32:
2881 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2882 sizeof(thunk.ifgr.ifgr_group));
2884 case SIOCGIFGROUP32:
2885 case SIOCGIFGMEMB32:
2886 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2889 data = (caddr_t)&thunk.ifgr;
2890 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2892 case SIOCGIFMEDIA32:
2893 case SIOCGIFXMEDIA32:
2894 ifmr32 = (struct ifmediareq32 *)data;
2895 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2896 sizeof(thunk.ifmr.ifm_name));
2897 thunk.ifmr.ifm_current = ifmr32->ifm_current;
2898 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
2899 thunk.ifmr.ifm_status = ifmr32->ifm_status;
2900 thunk.ifmr.ifm_active = ifmr32->ifm_active;
2901 thunk.ifmr.ifm_count = ifmr32->ifm_count;
2902 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
2903 data = (caddr_t)&thunk.ifmr;
2904 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2911 error = ifconf(cmd, data);
2915 ifr = (struct ifreq *)data;
2919 error = priv_check(td, PRIV_NET_SETIFVNET);
2921 error = if_vmove_reclaim(td, ifr->ifr_name,
2927 error = priv_check(td, PRIV_NET_IFCREATE);
2929 error = if_clone_create(ifr->ifr_name,
2930 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
2931 ifr_data_get_ptr(ifr) : NULL);
2934 error = priv_check(td, PRIV_NET_IFDESTROY);
2937 sx_xlock(&ifnet_detach_sxlock);
2938 error = if_clone_destroy(ifr->ifr_name);
2939 sx_xunlock(&ifnet_detach_sxlock);
2943 case SIOCIFGCLONERS:
2944 error = if_clone_list((struct if_clonereq *)data);
2948 error = if_getgroupmembers((struct ifgroupreq *)data);
2951 #if defined(INET) || defined(INET6)
2954 if (carp_ioctl_p == NULL)
2955 error = EPROTONOSUPPORT;
2957 error = (*carp_ioctl_p)(ifr, cmd, td);
2962 ifp = ifunit_ref(ifr->ifr_name);
2968 error = ifhwioctl(cmd, ifp, data, td);
2969 if (error != ENOIOCTL)
2972 oif_flags = ifp->if_flags;
2973 if (so->so_proto == NULL) {
2979 * Pass the request on to the socket control method, and if the
2980 * latter returns EOPNOTSUPP, directly to the interface.
2982 * Make an exception for the legacy SIOCSIF* requests. Drivers
2983 * trust SIOCSIFADDR et al to come from an already privileged
2984 * layer, and do not perform any credentials checks or input
2987 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2989 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2990 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2991 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2992 error = (*ifp->if_ioctl)(ifp, cmd, data);
2994 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2996 if (ifp->if_flags & IFF_UP)
3005 #ifdef COMPAT_FREEBSD32
3008 switch (saved_cmd) {
3010 ifc32->ifc_len = thunk.ifc.ifc_len;
3012 case SIOCGDRVSPEC32:
3014 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3015 * the struct so just assert that ifd_len (the only
3016 * field it might make sense to update) hasn't
3019 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3020 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3021 thunk.ifd.ifd_len));
3023 case SIOCGIFGROUP32:
3024 case SIOCGIFGMEMB32:
3025 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3027 case SIOCGIFMEDIA32:
3028 case SIOCGIFXMEDIA32:
3029 ifmr32->ifm_current = thunk.ifmr.ifm_current;
3030 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3031 ifmr32->ifm_status = thunk.ifmr.ifm_status;
3032 ifmr32->ifm_active = thunk.ifmr.ifm_active;
3033 ifmr32->ifm_count = thunk.ifmr.ifm_count;
3041 * The code common to handling reference counted flags,
3042 * e.g., in ifpromisc() and if_allmulti().
3043 * The "pflag" argument can specify a permanent mode flag to check,
3044 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3046 * Only to be used on stack-owned flags, not driver-owned flags.
3049 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3053 int oldflags, oldcount;
3055 /* Sanity checks to catch programming errors */
3056 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3057 ("%s: setting driver-owned flag %d", __func__, flag));
3060 KASSERT(*refcount >= 0,
3061 ("%s: increment negative refcount %d for flag %d",
3062 __func__, *refcount, flag));
3064 KASSERT(*refcount > 0,
3065 ("%s: decrement non-positive refcount %d for flag %d",
3066 __func__, *refcount, flag));
3068 /* In case this mode is permanent, just touch refcount */
3069 if (ifp->if_flags & pflag) {
3070 *refcount += onswitch ? 1 : -1;
3074 /* Save ifnet parameters for if_ioctl() may fail */
3075 oldcount = *refcount;
3076 oldflags = ifp->if_flags;
3079 * See if we aren't the only and touching refcount is enough.
3080 * Actually toggle interface flag if we are the first or last.
3085 ifp->if_flags |= flag;
3089 ifp->if_flags &= ~flag;
3092 /* Call down the driver since we've changed interface flags */
3093 if (ifp->if_ioctl == NULL) {
3097 ifr.ifr_flags = ifp->if_flags & 0xffff;
3098 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3099 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3102 /* Notify userland that interface flags have changed */
3107 /* Recover after driver error */
3108 *refcount = oldcount;
3109 ifp->if_flags = oldflags;
3114 * Set/clear promiscuous mode on interface ifp based on the truth value
3115 * of pswitch. The calls are reference counted so that only the first
3116 * "on" request actually has an effect, as does the final "off" request.
3117 * Results are undefined if the "off" and "on" requests are not matched.
3120 ifpromisc(struct ifnet *ifp, int pswitch)
3123 int oldflags = ifp->if_flags;
3125 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3126 &ifp->if_pcount, pswitch);
3127 /* If promiscuous mode status has changed, log a message */
3128 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3129 log_promisc_mode_change)
3130 if_printf(ifp, "promiscuous mode %s\n",
3131 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3136 * Return interface configuration
3137 * of system. List may be used
3138 * in later ioctl's (above) to get
3139 * other information.
3143 ifconf(u_long cmd, caddr_t data)
3145 struct ifconf *ifc = (struct ifconf *)data;
3150 int error, full = 0, valid_len, max_len;
3152 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3153 max_len = maxphys - 1;
3155 /* Prevent hostile input from being able to crash the system */
3156 if (ifc->ifc_len <= 0)
3160 if (ifc->ifc_len <= max_len) {
3161 max_len = ifc->ifc_len;
3164 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3169 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3170 struct epoch_tracker et;
3174 * Zero the ifr to make sure we don't disclose the contents
3177 memset(&ifr, 0, sizeof(ifr));
3179 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3180 >= sizeof(ifr.ifr_name)) {
3183 return (ENAMETOOLONG);
3187 NET_EPOCH_ENTER(et);
3188 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3189 struct sockaddr *sa = ifa->ifa_addr;
3191 if (prison_if(curthread->td_ucred, sa) != 0)
3194 if (sa->sa_len <= sizeof(*sa)) {
3195 if (sa->sa_len < sizeof(*sa)) {
3196 memset(&ifr.ifr_ifru.ifru_addr, 0,
3197 sizeof(ifr.ifr_ifru.ifru_addr));
3198 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3201 ifr.ifr_ifru.ifru_addr = *sa;
3202 sbuf_bcat(sb, &ifr, sizeof(ifr));
3203 max_len += sizeof(ifr);
3206 offsetof(struct ifreq, ifr_addr));
3207 max_len += offsetof(struct ifreq, ifr_addr);
3208 sbuf_bcat(sb, sa, sa->sa_len);
3209 max_len += sa->sa_len;
3212 if (sbuf_error(sb) == 0)
3213 valid_len = sbuf_len(sb);
3217 sbuf_bcat(sb, &ifr, sizeof(ifr));
3218 max_len += sizeof(ifr);
3220 if (sbuf_error(sb) == 0)
3221 valid_len = sbuf_len(sb);
3227 * If we didn't allocate enough space (uncommon), try again. If
3228 * we have already allocated as much space as we are allowed,
3229 * return what we've got.
3231 if (valid_len != max_len && !full) {
3236 ifc->ifc_len = valid_len;
3238 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3244 * Just like ifpromisc(), but for all-multicast-reception mode.
3247 if_allmulti(struct ifnet *ifp, int onswitch)
3250 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3253 struct ifmultiaddr *
3254 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3256 struct ifmultiaddr *ifma;
3258 IF_ADDR_LOCK_ASSERT(ifp);
3260 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3261 if (sa->sa_family == AF_LINK) {
3262 if (sa_dl_equal(ifma->ifma_addr, sa))
3265 if (sa_equal(ifma->ifma_addr, sa))
3274 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3275 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3276 * the ifnet multicast address list here, so the caller must do that and
3277 * other setup work (such as notifying the device driver). The reference
3278 * count is initialized to 1.
3280 static struct ifmultiaddr *
3281 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3284 struct ifmultiaddr *ifma;
3285 struct sockaddr *dupsa;
3287 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3292 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3293 if (dupsa == NULL) {
3294 free(ifma, M_IFMADDR);
3297 bcopy(sa, dupsa, sa->sa_len);
3298 ifma->ifma_addr = dupsa;
3300 ifma->ifma_ifp = ifp;
3301 ifma->ifma_refcount = 1;
3302 ifma->ifma_protospec = NULL;
3305 ifma->ifma_lladdr = NULL;
3309 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3310 if (dupsa == NULL) {
3311 free(ifma->ifma_addr, M_IFMADDR);
3312 free(ifma, M_IFMADDR);
3315 bcopy(llsa, dupsa, llsa->sa_len);
3316 ifma->ifma_lladdr = dupsa;
3322 * if_freemulti: free ifmultiaddr structure and possibly attached related
3323 * addresses. The caller is responsible for implementing reference
3324 * counting, notifying the driver, handling routing messages, and releasing
3325 * any dependent link layer state.
3327 #ifdef MCAST_VERBOSE
3328 extern void kdb_backtrace(void);
3331 if_freemulti_internal(struct ifmultiaddr *ifma)
3334 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3335 ifma->ifma_refcount));
3337 if (ifma->ifma_lladdr != NULL)
3338 free(ifma->ifma_lladdr, M_IFMADDR);
3339 #ifdef MCAST_VERBOSE
3341 printf("%s freeing ifma: %p\n", __func__, ifma);
3343 free(ifma->ifma_addr, M_IFMADDR);
3344 free(ifma, M_IFMADDR);
3348 if_destroymulti(epoch_context_t ctx)
3350 struct ifmultiaddr *ifma;
3352 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3353 if_freemulti_internal(ifma);
3357 if_freemulti(struct ifmultiaddr *ifma)
3359 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3360 ifma->ifma_refcount));
3362 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3366 * Register an additional multicast address with a network interface.
3368 * - If the address is already present, bump the reference count on the
3369 * address and return.
3370 * - If the address is not link-layer, look up a link layer address.
3371 * - Allocate address structures for one or both addresses, and attach to the
3372 * multicast address list on the interface. If automatically adding a link
3373 * layer address, the protocol address will own a reference to the link
3374 * layer address, to be freed when it is freed.
3375 * - Notify the network device driver of an addition to the multicast address
3378 * 'sa' points to caller-owned memory with the desired multicast address.
3380 * 'retifma' will be used to return a pointer to the resulting multicast
3381 * address reference, if desired.
3384 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3385 struct ifmultiaddr **retifma)
3387 struct ifmultiaddr *ifma, *ll_ifma;
3388 struct sockaddr *llsa;
3389 struct sockaddr_dl sdl;
3393 IN_MULTI_LIST_UNLOCK_ASSERT();
3396 IN6_MULTI_LIST_UNLOCK_ASSERT();
3399 * If the address is already present, return a new reference to it;
3400 * otherwise, allocate storage and set up a new address.
3403 ifma = if_findmulti(ifp, sa);
3405 ifma->ifma_refcount++;
3406 if (retifma != NULL)
3408 IF_ADDR_WUNLOCK(ifp);
3413 * The address isn't already present; resolve the protocol address
3414 * into a link layer address, and then look that up, bump its
3415 * refcount or allocate an ifma for that also.
3416 * Most link layer resolving functions returns address data which
3417 * fits inside default sockaddr_dl structure. However callback
3418 * can allocate another sockaddr structure, in that case we need to
3423 if (ifp->if_resolvemulti != NULL) {
3424 /* Provide called function with buffer size information */
3425 sdl.sdl_len = sizeof(sdl);
3426 llsa = (struct sockaddr *)&sdl;
3427 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3433 * Allocate the new address. Don't hook it up yet, as we may also
3434 * need to allocate a link layer multicast address.
3436 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3443 * If a link layer address is found, we'll need to see if it's
3444 * already present in the address list, or allocate is as well.
3445 * When this block finishes, the link layer address will be on the
3449 ll_ifma = if_findmulti(ifp, llsa);
3450 if (ll_ifma == NULL) {
3451 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3452 if (ll_ifma == NULL) {
3453 --ifma->ifma_refcount;
3458 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3459 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3462 ll_ifma->ifma_refcount++;
3463 ifma->ifma_llifma = ll_ifma;
3467 * We now have a new multicast address, ifma, and possibly a new or
3468 * referenced link layer address. Add the primary address to the
3469 * ifnet address list.
3471 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3472 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3474 if (retifma != NULL)
3478 * Must generate the message while holding the lock so that 'ifma'
3479 * pointer is still valid.
3481 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3482 IF_ADDR_WUNLOCK(ifp);
3485 * We are certain we have added something, so call down to the
3486 * interface to let them know about it.
3488 if (ifp->if_ioctl != NULL) {
3489 if (THREAD_CAN_SLEEP())
3490 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3492 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3495 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3496 link_free_sdl(llsa);
3501 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3502 link_free_sdl(llsa);
3505 IF_ADDR_WUNLOCK(ifp);
3510 if_siocaddmulti(void *arg, int pending)
3517 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3519 CURVNET_SET(ifp->if_vnet);
3520 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3525 * Delete a multicast group membership by network-layer group address.
3527 * Returns ENOENT if the entry could not be found. If ifp no longer
3528 * exists, results are undefined. This entry point should only be used
3529 * from subsystems which do appropriate locking to hold ifp for the
3530 * duration of the call.
3531 * Network-layer protocol domains must use if_delmulti_ifma().
3534 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3536 struct ifmultiaddr *ifma;
3539 KASSERT(ifp, ("%s: NULL ifp", __func__));
3543 ifma = if_findmulti(ifp, sa);
3545 lastref = if_delmulti_locked(ifp, ifma, 0);
3546 IF_ADDR_WUNLOCK(ifp);
3551 if (lastref && ifp->if_ioctl != NULL) {
3552 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3559 * Delete all multicast group membership for an interface.
3560 * Should be used to quickly flush all multicast filters.
3563 if_delallmulti(struct ifnet *ifp)
3565 struct ifmultiaddr *ifma;
3566 struct ifmultiaddr *next;
3569 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3570 if_delmulti_locked(ifp, ifma, 0);
3571 IF_ADDR_WUNLOCK(ifp);
3575 if_delmulti_ifma(struct ifmultiaddr *ifma)
3577 if_delmulti_ifma_flags(ifma, 0);
3581 * Delete a multicast group membership by group membership pointer.
3582 * Network-layer protocol domains must use this routine.
3584 * It is safe to call this routine if the ifp disappeared.
3587 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3591 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3593 IN_MULTI_LIST_UNLOCK_ASSERT();
3595 ifp = ifma->ifma_ifp;
3598 printf("%s: ifma_ifp seems to be detached\n", __func__);
3600 struct epoch_tracker et;
3603 NET_EPOCH_ENTER(et);
3604 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3613 * If and only if the ifnet instance exists: Acquire the address lock.
3618 lastref = if_delmulti_locked(ifp, ifma, flags);
3622 * If and only if the ifnet instance exists:
3623 * Release the address lock.
3624 * If the group was left: update the hardware hash filter.
3626 IF_ADDR_WUNLOCK(ifp);
3627 if (lastref && ifp->if_ioctl != NULL) {
3628 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3634 * Perform deletion of network-layer and/or link-layer multicast address.
3636 * Return 0 if the reference count was decremented.
3637 * Return 1 if the final reference was released, indicating that the
3638 * hardware hash filter should be reprogrammed.
3641 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3643 struct ifmultiaddr *ll_ifma;
3645 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3646 KASSERT(ifma->ifma_ifp == ifp,
3647 ("%s: inconsistent ifp %p", __func__, ifp));
3648 IF_ADDR_WLOCK_ASSERT(ifp);
3651 ifp = ifma->ifma_ifp;
3652 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3655 * If the ifnet is detaching, null out references to ifnet,
3656 * so that upper protocol layers will notice, and not attempt
3657 * to obtain locks for an ifnet which no longer exists. The
3658 * routing socket announcement must happen before the ifnet
3659 * instance is detached from the system.
3663 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3666 * ifp may already be nulled out if we are being reentered
3667 * to delete the ll_ifma.
3670 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3671 ifma->ifma_ifp = NULL;
3675 if (--ifma->ifma_refcount > 0)
3678 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3679 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3680 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3683 * If this ifma is a network-layer ifma, a link-layer ifma may
3684 * have been associated with it. Release it first if so.
3686 ll_ifma = ifma->ifma_llifma;
3687 if (ll_ifma != NULL) {
3688 KASSERT(ifma->ifma_lladdr != NULL,
3689 ("%s: llifma w/o lladdr", __func__));
3691 ll_ifma->ifma_ifp = NULL; /* XXX */
3692 if (--ll_ifma->ifma_refcount == 0) {
3694 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3695 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3697 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3700 if_freemulti(ll_ifma);
3705 struct ifmultiaddr *ifmatmp;
3707 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3708 MPASS(ifma != ifmatmp);
3713 * The last reference to this instance of struct ifmultiaddr
3714 * was released; the hardware should be notified of this change.
3720 * Set the link layer address on an interface.
3722 * At this time we only support certain types of interfaces,
3723 * and we don't allow the length of the address to change.
3725 * Set noinline to be dtrace-friendly
3728 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3730 struct sockaddr_dl *sdl;
3738 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3742 if (len != sdl->sdl_alen) /* don't allow length to change */
3745 switch (ifp->if_type) {
3750 case IFT_IEEE8023ADLAG:
3751 bcopy(lladdr, LLADDR(sdl), len);
3758 * If the interface is already up, we need
3759 * to re-init it in order to reprogram its
3762 if ((ifp->if_flags & IFF_UP) != 0) {
3763 if (ifp->if_ioctl) {
3764 ifp->if_flags &= ~IFF_UP;
3765 ifr.ifr_flags = ifp->if_flags & 0xffff;
3766 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3767 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3768 ifp->if_flags |= IFF_UP;
3769 ifr.ifr_flags = ifp->if_flags & 0xffff;
3770 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3771 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3774 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3780 * Compat function for handling basic encapsulation requests.
3781 * Not converted stacks (FDDI, IB, ..) supports traditional
3782 * output model: ARP (and other similar L2 protocols) are handled
3783 * inside output routine, arpresolve/nd6_resolve() returns MAC
3784 * address instead of full prepend.
3786 * This function creates calculated header==MAC for IPv4/IPv6 and
3787 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3791 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3794 if (req->rtype != IFENCAP_LL)
3795 return (EOPNOTSUPP);
3797 if (req->bufsize < req->lladdr_len)
3800 switch (req->family) {
3805 return (EAFNOSUPPORT);
3808 /* Copy lladdr to storage as is */
3809 memmove(req->buf, req->lladdr, req->lladdr_len);
3810 req->bufsize = req->lladdr_len;
3811 req->lladdr_off = 0;
3817 * Tunnel interfaces can nest, also they may cause infinite recursion
3818 * calls when misconfigured. We'll prevent this by detecting loops.
3819 * High nesting level may cause stack exhaustion. We'll prevent this
3820 * by introducing upper limit.
3822 * Return 0, if tunnel nesting count is equal or less than limit.
3825 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3833 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3834 if (*(struct ifnet **)(mtag + 1) == ifp) {
3835 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3840 if (count > limit) {
3842 "%s: if_output recursively called too many times(%d)\n",
3843 if_name(ifp), count);
3846 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3849 *(struct ifnet **)(mtag + 1) = ifp;
3850 m_tag_prepend(m, mtag);
3855 * Get the link layer address that was read from the hardware at attach.
3857 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3858 * their component interfaces as IFT_IEEE8023ADLAG.
3861 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3864 if (ifp->if_hw_addr == NULL)
3867 switch (ifp->if_type) {
3869 case IFT_IEEE8023ADLAG:
3870 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3878 * The name argument must be a pointer to storage which will last as
3879 * long as the interface does. For physical devices, the result of
3880 * device_get_name(dev) is a good choice and for pseudo-devices a
3881 * static string works well.
3884 if_initname(struct ifnet *ifp, const char *name, int unit)
3886 ifp->if_dname = name;
3887 ifp->if_dunit = unit;
3888 if (unit != IF_DUNIT_NONE)
3889 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3891 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3895 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
3899 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3900 vlog(pri, if_fmt, ap);
3906 if_printf(struct ifnet *ifp, const char *fmt, ...)
3911 if_vlog(ifp, LOG_INFO, fmt, ap);
3917 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
3922 if_vlog(ifp, pri, fmt, ap);
3928 if_start(struct ifnet *ifp)
3931 (*(ifp)->if_start)(ifp);
3935 * Backwards compatibility interface for drivers
3936 * that have not implemented it
3939 if_transmit(struct ifnet *ifp, struct mbuf *m)
3943 IFQ_HANDOFF(ifp, m, error);
3948 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3955 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3960 if (_IF_QFULL(ifq)) {
3962 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3967 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3968 if (m->m_flags & (M_BCAST|M_MCAST))
3969 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3970 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3972 _IF_ENQUEUE(ifq, m);
3974 if (ifp != NULL && !active)
3975 (*(ifp)->if_start)(ifp);
3980 if_register_com_alloc(u_char type,
3981 if_com_alloc_t *a, if_com_free_t *f)
3984 KASSERT(if_com_alloc[type] == NULL,
3985 ("if_register_com_alloc: %d already registered", type));
3986 KASSERT(if_com_free[type] == NULL,
3987 ("if_register_com_alloc: %d free already registered", type));
3989 if_com_alloc[type] = a;
3990 if_com_free[type] = f;
3994 if_deregister_com_alloc(u_char type)
3997 KASSERT(if_com_alloc[type] != NULL,
3998 ("if_deregister_com_alloc: %d not registered", type));
3999 KASSERT(if_com_free[type] != NULL,
4000 ("if_deregister_com_alloc: %d free not registered", type));
4003 * Ensure all pending EPOCH(9) callbacks have been executed. This
4004 * fixes issues about late invocation of if_destroy(), which leads
4005 * to memory leak from if_com_alloc[type] allocated if_l2com.
4007 epoch_drain_callbacks(net_epoch_preempt);
4009 if_com_alloc[type] = NULL;
4010 if_com_free[type] = NULL;
4013 /* API for driver access to network stack owned ifnet.*/
4015 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4019 oldbrate = ifp->if_baudrate;
4020 ifp->if_baudrate = baudrate;
4025 if_getbaudrate(if_t ifp)
4028 return (((struct ifnet *)ifp)->if_baudrate);
4032 if_setcapabilities(if_t ifp, int capabilities)
4034 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4039 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4041 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4042 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4048 if_getcapabilities(if_t ifp)
4050 return ((struct ifnet *)ifp)->if_capabilities;
4054 if_setcapenable(if_t ifp, int capabilities)
4056 ((struct ifnet *)ifp)->if_capenable = capabilities;
4061 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4064 ((struct ifnet *)ifp)->if_capenable |= setcap;
4066 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4072 if_getdname(if_t ifp)
4074 return ((struct ifnet *)ifp)->if_dname;
4078 if_togglecapenable(if_t ifp, int togglecap)
4080 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4085 if_getcapenable(if_t ifp)
4087 return ((struct ifnet *)ifp)->if_capenable;
4091 * This is largely undesirable because it ties ifnet to a device, but does
4092 * provide flexiblity for an embedded product vendor. Should be used with
4093 * the understanding that it violates the interface boundaries, and should be
4094 * a last resort only.
4097 if_setdev(if_t ifp, void *dev)
4103 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4105 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4106 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4112 if_getdrvflags(if_t ifp)
4114 return ((struct ifnet *)ifp)->if_drv_flags;
4118 if_setdrvflags(if_t ifp, int flags)
4120 ((struct ifnet *)ifp)->if_drv_flags = flags;
4125 if_setflags(if_t ifp, int flags)
4128 ifp->if_flags = flags;
4133 if_setflagbits(if_t ifp, int set, int clear)
4135 ((struct ifnet *)ifp)->if_flags |= set;
4136 ((struct ifnet *)ifp)->if_flags &= ~clear;
4142 if_getflags(if_t ifp)
4144 return ((struct ifnet *)ifp)->if_flags;
4148 if_clearhwassist(if_t ifp)
4150 ((struct ifnet *)ifp)->if_hwassist = 0;
4155 if_sethwassistbits(if_t ifp, int toset, int toclear)
4157 ((struct ifnet *)ifp)->if_hwassist |= toset;
4158 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4164 if_sethwassist(if_t ifp, int hwassist_bit)
4166 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4171 if_gethwassist(if_t ifp)
4173 return ((struct ifnet *)ifp)->if_hwassist;
4177 if_setmtu(if_t ifp, int mtu)
4179 ((struct ifnet *)ifp)->if_mtu = mtu;
4186 return ((struct ifnet *)ifp)->if_mtu;
4190 if_getmtu_family(if_t ifp, int family)
4194 for (dp = domains; dp; dp = dp->dom_next) {
4195 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4196 return (dp->dom_ifmtu((struct ifnet *)ifp));
4199 return (((struct ifnet *)ifp)->if_mtu);
4203 * Methods for drivers to access interface unicast and multicast
4204 * link level addresses. Driver shall not know 'struct ifaddr' neither
4205 * 'struct ifmultiaddr'.
4208 if_lladdr_count(if_t ifp)
4210 struct epoch_tracker et;
4215 NET_EPOCH_ENTER(et);
4216 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4217 if (ifa->ifa_addr->sa_family == AF_LINK)
4225 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4227 struct epoch_tracker et;
4234 NET_EPOCH_ENTER(et);
4235 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4236 if (ifa->ifa_addr->sa_family != AF_LINK)
4238 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4247 if_llmaddr_count(if_t ifp)
4249 struct epoch_tracker et;
4250 struct ifmultiaddr *ifma;
4254 NET_EPOCH_ENTER(et);
4255 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4256 if (ifma->ifma_addr->sa_family == AF_LINK)
4264 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4266 struct epoch_tracker et;
4267 struct ifmultiaddr *ifma;
4273 NET_EPOCH_ENTER(et);
4274 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4275 if (ifma->ifma_addr->sa_family != AF_LINK)
4277 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4286 if_setsoftc(if_t ifp, void *softc)
4288 ((struct ifnet *)ifp)->if_softc = softc;
4293 if_getsoftc(if_t ifp)
4295 return ((struct ifnet *)ifp)->if_softc;
4299 if_setrcvif(struct mbuf *m, if_t ifp)
4302 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4303 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4307 if_setvtag(struct mbuf *m, uint16_t tag)
4309 m->m_pkthdr.ether_vtag = tag;
4313 if_getvtag(struct mbuf *m)
4316 return (m->m_pkthdr.ether_vtag);
4320 if_sendq_empty(if_t ifp)
4322 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4326 if_getifaddr(if_t ifp)
4328 return ((struct ifnet *)ifp)->if_addr;
4332 if_getamcount(if_t ifp)
4334 return ((struct ifnet *)ifp)->if_amcount;
4338 if_setsendqready(if_t ifp)
4340 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4345 if_setsendqlen(if_t ifp, int tx_desc_count)
4347 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4348 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4354 if_vlantrunkinuse(if_t ifp)
4356 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4360 if_input(if_t ifp, struct mbuf* sendmp)
4362 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4368 if_dequeue(if_t ifp)
4371 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4377 if_sendq_prepend(if_t ifp, struct mbuf *m)
4379 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4384 if_setifheaderlen(if_t ifp, int len)
4386 ((struct ifnet *)ifp)->if_hdrlen = len;
4391 if_getlladdr(if_t ifp)
4393 return (IF_LLADDR((struct ifnet *)ifp));
4397 if_gethandle(u_char type)
4399 return (if_alloc(type));
4403 if_bpfmtap(if_t ifh, struct mbuf *m)
4405 struct ifnet *ifp = (struct ifnet *)ifh;
4411 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4413 struct ifnet *ifp = (struct ifnet *)ifh;
4415 ETHER_BPF_MTAP(ifp, m);
4419 if_vlancap(if_t ifh)
4421 struct ifnet *ifp = (struct ifnet *)ifh;
4422 VLAN_CAPABILITIES(ifp);
4426 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4429 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4434 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4437 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4442 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4445 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4450 if_gethwtsomax(if_t ifp)
4453 return (((struct ifnet *)ifp)->if_hw_tsomax);
4457 if_gethwtsomaxsegcount(if_t ifp)
4460 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4464 if_gethwtsomaxsegsize(if_t ifp)
4467 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4471 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4473 ((struct ifnet *)ifp)->if_init = init_fn;
4477 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4479 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4483 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4485 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4489 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4491 ((struct ifnet *)ifp)->if_transmit = start_fn;
4494 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4496 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4501 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4504 ifp->if_get_counter = fn;
4509 if_show_ifnet(struct ifnet *ifp)
4514 db_printf("%s:\n", ifp->if_xname);
4515 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
4516 IF_DB_PRINTF("%s", if_dname);
4517 IF_DB_PRINTF("%d", if_dunit);
4518 IF_DB_PRINTF("%s", if_description);
4519 IF_DB_PRINTF("%u", if_index);
4520 IF_DB_PRINTF("%d", if_idxgen);
4521 IF_DB_PRINTF("%u", if_refcount);
4522 IF_DB_PRINTF("%p", if_softc);
4523 IF_DB_PRINTF("%p", if_l2com);
4524 IF_DB_PRINTF("%p", if_llsoftc);
4525 IF_DB_PRINTF("%d", if_amcount);
4526 IF_DB_PRINTF("%p", if_addr);
4527 IF_DB_PRINTF("%p", if_broadcastaddr);
4528 IF_DB_PRINTF("%p", if_afdata);
4529 IF_DB_PRINTF("%d", if_afdata_initialized);
4530 IF_DB_PRINTF("%u", if_fib);
4531 IF_DB_PRINTF("%p", if_vnet);
4532 IF_DB_PRINTF("%p", if_home_vnet);
4533 IF_DB_PRINTF("%p", if_vlantrunk);
4534 IF_DB_PRINTF("%p", if_bpf);
4535 IF_DB_PRINTF("%u", if_pcount);
4536 IF_DB_PRINTF("%p", if_bridge);
4537 IF_DB_PRINTF("%p", if_lagg);
4538 IF_DB_PRINTF("%p", if_pf_kif);
4539 IF_DB_PRINTF("%p", if_carp);
4540 IF_DB_PRINTF("%p", if_label);
4541 IF_DB_PRINTF("%p", if_netmap);
4542 IF_DB_PRINTF("0x%08x", if_flags);
4543 IF_DB_PRINTF("0x%08x", if_drv_flags);
4544 IF_DB_PRINTF("0x%08x", if_capabilities);
4545 IF_DB_PRINTF("0x%08x", if_capenable);
4546 IF_DB_PRINTF("%p", if_snd.ifq_head);
4547 IF_DB_PRINTF("%p", if_snd.ifq_tail);
4548 IF_DB_PRINTF("%d", if_snd.ifq_len);
4549 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
4550 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
4551 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
4552 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
4553 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
4554 IF_DB_PRINTF("%d", if_snd.altq_type);
4555 IF_DB_PRINTF("%x", if_snd.altq_flags);
4559 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
4563 db_printf("usage: show ifnet <struct ifnet *>\n");
4567 if_show_ifnet((struct ifnet *)addr);
4570 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
4575 for (idx = 1; idx <= if_index; idx++) {
4576 ifp = ifindex_table[idx].ife_ifnet;
4579 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);