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>
83 #include <net/route/route_ctl.h>
86 #if defined(INET) || defined(INET6)
87 #include <net/ethernet.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_carp.h>
93 #include <net/debugnet.h>
94 #include <netinet/if_ether.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet6/in6_ifattach.h>
100 #endif /* INET || INET6 */
102 #include <security/mac/mac_framework.h>
105 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
106 * and ifr_ifru when it is used in SIOCGIFCONF.
108 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
109 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
111 __read_mostly epoch_t net_epoch_preempt;
112 #ifdef COMPAT_FREEBSD32
113 #include <sys/mount.h>
114 #include <compat/freebsd32/freebsd32.h>
116 struct ifreq_buffer32 {
117 uint32_t length; /* (size_t) */
118 uint32_t buffer; /* (void *) */
122 * Interface request structure used for socket
123 * ioctl's. All interface ioctl's must have parameter
124 * definitions which begin with ifr_name. The
125 * remainder may be interface specific.
128 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
130 struct sockaddr ifru_addr;
131 struct sockaddr ifru_dstaddr;
132 struct sockaddr ifru_broadaddr;
133 struct ifreq_buffer32 ifru_buffer;
144 u_char ifru_vlan_pcp;
147 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
148 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
149 __offsetof(struct ifreq32, ifr_ifru));
151 struct ifgroupreq32 {
152 char ifgr_name[IFNAMSIZ];
155 char ifgru_group[IFNAMSIZ];
156 uint32_t ifgru_groups;
160 struct ifmediareq32 {
161 char ifm_name[IFNAMSIZ];
167 uint32_t ifm_ulist; /* (int *) */
169 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
170 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
172 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
173 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
174 #else /* !COMPAT_FREEBSD32 */
175 #define _CASE_IOC_IFGROUPREQ_32(cmd)
176 #endif /* !COMPAT_FREEBSD32 */
178 #define CASE_IOC_IFGROUPREQ(cmd) \
179 _CASE_IOC_IFGROUPREQ_32(cmd) \
184 #ifdef COMPAT_FREEBSD32
185 struct ifreq32 ifr32;
189 union ifgroupreq_union {
190 struct ifgroupreq ifgr;
191 #ifdef COMPAT_FREEBSD32
192 struct ifgroupreq32 ifgr32;
196 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
198 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
199 "Generic link-management");
201 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
202 &ifqmaxlen, 0, "max send queue size");
204 /* Log link state change events */
205 static int log_link_state_change = 1;
207 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
208 &log_link_state_change, 0,
209 "log interface link state change events");
211 /* Log promiscuous mode change events */
212 static int log_promisc_mode_change = 1;
214 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
215 &log_promisc_mode_change, 1,
216 "log promiscuous mode change events");
218 /* Interface description */
219 static unsigned int ifdescr_maxlen = 1024;
220 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
222 "administrative maximum length for interface description");
224 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
226 /* global sx for non-critical path ifdescr */
227 static struct sx ifdescr_sx;
228 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
230 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
231 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
232 /* These are external hooks for CARP. */
233 void (*carp_linkstate_p)(struct ifnet *ifp);
234 void (*carp_demote_adj_p)(int, char *);
235 int (*carp_master_p)(struct ifaddr *);
236 #if defined(INET) || defined(INET6)
237 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
238 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
239 const struct sockaddr *sa);
240 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
241 int (*carp_attach_p)(struct ifaddr *, int);
242 void (*carp_detach_p)(struct ifaddr *, bool);
245 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
248 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
249 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
250 const struct in6_addr *taddr);
253 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
256 * XXX: Style; these should be sorted alphabetically, and unprototyped
257 * static functions should be prototyped. Currently they are sorted by
260 static void if_attachdomain(void *);
261 static void if_attachdomain1(struct ifnet *);
262 static int ifconf(u_long, caddr_t);
263 static void *if_grow(void);
264 static void if_input_default(struct ifnet *, struct mbuf *);
265 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
266 static void if_route(struct ifnet *, int flag, int fam);
267 static int if_setflag(struct ifnet *, int, int, int *, int);
268 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
269 static void if_unroute(struct ifnet *, int flag, int fam);
270 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
271 static void do_link_state_change(void *, int);
272 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
273 static int if_getgroupmembers(struct ifgroupreq *);
274 static void if_delgroups(struct ifnet *);
275 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
276 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
277 static void if_siocaddmulti(void *, int);
278 static void if_link_ifnet(struct ifnet *);
279 static bool if_unlink_ifnet(struct ifnet *, bool);
281 static int if_vmove(struct ifnet *, struct vnet *);
286 * XXX: declare here to avoid to include many inet6 related files..
287 * should be more generalized?
289 extern void nd6_setmtu(struct ifnet *);
292 /* ipsec helper hooks */
293 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
294 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
296 VNET_DEFINE(int, if_index);
297 int ifqmaxlen = IFQ_MAXLEN;
298 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
299 VNET_DEFINE(struct ifgrouphead, ifg_head);
301 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
303 /* Table of ifnet by index. */
304 VNET_DEFINE(struct ifnet **, ifindex_table);
306 #define V_if_indexlim VNET(if_indexlim)
307 #define V_ifindex_table VNET(ifindex_table)
310 * The global network interface list (V_ifnet) and related state (such as
311 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
312 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
314 struct sx ifnet_sxlock;
315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
317 struct sx ifnet_detach_sxlock;
318 SX_SYSINIT(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx");
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);
477 if_link_ifnet(struct ifnet *ifp)
481 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
483 curvnet->vnet_ifcnt++;
489 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
495 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
497 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
499 ifp->if_flags |= IFF_DYING;
504 curvnet->vnet_ifcnt--;
513 vnet_if_return(const void *unused __unused)
515 struct ifnet *ifp, *nifp;
516 struct ifnet **pending;
522 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
523 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
524 * if_detach_internal(), which waits for NET_EPOCH callbacks to
525 * complete. We can't do that from within NET_EPOCH.
527 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
528 * read/write lock. We cannot hold the lock as we call if_vmove()
529 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
534 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
535 M_IFNET, M_WAITOK | M_ZERO);
537 /* Return all inherited interfaces to their parent vnets. */
538 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
539 if (ifp->if_home_vnet != ifp->if_vnet) {
540 found = if_unlink_ifnet(ifp, true);
548 for (int j = 0; j < i; j++) {
549 sx_xlock(&ifnet_detach_sxlock);
550 if_vmove(pending[j], pending[j]->if_home_vnet);
551 sx_xunlock(&ifnet_detach_sxlock);
554 free(pending, M_IFNET);
556 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
557 vnet_if_return, NULL);
569 IFNET_WLOCK_ASSERT();
570 oldlim = V_if_indexlim;
572 n = (oldlim << 1) * sizeof(*e);
573 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
575 if (V_if_indexlim != oldlim) {
579 if (V_ifindex_table != NULL) {
580 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
581 old = V_ifindex_table;
589 * Allocate a struct ifnet and an index for an interface. A layer 2
590 * common structure will also be allocated if an allocation routine is
591 * registered for the passed type.
594 if_alloc_domain(u_char type, int numa_domain)
600 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
601 if (numa_domain == IF_NODOM)
602 ifp = malloc(sizeof(struct ifnet), M_IFNET,
605 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
606 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
609 idx = ifindex_alloc(&old);
610 if (__predict_false(idx == USHRT_MAX)) {
612 epoch_wait_preempt(net_epoch_preempt);
616 ifnet_setbyindex(idx, IFNET_HOLD);
620 ifp->if_alloctype = type;
621 ifp->if_numa_domain = numa_domain;
623 ifp->if_vnet = curvnet;
625 if (if_com_alloc[type] != NULL) {
626 ifp->if_l2com = if_com_alloc[type](type, ifp);
627 if (ifp->if_l2com == NULL) {
634 IF_ADDR_LOCK_INIT(ifp);
635 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
636 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
637 ifp->if_afdata_initialized = 0;
638 IF_AFDATA_LOCK_INIT(ifp);
639 CK_STAILQ_INIT(&ifp->if_addrhead);
640 CK_STAILQ_INIT(&ifp->if_multiaddrs);
641 CK_STAILQ_INIT(&ifp->if_groups);
645 ifq_init(&ifp->if_snd, ifp);
647 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
648 for (int i = 0; i < IFCOUNTERS; i++)
649 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
650 ifp->if_get_counter = if_get_counter_default;
651 ifp->if_pcp = IFNET_PCP_NONE;
652 ifnet_setbyindex(ifp->if_index, ifp);
657 if_alloc_dev(u_char type, device_t dev)
661 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
662 return (if_alloc_domain(type, IF_NODOM));
663 return (if_alloc_domain(type, numa_domain));
667 if_alloc(u_char type)
670 return (if_alloc_domain(type, IF_NODOM));
673 * Do the actual work of freeing a struct ifnet, and layer 2 common
674 * structure. This call is made when the last reference to an
675 * interface is released.
678 if_free_internal(struct ifnet *ifp)
681 KASSERT((ifp->if_flags & IFF_DYING),
682 ("if_free_internal: interface not dying"));
684 if (if_com_free[ifp->if_alloctype] != NULL)
685 if_com_free[ifp->if_alloctype](ifp->if_l2com,
689 mac_ifnet_destroy(ifp);
691 IF_AFDATA_DESTROY(ifp);
692 IF_ADDR_LOCK_DESTROY(ifp);
693 ifq_delete(&ifp->if_snd);
695 for (int i = 0; i < IFCOUNTERS; i++)
696 counter_u64_free(ifp->if_counters[i]);
698 free(ifp->if_description, M_IFDESCR);
699 free(ifp->if_hw_addr, M_IFADDR);
704 if_destroy(epoch_context_t ctx)
708 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
709 if_free_internal(ifp);
713 * Deregister an interface and free the associated storage.
716 if_free(struct ifnet *ifp)
719 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
721 CURVNET_SET_QUIET(ifp->if_vnet);
723 KASSERT(ifp == ifnet_byindex(ifp->if_index),
724 ("%s: freeing unallocated ifnet", ifp->if_xname));
726 ifindex_free_locked(ifp->if_index);
729 if (refcount_release(&ifp->if_refcount))
730 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
735 * Interfaces to keep an ifnet type-stable despite the possibility of the
736 * driver calling if_free(). If there are additional references, we defer
737 * freeing the underlying data structure.
740 if_ref(struct ifnet *ifp)
743 /* We don't assert the ifnet list lock here, but arguably should. */
744 refcount_acquire(&ifp->if_refcount);
748 if_rele(struct ifnet *ifp)
751 if (!refcount_release(&ifp->if_refcount))
753 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
757 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
760 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
762 if (ifq->ifq_maxlen == 0)
763 ifq->ifq_maxlen = ifqmaxlen;
766 ifq->altq_disc = NULL;
767 ifq->altq_flags &= ALTQF_CANTCHANGE;
768 ifq->altq_tbr = NULL;
773 ifq_delete(struct ifaltq *ifq)
775 mtx_destroy(&ifq->ifq_mtx);
779 * Perform generic interface initialization tasks and attach the interface
780 * to the list of "active" interfaces. If vmove flag is set on entry
781 * to if_attach_internal(), perform only a limited subset of initialization
782 * tasks, given that we are moving from one vnet to another an ifnet which
783 * has already been fully initialized.
785 * Note that if_detach_internal() removes group membership unconditionally
786 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
787 * Thus, when if_vmove() is applied to a cloned interface, group membership
788 * is lost while a cloned one always joins a group whose name is
789 * ifc->ifc_name. To recover this after if_detach_internal() and
790 * if_attach_internal(), the cloner should be specified to
791 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
792 * attempts to join a group whose name is ifc->ifc_name.
795 * - The decision to return void and thus require this function to
796 * succeed is questionable.
797 * - We should probably do more sanity checking. For instance we don't
798 * do anything to insure if_xname is unique or non-empty.
801 if_attach(struct ifnet *ifp)
804 if_attach_internal(ifp, 0, NULL);
808 * Compute the least common TSO limit.
811 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
814 * 1) If there is no limit currently, take the limit from
815 * the network adapter.
817 * 2) If the network adapter has a limit below the current
820 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
821 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
822 pmax->tsomaxbytes = ifp->if_hw_tsomax;
824 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
825 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
826 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
828 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
829 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
830 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
835 * Update TSO limit of a network adapter.
837 * Returns zero if no change. Else non-zero.
840 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
843 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
844 ifp->if_hw_tsomax = pmax->tsomaxbytes;
847 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
848 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
851 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
852 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
859 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
861 unsigned socksize, ifasize;
862 int namelen, masklen;
863 struct sockaddr_dl *sdl;
866 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
867 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
871 ifp->if_vnet = curvnet;
872 if (ifp->if_home_vnet == NULL)
873 ifp->if_home_vnet = curvnet;
876 if_addgroup(ifp, IFG_ALL);
878 /* Restore group membership for cloned interfaces. */
879 if (vmove && ifc != NULL)
880 if_clone_addgroup(ifp, ifc);
882 getmicrotime(&ifp->if_lastchange);
883 ifp->if_epoch = time_uptime;
885 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
886 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
887 ("transmit and qflush must both either be set or both be NULL"));
888 if (ifp->if_transmit == NULL) {
889 ifp->if_transmit = if_transmit;
890 ifp->if_qflush = if_qflush;
892 if (ifp->if_input == NULL)
893 ifp->if_input = if_input_default;
895 if (ifp->if_requestencap == NULL)
896 ifp->if_requestencap = if_requestencap_default;
900 mac_ifnet_create(ifp);
904 * Create a Link Level name for this device.
906 namelen = strlen(ifp->if_xname);
908 * Always save enough space for any possiable name so we
909 * can do a rename in place later.
911 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
912 socksize = masklen + ifp->if_addrlen;
913 if (socksize < sizeof(*sdl))
914 socksize = sizeof(*sdl);
915 socksize = roundup2(socksize, sizeof(long));
916 ifasize = sizeof(*ifa) + 2 * socksize;
917 ifa = ifa_alloc(ifasize, M_WAITOK);
918 sdl = (struct sockaddr_dl *)(ifa + 1);
919 sdl->sdl_len = socksize;
920 sdl->sdl_family = AF_LINK;
921 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
922 sdl->sdl_nlen = namelen;
923 sdl->sdl_index = ifp->if_index;
924 sdl->sdl_type = ifp->if_type;
927 ifa->ifa_addr = (struct sockaddr *)sdl;
928 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
929 ifa->ifa_netmask = (struct sockaddr *)sdl;
930 sdl->sdl_len = masklen;
932 sdl->sdl_data[--namelen] = 0xff;
933 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
934 /* Reliably crash if used uninitialized. */
935 ifp->if_broadcastaddr = NULL;
937 if (ifp->if_type == IFT_ETHER) {
938 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
942 #if defined(INET) || defined(INET6)
943 /* Use defaults for TSO, if nothing is set */
944 if (ifp->if_hw_tsomax == 0 &&
945 ifp->if_hw_tsomaxsegcount == 0 &&
946 ifp->if_hw_tsomaxsegsize == 0) {
948 * The TSO defaults needs to be such that an
949 * NFS mbuf list of 35 mbufs totalling just
950 * below 64K works and that a chain of mbufs
951 * can be defragged into at most 32 segments:
953 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
954 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
955 ifp->if_hw_tsomaxsegcount = 35;
956 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
958 /* XXX some drivers set IFCAP_TSO after ethernet attach */
959 if (ifp->if_capabilities & IFCAP_TSO) {
960 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
962 ifp->if_hw_tsomaxsegcount,
963 ifp->if_hw_tsomaxsegsize);
971 * Update the interface index in the link layer address
974 for (ifa = ifp->if_addr; ifa != NULL;
975 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
976 if (ifa->ifa_addr->sa_family == AF_LINK) {
977 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
978 sdl->sdl_index = ifp->if_index;
986 if (domain_init_status >= 2)
987 if_attachdomain1(ifp);
989 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
990 if (IS_DEFAULT_VNET(curvnet))
991 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
993 /* Announce the interface. */
994 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
998 if_epochalloc(void *dummy __unused)
1001 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
1003 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
1006 if_attachdomain(void *dummy)
1010 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
1011 if_attachdomain1(ifp);
1013 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
1014 if_attachdomain, NULL);
1017 if_attachdomain1(struct ifnet *ifp)
1022 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1023 * cannot lock ifp->if_afdata initialization, entirely.
1025 IF_AFDATA_LOCK(ifp);
1026 if (ifp->if_afdata_initialized >= domain_init_status) {
1027 IF_AFDATA_UNLOCK(ifp);
1028 log(LOG_WARNING, "%s called more than once on %s\n",
1029 __func__, ifp->if_xname);
1032 ifp->if_afdata_initialized = domain_init_status;
1033 IF_AFDATA_UNLOCK(ifp);
1035 /* address family dependent data region */
1036 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1037 for (dp = domains; dp; dp = dp->dom_next) {
1038 if (dp->dom_ifattach)
1039 ifp->if_afdata[dp->dom_family] =
1040 (*dp->dom_ifattach)(ifp);
1045 * Remove any unicast or broadcast network addresses from an interface.
1048 if_purgeaddrs(struct ifnet *ifp)
1053 struct epoch_tracker et;
1055 NET_EPOCH_ENTER(et);
1056 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1057 if (ifa->ifa_addr->sa_family != AF_LINK)
1065 /* XXX: Ugly!! ad hoc just for INET */
1066 if (ifa->ifa_addr->sa_family == AF_INET) {
1067 struct ifaliasreq ifr;
1069 bzero(&ifr, sizeof(ifr));
1070 ifr.ifra_addr = *ifa->ifa_addr;
1071 if (ifa->ifa_dstaddr)
1072 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1073 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1079 if (ifa->ifa_addr->sa_family == AF_INET6) {
1081 /* ifp_addrhead is already updated */
1086 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1087 IF_ADDR_WUNLOCK(ifp);
1093 * Remove any multicast network addresses from an interface when an ifnet
1097 if_purgemaddrs(struct ifnet *ifp)
1099 struct ifmultiaddr *ifma;
1102 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1103 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1104 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1105 if_delmulti_locked(ifp, ifma, 1);
1107 IF_ADDR_WUNLOCK(ifp);
1111 * Detach an interface, removing it from the list of "active" interfaces.
1112 * If vmove flag is set on entry to if_detach_internal(), perform only a
1113 * limited subset of cleanup tasks, given that we are moving an ifnet from
1114 * one vnet to another, where it must be fully operational.
1116 * XXXRW: There are some significant questions about event ordering, and
1117 * how to prevent things from starting to use the interface during detach.
1120 if_detach(struct ifnet *ifp)
1124 CURVNET_SET_QUIET(ifp->if_vnet);
1125 found = if_unlink_ifnet(ifp, false);
1127 sx_slock(&ifnet_detach_sxlock);
1128 if_detach_internal(ifp, 0, NULL);
1129 sx_sunlock(&ifnet_detach_sxlock);
1135 * The vmove flag, if set, indicates that we are called from a callpath
1136 * that is moving an interface to a different vnet instance.
1138 * The shutdown flag, if set, indicates that we are called in the
1139 * process of shutting down a vnet instance. Currently only the
1140 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1141 * on a vnet instance shutdown without this flag being set, e.g., when
1142 * the cloned interfaces are destoyed as first thing of teardown.
1145 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1153 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1157 * At this point we know the interface still was on the ifnet list
1158 * and we removed it so we are in a stable state.
1160 epoch_wait_preempt(net_epoch_preempt);
1163 * Ensure all pending EPOCH(9) callbacks have been executed. This
1164 * fixes issues about late destruction of multicast options
1165 * which lead to leave group calls, which in turn access the
1166 * belonging ifnet structure:
1168 epoch_drain_callbacks(net_epoch_preempt);
1171 * In any case (destroy or vmove) detach us from the groups
1172 * and remove/wait for pending events on the taskq.
1173 * XXX-BZ in theory an interface could still enqueue a taskq change?
1177 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1178 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1181 * Check if this is a cloned interface or not. Must do even if
1182 * shutting down as a if_vmove_reclaim() would move the ifp and
1183 * the if_clone_addgroup() will have a corrupted string overwise
1184 * from a gibberish pointer.
1186 if (vmove && ifcp != NULL)
1187 *ifcp = if_clone_findifc(ifp);
1193 * On VNET shutdown abort here as the stack teardown will do all
1194 * the work top-down for us.
1197 /* Give interface users the chance to clean up. */
1198 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1201 * In case of a vmove we are done here without error.
1202 * If we would signal an error it would lead to the same
1203 * abort as if we did not find the ifnet anymore.
1204 * if_detach() calls us in void context and does not care
1205 * about an early abort notification, so life is splendid :)
1207 goto finish_vnet_shutdown;
1212 * At this point we are not tearing down a VNET and are either
1213 * going to destroy or vmove the interface and have to cleanup
1218 * Remove routes and flush queues.
1221 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1222 altq_disable(&ifp->if_snd);
1223 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1224 altq_detach(&ifp->if_snd);
1235 * Remove all IPv6 kernel structs related to ifp. This should be done
1236 * before removing routing entries below, since IPv6 interface direct
1237 * routes are expected to be removed by the IPv6-specific kernel API.
1238 * Otherwise, the kernel will detect some inconsistency and bark it.
1242 if_purgemaddrs(ifp);
1244 /* Announce that the interface is gone. */
1245 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1246 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1247 if (IS_DEFAULT_VNET(curvnet))
1248 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1252 * Prevent further calls into the device driver via ifnet.
1257 * Clean up all addresses.
1260 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1261 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1262 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1263 IF_ADDR_WUNLOCK(ifp);
1266 IF_ADDR_WUNLOCK(ifp);
1269 rt_flushifroutes(ifp);
1272 finish_vnet_shutdown:
1275 * We cannot hold the lock over dom_ifdetach calls as they might
1276 * sleep, for example trying to drain a callout, thus open up the
1277 * theoretical race with re-attaching.
1279 IF_AFDATA_LOCK(ifp);
1280 i = ifp->if_afdata_initialized;
1281 ifp->if_afdata_initialized = 0;
1282 IF_AFDATA_UNLOCK(ifp);
1283 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1284 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1285 (*dp->dom_ifdetach)(ifp,
1286 ifp->if_afdata[dp->dom_family]);
1287 ifp->if_afdata[dp->dom_family] = NULL;
1296 * if_vmove() performs a limited version of if_detach() in current
1297 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1298 * An attempt is made to shrink if_index in current vnet, find an
1299 * unused if_index in target vnet and calls if_grow() if necessary,
1300 * and finally find an unused if_xname for the target vnet.
1303 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1305 struct if_clone *ifc;
1307 u_int bif_dlt, bif_hdrlen;
1314 * if_detach_internal() will call the eventhandler to notify
1315 * interface departure. That will detach if_bpf. We need to
1316 * safe the dlt and hdrlen so we can re-attach it later.
1318 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1322 * Detach from current vnet, but preserve LLADDR info, do not
1323 * mark as dead etc. so that the ifnet can be reattached later.
1324 * If we cannot find it, we lost the race to someone else.
1326 rc = if_detach_internal(ifp, 1, &ifc);
1331 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1332 * the if_index for that vnet if possible.
1334 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1335 * or we'd lock on one vnet and unlock on another.
1338 ifindex_free_locked(ifp->if_index);
1342 * Perform interface-specific reassignment tasks, if provided by
1345 if (ifp->if_reassign != NULL)
1346 ifp->if_reassign(ifp, new_vnet, NULL);
1349 * Switch to the context of the target vnet.
1351 CURVNET_SET_QUIET(new_vnet);
1354 ifp->if_index = ifindex_alloc(&old);
1355 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1357 epoch_wait_preempt(net_epoch_preempt);
1361 ifnet_setbyindex(ifp->if_index, ifp);
1364 if_attach_internal(ifp, 1, ifc);
1367 if (ifp->if_bpf == NULL)
1368 bpfattach(ifp, bif_dlt, bif_hdrlen);
1376 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1379 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1387 /* Try to find the prison within our visibility. */
1388 sx_slock(&allprison_lock);
1389 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1390 sx_sunlock(&allprison_lock);
1393 prison_hold_locked(pr);
1394 mtx_unlock(&pr->pr_mtx);
1396 /* Do not try to move the iface from and to the same prison. */
1397 if (pr->pr_vnet == ifp->if_vnet) {
1402 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1403 /* XXX Lock interfaces to avoid races. */
1404 CURVNET_SET_QUIET(pr->pr_vnet);
1405 difp = ifunit(ifname);
1412 /* Make sure the VNET is stable. */
1413 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1421 found = if_unlink_ifnet(ifp, true);
1424 /* Move the interface into the child jail/vnet. */
1425 error = if_vmove(ifp, pr->pr_vnet);
1427 /* Report the new if_xname back to the userland on success. */
1429 sprintf(ifname, "%s", ifp->if_xname);
1436 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1439 struct vnet *vnet_dst;
1444 /* Try to find the prison within our visibility. */
1445 sx_slock(&allprison_lock);
1446 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1447 sx_sunlock(&allprison_lock);
1450 prison_hold_locked(pr);
1451 mtx_unlock(&pr->pr_mtx);
1453 /* Make sure the named iface exists in the source prison/vnet. */
1454 CURVNET_SET(pr->pr_vnet);
1455 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1462 /* Do not try to move the iface from and to the same prison. */
1463 vnet_dst = TD_TO_VNET(td);
1464 if (vnet_dst == ifp->if_vnet) {
1470 /* Make sure the VNET is stable. */
1471 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1478 /* Get interface back from child jail/vnet. */
1479 found = if_unlink_ifnet(ifp, true);
1481 error = if_vmove(ifp, vnet_dst);
1484 /* Report the new if_xname back to the userland on success. */
1486 sprintf(ifname, "%s", ifp->if_xname);
1494 * Add a group to an interface
1497 if_addgroup(struct ifnet *ifp, const char *groupname)
1499 struct ifg_list *ifgl;
1500 struct ifg_group *ifg = NULL;
1501 struct ifg_member *ifgm;
1504 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1505 groupname[strlen(groupname) - 1] <= '9')
1509 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1510 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1515 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1520 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1526 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1527 if (!strcmp(ifg->ifg_group, groupname))
1531 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1537 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1538 ifg->ifg_refcnt = 0;
1539 CK_STAILQ_INIT(&ifg->ifg_members);
1540 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1545 ifgl->ifgl_group = ifg;
1546 ifgm->ifgm_ifp = ifp;
1549 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1550 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1551 IF_ADDR_WUNLOCK(ifp);
1556 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1557 EVENTHANDLER_INVOKE(group_change_event, groupname);
1563 * Helper function to remove a group out of an interface. Expects the global
1564 * ifnet lock to be write-locked, and drops it before returning.
1567 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1568 const char *groupname)
1570 struct ifg_member *ifgm;
1573 IFNET_WLOCK_ASSERT();
1576 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1577 IF_ADDR_WUNLOCK(ifp);
1579 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1580 if (ifgm->ifgm_ifp == ifp) {
1581 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1582 ifg_member, ifgm_next);
1587 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1588 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1596 epoch_wait_preempt(net_epoch_preempt);
1598 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1599 free(ifgl->ifgl_group, M_TEMP);
1604 EVENTHANDLER_INVOKE(group_change_event, groupname);
1608 * Remove a group from an interface
1611 if_delgroup(struct ifnet *ifp, const char *groupname)
1613 struct ifg_list *ifgl;
1616 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1617 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1624 _if_delgroup_locked(ifp, ifgl, groupname);
1630 * Remove an interface from all groups
1633 if_delgroups(struct ifnet *ifp)
1635 struct ifg_list *ifgl;
1636 char groupname[IFNAMSIZ];
1639 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1640 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1641 _if_delgroup_locked(ifp, ifgl, groupname);
1648 ifgr_group_get(void *ifgrp)
1650 union ifgroupreq_union *ifgrup;
1653 #ifdef COMPAT_FREEBSD32
1654 if (SV_CURPROC_FLAG(SV_ILP32))
1655 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1657 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1660 static struct ifg_req *
1661 ifgr_groups_get(void *ifgrp)
1663 union ifgroupreq_union *ifgrup;
1666 #ifdef COMPAT_FREEBSD32
1667 if (SV_CURPROC_FLAG(SV_ILP32))
1668 return ((struct ifg_req *)(uintptr_t)
1669 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1671 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1675 * Stores all groups from an interface in memory pointed to by ifgr.
1678 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1681 struct ifg_list *ifgl;
1682 struct ifg_req ifgrq, *ifgp;
1686 if (ifgr->ifgr_len == 0) {
1687 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1688 ifgr->ifgr_len += sizeof(struct ifg_req);
1692 len = ifgr->ifgr_len;
1693 ifgp = ifgr_groups_get(ifgr);
1695 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1696 if (len < sizeof(ifgrq))
1698 bzero(&ifgrq, sizeof ifgrq);
1699 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1700 sizeof(ifgrq.ifgrq_group));
1701 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1703 len -= sizeof(ifgrq);
1711 * Stores all members of a group in memory pointed to by igfr
1714 if_getgroupmembers(struct ifgroupreq *ifgr)
1716 struct ifg_group *ifg;
1717 struct ifg_member *ifgm;
1718 struct ifg_req ifgrq, *ifgp;
1722 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1723 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1730 if (ifgr->ifgr_len == 0) {
1731 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1732 ifgr->ifgr_len += sizeof(ifgrq);
1737 len = ifgr->ifgr_len;
1738 ifgp = ifgr_groups_get(ifgr);
1739 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1740 if (len < sizeof(ifgrq)) {
1744 bzero(&ifgrq, sizeof ifgrq);
1745 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1746 sizeof(ifgrq.ifgrq_member));
1747 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1751 len -= sizeof(ifgrq);
1760 * Return counter values from counter(9)s stored in ifnet.
1763 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1766 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1768 return (counter_u64_fetch(ifp->if_counters[cnt]));
1772 * Increase an ifnet counter. Usually used for counters shared
1773 * between the stack and a driver, but function supports them all.
1776 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1779 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1781 counter_u64_add(ifp->if_counters[cnt], inc);
1785 * Copy data from ifnet to userland API structure if_data.
1788 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1791 ifd->ifi_type = ifp->if_type;
1792 ifd->ifi_physical = 0;
1793 ifd->ifi_addrlen = ifp->if_addrlen;
1794 ifd->ifi_hdrlen = ifp->if_hdrlen;
1795 ifd->ifi_link_state = ifp->if_link_state;
1797 ifd->ifi_datalen = sizeof(struct if_data);
1798 ifd->ifi_mtu = ifp->if_mtu;
1799 ifd->ifi_metric = ifp->if_metric;
1800 ifd->ifi_baudrate = ifp->if_baudrate;
1801 ifd->ifi_hwassist = ifp->if_hwassist;
1802 ifd->ifi_epoch = ifp->if_epoch;
1803 ifd->ifi_lastchange = ifp->if_lastchange;
1805 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1806 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1807 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1808 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1809 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1810 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1811 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1812 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1813 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1814 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1815 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1816 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1820 * Initialization, destruction and refcounting functions for ifaddrs.
1823 ifa_alloc(size_t size, int flags)
1827 KASSERT(size >= sizeof(struct ifaddr),
1828 ("%s: invalid size %zu", __func__, size));
1830 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1834 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1836 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1838 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1840 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1843 refcount_init(&ifa->ifa_refcnt, 1);
1848 /* free(NULL) is okay */
1849 counter_u64_free(ifa->ifa_opackets);
1850 counter_u64_free(ifa->ifa_ipackets);
1851 counter_u64_free(ifa->ifa_obytes);
1852 counter_u64_free(ifa->ifa_ibytes);
1853 free(ifa, M_IFADDR);
1859 ifa_ref(struct ifaddr *ifa)
1862 refcount_acquire(&ifa->ifa_refcnt);
1866 ifa_destroy(epoch_context_t ctx)
1870 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1871 counter_u64_free(ifa->ifa_opackets);
1872 counter_u64_free(ifa->ifa_ipackets);
1873 counter_u64_free(ifa->ifa_obytes);
1874 counter_u64_free(ifa->ifa_ibytes);
1875 free(ifa, M_IFADDR);
1879 ifa_free(struct ifaddr *ifa)
1882 if (refcount_release(&ifa->ifa_refcnt))
1883 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1887 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1888 * structs used to represent other address families, it is necessary
1889 * to perform a different comparison.
1892 #define sa_dl_equal(a1, a2) \
1893 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1894 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1895 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1896 CLLADDR((const struct sockaddr_dl *)(a2)), \
1897 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1900 * Locate an interface based on a complete address.
1904 ifa_ifwithaddr(const struct sockaddr *addr)
1911 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1912 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1913 if (ifa->ifa_addr->sa_family != addr->sa_family)
1915 if (sa_equal(addr, ifa->ifa_addr)) {
1918 /* IP6 doesn't have broadcast */
1919 if ((ifp->if_flags & IFF_BROADCAST) &&
1920 ifa->ifa_broadaddr &&
1921 ifa->ifa_broadaddr->sa_len != 0 &&
1922 sa_equal(ifa->ifa_broadaddr, addr)) {
1933 ifa_ifwithaddr_check(const struct sockaddr *addr)
1935 struct epoch_tracker et;
1938 NET_EPOCH_ENTER(et);
1939 rc = (ifa_ifwithaddr(addr) != NULL);
1945 * Locate an interface based on the broadcast address.
1949 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1955 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1956 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1958 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1959 if (ifa->ifa_addr->sa_family != addr->sa_family)
1961 if ((ifp->if_flags & IFF_BROADCAST) &&
1962 ifa->ifa_broadaddr &&
1963 ifa->ifa_broadaddr->sa_len != 0 &&
1964 sa_equal(ifa->ifa_broadaddr, addr)) {
1975 * Locate the point to point interface with a given destination address.
1979 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1985 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1986 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1988 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1990 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1991 if (ifa->ifa_addr->sa_family != addr->sa_family)
1993 if (ifa->ifa_dstaddr != NULL &&
1994 sa_equal(addr, ifa->ifa_dstaddr)) {
2005 * Find an interface on a specific network. If many, choice
2006 * is most specific found.
2009 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2013 struct ifaddr *ifa_maybe = NULL;
2014 u_int af = addr->sa_family;
2015 const char *addr_data = addr->sa_data, *cplim;
2019 * AF_LINK addresses can be looked up directly by their index number,
2020 * so do that if we can.
2022 if (af == AF_LINK) {
2023 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2024 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2025 return (ifaddr_byindex(sdl->sdl_index));
2029 * Scan though each interface, looking for ones that have addresses
2030 * in this address family and the requested fib.
2032 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2033 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2035 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2036 const char *cp, *cp2, *cp3;
2038 if (ifa->ifa_addr->sa_family != af)
2040 if (af == AF_INET &&
2041 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2043 * This is a bit broken as it doesn't
2044 * take into account that the remote end may
2045 * be a single node in the network we are
2047 * The trouble is that we don't know the
2048 * netmask for the remote end.
2050 if (ifa->ifa_dstaddr != NULL &&
2051 sa_equal(addr, ifa->ifa_dstaddr)) {
2056 * Scan all the bits in the ifa's address.
2057 * If a bit dissagrees with what we are
2058 * looking for, mask it with the netmask
2059 * to see if it really matters.
2060 * (A byte at a time)
2062 if (ifa->ifa_netmask == 0)
2065 cp2 = ifa->ifa_addr->sa_data;
2066 cp3 = ifa->ifa_netmask->sa_data;
2067 cplim = ifa->ifa_netmask->sa_len
2068 + (char *)ifa->ifa_netmask;
2070 if ((*cp++ ^ *cp2++) & *cp3++)
2071 goto next; /* next address! */
2073 * If the netmask of what we just found
2074 * is more specific than what we had before
2075 * (if we had one), or if the virtual status
2076 * of new prefix is better than of the old one,
2077 * then remember the new one before continuing
2078 * to search for an even better one.
2080 if (ifa_maybe == NULL ||
2081 ifa_preferred(ifa_maybe, ifa) ||
2082 rn_refines((caddr_t)ifa->ifa_netmask,
2083 (caddr_t)ifa_maybe->ifa_netmask)) {
2096 * Find an interface address specific to an interface best matching
2100 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2103 const char *cp, *cp2, *cp3;
2105 struct ifaddr *ifa_maybe = NULL;
2106 u_int af = addr->sa_family;
2112 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2113 if (ifa->ifa_addr->sa_family != af)
2115 if (ifa_maybe == NULL)
2117 if (ifa->ifa_netmask == 0) {
2118 if (sa_equal(addr, ifa->ifa_addr) ||
2119 (ifa->ifa_dstaddr &&
2120 sa_equal(addr, ifa->ifa_dstaddr)))
2124 if (ifp->if_flags & IFF_POINTOPOINT) {
2125 if (sa_equal(addr, ifa->ifa_dstaddr))
2129 cp2 = ifa->ifa_addr->sa_data;
2130 cp3 = ifa->ifa_netmask->sa_data;
2131 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2132 for (; cp3 < cplim; cp3++)
2133 if ((*cp++ ^ *cp2++) & *cp3)
2145 * See whether new ifa is better than current one:
2146 * 1) A non-virtual one is preferred over virtual.
2147 * 2) A virtual in master state preferred over any other state.
2149 * Used in several address selecting functions.
2152 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2155 return (cur->ifa_carp && (!next->ifa_carp ||
2156 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2159 struct sockaddr_dl *
2160 link_alloc_sdl(size_t size, int flags)
2163 return (malloc(size, M_TEMP, flags));
2167 link_free_sdl(struct sockaddr *sa)
2173 * Fills in given sdl with interface basic info.
2174 * Returns pointer to filled sdl.
2176 struct sockaddr_dl *
2177 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2179 struct sockaddr_dl *sdl;
2181 sdl = (struct sockaddr_dl *)paddr;
2182 memset(sdl, 0, sizeof(struct sockaddr_dl));
2183 sdl->sdl_len = sizeof(struct sockaddr_dl);
2184 sdl->sdl_family = AF_LINK;
2185 sdl->sdl_index = ifp->if_index;
2186 sdl->sdl_type = iftype;
2192 * Mark an interface down and notify protocols of
2196 if_unroute(struct ifnet *ifp, int flag, int fam)
2200 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2202 ifp->if_flags &= ~flag;
2203 getmicrotime(&ifp->if_lastchange);
2204 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2205 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2206 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2207 ifp->if_qflush(ifp);
2210 (*carp_linkstate_p)(ifp);
2215 * Mark an interface up and notify protocols of
2219 if_route(struct ifnet *ifp, int flag, int fam)
2223 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2225 ifp->if_flags |= flag;
2226 getmicrotime(&ifp->if_lastchange);
2227 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2228 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2229 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2231 (*carp_linkstate_p)(ifp);
2238 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2239 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2240 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2241 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2242 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2243 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2244 int (*vlan_setcookie_p)(struct ifnet *, void *);
2245 void *(*vlan_cookie_p)(struct ifnet *);
2248 * Handle a change in the interface link state. To avoid LORs
2249 * between driver lock and upper layer locks, as well as possible
2250 * recursions, we post event to taskqueue, and all job
2251 * is done in static do_link_state_change().
2254 if_link_state_change(struct ifnet *ifp, int link_state)
2256 /* Return if state hasn't changed. */
2257 if (ifp->if_link_state == link_state)
2260 ifp->if_link_state = link_state;
2262 /* XXXGL: reference ifp? */
2263 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2267 do_link_state_change(void *arg, int pending)
2273 link_state = ifp->if_link_state;
2275 CURVNET_SET(ifp->if_vnet);
2277 if (ifp->if_vlantrunk != NULL)
2278 (*vlan_link_state_p)(ifp);
2280 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2281 ifp->if_l2com != NULL)
2282 (*ng_ether_link_state_p)(ifp, link_state);
2284 (*carp_linkstate_p)(ifp);
2286 ifp->if_bridge_linkstate(ifp);
2288 (*lagg_linkstate_p)(ifp, link_state);
2290 if (IS_DEFAULT_VNET(curvnet))
2291 devctl_notify("IFNET", ifp->if_xname,
2292 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2295 if_printf(ifp, "%d link states coalesced\n", pending);
2296 if (log_link_state_change)
2297 if_printf(ifp, "link state changed to %s\n",
2298 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2299 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2304 * Mark an interface down and notify protocols of
2308 if_down(struct ifnet *ifp)
2311 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2312 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2316 * Mark an interface up and notify protocols of
2320 if_up(struct ifnet *ifp)
2323 if_route(ifp, IFF_UP, AF_UNSPEC);
2324 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2328 * Flush an interface queue.
2331 if_qflush(struct ifnet *ifp)
2339 if (ALTQ_IS_ENABLED(ifq))
2343 while ((m = n) != NULL) {
2354 * Map interface name to interface structure pointer, with or without
2355 * returning a reference.
2358 ifunit_ref(const char *name)
2360 struct epoch_tracker et;
2363 NET_EPOCH_ENTER(et);
2364 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2365 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2366 !(ifp->if_flags & IFF_DYING))
2376 ifunit(const char *name)
2378 struct epoch_tracker et;
2381 NET_EPOCH_ENTER(et);
2382 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2383 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2391 ifr_buffer_get_buffer(void *data)
2393 union ifreq_union *ifrup;
2396 #ifdef COMPAT_FREEBSD32
2397 if (SV_CURPROC_FLAG(SV_ILP32))
2398 return ((void *)(uintptr_t)
2399 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2401 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2405 ifr_buffer_set_buffer_null(void *data)
2407 union ifreq_union *ifrup;
2410 #ifdef COMPAT_FREEBSD32
2411 if (SV_CURPROC_FLAG(SV_ILP32))
2412 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2415 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2419 ifr_buffer_get_length(void *data)
2421 union ifreq_union *ifrup;
2424 #ifdef COMPAT_FREEBSD32
2425 if (SV_CURPROC_FLAG(SV_ILP32))
2426 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2428 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2432 ifr_buffer_set_length(void *data, size_t len)
2434 union ifreq_union *ifrup;
2437 #ifdef COMPAT_FREEBSD32
2438 if (SV_CURPROC_FLAG(SV_ILP32))
2439 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2442 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2446 ifr_data_get_ptr(void *ifrp)
2448 union ifreq_union *ifrup;
2451 #ifdef COMPAT_FREEBSD32
2452 if (SV_CURPROC_FLAG(SV_ILP32))
2453 return ((void *)(uintptr_t)
2454 ifrup->ifr32.ifr_ifru.ifru_data);
2456 return (ifrup->ifr.ifr_ifru.ifru_data);
2460 * Hardware specific interface ioctls.
2463 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2466 int error = 0, do_ifup = 0;
2467 int new_flags, temp_flags;
2468 size_t namelen, onamelen;
2470 char *descrbuf, *odescrbuf;
2471 char new_name[IFNAMSIZ];
2473 struct sockaddr_dl *sdl;
2475 ifr = (struct ifreq *)data;
2478 ifr->ifr_index = ifp->if_index;
2482 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2483 ifr->ifr_flags = temp_flags & 0xffff;
2484 ifr->ifr_flagshigh = temp_flags >> 16;
2488 ifr->ifr_reqcap = ifp->if_capabilities;
2489 ifr->ifr_curcap = ifp->if_capenable;
2496 /* Ensure uninitialised padding is not leaked. */
2497 memset(&ifd, 0, sizeof(ifd));
2499 if_data_copy(ifp, &ifd);
2500 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2506 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2511 ifr->ifr_metric = ifp->if_metric;
2515 ifr->ifr_mtu = ifp->if_mtu;
2519 /* XXXGL: did this ever worked? */
2525 sx_slock(&ifdescr_sx);
2526 if (ifp->if_description == NULL)
2529 /* space for terminating nul */
2530 descrlen = strlen(ifp->if_description) + 1;
2531 if (ifr_buffer_get_length(ifr) < descrlen)
2532 ifr_buffer_set_buffer_null(ifr);
2534 error = copyout(ifp->if_description,
2535 ifr_buffer_get_buffer(ifr), descrlen);
2536 ifr_buffer_set_length(ifr, descrlen);
2538 sx_sunlock(&ifdescr_sx);
2542 error = priv_check(td, PRIV_NET_SETIFDESCR);
2547 * Copy only (length-1) bytes to make sure that
2548 * if_description is always nul terminated. The
2549 * length parameter is supposed to count the
2550 * terminating nul in.
2552 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2553 return (ENAMETOOLONG);
2554 else if (ifr_buffer_get_length(ifr) == 0)
2557 descrbuf = malloc(ifr_buffer_get_length(ifr),
2558 M_IFDESCR, M_WAITOK | M_ZERO);
2559 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2560 ifr_buffer_get_length(ifr) - 1);
2562 free(descrbuf, M_IFDESCR);
2567 sx_xlock(&ifdescr_sx);
2568 odescrbuf = ifp->if_description;
2569 ifp->if_description = descrbuf;
2570 sx_xunlock(&ifdescr_sx);
2572 getmicrotime(&ifp->if_lastchange);
2573 free(odescrbuf, M_IFDESCR);
2577 ifr->ifr_fib = ifp->if_fib;
2581 error = priv_check(td, PRIV_NET_SETIFFIB);
2584 if (ifr->ifr_fib >= rt_numfibs)
2587 ifp->if_fib = ifr->ifr_fib;
2591 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2595 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2596 * check, so we don't need special handling here yet.
2598 new_flags = (ifr->ifr_flags & 0xffff) |
2599 (ifr->ifr_flagshigh << 16);
2600 if (ifp->if_flags & IFF_UP &&
2601 (new_flags & IFF_UP) == 0) {
2603 } else if (new_flags & IFF_UP &&
2604 (ifp->if_flags & IFF_UP) == 0) {
2607 /* See if permanently promiscuous mode bit is about to flip */
2608 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2609 if (new_flags & IFF_PPROMISC)
2610 ifp->if_flags |= IFF_PROMISC;
2611 else if (ifp->if_pcount == 0)
2612 ifp->if_flags &= ~IFF_PROMISC;
2613 if (log_promisc_mode_change)
2614 if_printf(ifp, "permanently promiscuous mode %s\n",
2615 ((new_flags & IFF_PPROMISC) ?
2616 "enabled" : "disabled"));
2618 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2619 (new_flags &~ IFF_CANTCHANGE);
2620 if (ifp->if_ioctl) {
2621 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2625 getmicrotime(&ifp->if_lastchange);
2629 error = priv_check(td, PRIV_NET_SETIFCAP);
2632 if (ifp->if_ioctl == NULL)
2633 return (EOPNOTSUPP);
2634 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2636 error = (*ifp->if_ioctl)(ifp, cmd, data);
2638 getmicrotime(&ifp->if_lastchange);
2643 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2648 error = priv_check(td, PRIV_NET_SETIFNAME);
2651 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2655 if (new_name[0] == '\0')
2657 if (new_name[IFNAMSIZ-1] != '\0') {
2658 new_name[IFNAMSIZ-1] = '\0';
2659 if (strlen(new_name) == IFNAMSIZ-1)
2662 if (strcmp(new_name, ifp->if_xname) == 0)
2664 if (ifunit(new_name) != NULL)
2668 * XXX: Locking. Nothing else seems to lock if_flags,
2669 * and there are numerous other races with the
2670 * ifunit() checks not being atomic with namespace
2671 * changes (renames, vmoves, if_attach, etc).
2673 ifp->if_flags |= IFF_RENAMING;
2675 /* Announce the departure of the interface. */
2676 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2677 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2679 if_printf(ifp, "changing name to '%s'\n", new_name);
2682 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2684 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2685 namelen = strlen(new_name);
2686 onamelen = sdl->sdl_nlen;
2688 * Move the address if needed. This is safe because we
2689 * allocate space for a name of length IFNAMSIZ when we
2690 * create this in if_attach().
2692 if (namelen != onamelen) {
2693 bcopy(sdl->sdl_data + onamelen,
2694 sdl->sdl_data + namelen, sdl->sdl_alen);
2696 bcopy(new_name, sdl->sdl_data, namelen);
2697 sdl->sdl_nlen = namelen;
2698 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2699 bzero(sdl->sdl_data, onamelen);
2700 while (namelen != 0)
2701 sdl->sdl_data[--namelen] = 0xff;
2702 IF_ADDR_WUNLOCK(ifp);
2704 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2705 /* Announce the return of the interface. */
2706 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2708 ifp->if_flags &= ~IFF_RENAMING;
2713 error = priv_check(td, PRIV_NET_SETIFVNET);
2716 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2721 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2724 ifp->if_metric = ifr->ifr_metric;
2725 getmicrotime(&ifp->if_lastchange);
2729 error = priv_check(td, PRIV_NET_SETIFPHYS);
2732 if (ifp->if_ioctl == NULL)
2733 return (EOPNOTSUPP);
2734 error = (*ifp->if_ioctl)(ifp, cmd, data);
2736 getmicrotime(&ifp->if_lastchange);
2741 u_long oldmtu = ifp->if_mtu;
2743 error = priv_check(td, PRIV_NET_SETIFMTU);
2746 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2748 if (ifp->if_ioctl == NULL)
2749 return (EOPNOTSUPP);
2750 error = (*ifp->if_ioctl)(ifp, cmd, data);
2752 getmicrotime(&ifp->if_lastchange);
2755 DEBUGNET_NOTIFY_MTU(ifp);
2759 * If the link MTU changed, do network layer specific procedure.
2761 if (ifp->if_mtu != oldmtu) {
2772 if (cmd == SIOCADDMULTI)
2773 error = priv_check(td, PRIV_NET_ADDMULTI);
2775 error = priv_check(td, PRIV_NET_DELMULTI);
2779 /* Don't allow group membership on non-multicast interfaces. */
2780 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2781 return (EOPNOTSUPP);
2783 /* Don't let users screw up protocols' entries. */
2784 if (ifr->ifr_addr.sa_family != AF_LINK)
2787 if (cmd == SIOCADDMULTI) {
2788 struct epoch_tracker et;
2789 struct ifmultiaddr *ifma;
2792 * Userland is only permitted to join groups once
2793 * via the if_addmulti() KPI, because it cannot hold
2794 * struct ifmultiaddr * between calls. It may also
2795 * lose a race while we check if the membership
2798 NET_EPOCH_ENTER(et);
2799 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2804 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2806 error = if_delmulti(ifp, &ifr->ifr_addr);
2809 getmicrotime(&ifp->if_lastchange);
2812 case SIOCSIFPHYADDR:
2813 case SIOCDIFPHYADDR:
2815 case SIOCSIFPHYADDR_IN6:
2818 case SIOCSIFGENERIC:
2819 error = priv_check(td, PRIV_NET_HWIOCTL);
2822 if (ifp->if_ioctl == NULL)
2823 return (EOPNOTSUPP);
2824 error = (*ifp->if_ioctl)(ifp, cmd, data);
2826 getmicrotime(&ifp->if_lastchange);
2830 case SIOCGIFPSRCADDR:
2831 case SIOCGIFPDSTADDR:
2834 case SIOCGIFGENERIC:
2836 case SIOCGIFRSSHASH:
2837 case SIOCGIFDOWNREASON:
2838 if (ifp->if_ioctl == NULL)
2839 return (EOPNOTSUPP);
2840 error = (*ifp->if_ioctl)(ifp, cmd, data);
2844 error = priv_check(td, PRIV_NET_SETLLADDR);
2847 error = if_setlladdr(ifp,
2848 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2852 error = if_gethwaddr(ifp, ifr);
2855 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2856 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2859 if ((error = if_addgroup(ifp,
2860 ifgr_group_get((struct ifgroupreq *)data))))
2864 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2866 struct epoch_tracker et;
2868 NET_EPOCH_ENTER(et);
2869 error = if_getgroup((struct ifgroupreq *)data, ifp);
2874 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2875 error = priv_check(td, PRIV_NET_DELIFGROUP);
2878 if ((error = if_delgroup(ifp,
2879 ifgr_group_get((struct ifgroupreq *)data))))
2890 #ifdef COMPAT_FREEBSD32
2898 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2901 #ifdef COMPAT_FREEBSD32
2903 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2905 struct ifmediareq32 *ifmr32;
2907 ifmr32 = (struct ifmediareq32 *)data;
2908 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2909 sizeof(ifmr->ifm_name));
2910 ifmr->ifm_current = ifmr32->ifm_current;
2911 ifmr->ifm_mask = ifmr32->ifm_mask;
2912 ifmr->ifm_status = ifmr32->ifm_status;
2913 ifmr->ifm_active = ifmr32->ifm_active;
2914 ifmr->ifm_count = ifmr32->ifm_count;
2915 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2919 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2921 struct ifmediareq32 *ifmr32;
2923 ifmr32 = (struct ifmediareq32 *)data;
2924 ifmr32->ifm_current = ifmr->ifm_current;
2925 ifmr32->ifm_mask = ifmr->ifm_mask;
2926 ifmr32->ifm_status = ifmr->ifm_status;
2927 ifmr32->ifm_active = ifmr->ifm_active;
2928 ifmr32->ifm_count = ifmr->ifm_count;
2936 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2938 #ifdef COMPAT_FREEBSD32
2939 caddr_t saved_data = NULL;
2940 struct ifmediareq ifmr;
2941 struct ifmediareq *ifmrp = NULL;
2951 CURVNET_SET(so->so_vnet);
2953 /* Make sure the VNET is stable. */
2954 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2963 error = ifconf(cmd, data);
2966 #ifdef COMPAT_FREEBSD32
2969 struct ifconf32 *ifc32;
2972 ifc32 = (struct ifconf32 *)data;
2973 ifc.ifc_len = ifc32->ifc_len;
2974 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2976 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2978 ifc32->ifc_len = ifc.ifc_len;
2984 #ifdef COMPAT_FREEBSD32
2986 case SIOCGIFMEDIA32:
2987 case SIOCGIFXMEDIA32:
2989 ifmr_init(ifmrp, data);
2990 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2992 data = (caddr_t)ifmrp;
2996 ifr = (struct ifreq *)data;
3000 error = priv_check(td, PRIV_NET_SETIFVNET);
3002 error = if_vmove_reclaim(td, ifr->ifr_name,
3008 error = priv_check(td, PRIV_NET_IFCREATE);
3010 error = if_clone_create(ifr->ifr_name,
3011 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3012 ifr_data_get_ptr(ifr) : NULL);
3015 error = priv_check(td, PRIV_NET_IFDESTROY);
3018 sx_slock(&ifnet_detach_sxlock);
3019 error = if_clone_destroy(ifr->ifr_name);
3020 sx_sunlock(&ifnet_detach_sxlock);
3024 case SIOCIFGCLONERS:
3025 error = if_clone_list((struct if_clonereq *)data);
3028 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3029 error = if_getgroupmembers((struct ifgroupreq *)data);
3032 #if defined(INET) || defined(INET6)
3035 if (carp_ioctl_p == NULL)
3036 error = EPROTONOSUPPORT;
3038 error = (*carp_ioctl_p)(ifr, cmd, td);
3043 ifp = ifunit_ref(ifr->ifr_name);
3049 error = ifhwioctl(cmd, ifp, data, td);
3050 if (error != ENOIOCTL)
3053 oif_flags = ifp->if_flags;
3054 if (so->so_proto == NULL) {
3060 * Pass the request on to the socket control method, and if the
3061 * latter returns EOPNOTSUPP, directly to the interface.
3063 * Make an exception for the legacy SIOCSIF* requests. Drivers
3064 * trust SIOCSIFADDR et al to come from an already privileged
3065 * layer, and do not perform any credentials checks or input
3068 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3070 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3071 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3072 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3073 error = (*ifp->if_ioctl)(ifp, cmd, data);
3075 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3077 if (ifp->if_flags & IFF_UP)
3085 #ifdef COMPAT_FREEBSD32
3086 if (ifmrp != NULL) {
3087 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3088 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3091 ifmr_update(ifmrp, data);
3099 * The code common to handling reference counted flags,
3100 * e.g., in ifpromisc() and if_allmulti().
3101 * The "pflag" argument can specify a permanent mode flag to check,
3102 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3104 * Only to be used on stack-owned flags, not driver-owned flags.
3107 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3111 int oldflags, oldcount;
3113 /* Sanity checks to catch programming errors */
3114 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3115 ("%s: setting driver-owned flag %d", __func__, flag));
3118 KASSERT(*refcount >= 0,
3119 ("%s: increment negative refcount %d for flag %d",
3120 __func__, *refcount, flag));
3122 KASSERT(*refcount > 0,
3123 ("%s: decrement non-positive refcount %d for flag %d",
3124 __func__, *refcount, flag));
3126 /* In case this mode is permanent, just touch refcount */
3127 if (ifp->if_flags & pflag) {
3128 *refcount += onswitch ? 1 : -1;
3132 /* Save ifnet parameters for if_ioctl() may fail */
3133 oldcount = *refcount;
3134 oldflags = ifp->if_flags;
3137 * See if we aren't the only and touching refcount is enough.
3138 * Actually toggle interface flag if we are the first or last.
3143 ifp->if_flags |= flag;
3147 ifp->if_flags &= ~flag;
3150 /* Call down the driver since we've changed interface flags */
3151 if (ifp->if_ioctl == NULL) {
3155 ifr.ifr_flags = ifp->if_flags & 0xffff;
3156 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3157 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3160 /* Notify userland that interface flags have changed */
3165 /* Recover after driver error */
3166 *refcount = oldcount;
3167 ifp->if_flags = oldflags;
3172 * Set/clear promiscuous mode on interface ifp based on the truth value
3173 * of pswitch. The calls are reference counted so that only the first
3174 * "on" request actually has an effect, as does the final "off" request.
3175 * Results are undefined if the "off" and "on" requests are not matched.
3178 ifpromisc(struct ifnet *ifp, int pswitch)
3181 int oldflags = ifp->if_flags;
3183 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3184 &ifp->if_pcount, pswitch);
3185 /* If promiscuous mode status has changed, log a message */
3186 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3187 log_promisc_mode_change)
3188 if_printf(ifp, "promiscuous mode %s\n",
3189 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3194 * Return interface configuration
3195 * of system. List may be used
3196 * in later ioctl's (above) to get
3197 * other information.
3201 ifconf(u_long cmd, caddr_t data)
3203 struct ifconf *ifc = (struct ifconf *)data;
3208 int error, full = 0, valid_len, max_len;
3210 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3211 max_len = maxphys - 1;
3213 /* Prevent hostile input from being able to crash the system */
3214 if (ifc->ifc_len <= 0)
3218 if (ifc->ifc_len <= max_len) {
3219 max_len = ifc->ifc_len;
3222 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3227 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3228 struct epoch_tracker et;
3232 * Zero the ifr to make sure we don't disclose the contents
3235 memset(&ifr, 0, sizeof(ifr));
3237 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3238 >= sizeof(ifr.ifr_name)) {
3241 return (ENAMETOOLONG);
3245 NET_EPOCH_ENTER(et);
3246 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3247 struct sockaddr *sa = ifa->ifa_addr;
3249 if (prison_if(curthread->td_ucred, sa) != 0)
3252 if (sa->sa_len <= sizeof(*sa)) {
3253 if (sa->sa_len < sizeof(*sa)) {
3254 memset(&ifr.ifr_ifru.ifru_addr, 0,
3255 sizeof(ifr.ifr_ifru.ifru_addr));
3256 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3259 ifr.ifr_ifru.ifru_addr = *sa;
3260 sbuf_bcat(sb, &ifr, sizeof(ifr));
3261 max_len += sizeof(ifr);
3264 offsetof(struct ifreq, ifr_addr));
3265 max_len += offsetof(struct ifreq, ifr_addr);
3266 sbuf_bcat(sb, sa, sa->sa_len);
3267 max_len += sa->sa_len;
3270 if (sbuf_error(sb) == 0)
3271 valid_len = sbuf_len(sb);
3275 sbuf_bcat(sb, &ifr, sizeof(ifr));
3276 max_len += sizeof(ifr);
3278 if (sbuf_error(sb) == 0)
3279 valid_len = sbuf_len(sb);
3285 * If we didn't allocate enough space (uncommon), try again. If
3286 * we have already allocated as much space as we are allowed,
3287 * return what we've got.
3289 if (valid_len != max_len && !full) {
3294 ifc->ifc_len = valid_len;
3296 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3302 * Just like ifpromisc(), but for all-multicast-reception mode.
3305 if_allmulti(struct ifnet *ifp, int onswitch)
3308 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3311 struct ifmultiaddr *
3312 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3314 struct ifmultiaddr *ifma;
3316 IF_ADDR_LOCK_ASSERT(ifp);
3318 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3319 if (sa->sa_family == AF_LINK) {
3320 if (sa_dl_equal(ifma->ifma_addr, sa))
3323 if (sa_equal(ifma->ifma_addr, sa))
3332 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3333 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3334 * the ifnet multicast address list here, so the caller must do that and
3335 * other setup work (such as notifying the device driver). The reference
3336 * count is initialized to 1.
3338 static struct ifmultiaddr *
3339 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3342 struct ifmultiaddr *ifma;
3343 struct sockaddr *dupsa;
3345 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3350 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3351 if (dupsa == NULL) {
3352 free(ifma, M_IFMADDR);
3355 bcopy(sa, dupsa, sa->sa_len);
3356 ifma->ifma_addr = dupsa;
3358 ifma->ifma_ifp = ifp;
3359 ifma->ifma_refcount = 1;
3360 ifma->ifma_protospec = NULL;
3363 ifma->ifma_lladdr = NULL;
3367 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3368 if (dupsa == NULL) {
3369 free(ifma->ifma_addr, M_IFMADDR);
3370 free(ifma, M_IFMADDR);
3373 bcopy(llsa, dupsa, llsa->sa_len);
3374 ifma->ifma_lladdr = dupsa;
3380 * if_freemulti: free ifmultiaddr structure and possibly attached related
3381 * addresses. The caller is responsible for implementing reference
3382 * counting, notifying the driver, handling routing messages, and releasing
3383 * any dependent link layer state.
3385 #ifdef MCAST_VERBOSE
3386 extern void kdb_backtrace(void);
3389 if_freemulti_internal(struct ifmultiaddr *ifma)
3392 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3393 ifma->ifma_refcount));
3395 if (ifma->ifma_lladdr != NULL)
3396 free(ifma->ifma_lladdr, M_IFMADDR);
3397 #ifdef MCAST_VERBOSE
3399 printf("%s freeing ifma: %p\n", __func__, ifma);
3401 free(ifma->ifma_addr, M_IFMADDR);
3402 free(ifma, M_IFMADDR);
3406 if_destroymulti(epoch_context_t ctx)
3408 struct ifmultiaddr *ifma;
3410 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3411 if_freemulti_internal(ifma);
3415 if_freemulti(struct ifmultiaddr *ifma)
3417 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3418 ifma->ifma_refcount));
3420 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3424 * Register an additional multicast address with a network interface.
3426 * - If the address is already present, bump the reference count on the
3427 * address and return.
3428 * - If the address is not link-layer, look up a link layer address.
3429 * - Allocate address structures for one or both addresses, and attach to the
3430 * multicast address list on the interface. If automatically adding a link
3431 * layer address, the protocol address will own a reference to the link
3432 * layer address, to be freed when it is freed.
3433 * - Notify the network device driver of an addition to the multicast address
3436 * 'sa' points to caller-owned memory with the desired multicast address.
3438 * 'retifma' will be used to return a pointer to the resulting multicast
3439 * address reference, if desired.
3442 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3443 struct ifmultiaddr **retifma)
3445 struct ifmultiaddr *ifma, *ll_ifma;
3446 struct sockaddr *llsa;
3447 struct sockaddr_dl sdl;
3451 IN_MULTI_LIST_UNLOCK_ASSERT();
3454 IN6_MULTI_LIST_UNLOCK_ASSERT();
3457 * If the address is already present, return a new reference to it;
3458 * otherwise, allocate storage and set up a new address.
3461 ifma = if_findmulti(ifp, sa);
3463 ifma->ifma_refcount++;
3464 if (retifma != NULL)
3466 IF_ADDR_WUNLOCK(ifp);
3471 * The address isn't already present; resolve the protocol address
3472 * into a link layer address, and then look that up, bump its
3473 * refcount or allocate an ifma for that also.
3474 * Most link layer resolving functions returns address data which
3475 * fits inside default sockaddr_dl structure. However callback
3476 * can allocate another sockaddr structure, in that case we need to
3481 if (ifp->if_resolvemulti != NULL) {
3482 /* Provide called function with buffer size information */
3483 sdl.sdl_len = sizeof(sdl);
3484 llsa = (struct sockaddr *)&sdl;
3485 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3491 * Allocate the new address. Don't hook it up yet, as we may also
3492 * need to allocate a link layer multicast address.
3494 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3501 * If a link layer address is found, we'll need to see if it's
3502 * already present in the address list, or allocate is as well.
3503 * When this block finishes, the link layer address will be on the
3507 ll_ifma = if_findmulti(ifp, llsa);
3508 if (ll_ifma == NULL) {
3509 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3510 if (ll_ifma == NULL) {
3511 --ifma->ifma_refcount;
3516 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3517 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3520 ll_ifma->ifma_refcount++;
3521 ifma->ifma_llifma = ll_ifma;
3525 * We now have a new multicast address, ifma, and possibly a new or
3526 * referenced link layer address. Add the primary address to the
3527 * ifnet address list.
3529 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3530 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3532 if (retifma != NULL)
3536 * Must generate the message while holding the lock so that 'ifma'
3537 * pointer is still valid.
3539 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3540 IF_ADDR_WUNLOCK(ifp);
3543 * We are certain we have added something, so call down to the
3544 * interface to let them know about it.
3546 if (ifp->if_ioctl != NULL) {
3547 if (THREAD_CAN_SLEEP())
3548 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3550 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3553 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3554 link_free_sdl(llsa);
3559 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3560 link_free_sdl(llsa);
3563 IF_ADDR_WUNLOCK(ifp);
3568 if_siocaddmulti(void *arg, int pending)
3575 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3577 CURVNET_SET(ifp->if_vnet);
3578 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3583 * Delete a multicast group membership by network-layer group address.
3585 * Returns ENOENT if the entry could not be found. If ifp no longer
3586 * exists, results are undefined. This entry point should only be used
3587 * from subsystems which do appropriate locking to hold ifp for the
3588 * duration of the call.
3589 * Network-layer protocol domains must use if_delmulti_ifma().
3592 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3594 struct ifmultiaddr *ifma;
3597 KASSERT(ifp, ("%s: NULL ifp", __func__));
3601 ifma = if_findmulti(ifp, sa);
3603 lastref = if_delmulti_locked(ifp, ifma, 0);
3604 IF_ADDR_WUNLOCK(ifp);
3609 if (lastref && ifp->if_ioctl != NULL) {
3610 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3617 * Delete all multicast group membership for an interface.
3618 * Should be used to quickly flush all multicast filters.
3621 if_delallmulti(struct ifnet *ifp)
3623 struct ifmultiaddr *ifma;
3624 struct ifmultiaddr *next;
3627 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3628 if_delmulti_locked(ifp, ifma, 0);
3629 IF_ADDR_WUNLOCK(ifp);
3633 if_delmulti_ifma(struct ifmultiaddr *ifma)
3635 if_delmulti_ifma_flags(ifma, 0);
3639 * Delete a multicast group membership by group membership pointer.
3640 * Network-layer protocol domains must use this routine.
3642 * It is safe to call this routine if the ifp disappeared.
3645 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3649 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3651 IN_MULTI_LIST_UNLOCK_ASSERT();
3653 ifp = ifma->ifma_ifp;
3656 printf("%s: ifma_ifp seems to be detached\n", __func__);
3658 struct epoch_tracker et;
3661 NET_EPOCH_ENTER(et);
3662 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3671 * If and only if the ifnet instance exists: Acquire the address lock.
3676 lastref = if_delmulti_locked(ifp, ifma, flags);
3680 * If and only if the ifnet instance exists:
3681 * Release the address lock.
3682 * If the group was left: update the hardware hash filter.
3684 IF_ADDR_WUNLOCK(ifp);
3685 if (lastref && ifp->if_ioctl != NULL) {
3686 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3692 * Perform deletion of network-layer and/or link-layer multicast address.
3694 * Return 0 if the reference count was decremented.
3695 * Return 1 if the final reference was released, indicating that the
3696 * hardware hash filter should be reprogrammed.
3699 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3701 struct ifmultiaddr *ll_ifma;
3703 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3704 KASSERT(ifma->ifma_ifp == ifp,
3705 ("%s: inconsistent ifp %p", __func__, ifp));
3706 IF_ADDR_WLOCK_ASSERT(ifp);
3709 ifp = ifma->ifma_ifp;
3710 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3713 * If the ifnet is detaching, null out references to ifnet,
3714 * so that upper protocol layers will notice, and not attempt
3715 * to obtain locks for an ifnet which no longer exists. The
3716 * routing socket announcement must happen before the ifnet
3717 * instance is detached from the system.
3721 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3724 * ifp may already be nulled out if we are being reentered
3725 * to delete the ll_ifma.
3728 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3729 ifma->ifma_ifp = NULL;
3733 if (--ifma->ifma_refcount > 0)
3736 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3737 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3738 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3741 * If this ifma is a network-layer ifma, a link-layer ifma may
3742 * have been associated with it. Release it first if so.
3744 ll_ifma = ifma->ifma_llifma;
3745 if (ll_ifma != NULL) {
3746 KASSERT(ifma->ifma_lladdr != NULL,
3747 ("%s: llifma w/o lladdr", __func__));
3749 ll_ifma->ifma_ifp = NULL; /* XXX */
3750 if (--ll_ifma->ifma_refcount == 0) {
3752 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3753 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3755 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3758 if_freemulti(ll_ifma);
3763 struct ifmultiaddr *ifmatmp;
3765 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3766 MPASS(ifma != ifmatmp);
3771 * The last reference to this instance of struct ifmultiaddr
3772 * was released; the hardware should be notified of this change.
3778 * Set the link layer address on an interface.
3780 * At this time we only support certain types of interfaces,
3781 * and we don't allow the length of the address to change.
3783 * Set noinline to be dtrace-friendly
3786 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3788 struct sockaddr_dl *sdl;
3796 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3800 if (len != sdl->sdl_alen) /* don't allow length to change */
3803 switch (ifp->if_type) {
3808 case IFT_IEEE8023ADLAG:
3809 bcopy(lladdr, LLADDR(sdl), len);
3816 * If the interface is already up, we need
3817 * to re-init it in order to reprogram its
3820 if ((ifp->if_flags & IFF_UP) != 0) {
3821 if (ifp->if_ioctl) {
3822 ifp->if_flags &= ~IFF_UP;
3823 ifr.ifr_flags = ifp->if_flags & 0xffff;
3824 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3825 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3826 ifp->if_flags |= IFF_UP;
3827 ifr.ifr_flags = ifp->if_flags & 0xffff;
3828 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3829 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3832 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3838 * Compat function for handling basic encapsulation requests.
3839 * Not converted stacks (FDDI, IB, ..) supports traditional
3840 * output model: ARP (and other similar L2 protocols) are handled
3841 * inside output routine, arpresolve/nd6_resolve() returns MAC
3842 * address instead of full prepend.
3844 * This function creates calculated header==MAC for IPv4/IPv6 and
3845 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3849 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3852 if (req->rtype != IFENCAP_LL)
3853 return (EOPNOTSUPP);
3855 if (req->bufsize < req->lladdr_len)
3858 switch (req->family) {
3863 return (EAFNOSUPPORT);
3866 /* Copy lladdr to storage as is */
3867 memmove(req->buf, req->lladdr, req->lladdr_len);
3868 req->bufsize = req->lladdr_len;
3869 req->lladdr_off = 0;
3875 * Tunnel interfaces can nest, also they may cause infinite recursion
3876 * calls when misconfigured. We'll prevent this by detecting loops.
3877 * High nesting level may cause stack exhaustion. We'll prevent this
3878 * by introducing upper limit.
3880 * Return 0, if tunnel nesting count is equal or less than limit.
3883 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3891 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3892 if (*(struct ifnet **)(mtag + 1) == ifp) {
3893 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3898 if (count > limit) {
3900 "%s: if_output recursively called too many times(%d)\n",
3901 if_name(ifp), count);
3904 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3907 *(struct ifnet **)(mtag + 1) = ifp;
3908 m_tag_prepend(m, mtag);
3913 * Get the link layer address that was read from the hardware at attach.
3915 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3916 * their component interfaces as IFT_IEEE8023ADLAG.
3919 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3922 if (ifp->if_hw_addr == NULL)
3925 switch (ifp->if_type) {
3927 case IFT_IEEE8023ADLAG:
3928 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3936 * The name argument must be a pointer to storage which will last as
3937 * long as the interface does. For physical devices, the result of
3938 * device_get_name(dev) is a good choice and for pseudo-devices a
3939 * static string works well.
3942 if_initname(struct ifnet *ifp, const char *name, int unit)
3944 ifp->if_dname = name;
3945 ifp->if_dunit = unit;
3946 if (unit != IF_DUNIT_NONE)
3947 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3949 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3953 if_printf(struct ifnet *ifp, const char *fmt, ...)
3958 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3960 vlog(LOG_INFO, if_fmt, ap);
3966 if_start(struct ifnet *ifp)
3969 (*(ifp)->if_start)(ifp);
3973 * Backwards compatibility interface for drivers
3974 * that have not implemented it
3977 if_transmit(struct ifnet *ifp, struct mbuf *m)
3981 IFQ_HANDOFF(ifp, m, error);
3986 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3993 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3998 if (_IF_QFULL(ifq)) {
4000 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4005 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4006 if (m->m_flags & (M_BCAST|M_MCAST))
4007 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4008 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4010 _IF_ENQUEUE(ifq, m);
4012 if (ifp != NULL && !active)
4013 (*(ifp)->if_start)(ifp);
4018 if_register_com_alloc(u_char type,
4019 if_com_alloc_t *a, if_com_free_t *f)
4022 KASSERT(if_com_alloc[type] == NULL,
4023 ("if_register_com_alloc: %d already registered", type));
4024 KASSERT(if_com_free[type] == NULL,
4025 ("if_register_com_alloc: %d free already registered", type));
4027 if_com_alloc[type] = a;
4028 if_com_free[type] = f;
4032 if_deregister_com_alloc(u_char type)
4035 KASSERT(if_com_alloc[type] != NULL,
4036 ("if_deregister_com_alloc: %d not registered", type));
4037 KASSERT(if_com_free[type] != NULL,
4038 ("if_deregister_com_alloc: %d free not registered", type));
4039 if_com_alloc[type] = NULL;
4040 if_com_free[type] = NULL;
4043 /* API for driver access to network stack owned ifnet.*/
4045 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4049 oldbrate = ifp->if_baudrate;
4050 ifp->if_baudrate = baudrate;
4055 if_getbaudrate(if_t ifp)
4058 return (((struct ifnet *)ifp)->if_baudrate);
4062 if_setcapabilities(if_t ifp, int capabilities)
4064 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4069 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4071 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4072 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4078 if_getcapabilities(if_t ifp)
4080 return ((struct ifnet *)ifp)->if_capabilities;
4084 if_setcapenable(if_t ifp, int capabilities)
4086 ((struct ifnet *)ifp)->if_capenable = capabilities;
4091 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4094 ((struct ifnet *)ifp)->if_capenable |= setcap;
4096 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4102 if_getdname(if_t ifp)
4104 return ((struct ifnet *)ifp)->if_dname;
4108 if_togglecapenable(if_t ifp, int togglecap)
4110 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4115 if_getcapenable(if_t ifp)
4117 return ((struct ifnet *)ifp)->if_capenable;
4121 * This is largely undesirable because it ties ifnet to a device, but does
4122 * provide flexiblity for an embedded product vendor. Should be used with
4123 * the understanding that it violates the interface boundaries, and should be
4124 * a last resort only.
4127 if_setdev(if_t ifp, void *dev)
4133 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4135 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4136 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4142 if_getdrvflags(if_t ifp)
4144 return ((struct ifnet *)ifp)->if_drv_flags;
4148 if_setdrvflags(if_t ifp, int flags)
4150 ((struct ifnet *)ifp)->if_drv_flags = flags;
4155 if_setflags(if_t ifp, int flags)
4158 ifp->if_flags = flags;
4163 if_setflagbits(if_t ifp, int set, int clear)
4165 ((struct ifnet *)ifp)->if_flags |= set;
4166 ((struct ifnet *)ifp)->if_flags &= ~clear;
4172 if_getflags(if_t ifp)
4174 return ((struct ifnet *)ifp)->if_flags;
4178 if_clearhwassist(if_t ifp)
4180 ((struct ifnet *)ifp)->if_hwassist = 0;
4185 if_sethwassistbits(if_t ifp, int toset, int toclear)
4187 ((struct ifnet *)ifp)->if_hwassist |= toset;
4188 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4194 if_sethwassist(if_t ifp, int hwassist_bit)
4196 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4201 if_gethwassist(if_t ifp)
4203 return ((struct ifnet *)ifp)->if_hwassist;
4207 if_setmtu(if_t ifp, int mtu)
4209 ((struct ifnet *)ifp)->if_mtu = mtu;
4216 return ((struct ifnet *)ifp)->if_mtu;
4220 if_getmtu_family(if_t ifp, int family)
4224 for (dp = domains; dp; dp = dp->dom_next) {
4225 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4226 return (dp->dom_ifmtu((struct ifnet *)ifp));
4229 return (((struct ifnet *)ifp)->if_mtu);
4233 * Methods for drivers to access interface unicast and multicast
4234 * link level addresses. Driver shall not know 'struct ifaddr' neither
4235 * 'struct ifmultiaddr'.
4238 if_lladdr_count(if_t ifp)
4240 struct epoch_tracker et;
4245 NET_EPOCH_ENTER(et);
4246 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4247 if (ifa->ifa_addr->sa_family == AF_LINK)
4255 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4257 struct epoch_tracker et;
4264 NET_EPOCH_ENTER(et);
4265 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4266 if (ifa->ifa_addr->sa_family != AF_LINK)
4268 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4277 if_llmaddr_count(if_t ifp)
4279 struct epoch_tracker et;
4280 struct ifmultiaddr *ifma;
4284 NET_EPOCH_ENTER(et);
4285 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4286 if (ifma->ifma_addr->sa_family == AF_LINK)
4294 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4296 struct epoch_tracker et;
4297 struct ifmultiaddr *ifma;
4303 NET_EPOCH_ENTER(et);
4304 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4305 if (ifma->ifma_addr->sa_family != AF_LINK)
4307 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4316 if_setsoftc(if_t ifp, void *softc)
4318 ((struct ifnet *)ifp)->if_softc = softc;
4323 if_getsoftc(if_t ifp)
4325 return ((struct ifnet *)ifp)->if_softc;
4329 if_setrcvif(struct mbuf *m, if_t ifp)
4332 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4333 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4337 if_setvtag(struct mbuf *m, uint16_t tag)
4339 m->m_pkthdr.ether_vtag = tag;
4343 if_getvtag(struct mbuf *m)
4346 return (m->m_pkthdr.ether_vtag);
4350 if_sendq_empty(if_t ifp)
4352 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4356 if_getifaddr(if_t ifp)
4358 return ((struct ifnet *)ifp)->if_addr;
4362 if_getamcount(if_t ifp)
4364 return ((struct ifnet *)ifp)->if_amcount;
4368 if_setsendqready(if_t ifp)
4370 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4375 if_setsendqlen(if_t ifp, int tx_desc_count)
4377 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4378 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4384 if_vlantrunkinuse(if_t ifp)
4386 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4390 if_input(if_t ifp, struct mbuf* sendmp)
4392 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4398 if_dequeue(if_t ifp)
4401 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4407 if_sendq_prepend(if_t ifp, struct mbuf *m)
4409 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4414 if_setifheaderlen(if_t ifp, int len)
4416 ((struct ifnet *)ifp)->if_hdrlen = len;
4421 if_getlladdr(if_t ifp)
4423 return (IF_LLADDR((struct ifnet *)ifp));
4427 if_gethandle(u_char type)
4429 return (if_alloc(type));
4433 if_bpfmtap(if_t ifh, struct mbuf *m)
4435 struct ifnet *ifp = (struct ifnet *)ifh;
4441 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4443 struct ifnet *ifp = (struct ifnet *)ifh;
4445 ETHER_BPF_MTAP(ifp, m);
4449 if_vlancap(if_t ifh)
4451 struct ifnet *ifp = (struct ifnet *)ifh;
4452 VLAN_CAPABILITIES(ifp);
4456 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4459 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4464 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4467 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4472 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4475 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4480 if_gethwtsomax(if_t ifp)
4483 return (((struct ifnet *)ifp)->if_hw_tsomax);
4487 if_gethwtsomaxsegcount(if_t ifp)
4490 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4494 if_gethwtsomaxsegsize(if_t ifp)
4497 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4501 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4503 ((struct ifnet *)ifp)->if_init = init_fn;
4507 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4509 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4513 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4515 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4519 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4521 ((struct ifnet *)ifp)->if_transmit = start_fn;
4524 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4526 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4531 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4534 ifp->if_get_counter = fn;
4537 /* Revisit these - These are inline functions originally. */
4539 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4541 return drbr_inuse(ifh, br);
4545 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4547 return drbr_dequeue(ifh, br);
4551 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4553 return drbr_needs_enqueue(ifh, br);
4557 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4559 return drbr_enqueue(ifh, br, m);