2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
35 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
44 #include <sys/epoch.h>
46 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
54 #include <sys/refcount.h>
55 #include <sys/module.h>
56 #include <sys/rwlock.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/taskqueue.h>
62 #include <sys/domain.h>
65 #include <sys/sched.h>
68 #include <machine/stdarg.h>
72 #include <net/ethernet.h>
74 #include <net/if_arp.h>
75 #include <net/if_clone.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/if_media.h>
80 #include <net/if_vlan_var.h>
81 #include <net/radix.h>
82 #include <net/route.h>
85 #if defined(INET) || defined(INET6)
86 #include <net/ethernet.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_carp.h>
92 #include <netinet/if_ether.h>
93 #include <netinet/netdump/netdump.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/in6_ifattach.h>
99 #endif /* INET || INET6 */
101 #include <security/mac/mac_framework.h>
104 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
105 * and ifr_ifru when it is used in SIOCGIFCONF.
107 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
108 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
110 __read_mostly epoch_t net_epoch_preempt;
111 __read_mostly epoch_t net_epoch;
112 #ifdef COMPAT_FREEBSD32
113 #include <sys/mount.h>
114 #include <compat/freebsd32/freebsd32.h>
116 struct ifreq_buffer32 {
117 uint32_t length; /* (size_t) */
118 uint32_t buffer; /* (void *) */
122 * Interface request structure used for socket
123 * ioctl's. All interface ioctl's must have parameter
124 * definitions which begin with ifr_name. The
125 * remainder may be interface specific.
128 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
130 struct sockaddr ifru_addr;
131 struct sockaddr ifru_dstaddr;
132 struct sockaddr ifru_broadaddr;
133 struct ifreq_buffer32 ifru_buffer;
144 u_char ifru_vlan_pcp;
147 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
148 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
149 __offsetof(struct ifreq32, ifr_ifru));
151 struct ifgroupreq32 {
152 char ifgr_name[IFNAMSIZ];
155 char ifgru_group[IFNAMSIZ];
156 uint32_t ifgru_groups;
160 struct ifmediareq32 {
161 char ifm_name[IFNAMSIZ];
167 uint32_t ifm_ulist; /* (int *) */
169 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
170 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
172 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
173 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
174 #else /* !COMPAT_FREEBSD32 */
175 #define _CASE_IOC_IFGROUPREQ_32(cmd)
176 #endif /* !COMPAT_FREEBSD32 */
178 #define CASE_IOC_IFGROUPREQ(cmd) \
179 _CASE_IOC_IFGROUPREQ_32(cmd) \
184 #ifdef COMPAT_FREEBSD32
185 struct ifreq32 ifr32;
189 union ifgroupreq_union {
190 struct ifgroupreq ifgr;
191 #ifdef COMPAT_FREEBSD32
192 struct ifgroupreq32 ifgr32;
196 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
197 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
199 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
200 &ifqmaxlen, 0, "max send queue size");
202 /* Log link state change events */
203 static int log_link_state_change = 1;
205 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
206 &log_link_state_change, 0,
207 "log interface link state change events");
209 /* Log promiscuous mode change events */
210 static int log_promisc_mode_change = 1;
212 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
213 &log_promisc_mode_change, 1,
214 "log promiscuous mode change events");
216 /* Interface description */
217 static unsigned int ifdescr_maxlen = 1024;
218 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
220 "administrative maximum length for interface description");
222 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
224 /* global sx for non-critical path ifdescr */
225 static struct sx ifdescr_sx;
226 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
228 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
229 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
230 /* These are external hooks for CARP. */
231 void (*carp_linkstate_p)(struct ifnet *ifp);
232 void (*carp_demote_adj_p)(int, char *);
233 int (*carp_master_p)(struct ifaddr *);
234 #if defined(INET) || defined(INET6)
235 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
236 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
237 const struct sockaddr *sa);
238 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
239 int (*carp_attach_p)(struct ifaddr *, int);
240 void (*carp_detach_p)(struct ifaddr *, bool);
243 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
246 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
247 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
248 const struct in6_addr *taddr);
251 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
254 * XXX: Style; these should be sorted alphabetically, and unprototyped
255 * static functions should be prototyped. Currently they are sorted by
258 static void if_attachdomain(void *);
259 static void if_attachdomain1(struct ifnet *);
260 static int ifconf(u_long, caddr_t);
261 static void *if_grow(void);
262 static void if_input_default(struct ifnet *, struct mbuf *);
263 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
264 static void if_route(struct ifnet *, int flag, int fam);
265 static int if_setflag(struct ifnet *, int, int, int *, int);
266 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
267 static void if_unroute(struct ifnet *, int flag, int fam);
268 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
269 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
270 static void do_link_state_change(void *, int);
271 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
272 static int if_getgroupmembers(struct ifgroupreq *);
273 static void if_delgroups(struct ifnet *);
274 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
275 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
276 static void if_link_ifnet(struct ifnet *);
277 static bool if_unlink_ifnet(struct ifnet *, bool);
279 static void if_vmove(struct ifnet *, struct vnet *);
284 * XXX: declare here to avoid to include many inet6 related files..
285 * should be more generalized?
287 extern void nd6_setmtu(struct ifnet *);
290 /* ipsec helper hooks */
291 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
292 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
294 VNET_DEFINE(int, if_index);
295 int ifqmaxlen = IFQ_MAXLEN;
296 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
297 VNET_DEFINE(struct ifgrouphead, ifg_head);
299 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
301 /* Table of ifnet by index. */
302 VNET_DEFINE(struct ifnet **, ifindex_table);
304 #define V_if_indexlim VNET(if_indexlim)
305 #define V_ifindex_table VNET(ifindex_table)
308 * The global network interface list (V_ifnet) and related state (such as
309 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
310 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
312 struct rwlock ifnet_rwlock;
313 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
314 struct sx ifnet_sxlock;
315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
317 struct sx ifnet_detach_sxlock;
318 SX_SYSINIT(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx");
321 * The allocation of network interfaces is a rather non-atomic affair; we
322 * need to select an index before we are ready to expose the interface for
323 * use, so will use this pointer value to indicate reservation.
325 #define IFNET_HOLD (void *)(uintptr_t)(-1)
327 static if_com_alloc_t *if_com_alloc[256];
328 static if_com_free_t *if_com_free[256];
330 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
331 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
332 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
335 ifnet_byindex_locked(u_short idx)
338 if (idx > V_if_index)
340 if (V_ifindex_table[idx] == IFNET_HOLD)
342 return (V_ifindex_table[idx]);
346 ifnet_byindex(u_short idx)
350 ifp = ifnet_byindex_locked(idx);
355 ifnet_byindex_ref(u_short idx)
359 IFNET_RLOCK_NOSLEEP();
360 ifp = ifnet_byindex_locked(idx);
361 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
362 IFNET_RUNLOCK_NOSLEEP();
366 IFNET_RUNLOCK_NOSLEEP();
371 * Allocate an ifindex array entry; return 0 on success or an error on
375 ifindex_alloc(void **old)
379 IFNET_WLOCK_ASSERT();
381 * Try to find an empty slot below V_if_index. If we fail, take the
384 for (idx = 1; idx <= V_if_index; idx++) {
385 if (V_ifindex_table[idx] == NULL)
389 /* Catch if_index overflow. */
390 if (idx >= V_if_indexlim) {
394 if (idx > V_if_index)
400 ifindex_free_locked(u_short idx)
403 IFNET_WLOCK_ASSERT();
405 V_ifindex_table[idx] = NULL;
406 while (V_if_index > 0 &&
407 V_ifindex_table[V_if_index] == NULL)
412 ifindex_free(u_short idx)
416 ifindex_free_locked(idx);
421 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
424 V_ifindex_table[idx] = ifp;
428 ifaddr_byindex(u_short idx)
431 struct ifaddr *ifa = NULL;
433 IFNET_RLOCK_NOSLEEP();
434 ifp = ifnet_byindex_locked(idx);
435 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
437 IFNET_RUNLOCK_NOSLEEP();
442 * Network interface utility routines.
444 * Routines with ifa_ifwith* names take sockaddr *'s as
449 vnet_if_init(const void *unused __unused)
453 CK_STAILQ_INIT(&V_ifnet);
454 CK_STAILQ_INIT(&V_ifg_head);
456 old = if_grow(); /* create initial table */
458 epoch_wait_preempt(net_epoch_preempt);
460 vnet_if_clone_init();
462 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
467 vnet_if_uninit(const void *unused __unused)
470 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
471 "not empty", __func__, __LINE__, &V_ifnet));
472 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
473 "not empty", __func__, __LINE__, &V_ifg_head));
475 free((caddr_t)V_ifindex_table, M_IFNET);
477 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
478 vnet_if_uninit, NULL);
482 if_link_ifnet(struct ifnet *ifp)
486 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
488 curvnet->vnet_ifcnt++;
494 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
500 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
502 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
504 ifp->if_flags |= IFF_DYING;
509 curvnet->vnet_ifcnt--;
518 vnet_if_return(const void *unused __unused)
520 struct ifnet *ifp, *nifp;
521 struct ifnet **pending;
527 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
528 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
529 * if_detach_internal(), which waits for NET_EPOCH callbacks to
530 * complete. We can't do that from within NET_EPOCH.
532 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
533 * read/write lock. We cannot hold the lock as we call if_vmove()
534 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
539 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
540 M_IFNET, M_WAITOK | M_ZERO);
542 /* Return all inherited interfaces to their parent vnets. */
543 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
544 if (ifp->if_home_vnet != ifp->if_vnet) {
545 found = if_unlink_ifnet(ifp, true);
553 for (int j = 0; j < i; j++) {
554 sx_xlock(&ifnet_detach_sxlock);
555 if_vmove(pending[j], pending[j]->if_home_vnet);
556 sx_xunlock(&ifnet_detach_sxlock);
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_slock(&ifnet_detach_sxlock);
1111 if_detach_internal(ifp, 0, NULL);
1112 sx_sunlock(&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;
1289 u_int bif_dlt, bif_hdrlen;
1294 * if_detach_internal() will call the eventhandler to notify
1295 * interface departure. That will detach if_bpf. We need to
1296 * safe the dlt and hdrlen so we can re-attach it later.
1298 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1301 * Detach from current vnet, but preserve LLADDR info, do not
1302 * mark as dead etc. so that the ifnet can be reattached later.
1303 * If we cannot find it, we lost the race to someone else.
1305 rc = if_detach_internal(ifp, 1, &ifc);
1310 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1311 * the if_index for that vnet if possible.
1313 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1314 * or we'd lock on one vnet and unlock on another.
1317 ifindex_free_locked(ifp->if_index);
1321 * Perform interface-specific reassignment tasks, if provided by
1324 if (ifp->if_reassign != NULL)
1325 ifp->if_reassign(ifp, new_vnet, NULL);
1328 * Switch to the context of the target vnet.
1330 CURVNET_SET_QUIET(new_vnet);
1333 ifp->if_index = ifindex_alloc(&old);
1334 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1336 epoch_wait_preempt(net_epoch_preempt);
1340 ifnet_setbyindex(ifp->if_index, ifp);
1343 if_attach_internal(ifp, 1, ifc);
1345 if (ifp->if_bpf == NULL)
1346 bpfattach(ifp, bif_dlt, bif_hdrlen);
1352 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1355 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1362 /* Try to find the prison within our visibility. */
1363 sx_slock(&allprison_lock);
1364 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1365 sx_sunlock(&allprison_lock);
1368 prison_hold_locked(pr);
1369 mtx_unlock(&pr->pr_mtx);
1371 /* Do not try to move the iface from and to the same prison. */
1372 if (pr->pr_vnet == ifp->if_vnet) {
1377 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1378 /* XXX Lock interfaces to avoid races. */
1379 CURVNET_SET_QUIET(pr->pr_vnet);
1380 difp = ifunit(ifname);
1387 /* Make sure the VNET is stable. */
1388 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1389 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1397 found = if_unlink_ifnet(ifp, true);
1400 /* Move the interface into the child jail/vnet. */
1401 if_vmove(ifp, pr->pr_vnet);
1403 /* Report the new if_xname back to the userland. */
1404 sprintf(ifname, "%s", ifp->if_xname);
1411 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1414 struct vnet *vnet_dst;
1419 /* Try to find the prison within our visibility. */
1420 sx_slock(&allprison_lock);
1421 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1422 sx_sunlock(&allprison_lock);
1425 prison_hold_locked(pr);
1426 mtx_unlock(&pr->pr_mtx);
1428 /* Make sure the named iface exists in the source prison/vnet. */
1429 CURVNET_SET(pr->pr_vnet);
1430 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1437 /* Do not try to move the iface from and to the same prison. */
1438 vnet_dst = TD_TO_VNET(td);
1439 if (vnet_dst == ifp->if_vnet) {
1445 /* Make sure the VNET is stable. */
1446 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1447 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1454 /* Get interface back from child jail/vnet. */
1455 found = if_unlink_ifnet(ifp, true);
1457 if_vmove(ifp, vnet_dst);
1460 /* Report the new if_xname back to the userland. */
1461 sprintf(ifname, "%s", ifp->if_xname);
1469 * Add a group to an interface
1472 if_addgroup(struct ifnet *ifp, const char *groupname)
1474 struct ifg_list *ifgl;
1475 struct ifg_group *ifg = NULL;
1476 struct ifg_member *ifgm;
1479 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1480 groupname[strlen(groupname) - 1] <= '9')
1484 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1485 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1490 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1491 M_NOWAIT)) == NULL) {
1496 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1497 M_TEMP, M_NOWAIT)) == NULL) {
1503 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1504 if (!strcmp(ifg->ifg_group, groupname))
1508 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1509 M_TEMP, M_NOWAIT)) == NULL) {
1515 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1516 ifg->ifg_refcnt = 0;
1517 CK_STAILQ_INIT(&ifg->ifg_members);
1518 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1523 ifgl->ifgl_group = ifg;
1524 ifgm->ifgm_ifp = ifp;
1527 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1528 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1529 IF_ADDR_WUNLOCK(ifp);
1534 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1535 EVENTHANDLER_INVOKE(group_change_event, groupname);
1541 * Remove a group from an interface
1544 if_delgroup(struct ifnet *ifp, const char *groupname)
1546 struct ifg_list *ifgl;
1547 struct ifg_member *ifgm;
1551 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1552 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1561 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1562 IF_ADDR_WUNLOCK(ifp);
1564 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1565 if (ifgm->ifgm_ifp == ifp)
1569 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next);
1571 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1572 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
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);
1591 * Remove an interface from all groups
1594 if_delgroups(struct ifnet *ifp)
1596 struct ifg_list *ifgl;
1597 struct ifg_member *ifgm;
1598 char groupname[IFNAMSIZ];
1602 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) {
1603 ifgl = CK_STAILQ_FIRST(&ifp->if_groups);
1605 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1608 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1609 IF_ADDR_WUNLOCK(ifp);
1611 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1612 if (ifgm->ifgm_ifp == ifp)
1616 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member,
1619 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1620 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1625 epoch_wait_preempt(net_epoch_preempt);
1628 EVENTHANDLER_INVOKE(group_detach_event,
1630 free(ifgl->ifgl_group, M_TEMP);
1632 EVENTHANDLER_INVOKE(group_change_event, groupname);
1640 ifgr_group_get(void *ifgrp)
1642 union ifgroupreq_union *ifgrup;
1645 #ifdef COMPAT_FREEBSD32
1646 if (SV_CURPROC_FLAG(SV_ILP32))
1647 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1649 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1652 static struct ifg_req *
1653 ifgr_groups_get(void *ifgrp)
1655 union ifgroupreq_union *ifgrup;
1658 #ifdef COMPAT_FREEBSD32
1659 if (SV_CURPROC_FLAG(SV_ILP32))
1660 return ((struct ifg_req *)(uintptr_t)
1661 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1663 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1667 * Stores all groups from an interface in memory pointed to by ifgr.
1670 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1673 struct ifg_list *ifgl;
1674 struct ifg_req ifgrq, *ifgp;
1676 if (ifgr->ifgr_len == 0) {
1678 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1679 ifgr->ifgr_len += sizeof(struct ifg_req);
1680 IF_ADDR_RUNLOCK(ifp);
1684 len = ifgr->ifgr_len;
1685 ifgp = ifgr_groups_get(ifgr);
1688 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1689 if (len < sizeof(ifgrq)) {
1690 IF_ADDR_RUNLOCK(ifp);
1693 bzero(&ifgrq, sizeof ifgrq);
1694 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1695 sizeof(ifgrq.ifgrq_group));
1696 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1697 IF_ADDR_RUNLOCK(ifp);
1700 len -= sizeof(ifgrq);
1703 IF_ADDR_RUNLOCK(ifp);
1709 * Stores all members of a group in memory pointed to by igfr
1712 if_getgroupmembers(struct ifgroupreq *ifgr)
1714 struct ifg_group *ifg;
1715 struct ifg_member *ifgm;
1716 struct ifg_req ifgrq, *ifgp;
1720 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1721 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1728 if (ifgr->ifgr_len == 0) {
1729 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1730 ifgr->ifgr_len += sizeof(ifgrq);
1735 len = ifgr->ifgr_len;
1736 ifgp = ifgr_groups_get(ifgr);
1737 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1738 if (len < sizeof(ifgrq)) {
1742 bzero(&ifgrq, sizeof ifgrq);
1743 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1744 sizeof(ifgrq.ifgrq_member));
1745 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1749 len -= sizeof(ifgrq);
1758 * Return counter values from counter(9)s stored in ifnet.
1761 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1764 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1766 return (counter_u64_fetch(ifp->if_counters[cnt]));
1770 * Increase an ifnet counter. Usually used for counters shared
1771 * between the stack and a driver, but function supports them all.
1774 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1777 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1779 counter_u64_add(ifp->if_counters[cnt], inc);
1783 * Copy data from ifnet to userland API structure if_data.
1786 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1789 ifd->ifi_type = ifp->if_type;
1790 ifd->ifi_physical = 0;
1791 ifd->ifi_addrlen = ifp->if_addrlen;
1792 ifd->ifi_hdrlen = ifp->if_hdrlen;
1793 ifd->ifi_link_state = ifp->if_link_state;
1795 ifd->ifi_datalen = sizeof(struct if_data);
1796 ifd->ifi_mtu = ifp->if_mtu;
1797 ifd->ifi_metric = ifp->if_metric;
1798 ifd->ifi_baudrate = ifp->if_baudrate;
1799 ifd->ifi_hwassist = ifp->if_hwassist;
1800 ifd->ifi_epoch = ifp->if_epoch;
1801 ifd->ifi_lastchange = ifp->if_lastchange;
1803 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1804 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1805 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1806 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1807 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1808 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1809 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1810 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1811 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1812 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1813 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1814 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1817 struct ifnet_read_lock {
1818 struct mtx mtx; /* lock protecting tracker below */
1819 struct epoch_tracker et;
1822 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_addr_read_lock);
1823 DPCPU_DEFINE_STATIC(struct ifnet_read_lock, ifnet_maddr_read_lock);
1826 ifnet_read_lock_init(void __unused *arg)
1828 struct ifnet_read_lock *pifrl;
1832 pifrl = DPCPU_ID_PTR(cpu, ifnet_addr_read_lock);
1833 mtx_init(&pifrl->mtx, "ifnet_addr_read_lock", NULL, MTX_DEF);
1835 pifrl = DPCPU_ID_PTR(cpu, ifnet_maddr_read_lock);
1836 mtx_init(&pifrl->mtx, "ifnet_maddr_read_lock", NULL, MTX_DEF);
1839 SYSINIT(ifnet_read_lock_init, SI_SUB_CPU + 1, SI_ORDER_FIRST, &ifnet_read_lock_init, NULL);
1842 * Wrapper functions for struct ifnet address list locking macros. These are
1843 * used by kernel modules to avoid encoding programming interface or binary
1844 * interface assumptions that may be violated when kernel-internal locking
1845 * approaches change.
1848 if_addr_rlock(struct ifnet *ifp)
1850 struct ifnet_read_lock *pifrl;
1853 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1854 mtx_lock(&pifrl->mtx);
1855 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1859 if_addr_runlock(struct ifnet *ifp)
1861 struct ifnet_read_lock *pifrl;
1863 pifrl = DPCPU_PTR(ifnet_addr_read_lock);
1865 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1866 mtx_unlock(&pifrl->mtx);
1871 if_maddr_rlock(if_t ifp)
1873 struct ifnet_read_lock *pifrl;
1876 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1877 mtx_lock(&pifrl->mtx);
1878 epoch_enter_preempt(net_epoch_preempt, &pifrl->et);
1882 if_maddr_runlock(if_t ifp)
1884 struct ifnet_read_lock *pifrl;
1886 pifrl = DPCPU_PTR(ifnet_maddr_read_lock);
1888 epoch_exit_preempt(net_epoch_preempt, &pifrl->et);
1889 mtx_unlock(&pifrl->mtx);
1894 * Initialization, destruction and refcounting functions for ifaddrs.
1897 ifa_alloc(size_t size, int flags)
1901 KASSERT(size >= sizeof(struct ifaddr),
1902 ("%s: invalid size %zu", __func__, size));
1904 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1908 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1910 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1912 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1914 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1917 refcount_init(&ifa->ifa_refcnt, 1);
1922 /* free(NULL) is okay */
1923 counter_u64_free(ifa->ifa_opackets);
1924 counter_u64_free(ifa->ifa_ipackets);
1925 counter_u64_free(ifa->ifa_obytes);
1926 counter_u64_free(ifa->ifa_ibytes);
1927 free(ifa, M_IFADDR);
1933 ifa_ref(struct ifaddr *ifa)
1936 refcount_acquire(&ifa->ifa_refcnt);
1940 ifa_destroy(epoch_context_t ctx)
1944 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1945 counter_u64_free(ifa->ifa_opackets);
1946 counter_u64_free(ifa->ifa_ipackets);
1947 counter_u64_free(ifa->ifa_obytes);
1948 counter_u64_free(ifa->ifa_ibytes);
1949 free(ifa, M_IFADDR);
1953 ifa_free(struct ifaddr *ifa)
1956 if (refcount_release(&ifa->ifa_refcnt))
1957 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1962 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1963 struct sockaddr *ia)
1966 struct rt_addrinfo info;
1967 struct sockaddr_dl null_sdl;
1972 bzero(&info, sizeof(info));
1973 if (cmd != RTM_DELETE)
1974 info.rti_ifp = V_loif;
1975 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1976 info.rti_info[RTAX_DST] = ia;
1977 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1978 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1980 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1983 !(cmd == RTM_ADD && error == EEXIST) &&
1984 !(cmd == RTM_DELETE && error == ENOENT))
1985 if_printf(ifp, "%s failed: %d\n", otype, error);
1991 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1994 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1998 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
2001 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
2005 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
2008 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
2012 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
2013 * structs used to represent other address families, it is necessary
2014 * to perform a different comparison.
2017 #define sa_dl_equal(a1, a2) \
2018 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
2019 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
2020 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
2021 CLLADDR((const struct sockaddr_dl *)(a2)), \
2022 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
2025 * Locate an interface based on a complete address.
2029 ifa_ifwithaddr(const struct sockaddr *addr)
2034 MPASS(in_epoch(net_epoch_preempt));
2035 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2036 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2037 if (ifa->ifa_addr->sa_family != addr->sa_family)
2039 if (sa_equal(addr, ifa->ifa_addr)) {
2042 /* IP6 doesn't have broadcast */
2043 if ((ifp->if_flags & IFF_BROADCAST) &&
2044 ifa->ifa_broadaddr &&
2045 ifa->ifa_broadaddr->sa_len != 0 &&
2046 sa_equal(ifa->ifa_broadaddr, addr)) {
2057 ifa_ifwithaddr_check(const struct sockaddr *addr)
2062 rc = (ifa_ifwithaddr(addr) != NULL);
2068 * Locate an interface based on the broadcast address.
2072 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
2077 MPASS(in_epoch(net_epoch_preempt));
2078 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2079 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2081 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2082 if (ifa->ifa_addr->sa_family != addr->sa_family)
2084 if ((ifp->if_flags & IFF_BROADCAST) &&
2085 ifa->ifa_broadaddr &&
2086 ifa->ifa_broadaddr->sa_len != 0 &&
2087 sa_equal(ifa->ifa_broadaddr, addr)) {
2098 * Locate the point to point interface with a given destination address.
2102 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2107 MPASS(in_epoch(net_epoch_preempt));
2108 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2109 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2111 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2113 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2114 if (ifa->ifa_addr->sa_family != addr->sa_family)
2116 if (ifa->ifa_dstaddr != NULL &&
2117 sa_equal(addr, ifa->ifa_dstaddr)) {
2128 * Find an interface on a specific network. If many, choice
2129 * is most specific found.
2132 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2136 struct ifaddr *ifa_maybe = NULL;
2137 u_int af = addr->sa_family;
2138 const char *addr_data = addr->sa_data, *cplim;
2140 MPASS(in_epoch(net_epoch_preempt));
2142 * AF_LINK addresses can be looked up directly by their index number,
2143 * so do that if we can.
2145 if (af == AF_LINK) {
2146 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2147 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2148 return (ifaddr_byindex(sdl->sdl_index));
2152 * Scan though each interface, looking for ones that have addresses
2153 * in this address family and the requested fib. Maintain a reference
2154 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2155 * kept it stable when we move onto the next interface.
2157 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2158 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2160 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2161 const char *cp, *cp2, *cp3;
2163 if (ifa->ifa_addr->sa_family != af)
2165 if (af == AF_INET &&
2166 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2168 * This is a bit broken as it doesn't
2169 * take into account that the remote end may
2170 * be a single node in the network we are
2172 * The trouble is that we don't know the
2173 * netmask for the remote end.
2175 if (ifa->ifa_dstaddr != NULL &&
2176 sa_equal(addr, ifa->ifa_dstaddr)) {
2181 * Scan all the bits in the ifa's address.
2182 * If a bit dissagrees with what we are
2183 * looking for, mask it with the netmask
2184 * to see if it really matters.
2185 * (A byte at a time)
2187 if (ifa->ifa_netmask == 0)
2190 cp2 = ifa->ifa_addr->sa_data;
2191 cp3 = ifa->ifa_netmask->sa_data;
2192 cplim = ifa->ifa_netmask->sa_len
2193 + (char *)ifa->ifa_netmask;
2195 if ((*cp++ ^ *cp2++) & *cp3++)
2196 goto next; /* next address! */
2198 * If the netmask of what we just found
2199 * is more specific than what we had before
2200 * (if we had one), or if the virtual status
2201 * of new prefix is better than of the old one,
2202 * then remember the new one before continuing
2203 * to search for an even better one.
2205 if (ifa_maybe == NULL ||
2206 ifa_preferred(ifa_maybe, ifa) ||
2207 rn_refines((caddr_t)ifa->ifa_netmask,
2208 (caddr_t)ifa_maybe->ifa_netmask)) {
2221 * Find an interface address specific to an interface best matching
2225 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2228 const char *cp, *cp2, *cp3;
2230 struct ifaddr *ifa_maybe = NULL;
2231 u_int af = addr->sa_family;
2236 MPASS(in_epoch(net_epoch_preempt));
2237 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2238 if (ifa->ifa_addr->sa_family != af)
2240 if (ifa_maybe == NULL)
2242 if (ifa->ifa_netmask == 0) {
2243 if (sa_equal(addr, ifa->ifa_addr) ||
2244 (ifa->ifa_dstaddr &&
2245 sa_equal(addr, ifa->ifa_dstaddr)))
2249 if (ifp->if_flags & IFF_POINTOPOINT) {
2250 if (sa_equal(addr, ifa->ifa_dstaddr))
2254 cp2 = ifa->ifa_addr->sa_data;
2255 cp3 = ifa->ifa_netmask->sa_data;
2256 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2257 for (; cp3 < cplim; cp3++)
2258 if ((*cp++ ^ *cp2++) & *cp3)
2270 * See whether new ifa is better than current one:
2271 * 1) A non-virtual one is preferred over virtual.
2272 * 2) A virtual in master state preferred over any other state.
2274 * Used in several address selecting functions.
2277 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2280 return (cur->ifa_carp && (!next->ifa_carp ||
2281 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2284 #include <net/if_llatbl.h>
2287 * Default action when installing a route with a Link Level gateway.
2288 * Lookup an appropriate real ifa to point to.
2289 * This should be moved to /sys/net/link.c eventually.
2292 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2294 struct ifaddr *ifa, *oifa;
2295 struct sockaddr *dst;
2298 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2299 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2302 ifa = ifaof_ifpforaddr(dst, ifp);
2310 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2311 ifa->ifa_rtrequest(cmd, rt, info);
2316 struct sockaddr_dl *
2317 link_alloc_sdl(size_t size, int flags)
2320 return (malloc(size, M_TEMP, flags));
2324 link_free_sdl(struct sockaddr *sa)
2330 * Fills in given sdl with interface basic info.
2331 * Returns pointer to filled sdl.
2333 struct sockaddr_dl *
2334 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2336 struct sockaddr_dl *sdl;
2338 sdl = (struct sockaddr_dl *)paddr;
2339 memset(sdl, 0, sizeof(struct sockaddr_dl));
2340 sdl->sdl_len = sizeof(struct sockaddr_dl);
2341 sdl->sdl_family = AF_LINK;
2342 sdl->sdl_index = ifp->if_index;
2343 sdl->sdl_type = iftype;
2349 * Mark an interface down and notify protocols of
2353 if_unroute(struct ifnet *ifp, int flag, int fam)
2357 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2359 ifp->if_flags &= ~flag;
2360 getmicrotime(&ifp->if_lastchange);
2361 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2362 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2363 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2364 ifp->if_qflush(ifp);
2367 (*carp_linkstate_p)(ifp);
2372 * Mark an interface up and notify protocols of
2376 if_route(struct ifnet *ifp, int flag, int fam)
2380 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2382 ifp->if_flags |= flag;
2383 getmicrotime(&ifp->if_lastchange);
2384 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2385 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2386 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2388 (*carp_linkstate_p)(ifp);
2395 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2396 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2397 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2398 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2399 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2400 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2401 int (*vlan_setcookie_p)(struct ifnet *, void *);
2402 void *(*vlan_cookie_p)(struct ifnet *);
2405 * Handle a change in the interface link state. To avoid LORs
2406 * between driver lock and upper layer locks, as well as possible
2407 * recursions, we post event to taskqueue, and all job
2408 * is done in static do_link_state_change().
2411 if_link_state_change(struct ifnet *ifp, int link_state)
2413 /* Return if state hasn't changed. */
2414 if (ifp->if_link_state == link_state)
2417 ifp->if_link_state = link_state;
2419 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2423 do_link_state_change(void *arg, int pending)
2425 struct ifnet *ifp = (struct ifnet *)arg;
2426 int link_state = ifp->if_link_state;
2427 CURVNET_SET(ifp->if_vnet);
2429 /* Notify that the link state has changed. */
2431 if (ifp->if_vlantrunk != NULL)
2432 (*vlan_link_state_p)(ifp);
2434 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2435 ifp->if_l2com != NULL)
2436 (*ng_ether_link_state_p)(ifp, link_state);
2438 (*carp_linkstate_p)(ifp);
2440 ifp->if_bridge_linkstate(ifp);
2442 (*lagg_linkstate_p)(ifp, link_state);
2444 if (IS_DEFAULT_VNET(curvnet))
2445 devctl_notify("IFNET", ifp->if_xname,
2446 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2449 if_printf(ifp, "%d link states coalesced\n", pending);
2450 if (log_link_state_change)
2451 if_printf(ifp, "link state changed to %s\n",
2452 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2453 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2458 * Mark an interface down and notify protocols of
2462 if_down(struct ifnet *ifp)
2465 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2466 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2470 * Mark an interface up and notify protocols of
2474 if_up(struct ifnet *ifp)
2477 if_route(ifp, IFF_UP, AF_UNSPEC);
2478 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2482 * Flush an interface queue.
2485 if_qflush(struct ifnet *ifp)
2493 if (ALTQ_IS_ENABLED(ifq))
2497 while ((m = n) != NULL) {
2508 * Map interface name to interface structure pointer, with or without
2509 * returning a reference.
2512 ifunit_ref(const char *name)
2516 IFNET_RLOCK_NOSLEEP();
2517 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2518 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2519 !(ifp->if_flags & IFF_DYING))
2524 IFNET_RUNLOCK_NOSLEEP();
2529 ifunit(const char *name)
2533 IFNET_RLOCK_NOSLEEP();
2534 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2535 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2538 IFNET_RUNLOCK_NOSLEEP();
2543 ifr_buffer_get_buffer(void *data)
2545 union ifreq_union *ifrup;
2548 #ifdef COMPAT_FREEBSD32
2549 if (SV_CURPROC_FLAG(SV_ILP32))
2550 return ((void *)(uintptr_t)
2551 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2553 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2557 ifr_buffer_set_buffer_null(void *data)
2559 union ifreq_union *ifrup;
2562 #ifdef COMPAT_FREEBSD32
2563 if (SV_CURPROC_FLAG(SV_ILP32))
2564 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2567 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2571 ifr_buffer_get_length(void *data)
2573 union ifreq_union *ifrup;
2576 #ifdef COMPAT_FREEBSD32
2577 if (SV_CURPROC_FLAG(SV_ILP32))
2578 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2580 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2584 ifr_buffer_set_length(void *data, size_t len)
2586 union ifreq_union *ifrup;
2589 #ifdef COMPAT_FREEBSD32
2590 if (SV_CURPROC_FLAG(SV_ILP32))
2591 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2594 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2598 ifr_data_get_ptr(void *ifrp)
2600 union ifreq_union *ifrup;
2603 #ifdef COMPAT_FREEBSD32
2604 if (SV_CURPROC_FLAG(SV_ILP32))
2605 return ((void *)(uintptr_t)
2606 ifrup->ifr32.ifr_ifru.ifru_data);
2608 return (ifrup->ifr.ifr_ifru.ifru_data);
2612 * Hardware specific interface ioctls.
2615 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2618 int error = 0, do_ifup = 0;
2619 int new_flags, temp_flags;
2620 size_t namelen, onamelen;
2622 char *descrbuf, *odescrbuf;
2623 char new_name[IFNAMSIZ];
2625 struct sockaddr_dl *sdl;
2627 ifr = (struct ifreq *)data;
2630 ifr->ifr_index = ifp->if_index;
2634 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2635 ifr->ifr_flags = temp_flags & 0xffff;
2636 ifr->ifr_flagshigh = temp_flags >> 16;
2640 ifr->ifr_reqcap = ifp->if_capabilities;
2641 ifr->ifr_curcap = ifp->if_capenable;
2646 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2651 ifr->ifr_metric = ifp->if_metric;
2655 ifr->ifr_mtu = ifp->if_mtu;
2659 /* XXXGL: did this ever worked? */
2665 sx_slock(&ifdescr_sx);
2666 if (ifp->if_description == NULL)
2669 /* space for terminating nul */
2670 descrlen = strlen(ifp->if_description) + 1;
2671 if (ifr_buffer_get_length(ifr) < descrlen)
2672 ifr_buffer_set_buffer_null(ifr);
2674 error = copyout(ifp->if_description,
2675 ifr_buffer_get_buffer(ifr), descrlen);
2676 ifr_buffer_set_length(ifr, descrlen);
2678 sx_sunlock(&ifdescr_sx);
2682 error = priv_check(td, PRIV_NET_SETIFDESCR);
2687 * Copy only (length-1) bytes to make sure that
2688 * if_description is always nul terminated. The
2689 * length parameter is supposed to count the
2690 * terminating nul in.
2692 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2693 return (ENAMETOOLONG);
2694 else if (ifr_buffer_get_length(ifr) == 0)
2697 descrbuf = malloc(ifr_buffer_get_length(ifr),
2698 M_IFDESCR, M_WAITOK | M_ZERO);
2699 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2700 ifr_buffer_get_length(ifr) - 1);
2702 free(descrbuf, M_IFDESCR);
2707 sx_xlock(&ifdescr_sx);
2708 odescrbuf = ifp->if_description;
2709 ifp->if_description = descrbuf;
2710 sx_xunlock(&ifdescr_sx);
2712 getmicrotime(&ifp->if_lastchange);
2713 free(odescrbuf, M_IFDESCR);
2717 ifr->ifr_fib = ifp->if_fib;
2721 error = priv_check(td, PRIV_NET_SETIFFIB);
2724 if (ifr->ifr_fib >= rt_numfibs)
2727 ifp->if_fib = ifr->ifr_fib;
2731 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2735 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2736 * check, so we don't need special handling here yet.
2738 new_flags = (ifr->ifr_flags & 0xffff) |
2739 (ifr->ifr_flagshigh << 16);
2740 if (ifp->if_flags & IFF_UP &&
2741 (new_flags & IFF_UP) == 0) {
2743 } else if (new_flags & IFF_UP &&
2744 (ifp->if_flags & IFF_UP) == 0) {
2747 /* See if permanently promiscuous mode bit is about to flip */
2748 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2749 if (new_flags & IFF_PPROMISC)
2750 ifp->if_flags |= IFF_PROMISC;
2751 else if (ifp->if_pcount == 0)
2752 ifp->if_flags &= ~IFF_PROMISC;
2753 if (log_promisc_mode_change)
2754 if_printf(ifp, "permanently promiscuous mode %s\n",
2755 ((new_flags & IFF_PPROMISC) ?
2756 "enabled" : "disabled"));
2758 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2759 (new_flags &~ IFF_CANTCHANGE);
2760 if (ifp->if_ioctl) {
2761 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2765 getmicrotime(&ifp->if_lastchange);
2769 error = priv_check(td, PRIV_NET_SETIFCAP);
2772 if (ifp->if_ioctl == NULL)
2773 return (EOPNOTSUPP);
2774 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2776 error = (*ifp->if_ioctl)(ifp, cmd, data);
2778 getmicrotime(&ifp->if_lastchange);
2783 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2788 error = priv_check(td, PRIV_NET_SETIFNAME);
2791 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2795 if (new_name[0] == '\0')
2797 if (new_name[IFNAMSIZ-1] != '\0') {
2798 new_name[IFNAMSIZ-1] = '\0';
2799 if (strlen(new_name) == IFNAMSIZ-1)
2802 if (strcmp(new_name, ifp->if_xname) == 0)
2804 if (ifunit(new_name) != NULL)
2808 * XXX: Locking. Nothing else seems to lock if_flags,
2809 * and there are numerous other races with the
2810 * ifunit() checks not being atomic with namespace
2811 * changes (renames, vmoves, if_attach, etc).
2813 ifp->if_flags |= IFF_RENAMING;
2815 /* Announce the departure of the interface. */
2816 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2817 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2819 if_printf(ifp, "changing name to '%s'\n", new_name);
2822 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2824 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2825 namelen = strlen(new_name);
2826 onamelen = sdl->sdl_nlen;
2828 * Move the address if needed. This is safe because we
2829 * allocate space for a name of length IFNAMSIZ when we
2830 * create this in if_attach().
2832 if (namelen != onamelen) {
2833 bcopy(sdl->sdl_data + onamelen,
2834 sdl->sdl_data + namelen, sdl->sdl_alen);
2836 bcopy(new_name, sdl->sdl_data, namelen);
2837 sdl->sdl_nlen = namelen;
2838 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2839 bzero(sdl->sdl_data, onamelen);
2840 while (namelen != 0)
2841 sdl->sdl_data[--namelen] = 0xff;
2842 IF_ADDR_WUNLOCK(ifp);
2844 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2845 /* Announce the return of the interface. */
2846 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2848 ifp->if_flags &= ~IFF_RENAMING;
2853 error = priv_check(td, PRIV_NET_SETIFVNET);
2856 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2861 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2864 ifp->if_metric = ifr->ifr_metric;
2865 getmicrotime(&ifp->if_lastchange);
2869 error = priv_check(td, PRIV_NET_SETIFPHYS);
2872 if (ifp->if_ioctl == NULL)
2873 return (EOPNOTSUPP);
2874 error = (*ifp->if_ioctl)(ifp, cmd, data);
2876 getmicrotime(&ifp->if_lastchange);
2881 u_long oldmtu = ifp->if_mtu;
2883 error = priv_check(td, PRIV_NET_SETIFMTU);
2886 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2888 if (ifp->if_ioctl == NULL)
2889 return (EOPNOTSUPP);
2890 error = (*ifp->if_ioctl)(ifp, cmd, data);
2892 getmicrotime(&ifp->if_lastchange);
2895 NETDUMP_REINIT(ifp);
2899 * If the link MTU changed, do network layer specific procedure.
2901 if (ifp->if_mtu != oldmtu) {
2912 if (cmd == SIOCADDMULTI)
2913 error = priv_check(td, PRIV_NET_ADDMULTI);
2915 error = priv_check(td, PRIV_NET_DELMULTI);
2919 /* Don't allow group membership on non-multicast interfaces. */
2920 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2921 return (EOPNOTSUPP);
2923 /* Don't let users screw up protocols' entries. */
2924 if (ifr->ifr_addr.sa_family != AF_LINK)
2927 if (cmd == SIOCADDMULTI) {
2928 struct ifmultiaddr *ifma;
2931 * Userland is only permitted to join groups once
2932 * via the if_addmulti() KPI, because it cannot hold
2933 * struct ifmultiaddr * between calls. It may also
2934 * lose a race while we check if the membership
2938 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2939 IF_ADDR_RUNLOCK(ifp);
2943 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2945 error = if_delmulti(ifp, &ifr->ifr_addr);
2948 getmicrotime(&ifp->if_lastchange);
2951 case SIOCSIFPHYADDR:
2952 case SIOCDIFPHYADDR:
2954 case SIOCSIFPHYADDR_IN6:
2957 case SIOCSIFGENERIC:
2958 error = priv_check(td, PRIV_NET_HWIOCTL);
2961 if (ifp->if_ioctl == NULL)
2962 return (EOPNOTSUPP);
2963 error = (*ifp->if_ioctl)(ifp, cmd, data);
2965 getmicrotime(&ifp->if_lastchange);
2969 case SIOCGIFPSRCADDR:
2970 case SIOCGIFPDSTADDR:
2973 case SIOCGIFGENERIC:
2975 case SIOCGIFRSSHASH:
2976 if (ifp->if_ioctl == NULL)
2977 return (EOPNOTSUPP);
2978 error = (*ifp->if_ioctl)(ifp, cmd, data);
2982 error = priv_check(td, PRIV_NET_SETLLADDR);
2985 error = if_setlladdr(ifp,
2986 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2990 error = if_gethwaddr(ifp, ifr);
2993 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2994 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2997 if ((error = if_addgroup(ifp,
2998 ifgr_group_get((struct ifgroupreq *)data))))
3002 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
3003 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
3007 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
3008 error = priv_check(td, PRIV_NET_DELIFGROUP);
3011 if ((error = if_delgroup(ifp,
3012 ifgr_group_get((struct ifgroupreq *)data))))
3023 #ifdef COMPAT_FREEBSD32
3031 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
3034 #ifdef COMPAT_FREEBSD32
3036 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
3038 struct ifmediareq32 *ifmr32;
3040 ifmr32 = (struct ifmediareq32 *)data;
3041 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
3042 sizeof(ifmr->ifm_name));
3043 ifmr->ifm_current = ifmr32->ifm_current;
3044 ifmr->ifm_mask = ifmr32->ifm_mask;
3045 ifmr->ifm_status = ifmr32->ifm_status;
3046 ifmr->ifm_active = ifmr32->ifm_active;
3047 ifmr->ifm_count = ifmr32->ifm_count;
3048 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
3052 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
3054 struct ifmediareq32 *ifmr32;
3056 ifmr32 = (struct ifmediareq32 *)data;
3057 ifmr32->ifm_current = ifmr->ifm_current;
3058 ifmr32->ifm_mask = ifmr->ifm_mask;
3059 ifmr32->ifm_status = ifmr->ifm_status;
3060 ifmr32->ifm_active = ifmr->ifm_active;
3061 ifmr32->ifm_count = ifmr->ifm_count;
3069 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
3071 #ifdef COMPAT_FREEBSD32
3072 caddr_t saved_data = NULL;
3073 struct ifmediareq ifmr;
3074 struct ifmediareq *ifmrp;
3084 CURVNET_SET(so->so_vnet);
3086 /* Make sure the VNET is stable. */
3087 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
3088 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3098 error = ifconf(cmd, data);
3102 #ifdef COMPAT_FREEBSD32
3105 struct ifconf32 *ifc32;
3108 ifc32 = (struct ifconf32 *)data;
3109 ifc.ifc_len = ifc32->ifc_len;
3110 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3112 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3115 ifc32->ifc_len = ifc.ifc_len;
3121 #ifdef COMPAT_FREEBSD32
3124 case SIOCGIFMEDIA32:
3125 case SIOCGIFXMEDIA32:
3127 ifmr_init(ifmrp, data);
3128 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3130 data = (caddr_t)ifmrp;
3134 ifr = (struct ifreq *)data;
3138 error = priv_check(td, PRIV_NET_SETIFVNET);
3140 error = if_vmove_reclaim(td, ifr->ifr_name,
3146 error = priv_check(td, PRIV_NET_IFCREATE);
3148 error = if_clone_create(ifr->ifr_name,
3149 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3150 ifr_data_get_ptr(ifr) : NULL);
3153 error = priv_check(td, PRIV_NET_IFDESTROY);
3156 sx_slock(&ifnet_detach_sxlock);
3157 error = if_clone_destroy(ifr->ifr_name);
3158 sx_sunlock(&ifnet_detach_sxlock);
3162 case SIOCIFGCLONERS:
3163 error = if_clone_list((struct if_clonereq *)data);
3166 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3167 error = if_getgroupmembers((struct ifgroupreq *)data);
3170 #if defined(INET) || defined(INET6)
3173 if (carp_ioctl_p == NULL)
3174 error = EPROTONOSUPPORT;
3176 error = (*carp_ioctl_p)(ifr, cmd, td);
3181 ifp = ifunit_ref(ifr->ifr_name);
3187 error = ifhwioctl(cmd, ifp, data, td);
3188 if (error != ENOIOCTL)
3191 oif_flags = ifp->if_flags;
3192 if (so->so_proto == NULL) {
3198 * Pass the request on to the socket control method, and if the
3199 * latter returns EOPNOTSUPP, directly to the interface.
3201 * Make an exception for the legacy SIOCSIF* requests. Drivers
3202 * trust SIOCSIFADDR et al to come from an already privileged
3203 * layer, and do not perform any credentials checks or input
3206 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3208 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3209 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3210 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3211 error = (*ifp->if_ioctl)(ifp, cmd, data);
3213 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3215 if (ifp->if_flags & IFF_UP)
3223 #ifdef COMPAT_FREEBSD32
3224 if (ifmrp != NULL) {
3225 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3226 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3229 ifmr_update(ifmrp, data);
3237 * The code common to handling reference counted flags,
3238 * e.g., in ifpromisc() and if_allmulti().
3239 * The "pflag" argument can specify a permanent mode flag to check,
3240 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3242 * Only to be used on stack-owned flags, not driver-owned flags.
3245 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3249 int oldflags, oldcount;
3251 /* Sanity checks to catch programming errors */
3252 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3253 ("%s: setting driver-owned flag %d", __func__, flag));
3256 KASSERT(*refcount >= 0,
3257 ("%s: increment negative refcount %d for flag %d",
3258 __func__, *refcount, flag));
3260 KASSERT(*refcount > 0,
3261 ("%s: decrement non-positive refcount %d for flag %d",
3262 __func__, *refcount, flag));
3264 /* In case this mode is permanent, just touch refcount */
3265 if (ifp->if_flags & pflag) {
3266 *refcount += onswitch ? 1 : -1;
3270 /* Save ifnet parameters for if_ioctl() may fail */
3271 oldcount = *refcount;
3272 oldflags = ifp->if_flags;
3275 * See if we aren't the only and touching refcount is enough.
3276 * Actually toggle interface flag if we are the first or last.
3281 ifp->if_flags |= flag;
3285 ifp->if_flags &= ~flag;
3288 /* Call down the driver since we've changed interface flags */
3289 if (ifp->if_ioctl == NULL) {
3293 ifr.ifr_flags = ifp->if_flags & 0xffff;
3294 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3295 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3298 /* Notify userland that interface flags have changed */
3303 /* Recover after driver error */
3304 *refcount = oldcount;
3305 ifp->if_flags = oldflags;
3310 * Set/clear promiscuous mode on interface ifp based on the truth value
3311 * of pswitch. The calls are reference counted so that only the first
3312 * "on" request actually has an effect, as does the final "off" request.
3313 * Results are undefined if the "off" and "on" requests are not matched.
3316 ifpromisc(struct ifnet *ifp, int pswitch)
3319 int oldflags = ifp->if_flags;
3321 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3322 &ifp->if_pcount, pswitch);
3323 /* If promiscuous mode status has changed, log a message */
3324 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3325 log_promisc_mode_change)
3326 if_printf(ifp, "promiscuous mode %s\n",
3327 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3332 * Return interface configuration
3333 * of system. List may be used
3334 * in later ioctl's (above) to get
3335 * other information.
3339 ifconf(u_long cmd, caddr_t data)
3341 struct ifconf *ifc = (struct ifconf *)data;
3346 int error, full = 0, valid_len, max_len;
3348 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3349 max_len = MAXPHYS - 1;
3351 /* Prevent hostile input from being able to crash the system */
3352 if (ifc->ifc_len <= 0)
3356 if (ifc->ifc_len <= max_len) {
3357 max_len = ifc->ifc_len;
3360 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3365 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3369 * Zero the ifr to make sure we don't disclose the contents
3372 memset(&ifr, 0, sizeof(ifr));
3374 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3375 >= sizeof(ifr.ifr_name)) {
3378 return (ENAMETOOLONG);
3383 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3384 struct sockaddr *sa = ifa->ifa_addr;
3386 if (prison_if(curthread->td_ucred, sa) != 0)
3389 if (sa->sa_len <= sizeof(*sa)) {
3390 if (sa->sa_len < sizeof(*sa)) {
3391 memset(&ifr.ifr_ifru.ifru_addr, 0,
3392 sizeof(ifr.ifr_ifru.ifru_addr));
3393 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3396 ifr.ifr_ifru.ifru_addr = *sa;
3397 sbuf_bcat(sb, &ifr, sizeof(ifr));
3398 max_len += sizeof(ifr);
3401 offsetof(struct ifreq, ifr_addr));
3402 max_len += offsetof(struct ifreq, ifr_addr);
3403 sbuf_bcat(sb, sa, sa->sa_len);
3404 max_len += sa->sa_len;
3407 if (sbuf_error(sb) == 0)
3408 valid_len = sbuf_len(sb);
3410 IF_ADDR_RUNLOCK(ifp);
3412 sbuf_bcat(sb, &ifr, sizeof(ifr));
3413 max_len += sizeof(ifr);
3415 if (sbuf_error(sb) == 0)
3416 valid_len = sbuf_len(sb);
3422 * If we didn't allocate enough space (uncommon), try again. If
3423 * we have already allocated as much space as we are allowed,
3424 * return what we've got.
3426 if (valid_len != max_len && !full) {
3431 ifc->ifc_len = valid_len;
3433 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3439 * Just like ifpromisc(), but for all-multicast-reception mode.
3442 if_allmulti(struct ifnet *ifp, int onswitch)
3445 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3448 struct ifmultiaddr *
3449 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3451 struct ifmultiaddr *ifma;
3453 IF_ADDR_LOCK_ASSERT(ifp);
3455 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3456 if (sa->sa_family == AF_LINK) {
3457 if (sa_dl_equal(ifma->ifma_addr, sa))
3460 if (sa_equal(ifma->ifma_addr, sa))
3469 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3470 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3471 * the ifnet multicast address list here, so the caller must do that and
3472 * other setup work (such as notifying the device driver). The reference
3473 * count is initialized to 1.
3475 static struct ifmultiaddr *
3476 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3479 struct ifmultiaddr *ifma;
3480 struct sockaddr *dupsa;
3482 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3487 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3488 if (dupsa == NULL) {
3489 free(ifma, M_IFMADDR);
3492 bcopy(sa, dupsa, sa->sa_len);
3493 ifma->ifma_addr = dupsa;
3495 ifma->ifma_ifp = ifp;
3496 ifma->ifma_refcount = 1;
3497 ifma->ifma_protospec = NULL;
3500 ifma->ifma_lladdr = NULL;
3504 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3505 if (dupsa == NULL) {
3506 free(ifma->ifma_addr, M_IFMADDR);
3507 free(ifma, M_IFMADDR);
3510 bcopy(llsa, dupsa, llsa->sa_len);
3511 ifma->ifma_lladdr = dupsa;
3517 * if_freemulti: free ifmultiaddr structure and possibly attached related
3518 * addresses. The caller is responsible for implementing reference
3519 * counting, notifying the driver, handling routing messages, and releasing
3520 * any dependent link layer state.
3522 #ifdef MCAST_VERBOSE
3523 extern void kdb_backtrace(void);
3526 if_freemulti_internal(struct ifmultiaddr *ifma)
3529 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3530 ifma->ifma_refcount));
3532 if (ifma->ifma_lladdr != NULL)
3533 free(ifma->ifma_lladdr, M_IFMADDR);
3534 #ifdef MCAST_VERBOSE
3536 printf("%s freeing ifma: %p\n", __func__, ifma);
3538 free(ifma->ifma_addr, M_IFMADDR);
3539 free(ifma, M_IFMADDR);
3543 if_destroymulti(epoch_context_t ctx)
3545 struct ifmultiaddr *ifma;
3547 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3548 if_freemulti_internal(ifma);
3552 if_freemulti(struct ifmultiaddr *ifma)
3554 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3555 ifma->ifma_refcount));
3557 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3562 * Register an additional multicast address with a network interface.
3564 * - If the address is already present, bump the reference count on the
3565 * address and return.
3566 * - If the address is not link-layer, look up a link layer address.
3567 * - Allocate address structures for one or both addresses, and attach to the
3568 * multicast address list on the interface. If automatically adding a link
3569 * layer address, the protocol address will own a reference to the link
3570 * layer address, to be freed when it is freed.
3571 * - Notify the network device driver of an addition to the multicast address
3574 * 'sa' points to caller-owned memory with the desired multicast address.
3576 * 'retifma' will be used to return a pointer to the resulting multicast
3577 * address reference, if desired.
3580 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3581 struct ifmultiaddr **retifma)
3583 struct ifmultiaddr *ifma, *ll_ifma;
3584 struct sockaddr *llsa;
3585 struct sockaddr_dl sdl;
3589 IN_MULTI_LIST_UNLOCK_ASSERT();
3592 IN6_MULTI_LIST_UNLOCK_ASSERT();
3595 * If the address is already present, return a new reference to it;
3596 * otherwise, allocate storage and set up a new address.
3599 ifma = if_findmulti(ifp, sa);
3601 ifma->ifma_refcount++;
3602 if (retifma != NULL)
3604 IF_ADDR_WUNLOCK(ifp);
3609 * The address isn't already present; resolve the protocol address
3610 * into a link layer address, and then look that up, bump its
3611 * refcount or allocate an ifma for that also.
3612 * Most link layer resolving functions returns address data which
3613 * fits inside default sockaddr_dl structure. However callback
3614 * can allocate another sockaddr structure, in that case we need to
3619 if (ifp->if_resolvemulti != NULL) {
3620 /* Provide called function with buffer size information */
3621 sdl.sdl_len = sizeof(sdl);
3622 llsa = (struct sockaddr *)&sdl;
3623 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3629 * Allocate the new address. Don't hook it up yet, as we may also
3630 * need to allocate a link layer multicast address.
3632 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3639 * If a link layer address is found, we'll need to see if it's
3640 * already present in the address list, or allocate is as well.
3641 * When this block finishes, the link layer address will be on the
3645 ll_ifma = if_findmulti(ifp, llsa);
3646 if (ll_ifma == NULL) {
3647 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3648 if (ll_ifma == NULL) {
3649 --ifma->ifma_refcount;
3654 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3655 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3658 ll_ifma->ifma_refcount++;
3659 ifma->ifma_llifma = ll_ifma;
3663 * We now have a new multicast address, ifma, and possibly a new or
3664 * referenced link layer address. Add the primary address to the
3665 * ifnet address list.
3667 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3668 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3670 if (retifma != NULL)
3674 * Must generate the message while holding the lock so that 'ifma'
3675 * pointer is still valid.
3677 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3678 IF_ADDR_WUNLOCK(ifp);
3681 * We are certain we have added something, so call down to the
3682 * interface to let them know about it.
3684 if (ifp->if_ioctl != NULL) {
3685 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3688 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3689 link_free_sdl(llsa);
3694 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3695 link_free_sdl(llsa);
3698 IF_ADDR_WUNLOCK(ifp);
3703 * Delete a multicast group membership by network-layer group address.
3705 * Returns ENOENT if the entry could not be found. If ifp no longer
3706 * exists, results are undefined. This entry point should only be used
3707 * from subsystems which do appropriate locking to hold ifp for the
3708 * duration of the call.
3709 * Network-layer protocol domains must use if_delmulti_ifma().
3712 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3714 struct ifmultiaddr *ifma;
3719 IFNET_RLOCK_NOSLEEP();
3720 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3725 IFNET_RUNLOCK_NOSLEEP();
3727 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3734 ifma = if_findmulti(ifp, sa);
3736 lastref = if_delmulti_locked(ifp, ifma, 0);
3737 IF_ADDR_WUNLOCK(ifp);
3742 if (lastref && ifp->if_ioctl != NULL) {
3743 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3750 * Delete all multicast group membership for an interface.
3751 * Should be used to quickly flush all multicast filters.
3754 if_delallmulti(struct ifnet *ifp)
3756 struct ifmultiaddr *ifma;
3757 struct ifmultiaddr *next;
3760 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3761 if_delmulti_locked(ifp, ifma, 0);
3762 IF_ADDR_WUNLOCK(ifp);
3766 if_delmulti_ifma(struct ifmultiaddr *ifma)
3768 if_delmulti_ifma_flags(ifma, 0);
3772 * Delete a multicast group membership by group membership pointer.
3773 * Network-layer protocol domains must use this routine.
3775 * It is safe to call this routine if the ifp disappeared.
3778 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3782 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3784 IN_MULTI_LIST_UNLOCK_ASSERT();
3786 ifp = ifma->ifma_ifp;
3789 printf("%s: ifma_ifp seems to be detached\n", __func__);
3793 IFNET_RLOCK_NOSLEEP();
3794 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3799 IFNET_RUNLOCK_NOSLEEP();
3803 * If and only if the ifnet instance exists: Acquire the address lock.
3808 lastref = if_delmulti_locked(ifp, ifma, flags);
3812 * If and only if the ifnet instance exists:
3813 * Release the address lock.
3814 * If the group was left: update the hardware hash filter.
3816 IF_ADDR_WUNLOCK(ifp);
3817 if (lastref && ifp->if_ioctl != NULL) {
3818 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3824 * Perform deletion of network-layer and/or link-layer multicast address.
3826 * Return 0 if the reference count was decremented.
3827 * Return 1 if the final reference was released, indicating that the
3828 * hardware hash filter should be reprogrammed.
3831 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3833 struct ifmultiaddr *ll_ifma;
3835 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3836 KASSERT(ifma->ifma_ifp == ifp,
3837 ("%s: inconsistent ifp %p", __func__, ifp));
3838 IF_ADDR_WLOCK_ASSERT(ifp);
3841 ifp = ifma->ifma_ifp;
3842 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3845 * If the ifnet is detaching, null out references to ifnet,
3846 * so that upper protocol layers will notice, and not attempt
3847 * to obtain locks for an ifnet which no longer exists. The
3848 * routing socket announcement must happen before the ifnet
3849 * instance is detached from the system.
3853 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3856 * ifp may already be nulled out if we are being reentered
3857 * to delete the ll_ifma.
3860 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3861 ifma->ifma_ifp = NULL;
3865 if (--ifma->ifma_refcount > 0)
3868 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3869 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3870 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3873 * If this ifma is a network-layer ifma, a link-layer ifma may
3874 * have been associated with it. Release it first if so.
3876 ll_ifma = ifma->ifma_llifma;
3877 if (ll_ifma != NULL) {
3878 KASSERT(ifma->ifma_lladdr != NULL,
3879 ("%s: llifma w/o lladdr", __func__));
3881 ll_ifma->ifma_ifp = NULL; /* XXX */
3882 if (--ll_ifma->ifma_refcount == 0) {
3884 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3885 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3887 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3890 if_freemulti(ll_ifma);
3895 struct ifmultiaddr *ifmatmp;
3897 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3898 MPASS(ifma != ifmatmp);
3903 * The last reference to this instance of struct ifmultiaddr
3904 * was released; the hardware should be notified of this change.
3910 * Set the link layer address on an interface.
3912 * At this time we only support certain types of interfaces,
3913 * and we don't allow the length of the address to change.
3915 * Set noinline to be dtrace-friendly
3918 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3920 struct sockaddr_dl *sdl;
3933 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3938 if (len != sdl->sdl_alen) { /* don't allow length to change */
3942 switch (ifp->if_type) {
3947 case IFT_IEEE8023ADLAG:
3948 bcopy(lladdr, LLADDR(sdl), len);
3956 * If the interface is already up, we need
3957 * to re-init it in order to reprogram its
3961 if ((ifp->if_flags & IFF_UP) != 0) {
3962 if (ifp->if_ioctl) {
3963 ifp->if_flags &= ~IFF_UP;
3964 ifr.ifr_flags = ifp->if_flags & 0xffff;
3965 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3966 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3967 ifp->if_flags |= IFF_UP;
3968 ifr.ifr_flags = ifp->if_flags & 0xffff;
3969 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3970 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3973 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3981 * Compat function for handling basic encapsulation requests.
3982 * Not converted stacks (FDDI, IB, ..) supports traditional
3983 * output model: ARP (and other similar L2 protocols) are handled
3984 * inside output routine, arpresolve/nd6_resolve() returns MAC
3985 * address instead of full prepend.
3987 * This function creates calculated header==MAC for IPv4/IPv6 and
3988 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3992 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3995 if (req->rtype != IFENCAP_LL)
3996 return (EOPNOTSUPP);
3998 if (req->bufsize < req->lladdr_len)
4001 switch (req->family) {
4006 return (EAFNOSUPPORT);
4009 /* Copy lladdr to storage as is */
4010 memmove(req->buf, req->lladdr, req->lladdr_len);
4011 req->bufsize = req->lladdr_len;
4012 req->lladdr_off = 0;
4018 * Tunnel interfaces can nest, also they may cause infinite recursion
4019 * calls when misconfigured. We'll prevent this by detecting loops.
4020 * High nesting level may cause stack exhaustion. We'll prevent this
4021 * by introducing upper limit.
4023 * Return 0, if tunnel nesting count is equal or less than limit.
4026 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
4034 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
4035 if (*(struct ifnet **)(mtag + 1) == ifp) {
4036 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
4041 if (count > limit) {
4043 "%s: if_output recursively called too many times(%d)\n",
4044 if_name(ifp), count);
4047 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
4050 *(struct ifnet **)(mtag + 1) = ifp;
4051 m_tag_prepend(m, mtag);
4056 * Get the link layer address that was read from the hardware at attach.
4058 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4059 * their component interfaces as IFT_IEEE8023ADLAG.
4062 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4065 if (ifp->if_hw_addr == NULL)
4068 switch (ifp->if_type) {
4070 case IFT_IEEE8023ADLAG:
4071 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4079 * The name argument must be a pointer to storage which will last as
4080 * long as the interface does. For physical devices, the result of
4081 * device_get_name(dev) is a good choice and for pseudo-devices a
4082 * static string works well.
4085 if_initname(struct ifnet *ifp, const char *name, int unit)
4087 ifp->if_dname = name;
4088 ifp->if_dunit = unit;
4089 if (unit != IF_DUNIT_NONE)
4090 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4092 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4096 if_printf(struct ifnet *ifp, const char *fmt, ...)
4101 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4103 vlog(LOG_INFO, if_fmt, ap);
4109 if_start(struct ifnet *ifp)
4112 (*(ifp)->if_start)(ifp);
4116 * Backwards compatibility interface for drivers
4117 * that have not implemented it
4120 if_transmit(struct ifnet *ifp, struct mbuf *m)
4124 IFQ_HANDOFF(ifp, m, error);
4129 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4136 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4141 if (_IF_QFULL(ifq)) {
4143 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4148 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4149 if (m->m_flags & (M_BCAST|M_MCAST))
4150 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4151 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4153 _IF_ENQUEUE(ifq, m);
4155 if (ifp != NULL && !active)
4156 (*(ifp)->if_start)(ifp);
4161 if_register_com_alloc(u_char type,
4162 if_com_alloc_t *a, if_com_free_t *f)
4165 KASSERT(if_com_alloc[type] == NULL,
4166 ("if_register_com_alloc: %d already registered", type));
4167 KASSERT(if_com_free[type] == NULL,
4168 ("if_register_com_alloc: %d free already registered", type));
4170 if_com_alloc[type] = a;
4171 if_com_free[type] = f;
4175 if_deregister_com_alloc(u_char type)
4178 KASSERT(if_com_alloc[type] != NULL,
4179 ("if_deregister_com_alloc: %d not registered", type));
4180 KASSERT(if_com_free[type] != NULL,
4181 ("if_deregister_com_alloc: %d free not registered", type));
4182 if_com_alloc[type] = NULL;
4183 if_com_free[type] = NULL;
4186 /* API for driver access to network stack owned ifnet.*/
4188 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4192 oldbrate = ifp->if_baudrate;
4193 ifp->if_baudrate = baudrate;
4198 if_getbaudrate(if_t ifp)
4201 return (((struct ifnet *)ifp)->if_baudrate);
4205 if_setcapabilities(if_t ifp, int capabilities)
4207 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4212 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4214 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4215 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4221 if_getcapabilities(if_t ifp)
4223 return ((struct ifnet *)ifp)->if_capabilities;
4227 if_setcapenable(if_t ifp, int capabilities)
4229 ((struct ifnet *)ifp)->if_capenable = capabilities;
4234 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4237 ((struct ifnet *)ifp)->if_capenable |= setcap;
4239 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4245 if_getdname(if_t ifp)
4247 return ((struct ifnet *)ifp)->if_dname;
4251 if_togglecapenable(if_t ifp, int togglecap)
4253 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4258 if_getcapenable(if_t ifp)
4260 return ((struct ifnet *)ifp)->if_capenable;
4264 * This is largely undesirable because it ties ifnet to a device, but does
4265 * provide flexiblity for an embedded product vendor. Should be used with
4266 * the understanding that it violates the interface boundaries, and should be
4267 * a last resort only.
4270 if_setdev(if_t ifp, void *dev)
4276 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4278 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4279 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4285 if_getdrvflags(if_t ifp)
4287 return ((struct ifnet *)ifp)->if_drv_flags;
4291 if_setdrvflags(if_t ifp, int flags)
4293 ((struct ifnet *)ifp)->if_drv_flags = flags;
4299 if_setflags(if_t ifp, int flags)
4301 ((struct ifnet *)ifp)->if_flags = flags;
4306 if_setflagbits(if_t ifp, int set, int clear)
4308 ((struct ifnet *)ifp)->if_flags |= set;
4309 ((struct ifnet *)ifp)->if_flags &= ~clear;
4315 if_getflags(if_t ifp)
4317 return ((struct ifnet *)ifp)->if_flags;
4321 if_clearhwassist(if_t ifp)
4323 ((struct ifnet *)ifp)->if_hwassist = 0;
4328 if_sethwassistbits(if_t ifp, int toset, int toclear)
4330 ((struct ifnet *)ifp)->if_hwassist |= toset;
4331 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4337 if_sethwassist(if_t ifp, int hwassist_bit)
4339 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4344 if_gethwassist(if_t ifp)
4346 return ((struct ifnet *)ifp)->if_hwassist;
4350 if_setmtu(if_t ifp, int mtu)
4352 ((struct ifnet *)ifp)->if_mtu = mtu;
4359 return ((struct ifnet *)ifp)->if_mtu;
4363 if_getmtu_family(if_t ifp, int family)
4367 for (dp = domains; dp; dp = dp->dom_next) {
4368 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4369 return (dp->dom_ifmtu((struct ifnet *)ifp));
4372 return (((struct ifnet *)ifp)->if_mtu);
4376 if_setsoftc(if_t ifp, void *softc)
4378 ((struct ifnet *)ifp)->if_softc = softc;
4383 if_getsoftc(if_t ifp)
4385 return ((struct ifnet *)ifp)->if_softc;
4389 if_setrcvif(struct mbuf *m, if_t ifp)
4391 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4395 if_setvtag(struct mbuf *m, uint16_t tag)
4397 m->m_pkthdr.ether_vtag = tag;
4401 if_getvtag(struct mbuf *m)
4404 return (m->m_pkthdr.ether_vtag);
4408 if_sendq_empty(if_t ifp)
4410 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4414 if_getifaddr(if_t ifp)
4416 return ((struct ifnet *)ifp)->if_addr;
4420 if_getamcount(if_t ifp)
4422 return ((struct ifnet *)ifp)->if_amcount;
4427 if_setsendqready(if_t ifp)
4429 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4434 if_setsendqlen(if_t ifp, int tx_desc_count)
4436 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4437 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4443 if_vlantrunkinuse(if_t ifp)
4445 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4449 if_input(if_t ifp, struct mbuf* sendmp)
4451 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4457 #ifndef ETH_ADDR_LEN
4458 #define ETH_ADDR_LEN 6
4462 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4464 struct ifmultiaddr *ifma;
4465 uint8_t *lmta = (uint8_t *)mta;
4468 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4469 if (ifma->ifma_addr->sa_family != AF_LINK)
4475 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4476 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4485 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4489 if_maddr_rlock(ifp);
4490 error = if_setupmultiaddr(ifp, mta, cnt, max);
4491 if_maddr_runlock(ifp);
4496 if_multiaddr_count(if_t ifp, int max)
4498 struct ifmultiaddr *ifma;
4502 if_maddr_rlock(ifp);
4503 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4504 if (ifma->ifma_addr->sa_family != AF_LINK)
4510 if_maddr_runlock(ifp);
4515 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4517 struct ifmultiaddr *ifma;
4520 if_maddr_rlock(ifp);
4521 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4522 cnt += filter(arg, ifma, cnt);
4523 if_maddr_runlock(ifp);
4528 if_dequeue(if_t ifp)
4531 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4537 if_sendq_prepend(if_t ifp, struct mbuf *m)
4539 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4544 if_setifheaderlen(if_t ifp, int len)
4546 ((struct ifnet *)ifp)->if_hdrlen = len;
4551 if_getlladdr(if_t ifp)
4553 return (IF_LLADDR((struct ifnet *)ifp));
4557 if_gethandle(u_char type)
4559 return (if_alloc(type));
4563 if_bpfmtap(if_t ifh, struct mbuf *m)
4565 struct ifnet *ifp = (struct ifnet *)ifh;
4571 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4573 struct ifnet *ifp = (struct ifnet *)ifh;
4575 ETHER_BPF_MTAP(ifp, m);
4579 if_vlancap(if_t ifh)
4581 struct ifnet *ifp = (struct ifnet *)ifh;
4582 VLAN_CAPABILITIES(ifp);
4586 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4589 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4594 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4597 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4602 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4605 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4610 if_gethwtsomax(if_t ifp)
4613 return (((struct ifnet *)ifp)->if_hw_tsomax);
4617 if_gethwtsomaxsegcount(if_t ifp)
4620 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4624 if_gethwtsomaxsegsize(if_t ifp)
4627 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4631 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4633 ((struct ifnet *)ifp)->if_init = init_fn;
4637 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4639 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4643 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4645 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4649 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4651 ((struct ifnet *)ifp)->if_transmit = start_fn;
4654 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4656 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4661 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4664 ifp->if_get_counter = fn;
4667 /* Revisit these - These are inline functions originally. */
4669 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4671 return drbr_inuse(ifh, br);
4675 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4677 return drbr_dequeue(ifh, br);
4681 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4683 return drbr_needs_enqueue(ifh, br);
4687 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4689 return drbr_enqueue(ifh, br, m);