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
35 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
42 #include <sys/domainset.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>
67 #include <machine/stdarg.h>
71 #include <net/ethernet.h>
73 #include <net/if_arp.h>
74 #include <net/if_clone.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
77 #include <net/if_var.h>
78 #include <net/if_media.h>
79 #include <net/if_vlan_var.h>
80 #include <net/radix.h>
81 #include <net/route.h>
84 #if defined(INET) || defined(INET6)
85 #include <net/ethernet.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_carp.h>
91 #include <netinet/if_ether.h>
92 #include <netinet/netdump/netdump.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/in6_ifattach.h>
98 #endif /* INET || INET6 */
100 #include <security/mac/mac_framework.h>
103 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
104 * and ifr_ifru when it is used in SIOCGIFCONF.
106 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
107 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
109 __read_mostly epoch_t net_epoch_preempt;
110 __read_mostly epoch_t net_epoch;
111 #ifdef COMPAT_FREEBSD32
112 #include <sys/mount.h>
113 #include <compat/freebsd32/freebsd32.h>
115 struct ifreq_buffer32 {
116 uint32_t length; /* (size_t) */
117 uint32_t buffer; /* (void *) */
121 * Interface request structure used for socket
122 * ioctl's. All interface ioctl's must have parameter
123 * definitions which begin with ifr_name. The
124 * remainder may be interface specific.
127 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
129 struct sockaddr ifru_addr;
130 struct sockaddr ifru_dstaddr;
131 struct sockaddr ifru_broadaddr;
132 struct ifreq_buffer32 ifru_buffer;
143 u_char ifru_vlan_pcp;
146 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
147 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
148 __offsetof(struct ifreq32, ifr_ifru));
150 struct ifgroupreq32 {
151 char ifgr_name[IFNAMSIZ];
154 char ifgru_group[IFNAMSIZ];
155 uint32_t ifgru_groups;
159 struct ifmediareq32 {
160 char ifm_name[IFNAMSIZ];
166 uint32_t ifm_ulist; /* (int *) */
168 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
169 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
171 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
172 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
173 #else /* !COMPAT_FREEBSD32 */
174 #define _CASE_IOC_IFGROUPREQ_32(cmd)
175 #endif /* !COMPAT_FREEBSD32 */
177 #define CASE_IOC_IFGROUPREQ(cmd) \
178 _CASE_IOC_IFGROUPREQ_32(cmd) \
183 #ifdef COMPAT_FREEBSD32
184 struct ifreq32 ifr32;
188 union ifgroupreq_union {
189 struct ifgroupreq ifgr;
190 #ifdef COMPAT_FREEBSD32
191 struct ifgroupreq32 ifgr32;
195 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
196 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
198 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
199 &ifqmaxlen, 0, "max send queue size");
201 /* Log link state change events */
202 static int log_link_state_change = 1;
204 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
205 &log_link_state_change, 0,
206 "log interface link state change events");
208 /* Log promiscuous mode change events */
209 static int log_promisc_mode_change = 1;
211 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
212 &log_promisc_mode_change, 1,
213 "log promiscuous mode change events");
215 /* Interface description */
216 static unsigned int ifdescr_maxlen = 1024;
217 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
219 "administrative maximum length for interface description");
221 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
223 /* global sx for non-critical path ifdescr */
224 static struct sx ifdescr_sx;
225 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
227 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
228 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
229 /* These are external hooks for CARP. */
230 void (*carp_linkstate_p)(struct ifnet *ifp);
231 void (*carp_demote_adj_p)(int, char *);
232 int (*carp_master_p)(struct ifaddr *);
233 #if defined(INET) || defined(INET6)
234 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
235 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
236 const struct sockaddr *sa);
237 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
238 int (*carp_attach_p)(struct ifaddr *, int);
239 void (*carp_detach_p)(struct ifaddr *, bool);
242 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
245 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
246 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
247 const struct in6_addr *taddr);
250 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
253 * XXX: Style; these should be sorted alphabetically, and unprototyped
254 * static functions should be prototyped. Currently they are sorted by
257 static void if_attachdomain(void *);
258 static void if_attachdomain1(struct ifnet *);
259 static int ifconf(u_long, caddr_t);
260 static void *if_grow(void);
261 static void if_input_default(struct ifnet *, struct mbuf *);
262 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
263 static void if_route(struct ifnet *, int flag, int fam);
264 static int if_setflag(struct ifnet *, int, int, int *, int);
265 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
266 static void if_unroute(struct ifnet *, int flag, int fam);
267 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
268 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
269 static void do_link_state_change(void *, int);
270 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
271 static int if_getgroupmembers(struct ifgroupreq *);
272 static void if_delgroups(struct ifnet *);
273 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
274 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
276 static void if_vmove(struct ifnet *, struct vnet *);
281 * XXX: declare here to avoid to include many inet6 related files..
282 * should be more generalized?
284 extern void nd6_setmtu(struct ifnet *);
287 /* ipsec helper hooks */
288 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
289 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
291 VNET_DEFINE(int, if_index);
292 int ifqmaxlen = IFQ_MAXLEN;
293 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
294 VNET_DEFINE(struct ifgrouphead, ifg_head);
296 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
298 /* Table of ifnet by index. */
299 VNET_DEFINE(struct ifnet **, ifindex_table);
301 #define V_if_indexlim VNET(if_indexlim)
302 #define V_ifindex_table VNET(ifindex_table)
305 * The global network interface list (V_ifnet) and related state (such as
306 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
307 * an rwlock. Either may be acquired shared to stablize the list, but both
308 * must be acquired writable to modify the list. This model allows us to
309 * both stablize the interface list during interrupt thread processing, but
310 * also to stablize it over long-running ioctls, without introducing priority
311 * inversions and deadlocks.
313 struct rwlock ifnet_rwlock;
314 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
315 struct sx ifnet_sxlock;
316 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
319 * The allocation of network interfaces is a rather non-atomic affair; we
320 * need to select an index before we are ready to expose the interface for
321 * use, so will use this pointer value to indicate reservation.
323 #define IFNET_HOLD (void *)(uintptr_t)(-1)
325 static if_com_alloc_t *if_com_alloc[256];
326 static if_com_free_t *if_com_free[256];
328 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
329 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
330 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
333 ifnet_byindex_locked(u_short idx)
336 if (idx > V_if_index)
338 if (V_ifindex_table[idx] == IFNET_HOLD)
340 return (V_ifindex_table[idx]);
344 ifnet_byindex(u_short idx)
348 ifp = ifnet_byindex_locked(idx);
353 ifnet_byindex_ref(u_short idx)
355 struct epoch_tracker et;
359 ifp = ifnet_byindex_locked(idx);
360 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
370 * Allocate an ifindex array entry; return 0 on success or an error on
374 ifindex_alloc(void **old)
378 IFNET_WLOCK_ASSERT();
380 * Try to find an empty slot below V_if_index. If we fail, take the
383 for (idx = 1; idx <= V_if_index; idx++) {
384 if (V_ifindex_table[idx] == NULL)
388 /* Catch if_index overflow. */
389 if (idx >= V_if_indexlim) {
393 if (idx > V_if_index)
399 ifindex_free_locked(u_short idx)
402 IFNET_WLOCK_ASSERT();
404 V_ifindex_table[idx] = NULL;
405 while (V_if_index > 0 &&
406 V_ifindex_table[V_if_index] == NULL)
411 ifindex_free(u_short idx)
415 ifindex_free_locked(idx);
420 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
423 V_ifindex_table[idx] = ifp;
427 ifaddr_byindex(u_short idx)
429 struct epoch_tracker et;
431 struct ifaddr *ifa = NULL;
434 ifp = ifnet_byindex_locked(idx);
435 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
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_domain(u_char type, int numa_domain)
536 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
537 if (numa_domain == IF_NODOM)
538 ifp = malloc(sizeof(struct ifnet), M_IFNET,
541 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
542 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
545 idx = ifindex_alloc(&old);
546 if (__predict_false(idx == USHRT_MAX)) {
548 epoch_wait_preempt(net_epoch_preempt);
552 ifnet_setbyindex(idx, IFNET_HOLD);
556 ifp->if_alloctype = type;
557 ifp->if_numa_domain = numa_domain;
559 ifp->if_vnet = curvnet;
561 if (if_com_alloc[type] != NULL) {
562 ifp->if_l2com = if_com_alloc[type](type, ifp);
563 if (ifp->if_l2com == NULL) {
570 IF_ADDR_LOCK_INIT(ifp);
571 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
572 ifp->if_afdata_initialized = 0;
573 IF_AFDATA_LOCK_INIT(ifp);
574 CK_STAILQ_INIT(&ifp->if_addrhead);
575 CK_STAILQ_INIT(&ifp->if_multiaddrs);
576 CK_STAILQ_INIT(&ifp->if_groups);
580 ifq_init(&ifp->if_snd, ifp);
582 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
583 for (int i = 0; i < IFCOUNTERS; i++)
584 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
585 ifp->if_get_counter = if_get_counter_default;
586 ifp->if_pcp = IFNET_PCP_NONE;
587 ifnet_setbyindex(ifp->if_index, ifp);
592 if_alloc_dev(u_char type, device_t dev)
596 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
597 return (if_alloc_domain(type, IF_NODOM));
598 return (if_alloc_domain(type, numa_domain));
602 if_alloc(u_char type)
605 return (if_alloc_domain(type, IF_NODOM));
608 * Do the actual work of freeing a struct ifnet, and layer 2 common
609 * structure. This call is made when the last reference to an
610 * interface is released.
613 if_free_internal(struct ifnet *ifp)
616 KASSERT((ifp->if_flags & IFF_DYING),
617 ("if_free_internal: interface not dying"));
619 if (if_com_free[ifp->if_alloctype] != NULL)
620 if_com_free[ifp->if_alloctype](ifp->if_l2com,
624 mac_ifnet_destroy(ifp);
626 IF_AFDATA_DESTROY(ifp);
627 IF_ADDR_LOCK_DESTROY(ifp);
628 ifq_delete(&ifp->if_snd);
630 for (int i = 0; i < IFCOUNTERS; i++)
631 counter_u64_free(ifp->if_counters[i]);
633 free(ifp->if_description, M_IFDESCR);
634 free(ifp->if_hw_addr, M_IFADDR);
635 if (ifp->if_numa_domain == IF_NODOM)
638 free_domain(ifp, M_IFNET);
642 if_destroy(epoch_context_t ctx)
646 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
647 if_free_internal(ifp);
651 * Deregister an interface and free the associated storage.
654 if_free(struct ifnet *ifp)
657 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
659 CURVNET_SET_QUIET(ifp->if_vnet);
661 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
662 ("%s: freeing unallocated ifnet", ifp->if_xname));
664 ifindex_free_locked(ifp->if_index);
667 if (refcount_release(&ifp->if_refcount))
668 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
673 * Interfaces to keep an ifnet type-stable despite the possibility of the
674 * driver calling if_free(). If there are additional references, we defer
675 * freeing the underlying data structure.
678 if_ref(struct ifnet *ifp)
681 /* We don't assert the ifnet list lock here, but arguably should. */
682 refcount_acquire(&ifp->if_refcount);
686 if_rele(struct ifnet *ifp)
689 if (!refcount_release(&ifp->if_refcount))
691 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
695 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
698 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
700 if (ifq->ifq_maxlen == 0)
701 ifq->ifq_maxlen = ifqmaxlen;
704 ifq->altq_disc = NULL;
705 ifq->altq_flags &= ALTQF_CANTCHANGE;
706 ifq->altq_tbr = NULL;
711 ifq_delete(struct ifaltq *ifq)
713 mtx_destroy(&ifq->ifq_mtx);
717 * Perform generic interface initialization tasks and attach the interface
718 * to the list of "active" interfaces. If vmove flag is set on entry
719 * to if_attach_internal(), perform only a limited subset of initialization
720 * tasks, given that we are moving from one vnet to another an ifnet which
721 * has already been fully initialized.
723 * Note that if_detach_internal() removes group membership unconditionally
724 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
725 * Thus, when if_vmove() is applied to a cloned interface, group membership
726 * is lost while a cloned one always joins a group whose name is
727 * ifc->ifc_name. To recover this after if_detach_internal() and
728 * if_attach_internal(), the cloner should be specified to
729 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
730 * attempts to join a group whose name is ifc->ifc_name.
733 * - The decision to return void and thus require this function to
734 * succeed is questionable.
735 * - We should probably do more sanity checking. For instance we don't
736 * do anything to insure if_xname is unique or non-empty.
739 if_attach(struct ifnet *ifp)
742 if_attach_internal(ifp, 0, NULL);
746 * Compute the least common TSO limit.
749 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
752 * 1) If there is no limit currently, take the limit from
753 * the network adapter.
755 * 2) If the network adapter has a limit below the current
758 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
759 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
760 pmax->tsomaxbytes = ifp->if_hw_tsomax;
762 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
763 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
764 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
766 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
767 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
768 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
773 * Update TSO limit of a network adapter.
775 * Returns zero if no change. Else non-zero.
778 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
781 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
782 ifp->if_hw_tsomax = pmax->tsomaxbytes;
785 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
786 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
789 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
790 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
797 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
799 unsigned socksize, ifasize;
800 int namelen, masklen;
801 struct sockaddr_dl *sdl;
804 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
805 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
809 ifp->if_vnet = curvnet;
810 if (ifp->if_home_vnet == NULL)
811 ifp->if_home_vnet = curvnet;
814 if_addgroup(ifp, IFG_ALL);
816 /* Restore group membership for cloned interfaces. */
817 if (vmove && ifc != NULL)
818 if_clone_addgroup(ifp, ifc);
820 getmicrotime(&ifp->if_lastchange);
821 ifp->if_epoch = time_uptime;
823 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
824 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
825 ("transmit and qflush must both either be set or both be NULL"));
826 if (ifp->if_transmit == NULL) {
827 ifp->if_transmit = if_transmit;
828 ifp->if_qflush = if_qflush;
830 if (ifp->if_input == NULL)
831 ifp->if_input = if_input_default;
833 if (ifp->if_requestencap == NULL)
834 ifp->if_requestencap = if_requestencap_default;
838 mac_ifnet_create(ifp);
842 * Create a Link Level name for this device.
844 namelen = strlen(ifp->if_xname);
846 * Always save enough space for any possiable name so we
847 * can do a rename in place later.
849 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
850 socksize = masklen + ifp->if_addrlen;
851 if (socksize < sizeof(*sdl))
852 socksize = sizeof(*sdl);
853 socksize = roundup2(socksize, sizeof(long));
854 ifasize = sizeof(*ifa) + 2 * socksize;
855 ifa = ifa_alloc(ifasize, M_WAITOK);
856 sdl = (struct sockaddr_dl *)(ifa + 1);
857 sdl->sdl_len = socksize;
858 sdl->sdl_family = AF_LINK;
859 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
860 sdl->sdl_nlen = namelen;
861 sdl->sdl_index = ifp->if_index;
862 sdl->sdl_type = ifp->if_type;
865 ifa->ifa_rtrequest = link_rtrequest;
866 ifa->ifa_addr = (struct sockaddr *)sdl;
867 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
868 ifa->ifa_netmask = (struct sockaddr *)sdl;
869 sdl->sdl_len = masklen;
871 sdl->sdl_data[--namelen] = 0xff;
872 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
873 /* Reliably crash if used uninitialized. */
874 ifp->if_broadcastaddr = NULL;
876 if (ifp->if_type == IFT_ETHER) {
877 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
881 #if defined(INET) || defined(INET6)
882 /* Use defaults for TSO, if nothing is set */
883 if (ifp->if_hw_tsomax == 0 &&
884 ifp->if_hw_tsomaxsegcount == 0 &&
885 ifp->if_hw_tsomaxsegsize == 0) {
887 * The TSO defaults needs to be such that an
888 * NFS mbuf list of 35 mbufs totalling just
889 * below 64K works and that a chain of mbufs
890 * can be defragged into at most 32 segments:
892 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
893 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
894 ifp->if_hw_tsomaxsegcount = 35;
895 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
897 /* XXX some drivers set IFCAP_TSO after ethernet attach */
898 if (ifp->if_capabilities & IFCAP_TSO) {
899 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
901 ifp->if_hw_tsomaxsegcount,
902 ifp->if_hw_tsomaxsegsize);
910 * Update the interface index in the link layer address
913 for (ifa = ifp->if_addr; ifa != NULL;
914 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
915 if (ifa->ifa_addr->sa_family == AF_LINK) {
916 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
917 sdl->sdl_index = ifp->if_index;
924 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
926 curvnet->vnet_ifcnt++;
930 if (domain_init_status >= 2)
931 if_attachdomain1(ifp);
933 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
934 if (IS_DEFAULT_VNET(curvnet))
935 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
937 /* Announce the interface. */
938 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
942 if_epochalloc(void *dummy __unused)
945 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
946 net_epoch = epoch_alloc(0);
948 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
949 if_epochalloc, NULL);
952 if_attachdomain(void *dummy)
956 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
957 if_attachdomain1(ifp);
959 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
960 if_attachdomain, NULL);
963 if_attachdomain1(struct ifnet *ifp)
968 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
969 * cannot lock ifp->if_afdata initialization, entirely.
972 if (ifp->if_afdata_initialized >= domain_init_status) {
973 IF_AFDATA_UNLOCK(ifp);
974 log(LOG_WARNING, "%s called more than once on %s\n",
975 __func__, ifp->if_xname);
978 ifp->if_afdata_initialized = domain_init_status;
979 IF_AFDATA_UNLOCK(ifp);
981 /* address family dependent data region */
982 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
983 for (dp = domains; dp; dp = dp->dom_next) {
984 if (dp->dom_ifattach)
985 ifp->if_afdata[dp->dom_family] =
986 (*dp->dom_ifattach)(ifp);
991 * Remove any unicast or broadcast network addresses from an interface.
994 if_purgeaddrs(struct ifnet *ifp)
999 struct epoch_tracker et;
1001 NET_EPOCH_ENTER(et);
1002 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1003 if (ifa->ifa_addr->sa_family != AF_LINK)
1011 /* XXX: Ugly!! ad hoc just for INET */
1012 if (ifa->ifa_addr->sa_family == AF_INET) {
1013 struct ifaliasreq ifr;
1015 bzero(&ifr, sizeof(ifr));
1016 ifr.ifra_addr = *ifa->ifa_addr;
1017 if (ifa->ifa_dstaddr)
1018 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1019 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1025 if (ifa->ifa_addr->sa_family == AF_INET6) {
1027 /* ifp_addrhead is already updated */
1032 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1033 IF_ADDR_WUNLOCK(ifp);
1039 * Remove any multicast network addresses from an interface when an ifnet
1043 if_purgemaddrs(struct ifnet *ifp)
1045 struct ifmultiaddr *ifma;
1048 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1049 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1050 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1051 if_delmulti_locked(ifp, ifma, 1);
1053 IF_ADDR_WUNLOCK(ifp);
1057 * Detach an interface, removing it from the list of "active" interfaces.
1058 * If vmove flag is set on entry to if_detach_internal(), perform only a
1059 * limited subset of cleanup tasks, given that we are moving an ifnet from
1060 * one vnet to another, where it must be fully operational.
1062 * XXXRW: There are some significant questions about event ordering, and
1063 * how to prevent things from starting to use the interface during detach.
1066 if_detach(struct ifnet *ifp)
1069 CURVNET_SET_QUIET(ifp->if_vnet);
1070 if_detach_internal(ifp, 0, NULL);
1075 * The vmove flag, if set, indicates that we are called from a callpath
1076 * that is moving an interface to a different vnet instance.
1078 * The shutdown flag, if set, indicates that we are called in the
1079 * process of shutting down a vnet instance. Currently only the
1080 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1081 * on a vnet instance shutdown without this flag being set, e.g., when
1082 * the cloned interfaces are destoyed as first thing of teardown.
1085 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1095 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1096 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1099 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1101 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1103 ifp->if_flags |= IFF_DYING;
1110 * While we would want to panic here, we cannot
1111 * guarantee that the interface is indeed still on
1112 * the list given we don't hold locks all the way.
1117 panic("%s: ifp=%p not on the ifnet tailq %p",
1118 __func__, ifp, &V_ifnet);
1120 return; /* XXX this should panic as well? */
1125 * At this point we know the interface still was on the ifnet list
1126 * and we removed it so we are in a stable state.
1129 curvnet->vnet_ifcnt--;
1131 epoch_wait_preempt(net_epoch_preempt);
1133 * In any case (destroy or vmove) detach us from the groups
1134 * and remove/wait for pending events on the taskq.
1135 * XXX-BZ in theory an interface could still enqueue a taskq change?
1139 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1142 * Check if this is a cloned interface or not. Must do even if
1143 * shutting down as a if_vmove_reclaim() would move the ifp and
1144 * the if_clone_addgroup() will have a corrupted string overwise
1145 * from a gibberish pointer.
1147 if (vmove && ifcp != NULL)
1148 *ifcp = if_clone_findifc(ifp);
1154 * On VNET shutdown abort here as the stack teardown will do all
1155 * the work top-down for us.
1158 /* Give interface users the chance to clean up. */
1159 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1162 * In case of a vmove we are done here without error.
1163 * If we would signal an error it would lead to the same
1164 * abort as if we did not find the ifnet anymore.
1165 * if_detach() calls us in void context and does not care
1166 * about an early abort notification, so life is splendid :)
1168 goto finish_vnet_shutdown;
1173 * At this point we are not tearing down a VNET and are either
1174 * going to destroy or vmove the interface and have to cleanup
1179 * Remove routes and flush queues.
1182 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1183 altq_disable(&ifp->if_snd);
1184 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1185 altq_detach(&ifp->if_snd);
1196 * Remove all IPv6 kernel structs related to ifp. This should be done
1197 * before removing routing entries below, since IPv6 interface direct
1198 * routes are expected to be removed by the IPv6-specific kernel API.
1199 * Otherwise, the kernel will detect some inconsistency and bark it.
1203 if_purgemaddrs(ifp);
1205 /* Announce that the interface is gone. */
1206 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1207 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1208 if (IS_DEFAULT_VNET(curvnet))
1209 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1213 * Prevent further calls into the device driver via ifnet.
1218 * Clean up all addresses.
1221 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1222 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1223 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1224 IF_ADDR_WUNLOCK(ifp);
1227 IF_ADDR_WUNLOCK(ifp);
1230 rt_flushifroutes(ifp);
1233 finish_vnet_shutdown:
1236 * We cannot hold the lock over dom_ifdetach calls as they might
1237 * sleep, for example trying to drain a callout, thus open up the
1238 * theoretical race with re-attaching.
1240 IF_AFDATA_LOCK(ifp);
1241 i = ifp->if_afdata_initialized;
1242 ifp->if_afdata_initialized = 0;
1243 IF_AFDATA_UNLOCK(ifp);
1244 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1245 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1246 (*dp->dom_ifdetach)(ifp,
1247 ifp->if_afdata[dp->dom_family]);
1248 ifp->if_afdata[dp->dom_family] = NULL;
1257 * if_vmove() performs a limited version of if_detach() in current
1258 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1259 * An attempt is made to shrink if_index in current vnet, find an
1260 * unused if_index in target vnet and calls if_grow() if necessary,
1261 * and finally find an unused if_xname for the target vnet.
1264 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1266 struct if_clone *ifc;
1267 u_int bif_dlt, bif_hdrlen;
1272 * if_detach_internal() will call the eventhandler to notify
1273 * interface departure. That will detach if_bpf. We need to
1274 * safe the dlt and hdrlen so we can re-attach it later.
1276 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1279 * Detach from current vnet, but preserve LLADDR info, do not
1280 * mark as dead etc. so that the ifnet can be reattached later.
1281 * If we cannot find it, we lost the race to someone else.
1283 rc = if_detach_internal(ifp, 1, &ifc);
1288 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1289 * the if_index for that vnet if possible.
1291 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1292 * or we'd lock on one vnet and unlock on another.
1295 ifindex_free_locked(ifp->if_index);
1299 * Perform interface-specific reassignment tasks, if provided by
1302 if (ifp->if_reassign != NULL)
1303 ifp->if_reassign(ifp, new_vnet, NULL);
1306 * Switch to the context of the target vnet.
1308 CURVNET_SET_QUIET(new_vnet);
1311 ifp->if_index = ifindex_alloc(&old);
1312 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1314 epoch_wait_preempt(net_epoch_preempt);
1318 ifnet_setbyindex(ifp->if_index, ifp);
1321 if_attach_internal(ifp, 1, ifc);
1323 if (ifp->if_bpf == NULL)
1324 bpfattach(ifp, bif_dlt, bif_hdrlen);
1330 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1333 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1339 /* Try to find the prison within our visibility. */
1340 sx_slock(&allprison_lock);
1341 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1342 sx_sunlock(&allprison_lock);
1345 prison_hold_locked(pr);
1346 mtx_unlock(&pr->pr_mtx);
1348 /* Do not try to move the iface from and to the same prison. */
1349 if (pr->pr_vnet == ifp->if_vnet) {
1354 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1355 /* XXX Lock interfaces to avoid races. */
1356 CURVNET_SET_QUIET(pr->pr_vnet);
1357 difp = ifunit(ifname);
1364 /* Make sure the VNET is stable. */
1365 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1366 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1374 /* Move the interface into the child jail/vnet. */
1375 if_vmove(ifp, pr->pr_vnet);
1377 /* Report the new if_xname back to the userland. */
1378 sprintf(ifname, "%s", ifp->if_xname);
1385 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1388 struct vnet *vnet_dst;
1392 /* Try to find the prison within our visibility. */
1393 sx_slock(&allprison_lock);
1394 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1395 sx_sunlock(&allprison_lock);
1398 prison_hold_locked(pr);
1399 mtx_unlock(&pr->pr_mtx);
1401 /* Make sure the named iface exists in the source prison/vnet. */
1402 CURVNET_SET(pr->pr_vnet);
1403 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1410 /* Do not try to move the iface from and to the same prison. */
1411 vnet_dst = TD_TO_VNET(td);
1412 if (vnet_dst == ifp->if_vnet) {
1418 /* Make sure the VNET is stable. */
1419 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1420 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1427 /* Get interface back from child jail/vnet. */
1428 if_vmove(ifp, vnet_dst);
1431 /* Report the new if_xname back to the userland. */
1432 sprintf(ifname, "%s", ifp->if_xname);
1440 * Add a group to an interface
1443 if_addgroup(struct ifnet *ifp, const char *groupname)
1445 struct ifg_list *ifgl;
1446 struct ifg_group *ifg = NULL;
1447 struct ifg_member *ifgm;
1450 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1451 groupname[strlen(groupname) - 1] <= '9')
1455 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1456 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1461 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1462 M_NOWAIT)) == NULL) {
1467 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1468 M_TEMP, M_NOWAIT)) == NULL) {
1474 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1475 if (!strcmp(ifg->ifg_group, groupname))
1479 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1480 M_TEMP, M_NOWAIT)) == NULL) {
1486 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1487 ifg->ifg_refcnt = 0;
1488 CK_STAILQ_INIT(&ifg->ifg_members);
1489 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1494 ifgl->ifgl_group = ifg;
1495 ifgm->ifgm_ifp = ifp;
1498 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1499 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1500 IF_ADDR_WUNLOCK(ifp);
1505 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1506 EVENTHANDLER_INVOKE(group_change_event, groupname);
1512 * Remove a group from an interface
1515 if_delgroup(struct ifnet *ifp, const char *groupname)
1517 struct ifg_list *ifgl;
1518 struct ifg_member *ifgm;
1522 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1523 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1532 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1533 IF_ADDR_WUNLOCK(ifp);
1535 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1536 if (ifgm->ifgm_ifp == ifp)
1540 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next);
1542 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1543 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1548 epoch_wait_preempt(net_epoch_preempt);
1550 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1551 free(ifgl->ifgl_group, M_TEMP);
1556 EVENTHANDLER_INVOKE(group_change_event, groupname);
1562 * Remove an interface from all groups
1565 if_delgroups(struct ifnet *ifp)
1567 struct ifg_list *ifgl;
1568 struct ifg_member *ifgm;
1569 char groupname[IFNAMSIZ];
1573 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) {
1574 ifgl = CK_STAILQ_FIRST(&ifp->if_groups);
1576 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1579 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1580 IF_ADDR_WUNLOCK(ifp);
1582 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1583 if (ifgm->ifgm_ifp == ifp)
1587 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member,
1590 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1591 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1596 epoch_wait_preempt(net_epoch_preempt);
1599 EVENTHANDLER_INVOKE(group_detach_event,
1601 free(ifgl->ifgl_group, M_TEMP);
1603 EVENTHANDLER_INVOKE(group_change_event, groupname);
1611 ifgr_group_get(void *ifgrp)
1613 union ifgroupreq_union *ifgrup;
1616 #ifdef COMPAT_FREEBSD32
1617 if (SV_CURPROC_FLAG(SV_ILP32))
1618 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1620 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1623 static struct ifg_req *
1624 ifgr_groups_get(void *ifgrp)
1626 union ifgroupreq_union *ifgrup;
1629 #ifdef COMPAT_FREEBSD32
1630 if (SV_CURPROC_FLAG(SV_ILP32))
1631 return ((struct ifg_req *)(uintptr_t)
1632 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1634 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1638 * Stores all groups from an interface in memory pointed to by ifgr.
1641 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1643 struct epoch_tracker et;
1645 struct ifg_list *ifgl;
1646 struct ifg_req ifgrq, *ifgp;
1648 if (ifgr->ifgr_len == 0) {
1649 NET_EPOCH_ENTER(et);
1650 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1651 ifgr->ifgr_len += sizeof(struct ifg_req);
1656 len = ifgr->ifgr_len;
1657 ifgp = ifgr_groups_get(ifgr);
1659 NET_EPOCH_ENTER(et);
1660 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1661 if (len < sizeof(ifgrq)) {
1665 bzero(&ifgrq, sizeof ifgrq);
1666 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1667 sizeof(ifgrq.ifgrq_group));
1668 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1672 len -= sizeof(ifgrq);
1681 * Stores all members of a group in memory pointed to by igfr
1684 if_getgroupmembers(struct ifgroupreq *ifgr)
1686 struct ifg_group *ifg;
1687 struct ifg_member *ifgm;
1688 struct ifg_req ifgrq, *ifgp;
1692 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1693 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1700 if (ifgr->ifgr_len == 0) {
1701 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1702 ifgr->ifgr_len += sizeof(ifgrq);
1707 len = ifgr->ifgr_len;
1708 ifgp = ifgr_groups_get(ifgr);
1709 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1710 if (len < sizeof(ifgrq)) {
1714 bzero(&ifgrq, sizeof ifgrq);
1715 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1716 sizeof(ifgrq.ifgrq_member));
1717 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1721 len -= sizeof(ifgrq);
1730 * Return counter values from counter(9)s stored in ifnet.
1733 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1736 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1738 return (counter_u64_fetch(ifp->if_counters[cnt]));
1742 * Increase an ifnet counter. Usually used for counters shared
1743 * between the stack and a driver, but function supports them all.
1746 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1749 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1751 counter_u64_add(ifp->if_counters[cnt], inc);
1755 * Copy data from ifnet to userland API structure if_data.
1758 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1761 ifd->ifi_type = ifp->if_type;
1762 ifd->ifi_physical = 0;
1763 ifd->ifi_addrlen = ifp->if_addrlen;
1764 ifd->ifi_hdrlen = ifp->if_hdrlen;
1765 ifd->ifi_link_state = ifp->if_link_state;
1767 ifd->ifi_datalen = sizeof(struct if_data);
1768 ifd->ifi_mtu = ifp->if_mtu;
1769 ifd->ifi_metric = ifp->if_metric;
1770 ifd->ifi_baudrate = ifp->if_baudrate;
1771 ifd->ifi_hwassist = ifp->if_hwassist;
1772 ifd->ifi_epoch = ifp->if_epoch;
1773 ifd->ifi_lastchange = ifp->if_lastchange;
1775 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1776 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1777 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1778 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1779 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1780 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1781 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1782 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1783 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1784 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1785 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1786 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1790 * Wrapper functions for struct ifnet address list locking macros. These are
1791 * used by kernel modules to avoid encoding programming interface or binary
1792 * interface assumptions that may be violated when kernel-internal locking
1793 * approaches change.
1796 if_addr_rlock(struct ifnet *ifp)
1799 epoch_enter_preempt(net_epoch_preempt, curthread->td_et);
1803 if_addr_runlock(struct ifnet *ifp)
1806 epoch_exit_preempt(net_epoch_preempt, curthread->td_et);
1810 if_maddr_rlock(if_t ifp)
1813 epoch_enter_preempt(net_epoch_preempt, curthread->td_et);
1817 if_maddr_runlock(if_t ifp)
1820 epoch_exit_preempt(net_epoch_preempt, curthread->td_et);
1824 * Initialization, destruction and refcounting functions for ifaddrs.
1827 ifa_alloc(size_t size, int flags)
1831 KASSERT(size >= sizeof(struct ifaddr),
1832 ("%s: invalid size %zu", __func__, size));
1834 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1838 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1840 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1842 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1844 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1847 refcount_init(&ifa->ifa_refcnt, 1);
1852 /* free(NULL) is okay */
1853 counter_u64_free(ifa->ifa_opackets);
1854 counter_u64_free(ifa->ifa_ipackets);
1855 counter_u64_free(ifa->ifa_obytes);
1856 counter_u64_free(ifa->ifa_ibytes);
1857 free(ifa, M_IFADDR);
1863 ifa_ref(struct ifaddr *ifa)
1866 refcount_acquire(&ifa->ifa_refcnt);
1870 ifa_destroy(epoch_context_t ctx)
1874 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1875 counter_u64_free(ifa->ifa_opackets);
1876 counter_u64_free(ifa->ifa_ipackets);
1877 counter_u64_free(ifa->ifa_obytes);
1878 counter_u64_free(ifa->ifa_ibytes);
1879 free(ifa, M_IFADDR);
1883 ifa_free(struct ifaddr *ifa)
1886 if (refcount_release(&ifa->ifa_refcnt))
1887 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1892 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1893 struct sockaddr *ia)
1896 struct rt_addrinfo info;
1897 struct sockaddr_dl null_sdl;
1902 bzero(&info, sizeof(info));
1903 if (cmd != RTM_DELETE)
1904 info.rti_ifp = V_loif;
1905 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1906 info.rti_info[RTAX_DST] = ia;
1907 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1908 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1910 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1913 !(cmd == RTM_ADD && error == EEXIST) &&
1914 !(cmd == RTM_DELETE && error == ENOENT))
1915 if_printf(ifp, "%s failed: %d\n", otype, error);
1921 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1924 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1928 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1931 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1935 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1938 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1942 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1943 * structs used to represent other address families, it is necessary
1944 * to perform a different comparison.
1947 #define sa_dl_equal(a1, a2) \
1948 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1949 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1950 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1951 CLLADDR((const struct sockaddr_dl *)(a2)), \
1952 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1955 * Locate an interface based on a complete address.
1959 ifa_ifwithaddr(const struct sockaddr *addr)
1964 MPASS(in_epoch(net_epoch_preempt));
1965 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1966 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1967 if (ifa->ifa_addr->sa_family != addr->sa_family)
1969 if (sa_equal(addr, ifa->ifa_addr)) {
1972 /* IP6 doesn't have broadcast */
1973 if ((ifp->if_flags & IFF_BROADCAST) &&
1974 ifa->ifa_broadaddr &&
1975 ifa->ifa_broadaddr->sa_len != 0 &&
1976 sa_equal(ifa->ifa_broadaddr, addr)) {
1987 ifa_ifwithaddr_check(const struct sockaddr *addr)
1989 struct epoch_tracker et;
1992 NET_EPOCH_ENTER(et);
1993 rc = (ifa_ifwithaddr(addr) != NULL);
1999 * Locate an interface based on the broadcast address.
2003 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
2008 MPASS(in_epoch(net_epoch_preempt));
2009 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2010 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2012 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2013 if (ifa->ifa_addr->sa_family != addr->sa_family)
2015 if ((ifp->if_flags & IFF_BROADCAST) &&
2016 ifa->ifa_broadaddr &&
2017 ifa->ifa_broadaddr->sa_len != 0 &&
2018 sa_equal(ifa->ifa_broadaddr, addr)) {
2029 * Locate the point to point interface with a given destination address.
2033 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2038 MPASS(in_epoch(net_epoch_preempt));
2039 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2040 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2042 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2044 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2045 if (ifa->ifa_addr->sa_family != addr->sa_family)
2047 if (ifa->ifa_dstaddr != NULL &&
2048 sa_equal(addr, ifa->ifa_dstaddr)) {
2059 * Find an interface on a specific network. If many, choice
2060 * is most specific found.
2063 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2067 struct ifaddr *ifa_maybe = NULL;
2068 u_int af = addr->sa_family;
2069 const char *addr_data = addr->sa_data, *cplim;
2071 MPASS(in_epoch(net_epoch_preempt));
2073 * AF_LINK addresses can be looked up directly by their index number,
2074 * so do that if we can.
2076 if (af == AF_LINK) {
2077 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2078 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2079 return (ifaddr_byindex(sdl->sdl_index));
2083 * Scan though each interface, looking for ones that have addresses
2084 * in this address family and the requested fib.
2086 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2087 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2089 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2090 const char *cp, *cp2, *cp3;
2092 if (ifa->ifa_addr->sa_family != af)
2094 if (af == AF_INET &&
2095 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2097 * This is a bit broken as it doesn't
2098 * take into account that the remote end may
2099 * be a single node in the network we are
2101 * The trouble is that we don't know the
2102 * netmask for the remote end.
2104 if (ifa->ifa_dstaddr != NULL &&
2105 sa_equal(addr, ifa->ifa_dstaddr)) {
2110 * Scan all the bits in the ifa's address.
2111 * If a bit dissagrees with what we are
2112 * looking for, mask it with the netmask
2113 * to see if it really matters.
2114 * (A byte at a time)
2116 if (ifa->ifa_netmask == 0)
2119 cp2 = ifa->ifa_addr->sa_data;
2120 cp3 = ifa->ifa_netmask->sa_data;
2121 cplim = ifa->ifa_netmask->sa_len
2122 + (char *)ifa->ifa_netmask;
2124 if ((*cp++ ^ *cp2++) & *cp3++)
2125 goto next; /* next address! */
2127 * If the netmask of what we just found
2128 * is more specific than what we had before
2129 * (if we had one), or if the virtual status
2130 * of new prefix is better than of the old one,
2131 * then remember the new one before continuing
2132 * to search for an even better one.
2134 if (ifa_maybe == NULL ||
2135 ifa_preferred(ifa_maybe, ifa) ||
2136 rn_refines((caddr_t)ifa->ifa_netmask,
2137 (caddr_t)ifa_maybe->ifa_netmask)) {
2150 * Find an interface address specific to an interface best matching
2154 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2157 const char *cp, *cp2, *cp3;
2159 struct ifaddr *ifa_maybe = NULL;
2160 u_int af = addr->sa_family;
2165 MPASS(in_epoch(net_epoch_preempt));
2166 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2167 if (ifa->ifa_addr->sa_family != af)
2169 if (ifa_maybe == NULL)
2171 if (ifa->ifa_netmask == 0) {
2172 if (sa_equal(addr, ifa->ifa_addr) ||
2173 (ifa->ifa_dstaddr &&
2174 sa_equal(addr, ifa->ifa_dstaddr)))
2178 if (ifp->if_flags & IFF_POINTOPOINT) {
2179 if (sa_equal(addr, ifa->ifa_dstaddr))
2183 cp2 = ifa->ifa_addr->sa_data;
2184 cp3 = ifa->ifa_netmask->sa_data;
2185 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2186 for (; cp3 < cplim; cp3++)
2187 if ((*cp++ ^ *cp2++) & *cp3)
2199 * See whether new ifa is better than current one:
2200 * 1) A non-virtual one is preferred over virtual.
2201 * 2) A virtual in master state preferred over any other state.
2203 * Used in several address selecting functions.
2206 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2209 return (cur->ifa_carp && (!next->ifa_carp ||
2210 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2213 #include <net/if_llatbl.h>
2216 * Default action when installing a route with a Link Level gateway.
2217 * Lookup an appropriate real ifa to point to.
2218 * This should be moved to /sys/net/link.c eventually.
2221 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2223 struct epoch_tracker et;
2224 struct ifaddr *ifa, *oifa;
2225 struct sockaddr *dst;
2228 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2229 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2231 NET_EPOCH_ENTER(et);
2232 ifa = ifaof_ifpforaddr(dst, ifp);
2240 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2241 ifa->ifa_rtrequest(cmd, rt, info);
2246 struct sockaddr_dl *
2247 link_alloc_sdl(size_t size, int flags)
2250 return (malloc(size, M_TEMP, flags));
2254 link_free_sdl(struct sockaddr *sa)
2260 * Fills in given sdl with interface basic info.
2261 * Returns pointer to filled sdl.
2263 struct sockaddr_dl *
2264 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2266 struct sockaddr_dl *sdl;
2268 sdl = (struct sockaddr_dl *)paddr;
2269 memset(sdl, 0, sizeof(struct sockaddr_dl));
2270 sdl->sdl_len = sizeof(struct sockaddr_dl);
2271 sdl->sdl_family = AF_LINK;
2272 sdl->sdl_index = ifp->if_index;
2273 sdl->sdl_type = iftype;
2279 * Mark an interface down and notify protocols of
2283 if_unroute(struct ifnet *ifp, int flag, int fam)
2287 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2289 ifp->if_flags &= ~flag;
2290 getmicrotime(&ifp->if_lastchange);
2291 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2292 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2293 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2294 ifp->if_qflush(ifp);
2297 (*carp_linkstate_p)(ifp);
2302 * Mark an interface up and notify protocols of
2306 if_route(struct ifnet *ifp, int flag, int fam)
2310 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2312 ifp->if_flags |= flag;
2313 getmicrotime(&ifp->if_lastchange);
2314 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2315 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2316 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2318 (*carp_linkstate_p)(ifp);
2325 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2326 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2327 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2328 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2329 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2330 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2331 int (*vlan_setcookie_p)(struct ifnet *, void *);
2332 void *(*vlan_cookie_p)(struct ifnet *);
2335 * Handle a change in the interface link state. To avoid LORs
2336 * between driver lock and upper layer locks, as well as possible
2337 * recursions, we post event to taskqueue, and all job
2338 * is done in static do_link_state_change().
2341 if_link_state_change(struct ifnet *ifp, int link_state)
2343 /* Return if state hasn't changed. */
2344 if (ifp->if_link_state == link_state)
2347 ifp->if_link_state = link_state;
2349 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2353 do_link_state_change(void *arg, int pending)
2355 struct ifnet *ifp = (struct ifnet *)arg;
2356 int link_state = ifp->if_link_state;
2357 CURVNET_SET(ifp->if_vnet);
2359 /* Notify that the link state has changed. */
2361 if (ifp->if_vlantrunk != NULL)
2362 (*vlan_link_state_p)(ifp);
2364 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2365 ifp->if_l2com != NULL)
2366 (*ng_ether_link_state_p)(ifp, link_state);
2368 (*carp_linkstate_p)(ifp);
2370 ifp->if_bridge_linkstate(ifp);
2372 (*lagg_linkstate_p)(ifp, link_state);
2374 if (IS_DEFAULT_VNET(curvnet))
2375 devctl_notify("IFNET", ifp->if_xname,
2376 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2379 if_printf(ifp, "%d link states coalesced\n", pending);
2380 if (log_link_state_change)
2381 if_printf(ifp, "link state changed to %s\n",
2382 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2383 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2388 * Mark an interface down and notify protocols of
2392 if_down(struct ifnet *ifp)
2395 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2396 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2400 * Mark an interface up and notify protocols of
2404 if_up(struct ifnet *ifp)
2407 if_route(ifp, IFF_UP, AF_UNSPEC);
2408 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2412 * Flush an interface queue.
2415 if_qflush(struct ifnet *ifp)
2423 if (ALTQ_IS_ENABLED(ifq))
2427 while ((m = n) != NULL) {
2438 * Map interface name to interface structure pointer, with or without
2439 * returning a reference.
2442 ifunit_ref(const char *name)
2444 struct epoch_tracker et;
2447 NET_EPOCH_ENTER(et);
2448 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2449 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2450 !(ifp->if_flags & IFF_DYING))
2460 ifunit(const char *name)
2462 struct epoch_tracker et;
2465 NET_EPOCH_ENTER(et);
2466 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2467 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2475 ifr_buffer_get_buffer(void *data)
2477 union ifreq_union *ifrup;
2480 #ifdef COMPAT_FREEBSD32
2481 if (SV_CURPROC_FLAG(SV_ILP32))
2482 return ((void *)(uintptr_t)
2483 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2485 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2489 ifr_buffer_set_buffer_null(void *data)
2491 union ifreq_union *ifrup;
2494 #ifdef COMPAT_FREEBSD32
2495 if (SV_CURPROC_FLAG(SV_ILP32))
2496 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2499 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2503 ifr_buffer_get_length(void *data)
2505 union ifreq_union *ifrup;
2508 #ifdef COMPAT_FREEBSD32
2509 if (SV_CURPROC_FLAG(SV_ILP32))
2510 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2512 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2516 ifr_buffer_set_length(void *data, size_t len)
2518 union ifreq_union *ifrup;
2521 #ifdef COMPAT_FREEBSD32
2522 if (SV_CURPROC_FLAG(SV_ILP32))
2523 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2526 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2530 ifr_data_get_ptr(void *ifrp)
2532 union ifreq_union *ifrup;
2535 #ifdef COMPAT_FREEBSD32
2536 if (SV_CURPROC_FLAG(SV_ILP32))
2537 return ((void *)(uintptr_t)
2538 ifrup->ifr32.ifr_ifru.ifru_data);
2540 return (ifrup->ifr.ifr_ifru.ifru_data);
2544 * Hardware specific interface ioctls.
2547 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2550 int error = 0, do_ifup = 0;
2551 int new_flags, temp_flags;
2552 size_t namelen, onamelen;
2554 char *descrbuf, *odescrbuf;
2555 char new_name[IFNAMSIZ];
2557 struct sockaddr_dl *sdl;
2559 ifr = (struct ifreq *)data;
2562 ifr->ifr_index = ifp->if_index;
2566 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2567 ifr->ifr_flags = temp_flags & 0xffff;
2568 ifr->ifr_flagshigh = temp_flags >> 16;
2572 ifr->ifr_reqcap = ifp->if_capabilities;
2573 ifr->ifr_curcap = ifp->if_capenable;
2578 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2583 ifr->ifr_metric = ifp->if_metric;
2587 ifr->ifr_mtu = ifp->if_mtu;
2591 /* XXXGL: did this ever worked? */
2597 sx_slock(&ifdescr_sx);
2598 if (ifp->if_description == NULL)
2601 /* space for terminating nul */
2602 descrlen = strlen(ifp->if_description) + 1;
2603 if (ifr_buffer_get_length(ifr) < descrlen)
2604 ifr_buffer_set_buffer_null(ifr);
2606 error = copyout(ifp->if_description,
2607 ifr_buffer_get_buffer(ifr), descrlen);
2608 ifr_buffer_set_length(ifr, descrlen);
2610 sx_sunlock(&ifdescr_sx);
2614 error = priv_check(td, PRIV_NET_SETIFDESCR);
2619 * Copy only (length-1) bytes to make sure that
2620 * if_description is always nul terminated. The
2621 * length parameter is supposed to count the
2622 * terminating nul in.
2624 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2625 return (ENAMETOOLONG);
2626 else if (ifr_buffer_get_length(ifr) == 0)
2629 descrbuf = malloc(ifr_buffer_get_length(ifr),
2630 M_IFDESCR, M_WAITOK | M_ZERO);
2631 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2632 ifr_buffer_get_length(ifr) - 1);
2634 free(descrbuf, M_IFDESCR);
2639 sx_xlock(&ifdescr_sx);
2640 odescrbuf = ifp->if_description;
2641 ifp->if_description = descrbuf;
2642 sx_xunlock(&ifdescr_sx);
2644 getmicrotime(&ifp->if_lastchange);
2645 free(odescrbuf, M_IFDESCR);
2649 ifr->ifr_fib = ifp->if_fib;
2653 error = priv_check(td, PRIV_NET_SETIFFIB);
2656 if (ifr->ifr_fib >= rt_numfibs)
2659 ifp->if_fib = ifr->ifr_fib;
2663 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2667 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2668 * check, so we don't need special handling here yet.
2670 new_flags = (ifr->ifr_flags & 0xffff) |
2671 (ifr->ifr_flagshigh << 16);
2672 if (ifp->if_flags & IFF_UP &&
2673 (new_flags & IFF_UP) == 0) {
2675 } else if (new_flags & IFF_UP &&
2676 (ifp->if_flags & IFF_UP) == 0) {
2679 /* See if permanently promiscuous mode bit is about to flip */
2680 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2681 if (new_flags & IFF_PPROMISC)
2682 ifp->if_flags |= IFF_PROMISC;
2683 else if (ifp->if_pcount == 0)
2684 ifp->if_flags &= ~IFF_PROMISC;
2685 if (log_promisc_mode_change)
2686 if_printf(ifp, "permanently promiscuous mode %s\n",
2687 ((new_flags & IFF_PPROMISC) ?
2688 "enabled" : "disabled"));
2690 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2691 (new_flags &~ IFF_CANTCHANGE);
2692 if (ifp->if_ioctl) {
2693 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2697 getmicrotime(&ifp->if_lastchange);
2701 error = priv_check(td, PRIV_NET_SETIFCAP);
2704 if (ifp->if_ioctl == NULL)
2705 return (EOPNOTSUPP);
2706 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2708 error = (*ifp->if_ioctl)(ifp, cmd, data);
2710 getmicrotime(&ifp->if_lastchange);
2715 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2720 error = priv_check(td, PRIV_NET_SETIFNAME);
2723 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2727 if (new_name[0] == '\0')
2729 if (new_name[IFNAMSIZ-1] != '\0') {
2730 new_name[IFNAMSIZ-1] = '\0';
2731 if (strlen(new_name) == IFNAMSIZ-1)
2734 if (ifunit(new_name) != NULL)
2738 * XXX: Locking. Nothing else seems to lock if_flags,
2739 * and there are numerous other races with the
2740 * ifunit() checks not being atomic with namespace
2741 * changes (renames, vmoves, if_attach, etc).
2743 ifp->if_flags |= IFF_RENAMING;
2745 /* Announce the departure of the interface. */
2746 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2747 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2749 if_printf(ifp, "changing name to '%s'\n", new_name);
2752 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2754 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2755 namelen = strlen(new_name);
2756 onamelen = sdl->sdl_nlen;
2758 * Move the address if needed. This is safe because we
2759 * allocate space for a name of length IFNAMSIZ when we
2760 * create this in if_attach().
2762 if (namelen != onamelen) {
2763 bcopy(sdl->sdl_data + onamelen,
2764 sdl->sdl_data + namelen, sdl->sdl_alen);
2766 bcopy(new_name, sdl->sdl_data, namelen);
2767 sdl->sdl_nlen = namelen;
2768 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2769 bzero(sdl->sdl_data, onamelen);
2770 while (namelen != 0)
2771 sdl->sdl_data[--namelen] = 0xff;
2772 IF_ADDR_WUNLOCK(ifp);
2774 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2775 /* Announce the return of the interface. */
2776 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2778 ifp->if_flags &= ~IFF_RENAMING;
2783 error = priv_check(td, PRIV_NET_SETIFVNET);
2786 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2791 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2794 ifp->if_metric = ifr->ifr_metric;
2795 getmicrotime(&ifp->if_lastchange);
2799 error = priv_check(td, PRIV_NET_SETIFPHYS);
2802 if (ifp->if_ioctl == NULL)
2803 return (EOPNOTSUPP);
2804 error = (*ifp->if_ioctl)(ifp, cmd, data);
2806 getmicrotime(&ifp->if_lastchange);
2811 u_long oldmtu = ifp->if_mtu;
2813 error = priv_check(td, PRIV_NET_SETIFMTU);
2816 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2818 if (ifp->if_ioctl == NULL)
2819 return (EOPNOTSUPP);
2820 error = (*ifp->if_ioctl)(ifp, cmd, data);
2822 getmicrotime(&ifp->if_lastchange);
2825 NETDUMP_REINIT(ifp);
2829 * If the link MTU changed, do network layer specific procedure.
2831 if (ifp->if_mtu != oldmtu) {
2842 if (cmd == SIOCADDMULTI)
2843 error = priv_check(td, PRIV_NET_ADDMULTI);
2845 error = priv_check(td, PRIV_NET_DELMULTI);
2849 /* Don't allow group membership on non-multicast interfaces. */
2850 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2851 return (EOPNOTSUPP);
2853 /* Don't let users screw up protocols' entries. */
2854 if (ifr->ifr_addr.sa_family != AF_LINK)
2857 if (cmd == SIOCADDMULTI) {
2858 struct epoch_tracker et;
2859 struct ifmultiaddr *ifma;
2862 * Userland is only permitted to join groups once
2863 * via the if_addmulti() KPI, because it cannot hold
2864 * struct ifmultiaddr * between calls. It may also
2865 * lose a race while we check if the membership
2868 NET_EPOCH_ENTER(et);
2869 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2874 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2876 error = if_delmulti(ifp, &ifr->ifr_addr);
2879 getmicrotime(&ifp->if_lastchange);
2882 case SIOCSIFPHYADDR:
2883 case SIOCDIFPHYADDR:
2885 case SIOCSIFPHYADDR_IN6:
2888 case SIOCSIFGENERIC:
2889 error = priv_check(td, PRIV_NET_HWIOCTL);
2892 if (ifp->if_ioctl == NULL)
2893 return (EOPNOTSUPP);
2894 error = (*ifp->if_ioctl)(ifp, cmd, data);
2896 getmicrotime(&ifp->if_lastchange);
2900 case SIOCGIFPSRCADDR:
2901 case SIOCGIFPDSTADDR:
2904 case SIOCGIFGENERIC:
2906 case SIOCGIFRSSHASH:
2907 if (ifp->if_ioctl == NULL)
2908 return (EOPNOTSUPP);
2909 error = (*ifp->if_ioctl)(ifp, cmd, data);
2913 error = priv_check(td, PRIV_NET_SETLLADDR);
2916 error = if_setlladdr(ifp,
2917 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2921 error = if_gethwaddr(ifp, ifr);
2924 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2925 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2928 if ((error = if_addgroup(ifp,
2929 ifgr_group_get((struct ifgroupreq *)data))))
2933 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2934 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2938 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2939 error = priv_check(td, PRIV_NET_DELIFGROUP);
2942 if ((error = if_delgroup(ifp,
2943 ifgr_group_get((struct ifgroupreq *)data))))
2954 #ifdef COMPAT_FREEBSD32
2962 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2965 #ifdef COMPAT_FREEBSD32
2967 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2969 struct ifmediareq32 *ifmr32;
2971 ifmr32 = (struct ifmediareq32 *)data;
2972 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2973 sizeof(ifmr->ifm_name));
2974 ifmr->ifm_current = ifmr32->ifm_current;
2975 ifmr->ifm_mask = ifmr32->ifm_mask;
2976 ifmr->ifm_status = ifmr32->ifm_status;
2977 ifmr->ifm_active = ifmr32->ifm_active;
2978 ifmr->ifm_count = ifmr32->ifm_count;
2979 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2983 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2985 struct ifmediareq32 *ifmr32;
2987 ifmr32 = (struct ifmediareq32 *)data;
2988 ifmr32->ifm_current = ifmr->ifm_current;
2989 ifmr32->ifm_mask = ifmr->ifm_mask;
2990 ifmr32->ifm_status = ifmr->ifm_status;
2991 ifmr32->ifm_active = ifmr->ifm_active;
2992 ifmr32->ifm_count = ifmr->ifm_count;
3000 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
3002 #ifdef COMPAT_FREEBSD32
3003 caddr_t saved_data = NULL;
3004 struct ifmediareq ifmr;
3005 struct ifmediareq *ifmrp;
3015 CURVNET_SET(so->so_vnet);
3017 /* Make sure the VNET is stable. */
3018 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
3019 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3029 error = ifconf(cmd, data);
3033 #ifdef COMPAT_FREEBSD32
3036 struct ifconf32 *ifc32;
3039 ifc32 = (struct ifconf32 *)data;
3040 ifc.ifc_len = ifc32->ifc_len;
3041 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3043 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3046 ifc32->ifc_len = ifc.ifc_len;
3052 #ifdef COMPAT_FREEBSD32
3055 case SIOCGIFMEDIA32:
3056 case SIOCGIFXMEDIA32:
3058 ifmr_init(ifmrp, data);
3059 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3061 data = (caddr_t)ifmrp;
3065 ifr = (struct ifreq *)data;
3069 error = priv_check(td, PRIV_NET_SETIFVNET);
3071 error = if_vmove_reclaim(td, ifr->ifr_name,
3077 error = priv_check(td, PRIV_NET_IFCREATE);
3079 error = if_clone_create(ifr->ifr_name,
3080 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3081 ifr_data_get_ptr(ifr) : NULL);
3084 error = priv_check(td, PRIV_NET_IFDESTROY);
3086 error = if_clone_destroy(ifr->ifr_name);
3089 case SIOCIFGCLONERS:
3090 error = if_clone_list((struct if_clonereq *)data);
3093 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3094 error = if_getgroupmembers((struct ifgroupreq *)data);
3097 #if defined(INET) || defined(INET6)
3100 if (carp_ioctl_p == NULL)
3101 error = EPROTONOSUPPORT;
3103 error = (*carp_ioctl_p)(ifr, cmd, td);
3108 ifp = ifunit_ref(ifr->ifr_name);
3114 error = ifhwioctl(cmd, ifp, data, td);
3115 if (error != ENOIOCTL)
3118 oif_flags = ifp->if_flags;
3119 if (so->so_proto == NULL) {
3125 * Pass the request on to the socket control method, and if the
3126 * latter returns EOPNOTSUPP, directly to the interface.
3128 * Make an exception for the legacy SIOCSIF* requests. Drivers
3129 * trust SIOCSIFADDR et al to come from an already privileged
3130 * layer, and do not perform any credentials checks or input
3133 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3135 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3136 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3137 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3138 error = (*ifp->if_ioctl)(ifp, cmd, data);
3140 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3142 if (ifp->if_flags & IFF_UP)
3150 #ifdef COMPAT_FREEBSD32
3151 if (ifmrp != NULL) {
3152 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3153 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3156 ifmr_update(ifmrp, data);
3164 * The code common to handling reference counted flags,
3165 * e.g., in ifpromisc() and if_allmulti().
3166 * The "pflag" argument can specify a permanent mode flag to check,
3167 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3169 * Only to be used on stack-owned flags, not driver-owned flags.
3172 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3176 int oldflags, oldcount;
3178 /* Sanity checks to catch programming errors */
3179 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3180 ("%s: setting driver-owned flag %d", __func__, flag));
3183 KASSERT(*refcount >= 0,
3184 ("%s: increment negative refcount %d for flag %d",
3185 __func__, *refcount, flag));
3187 KASSERT(*refcount > 0,
3188 ("%s: decrement non-positive refcount %d for flag %d",
3189 __func__, *refcount, flag));
3191 /* In case this mode is permanent, just touch refcount */
3192 if (ifp->if_flags & pflag) {
3193 *refcount += onswitch ? 1 : -1;
3197 /* Save ifnet parameters for if_ioctl() may fail */
3198 oldcount = *refcount;
3199 oldflags = ifp->if_flags;
3202 * See if we aren't the only and touching refcount is enough.
3203 * Actually toggle interface flag if we are the first or last.
3208 ifp->if_flags |= flag;
3212 ifp->if_flags &= ~flag;
3215 /* Call down the driver since we've changed interface flags */
3216 if (ifp->if_ioctl == NULL) {
3220 ifr.ifr_flags = ifp->if_flags & 0xffff;
3221 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3222 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3225 /* Notify userland that interface flags have changed */
3230 /* Recover after driver error */
3231 *refcount = oldcount;
3232 ifp->if_flags = oldflags;
3237 * Set/clear promiscuous mode on interface ifp based on the truth value
3238 * of pswitch. The calls are reference counted so that only the first
3239 * "on" request actually has an effect, as does the final "off" request.
3240 * Results are undefined if the "off" and "on" requests are not matched.
3243 ifpromisc(struct ifnet *ifp, int pswitch)
3246 int oldflags = ifp->if_flags;
3248 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3249 &ifp->if_pcount, pswitch);
3250 /* If promiscuous mode status has changed, log a message */
3251 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3252 log_promisc_mode_change)
3253 if_printf(ifp, "promiscuous mode %s\n",
3254 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3259 * Return interface configuration
3260 * of system. List may be used
3261 * in later ioctl's (above) to get
3262 * other information.
3266 ifconf(u_long cmd, caddr_t data)
3268 struct ifconf *ifc = (struct ifconf *)data;
3273 int error, full = 0, valid_len, max_len;
3275 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3276 max_len = MAXPHYS - 1;
3278 /* Prevent hostile input from being able to crash the system */
3279 if (ifc->ifc_len <= 0)
3283 if (ifc->ifc_len <= max_len) {
3284 max_len = ifc->ifc_len;
3287 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3292 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3293 struct epoch_tracker et;
3297 * Zero the ifr to make sure we don't disclose the contents
3300 memset(&ifr, 0, sizeof(ifr));
3302 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3303 >= sizeof(ifr.ifr_name)) {
3306 return (ENAMETOOLONG);
3310 NET_EPOCH_ENTER(et);
3311 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3312 struct sockaddr *sa = ifa->ifa_addr;
3314 if (prison_if(curthread->td_ucred, sa) != 0)
3317 if (sa->sa_len <= sizeof(*sa)) {
3318 if (sa->sa_len < sizeof(*sa)) {
3319 memset(&ifr.ifr_ifru.ifru_addr, 0,
3320 sizeof(ifr.ifr_ifru.ifru_addr));
3321 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3324 ifr.ifr_ifru.ifru_addr = *sa;
3325 sbuf_bcat(sb, &ifr, sizeof(ifr));
3326 max_len += sizeof(ifr);
3329 offsetof(struct ifreq, ifr_addr));
3330 max_len += offsetof(struct ifreq, ifr_addr);
3331 sbuf_bcat(sb, sa, sa->sa_len);
3332 max_len += sa->sa_len;
3335 if (sbuf_error(sb) == 0)
3336 valid_len = sbuf_len(sb);
3340 sbuf_bcat(sb, &ifr, sizeof(ifr));
3341 max_len += sizeof(ifr);
3343 if (sbuf_error(sb) == 0)
3344 valid_len = sbuf_len(sb);
3350 * If we didn't allocate enough space (uncommon), try again. If
3351 * we have already allocated as much space as we are allowed,
3352 * return what we've got.
3354 if (valid_len != max_len && !full) {
3359 ifc->ifc_len = valid_len;
3361 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3367 * Just like ifpromisc(), but for all-multicast-reception mode.
3370 if_allmulti(struct ifnet *ifp, int onswitch)
3373 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3376 struct ifmultiaddr *
3377 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3379 struct ifmultiaddr *ifma;
3381 IF_ADDR_LOCK_ASSERT(ifp);
3383 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3384 if (sa->sa_family == AF_LINK) {
3385 if (sa_dl_equal(ifma->ifma_addr, sa))
3388 if (sa_equal(ifma->ifma_addr, sa))
3397 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3398 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3399 * the ifnet multicast address list here, so the caller must do that and
3400 * other setup work (such as notifying the device driver). The reference
3401 * count is initialized to 1.
3403 static struct ifmultiaddr *
3404 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3407 struct ifmultiaddr *ifma;
3408 struct sockaddr *dupsa;
3410 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3415 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3416 if (dupsa == NULL) {
3417 free(ifma, M_IFMADDR);
3420 bcopy(sa, dupsa, sa->sa_len);
3421 ifma->ifma_addr = dupsa;
3423 ifma->ifma_ifp = ifp;
3424 ifma->ifma_refcount = 1;
3425 ifma->ifma_protospec = NULL;
3428 ifma->ifma_lladdr = NULL;
3432 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3433 if (dupsa == NULL) {
3434 free(ifma->ifma_addr, M_IFMADDR);
3435 free(ifma, M_IFMADDR);
3438 bcopy(llsa, dupsa, llsa->sa_len);
3439 ifma->ifma_lladdr = dupsa;
3445 * if_freemulti: free ifmultiaddr structure and possibly attached related
3446 * addresses. The caller is responsible for implementing reference
3447 * counting, notifying the driver, handling routing messages, and releasing
3448 * any dependent link layer state.
3450 #ifdef MCAST_VERBOSE
3451 extern void kdb_backtrace(void);
3454 if_freemulti_internal(struct ifmultiaddr *ifma)
3457 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3458 ifma->ifma_refcount));
3460 if (ifma->ifma_lladdr != NULL)
3461 free(ifma->ifma_lladdr, M_IFMADDR);
3462 #ifdef MCAST_VERBOSE
3464 printf("%s freeing ifma: %p\n", __func__, ifma);
3466 free(ifma->ifma_addr, M_IFMADDR);
3467 free(ifma, M_IFMADDR);
3471 if_destroymulti(epoch_context_t ctx)
3473 struct ifmultiaddr *ifma;
3475 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3476 if_freemulti_internal(ifma);
3480 if_freemulti(struct ifmultiaddr *ifma)
3482 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3483 ifma->ifma_refcount));
3485 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3490 * Register an additional multicast address with a network interface.
3492 * - If the address is already present, bump the reference count on the
3493 * address and return.
3494 * - If the address is not link-layer, look up a link layer address.
3495 * - Allocate address structures for one or both addresses, and attach to the
3496 * multicast address list on the interface. If automatically adding a link
3497 * layer address, the protocol address will own a reference to the link
3498 * layer address, to be freed when it is freed.
3499 * - Notify the network device driver of an addition to the multicast address
3502 * 'sa' points to caller-owned memory with the desired multicast address.
3504 * 'retifma' will be used to return a pointer to the resulting multicast
3505 * address reference, if desired.
3508 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3509 struct ifmultiaddr **retifma)
3511 struct ifmultiaddr *ifma, *ll_ifma;
3512 struct sockaddr *llsa;
3513 struct sockaddr_dl sdl;
3517 IN_MULTI_LIST_UNLOCK_ASSERT();
3520 IN6_MULTI_LIST_UNLOCK_ASSERT();
3523 * If the address is already present, return a new reference to it;
3524 * otherwise, allocate storage and set up a new address.
3527 ifma = if_findmulti(ifp, sa);
3529 ifma->ifma_refcount++;
3530 if (retifma != NULL)
3532 IF_ADDR_WUNLOCK(ifp);
3537 * The address isn't already present; resolve the protocol address
3538 * into a link layer address, and then look that up, bump its
3539 * refcount or allocate an ifma for that also.
3540 * Most link layer resolving functions returns address data which
3541 * fits inside default sockaddr_dl structure. However callback
3542 * can allocate another sockaddr structure, in that case we need to
3547 if (ifp->if_resolvemulti != NULL) {
3548 /* Provide called function with buffer size information */
3549 sdl.sdl_len = sizeof(sdl);
3550 llsa = (struct sockaddr *)&sdl;
3551 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3557 * Allocate the new address. Don't hook it up yet, as we may also
3558 * need to allocate a link layer multicast address.
3560 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3567 * If a link layer address is found, we'll need to see if it's
3568 * already present in the address list, or allocate is as well.
3569 * When this block finishes, the link layer address will be on the
3573 ll_ifma = if_findmulti(ifp, llsa);
3574 if (ll_ifma == NULL) {
3575 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3576 if (ll_ifma == NULL) {
3577 --ifma->ifma_refcount;
3582 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3583 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3586 ll_ifma->ifma_refcount++;
3587 ifma->ifma_llifma = ll_ifma;
3591 * We now have a new multicast address, ifma, and possibly a new or
3592 * referenced link layer address. Add the primary address to the
3593 * ifnet address list.
3595 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3596 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3598 if (retifma != NULL)
3602 * Must generate the message while holding the lock so that 'ifma'
3603 * pointer is still valid.
3605 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3606 IF_ADDR_WUNLOCK(ifp);
3609 * We are certain we have added something, so call down to the
3610 * interface to let them know about it.
3612 if (ifp->if_ioctl != NULL) {
3613 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3616 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3617 link_free_sdl(llsa);
3622 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3623 link_free_sdl(llsa);
3626 IF_ADDR_WUNLOCK(ifp);
3631 * Delete a multicast group membership by network-layer group address.
3633 * Returns ENOENT if the entry could not be found. If ifp no longer
3634 * exists, results are undefined. This entry point should only be used
3635 * from subsystems which do appropriate locking to hold ifp for the
3636 * duration of the call.
3637 * Network-layer protocol domains must use if_delmulti_ifma().
3640 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3642 struct ifmultiaddr *ifma;
3645 struct epoch_tracker et;
3648 NET_EPOCH_ENTER(et);
3649 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3656 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3663 ifma = if_findmulti(ifp, sa);
3665 lastref = if_delmulti_locked(ifp, ifma, 0);
3666 IF_ADDR_WUNLOCK(ifp);
3671 if (lastref && ifp->if_ioctl != NULL) {
3672 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3679 * Delete all multicast group membership for an interface.
3680 * Should be used to quickly flush all multicast filters.
3683 if_delallmulti(struct ifnet *ifp)
3685 struct ifmultiaddr *ifma;
3686 struct ifmultiaddr *next;
3689 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3690 if_delmulti_locked(ifp, ifma, 0);
3691 IF_ADDR_WUNLOCK(ifp);
3695 if_delmulti_ifma(struct ifmultiaddr *ifma)
3697 if_delmulti_ifma_flags(ifma, 0);
3701 * Delete a multicast group membership by group membership pointer.
3702 * Network-layer protocol domains must use this routine.
3704 * It is safe to call this routine if the ifp disappeared.
3707 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3711 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3713 IN_MULTI_LIST_UNLOCK_ASSERT();
3715 ifp = ifma->ifma_ifp;
3718 printf("%s: ifma_ifp seems to be detached\n", __func__);
3720 struct epoch_tracker et;
3723 NET_EPOCH_ENTER(et);
3724 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3733 * If and only if the ifnet instance exists: Acquire the address lock.
3738 lastref = if_delmulti_locked(ifp, ifma, flags);
3742 * If and only if the ifnet instance exists:
3743 * Release the address lock.
3744 * If the group was left: update the hardware hash filter.
3746 IF_ADDR_WUNLOCK(ifp);
3747 if (lastref && ifp->if_ioctl != NULL) {
3748 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3754 * Perform deletion of network-layer and/or link-layer multicast address.
3756 * Return 0 if the reference count was decremented.
3757 * Return 1 if the final reference was released, indicating that the
3758 * hardware hash filter should be reprogrammed.
3761 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3763 struct ifmultiaddr *ll_ifma;
3765 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3766 KASSERT(ifma->ifma_ifp == ifp,
3767 ("%s: inconsistent ifp %p", __func__, ifp));
3768 IF_ADDR_WLOCK_ASSERT(ifp);
3771 ifp = ifma->ifma_ifp;
3772 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3775 * If the ifnet is detaching, null out references to ifnet,
3776 * so that upper protocol layers will notice, and not attempt
3777 * to obtain locks for an ifnet which no longer exists. The
3778 * routing socket announcement must happen before the ifnet
3779 * instance is detached from the system.
3783 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3786 * ifp may already be nulled out if we are being reentered
3787 * to delete the ll_ifma.
3790 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3791 ifma->ifma_ifp = NULL;
3795 if (--ifma->ifma_refcount > 0)
3798 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3799 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3800 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3803 * If this ifma is a network-layer ifma, a link-layer ifma may
3804 * have been associated with it. Release it first if so.
3806 ll_ifma = ifma->ifma_llifma;
3807 if (ll_ifma != NULL) {
3808 KASSERT(ifma->ifma_lladdr != NULL,
3809 ("%s: llifma w/o lladdr", __func__));
3811 ll_ifma->ifma_ifp = NULL; /* XXX */
3812 if (--ll_ifma->ifma_refcount == 0) {
3814 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3815 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3817 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3820 if_freemulti(ll_ifma);
3825 struct ifmultiaddr *ifmatmp;
3827 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3828 MPASS(ifma != ifmatmp);
3833 * The last reference to this instance of struct ifmultiaddr
3834 * was released; the hardware should be notified of this change.
3840 * Set the link layer address on an interface.
3842 * At this time we only support certain types of interfaces,
3843 * and we don't allow the length of the address to change.
3845 * Set noinline to be dtrace-friendly
3848 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3850 struct sockaddr_dl *sdl;
3853 struct epoch_tracker et;
3857 NET_EPOCH_ENTER(et);
3864 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3869 if (len != sdl->sdl_alen) { /* don't allow length to change */
3873 switch (ifp->if_type) {
3878 case IFT_IEEE8023ADLAG:
3879 bcopy(lladdr, LLADDR(sdl), len);
3887 * If the interface is already up, we need
3888 * to re-init it in order to reprogram its
3892 if ((ifp->if_flags & IFF_UP) != 0) {
3893 if (ifp->if_ioctl) {
3894 ifp->if_flags &= ~IFF_UP;
3895 ifr.ifr_flags = ifp->if_flags & 0xffff;
3896 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3897 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3898 ifp->if_flags |= IFF_UP;
3899 ifr.ifr_flags = ifp->if_flags & 0xffff;
3900 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3901 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3904 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3912 * Compat function for handling basic encapsulation requests.
3913 * Not converted stacks (FDDI, IB, ..) supports traditional
3914 * output model: ARP (and other similar L2 protocols) are handled
3915 * inside output routine, arpresolve/nd6_resolve() returns MAC
3916 * address instead of full prepend.
3918 * This function creates calculated header==MAC for IPv4/IPv6 and
3919 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3923 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3926 if (req->rtype != IFENCAP_LL)
3927 return (EOPNOTSUPP);
3929 if (req->bufsize < req->lladdr_len)
3932 switch (req->family) {
3937 return (EAFNOSUPPORT);
3940 /* Copy lladdr to storage as is */
3941 memmove(req->buf, req->lladdr, req->lladdr_len);
3942 req->bufsize = req->lladdr_len;
3943 req->lladdr_off = 0;
3949 * Tunnel interfaces can nest, also they may cause infinite recursion
3950 * calls when misconfigured. We'll prevent this by detecting loops.
3951 * High nesting level may cause stack exhaustion. We'll prevent this
3952 * by introducing upper limit.
3954 * Return 0, if tunnel nesting count is equal or less than limit.
3957 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3965 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3966 if (*(struct ifnet **)(mtag + 1) == ifp) {
3967 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3972 if (count > limit) {
3974 "%s: if_output recursively called too many times(%d)\n",
3975 if_name(ifp), count);
3978 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3981 *(struct ifnet **)(mtag + 1) = ifp;
3982 m_tag_prepend(m, mtag);
3987 * Get the link layer address that was read from the hardware at attach.
3989 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3990 * their component interfaces as IFT_IEEE8023ADLAG.
3993 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3996 if (ifp->if_hw_addr == NULL)
3999 switch (ifp->if_type) {
4001 case IFT_IEEE8023ADLAG:
4002 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4010 * The name argument must be a pointer to storage which will last as
4011 * long as the interface does. For physical devices, the result of
4012 * device_get_name(dev) is a good choice and for pseudo-devices a
4013 * static string works well.
4016 if_initname(struct ifnet *ifp, const char *name, int unit)
4018 ifp->if_dname = name;
4019 ifp->if_dunit = unit;
4020 if (unit != IF_DUNIT_NONE)
4021 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4023 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4027 if_printf(struct ifnet *ifp, const char *fmt, ...)
4032 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4034 vlog(LOG_INFO, if_fmt, ap);
4040 if_start(struct ifnet *ifp)
4043 (*(ifp)->if_start)(ifp);
4047 * Backwards compatibility interface for drivers
4048 * that have not implemented it
4051 if_transmit(struct ifnet *ifp, struct mbuf *m)
4055 IFQ_HANDOFF(ifp, m, error);
4060 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4067 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4072 if (_IF_QFULL(ifq)) {
4074 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4079 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4080 if (m->m_flags & (M_BCAST|M_MCAST))
4081 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4082 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4084 _IF_ENQUEUE(ifq, m);
4086 if (ifp != NULL && !active)
4087 (*(ifp)->if_start)(ifp);
4092 if_register_com_alloc(u_char type,
4093 if_com_alloc_t *a, if_com_free_t *f)
4096 KASSERT(if_com_alloc[type] == NULL,
4097 ("if_register_com_alloc: %d already registered", type));
4098 KASSERT(if_com_free[type] == NULL,
4099 ("if_register_com_alloc: %d free already registered", type));
4101 if_com_alloc[type] = a;
4102 if_com_free[type] = f;
4106 if_deregister_com_alloc(u_char type)
4109 KASSERT(if_com_alloc[type] != NULL,
4110 ("if_deregister_com_alloc: %d not registered", type));
4111 KASSERT(if_com_free[type] != NULL,
4112 ("if_deregister_com_alloc: %d free not registered", type));
4113 if_com_alloc[type] = NULL;
4114 if_com_free[type] = NULL;
4117 /* API for driver access to network stack owned ifnet.*/
4119 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4123 oldbrate = ifp->if_baudrate;
4124 ifp->if_baudrate = baudrate;
4129 if_getbaudrate(if_t ifp)
4132 return (((struct ifnet *)ifp)->if_baudrate);
4136 if_setcapabilities(if_t ifp, int capabilities)
4138 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4143 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4145 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4146 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4152 if_getcapabilities(if_t ifp)
4154 return ((struct ifnet *)ifp)->if_capabilities;
4158 if_setcapenable(if_t ifp, int capabilities)
4160 ((struct ifnet *)ifp)->if_capenable = capabilities;
4165 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4168 ((struct ifnet *)ifp)->if_capenable |= setcap;
4170 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4176 if_getdname(if_t ifp)
4178 return ((struct ifnet *)ifp)->if_dname;
4182 if_togglecapenable(if_t ifp, int togglecap)
4184 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4189 if_getcapenable(if_t ifp)
4191 return ((struct ifnet *)ifp)->if_capenable;
4195 * This is largely undesirable because it ties ifnet to a device, but does
4196 * provide flexiblity for an embedded product vendor. Should be used with
4197 * the understanding that it violates the interface boundaries, and should be
4198 * a last resort only.
4201 if_setdev(if_t ifp, void *dev)
4207 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4209 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4210 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4216 if_getdrvflags(if_t ifp)
4218 return ((struct ifnet *)ifp)->if_drv_flags;
4222 if_setdrvflags(if_t ifp, int flags)
4224 ((struct ifnet *)ifp)->if_drv_flags = flags;
4230 if_setflags(if_t ifp, int flags)
4232 ((struct ifnet *)ifp)->if_flags = flags;
4237 if_setflagbits(if_t ifp, int set, int clear)
4239 ((struct ifnet *)ifp)->if_flags |= set;
4240 ((struct ifnet *)ifp)->if_flags &= ~clear;
4246 if_getflags(if_t ifp)
4248 return ((struct ifnet *)ifp)->if_flags;
4252 if_clearhwassist(if_t ifp)
4254 ((struct ifnet *)ifp)->if_hwassist = 0;
4259 if_sethwassistbits(if_t ifp, int toset, int toclear)
4261 ((struct ifnet *)ifp)->if_hwassist |= toset;
4262 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4268 if_sethwassist(if_t ifp, int hwassist_bit)
4270 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4275 if_gethwassist(if_t ifp)
4277 return ((struct ifnet *)ifp)->if_hwassist;
4281 if_setmtu(if_t ifp, int mtu)
4283 ((struct ifnet *)ifp)->if_mtu = mtu;
4290 return ((struct ifnet *)ifp)->if_mtu;
4294 if_getmtu_family(if_t ifp, int family)
4298 for (dp = domains; dp; dp = dp->dom_next) {
4299 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4300 return (dp->dom_ifmtu((struct ifnet *)ifp));
4303 return (((struct ifnet *)ifp)->if_mtu);
4307 if_setsoftc(if_t ifp, void *softc)
4309 ((struct ifnet *)ifp)->if_softc = softc;
4314 if_getsoftc(if_t ifp)
4316 return ((struct ifnet *)ifp)->if_softc;
4320 if_setrcvif(struct mbuf *m, if_t ifp)
4322 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4326 if_setvtag(struct mbuf *m, uint16_t tag)
4328 m->m_pkthdr.ether_vtag = tag;
4332 if_getvtag(struct mbuf *m)
4335 return (m->m_pkthdr.ether_vtag);
4339 if_sendq_empty(if_t ifp)
4341 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4345 if_getifaddr(if_t ifp)
4347 return ((struct ifnet *)ifp)->if_addr;
4351 if_getamcount(if_t ifp)
4353 return ((struct ifnet *)ifp)->if_amcount;
4358 if_setsendqready(if_t ifp)
4360 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4365 if_setsendqlen(if_t ifp, int tx_desc_count)
4367 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4368 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4374 if_vlantrunkinuse(if_t ifp)
4376 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4380 if_input(if_t ifp, struct mbuf* sendmp)
4382 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4388 #ifndef ETH_ADDR_LEN
4389 #define ETH_ADDR_LEN 6
4393 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4395 struct ifmultiaddr *ifma;
4396 uint8_t *lmta = (uint8_t *)mta;
4399 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4400 if (ifma->ifma_addr->sa_family != AF_LINK)
4406 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4407 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4416 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4420 if_maddr_rlock(ifp);
4421 error = if_setupmultiaddr(ifp, mta, cnt, max);
4422 if_maddr_runlock(ifp);
4427 if_multiaddr_count(if_t ifp, int max)
4429 struct ifmultiaddr *ifma;
4433 if_maddr_rlock(ifp);
4434 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4435 if (ifma->ifma_addr->sa_family != AF_LINK)
4441 if_maddr_runlock(ifp);
4446 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4448 struct ifmultiaddr *ifma;
4451 if_maddr_rlock(ifp);
4452 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4453 cnt += filter(arg, ifma, cnt);
4454 if_maddr_runlock(ifp);
4459 if_dequeue(if_t ifp)
4462 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4468 if_sendq_prepend(if_t ifp, struct mbuf *m)
4470 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4475 if_setifheaderlen(if_t ifp, int len)
4477 ((struct ifnet *)ifp)->if_hdrlen = len;
4482 if_getlladdr(if_t ifp)
4484 return (IF_LLADDR((struct ifnet *)ifp));
4488 if_gethandle(u_char type)
4490 return (if_alloc(type));
4494 if_bpfmtap(if_t ifh, struct mbuf *m)
4496 struct ifnet *ifp = (struct ifnet *)ifh;
4502 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4504 struct ifnet *ifp = (struct ifnet *)ifh;
4506 ETHER_BPF_MTAP(ifp, m);
4510 if_vlancap(if_t ifh)
4512 struct ifnet *ifp = (struct ifnet *)ifh;
4513 VLAN_CAPABILITIES(ifp);
4517 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4520 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4525 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4528 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4533 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4536 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4541 if_gethwtsomax(if_t ifp)
4544 return (((struct ifnet *)ifp)->if_hw_tsomax);
4548 if_gethwtsomaxsegcount(if_t ifp)
4551 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4555 if_gethwtsomaxsegsize(if_t ifp)
4558 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4562 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4564 ((struct ifnet *)ifp)->if_init = init_fn;
4568 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4570 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4574 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4576 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4580 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4582 ((struct ifnet *)ifp)->if_transmit = start_fn;
4585 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4587 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4592 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4595 ifp->if_get_counter = fn;
4598 /* Revisit these - These are inline functions originally. */
4600 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4602 return drbr_inuse(ifh, br);
4606 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4608 return drbr_dequeue(ifh, br);
4612 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4614 return drbr_needs_enqueue(ifh, br);
4618 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4620 return drbr_enqueue(ifh, br, m);