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>
66 #include <machine/stdarg.h>
70 #include <net/ethernet.h>
72 #include <net/if_arp.h>
73 #include <net/if_clone.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_var.h>
77 #include <net/if_media.h>
78 #include <net/if_vlan_var.h>
79 #include <net/radix.h>
80 #include <net/route.h>
83 #if defined(INET) || defined(INET6)
84 #include <net/ethernet.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_carp.h>
90 #include <netinet/if_ether.h>
91 #include <netinet/netdump/netdump.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet6/in6_ifattach.h>
97 #endif /* INET || INET6 */
99 #include <security/mac/mac_framework.h>
102 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
103 * and ifr_ifru when it is used in SIOCGIFCONF.
105 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
106 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
108 __read_mostly epoch_t net_epoch_preempt;
109 __read_mostly epoch_t net_epoch;
110 #ifdef COMPAT_FREEBSD32
111 #include <sys/mount.h>
112 #include <compat/freebsd32/freebsd32.h>
114 struct ifreq_buffer32 {
115 uint32_t length; /* (size_t) */
116 uint32_t buffer; /* (void *) */
120 * Interface request structure used for socket
121 * ioctl's. All interface ioctl's must have parameter
122 * definitions which begin with ifr_name. The
123 * remainder may be interface specific.
126 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
128 struct sockaddr ifru_addr;
129 struct sockaddr ifru_dstaddr;
130 struct sockaddr ifru_broadaddr;
131 struct ifreq_buffer32 ifru_buffer;
142 u_char ifru_vlan_pcp;
145 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
146 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
147 __offsetof(struct ifreq32, ifr_ifru));
149 struct ifgroupreq32 {
150 char ifgr_name[IFNAMSIZ];
153 char ifgru_group[IFNAMSIZ];
154 uint32_t ifgru_groups;
158 struct ifmediareq32 {
159 char ifm_name[IFNAMSIZ];
165 uint32_t ifm_ulist; /* (int *) */
167 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
168 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
170 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
171 _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
172 #else /* !COMPAT_FREEBSD32 */
173 #define _CASE_IOC_IFGROUPREQ_32(cmd)
174 #endif /* !COMPAT_FREEBSD32 */
176 #define CASE_IOC_IFGROUPREQ(cmd) \
177 _CASE_IOC_IFGROUPREQ_32(cmd) \
182 #ifdef COMPAT_FREEBSD32
183 struct ifreq32 ifr32;
187 union ifgroupreq_union {
188 struct ifgroupreq ifgr;
189 #ifdef COMPAT_FREEBSD32
190 struct ifgroupreq32 ifgr32;
194 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
195 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
197 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
198 &ifqmaxlen, 0, "max send queue size");
200 /* Log link state change events */
201 static int log_link_state_change = 1;
203 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
204 &log_link_state_change, 0,
205 "log interface link state change events");
207 /* Log promiscuous mode change events */
208 static int log_promisc_mode_change = 1;
210 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
211 &log_promisc_mode_change, 1,
212 "log promiscuous mode change events");
214 /* Interface description */
215 static unsigned int ifdescr_maxlen = 1024;
216 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
218 "administrative maximum length for interface description");
220 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
222 /* global sx for non-critical path ifdescr */
223 static struct sx ifdescr_sx;
224 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
226 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
227 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
228 /* These are external hooks for CARP. */
229 void (*carp_linkstate_p)(struct ifnet *ifp);
230 void (*carp_demote_adj_p)(int, char *);
231 int (*carp_master_p)(struct ifaddr *);
232 #if defined(INET) || defined(INET6)
233 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
234 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
235 const struct sockaddr *sa);
236 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
237 int (*carp_attach_p)(struct ifaddr *, int);
238 void (*carp_detach_p)(struct ifaddr *, bool);
241 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
244 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
245 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
246 const struct in6_addr *taddr);
249 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
252 * XXX: Style; these should be sorted alphabetically, and unprototyped
253 * static functions should be prototyped. Currently they are sorted by
256 static void if_attachdomain(void *);
257 static void if_attachdomain1(struct ifnet *);
258 static int ifconf(u_long, caddr_t);
259 static void *if_grow(void);
260 static void if_input_default(struct ifnet *, struct mbuf *);
261 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
262 static void if_route(struct ifnet *, int flag, int fam);
263 static int if_setflag(struct ifnet *, int, int, int *, int);
264 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
265 static void if_unroute(struct ifnet *, int flag, int fam);
266 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
267 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
268 static void do_link_state_change(void *, int);
269 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
270 static int if_getgroupmembers(struct ifgroupreq *);
271 static void if_delgroups(struct ifnet *);
272 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
273 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
275 static void if_vmove(struct ifnet *, struct vnet *);
280 * XXX: declare here to avoid to include many inet6 related files..
281 * should be more generalized?
283 extern void nd6_setmtu(struct ifnet *);
286 /* ipsec helper hooks */
287 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
288 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
290 VNET_DEFINE(int, if_index);
291 int ifqmaxlen = IFQ_MAXLEN;
292 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
293 VNET_DEFINE(struct ifgrouphead, ifg_head);
295 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
297 /* Table of ifnet by index. */
298 VNET_DEFINE(struct ifnet **, ifindex_table);
300 #define V_if_indexlim VNET(if_indexlim)
301 #define V_ifindex_table VNET(ifindex_table)
304 * The global network interface list (V_ifnet) and related state (such as
305 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
306 * an rwlock. Either may be acquired shared to stablize the list, but both
307 * must be acquired writable to modify the list. This model allows us to
308 * both stablize the interface list during interrupt thread processing, but
309 * also to stablize it over long-running ioctls, without introducing priority
310 * inversions and deadlocks.
312 struct rwlock ifnet_rwlock;
313 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
314 struct sx ifnet_sxlock;
315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
318 * The allocation of network interfaces is a rather non-atomic affair; we
319 * need to select an index before we are ready to expose the interface for
320 * use, so will use this pointer value to indicate reservation.
322 #define IFNET_HOLD (void *)(uintptr_t)(-1)
324 static if_com_alloc_t *if_com_alloc[256];
325 static if_com_free_t *if_com_free[256];
327 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
328 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
329 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
332 ifnet_byindex_locked(u_short idx)
335 if (idx > V_if_index)
337 if (V_ifindex_table[idx] == IFNET_HOLD)
339 return (V_ifindex_table[idx]);
343 ifnet_byindex(u_short idx)
347 ifp = ifnet_byindex_locked(idx);
352 ifnet_byindex_ref(u_short idx)
354 struct epoch_tracker et;
358 ifp = ifnet_byindex_locked(idx);
359 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
369 * Allocate an ifindex array entry; return 0 on success or an error on
373 ifindex_alloc(void **old)
377 IFNET_WLOCK_ASSERT();
379 * Try to find an empty slot below V_if_index. If we fail, take the
382 for (idx = 1; idx <= V_if_index; idx++) {
383 if (V_ifindex_table[idx] == NULL)
387 /* Catch if_index overflow. */
388 if (idx >= V_if_indexlim) {
392 if (idx > V_if_index)
398 ifindex_free_locked(u_short idx)
401 IFNET_WLOCK_ASSERT();
403 V_ifindex_table[idx] = NULL;
404 while (V_if_index > 0 &&
405 V_ifindex_table[V_if_index] == NULL)
410 ifindex_free(u_short idx)
414 ifindex_free_locked(idx);
419 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
422 V_ifindex_table[idx] = ifp;
426 ifaddr_byindex(u_short idx)
428 struct epoch_tracker et;
430 struct ifaddr *ifa = NULL;
433 ifp = ifnet_byindex_locked(idx);
434 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
441 * Network interface utility routines.
443 * Routines with ifa_ifwith* names take sockaddr *'s as
448 vnet_if_init(const void *unused __unused)
452 CK_STAILQ_INIT(&V_ifnet);
453 CK_STAILQ_INIT(&V_ifg_head);
455 old = if_grow(); /* create initial table */
457 epoch_wait_preempt(net_epoch_preempt);
459 vnet_if_clone_init();
461 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
466 vnet_if_uninit(const void *unused __unused)
469 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
470 "not empty", __func__, __LINE__, &V_ifnet));
471 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
472 "not empty", __func__, __LINE__, &V_ifg_head));
474 free((caddr_t)V_ifindex_table, M_IFNET);
476 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
477 vnet_if_uninit, NULL);
480 vnet_if_return(const void *unused __unused)
482 struct ifnet *ifp, *nifp;
484 /* Return all inherited interfaces to their parent vnets. */
485 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
486 if (ifp->if_home_vnet != ifp->if_vnet)
487 if_vmove(ifp, ifp->if_home_vnet);
490 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
491 vnet_if_return, NULL);
504 IFNET_WLOCK_ASSERT();
505 oldlim = V_if_indexlim;
507 n = (oldlim << 1) * sizeof(*e);
508 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
510 if (V_if_indexlim != oldlim) {
514 if (V_ifindex_table != NULL) {
515 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
516 old = V_ifindex_table;
524 * Allocate a struct ifnet and an index for an interface. A layer 2
525 * common structure will also be allocated if an allocation routine is
526 * registered for the passed type.
529 if_alloc(u_char type)
535 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
538 idx = ifindex_alloc(&old);
539 if (__predict_false(idx == USHRT_MAX)) {
541 epoch_wait_preempt(net_epoch_preempt);
545 ifnet_setbyindex(idx, IFNET_HOLD);
549 ifp->if_alloctype = type;
551 ifp->if_vnet = curvnet;
553 if (if_com_alloc[type] != NULL) {
554 ifp->if_l2com = if_com_alloc[type](type, ifp);
555 if (ifp->if_l2com == NULL) {
562 IF_ADDR_LOCK_INIT(ifp);
563 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
564 ifp->if_afdata_initialized = 0;
565 IF_AFDATA_LOCK_INIT(ifp);
566 CK_STAILQ_INIT(&ifp->if_addrhead);
567 CK_STAILQ_INIT(&ifp->if_multiaddrs);
568 CK_STAILQ_INIT(&ifp->if_groups);
572 ifq_init(&ifp->if_snd, ifp);
574 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
575 for (int i = 0; i < IFCOUNTERS; i++)
576 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
577 ifp->if_get_counter = if_get_counter_default;
578 ifp->if_pcp = IFNET_PCP_NONE;
579 ifnet_setbyindex(ifp->if_index, ifp);
584 * Do the actual work of freeing a struct ifnet, and layer 2 common
585 * structure. This call is made when the last reference to an
586 * interface is released.
589 if_free_internal(struct ifnet *ifp)
592 KASSERT((ifp->if_flags & IFF_DYING),
593 ("if_free_internal: interface not dying"));
595 if (if_com_free[ifp->if_alloctype] != NULL)
596 if_com_free[ifp->if_alloctype](ifp->if_l2com,
600 mac_ifnet_destroy(ifp);
602 IF_AFDATA_DESTROY(ifp);
603 IF_ADDR_LOCK_DESTROY(ifp);
604 ifq_delete(&ifp->if_snd);
606 for (int i = 0; i < IFCOUNTERS; i++)
607 counter_u64_free(ifp->if_counters[i]);
609 free(ifp->if_description, M_IFDESCR);
610 free(ifp->if_hw_addr, M_IFADDR);
615 if_destroy(epoch_context_t ctx)
619 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
620 if_free_internal(ifp);
624 * Deregister an interface and free the associated storage.
627 if_free(struct ifnet *ifp)
630 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
632 CURVNET_SET_QUIET(ifp->if_vnet);
634 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
635 ("%s: freeing unallocated ifnet", ifp->if_xname));
637 ifindex_free_locked(ifp->if_index);
640 if (refcount_release(&ifp->if_refcount))
641 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
646 * Interfaces to keep an ifnet type-stable despite the possibility of the
647 * driver calling if_free(). If there are additional references, we defer
648 * freeing the underlying data structure.
651 if_ref(struct ifnet *ifp)
654 /* We don't assert the ifnet list lock here, but arguably should. */
655 refcount_acquire(&ifp->if_refcount);
659 if_rele(struct ifnet *ifp)
662 if (!refcount_release(&ifp->if_refcount))
664 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
668 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
671 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
673 if (ifq->ifq_maxlen == 0)
674 ifq->ifq_maxlen = ifqmaxlen;
677 ifq->altq_disc = NULL;
678 ifq->altq_flags &= ALTQF_CANTCHANGE;
679 ifq->altq_tbr = NULL;
684 ifq_delete(struct ifaltq *ifq)
686 mtx_destroy(&ifq->ifq_mtx);
690 * Perform generic interface initialization tasks and attach the interface
691 * to the list of "active" interfaces. If vmove flag is set on entry
692 * to if_attach_internal(), perform only a limited subset of initialization
693 * tasks, given that we are moving from one vnet to another an ifnet which
694 * has already been fully initialized.
696 * Note that if_detach_internal() removes group membership unconditionally
697 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
698 * Thus, when if_vmove() is applied to a cloned interface, group membership
699 * is lost while a cloned one always joins a group whose name is
700 * ifc->ifc_name. To recover this after if_detach_internal() and
701 * if_attach_internal(), the cloner should be specified to
702 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
703 * attempts to join a group whose name is ifc->ifc_name.
706 * - The decision to return void and thus require this function to
707 * succeed is questionable.
708 * - We should probably do more sanity checking. For instance we don't
709 * do anything to insure if_xname is unique or non-empty.
712 if_attach(struct ifnet *ifp)
715 if_attach_internal(ifp, 0, NULL);
719 * Compute the least common TSO limit.
722 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
725 * 1) If there is no limit currently, take the limit from
726 * the network adapter.
728 * 2) If the network adapter has a limit below the current
731 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
732 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
733 pmax->tsomaxbytes = ifp->if_hw_tsomax;
735 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
736 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
737 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
739 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
740 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
741 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
746 * Update TSO limit of a network adapter.
748 * Returns zero if no change. Else non-zero.
751 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
754 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
755 ifp->if_hw_tsomax = pmax->tsomaxbytes;
758 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
759 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
762 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
763 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
770 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
772 unsigned socksize, ifasize;
773 int namelen, masklen;
774 struct sockaddr_dl *sdl;
777 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
778 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
782 ifp->if_vnet = curvnet;
783 if (ifp->if_home_vnet == NULL)
784 ifp->if_home_vnet = curvnet;
787 if_addgroup(ifp, IFG_ALL);
789 /* Restore group membership for cloned interfaces. */
790 if (vmove && ifc != NULL)
791 if_clone_addgroup(ifp, ifc);
793 getmicrotime(&ifp->if_lastchange);
794 ifp->if_epoch = time_uptime;
796 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
797 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
798 ("transmit and qflush must both either be set or both be NULL"));
799 if (ifp->if_transmit == NULL) {
800 ifp->if_transmit = if_transmit;
801 ifp->if_qflush = if_qflush;
803 if (ifp->if_input == NULL)
804 ifp->if_input = if_input_default;
806 if (ifp->if_requestencap == NULL)
807 ifp->if_requestencap = if_requestencap_default;
811 mac_ifnet_create(ifp);
815 * Create a Link Level name for this device.
817 namelen = strlen(ifp->if_xname);
819 * Always save enough space for any possiable name so we
820 * can do a rename in place later.
822 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
823 socksize = masklen + ifp->if_addrlen;
824 if (socksize < sizeof(*sdl))
825 socksize = sizeof(*sdl);
826 socksize = roundup2(socksize, sizeof(long));
827 ifasize = sizeof(*ifa) + 2 * socksize;
828 ifa = ifa_alloc(ifasize, M_WAITOK);
829 sdl = (struct sockaddr_dl *)(ifa + 1);
830 sdl->sdl_len = socksize;
831 sdl->sdl_family = AF_LINK;
832 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
833 sdl->sdl_nlen = namelen;
834 sdl->sdl_index = ifp->if_index;
835 sdl->sdl_type = ifp->if_type;
838 ifa->ifa_rtrequest = link_rtrequest;
839 ifa->ifa_addr = (struct sockaddr *)sdl;
840 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
841 ifa->ifa_netmask = (struct sockaddr *)sdl;
842 sdl->sdl_len = masklen;
844 sdl->sdl_data[--namelen] = 0xff;
845 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
846 /* Reliably crash if used uninitialized. */
847 ifp->if_broadcastaddr = NULL;
849 if (ifp->if_type == IFT_ETHER) {
850 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
854 #if defined(INET) || defined(INET6)
855 /* Use defaults for TSO, if nothing is set */
856 if (ifp->if_hw_tsomax == 0 &&
857 ifp->if_hw_tsomaxsegcount == 0 &&
858 ifp->if_hw_tsomaxsegsize == 0) {
860 * The TSO defaults needs to be such that an
861 * NFS mbuf list of 35 mbufs totalling just
862 * below 64K works and that a chain of mbufs
863 * can be defragged into at most 32 segments:
865 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
866 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
867 ifp->if_hw_tsomaxsegcount = 35;
868 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
870 /* XXX some drivers set IFCAP_TSO after ethernet attach */
871 if (ifp->if_capabilities & IFCAP_TSO) {
872 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
874 ifp->if_hw_tsomaxsegcount,
875 ifp->if_hw_tsomaxsegsize);
883 * Update the interface index in the link layer address
886 for (ifa = ifp->if_addr; ifa != NULL;
887 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
888 if (ifa->ifa_addr->sa_family == AF_LINK) {
889 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
890 sdl->sdl_index = ifp->if_index;
897 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
899 curvnet->vnet_ifcnt++;
903 if (domain_init_status >= 2)
904 if_attachdomain1(ifp);
906 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
907 if (IS_DEFAULT_VNET(curvnet))
908 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
910 /* Announce the interface. */
911 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
915 if_epochalloc(void *dummy __unused)
918 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
919 net_epoch = epoch_alloc(0);
921 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
922 if_epochalloc, NULL);
925 if_attachdomain(void *dummy)
929 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
930 if_attachdomain1(ifp);
932 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
933 if_attachdomain, NULL);
936 if_attachdomain1(struct ifnet *ifp)
941 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
942 * cannot lock ifp->if_afdata initialization, entirely.
945 if (ifp->if_afdata_initialized >= domain_init_status) {
946 IF_AFDATA_UNLOCK(ifp);
947 log(LOG_WARNING, "%s called more than once on %s\n",
948 __func__, ifp->if_xname);
951 ifp->if_afdata_initialized = domain_init_status;
952 IF_AFDATA_UNLOCK(ifp);
954 /* address family dependent data region */
955 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
956 for (dp = domains; dp; dp = dp->dom_next) {
957 if (dp->dom_ifattach)
958 ifp->if_afdata[dp->dom_family] =
959 (*dp->dom_ifattach)(ifp);
964 * Remove any unicast or broadcast network addresses from an interface.
967 if_purgeaddrs(struct ifnet *ifp)
972 struct epoch_tracker et;
975 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
976 if (ifa->ifa_addr->sa_family != AF_LINK)
984 /* XXX: Ugly!! ad hoc just for INET */
985 if (ifa->ifa_addr->sa_family == AF_INET) {
986 struct ifaliasreq ifr;
988 bzero(&ifr, sizeof(ifr));
989 ifr.ifra_addr = *ifa->ifa_addr;
990 if (ifa->ifa_dstaddr)
991 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
992 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
998 if (ifa->ifa_addr->sa_family == AF_INET6) {
1000 /* ifp_addrhead is already updated */
1005 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1006 IF_ADDR_WUNLOCK(ifp);
1012 * Remove any multicast network addresses from an interface when an ifnet
1016 if_purgemaddrs(struct ifnet *ifp)
1018 struct ifmultiaddr *ifma;
1021 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1022 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1023 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1024 if_delmulti_locked(ifp, ifma, 1);
1026 IF_ADDR_WUNLOCK(ifp);
1030 * Detach an interface, removing it from the list of "active" interfaces.
1031 * If vmove flag is set on entry to if_detach_internal(), perform only a
1032 * limited subset of cleanup tasks, given that we are moving an ifnet from
1033 * one vnet to another, where it must be fully operational.
1035 * XXXRW: There are some significant questions about event ordering, and
1036 * how to prevent things from starting to use the interface during detach.
1039 if_detach(struct ifnet *ifp)
1042 CURVNET_SET_QUIET(ifp->if_vnet);
1043 if_detach_internal(ifp, 0, NULL);
1048 * The vmove flag, if set, indicates that we are called from a callpath
1049 * that is moving an interface to a different vnet instance.
1051 * The shutdown flag, if set, indicates that we are called in the
1052 * process of shutting down a vnet instance. Currently only the
1053 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1054 * on a vnet instance shutdown without this flag being set, e.g., when
1055 * the cloned interfaces are destoyed as first thing of teardown.
1058 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1068 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1069 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1072 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1074 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1076 ifp->if_flags |= IFF_DYING;
1083 * While we would want to panic here, we cannot
1084 * guarantee that the interface is indeed still on
1085 * the list given we don't hold locks all the way.
1090 panic("%s: ifp=%p not on the ifnet tailq %p",
1091 __func__, ifp, &V_ifnet);
1093 return; /* XXX this should panic as well? */
1098 * At this point we know the interface still was on the ifnet list
1099 * and we removed it so we are in a stable state.
1102 curvnet->vnet_ifcnt--;
1104 epoch_wait_preempt(net_epoch_preempt);
1106 * In any case (destroy or vmove) detach us from the groups
1107 * and remove/wait for pending events on the taskq.
1108 * XXX-BZ in theory an interface could still enqueue a taskq change?
1112 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1115 * Check if this is a cloned interface or not. Must do even if
1116 * shutting down as a if_vmove_reclaim() would move the ifp and
1117 * the if_clone_addgroup() will have a corrupted string overwise
1118 * from a gibberish pointer.
1120 if (vmove && ifcp != NULL)
1121 *ifcp = if_clone_findifc(ifp);
1127 * On VNET shutdown abort here as the stack teardown will do all
1128 * the work top-down for us.
1131 /* Give interface users the chance to clean up. */
1132 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1135 * In case of a vmove we are done here without error.
1136 * If we would signal an error it would lead to the same
1137 * abort as if we did not find the ifnet anymore.
1138 * if_detach() calls us in void context and does not care
1139 * about an early abort notification, so life is splendid :)
1141 goto finish_vnet_shutdown;
1146 * At this point we are not tearing down a VNET and are either
1147 * going to destroy or vmove the interface and have to cleanup
1152 * Remove routes and flush queues.
1155 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1156 altq_disable(&ifp->if_snd);
1157 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1158 altq_detach(&ifp->if_snd);
1169 * Remove all IPv6 kernel structs related to ifp. This should be done
1170 * before removing routing entries below, since IPv6 interface direct
1171 * routes are expected to be removed by the IPv6-specific kernel API.
1172 * Otherwise, the kernel will detect some inconsistency and bark it.
1176 if_purgemaddrs(ifp);
1178 /* Announce that the interface is gone. */
1179 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1180 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1181 if (IS_DEFAULT_VNET(curvnet))
1182 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1186 * Prevent further calls into the device driver via ifnet.
1191 * Clean up all addresses.
1194 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1195 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1196 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1197 IF_ADDR_WUNLOCK(ifp);
1200 IF_ADDR_WUNLOCK(ifp);
1203 rt_flushifroutes(ifp);
1206 finish_vnet_shutdown:
1209 * We cannot hold the lock over dom_ifdetach calls as they might
1210 * sleep, for example trying to drain a callout, thus open up the
1211 * theoretical race with re-attaching.
1213 IF_AFDATA_LOCK(ifp);
1214 i = ifp->if_afdata_initialized;
1215 ifp->if_afdata_initialized = 0;
1216 IF_AFDATA_UNLOCK(ifp);
1217 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1218 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1219 (*dp->dom_ifdetach)(ifp,
1220 ifp->if_afdata[dp->dom_family]);
1221 ifp->if_afdata[dp->dom_family] = NULL;
1230 * if_vmove() performs a limited version of if_detach() in current
1231 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1232 * An attempt is made to shrink if_index in current vnet, find an
1233 * unused if_index in target vnet and calls if_grow() if necessary,
1234 * and finally find an unused if_xname for the target vnet.
1237 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1239 struct if_clone *ifc;
1240 u_int bif_dlt, bif_hdrlen;
1245 * if_detach_internal() will call the eventhandler to notify
1246 * interface departure. That will detach if_bpf. We need to
1247 * safe the dlt and hdrlen so we can re-attach it later.
1249 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1252 * Detach from current vnet, but preserve LLADDR info, do not
1253 * mark as dead etc. so that the ifnet can be reattached later.
1254 * If we cannot find it, we lost the race to someone else.
1256 rc = if_detach_internal(ifp, 1, &ifc);
1261 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1262 * the if_index for that vnet if possible.
1264 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1265 * or we'd lock on one vnet and unlock on another.
1268 ifindex_free_locked(ifp->if_index);
1272 * Perform interface-specific reassignment tasks, if provided by
1275 if (ifp->if_reassign != NULL)
1276 ifp->if_reassign(ifp, new_vnet, NULL);
1279 * Switch to the context of the target vnet.
1281 CURVNET_SET_QUIET(new_vnet);
1284 ifp->if_index = ifindex_alloc(&old);
1285 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1287 epoch_wait_preempt(net_epoch_preempt);
1291 ifnet_setbyindex(ifp->if_index, ifp);
1294 if_attach_internal(ifp, 1, ifc);
1296 if (ifp->if_bpf == NULL)
1297 bpfattach(ifp, bif_dlt, bif_hdrlen);
1303 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1306 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1312 /* Try to find the prison within our visibility. */
1313 sx_slock(&allprison_lock);
1314 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1315 sx_sunlock(&allprison_lock);
1318 prison_hold_locked(pr);
1319 mtx_unlock(&pr->pr_mtx);
1321 /* Do not try to move the iface from and to the same prison. */
1322 if (pr->pr_vnet == ifp->if_vnet) {
1327 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1328 /* XXX Lock interfaces to avoid races. */
1329 CURVNET_SET_QUIET(pr->pr_vnet);
1330 difp = ifunit(ifname);
1337 /* Make sure the VNET is stable. */
1338 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1339 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1347 /* Move the interface into the child jail/vnet. */
1348 if_vmove(ifp, pr->pr_vnet);
1350 /* Report the new if_xname back to the userland. */
1351 sprintf(ifname, "%s", ifp->if_xname);
1358 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1361 struct vnet *vnet_dst;
1365 /* Try to find the prison within our visibility. */
1366 sx_slock(&allprison_lock);
1367 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1368 sx_sunlock(&allprison_lock);
1371 prison_hold_locked(pr);
1372 mtx_unlock(&pr->pr_mtx);
1374 /* Make sure the named iface exists in the source prison/vnet. */
1375 CURVNET_SET(pr->pr_vnet);
1376 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1383 /* Do not try to move the iface from and to the same prison. */
1384 vnet_dst = TD_TO_VNET(td);
1385 if (vnet_dst == ifp->if_vnet) {
1391 /* Make sure the VNET is stable. */
1392 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1393 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1400 /* Get interface back from child jail/vnet. */
1401 if_vmove(ifp, vnet_dst);
1404 /* Report the new if_xname back to the userland. */
1405 sprintf(ifname, "%s", ifp->if_xname);
1413 * Add a group to an interface
1416 if_addgroup(struct ifnet *ifp, const char *groupname)
1418 struct ifg_list *ifgl;
1419 struct ifg_group *ifg = NULL;
1420 struct ifg_member *ifgm;
1423 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1424 groupname[strlen(groupname) - 1] <= '9')
1428 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1429 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1434 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1435 M_NOWAIT)) == NULL) {
1440 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1441 M_TEMP, M_NOWAIT)) == NULL) {
1447 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1448 if (!strcmp(ifg->ifg_group, groupname))
1452 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1453 M_TEMP, M_NOWAIT)) == NULL) {
1459 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1460 ifg->ifg_refcnt = 0;
1461 CK_STAILQ_INIT(&ifg->ifg_members);
1462 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1467 ifgl->ifgl_group = ifg;
1468 ifgm->ifgm_ifp = ifp;
1471 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1472 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1473 IF_ADDR_WUNLOCK(ifp);
1478 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1479 EVENTHANDLER_INVOKE(group_change_event, groupname);
1485 * Remove a group from an interface
1488 if_delgroup(struct ifnet *ifp, const char *groupname)
1490 struct ifg_list *ifgl;
1491 struct ifg_member *ifgm;
1495 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1496 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1505 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1506 IF_ADDR_WUNLOCK(ifp);
1508 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1509 if (ifgm->ifgm_ifp == ifp)
1513 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next);
1515 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1516 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1521 epoch_wait_preempt(net_epoch_preempt);
1523 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1524 free(ifgl->ifgl_group, M_TEMP);
1529 EVENTHANDLER_INVOKE(group_change_event, groupname);
1535 * Remove an interface from all groups
1538 if_delgroups(struct ifnet *ifp)
1540 struct ifg_list *ifgl;
1541 struct ifg_member *ifgm;
1542 char groupname[IFNAMSIZ];
1546 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) {
1547 ifgl = CK_STAILQ_FIRST(&ifp->if_groups);
1549 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1552 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1553 IF_ADDR_WUNLOCK(ifp);
1555 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1556 if (ifgm->ifgm_ifp == ifp)
1560 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member,
1563 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1564 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1569 epoch_wait_preempt(net_epoch_preempt);
1572 EVENTHANDLER_INVOKE(group_detach_event,
1574 free(ifgl->ifgl_group, M_TEMP);
1576 EVENTHANDLER_INVOKE(group_change_event, groupname);
1584 ifgr_group_get(void *ifgrp)
1586 union ifgroupreq_union *ifgrup;
1589 #ifdef COMPAT_FREEBSD32
1590 if (SV_CURPROC_FLAG(SV_ILP32))
1591 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1593 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1596 static struct ifg_req *
1597 ifgr_groups_get(void *ifgrp)
1599 union ifgroupreq_union *ifgrup;
1602 #ifdef COMPAT_FREEBSD32
1603 if (SV_CURPROC_FLAG(SV_ILP32))
1604 return ((struct ifg_req *)(uintptr_t)
1605 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1607 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1611 * Stores all groups from an interface in memory pointed to by ifgr.
1614 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1616 struct epoch_tracker et;
1618 struct ifg_list *ifgl;
1619 struct ifg_req ifgrq, *ifgp;
1621 if (ifgr->ifgr_len == 0) {
1622 NET_EPOCH_ENTER(et);
1623 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1624 ifgr->ifgr_len += sizeof(struct ifg_req);
1629 len = ifgr->ifgr_len;
1630 ifgp = ifgr_groups_get(ifgr);
1632 NET_EPOCH_ENTER(et);
1633 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1634 if (len < sizeof(ifgrq)) {
1638 bzero(&ifgrq, sizeof ifgrq);
1639 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1640 sizeof(ifgrq.ifgrq_group));
1641 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1645 len -= sizeof(ifgrq);
1654 * Stores all members of a group in memory pointed to by igfr
1657 if_getgroupmembers(struct ifgroupreq *ifgr)
1659 struct ifg_group *ifg;
1660 struct ifg_member *ifgm;
1661 struct ifg_req ifgrq, *ifgp;
1665 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1666 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1673 if (ifgr->ifgr_len == 0) {
1674 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1675 ifgr->ifgr_len += sizeof(ifgrq);
1680 len = ifgr->ifgr_len;
1681 ifgp = ifgr_groups_get(ifgr);
1682 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1683 if (len < sizeof(ifgrq)) {
1687 bzero(&ifgrq, sizeof ifgrq);
1688 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1689 sizeof(ifgrq.ifgrq_member));
1690 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1694 len -= sizeof(ifgrq);
1703 * Return counter values from counter(9)s stored in ifnet.
1706 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1709 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1711 return (counter_u64_fetch(ifp->if_counters[cnt]));
1715 * Increase an ifnet counter. Usually used for counters shared
1716 * between the stack and a driver, but function supports them all.
1719 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1722 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1724 counter_u64_add(ifp->if_counters[cnt], inc);
1728 * Copy data from ifnet to userland API structure if_data.
1731 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1734 ifd->ifi_type = ifp->if_type;
1735 ifd->ifi_physical = 0;
1736 ifd->ifi_addrlen = ifp->if_addrlen;
1737 ifd->ifi_hdrlen = ifp->if_hdrlen;
1738 ifd->ifi_link_state = ifp->if_link_state;
1740 ifd->ifi_datalen = sizeof(struct if_data);
1741 ifd->ifi_mtu = ifp->if_mtu;
1742 ifd->ifi_metric = ifp->if_metric;
1743 ifd->ifi_baudrate = ifp->if_baudrate;
1744 ifd->ifi_hwassist = ifp->if_hwassist;
1745 ifd->ifi_epoch = ifp->if_epoch;
1746 ifd->ifi_lastchange = ifp->if_lastchange;
1748 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1749 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1750 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1751 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1752 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1753 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1754 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1755 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1756 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1757 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1758 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1759 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1763 * Wrapper functions for struct ifnet address list locking macros. These are
1764 * used by kernel modules to avoid encoding programming interface or binary
1765 * interface assumptions that may be violated when kernel-internal locking
1766 * approaches change.
1769 if_addr_rlock(struct ifnet *ifp)
1772 epoch_enter_preempt(net_epoch_preempt, curthread->td_et);
1776 if_addr_runlock(struct ifnet *ifp)
1779 epoch_exit_preempt(net_epoch_preempt, curthread->td_et);
1783 if_maddr_rlock(if_t ifp)
1786 epoch_enter_preempt(net_epoch_preempt, curthread->td_et);
1790 if_maddr_runlock(if_t ifp)
1793 epoch_exit_preempt(net_epoch_preempt, curthread->td_et);
1797 * Initialization, destruction and refcounting functions for ifaddrs.
1800 ifa_alloc(size_t size, int flags)
1804 KASSERT(size >= sizeof(struct ifaddr),
1805 ("%s: invalid size %zu", __func__, size));
1807 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1811 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1813 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1815 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1817 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1820 refcount_init(&ifa->ifa_refcnt, 1);
1825 /* free(NULL) is okay */
1826 counter_u64_free(ifa->ifa_opackets);
1827 counter_u64_free(ifa->ifa_ipackets);
1828 counter_u64_free(ifa->ifa_obytes);
1829 counter_u64_free(ifa->ifa_ibytes);
1830 free(ifa, M_IFADDR);
1836 ifa_ref(struct ifaddr *ifa)
1839 refcount_acquire(&ifa->ifa_refcnt);
1843 ifa_destroy(epoch_context_t ctx)
1847 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1848 counter_u64_free(ifa->ifa_opackets);
1849 counter_u64_free(ifa->ifa_ipackets);
1850 counter_u64_free(ifa->ifa_obytes);
1851 counter_u64_free(ifa->ifa_ibytes);
1852 free(ifa, M_IFADDR);
1856 ifa_free(struct ifaddr *ifa)
1859 if (refcount_release(&ifa->ifa_refcnt))
1860 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1865 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1866 struct sockaddr *ia)
1869 struct rt_addrinfo info;
1870 struct sockaddr_dl null_sdl;
1875 bzero(&info, sizeof(info));
1876 if (cmd != RTM_DELETE)
1877 info.rti_ifp = V_loif;
1878 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1879 info.rti_info[RTAX_DST] = ia;
1880 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1881 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1883 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1886 !(cmd == RTM_ADD && error == EEXIST) &&
1887 !(cmd == RTM_DELETE && error == ENOENT))
1888 if_printf(ifp, "%s failed: %d\n", otype, error);
1894 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1897 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1901 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1904 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1908 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1911 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1915 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1916 * structs used to represent other address families, it is necessary
1917 * to perform a different comparison.
1920 #define sa_dl_equal(a1, a2) \
1921 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1922 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1923 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1924 CLLADDR((const struct sockaddr_dl *)(a2)), \
1925 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1928 * Locate an interface based on a complete address.
1932 ifa_ifwithaddr(const struct sockaddr *addr)
1937 MPASS(in_epoch(net_epoch_preempt));
1938 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1939 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1940 if (ifa->ifa_addr->sa_family != addr->sa_family)
1942 if (sa_equal(addr, ifa->ifa_addr)) {
1945 /* IP6 doesn't have broadcast */
1946 if ((ifp->if_flags & IFF_BROADCAST) &&
1947 ifa->ifa_broadaddr &&
1948 ifa->ifa_broadaddr->sa_len != 0 &&
1949 sa_equal(ifa->ifa_broadaddr, addr)) {
1960 ifa_ifwithaddr_check(const struct sockaddr *addr)
1962 struct epoch_tracker et;
1965 NET_EPOCH_ENTER(et);
1966 rc = (ifa_ifwithaddr(addr) != NULL);
1972 * Locate an interface based on the broadcast address.
1976 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1981 MPASS(in_epoch(net_epoch_preempt));
1982 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1983 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1985 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1986 if (ifa->ifa_addr->sa_family != addr->sa_family)
1988 if ((ifp->if_flags & IFF_BROADCAST) &&
1989 ifa->ifa_broadaddr &&
1990 ifa->ifa_broadaddr->sa_len != 0 &&
1991 sa_equal(ifa->ifa_broadaddr, addr)) {
2002 * Locate the point to point interface with a given destination address.
2006 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2011 MPASS(in_epoch(net_epoch_preempt));
2012 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2013 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2015 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2017 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2018 if (ifa->ifa_addr->sa_family != addr->sa_family)
2020 if (ifa->ifa_dstaddr != NULL &&
2021 sa_equal(addr, ifa->ifa_dstaddr)) {
2032 * Find an interface on a specific network. If many, choice
2033 * is most specific found.
2036 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2040 struct ifaddr *ifa_maybe = NULL;
2041 u_int af = addr->sa_family;
2042 const char *addr_data = addr->sa_data, *cplim;
2044 MPASS(in_epoch(net_epoch_preempt));
2046 * AF_LINK addresses can be looked up directly by their index number,
2047 * so do that if we can.
2049 if (af == AF_LINK) {
2050 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2051 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2052 return (ifaddr_byindex(sdl->sdl_index));
2056 * Scan though each interface, looking for ones that have addresses
2057 * in this address family and the requested fib.
2059 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2060 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2062 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2063 const char *cp, *cp2, *cp3;
2065 if (ifa->ifa_addr->sa_family != af)
2067 if (af == AF_INET &&
2068 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2070 * This is a bit broken as it doesn't
2071 * take into account that the remote end may
2072 * be a single node in the network we are
2074 * The trouble is that we don't know the
2075 * netmask for the remote end.
2077 if (ifa->ifa_dstaddr != NULL &&
2078 sa_equal(addr, ifa->ifa_dstaddr)) {
2083 * Scan all the bits in the ifa's address.
2084 * If a bit dissagrees with what we are
2085 * looking for, mask it with the netmask
2086 * to see if it really matters.
2087 * (A byte at a time)
2089 if (ifa->ifa_netmask == 0)
2092 cp2 = ifa->ifa_addr->sa_data;
2093 cp3 = ifa->ifa_netmask->sa_data;
2094 cplim = ifa->ifa_netmask->sa_len
2095 + (char *)ifa->ifa_netmask;
2097 if ((*cp++ ^ *cp2++) & *cp3++)
2098 goto next; /* next address! */
2100 * If the netmask of what we just found
2101 * is more specific than what we had before
2102 * (if we had one), or if the virtual status
2103 * of new prefix is better than of the old one,
2104 * then remember the new one before continuing
2105 * to search for an even better one.
2107 if (ifa_maybe == NULL ||
2108 ifa_preferred(ifa_maybe, ifa) ||
2109 rn_refines((caddr_t)ifa->ifa_netmask,
2110 (caddr_t)ifa_maybe->ifa_netmask)) {
2123 * Find an interface address specific to an interface best matching
2127 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2130 const char *cp, *cp2, *cp3;
2132 struct ifaddr *ifa_maybe = NULL;
2133 u_int af = addr->sa_family;
2138 MPASS(in_epoch(net_epoch_preempt));
2139 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2140 if (ifa->ifa_addr->sa_family != af)
2142 if (ifa_maybe == NULL)
2144 if (ifa->ifa_netmask == 0) {
2145 if (sa_equal(addr, ifa->ifa_addr) ||
2146 (ifa->ifa_dstaddr &&
2147 sa_equal(addr, ifa->ifa_dstaddr)))
2151 if (ifp->if_flags & IFF_POINTOPOINT) {
2152 if (sa_equal(addr, ifa->ifa_dstaddr))
2156 cp2 = ifa->ifa_addr->sa_data;
2157 cp3 = ifa->ifa_netmask->sa_data;
2158 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2159 for (; cp3 < cplim; cp3++)
2160 if ((*cp++ ^ *cp2++) & *cp3)
2172 * See whether new ifa is better than current one:
2173 * 1) A non-virtual one is preferred over virtual.
2174 * 2) A virtual in master state preferred over any other state.
2176 * Used in several address selecting functions.
2179 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2182 return (cur->ifa_carp && (!next->ifa_carp ||
2183 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2186 #include <net/if_llatbl.h>
2189 * Default action when installing a route with a Link Level gateway.
2190 * Lookup an appropriate real ifa to point to.
2191 * This should be moved to /sys/net/link.c eventually.
2194 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2196 struct epoch_tracker et;
2197 struct ifaddr *ifa, *oifa;
2198 struct sockaddr *dst;
2201 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2202 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2204 NET_EPOCH_ENTER(et);
2205 ifa = ifaof_ifpforaddr(dst, ifp);
2213 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2214 ifa->ifa_rtrequest(cmd, rt, info);
2219 struct sockaddr_dl *
2220 link_alloc_sdl(size_t size, int flags)
2223 return (malloc(size, M_TEMP, flags));
2227 link_free_sdl(struct sockaddr *sa)
2233 * Fills in given sdl with interface basic info.
2234 * Returns pointer to filled sdl.
2236 struct sockaddr_dl *
2237 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2239 struct sockaddr_dl *sdl;
2241 sdl = (struct sockaddr_dl *)paddr;
2242 memset(sdl, 0, sizeof(struct sockaddr_dl));
2243 sdl->sdl_len = sizeof(struct sockaddr_dl);
2244 sdl->sdl_family = AF_LINK;
2245 sdl->sdl_index = ifp->if_index;
2246 sdl->sdl_type = iftype;
2252 * Mark an interface down and notify protocols of
2256 if_unroute(struct ifnet *ifp, int flag, int fam)
2260 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2262 ifp->if_flags &= ~flag;
2263 getmicrotime(&ifp->if_lastchange);
2264 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2265 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2266 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2267 ifp->if_qflush(ifp);
2270 (*carp_linkstate_p)(ifp);
2275 * Mark an interface up and notify protocols of
2279 if_route(struct ifnet *ifp, int flag, int fam)
2283 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2285 ifp->if_flags |= flag;
2286 getmicrotime(&ifp->if_lastchange);
2287 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2288 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2289 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2291 (*carp_linkstate_p)(ifp);
2298 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2299 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2300 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2301 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2302 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2303 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2304 int (*vlan_setcookie_p)(struct ifnet *, void *);
2305 void *(*vlan_cookie_p)(struct ifnet *);
2308 * Handle a change in the interface link state. To avoid LORs
2309 * between driver lock and upper layer locks, as well as possible
2310 * recursions, we post event to taskqueue, and all job
2311 * is done in static do_link_state_change().
2314 if_link_state_change(struct ifnet *ifp, int link_state)
2316 /* Return if state hasn't changed. */
2317 if (ifp->if_link_state == link_state)
2320 ifp->if_link_state = link_state;
2322 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2326 do_link_state_change(void *arg, int pending)
2328 struct ifnet *ifp = (struct ifnet *)arg;
2329 int link_state = ifp->if_link_state;
2330 CURVNET_SET(ifp->if_vnet);
2332 /* Notify that the link state has changed. */
2334 if (ifp->if_vlantrunk != NULL)
2335 (*vlan_link_state_p)(ifp);
2337 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2338 ifp->if_l2com != NULL)
2339 (*ng_ether_link_state_p)(ifp, link_state);
2341 (*carp_linkstate_p)(ifp);
2343 ifp->if_bridge_linkstate(ifp);
2345 (*lagg_linkstate_p)(ifp, link_state);
2347 if (IS_DEFAULT_VNET(curvnet))
2348 devctl_notify("IFNET", ifp->if_xname,
2349 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2352 if_printf(ifp, "%d link states coalesced\n", pending);
2353 if (log_link_state_change)
2354 if_printf(ifp, "link state changed to %s\n",
2355 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2356 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2361 * Mark an interface down and notify protocols of
2365 if_down(struct ifnet *ifp)
2368 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2369 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2373 * Mark an interface up and notify protocols of
2377 if_up(struct ifnet *ifp)
2380 if_route(ifp, IFF_UP, AF_UNSPEC);
2381 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2385 * Flush an interface queue.
2388 if_qflush(struct ifnet *ifp)
2396 if (ALTQ_IS_ENABLED(ifq))
2400 while ((m = n) != NULL) {
2411 * Map interface name to interface structure pointer, with or without
2412 * returning a reference.
2415 ifunit_ref(const char *name)
2417 struct epoch_tracker et;
2420 NET_EPOCH_ENTER(et);
2421 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2422 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2423 !(ifp->if_flags & IFF_DYING))
2433 ifunit(const char *name)
2435 struct epoch_tracker et;
2438 NET_EPOCH_ENTER(et);
2439 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2440 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2448 ifr_buffer_get_buffer(void *data)
2450 union ifreq_union *ifrup;
2453 #ifdef COMPAT_FREEBSD32
2454 if (SV_CURPROC_FLAG(SV_ILP32))
2455 return ((void *)(uintptr_t)
2456 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2458 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2462 ifr_buffer_set_buffer_null(void *data)
2464 union ifreq_union *ifrup;
2467 #ifdef COMPAT_FREEBSD32
2468 if (SV_CURPROC_FLAG(SV_ILP32))
2469 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2472 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2476 ifr_buffer_get_length(void *data)
2478 union ifreq_union *ifrup;
2481 #ifdef COMPAT_FREEBSD32
2482 if (SV_CURPROC_FLAG(SV_ILP32))
2483 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2485 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2489 ifr_buffer_set_length(void *data, size_t len)
2491 union ifreq_union *ifrup;
2494 #ifdef COMPAT_FREEBSD32
2495 if (SV_CURPROC_FLAG(SV_ILP32))
2496 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2499 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2503 ifr_data_get_ptr(void *ifrp)
2505 union ifreq_union *ifrup;
2508 #ifdef COMPAT_FREEBSD32
2509 if (SV_CURPROC_FLAG(SV_ILP32))
2510 return ((void *)(uintptr_t)
2511 ifrup->ifr32.ifr_ifru.ifru_data);
2513 return (ifrup->ifr.ifr_ifru.ifru_data);
2517 * Hardware specific interface ioctls.
2520 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2523 int error = 0, do_ifup = 0;
2524 int new_flags, temp_flags;
2525 size_t namelen, onamelen;
2527 char *descrbuf, *odescrbuf;
2528 char new_name[IFNAMSIZ];
2530 struct sockaddr_dl *sdl;
2532 ifr = (struct ifreq *)data;
2535 ifr->ifr_index = ifp->if_index;
2539 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2540 ifr->ifr_flags = temp_flags & 0xffff;
2541 ifr->ifr_flagshigh = temp_flags >> 16;
2545 ifr->ifr_reqcap = ifp->if_capabilities;
2546 ifr->ifr_curcap = ifp->if_capenable;
2551 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2556 ifr->ifr_metric = ifp->if_metric;
2560 ifr->ifr_mtu = ifp->if_mtu;
2564 /* XXXGL: did this ever worked? */
2570 sx_slock(&ifdescr_sx);
2571 if (ifp->if_description == NULL)
2574 /* space for terminating nul */
2575 descrlen = strlen(ifp->if_description) + 1;
2576 if (ifr_buffer_get_length(ifr) < descrlen)
2577 ifr_buffer_set_buffer_null(ifr);
2579 error = copyout(ifp->if_description,
2580 ifr_buffer_get_buffer(ifr), descrlen);
2581 ifr_buffer_set_length(ifr, descrlen);
2583 sx_sunlock(&ifdescr_sx);
2587 error = priv_check(td, PRIV_NET_SETIFDESCR);
2592 * Copy only (length-1) bytes to make sure that
2593 * if_description is always nul terminated. The
2594 * length parameter is supposed to count the
2595 * terminating nul in.
2597 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2598 return (ENAMETOOLONG);
2599 else if (ifr_buffer_get_length(ifr) == 0)
2602 descrbuf = malloc(ifr_buffer_get_length(ifr),
2603 M_IFDESCR, M_WAITOK | M_ZERO);
2604 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2605 ifr_buffer_get_length(ifr) - 1);
2607 free(descrbuf, M_IFDESCR);
2612 sx_xlock(&ifdescr_sx);
2613 odescrbuf = ifp->if_description;
2614 ifp->if_description = descrbuf;
2615 sx_xunlock(&ifdescr_sx);
2617 getmicrotime(&ifp->if_lastchange);
2618 free(odescrbuf, M_IFDESCR);
2622 ifr->ifr_fib = ifp->if_fib;
2626 error = priv_check(td, PRIV_NET_SETIFFIB);
2629 if (ifr->ifr_fib >= rt_numfibs)
2632 ifp->if_fib = ifr->ifr_fib;
2636 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2640 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2641 * check, so we don't need special handling here yet.
2643 new_flags = (ifr->ifr_flags & 0xffff) |
2644 (ifr->ifr_flagshigh << 16);
2645 if (ifp->if_flags & IFF_UP &&
2646 (new_flags & IFF_UP) == 0) {
2648 } else if (new_flags & IFF_UP &&
2649 (ifp->if_flags & IFF_UP) == 0) {
2652 /* See if permanently promiscuous mode bit is about to flip */
2653 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2654 if (new_flags & IFF_PPROMISC)
2655 ifp->if_flags |= IFF_PROMISC;
2656 else if (ifp->if_pcount == 0)
2657 ifp->if_flags &= ~IFF_PROMISC;
2658 if (log_promisc_mode_change)
2659 if_printf(ifp, "permanently promiscuous mode %s\n",
2660 ((new_flags & IFF_PPROMISC) ?
2661 "enabled" : "disabled"));
2663 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2664 (new_flags &~ IFF_CANTCHANGE);
2665 if (ifp->if_ioctl) {
2666 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2670 getmicrotime(&ifp->if_lastchange);
2674 error = priv_check(td, PRIV_NET_SETIFCAP);
2677 if (ifp->if_ioctl == NULL)
2678 return (EOPNOTSUPP);
2679 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2681 error = (*ifp->if_ioctl)(ifp, cmd, data);
2683 getmicrotime(&ifp->if_lastchange);
2688 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2693 error = priv_check(td, PRIV_NET_SETIFNAME);
2696 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2700 if (new_name[0] == '\0')
2702 if (new_name[IFNAMSIZ-1] != '\0') {
2703 new_name[IFNAMSIZ-1] = '\0';
2704 if (strlen(new_name) == IFNAMSIZ-1)
2707 if (ifunit(new_name) != NULL)
2711 * XXX: Locking. Nothing else seems to lock if_flags,
2712 * and there are numerous other races with the
2713 * ifunit() checks not being atomic with namespace
2714 * changes (renames, vmoves, if_attach, etc).
2716 ifp->if_flags |= IFF_RENAMING;
2718 /* Announce the departure of the interface. */
2719 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2720 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2722 if_printf(ifp, "changing name to '%s'\n", new_name);
2725 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2727 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2728 namelen = strlen(new_name);
2729 onamelen = sdl->sdl_nlen;
2731 * Move the address if needed. This is safe because we
2732 * allocate space for a name of length IFNAMSIZ when we
2733 * create this in if_attach().
2735 if (namelen != onamelen) {
2736 bcopy(sdl->sdl_data + onamelen,
2737 sdl->sdl_data + namelen, sdl->sdl_alen);
2739 bcopy(new_name, sdl->sdl_data, namelen);
2740 sdl->sdl_nlen = namelen;
2741 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2742 bzero(sdl->sdl_data, onamelen);
2743 while (namelen != 0)
2744 sdl->sdl_data[--namelen] = 0xff;
2745 IF_ADDR_WUNLOCK(ifp);
2747 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2748 /* Announce the return of the interface. */
2749 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2751 ifp->if_flags &= ~IFF_RENAMING;
2756 error = priv_check(td, PRIV_NET_SETIFVNET);
2759 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2764 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2767 ifp->if_metric = ifr->ifr_metric;
2768 getmicrotime(&ifp->if_lastchange);
2772 error = priv_check(td, PRIV_NET_SETIFPHYS);
2775 if (ifp->if_ioctl == NULL)
2776 return (EOPNOTSUPP);
2777 error = (*ifp->if_ioctl)(ifp, cmd, data);
2779 getmicrotime(&ifp->if_lastchange);
2784 u_long oldmtu = ifp->if_mtu;
2786 error = priv_check(td, PRIV_NET_SETIFMTU);
2789 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2791 if (ifp->if_ioctl == NULL)
2792 return (EOPNOTSUPP);
2793 error = (*ifp->if_ioctl)(ifp, cmd, data);
2795 getmicrotime(&ifp->if_lastchange);
2798 NETDUMP_REINIT(ifp);
2802 * If the link MTU changed, do network layer specific procedure.
2804 if (ifp->if_mtu != oldmtu) {
2815 if (cmd == SIOCADDMULTI)
2816 error = priv_check(td, PRIV_NET_ADDMULTI);
2818 error = priv_check(td, PRIV_NET_DELMULTI);
2822 /* Don't allow group membership on non-multicast interfaces. */
2823 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2824 return (EOPNOTSUPP);
2826 /* Don't let users screw up protocols' entries. */
2827 if (ifr->ifr_addr.sa_family != AF_LINK)
2830 if (cmd == SIOCADDMULTI) {
2831 struct epoch_tracker et;
2832 struct ifmultiaddr *ifma;
2835 * Userland is only permitted to join groups once
2836 * via the if_addmulti() KPI, because it cannot hold
2837 * struct ifmultiaddr * between calls. It may also
2838 * lose a race while we check if the membership
2841 NET_EPOCH_ENTER(et);
2842 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2847 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2849 error = if_delmulti(ifp, &ifr->ifr_addr);
2852 getmicrotime(&ifp->if_lastchange);
2855 case SIOCSIFPHYADDR:
2856 case SIOCDIFPHYADDR:
2858 case SIOCSIFPHYADDR_IN6:
2861 case SIOCSIFGENERIC:
2862 error = priv_check(td, PRIV_NET_HWIOCTL);
2865 if (ifp->if_ioctl == NULL)
2866 return (EOPNOTSUPP);
2867 error = (*ifp->if_ioctl)(ifp, cmd, data);
2869 getmicrotime(&ifp->if_lastchange);
2873 case SIOCGIFPSRCADDR:
2874 case SIOCGIFPDSTADDR:
2877 case SIOCGIFGENERIC:
2879 case SIOCGIFRSSHASH:
2880 if (ifp->if_ioctl == NULL)
2881 return (EOPNOTSUPP);
2882 error = (*ifp->if_ioctl)(ifp, cmd, data);
2886 error = priv_check(td, PRIV_NET_SETLLADDR);
2889 error = if_setlladdr(ifp,
2890 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2894 error = if_gethwaddr(ifp, ifr);
2897 case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2898 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2901 if ((error = if_addgroup(ifp,
2902 ifgr_group_get((struct ifgroupreq *)data))))
2906 case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2907 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2911 case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2912 error = priv_check(td, PRIV_NET_DELIFGROUP);
2915 if ((error = if_delgroup(ifp,
2916 ifgr_group_get((struct ifgroupreq *)data))))
2927 #ifdef COMPAT_FREEBSD32
2935 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2938 #ifdef COMPAT_FREEBSD32
2940 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2942 struct ifmediareq32 *ifmr32;
2944 ifmr32 = (struct ifmediareq32 *)data;
2945 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2946 sizeof(ifmr->ifm_name));
2947 ifmr->ifm_current = ifmr32->ifm_current;
2948 ifmr->ifm_mask = ifmr32->ifm_mask;
2949 ifmr->ifm_status = ifmr32->ifm_status;
2950 ifmr->ifm_active = ifmr32->ifm_active;
2951 ifmr->ifm_count = ifmr32->ifm_count;
2952 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2956 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2958 struct ifmediareq32 *ifmr32;
2960 ifmr32 = (struct ifmediareq32 *)data;
2961 ifmr32->ifm_current = ifmr->ifm_current;
2962 ifmr32->ifm_mask = ifmr->ifm_mask;
2963 ifmr32->ifm_status = ifmr->ifm_status;
2964 ifmr32->ifm_active = ifmr->ifm_active;
2965 ifmr32->ifm_count = ifmr->ifm_count;
2973 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2975 #ifdef COMPAT_FREEBSD32
2976 caddr_t saved_data = NULL;
2977 struct ifmediareq ifmr;
2978 struct ifmediareq *ifmrp;
2988 CURVNET_SET(so->so_vnet);
2990 /* Make sure the VNET is stable. */
2991 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2992 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
3002 error = ifconf(cmd, data);
3006 #ifdef COMPAT_FREEBSD32
3009 struct ifconf32 *ifc32;
3012 ifc32 = (struct ifconf32 *)data;
3013 ifc.ifc_len = ifc32->ifc_len;
3014 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3016 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3019 ifc32->ifc_len = ifc.ifc_len;
3025 #ifdef COMPAT_FREEBSD32
3028 case SIOCGIFMEDIA32:
3029 case SIOCGIFXMEDIA32:
3031 ifmr_init(ifmrp, data);
3032 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3034 data = (caddr_t)ifmrp;
3038 ifr = (struct ifreq *)data;
3042 error = priv_check(td, PRIV_NET_SETIFVNET);
3044 error = if_vmove_reclaim(td, ifr->ifr_name,
3050 error = priv_check(td, PRIV_NET_IFCREATE);
3052 error = if_clone_create(ifr->ifr_name,
3053 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3054 ifr_data_get_ptr(ifr) : NULL);
3057 error = priv_check(td, PRIV_NET_IFDESTROY);
3059 error = if_clone_destroy(ifr->ifr_name);
3062 case SIOCIFGCLONERS:
3063 error = if_clone_list((struct if_clonereq *)data);
3066 case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3067 error = if_getgroupmembers((struct ifgroupreq *)data);
3070 #if defined(INET) || defined(INET6)
3073 if (carp_ioctl_p == NULL)
3074 error = EPROTONOSUPPORT;
3076 error = (*carp_ioctl_p)(ifr, cmd, td);
3081 ifp = ifunit_ref(ifr->ifr_name);
3087 error = ifhwioctl(cmd, ifp, data, td);
3088 if (error != ENOIOCTL)
3091 oif_flags = ifp->if_flags;
3092 if (so->so_proto == NULL) {
3098 * Pass the request on to the socket control method, and if the
3099 * latter returns EOPNOTSUPP, directly to the interface.
3101 * Make an exception for the legacy SIOCSIF* requests. Drivers
3102 * trust SIOCSIFADDR et al to come from an already privileged
3103 * layer, and do not perform any credentials checks or input
3106 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3108 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3109 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3110 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3111 error = (*ifp->if_ioctl)(ifp, cmd, data);
3113 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3115 if (ifp->if_flags & IFF_UP)
3123 #ifdef COMPAT_FREEBSD32
3124 if (ifmrp != NULL) {
3125 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3126 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3129 ifmr_update(ifmrp, data);
3137 * The code common to handling reference counted flags,
3138 * e.g., in ifpromisc() and if_allmulti().
3139 * The "pflag" argument can specify a permanent mode flag to check,
3140 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3142 * Only to be used on stack-owned flags, not driver-owned flags.
3145 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3149 int oldflags, oldcount;
3151 /* Sanity checks to catch programming errors */
3152 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3153 ("%s: setting driver-owned flag %d", __func__, flag));
3156 KASSERT(*refcount >= 0,
3157 ("%s: increment negative refcount %d for flag %d",
3158 __func__, *refcount, flag));
3160 KASSERT(*refcount > 0,
3161 ("%s: decrement non-positive refcount %d for flag %d",
3162 __func__, *refcount, flag));
3164 /* In case this mode is permanent, just touch refcount */
3165 if (ifp->if_flags & pflag) {
3166 *refcount += onswitch ? 1 : -1;
3170 /* Save ifnet parameters for if_ioctl() may fail */
3171 oldcount = *refcount;
3172 oldflags = ifp->if_flags;
3175 * See if we aren't the only and touching refcount is enough.
3176 * Actually toggle interface flag if we are the first or last.
3181 ifp->if_flags |= flag;
3185 ifp->if_flags &= ~flag;
3188 /* Call down the driver since we've changed interface flags */
3189 if (ifp->if_ioctl == NULL) {
3193 ifr.ifr_flags = ifp->if_flags & 0xffff;
3194 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3195 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3198 /* Notify userland that interface flags have changed */
3203 /* Recover after driver error */
3204 *refcount = oldcount;
3205 ifp->if_flags = oldflags;
3210 * Set/clear promiscuous mode on interface ifp based on the truth value
3211 * of pswitch. The calls are reference counted so that only the first
3212 * "on" request actually has an effect, as does the final "off" request.
3213 * Results are undefined if the "off" and "on" requests are not matched.
3216 ifpromisc(struct ifnet *ifp, int pswitch)
3219 int oldflags = ifp->if_flags;
3221 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3222 &ifp->if_pcount, pswitch);
3223 /* If promiscuous mode status has changed, log a message */
3224 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3225 log_promisc_mode_change)
3226 if_printf(ifp, "promiscuous mode %s\n",
3227 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3232 * Return interface configuration
3233 * of system. List may be used
3234 * in later ioctl's (above) to get
3235 * other information.
3239 ifconf(u_long cmd, caddr_t data)
3241 struct ifconf *ifc = (struct ifconf *)data;
3246 int error, full = 0, valid_len, max_len;
3248 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3249 max_len = MAXPHYS - 1;
3251 /* Prevent hostile input from being able to crash the system */
3252 if (ifc->ifc_len <= 0)
3256 if (ifc->ifc_len <= max_len) {
3257 max_len = ifc->ifc_len;
3260 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3265 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3266 struct epoch_tracker et;
3270 * Zero the ifr to make sure we don't disclose the contents
3273 memset(&ifr, 0, sizeof(ifr));
3275 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3276 >= sizeof(ifr.ifr_name)) {
3279 return (ENAMETOOLONG);
3283 NET_EPOCH_ENTER(et);
3284 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3285 struct sockaddr *sa = ifa->ifa_addr;
3287 if (prison_if(curthread->td_ucred, sa) != 0)
3290 if (sa->sa_len <= sizeof(*sa)) {
3291 if (sa->sa_len < sizeof(*sa)) {
3292 memset(&ifr.ifr_ifru.ifru_addr, 0,
3293 sizeof(ifr.ifr_ifru.ifru_addr));
3294 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3297 ifr.ifr_ifru.ifru_addr = *sa;
3298 sbuf_bcat(sb, &ifr, sizeof(ifr));
3299 max_len += sizeof(ifr);
3302 offsetof(struct ifreq, ifr_addr));
3303 max_len += offsetof(struct ifreq, ifr_addr);
3304 sbuf_bcat(sb, sa, sa->sa_len);
3305 max_len += sa->sa_len;
3308 if (sbuf_error(sb) == 0)
3309 valid_len = sbuf_len(sb);
3313 sbuf_bcat(sb, &ifr, sizeof(ifr));
3314 max_len += sizeof(ifr);
3316 if (sbuf_error(sb) == 0)
3317 valid_len = sbuf_len(sb);
3323 * If we didn't allocate enough space (uncommon), try again. If
3324 * we have already allocated as much space as we are allowed,
3325 * return what we've got.
3327 if (valid_len != max_len && !full) {
3332 ifc->ifc_len = valid_len;
3334 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3340 * Just like ifpromisc(), but for all-multicast-reception mode.
3343 if_allmulti(struct ifnet *ifp, int onswitch)
3346 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3349 struct ifmultiaddr *
3350 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3352 struct ifmultiaddr *ifma;
3354 IF_ADDR_LOCK_ASSERT(ifp);
3356 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3357 if (sa->sa_family == AF_LINK) {
3358 if (sa_dl_equal(ifma->ifma_addr, sa))
3361 if (sa_equal(ifma->ifma_addr, sa))
3370 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3371 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3372 * the ifnet multicast address list here, so the caller must do that and
3373 * other setup work (such as notifying the device driver). The reference
3374 * count is initialized to 1.
3376 static struct ifmultiaddr *
3377 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3380 struct ifmultiaddr *ifma;
3381 struct sockaddr *dupsa;
3383 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3388 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3389 if (dupsa == NULL) {
3390 free(ifma, M_IFMADDR);
3393 bcopy(sa, dupsa, sa->sa_len);
3394 ifma->ifma_addr = dupsa;
3396 ifma->ifma_ifp = ifp;
3397 ifma->ifma_refcount = 1;
3398 ifma->ifma_protospec = NULL;
3401 ifma->ifma_lladdr = NULL;
3405 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3406 if (dupsa == NULL) {
3407 free(ifma->ifma_addr, M_IFMADDR);
3408 free(ifma, M_IFMADDR);
3411 bcopy(llsa, dupsa, llsa->sa_len);
3412 ifma->ifma_lladdr = dupsa;
3418 * if_freemulti: free ifmultiaddr structure and possibly attached related
3419 * addresses. The caller is responsible for implementing reference
3420 * counting, notifying the driver, handling routing messages, and releasing
3421 * any dependent link layer state.
3423 #ifdef MCAST_VERBOSE
3424 extern void kdb_backtrace(void);
3427 if_freemulti_internal(struct ifmultiaddr *ifma)
3430 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3431 ifma->ifma_refcount));
3433 if (ifma->ifma_lladdr != NULL)
3434 free(ifma->ifma_lladdr, M_IFMADDR);
3435 #ifdef MCAST_VERBOSE
3437 printf("%s freeing ifma: %p\n", __func__, ifma);
3439 free(ifma->ifma_addr, M_IFMADDR);
3440 free(ifma, M_IFMADDR);
3444 if_destroymulti(epoch_context_t ctx)
3446 struct ifmultiaddr *ifma;
3448 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3449 if_freemulti_internal(ifma);
3453 if_freemulti(struct ifmultiaddr *ifma)
3455 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3456 ifma->ifma_refcount));
3458 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3463 * Register an additional multicast address with a network interface.
3465 * - If the address is already present, bump the reference count on the
3466 * address and return.
3467 * - If the address is not link-layer, look up a link layer address.
3468 * - Allocate address structures for one or both addresses, and attach to the
3469 * multicast address list on the interface. If automatically adding a link
3470 * layer address, the protocol address will own a reference to the link
3471 * layer address, to be freed when it is freed.
3472 * - Notify the network device driver of an addition to the multicast address
3475 * 'sa' points to caller-owned memory with the desired multicast address.
3477 * 'retifma' will be used to return a pointer to the resulting multicast
3478 * address reference, if desired.
3481 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3482 struct ifmultiaddr **retifma)
3484 struct ifmultiaddr *ifma, *ll_ifma;
3485 struct sockaddr *llsa;
3486 struct sockaddr_dl sdl;
3490 IN_MULTI_LIST_UNLOCK_ASSERT();
3493 IN6_MULTI_LIST_UNLOCK_ASSERT();
3496 * If the address is already present, return a new reference to it;
3497 * otherwise, allocate storage and set up a new address.
3500 ifma = if_findmulti(ifp, sa);
3502 ifma->ifma_refcount++;
3503 if (retifma != NULL)
3505 IF_ADDR_WUNLOCK(ifp);
3510 * The address isn't already present; resolve the protocol address
3511 * into a link layer address, and then look that up, bump its
3512 * refcount or allocate an ifma for that also.
3513 * Most link layer resolving functions returns address data which
3514 * fits inside default sockaddr_dl structure. However callback
3515 * can allocate another sockaddr structure, in that case we need to
3520 if (ifp->if_resolvemulti != NULL) {
3521 /* Provide called function with buffer size information */
3522 sdl.sdl_len = sizeof(sdl);
3523 llsa = (struct sockaddr *)&sdl;
3524 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3530 * Allocate the new address. Don't hook it up yet, as we may also
3531 * need to allocate a link layer multicast address.
3533 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3540 * If a link layer address is found, we'll need to see if it's
3541 * already present in the address list, or allocate is as well.
3542 * When this block finishes, the link layer address will be on the
3546 ll_ifma = if_findmulti(ifp, llsa);
3547 if (ll_ifma == NULL) {
3548 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3549 if (ll_ifma == NULL) {
3550 --ifma->ifma_refcount;
3555 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3556 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3559 ll_ifma->ifma_refcount++;
3560 ifma->ifma_llifma = ll_ifma;
3564 * We now have a new multicast address, ifma, and possibly a new or
3565 * referenced link layer address. Add the primary address to the
3566 * ifnet address list.
3568 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3569 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3571 if (retifma != NULL)
3575 * Must generate the message while holding the lock so that 'ifma'
3576 * pointer is still valid.
3578 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3579 IF_ADDR_WUNLOCK(ifp);
3582 * We are certain we have added something, so call down to the
3583 * interface to let them know about it.
3585 if (ifp->if_ioctl != NULL) {
3586 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3589 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3590 link_free_sdl(llsa);
3595 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3596 link_free_sdl(llsa);
3599 IF_ADDR_WUNLOCK(ifp);
3604 * Delete a multicast group membership by network-layer group address.
3606 * Returns ENOENT if the entry could not be found. If ifp no longer
3607 * exists, results are undefined. This entry point should only be used
3608 * from subsystems which do appropriate locking to hold ifp for the
3609 * duration of the call.
3610 * Network-layer protocol domains must use if_delmulti_ifma().
3613 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3615 struct ifmultiaddr *ifma;
3618 struct epoch_tracker et;
3621 NET_EPOCH_ENTER(et);
3622 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3629 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3636 ifma = if_findmulti(ifp, sa);
3638 lastref = if_delmulti_locked(ifp, ifma, 0);
3639 IF_ADDR_WUNLOCK(ifp);
3644 if (lastref && ifp->if_ioctl != NULL) {
3645 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3652 * Delete all multicast group membership for an interface.
3653 * Should be used to quickly flush all multicast filters.
3656 if_delallmulti(struct ifnet *ifp)
3658 struct ifmultiaddr *ifma;
3659 struct ifmultiaddr *next;
3662 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3663 if_delmulti_locked(ifp, ifma, 0);
3664 IF_ADDR_WUNLOCK(ifp);
3668 if_delmulti_ifma(struct ifmultiaddr *ifma)
3670 if_delmulti_ifma_flags(ifma, 0);
3674 * Delete a multicast group membership by group membership pointer.
3675 * Network-layer protocol domains must use this routine.
3677 * It is safe to call this routine if the ifp disappeared.
3680 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3684 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3686 IN_MULTI_LIST_UNLOCK_ASSERT();
3688 ifp = ifma->ifma_ifp;
3691 printf("%s: ifma_ifp seems to be detached\n", __func__);
3693 struct epoch_tracker et;
3696 NET_EPOCH_ENTER(et);
3697 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3706 * If and only if the ifnet instance exists: Acquire the address lock.
3711 lastref = if_delmulti_locked(ifp, ifma, flags);
3715 * If and only if the ifnet instance exists:
3716 * Release the address lock.
3717 * If the group was left: update the hardware hash filter.
3719 IF_ADDR_WUNLOCK(ifp);
3720 if (lastref && ifp->if_ioctl != NULL) {
3721 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3727 * Perform deletion of network-layer and/or link-layer multicast address.
3729 * Return 0 if the reference count was decremented.
3730 * Return 1 if the final reference was released, indicating that the
3731 * hardware hash filter should be reprogrammed.
3734 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3736 struct ifmultiaddr *ll_ifma;
3738 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3739 KASSERT(ifma->ifma_ifp == ifp,
3740 ("%s: inconsistent ifp %p", __func__, ifp));
3741 IF_ADDR_WLOCK_ASSERT(ifp);
3744 ifp = ifma->ifma_ifp;
3745 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3748 * If the ifnet is detaching, null out references to ifnet,
3749 * so that upper protocol layers will notice, and not attempt
3750 * to obtain locks for an ifnet which no longer exists. The
3751 * routing socket announcement must happen before the ifnet
3752 * instance is detached from the system.
3756 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3759 * ifp may already be nulled out if we are being reentered
3760 * to delete the ll_ifma.
3763 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3764 ifma->ifma_ifp = NULL;
3768 if (--ifma->ifma_refcount > 0)
3771 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3772 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3773 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3776 * If this ifma is a network-layer ifma, a link-layer ifma may
3777 * have been associated with it. Release it first if so.
3779 ll_ifma = ifma->ifma_llifma;
3780 if (ll_ifma != NULL) {
3781 KASSERT(ifma->ifma_lladdr != NULL,
3782 ("%s: llifma w/o lladdr", __func__));
3784 ll_ifma->ifma_ifp = NULL; /* XXX */
3785 if (--ll_ifma->ifma_refcount == 0) {
3787 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3788 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3790 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3793 if_freemulti(ll_ifma);
3798 struct ifmultiaddr *ifmatmp;
3800 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3801 MPASS(ifma != ifmatmp);
3806 * The last reference to this instance of struct ifmultiaddr
3807 * was released; the hardware should be notified of this change.
3813 * Set the link layer address on an interface.
3815 * At this time we only support certain types of interfaces,
3816 * and we don't allow the length of the address to change.
3818 * Set noinline to be dtrace-friendly
3821 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3823 struct sockaddr_dl *sdl;
3826 struct epoch_tracker et;
3830 NET_EPOCH_ENTER(et);
3837 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3842 if (len != sdl->sdl_alen) { /* don't allow length to change */
3846 switch (ifp->if_type) {
3851 case IFT_IEEE8023ADLAG:
3852 bcopy(lladdr, LLADDR(sdl), len);
3860 * If the interface is already up, we need
3861 * to re-init it in order to reprogram its
3865 if ((ifp->if_flags & IFF_UP) != 0) {
3866 if (ifp->if_ioctl) {
3867 ifp->if_flags &= ~IFF_UP;
3868 ifr.ifr_flags = ifp->if_flags & 0xffff;
3869 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3870 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3871 ifp->if_flags |= IFF_UP;
3872 ifr.ifr_flags = ifp->if_flags & 0xffff;
3873 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3874 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3877 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3885 * Compat function for handling basic encapsulation requests.
3886 * Not converted stacks (FDDI, IB, ..) supports traditional
3887 * output model: ARP (and other similar L2 protocols) are handled
3888 * inside output routine, arpresolve/nd6_resolve() returns MAC
3889 * address instead of full prepend.
3891 * This function creates calculated header==MAC for IPv4/IPv6 and
3892 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3896 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3899 if (req->rtype != IFENCAP_LL)
3900 return (EOPNOTSUPP);
3902 if (req->bufsize < req->lladdr_len)
3905 switch (req->family) {
3910 return (EAFNOSUPPORT);
3913 /* Copy lladdr to storage as is */
3914 memmove(req->buf, req->lladdr, req->lladdr_len);
3915 req->bufsize = req->lladdr_len;
3916 req->lladdr_off = 0;
3922 * Tunnel interfaces can nest, also they may cause infinite recursion
3923 * calls when misconfigured. We'll prevent this by detecting loops.
3924 * High nesting level may cause stack exhaustion. We'll prevent this
3925 * by introducing upper limit.
3927 * Return 0, if tunnel nesting count is equal or less than limit.
3930 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3938 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3939 if (*(struct ifnet **)(mtag + 1) == ifp) {
3940 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3945 if (count > limit) {
3947 "%s: if_output recursively called too many times(%d)\n",
3948 if_name(ifp), count);
3951 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3954 *(struct ifnet **)(mtag + 1) = ifp;
3955 m_tag_prepend(m, mtag);
3960 * Get the link layer address that was read from the hardware at attach.
3962 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3963 * their component interfaces as IFT_IEEE8023ADLAG.
3966 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3969 if (ifp->if_hw_addr == NULL)
3972 switch (ifp->if_type) {
3974 case IFT_IEEE8023ADLAG:
3975 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3983 * The name argument must be a pointer to storage which will last as
3984 * long as the interface does. For physical devices, the result of
3985 * device_get_name(dev) is a good choice and for pseudo-devices a
3986 * static string works well.
3989 if_initname(struct ifnet *ifp, const char *name, int unit)
3991 ifp->if_dname = name;
3992 ifp->if_dunit = unit;
3993 if (unit != IF_DUNIT_NONE)
3994 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3996 strlcpy(ifp->if_xname, name, IFNAMSIZ);
4000 if_printf(struct ifnet *ifp, const char *fmt, ...)
4005 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4007 vlog(LOG_INFO, if_fmt, ap);
4013 if_start(struct ifnet *ifp)
4016 (*(ifp)->if_start)(ifp);
4020 * Backwards compatibility interface for drivers
4021 * that have not implemented it
4024 if_transmit(struct ifnet *ifp, struct mbuf *m)
4028 IFQ_HANDOFF(ifp, m, error);
4033 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4040 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4045 if (_IF_QFULL(ifq)) {
4047 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4052 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4053 if (m->m_flags & (M_BCAST|M_MCAST))
4054 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4055 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4057 _IF_ENQUEUE(ifq, m);
4059 if (ifp != NULL && !active)
4060 (*(ifp)->if_start)(ifp);
4065 if_register_com_alloc(u_char type,
4066 if_com_alloc_t *a, if_com_free_t *f)
4069 KASSERT(if_com_alloc[type] == NULL,
4070 ("if_register_com_alloc: %d already registered", type));
4071 KASSERT(if_com_free[type] == NULL,
4072 ("if_register_com_alloc: %d free already registered", type));
4074 if_com_alloc[type] = a;
4075 if_com_free[type] = f;
4079 if_deregister_com_alloc(u_char type)
4082 KASSERT(if_com_alloc[type] != NULL,
4083 ("if_deregister_com_alloc: %d not registered", type));
4084 KASSERT(if_com_free[type] != NULL,
4085 ("if_deregister_com_alloc: %d free not registered", type));
4086 if_com_alloc[type] = NULL;
4087 if_com_free[type] = NULL;
4090 /* API for driver access to network stack owned ifnet.*/
4092 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4096 oldbrate = ifp->if_baudrate;
4097 ifp->if_baudrate = baudrate;
4102 if_getbaudrate(if_t ifp)
4105 return (((struct ifnet *)ifp)->if_baudrate);
4109 if_setcapabilities(if_t ifp, int capabilities)
4111 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4116 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4118 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4119 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4125 if_getcapabilities(if_t ifp)
4127 return ((struct ifnet *)ifp)->if_capabilities;
4131 if_setcapenable(if_t ifp, int capabilities)
4133 ((struct ifnet *)ifp)->if_capenable = capabilities;
4138 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4141 ((struct ifnet *)ifp)->if_capenable |= setcap;
4143 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4149 if_getdname(if_t ifp)
4151 return ((struct ifnet *)ifp)->if_dname;
4155 if_togglecapenable(if_t ifp, int togglecap)
4157 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4162 if_getcapenable(if_t ifp)
4164 return ((struct ifnet *)ifp)->if_capenable;
4168 * This is largely undesirable because it ties ifnet to a device, but does
4169 * provide flexiblity for an embedded product vendor. Should be used with
4170 * the understanding that it violates the interface boundaries, and should be
4171 * a last resort only.
4174 if_setdev(if_t ifp, void *dev)
4180 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4182 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4183 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4189 if_getdrvflags(if_t ifp)
4191 return ((struct ifnet *)ifp)->if_drv_flags;
4195 if_setdrvflags(if_t ifp, int flags)
4197 ((struct ifnet *)ifp)->if_drv_flags = flags;
4203 if_setflags(if_t ifp, int flags)
4205 ((struct ifnet *)ifp)->if_flags = flags;
4210 if_setflagbits(if_t ifp, int set, int clear)
4212 ((struct ifnet *)ifp)->if_flags |= set;
4213 ((struct ifnet *)ifp)->if_flags &= ~clear;
4219 if_getflags(if_t ifp)
4221 return ((struct ifnet *)ifp)->if_flags;
4225 if_clearhwassist(if_t ifp)
4227 ((struct ifnet *)ifp)->if_hwassist = 0;
4232 if_sethwassistbits(if_t ifp, int toset, int toclear)
4234 ((struct ifnet *)ifp)->if_hwassist |= toset;
4235 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4241 if_sethwassist(if_t ifp, int hwassist_bit)
4243 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4248 if_gethwassist(if_t ifp)
4250 return ((struct ifnet *)ifp)->if_hwassist;
4254 if_setmtu(if_t ifp, int mtu)
4256 ((struct ifnet *)ifp)->if_mtu = mtu;
4263 return ((struct ifnet *)ifp)->if_mtu;
4267 if_getmtu_family(if_t ifp, int family)
4271 for (dp = domains; dp; dp = dp->dom_next) {
4272 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4273 return (dp->dom_ifmtu((struct ifnet *)ifp));
4276 return (((struct ifnet *)ifp)->if_mtu);
4280 if_setsoftc(if_t ifp, void *softc)
4282 ((struct ifnet *)ifp)->if_softc = softc;
4287 if_getsoftc(if_t ifp)
4289 return ((struct ifnet *)ifp)->if_softc;
4293 if_setrcvif(struct mbuf *m, if_t ifp)
4295 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4299 if_setvtag(struct mbuf *m, uint16_t tag)
4301 m->m_pkthdr.ether_vtag = tag;
4305 if_getvtag(struct mbuf *m)
4308 return (m->m_pkthdr.ether_vtag);
4312 if_sendq_empty(if_t ifp)
4314 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4318 if_getifaddr(if_t ifp)
4320 return ((struct ifnet *)ifp)->if_addr;
4324 if_getamcount(if_t ifp)
4326 return ((struct ifnet *)ifp)->if_amcount;
4331 if_setsendqready(if_t ifp)
4333 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4338 if_setsendqlen(if_t ifp, int tx_desc_count)
4340 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4341 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4347 if_vlantrunkinuse(if_t ifp)
4349 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4353 if_input(if_t ifp, struct mbuf* sendmp)
4355 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4361 #ifndef ETH_ADDR_LEN
4362 #define ETH_ADDR_LEN 6
4366 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4368 struct ifmultiaddr *ifma;
4369 uint8_t *lmta = (uint8_t *)mta;
4372 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4373 if (ifma->ifma_addr->sa_family != AF_LINK)
4379 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4380 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4389 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4393 if_maddr_rlock(ifp);
4394 error = if_setupmultiaddr(ifp, mta, cnt, max);
4395 if_maddr_runlock(ifp);
4400 if_multiaddr_count(if_t ifp, int max)
4402 struct ifmultiaddr *ifma;
4406 if_maddr_rlock(ifp);
4407 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4408 if (ifma->ifma_addr->sa_family != AF_LINK)
4414 if_maddr_runlock(ifp);
4419 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4421 struct ifmultiaddr *ifma;
4424 if_maddr_rlock(ifp);
4425 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4426 cnt += filter(arg, ifma, cnt);
4427 if_maddr_runlock(ifp);
4432 if_dequeue(if_t ifp)
4435 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4441 if_sendq_prepend(if_t ifp, struct mbuf *m)
4443 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4448 if_setifheaderlen(if_t ifp, int len)
4450 ((struct ifnet *)ifp)->if_hdrlen = len;
4455 if_getlladdr(if_t ifp)
4457 return (IF_LLADDR((struct ifnet *)ifp));
4461 if_gethandle(u_char type)
4463 return (if_alloc(type));
4467 if_bpfmtap(if_t ifh, struct mbuf *m)
4469 struct ifnet *ifp = (struct ifnet *)ifh;
4475 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4477 struct ifnet *ifp = (struct ifnet *)ifh;
4479 ETHER_BPF_MTAP(ifp, m);
4483 if_vlancap(if_t ifh)
4485 struct ifnet *ifp = (struct ifnet *)ifh;
4486 VLAN_CAPABILITIES(ifp);
4490 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4493 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4498 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4501 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4506 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4509 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4514 if_gethwtsomax(if_t ifp)
4517 return (((struct ifnet *)ifp)->if_hw_tsomax);
4521 if_gethwtsomaxsegcount(if_t ifp)
4524 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4528 if_gethwtsomaxsegsize(if_t ifp)
4531 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4535 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4537 ((struct ifnet *)ifp)->if_init = init_fn;
4541 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4543 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4547 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4549 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4553 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4555 ((struct ifnet *)ifp)->if_transmit = start_fn;
4558 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4560 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4565 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4568 ifp->if_get_counter = fn;
4571 /* Revisit these - These are inline functions originally. */
4573 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4575 return drbr_inuse(ifh, br);
4579 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4581 return drbr_dequeue(ifh, br);
4585 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4587 return drbr_needs_enqueue(ifh, br);
4591 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4593 return drbr_enqueue(ifh, br, m);