2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
45 #include <sys/epoch.h>
47 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/protosw.h>
53 #include <sys/kernel.h>
55 #include <sys/refcount.h>
56 #include <sys/module.h>
57 #include <sys/rwlock.h>
58 #include <sys/sockio.h>
59 #include <sys/syslog.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/taskqueue.h>
63 #include <sys/domain.h>
66 #include <sys/sched.h>
69 #include <machine/stdarg.h>
73 #include <net/ethernet.h>
75 #include <net/if_arp.h>
76 #include <net/if_clone.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/if_var.h>
80 #include <net/if_media.h>
81 #include <net/if_vlan_var.h>
82 #include <net/radix.h>
83 #include <net/route.h>
86 #if defined(INET) || defined(INET6)
87 #include <net/ethernet.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_carp.h>
93 #include <netinet/if_ether.h>
94 #include <netinet/netdump/netdump.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet6/in6_ifattach.h>
100 #endif /* INET || INET6 */
102 #include <security/mac/mac_framework.h>
105 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
106 * and ifr_ifru when it is used in SIOCGIFCONF.
108 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
109 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
111 __read_mostly epoch_t net_epoch_preempt;
112 __read_mostly epoch_t net_epoch;
113 #ifdef COMPAT_FREEBSD32
114 #include <sys/mount.h>
115 #include <compat/freebsd32/freebsd32.h>
117 struct ifreq_buffer32 {
118 uint32_t length; /* (size_t) */
119 uint32_t buffer; /* (void *) */
123 * Interface request structure used for socket
124 * ioctl's. All interface ioctl's must have parameter
125 * definitions which begin with ifr_name. The
126 * remainder may be interface specific.
129 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
131 struct sockaddr ifru_addr;
132 struct sockaddr ifru_dstaddr;
133 struct sockaddr ifru_broadaddr;
134 struct ifreq_buffer32 ifru_buffer;
145 u_char ifru_vlan_pcp;
148 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
149 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
150 __offsetof(struct ifreq32, ifr_ifru));
152 struct ifgroupreq32 {
153 char ifgr_name[IFNAMSIZ];
156 char ifgru_group[IFNAMSIZ];
157 uint32_t ifgru_groups;
161 struct ifmediareq32 {
162 char ifm_name[IFNAMSIZ];
168 uint32_t ifm_ulist; /* (int *) */
170 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
171 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
173 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
174 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
175 #else /* !COMPAT_FREEBSD32 */
176 #define _CASE_IOC_IFGROUPREQ_32(cmd)
177 #endif /* !COMPAT_FREEBSD32 */
179 #define CASE_IOC_IFGROUPREQ(cmd) \
180 _CASE_IOC_IFGROUPREQ_32(cmd) \
185 #ifdef COMPAT_FREEBSD32
186 struct ifreq32 ifr32;
190 union ifgroupreq_union {
191 struct ifgroupreq ifgr;
192 #ifdef COMPAT_FREEBSD32
193 struct ifgroupreq32 ifgr32;
197 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
198 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
200 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
201 &ifqmaxlen, 0, "max send queue size");
203 /* Log link state change events */
204 static int log_link_state_change = 1;
206 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
207 &log_link_state_change, 0,
208 "log interface link state change events");
210 /* Log promiscuous mode change events */
211 static int log_promisc_mode_change = 1;
213 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
214 &log_promisc_mode_change, 1,
215 "log promiscuous mode change events");
217 /* Interface description */
218 static unsigned int ifdescr_maxlen = 1024;
219 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
221 "administrative maximum length for interface description");
223 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
225 /* global sx for non-critical path ifdescr */
226 static struct sx ifdescr_sx;
227 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
229 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
230 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
231 /* These are external hooks for CARP. */
232 void (*carp_linkstate_p)(struct ifnet *ifp);
233 void (*carp_demote_adj_p)(int, char *);
234 int (*carp_master_p)(struct ifaddr *);
235 #if defined(INET) || defined(INET6)
236 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
237 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
238 const struct sockaddr *sa);
239 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
240 int (*carp_attach_p)(struct ifaddr *, int);
241 void (*carp_detach_p)(struct ifaddr *, bool);
244 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
247 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
248 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
249 const struct in6_addr *taddr);
252 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
255 * XXX: Style; these should be sorted alphabetically, and unprototyped
256 * static functions should be prototyped. Currently they are sorted by
259 static void if_attachdomain(void *);
260 static void if_attachdomain1(struct ifnet *);
261 static int ifconf(u_long, caddr_t);
262 static void *if_grow(void);
263 static void if_input_default(struct ifnet *, struct mbuf *);
264 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
265 static void if_route(struct ifnet *, int flag, int fam);
266 static int if_setflag(struct ifnet *, int, int, int *, int);
267 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
268 static void if_unroute(struct ifnet *, int flag, int fam);
269 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
270 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
271 static void do_link_state_change(void *, int);
272 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
273 static int if_getgroupmembers(struct ifgroupreq *);
274 static void if_delgroups(struct ifnet *);
275 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
276 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
277 static void if_link_ifnet(struct ifnet *);
278 static bool if_unlink_ifnet(struct ifnet *, bool);
280 static void if_vmove(struct ifnet *, struct vnet *);
285 * XXX: declare here to avoid to include many inet6 related files..
286 * should be more generalized?
288 extern void nd6_setmtu(struct ifnet *);
291 /* ipsec helper hooks */
292 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
293 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
295 VNET_DEFINE(int, if_index);
296 int ifqmaxlen = IFQ_MAXLEN;
297 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
298 VNET_DEFINE(struct ifgrouphead, ifg_head);
300 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
302 /* Table of ifnet by index. */
303 VNET_DEFINE(struct ifnet **, ifindex_table);
305 #define V_if_indexlim VNET(if_indexlim)
306 #define V_ifindex_table VNET(ifindex_table)
309 * The global network interface list (V_ifnet) and related state (such as
310 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
311 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
313 struct rwlock ifnet_rwlock;
314 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
315 struct sx ifnet_sxlock;
316 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
318 struct sx ifnet_detach_sxlock;
319 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
323 * The allocation of network interfaces is a rather non-atomic affair; we
324 * need to select an index before we are ready to expose the interface for
325 * use, so will use this pointer value to indicate reservation.
327 #define IFNET_HOLD (void *)(uintptr_t)(-1)
329 static if_com_alloc_t *if_com_alloc[256];
330 static if_com_free_t *if_com_free[256];
332 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
333 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
334 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
337 ifnet_byindex_locked(u_short idx)
340 if (idx > V_if_index)
342 if (V_ifindex_table[idx] == IFNET_HOLD)
344 return (V_ifindex_table[idx]);
348 ifnet_byindex(u_short idx)
352 ifp = ifnet_byindex_locked(idx);
357 ifnet_byindex_ref(u_short idx)
361 IFNET_RLOCK_NOSLEEP();
362 ifp = ifnet_byindex_locked(idx);
363 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
364 IFNET_RUNLOCK_NOSLEEP();
368 IFNET_RUNLOCK_NOSLEEP();
373 * Allocate an ifindex array entry; return 0 on success or an error on
377 ifindex_alloc(void **old)
381 IFNET_WLOCK_ASSERT();
383 * Try to find an empty slot below V_if_index. If we fail, take the
386 for (idx = 1; idx <= V_if_index; idx++) {
387 if (V_ifindex_table[idx] == NULL)
391 /* Catch if_index overflow. */
392 if (idx >= V_if_indexlim) {
396 if (idx > V_if_index)
402 ifindex_free_locked(u_short idx)
405 IFNET_WLOCK_ASSERT();
407 V_ifindex_table[idx] = NULL;
408 while (V_if_index > 0 &&
409 V_ifindex_table[V_if_index] == NULL)
414 ifindex_free(u_short idx)
418 ifindex_free_locked(idx);
423 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
426 V_ifindex_table[idx] = ifp;
430 ifaddr_byindex(u_short idx)
433 struct ifaddr *ifa = NULL;
435 IFNET_RLOCK_NOSLEEP();
436 ifp = ifnet_byindex_locked(idx);
437 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
439 IFNET_RUNLOCK_NOSLEEP();
444 * Network interface utility routines.
446 * Routines with ifa_ifwith* names take sockaddr *'s as
451 vnet_if_init(const void *unused __unused)
455 CK_STAILQ_INIT(&V_ifnet);
456 CK_STAILQ_INIT(&V_ifg_head);
458 old = if_grow(); /* create initial table */
460 epoch_wait_preempt(net_epoch_preempt);
462 vnet_if_clone_init();
464 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
469 vnet_if_uninit(const void *unused __unused)
472 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
473 "not empty", __func__, __LINE__, &V_ifnet));
474 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
475 "not empty", __func__, __LINE__, &V_ifg_head));
477 free((caddr_t)V_ifindex_table, M_IFNET);
479 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
480 vnet_if_uninit, NULL);
484 if_link_ifnet(struct ifnet *ifp)
488 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
490 curvnet->vnet_ifcnt++;
496 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
502 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
504 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
506 ifp->if_flags |= IFF_DYING;
511 curvnet->vnet_ifcnt--;
520 vnet_if_return(const void *unused __unused)
522 struct ifnet *ifp, *nifp;
523 struct ifnet **pending;
529 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
530 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
531 * if_detach_internal(), which waits for NET_EPOCH callbacks to
532 * complete. We can't do that from within NET_EPOCH.
534 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
535 * read/write lock. We cannot hold the lock as we call if_vmove()
536 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
541 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
542 M_IFNET, M_WAITOK | M_ZERO);
544 /* Return all inherited interfaces to their parent vnets. */
545 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
546 if (ifp->if_home_vnet != ifp->if_vnet) {
547 found = if_unlink_ifnet(ifp, true);
555 for (int j = 0; j < i; j++) {
556 if_vmove(pending[j], pending[j]->if_home_vnet);
559 free(pending, M_IFNET);
561 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
562 vnet_if_return, NULL);
575 IFNET_WLOCK_ASSERT();
576 oldlim = V_if_indexlim;
578 n = (oldlim << 1) * sizeof(*e);
579 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
581 if (V_if_indexlim != oldlim) {
585 if (V_ifindex_table != NULL) {
586 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
587 old = V_ifindex_table;
595 * Allocate a struct ifnet and an index for an interface. A layer 2
596 * common structure will also be allocated if an allocation routine is
597 * registered for the passed type.
600 if_alloc(u_char type)
606 ifp = malloc(sizeof(struct ifnet), M_IFNET, 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;
622 ifp->if_vnet = curvnet;
624 if (if_com_alloc[type] != NULL) {
625 ifp->if_l2com = if_com_alloc[type](type, ifp);
626 if (ifp->if_l2com == NULL) {
633 IF_ADDR_LOCK_INIT(ifp);
634 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
635 ifp->if_afdata_initialized = 0;
636 IF_AFDATA_LOCK_INIT(ifp);
637 CK_STAILQ_INIT(&ifp->if_addrhead);
638 CK_STAILQ_INIT(&ifp->if_multiaddrs);
639 CK_STAILQ_INIT(&ifp->if_groups);
643 ifq_init(&ifp->if_snd, ifp);
645 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
646 for (int i = 0; i < IFCOUNTERS; i++)
647 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
648 ifp->if_get_counter = if_get_counter_default;
649 ifp->if_pcp = IFNET_PCP_NONE;
650 ifnet_setbyindex(ifp->if_index, ifp);
655 * Do the actual work of freeing a struct ifnet, and layer 2 common
656 * structure. This call is made when the last reference to an
657 * interface is released.
660 if_free_internal(struct ifnet *ifp)
663 KASSERT((ifp->if_flags & IFF_DYING),
664 ("if_free_internal: interface not dying"));
666 if (if_com_free[ifp->if_alloctype] != NULL)
667 if_com_free[ifp->if_alloctype](ifp->if_l2com,
671 mac_ifnet_destroy(ifp);
673 IF_AFDATA_DESTROY(ifp);
674 IF_ADDR_LOCK_DESTROY(ifp);
675 ifq_delete(&ifp->if_snd);
677 for (int i = 0; i < IFCOUNTERS; i++)
678 counter_u64_free(ifp->if_counters[i]);
680 free(ifp->if_description, M_IFDESCR);
681 free(ifp->if_hw_addr, M_IFADDR);
686 if_destroy(epoch_context_t ctx)
690 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
691 if_free_internal(ifp);
695 * Deregister an interface and free the associated storage.
698 if_free(struct ifnet *ifp)
701 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
703 CURVNET_SET_QUIET(ifp->if_vnet);
705 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
706 ("%s: freeing unallocated ifnet", ifp->if_xname));
708 ifindex_free_locked(ifp->if_index);
711 if (refcount_release(&ifp->if_refcount))
712 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
717 * Interfaces to keep an ifnet type-stable despite the possibility of the
718 * driver calling if_free(). If there are additional references, we defer
719 * freeing the underlying data structure.
722 if_ref(struct ifnet *ifp)
725 /* We don't assert the ifnet list lock here, but arguably should. */
726 refcount_acquire(&ifp->if_refcount);
730 if_rele(struct ifnet *ifp)
733 if (!refcount_release(&ifp->if_refcount))
735 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
739 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
742 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
744 if (ifq->ifq_maxlen == 0)
745 ifq->ifq_maxlen = ifqmaxlen;
748 ifq->altq_disc = NULL;
749 ifq->altq_flags &= ALTQF_CANTCHANGE;
750 ifq->altq_tbr = NULL;
755 ifq_delete(struct ifaltq *ifq)
757 mtx_destroy(&ifq->ifq_mtx);
761 * Perform generic interface initialization tasks and attach the interface
762 * to the list of "active" interfaces. If vmove flag is set on entry
763 * to if_attach_internal(), perform only a limited subset of initialization
764 * tasks, given that we are moving from one vnet to another an ifnet which
765 * has already been fully initialized.
767 * Note that if_detach_internal() removes group membership unconditionally
768 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
769 * Thus, when if_vmove() is applied to a cloned interface, group membership
770 * is lost while a cloned one always joins a group whose name is
771 * ifc->ifc_name. To recover this after if_detach_internal() and
772 * if_attach_internal(), the cloner should be specified to
773 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
774 * attempts to join a group whose name is ifc->ifc_name.
777 * - The decision to return void and thus require this function to
778 * succeed is questionable.
779 * - We should probably do more sanity checking. For instance we don't
780 * do anything to insure if_xname is unique or non-empty.
783 if_attach(struct ifnet *ifp)
786 if_attach_internal(ifp, 0, NULL);
790 * Compute the least common TSO limit.
793 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
796 * 1) If there is no limit currently, take the limit from
797 * the network adapter.
799 * 2) If the network adapter has a limit below the current
802 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
803 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
804 pmax->tsomaxbytes = ifp->if_hw_tsomax;
806 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
807 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
808 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
810 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
811 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
812 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
817 * Update TSO limit of a network adapter.
819 * Returns zero if no change. Else non-zero.
822 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
825 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
826 ifp->if_hw_tsomax = pmax->tsomaxbytes;
829 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
830 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
833 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
834 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
841 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
843 unsigned socksize, ifasize;
844 int namelen, masklen;
845 struct sockaddr_dl *sdl;
848 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
849 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
853 ifp->if_vnet = curvnet;
854 if (ifp->if_home_vnet == NULL)
855 ifp->if_home_vnet = curvnet;
858 if_addgroup(ifp, IFG_ALL);
860 /* Restore group membership for cloned interfaces. */
861 if (vmove && ifc != NULL)
862 if_clone_addgroup(ifp, ifc);
864 getmicrotime(&ifp->if_lastchange);
865 ifp->if_epoch = time_uptime;
867 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
868 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
869 ("transmit and qflush must both either be set or both be NULL"));
870 if (ifp->if_transmit == NULL) {
871 ifp->if_transmit = if_transmit;
872 ifp->if_qflush = if_qflush;
874 if (ifp->if_input == NULL)
875 ifp->if_input = if_input_default;
877 if (ifp->if_requestencap == NULL)
878 ifp->if_requestencap = if_requestencap_default;
882 mac_ifnet_create(ifp);
886 * Create a Link Level name for this device.
888 namelen = strlen(ifp->if_xname);
890 * Always save enough space for any possiable name so we
891 * can do a rename in place later.
893 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
894 socksize = masklen + ifp->if_addrlen;
895 if (socksize < sizeof(*sdl))
896 socksize = sizeof(*sdl);
897 socksize = roundup2(socksize, sizeof(long));
898 ifasize = sizeof(*ifa) + 2 * socksize;
899 ifa = ifa_alloc(ifasize, M_WAITOK);
900 sdl = (struct sockaddr_dl *)(ifa + 1);
901 sdl->sdl_len = socksize;
902 sdl->sdl_family = AF_LINK;
903 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
904 sdl->sdl_nlen = namelen;
905 sdl->sdl_index = ifp->if_index;
906 sdl->sdl_type = ifp->if_type;
909 ifa->ifa_rtrequest = link_rtrequest;
910 ifa->ifa_addr = (struct sockaddr *)sdl;
911 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
912 ifa->ifa_netmask = (struct sockaddr *)sdl;
913 sdl->sdl_len = masklen;
915 sdl->sdl_data[--namelen] = 0xff;
916 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
917 /* Reliably crash if used uninitialized. */
918 ifp->if_broadcastaddr = NULL;
920 if (ifp->if_type == IFT_ETHER) {
921 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
925 #if defined(INET) || defined(INET6)
926 /* Use defaults for TSO, if nothing is set */
927 if (ifp->if_hw_tsomax == 0 &&
928 ifp->if_hw_tsomaxsegcount == 0 &&
929 ifp->if_hw_tsomaxsegsize == 0) {
931 * The TSO defaults needs to be such that an
932 * NFS mbuf list of 35 mbufs totalling just
933 * below 64K works and that a chain of mbufs
934 * can be defragged into at most 32 segments:
936 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
937 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
938 ifp->if_hw_tsomaxsegcount = 35;
939 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
941 /* XXX some drivers set IFCAP_TSO after ethernet attach */
942 if (ifp->if_capabilities & IFCAP_TSO) {
943 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
945 ifp->if_hw_tsomaxsegcount,
946 ifp->if_hw_tsomaxsegsize);
954 * Update the interface index in the link layer address
957 for (ifa = ifp->if_addr; ifa != NULL;
958 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
959 if (ifa->ifa_addr->sa_family == AF_LINK) {
960 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
961 sdl->sdl_index = ifp->if_index;
969 if (domain_init_status >= 2)
970 if_attachdomain1(ifp);
972 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
973 if (IS_DEFAULT_VNET(curvnet))
974 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
976 /* Announce the interface. */
977 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
981 if_epochalloc(void *dummy __unused)
984 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
985 net_epoch = epoch_alloc(0);
987 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
988 if_epochalloc, NULL);
991 if_attachdomain(void *dummy)
995 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
996 if_attachdomain1(ifp);
998 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
999 if_attachdomain, NULL);
1002 if_attachdomain1(struct ifnet *ifp)
1007 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1008 * cannot lock ifp->if_afdata initialization, entirely.
1010 IF_AFDATA_LOCK(ifp);
1011 if (ifp->if_afdata_initialized >= domain_init_status) {
1012 IF_AFDATA_UNLOCK(ifp);
1013 log(LOG_WARNING, "%s called more than once on %s\n",
1014 __func__, ifp->if_xname);
1017 ifp->if_afdata_initialized = domain_init_status;
1018 IF_AFDATA_UNLOCK(ifp);
1020 /* address family dependent data region */
1021 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1022 for (dp = domains; dp; dp = dp->dom_next) {
1023 if (dp->dom_ifattach)
1024 ifp->if_afdata[dp->dom_family] =
1025 (*dp->dom_ifattach)(ifp);
1030 * Remove any unicast or broadcast network addresses from an interface.
1033 if_purgeaddrs(struct ifnet *ifp)
1039 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1040 if (ifa->ifa_addr->sa_family != AF_LINK)
1048 /* XXX: Ugly!! ad hoc just for INET */
1049 if (ifa->ifa_addr->sa_family == AF_INET) {
1050 struct ifaliasreq ifr;
1052 bzero(&ifr, sizeof(ifr));
1053 ifr.ifra_addr = *ifa->ifa_addr;
1054 if (ifa->ifa_dstaddr)
1055 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1056 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1062 if (ifa->ifa_addr->sa_family == AF_INET6) {
1064 /* ifp_addrhead is already updated */
1069 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1070 IF_ADDR_WUNLOCK(ifp);
1076 * Remove any multicast network addresses from an interface when an ifnet
1080 if_purgemaddrs(struct ifnet *ifp)
1082 struct ifmultiaddr *ifma;
1085 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1086 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1087 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1088 if_delmulti_locked(ifp, ifma, 1);
1090 IF_ADDR_WUNLOCK(ifp);
1094 * Detach an interface, removing it from the list of "active" interfaces.
1095 * If vmove flag is set on entry to if_detach_internal(), perform only a
1096 * limited subset of cleanup tasks, given that we are moving an ifnet from
1097 * one vnet to another, where it must be fully operational.
1099 * XXXRW: There are some significant questions about event ordering, and
1100 * how to prevent things from starting to use the interface during detach.
1103 if_detach(struct ifnet *ifp)
1107 CURVNET_SET_QUIET(ifp->if_vnet);
1108 found = if_unlink_ifnet(ifp, false);
1110 sx_xlock(&ifnet_detach_sxlock);
1111 if_detach_internal(ifp, 0, NULL);
1112 sx_xunlock(&ifnet_detach_sxlock);
1118 * The vmove flag, if set, indicates that we are called from a callpath
1119 * that is moving an interface to a different vnet instance.
1121 * The shutdown flag, if set, indicates that we are called in the
1122 * process of shutting down a vnet instance. Currently only the
1123 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1124 * on a vnet instance shutdown without this flag being set, e.g., when
1125 * the cloned interfaces are destoyed as first thing of teardown.
1128 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1136 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1137 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1141 * At this point we know the interface still was on the ifnet list
1142 * and we removed it so we are in a stable state.
1144 epoch_wait_preempt(net_epoch_preempt);
1147 * Ensure all pending EPOCH(9) callbacks have been executed. This
1148 * fixes issues about late destruction of multicast options
1149 * which lead to leave group calls, which in turn access the
1150 * belonging ifnet structure:
1152 epoch_drain_callbacks(net_epoch_preempt);
1155 * In any case (destroy or vmove) detach us from the groups
1156 * and remove/wait for pending events on the taskq.
1157 * XXX-BZ in theory an interface could still enqueue a taskq change?
1161 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1164 * Check if this is a cloned interface or not. Must do even if
1165 * shutting down as a if_vmove_reclaim() would move the ifp and
1166 * the if_clone_addgroup() will have a corrupted string overwise
1167 * from a gibberish pointer.
1169 if (vmove && ifcp != NULL)
1170 *ifcp = if_clone_findifc(ifp);
1176 * On VNET shutdown abort here as the stack teardown will do all
1177 * the work top-down for us.
1180 /* Give interface users the chance to clean up. */
1181 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1184 * In case of a vmove we are done here without error.
1185 * If we would signal an error it would lead to the same
1186 * abort as if we did not find the ifnet anymore.
1187 * if_detach() calls us in void context and does not care
1188 * about an early abort notification, so life is splendid :)
1190 goto finish_vnet_shutdown;
1195 * At this point we are not tearing down a VNET and are either
1196 * going to destroy or vmove the interface and have to cleanup
1201 * Remove routes and flush queues.
1204 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1205 altq_disable(&ifp->if_snd);
1206 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1207 altq_detach(&ifp->if_snd);
1218 * Remove all IPv6 kernel structs related to ifp. This should be done
1219 * before removing routing entries below, since IPv6 interface direct
1220 * routes are expected to be removed by the IPv6-specific kernel API.
1221 * Otherwise, the kernel will detect some inconsistency and bark it.
1225 if_purgemaddrs(ifp);
1227 /* Announce that the interface is gone. */
1228 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1229 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1230 if (IS_DEFAULT_VNET(curvnet))
1231 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1235 * Prevent further calls into the device driver via ifnet.
1240 * Clean up all addresses.
1243 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1244 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1245 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1246 IF_ADDR_WUNLOCK(ifp);
1249 IF_ADDR_WUNLOCK(ifp);
1252 rt_flushifroutes(ifp);
1255 finish_vnet_shutdown:
1258 * We cannot hold the lock over dom_ifdetach calls as they might
1259 * sleep, for example trying to drain a callout, thus open up the
1260 * theoretical race with re-attaching.
1262 IF_AFDATA_LOCK(ifp);
1263 i = ifp->if_afdata_initialized;
1264 ifp->if_afdata_initialized = 0;
1265 IF_AFDATA_UNLOCK(ifp);
1266 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1267 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1268 (*dp->dom_ifdetach)(ifp,
1269 ifp->if_afdata[dp->dom_family]);
1270 ifp->if_afdata[dp->dom_family] = NULL;
1279 * if_vmove() performs a limited version of if_detach() in current
1280 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1281 * An attempt is made to shrink if_index in current vnet, find an
1282 * unused if_index in target vnet and calls if_grow() if necessary,
1283 * and finally find an unused if_xname for the target vnet.
1286 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1288 struct if_clone *ifc;
1290 u_int bif_dlt, bif_hdrlen;
1297 * if_detach_internal() will call the eventhandler to notify
1298 * interface departure. That will detach if_bpf. We need to
1299 * safe the dlt and hdrlen so we can re-attach it later.
1301 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1305 * Detach from current vnet, but preserve LLADDR info, do not
1306 * mark as dead etc. so that the ifnet can be reattached later.
1307 * If we cannot find it, we lost the race to someone else.
1309 rc = if_detach_internal(ifp, 1, &ifc);
1314 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1315 * the if_index for that vnet if possible.
1317 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1318 * or we'd lock on one vnet and unlock on another.
1321 ifindex_free_locked(ifp->if_index);
1325 * Perform interface-specific reassignment tasks, if provided by
1328 if (ifp->if_reassign != NULL)
1329 ifp->if_reassign(ifp, new_vnet, NULL);
1332 * Switch to the context of the target vnet.
1334 CURVNET_SET_QUIET(new_vnet);
1337 ifp->if_index = ifindex_alloc(&old);
1338 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1340 epoch_wait_preempt(net_epoch_preempt);
1344 ifnet_setbyindex(ifp->if_index, ifp);
1347 if_attach_internal(ifp, 1, ifc);
1350 if (ifp->if_bpf == NULL)
1351 bpfattach(ifp, bif_dlt, bif_hdrlen);
1358 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1361 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1368 /* Try to find the prison within our visibility. */
1369 sx_slock(&allprison_lock);
1370 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1371 sx_sunlock(&allprison_lock);
1374 prison_hold_locked(pr);
1375 mtx_unlock(&pr->pr_mtx);
1377 /* Do not try to move the iface from and to the same prison. */
1378 if (pr->pr_vnet == ifp->if_vnet) {
1383 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1384 /* XXX Lock interfaces to avoid races. */
1385 CURVNET_SET_QUIET(pr->pr_vnet);
1386 difp = ifunit(ifname);
1393 /* Make sure the VNET is stable. */
1394 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1395 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1403 found = if_unlink_ifnet(ifp, true);
1406 /* Move the interface into the child jail/vnet. */
1407 if_vmove(ifp, pr->pr_vnet);
1409 /* Report the new if_xname back to the userland. */
1410 sprintf(ifname, "%s", ifp->if_xname);
1417 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1420 struct vnet *vnet_dst;
1425 /* Try to find the prison within our visibility. */
1426 sx_slock(&allprison_lock);
1427 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1428 sx_sunlock(&allprison_lock);
1431 prison_hold_locked(pr);
1432 mtx_unlock(&pr->pr_mtx);
1434 /* Make sure the named iface exists in the source prison/vnet. */
1435 CURVNET_SET(pr->pr_vnet);
1436 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1443 /* Do not try to move the iface from and to the same prison. */
1444 vnet_dst = TD_TO_VNET(td);
1445 if (vnet_dst == ifp->if_vnet) {
1451 /* Make sure the VNET is stable. */
1452 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1453 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1460 /* Get interface back from child jail/vnet. */
1461 found = if_unlink_ifnet(ifp, true);
1463 if_vmove(ifp, vnet_dst);
1466 /* Report the new if_xname back to the userland. */
1467 sprintf(ifname, "%s", ifp->if_xname);
1475 * Add a group to an interface
1478 if_addgroup(struct ifnet *ifp, const char *groupname)
1480 struct ifg_list *ifgl;
1481 struct ifg_group *ifg = NULL;
1482 struct ifg_member *ifgm;
1485 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1486 groupname[strlen(groupname) - 1] <= '9')
1490 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1491 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1496 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1501 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1507 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1508 if (!strcmp(ifg->ifg_group, groupname))
1512 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1518 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1519 ifg->ifg_refcnt = 0;
1520 CK_STAILQ_INIT(&ifg->ifg_members);
1521 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1526 ifgl->ifgl_group = ifg;
1527 ifgm->ifgm_ifp = ifp;
1530 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1531 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1532 IF_ADDR_WUNLOCK(ifp);
1537 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1538 EVENTHANDLER_INVOKE(group_change_event, groupname);
1544 * Helper function to remove a group out of an interface. Expects the global
1545 * ifnet lock to be write-locked, and drops it before returning.
1548 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1549 const char *groupname)
1551 struct ifg_member *ifgm;
1554 IFNET_WLOCK_ASSERT();
1557 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1558 IF_ADDR_WUNLOCK(ifp);
1560 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1561 if (ifgm->ifgm_ifp == ifp) {
1562 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1563 ifg_member, ifgm_next);
1568 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1569 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1577 epoch_wait_preempt(net_epoch_preempt);
1579 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1580 free(ifgl->ifgl_group, M_TEMP);
1585 EVENTHANDLER_INVOKE(group_change_event, groupname);
1589 * Remove a group from an interface
1592 if_delgroup(struct ifnet *ifp, const char *groupname)
1594 struct ifg_list *ifgl;
1597 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1598 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1605 _if_delgroup_locked(ifp, ifgl, groupname);
1611 * Remove an interface from all groups
1614 if_delgroups(struct ifnet *ifp)
1616 struct ifg_list *ifgl;
1617 char groupname[IFNAMSIZ];
1620 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1621 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1622 _if_delgroup_locked(ifp, ifgl, groupname);
1629 ifgr_group_get(void *ifgrp)
1631 union ifgroupreq_union *ifgrup;
1634 #ifdef COMPAT_FREEBSD32
1635 if (SV_CURPROC_FLAG(SV_ILP32))
1636 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1638 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1641 static struct ifg_req *
1642 ifgr_groups_get(void *ifgrp)
1644 union ifgroupreq_union *ifgrup;
1647 #ifdef COMPAT_FREEBSD32
1648 if (SV_CURPROC_FLAG(SV_ILP32))
1649 return ((struct ifg_req *)(uintptr_t)
1650 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1652 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1656 * Stores all groups from an interface in memory pointed to by ifgr.
1659 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1662 struct ifg_list *ifgl;
1663 struct ifg_req ifgrq, *ifgp;
1665 if (ifgr->ifgr_len == 0) {
1667 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1668 ifgr->ifgr_len += sizeof(struct ifg_req);
1669 IF_ADDR_RUNLOCK(ifp);
1673 len = ifgr->ifgr_len;
1674 ifgp = ifgr_groups_get(ifgr);
1677 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1678 if (len < sizeof(ifgrq)) {
1679 IF_ADDR_RUNLOCK(ifp);
1682 bzero(&ifgrq, sizeof ifgrq);
1683 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1684 sizeof(ifgrq.ifgrq_group));
1685 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1686 IF_ADDR_RUNLOCK(ifp);
1689 len -= sizeof(ifgrq);
1692 IF_ADDR_RUNLOCK(ifp);
1698 * Stores all members of a group in memory pointed to by igfr
1701 if_getgroupmembers(struct ifgroupreq *ifgr)
1703 struct ifg_group *ifg;
1704 struct ifg_member *ifgm;
1705 struct ifg_req ifgrq, *ifgp;
1709 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1710 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1717 if (ifgr->ifgr_len == 0) {
1718 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1719 ifgr->ifgr_len += sizeof(ifgrq);
1724 len = ifgr->ifgr_len;
1725 ifgp = ifgr_groups_get(ifgr);
1726 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1727 if (len < sizeof(ifgrq)) {
1731 bzero(&ifgrq, sizeof ifgrq);
1732 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1733 sizeof(ifgrq.ifgrq_member));
1734 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1738 len -= sizeof(ifgrq);
1747 * Return counter values from counter(9)s stored in ifnet.
1750 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1753 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1755 return (counter_u64_fetch(ifp->if_counters[cnt]));
1759 * Increase an ifnet counter. Usually used for counters shared
1760 * between the stack and a driver, but function supports them all.
1763 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1766 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1768 counter_u64_add(ifp->if_counters[cnt], inc);
1772 * Copy data from ifnet to userland API structure if_data.
1775 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1778 ifd->ifi_type = ifp->if_type;
1779 ifd->ifi_physical = 0;
1780 ifd->ifi_addrlen = ifp->if_addrlen;
1781 ifd->ifi_hdrlen = ifp->if_hdrlen;
1782 ifd->ifi_link_state = ifp->if_link_state;
1784 ifd->ifi_datalen = sizeof(struct if_data);
1785 ifd->ifi_mtu = ifp->if_mtu;
1786 ifd->ifi_metric = ifp->if_metric;
1787 ifd->ifi_baudrate = ifp->if_baudrate;
1788 ifd->ifi_hwassist = ifp->if_hwassist;
1789 ifd->ifi_epoch = ifp->if_epoch;
1790 ifd->ifi_lastchange = ifp->if_lastchange;
1792 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1793 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1794 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1795 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1796 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1797 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1798 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1799 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1800 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1801 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1802 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1803 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1806 struct ifnet_read_lock {
1807 struct mtx mtx; /* lock protecting tracker below */
1808 struct epoch_tracker et;
1811 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_addr_read_lock);
1812 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_maddr_read_lock);
1815 ifnet_read_lock_init(void __unused *arg)
1817 struct ifnet_read_lock *pifrl;
1821 pifrl = DPCPU_ID_PTR(cpu, ifnet_addr_read_lock);
1822 mtx_init(&pifrl->mtx, "ifnet_addr_read_lock", NULL, MTX_DEF);
1824 pifrl = DPCPU_ID_PTR(cpu, ifnet_maddr_read_lock);
1825 mtx_init(&pifrl->mtx, "ifnet_maddr_read_lock", NULL, MTX_DEF);
1828 SYSINIT(ifnet_read_lock_init, SI_SUB_CPU + 1, SI_ORDER_FIRST, &ifnet_read_lock_init, NULL);
1831 * Wrapper functions for struct ifnet address list locking macros. These are
1832 * used by kernel modules to avoid encoding programming interface or binary
1833 * interface assumptions that may be violated when kernel-internal locking
1834 * approaches change.
1837 if_addr_rlock(struct ifnet *ifp)
1839 struct ifnet_read_lock *pifrl;
1842 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1843 mtx_lock(&pifrl->mtx);
1844 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1848 if_addr_runlock(struct ifnet *ifp)
1850 struct ifnet_read_lock *pifrl;
1852 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1854 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1855 mtx_unlock(&pifrl->mtx);
1860 if_maddr_rlock(if_t ifp)
1862 struct ifnet_read_lock *pifrl;
1865 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1866 mtx_lock(&pifrl->mtx);
1867 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1871 if_maddr_runlock(if_t ifp)
1873 struct ifnet_read_lock *pifrl;
1875 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1877 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1878 mtx_unlock(&pifrl->mtx);
1883 * Initialization, destruction and refcounting functions for ifaddrs.
1886 ifa_alloc(size_t size, int flags)
1890 KASSERT(size >= sizeof(struct ifaddr),
1891 ("%s: invalid size %zu", __func__, size));
1893 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1897 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1899 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1901 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1903 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1906 refcount_init(&ifa->ifa_refcnt, 1);
1911 /* free(NULL) is okay */
1912 counter_u64_free(ifa->ifa_opackets);
1913 counter_u64_free(ifa->ifa_ipackets);
1914 counter_u64_free(ifa->ifa_obytes);
1915 counter_u64_free(ifa->ifa_ibytes);
1916 free(ifa, M_IFADDR);
1922 ifa_ref(struct ifaddr *ifa)
1925 refcount_acquire(&ifa->ifa_refcnt);
1929 ifa_destroy(epoch_context_t ctx)
1933 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1934 counter_u64_free(ifa->ifa_opackets);
1935 counter_u64_free(ifa->ifa_ipackets);
1936 counter_u64_free(ifa->ifa_obytes);
1937 counter_u64_free(ifa->ifa_ibytes);
1938 free(ifa, M_IFADDR);
1942 ifa_free(struct ifaddr *ifa)
1945 if (refcount_release(&ifa->ifa_refcnt))
1946 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1951 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1952 struct sockaddr *ia)
1955 struct rt_addrinfo info;
1956 struct sockaddr_dl null_sdl;
1961 bzero(&info, sizeof(info));
1962 if (cmd != RTM_DELETE)
1963 info.rti_ifp = V_loif;
1964 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1965 info.rti_info[RTAX_DST] = ia;
1966 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1967 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1969 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1972 (cmd == RTM_ADD && error == EEXIST) ||
1973 (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
1976 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1977 __func__, otype, if_name(ifp), error);
1983 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1986 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1990 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1993 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1997 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
2000 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
2004 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
2005 * structs used to represent other address families, it is necessary
2006 * to perform a different comparison.
2009 #define sa_dl_equal(a1, a2) \
2010 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
2011 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
2012 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
2013 CLLADDR((const struct sockaddr_dl *)(a2)), \
2014 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
2017 * Locate an interface based on a complete address.
2021 ifa_ifwithaddr(const struct sockaddr *addr)
2026 MPASS(in_epoch(net_epoch_preempt));
2027 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2028 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2029 if (ifa->ifa_addr->sa_family != addr->sa_family)
2031 if (sa_equal(addr, ifa->ifa_addr)) {
2034 /* IP6 doesn't have broadcast */
2035 if ((ifp->if_flags & IFF_BROADCAST) &&
2036 ifa->ifa_broadaddr &&
2037 ifa->ifa_broadaddr->sa_len != 0 &&
2038 sa_equal(ifa->ifa_broadaddr, addr)) {
2049 ifa_ifwithaddr_check(const struct sockaddr *addr)
2054 rc = (ifa_ifwithaddr(addr) != NULL);
2060 * Locate an interface based on the broadcast address.
2064 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
2069 MPASS(in_epoch(net_epoch_preempt));
2070 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2071 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2073 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2074 if (ifa->ifa_addr->sa_family != addr->sa_family)
2076 if ((ifp->if_flags & IFF_BROADCAST) &&
2077 ifa->ifa_broadaddr &&
2078 ifa->ifa_broadaddr->sa_len != 0 &&
2079 sa_equal(ifa->ifa_broadaddr, addr)) {
2090 * Locate the point to point interface with a given destination address.
2094 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2099 MPASS(in_epoch(net_epoch_preempt));
2100 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2101 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2103 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2105 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2106 if (ifa->ifa_addr->sa_family != addr->sa_family)
2108 if (ifa->ifa_dstaddr != NULL &&
2109 sa_equal(addr, ifa->ifa_dstaddr)) {
2120 * Find an interface on a specific network. If many, choice
2121 * is most specific found.
2124 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2128 struct ifaddr *ifa_maybe = NULL;
2129 u_int af = addr->sa_family;
2130 const char *addr_data = addr->sa_data, *cplim;
2132 MPASS(in_epoch(net_epoch_preempt));
2134 * AF_LINK addresses can be looked up directly by their index number,
2135 * so do that if we can.
2137 if (af == AF_LINK) {
2138 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2139 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2140 return (ifaddr_byindex(sdl->sdl_index));
2144 * Scan though each interface, looking for ones that have addresses
2145 * in this address family and the requested fib. Maintain a reference
2146 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2147 * kept it stable when we move onto the next interface.
2149 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2150 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2152 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2153 const char *cp, *cp2, *cp3;
2155 if (ifa->ifa_addr->sa_family != af)
2157 if (af == AF_INET &&
2158 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2160 * This is a bit broken as it doesn't
2161 * take into account that the remote end may
2162 * be a single node in the network we are
2164 * The trouble is that we don't know the
2165 * netmask for the remote end.
2167 if (ifa->ifa_dstaddr != NULL &&
2168 sa_equal(addr, ifa->ifa_dstaddr)) {
2173 * Scan all the bits in the ifa's address.
2174 * If a bit dissagrees with what we are
2175 * looking for, mask it with the netmask
2176 * to see if it really matters.
2177 * (A byte at a time)
2179 if (ifa->ifa_netmask == 0)
2182 cp2 = ifa->ifa_addr->sa_data;
2183 cp3 = ifa->ifa_netmask->sa_data;
2184 cplim = ifa->ifa_netmask->sa_len
2185 + (char *)ifa->ifa_netmask;
2187 if ((*cp++ ^ *cp2++) & *cp3++)
2188 goto next; /* next address! */
2190 * If the netmask of what we just found
2191 * is more specific than what we had before
2192 * (if we had one), or if the virtual status
2193 * of new prefix is better than of the old one,
2194 * then remember the new one before continuing
2195 * to search for an even better one.
2197 if (ifa_maybe == NULL ||
2198 ifa_preferred(ifa_maybe, ifa) ||
2199 rn_refines((caddr_t)ifa->ifa_netmask,
2200 (caddr_t)ifa_maybe->ifa_netmask)) {
2213 * Find an interface address specific to an interface best matching
2217 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2220 const char *cp, *cp2, *cp3;
2222 struct ifaddr *ifa_maybe = NULL;
2223 u_int af = addr->sa_family;
2228 MPASS(in_epoch(net_epoch_preempt));
2229 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2230 if (ifa->ifa_addr->sa_family != af)
2232 if (ifa_maybe == NULL)
2234 if (ifa->ifa_netmask == 0) {
2235 if (sa_equal(addr, ifa->ifa_addr) ||
2236 (ifa->ifa_dstaddr &&
2237 sa_equal(addr, ifa->ifa_dstaddr)))
2241 if (ifp->if_flags & IFF_POINTOPOINT) {
2242 if (sa_equal(addr, ifa->ifa_dstaddr))
2246 cp2 = ifa->ifa_addr->sa_data;
2247 cp3 = ifa->ifa_netmask->sa_data;
2248 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2249 for (; cp3 < cplim; cp3++)
2250 if ((*cp++ ^ *cp2++) & *cp3)
2262 * See whether new ifa is better than current one:
2263 * 1) A non-virtual one is preferred over virtual.
2264 * 2) A virtual in master state preferred over any other state.
2266 * Used in several address selecting functions.
2269 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2272 return (cur->ifa_carp && (!next->ifa_carp ||
2273 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2276 #include <net/if_llatbl.h>
2279 * Default action when installing a route with a Link Level gateway.
2280 * Lookup an appropriate real ifa to point to.
2281 * This should be moved to /sys/net/link.c eventually.
2284 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2286 struct ifaddr *ifa, *oifa;
2287 struct sockaddr *dst;
2290 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2291 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2294 ifa = ifaof_ifpforaddr(dst, ifp);
2302 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2303 ifa->ifa_rtrequest(cmd, rt, info);
2308 struct sockaddr_dl *
2309 link_alloc_sdl(size_t size, int flags)
2312 return (malloc(size, M_TEMP, flags));
2316 link_free_sdl(struct sockaddr *sa)
2322 * Fills in given sdl with interface basic info.
2323 * Returns pointer to filled sdl.
2325 struct sockaddr_dl *
2326 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2328 struct sockaddr_dl *sdl;
2330 sdl = (struct sockaddr_dl *)paddr;
2331 memset(sdl, 0, sizeof(struct sockaddr_dl));
2332 sdl->sdl_len = sizeof(struct sockaddr_dl);
2333 sdl->sdl_family = AF_LINK;
2334 sdl->sdl_index = ifp->if_index;
2335 sdl->sdl_type = iftype;
2341 * Mark an interface down and notify protocols of
2345 if_unroute(struct ifnet *ifp, int flag, int fam)
2349 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2351 ifp->if_flags &= ~flag;
2352 getmicrotime(&ifp->if_lastchange);
2353 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2354 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2355 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2356 ifp->if_qflush(ifp);
2359 (*carp_linkstate_p)(ifp);
2364 * Mark an interface up and notify protocols of
2368 if_route(struct ifnet *ifp, int flag, int fam)
2372 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2374 ifp->if_flags |= flag;
2375 getmicrotime(&ifp->if_lastchange);
2376 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2377 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2378 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2380 (*carp_linkstate_p)(ifp);
2387 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2388 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2389 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2390 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2391 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2392 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2393 int (*vlan_setcookie_p)(struct ifnet *, void *);
2394 void *(*vlan_cookie_p)(struct ifnet *);
2397 * Handle a change in the interface link state. To avoid LORs
2398 * between driver lock and upper layer locks, as well as possible
2399 * recursions, we post event to taskqueue, and all job
2400 * is done in static do_link_state_change().
2403 if_link_state_change(struct ifnet *ifp, int link_state)
2405 /* Return if state hasn't changed. */
2406 if (ifp->if_link_state == link_state)
2409 ifp->if_link_state = link_state;
2411 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2415 do_link_state_change(void *arg, int pending)
2417 struct ifnet *ifp = (struct ifnet *)arg;
2418 int link_state = ifp->if_link_state;
2419 CURVNET_SET(ifp->if_vnet);
2421 /* Notify that the link state has changed. */
2423 if (ifp->if_vlantrunk != NULL)
2424 (*vlan_link_state_p)(ifp);
2426 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2427 ifp->if_l2com != NULL)
2428 (*ng_ether_link_state_p)(ifp, link_state);
2430 (*carp_linkstate_p)(ifp);
2432 ifp->if_bridge_linkstate(ifp);
2434 (*lagg_linkstate_p)(ifp, link_state);
2436 if (IS_DEFAULT_VNET(curvnet))
2437 devctl_notify("IFNET", ifp->if_xname,
2438 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2441 if_printf(ifp, "%d link states coalesced\n", pending);
2442 if (log_link_state_change)
2443 if_printf(ifp, "link state changed to %s\n",
2444 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2445 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2450 * Mark an interface down and notify protocols of
2454 if_down(struct ifnet *ifp)
2457 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2458 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2462 * Mark an interface up and notify protocols of
2466 if_up(struct ifnet *ifp)
2469 if_route(ifp, IFF_UP, AF_UNSPEC);
2470 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2474 * Flush an interface queue.
2477 if_qflush(struct ifnet *ifp)
2485 if (ALTQ_IS_ENABLED(ifq))
2489 while ((m = n) != NULL) {
2500 * Map interface name to interface structure pointer, with or without
2501 * returning a reference.
2504 ifunit_ref(const char *name)
2508 IFNET_RLOCK_NOSLEEP();
2509 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2510 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2511 !(ifp->if_flags & IFF_DYING))
2516 IFNET_RUNLOCK_NOSLEEP();
2521 ifunit(const char *name)
2525 IFNET_RLOCK_NOSLEEP();
2526 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2527 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2530 IFNET_RUNLOCK_NOSLEEP();
2535 ifr_buffer_get_buffer(void *data)
2537 union ifreq_union *ifrup;
2540 #ifdef COMPAT_FREEBSD32
2541 if (SV_CURPROC_FLAG(SV_ILP32))
2542 return ((void *)(uintptr_t)
2543 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2545 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2549 ifr_buffer_set_buffer_null(void *data)
2551 union ifreq_union *ifrup;
2554 #ifdef COMPAT_FREEBSD32
2555 if (SV_CURPROC_FLAG(SV_ILP32))
2556 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2559 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2563 ifr_buffer_get_length(void *data)
2565 union ifreq_union *ifrup;
2568 #ifdef COMPAT_FREEBSD32
2569 if (SV_CURPROC_FLAG(SV_ILP32))
2570 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2572 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2576 ifr_buffer_set_length(void *data, size_t len)
2578 union ifreq_union *ifrup;
2581 #ifdef COMPAT_FREEBSD32
2582 if (SV_CURPROC_FLAG(SV_ILP32))
2583 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2586 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2590 ifr_data_get_ptr(void *ifrp)
2592 union ifreq_union *ifrup;
2595 #ifdef COMPAT_FREEBSD32
2596 if (SV_CURPROC_FLAG(SV_ILP32))
2597 return ((void *)(uintptr_t)
2598 ifrup->ifr32.ifr_ifru.ifru_data);
2600 return (ifrup->ifr.ifr_ifru.ifru_data);
2604 * Hardware specific interface ioctls.
2607 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2610 int error = 0, do_ifup = 0;
2611 int new_flags, temp_flags;
2612 size_t namelen, onamelen;
2614 char *descrbuf, *odescrbuf;
2615 char new_name[IFNAMSIZ];
2617 struct sockaddr_dl *sdl;
2619 ifr = (struct ifreq *)data;
2622 ifr->ifr_index = ifp->if_index;
2626 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2627 ifr->ifr_flags = temp_flags & 0xffff;
2628 ifr->ifr_flagshigh = temp_flags >> 16;
2632 ifr->ifr_reqcap = ifp->if_capabilities;
2633 ifr->ifr_curcap = ifp->if_capenable;
2638 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2643 ifr->ifr_metric = ifp->if_metric;
2647 ifr->ifr_mtu = ifp->if_mtu;
2651 /* XXXGL: did this ever worked? */
2657 sx_slock(&ifdescr_sx);
2658 if (ifp->if_description == NULL)
2661 /* space for terminating nul */
2662 descrlen = strlen(ifp->if_description) + 1;
2663 if (ifr_buffer_get_length(ifr) < descrlen)
2664 ifr_buffer_set_buffer_null(ifr);
2666 error = copyout(ifp->if_description,
2667 ifr_buffer_get_buffer(ifr), descrlen);
2668 ifr_buffer_set_length(ifr, descrlen);
2670 sx_sunlock(&ifdescr_sx);
2674 error = priv_check(td, PRIV_NET_SETIFDESCR);
2679 * Copy only (length-1) bytes to make sure that
2680 * if_description is always nul terminated. The
2681 * length parameter is supposed to count the
2682 * terminating nul in.
2684 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2685 return (ENAMETOOLONG);
2686 else if (ifr_buffer_get_length(ifr) == 0)
2689 descrbuf = malloc(ifr_buffer_get_length(ifr),
2690 M_IFDESCR, M_WAITOK | M_ZERO);
2691 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2692 ifr_buffer_get_length(ifr) - 1);
2694 free(descrbuf, M_IFDESCR);
2699 sx_xlock(&ifdescr_sx);
2700 odescrbuf = ifp->if_description;
2701 ifp->if_description = descrbuf;
2702 sx_xunlock(&ifdescr_sx);
2704 getmicrotime(&ifp->if_lastchange);
2705 free(odescrbuf, M_IFDESCR);
2709 ifr->ifr_fib = ifp->if_fib;
2713 error = priv_check(td, PRIV_NET_SETIFFIB);
2716 if (ifr->ifr_fib >= rt_numfibs)
2719 ifp->if_fib = ifr->ifr_fib;
2723 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2727 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2728 * check, so we don't need special handling here yet.
2730 new_flags = (ifr->ifr_flags & 0xffff) |
2731 (ifr->ifr_flagshigh << 16);
2732 if (ifp->if_flags & IFF_UP &&
2733 (new_flags & IFF_UP) == 0) {
2735 } else if (new_flags & IFF_UP &&
2736 (ifp->if_flags & IFF_UP) == 0) {
2739 /* See if permanently promiscuous mode bit is about to flip */
2740 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2741 if (new_flags & IFF_PPROMISC)
2742 ifp->if_flags |= IFF_PROMISC;
2743 else if (ifp->if_pcount == 0)
2744 ifp->if_flags &= ~IFF_PROMISC;
2745 if (log_promisc_mode_change)
2746 if_printf(ifp, "permanently promiscuous mode %s\n",
2747 ((new_flags & IFF_PPROMISC) ?
2748 "enabled" : "disabled"));
2750 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2751 (new_flags &~ IFF_CANTCHANGE);
2752 if (ifp->if_ioctl) {
2753 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2757 getmicrotime(&ifp->if_lastchange);
2761 error = priv_check(td, PRIV_NET_SETIFCAP);
2764 if (ifp->if_ioctl == NULL)
2765 return (EOPNOTSUPP);
2766 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2768 error = (*ifp->if_ioctl)(ifp, cmd, data);
2770 getmicrotime(&ifp->if_lastchange);
2775 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2780 error = priv_check(td, PRIV_NET_SETIFNAME);
2783 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2787 if (new_name[0] == '\0')
2789 if (new_name[IFNAMSIZ-1] != '\0') {
2790 new_name[IFNAMSIZ-1] = '\0';
2791 if (strlen(new_name) == IFNAMSIZ-1)
2794 if (strcmp(new_name, ifp->if_xname) == 0)
2796 if (ifunit(new_name) != NULL)
2800 * XXX: Locking. Nothing else seems to lock if_flags,
2801 * and there are numerous other races with the
2802 * ifunit() checks not being atomic with namespace
2803 * changes (renames, vmoves, if_attach, etc).
2805 ifp->if_flags |= IFF_RENAMING;
2807 /* Announce the departure of the interface. */
2808 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2809 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2811 if_printf(ifp, "changing name to '%s'\n", new_name);
2814 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2816 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2817 namelen = strlen(new_name);
2818 onamelen = sdl->sdl_nlen;
2820 * Move the address if needed. This is safe because we
2821 * allocate space for a name of length IFNAMSIZ when we
2822 * create this in if_attach().
2824 if (namelen != onamelen) {
2825 bcopy(sdl->sdl_data + onamelen,
2826 sdl->sdl_data + namelen, sdl->sdl_alen);
2828 bcopy(new_name, sdl->sdl_data, namelen);
2829 sdl->sdl_nlen = namelen;
2830 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2831 bzero(sdl->sdl_data, onamelen);
2832 while (namelen != 0)
2833 sdl->sdl_data[--namelen] = 0xff;
2834 IF_ADDR_WUNLOCK(ifp);
2836 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2837 /* Announce the return of the interface. */
2838 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2840 ifp->if_flags &= ~IFF_RENAMING;
2845 error = priv_check(td, PRIV_NET_SETIFVNET);
2848 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2853 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2856 ifp->if_metric = ifr->ifr_metric;
2857 getmicrotime(&ifp->if_lastchange);
2861 error = priv_check(td, PRIV_NET_SETIFPHYS);
2864 if (ifp->if_ioctl == NULL)
2865 return (EOPNOTSUPP);
2866 error = (*ifp->if_ioctl)(ifp, cmd, data);
2868 getmicrotime(&ifp->if_lastchange);
2873 u_long oldmtu = ifp->if_mtu;
2875 error = priv_check(td, PRIV_NET_SETIFMTU);
2878 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2880 if (ifp->if_ioctl == NULL)
2881 return (EOPNOTSUPP);
2882 error = (*ifp->if_ioctl)(ifp, cmd, data);
2884 getmicrotime(&ifp->if_lastchange);
2887 NETDUMP_REINIT(ifp);
2891 * If the link MTU changed, do network layer specific procedure.
2893 if (ifp->if_mtu != oldmtu) {
2904 if (cmd == SIOCADDMULTI)
2905 error = priv_check(td, PRIV_NET_ADDMULTI);
2907 error = priv_check(td, PRIV_NET_DELMULTI);
2911 /* Don't allow group membership on non-multicast interfaces. */
2912 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2913 return (EOPNOTSUPP);
2915 /* Don't let users screw up protocols' entries. */
2916 if (ifr->ifr_addr.sa_family != AF_LINK)
2919 if (cmd == SIOCADDMULTI) {
2920 struct ifmultiaddr *ifma;
2923 * Userland is only permitted to join groups once
2924 * via the if_addmulti() KPI, because it cannot hold
2925 * struct ifmultiaddr * between calls. It may also
2926 * lose a race while we check if the membership
2930 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2931 IF_ADDR_RUNLOCK(ifp);
2935 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2937 error = if_delmulti(ifp, &ifr->ifr_addr);
2940 getmicrotime(&ifp->if_lastchange);
2943 case SIOCSIFPHYADDR:
2944 case SIOCDIFPHYADDR:
2946 case SIOCSIFPHYADDR_IN6:
2949 case SIOCSIFGENERIC:
2950 error = priv_check(td, PRIV_NET_HWIOCTL);
2953 if (ifp->if_ioctl == NULL)
2954 return (EOPNOTSUPP);
2955 error = (*ifp->if_ioctl)(ifp, cmd, data);
2957 getmicrotime(&ifp->if_lastchange);
2961 case SIOCGIFPSRCADDR:
2962 case SIOCGIFPDSTADDR:
2965 case SIOCGIFGENERIC:
2967 case SIOCGIFRSSHASH:
2968 case SIOCGIFDOWNREASON:
2969 if (ifp->if_ioctl == NULL)
2970 return (EOPNOTSUPP);
2971 error = (*ifp->if_ioctl)(ifp, cmd, data);
2975 error = priv_check(td, PRIV_NET_SETLLADDR);
2978 error = if_setlladdr(ifp,
2979 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2983 error = if_gethwaddr(ifp, ifr);
2986 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2987 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2990 if ((error = if_addgroup(ifp,
2991 ifgr_group_get((struct ifgroupreq *)data))))
2995 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2996 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
3000 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
3001 error = priv_check(td, PRIV_NET_DELIFGROUP);
3004 if ((error = if_delgroup(ifp,
3005 ifgr_group_get((struct ifgroupreq *)data))))
3016 #ifdef COMPAT_FREEBSD32
3024 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
3027 #ifdef COMPAT_FREEBSD32
3029 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
3031 struct ifmediareq32 *ifmr32;
3033 ifmr32 = (struct ifmediareq32 *)data;
3034 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
3035 sizeof(ifmr->ifm_name));
3036 ifmr->ifm_current = ifmr32->ifm_current;
3037 ifmr->ifm_mask = ifmr32->ifm_mask;
3038 ifmr->ifm_status = ifmr32->ifm_status;
3039 ifmr->ifm_active = ifmr32->ifm_active;
3040 ifmr->ifm_count = ifmr32->ifm_count;
3041 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
3045 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
3047 struct ifmediareq32 *ifmr32;
3049 ifmr32 = (struct ifmediareq32 *)data;
3050 ifmr32->ifm_current = ifmr->ifm_current;
3051 ifmr32->ifm_mask = ifmr->ifm_mask;
3052 ifmr32->ifm_status = ifmr->ifm_status;
3053 ifmr32->ifm_active = ifmr->ifm_active;
3054 ifmr32->ifm_count = ifmr->ifm_count;
3062 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
3064 #ifdef COMPAT_FREEBSD32
3065 caddr_t saved_data = NULL;
3066 struct ifmediareq ifmr;
3067 struct ifmediareq *ifmrp;
3077 CURVNET_SET(so->so_vnet);
3079 /* Make sure the VNET is stable. */
3080 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
3081 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3091 error = ifconf(cmd, data);
3095 #ifdef COMPAT_FREEBSD32
3098 struct ifconf32 *ifc32;
3101 ifc32 = (struct ifconf32 *)data;
3102 ifc.ifc_len = ifc32->ifc_len;
3103 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3105 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3108 ifc32->ifc_len = ifc.ifc_len;
3114 #ifdef COMPAT_FREEBSD32
3117 case SIOCGIFMEDIA32:
3118 case SIOCGIFXMEDIA32:
3120 ifmr_init(ifmrp, data);
3121 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3123 data = (caddr_t)ifmrp;
3127 ifr = (struct ifreq *)data;
3131 error = priv_check(td, PRIV_NET_SETIFVNET);
3133 error = if_vmove_reclaim(td, ifr->ifr_name,
3139 error = priv_check(td, PRIV_NET_IFCREATE);
3141 error = if_clone_create(ifr->ifr_name,
3142 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3143 ifr_data_get_ptr(ifr) : NULL);
3146 error = priv_check(td, PRIV_NET_IFDESTROY);
3149 sx_xlock(&ifnet_detach_sxlock);
3150 error = if_clone_destroy(ifr->ifr_name);
3151 sx_xunlock(&ifnet_detach_sxlock);
3155 case SIOCIFGCLONERS:
3156 error = if_clone_list((struct if_clonereq *)data);
3159 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3160 error = if_getgroupmembers((struct ifgroupreq *)data);
3163 #if defined(INET) || defined(INET6)
3166 if (carp_ioctl_p == NULL)
3167 error = EPROTONOSUPPORT;
3169 error = (*carp_ioctl_p)(ifr, cmd, td);
3174 ifp = ifunit_ref(ifr->ifr_name);
3180 error = ifhwioctl(cmd, ifp, data, td);
3181 if (error != ENOIOCTL)
3184 oif_flags = ifp->if_flags;
3185 if (so->so_proto == NULL) {
3191 * Pass the request on to the socket control method, and if the
3192 * latter returns EOPNOTSUPP, directly to the interface.
3194 * Make an exception for the legacy SIOCSIF* requests. Drivers
3195 * trust SIOCSIFADDR et al to come from an already privileged
3196 * layer, and do not perform any credentials checks or input
3199 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3201 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3202 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3203 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3204 error = (*ifp->if_ioctl)(ifp, cmd, data);
3206 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3208 if (ifp->if_flags & IFF_UP)
3216 #ifdef COMPAT_FREEBSD32
3217 if (ifmrp != NULL) {
3218 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3219 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3222 ifmr_update(ifmrp, data);
3230 * The code common to handling reference counted flags,
3231 * e.g., in ifpromisc() and if_allmulti().
3232 * The "pflag" argument can specify a permanent mode flag to check,
3233 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3235 * Only to be used on stack-owned flags, not driver-owned flags.
3238 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3242 int oldflags, oldcount;
3244 /* Sanity checks to catch programming errors */
3245 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3246 ("%s: setting driver-owned flag %d", __func__, flag));
3249 KASSERT(*refcount >= 0,
3250 ("%s: increment negative refcount %d for flag %d",
3251 __func__, *refcount, flag));
3253 KASSERT(*refcount > 0,
3254 ("%s: decrement non-positive refcount %d for flag %d",
3255 __func__, *refcount, flag));
3257 /* In case this mode is permanent, just touch refcount */
3258 if (ifp->if_flags & pflag) {
3259 *refcount += onswitch ? 1 : -1;
3263 /* Save ifnet parameters for if_ioctl() may fail */
3264 oldcount = *refcount;
3265 oldflags = ifp->if_flags;
3268 * See if we aren't the only and touching refcount is enough.
3269 * Actually toggle interface flag if we are the first or last.
3274 ifp->if_flags |= flag;
3278 ifp->if_flags &= ~flag;
3281 /* Call down the driver since we've changed interface flags */
3282 if (ifp->if_ioctl == NULL) {
3286 ifr.ifr_flags = ifp->if_flags & 0xffff;
3287 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3288 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3291 /* Notify userland that interface flags have changed */
3296 /* Recover after driver error */
3297 *refcount = oldcount;
3298 ifp->if_flags = oldflags;
3303 * Set/clear promiscuous mode on interface ifp based on the truth value
3304 * of pswitch. The calls are reference counted so that only the first
3305 * "on" request actually has an effect, as does the final "off" request.
3306 * Results are undefined if the "off" and "on" requests are not matched.
3309 ifpromisc(struct ifnet *ifp, int pswitch)
3312 int oldflags = ifp->if_flags;
3314 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3315 &ifp->if_pcount, pswitch);
3316 /* If promiscuous mode status has changed, log a message */
3317 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3318 log_promisc_mode_change)
3319 if_printf(ifp, "promiscuous mode %s\n",
3320 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3325 * Return interface configuration
3326 * of system. List may be used
3327 * in later ioctl's (above) to get
3328 * other information.
3332 ifconf(u_long cmd, caddr_t data)
3334 struct ifconf *ifc = (struct ifconf *)data;
3339 int error, full = 0, valid_len, max_len;
3341 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3342 max_len = MAXPHYS - 1;
3344 /* Prevent hostile input from being able to crash the system */
3345 if (ifc->ifc_len <= 0)
3349 if (ifc->ifc_len <= max_len) {
3350 max_len = ifc->ifc_len;
3353 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3358 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3362 * Zero the ifr to make sure we don't disclose the contents
3365 memset(&ifr, 0, sizeof(ifr));
3367 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3368 >= sizeof(ifr.ifr_name)) {
3371 return (ENAMETOOLONG);
3376 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3377 struct sockaddr *sa = ifa->ifa_addr;
3379 if (prison_if(curthread->td_ucred, sa) != 0)
3382 if (sa->sa_len <= sizeof(*sa)) {
3383 if (sa->sa_len < sizeof(*sa)) {
3384 memset(&ifr.ifr_ifru.ifru_addr, 0,
3385 sizeof(ifr.ifr_ifru.ifru_addr));
3386 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3389 ifr.ifr_ifru.ifru_addr = *sa;
3390 sbuf_bcat(sb, &ifr, sizeof(ifr));
3391 max_len += sizeof(ifr);
3394 offsetof(struct ifreq, ifr_addr));
3395 max_len += offsetof(struct ifreq, ifr_addr);
3396 sbuf_bcat(sb, sa, sa->sa_len);
3397 max_len += sa->sa_len;
3400 if (sbuf_error(sb) == 0)
3401 valid_len = sbuf_len(sb);
3403 IF_ADDR_RUNLOCK(ifp);
3405 sbuf_bcat(sb, &ifr, sizeof(ifr));
3406 max_len += sizeof(ifr);
3408 if (sbuf_error(sb) == 0)
3409 valid_len = sbuf_len(sb);
3415 * If we didn't allocate enough space (uncommon), try again. If
3416 * we have already allocated as much space as we are allowed,
3417 * return what we've got.
3419 if (valid_len != max_len && !full) {
3424 ifc->ifc_len = valid_len;
3426 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3432 * Just like ifpromisc(), but for all-multicast-reception mode.
3435 if_allmulti(struct ifnet *ifp, int onswitch)
3438 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3441 struct ifmultiaddr *
3442 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3444 struct ifmultiaddr *ifma;
3446 IF_ADDR_LOCK_ASSERT(ifp);
3448 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3449 if (sa->sa_family == AF_LINK) {
3450 if (sa_dl_equal(ifma->ifma_addr, sa))
3453 if (sa_equal(ifma->ifma_addr, sa))
3462 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3463 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3464 * the ifnet multicast address list here, so the caller must do that and
3465 * other setup work (such as notifying the device driver). The reference
3466 * count is initialized to 1.
3468 static struct ifmultiaddr *
3469 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3472 struct ifmultiaddr *ifma;
3473 struct sockaddr *dupsa;
3475 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3480 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3481 if (dupsa == NULL) {
3482 free(ifma, M_IFMADDR);
3485 bcopy(sa, dupsa, sa->sa_len);
3486 ifma->ifma_addr = dupsa;
3488 ifma->ifma_ifp = ifp;
3489 ifma->ifma_refcount = 1;
3490 ifma->ifma_protospec = NULL;
3493 ifma->ifma_lladdr = NULL;
3497 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3498 if (dupsa == NULL) {
3499 free(ifma->ifma_addr, M_IFMADDR);
3500 free(ifma, M_IFMADDR);
3503 bcopy(llsa, dupsa, llsa->sa_len);
3504 ifma->ifma_lladdr = dupsa;
3510 * if_freemulti: free ifmultiaddr structure and possibly attached related
3511 * addresses. The caller is responsible for implementing reference
3512 * counting, notifying the driver, handling routing messages, and releasing
3513 * any dependent link layer state.
3515 #ifdef MCAST_VERBOSE
3516 extern void kdb_backtrace(void);
3519 if_freemulti_internal(struct ifmultiaddr *ifma)
3522 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3523 ifma->ifma_refcount));
3525 if (ifma->ifma_lladdr != NULL)
3526 free(ifma->ifma_lladdr, M_IFMADDR);
3527 #ifdef MCAST_VERBOSE
3529 printf("%s freeing ifma: %p\n", __func__, ifma);
3531 free(ifma->ifma_addr, M_IFMADDR);
3532 free(ifma, M_IFMADDR);
3536 if_destroymulti(epoch_context_t ctx)
3538 struct ifmultiaddr *ifma;
3540 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3541 if_freemulti_internal(ifma);
3545 if_freemulti(struct ifmultiaddr *ifma)
3547 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3548 ifma->ifma_refcount));
3550 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3555 * Register an additional multicast address with a network interface.
3557 * - If the address is already present, bump the reference count on the
3558 * address and return.
3559 * - If the address is not link-layer, look up a link layer address.
3560 * - Allocate address structures for one or both addresses, and attach to the
3561 * multicast address list on the interface. If automatically adding a link
3562 * layer address, the protocol address will own a reference to the link
3563 * layer address, to be freed when it is freed.
3564 * - Notify the network device driver of an addition to the multicast address
3567 * 'sa' points to caller-owned memory with the desired multicast address.
3569 * 'retifma' will be used to return a pointer to the resulting multicast
3570 * address reference, if desired.
3573 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3574 struct ifmultiaddr **retifma)
3576 struct ifmultiaddr *ifma, *ll_ifma;
3577 struct sockaddr *llsa;
3578 struct sockaddr_dl sdl;
3582 IN_MULTI_LIST_UNLOCK_ASSERT();
3585 IN6_MULTI_LIST_UNLOCK_ASSERT();
3588 * If the address is already present, return a new reference to it;
3589 * otherwise, allocate storage and set up a new address.
3592 ifma = if_findmulti(ifp, sa);
3594 ifma->ifma_refcount++;
3595 if (retifma != NULL)
3597 IF_ADDR_WUNLOCK(ifp);
3602 * The address isn't already present; resolve the protocol address
3603 * into a link layer address, and then look that up, bump its
3604 * refcount or allocate an ifma for that also.
3605 * Most link layer resolving functions returns address data which
3606 * fits inside default sockaddr_dl structure. However callback
3607 * can allocate another sockaddr structure, in that case we need to
3612 if (ifp->if_resolvemulti != NULL) {
3613 /* Provide called function with buffer size information */
3614 sdl.sdl_len = sizeof(sdl);
3615 llsa = (struct sockaddr *)&sdl;
3616 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3622 * Allocate the new address. Don't hook it up yet, as we may also
3623 * need to allocate a link layer multicast address.
3625 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3632 * If a link layer address is found, we'll need to see if it's
3633 * already present in the address list, or allocate is as well.
3634 * When this block finishes, the link layer address will be on the
3638 ll_ifma = if_findmulti(ifp, llsa);
3639 if (ll_ifma == NULL) {
3640 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3641 if (ll_ifma == NULL) {
3642 --ifma->ifma_refcount;
3647 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3648 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3651 ll_ifma->ifma_refcount++;
3652 ifma->ifma_llifma = ll_ifma;
3656 * We now have a new multicast address, ifma, and possibly a new or
3657 * referenced link layer address. Add the primary address to the
3658 * ifnet address list.
3660 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3661 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3663 if (retifma != NULL)
3667 * Must generate the message while holding the lock so that 'ifma'
3668 * pointer is still valid.
3670 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3671 IF_ADDR_WUNLOCK(ifp);
3674 * We are certain we have added something, so call down to the
3675 * interface to let them know about it.
3677 if (ifp->if_ioctl != NULL) {
3678 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3681 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3682 link_free_sdl(llsa);
3687 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3688 link_free_sdl(llsa);
3691 IF_ADDR_WUNLOCK(ifp);
3696 * Delete a multicast group membership by network-layer group address.
3698 * Returns ENOENT if the entry could not be found. If ifp no longer
3699 * exists, results are undefined. This entry point should only be used
3700 * from subsystems which do appropriate locking to hold ifp for the
3701 * duration of the call.
3702 * Network-layer protocol domains must use if_delmulti_ifma().
3705 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3707 struct ifmultiaddr *ifma;
3712 IFNET_RLOCK_NOSLEEP();
3713 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3718 IFNET_RUNLOCK_NOSLEEP();
3720 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3727 ifma = if_findmulti(ifp, sa);
3729 lastref = if_delmulti_locked(ifp, ifma, 0);
3730 IF_ADDR_WUNLOCK(ifp);
3735 if (lastref && ifp->if_ioctl != NULL) {
3736 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3743 * Delete all multicast group membership for an interface.
3744 * Should be used to quickly flush all multicast filters.
3747 if_delallmulti(struct ifnet *ifp)
3749 struct ifmultiaddr *ifma;
3750 struct ifmultiaddr *next;
3753 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3754 if_delmulti_locked(ifp, ifma, 0);
3755 IF_ADDR_WUNLOCK(ifp);
3759 if_delmulti_ifma(struct ifmultiaddr *ifma)
3761 if_delmulti_ifma_flags(ifma, 0);
3765 * Delete a multicast group membership by group membership pointer.
3766 * Network-layer protocol domains must use this routine.
3768 * It is safe to call this routine if the ifp disappeared.
3771 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3775 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3777 IN_MULTI_LIST_UNLOCK_ASSERT();
3779 ifp = ifma->ifma_ifp;
3782 printf("%s: ifma_ifp seems to be detached\n", __func__);
3786 IFNET_RLOCK_NOSLEEP();
3787 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3792 IFNET_RUNLOCK_NOSLEEP();
3796 * If and only if the ifnet instance exists: Acquire the address lock.
3801 lastref = if_delmulti_locked(ifp, ifma, flags);
3805 * If and only if the ifnet instance exists:
3806 * Release the address lock.
3807 * If the group was left: update the hardware hash filter.
3809 IF_ADDR_WUNLOCK(ifp);
3810 if (lastref && ifp->if_ioctl != NULL) {
3811 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3817 * Perform deletion of network-layer and/or link-layer multicast address.
3819 * Return 0 if the reference count was decremented.
3820 * Return 1 if the final reference was released, indicating that the
3821 * hardware hash filter should be reprogrammed.
3824 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3826 struct ifmultiaddr *ll_ifma;
3828 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3829 KASSERT(ifma->ifma_ifp == ifp,
3830 ("%s: inconsistent ifp %p", __func__, ifp));
3831 IF_ADDR_WLOCK_ASSERT(ifp);
3834 ifp = ifma->ifma_ifp;
3835 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3838 * If the ifnet is detaching, null out references to ifnet,
3839 * so that upper protocol layers will notice, and not attempt
3840 * to obtain locks for an ifnet which no longer exists. The
3841 * routing socket announcement must happen before the ifnet
3842 * instance is detached from the system.
3846 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3849 * ifp may already be nulled out if we are being reentered
3850 * to delete the ll_ifma.
3853 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3854 ifma->ifma_ifp = NULL;
3858 if (--ifma->ifma_refcount > 0)
3861 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3862 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3863 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3866 * If this ifma is a network-layer ifma, a link-layer ifma may
3867 * have been associated with it. Release it first if so.
3869 ll_ifma = ifma->ifma_llifma;
3870 if (ll_ifma != NULL) {
3871 KASSERT(ifma->ifma_lladdr != NULL,
3872 ("%s: llifma w/o lladdr", __func__));
3874 ll_ifma->ifma_ifp = NULL; /* XXX */
3875 if (--ll_ifma->ifma_refcount == 0) {
3877 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3878 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3880 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3883 if_freemulti(ll_ifma);
3888 struct ifmultiaddr *ifmatmp;
3890 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3891 MPASS(ifma != ifmatmp);
3896 * The last reference to this instance of struct ifmultiaddr
3897 * was released; the hardware should be notified of this change.
3903 * Set the link layer address on an interface.
3905 * At this time we only support certain types of interfaces,
3906 * and we don't allow the length of the address to change.
3908 * Set noinline to be dtrace-friendly
3911 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3913 struct sockaddr_dl *sdl;
3926 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3931 if (len != sdl->sdl_alen) { /* don't allow length to change */
3935 switch (ifp->if_type) {
3940 case IFT_IEEE8023ADLAG:
3941 bcopy(lladdr, LLADDR(sdl), len);
3949 * If the interface is already up, we need
3950 * to re-init it in order to reprogram its
3954 if ((ifp->if_flags & IFF_UP) != 0) {
3955 if (ifp->if_ioctl) {
3956 ifp->if_flags &= ~IFF_UP;
3957 ifr.ifr_flags = ifp->if_flags & 0xffff;
3958 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3959 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3960 ifp->if_flags |= IFF_UP;
3961 ifr.ifr_flags = ifp->if_flags & 0xffff;
3962 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3963 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3966 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3974 * Compat function for handling basic encapsulation requests.
3975 * Not converted stacks (FDDI, IB, ..) supports traditional
3976 * output model: ARP (and other similar L2 protocols) are handled
3977 * inside output routine, arpresolve/nd6_resolve() returns MAC
3978 * address instead of full prepend.
3980 * This function creates calculated header==MAC for IPv4/IPv6 and
3981 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3985 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3988 if (req->rtype != IFENCAP_LL)
3989 return (EOPNOTSUPP);
3991 if (req->bufsize < req->lladdr_len)
3994 switch (req->family) {
3999 return (EAFNOSUPPORT);
4002 /* Copy lladdr to storage as is */
4003 memmove(req->buf, req->lladdr, req->lladdr_len);
4004 req->bufsize = req->lladdr_len;
4005 req->lladdr_off = 0;
4011 * Tunnel interfaces can nest, also they may cause infinite recursion
4012 * calls when misconfigured. We'll prevent this by detecting loops.
4013 * High nesting level may cause stack exhaustion. We'll prevent this
4014 * by introducing upper limit.
4016 * Return 0, if tunnel nesting count is equal or less than limit.
4019 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
4027 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
4028 if (*(struct ifnet **)(mtag + 1) == ifp) {
4029 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
4034 if (count > limit) {
4036 "%s: if_output recursively called too many times(%d)\n",
4037 if_name(ifp), count);
4040 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
4043 *(struct ifnet **)(mtag + 1) = ifp;
4044 m_tag_prepend(m, mtag);
4049 * Get the link layer address that was read from the hardware at attach.
4051 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4052 * their component interfaces as IFT_IEEE8023ADLAG.
4055 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4058 if (ifp->if_hw_addr == NULL)
4061 switch (ifp->if_type) {
4063 case IFT_IEEE8023ADLAG:
4064 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4072 * The name argument must be a pointer to storage which will last as
4073 * long as the interface does. For physical devices, the result of
4074 * device_get_name(dev) is a good choice and for pseudo-devices a
4075 * static string works well.
4078 if_initname(struct ifnet *ifp, const char *name, int unit)
4080 ifp->if_dname = name;
4081 ifp->if_dunit = unit;
4082 if (unit != IF_DUNIT_NONE)
4083 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4085 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4089 if_printf(struct ifnet *ifp, const char *fmt, ...)
4094 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4096 vlog(LOG_INFO, if_fmt, ap);
4102 if_start(struct ifnet *ifp)
4105 (*(ifp)->if_start)(ifp);
4109 * Backwards compatibility interface for drivers
4110 * that have not implemented it
4113 if_transmit(struct ifnet *ifp, struct mbuf *m)
4117 IFQ_HANDOFF(ifp, m, error);
4122 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4129 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4134 if (_IF_QFULL(ifq)) {
4136 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4141 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4142 if (m->m_flags & (M_BCAST|M_MCAST))
4143 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4144 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4146 _IF_ENQUEUE(ifq, m);
4148 if (ifp != NULL && !active)
4149 (*(ifp)->if_start)(ifp);
4154 if_register_com_alloc(u_char type,
4155 if_com_alloc_t *a, if_com_free_t *f)
4158 KASSERT(if_com_alloc[type] == NULL,
4159 ("if_register_com_alloc: %d already registered", type));
4160 KASSERT(if_com_free[type] == NULL,
4161 ("if_register_com_alloc: %d free already registered", type));
4163 if_com_alloc[type] = a;
4164 if_com_free[type] = f;
4168 if_deregister_com_alloc(u_char type)
4171 KASSERT(if_com_alloc[type] != NULL,
4172 ("if_deregister_com_alloc: %d not registered", type));
4173 KASSERT(if_com_free[type] != NULL,
4174 ("if_deregister_com_alloc: %d free not registered", type));
4177 * Ensure all pending EPOCH(9) callbacks have been executed. This
4178 * fixes issues about late invocation of if_destroy(), which leads
4179 * to memory leak from if_com_alloc[type] allocated if_l2com.
4181 epoch_drain_callbacks(net_epoch_preempt);
4183 if_com_alloc[type] = NULL;
4184 if_com_free[type] = NULL;
4187 /* API for driver access to network stack owned ifnet.*/
4189 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4193 oldbrate = ifp->if_baudrate;
4194 ifp->if_baudrate = baudrate;
4199 if_getbaudrate(if_t ifp)
4202 return (((struct ifnet *)ifp)->if_baudrate);
4206 if_setcapabilities(if_t ifp, int capabilities)
4208 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4213 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4215 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4216 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4222 if_getcapabilities(if_t ifp)
4224 return ((struct ifnet *)ifp)->if_capabilities;
4228 if_setcapenable(if_t ifp, int capabilities)
4230 ((struct ifnet *)ifp)->if_capenable = capabilities;
4235 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4238 ((struct ifnet *)ifp)->if_capenable |= setcap;
4240 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4246 if_getdname(if_t ifp)
4248 return ((struct ifnet *)ifp)->if_dname;
4252 if_togglecapenable(if_t ifp, int togglecap)
4254 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4259 if_getcapenable(if_t ifp)
4261 return ((struct ifnet *)ifp)->if_capenable;
4265 * This is largely undesirable because it ties ifnet to a device, but does
4266 * provide flexiblity for an embedded product vendor. Should be used with
4267 * the understanding that it violates the interface boundaries, and should be
4268 * a last resort only.
4271 if_setdev(if_t ifp, void *dev)
4277 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4279 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4280 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4286 if_getdrvflags(if_t ifp)
4288 return ((struct ifnet *)ifp)->if_drv_flags;
4292 if_setdrvflags(if_t ifp, int flags)
4294 ((struct ifnet *)ifp)->if_drv_flags = flags;
4300 if_setflags(if_t ifp, int flags)
4302 ((struct ifnet *)ifp)->if_flags = flags;
4307 if_setflagbits(if_t ifp, int set, int clear)
4309 ((struct ifnet *)ifp)->if_flags |= set;
4310 ((struct ifnet *)ifp)->if_flags &= ~clear;
4316 if_getflags(if_t ifp)
4318 return ((struct ifnet *)ifp)->if_flags;
4322 if_clearhwassist(if_t ifp)
4324 ((struct ifnet *)ifp)->if_hwassist = 0;
4329 if_sethwassistbits(if_t ifp, int toset, int toclear)
4331 ((struct ifnet *)ifp)->if_hwassist |= toset;
4332 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4338 if_sethwassist(if_t ifp, int hwassist_bit)
4340 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4345 if_gethwassist(if_t ifp)
4347 return ((struct ifnet *)ifp)->if_hwassist;
4351 if_setmtu(if_t ifp, int mtu)
4353 ((struct ifnet *)ifp)->if_mtu = mtu;
4360 return ((struct ifnet *)ifp)->if_mtu;
4364 if_getmtu_family(if_t ifp, int family)
4368 for (dp = domains; dp; dp = dp->dom_next) {
4369 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4370 return (dp->dom_ifmtu((struct ifnet *)ifp));
4373 return (((struct ifnet *)ifp)->if_mtu);
4377 * Methods for drivers to access interface unicast and multicast
4378 * link level addresses. Driver shall not know 'struct ifaddr' neither
4379 * 'struct ifmultiaddr'.
4382 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4391 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4392 if (ifa->ifa_addr->sa_family != AF_LINK)
4394 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4397 IF_ADDR_RUNLOCK(ifp);
4403 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4405 struct ifmultiaddr *ifma;
4412 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4413 if (ifma->ifma_addr->sa_family != AF_LINK)
4415 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4418 IF_ADDR_RUNLOCK(ifp);
4424 if_setsoftc(if_t ifp, void *softc)
4426 ((struct ifnet *)ifp)->if_softc = softc;
4431 if_getsoftc(if_t ifp)
4433 return ((struct ifnet *)ifp)->if_softc;
4437 if_setrcvif(struct mbuf *m, if_t ifp)
4439 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4443 if_setvtag(struct mbuf *m, uint16_t tag)
4445 m->m_pkthdr.ether_vtag = tag;
4449 if_getvtag(struct mbuf *m)
4452 return (m->m_pkthdr.ether_vtag);
4456 if_sendq_empty(if_t ifp)
4458 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4462 if_getifaddr(if_t ifp)
4464 return ((struct ifnet *)ifp)->if_addr;
4468 if_getamcount(if_t ifp)
4470 return ((struct ifnet *)ifp)->if_amcount;
4475 if_setsendqready(if_t ifp)
4477 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4482 if_setsendqlen(if_t ifp, int tx_desc_count)
4484 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4485 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4491 if_vlantrunkinuse(if_t ifp)
4493 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4497 if_input(if_t ifp, struct mbuf* sendmp)
4499 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4505 #ifndef ETH_ADDR_LEN
4506 #define ETH_ADDR_LEN 6
4510 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4512 struct ifmultiaddr *ifma;
4513 uint8_t *lmta = (uint8_t *)mta;
4516 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4517 if (ifma->ifma_addr->sa_family != AF_LINK)
4523 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4524 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4533 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4537 if_maddr_rlock(ifp);
4538 error = if_setupmultiaddr(ifp, mta, cnt, max);
4539 if_maddr_runlock(ifp);
4544 if_multiaddr_count(if_t ifp, int max)
4546 struct ifmultiaddr *ifma;
4550 if_maddr_rlock(ifp);
4551 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4552 if (ifma->ifma_addr->sa_family != AF_LINK)
4558 if_maddr_runlock(ifp);
4563 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4565 struct ifmultiaddr *ifma;
4568 if_maddr_rlock(ifp);
4569 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4570 cnt += filter(arg, ifma, cnt);
4571 if_maddr_runlock(ifp);
4576 if_dequeue(if_t ifp)
4579 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4585 if_sendq_prepend(if_t ifp, struct mbuf *m)
4587 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4592 if_setifheaderlen(if_t ifp, int len)
4594 ((struct ifnet *)ifp)->if_hdrlen = len;
4599 if_getlladdr(if_t ifp)
4601 return (IF_LLADDR((struct ifnet *)ifp));
4605 if_gethandle(u_char type)
4607 return (if_alloc(type));
4611 if_bpfmtap(if_t ifh, struct mbuf *m)
4613 struct ifnet *ifp = (struct ifnet *)ifh;
4619 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4621 struct ifnet *ifp = (struct ifnet *)ifh;
4623 ETHER_BPF_MTAP(ifp, m);
4627 if_vlancap(if_t ifh)
4629 struct ifnet *ifp = (struct ifnet *)ifh;
4630 VLAN_CAPABILITIES(ifp);
4634 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4637 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4642 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4645 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4650 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4653 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4658 if_gethwtsomax(if_t ifp)
4661 return (((struct ifnet *)ifp)->if_hw_tsomax);
4665 if_gethwtsomaxsegcount(if_t ifp)
4668 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4672 if_gethwtsomaxsegsize(if_t ifp)
4675 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4679 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4681 ((struct ifnet *)ifp)->if_init = init_fn;
4685 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4687 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4691 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4693 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4697 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4699 ((struct ifnet *)ifp)->if_transmit = start_fn;
4702 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4704 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4709 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4712 ifp->if_get_counter = fn;
4715 /* Revisit these - These are inline functions originally. */
4717 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4719 return drbr_inuse(ifh, br);
4723 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4725 return drbr_dequeue(ifh, br);
4729 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4731 return drbr_needs_enqueue(ifh, br);
4735 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4737 return drbr_enqueue(ifh, br, m);