2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
45 #include <sys/epoch.h>
47 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/protosw.h>
53 #include <sys/kernel.h>
55 #include <sys/refcount.h>
56 #include <sys/module.h>
57 #include <sys/rwlock.h>
58 #include <sys/sockio.h>
59 #include <sys/syslog.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/taskqueue.h>
63 #include <sys/domain.h>
66 #include <sys/sched.h>
69 #include <machine/stdarg.h>
73 #include <net/ethernet.h>
75 #include <net/if_arp.h>
76 #include <net/if_clone.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/if_var.h>
80 #include <net/if_media.h>
81 #include <net/if_vlan_var.h>
82 #include <net/radix.h>
83 #include <net/route.h>
86 #if defined(INET) || defined(INET6)
87 #include <net/ethernet.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_carp.h>
93 #include <netinet/if_ether.h>
94 #include <netinet/netdump/netdump.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet6/in6_ifattach.h>
100 #endif /* INET || INET6 */
102 #include <security/mac/mac_framework.h>
105 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
106 * and ifr_ifru when it is used in SIOCGIFCONF.
108 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
109 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
111 __read_mostly epoch_t net_epoch_preempt;
112 __read_mostly epoch_t net_epoch;
113 #ifdef COMPAT_FREEBSD32
114 #include <sys/mount.h>
115 #include <compat/freebsd32/freebsd32.h>
117 struct ifreq_buffer32 {
118 uint32_t length; /* (size_t) */
119 uint32_t buffer; /* (void *) */
123 * Interface request structure used for socket
124 * ioctl's. All interface ioctl's must have parameter
125 * definitions which begin with ifr_name. The
126 * remainder may be interface specific.
129 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
131 struct sockaddr ifru_addr;
132 struct sockaddr ifru_dstaddr;
133 struct sockaddr ifru_broadaddr;
134 struct ifreq_buffer32 ifru_buffer;
145 u_char ifru_vlan_pcp;
148 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
149 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
150 __offsetof(struct ifreq32, ifr_ifru));
152 struct ifgroupreq32 {
153 char ifgr_name[IFNAMSIZ];
156 char ifgru_group[IFNAMSIZ];
157 uint32_t ifgru_groups;
161 struct ifmediareq32 {
162 char ifm_name[IFNAMSIZ];
168 uint32_t ifm_ulist; /* (int *) */
170 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
171 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
173 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
174 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
175 #else /* !COMPAT_FREEBSD32 */
176 #define _CASE_IOC_IFGROUPREQ_32(cmd)
177 #endif /* !COMPAT_FREEBSD32 */
179 #define CASE_IOC_IFGROUPREQ(cmd) \
180 _CASE_IOC_IFGROUPREQ_32(cmd) \
185 #ifdef COMPAT_FREEBSD32
186 struct ifreq32 ifr32;
190 union ifgroupreq_union {
191 struct ifgroupreq ifgr;
192 #ifdef COMPAT_FREEBSD32
193 struct ifgroupreq32 ifgr32;
197 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
198 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
200 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
201 &ifqmaxlen, 0, "max send queue size");
203 /* Log link state change events */
204 static int log_link_state_change = 1;
206 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
207 &log_link_state_change, 0,
208 "log interface link state change events");
210 /* Log promiscuous mode change events */
211 static int log_promisc_mode_change = 1;
213 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
214 &log_promisc_mode_change, 1,
215 "log promiscuous mode change events");
217 /* Interface description */
218 static unsigned int ifdescr_maxlen = 1024;
219 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
221 "administrative maximum length for interface description");
223 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
225 /* global sx for non-critical path ifdescr */
226 static struct sx ifdescr_sx;
227 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
229 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
230 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
231 /* These are external hooks for CARP. */
232 void (*carp_linkstate_p)(struct ifnet *ifp);
233 void (*carp_demote_adj_p)(int, char *);
234 int (*carp_master_p)(struct ifaddr *);
235 #if defined(INET) || defined(INET6)
236 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
237 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
238 const struct sockaddr *sa);
239 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
240 int (*carp_attach_p)(struct ifaddr *, int);
241 void (*carp_detach_p)(struct ifaddr *, bool);
244 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
247 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
248 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
249 const struct in6_addr *taddr);
252 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
255 * XXX: Style; these should be sorted alphabetically, and unprototyped
256 * static functions should be prototyped. Currently they are sorted by
259 static void if_attachdomain(void *);
260 static void if_attachdomain1(struct ifnet *);
261 static int ifconf(u_long, caddr_t);
262 static void *if_grow(void);
263 static void if_input_default(struct ifnet *, struct mbuf *);
264 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
265 static void if_route(struct ifnet *, int flag, int fam);
266 static int if_setflag(struct ifnet *, int, int, int *, int);
267 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
268 static void if_unroute(struct ifnet *, int flag, int fam);
269 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
270 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
271 static void do_link_state_change(void *, int);
272 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
273 static int if_getgroupmembers(struct ifgroupreq *);
274 static void if_delgroups(struct ifnet *);
275 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
276 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
278 static void if_vmove(struct ifnet *, struct vnet *);
283 * XXX: declare here to avoid to include many inet6 related files..
284 * should be more generalized?
286 extern void nd6_setmtu(struct ifnet *);
289 /* ipsec helper hooks */
290 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
291 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
293 VNET_DEFINE(int, if_index);
294 int ifqmaxlen = IFQ_MAXLEN;
295 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
296 VNET_DEFINE(struct ifgrouphead, ifg_head);
298 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
300 /* Table of ifnet by index. */
301 VNET_DEFINE(struct ifnet **, ifindex_table);
303 #define V_if_indexlim VNET(if_indexlim)
304 #define V_ifindex_table VNET(ifindex_table)
307 * The global network interface list (V_ifnet) and related state (such as
308 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
309 * an rwlock. Either may be acquired shared to stablize the list, but both
310 * must be acquired writable to modify the list. This model allows us to
311 * both stablize the interface list during interrupt thread processing, but
312 * also to stablize it over long-running ioctls, without introducing priority
313 * inversions and deadlocks.
315 struct rwlock ifnet_rwlock;
316 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
317 struct sx ifnet_sxlock;
318 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
321 * The allocation of network interfaces is a rather non-atomic affair; we
322 * need to select an index before we are ready to expose the interface for
323 * use, so will use this pointer value to indicate reservation.
325 #define IFNET_HOLD (void *)(uintptr_t)(-1)
327 static if_com_alloc_t *if_com_alloc[256];
328 static if_com_free_t *if_com_free[256];
330 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
331 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
332 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
335 ifnet_byindex_locked(u_short idx)
338 if (idx > V_if_index)
340 if (V_ifindex_table[idx] == IFNET_HOLD)
342 return (V_ifindex_table[idx]);
346 ifnet_byindex(u_short idx)
350 ifp = ifnet_byindex_locked(idx);
355 ifnet_byindex_ref(u_short idx)
359 IFNET_RLOCK_NOSLEEP();
360 ifp = ifnet_byindex_locked(idx);
361 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
362 IFNET_RUNLOCK_NOSLEEP();
366 IFNET_RUNLOCK_NOSLEEP();
371 * Allocate an ifindex array entry; return 0 on success or an error on
375 ifindex_alloc(void **old)
379 IFNET_WLOCK_ASSERT();
381 * Try to find an empty slot below V_if_index. If we fail, take the
384 for (idx = 1; idx <= V_if_index; idx++) {
385 if (V_ifindex_table[idx] == NULL)
389 /* Catch if_index overflow. */
390 if (idx >= V_if_indexlim) {
394 if (idx > V_if_index)
400 ifindex_free_locked(u_short idx)
403 IFNET_WLOCK_ASSERT();
405 V_ifindex_table[idx] = NULL;
406 while (V_if_index > 0 &&
407 V_ifindex_table[V_if_index] == NULL)
412 ifindex_free(u_short idx)
416 ifindex_free_locked(idx);
421 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
424 V_ifindex_table[idx] = ifp;
428 ifaddr_byindex(u_short idx)
431 struct ifaddr *ifa = NULL;
433 IFNET_RLOCK_NOSLEEP();
434 ifp = ifnet_byindex_locked(idx);
435 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
437 IFNET_RUNLOCK_NOSLEEP();
442 * Network interface utility routines.
444 * Routines with ifa_ifwith* names take sockaddr *'s as
449 vnet_if_init(const void *unused __unused)
453 CK_STAILQ_INIT(&V_ifnet);
454 CK_STAILQ_INIT(&V_ifg_head);
456 old = if_grow(); /* create initial table */
458 epoch_wait_preempt(net_epoch_preempt);
460 vnet_if_clone_init();
462 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
467 vnet_if_uninit(const void *unused __unused)
470 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
471 "not empty", __func__, __LINE__, &V_ifnet));
472 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
473 "not empty", __func__, __LINE__, &V_ifg_head));
475 free((caddr_t)V_ifindex_table, M_IFNET);
477 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
478 vnet_if_uninit, NULL);
481 vnet_if_return(const void *unused __unused)
483 struct ifnet *ifp, *nifp;
485 /* Return all inherited interfaces to their parent vnets. */
486 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
487 if (ifp->if_home_vnet != ifp->if_vnet)
488 if_vmove(ifp, ifp->if_home_vnet);
491 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
492 vnet_if_return, NULL);
505 IFNET_WLOCK_ASSERT();
506 oldlim = V_if_indexlim;
508 n = (oldlim << 1) * sizeof(*e);
509 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
511 if (V_if_indexlim != oldlim) {
515 if (V_ifindex_table != NULL) {
516 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
517 old = V_ifindex_table;
525 * Allocate a struct ifnet and an index for an interface. A layer 2
526 * common structure will also be allocated if an allocation routine is
527 * registered for the passed type.
530 if_alloc(u_char type)
536 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
539 idx = ifindex_alloc(&old);
540 if (__predict_false(idx == USHRT_MAX)) {
542 epoch_wait_preempt(net_epoch_preempt);
546 ifnet_setbyindex(idx, IFNET_HOLD);
550 ifp->if_alloctype = type;
552 ifp->if_vnet = curvnet;
554 if (if_com_alloc[type] != NULL) {
555 ifp->if_l2com = if_com_alloc[type](type, ifp);
556 if (ifp->if_l2com == NULL) {
563 IF_ADDR_LOCK_INIT(ifp);
564 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
565 ifp->if_afdata_initialized = 0;
566 IF_AFDATA_LOCK_INIT(ifp);
567 CK_STAILQ_INIT(&ifp->if_addrhead);
568 CK_STAILQ_INIT(&ifp->if_multiaddrs);
569 CK_STAILQ_INIT(&ifp->if_groups);
573 ifq_init(&ifp->if_snd, ifp);
575 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
576 for (int i = 0; i < IFCOUNTERS; i++)
577 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
578 ifp->if_get_counter = if_get_counter_default;
579 ifp->if_pcp = IFNET_PCP_NONE;
580 ifnet_setbyindex(ifp->if_index, ifp);
585 * Do the actual work of freeing a struct ifnet, and layer 2 common
586 * structure. This call is made when the last reference to an
587 * interface is released.
590 if_free_internal(struct ifnet *ifp)
593 KASSERT((ifp->if_flags & IFF_DYING),
594 ("if_free_internal: interface not dying"));
596 if (if_com_free[ifp->if_alloctype] != NULL)
597 if_com_free[ifp->if_alloctype](ifp->if_l2com,
601 mac_ifnet_destroy(ifp);
603 IF_AFDATA_DESTROY(ifp);
604 IF_ADDR_LOCK_DESTROY(ifp);
605 ifq_delete(&ifp->if_snd);
607 for (int i = 0; i < IFCOUNTERS; i++)
608 counter_u64_free(ifp->if_counters[i]);
610 free(ifp->if_description, M_IFDESCR);
611 free(ifp->if_hw_addr, M_IFADDR);
616 if_destroy(epoch_context_t ctx)
620 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
621 if_free_internal(ifp);
625 * Deregister an interface and free the associated storage.
628 if_free(struct ifnet *ifp)
631 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
633 CURVNET_SET_QUIET(ifp->if_vnet);
635 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
636 ("%s: freeing unallocated ifnet", ifp->if_xname));
638 ifindex_free_locked(ifp->if_index);
641 if (refcount_release(&ifp->if_refcount))
642 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
647 * Interfaces to keep an ifnet type-stable despite the possibility of the
648 * driver calling if_free(). If there are additional references, we defer
649 * freeing the underlying data structure.
652 if_ref(struct ifnet *ifp)
655 /* We don't assert the ifnet list lock here, but arguably should. */
656 refcount_acquire(&ifp->if_refcount);
660 if_rele(struct ifnet *ifp)
663 if (!refcount_release(&ifp->if_refcount))
665 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
669 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
672 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
674 if (ifq->ifq_maxlen == 0)
675 ifq->ifq_maxlen = ifqmaxlen;
678 ifq->altq_disc = NULL;
679 ifq->altq_flags &= ALTQF_CANTCHANGE;
680 ifq->altq_tbr = NULL;
685 ifq_delete(struct ifaltq *ifq)
687 mtx_destroy(&ifq->ifq_mtx);
691 * Perform generic interface initialization tasks and attach the interface
692 * to the list of "active" interfaces. If vmove flag is set on entry
693 * to if_attach_internal(), perform only a limited subset of initialization
694 * tasks, given that we are moving from one vnet to another an ifnet which
695 * has already been fully initialized.
697 * Note that if_detach_internal() removes group membership unconditionally
698 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
699 * Thus, when if_vmove() is applied to a cloned interface, group membership
700 * is lost while a cloned one always joins a group whose name is
701 * ifc->ifc_name. To recover this after if_detach_internal() and
702 * if_attach_internal(), the cloner should be specified to
703 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
704 * attempts to join a group whose name is ifc->ifc_name.
707 * - The decision to return void and thus require this function to
708 * succeed is questionable.
709 * - We should probably do more sanity checking. For instance we don't
710 * do anything to insure if_xname is unique or non-empty.
713 if_attach(struct ifnet *ifp)
716 if_attach_internal(ifp, 0, NULL);
720 * Compute the least common TSO limit.
723 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
726 * 1) If there is no limit currently, take the limit from
727 * the network adapter.
729 * 2) If the network adapter has a limit below the current
732 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
733 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
734 pmax->tsomaxbytes = ifp->if_hw_tsomax;
736 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
737 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
738 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
740 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
741 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
742 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
747 * Update TSO limit of a network adapter.
749 * Returns zero if no change. Else non-zero.
752 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
755 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
756 ifp->if_hw_tsomax = pmax->tsomaxbytes;
759 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
760 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
763 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
764 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
771 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
773 unsigned socksize, ifasize;
774 int namelen, masklen;
775 struct sockaddr_dl *sdl;
778 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
779 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
783 ifp->if_vnet = curvnet;
784 if (ifp->if_home_vnet == NULL)
785 ifp->if_home_vnet = curvnet;
788 if_addgroup(ifp, IFG_ALL);
790 /* Restore group membership for cloned interfaces. */
791 if (vmove && ifc != NULL)
792 if_clone_addgroup(ifp, ifc);
794 getmicrotime(&ifp->if_lastchange);
795 ifp->if_epoch = time_uptime;
797 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
798 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
799 ("transmit and qflush must both either be set or both be NULL"));
800 if (ifp->if_transmit == NULL) {
801 ifp->if_transmit = if_transmit;
802 ifp->if_qflush = if_qflush;
804 if (ifp->if_input == NULL)
805 ifp->if_input = if_input_default;
807 if (ifp->if_requestencap == NULL)
808 ifp->if_requestencap = if_requestencap_default;
812 mac_ifnet_create(ifp);
816 * Create a Link Level name for this device.
818 namelen = strlen(ifp->if_xname);
820 * Always save enough space for any possiable name so we
821 * can do a rename in place later.
823 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
824 socksize = masklen + ifp->if_addrlen;
825 if (socksize < sizeof(*sdl))
826 socksize = sizeof(*sdl);
827 socksize = roundup2(socksize, sizeof(long));
828 ifasize = sizeof(*ifa) + 2 * socksize;
829 ifa = ifa_alloc(ifasize, M_WAITOK);
830 sdl = (struct sockaddr_dl *)(ifa + 1);
831 sdl->sdl_len = socksize;
832 sdl->sdl_family = AF_LINK;
833 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
834 sdl->sdl_nlen = namelen;
835 sdl->sdl_index = ifp->if_index;
836 sdl->sdl_type = ifp->if_type;
839 ifa->ifa_rtrequest = link_rtrequest;
840 ifa->ifa_addr = (struct sockaddr *)sdl;
841 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
842 ifa->ifa_netmask = (struct sockaddr *)sdl;
843 sdl->sdl_len = masklen;
845 sdl->sdl_data[--namelen] = 0xff;
846 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
847 /* Reliably crash if used uninitialized. */
848 ifp->if_broadcastaddr = NULL;
850 if (ifp->if_type == IFT_ETHER) {
851 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
855 #if defined(INET) || defined(INET6)
856 /* Use defaults for TSO, if nothing is set */
857 if (ifp->if_hw_tsomax == 0 &&
858 ifp->if_hw_tsomaxsegcount == 0 &&
859 ifp->if_hw_tsomaxsegsize == 0) {
861 * The TSO defaults needs to be such that an
862 * NFS mbuf list of 35 mbufs totalling just
863 * below 64K works and that a chain of mbufs
864 * can be defragged into at most 32 segments:
866 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
867 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
868 ifp->if_hw_tsomaxsegcount = 35;
869 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
871 /* XXX some drivers set IFCAP_TSO after ethernet attach */
872 if (ifp->if_capabilities & IFCAP_TSO) {
873 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
875 ifp->if_hw_tsomaxsegcount,
876 ifp->if_hw_tsomaxsegsize);
884 * Update the interface index in the link layer address
887 for (ifa = ifp->if_addr; ifa != NULL;
888 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
889 if (ifa->ifa_addr->sa_family == AF_LINK) {
890 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
891 sdl->sdl_index = ifp->if_index;
898 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
900 curvnet->vnet_ifcnt++;
904 if (domain_init_status >= 2)
905 if_attachdomain1(ifp);
907 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
908 if (IS_DEFAULT_VNET(curvnet))
909 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
911 /* Announce the interface. */
912 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
916 if_epochalloc(void *dummy __unused)
919 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
920 net_epoch = epoch_alloc(0);
922 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
923 if_epochalloc, NULL);
926 if_attachdomain(void *dummy)
930 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
931 if_attachdomain1(ifp);
933 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
934 if_attachdomain, NULL);
937 if_attachdomain1(struct ifnet *ifp)
942 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
943 * cannot lock ifp->if_afdata initialization, entirely.
946 if (ifp->if_afdata_initialized >= domain_init_status) {
947 IF_AFDATA_UNLOCK(ifp);
948 log(LOG_WARNING, "%s called more than once on %s\n",
949 __func__, ifp->if_xname);
952 ifp->if_afdata_initialized = domain_init_status;
953 IF_AFDATA_UNLOCK(ifp);
955 /* address family dependent data region */
956 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
957 for (dp = domains; dp; dp = dp->dom_next) {
958 if (dp->dom_ifattach)
959 ifp->if_afdata[dp->dom_family] =
960 (*dp->dom_ifattach)(ifp);
965 * Remove any unicast or broadcast network addresses from an interface.
968 if_purgeaddrs(struct ifnet *ifp)
974 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
975 if (ifa->ifa_addr->sa_family != AF_LINK)
983 /* XXX: Ugly!! ad hoc just for INET */
984 if (ifa->ifa_addr->sa_family == AF_INET) {
985 struct ifaliasreq ifr;
987 bzero(&ifr, sizeof(ifr));
988 ifr.ifra_addr = *ifa->ifa_addr;
989 if (ifa->ifa_dstaddr)
990 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
991 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
997 if (ifa->ifa_addr->sa_family == AF_INET6) {
999 /* ifp_addrhead is already updated */
1004 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1005 IF_ADDR_WUNLOCK(ifp);
1011 * Remove any multicast network addresses from an interface when an ifnet
1015 if_purgemaddrs(struct ifnet *ifp)
1017 struct ifmultiaddr *ifma;
1020 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1021 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1022 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1023 if_delmulti_locked(ifp, ifma, 1);
1025 IF_ADDR_WUNLOCK(ifp);
1029 * Detach an interface, removing it from the list of "active" interfaces.
1030 * If vmove flag is set on entry to if_detach_internal(), perform only a
1031 * limited subset of cleanup tasks, given that we are moving an ifnet from
1032 * one vnet to another, where it must be fully operational.
1034 * XXXRW: There are some significant questions about event ordering, and
1035 * how to prevent things from starting to use the interface during detach.
1038 if_detach(struct ifnet *ifp)
1041 CURVNET_SET_QUIET(ifp->if_vnet);
1042 if_detach_internal(ifp, 0, NULL);
1047 * The vmove flag, if set, indicates that we are called from a callpath
1048 * that is moving an interface to a different vnet instance.
1050 * The shutdown flag, if set, indicates that we are called in the
1051 * process of shutting down a vnet instance. Currently only the
1052 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1053 * on a vnet instance shutdown without this flag being set, e.g., when
1054 * the cloned interfaces are destoyed as first thing of teardown.
1057 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1067 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1068 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1071 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1073 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1075 ifp->if_flags |= IFF_DYING;
1082 * While we would want to panic here, we cannot
1083 * guarantee that the interface is indeed still on
1084 * the list given we don't hold locks all the way.
1089 panic("%s: ifp=%p not on the ifnet tailq %p",
1090 __func__, ifp, &V_ifnet);
1092 return; /* XXX this should panic as well? */
1097 * At this point we know the interface still was on the ifnet list
1098 * and we removed it so we are in a stable state.
1101 curvnet->vnet_ifcnt--;
1103 epoch_wait_preempt(net_epoch_preempt);
1106 * Ensure all pending EPOCH(9) callbacks have been executed. This
1107 * fixes issues about late destruction of multicast options
1108 * which lead to leave group calls, which in turn access the
1109 * belonging ifnet structure:
1111 epoch_drain_callbacks(net_epoch_preempt);
1114 * In any case (destroy or vmove) detach us from the groups
1115 * and remove/wait for pending events on the taskq.
1116 * XXX-BZ in theory an interface could still enqueue a taskq change?
1120 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1123 * Check if this is a cloned interface or not. Must do even if
1124 * shutting down as a if_vmove_reclaim() would move the ifp and
1125 * the if_clone_addgroup() will have a corrupted string overwise
1126 * from a gibberish pointer.
1128 if (vmove && ifcp != NULL)
1129 *ifcp = if_clone_findifc(ifp);
1135 * On VNET shutdown abort here as the stack teardown will do all
1136 * the work top-down for us.
1139 /* Give interface users the chance to clean up. */
1140 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1143 * In case of a vmove we are done here without error.
1144 * If we would signal an error it would lead to the same
1145 * abort as if we did not find the ifnet anymore.
1146 * if_detach() calls us in void context and does not care
1147 * about an early abort notification, so life is splendid :)
1149 goto finish_vnet_shutdown;
1154 * At this point we are not tearing down a VNET and are either
1155 * going to destroy or vmove the interface and have to cleanup
1160 * Remove routes and flush queues.
1163 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1164 altq_disable(&ifp->if_snd);
1165 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1166 altq_detach(&ifp->if_snd);
1177 * Remove all IPv6 kernel structs related to ifp. This should be done
1178 * before removing routing entries below, since IPv6 interface direct
1179 * routes are expected to be removed by the IPv6-specific kernel API.
1180 * Otherwise, the kernel will detect some inconsistency and bark it.
1184 if_purgemaddrs(ifp);
1186 /* Announce that the interface is gone. */
1187 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1188 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1189 if (IS_DEFAULT_VNET(curvnet))
1190 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1194 * Prevent further calls into the device driver via ifnet.
1199 * Clean up all addresses.
1202 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1203 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1204 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1205 IF_ADDR_WUNLOCK(ifp);
1208 IF_ADDR_WUNLOCK(ifp);
1211 rt_flushifroutes(ifp);
1214 finish_vnet_shutdown:
1217 * We cannot hold the lock over dom_ifdetach calls as they might
1218 * sleep, for example trying to drain a callout, thus open up the
1219 * theoretical race with re-attaching.
1221 IF_AFDATA_LOCK(ifp);
1222 i = ifp->if_afdata_initialized;
1223 ifp->if_afdata_initialized = 0;
1224 IF_AFDATA_UNLOCK(ifp);
1225 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1226 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1227 (*dp->dom_ifdetach)(ifp,
1228 ifp->if_afdata[dp->dom_family]);
1229 ifp->if_afdata[dp->dom_family] = NULL;
1238 * if_vmove() performs a limited version of if_detach() in current
1239 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1240 * An attempt is made to shrink if_index in current vnet, find an
1241 * unused if_index in target vnet and calls if_grow() if necessary,
1242 * and finally find an unused if_xname for the target vnet.
1245 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1247 struct if_clone *ifc;
1249 u_int bif_dlt, bif_hdrlen;
1256 * if_detach_internal() will call the eventhandler to notify
1257 * interface departure. That will detach if_bpf. We need to
1258 * safe the dlt and hdrlen so we can re-attach it later.
1260 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1264 * Detach from current vnet, but preserve LLADDR info, do not
1265 * mark as dead etc. so that the ifnet can be reattached later.
1266 * If we cannot find it, we lost the race to someone else.
1268 rc = if_detach_internal(ifp, 1, &ifc);
1273 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1274 * the if_index for that vnet if possible.
1276 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1277 * or we'd lock on one vnet and unlock on another.
1280 ifindex_free_locked(ifp->if_index);
1284 * Perform interface-specific reassignment tasks, if provided by
1287 if (ifp->if_reassign != NULL)
1288 ifp->if_reassign(ifp, new_vnet, NULL);
1291 * Switch to the context of the target vnet.
1293 CURVNET_SET_QUIET(new_vnet);
1296 ifp->if_index = ifindex_alloc(&old);
1297 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1299 epoch_wait_preempt(net_epoch_preempt);
1303 ifnet_setbyindex(ifp->if_index, ifp);
1306 if_attach_internal(ifp, 1, ifc);
1309 if (ifp->if_bpf == NULL)
1310 bpfattach(ifp, bif_dlt, bif_hdrlen);
1317 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1320 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1326 /* Try to find the prison within our visibility. */
1327 sx_slock(&allprison_lock);
1328 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1329 sx_sunlock(&allprison_lock);
1332 prison_hold_locked(pr);
1333 mtx_unlock(&pr->pr_mtx);
1335 /* Do not try to move the iface from and to the same prison. */
1336 if (pr->pr_vnet == ifp->if_vnet) {
1341 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1342 /* XXX Lock interfaces to avoid races. */
1343 CURVNET_SET_QUIET(pr->pr_vnet);
1344 difp = ifunit(ifname);
1351 /* Make sure the VNET is stable. */
1352 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1353 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1361 /* Move the interface into the child jail/vnet. */
1362 if_vmove(ifp, pr->pr_vnet);
1364 /* Report the new if_xname back to the userland. */
1365 sprintf(ifname, "%s", ifp->if_xname);
1372 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1375 struct vnet *vnet_dst;
1379 /* Try to find the prison within our visibility. */
1380 sx_slock(&allprison_lock);
1381 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1382 sx_sunlock(&allprison_lock);
1385 prison_hold_locked(pr);
1386 mtx_unlock(&pr->pr_mtx);
1388 /* Make sure the named iface exists in the source prison/vnet. */
1389 CURVNET_SET(pr->pr_vnet);
1390 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1397 /* Do not try to move the iface from and to the same prison. */
1398 vnet_dst = TD_TO_VNET(td);
1399 if (vnet_dst == ifp->if_vnet) {
1405 /* Make sure the VNET is stable. */
1406 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1407 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1414 /* Get interface back from child jail/vnet. */
1415 if_vmove(ifp, vnet_dst);
1418 /* Report the new if_xname back to the userland. */
1419 sprintf(ifname, "%s", ifp->if_xname);
1427 * Add a group to an interface
1430 if_addgroup(struct ifnet *ifp, const char *groupname)
1432 struct ifg_list *ifgl;
1433 struct ifg_group *ifg = NULL;
1434 struct ifg_member *ifgm;
1437 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1438 groupname[strlen(groupname) - 1] <= '9')
1442 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1443 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1448 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1453 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1459 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1460 if (!strcmp(ifg->ifg_group, groupname))
1464 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1470 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1471 ifg->ifg_refcnt = 0;
1472 CK_STAILQ_INIT(&ifg->ifg_members);
1473 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1478 ifgl->ifgl_group = ifg;
1479 ifgm->ifgm_ifp = ifp;
1482 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1483 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1484 IF_ADDR_WUNLOCK(ifp);
1489 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1490 EVENTHANDLER_INVOKE(group_change_event, groupname);
1496 * Helper function to remove a group out of an interface. Expects the global
1497 * ifnet lock to be write-locked, and drops it before returning.
1500 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1501 const char *groupname)
1503 struct ifg_member *ifgm;
1506 IFNET_WLOCK_ASSERT();
1509 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1510 IF_ADDR_WUNLOCK(ifp);
1512 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1513 if (ifgm->ifgm_ifp == ifp) {
1514 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1515 ifg_member, ifgm_next);
1520 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1521 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1529 epoch_wait_preempt(net_epoch_preempt);
1531 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1532 free(ifgl->ifgl_group, M_TEMP);
1537 EVENTHANDLER_INVOKE(group_change_event, groupname);
1541 * Remove a group from an interface
1544 if_delgroup(struct ifnet *ifp, const char *groupname)
1546 struct ifg_list *ifgl;
1549 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1550 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1557 _if_delgroup_locked(ifp, ifgl, groupname);
1563 * Remove an interface from all groups
1566 if_delgroups(struct ifnet *ifp)
1568 struct ifg_list *ifgl;
1569 char groupname[IFNAMSIZ];
1572 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1573 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1574 _if_delgroup_locked(ifp, ifgl, groupname);
1581 ifgr_group_get(void *ifgrp)
1583 union ifgroupreq_union *ifgrup;
1586 #ifdef COMPAT_FREEBSD32
1587 if (SV_CURPROC_FLAG(SV_ILP32))
1588 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1590 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1593 static struct ifg_req *
1594 ifgr_groups_get(void *ifgrp)
1596 union ifgroupreq_union *ifgrup;
1599 #ifdef COMPAT_FREEBSD32
1600 if (SV_CURPROC_FLAG(SV_ILP32))
1601 return ((struct ifg_req *)(uintptr_t)
1602 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1604 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1608 * Stores all groups from an interface in memory pointed to by ifgr.
1611 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1614 struct ifg_list *ifgl;
1615 struct ifg_req ifgrq, *ifgp;
1617 if (ifgr->ifgr_len == 0) {
1619 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1620 ifgr->ifgr_len += sizeof(struct ifg_req);
1621 IF_ADDR_RUNLOCK(ifp);
1625 len = ifgr->ifgr_len;
1626 ifgp = ifgr_groups_get(ifgr);
1629 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1630 if (len < sizeof(ifgrq)) {
1631 IF_ADDR_RUNLOCK(ifp);
1634 bzero(&ifgrq, sizeof ifgrq);
1635 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1636 sizeof(ifgrq.ifgrq_group));
1637 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1638 IF_ADDR_RUNLOCK(ifp);
1641 len -= sizeof(ifgrq);
1644 IF_ADDR_RUNLOCK(ifp);
1650 * Stores all members of a group in memory pointed to by igfr
1653 if_getgroupmembers(struct ifgroupreq *ifgr)
1655 struct ifg_group *ifg;
1656 struct ifg_member *ifgm;
1657 struct ifg_req ifgrq, *ifgp;
1661 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1662 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1669 if (ifgr->ifgr_len == 0) {
1670 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1671 ifgr->ifgr_len += sizeof(ifgrq);
1676 len = ifgr->ifgr_len;
1677 ifgp = ifgr_groups_get(ifgr);
1678 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1679 if (len < sizeof(ifgrq)) {
1683 bzero(&ifgrq, sizeof ifgrq);
1684 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1685 sizeof(ifgrq.ifgrq_member));
1686 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1690 len -= sizeof(ifgrq);
1699 * Return counter values from counter(9)s stored in ifnet.
1702 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1705 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1707 return (counter_u64_fetch(ifp->if_counters[cnt]));
1711 * Increase an ifnet counter. Usually used for counters shared
1712 * between the stack and a driver, but function supports them all.
1715 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1718 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1720 counter_u64_add(ifp->if_counters[cnt], inc);
1724 * Copy data from ifnet to userland API structure if_data.
1727 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1730 ifd->ifi_type = ifp->if_type;
1731 ifd->ifi_physical = 0;
1732 ifd->ifi_addrlen = ifp->if_addrlen;
1733 ifd->ifi_hdrlen = ifp->if_hdrlen;
1734 ifd->ifi_link_state = ifp->if_link_state;
1736 ifd->ifi_datalen = sizeof(struct if_data);
1737 ifd->ifi_mtu = ifp->if_mtu;
1738 ifd->ifi_metric = ifp->if_metric;
1739 ifd->ifi_baudrate = ifp->if_baudrate;
1740 ifd->ifi_hwassist = ifp->if_hwassist;
1741 ifd->ifi_epoch = ifp->if_epoch;
1742 ifd->ifi_lastchange = ifp->if_lastchange;
1744 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1745 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1746 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1747 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1748 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1749 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1750 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1751 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1752 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1753 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1754 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1755 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1758 struct ifnet_read_lock {
1759 struct mtx mtx; /* lock protecting tracker below */
1760 struct epoch_tracker et;
1763 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_addr_read_lock);
1764 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_maddr_read_lock);
1767 ifnet_read_lock_init(void __unused *arg)
1769 struct ifnet_read_lock *pifrl;
1773 pifrl = DPCPU_ID_PTR(cpu, ifnet_addr_read_lock);
1774 mtx_init(&pifrl->mtx, "ifnet_addr_read_lock", NULL, MTX_DEF);
1776 pifrl = DPCPU_ID_PTR(cpu, ifnet_maddr_read_lock);
1777 mtx_init(&pifrl->mtx, "ifnet_maddr_read_lock", NULL, MTX_DEF);
1780 SYSINIT(ifnet_read_lock_init, SI_SUB_CPU + 1, SI_ORDER_FIRST, &ifnet_read_lock_init, NULL);
1783 * Wrapper functions for struct ifnet address list locking macros. These are
1784 * used by kernel modules to avoid encoding programming interface or binary
1785 * interface assumptions that may be violated when kernel-internal locking
1786 * approaches change.
1789 if_addr_rlock(struct ifnet *ifp)
1791 struct ifnet_read_lock *pifrl;
1794 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1795 mtx_lock(&pifrl->mtx);
1796 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1800 if_addr_runlock(struct ifnet *ifp)
1802 struct ifnet_read_lock *pifrl;
1804 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1806 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1807 mtx_unlock(&pifrl->mtx);
1812 if_maddr_rlock(if_t ifp)
1814 struct ifnet_read_lock *pifrl;
1817 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1818 mtx_lock(&pifrl->mtx);
1819 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1823 if_maddr_runlock(if_t ifp)
1825 struct ifnet_read_lock *pifrl;
1827 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1829 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1830 mtx_unlock(&pifrl->mtx);
1835 * Initialization, destruction and refcounting functions for ifaddrs.
1838 ifa_alloc(size_t size, int flags)
1842 KASSERT(size >= sizeof(struct ifaddr),
1843 ("%s: invalid size %zu", __func__, size));
1845 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1849 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1851 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1853 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1855 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1858 refcount_init(&ifa->ifa_refcnt, 1);
1863 /* free(NULL) is okay */
1864 counter_u64_free(ifa->ifa_opackets);
1865 counter_u64_free(ifa->ifa_ipackets);
1866 counter_u64_free(ifa->ifa_obytes);
1867 counter_u64_free(ifa->ifa_ibytes);
1868 free(ifa, M_IFADDR);
1874 ifa_ref(struct ifaddr *ifa)
1877 refcount_acquire(&ifa->ifa_refcnt);
1881 ifa_destroy(epoch_context_t ctx)
1885 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1886 counter_u64_free(ifa->ifa_opackets);
1887 counter_u64_free(ifa->ifa_ipackets);
1888 counter_u64_free(ifa->ifa_obytes);
1889 counter_u64_free(ifa->ifa_ibytes);
1890 free(ifa, M_IFADDR);
1894 ifa_free(struct ifaddr *ifa)
1897 if (refcount_release(&ifa->ifa_refcnt))
1898 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1903 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1904 struct sockaddr *ia)
1907 struct rt_addrinfo info;
1908 struct sockaddr_dl null_sdl;
1913 bzero(&info, sizeof(info));
1914 if (cmd != RTM_DELETE)
1915 info.rti_ifp = V_loif;
1916 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1917 info.rti_info[RTAX_DST] = ia;
1918 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1919 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1921 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1924 (cmd == RTM_ADD && error == EEXIST) ||
1925 (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
1928 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1929 __func__, otype, if_name(ifp), error);
1935 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1938 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1942 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1945 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1949 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1952 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1956 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1957 * structs used to represent other address families, it is necessary
1958 * to perform a different comparison.
1961 #define sa_dl_equal(a1, a2) \
1962 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1963 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1964 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1965 CLLADDR((const struct sockaddr_dl *)(a2)), \
1966 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1969 * Locate an interface based on a complete address.
1973 ifa_ifwithaddr(const struct sockaddr *addr)
1978 MPASS(in_epoch(net_epoch_preempt));
1979 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1980 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1981 if (ifa->ifa_addr->sa_family != addr->sa_family)
1983 if (sa_equal(addr, ifa->ifa_addr)) {
1986 /* IP6 doesn't have broadcast */
1987 if ((ifp->if_flags & IFF_BROADCAST) &&
1988 ifa->ifa_broadaddr &&
1989 ifa->ifa_broadaddr->sa_len != 0 &&
1990 sa_equal(ifa->ifa_broadaddr, addr)) {
2001 ifa_ifwithaddr_check(const struct sockaddr *addr)
2006 rc = (ifa_ifwithaddr(addr) != NULL);
2012 * Locate an interface based on the broadcast address.
2016 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
2021 MPASS(in_epoch(net_epoch_preempt));
2022 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2023 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2025 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2026 if (ifa->ifa_addr->sa_family != addr->sa_family)
2028 if ((ifp->if_flags & IFF_BROADCAST) &&
2029 ifa->ifa_broadaddr &&
2030 ifa->ifa_broadaddr->sa_len != 0 &&
2031 sa_equal(ifa->ifa_broadaddr, addr)) {
2042 * Locate the point to point interface with a given destination address.
2046 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2051 MPASS(in_epoch(net_epoch_preempt));
2052 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2053 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2055 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2057 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2058 if (ifa->ifa_addr->sa_family != addr->sa_family)
2060 if (ifa->ifa_dstaddr != NULL &&
2061 sa_equal(addr, ifa->ifa_dstaddr)) {
2072 * Find an interface on a specific network. If many, choice
2073 * is most specific found.
2076 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2080 struct ifaddr *ifa_maybe = NULL;
2081 u_int af = addr->sa_family;
2082 const char *addr_data = addr->sa_data, *cplim;
2084 MPASS(in_epoch(net_epoch_preempt));
2086 * AF_LINK addresses can be looked up directly by their index number,
2087 * so do that if we can.
2089 if (af == AF_LINK) {
2090 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2091 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2092 return (ifaddr_byindex(sdl->sdl_index));
2096 * Scan though each interface, looking for ones that have addresses
2097 * in this address family and the requested fib. Maintain a reference
2098 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2099 * kept it stable when we move onto the next interface.
2101 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2102 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2104 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2105 const char *cp, *cp2, *cp3;
2107 if (ifa->ifa_addr->sa_family != af)
2109 if (af == AF_INET &&
2110 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2112 * This is a bit broken as it doesn't
2113 * take into account that the remote end may
2114 * be a single node in the network we are
2116 * The trouble is that we don't know the
2117 * netmask for the remote end.
2119 if (ifa->ifa_dstaddr != NULL &&
2120 sa_equal(addr, ifa->ifa_dstaddr)) {
2125 * Scan all the bits in the ifa's address.
2126 * If a bit dissagrees with what we are
2127 * looking for, mask it with the netmask
2128 * to see if it really matters.
2129 * (A byte at a time)
2131 if (ifa->ifa_netmask == 0)
2134 cp2 = ifa->ifa_addr->sa_data;
2135 cp3 = ifa->ifa_netmask->sa_data;
2136 cplim = ifa->ifa_netmask->sa_len
2137 + (char *)ifa->ifa_netmask;
2139 if ((*cp++ ^ *cp2++) & *cp3++)
2140 goto next; /* next address! */
2142 * If the netmask of what we just found
2143 * is more specific than what we had before
2144 * (if we had one), or if the virtual status
2145 * of new prefix is better than of the old one,
2146 * then remember the new one before continuing
2147 * to search for an even better one.
2149 if (ifa_maybe == NULL ||
2150 ifa_preferred(ifa_maybe, ifa) ||
2151 rn_refines((caddr_t)ifa->ifa_netmask,
2152 (caddr_t)ifa_maybe->ifa_netmask)) {
2165 * Find an interface address specific to an interface best matching
2169 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2172 const char *cp, *cp2, *cp3;
2174 struct ifaddr *ifa_maybe = NULL;
2175 u_int af = addr->sa_family;
2180 MPASS(in_epoch(net_epoch_preempt));
2181 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2182 if (ifa->ifa_addr->sa_family != af)
2184 if (ifa_maybe == NULL)
2186 if (ifa->ifa_netmask == 0) {
2187 if (sa_equal(addr, ifa->ifa_addr) ||
2188 (ifa->ifa_dstaddr &&
2189 sa_equal(addr, ifa->ifa_dstaddr)))
2193 if (ifp->if_flags & IFF_POINTOPOINT) {
2194 if (sa_equal(addr, ifa->ifa_dstaddr))
2198 cp2 = ifa->ifa_addr->sa_data;
2199 cp3 = ifa->ifa_netmask->sa_data;
2200 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2201 for (; cp3 < cplim; cp3++)
2202 if ((*cp++ ^ *cp2++) & *cp3)
2214 * See whether new ifa is better than current one:
2215 * 1) A non-virtual one is preferred over virtual.
2216 * 2) A virtual in master state preferred over any other state.
2218 * Used in several address selecting functions.
2221 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2224 return (cur->ifa_carp && (!next->ifa_carp ||
2225 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2228 #include <net/if_llatbl.h>
2231 * Default action when installing a route with a Link Level gateway.
2232 * Lookup an appropriate real ifa to point to.
2233 * This should be moved to /sys/net/link.c eventually.
2236 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2238 struct ifaddr *ifa, *oifa;
2239 struct sockaddr *dst;
2242 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2243 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2246 ifa = ifaof_ifpforaddr(dst, ifp);
2254 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2255 ifa->ifa_rtrequest(cmd, rt, info);
2260 struct sockaddr_dl *
2261 link_alloc_sdl(size_t size, int flags)
2264 return (malloc(size, M_TEMP, flags));
2268 link_free_sdl(struct sockaddr *sa)
2274 * Fills in given sdl with interface basic info.
2275 * Returns pointer to filled sdl.
2277 struct sockaddr_dl *
2278 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2280 struct sockaddr_dl *sdl;
2282 sdl = (struct sockaddr_dl *)paddr;
2283 memset(sdl, 0, sizeof(struct sockaddr_dl));
2284 sdl->sdl_len = sizeof(struct sockaddr_dl);
2285 sdl->sdl_family = AF_LINK;
2286 sdl->sdl_index = ifp->if_index;
2287 sdl->sdl_type = iftype;
2293 * Mark an interface down and notify protocols of
2297 if_unroute(struct ifnet *ifp, int flag, int fam)
2301 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2303 ifp->if_flags &= ~flag;
2304 getmicrotime(&ifp->if_lastchange);
2305 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2306 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2307 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2308 ifp->if_qflush(ifp);
2311 (*carp_linkstate_p)(ifp);
2316 * Mark an interface up and notify protocols of
2320 if_route(struct ifnet *ifp, int flag, int fam)
2324 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2326 ifp->if_flags |= flag;
2327 getmicrotime(&ifp->if_lastchange);
2328 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2329 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2330 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2332 (*carp_linkstate_p)(ifp);
2339 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2340 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2341 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2342 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2343 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2344 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2345 int (*vlan_setcookie_p)(struct ifnet *, void *);
2346 void *(*vlan_cookie_p)(struct ifnet *);
2349 * Handle a change in the interface link state. To avoid LORs
2350 * between driver lock and upper layer locks, as well as possible
2351 * recursions, we post event to taskqueue, and all job
2352 * is done in static do_link_state_change().
2355 if_link_state_change(struct ifnet *ifp, int link_state)
2357 /* Return if state hasn't changed. */
2358 if (ifp->if_link_state == link_state)
2361 ifp->if_link_state = link_state;
2363 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2367 do_link_state_change(void *arg, int pending)
2369 struct ifnet *ifp = (struct ifnet *)arg;
2370 int link_state = ifp->if_link_state;
2371 CURVNET_SET(ifp->if_vnet);
2373 /* Notify that the link state has changed. */
2375 if (ifp->if_vlantrunk != NULL)
2376 (*vlan_link_state_p)(ifp);
2378 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2379 ifp->if_l2com != NULL)
2380 (*ng_ether_link_state_p)(ifp, link_state);
2382 (*carp_linkstate_p)(ifp);
2384 ifp->if_bridge_linkstate(ifp);
2386 (*lagg_linkstate_p)(ifp, link_state);
2388 if (IS_DEFAULT_VNET(curvnet))
2389 devctl_notify("IFNET", ifp->if_xname,
2390 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2393 if_printf(ifp, "%d link states coalesced\n", pending);
2394 if (log_link_state_change)
2395 if_printf(ifp, "link state changed to %s\n",
2396 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2397 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2402 * Mark an interface down and notify protocols of
2406 if_down(struct ifnet *ifp)
2409 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2410 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2414 * Mark an interface up and notify protocols of
2418 if_up(struct ifnet *ifp)
2421 if_route(ifp, IFF_UP, AF_UNSPEC);
2422 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2426 * Flush an interface queue.
2429 if_qflush(struct ifnet *ifp)
2437 if (ALTQ_IS_ENABLED(ifq))
2441 while ((m = n) != NULL) {
2452 * Map interface name to interface structure pointer, with or without
2453 * returning a reference.
2456 ifunit_ref(const char *name)
2460 IFNET_RLOCK_NOSLEEP();
2461 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2462 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2463 !(ifp->if_flags & IFF_DYING))
2468 IFNET_RUNLOCK_NOSLEEP();
2473 ifunit(const char *name)
2477 IFNET_RLOCK_NOSLEEP();
2478 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2479 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2482 IFNET_RUNLOCK_NOSLEEP();
2487 ifr_buffer_get_buffer(void *data)
2489 union ifreq_union *ifrup;
2492 #ifdef COMPAT_FREEBSD32
2493 if (SV_CURPROC_FLAG(SV_ILP32))
2494 return ((void *)(uintptr_t)
2495 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2497 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2501 ifr_buffer_set_buffer_null(void *data)
2503 union ifreq_union *ifrup;
2506 #ifdef COMPAT_FREEBSD32
2507 if (SV_CURPROC_FLAG(SV_ILP32))
2508 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2511 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2515 ifr_buffer_get_length(void *data)
2517 union ifreq_union *ifrup;
2520 #ifdef COMPAT_FREEBSD32
2521 if (SV_CURPROC_FLAG(SV_ILP32))
2522 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2524 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2528 ifr_buffer_set_length(void *data, size_t len)
2530 union ifreq_union *ifrup;
2533 #ifdef COMPAT_FREEBSD32
2534 if (SV_CURPROC_FLAG(SV_ILP32))
2535 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2538 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2542 ifr_data_get_ptr(void *ifrp)
2544 union ifreq_union *ifrup;
2547 #ifdef COMPAT_FREEBSD32
2548 if (SV_CURPROC_FLAG(SV_ILP32))
2549 return ((void *)(uintptr_t)
2550 ifrup->ifr32.ifr_ifru.ifru_data);
2552 return (ifrup->ifr.ifr_ifru.ifru_data);
2556 * Hardware specific interface ioctls.
2559 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2562 int error = 0, do_ifup = 0;
2563 int new_flags, temp_flags;
2564 size_t namelen, onamelen;
2566 char *descrbuf, *odescrbuf;
2567 char new_name[IFNAMSIZ];
2569 struct sockaddr_dl *sdl;
2571 ifr = (struct ifreq *)data;
2574 ifr->ifr_index = ifp->if_index;
2578 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2579 ifr->ifr_flags = temp_flags & 0xffff;
2580 ifr->ifr_flagshigh = temp_flags >> 16;
2584 ifr->ifr_reqcap = ifp->if_capabilities;
2585 ifr->ifr_curcap = ifp->if_capenable;
2590 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2595 ifr->ifr_metric = ifp->if_metric;
2599 ifr->ifr_mtu = ifp->if_mtu;
2603 /* XXXGL: did this ever worked? */
2609 sx_slock(&ifdescr_sx);
2610 if (ifp->if_description == NULL)
2613 /* space for terminating nul */
2614 descrlen = strlen(ifp->if_description) + 1;
2615 if (ifr_buffer_get_length(ifr) < descrlen)
2616 ifr_buffer_set_buffer_null(ifr);
2618 error = copyout(ifp->if_description,
2619 ifr_buffer_get_buffer(ifr), descrlen);
2620 ifr_buffer_set_length(ifr, descrlen);
2622 sx_sunlock(&ifdescr_sx);
2626 error = priv_check(td, PRIV_NET_SETIFDESCR);
2631 * Copy only (length-1) bytes to make sure that
2632 * if_description is always nul terminated. The
2633 * length parameter is supposed to count the
2634 * terminating nul in.
2636 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2637 return (ENAMETOOLONG);
2638 else if (ifr_buffer_get_length(ifr) == 0)
2641 descrbuf = malloc(ifr_buffer_get_length(ifr),
2642 M_IFDESCR, M_WAITOK | M_ZERO);
2643 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2644 ifr_buffer_get_length(ifr) - 1);
2646 free(descrbuf, M_IFDESCR);
2651 sx_xlock(&ifdescr_sx);
2652 odescrbuf = ifp->if_description;
2653 ifp->if_description = descrbuf;
2654 sx_xunlock(&ifdescr_sx);
2656 getmicrotime(&ifp->if_lastchange);
2657 free(odescrbuf, M_IFDESCR);
2661 ifr->ifr_fib = ifp->if_fib;
2665 error = priv_check(td, PRIV_NET_SETIFFIB);
2668 if (ifr->ifr_fib >= rt_numfibs)
2671 ifp->if_fib = ifr->ifr_fib;
2675 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2679 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2680 * check, so we don't need special handling here yet.
2682 new_flags = (ifr->ifr_flags & 0xffff) |
2683 (ifr->ifr_flagshigh << 16);
2684 if (ifp->if_flags & IFF_UP &&
2685 (new_flags & IFF_UP) == 0) {
2687 } else if (new_flags & IFF_UP &&
2688 (ifp->if_flags & IFF_UP) == 0) {
2691 /* See if permanently promiscuous mode bit is about to flip */
2692 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2693 if (new_flags & IFF_PPROMISC)
2694 ifp->if_flags |= IFF_PROMISC;
2695 else if (ifp->if_pcount == 0)
2696 ifp->if_flags &= ~IFF_PROMISC;
2697 if (log_promisc_mode_change)
2698 if_printf(ifp, "permanently promiscuous mode %s\n",
2699 ((new_flags & IFF_PPROMISC) ?
2700 "enabled" : "disabled"));
2702 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2703 (new_flags &~ IFF_CANTCHANGE);
2704 if (ifp->if_ioctl) {
2705 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2709 getmicrotime(&ifp->if_lastchange);
2713 error = priv_check(td, PRIV_NET_SETIFCAP);
2716 if (ifp->if_ioctl == NULL)
2717 return (EOPNOTSUPP);
2718 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2720 error = (*ifp->if_ioctl)(ifp, cmd, data);
2722 getmicrotime(&ifp->if_lastchange);
2727 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2732 error = priv_check(td, PRIV_NET_SETIFNAME);
2735 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2739 if (new_name[0] == '\0')
2741 if (new_name[IFNAMSIZ-1] != '\0') {
2742 new_name[IFNAMSIZ-1] = '\0';
2743 if (strlen(new_name) == IFNAMSIZ-1)
2746 if (strcmp(new_name, ifp->if_xname) == 0)
2748 if (ifunit(new_name) != NULL)
2752 * XXX: Locking. Nothing else seems to lock if_flags,
2753 * and there are numerous other races with the
2754 * ifunit() checks not being atomic with namespace
2755 * changes (renames, vmoves, if_attach, etc).
2757 ifp->if_flags |= IFF_RENAMING;
2759 /* Announce the departure of the interface. */
2760 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2761 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2763 if_printf(ifp, "changing name to '%s'\n", new_name);
2766 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2768 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2769 namelen = strlen(new_name);
2770 onamelen = sdl->sdl_nlen;
2772 * Move the address if needed. This is safe because we
2773 * allocate space for a name of length IFNAMSIZ when we
2774 * create this in if_attach().
2776 if (namelen != onamelen) {
2777 bcopy(sdl->sdl_data + onamelen,
2778 sdl->sdl_data + namelen, sdl->sdl_alen);
2780 bcopy(new_name, sdl->sdl_data, namelen);
2781 sdl->sdl_nlen = namelen;
2782 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2783 bzero(sdl->sdl_data, onamelen);
2784 while (namelen != 0)
2785 sdl->sdl_data[--namelen] = 0xff;
2786 IF_ADDR_WUNLOCK(ifp);
2788 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2789 /* Announce the return of the interface. */
2790 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2792 ifp->if_flags &= ~IFF_RENAMING;
2797 error = priv_check(td, PRIV_NET_SETIFVNET);
2800 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2805 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2808 ifp->if_metric = ifr->ifr_metric;
2809 getmicrotime(&ifp->if_lastchange);
2813 error = priv_check(td, PRIV_NET_SETIFPHYS);
2816 if (ifp->if_ioctl == NULL)
2817 return (EOPNOTSUPP);
2818 error = (*ifp->if_ioctl)(ifp, cmd, data);
2820 getmicrotime(&ifp->if_lastchange);
2825 u_long oldmtu = ifp->if_mtu;
2827 error = priv_check(td, PRIV_NET_SETIFMTU);
2830 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2832 if (ifp->if_ioctl == NULL)
2833 return (EOPNOTSUPP);
2834 error = (*ifp->if_ioctl)(ifp, cmd, data);
2836 getmicrotime(&ifp->if_lastchange);
2839 NETDUMP_REINIT(ifp);
2843 * If the link MTU changed, do network layer specific procedure.
2845 if (ifp->if_mtu != oldmtu) {
2856 if (cmd == SIOCADDMULTI)
2857 error = priv_check(td, PRIV_NET_ADDMULTI);
2859 error = priv_check(td, PRIV_NET_DELMULTI);
2863 /* Don't allow group membership on non-multicast interfaces. */
2864 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2865 return (EOPNOTSUPP);
2867 /* Don't let users screw up protocols' entries. */
2868 if (ifr->ifr_addr.sa_family != AF_LINK)
2871 if (cmd == SIOCADDMULTI) {
2872 struct ifmultiaddr *ifma;
2875 * Userland is only permitted to join groups once
2876 * via the if_addmulti() KPI, because it cannot hold
2877 * struct ifmultiaddr * between calls. It may also
2878 * lose a race while we check if the membership
2882 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2883 IF_ADDR_RUNLOCK(ifp);
2887 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2889 error = if_delmulti(ifp, &ifr->ifr_addr);
2892 getmicrotime(&ifp->if_lastchange);
2895 case SIOCSIFPHYADDR:
2896 case SIOCDIFPHYADDR:
2898 case SIOCSIFPHYADDR_IN6:
2901 case SIOCSIFGENERIC:
2902 error = priv_check(td, PRIV_NET_HWIOCTL);
2905 if (ifp->if_ioctl == NULL)
2906 return (EOPNOTSUPP);
2907 error = (*ifp->if_ioctl)(ifp, cmd, data);
2909 getmicrotime(&ifp->if_lastchange);
2913 case SIOCGIFPSRCADDR:
2914 case SIOCGIFPDSTADDR:
2917 case SIOCGIFGENERIC:
2919 case SIOCGIFRSSHASH:
2920 case SIOCGIFDOWNREASON:
2921 if (ifp->if_ioctl == NULL)
2922 return (EOPNOTSUPP);
2923 error = (*ifp->if_ioctl)(ifp, cmd, data);
2927 error = priv_check(td, PRIV_NET_SETLLADDR);
2930 error = if_setlladdr(ifp,
2931 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2935 error = if_gethwaddr(ifp, ifr);
2938 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2939 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2942 if ((error = if_addgroup(ifp,
2943 ifgr_group_get((struct ifgroupreq *)data))))
2947 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2948 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2952 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2953 error = priv_check(td, PRIV_NET_DELIFGROUP);
2956 if ((error = if_delgroup(ifp,
2957 ifgr_group_get((struct ifgroupreq *)data))))
2968 #ifdef COMPAT_FREEBSD32
2976 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2979 #ifdef COMPAT_FREEBSD32
2981 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2983 struct ifmediareq32 *ifmr32;
2985 ifmr32 = (struct ifmediareq32 *)data;
2986 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2987 sizeof(ifmr->ifm_name));
2988 ifmr->ifm_current = ifmr32->ifm_current;
2989 ifmr->ifm_mask = ifmr32->ifm_mask;
2990 ifmr->ifm_status = ifmr32->ifm_status;
2991 ifmr->ifm_active = ifmr32->ifm_active;
2992 ifmr->ifm_count = ifmr32->ifm_count;
2993 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2997 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2999 struct ifmediareq32 *ifmr32;
3001 ifmr32 = (struct ifmediareq32 *)data;
3002 ifmr32->ifm_current = ifmr->ifm_current;
3003 ifmr32->ifm_mask = ifmr->ifm_mask;
3004 ifmr32->ifm_status = ifmr->ifm_status;
3005 ifmr32->ifm_active = ifmr->ifm_active;
3006 ifmr32->ifm_count = ifmr->ifm_count;
3014 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
3016 #ifdef COMPAT_FREEBSD32
3017 caddr_t saved_data = NULL;
3018 struct ifmediareq ifmr;
3019 struct ifmediareq *ifmrp;
3029 CURVNET_SET(so->so_vnet);
3031 /* Make sure the VNET is stable. */
3032 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
3033 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3043 error = ifconf(cmd, data);
3047 #ifdef COMPAT_FREEBSD32
3050 struct ifconf32 *ifc32;
3053 ifc32 = (struct ifconf32 *)data;
3054 ifc.ifc_len = ifc32->ifc_len;
3055 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3057 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3060 ifc32->ifc_len = ifc.ifc_len;
3066 #ifdef COMPAT_FREEBSD32
3069 case SIOCGIFMEDIA32:
3070 case SIOCGIFXMEDIA32:
3072 ifmr_init(ifmrp, data);
3073 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3075 data = (caddr_t)ifmrp;
3079 ifr = (struct ifreq *)data;
3083 error = priv_check(td, PRIV_NET_SETIFVNET);
3085 error = if_vmove_reclaim(td, ifr->ifr_name,
3091 error = priv_check(td, PRIV_NET_IFCREATE);
3093 error = if_clone_create(ifr->ifr_name,
3094 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3095 ifr_data_get_ptr(ifr) : NULL);
3098 error = priv_check(td, PRIV_NET_IFDESTROY);
3100 error = if_clone_destroy(ifr->ifr_name);
3103 case SIOCIFGCLONERS:
3104 error = if_clone_list((struct if_clonereq *)data);
3107 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3108 error = if_getgroupmembers((struct ifgroupreq *)data);
3111 #if defined(INET) || defined(INET6)
3114 if (carp_ioctl_p == NULL)
3115 error = EPROTONOSUPPORT;
3117 error = (*carp_ioctl_p)(ifr, cmd, td);
3122 ifp = ifunit_ref(ifr->ifr_name);
3128 error = ifhwioctl(cmd, ifp, data, td);
3129 if (error != ENOIOCTL)
3132 oif_flags = ifp->if_flags;
3133 if (so->so_proto == NULL) {
3139 * Pass the request on to the socket control method, and if the
3140 * latter returns EOPNOTSUPP, directly to the interface.
3142 * Make an exception for the legacy SIOCSIF* requests. Drivers
3143 * trust SIOCSIFADDR et al to come from an already privileged
3144 * layer, and do not perform any credentials checks or input
3147 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3149 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3150 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3151 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3152 error = (*ifp->if_ioctl)(ifp, cmd, data);
3154 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3156 if (ifp->if_flags & IFF_UP)
3164 #ifdef COMPAT_FREEBSD32
3165 if (ifmrp != NULL) {
3166 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3167 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3170 ifmr_update(ifmrp, data);
3178 * The code common to handling reference counted flags,
3179 * e.g., in ifpromisc() and if_allmulti().
3180 * The "pflag" argument can specify a permanent mode flag to check,
3181 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3183 * Only to be used on stack-owned flags, not driver-owned flags.
3186 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3190 int oldflags, oldcount;
3192 /* Sanity checks to catch programming errors */
3193 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3194 ("%s: setting driver-owned flag %d", __func__, flag));
3197 KASSERT(*refcount >= 0,
3198 ("%s: increment negative refcount %d for flag %d",
3199 __func__, *refcount, flag));
3201 KASSERT(*refcount > 0,
3202 ("%s: decrement non-positive refcount %d for flag %d",
3203 __func__, *refcount, flag));
3205 /* In case this mode is permanent, just touch refcount */
3206 if (ifp->if_flags & pflag) {
3207 *refcount += onswitch ? 1 : -1;
3211 /* Save ifnet parameters for if_ioctl() may fail */
3212 oldcount = *refcount;
3213 oldflags = ifp->if_flags;
3216 * See if we aren't the only and touching refcount is enough.
3217 * Actually toggle interface flag if we are the first or last.
3222 ifp->if_flags |= flag;
3226 ifp->if_flags &= ~flag;
3229 /* Call down the driver since we've changed interface flags */
3230 if (ifp->if_ioctl == NULL) {
3234 ifr.ifr_flags = ifp->if_flags & 0xffff;
3235 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3236 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3239 /* Notify userland that interface flags have changed */
3244 /* Recover after driver error */
3245 *refcount = oldcount;
3246 ifp->if_flags = oldflags;
3251 * Set/clear promiscuous mode on interface ifp based on the truth value
3252 * of pswitch. The calls are reference counted so that only the first
3253 * "on" request actually has an effect, as does the final "off" request.
3254 * Results are undefined if the "off" and "on" requests are not matched.
3257 ifpromisc(struct ifnet *ifp, int pswitch)
3260 int oldflags = ifp->if_flags;
3262 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3263 &ifp->if_pcount, pswitch);
3264 /* If promiscuous mode status has changed, log a message */
3265 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3266 log_promisc_mode_change)
3267 if_printf(ifp, "promiscuous mode %s\n",
3268 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3273 * Return interface configuration
3274 * of system. List may be used
3275 * in later ioctl's (above) to get
3276 * other information.
3280 ifconf(u_long cmd, caddr_t data)
3282 struct ifconf *ifc = (struct ifconf *)data;
3287 int error, full = 0, valid_len, max_len;
3289 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3290 max_len = MAXPHYS - 1;
3292 /* Prevent hostile input from being able to crash the system */
3293 if (ifc->ifc_len <= 0)
3297 if (ifc->ifc_len <= max_len) {
3298 max_len = ifc->ifc_len;
3301 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3306 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3310 * Zero the ifr to make sure we don't disclose the contents
3313 memset(&ifr, 0, sizeof(ifr));
3315 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3316 >= sizeof(ifr.ifr_name)) {
3319 return (ENAMETOOLONG);
3324 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3325 struct sockaddr *sa = ifa->ifa_addr;
3327 if (prison_if(curthread->td_ucred, sa) != 0)
3330 if (sa->sa_len <= sizeof(*sa)) {
3331 if (sa->sa_len < sizeof(*sa)) {
3332 memset(&ifr.ifr_ifru.ifru_addr, 0,
3333 sizeof(ifr.ifr_ifru.ifru_addr));
3334 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3337 ifr.ifr_ifru.ifru_addr = *sa;
3338 sbuf_bcat(sb, &ifr, sizeof(ifr));
3339 max_len += sizeof(ifr);
3342 offsetof(struct ifreq, ifr_addr));
3343 max_len += offsetof(struct ifreq, ifr_addr);
3344 sbuf_bcat(sb, sa, sa->sa_len);
3345 max_len += sa->sa_len;
3348 if (sbuf_error(sb) == 0)
3349 valid_len = sbuf_len(sb);
3351 IF_ADDR_RUNLOCK(ifp);
3353 sbuf_bcat(sb, &ifr, sizeof(ifr));
3354 max_len += sizeof(ifr);
3356 if (sbuf_error(sb) == 0)
3357 valid_len = sbuf_len(sb);
3363 * If we didn't allocate enough space (uncommon), try again. If
3364 * we have already allocated as much space as we are allowed,
3365 * return what we've got.
3367 if (valid_len != max_len && !full) {
3372 ifc->ifc_len = valid_len;
3374 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3380 * Just like ifpromisc(), but for all-multicast-reception mode.
3383 if_allmulti(struct ifnet *ifp, int onswitch)
3386 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3389 struct ifmultiaddr *
3390 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3392 struct ifmultiaddr *ifma;
3394 IF_ADDR_LOCK_ASSERT(ifp);
3396 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3397 if (sa->sa_family == AF_LINK) {
3398 if (sa_dl_equal(ifma->ifma_addr, sa))
3401 if (sa_equal(ifma->ifma_addr, sa))
3410 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3411 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3412 * the ifnet multicast address list here, so the caller must do that and
3413 * other setup work (such as notifying the device driver). The reference
3414 * count is initialized to 1.
3416 static struct ifmultiaddr *
3417 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3420 struct ifmultiaddr *ifma;
3421 struct sockaddr *dupsa;
3423 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3428 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3429 if (dupsa == NULL) {
3430 free(ifma, M_IFMADDR);
3433 bcopy(sa, dupsa, sa->sa_len);
3434 ifma->ifma_addr = dupsa;
3436 ifma->ifma_ifp = ifp;
3437 ifma->ifma_refcount = 1;
3438 ifma->ifma_protospec = NULL;
3441 ifma->ifma_lladdr = NULL;
3445 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3446 if (dupsa == NULL) {
3447 free(ifma->ifma_addr, M_IFMADDR);
3448 free(ifma, M_IFMADDR);
3451 bcopy(llsa, dupsa, llsa->sa_len);
3452 ifma->ifma_lladdr = dupsa;
3458 * if_freemulti: free ifmultiaddr structure and possibly attached related
3459 * addresses. The caller is responsible for implementing reference
3460 * counting, notifying the driver, handling routing messages, and releasing
3461 * any dependent link layer state.
3463 #ifdef MCAST_VERBOSE
3464 extern void kdb_backtrace(void);
3467 if_freemulti_internal(struct ifmultiaddr *ifma)
3470 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3471 ifma->ifma_refcount));
3473 if (ifma->ifma_lladdr != NULL)
3474 free(ifma->ifma_lladdr, M_IFMADDR);
3475 #ifdef MCAST_VERBOSE
3477 printf("%s freeing ifma: %p\n", __func__, ifma);
3479 free(ifma->ifma_addr, M_IFMADDR);
3480 free(ifma, M_IFMADDR);
3484 if_destroymulti(epoch_context_t ctx)
3486 struct ifmultiaddr *ifma;
3488 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3489 if_freemulti_internal(ifma);
3493 if_freemulti(struct ifmultiaddr *ifma)
3495 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3496 ifma->ifma_refcount));
3498 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3503 * Register an additional multicast address with a network interface.
3505 * - If the address is already present, bump the reference count on the
3506 * address and return.
3507 * - If the address is not link-layer, look up a link layer address.
3508 * - Allocate address structures for one or both addresses, and attach to the
3509 * multicast address list on the interface. If automatically adding a link
3510 * layer address, the protocol address will own a reference to the link
3511 * layer address, to be freed when it is freed.
3512 * - Notify the network device driver of an addition to the multicast address
3515 * 'sa' points to caller-owned memory with the desired multicast address.
3517 * 'retifma' will be used to return a pointer to the resulting multicast
3518 * address reference, if desired.
3521 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3522 struct ifmultiaddr **retifma)
3524 struct ifmultiaddr *ifma, *ll_ifma;
3525 struct sockaddr *llsa;
3526 struct sockaddr_dl sdl;
3530 IN_MULTI_LIST_UNLOCK_ASSERT();
3533 IN6_MULTI_LIST_UNLOCK_ASSERT();
3536 * If the address is already present, return a new reference to it;
3537 * otherwise, allocate storage and set up a new address.
3540 ifma = if_findmulti(ifp, sa);
3542 ifma->ifma_refcount++;
3543 if (retifma != NULL)
3545 IF_ADDR_WUNLOCK(ifp);
3550 * The address isn't already present; resolve the protocol address
3551 * into a link layer address, and then look that up, bump its
3552 * refcount or allocate an ifma for that also.
3553 * Most link layer resolving functions returns address data which
3554 * fits inside default sockaddr_dl structure. However callback
3555 * can allocate another sockaddr structure, in that case we need to
3560 if (ifp->if_resolvemulti != NULL) {
3561 /* Provide called function with buffer size information */
3562 sdl.sdl_len = sizeof(sdl);
3563 llsa = (struct sockaddr *)&sdl;
3564 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3570 * Allocate the new address. Don't hook it up yet, as we may also
3571 * need to allocate a link layer multicast address.
3573 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3580 * If a link layer address is found, we'll need to see if it's
3581 * already present in the address list, or allocate is as well.
3582 * When this block finishes, the link layer address will be on the
3586 ll_ifma = if_findmulti(ifp, llsa);
3587 if (ll_ifma == NULL) {
3588 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3589 if (ll_ifma == NULL) {
3590 --ifma->ifma_refcount;
3595 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3596 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3599 ll_ifma->ifma_refcount++;
3600 ifma->ifma_llifma = ll_ifma;
3604 * We now have a new multicast address, ifma, and possibly a new or
3605 * referenced link layer address. Add the primary address to the
3606 * ifnet address list.
3608 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3609 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3611 if (retifma != NULL)
3615 * Must generate the message while holding the lock so that 'ifma'
3616 * pointer is still valid.
3618 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3619 IF_ADDR_WUNLOCK(ifp);
3622 * We are certain we have added something, so call down to the
3623 * interface to let them know about it.
3625 if (ifp->if_ioctl != NULL) {
3626 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3629 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3630 link_free_sdl(llsa);
3635 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3636 link_free_sdl(llsa);
3639 IF_ADDR_WUNLOCK(ifp);
3644 * Delete a multicast group membership by network-layer group address.
3646 * Returns ENOENT if the entry could not be found. If ifp no longer
3647 * exists, results are undefined. This entry point should only be used
3648 * from subsystems which do appropriate locking to hold ifp for the
3649 * duration of the call.
3650 * Network-layer protocol domains must use if_delmulti_ifma().
3653 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3655 struct ifmultiaddr *ifma;
3660 IFNET_RLOCK_NOSLEEP();
3661 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3666 IFNET_RUNLOCK_NOSLEEP();
3668 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3675 ifma = if_findmulti(ifp, sa);
3677 lastref = if_delmulti_locked(ifp, ifma, 0);
3678 IF_ADDR_WUNLOCK(ifp);
3683 if (lastref && ifp->if_ioctl != NULL) {
3684 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3691 * Delete all multicast group membership for an interface.
3692 * Should be used to quickly flush all multicast filters.
3695 if_delallmulti(struct ifnet *ifp)
3697 struct ifmultiaddr *ifma;
3698 struct ifmultiaddr *next;
3701 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3702 if_delmulti_locked(ifp, ifma, 0);
3703 IF_ADDR_WUNLOCK(ifp);
3707 if_delmulti_ifma(struct ifmultiaddr *ifma)
3709 if_delmulti_ifma_flags(ifma, 0);
3713 * Delete a multicast group membership by group membership pointer.
3714 * Network-layer protocol domains must use this routine.
3716 * It is safe to call this routine if the ifp disappeared.
3719 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3723 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3725 IN_MULTI_LIST_UNLOCK_ASSERT();
3727 ifp = ifma->ifma_ifp;
3730 printf("%s: ifma_ifp seems to be detached\n", __func__);
3734 IFNET_RLOCK_NOSLEEP();
3735 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3740 IFNET_RUNLOCK_NOSLEEP();
3744 * If and only if the ifnet instance exists: Acquire the address lock.
3749 lastref = if_delmulti_locked(ifp, ifma, flags);
3753 * If and only if the ifnet instance exists:
3754 * Release the address lock.
3755 * If the group was left: update the hardware hash filter.
3757 IF_ADDR_WUNLOCK(ifp);
3758 if (lastref && ifp->if_ioctl != NULL) {
3759 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3765 * Perform deletion of network-layer and/or link-layer multicast address.
3767 * Return 0 if the reference count was decremented.
3768 * Return 1 if the final reference was released, indicating that the
3769 * hardware hash filter should be reprogrammed.
3772 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3774 struct ifmultiaddr *ll_ifma;
3776 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3777 KASSERT(ifma->ifma_ifp == ifp,
3778 ("%s: inconsistent ifp %p", __func__, ifp));
3779 IF_ADDR_WLOCK_ASSERT(ifp);
3782 ifp = ifma->ifma_ifp;
3783 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3786 * If the ifnet is detaching, null out references to ifnet,
3787 * so that upper protocol layers will notice, and not attempt
3788 * to obtain locks for an ifnet which no longer exists. The
3789 * routing socket announcement must happen before the ifnet
3790 * instance is detached from the system.
3794 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3797 * ifp may already be nulled out if we are being reentered
3798 * to delete the ll_ifma.
3801 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3802 ifma->ifma_ifp = NULL;
3806 if (--ifma->ifma_refcount > 0)
3809 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3810 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3811 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3814 * If this ifma is a network-layer ifma, a link-layer ifma may
3815 * have been associated with it. Release it first if so.
3817 ll_ifma = ifma->ifma_llifma;
3818 if (ll_ifma != NULL) {
3819 KASSERT(ifma->ifma_lladdr != NULL,
3820 ("%s: llifma w/o lladdr", __func__));
3822 ll_ifma->ifma_ifp = NULL; /* XXX */
3823 if (--ll_ifma->ifma_refcount == 0) {
3825 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3826 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3828 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3831 if_freemulti(ll_ifma);
3836 struct ifmultiaddr *ifmatmp;
3838 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3839 MPASS(ifma != ifmatmp);
3844 * The last reference to this instance of struct ifmultiaddr
3845 * was released; the hardware should be notified of this change.
3851 * Set the link layer address on an interface.
3853 * At this time we only support certain types of interfaces,
3854 * and we don't allow the length of the address to change.
3856 * Set noinline to be dtrace-friendly
3859 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3861 struct sockaddr_dl *sdl;
3874 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3879 if (len != sdl->sdl_alen) { /* don't allow length to change */
3883 switch (ifp->if_type) {
3888 case IFT_IEEE8023ADLAG:
3889 bcopy(lladdr, LLADDR(sdl), len);
3897 * If the interface is already up, we need
3898 * to re-init it in order to reprogram its
3902 if ((ifp->if_flags & IFF_UP) != 0) {
3903 if (ifp->if_ioctl) {
3904 ifp->if_flags &= ~IFF_UP;
3905 ifr.ifr_flags = ifp->if_flags & 0xffff;
3906 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3907 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3908 ifp->if_flags |= IFF_UP;
3909 ifr.ifr_flags = ifp->if_flags & 0xffff;
3910 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3911 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3914 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3922 * Compat function for handling basic encapsulation requests.
3923 * Not converted stacks (FDDI, IB, ..) supports traditional
3924 * output model: ARP (and other similar L2 protocols) are handled
3925 * inside output routine, arpresolve/nd6_resolve() returns MAC
3926 * address instead of full prepend.
3928 * This function creates calculated header==MAC for IPv4/IPv6 and
3929 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3933 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3936 if (req->rtype != IFENCAP_LL)
3937 return (EOPNOTSUPP);
3939 if (req->bufsize < req->lladdr_len)
3942 switch (req->family) {
3947 return (EAFNOSUPPORT);
3950 /* Copy lladdr to storage as is */
3951 memmove(req->buf, req->lladdr, req->lladdr_len);
3952 req->bufsize = req->lladdr_len;
3953 req->lladdr_off = 0;
3959 * Tunnel interfaces can nest, also they may cause infinite recursion
3960 * calls when misconfigured. We'll prevent this by detecting loops.
3961 * High nesting level may cause stack exhaustion. We'll prevent this
3962 * by introducing upper limit.
3964 * Return 0, if tunnel nesting count is equal or less than limit.
3967 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3975 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3976 if (*(struct ifnet **)(mtag + 1) == ifp) {
3977 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3982 if (count > limit) {
3984 "%s: if_output recursively called too many times(%d)\n",
3985 if_name(ifp), count);
3988 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3991 *(struct ifnet **)(mtag + 1) = ifp;
3992 m_tag_prepend(m, mtag);
3997 * Get the link layer address that was read from the hardware at attach.
3999 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4000 * their component interfaces as IFT_IEEE8023ADLAG.
4003 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4006 if (ifp->if_hw_addr == NULL)
4009 switch (ifp->if_type) {
4011 case IFT_IEEE8023ADLAG:
4012 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4020 * The name argument must be a pointer to storage which will last as
4021 * long as the interface does. For physical devices, the result of
4022 * device_get_name(dev) is a good choice and for pseudo-devices a
4023 * static string works well.
4026 if_initname(struct ifnet *ifp, const char *name, int unit)
4028 ifp->if_dname = name;
4029 ifp->if_dunit = unit;
4030 if (unit != IF_DUNIT_NONE)
4031 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4033 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4037 if_printf(struct ifnet *ifp, const char *fmt, ...)
4042 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4044 vlog(LOG_INFO, if_fmt, ap);
4050 if_start(struct ifnet *ifp)
4053 (*(ifp)->if_start)(ifp);
4057 * Backwards compatibility interface for drivers
4058 * that have not implemented it
4061 if_transmit(struct ifnet *ifp, struct mbuf *m)
4065 IFQ_HANDOFF(ifp, m, error);
4070 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4077 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4082 if (_IF_QFULL(ifq)) {
4084 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4089 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4090 if (m->m_flags & (M_BCAST|M_MCAST))
4091 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4092 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4094 _IF_ENQUEUE(ifq, m);
4096 if (ifp != NULL && !active)
4097 (*(ifp)->if_start)(ifp);
4102 if_register_com_alloc(u_char type,
4103 if_com_alloc_t *a, if_com_free_t *f)
4106 KASSERT(if_com_alloc[type] == NULL,
4107 ("if_register_com_alloc: %d already registered", type));
4108 KASSERT(if_com_free[type] == NULL,
4109 ("if_register_com_alloc: %d free already registered", type));
4111 if_com_alloc[type] = a;
4112 if_com_free[type] = f;
4116 if_deregister_com_alloc(u_char type)
4119 KASSERT(if_com_alloc[type] != NULL,
4120 ("if_deregister_com_alloc: %d not registered", type));
4121 KASSERT(if_com_free[type] != NULL,
4122 ("if_deregister_com_alloc: %d free not registered", type));
4123 if_com_alloc[type] = NULL;
4124 if_com_free[type] = NULL;
4127 /* API for driver access to network stack owned ifnet.*/
4129 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4133 oldbrate = ifp->if_baudrate;
4134 ifp->if_baudrate = baudrate;
4139 if_getbaudrate(if_t ifp)
4142 return (((struct ifnet *)ifp)->if_baudrate);
4146 if_setcapabilities(if_t ifp, int capabilities)
4148 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4153 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4155 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4156 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4162 if_getcapabilities(if_t ifp)
4164 return ((struct ifnet *)ifp)->if_capabilities;
4168 if_setcapenable(if_t ifp, int capabilities)
4170 ((struct ifnet *)ifp)->if_capenable = capabilities;
4175 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4178 ((struct ifnet *)ifp)->if_capenable |= setcap;
4180 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4186 if_getdname(if_t ifp)
4188 return ((struct ifnet *)ifp)->if_dname;
4192 if_togglecapenable(if_t ifp, int togglecap)
4194 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4199 if_getcapenable(if_t ifp)
4201 return ((struct ifnet *)ifp)->if_capenable;
4205 * This is largely undesirable because it ties ifnet to a device, but does
4206 * provide flexiblity for an embedded product vendor. Should be used with
4207 * the understanding that it violates the interface boundaries, and should be
4208 * a last resort only.
4211 if_setdev(if_t ifp, void *dev)
4217 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4219 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4220 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4226 if_getdrvflags(if_t ifp)
4228 return ((struct ifnet *)ifp)->if_drv_flags;
4232 if_setdrvflags(if_t ifp, int flags)
4234 ((struct ifnet *)ifp)->if_drv_flags = flags;
4240 if_setflags(if_t ifp, int flags)
4242 ((struct ifnet *)ifp)->if_flags = flags;
4247 if_setflagbits(if_t ifp, int set, int clear)
4249 ((struct ifnet *)ifp)->if_flags |= set;
4250 ((struct ifnet *)ifp)->if_flags &= ~clear;
4256 if_getflags(if_t ifp)
4258 return ((struct ifnet *)ifp)->if_flags;
4262 if_clearhwassist(if_t ifp)
4264 ((struct ifnet *)ifp)->if_hwassist = 0;
4269 if_sethwassistbits(if_t ifp, int toset, int toclear)
4271 ((struct ifnet *)ifp)->if_hwassist |= toset;
4272 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4278 if_sethwassist(if_t ifp, int hwassist_bit)
4280 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4285 if_gethwassist(if_t ifp)
4287 return ((struct ifnet *)ifp)->if_hwassist;
4291 if_setmtu(if_t ifp, int mtu)
4293 ((struct ifnet *)ifp)->if_mtu = mtu;
4300 return ((struct ifnet *)ifp)->if_mtu;
4304 if_getmtu_family(if_t ifp, int family)
4308 for (dp = domains; dp; dp = dp->dom_next) {
4309 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4310 return (dp->dom_ifmtu((struct ifnet *)ifp));
4313 return (((struct ifnet *)ifp)->if_mtu);
4317 if_setsoftc(if_t ifp, void *softc)
4319 ((struct ifnet *)ifp)->if_softc = softc;
4324 if_getsoftc(if_t ifp)
4326 return ((struct ifnet *)ifp)->if_softc;
4330 if_setrcvif(struct mbuf *m, if_t ifp)
4332 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4336 if_setvtag(struct mbuf *m, uint16_t tag)
4338 m->m_pkthdr.ether_vtag = tag;
4342 if_getvtag(struct mbuf *m)
4345 return (m->m_pkthdr.ether_vtag);
4349 if_sendq_empty(if_t ifp)
4351 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4355 if_getifaddr(if_t ifp)
4357 return ((struct ifnet *)ifp)->if_addr;
4361 if_getamcount(if_t ifp)
4363 return ((struct ifnet *)ifp)->if_amcount;
4368 if_setsendqready(if_t ifp)
4370 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4375 if_setsendqlen(if_t ifp, int tx_desc_count)
4377 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4378 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4384 if_vlantrunkinuse(if_t ifp)
4386 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4390 if_input(if_t ifp, struct mbuf* sendmp)
4392 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4398 #ifndef ETH_ADDR_LEN
4399 #define ETH_ADDR_LEN 6
4403 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4405 struct ifmultiaddr *ifma;
4406 uint8_t *lmta = (uint8_t *)mta;
4409 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4410 if (ifma->ifma_addr->sa_family != AF_LINK)
4416 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4417 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4426 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4430 if_maddr_rlock(ifp);
4431 error = if_setupmultiaddr(ifp, mta, cnt, max);
4432 if_maddr_runlock(ifp);
4437 if_multiaddr_count(if_t ifp, int max)
4439 struct ifmultiaddr *ifma;
4443 if_maddr_rlock(ifp);
4444 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4445 if (ifma->ifma_addr->sa_family != AF_LINK)
4451 if_maddr_runlock(ifp);
4456 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4458 struct ifmultiaddr *ifma;
4461 if_maddr_rlock(ifp);
4462 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4463 cnt += filter(arg, ifma, cnt);
4464 if_maddr_runlock(ifp);
4469 if_dequeue(if_t ifp)
4472 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4478 if_sendq_prepend(if_t ifp, struct mbuf *m)
4480 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4485 if_setifheaderlen(if_t ifp, int len)
4487 ((struct ifnet *)ifp)->if_hdrlen = len;
4492 if_getlladdr(if_t ifp)
4494 return (IF_LLADDR((struct ifnet *)ifp));
4498 if_gethandle(u_char type)
4500 return (if_alloc(type));
4504 if_bpfmtap(if_t ifh, struct mbuf *m)
4506 struct ifnet *ifp = (struct ifnet *)ifh;
4512 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4514 struct ifnet *ifp = (struct ifnet *)ifh;
4516 ETHER_BPF_MTAP(ifp, m);
4520 if_vlancap(if_t ifh)
4522 struct ifnet *ifp = (struct ifnet *)ifh;
4523 VLAN_CAPABILITIES(ifp);
4527 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4530 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4535 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4538 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4543 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4546 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4551 if_gethwtsomax(if_t ifp)
4554 return (((struct ifnet *)ifp)->if_hw_tsomax);
4558 if_gethwtsomaxsegcount(if_t ifp)
4561 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4565 if_gethwtsomaxsegsize(if_t ifp)
4568 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4572 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4574 ((struct ifnet *)ifp)->if_init = init_fn;
4578 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4580 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4584 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4586 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4590 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4592 ((struct ifnet *)ifp)->if_transmit = start_fn;
4595 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4597 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4602 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4605 ifp->if_get_counter = fn;
4608 /* Revisit these - These are inline functions originally. */
4610 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4612 return drbr_inuse(ifh, br);
4616 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4618 return drbr_dequeue(ifh, br);
4622 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4624 return drbr_needs_enqueue(ifh, br);
4628 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4630 return drbr_enqueue(ifh, br, m);