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);
1283 /* Don't re-attach DYING interfaces. */
1284 if (ifp->if_flags & IFF_DYING)
1288 * Perform interface-specific reassignment tasks, if provided by
1291 if (ifp->if_reassign != NULL)
1292 ifp->if_reassign(ifp, new_vnet, NULL);
1295 * Switch to the context of the target vnet.
1297 CURVNET_SET_QUIET(new_vnet);
1300 ifp->if_index = ifindex_alloc(&old);
1301 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1303 epoch_wait_preempt(net_epoch_preempt);
1307 ifnet_setbyindex(ifp->if_index, ifp);
1310 if_attach_internal(ifp, 1, ifc);
1313 if (ifp->if_bpf == NULL)
1314 bpfattach(ifp, bif_dlt, bif_hdrlen);
1321 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1324 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1330 /* Try to find the prison within our visibility. */
1331 sx_slock(&allprison_lock);
1332 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1333 sx_sunlock(&allprison_lock);
1336 prison_hold_locked(pr);
1337 mtx_unlock(&pr->pr_mtx);
1339 /* Do not try to move the iface from and to the same prison. */
1340 if (pr->pr_vnet == ifp->if_vnet) {
1345 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1346 /* XXX Lock interfaces to avoid races. */
1347 CURVNET_SET_QUIET(pr->pr_vnet);
1348 difp = ifunit(ifname);
1355 /* Make sure the VNET is stable. */
1356 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1357 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1365 /* Move the interface into the child jail/vnet. */
1366 if_vmove(ifp, pr->pr_vnet);
1368 /* Report the new if_xname back to the userland. */
1369 sprintf(ifname, "%s", ifp->if_xname);
1376 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1379 struct vnet *vnet_dst;
1383 /* Try to find the prison within our visibility. */
1384 sx_slock(&allprison_lock);
1385 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1386 sx_sunlock(&allprison_lock);
1389 prison_hold_locked(pr);
1390 mtx_unlock(&pr->pr_mtx);
1392 /* Make sure the named iface exists in the source prison/vnet. */
1393 CURVNET_SET(pr->pr_vnet);
1394 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1401 /* Do not try to move the iface from and to the same prison. */
1402 vnet_dst = TD_TO_VNET(td);
1403 if (vnet_dst == ifp->if_vnet) {
1409 /* Make sure the VNET is stable. */
1410 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1411 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1418 /* Get interface back from child jail/vnet. */
1419 if_vmove(ifp, vnet_dst);
1422 /* Report the new if_xname back to the userland. */
1423 sprintf(ifname, "%s", ifp->if_xname);
1431 * Add a group to an interface
1434 if_addgroup(struct ifnet *ifp, const char *groupname)
1436 struct ifg_list *ifgl;
1437 struct ifg_group *ifg = NULL;
1438 struct ifg_member *ifgm;
1441 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1442 groupname[strlen(groupname) - 1] <= '9')
1446 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1447 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1452 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1457 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1463 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1464 if (!strcmp(ifg->ifg_group, groupname))
1468 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1474 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1475 ifg->ifg_refcnt = 0;
1476 CK_STAILQ_INIT(&ifg->ifg_members);
1477 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1482 ifgl->ifgl_group = ifg;
1483 ifgm->ifgm_ifp = ifp;
1486 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1487 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1488 IF_ADDR_WUNLOCK(ifp);
1493 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1494 EVENTHANDLER_INVOKE(group_change_event, groupname);
1500 * Helper function to remove a group out of an interface. Expects the global
1501 * ifnet lock to be write-locked, and drops it before returning.
1504 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1505 const char *groupname)
1507 struct ifg_member *ifgm;
1510 IFNET_WLOCK_ASSERT();
1513 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1514 IF_ADDR_WUNLOCK(ifp);
1516 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1517 if (ifgm->ifgm_ifp == ifp) {
1518 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1519 ifg_member, ifgm_next);
1524 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1525 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1533 epoch_wait_preempt(net_epoch_preempt);
1535 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1536 free(ifgl->ifgl_group, M_TEMP);
1541 EVENTHANDLER_INVOKE(group_change_event, groupname);
1545 * Remove a group from an interface
1548 if_delgroup(struct ifnet *ifp, const char *groupname)
1550 struct ifg_list *ifgl;
1553 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1554 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1561 _if_delgroup_locked(ifp, ifgl, groupname);
1567 * Remove an interface from all groups
1570 if_delgroups(struct ifnet *ifp)
1572 struct ifg_list *ifgl;
1573 char groupname[IFNAMSIZ];
1576 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1577 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1578 _if_delgroup_locked(ifp, ifgl, groupname);
1585 ifgr_group_get(void *ifgrp)
1587 union ifgroupreq_union *ifgrup;
1590 #ifdef COMPAT_FREEBSD32
1591 if (SV_CURPROC_FLAG(SV_ILP32))
1592 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1594 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1597 static struct ifg_req *
1598 ifgr_groups_get(void *ifgrp)
1600 union ifgroupreq_union *ifgrup;
1603 #ifdef COMPAT_FREEBSD32
1604 if (SV_CURPROC_FLAG(SV_ILP32))
1605 return ((struct ifg_req *)(uintptr_t)
1606 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1608 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1612 * Stores all groups from an interface in memory pointed to by ifgr.
1615 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1618 struct ifg_list *ifgl;
1619 struct ifg_req ifgrq, *ifgp;
1621 if (ifgr->ifgr_len == 0) {
1623 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1624 ifgr->ifgr_len += sizeof(struct ifg_req);
1625 IF_ADDR_RUNLOCK(ifp);
1629 len = ifgr->ifgr_len;
1630 ifgp = ifgr_groups_get(ifgr);
1633 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1634 if (len < sizeof(ifgrq)) {
1635 IF_ADDR_RUNLOCK(ifp);
1638 bzero(&ifgrq, sizeof ifgrq);
1639 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1640 sizeof(ifgrq.ifgrq_group));
1641 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1642 IF_ADDR_RUNLOCK(ifp);
1645 len -= sizeof(ifgrq);
1648 IF_ADDR_RUNLOCK(ifp);
1654 * Stores all members of a group in memory pointed to by igfr
1657 if_getgroupmembers(struct ifgroupreq *ifgr)
1659 struct ifg_group *ifg;
1660 struct ifg_member *ifgm;
1661 struct ifg_req ifgrq, *ifgp;
1665 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1666 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1673 if (ifgr->ifgr_len == 0) {
1674 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1675 ifgr->ifgr_len += sizeof(ifgrq);
1680 len = ifgr->ifgr_len;
1681 ifgp = ifgr_groups_get(ifgr);
1682 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1683 if (len < sizeof(ifgrq)) {
1687 bzero(&ifgrq, sizeof ifgrq);
1688 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1689 sizeof(ifgrq.ifgrq_member));
1690 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1694 len -= sizeof(ifgrq);
1703 * Return counter values from counter(9)s stored in ifnet.
1706 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1709 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1711 return (counter_u64_fetch(ifp->if_counters[cnt]));
1715 * Increase an ifnet counter. Usually used for counters shared
1716 * between the stack and a driver, but function supports them all.
1719 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1722 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1724 counter_u64_add(ifp->if_counters[cnt], inc);
1728 * Copy data from ifnet to userland API structure if_data.
1731 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1734 ifd->ifi_type = ifp->if_type;
1735 ifd->ifi_physical = 0;
1736 ifd->ifi_addrlen = ifp->if_addrlen;
1737 ifd->ifi_hdrlen = ifp->if_hdrlen;
1738 ifd->ifi_link_state = ifp->if_link_state;
1740 ifd->ifi_datalen = sizeof(struct if_data);
1741 ifd->ifi_mtu = ifp->if_mtu;
1742 ifd->ifi_metric = ifp->if_metric;
1743 ifd->ifi_baudrate = ifp->if_baudrate;
1744 ifd->ifi_hwassist = ifp->if_hwassist;
1745 ifd->ifi_epoch = ifp->if_epoch;
1746 ifd->ifi_lastchange = ifp->if_lastchange;
1748 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1749 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1750 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1751 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1752 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1753 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1754 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1755 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1756 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1757 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1758 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1759 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1762 struct ifnet_read_lock {
1763 struct mtx mtx; /* lock protecting tracker below */
1764 struct epoch_tracker et;
1767 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_addr_read_lock);
1768 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_maddr_read_lock);
1771 ifnet_read_lock_init(void __unused *arg)
1773 struct ifnet_read_lock *pifrl;
1777 pifrl = DPCPU_ID_PTR(cpu, ifnet_addr_read_lock);
1778 mtx_init(&pifrl->mtx, "ifnet_addr_read_lock", NULL, MTX_DEF);
1780 pifrl = DPCPU_ID_PTR(cpu, ifnet_maddr_read_lock);
1781 mtx_init(&pifrl->mtx, "ifnet_maddr_read_lock", NULL, MTX_DEF);
1784 SYSINIT(ifnet_read_lock_init, SI_SUB_CPU + 1, SI_ORDER_FIRST, &ifnet_read_lock_init, NULL);
1787 * Wrapper functions for struct ifnet address list locking macros. These are
1788 * used by kernel modules to avoid encoding programming interface or binary
1789 * interface assumptions that may be violated when kernel-internal locking
1790 * approaches change.
1793 if_addr_rlock(struct ifnet *ifp)
1795 struct ifnet_read_lock *pifrl;
1798 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1799 mtx_lock(&pifrl->mtx);
1800 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1804 if_addr_runlock(struct ifnet *ifp)
1806 struct ifnet_read_lock *pifrl;
1808 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1810 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1811 mtx_unlock(&pifrl->mtx);
1816 if_maddr_rlock(if_t ifp)
1818 struct ifnet_read_lock *pifrl;
1821 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1822 mtx_lock(&pifrl->mtx);
1823 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1827 if_maddr_runlock(if_t ifp)
1829 struct ifnet_read_lock *pifrl;
1831 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1833 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1834 mtx_unlock(&pifrl->mtx);
1839 * Initialization, destruction and refcounting functions for ifaddrs.
1842 ifa_alloc(size_t size, int flags)
1846 KASSERT(size >= sizeof(struct ifaddr),
1847 ("%s: invalid size %zu", __func__, size));
1849 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1853 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1855 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1857 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1859 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1862 refcount_init(&ifa->ifa_refcnt, 1);
1867 /* free(NULL) is okay */
1868 counter_u64_free(ifa->ifa_opackets);
1869 counter_u64_free(ifa->ifa_ipackets);
1870 counter_u64_free(ifa->ifa_obytes);
1871 counter_u64_free(ifa->ifa_ibytes);
1872 free(ifa, M_IFADDR);
1878 ifa_ref(struct ifaddr *ifa)
1881 refcount_acquire(&ifa->ifa_refcnt);
1885 ifa_destroy(epoch_context_t ctx)
1889 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1890 counter_u64_free(ifa->ifa_opackets);
1891 counter_u64_free(ifa->ifa_ipackets);
1892 counter_u64_free(ifa->ifa_obytes);
1893 counter_u64_free(ifa->ifa_ibytes);
1894 free(ifa, M_IFADDR);
1898 ifa_free(struct ifaddr *ifa)
1901 if (refcount_release(&ifa->ifa_refcnt))
1902 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1907 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1908 struct sockaddr *ia)
1911 struct rt_addrinfo info;
1912 struct sockaddr_dl null_sdl;
1917 bzero(&info, sizeof(info));
1918 if (cmd != RTM_DELETE)
1919 info.rti_ifp = V_loif;
1920 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1921 info.rti_info[RTAX_DST] = ia;
1922 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1923 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1925 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1928 (cmd == RTM_ADD && error == EEXIST) ||
1929 (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
1932 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1933 __func__, otype, if_name(ifp), error);
1939 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1942 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1946 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1949 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1953 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1956 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1960 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1961 * structs used to represent other address families, it is necessary
1962 * to perform a different comparison.
1965 #define sa_dl_equal(a1, a2) \
1966 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1967 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1968 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1969 CLLADDR((const struct sockaddr_dl *)(a2)), \
1970 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1973 * Locate an interface based on a complete address.
1977 ifa_ifwithaddr(const struct sockaddr *addr)
1982 MPASS(in_epoch(net_epoch_preempt));
1983 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1984 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1985 if (ifa->ifa_addr->sa_family != addr->sa_family)
1987 if (sa_equal(addr, ifa->ifa_addr)) {
1990 /* IP6 doesn't have broadcast */
1991 if ((ifp->if_flags & IFF_BROADCAST) &&
1992 ifa->ifa_broadaddr &&
1993 ifa->ifa_broadaddr->sa_len != 0 &&
1994 sa_equal(ifa->ifa_broadaddr, addr)) {
2005 ifa_ifwithaddr_check(const struct sockaddr *addr)
2010 rc = (ifa_ifwithaddr(addr) != NULL);
2016 * Locate an interface based on the broadcast address.
2020 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
2025 MPASS(in_epoch(net_epoch_preempt));
2026 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2027 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2029 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2030 if (ifa->ifa_addr->sa_family != addr->sa_family)
2032 if ((ifp->if_flags & IFF_BROADCAST) &&
2033 ifa->ifa_broadaddr &&
2034 ifa->ifa_broadaddr->sa_len != 0 &&
2035 sa_equal(ifa->ifa_broadaddr, addr)) {
2046 * Locate the point to point interface with a given destination address.
2050 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2055 MPASS(in_epoch(net_epoch_preempt));
2056 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2057 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2059 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2061 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2062 if (ifa->ifa_addr->sa_family != addr->sa_family)
2064 if (ifa->ifa_dstaddr != NULL &&
2065 sa_equal(addr, ifa->ifa_dstaddr)) {
2076 * Find an interface on a specific network. If many, choice
2077 * is most specific found.
2080 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2084 struct ifaddr *ifa_maybe = NULL;
2085 u_int af = addr->sa_family;
2086 const char *addr_data = addr->sa_data, *cplim;
2088 MPASS(in_epoch(net_epoch_preempt));
2090 * AF_LINK addresses can be looked up directly by their index number,
2091 * so do that if we can.
2093 if (af == AF_LINK) {
2094 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2095 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2096 return (ifaddr_byindex(sdl->sdl_index));
2100 * Scan though each interface, looking for ones that have addresses
2101 * in this address family and the requested fib. Maintain a reference
2102 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2103 * kept it stable when we move onto the next interface.
2105 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2106 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2108 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2109 const char *cp, *cp2, *cp3;
2111 if (ifa->ifa_addr->sa_family != af)
2113 if (af == AF_INET &&
2114 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2116 * This is a bit broken as it doesn't
2117 * take into account that the remote end may
2118 * be a single node in the network we are
2120 * The trouble is that we don't know the
2121 * netmask for the remote end.
2123 if (ifa->ifa_dstaddr != NULL &&
2124 sa_equal(addr, ifa->ifa_dstaddr)) {
2129 * Scan all the bits in the ifa's address.
2130 * If a bit dissagrees with what we are
2131 * looking for, mask it with the netmask
2132 * to see if it really matters.
2133 * (A byte at a time)
2135 if (ifa->ifa_netmask == 0)
2138 cp2 = ifa->ifa_addr->sa_data;
2139 cp3 = ifa->ifa_netmask->sa_data;
2140 cplim = ifa->ifa_netmask->sa_len
2141 + (char *)ifa->ifa_netmask;
2143 if ((*cp++ ^ *cp2++) & *cp3++)
2144 goto next; /* next address! */
2146 * If the netmask of what we just found
2147 * is more specific than what we had before
2148 * (if we had one), or if the virtual status
2149 * of new prefix is better than of the old one,
2150 * then remember the new one before continuing
2151 * to search for an even better one.
2153 if (ifa_maybe == NULL ||
2154 ifa_preferred(ifa_maybe, ifa) ||
2155 rn_refines((caddr_t)ifa->ifa_netmask,
2156 (caddr_t)ifa_maybe->ifa_netmask)) {
2169 * Find an interface address specific to an interface best matching
2173 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2176 const char *cp, *cp2, *cp3;
2178 struct ifaddr *ifa_maybe = NULL;
2179 u_int af = addr->sa_family;
2184 MPASS(in_epoch(net_epoch_preempt));
2185 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2186 if (ifa->ifa_addr->sa_family != af)
2188 if (ifa_maybe == NULL)
2190 if (ifa->ifa_netmask == 0) {
2191 if (sa_equal(addr, ifa->ifa_addr) ||
2192 (ifa->ifa_dstaddr &&
2193 sa_equal(addr, ifa->ifa_dstaddr)))
2197 if (ifp->if_flags & IFF_POINTOPOINT) {
2198 if (sa_equal(addr, ifa->ifa_dstaddr))
2202 cp2 = ifa->ifa_addr->sa_data;
2203 cp3 = ifa->ifa_netmask->sa_data;
2204 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2205 for (; cp3 < cplim; cp3++)
2206 if ((*cp++ ^ *cp2++) & *cp3)
2218 * See whether new ifa is better than current one:
2219 * 1) A non-virtual one is preferred over virtual.
2220 * 2) A virtual in master state preferred over any other state.
2222 * Used in several address selecting functions.
2225 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2228 return (cur->ifa_carp && (!next->ifa_carp ||
2229 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2232 #include <net/if_llatbl.h>
2235 * Default action when installing a route with a Link Level gateway.
2236 * Lookup an appropriate real ifa to point to.
2237 * This should be moved to /sys/net/link.c eventually.
2240 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2242 struct ifaddr *ifa, *oifa;
2243 struct sockaddr *dst;
2246 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2247 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2250 ifa = ifaof_ifpforaddr(dst, ifp);
2258 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2259 ifa->ifa_rtrequest(cmd, rt, info);
2264 struct sockaddr_dl *
2265 link_alloc_sdl(size_t size, int flags)
2268 return (malloc(size, M_TEMP, flags));
2272 link_free_sdl(struct sockaddr *sa)
2278 * Fills in given sdl with interface basic info.
2279 * Returns pointer to filled sdl.
2281 struct sockaddr_dl *
2282 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2284 struct sockaddr_dl *sdl;
2286 sdl = (struct sockaddr_dl *)paddr;
2287 memset(sdl, 0, sizeof(struct sockaddr_dl));
2288 sdl->sdl_len = sizeof(struct sockaddr_dl);
2289 sdl->sdl_family = AF_LINK;
2290 sdl->sdl_index = ifp->if_index;
2291 sdl->sdl_type = iftype;
2297 * Mark an interface down and notify protocols of
2301 if_unroute(struct ifnet *ifp, int flag, int fam)
2305 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2307 ifp->if_flags &= ~flag;
2308 getmicrotime(&ifp->if_lastchange);
2309 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2310 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2311 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2312 ifp->if_qflush(ifp);
2315 (*carp_linkstate_p)(ifp);
2320 * Mark an interface up and notify protocols of
2324 if_route(struct ifnet *ifp, int flag, int fam)
2328 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2330 ifp->if_flags |= flag;
2331 getmicrotime(&ifp->if_lastchange);
2332 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2333 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2334 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2336 (*carp_linkstate_p)(ifp);
2343 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2344 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2345 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2346 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2347 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2348 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2349 int (*vlan_setcookie_p)(struct ifnet *, void *);
2350 void *(*vlan_cookie_p)(struct ifnet *);
2353 * Handle a change in the interface link state. To avoid LORs
2354 * between driver lock and upper layer locks, as well as possible
2355 * recursions, we post event to taskqueue, and all job
2356 * is done in static do_link_state_change().
2359 if_link_state_change(struct ifnet *ifp, int link_state)
2361 /* Return if state hasn't changed. */
2362 if (ifp->if_link_state == link_state)
2365 ifp->if_link_state = link_state;
2367 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2371 do_link_state_change(void *arg, int pending)
2373 struct ifnet *ifp = (struct ifnet *)arg;
2374 int link_state = ifp->if_link_state;
2375 CURVNET_SET(ifp->if_vnet);
2377 /* Notify that the link state has changed. */
2379 if (ifp->if_vlantrunk != NULL)
2380 (*vlan_link_state_p)(ifp);
2382 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2383 ifp->if_l2com != NULL)
2384 (*ng_ether_link_state_p)(ifp, link_state);
2386 (*carp_linkstate_p)(ifp);
2388 ifp->if_bridge_linkstate(ifp);
2390 (*lagg_linkstate_p)(ifp, link_state);
2392 if (IS_DEFAULT_VNET(curvnet))
2393 devctl_notify("IFNET", ifp->if_xname,
2394 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2397 if_printf(ifp, "%d link states coalesced\n", pending);
2398 if (log_link_state_change)
2399 if_printf(ifp, "link state changed to %s\n",
2400 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2401 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2406 * Mark an interface down and notify protocols of
2410 if_down(struct ifnet *ifp)
2413 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2414 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2418 * Mark an interface up and notify protocols of
2422 if_up(struct ifnet *ifp)
2425 if_route(ifp, IFF_UP, AF_UNSPEC);
2426 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2430 * Flush an interface queue.
2433 if_qflush(struct ifnet *ifp)
2441 if (ALTQ_IS_ENABLED(ifq))
2445 while ((m = n) != NULL) {
2456 * Map interface name to interface structure pointer, with or without
2457 * returning a reference.
2460 ifunit_ref(const char *name)
2464 IFNET_RLOCK_NOSLEEP();
2465 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2466 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2467 !(ifp->if_flags & IFF_DYING))
2472 IFNET_RUNLOCK_NOSLEEP();
2477 ifunit(const char *name)
2481 IFNET_RLOCK_NOSLEEP();
2482 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2483 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2486 IFNET_RUNLOCK_NOSLEEP();
2491 ifr_buffer_get_buffer(void *data)
2493 union ifreq_union *ifrup;
2496 #ifdef COMPAT_FREEBSD32
2497 if (SV_CURPROC_FLAG(SV_ILP32))
2498 return ((void *)(uintptr_t)
2499 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2501 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2505 ifr_buffer_set_buffer_null(void *data)
2507 union ifreq_union *ifrup;
2510 #ifdef COMPAT_FREEBSD32
2511 if (SV_CURPROC_FLAG(SV_ILP32))
2512 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2515 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2519 ifr_buffer_get_length(void *data)
2521 union ifreq_union *ifrup;
2524 #ifdef COMPAT_FREEBSD32
2525 if (SV_CURPROC_FLAG(SV_ILP32))
2526 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2528 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2532 ifr_buffer_set_length(void *data, size_t len)
2534 union ifreq_union *ifrup;
2537 #ifdef COMPAT_FREEBSD32
2538 if (SV_CURPROC_FLAG(SV_ILP32))
2539 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2542 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2546 ifr_data_get_ptr(void *ifrp)
2548 union ifreq_union *ifrup;
2551 #ifdef COMPAT_FREEBSD32
2552 if (SV_CURPROC_FLAG(SV_ILP32))
2553 return ((void *)(uintptr_t)
2554 ifrup->ifr32.ifr_ifru.ifru_data);
2556 return (ifrup->ifr.ifr_ifru.ifru_data);
2560 * Hardware specific interface ioctls.
2563 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2566 int error = 0, do_ifup = 0;
2567 int new_flags, temp_flags;
2568 size_t namelen, onamelen;
2570 char *descrbuf, *odescrbuf;
2571 char new_name[IFNAMSIZ];
2573 struct sockaddr_dl *sdl;
2575 ifr = (struct ifreq *)data;
2578 ifr->ifr_index = ifp->if_index;
2582 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2583 ifr->ifr_flags = temp_flags & 0xffff;
2584 ifr->ifr_flagshigh = temp_flags >> 16;
2588 ifr->ifr_reqcap = ifp->if_capabilities;
2589 ifr->ifr_curcap = ifp->if_capenable;
2594 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2599 ifr->ifr_metric = ifp->if_metric;
2603 ifr->ifr_mtu = ifp->if_mtu;
2607 /* XXXGL: did this ever worked? */
2613 sx_slock(&ifdescr_sx);
2614 if (ifp->if_description == NULL)
2617 /* space for terminating nul */
2618 descrlen = strlen(ifp->if_description) + 1;
2619 if (ifr_buffer_get_length(ifr) < descrlen)
2620 ifr_buffer_set_buffer_null(ifr);
2622 error = copyout(ifp->if_description,
2623 ifr_buffer_get_buffer(ifr), descrlen);
2624 ifr_buffer_set_length(ifr, descrlen);
2626 sx_sunlock(&ifdescr_sx);
2630 error = priv_check(td, PRIV_NET_SETIFDESCR);
2635 * Copy only (length-1) bytes to make sure that
2636 * if_description is always nul terminated. The
2637 * length parameter is supposed to count the
2638 * terminating nul in.
2640 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2641 return (ENAMETOOLONG);
2642 else if (ifr_buffer_get_length(ifr) == 0)
2645 descrbuf = malloc(ifr_buffer_get_length(ifr),
2646 M_IFDESCR, M_WAITOK | M_ZERO);
2647 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2648 ifr_buffer_get_length(ifr) - 1);
2650 free(descrbuf, M_IFDESCR);
2655 sx_xlock(&ifdescr_sx);
2656 odescrbuf = ifp->if_description;
2657 ifp->if_description = descrbuf;
2658 sx_xunlock(&ifdescr_sx);
2660 getmicrotime(&ifp->if_lastchange);
2661 free(odescrbuf, M_IFDESCR);
2665 ifr->ifr_fib = ifp->if_fib;
2669 error = priv_check(td, PRIV_NET_SETIFFIB);
2672 if (ifr->ifr_fib >= rt_numfibs)
2675 ifp->if_fib = ifr->ifr_fib;
2679 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2683 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2684 * check, so we don't need special handling here yet.
2686 new_flags = (ifr->ifr_flags & 0xffff) |
2687 (ifr->ifr_flagshigh << 16);
2688 if (ifp->if_flags & IFF_UP &&
2689 (new_flags & IFF_UP) == 0) {
2691 } else if (new_flags & IFF_UP &&
2692 (ifp->if_flags & IFF_UP) == 0) {
2695 /* See if permanently promiscuous mode bit is about to flip */
2696 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2697 if (new_flags & IFF_PPROMISC)
2698 ifp->if_flags |= IFF_PROMISC;
2699 else if (ifp->if_pcount == 0)
2700 ifp->if_flags &= ~IFF_PROMISC;
2701 if (log_promisc_mode_change)
2702 if_printf(ifp, "permanently promiscuous mode %s\n",
2703 ((new_flags & IFF_PPROMISC) ?
2704 "enabled" : "disabled"));
2706 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2707 (new_flags &~ IFF_CANTCHANGE);
2708 if (ifp->if_ioctl) {
2709 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2713 getmicrotime(&ifp->if_lastchange);
2717 error = priv_check(td, PRIV_NET_SETIFCAP);
2720 if (ifp->if_ioctl == NULL)
2721 return (EOPNOTSUPP);
2722 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2724 error = (*ifp->if_ioctl)(ifp, cmd, data);
2726 getmicrotime(&ifp->if_lastchange);
2731 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2736 error = priv_check(td, PRIV_NET_SETIFNAME);
2739 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2743 if (new_name[0] == '\0')
2745 if (new_name[IFNAMSIZ-1] != '\0') {
2746 new_name[IFNAMSIZ-1] = '\0';
2747 if (strlen(new_name) == IFNAMSIZ-1)
2750 if (strcmp(new_name, ifp->if_xname) == 0)
2752 if (ifunit(new_name) != NULL)
2756 * XXX: Locking. Nothing else seems to lock if_flags,
2757 * and there are numerous other races with the
2758 * ifunit() checks not being atomic with namespace
2759 * changes (renames, vmoves, if_attach, etc).
2761 ifp->if_flags |= IFF_RENAMING;
2763 /* Announce the departure of the interface. */
2764 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2765 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2767 if_printf(ifp, "changing name to '%s'\n", new_name);
2770 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2772 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2773 namelen = strlen(new_name);
2774 onamelen = sdl->sdl_nlen;
2776 * Move the address if needed. This is safe because we
2777 * allocate space for a name of length IFNAMSIZ when we
2778 * create this in if_attach().
2780 if (namelen != onamelen) {
2781 bcopy(sdl->sdl_data + onamelen,
2782 sdl->sdl_data + namelen, sdl->sdl_alen);
2784 bcopy(new_name, sdl->sdl_data, namelen);
2785 sdl->sdl_nlen = namelen;
2786 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2787 bzero(sdl->sdl_data, onamelen);
2788 while (namelen != 0)
2789 sdl->sdl_data[--namelen] = 0xff;
2790 IF_ADDR_WUNLOCK(ifp);
2792 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2793 /* Announce the return of the interface. */
2794 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2796 ifp->if_flags &= ~IFF_RENAMING;
2801 error = priv_check(td, PRIV_NET_SETIFVNET);
2804 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2809 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2812 ifp->if_metric = ifr->ifr_metric;
2813 getmicrotime(&ifp->if_lastchange);
2817 error = priv_check(td, PRIV_NET_SETIFPHYS);
2820 if (ifp->if_ioctl == NULL)
2821 return (EOPNOTSUPP);
2822 error = (*ifp->if_ioctl)(ifp, cmd, data);
2824 getmicrotime(&ifp->if_lastchange);
2829 u_long oldmtu = ifp->if_mtu;
2831 error = priv_check(td, PRIV_NET_SETIFMTU);
2834 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2836 if (ifp->if_ioctl == NULL)
2837 return (EOPNOTSUPP);
2838 error = (*ifp->if_ioctl)(ifp, cmd, data);
2840 getmicrotime(&ifp->if_lastchange);
2843 NETDUMP_REINIT(ifp);
2847 * If the link MTU changed, do network layer specific procedure.
2849 if (ifp->if_mtu != oldmtu) {
2860 if (cmd == SIOCADDMULTI)
2861 error = priv_check(td, PRIV_NET_ADDMULTI);
2863 error = priv_check(td, PRIV_NET_DELMULTI);
2867 /* Don't allow group membership on non-multicast interfaces. */
2868 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2869 return (EOPNOTSUPP);
2871 /* Don't let users screw up protocols' entries. */
2872 if (ifr->ifr_addr.sa_family != AF_LINK)
2875 if (cmd == SIOCADDMULTI) {
2876 struct ifmultiaddr *ifma;
2879 * Userland is only permitted to join groups once
2880 * via the if_addmulti() KPI, because it cannot hold
2881 * struct ifmultiaddr * between calls. It may also
2882 * lose a race while we check if the membership
2886 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2887 IF_ADDR_RUNLOCK(ifp);
2891 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2893 error = if_delmulti(ifp, &ifr->ifr_addr);
2896 getmicrotime(&ifp->if_lastchange);
2899 case SIOCSIFPHYADDR:
2900 case SIOCDIFPHYADDR:
2902 case SIOCSIFPHYADDR_IN6:
2905 case SIOCSIFGENERIC:
2906 error = priv_check(td, PRIV_NET_HWIOCTL);
2909 if (ifp->if_ioctl == NULL)
2910 return (EOPNOTSUPP);
2911 error = (*ifp->if_ioctl)(ifp, cmd, data);
2913 getmicrotime(&ifp->if_lastchange);
2917 case SIOCGIFPSRCADDR:
2918 case SIOCGIFPDSTADDR:
2921 case SIOCGIFGENERIC:
2923 case SIOCGIFRSSHASH:
2924 case SIOCGIFDOWNREASON:
2925 if (ifp->if_ioctl == NULL)
2926 return (EOPNOTSUPP);
2927 error = (*ifp->if_ioctl)(ifp, cmd, data);
2931 error = priv_check(td, PRIV_NET_SETLLADDR);
2934 error = if_setlladdr(ifp,
2935 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2939 error = if_gethwaddr(ifp, ifr);
2942 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2943 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2946 if ((error = if_addgroup(ifp,
2947 ifgr_group_get((struct ifgroupreq *)data))))
2951 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2952 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2956 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2957 error = priv_check(td, PRIV_NET_DELIFGROUP);
2960 if ((error = if_delgroup(ifp,
2961 ifgr_group_get((struct ifgroupreq *)data))))
2972 #ifdef COMPAT_FREEBSD32
2980 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2983 #ifdef COMPAT_FREEBSD32
2985 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2987 struct ifmediareq32 *ifmr32;
2989 ifmr32 = (struct ifmediareq32 *)data;
2990 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2991 sizeof(ifmr->ifm_name));
2992 ifmr->ifm_current = ifmr32->ifm_current;
2993 ifmr->ifm_mask = ifmr32->ifm_mask;
2994 ifmr->ifm_status = ifmr32->ifm_status;
2995 ifmr->ifm_active = ifmr32->ifm_active;
2996 ifmr->ifm_count = ifmr32->ifm_count;
2997 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
3001 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
3003 struct ifmediareq32 *ifmr32;
3005 ifmr32 = (struct ifmediareq32 *)data;
3006 ifmr32->ifm_current = ifmr->ifm_current;
3007 ifmr32->ifm_mask = ifmr->ifm_mask;
3008 ifmr32->ifm_status = ifmr->ifm_status;
3009 ifmr32->ifm_active = ifmr->ifm_active;
3010 ifmr32->ifm_count = ifmr->ifm_count;
3018 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
3020 #ifdef COMPAT_FREEBSD32
3021 caddr_t saved_data = NULL;
3022 struct ifmediareq ifmr;
3023 struct ifmediareq *ifmrp;
3033 CURVNET_SET(so->so_vnet);
3035 /* Make sure the VNET is stable. */
3036 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
3037 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3047 error = ifconf(cmd, data);
3051 #ifdef COMPAT_FREEBSD32
3054 struct ifconf32 *ifc32;
3057 ifc32 = (struct ifconf32 *)data;
3058 ifc.ifc_len = ifc32->ifc_len;
3059 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3061 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3064 ifc32->ifc_len = ifc.ifc_len;
3070 #ifdef COMPAT_FREEBSD32
3073 case SIOCGIFMEDIA32:
3074 case SIOCGIFXMEDIA32:
3076 ifmr_init(ifmrp, data);
3077 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3079 data = (caddr_t)ifmrp;
3083 ifr = (struct ifreq *)data;
3087 error = priv_check(td, PRIV_NET_SETIFVNET);
3089 error = if_vmove_reclaim(td, ifr->ifr_name,
3095 error = priv_check(td, PRIV_NET_IFCREATE);
3097 error = if_clone_create(ifr->ifr_name,
3098 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3099 ifr_data_get_ptr(ifr) : NULL);
3102 error = priv_check(td, PRIV_NET_IFDESTROY);
3104 error = if_clone_destroy(ifr->ifr_name);
3107 case SIOCIFGCLONERS:
3108 error = if_clone_list((struct if_clonereq *)data);
3111 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3112 error = if_getgroupmembers((struct ifgroupreq *)data);
3115 #if defined(INET) || defined(INET6)
3118 if (carp_ioctl_p == NULL)
3119 error = EPROTONOSUPPORT;
3121 error = (*carp_ioctl_p)(ifr, cmd, td);
3126 ifp = ifunit_ref(ifr->ifr_name);
3132 error = ifhwioctl(cmd, ifp, data, td);
3133 if (error != ENOIOCTL)
3136 oif_flags = ifp->if_flags;
3137 if (so->so_proto == NULL) {
3143 * Pass the request on to the socket control method, and if the
3144 * latter returns EOPNOTSUPP, directly to the interface.
3146 * Make an exception for the legacy SIOCSIF* requests. Drivers
3147 * trust SIOCSIFADDR et al to come from an already privileged
3148 * layer, and do not perform any credentials checks or input
3151 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3153 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3154 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3155 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3156 error = (*ifp->if_ioctl)(ifp, cmd, data);
3158 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3160 if (ifp->if_flags & IFF_UP)
3168 #ifdef COMPAT_FREEBSD32
3169 if (ifmrp != NULL) {
3170 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3171 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3174 ifmr_update(ifmrp, data);
3182 * The code common to handling reference counted flags,
3183 * e.g., in ifpromisc() and if_allmulti().
3184 * The "pflag" argument can specify a permanent mode flag to check,
3185 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3187 * Only to be used on stack-owned flags, not driver-owned flags.
3190 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3194 int oldflags, oldcount;
3196 /* Sanity checks to catch programming errors */
3197 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3198 ("%s: setting driver-owned flag %d", __func__, flag));
3201 KASSERT(*refcount >= 0,
3202 ("%s: increment negative refcount %d for flag %d",
3203 __func__, *refcount, flag));
3205 KASSERT(*refcount > 0,
3206 ("%s: decrement non-positive refcount %d for flag %d",
3207 __func__, *refcount, flag));
3209 /* In case this mode is permanent, just touch refcount */
3210 if (ifp->if_flags & pflag) {
3211 *refcount += onswitch ? 1 : -1;
3215 /* Save ifnet parameters for if_ioctl() may fail */
3216 oldcount = *refcount;
3217 oldflags = ifp->if_flags;
3220 * See if we aren't the only and touching refcount is enough.
3221 * Actually toggle interface flag if we are the first or last.
3226 ifp->if_flags |= flag;
3230 ifp->if_flags &= ~flag;
3233 /* Call down the driver since we've changed interface flags */
3234 if (ifp->if_ioctl == NULL) {
3238 ifr.ifr_flags = ifp->if_flags & 0xffff;
3239 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3240 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3243 /* Notify userland that interface flags have changed */
3248 /* Recover after driver error */
3249 *refcount = oldcount;
3250 ifp->if_flags = oldflags;
3255 * Set/clear promiscuous mode on interface ifp based on the truth value
3256 * of pswitch. The calls are reference counted so that only the first
3257 * "on" request actually has an effect, as does the final "off" request.
3258 * Results are undefined if the "off" and "on" requests are not matched.
3261 ifpromisc(struct ifnet *ifp, int pswitch)
3264 int oldflags = ifp->if_flags;
3266 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3267 &ifp->if_pcount, pswitch);
3268 /* If promiscuous mode status has changed, log a message */
3269 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3270 log_promisc_mode_change)
3271 if_printf(ifp, "promiscuous mode %s\n",
3272 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3277 * Return interface configuration
3278 * of system. List may be used
3279 * in later ioctl's (above) to get
3280 * other information.
3284 ifconf(u_long cmd, caddr_t data)
3286 struct ifconf *ifc = (struct ifconf *)data;
3291 int error, full = 0, valid_len, max_len;
3293 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3294 max_len = MAXPHYS - 1;
3296 /* Prevent hostile input from being able to crash the system */
3297 if (ifc->ifc_len <= 0)
3301 if (ifc->ifc_len <= max_len) {
3302 max_len = ifc->ifc_len;
3305 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3310 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3314 * Zero the ifr to make sure we don't disclose the contents
3317 memset(&ifr, 0, sizeof(ifr));
3319 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3320 >= sizeof(ifr.ifr_name)) {
3323 return (ENAMETOOLONG);
3328 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3329 struct sockaddr *sa = ifa->ifa_addr;
3331 if (prison_if(curthread->td_ucred, sa) != 0)
3334 if (sa->sa_len <= sizeof(*sa)) {
3335 if (sa->sa_len < sizeof(*sa)) {
3336 memset(&ifr.ifr_ifru.ifru_addr, 0,
3337 sizeof(ifr.ifr_ifru.ifru_addr));
3338 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3341 ifr.ifr_ifru.ifru_addr = *sa;
3342 sbuf_bcat(sb, &ifr, sizeof(ifr));
3343 max_len += sizeof(ifr);
3346 offsetof(struct ifreq, ifr_addr));
3347 max_len += offsetof(struct ifreq, ifr_addr);
3348 sbuf_bcat(sb, sa, sa->sa_len);
3349 max_len += sa->sa_len;
3352 if (sbuf_error(sb) == 0)
3353 valid_len = sbuf_len(sb);
3355 IF_ADDR_RUNLOCK(ifp);
3357 sbuf_bcat(sb, &ifr, sizeof(ifr));
3358 max_len += sizeof(ifr);
3360 if (sbuf_error(sb) == 0)
3361 valid_len = sbuf_len(sb);
3367 * If we didn't allocate enough space (uncommon), try again. If
3368 * we have already allocated as much space as we are allowed,
3369 * return what we've got.
3371 if (valid_len != max_len && !full) {
3376 ifc->ifc_len = valid_len;
3378 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3384 * Just like ifpromisc(), but for all-multicast-reception mode.
3387 if_allmulti(struct ifnet *ifp, int onswitch)
3390 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3393 struct ifmultiaddr *
3394 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3396 struct ifmultiaddr *ifma;
3398 IF_ADDR_LOCK_ASSERT(ifp);
3400 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3401 if (sa->sa_family == AF_LINK) {
3402 if (sa_dl_equal(ifma->ifma_addr, sa))
3405 if (sa_equal(ifma->ifma_addr, sa))
3414 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3415 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3416 * the ifnet multicast address list here, so the caller must do that and
3417 * other setup work (such as notifying the device driver). The reference
3418 * count is initialized to 1.
3420 static struct ifmultiaddr *
3421 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3424 struct ifmultiaddr *ifma;
3425 struct sockaddr *dupsa;
3427 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3432 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3433 if (dupsa == NULL) {
3434 free(ifma, M_IFMADDR);
3437 bcopy(sa, dupsa, sa->sa_len);
3438 ifma->ifma_addr = dupsa;
3440 ifma->ifma_ifp = ifp;
3441 ifma->ifma_refcount = 1;
3442 ifma->ifma_protospec = NULL;
3445 ifma->ifma_lladdr = NULL;
3449 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3450 if (dupsa == NULL) {
3451 free(ifma->ifma_addr, M_IFMADDR);
3452 free(ifma, M_IFMADDR);
3455 bcopy(llsa, dupsa, llsa->sa_len);
3456 ifma->ifma_lladdr = dupsa;
3462 * if_freemulti: free ifmultiaddr structure and possibly attached related
3463 * addresses. The caller is responsible for implementing reference
3464 * counting, notifying the driver, handling routing messages, and releasing
3465 * any dependent link layer state.
3467 #ifdef MCAST_VERBOSE
3468 extern void kdb_backtrace(void);
3471 if_freemulti_internal(struct ifmultiaddr *ifma)
3474 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3475 ifma->ifma_refcount));
3477 if (ifma->ifma_lladdr != NULL)
3478 free(ifma->ifma_lladdr, M_IFMADDR);
3479 #ifdef MCAST_VERBOSE
3481 printf("%s freeing ifma: %p\n", __func__, ifma);
3483 free(ifma->ifma_addr, M_IFMADDR);
3484 free(ifma, M_IFMADDR);
3488 if_destroymulti(epoch_context_t ctx)
3490 struct ifmultiaddr *ifma;
3492 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3493 if_freemulti_internal(ifma);
3497 if_freemulti(struct ifmultiaddr *ifma)
3499 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3500 ifma->ifma_refcount));
3502 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3507 * Register an additional multicast address with a network interface.
3509 * - If the address is already present, bump the reference count on the
3510 * address and return.
3511 * - If the address is not link-layer, look up a link layer address.
3512 * - Allocate address structures for one or both addresses, and attach to the
3513 * multicast address list on the interface. If automatically adding a link
3514 * layer address, the protocol address will own a reference to the link
3515 * layer address, to be freed when it is freed.
3516 * - Notify the network device driver of an addition to the multicast address
3519 * 'sa' points to caller-owned memory with the desired multicast address.
3521 * 'retifma' will be used to return a pointer to the resulting multicast
3522 * address reference, if desired.
3525 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3526 struct ifmultiaddr **retifma)
3528 struct ifmultiaddr *ifma, *ll_ifma;
3529 struct sockaddr *llsa;
3530 struct sockaddr_dl sdl;
3534 IN_MULTI_LIST_UNLOCK_ASSERT();
3537 IN6_MULTI_LIST_UNLOCK_ASSERT();
3540 * If the address is already present, return a new reference to it;
3541 * otherwise, allocate storage and set up a new address.
3544 ifma = if_findmulti(ifp, sa);
3546 ifma->ifma_refcount++;
3547 if (retifma != NULL)
3549 IF_ADDR_WUNLOCK(ifp);
3554 * The address isn't already present; resolve the protocol address
3555 * into a link layer address, and then look that up, bump its
3556 * refcount or allocate an ifma for that also.
3557 * Most link layer resolving functions returns address data which
3558 * fits inside default sockaddr_dl structure. However callback
3559 * can allocate another sockaddr structure, in that case we need to
3564 if (ifp->if_resolvemulti != NULL) {
3565 /* Provide called function with buffer size information */
3566 sdl.sdl_len = sizeof(sdl);
3567 llsa = (struct sockaddr *)&sdl;
3568 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3574 * Allocate the new address. Don't hook it up yet, as we may also
3575 * need to allocate a link layer multicast address.
3577 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3584 * If a link layer address is found, we'll need to see if it's
3585 * already present in the address list, or allocate is as well.
3586 * When this block finishes, the link layer address will be on the
3590 ll_ifma = if_findmulti(ifp, llsa);
3591 if (ll_ifma == NULL) {
3592 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3593 if (ll_ifma == NULL) {
3594 --ifma->ifma_refcount;
3599 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3600 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3603 ll_ifma->ifma_refcount++;
3604 ifma->ifma_llifma = ll_ifma;
3608 * We now have a new multicast address, ifma, and possibly a new or
3609 * referenced link layer address. Add the primary address to the
3610 * ifnet address list.
3612 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3613 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3615 if (retifma != NULL)
3619 * Must generate the message while holding the lock so that 'ifma'
3620 * pointer is still valid.
3622 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3623 IF_ADDR_WUNLOCK(ifp);
3626 * We are certain we have added something, so call down to the
3627 * interface to let them know about it.
3629 if (ifp->if_ioctl != NULL) {
3630 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3633 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3634 link_free_sdl(llsa);
3639 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3640 link_free_sdl(llsa);
3643 IF_ADDR_WUNLOCK(ifp);
3648 * Delete a multicast group membership by network-layer group address.
3650 * Returns ENOENT if the entry could not be found. If ifp no longer
3651 * exists, results are undefined. This entry point should only be used
3652 * from subsystems which do appropriate locking to hold ifp for the
3653 * duration of the call.
3654 * Network-layer protocol domains must use if_delmulti_ifma().
3657 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3659 struct ifmultiaddr *ifma;
3664 IFNET_RLOCK_NOSLEEP();
3665 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3670 IFNET_RUNLOCK_NOSLEEP();
3672 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3679 ifma = if_findmulti(ifp, sa);
3681 lastref = if_delmulti_locked(ifp, ifma, 0);
3682 IF_ADDR_WUNLOCK(ifp);
3687 if (lastref && ifp->if_ioctl != NULL) {
3688 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3695 * Delete all multicast group membership for an interface.
3696 * Should be used to quickly flush all multicast filters.
3699 if_delallmulti(struct ifnet *ifp)
3701 struct ifmultiaddr *ifma;
3702 struct ifmultiaddr *next;
3705 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3706 if_delmulti_locked(ifp, ifma, 0);
3707 IF_ADDR_WUNLOCK(ifp);
3711 if_delmulti_ifma(struct ifmultiaddr *ifma)
3713 if_delmulti_ifma_flags(ifma, 0);
3717 * Delete a multicast group membership by group membership pointer.
3718 * Network-layer protocol domains must use this routine.
3720 * It is safe to call this routine if the ifp disappeared.
3723 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3727 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3729 IN_MULTI_LIST_UNLOCK_ASSERT();
3731 ifp = ifma->ifma_ifp;
3734 printf("%s: ifma_ifp seems to be detached\n", __func__);
3738 IFNET_RLOCK_NOSLEEP();
3739 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3744 IFNET_RUNLOCK_NOSLEEP();
3748 * If and only if the ifnet instance exists: Acquire the address lock.
3753 lastref = if_delmulti_locked(ifp, ifma, flags);
3757 * If and only if the ifnet instance exists:
3758 * Release the address lock.
3759 * If the group was left: update the hardware hash filter.
3761 IF_ADDR_WUNLOCK(ifp);
3762 if (lastref && ifp->if_ioctl != NULL) {
3763 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3769 * Perform deletion of network-layer and/or link-layer multicast address.
3771 * Return 0 if the reference count was decremented.
3772 * Return 1 if the final reference was released, indicating that the
3773 * hardware hash filter should be reprogrammed.
3776 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3778 struct ifmultiaddr *ll_ifma;
3780 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3781 KASSERT(ifma->ifma_ifp == ifp,
3782 ("%s: inconsistent ifp %p", __func__, ifp));
3783 IF_ADDR_WLOCK_ASSERT(ifp);
3786 ifp = ifma->ifma_ifp;
3787 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3790 * If the ifnet is detaching, null out references to ifnet,
3791 * so that upper protocol layers will notice, and not attempt
3792 * to obtain locks for an ifnet which no longer exists. The
3793 * routing socket announcement must happen before the ifnet
3794 * instance is detached from the system.
3798 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3801 * ifp may already be nulled out if we are being reentered
3802 * to delete the ll_ifma.
3805 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3806 ifma->ifma_ifp = NULL;
3810 if (--ifma->ifma_refcount > 0)
3813 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3814 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3815 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3818 * If this ifma is a network-layer ifma, a link-layer ifma may
3819 * have been associated with it. Release it first if so.
3821 ll_ifma = ifma->ifma_llifma;
3822 if (ll_ifma != NULL) {
3823 KASSERT(ifma->ifma_lladdr != NULL,
3824 ("%s: llifma w/o lladdr", __func__));
3826 ll_ifma->ifma_ifp = NULL; /* XXX */
3827 if (--ll_ifma->ifma_refcount == 0) {
3829 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3830 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3832 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3835 if_freemulti(ll_ifma);
3840 struct ifmultiaddr *ifmatmp;
3842 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3843 MPASS(ifma != ifmatmp);
3848 * The last reference to this instance of struct ifmultiaddr
3849 * was released; the hardware should be notified of this change.
3855 * Set the link layer address on an interface.
3857 * At this time we only support certain types of interfaces,
3858 * and we don't allow the length of the address to change.
3860 * Set noinline to be dtrace-friendly
3863 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3865 struct sockaddr_dl *sdl;
3878 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3883 if (len != sdl->sdl_alen) { /* don't allow length to change */
3887 switch (ifp->if_type) {
3892 case IFT_IEEE8023ADLAG:
3893 bcopy(lladdr, LLADDR(sdl), len);
3901 * If the interface is already up, we need
3902 * to re-init it in order to reprogram its
3906 if ((ifp->if_flags & IFF_UP) != 0) {
3907 if (ifp->if_ioctl) {
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);
3912 ifp->if_flags |= IFF_UP;
3913 ifr.ifr_flags = ifp->if_flags & 0xffff;
3914 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3915 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3918 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3926 * Compat function for handling basic encapsulation requests.
3927 * Not converted stacks (FDDI, IB, ..) supports traditional
3928 * output model: ARP (and other similar L2 protocols) are handled
3929 * inside output routine, arpresolve/nd6_resolve() returns MAC
3930 * address instead of full prepend.
3932 * This function creates calculated header==MAC for IPv4/IPv6 and
3933 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3937 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3940 if (req->rtype != IFENCAP_LL)
3941 return (EOPNOTSUPP);
3943 if (req->bufsize < req->lladdr_len)
3946 switch (req->family) {
3951 return (EAFNOSUPPORT);
3954 /* Copy lladdr to storage as is */
3955 memmove(req->buf, req->lladdr, req->lladdr_len);
3956 req->bufsize = req->lladdr_len;
3957 req->lladdr_off = 0;
3963 * Tunnel interfaces can nest, also they may cause infinite recursion
3964 * calls when misconfigured. We'll prevent this by detecting loops.
3965 * High nesting level may cause stack exhaustion. We'll prevent this
3966 * by introducing upper limit.
3968 * Return 0, if tunnel nesting count is equal or less than limit.
3971 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3979 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3980 if (*(struct ifnet **)(mtag + 1) == ifp) {
3981 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3986 if (count > limit) {
3988 "%s: if_output recursively called too many times(%d)\n",
3989 if_name(ifp), count);
3992 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3995 *(struct ifnet **)(mtag + 1) = ifp;
3996 m_tag_prepend(m, mtag);
4001 * Get the link layer address that was read from the hardware at attach.
4003 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4004 * their component interfaces as IFT_IEEE8023ADLAG.
4007 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4010 if (ifp->if_hw_addr == NULL)
4013 switch (ifp->if_type) {
4015 case IFT_IEEE8023ADLAG:
4016 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4024 * The name argument must be a pointer to storage which will last as
4025 * long as the interface does. For physical devices, the result of
4026 * device_get_name(dev) is a good choice and for pseudo-devices a
4027 * static string works well.
4030 if_initname(struct ifnet *ifp, const char *name, int unit)
4032 ifp->if_dname = name;
4033 ifp->if_dunit = unit;
4034 if (unit != IF_DUNIT_NONE)
4035 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4037 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4041 if_printf(struct ifnet *ifp, const char *fmt, ...)
4046 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4048 vlog(LOG_INFO, if_fmt, ap);
4054 if_start(struct ifnet *ifp)
4057 (*(ifp)->if_start)(ifp);
4061 * Backwards compatibility interface for drivers
4062 * that have not implemented it
4065 if_transmit(struct ifnet *ifp, struct mbuf *m)
4069 IFQ_HANDOFF(ifp, m, error);
4074 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4081 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4086 if (_IF_QFULL(ifq)) {
4088 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4093 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4094 if (m->m_flags & (M_BCAST|M_MCAST))
4095 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4096 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4098 _IF_ENQUEUE(ifq, m);
4100 if (ifp != NULL && !active)
4101 (*(ifp)->if_start)(ifp);
4106 if_register_com_alloc(u_char type,
4107 if_com_alloc_t *a, if_com_free_t *f)
4110 KASSERT(if_com_alloc[type] == NULL,
4111 ("if_register_com_alloc: %d already registered", type));
4112 KASSERT(if_com_free[type] == NULL,
4113 ("if_register_com_alloc: %d free already registered", type));
4115 if_com_alloc[type] = a;
4116 if_com_free[type] = f;
4120 if_deregister_com_alloc(u_char type)
4123 KASSERT(if_com_alloc[type] != NULL,
4124 ("if_deregister_com_alloc: %d not registered", type));
4125 KASSERT(if_com_free[type] != NULL,
4126 ("if_deregister_com_alloc: %d free not registered", type));
4127 if_com_alloc[type] = NULL;
4128 if_com_free[type] = NULL;
4131 /* API for driver access to network stack owned ifnet.*/
4133 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4137 oldbrate = ifp->if_baudrate;
4138 ifp->if_baudrate = baudrate;
4143 if_getbaudrate(if_t ifp)
4146 return (((struct ifnet *)ifp)->if_baudrate);
4150 if_setcapabilities(if_t ifp, int capabilities)
4152 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4157 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4159 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4160 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4166 if_getcapabilities(if_t ifp)
4168 return ((struct ifnet *)ifp)->if_capabilities;
4172 if_setcapenable(if_t ifp, int capabilities)
4174 ((struct ifnet *)ifp)->if_capenable = capabilities;
4179 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4182 ((struct ifnet *)ifp)->if_capenable |= setcap;
4184 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4190 if_getdname(if_t ifp)
4192 return ((struct ifnet *)ifp)->if_dname;
4196 if_togglecapenable(if_t ifp, int togglecap)
4198 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4203 if_getcapenable(if_t ifp)
4205 return ((struct ifnet *)ifp)->if_capenable;
4209 * This is largely undesirable because it ties ifnet to a device, but does
4210 * provide flexiblity for an embedded product vendor. Should be used with
4211 * the understanding that it violates the interface boundaries, and should be
4212 * a last resort only.
4215 if_setdev(if_t ifp, void *dev)
4221 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4223 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4224 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4230 if_getdrvflags(if_t ifp)
4232 return ((struct ifnet *)ifp)->if_drv_flags;
4236 if_setdrvflags(if_t ifp, int flags)
4238 ((struct ifnet *)ifp)->if_drv_flags = flags;
4244 if_setflags(if_t ifp, int flags)
4246 ((struct ifnet *)ifp)->if_flags = flags;
4251 if_setflagbits(if_t ifp, int set, int clear)
4253 ((struct ifnet *)ifp)->if_flags |= set;
4254 ((struct ifnet *)ifp)->if_flags &= ~clear;
4260 if_getflags(if_t ifp)
4262 return ((struct ifnet *)ifp)->if_flags;
4266 if_clearhwassist(if_t ifp)
4268 ((struct ifnet *)ifp)->if_hwassist = 0;
4273 if_sethwassistbits(if_t ifp, int toset, int toclear)
4275 ((struct ifnet *)ifp)->if_hwassist |= toset;
4276 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4282 if_sethwassist(if_t ifp, int hwassist_bit)
4284 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4289 if_gethwassist(if_t ifp)
4291 return ((struct ifnet *)ifp)->if_hwassist;
4295 if_setmtu(if_t ifp, int mtu)
4297 ((struct ifnet *)ifp)->if_mtu = mtu;
4304 return ((struct ifnet *)ifp)->if_mtu;
4308 if_getmtu_family(if_t ifp, int family)
4312 for (dp = domains; dp; dp = dp->dom_next) {
4313 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4314 return (dp->dom_ifmtu((struct ifnet *)ifp));
4317 return (((struct ifnet *)ifp)->if_mtu);
4321 * Methods for drivers to access interface unicast and multicast
4322 * link level addresses. Driver shall not know 'struct ifaddr' neither
4323 * 'struct ifmultiaddr'.
4326 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4335 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4336 if (ifa->ifa_addr->sa_family != AF_LINK)
4338 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4341 IF_ADDR_RUNLOCK(ifp);
4347 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4349 struct ifmultiaddr *ifma;
4356 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4357 if (ifma->ifma_addr->sa_family != AF_LINK)
4359 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4362 IF_ADDR_RUNLOCK(ifp);
4368 if_setsoftc(if_t ifp, void *softc)
4370 ((struct ifnet *)ifp)->if_softc = softc;
4375 if_getsoftc(if_t ifp)
4377 return ((struct ifnet *)ifp)->if_softc;
4381 if_setrcvif(struct mbuf *m, if_t ifp)
4383 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4387 if_setvtag(struct mbuf *m, uint16_t tag)
4389 m->m_pkthdr.ether_vtag = tag;
4393 if_getvtag(struct mbuf *m)
4396 return (m->m_pkthdr.ether_vtag);
4400 if_sendq_empty(if_t ifp)
4402 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4406 if_getifaddr(if_t ifp)
4408 return ((struct ifnet *)ifp)->if_addr;
4412 if_getamcount(if_t ifp)
4414 return ((struct ifnet *)ifp)->if_amcount;
4419 if_setsendqready(if_t ifp)
4421 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4426 if_setsendqlen(if_t ifp, int tx_desc_count)
4428 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4429 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4435 if_vlantrunkinuse(if_t ifp)
4437 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4441 if_input(if_t ifp, struct mbuf* sendmp)
4443 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4449 #ifndef ETH_ADDR_LEN
4450 #define ETH_ADDR_LEN 6
4454 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4456 struct ifmultiaddr *ifma;
4457 uint8_t *lmta = (uint8_t *)mta;
4460 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4461 if (ifma->ifma_addr->sa_family != AF_LINK)
4467 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4468 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4477 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4481 if_maddr_rlock(ifp);
4482 error = if_setupmultiaddr(ifp, mta, cnt, max);
4483 if_maddr_runlock(ifp);
4488 if_multiaddr_count(if_t ifp, int max)
4490 struct ifmultiaddr *ifma;
4494 if_maddr_rlock(ifp);
4495 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4496 if (ifma->ifma_addr->sa_family != AF_LINK)
4502 if_maddr_runlock(ifp);
4507 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4509 struct ifmultiaddr *ifma;
4512 if_maddr_rlock(ifp);
4513 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4514 cnt += filter(arg, ifma, cnt);
4515 if_maddr_runlock(ifp);
4520 if_dequeue(if_t ifp)
4523 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4529 if_sendq_prepend(if_t ifp, struct mbuf *m)
4531 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4536 if_setifheaderlen(if_t ifp, int len)
4538 ((struct ifnet *)ifp)->if_hdrlen = len;
4543 if_getlladdr(if_t ifp)
4545 return (IF_LLADDR((struct ifnet *)ifp));
4549 if_gethandle(u_char type)
4551 return (if_alloc(type));
4555 if_bpfmtap(if_t ifh, struct mbuf *m)
4557 struct ifnet *ifp = (struct ifnet *)ifh;
4563 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4565 struct ifnet *ifp = (struct ifnet *)ifh;
4567 ETHER_BPF_MTAP(ifp, m);
4571 if_vlancap(if_t ifh)
4573 struct ifnet *ifp = (struct ifnet *)ifh;
4574 VLAN_CAPABILITIES(ifp);
4578 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4581 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4586 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4589 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4594 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4597 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4602 if_gethwtsomax(if_t ifp)
4605 return (((struct ifnet *)ifp)->if_hw_tsomax);
4609 if_gethwtsomaxsegcount(if_t ifp)
4612 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4616 if_gethwtsomaxsegsize(if_t ifp)
4619 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4623 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4625 ((struct ifnet *)ifp)->if_init = init_fn;
4629 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4631 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4635 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4637 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4641 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4643 ((struct ifnet *)ifp)->if_transmit = start_fn;
4646 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4648 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4653 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4656 ifp->if_get_counter = fn;
4659 /* Revisit these - These are inline functions originally. */
4661 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4663 return drbr_inuse(ifh, br);
4667 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4669 return drbr_dequeue(ifh, br);
4673 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4675 return drbr_needs_enqueue(ifh, br);
4679 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4681 return drbr_enqueue(ifh, br, m);