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>
40 #include <sys/eventhandler.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 int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
268 static void do_link_state_change(void *, int);
269 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
270 static int if_getgroupmembers(struct ifgroupreq *);
271 static void if_delgroups(struct ifnet *);
272 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
273 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
275 static void if_vmove(struct ifnet *, struct vnet *);
280 * XXX: declare here to avoid to include many inet6 related files..
281 * should be more generalized?
283 extern void nd6_setmtu(struct ifnet *);
286 /* ipsec helper hooks */
287 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
288 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
290 VNET_DEFINE(int, if_index);
291 int ifqmaxlen = IFQ_MAXLEN;
292 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
293 VNET_DEFINE(struct ifgrouphead, ifg_head);
295 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
297 /* Table of ifnet by index. */
298 VNET_DEFINE(struct ifnet **, ifindex_table);
300 #define V_if_indexlim VNET(if_indexlim)
301 #define V_ifindex_table VNET(ifindex_table)
304 * The global network interface list (V_ifnet) and related state (such as
305 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
306 * an rwlock. Either may be acquired shared to stablize the list, but both
307 * must be acquired writable to modify the list. This model allows us to
308 * both stablize the interface list during interrupt thread processing, but
309 * also to stablize it over long-running ioctls, without introducing priority
310 * inversions and deadlocks.
312 struct rwlock ifnet_rwlock;
313 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
314 struct sx ifnet_sxlock;
315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
318 * The allocation of network interfaces is a rather non-atomic affair; we
319 * need to select an index before we are ready to expose the interface for
320 * use, so will use this pointer value to indicate reservation.
322 #define IFNET_HOLD (void *)(uintptr_t)(-1)
324 static if_com_alloc_t *if_com_alloc[256];
325 static if_com_free_t *if_com_free[256];
327 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
328 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
329 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
332 ifnet_byindex_locked(u_short idx)
335 if (idx > V_if_index)
337 if (V_ifindex_table[idx] == IFNET_HOLD)
339 return (V_ifindex_table[idx]);
343 ifnet_byindex(u_short idx)
347 ifp = ifnet_byindex_locked(idx);
352 ifnet_byindex_ref(u_short idx)
354 struct epoch_tracker et;
358 ifp = ifnet_byindex_locked(idx);
359 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
369 * Allocate an ifindex array entry; return 0 on success or an error on
373 ifindex_alloc(void **old)
377 IFNET_WLOCK_ASSERT();
379 * Try to find an empty slot below V_if_index. If we fail, take the
382 for (idx = 1; idx <= V_if_index; idx++) {
383 if (V_ifindex_table[idx] == NULL)
387 /* Catch if_index overflow. */
388 if (idx >= V_if_indexlim) {
392 if (idx > V_if_index)
398 ifindex_free_locked(u_short idx)
401 IFNET_WLOCK_ASSERT();
403 V_ifindex_table[idx] = NULL;
404 while (V_if_index > 0 &&
405 V_ifindex_table[V_if_index] == NULL)
410 ifindex_free(u_short idx)
414 ifindex_free_locked(idx);
419 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
422 V_ifindex_table[idx] = ifp;
426 ifaddr_byindex(u_short idx)
428 struct epoch_tracker et;
430 struct ifaddr *ifa = NULL;
433 ifp = ifnet_byindex_locked(idx);
434 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
441 * Network interface utility routines.
443 * Routines with ifa_ifwith* names take sockaddr *'s as
448 vnet_if_init(const void *unused __unused)
452 CK_STAILQ_INIT(&V_ifnet);
453 CK_STAILQ_INIT(&V_ifg_head);
455 old = if_grow(); /* create initial table */
457 epoch_wait_preempt(net_epoch_preempt);
459 vnet_if_clone_init();
461 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
466 vnet_if_uninit(const void *unused __unused)
469 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
470 "not empty", __func__, __LINE__, &V_ifnet));
471 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
472 "not empty", __func__, __LINE__, &V_ifg_head));
474 free((caddr_t)V_ifindex_table, M_IFNET);
476 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
477 vnet_if_uninit, NULL);
480 vnet_if_return(const void *unused __unused)
482 struct ifnet *ifp, *nifp;
484 /* Return all inherited interfaces to their parent vnets. */
485 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
486 if (ifp->if_home_vnet != ifp->if_vnet)
487 if_vmove(ifp, ifp->if_home_vnet);
490 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
491 vnet_if_return, NULL);
504 IFNET_WLOCK_ASSERT();
505 oldlim = V_if_indexlim;
507 n = (oldlim << 1) * sizeof(*e);
508 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
510 if (V_if_indexlim != oldlim) {
514 if (V_ifindex_table != NULL) {
515 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
516 old = V_ifindex_table;
524 * Allocate a struct ifnet and an index for an interface. A layer 2
525 * common structure will also be allocated if an allocation routine is
526 * registered for the passed type.
529 if_alloc_domain(u_char type, int numa_domain)
535 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
536 if (numa_domain == IF_NODOM)
537 ifp = malloc(sizeof(struct ifnet), M_IFNET,
540 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
541 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
544 idx = ifindex_alloc(&old);
545 if (__predict_false(idx == USHRT_MAX)) {
547 epoch_wait_preempt(net_epoch_preempt);
551 ifnet_setbyindex(idx, IFNET_HOLD);
555 ifp->if_alloctype = type;
556 ifp->if_numa_domain = numa_domain;
558 ifp->if_vnet = curvnet;
560 if (if_com_alloc[type] != NULL) {
561 ifp->if_l2com = if_com_alloc[type](type, ifp);
562 if (ifp->if_l2com == NULL) {
569 IF_ADDR_LOCK_INIT(ifp);
570 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
571 ifp->if_afdata_initialized = 0;
572 IF_AFDATA_LOCK_INIT(ifp);
573 CK_STAILQ_INIT(&ifp->if_addrhead);
574 CK_STAILQ_INIT(&ifp->if_multiaddrs);
575 CK_STAILQ_INIT(&ifp->if_groups);
579 ifq_init(&ifp->if_snd, ifp);
581 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
582 for (int i = 0; i < IFCOUNTERS; i++)
583 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
584 ifp->if_get_counter = if_get_counter_default;
585 ifp->if_pcp = IFNET_PCP_NONE;
586 ifnet_setbyindex(ifp->if_index, ifp);
591 if_alloc_dev(u_char type, device_t dev)
595 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
596 return (if_alloc_domain(type, IF_NODOM));
597 return (if_alloc_domain(type, numa_domain));
601 if_alloc(u_char type)
604 return (if_alloc_domain(type, IF_NODOM));
607 * Do the actual work of freeing a struct ifnet, and layer 2 common
608 * structure. This call is made when the last reference to an
609 * interface is released.
612 if_free_internal(struct ifnet *ifp)
615 KASSERT((ifp->if_flags & IFF_DYING),
616 ("if_free_internal: interface not dying"));
618 if (if_com_free[ifp->if_alloctype] != NULL)
619 if_com_free[ifp->if_alloctype](ifp->if_l2com,
623 mac_ifnet_destroy(ifp);
625 IF_AFDATA_DESTROY(ifp);
626 IF_ADDR_LOCK_DESTROY(ifp);
627 ifq_delete(&ifp->if_snd);
629 for (int i = 0; i < IFCOUNTERS; i++)
630 counter_u64_free(ifp->if_counters[i]);
632 free(ifp->if_description, M_IFDESCR);
633 free(ifp->if_hw_addr, M_IFADDR);
634 if (ifp->if_numa_domain == IF_NODOM)
637 free_domain(ifp, M_IFNET);
641 if_destroy(epoch_context_t ctx)
645 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
646 if_free_internal(ifp);
650 * Deregister an interface and free the associated storage.
653 if_free(struct ifnet *ifp)
656 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
658 CURVNET_SET_QUIET(ifp->if_vnet);
660 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
661 ("%s: freeing unallocated ifnet", ifp->if_xname));
663 ifindex_free_locked(ifp->if_index);
666 if (refcount_release(&ifp->if_refcount))
667 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
672 * Interfaces to keep an ifnet type-stable despite the possibility of the
673 * driver calling if_free(). If there are additional references, we defer
674 * freeing the underlying data structure.
677 if_ref(struct ifnet *ifp)
680 /* We don't assert the ifnet list lock here, but arguably should. */
681 refcount_acquire(&ifp->if_refcount);
685 if_rele(struct ifnet *ifp)
688 if (!refcount_release(&ifp->if_refcount))
690 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
694 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
697 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
699 if (ifq->ifq_maxlen == 0)
700 ifq->ifq_maxlen = ifqmaxlen;
703 ifq->altq_disc = NULL;
704 ifq->altq_flags &= ALTQF_CANTCHANGE;
705 ifq->altq_tbr = NULL;
710 ifq_delete(struct ifaltq *ifq)
712 mtx_destroy(&ifq->ifq_mtx);
716 * Perform generic interface initialization tasks and attach the interface
717 * to the list of "active" interfaces. If vmove flag is set on entry
718 * to if_attach_internal(), perform only a limited subset of initialization
719 * tasks, given that we are moving from one vnet to another an ifnet which
720 * has already been fully initialized.
722 * Note that if_detach_internal() removes group membership unconditionally
723 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
724 * Thus, when if_vmove() is applied to a cloned interface, group membership
725 * is lost while a cloned one always joins a group whose name is
726 * ifc->ifc_name. To recover this after if_detach_internal() and
727 * if_attach_internal(), the cloner should be specified to
728 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
729 * attempts to join a group whose name is ifc->ifc_name.
732 * - The decision to return void and thus require this function to
733 * succeed is questionable.
734 * - We should probably do more sanity checking. For instance we don't
735 * do anything to insure if_xname is unique or non-empty.
738 if_attach(struct ifnet *ifp)
741 if_attach_internal(ifp, 0, NULL);
745 * Compute the least common TSO limit.
748 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
751 * 1) If there is no limit currently, take the limit from
752 * the network adapter.
754 * 2) If the network adapter has a limit below the current
757 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
758 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
759 pmax->tsomaxbytes = ifp->if_hw_tsomax;
761 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
762 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
763 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
765 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
766 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
767 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
772 * Update TSO limit of a network adapter.
774 * Returns zero if no change. Else non-zero.
777 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
780 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
781 ifp->if_hw_tsomax = pmax->tsomaxbytes;
784 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
785 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
788 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
789 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
796 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
798 unsigned socksize, ifasize;
799 int namelen, masklen;
800 struct sockaddr_dl *sdl;
803 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
804 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
808 ifp->if_vnet = curvnet;
809 if (ifp->if_home_vnet == NULL)
810 ifp->if_home_vnet = curvnet;
813 if_addgroup(ifp, IFG_ALL);
815 /* Restore group membership for cloned interfaces. */
816 if (vmove && ifc != NULL)
817 if_clone_addgroup(ifp, ifc);
819 getmicrotime(&ifp->if_lastchange);
820 ifp->if_epoch = time_uptime;
822 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
823 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
824 ("transmit and qflush must both either be set or both be NULL"));
825 if (ifp->if_transmit == NULL) {
826 ifp->if_transmit = if_transmit;
827 ifp->if_qflush = if_qflush;
829 if (ifp->if_input == NULL)
830 ifp->if_input = if_input_default;
832 if (ifp->if_requestencap == NULL)
833 ifp->if_requestencap = if_requestencap_default;
837 mac_ifnet_create(ifp);
841 * Create a Link Level name for this device.
843 namelen = strlen(ifp->if_xname);
845 * Always save enough space for any possiable name so we
846 * can do a rename in place later.
848 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
849 socksize = masklen + ifp->if_addrlen;
850 if (socksize < sizeof(*sdl))
851 socksize = sizeof(*sdl);
852 socksize = roundup2(socksize, sizeof(long));
853 ifasize = sizeof(*ifa) + 2 * socksize;
854 ifa = ifa_alloc(ifasize, M_WAITOK);
855 sdl = (struct sockaddr_dl *)(ifa + 1);
856 sdl->sdl_len = socksize;
857 sdl->sdl_family = AF_LINK;
858 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
859 sdl->sdl_nlen = namelen;
860 sdl->sdl_index = ifp->if_index;
861 sdl->sdl_type = ifp->if_type;
864 ifa->ifa_addr = (struct sockaddr *)sdl;
865 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
866 ifa->ifa_netmask = (struct sockaddr *)sdl;
867 sdl->sdl_len = masklen;
869 sdl->sdl_data[--namelen] = 0xff;
870 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
871 /* Reliably crash if used uninitialized. */
872 ifp->if_broadcastaddr = NULL;
874 if (ifp->if_type == IFT_ETHER) {
875 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
879 #if defined(INET) || defined(INET6)
880 /* Use defaults for TSO, if nothing is set */
881 if (ifp->if_hw_tsomax == 0 &&
882 ifp->if_hw_tsomaxsegcount == 0 &&
883 ifp->if_hw_tsomaxsegsize == 0) {
885 * The TSO defaults needs to be such that an
886 * NFS mbuf list of 35 mbufs totalling just
887 * below 64K works and that a chain of mbufs
888 * can be defragged into at most 32 segments:
890 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
891 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
892 ifp->if_hw_tsomaxsegcount = 35;
893 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
895 /* XXX some drivers set IFCAP_TSO after ethernet attach */
896 if (ifp->if_capabilities & IFCAP_TSO) {
897 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
899 ifp->if_hw_tsomaxsegcount,
900 ifp->if_hw_tsomaxsegsize);
908 * Update the interface index in the link layer address
911 for (ifa = ifp->if_addr; ifa != NULL;
912 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
913 if (ifa->ifa_addr->sa_family == AF_LINK) {
914 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
915 sdl->sdl_index = ifp->if_index;
922 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
924 curvnet->vnet_ifcnt++;
928 if (domain_init_status >= 2)
929 if_attachdomain1(ifp);
931 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
932 if (IS_DEFAULT_VNET(curvnet))
933 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
935 /* Announce the interface. */
936 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
940 if_epochalloc(void *dummy __unused)
943 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
944 net_epoch = epoch_alloc(0);
946 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
947 if_epochalloc, NULL);
950 if_attachdomain(void *dummy)
954 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
955 if_attachdomain1(ifp);
957 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
958 if_attachdomain, NULL);
961 if_attachdomain1(struct ifnet *ifp)
966 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
967 * cannot lock ifp->if_afdata initialization, entirely.
970 if (ifp->if_afdata_initialized >= domain_init_status) {
971 IF_AFDATA_UNLOCK(ifp);
972 log(LOG_WARNING, "%s called more than once on %s\n",
973 __func__, ifp->if_xname);
976 ifp->if_afdata_initialized = domain_init_status;
977 IF_AFDATA_UNLOCK(ifp);
979 /* address family dependent data region */
980 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
981 for (dp = domains; dp; dp = dp->dom_next) {
982 if (dp->dom_ifattach)
983 ifp->if_afdata[dp->dom_family] =
984 (*dp->dom_ifattach)(ifp);
989 * Remove any unicast or broadcast network addresses from an interface.
992 if_purgeaddrs(struct ifnet *ifp)
997 struct epoch_tracker et;
1000 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1001 if (ifa->ifa_addr->sa_family != AF_LINK)
1009 /* XXX: Ugly!! ad hoc just for INET */
1010 if (ifa->ifa_addr->sa_family == AF_INET) {
1011 struct ifaliasreq ifr;
1013 bzero(&ifr, sizeof(ifr));
1014 ifr.ifra_addr = *ifa->ifa_addr;
1015 if (ifa->ifa_dstaddr)
1016 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1017 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1023 if (ifa->ifa_addr->sa_family == AF_INET6) {
1025 /* ifp_addrhead is already updated */
1030 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1031 IF_ADDR_WUNLOCK(ifp);
1037 * Remove any multicast network addresses from an interface when an ifnet
1041 if_purgemaddrs(struct ifnet *ifp)
1043 struct ifmultiaddr *ifma;
1046 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1047 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1048 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1049 if_delmulti_locked(ifp, ifma, 1);
1051 IF_ADDR_WUNLOCK(ifp);
1055 * Detach an interface, removing it from the list of "active" interfaces.
1056 * If vmove flag is set on entry to if_detach_internal(), perform only a
1057 * limited subset of cleanup tasks, given that we are moving an ifnet from
1058 * one vnet to another, where it must be fully operational.
1060 * XXXRW: There are some significant questions about event ordering, and
1061 * how to prevent things from starting to use the interface during detach.
1064 if_detach(struct ifnet *ifp)
1067 CURVNET_SET_QUIET(ifp->if_vnet);
1068 if_detach_internal(ifp, 0, NULL);
1073 * The vmove flag, if set, indicates that we are called from a callpath
1074 * that is moving an interface to a different vnet instance.
1076 * The shutdown flag, if set, indicates that we are called in the
1077 * process of shutting down a vnet instance. Currently only the
1078 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1079 * on a vnet instance shutdown without this flag being set, e.g., when
1080 * the cloned interfaces are destoyed as first thing of teardown.
1083 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1093 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1094 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1097 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1099 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1101 ifp->if_flags |= IFF_DYING;
1108 * While we would want to panic here, we cannot
1109 * guarantee that the interface is indeed still on
1110 * the list given we don't hold locks all the way.
1115 panic("%s: ifp=%p not on the ifnet tailq %p",
1116 __func__, ifp, &V_ifnet);
1118 return; /* XXX this should panic as well? */
1123 * At this point we know the interface still was on the ifnet list
1124 * and we removed it so we are in a stable state.
1127 curvnet->vnet_ifcnt--;
1129 epoch_wait_preempt(net_epoch_preempt);
1131 * In any case (destroy or vmove) detach us from the groups
1132 * and remove/wait for pending events on the taskq.
1133 * XXX-BZ in theory an interface could still enqueue a taskq change?
1137 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1140 * Check if this is a cloned interface or not. Must do even if
1141 * shutting down as a if_vmove_reclaim() would move the ifp and
1142 * the if_clone_addgroup() will have a corrupted string overwise
1143 * from a gibberish pointer.
1145 if (vmove && ifcp != NULL)
1146 *ifcp = if_clone_findifc(ifp);
1152 * On VNET shutdown abort here as the stack teardown will do all
1153 * the work top-down for us.
1156 /* Give interface users the chance to clean up. */
1157 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1160 * In case of a vmove we are done here without error.
1161 * If we would signal an error it would lead to the same
1162 * abort as if we did not find the ifnet anymore.
1163 * if_detach() calls us in void context and does not care
1164 * about an early abort notification, so life is splendid :)
1166 goto finish_vnet_shutdown;
1171 * At this point we are not tearing down a VNET and are either
1172 * going to destroy or vmove the interface and have to cleanup
1177 * Remove routes and flush queues.
1180 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1181 altq_disable(&ifp->if_snd);
1182 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1183 altq_detach(&ifp->if_snd);
1194 * Remove all IPv6 kernel structs related to ifp. This should be done
1195 * before removing routing entries below, since IPv6 interface direct
1196 * routes are expected to be removed by the IPv6-specific kernel API.
1197 * Otherwise, the kernel will detect some inconsistency and bark it.
1201 if_purgemaddrs(ifp);
1203 /* Announce that the interface is gone. */
1204 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1205 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1206 if (IS_DEFAULT_VNET(curvnet))
1207 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1211 * Prevent further calls into the device driver via ifnet.
1216 * Clean up all addresses.
1219 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1220 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1221 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1222 IF_ADDR_WUNLOCK(ifp);
1225 IF_ADDR_WUNLOCK(ifp);
1228 rt_flushifroutes(ifp);
1231 finish_vnet_shutdown:
1234 * We cannot hold the lock over dom_ifdetach calls as they might
1235 * sleep, for example trying to drain a callout, thus open up the
1236 * theoretical race with re-attaching.
1238 IF_AFDATA_LOCK(ifp);
1239 i = ifp->if_afdata_initialized;
1240 ifp->if_afdata_initialized = 0;
1241 IF_AFDATA_UNLOCK(ifp);
1242 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1243 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1244 (*dp->dom_ifdetach)(ifp,
1245 ifp->if_afdata[dp->dom_family]);
1246 ifp->if_afdata[dp->dom_family] = NULL;
1255 * if_vmove() performs a limited version of if_detach() in current
1256 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1257 * An attempt is made to shrink if_index in current vnet, find an
1258 * unused if_index in target vnet and calls if_grow() if necessary,
1259 * and finally find an unused if_xname for the target vnet.
1262 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1264 struct if_clone *ifc;
1265 u_int bif_dlt, bif_hdrlen;
1270 * if_detach_internal() will call the eventhandler to notify
1271 * interface departure. That will detach if_bpf. We need to
1272 * safe the dlt and hdrlen so we can re-attach it later.
1274 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1277 * Detach from current vnet, but preserve LLADDR info, do not
1278 * mark as dead etc. so that the ifnet can be reattached later.
1279 * If we cannot find it, we lost the race to someone else.
1281 rc = if_detach_internal(ifp, 1, &ifc);
1286 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1287 * the if_index for that vnet if possible.
1289 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1290 * or we'd lock on one vnet and unlock on another.
1293 ifindex_free_locked(ifp->if_index);
1297 * Perform interface-specific reassignment tasks, if provided by
1300 if (ifp->if_reassign != NULL)
1301 ifp->if_reassign(ifp, new_vnet, NULL);
1304 * Switch to the context of the target vnet.
1306 CURVNET_SET_QUIET(new_vnet);
1309 ifp->if_index = ifindex_alloc(&old);
1310 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1312 epoch_wait_preempt(net_epoch_preempt);
1316 ifnet_setbyindex(ifp->if_index, ifp);
1319 if_attach_internal(ifp, 1, ifc);
1321 if (ifp->if_bpf == NULL)
1322 bpfattach(ifp, bif_dlt, bif_hdrlen);
1328 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1331 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1337 /* Try to find the prison within our visibility. */
1338 sx_slock(&allprison_lock);
1339 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1340 sx_sunlock(&allprison_lock);
1343 prison_hold_locked(pr);
1344 mtx_unlock(&pr->pr_mtx);
1346 /* Do not try to move the iface from and to the same prison. */
1347 if (pr->pr_vnet == ifp->if_vnet) {
1352 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1353 /* XXX Lock interfaces to avoid races. */
1354 CURVNET_SET_QUIET(pr->pr_vnet);
1355 difp = ifunit(ifname);
1362 /* Make sure the VNET is stable. */
1363 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1364 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1372 /* Move the interface into the child jail/vnet. */
1373 if_vmove(ifp, pr->pr_vnet);
1375 /* Report the new if_xname back to the userland. */
1376 sprintf(ifname, "%s", ifp->if_xname);
1383 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1386 struct vnet *vnet_dst;
1390 /* Try to find the prison within our visibility. */
1391 sx_slock(&allprison_lock);
1392 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1393 sx_sunlock(&allprison_lock);
1396 prison_hold_locked(pr);
1397 mtx_unlock(&pr->pr_mtx);
1399 /* Make sure the named iface exists in the source prison/vnet. */
1400 CURVNET_SET(pr->pr_vnet);
1401 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1408 /* Do not try to move the iface from and to the same prison. */
1409 vnet_dst = TD_TO_VNET(td);
1410 if (vnet_dst == ifp->if_vnet) {
1416 /* Make sure the VNET is stable. */
1417 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1418 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1425 /* Get interface back from child jail/vnet. */
1426 if_vmove(ifp, vnet_dst);
1429 /* Report the new if_xname back to the userland. */
1430 sprintf(ifname, "%s", ifp->if_xname);
1438 * Add a group to an interface
1441 if_addgroup(struct ifnet *ifp, const char *groupname)
1443 struct ifg_list *ifgl;
1444 struct ifg_group *ifg = NULL;
1445 struct ifg_member *ifgm;
1448 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1449 groupname[strlen(groupname) - 1] <= '9')
1453 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1454 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1459 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1460 M_NOWAIT)) == NULL) {
1465 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1466 M_TEMP, M_NOWAIT)) == NULL) {
1472 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1473 if (!strcmp(ifg->ifg_group, groupname))
1477 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1478 M_TEMP, M_NOWAIT)) == NULL) {
1484 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1485 ifg->ifg_refcnt = 0;
1486 CK_STAILQ_INIT(&ifg->ifg_members);
1487 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1492 ifgl->ifgl_group = ifg;
1493 ifgm->ifgm_ifp = ifp;
1496 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1497 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1498 IF_ADDR_WUNLOCK(ifp);
1503 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1504 EVENTHANDLER_INVOKE(group_change_event, groupname);
1510 * Remove a group from an interface
1513 if_delgroup(struct ifnet *ifp, const char *groupname)
1515 struct ifg_list *ifgl;
1516 struct ifg_member *ifgm;
1520 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1521 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1530 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1531 IF_ADDR_WUNLOCK(ifp);
1533 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1534 if (ifgm->ifgm_ifp == ifp)
1538 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next);
1540 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1541 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1546 epoch_wait_preempt(net_epoch_preempt);
1548 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1549 free(ifgl->ifgl_group, M_TEMP);
1554 EVENTHANDLER_INVOKE(group_change_event, groupname);
1560 * Remove an interface from all groups
1563 if_delgroups(struct ifnet *ifp)
1565 struct ifg_list *ifgl;
1566 struct ifg_member *ifgm;
1567 char groupname[IFNAMSIZ];
1571 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) {
1572 ifgl = CK_STAILQ_FIRST(&ifp->if_groups);
1574 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1577 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1578 IF_ADDR_WUNLOCK(ifp);
1580 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1581 if (ifgm->ifgm_ifp == ifp)
1585 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member,
1588 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1589 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1594 epoch_wait_preempt(net_epoch_preempt);
1597 EVENTHANDLER_INVOKE(group_detach_event,
1599 free(ifgl->ifgl_group, M_TEMP);
1601 EVENTHANDLER_INVOKE(group_change_event, groupname);
1609 ifgr_group_get(void *ifgrp)
1611 union ifgroupreq_union *ifgrup;
1614 #ifdef COMPAT_FREEBSD32
1615 if (SV_CURPROC_FLAG(SV_ILP32))
1616 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1618 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1621 static struct ifg_req *
1622 ifgr_groups_get(void *ifgrp)
1624 union ifgroupreq_union *ifgrup;
1627 #ifdef COMPAT_FREEBSD32
1628 if (SV_CURPROC_FLAG(SV_ILP32))
1629 return ((struct ifg_req *)(uintptr_t)
1630 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1632 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1636 * Stores all groups from an interface in memory pointed to by ifgr.
1639 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1641 struct epoch_tracker et;
1643 struct ifg_list *ifgl;
1644 struct ifg_req ifgrq, *ifgp;
1646 if (ifgr->ifgr_len == 0) {
1647 NET_EPOCH_ENTER(et);
1648 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1649 ifgr->ifgr_len += sizeof(struct ifg_req);
1654 len = ifgr->ifgr_len;
1655 ifgp = ifgr_groups_get(ifgr);
1657 NET_EPOCH_ENTER(et);
1658 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1659 if (len < sizeof(ifgrq)) {
1663 bzero(&ifgrq, sizeof ifgrq);
1664 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1665 sizeof(ifgrq.ifgrq_group));
1666 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1670 len -= sizeof(ifgrq);
1679 * Stores all members of a group in memory pointed to by igfr
1682 if_getgroupmembers(struct ifgroupreq *ifgr)
1684 struct ifg_group *ifg;
1685 struct ifg_member *ifgm;
1686 struct ifg_req ifgrq, *ifgp;
1690 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1691 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1698 if (ifgr->ifgr_len == 0) {
1699 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1700 ifgr->ifgr_len += sizeof(ifgrq);
1705 len = ifgr->ifgr_len;
1706 ifgp = ifgr_groups_get(ifgr);
1707 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1708 if (len < sizeof(ifgrq)) {
1712 bzero(&ifgrq, sizeof ifgrq);
1713 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1714 sizeof(ifgrq.ifgrq_member));
1715 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1719 len -= sizeof(ifgrq);
1728 * Return counter values from counter(9)s stored in ifnet.
1731 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1734 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1736 return (counter_u64_fetch(ifp->if_counters[cnt]));
1740 * Increase an ifnet counter. Usually used for counters shared
1741 * between the stack and a driver, but function supports them all.
1744 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1747 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1749 counter_u64_add(ifp->if_counters[cnt], inc);
1753 * Copy data from ifnet to userland API structure if_data.
1756 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1759 ifd->ifi_type = ifp->if_type;
1760 ifd->ifi_physical = 0;
1761 ifd->ifi_addrlen = ifp->if_addrlen;
1762 ifd->ifi_hdrlen = ifp->if_hdrlen;
1763 ifd->ifi_link_state = ifp->if_link_state;
1765 ifd->ifi_datalen = sizeof(struct if_data);
1766 ifd->ifi_mtu = ifp->if_mtu;
1767 ifd->ifi_metric = ifp->if_metric;
1768 ifd->ifi_baudrate = ifp->if_baudrate;
1769 ifd->ifi_hwassist = ifp->if_hwassist;
1770 ifd->ifi_epoch = ifp->if_epoch;
1771 ifd->ifi_lastchange = ifp->if_lastchange;
1773 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1774 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1775 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1776 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1777 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1778 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1779 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1780 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1781 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1782 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1783 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1784 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1788 * Wrapper functions for struct ifnet address list locking macros. These are
1789 * used by kernel modules to avoid encoding programming interface or binary
1790 * interface assumptions that may be violated when kernel-internal locking
1791 * approaches change.
1794 if_addr_rlock(struct ifnet *ifp)
1797 epoch_enter_preempt(net_epoch_preempt, curthread->td_et);
1801 if_addr_runlock(struct ifnet *ifp)
1804 epoch_exit_preempt(net_epoch_preempt, curthread->td_et);
1808 if_maddr_rlock(if_t ifp)
1811 epoch_enter_preempt(net_epoch_preempt, curthread->td_et);
1815 if_maddr_runlock(if_t ifp)
1818 epoch_exit_preempt(net_epoch_preempt, curthread->td_et);
1822 * Initialization, destruction and refcounting functions for ifaddrs.
1825 ifa_alloc(size_t size, int flags)
1829 KASSERT(size >= sizeof(struct ifaddr),
1830 ("%s: invalid size %zu", __func__, size));
1832 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1836 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1838 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1840 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1842 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1845 refcount_init(&ifa->ifa_refcnt, 1);
1850 /* free(NULL) is okay */
1851 counter_u64_free(ifa->ifa_opackets);
1852 counter_u64_free(ifa->ifa_ipackets);
1853 counter_u64_free(ifa->ifa_obytes);
1854 counter_u64_free(ifa->ifa_ibytes);
1855 free(ifa, M_IFADDR);
1861 ifa_ref(struct ifaddr *ifa)
1864 refcount_acquire(&ifa->ifa_refcnt);
1868 ifa_destroy(epoch_context_t ctx)
1872 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1873 counter_u64_free(ifa->ifa_opackets);
1874 counter_u64_free(ifa->ifa_ipackets);
1875 counter_u64_free(ifa->ifa_obytes);
1876 counter_u64_free(ifa->ifa_ibytes);
1877 free(ifa, M_IFADDR);
1881 ifa_free(struct ifaddr *ifa)
1884 if (refcount_release(&ifa->ifa_refcnt))
1885 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1890 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1891 struct sockaddr *ia)
1893 struct epoch_tracker et;
1895 struct rt_addrinfo info;
1896 struct sockaddr_dl null_sdl;
1901 bzero(&info, sizeof(info));
1902 if (cmd != RTM_DELETE)
1903 info.rti_ifp = V_loif;
1904 if (cmd == RTM_ADD) {
1905 /* explicitly specify (loopback) ifa */
1906 if (info.rti_ifp != NULL) {
1907 NET_EPOCH_ENTER(et);
1908 info.rti_ifa = ifaof_ifpforaddr(ifa->ifa_addr, info.rti_ifp);
1909 if (info.rti_ifa != NULL)
1910 ifa_ref(info.rti_ifa);
1914 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1915 info.rti_info[RTAX_DST] = ia;
1916 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1917 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1919 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1922 !(cmd == RTM_ADD && error == EEXIST) &&
1923 !(cmd == RTM_DELETE && error == ENOENT))
1924 if_printf(ifp, "%s failed: %d\n", otype, error);
1930 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1933 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1937 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1940 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1944 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1947 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1951 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1952 * structs used to represent other address families, it is necessary
1953 * to perform a different comparison.
1956 #define sa_dl_equal(a1, a2) \
1957 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1958 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1959 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1960 CLLADDR((const struct sockaddr_dl *)(a2)), \
1961 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1964 * Locate an interface based on a complete address.
1968 ifa_ifwithaddr(const struct sockaddr *addr)
1973 MPASS(in_epoch(net_epoch_preempt));
1974 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1975 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1976 if (ifa->ifa_addr->sa_family != addr->sa_family)
1978 if (sa_equal(addr, ifa->ifa_addr)) {
1981 /* IP6 doesn't have broadcast */
1982 if ((ifp->if_flags & IFF_BROADCAST) &&
1983 ifa->ifa_broadaddr &&
1984 ifa->ifa_broadaddr->sa_len != 0 &&
1985 sa_equal(ifa->ifa_broadaddr, addr)) {
1996 ifa_ifwithaddr_check(const struct sockaddr *addr)
1998 struct epoch_tracker et;
2001 NET_EPOCH_ENTER(et);
2002 rc = (ifa_ifwithaddr(addr) != NULL);
2008 * Locate an interface based on the broadcast address.
2012 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
2017 MPASS(in_epoch(net_epoch_preempt));
2018 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2019 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2021 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2022 if (ifa->ifa_addr->sa_family != addr->sa_family)
2024 if ((ifp->if_flags & IFF_BROADCAST) &&
2025 ifa->ifa_broadaddr &&
2026 ifa->ifa_broadaddr->sa_len != 0 &&
2027 sa_equal(ifa->ifa_broadaddr, addr)) {
2038 * Locate the point to point interface with a given destination address.
2042 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2047 MPASS(in_epoch(net_epoch_preempt));
2048 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2049 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2051 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2053 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2054 if (ifa->ifa_addr->sa_family != addr->sa_family)
2056 if (ifa->ifa_dstaddr != NULL &&
2057 sa_equal(addr, ifa->ifa_dstaddr)) {
2068 * Find an interface on a specific network. If many, choice
2069 * is most specific found.
2072 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2076 struct ifaddr *ifa_maybe = NULL;
2077 u_int af = addr->sa_family;
2078 const char *addr_data = addr->sa_data, *cplim;
2080 MPASS(in_epoch(net_epoch_preempt));
2082 * AF_LINK addresses can be looked up directly by their index number,
2083 * so do that if we can.
2085 if (af == AF_LINK) {
2086 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2087 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2088 return (ifaddr_byindex(sdl->sdl_index));
2092 * Scan though each interface, looking for ones that have addresses
2093 * in this address family and the requested fib.
2095 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2096 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2098 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2099 const char *cp, *cp2, *cp3;
2101 if (ifa->ifa_addr->sa_family != af)
2103 if (af == AF_INET &&
2104 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2106 * This is a bit broken as it doesn't
2107 * take into account that the remote end may
2108 * be a single node in the network we are
2110 * The trouble is that we don't know the
2111 * netmask for the remote end.
2113 if (ifa->ifa_dstaddr != NULL &&
2114 sa_equal(addr, ifa->ifa_dstaddr)) {
2119 * Scan all the bits in the ifa's address.
2120 * If a bit dissagrees with what we are
2121 * looking for, mask it with the netmask
2122 * to see if it really matters.
2123 * (A byte at a time)
2125 if (ifa->ifa_netmask == 0)
2128 cp2 = ifa->ifa_addr->sa_data;
2129 cp3 = ifa->ifa_netmask->sa_data;
2130 cplim = ifa->ifa_netmask->sa_len
2131 + (char *)ifa->ifa_netmask;
2133 if ((*cp++ ^ *cp2++) & *cp3++)
2134 goto next; /* next address! */
2136 * If the netmask of what we just found
2137 * is more specific than what we had before
2138 * (if we had one), or if the virtual status
2139 * of new prefix is better than of the old one,
2140 * then remember the new one before continuing
2141 * to search for an even better one.
2143 if (ifa_maybe == NULL ||
2144 ifa_preferred(ifa_maybe, ifa) ||
2145 rn_refines((caddr_t)ifa->ifa_netmask,
2146 (caddr_t)ifa_maybe->ifa_netmask)) {
2159 * Find an interface address specific to an interface best matching
2163 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2166 const char *cp, *cp2, *cp3;
2168 struct ifaddr *ifa_maybe = NULL;
2169 u_int af = addr->sa_family;
2174 MPASS(in_epoch(net_epoch_preempt));
2175 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2176 if (ifa->ifa_addr->sa_family != af)
2178 if (ifa_maybe == NULL)
2180 if (ifa->ifa_netmask == 0) {
2181 if (sa_equal(addr, ifa->ifa_addr) ||
2182 (ifa->ifa_dstaddr &&
2183 sa_equal(addr, ifa->ifa_dstaddr)))
2187 if (ifp->if_flags & IFF_POINTOPOINT) {
2188 if (sa_equal(addr, ifa->ifa_dstaddr))
2192 cp2 = ifa->ifa_addr->sa_data;
2193 cp3 = ifa->ifa_netmask->sa_data;
2194 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2195 for (; cp3 < cplim; cp3++)
2196 if ((*cp++ ^ *cp2++) & *cp3)
2208 * See whether new ifa is better than current one:
2209 * 1) A non-virtual one is preferred over virtual.
2210 * 2) A virtual in master state preferred over any other state.
2212 * Used in several address selecting functions.
2215 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2218 return (cur->ifa_carp && (!next->ifa_carp ||
2219 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2222 struct sockaddr_dl *
2223 link_alloc_sdl(size_t size, int flags)
2226 return (malloc(size, M_TEMP, flags));
2230 link_free_sdl(struct sockaddr *sa)
2236 * Fills in given sdl with interface basic info.
2237 * Returns pointer to filled sdl.
2239 struct sockaddr_dl *
2240 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2242 struct sockaddr_dl *sdl;
2244 sdl = (struct sockaddr_dl *)paddr;
2245 memset(sdl, 0, sizeof(struct sockaddr_dl));
2246 sdl->sdl_len = sizeof(struct sockaddr_dl);
2247 sdl->sdl_family = AF_LINK;
2248 sdl->sdl_index = ifp->if_index;
2249 sdl->sdl_type = iftype;
2255 * Mark an interface down and notify protocols of
2259 if_unroute(struct ifnet *ifp, int flag, int fam)
2263 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2265 ifp->if_flags &= ~flag;
2266 getmicrotime(&ifp->if_lastchange);
2267 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2268 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2269 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2270 ifp->if_qflush(ifp);
2273 (*carp_linkstate_p)(ifp);
2278 * Mark an interface up and notify protocols of
2282 if_route(struct ifnet *ifp, int flag, int fam)
2286 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2288 ifp->if_flags |= flag;
2289 getmicrotime(&ifp->if_lastchange);
2290 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2291 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2292 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2294 (*carp_linkstate_p)(ifp);
2301 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2302 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2303 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2304 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2305 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2306 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2307 int (*vlan_setcookie_p)(struct ifnet *, void *);
2308 void *(*vlan_cookie_p)(struct ifnet *);
2311 * Handle a change in the interface link state. To avoid LORs
2312 * between driver lock and upper layer locks, as well as possible
2313 * recursions, we post event to taskqueue, and all job
2314 * is done in static do_link_state_change().
2317 if_link_state_change(struct ifnet *ifp, int link_state)
2319 /* Return if state hasn't changed. */
2320 if (ifp->if_link_state == link_state)
2323 ifp->if_link_state = link_state;
2325 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2329 do_link_state_change(void *arg, int pending)
2331 struct ifnet *ifp = (struct ifnet *)arg;
2332 int link_state = ifp->if_link_state;
2333 CURVNET_SET(ifp->if_vnet);
2335 /* Notify that the link state has changed. */
2337 if (ifp->if_vlantrunk != NULL)
2338 (*vlan_link_state_p)(ifp);
2340 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2341 ifp->if_l2com != NULL)
2342 (*ng_ether_link_state_p)(ifp, link_state);
2344 (*carp_linkstate_p)(ifp);
2346 ifp->if_bridge_linkstate(ifp);
2348 (*lagg_linkstate_p)(ifp, link_state);
2350 if (IS_DEFAULT_VNET(curvnet))
2351 devctl_notify("IFNET", ifp->if_xname,
2352 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2355 if_printf(ifp, "%d link states coalesced\n", pending);
2356 if (log_link_state_change)
2357 if_printf(ifp, "link state changed to %s\n",
2358 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2359 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2364 * Mark an interface down and notify protocols of
2368 if_down(struct ifnet *ifp)
2371 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2372 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2376 * Mark an interface up and notify protocols of
2380 if_up(struct ifnet *ifp)
2383 if_route(ifp, IFF_UP, AF_UNSPEC);
2384 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2388 * Flush an interface queue.
2391 if_qflush(struct ifnet *ifp)
2399 if (ALTQ_IS_ENABLED(ifq))
2403 while ((m = n) != NULL) {
2414 * Map interface name to interface structure pointer, with or without
2415 * returning a reference.
2418 ifunit_ref(const char *name)
2420 struct epoch_tracker et;
2423 NET_EPOCH_ENTER(et);
2424 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2425 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2426 !(ifp->if_flags & IFF_DYING))
2436 ifunit(const char *name)
2438 struct epoch_tracker et;
2441 NET_EPOCH_ENTER(et);
2442 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2443 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2451 ifr_buffer_get_buffer(void *data)
2453 union ifreq_union *ifrup;
2456 #ifdef COMPAT_FREEBSD32
2457 if (SV_CURPROC_FLAG(SV_ILP32))
2458 return ((void *)(uintptr_t)
2459 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2461 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2465 ifr_buffer_set_buffer_null(void *data)
2467 union ifreq_union *ifrup;
2470 #ifdef COMPAT_FREEBSD32
2471 if (SV_CURPROC_FLAG(SV_ILP32))
2472 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2475 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2479 ifr_buffer_get_length(void *data)
2481 union ifreq_union *ifrup;
2484 #ifdef COMPAT_FREEBSD32
2485 if (SV_CURPROC_FLAG(SV_ILP32))
2486 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2488 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2492 ifr_buffer_set_length(void *data, size_t len)
2494 union ifreq_union *ifrup;
2497 #ifdef COMPAT_FREEBSD32
2498 if (SV_CURPROC_FLAG(SV_ILP32))
2499 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2502 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2506 ifr_data_get_ptr(void *ifrp)
2508 union ifreq_union *ifrup;
2511 #ifdef COMPAT_FREEBSD32
2512 if (SV_CURPROC_FLAG(SV_ILP32))
2513 return ((void *)(uintptr_t)
2514 ifrup->ifr32.ifr_ifru.ifru_data);
2516 return (ifrup->ifr.ifr_ifru.ifru_data);
2520 * Hardware specific interface ioctls.
2523 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2526 int error = 0, do_ifup = 0;
2527 int new_flags, temp_flags;
2528 size_t namelen, onamelen;
2530 char *descrbuf, *odescrbuf;
2531 char new_name[IFNAMSIZ];
2533 struct sockaddr_dl *sdl;
2535 ifr = (struct ifreq *)data;
2538 ifr->ifr_index = ifp->if_index;
2542 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2543 ifr->ifr_flags = temp_flags & 0xffff;
2544 ifr->ifr_flagshigh = temp_flags >> 16;
2548 ifr->ifr_reqcap = ifp->if_capabilities;
2549 ifr->ifr_curcap = ifp->if_capenable;
2554 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2559 ifr->ifr_metric = ifp->if_metric;
2563 ifr->ifr_mtu = ifp->if_mtu;
2567 /* XXXGL: did this ever worked? */
2573 sx_slock(&ifdescr_sx);
2574 if (ifp->if_description == NULL)
2577 /* space for terminating nul */
2578 descrlen = strlen(ifp->if_description) + 1;
2579 if (ifr_buffer_get_length(ifr) < descrlen)
2580 ifr_buffer_set_buffer_null(ifr);
2582 error = copyout(ifp->if_description,
2583 ifr_buffer_get_buffer(ifr), descrlen);
2584 ifr_buffer_set_length(ifr, descrlen);
2586 sx_sunlock(&ifdescr_sx);
2590 error = priv_check(td, PRIV_NET_SETIFDESCR);
2595 * Copy only (length-1) bytes to make sure that
2596 * if_description is always nul terminated. The
2597 * length parameter is supposed to count the
2598 * terminating nul in.
2600 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2601 return (ENAMETOOLONG);
2602 else if (ifr_buffer_get_length(ifr) == 0)
2605 descrbuf = malloc(ifr_buffer_get_length(ifr),
2606 M_IFDESCR, M_WAITOK | M_ZERO);
2607 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2608 ifr_buffer_get_length(ifr) - 1);
2610 free(descrbuf, M_IFDESCR);
2615 sx_xlock(&ifdescr_sx);
2616 odescrbuf = ifp->if_description;
2617 ifp->if_description = descrbuf;
2618 sx_xunlock(&ifdescr_sx);
2620 getmicrotime(&ifp->if_lastchange);
2621 free(odescrbuf, M_IFDESCR);
2625 ifr->ifr_fib = ifp->if_fib;
2629 error = priv_check(td, PRIV_NET_SETIFFIB);
2632 if (ifr->ifr_fib >= rt_numfibs)
2635 ifp->if_fib = ifr->ifr_fib;
2639 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2643 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2644 * check, so we don't need special handling here yet.
2646 new_flags = (ifr->ifr_flags & 0xffff) |
2647 (ifr->ifr_flagshigh << 16);
2648 if (ifp->if_flags & IFF_UP &&
2649 (new_flags & IFF_UP) == 0) {
2651 } else if (new_flags & IFF_UP &&
2652 (ifp->if_flags & IFF_UP) == 0) {
2655 /* See if permanently promiscuous mode bit is about to flip */
2656 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2657 if (new_flags & IFF_PPROMISC)
2658 ifp->if_flags |= IFF_PROMISC;
2659 else if (ifp->if_pcount == 0)
2660 ifp->if_flags &= ~IFF_PROMISC;
2661 if (log_promisc_mode_change)
2662 if_printf(ifp, "permanently promiscuous mode %s\n",
2663 ((new_flags & IFF_PPROMISC) ?
2664 "enabled" : "disabled"));
2666 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2667 (new_flags &~ IFF_CANTCHANGE);
2668 if (ifp->if_ioctl) {
2669 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2673 getmicrotime(&ifp->if_lastchange);
2677 error = priv_check(td, PRIV_NET_SETIFCAP);
2680 if (ifp->if_ioctl == NULL)
2681 return (EOPNOTSUPP);
2682 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2684 error = (*ifp->if_ioctl)(ifp, cmd, data);
2686 getmicrotime(&ifp->if_lastchange);
2691 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2696 error = priv_check(td, PRIV_NET_SETIFNAME);
2699 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2703 if (new_name[0] == '\0')
2705 if (new_name[IFNAMSIZ-1] != '\0') {
2706 new_name[IFNAMSIZ-1] = '\0';
2707 if (strlen(new_name) == IFNAMSIZ-1)
2710 if (ifunit(new_name) != NULL)
2714 * XXX: Locking. Nothing else seems to lock if_flags,
2715 * and there are numerous other races with the
2716 * ifunit() checks not being atomic with namespace
2717 * changes (renames, vmoves, if_attach, etc).
2719 ifp->if_flags |= IFF_RENAMING;
2721 /* Announce the departure of the interface. */
2722 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2723 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2725 if_printf(ifp, "changing name to '%s'\n", new_name);
2728 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2730 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2731 namelen = strlen(new_name);
2732 onamelen = sdl->sdl_nlen;
2734 * Move the address if needed. This is safe because we
2735 * allocate space for a name of length IFNAMSIZ when we
2736 * create this in if_attach().
2738 if (namelen != onamelen) {
2739 bcopy(sdl->sdl_data + onamelen,
2740 sdl->sdl_data + namelen, sdl->sdl_alen);
2742 bcopy(new_name, sdl->sdl_data, namelen);
2743 sdl->sdl_nlen = namelen;
2744 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2745 bzero(sdl->sdl_data, onamelen);
2746 while (namelen != 0)
2747 sdl->sdl_data[--namelen] = 0xff;
2748 IF_ADDR_WUNLOCK(ifp);
2750 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2751 /* Announce the return of the interface. */
2752 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2754 ifp->if_flags &= ~IFF_RENAMING;
2759 error = priv_check(td, PRIV_NET_SETIFVNET);
2762 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2767 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2770 ifp->if_metric = ifr->ifr_metric;
2771 getmicrotime(&ifp->if_lastchange);
2775 error = priv_check(td, PRIV_NET_SETIFPHYS);
2778 if (ifp->if_ioctl == NULL)
2779 return (EOPNOTSUPP);
2780 error = (*ifp->if_ioctl)(ifp, cmd, data);
2782 getmicrotime(&ifp->if_lastchange);
2787 u_long oldmtu = ifp->if_mtu;
2789 error = priv_check(td, PRIV_NET_SETIFMTU);
2792 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2794 if (ifp->if_ioctl == NULL)
2795 return (EOPNOTSUPP);
2796 error = (*ifp->if_ioctl)(ifp, cmd, data);
2798 getmicrotime(&ifp->if_lastchange);
2801 NETDUMP_REINIT(ifp);
2805 * If the link MTU changed, do network layer specific procedure.
2807 if (ifp->if_mtu != oldmtu) {
2818 if (cmd == SIOCADDMULTI)
2819 error = priv_check(td, PRIV_NET_ADDMULTI);
2821 error = priv_check(td, PRIV_NET_DELMULTI);
2825 /* Don't allow group membership on non-multicast interfaces. */
2826 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2827 return (EOPNOTSUPP);
2829 /* Don't let users screw up protocols' entries. */
2830 if (ifr->ifr_addr.sa_family != AF_LINK)
2833 if (cmd == SIOCADDMULTI) {
2834 struct epoch_tracker et;
2835 struct ifmultiaddr *ifma;
2838 * Userland is only permitted to join groups once
2839 * via the if_addmulti() KPI, because it cannot hold
2840 * struct ifmultiaddr * between calls. It may also
2841 * lose a race while we check if the membership
2844 NET_EPOCH_ENTER(et);
2845 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2850 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2852 error = if_delmulti(ifp, &ifr->ifr_addr);
2855 getmicrotime(&ifp->if_lastchange);
2858 case SIOCSIFPHYADDR:
2859 case SIOCDIFPHYADDR:
2861 case SIOCSIFPHYADDR_IN6:
2864 case SIOCSIFGENERIC:
2865 error = priv_check(td, PRIV_NET_HWIOCTL);
2868 if (ifp->if_ioctl == NULL)
2869 return (EOPNOTSUPP);
2870 error = (*ifp->if_ioctl)(ifp, cmd, data);
2872 getmicrotime(&ifp->if_lastchange);
2876 case SIOCGIFPSRCADDR:
2877 case SIOCGIFPDSTADDR:
2880 case SIOCGIFGENERIC:
2882 case SIOCGIFRSSHASH:
2883 if (ifp->if_ioctl == NULL)
2884 return (EOPNOTSUPP);
2885 error = (*ifp->if_ioctl)(ifp, cmd, data);
2889 error = priv_check(td, PRIV_NET_SETLLADDR);
2892 error = if_setlladdr(ifp,
2893 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2897 error = if_gethwaddr(ifp, ifr);
2900 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2901 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2904 if ((error = if_addgroup(ifp,
2905 ifgr_group_get((struct ifgroupreq *)data))))
2909 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2910 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2914 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2915 error = priv_check(td, PRIV_NET_DELIFGROUP);
2918 if ((error = if_delgroup(ifp,
2919 ifgr_group_get((struct ifgroupreq *)data))))
2930 #ifdef COMPAT_FREEBSD32
2938 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2941 #ifdef COMPAT_FREEBSD32
2943 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2945 struct ifmediareq32 *ifmr32;
2947 ifmr32 = (struct ifmediareq32 *)data;
2948 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2949 sizeof(ifmr->ifm_name));
2950 ifmr->ifm_current = ifmr32->ifm_current;
2951 ifmr->ifm_mask = ifmr32->ifm_mask;
2952 ifmr->ifm_status = ifmr32->ifm_status;
2953 ifmr->ifm_active = ifmr32->ifm_active;
2954 ifmr->ifm_count = ifmr32->ifm_count;
2955 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2959 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2961 struct ifmediareq32 *ifmr32;
2963 ifmr32 = (struct ifmediareq32 *)data;
2964 ifmr32->ifm_current = ifmr->ifm_current;
2965 ifmr32->ifm_mask = ifmr->ifm_mask;
2966 ifmr32->ifm_status = ifmr->ifm_status;
2967 ifmr32->ifm_active = ifmr->ifm_active;
2968 ifmr32->ifm_count = ifmr->ifm_count;
2976 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2978 #ifdef COMPAT_FREEBSD32
2979 caddr_t saved_data = NULL;
2980 struct ifmediareq ifmr;
2981 struct ifmediareq *ifmrp;
2991 CURVNET_SET(so->so_vnet);
2993 /* Make sure the VNET is stable. */
2994 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2995 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3005 error = ifconf(cmd, data);
3009 #ifdef COMPAT_FREEBSD32
3012 struct ifconf32 *ifc32;
3015 ifc32 = (struct ifconf32 *)data;
3016 ifc.ifc_len = ifc32->ifc_len;
3017 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3019 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3022 ifc32->ifc_len = ifc.ifc_len;
3028 #ifdef COMPAT_FREEBSD32
3031 case SIOCGIFMEDIA32:
3032 case SIOCGIFXMEDIA32:
3034 ifmr_init(ifmrp, data);
3035 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3037 data = (caddr_t)ifmrp;
3041 ifr = (struct ifreq *)data;
3045 error = priv_check(td, PRIV_NET_SETIFVNET);
3047 error = if_vmove_reclaim(td, ifr->ifr_name,
3053 error = priv_check(td, PRIV_NET_IFCREATE);
3055 error = if_clone_create(ifr->ifr_name,
3056 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3057 ifr_data_get_ptr(ifr) : NULL);
3060 error = priv_check(td, PRIV_NET_IFDESTROY);
3062 error = if_clone_destroy(ifr->ifr_name);
3065 case SIOCIFGCLONERS:
3066 error = if_clone_list((struct if_clonereq *)data);
3069 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3070 error = if_getgroupmembers((struct ifgroupreq *)data);
3073 #if defined(INET) || defined(INET6)
3076 if (carp_ioctl_p == NULL)
3077 error = EPROTONOSUPPORT;
3079 error = (*carp_ioctl_p)(ifr, cmd, td);
3084 ifp = ifunit_ref(ifr->ifr_name);
3090 error = ifhwioctl(cmd, ifp, data, td);
3091 if (error != ENOIOCTL)
3094 oif_flags = ifp->if_flags;
3095 if (so->so_proto == NULL) {
3101 * Pass the request on to the socket control method, and if the
3102 * latter returns EOPNOTSUPP, directly to the interface.
3104 * Make an exception for the legacy SIOCSIF* requests. Drivers
3105 * trust SIOCSIFADDR et al to come from an already privileged
3106 * layer, and do not perform any credentials checks or input
3109 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3111 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3112 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3113 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3114 error = (*ifp->if_ioctl)(ifp, cmd, data);
3116 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3118 if (ifp->if_flags & IFF_UP)
3126 #ifdef COMPAT_FREEBSD32
3127 if (ifmrp != NULL) {
3128 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3129 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3132 ifmr_update(ifmrp, data);
3140 * The code common to handling reference counted flags,
3141 * e.g., in ifpromisc() and if_allmulti().
3142 * The "pflag" argument can specify a permanent mode flag to check,
3143 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3145 * Only to be used on stack-owned flags, not driver-owned flags.
3148 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3152 int oldflags, oldcount;
3154 /* Sanity checks to catch programming errors */
3155 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3156 ("%s: setting driver-owned flag %d", __func__, flag));
3159 KASSERT(*refcount >= 0,
3160 ("%s: increment negative refcount %d for flag %d",
3161 __func__, *refcount, flag));
3163 KASSERT(*refcount > 0,
3164 ("%s: decrement non-positive refcount %d for flag %d",
3165 __func__, *refcount, flag));
3167 /* In case this mode is permanent, just touch refcount */
3168 if (ifp->if_flags & pflag) {
3169 *refcount += onswitch ? 1 : -1;
3173 /* Save ifnet parameters for if_ioctl() may fail */
3174 oldcount = *refcount;
3175 oldflags = ifp->if_flags;
3178 * See if we aren't the only and touching refcount is enough.
3179 * Actually toggle interface flag if we are the first or last.
3184 ifp->if_flags |= flag;
3188 ifp->if_flags &= ~flag;
3191 /* Call down the driver since we've changed interface flags */
3192 if (ifp->if_ioctl == NULL) {
3196 ifr.ifr_flags = ifp->if_flags & 0xffff;
3197 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3198 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3201 /* Notify userland that interface flags have changed */
3206 /* Recover after driver error */
3207 *refcount = oldcount;
3208 ifp->if_flags = oldflags;
3213 * Set/clear promiscuous mode on interface ifp based on the truth value
3214 * of pswitch. The calls are reference counted so that only the first
3215 * "on" request actually has an effect, as does the final "off" request.
3216 * Results are undefined if the "off" and "on" requests are not matched.
3219 ifpromisc(struct ifnet *ifp, int pswitch)
3222 int oldflags = ifp->if_flags;
3224 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3225 &ifp->if_pcount, pswitch);
3226 /* If promiscuous mode status has changed, log a message */
3227 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3228 log_promisc_mode_change)
3229 if_printf(ifp, "promiscuous mode %s\n",
3230 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3235 * Return interface configuration
3236 * of system. List may be used
3237 * in later ioctl's (above) to get
3238 * other information.
3242 ifconf(u_long cmd, caddr_t data)
3244 struct ifconf *ifc = (struct ifconf *)data;
3249 int error, full = 0, valid_len, max_len;
3251 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3252 max_len = MAXPHYS - 1;
3254 /* Prevent hostile input from being able to crash the system */
3255 if (ifc->ifc_len <= 0)
3259 if (ifc->ifc_len <= max_len) {
3260 max_len = ifc->ifc_len;
3263 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3268 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3269 struct epoch_tracker et;
3273 * Zero the ifr to make sure we don't disclose the contents
3276 memset(&ifr, 0, sizeof(ifr));
3278 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3279 >= sizeof(ifr.ifr_name)) {
3282 return (ENAMETOOLONG);
3286 NET_EPOCH_ENTER(et);
3287 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3288 struct sockaddr *sa = ifa->ifa_addr;
3290 if (prison_if(curthread->td_ucred, sa) != 0)
3293 if (sa->sa_len <= sizeof(*sa)) {
3294 if (sa->sa_len < sizeof(*sa)) {
3295 memset(&ifr.ifr_ifru.ifru_addr, 0,
3296 sizeof(ifr.ifr_ifru.ifru_addr));
3297 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3300 ifr.ifr_ifru.ifru_addr = *sa;
3301 sbuf_bcat(sb, &ifr, sizeof(ifr));
3302 max_len += sizeof(ifr);
3305 offsetof(struct ifreq, ifr_addr));
3306 max_len += offsetof(struct ifreq, ifr_addr);
3307 sbuf_bcat(sb, sa, sa->sa_len);
3308 max_len += sa->sa_len;
3311 if (sbuf_error(sb) == 0)
3312 valid_len = sbuf_len(sb);
3316 sbuf_bcat(sb, &ifr, sizeof(ifr));
3317 max_len += sizeof(ifr);
3319 if (sbuf_error(sb) == 0)
3320 valid_len = sbuf_len(sb);
3326 * If we didn't allocate enough space (uncommon), try again. If
3327 * we have already allocated as much space as we are allowed,
3328 * return what we've got.
3330 if (valid_len != max_len && !full) {
3335 ifc->ifc_len = valid_len;
3337 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3343 * Just like ifpromisc(), but for all-multicast-reception mode.
3346 if_allmulti(struct ifnet *ifp, int onswitch)
3349 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3352 struct ifmultiaddr *
3353 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3355 struct ifmultiaddr *ifma;
3357 IF_ADDR_LOCK_ASSERT(ifp);
3359 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3360 if (sa->sa_family == AF_LINK) {
3361 if (sa_dl_equal(ifma->ifma_addr, sa))
3364 if (sa_equal(ifma->ifma_addr, sa))
3373 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3374 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3375 * the ifnet multicast address list here, so the caller must do that and
3376 * other setup work (such as notifying the device driver). The reference
3377 * count is initialized to 1.
3379 static struct ifmultiaddr *
3380 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3383 struct ifmultiaddr *ifma;
3384 struct sockaddr *dupsa;
3386 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3391 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3392 if (dupsa == NULL) {
3393 free(ifma, M_IFMADDR);
3396 bcopy(sa, dupsa, sa->sa_len);
3397 ifma->ifma_addr = dupsa;
3399 ifma->ifma_ifp = ifp;
3400 ifma->ifma_refcount = 1;
3401 ifma->ifma_protospec = NULL;
3404 ifma->ifma_lladdr = NULL;
3408 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3409 if (dupsa == NULL) {
3410 free(ifma->ifma_addr, M_IFMADDR);
3411 free(ifma, M_IFMADDR);
3414 bcopy(llsa, dupsa, llsa->sa_len);
3415 ifma->ifma_lladdr = dupsa;
3421 * if_freemulti: free ifmultiaddr structure and possibly attached related
3422 * addresses. The caller is responsible for implementing reference
3423 * counting, notifying the driver, handling routing messages, and releasing
3424 * any dependent link layer state.
3426 #ifdef MCAST_VERBOSE
3427 extern void kdb_backtrace(void);
3430 if_freemulti_internal(struct ifmultiaddr *ifma)
3433 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3434 ifma->ifma_refcount));
3436 if (ifma->ifma_lladdr != NULL)
3437 free(ifma->ifma_lladdr, M_IFMADDR);
3438 #ifdef MCAST_VERBOSE
3440 printf("%s freeing ifma: %p\n", __func__, ifma);
3442 free(ifma->ifma_addr, M_IFMADDR);
3443 free(ifma, M_IFMADDR);
3447 if_destroymulti(epoch_context_t ctx)
3449 struct ifmultiaddr *ifma;
3451 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3452 if_freemulti_internal(ifma);
3456 if_freemulti(struct ifmultiaddr *ifma)
3458 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3459 ifma->ifma_refcount));
3461 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3466 * Register an additional multicast address with a network interface.
3468 * - If the address is already present, bump the reference count on the
3469 * address and return.
3470 * - If the address is not link-layer, look up a link layer address.
3471 * - Allocate address structures for one or both addresses, and attach to the
3472 * multicast address list on the interface. If automatically adding a link
3473 * layer address, the protocol address will own a reference to the link
3474 * layer address, to be freed when it is freed.
3475 * - Notify the network device driver of an addition to the multicast address
3478 * 'sa' points to caller-owned memory with the desired multicast address.
3480 * 'retifma' will be used to return a pointer to the resulting multicast
3481 * address reference, if desired.
3484 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3485 struct ifmultiaddr **retifma)
3487 struct ifmultiaddr *ifma, *ll_ifma;
3488 struct sockaddr *llsa;
3489 struct sockaddr_dl sdl;
3493 IN_MULTI_LIST_UNLOCK_ASSERT();
3496 IN6_MULTI_LIST_UNLOCK_ASSERT();
3499 * If the address is already present, return a new reference to it;
3500 * otherwise, allocate storage and set up a new address.
3503 ifma = if_findmulti(ifp, sa);
3505 ifma->ifma_refcount++;
3506 if (retifma != NULL)
3508 IF_ADDR_WUNLOCK(ifp);
3513 * The address isn't already present; resolve the protocol address
3514 * into a link layer address, and then look that up, bump its
3515 * refcount or allocate an ifma for that also.
3516 * Most link layer resolving functions returns address data which
3517 * fits inside default sockaddr_dl structure. However callback
3518 * can allocate another sockaddr structure, in that case we need to
3523 if (ifp->if_resolvemulti != NULL) {
3524 /* Provide called function with buffer size information */
3525 sdl.sdl_len = sizeof(sdl);
3526 llsa = (struct sockaddr *)&sdl;
3527 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3533 * Allocate the new address. Don't hook it up yet, as we may also
3534 * need to allocate a link layer multicast address.
3536 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3543 * If a link layer address is found, we'll need to see if it's
3544 * already present in the address list, or allocate is as well.
3545 * When this block finishes, the link layer address will be on the
3549 ll_ifma = if_findmulti(ifp, llsa);
3550 if (ll_ifma == NULL) {
3551 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3552 if (ll_ifma == NULL) {
3553 --ifma->ifma_refcount;
3558 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3559 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3562 ll_ifma->ifma_refcount++;
3563 ifma->ifma_llifma = ll_ifma;
3567 * We now have a new multicast address, ifma, and possibly a new or
3568 * referenced link layer address. Add the primary address to the
3569 * ifnet address list.
3571 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3572 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3574 if (retifma != NULL)
3578 * Must generate the message while holding the lock so that 'ifma'
3579 * pointer is still valid.
3581 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3582 IF_ADDR_WUNLOCK(ifp);
3585 * We are certain we have added something, so call down to the
3586 * interface to let them know about it.
3588 if (ifp->if_ioctl != NULL) {
3589 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3592 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3593 link_free_sdl(llsa);
3598 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3599 link_free_sdl(llsa);
3602 IF_ADDR_WUNLOCK(ifp);
3607 * Delete a multicast group membership by network-layer group address.
3609 * Returns ENOENT if the entry could not be found. If ifp no longer
3610 * exists, results are undefined. This entry point should only be used
3611 * from subsystems which do appropriate locking to hold ifp for the
3612 * duration of the call.
3613 * Network-layer protocol domains must use if_delmulti_ifma().
3616 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3618 struct ifmultiaddr *ifma;
3621 struct epoch_tracker et;
3624 NET_EPOCH_ENTER(et);
3625 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3632 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3639 ifma = if_findmulti(ifp, sa);
3641 lastref = if_delmulti_locked(ifp, ifma, 0);
3642 IF_ADDR_WUNLOCK(ifp);
3647 if (lastref && ifp->if_ioctl != NULL) {
3648 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3655 * Delete all multicast group membership for an interface.
3656 * Should be used to quickly flush all multicast filters.
3659 if_delallmulti(struct ifnet *ifp)
3661 struct ifmultiaddr *ifma;
3662 struct ifmultiaddr *next;
3665 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3666 if_delmulti_locked(ifp, ifma, 0);
3667 IF_ADDR_WUNLOCK(ifp);
3671 if_delmulti_ifma(struct ifmultiaddr *ifma)
3673 if_delmulti_ifma_flags(ifma, 0);
3677 * Delete a multicast group membership by group membership pointer.
3678 * Network-layer protocol domains must use this routine.
3680 * It is safe to call this routine if the ifp disappeared.
3683 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3687 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3689 IN_MULTI_LIST_UNLOCK_ASSERT();
3691 ifp = ifma->ifma_ifp;
3694 printf("%s: ifma_ifp seems to be detached\n", __func__);
3696 struct epoch_tracker et;
3699 NET_EPOCH_ENTER(et);
3700 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3709 * If and only if the ifnet instance exists: Acquire the address lock.
3714 lastref = if_delmulti_locked(ifp, ifma, flags);
3718 * If and only if the ifnet instance exists:
3719 * Release the address lock.
3720 * If the group was left: update the hardware hash filter.
3722 IF_ADDR_WUNLOCK(ifp);
3723 if (lastref && ifp->if_ioctl != NULL) {
3724 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3730 * Perform deletion of network-layer and/or link-layer multicast address.
3732 * Return 0 if the reference count was decremented.
3733 * Return 1 if the final reference was released, indicating that the
3734 * hardware hash filter should be reprogrammed.
3737 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3739 struct ifmultiaddr *ll_ifma;
3741 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3742 KASSERT(ifma->ifma_ifp == ifp,
3743 ("%s: inconsistent ifp %p", __func__, ifp));
3744 IF_ADDR_WLOCK_ASSERT(ifp);
3747 ifp = ifma->ifma_ifp;
3748 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3751 * If the ifnet is detaching, null out references to ifnet,
3752 * so that upper protocol layers will notice, and not attempt
3753 * to obtain locks for an ifnet which no longer exists. The
3754 * routing socket announcement must happen before the ifnet
3755 * instance is detached from the system.
3759 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3762 * ifp may already be nulled out if we are being reentered
3763 * to delete the ll_ifma.
3766 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3767 ifma->ifma_ifp = NULL;
3771 if (--ifma->ifma_refcount > 0)
3774 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3775 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3776 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3779 * If this ifma is a network-layer ifma, a link-layer ifma may
3780 * have been associated with it. Release it first if so.
3782 ll_ifma = ifma->ifma_llifma;
3783 if (ll_ifma != NULL) {
3784 KASSERT(ifma->ifma_lladdr != NULL,
3785 ("%s: llifma w/o lladdr", __func__));
3787 ll_ifma->ifma_ifp = NULL; /* XXX */
3788 if (--ll_ifma->ifma_refcount == 0) {
3790 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3791 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3793 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3796 if_freemulti(ll_ifma);
3801 struct ifmultiaddr *ifmatmp;
3803 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3804 MPASS(ifma != ifmatmp);
3809 * The last reference to this instance of struct ifmultiaddr
3810 * was released; the hardware should be notified of this change.
3816 * Set the link layer address on an interface.
3818 * At this time we only support certain types of interfaces,
3819 * and we don't allow the length of the address to change.
3821 * Set noinline to be dtrace-friendly
3824 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3826 struct sockaddr_dl *sdl;
3829 struct epoch_tracker et;
3833 NET_EPOCH_ENTER(et);
3840 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3845 if (len != sdl->sdl_alen) { /* don't allow length to change */
3849 switch (ifp->if_type) {
3854 case IFT_IEEE8023ADLAG:
3855 bcopy(lladdr, LLADDR(sdl), len);
3863 * If the interface is already up, we need
3864 * to re-init it in order to reprogram its
3868 if ((ifp->if_flags & IFF_UP) != 0) {
3869 if (ifp->if_ioctl) {
3870 ifp->if_flags &= ~IFF_UP;
3871 ifr.ifr_flags = ifp->if_flags & 0xffff;
3872 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3873 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3874 ifp->if_flags |= IFF_UP;
3875 ifr.ifr_flags = ifp->if_flags & 0xffff;
3876 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3877 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3880 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3888 * Compat function for handling basic encapsulation requests.
3889 * Not converted stacks (FDDI, IB, ..) supports traditional
3890 * output model: ARP (and other similar L2 protocols) are handled
3891 * inside output routine, arpresolve/nd6_resolve() returns MAC
3892 * address instead of full prepend.
3894 * This function creates calculated header==MAC for IPv4/IPv6 and
3895 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3899 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3902 if (req->rtype != IFENCAP_LL)
3903 return (EOPNOTSUPP);
3905 if (req->bufsize < req->lladdr_len)
3908 switch (req->family) {
3913 return (EAFNOSUPPORT);
3916 /* Copy lladdr to storage as is */
3917 memmove(req->buf, req->lladdr, req->lladdr_len);
3918 req->bufsize = req->lladdr_len;
3919 req->lladdr_off = 0;
3925 * Tunnel interfaces can nest, also they may cause infinite recursion
3926 * calls when misconfigured. We'll prevent this by detecting loops.
3927 * High nesting level may cause stack exhaustion. We'll prevent this
3928 * by introducing upper limit.
3930 * Return 0, if tunnel nesting count is equal or less than limit.
3933 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3941 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3942 if (*(struct ifnet **)(mtag + 1) == ifp) {
3943 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3948 if (count > limit) {
3950 "%s: if_output recursively called too many times(%d)\n",
3951 if_name(ifp), count);
3954 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3957 *(struct ifnet **)(mtag + 1) = ifp;
3958 m_tag_prepend(m, mtag);
3963 * Get the link layer address that was read from the hardware at attach.
3965 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3966 * their component interfaces as IFT_IEEE8023ADLAG.
3969 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3972 if (ifp->if_hw_addr == NULL)
3975 switch (ifp->if_type) {
3977 case IFT_IEEE8023ADLAG:
3978 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3986 * The name argument must be a pointer to storage which will last as
3987 * long as the interface does. For physical devices, the result of
3988 * device_get_name(dev) is a good choice and for pseudo-devices a
3989 * static string works well.
3992 if_initname(struct ifnet *ifp, const char *name, int unit)
3994 ifp->if_dname = name;
3995 ifp->if_dunit = unit;
3996 if (unit != IF_DUNIT_NONE)
3997 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3999 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4003 if_printf(struct ifnet *ifp, const char *fmt, ...)
4008 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4010 vlog(LOG_INFO, if_fmt, ap);
4016 if_start(struct ifnet *ifp)
4019 (*(ifp)->if_start)(ifp);
4023 * Backwards compatibility interface for drivers
4024 * that have not implemented it
4027 if_transmit(struct ifnet *ifp, struct mbuf *m)
4031 IFQ_HANDOFF(ifp, m, error);
4036 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4043 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4048 if (_IF_QFULL(ifq)) {
4050 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4055 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4056 if (m->m_flags & (M_BCAST|M_MCAST))
4057 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4058 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4060 _IF_ENQUEUE(ifq, m);
4062 if (ifp != NULL && !active)
4063 (*(ifp)->if_start)(ifp);
4068 if_register_com_alloc(u_char type,
4069 if_com_alloc_t *a, if_com_free_t *f)
4072 KASSERT(if_com_alloc[type] == NULL,
4073 ("if_register_com_alloc: %d already registered", type));
4074 KASSERT(if_com_free[type] == NULL,
4075 ("if_register_com_alloc: %d free already registered", type));
4077 if_com_alloc[type] = a;
4078 if_com_free[type] = f;
4082 if_deregister_com_alloc(u_char type)
4085 KASSERT(if_com_alloc[type] != NULL,
4086 ("if_deregister_com_alloc: %d not registered", type));
4087 KASSERT(if_com_free[type] != NULL,
4088 ("if_deregister_com_alloc: %d free not registered", type));
4089 if_com_alloc[type] = NULL;
4090 if_com_free[type] = NULL;
4093 /* API for driver access to network stack owned ifnet.*/
4095 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4099 oldbrate = ifp->if_baudrate;
4100 ifp->if_baudrate = baudrate;
4105 if_getbaudrate(if_t ifp)
4108 return (((struct ifnet *)ifp)->if_baudrate);
4112 if_setcapabilities(if_t ifp, int capabilities)
4114 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4119 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4121 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4122 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4128 if_getcapabilities(if_t ifp)
4130 return ((struct ifnet *)ifp)->if_capabilities;
4134 if_setcapenable(if_t ifp, int capabilities)
4136 ((struct ifnet *)ifp)->if_capenable = capabilities;
4141 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4144 ((struct ifnet *)ifp)->if_capenable |= setcap;
4146 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4152 if_getdname(if_t ifp)
4154 return ((struct ifnet *)ifp)->if_dname;
4158 if_togglecapenable(if_t ifp, int togglecap)
4160 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4165 if_getcapenable(if_t ifp)
4167 return ((struct ifnet *)ifp)->if_capenable;
4171 * This is largely undesirable because it ties ifnet to a device, but does
4172 * provide flexiblity for an embedded product vendor. Should be used with
4173 * the understanding that it violates the interface boundaries, and should be
4174 * a last resort only.
4177 if_setdev(if_t ifp, void *dev)
4183 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4185 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4186 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4192 if_getdrvflags(if_t ifp)
4194 return ((struct ifnet *)ifp)->if_drv_flags;
4198 if_setdrvflags(if_t ifp, int flags)
4200 ((struct ifnet *)ifp)->if_drv_flags = flags;
4206 if_setflags(if_t ifp, int flags)
4208 ((struct ifnet *)ifp)->if_flags = flags;
4213 if_setflagbits(if_t ifp, int set, int clear)
4215 ((struct ifnet *)ifp)->if_flags |= set;
4216 ((struct ifnet *)ifp)->if_flags &= ~clear;
4222 if_getflags(if_t ifp)
4224 return ((struct ifnet *)ifp)->if_flags;
4228 if_clearhwassist(if_t ifp)
4230 ((struct ifnet *)ifp)->if_hwassist = 0;
4235 if_sethwassistbits(if_t ifp, int toset, int toclear)
4237 ((struct ifnet *)ifp)->if_hwassist |= toset;
4238 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4244 if_sethwassist(if_t ifp, int hwassist_bit)
4246 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4251 if_gethwassist(if_t ifp)
4253 return ((struct ifnet *)ifp)->if_hwassist;
4257 if_setmtu(if_t ifp, int mtu)
4259 ((struct ifnet *)ifp)->if_mtu = mtu;
4266 return ((struct ifnet *)ifp)->if_mtu;
4270 if_getmtu_family(if_t ifp, int family)
4274 for (dp = domains; dp; dp = dp->dom_next) {
4275 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4276 return (dp->dom_ifmtu((struct ifnet *)ifp));
4279 return (((struct ifnet *)ifp)->if_mtu);
4283 if_setsoftc(if_t ifp, void *softc)
4285 ((struct ifnet *)ifp)->if_softc = softc;
4290 if_getsoftc(if_t ifp)
4292 return ((struct ifnet *)ifp)->if_softc;
4296 if_setrcvif(struct mbuf *m, if_t ifp)
4298 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4302 if_setvtag(struct mbuf *m, uint16_t tag)
4304 m->m_pkthdr.ether_vtag = tag;
4308 if_getvtag(struct mbuf *m)
4311 return (m->m_pkthdr.ether_vtag);
4315 if_sendq_empty(if_t ifp)
4317 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4321 if_getifaddr(if_t ifp)
4323 return ((struct ifnet *)ifp)->if_addr;
4327 if_getamcount(if_t ifp)
4329 return ((struct ifnet *)ifp)->if_amcount;
4334 if_setsendqready(if_t ifp)
4336 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4341 if_setsendqlen(if_t ifp, int tx_desc_count)
4343 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4344 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4350 if_vlantrunkinuse(if_t ifp)
4352 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4356 if_input(if_t ifp, struct mbuf* sendmp)
4358 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4364 #ifndef ETH_ADDR_LEN
4365 #define ETH_ADDR_LEN 6
4369 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4371 struct ifmultiaddr *ifma;
4372 uint8_t *lmta = (uint8_t *)mta;
4375 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4376 if (ifma->ifma_addr->sa_family != AF_LINK)
4382 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4383 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4392 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4396 if_maddr_rlock(ifp);
4397 error = if_setupmultiaddr(ifp, mta, cnt, max);
4398 if_maddr_runlock(ifp);
4403 if_multiaddr_count(if_t ifp, int max)
4405 struct ifmultiaddr *ifma;
4409 if_maddr_rlock(ifp);
4410 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4411 if (ifma->ifma_addr->sa_family != AF_LINK)
4417 if_maddr_runlock(ifp);
4422 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4424 struct ifmultiaddr *ifma;
4427 if_maddr_rlock(ifp);
4428 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4429 cnt += filter(arg, ifma, cnt);
4430 if_maddr_runlock(ifp);
4435 if_dequeue(if_t ifp)
4438 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4444 if_sendq_prepend(if_t ifp, struct mbuf *m)
4446 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4451 if_setifheaderlen(if_t ifp, int len)
4453 ((struct ifnet *)ifp)->if_hdrlen = len;
4458 if_getlladdr(if_t ifp)
4460 return (IF_LLADDR((struct ifnet *)ifp));
4464 if_gethandle(u_char type)
4466 return (if_alloc(type));
4470 if_bpfmtap(if_t ifh, struct mbuf *m)
4472 struct ifnet *ifp = (struct ifnet *)ifh;
4478 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4480 struct ifnet *ifp = (struct ifnet *)ifh;
4482 ETHER_BPF_MTAP(ifp, m);
4486 if_vlancap(if_t ifh)
4488 struct ifnet *ifp = (struct ifnet *)ifh;
4489 VLAN_CAPABILITIES(ifp);
4493 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4496 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4501 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4504 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4509 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4512 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4517 if_gethwtsomax(if_t ifp)
4520 return (((struct ifnet *)ifp)->if_hw_tsomax);
4524 if_gethwtsomaxsegcount(if_t ifp)
4527 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4531 if_gethwtsomaxsegsize(if_t ifp)
4534 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4538 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4540 ((struct ifnet *)ifp)->if_init = init_fn;
4544 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4546 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4550 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4552 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4556 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4558 ((struct ifnet *)ifp)->if_transmit = start_fn;
4561 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4563 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4568 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4571 ifp->if_get_counter = fn;
4574 /* Revisit these - These are inline functions originally. */
4576 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4578 return drbr_inuse(ifh, br);
4582 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4584 return drbr_dequeue(ifh, br);
4588 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4590 return drbr_needs_enqueue(ifh, br);
4594 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4596 return drbr_enqueue(ifh, br, m);