2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
36 #include "opt_inet6.h"
39 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
46 #include <sys/epoch.h>
48 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/rwlock.h>
59 #include <sys/sockio.h>
60 #include <sys/syslog.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/taskqueue.h>
64 #include <sys/domain.h>
68 #include <machine/stdarg.h>
72 #include <net/ethernet.h>
74 #include <net/if_arp.h>
75 #include <net/if_clone.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/if_media.h>
80 #include <net/if_vlan_var.h>
81 #include <net/radix.h>
82 #include <net/route.h>
85 #if defined(INET) || defined(INET6)
86 #include <net/ethernet.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_carp.h>
92 #include <net/debugnet.h>
93 #include <netinet/if_ether.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/in6_ifattach.h>
99 #endif /* INET || INET6 */
101 #include <security/mac/mac_framework.h>
104 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
105 * and ifr_ifru when it is used in SIOCGIFCONF.
107 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
108 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
110 __read_mostly epoch_t net_epoch_preempt;
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 | CTLFLAG_MPSAFE, 0,
197 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
198 "Generic link-management");
200 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
201 &ifqmaxlen, 0, "max send queue size");
203 /* Log link state change events */
204 static int log_link_state_change = 1;
206 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
207 &log_link_state_change, 0,
208 "log interface link state change events");
210 /* Log promiscuous mode change events */
211 static int log_promisc_mode_change = 1;
213 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
214 &log_promisc_mode_change, 1,
215 "log promiscuous mode change events");
217 /* Interface description */
218 static unsigned int ifdescr_maxlen = 1024;
219 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
221 "administrative maximum length for interface description");
223 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
225 /* global sx for non-critical path ifdescr */
226 static struct sx ifdescr_sx;
227 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
229 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
230 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
231 /* These are external hooks for CARP. */
232 void (*carp_linkstate_p)(struct ifnet *ifp);
233 void (*carp_demote_adj_p)(int, char *);
234 int (*carp_master_p)(struct ifaddr *);
235 #if defined(INET) || defined(INET6)
236 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
237 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
238 const struct sockaddr *sa);
239 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
240 int (*carp_attach_p)(struct ifaddr *, int);
241 void (*carp_detach_p)(struct ifaddr *, bool);
244 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
247 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
248 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
249 const struct in6_addr *taddr);
252 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
255 * XXX: Style; these should be sorted alphabetically, and unprototyped
256 * static functions should be prototyped. Currently they are sorted by
259 static void if_attachdomain(void *);
260 static void if_attachdomain1(struct ifnet *);
261 static int ifconf(u_long, caddr_t);
262 static void *if_grow(void);
263 static void if_input_default(struct ifnet *, struct mbuf *);
264 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
265 static void if_route(struct ifnet *, int flag, int fam);
266 static int if_setflag(struct ifnet *, int, int, int *, int);
267 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
268 static void if_unroute(struct ifnet *, int flag, int fam);
269 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
270 static void do_link_state_change(void *, int);
271 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
272 static int if_getgroupmembers(struct ifgroupreq *);
273 static void if_delgroups(struct ifnet *);
274 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
275 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
276 static void if_siocaddmulti(void *, int);
278 static int if_vmove(struct ifnet *, struct vnet *);
283 * XXX: declare here to avoid to include many inet6 related files..
284 * should be more generalized?
286 extern void nd6_setmtu(struct ifnet *);
289 /* ipsec helper hooks */
290 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
291 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
293 VNET_DEFINE(int, if_index);
294 int ifqmaxlen = IFQ_MAXLEN;
295 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
296 VNET_DEFINE(struct ifgrouphead, ifg_head);
298 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
300 /* Table of ifnet by index. */
301 VNET_DEFINE(struct ifnet **, ifindex_table);
303 #define V_if_indexlim VNET(if_indexlim)
304 #define V_ifindex_table VNET(ifindex_table)
307 * The global network interface list (V_ifnet) and related state (such as
308 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
309 * an rwlock. Either may be acquired shared to stablize the list, but both
310 * must be acquired writable to modify the list. This model allows us to
311 * both stablize the interface list during interrupt thread processing, but
312 * also to stablize it over long-running ioctls, without introducing priority
313 * inversions and deadlocks.
315 struct rwlock ifnet_rwlock;
316 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
317 struct sx ifnet_sxlock;
318 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
321 * The allocation of network interfaces is a rather non-atomic affair; we
322 * need to select an index before we are ready to expose the interface for
323 * use, so will use this pointer value to indicate reservation.
325 #define IFNET_HOLD (void *)(uintptr_t)(-1)
328 #define VNET_IS_SHUTTING_DOWN(_vnet) \
329 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
332 static if_com_alloc_t *if_com_alloc[256];
333 static if_com_free_t *if_com_free[256];
335 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
336 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
337 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
340 ifnet_byindex(u_short idx)
344 if (__predict_false(idx > V_if_index))
347 ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx);
348 return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp);
352 ifnet_byindex_ref(u_short idx)
358 ifp = ifnet_byindex(idx);
359 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
366 * Allocate an ifindex array entry; return 0 on success or an error on
370 ifindex_alloc(void **old)
374 IFNET_WLOCK_ASSERT();
376 * Try to find an empty slot below V_if_index. If we fail, take the
379 for (idx = 1; idx <= V_if_index; idx++) {
380 if (V_ifindex_table[idx] == NULL)
384 /* Catch if_index overflow. */
385 if (idx >= V_if_indexlim) {
389 if (idx > V_if_index)
395 ifindex_free_locked(u_short idx)
398 IFNET_WLOCK_ASSERT();
400 V_ifindex_table[idx] = NULL;
401 while (V_if_index > 0 &&
402 V_ifindex_table[V_if_index] == NULL)
407 ifindex_free(u_short idx)
411 ifindex_free_locked(idx);
416 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
419 V_ifindex_table[idx] = ifp;
423 ifaddr_byindex(u_short idx)
426 struct ifaddr *ifa = NULL;
430 ifp = ifnet_byindex(idx);
431 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
437 * Network interface utility routines.
439 * Routines with ifa_ifwith* names take sockaddr *'s as
444 vnet_if_init(const void *unused __unused)
448 CK_STAILQ_INIT(&V_ifnet);
449 CK_STAILQ_INIT(&V_ifg_head);
451 old = if_grow(); /* create initial table */
453 epoch_wait_preempt(net_epoch_preempt);
455 vnet_if_clone_init();
457 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
462 vnet_if_uninit(const void *unused __unused)
465 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
466 "not empty", __func__, __LINE__, &V_ifnet));
467 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
468 "not empty", __func__, __LINE__, &V_ifg_head));
470 free((caddr_t)V_ifindex_table, M_IFNET);
472 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
473 vnet_if_uninit, NULL);
476 vnet_if_return(const void *unused __unused)
478 struct ifnet *ifp, *nifp;
480 /* Return all inherited interfaces to their parent vnets. */
481 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
482 if (ifp->if_home_vnet != ifp->if_vnet)
483 if_vmove(ifp, ifp->if_home_vnet);
486 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
487 vnet_if_return, NULL);
500 IFNET_WLOCK_ASSERT();
501 oldlim = V_if_indexlim;
503 n = (oldlim << 1) * sizeof(*e);
504 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
506 if (V_if_indexlim != oldlim) {
510 if (V_ifindex_table != NULL) {
511 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
512 old = V_ifindex_table;
520 * Allocate a struct ifnet and an index for an interface. A layer 2
521 * common structure will also be allocated if an allocation routine is
522 * registered for the passed type.
525 if_alloc_domain(u_char type, int numa_domain)
531 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
532 if (numa_domain == IF_NODOM)
533 ifp = malloc(sizeof(struct ifnet), M_IFNET,
536 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
537 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
540 idx = ifindex_alloc(&old);
541 if (__predict_false(idx == USHRT_MAX)) {
543 epoch_wait_preempt(net_epoch_preempt);
547 ifnet_setbyindex(idx, IFNET_HOLD);
551 ifp->if_alloctype = type;
552 ifp->if_numa_domain = numa_domain;
554 ifp->if_vnet = curvnet;
556 if (if_com_alloc[type] != NULL) {
557 ifp->if_l2com = if_com_alloc[type](type, ifp);
558 if (ifp->if_l2com == NULL) {
565 IF_ADDR_LOCK_INIT(ifp);
566 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
567 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
568 ifp->if_afdata_initialized = 0;
569 IF_AFDATA_LOCK_INIT(ifp);
570 CK_STAILQ_INIT(&ifp->if_addrhead);
571 CK_STAILQ_INIT(&ifp->if_multiaddrs);
572 CK_STAILQ_INIT(&ifp->if_groups);
576 ifq_init(&ifp->if_snd, ifp);
578 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
579 for (int i = 0; i < IFCOUNTERS; i++)
580 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
581 ifp->if_get_counter = if_get_counter_default;
582 ifp->if_pcp = IFNET_PCP_NONE;
583 ifnet_setbyindex(ifp->if_index, ifp);
588 if_alloc_dev(u_char type, device_t dev)
592 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
593 return (if_alloc_domain(type, IF_NODOM));
594 return (if_alloc_domain(type, numa_domain));
598 if_alloc(u_char type)
601 return (if_alloc_domain(type, IF_NODOM));
604 * Do the actual work of freeing a struct ifnet, and layer 2 common
605 * structure. This call is made when the last reference to an
606 * interface is released.
609 if_free_internal(struct ifnet *ifp)
612 KASSERT((ifp->if_flags & IFF_DYING),
613 ("if_free_internal: interface not dying"));
615 if (if_com_free[ifp->if_alloctype] != NULL)
616 if_com_free[ifp->if_alloctype](ifp->if_l2com,
620 mac_ifnet_destroy(ifp);
622 IF_AFDATA_DESTROY(ifp);
623 IF_ADDR_LOCK_DESTROY(ifp);
624 ifq_delete(&ifp->if_snd);
626 for (int i = 0; i < IFCOUNTERS; i++)
627 counter_u64_free(ifp->if_counters[i]);
629 free(ifp->if_description, M_IFDESCR);
630 free(ifp->if_hw_addr, M_IFADDR);
631 if (ifp->if_numa_domain == IF_NODOM)
634 free_domain(ifp, M_IFNET);
638 if_destroy(epoch_context_t ctx)
642 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
643 if_free_internal(ifp);
647 * Deregister an interface and free the associated storage.
650 if_free(struct ifnet *ifp)
653 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
655 CURVNET_SET_QUIET(ifp->if_vnet);
657 KASSERT(ifp == ifnet_byindex(ifp->if_index),
658 ("%s: freeing unallocated ifnet", ifp->if_xname));
660 ifindex_free_locked(ifp->if_index);
663 if (refcount_release(&ifp->if_refcount))
664 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
669 * Interfaces to keep an ifnet type-stable despite the possibility of the
670 * driver calling if_free(). If there are additional references, we defer
671 * freeing the underlying data structure.
674 if_ref(struct ifnet *ifp)
677 /* We don't assert the ifnet list lock here, but arguably should. */
678 refcount_acquire(&ifp->if_refcount);
682 if_rele(struct ifnet *ifp)
685 if (!refcount_release(&ifp->if_refcount))
687 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
691 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
694 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
696 if (ifq->ifq_maxlen == 0)
697 ifq->ifq_maxlen = ifqmaxlen;
700 ifq->altq_disc = NULL;
701 ifq->altq_flags &= ALTQF_CANTCHANGE;
702 ifq->altq_tbr = NULL;
707 ifq_delete(struct ifaltq *ifq)
709 mtx_destroy(&ifq->ifq_mtx);
713 * Perform generic interface initialization tasks and attach the interface
714 * to the list of "active" interfaces. If vmove flag is set on entry
715 * to if_attach_internal(), perform only a limited subset of initialization
716 * tasks, given that we are moving from one vnet to another an ifnet which
717 * has already been fully initialized.
719 * Note that if_detach_internal() removes group membership unconditionally
720 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
721 * Thus, when if_vmove() is applied to a cloned interface, group membership
722 * is lost while a cloned one always joins a group whose name is
723 * ifc->ifc_name. To recover this after if_detach_internal() and
724 * if_attach_internal(), the cloner should be specified to
725 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
726 * attempts to join a group whose name is ifc->ifc_name.
729 * - The decision to return void and thus require this function to
730 * succeed is questionable.
731 * - We should probably do more sanity checking. For instance we don't
732 * do anything to insure if_xname is unique or non-empty.
735 if_attach(struct ifnet *ifp)
738 if_attach_internal(ifp, 0, NULL);
742 * Compute the least common TSO limit.
745 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
748 * 1) If there is no limit currently, take the limit from
749 * the network adapter.
751 * 2) If the network adapter has a limit below the current
754 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
755 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
756 pmax->tsomaxbytes = ifp->if_hw_tsomax;
758 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
759 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
760 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
762 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
763 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
764 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
769 * Update TSO limit of a network adapter.
771 * Returns zero if no change. Else non-zero.
774 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
777 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
778 ifp->if_hw_tsomax = pmax->tsomaxbytes;
781 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
782 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
785 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
786 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
793 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
795 unsigned socksize, ifasize;
796 int namelen, masklen;
797 struct sockaddr_dl *sdl;
800 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
801 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
805 ifp->if_vnet = curvnet;
806 if (ifp->if_home_vnet == NULL)
807 ifp->if_home_vnet = curvnet;
810 if_addgroup(ifp, IFG_ALL);
812 /* Restore group membership for cloned interfaces. */
813 if (vmove && ifc != NULL)
814 if_clone_addgroup(ifp, ifc);
816 getmicrotime(&ifp->if_lastchange);
817 ifp->if_epoch = time_uptime;
819 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
820 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
821 ("transmit and qflush must both either be set or both be NULL"));
822 if (ifp->if_transmit == NULL) {
823 ifp->if_transmit = if_transmit;
824 ifp->if_qflush = if_qflush;
826 if (ifp->if_input == NULL)
827 ifp->if_input = if_input_default;
829 if (ifp->if_requestencap == NULL)
830 ifp->if_requestencap = if_requestencap_default;
834 mac_ifnet_create(ifp);
838 * Create a Link Level name for this device.
840 namelen = strlen(ifp->if_xname);
842 * Always save enough space for any possiable name so we
843 * can do a rename in place later.
845 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
846 socksize = masklen + ifp->if_addrlen;
847 if (socksize < sizeof(*sdl))
848 socksize = sizeof(*sdl);
849 socksize = roundup2(socksize, sizeof(long));
850 ifasize = sizeof(*ifa) + 2 * socksize;
851 ifa = ifa_alloc(ifasize, M_WAITOK);
852 sdl = (struct sockaddr_dl *)(ifa + 1);
853 sdl->sdl_len = socksize;
854 sdl->sdl_family = AF_LINK;
855 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
856 sdl->sdl_nlen = namelen;
857 sdl->sdl_index = ifp->if_index;
858 sdl->sdl_type = ifp->if_type;
861 ifa->ifa_addr = (struct sockaddr *)sdl;
862 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
863 ifa->ifa_netmask = (struct sockaddr *)sdl;
864 sdl->sdl_len = masklen;
866 sdl->sdl_data[--namelen] = 0xff;
867 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
868 /* Reliably crash if used uninitialized. */
869 ifp->if_broadcastaddr = NULL;
871 if (ifp->if_type == IFT_ETHER) {
872 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
876 #if defined(INET) || defined(INET6)
877 /* Use defaults for TSO, if nothing is set */
878 if (ifp->if_hw_tsomax == 0 &&
879 ifp->if_hw_tsomaxsegcount == 0 &&
880 ifp->if_hw_tsomaxsegsize == 0) {
882 * The TSO defaults needs to be such that an
883 * NFS mbuf list of 35 mbufs totalling just
884 * below 64K works and that a chain of mbufs
885 * can be defragged into at most 32 segments:
887 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
888 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
889 ifp->if_hw_tsomaxsegcount = 35;
890 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
892 /* XXX some drivers set IFCAP_TSO after ethernet attach */
893 if (ifp->if_capabilities & IFCAP_TSO) {
894 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
896 ifp->if_hw_tsomaxsegcount,
897 ifp->if_hw_tsomaxsegsize);
905 * Update the interface index in the link layer address
908 for (ifa = ifp->if_addr; ifa != NULL;
909 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
910 if (ifa->ifa_addr->sa_family == AF_LINK) {
911 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
912 sdl->sdl_index = ifp->if_index;
919 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
921 curvnet->vnet_ifcnt++;
925 if (domain_init_status >= 2)
926 if_attachdomain1(ifp);
928 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
929 if (IS_DEFAULT_VNET(curvnet))
930 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
932 /* Announce the interface. */
933 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
937 if_epochalloc(void *dummy __unused)
940 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
942 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
945 if_attachdomain(void *dummy)
949 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
950 if_attachdomain1(ifp);
952 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
953 if_attachdomain, NULL);
956 if_attachdomain1(struct ifnet *ifp)
961 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
962 * cannot lock ifp->if_afdata initialization, entirely.
965 if (ifp->if_afdata_initialized >= domain_init_status) {
966 IF_AFDATA_UNLOCK(ifp);
967 log(LOG_WARNING, "%s called more than once on %s\n",
968 __func__, ifp->if_xname);
971 ifp->if_afdata_initialized = domain_init_status;
972 IF_AFDATA_UNLOCK(ifp);
974 /* address family dependent data region */
975 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
976 for (dp = domains; dp; dp = dp->dom_next) {
977 if (dp->dom_ifattach)
978 ifp->if_afdata[dp->dom_family] =
979 (*dp->dom_ifattach)(ifp);
984 * Remove any unicast or broadcast network addresses from an interface.
987 if_purgeaddrs(struct ifnet *ifp)
992 struct epoch_tracker et;
995 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
996 if (ifa->ifa_addr->sa_family != AF_LINK)
1004 /* XXX: Ugly!! ad hoc just for INET */
1005 if (ifa->ifa_addr->sa_family == AF_INET) {
1006 struct ifaliasreq ifr;
1008 bzero(&ifr, sizeof(ifr));
1009 ifr.ifra_addr = *ifa->ifa_addr;
1010 if (ifa->ifa_dstaddr)
1011 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1012 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1018 if (ifa->ifa_addr->sa_family == AF_INET6) {
1020 /* ifp_addrhead is already updated */
1025 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1026 IF_ADDR_WUNLOCK(ifp);
1032 * Remove any multicast network addresses from an interface when an ifnet
1036 if_purgemaddrs(struct ifnet *ifp)
1038 struct ifmultiaddr *ifma;
1041 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1042 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1043 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1044 if_delmulti_locked(ifp, ifma, 1);
1046 IF_ADDR_WUNLOCK(ifp);
1050 * Detach an interface, removing it from the list of "active" interfaces.
1051 * If vmove flag is set on entry to if_detach_internal(), perform only a
1052 * limited subset of cleanup tasks, given that we are moving an ifnet from
1053 * one vnet to another, where it must be fully operational.
1055 * XXXRW: There are some significant questions about event ordering, and
1056 * how to prevent things from starting to use the interface during detach.
1059 if_detach(struct ifnet *ifp)
1062 CURVNET_SET_QUIET(ifp->if_vnet);
1063 if_detach_internal(ifp, 0, NULL);
1068 * The vmove flag, if set, indicates that we are called from a callpath
1069 * that is moving an interface to a different vnet instance.
1071 * The shutdown flag, if set, indicates that we are called in the
1072 * process of shutting down a vnet instance. Currently only the
1073 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1074 * on a vnet instance shutdown without this flag being set, e.g., when
1075 * the cloned interfaces are destoyed as first thing of teardown.
1078 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1088 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1091 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1093 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1095 ifp->if_flags |= IFF_DYING;
1102 * While we would want to panic here, we cannot
1103 * guarantee that the interface is indeed still on
1104 * the list given we don't hold locks all the way.
1109 panic("%s: ifp=%p not on the ifnet tailq %p",
1110 __func__, ifp, &V_ifnet);
1112 return; /* XXX this should panic as well? */
1117 * At this point we know the interface still was on the ifnet list
1118 * and we removed it so we are in a stable state.
1121 curvnet->vnet_ifcnt--;
1123 epoch_wait_preempt(net_epoch_preempt);
1126 * Ensure all pending EPOCH(9) callbacks have been executed. This
1127 * fixes issues about late destruction of multicast options
1128 * which lead to leave group calls, which in turn access the
1129 * belonging ifnet structure:
1131 epoch_drain_callbacks(net_epoch_preempt);
1134 * In any case (destroy or vmove) detach us from the groups
1135 * and remove/wait for pending events on the taskq.
1136 * XXX-BZ in theory an interface could still enqueue a taskq change?
1140 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1141 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1144 * Check if this is a cloned interface or not. Must do even if
1145 * shutting down as a if_vmove_reclaim() would move the ifp and
1146 * the if_clone_addgroup() will have a corrupted string overwise
1147 * from a gibberish pointer.
1149 if (vmove && ifcp != NULL)
1150 *ifcp = if_clone_findifc(ifp);
1156 * On VNET shutdown abort here as the stack teardown will do all
1157 * the work top-down for us.
1160 /* Give interface users the chance to clean up. */
1161 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1164 * In case of a vmove we are done here without error.
1165 * If we would signal an error it would lead to the same
1166 * abort as if we did not find the ifnet anymore.
1167 * if_detach() calls us in void context and does not care
1168 * about an early abort notification, so life is splendid :)
1170 goto finish_vnet_shutdown;
1175 * At this point we are not tearing down a VNET and are either
1176 * going to destroy or vmove the interface and have to cleanup
1181 * Remove routes and flush queues.
1184 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1185 altq_disable(&ifp->if_snd);
1186 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1187 altq_detach(&ifp->if_snd);
1198 * Remove all IPv6 kernel structs related to ifp. This should be done
1199 * before removing routing entries below, since IPv6 interface direct
1200 * routes are expected to be removed by the IPv6-specific kernel API.
1201 * Otherwise, the kernel will detect some inconsistency and bark it.
1205 if_purgemaddrs(ifp);
1207 /* Announce that the interface is gone. */
1208 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1209 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1210 if (IS_DEFAULT_VNET(curvnet))
1211 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1215 * Prevent further calls into the device driver via ifnet.
1220 * Clean up all addresses.
1223 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1224 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1225 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1226 IF_ADDR_WUNLOCK(ifp);
1229 IF_ADDR_WUNLOCK(ifp);
1232 rt_flushifroutes(ifp);
1235 finish_vnet_shutdown:
1238 * We cannot hold the lock over dom_ifdetach calls as they might
1239 * sleep, for example trying to drain a callout, thus open up the
1240 * theoretical race with re-attaching.
1242 IF_AFDATA_LOCK(ifp);
1243 i = ifp->if_afdata_initialized;
1244 ifp->if_afdata_initialized = 0;
1245 IF_AFDATA_UNLOCK(ifp);
1246 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1247 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1248 (*dp->dom_ifdetach)(ifp,
1249 ifp->if_afdata[dp->dom_family]);
1250 ifp->if_afdata[dp->dom_family] = NULL;
1259 * if_vmove() performs a limited version of if_detach() in current
1260 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1261 * An attempt is made to shrink if_index in current vnet, find an
1262 * unused if_index in target vnet and calls if_grow() if necessary,
1263 * and finally find an unused if_xname for the target vnet.
1266 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1268 struct if_clone *ifc;
1270 u_int bif_dlt, bif_hdrlen;
1277 * if_detach_internal() will call the eventhandler to notify
1278 * interface departure. That will detach if_bpf. We need to
1279 * safe the dlt and hdrlen so we can re-attach it later.
1281 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1285 * Detach from current vnet, but preserve LLADDR info, do not
1286 * mark as dead etc. so that the ifnet can be reattached later.
1287 * If we cannot find it, we lost the race to someone else.
1289 rc = if_detach_internal(ifp, 1, &ifc);
1294 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1295 * the if_index for that vnet if possible.
1297 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1298 * or we'd lock on one vnet and unlock on another.
1301 ifindex_free_locked(ifp->if_index);
1305 * Perform interface-specific reassignment tasks, if provided by
1308 if (ifp->if_reassign != NULL)
1309 ifp->if_reassign(ifp, new_vnet, NULL);
1312 * Switch to the context of the target vnet.
1314 CURVNET_SET_QUIET(new_vnet);
1317 ifp->if_index = ifindex_alloc(&old);
1318 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1320 epoch_wait_preempt(net_epoch_preempt);
1324 ifnet_setbyindex(ifp->if_index, ifp);
1327 if_attach_internal(ifp, 1, ifc);
1330 if (ifp->if_bpf == NULL)
1331 bpfattach(ifp, bif_dlt, bif_hdrlen);
1339 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1342 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1349 /* Try to find the prison within our visibility. */
1350 sx_slock(&allprison_lock);
1351 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1352 sx_sunlock(&allprison_lock);
1355 prison_hold_locked(pr);
1356 mtx_unlock(&pr->pr_mtx);
1358 /* Do not try to move the iface from and to the same prison. */
1359 if (pr->pr_vnet == ifp->if_vnet) {
1364 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1365 /* XXX Lock interfaces to avoid races. */
1366 CURVNET_SET_QUIET(pr->pr_vnet);
1367 difp = ifunit(ifname);
1374 /* Make sure the VNET is stable. */
1375 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1383 /* Move the interface into the child jail/vnet. */
1384 error = if_vmove(ifp, pr->pr_vnet);
1386 /* Report the new if_xname back to the userland on success. */
1388 sprintf(ifname, "%s", ifp->if_xname);
1395 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1398 struct vnet *vnet_dst;
1403 /* Try to find the prison within our visibility. */
1404 sx_slock(&allprison_lock);
1405 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1406 sx_sunlock(&allprison_lock);
1409 prison_hold_locked(pr);
1410 mtx_unlock(&pr->pr_mtx);
1412 /* Make sure the named iface exists in the source prison/vnet. */
1413 CURVNET_SET(pr->pr_vnet);
1414 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1421 /* Do not try to move the iface from and to the same prison. */
1422 vnet_dst = TD_TO_VNET(td);
1423 if (vnet_dst == ifp->if_vnet) {
1429 /* Make sure the VNET is stable. */
1430 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1437 /* Get interface back from child jail/vnet. */
1438 error = if_vmove(ifp, vnet_dst);
1441 /* Report the new if_xname back to the userland on success. */
1443 sprintf(ifname, "%s", ifp->if_xname);
1451 * Add a group to an interface
1454 if_addgroup(struct ifnet *ifp, const char *groupname)
1456 struct ifg_list *ifgl;
1457 struct ifg_group *ifg = NULL;
1458 struct ifg_member *ifgm;
1461 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1462 groupname[strlen(groupname) - 1] <= '9')
1466 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1467 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1472 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1477 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1483 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1484 if (!strcmp(ifg->ifg_group, groupname))
1488 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1494 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1495 ifg->ifg_refcnt = 0;
1496 CK_STAILQ_INIT(&ifg->ifg_members);
1497 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1502 ifgl->ifgl_group = ifg;
1503 ifgm->ifgm_ifp = ifp;
1506 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1507 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1508 IF_ADDR_WUNLOCK(ifp);
1513 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1514 EVENTHANDLER_INVOKE(group_change_event, groupname);
1520 * Helper function to remove a group out of an interface. Expects the global
1521 * ifnet lock to be write-locked, and drops it before returning.
1524 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1525 const char *groupname)
1527 struct ifg_member *ifgm;
1530 IFNET_WLOCK_ASSERT();
1533 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1534 IF_ADDR_WUNLOCK(ifp);
1536 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1537 if (ifgm->ifgm_ifp == ifp) {
1538 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1539 ifg_member, ifgm_next);
1544 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1545 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1553 epoch_wait_preempt(net_epoch_preempt);
1555 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1556 free(ifgl->ifgl_group, M_TEMP);
1561 EVENTHANDLER_INVOKE(group_change_event, groupname);
1565 * Remove a group from an interface
1568 if_delgroup(struct ifnet *ifp, const char *groupname)
1570 struct ifg_list *ifgl;
1573 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1574 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1581 _if_delgroup_locked(ifp, ifgl, groupname);
1587 * Remove an interface from all groups
1590 if_delgroups(struct ifnet *ifp)
1592 struct ifg_list *ifgl;
1593 char groupname[IFNAMSIZ];
1596 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1597 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1598 _if_delgroup_locked(ifp, ifgl, groupname);
1605 ifgr_group_get(void *ifgrp)
1607 union ifgroupreq_union *ifgrup;
1610 #ifdef COMPAT_FREEBSD32
1611 if (SV_CURPROC_FLAG(SV_ILP32))
1612 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1614 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1617 static struct ifg_req *
1618 ifgr_groups_get(void *ifgrp)
1620 union ifgroupreq_union *ifgrup;
1623 #ifdef COMPAT_FREEBSD32
1624 if (SV_CURPROC_FLAG(SV_ILP32))
1625 return ((struct ifg_req *)(uintptr_t)
1626 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1628 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1632 * Stores all groups from an interface in memory pointed to by ifgr.
1635 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1638 struct ifg_list *ifgl;
1639 struct ifg_req ifgrq, *ifgp;
1643 if (ifgr->ifgr_len == 0) {
1644 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1645 ifgr->ifgr_len += sizeof(struct ifg_req);
1649 len = ifgr->ifgr_len;
1650 ifgp = ifgr_groups_get(ifgr);
1652 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1653 if (len < sizeof(ifgrq))
1655 bzero(&ifgrq, sizeof ifgrq);
1656 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1657 sizeof(ifgrq.ifgrq_group));
1658 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1660 len -= sizeof(ifgrq);
1668 * Stores all members of a group in memory pointed to by igfr
1671 if_getgroupmembers(struct ifgroupreq *ifgr)
1673 struct ifg_group *ifg;
1674 struct ifg_member *ifgm;
1675 struct ifg_req ifgrq, *ifgp;
1679 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1680 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1687 if (ifgr->ifgr_len == 0) {
1688 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1689 ifgr->ifgr_len += sizeof(ifgrq);
1694 len = ifgr->ifgr_len;
1695 ifgp = ifgr_groups_get(ifgr);
1696 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1697 if (len < sizeof(ifgrq)) {
1701 bzero(&ifgrq, sizeof ifgrq);
1702 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1703 sizeof(ifgrq.ifgrq_member));
1704 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1708 len -= sizeof(ifgrq);
1717 * Return counter values from counter(9)s stored in ifnet.
1720 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1723 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1725 return (counter_u64_fetch(ifp->if_counters[cnt]));
1729 * Increase an ifnet counter. Usually used for counters shared
1730 * between the stack and a driver, but function supports them all.
1733 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1736 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1738 counter_u64_add(ifp->if_counters[cnt], inc);
1742 * Copy data from ifnet to userland API structure if_data.
1745 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1748 ifd->ifi_type = ifp->if_type;
1749 ifd->ifi_physical = 0;
1750 ifd->ifi_addrlen = ifp->if_addrlen;
1751 ifd->ifi_hdrlen = ifp->if_hdrlen;
1752 ifd->ifi_link_state = ifp->if_link_state;
1754 ifd->ifi_datalen = sizeof(struct if_data);
1755 ifd->ifi_mtu = ifp->if_mtu;
1756 ifd->ifi_metric = ifp->if_metric;
1757 ifd->ifi_baudrate = ifp->if_baudrate;
1758 ifd->ifi_hwassist = ifp->if_hwassist;
1759 ifd->ifi_epoch = ifp->if_epoch;
1760 ifd->ifi_lastchange = ifp->if_lastchange;
1762 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1763 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1764 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1765 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1766 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1767 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1768 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1769 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1770 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1771 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1772 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1773 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1777 * Initialization, destruction and refcounting functions for ifaddrs.
1780 ifa_alloc(size_t size, int flags)
1784 KASSERT(size >= sizeof(struct ifaddr),
1785 ("%s: invalid size %zu", __func__, size));
1787 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1791 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1793 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1795 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1797 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1800 refcount_init(&ifa->ifa_refcnt, 1);
1805 /* free(NULL) is okay */
1806 counter_u64_free(ifa->ifa_opackets);
1807 counter_u64_free(ifa->ifa_ipackets);
1808 counter_u64_free(ifa->ifa_obytes);
1809 counter_u64_free(ifa->ifa_ibytes);
1810 free(ifa, M_IFADDR);
1816 ifa_ref(struct ifaddr *ifa)
1819 refcount_acquire(&ifa->ifa_refcnt);
1823 ifa_destroy(epoch_context_t ctx)
1827 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1828 counter_u64_free(ifa->ifa_opackets);
1829 counter_u64_free(ifa->ifa_ipackets);
1830 counter_u64_free(ifa->ifa_obytes);
1831 counter_u64_free(ifa->ifa_ibytes);
1832 free(ifa, M_IFADDR);
1836 ifa_free(struct ifaddr *ifa)
1839 if (refcount_release(&ifa->ifa_refcnt))
1840 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1845 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1846 struct sockaddr *ia)
1848 struct epoch_tracker et;
1850 struct rt_addrinfo info;
1851 struct sockaddr_dl null_sdl;
1853 struct ifaddr *rti_ifa = NULL;
1857 NET_EPOCH_ENTER(et);
1858 bzero(&info, sizeof(info));
1859 if (cmd != RTM_DELETE)
1860 info.rti_ifp = V_loif;
1861 if (cmd == RTM_ADD) {
1862 /* explicitly specify (loopback) ifa */
1863 if (info.rti_ifp != NULL) {
1864 rti_ifa = ifaof_ifpforaddr(ifa->ifa_addr, info.rti_ifp);
1865 if (rti_ifa != NULL)
1867 info.rti_ifa = rti_ifa;
1870 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1871 info.rti_info[RTAX_DST] = ia;
1872 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1873 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1875 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1878 if (rti_ifa != NULL)
1882 (cmd == RTM_ADD && error == EEXIST) ||
1883 (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
1886 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1887 __func__, otype, if_name(ifp), error);
1893 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1896 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1900 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1903 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1907 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1910 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1914 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1915 * structs used to represent other address families, it is necessary
1916 * to perform a different comparison.
1919 #define sa_dl_equal(a1, a2) \
1920 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1921 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1922 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1923 CLLADDR((const struct sockaddr_dl *)(a2)), \
1924 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1927 * Locate an interface based on a complete address.
1931 ifa_ifwithaddr(const struct sockaddr *addr)
1938 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1939 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1940 if (ifa->ifa_addr->sa_family != addr->sa_family)
1942 if (sa_equal(addr, ifa->ifa_addr)) {
1945 /* IP6 doesn't have broadcast */
1946 if ((ifp->if_flags & IFF_BROADCAST) &&
1947 ifa->ifa_broadaddr &&
1948 ifa->ifa_broadaddr->sa_len != 0 &&
1949 sa_equal(ifa->ifa_broadaddr, addr)) {
1960 ifa_ifwithaddr_check(const struct sockaddr *addr)
1962 struct epoch_tracker et;
1965 NET_EPOCH_ENTER(et);
1966 rc = (ifa_ifwithaddr(addr) != NULL);
1972 * Locate an interface based on the broadcast address.
1976 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1982 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1983 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1985 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1986 if (ifa->ifa_addr->sa_family != addr->sa_family)
1988 if ((ifp->if_flags & IFF_BROADCAST) &&
1989 ifa->ifa_broadaddr &&
1990 ifa->ifa_broadaddr->sa_len != 0 &&
1991 sa_equal(ifa->ifa_broadaddr, addr)) {
2002 * Locate the point to point interface with a given destination address.
2006 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2012 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2013 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2015 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2017 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2018 if (ifa->ifa_addr->sa_family != addr->sa_family)
2020 if (ifa->ifa_dstaddr != NULL &&
2021 sa_equal(addr, ifa->ifa_dstaddr)) {
2032 * Find an interface on a specific network. If many, choice
2033 * is most specific found.
2036 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2040 struct ifaddr *ifa_maybe = NULL;
2041 u_int af = addr->sa_family;
2042 const char *addr_data = addr->sa_data, *cplim;
2046 * AF_LINK addresses can be looked up directly by their index number,
2047 * so do that if we can.
2049 if (af == AF_LINK) {
2050 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2051 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2052 return (ifaddr_byindex(sdl->sdl_index));
2056 * Scan though each interface, looking for ones that have addresses
2057 * in this address family and the requested fib.
2059 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2060 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2062 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2063 const char *cp, *cp2, *cp3;
2065 if (ifa->ifa_addr->sa_family != af)
2067 if (af == AF_INET &&
2068 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2070 * This is a bit broken as it doesn't
2071 * take into account that the remote end may
2072 * be a single node in the network we are
2074 * The trouble is that we don't know the
2075 * netmask for the remote end.
2077 if (ifa->ifa_dstaddr != NULL &&
2078 sa_equal(addr, ifa->ifa_dstaddr)) {
2083 * Scan all the bits in the ifa's address.
2084 * If a bit dissagrees with what we are
2085 * looking for, mask it with the netmask
2086 * to see if it really matters.
2087 * (A byte at a time)
2089 if (ifa->ifa_netmask == 0)
2092 cp2 = ifa->ifa_addr->sa_data;
2093 cp3 = ifa->ifa_netmask->sa_data;
2094 cplim = ifa->ifa_netmask->sa_len
2095 + (char *)ifa->ifa_netmask;
2097 if ((*cp++ ^ *cp2++) & *cp3++)
2098 goto next; /* next address! */
2100 * If the netmask of what we just found
2101 * is more specific than what we had before
2102 * (if we had one), or if the virtual status
2103 * of new prefix is better than of the old one,
2104 * then remember the new one before continuing
2105 * to search for an even better one.
2107 if (ifa_maybe == NULL ||
2108 ifa_preferred(ifa_maybe, ifa) ||
2109 rn_refines((caddr_t)ifa->ifa_netmask,
2110 (caddr_t)ifa_maybe->ifa_netmask)) {
2123 * Find an interface address specific to an interface best matching
2127 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2130 const char *cp, *cp2, *cp3;
2132 struct ifaddr *ifa_maybe = NULL;
2133 u_int af = addr->sa_family;
2139 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2140 if (ifa->ifa_addr->sa_family != af)
2142 if (ifa_maybe == NULL)
2144 if (ifa->ifa_netmask == 0) {
2145 if (sa_equal(addr, ifa->ifa_addr) ||
2146 (ifa->ifa_dstaddr &&
2147 sa_equal(addr, ifa->ifa_dstaddr)))
2151 if (ifp->if_flags & IFF_POINTOPOINT) {
2152 if (sa_equal(addr, ifa->ifa_dstaddr))
2156 cp2 = ifa->ifa_addr->sa_data;
2157 cp3 = ifa->ifa_netmask->sa_data;
2158 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2159 for (; cp3 < cplim; cp3++)
2160 if ((*cp++ ^ *cp2++) & *cp3)
2172 * See whether new ifa is better than current one:
2173 * 1) A non-virtual one is preferred over virtual.
2174 * 2) A virtual in master state preferred over any other state.
2176 * Used in several address selecting functions.
2179 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2182 return (cur->ifa_carp && (!next->ifa_carp ||
2183 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2186 struct sockaddr_dl *
2187 link_alloc_sdl(size_t size, int flags)
2190 return (malloc(size, M_TEMP, flags));
2194 link_free_sdl(struct sockaddr *sa)
2200 * Fills in given sdl with interface basic info.
2201 * Returns pointer to filled sdl.
2203 struct sockaddr_dl *
2204 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2206 struct sockaddr_dl *sdl;
2208 sdl = (struct sockaddr_dl *)paddr;
2209 memset(sdl, 0, sizeof(struct sockaddr_dl));
2210 sdl->sdl_len = sizeof(struct sockaddr_dl);
2211 sdl->sdl_family = AF_LINK;
2212 sdl->sdl_index = ifp->if_index;
2213 sdl->sdl_type = iftype;
2219 * Mark an interface down and notify protocols of
2223 if_unroute(struct ifnet *ifp, int flag, int fam)
2227 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2229 ifp->if_flags &= ~flag;
2230 getmicrotime(&ifp->if_lastchange);
2231 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2232 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2233 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2234 ifp->if_qflush(ifp);
2237 (*carp_linkstate_p)(ifp);
2242 * Mark an interface up and notify protocols of
2246 if_route(struct ifnet *ifp, int flag, int fam)
2250 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2252 ifp->if_flags |= flag;
2253 getmicrotime(&ifp->if_lastchange);
2254 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2255 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2256 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2258 (*carp_linkstate_p)(ifp);
2265 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2266 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2267 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2268 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2269 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2270 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2271 int (*vlan_setcookie_p)(struct ifnet *, void *);
2272 void *(*vlan_cookie_p)(struct ifnet *);
2275 * Handle a change in the interface link state. To avoid LORs
2276 * between driver lock and upper layer locks, as well as possible
2277 * recursions, we post event to taskqueue, and all job
2278 * is done in static do_link_state_change().
2281 if_link_state_change(struct ifnet *ifp, int link_state)
2283 /* Return if state hasn't changed. */
2284 if (ifp->if_link_state == link_state)
2287 ifp->if_link_state = link_state;
2289 /* XXXGL: reference ifp? */
2290 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2294 do_link_state_change(void *arg, int pending)
2300 link_state = ifp->if_link_state;
2302 CURVNET_SET(ifp->if_vnet);
2304 if (ifp->if_vlantrunk != NULL)
2305 (*vlan_link_state_p)(ifp);
2307 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2308 ifp->if_l2com != NULL)
2309 (*ng_ether_link_state_p)(ifp, link_state);
2311 (*carp_linkstate_p)(ifp);
2313 ifp->if_bridge_linkstate(ifp);
2315 (*lagg_linkstate_p)(ifp, link_state);
2317 if (IS_DEFAULT_VNET(curvnet))
2318 devctl_notify("IFNET", ifp->if_xname,
2319 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2322 if_printf(ifp, "%d link states coalesced\n", pending);
2323 if (log_link_state_change)
2324 if_printf(ifp, "link state changed to %s\n",
2325 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2326 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2331 * Mark an interface down and notify protocols of
2335 if_down(struct ifnet *ifp)
2338 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2339 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2343 * Mark an interface up and notify protocols of
2347 if_up(struct ifnet *ifp)
2350 if_route(ifp, IFF_UP, AF_UNSPEC);
2351 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2355 * Flush an interface queue.
2358 if_qflush(struct ifnet *ifp)
2366 if (ALTQ_IS_ENABLED(ifq))
2370 while ((m = n) != NULL) {
2381 * Map interface name to interface structure pointer, with or without
2382 * returning a reference.
2385 ifunit_ref(const char *name)
2387 struct epoch_tracker et;
2390 NET_EPOCH_ENTER(et);
2391 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2392 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2393 !(ifp->if_flags & IFF_DYING))
2403 ifunit(const char *name)
2405 struct epoch_tracker et;
2408 NET_EPOCH_ENTER(et);
2409 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2410 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2418 ifr_buffer_get_buffer(void *data)
2420 union ifreq_union *ifrup;
2423 #ifdef COMPAT_FREEBSD32
2424 if (SV_CURPROC_FLAG(SV_ILP32))
2425 return ((void *)(uintptr_t)
2426 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2428 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2432 ifr_buffer_set_buffer_null(void *data)
2434 union ifreq_union *ifrup;
2437 #ifdef COMPAT_FREEBSD32
2438 if (SV_CURPROC_FLAG(SV_ILP32))
2439 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2442 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2446 ifr_buffer_get_length(void *data)
2448 union ifreq_union *ifrup;
2451 #ifdef COMPAT_FREEBSD32
2452 if (SV_CURPROC_FLAG(SV_ILP32))
2453 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2455 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2459 ifr_buffer_set_length(void *data, size_t len)
2461 union ifreq_union *ifrup;
2464 #ifdef COMPAT_FREEBSD32
2465 if (SV_CURPROC_FLAG(SV_ILP32))
2466 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2469 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2473 ifr_data_get_ptr(void *ifrp)
2475 union ifreq_union *ifrup;
2478 #ifdef COMPAT_FREEBSD32
2479 if (SV_CURPROC_FLAG(SV_ILP32))
2480 return ((void *)(uintptr_t)
2481 ifrup->ifr32.ifr_ifru.ifru_data);
2483 return (ifrup->ifr.ifr_ifru.ifru_data);
2487 * Hardware specific interface ioctls.
2490 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2493 int error = 0, do_ifup = 0;
2494 int new_flags, temp_flags;
2495 size_t namelen, onamelen;
2497 char *descrbuf, *odescrbuf;
2498 char new_name[IFNAMSIZ];
2500 struct sockaddr_dl *sdl;
2502 ifr = (struct ifreq *)data;
2505 ifr->ifr_index = ifp->if_index;
2509 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2510 ifr->ifr_flags = temp_flags & 0xffff;
2511 ifr->ifr_flagshigh = temp_flags >> 16;
2515 ifr->ifr_reqcap = ifp->if_capabilities;
2516 ifr->ifr_curcap = ifp->if_capenable;
2521 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2526 ifr->ifr_metric = ifp->if_metric;
2530 ifr->ifr_mtu = ifp->if_mtu;
2534 /* XXXGL: did this ever worked? */
2540 sx_slock(&ifdescr_sx);
2541 if (ifp->if_description == NULL)
2544 /* space for terminating nul */
2545 descrlen = strlen(ifp->if_description) + 1;
2546 if (ifr_buffer_get_length(ifr) < descrlen)
2547 ifr_buffer_set_buffer_null(ifr);
2549 error = copyout(ifp->if_description,
2550 ifr_buffer_get_buffer(ifr), descrlen);
2551 ifr_buffer_set_length(ifr, descrlen);
2553 sx_sunlock(&ifdescr_sx);
2557 error = priv_check(td, PRIV_NET_SETIFDESCR);
2562 * Copy only (length-1) bytes to make sure that
2563 * if_description is always nul terminated. The
2564 * length parameter is supposed to count the
2565 * terminating nul in.
2567 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2568 return (ENAMETOOLONG);
2569 else if (ifr_buffer_get_length(ifr) == 0)
2572 descrbuf = malloc(ifr_buffer_get_length(ifr),
2573 M_IFDESCR, M_WAITOK | M_ZERO);
2574 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2575 ifr_buffer_get_length(ifr) - 1);
2577 free(descrbuf, M_IFDESCR);
2582 sx_xlock(&ifdescr_sx);
2583 odescrbuf = ifp->if_description;
2584 ifp->if_description = descrbuf;
2585 sx_xunlock(&ifdescr_sx);
2587 getmicrotime(&ifp->if_lastchange);
2588 free(odescrbuf, M_IFDESCR);
2592 ifr->ifr_fib = ifp->if_fib;
2596 error = priv_check(td, PRIV_NET_SETIFFIB);
2599 if (ifr->ifr_fib >= rt_numfibs)
2602 ifp->if_fib = ifr->ifr_fib;
2606 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2610 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2611 * check, so we don't need special handling here yet.
2613 new_flags = (ifr->ifr_flags & 0xffff) |
2614 (ifr->ifr_flagshigh << 16);
2615 if (ifp->if_flags & IFF_UP &&
2616 (new_flags & IFF_UP) == 0) {
2618 } else if (new_flags & IFF_UP &&
2619 (ifp->if_flags & IFF_UP) == 0) {
2622 /* See if permanently promiscuous mode bit is about to flip */
2623 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2624 if (new_flags & IFF_PPROMISC)
2625 ifp->if_flags |= IFF_PROMISC;
2626 else if (ifp->if_pcount == 0)
2627 ifp->if_flags &= ~IFF_PROMISC;
2628 if (log_promisc_mode_change)
2629 if_printf(ifp, "permanently promiscuous mode %s\n",
2630 ((new_flags & IFF_PPROMISC) ?
2631 "enabled" : "disabled"));
2633 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2634 (new_flags &~ IFF_CANTCHANGE);
2635 if (ifp->if_ioctl) {
2636 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2640 getmicrotime(&ifp->if_lastchange);
2644 error = priv_check(td, PRIV_NET_SETIFCAP);
2647 if (ifp->if_ioctl == NULL)
2648 return (EOPNOTSUPP);
2649 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2651 error = (*ifp->if_ioctl)(ifp, cmd, data);
2653 getmicrotime(&ifp->if_lastchange);
2658 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2663 error = priv_check(td, PRIV_NET_SETIFNAME);
2666 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2670 if (new_name[0] == '\0')
2672 if (new_name[IFNAMSIZ-1] != '\0') {
2673 new_name[IFNAMSIZ-1] = '\0';
2674 if (strlen(new_name) == IFNAMSIZ-1)
2677 if (strcmp(new_name, ifp->if_xname) == 0)
2679 if (ifunit(new_name) != NULL)
2683 * XXX: Locking. Nothing else seems to lock if_flags,
2684 * and there are numerous other races with the
2685 * ifunit() checks not being atomic with namespace
2686 * changes (renames, vmoves, if_attach, etc).
2688 ifp->if_flags |= IFF_RENAMING;
2690 /* Announce the departure of the interface. */
2691 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2692 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2694 if_printf(ifp, "changing name to '%s'\n", new_name);
2697 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2699 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2700 namelen = strlen(new_name);
2701 onamelen = sdl->sdl_nlen;
2703 * Move the address if needed. This is safe because we
2704 * allocate space for a name of length IFNAMSIZ when we
2705 * create this in if_attach().
2707 if (namelen != onamelen) {
2708 bcopy(sdl->sdl_data + onamelen,
2709 sdl->sdl_data + namelen, sdl->sdl_alen);
2711 bcopy(new_name, sdl->sdl_data, namelen);
2712 sdl->sdl_nlen = namelen;
2713 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2714 bzero(sdl->sdl_data, onamelen);
2715 while (namelen != 0)
2716 sdl->sdl_data[--namelen] = 0xff;
2717 IF_ADDR_WUNLOCK(ifp);
2719 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2720 /* Announce the return of the interface. */
2721 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2723 ifp->if_flags &= ~IFF_RENAMING;
2728 error = priv_check(td, PRIV_NET_SETIFVNET);
2731 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2736 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2739 ifp->if_metric = ifr->ifr_metric;
2740 getmicrotime(&ifp->if_lastchange);
2744 error = priv_check(td, PRIV_NET_SETIFPHYS);
2747 if (ifp->if_ioctl == NULL)
2748 return (EOPNOTSUPP);
2749 error = (*ifp->if_ioctl)(ifp, cmd, data);
2751 getmicrotime(&ifp->if_lastchange);
2756 u_long oldmtu = ifp->if_mtu;
2758 error = priv_check(td, PRIV_NET_SETIFMTU);
2761 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2763 if (ifp->if_ioctl == NULL)
2764 return (EOPNOTSUPP);
2765 error = (*ifp->if_ioctl)(ifp, cmd, data);
2767 getmicrotime(&ifp->if_lastchange);
2770 DEBUGNET_NOTIFY_MTU(ifp);
2774 * If the link MTU changed, do network layer specific procedure.
2776 if (ifp->if_mtu != oldmtu) {
2787 if (cmd == SIOCADDMULTI)
2788 error = priv_check(td, PRIV_NET_ADDMULTI);
2790 error = priv_check(td, PRIV_NET_DELMULTI);
2794 /* Don't allow group membership on non-multicast interfaces. */
2795 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2796 return (EOPNOTSUPP);
2798 /* Don't let users screw up protocols' entries. */
2799 if (ifr->ifr_addr.sa_family != AF_LINK)
2802 if (cmd == SIOCADDMULTI) {
2803 struct epoch_tracker et;
2804 struct ifmultiaddr *ifma;
2807 * Userland is only permitted to join groups once
2808 * via the if_addmulti() KPI, because it cannot hold
2809 * struct ifmultiaddr * between calls. It may also
2810 * lose a race while we check if the membership
2813 NET_EPOCH_ENTER(et);
2814 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2819 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2821 error = if_delmulti(ifp, &ifr->ifr_addr);
2824 getmicrotime(&ifp->if_lastchange);
2827 case SIOCSIFPHYADDR:
2828 case SIOCDIFPHYADDR:
2830 case SIOCSIFPHYADDR_IN6:
2833 case SIOCSIFGENERIC:
2834 error = priv_check(td, PRIV_NET_HWIOCTL);
2837 if (ifp->if_ioctl == NULL)
2838 return (EOPNOTSUPP);
2839 error = (*ifp->if_ioctl)(ifp, cmd, data);
2841 getmicrotime(&ifp->if_lastchange);
2845 case SIOCGIFPSRCADDR:
2846 case SIOCGIFPDSTADDR:
2849 case SIOCGIFGENERIC:
2851 case SIOCGIFRSSHASH:
2852 case SIOCGIFDOWNREASON:
2853 if (ifp->if_ioctl == NULL)
2854 return (EOPNOTSUPP);
2855 error = (*ifp->if_ioctl)(ifp, cmd, data);
2859 error = priv_check(td, PRIV_NET_SETLLADDR);
2862 error = if_setlladdr(ifp,
2863 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2867 error = if_gethwaddr(ifp, ifr);
2870 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2871 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2874 if ((error = if_addgroup(ifp,
2875 ifgr_group_get((struct ifgroupreq *)data))))
2879 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2881 struct epoch_tracker et;
2883 NET_EPOCH_ENTER(et);
2884 error = if_getgroup((struct ifgroupreq *)data, ifp);
2889 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2890 error = priv_check(td, PRIV_NET_DELIFGROUP);
2893 if ((error = if_delgroup(ifp,
2894 ifgr_group_get((struct ifgroupreq *)data))))
2905 #ifdef COMPAT_FREEBSD32
2913 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2916 #ifdef COMPAT_FREEBSD32
2918 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2920 struct ifmediareq32 *ifmr32;
2922 ifmr32 = (struct ifmediareq32 *)data;
2923 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2924 sizeof(ifmr->ifm_name));
2925 ifmr->ifm_current = ifmr32->ifm_current;
2926 ifmr->ifm_mask = ifmr32->ifm_mask;
2927 ifmr->ifm_status = ifmr32->ifm_status;
2928 ifmr->ifm_active = ifmr32->ifm_active;
2929 ifmr->ifm_count = ifmr32->ifm_count;
2930 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2934 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2936 struct ifmediareq32 *ifmr32;
2938 ifmr32 = (struct ifmediareq32 *)data;
2939 ifmr32->ifm_current = ifmr->ifm_current;
2940 ifmr32->ifm_mask = ifmr->ifm_mask;
2941 ifmr32->ifm_status = ifmr->ifm_status;
2942 ifmr32->ifm_active = ifmr->ifm_active;
2943 ifmr32->ifm_count = ifmr->ifm_count;
2951 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2953 #ifdef COMPAT_FREEBSD32
2954 caddr_t saved_data = NULL;
2955 struct ifmediareq ifmr;
2956 struct ifmediareq *ifmrp = NULL;
2966 CURVNET_SET(so->so_vnet);
2968 /* Make sure the VNET is stable. */
2969 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2978 error = ifconf(cmd, data);
2981 #ifdef COMPAT_FREEBSD32
2984 struct ifconf32 *ifc32;
2987 ifc32 = (struct ifconf32 *)data;
2988 ifc.ifc_len = ifc32->ifc_len;
2989 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2991 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2993 ifc32->ifc_len = ifc.ifc_len;
2999 #ifdef COMPAT_FREEBSD32
3001 case SIOCGIFMEDIA32:
3002 case SIOCGIFXMEDIA32:
3004 ifmr_init(ifmrp, data);
3005 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3007 data = (caddr_t)ifmrp;
3011 ifr = (struct ifreq *)data;
3015 error = priv_check(td, PRIV_NET_SETIFVNET);
3017 error = if_vmove_reclaim(td, ifr->ifr_name,
3023 error = priv_check(td, PRIV_NET_IFCREATE);
3025 error = if_clone_create(ifr->ifr_name,
3026 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3027 ifr_data_get_ptr(ifr) : NULL);
3030 error = priv_check(td, PRIV_NET_IFDESTROY);
3032 error = if_clone_destroy(ifr->ifr_name);
3035 case SIOCIFGCLONERS:
3036 error = if_clone_list((struct if_clonereq *)data);
3039 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3040 error = if_getgroupmembers((struct ifgroupreq *)data);
3043 #if defined(INET) || defined(INET6)
3046 if (carp_ioctl_p == NULL)
3047 error = EPROTONOSUPPORT;
3049 error = (*carp_ioctl_p)(ifr, cmd, td);
3054 ifp = ifunit_ref(ifr->ifr_name);
3060 error = ifhwioctl(cmd, ifp, data, td);
3061 if (error != ENOIOCTL)
3064 oif_flags = ifp->if_flags;
3065 if (so->so_proto == NULL) {
3071 * Pass the request on to the socket control method, and if the
3072 * latter returns EOPNOTSUPP, directly to the interface.
3074 * Make an exception for the legacy SIOCSIF* requests. Drivers
3075 * trust SIOCSIFADDR et al to come from an already privileged
3076 * layer, and do not perform any credentials checks or input
3079 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3081 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3082 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3083 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3084 error = (*ifp->if_ioctl)(ifp, cmd, data);
3086 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3088 if (ifp->if_flags & IFF_UP)
3096 #ifdef COMPAT_FREEBSD32
3097 if (ifmrp != NULL) {
3098 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3099 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3102 ifmr_update(ifmrp, data);
3110 * The code common to handling reference counted flags,
3111 * e.g., in ifpromisc() and if_allmulti().
3112 * The "pflag" argument can specify a permanent mode flag to check,
3113 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3115 * Only to be used on stack-owned flags, not driver-owned flags.
3118 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3122 int oldflags, oldcount;
3124 /* Sanity checks to catch programming errors */
3125 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3126 ("%s: setting driver-owned flag %d", __func__, flag));
3129 KASSERT(*refcount >= 0,
3130 ("%s: increment negative refcount %d for flag %d",
3131 __func__, *refcount, flag));
3133 KASSERT(*refcount > 0,
3134 ("%s: decrement non-positive refcount %d for flag %d",
3135 __func__, *refcount, flag));
3137 /* In case this mode is permanent, just touch refcount */
3138 if (ifp->if_flags & pflag) {
3139 *refcount += onswitch ? 1 : -1;
3143 /* Save ifnet parameters for if_ioctl() may fail */
3144 oldcount = *refcount;
3145 oldflags = ifp->if_flags;
3148 * See if we aren't the only and touching refcount is enough.
3149 * Actually toggle interface flag if we are the first or last.
3154 ifp->if_flags |= flag;
3158 ifp->if_flags &= ~flag;
3161 /* Call down the driver since we've changed interface flags */
3162 if (ifp->if_ioctl == NULL) {
3166 ifr.ifr_flags = ifp->if_flags & 0xffff;
3167 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3168 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3171 /* Notify userland that interface flags have changed */
3176 /* Recover after driver error */
3177 *refcount = oldcount;
3178 ifp->if_flags = oldflags;
3183 * Set/clear promiscuous mode on interface ifp based on the truth value
3184 * of pswitch. The calls are reference counted so that only the first
3185 * "on" request actually has an effect, as does the final "off" request.
3186 * Results are undefined if the "off" and "on" requests are not matched.
3189 ifpromisc(struct ifnet *ifp, int pswitch)
3192 int oldflags = ifp->if_flags;
3194 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3195 &ifp->if_pcount, pswitch);
3196 /* If promiscuous mode status has changed, log a message */
3197 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3198 log_promisc_mode_change)
3199 if_printf(ifp, "promiscuous mode %s\n",
3200 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3205 * Return interface configuration
3206 * of system. List may be used
3207 * in later ioctl's (above) to get
3208 * other information.
3212 ifconf(u_long cmd, caddr_t data)
3214 struct ifconf *ifc = (struct ifconf *)data;
3219 int error, full = 0, valid_len, max_len;
3221 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3222 max_len = MAXPHYS - 1;
3224 /* Prevent hostile input from being able to crash the system */
3225 if (ifc->ifc_len <= 0)
3229 if (ifc->ifc_len <= max_len) {
3230 max_len = ifc->ifc_len;
3233 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3238 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3239 struct epoch_tracker et;
3243 * Zero the ifr to make sure we don't disclose the contents
3246 memset(&ifr, 0, sizeof(ifr));
3248 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3249 >= sizeof(ifr.ifr_name)) {
3252 return (ENAMETOOLONG);
3256 NET_EPOCH_ENTER(et);
3257 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3258 struct sockaddr *sa = ifa->ifa_addr;
3260 if (prison_if(curthread->td_ucred, sa) != 0)
3263 if (sa->sa_len <= sizeof(*sa)) {
3264 if (sa->sa_len < sizeof(*sa)) {
3265 memset(&ifr.ifr_ifru.ifru_addr, 0,
3266 sizeof(ifr.ifr_ifru.ifru_addr));
3267 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3270 ifr.ifr_ifru.ifru_addr = *sa;
3271 sbuf_bcat(sb, &ifr, sizeof(ifr));
3272 max_len += sizeof(ifr);
3275 offsetof(struct ifreq, ifr_addr));
3276 max_len += offsetof(struct ifreq, ifr_addr);
3277 sbuf_bcat(sb, sa, sa->sa_len);
3278 max_len += sa->sa_len;
3281 if (sbuf_error(sb) == 0)
3282 valid_len = sbuf_len(sb);
3286 sbuf_bcat(sb, &ifr, sizeof(ifr));
3287 max_len += sizeof(ifr);
3289 if (sbuf_error(sb) == 0)
3290 valid_len = sbuf_len(sb);
3296 * If we didn't allocate enough space (uncommon), try again. If
3297 * we have already allocated as much space as we are allowed,
3298 * return what we've got.
3300 if (valid_len != max_len && !full) {
3305 ifc->ifc_len = valid_len;
3307 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3313 * Just like ifpromisc(), but for all-multicast-reception mode.
3316 if_allmulti(struct ifnet *ifp, int onswitch)
3319 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3322 struct ifmultiaddr *
3323 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3325 struct ifmultiaddr *ifma;
3327 IF_ADDR_LOCK_ASSERT(ifp);
3329 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3330 if (sa->sa_family == AF_LINK) {
3331 if (sa_dl_equal(ifma->ifma_addr, sa))
3334 if (sa_equal(ifma->ifma_addr, sa))
3343 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3344 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3345 * the ifnet multicast address list here, so the caller must do that and
3346 * other setup work (such as notifying the device driver). The reference
3347 * count is initialized to 1.
3349 static struct ifmultiaddr *
3350 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3353 struct ifmultiaddr *ifma;
3354 struct sockaddr *dupsa;
3356 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3361 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3362 if (dupsa == NULL) {
3363 free(ifma, M_IFMADDR);
3366 bcopy(sa, dupsa, sa->sa_len);
3367 ifma->ifma_addr = dupsa;
3369 ifma->ifma_ifp = ifp;
3370 ifma->ifma_refcount = 1;
3371 ifma->ifma_protospec = NULL;
3374 ifma->ifma_lladdr = NULL;
3378 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3379 if (dupsa == NULL) {
3380 free(ifma->ifma_addr, M_IFMADDR);
3381 free(ifma, M_IFMADDR);
3384 bcopy(llsa, dupsa, llsa->sa_len);
3385 ifma->ifma_lladdr = dupsa;
3391 * if_freemulti: free ifmultiaddr structure and possibly attached related
3392 * addresses. The caller is responsible for implementing reference
3393 * counting, notifying the driver, handling routing messages, and releasing
3394 * any dependent link layer state.
3396 #ifdef MCAST_VERBOSE
3397 extern void kdb_backtrace(void);
3400 if_freemulti_internal(struct ifmultiaddr *ifma)
3403 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3404 ifma->ifma_refcount));
3406 if (ifma->ifma_lladdr != NULL)
3407 free(ifma->ifma_lladdr, M_IFMADDR);
3408 #ifdef MCAST_VERBOSE
3410 printf("%s freeing ifma: %p\n", __func__, ifma);
3412 free(ifma->ifma_addr, M_IFMADDR);
3413 free(ifma, M_IFMADDR);
3417 if_destroymulti(epoch_context_t ctx)
3419 struct ifmultiaddr *ifma;
3421 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3422 if_freemulti_internal(ifma);
3426 if_freemulti(struct ifmultiaddr *ifma)
3428 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3429 ifma->ifma_refcount));
3431 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3436 * Register an additional multicast address with a network interface.
3438 * - If the address is already present, bump the reference count on the
3439 * address and return.
3440 * - If the address is not link-layer, look up a link layer address.
3441 * - Allocate address structures for one or both addresses, and attach to the
3442 * multicast address list on the interface. If automatically adding a link
3443 * layer address, the protocol address will own a reference to the link
3444 * layer address, to be freed when it is freed.
3445 * - Notify the network device driver of an addition to the multicast address
3448 * 'sa' points to caller-owned memory with the desired multicast address.
3450 * 'retifma' will be used to return a pointer to the resulting multicast
3451 * address reference, if desired.
3454 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3455 struct ifmultiaddr **retifma)
3457 struct ifmultiaddr *ifma, *ll_ifma;
3458 struct sockaddr *llsa;
3459 struct sockaddr_dl sdl;
3463 IN_MULTI_LIST_UNLOCK_ASSERT();
3466 IN6_MULTI_LIST_UNLOCK_ASSERT();
3469 * If the address is already present, return a new reference to it;
3470 * otherwise, allocate storage and set up a new address.
3473 ifma = if_findmulti(ifp, sa);
3475 ifma->ifma_refcount++;
3476 if (retifma != NULL)
3478 IF_ADDR_WUNLOCK(ifp);
3483 * The address isn't already present; resolve the protocol address
3484 * into a link layer address, and then look that up, bump its
3485 * refcount or allocate an ifma for that also.
3486 * Most link layer resolving functions returns address data which
3487 * fits inside default sockaddr_dl structure. However callback
3488 * can allocate another sockaddr structure, in that case we need to
3493 if (ifp->if_resolvemulti != NULL) {
3494 /* Provide called function with buffer size information */
3495 sdl.sdl_len = sizeof(sdl);
3496 llsa = (struct sockaddr *)&sdl;
3497 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3503 * Allocate the new address. Don't hook it up yet, as we may also
3504 * need to allocate a link layer multicast address.
3506 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3513 * If a link layer address is found, we'll need to see if it's
3514 * already present in the address list, or allocate is as well.
3515 * When this block finishes, the link layer address will be on the
3519 ll_ifma = if_findmulti(ifp, llsa);
3520 if (ll_ifma == NULL) {
3521 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3522 if (ll_ifma == NULL) {
3523 --ifma->ifma_refcount;
3528 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3529 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3532 ll_ifma->ifma_refcount++;
3533 ifma->ifma_llifma = ll_ifma;
3537 * We now have a new multicast address, ifma, and possibly a new or
3538 * referenced link layer address. Add the primary address to the
3539 * ifnet address list.
3541 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3542 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3544 if (retifma != NULL)
3548 * Must generate the message while holding the lock so that 'ifma'
3549 * pointer is still valid.
3551 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3552 IF_ADDR_WUNLOCK(ifp);
3555 * We are certain we have added something, so call down to the
3556 * interface to let them know about it.
3558 if (ifp->if_ioctl != NULL) {
3559 if (THREAD_CAN_SLEEP())
3560 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3562 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3565 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3566 link_free_sdl(llsa);
3571 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3572 link_free_sdl(llsa);
3575 IF_ADDR_WUNLOCK(ifp);
3580 if_siocaddmulti(void *arg, int pending)
3587 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3589 CURVNET_SET(ifp->if_vnet);
3590 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3595 * Delete a multicast group membership by network-layer group address.
3597 * Returns ENOENT if the entry could not be found. If ifp no longer
3598 * exists, results are undefined. This entry point should only be used
3599 * from subsystems which do appropriate locking to hold ifp for the
3600 * duration of the call.
3601 * Network-layer protocol domains must use if_delmulti_ifma().
3604 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3606 struct ifmultiaddr *ifma;
3609 KASSERT(ifp, ("%s: NULL ifp", __func__));
3613 ifma = if_findmulti(ifp, sa);
3615 lastref = if_delmulti_locked(ifp, ifma, 0);
3616 IF_ADDR_WUNLOCK(ifp);
3621 if (lastref && ifp->if_ioctl != NULL) {
3622 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3629 * Delete all multicast group membership for an interface.
3630 * Should be used to quickly flush all multicast filters.
3633 if_delallmulti(struct ifnet *ifp)
3635 struct ifmultiaddr *ifma;
3636 struct ifmultiaddr *next;
3639 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3640 if_delmulti_locked(ifp, ifma, 0);
3641 IF_ADDR_WUNLOCK(ifp);
3645 if_delmulti_ifma(struct ifmultiaddr *ifma)
3647 if_delmulti_ifma_flags(ifma, 0);
3651 * Delete a multicast group membership by group membership pointer.
3652 * Network-layer protocol domains must use this routine.
3654 * It is safe to call this routine if the ifp disappeared.
3657 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3661 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3663 IN_MULTI_LIST_UNLOCK_ASSERT();
3665 ifp = ifma->ifma_ifp;
3668 printf("%s: ifma_ifp seems to be detached\n", __func__);
3670 struct epoch_tracker et;
3673 NET_EPOCH_ENTER(et);
3674 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3683 * If and only if the ifnet instance exists: Acquire the address lock.
3688 lastref = if_delmulti_locked(ifp, ifma, flags);
3692 * If and only if the ifnet instance exists:
3693 * Release the address lock.
3694 * If the group was left: update the hardware hash filter.
3696 IF_ADDR_WUNLOCK(ifp);
3697 if (lastref && ifp->if_ioctl != NULL) {
3698 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3704 * Perform deletion of network-layer and/or link-layer multicast address.
3706 * Return 0 if the reference count was decremented.
3707 * Return 1 if the final reference was released, indicating that the
3708 * hardware hash filter should be reprogrammed.
3711 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3713 struct ifmultiaddr *ll_ifma;
3715 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3716 KASSERT(ifma->ifma_ifp == ifp,
3717 ("%s: inconsistent ifp %p", __func__, ifp));
3718 IF_ADDR_WLOCK_ASSERT(ifp);
3721 ifp = ifma->ifma_ifp;
3722 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3725 * If the ifnet is detaching, null out references to ifnet,
3726 * so that upper protocol layers will notice, and not attempt
3727 * to obtain locks for an ifnet which no longer exists. The
3728 * routing socket announcement must happen before the ifnet
3729 * instance is detached from the system.
3733 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3736 * ifp may already be nulled out if we are being reentered
3737 * to delete the ll_ifma.
3740 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3741 ifma->ifma_ifp = NULL;
3745 if (--ifma->ifma_refcount > 0)
3748 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3749 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3750 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3753 * If this ifma is a network-layer ifma, a link-layer ifma may
3754 * have been associated with it. Release it first if so.
3756 ll_ifma = ifma->ifma_llifma;
3757 if (ll_ifma != NULL) {
3758 KASSERT(ifma->ifma_lladdr != NULL,
3759 ("%s: llifma w/o lladdr", __func__));
3761 ll_ifma->ifma_ifp = NULL; /* XXX */
3762 if (--ll_ifma->ifma_refcount == 0) {
3764 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3765 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3767 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3770 if_freemulti(ll_ifma);
3775 struct ifmultiaddr *ifmatmp;
3777 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3778 MPASS(ifma != ifmatmp);
3783 * The last reference to this instance of struct ifmultiaddr
3784 * was released; the hardware should be notified of this change.
3790 * Set the link layer address on an interface.
3792 * At this time we only support certain types of interfaces,
3793 * and we don't allow the length of the address to change.
3795 * Set noinline to be dtrace-friendly
3798 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3800 struct sockaddr_dl *sdl;
3808 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3812 if (len != sdl->sdl_alen) /* don't allow length to change */
3815 switch (ifp->if_type) {
3820 case IFT_IEEE8023ADLAG:
3821 bcopy(lladdr, LLADDR(sdl), len);
3828 * If the interface is already up, we need
3829 * to re-init it in order to reprogram its
3832 if ((ifp->if_flags & IFF_UP) != 0) {
3833 if (ifp->if_ioctl) {
3834 ifp->if_flags &= ~IFF_UP;
3835 ifr.ifr_flags = ifp->if_flags & 0xffff;
3836 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3837 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3838 ifp->if_flags |= IFF_UP;
3839 ifr.ifr_flags = ifp->if_flags & 0xffff;
3840 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3841 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3844 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3850 * Compat function for handling basic encapsulation requests.
3851 * Not converted stacks (FDDI, IB, ..) supports traditional
3852 * output model: ARP (and other similar L2 protocols) are handled
3853 * inside output routine, arpresolve/nd6_resolve() returns MAC
3854 * address instead of full prepend.
3856 * This function creates calculated header==MAC for IPv4/IPv6 and
3857 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3861 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3864 if (req->rtype != IFENCAP_LL)
3865 return (EOPNOTSUPP);
3867 if (req->bufsize < req->lladdr_len)
3870 switch (req->family) {
3875 return (EAFNOSUPPORT);
3878 /* Copy lladdr to storage as is */
3879 memmove(req->buf, req->lladdr, req->lladdr_len);
3880 req->bufsize = req->lladdr_len;
3881 req->lladdr_off = 0;
3887 * Tunnel interfaces can nest, also they may cause infinite recursion
3888 * calls when misconfigured. We'll prevent this by detecting loops.
3889 * High nesting level may cause stack exhaustion. We'll prevent this
3890 * by introducing upper limit.
3892 * Return 0, if tunnel nesting count is equal or less than limit.
3895 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3903 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3904 if (*(struct ifnet **)(mtag + 1) == ifp) {
3905 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3910 if (count > limit) {
3912 "%s: if_output recursively called too many times(%d)\n",
3913 if_name(ifp), count);
3916 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3919 *(struct ifnet **)(mtag + 1) = ifp;
3920 m_tag_prepend(m, mtag);
3925 * Get the link layer address that was read from the hardware at attach.
3927 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3928 * their component interfaces as IFT_IEEE8023ADLAG.
3931 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3934 if (ifp->if_hw_addr == NULL)
3937 switch (ifp->if_type) {
3939 case IFT_IEEE8023ADLAG:
3940 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3948 * The name argument must be a pointer to storage which will last as
3949 * long as the interface does. For physical devices, the result of
3950 * device_get_name(dev) is a good choice and for pseudo-devices a
3951 * static string works well.
3954 if_initname(struct ifnet *ifp, const char *name, int unit)
3956 ifp->if_dname = name;
3957 ifp->if_dunit = unit;
3958 if (unit != IF_DUNIT_NONE)
3959 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3961 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3965 if_printf(struct ifnet *ifp, const char *fmt, ...)
3970 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3972 vlog(LOG_INFO, if_fmt, ap);
3978 if_start(struct ifnet *ifp)
3981 (*(ifp)->if_start)(ifp);
3985 * Backwards compatibility interface for drivers
3986 * that have not implemented it
3989 if_transmit(struct ifnet *ifp, struct mbuf *m)
3993 IFQ_HANDOFF(ifp, m, error);
3998 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4005 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4010 if (_IF_QFULL(ifq)) {
4012 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4017 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4018 if (m->m_flags & (M_BCAST|M_MCAST))
4019 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4020 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4022 _IF_ENQUEUE(ifq, m);
4024 if (ifp != NULL && !active)
4025 (*(ifp)->if_start)(ifp);
4030 if_register_com_alloc(u_char type,
4031 if_com_alloc_t *a, if_com_free_t *f)
4034 KASSERT(if_com_alloc[type] == NULL,
4035 ("if_register_com_alloc: %d already registered", type));
4036 KASSERT(if_com_free[type] == NULL,
4037 ("if_register_com_alloc: %d free already registered", type));
4039 if_com_alloc[type] = a;
4040 if_com_free[type] = f;
4044 if_deregister_com_alloc(u_char type)
4047 KASSERT(if_com_alloc[type] != NULL,
4048 ("if_deregister_com_alloc: %d not registered", type));
4049 KASSERT(if_com_free[type] != NULL,
4050 ("if_deregister_com_alloc: %d free not registered", type));
4051 if_com_alloc[type] = NULL;
4052 if_com_free[type] = NULL;
4055 /* API for driver access to network stack owned ifnet.*/
4057 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4061 oldbrate = ifp->if_baudrate;
4062 ifp->if_baudrate = baudrate;
4067 if_getbaudrate(if_t ifp)
4070 return (((struct ifnet *)ifp)->if_baudrate);
4074 if_setcapabilities(if_t ifp, int capabilities)
4076 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4081 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4083 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4084 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4090 if_getcapabilities(if_t ifp)
4092 return ((struct ifnet *)ifp)->if_capabilities;
4096 if_setcapenable(if_t ifp, int capabilities)
4098 ((struct ifnet *)ifp)->if_capenable = capabilities;
4103 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4106 ((struct ifnet *)ifp)->if_capenable |= setcap;
4108 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4114 if_getdname(if_t ifp)
4116 return ((struct ifnet *)ifp)->if_dname;
4120 if_togglecapenable(if_t ifp, int togglecap)
4122 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4127 if_getcapenable(if_t ifp)
4129 return ((struct ifnet *)ifp)->if_capenable;
4133 * This is largely undesirable because it ties ifnet to a device, but does
4134 * provide flexiblity for an embedded product vendor. Should be used with
4135 * the understanding that it violates the interface boundaries, and should be
4136 * a last resort only.
4139 if_setdev(if_t ifp, void *dev)
4145 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4147 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4148 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4154 if_getdrvflags(if_t ifp)
4156 return ((struct ifnet *)ifp)->if_drv_flags;
4160 if_setdrvflags(if_t ifp, int flags)
4162 ((struct ifnet *)ifp)->if_drv_flags = flags;
4168 if_setflags(if_t ifp, int flags)
4171 ifp->if_flags = flags;
4176 if_setflagbits(if_t ifp, int set, int clear)
4178 ((struct ifnet *)ifp)->if_flags |= set;
4179 ((struct ifnet *)ifp)->if_flags &= ~clear;
4185 if_getflags(if_t ifp)
4187 return ((struct ifnet *)ifp)->if_flags;
4191 if_clearhwassist(if_t ifp)
4193 ((struct ifnet *)ifp)->if_hwassist = 0;
4198 if_sethwassistbits(if_t ifp, int toset, int toclear)
4200 ((struct ifnet *)ifp)->if_hwassist |= toset;
4201 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4207 if_sethwassist(if_t ifp, int hwassist_bit)
4209 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4214 if_gethwassist(if_t ifp)
4216 return ((struct ifnet *)ifp)->if_hwassist;
4220 if_setmtu(if_t ifp, int mtu)
4222 ((struct ifnet *)ifp)->if_mtu = mtu;
4229 return ((struct ifnet *)ifp)->if_mtu;
4233 if_getmtu_family(if_t ifp, int family)
4237 for (dp = domains; dp; dp = dp->dom_next) {
4238 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4239 return (dp->dom_ifmtu((struct ifnet *)ifp));
4242 return (((struct ifnet *)ifp)->if_mtu);
4246 * Methods for drivers to access interface unicast and multicast
4247 * link level addresses. Driver shall not know 'struct ifaddr' neither
4248 * 'struct ifmultiaddr'.
4251 if_lladdr_count(if_t ifp)
4253 struct epoch_tracker et;
4258 NET_EPOCH_ENTER(et);
4259 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4260 if (ifa->ifa_addr->sa_family == AF_LINK)
4268 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4270 struct epoch_tracker et;
4277 NET_EPOCH_ENTER(et);
4278 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4279 if (ifa->ifa_addr->sa_family != AF_LINK)
4281 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4290 if_llmaddr_count(if_t ifp)
4292 struct epoch_tracker et;
4293 struct ifmultiaddr *ifma;
4297 NET_EPOCH_ENTER(et);
4298 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4299 if (ifma->ifma_addr->sa_family == AF_LINK)
4307 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4309 struct epoch_tracker et;
4310 struct ifmultiaddr *ifma;
4316 NET_EPOCH_ENTER(et);
4317 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4318 if (ifma->ifma_addr->sa_family != AF_LINK)
4320 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4329 if_setsoftc(if_t ifp, void *softc)
4331 ((struct ifnet *)ifp)->if_softc = softc;
4336 if_getsoftc(if_t ifp)
4338 return ((struct ifnet *)ifp)->if_softc;
4342 if_setrcvif(struct mbuf *m, if_t ifp)
4345 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4346 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4350 if_setvtag(struct mbuf *m, uint16_t tag)
4352 m->m_pkthdr.ether_vtag = tag;
4356 if_getvtag(struct mbuf *m)
4359 return (m->m_pkthdr.ether_vtag);
4363 if_sendq_empty(if_t ifp)
4365 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4369 if_getifaddr(if_t ifp)
4371 return ((struct ifnet *)ifp)->if_addr;
4375 if_getamcount(if_t ifp)
4377 return ((struct ifnet *)ifp)->if_amcount;
4382 if_setsendqready(if_t ifp)
4384 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4389 if_setsendqlen(if_t ifp, int tx_desc_count)
4391 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4392 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4398 if_vlantrunkinuse(if_t ifp)
4400 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4404 if_input(if_t ifp, struct mbuf* sendmp)
4406 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4412 if_dequeue(if_t ifp)
4415 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4421 if_sendq_prepend(if_t ifp, struct mbuf *m)
4423 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4428 if_setifheaderlen(if_t ifp, int len)
4430 ((struct ifnet *)ifp)->if_hdrlen = len;
4435 if_getlladdr(if_t ifp)
4437 return (IF_LLADDR((struct ifnet *)ifp));
4441 if_gethandle(u_char type)
4443 return (if_alloc(type));
4447 if_bpfmtap(if_t ifh, struct mbuf *m)
4449 struct ifnet *ifp = (struct ifnet *)ifh;
4455 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4457 struct ifnet *ifp = (struct ifnet *)ifh;
4459 ETHER_BPF_MTAP(ifp, m);
4463 if_vlancap(if_t ifh)
4465 struct ifnet *ifp = (struct ifnet *)ifh;
4466 VLAN_CAPABILITIES(ifp);
4470 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4473 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4478 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4481 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4486 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4489 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4494 if_gethwtsomax(if_t ifp)
4497 return (((struct ifnet *)ifp)->if_hw_tsomax);
4501 if_gethwtsomaxsegcount(if_t ifp)
4504 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4508 if_gethwtsomaxsegsize(if_t ifp)
4511 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4515 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4517 ((struct ifnet *)ifp)->if_init = init_fn;
4521 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4523 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4527 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4529 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4533 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4535 ((struct ifnet *)ifp)->if_transmit = start_fn;
4538 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4540 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4545 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4548 ifp->if_get_counter = fn;
4551 /* Revisit these - These are inline functions originally. */
4553 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4555 return drbr_inuse(ifh, br);
4559 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4561 return drbr_dequeue(ifh, br);
4565 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4567 return drbr_needs_enqueue(ifh, br);
4571 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4573 return drbr_enqueue(ifh, br, m);