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 case _IOC_NEWTYPE((cmd), struct ifgroupreq32):
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 ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
268 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
269 static void do_link_state_change(void *, int);
270 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
271 static int if_getgroupmembers(struct ifgroupreq *);
272 static void if_delgroups(struct ifnet *);
273 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
274 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
276 static void if_vmove(struct ifnet *, struct vnet *);
281 * XXX: declare here to avoid to include many inet6 related files..
282 * should be more generalized?
284 extern void nd6_setmtu(struct ifnet *);
287 /* ipsec helper hooks */
288 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
289 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
291 VNET_DEFINE(int, if_index);
292 int ifqmaxlen = IFQ_MAXLEN;
293 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
294 VNET_DEFINE(struct ifgrouphead, ifg_head);
296 static VNET_DEFINE(int, if_indexlim) = 8;
298 /* Table of ifnet by index. */
299 VNET_DEFINE(struct ifnet **, ifindex_table);
301 #define V_if_indexlim VNET(if_indexlim)
302 #define V_ifindex_table VNET(ifindex_table)
305 * The global network interface list (V_ifnet) and related state (such as
306 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
307 * an rwlock. Either may be acquired shared to stablize the list, but both
308 * must be acquired writable to modify the list. This model allows us to
309 * both stablize the interface list during interrupt thread processing, but
310 * also to stablize it over long-running ioctls, without introducing priority
311 * inversions and deadlocks.
313 struct rwlock ifnet_rwlock;
314 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
315 struct sx ifnet_sxlock;
316 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
319 * The allocation of network interfaces is a rather non-atomic affair; we
320 * need to select an index before we are ready to expose the interface for
321 * use, so will use this pointer value to indicate reservation.
323 #define IFNET_HOLD (void *)(uintptr_t)(-1)
325 static if_com_alloc_t *if_com_alloc[256];
326 static if_com_free_t *if_com_free[256];
328 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
329 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
330 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
333 ifnet_byindex_locked(u_short idx)
336 if (idx > V_if_index)
338 if (V_ifindex_table[idx] == IFNET_HOLD)
340 return (V_ifindex_table[idx]);
344 ifnet_byindex(u_short idx)
348 ifp = ifnet_byindex_locked(idx);
353 ifnet_byindex_ref(u_short idx)
357 IFNET_RLOCK_NOSLEEP();
358 ifp = ifnet_byindex_locked(idx);
359 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
360 IFNET_RUNLOCK_NOSLEEP();
364 IFNET_RUNLOCK_NOSLEEP();
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)
429 struct ifaddr *ifa = NULL;
431 IFNET_RLOCK_NOSLEEP();
432 ifp = ifnet_byindex_locked(idx);
433 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
435 IFNET_RUNLOCK_NOSLEEP();
440 * Network interface utility routines.
442 * Routines with ifa_ifwith* names take sockaddr *'s as
447 vnet_if_init(const void *unused __unused)
451 CK_STAILQ_INIT(&V_ifnet);
452 CK_STAILQ_INIT(&V_ifg_head);
454 old = if_grow(); /* create initial table */
456 epoch_wait_preempt(net_epoch_preempt);
458 vnet_if_clone_init();
460 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
465 vnet_if_uninit(const void *unused __unused)
468 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
469 "not empty", __func__, __LINE__, &V_ifnet));
470 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
471 "not empty", __func__, __LINE__, &V_ifg_head));
473 free((caddr_t)V_ifindex_table, M_IFNET);
475 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
476 vnet_if_uninit, NULL);
479 vnet_if_return(const void *unused __unused)
481 struct ifnet *ifp, *nifp;
483 /* Return all inherited interfaces to their parent vnets. */
484 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
485 if (ifp->if_home_vnet != ifp->if_vnet)
486 if_vmove(ifp, ifp->if_home_vnet);
489 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
490 vnet_if_return, NULL);
503 IFNET_WLOCK_ASSERT();
504 oldlim = V_if_indexlim;
506 n = (oldlim << 1) * sizeof(*e);
507 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
509 if (V_if_indexlim != oldlim) {
513 if (V_ifindex_table != NULL) {
514 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
515 old = V_ifindex_table;
523 * Allocate a struct ifnet and an index for an interface. A layer 2
524 * common structure will also be allocated if an allocation routine is
525 * registered for the passed type.
528 if_alloc(u_char type)
534 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
537 idx = ifindex_alloc(&old);
538 if (__predict_false(idx == USHRT_MAX)) {
540 epoch_wait_preempt(net_epoch_preempt);
544 ifnet_setbyindex(idx, IFNET_HOLD);
548 ifp->if_alloctype = type;
550 ifp->if_vnet = curvnet;
552 if (if_com_alloc[type] != NULL) {
553 ifp->if_l2com = if_com_alloc[type](type, ifp);
554 if (ifp->if_l2com == NULL) {
561 IF_ADDR_LOCK_INIT(ifp);
562 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
563 ifp->if_afdata_initialized = 0;
564 IF_AFDATA_LOCK_INIT(ifp);
565 CK_STAILQ_INIT(&ifp->if_addrhead);
566 CK_STAILQ_INIT(&ifp->if_multiaddrs);
567 CK_STAILQ_INIT(&ifp->if_groups);
571 ifq_init(&ifp->if_snd, ifp);
573 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
574 for (int i = 0; i < IFCOUNTERS; i++)
575 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
576 ifp->if_get_counter = if_get_counter_default;
577 ifp->if_pcp = IFNET_PCP_NONE;
578 ifnet_setbyindex(ifp->if_index, ifp);
583 * Do the actual work of freeing a struct ifnet, and layer 2 common
584 * structure. This call is made when the last reference to an
585 * interface is released.
588 if_free_internal(struct ifnet *ifp)
591 KASSERT((ifp->if_flags & IFF_DYING),
592 ("if_free_internal: interface not dying"));
594 if (if_com_free[ifp->if_alloctype] != NULL)
595 if_com_free[ifp->if_alloctype](ifp->if_l2com,
599 mac_ifnet_destroy(ifp);
601 if (ifp->if_description != NULL)
602 free(ifp->if_description, M_IFDESCR);
603 IF_AFDATA_DESTROY(ifp);
604 IF_ADDR_LOCK_DESTROY(ifp);
605 ifq_delete(&ifp->if_snd);
607 for (int i = 0; i < IFCOUNTERS; i++)
608 counter_u64_free(ifp->if_counters[i]);
614 if_destroy(epoch_context_t ctx)
618 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
619 if_free_internal(ifp);
623 * Deregister an interface and free the associated storage.
626 if_free(struct ifnet *ifp)
629 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
631 CURVNET_SET_QUIET(ifp->if_vnet);
633 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
634 ("%s: freeing unallocated ifnet", ifp->if_xname));
636 ifindex_free_locked(ifp->if_index);
639 if (refcount_release(&ifp->if_refcount))
640 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
645 * Interfaces to keep an ifnet type-stable despite the possibility of the
646 * driver calling if_free(). If there are additional references, we defer
647 * freeing the underlying data structure.
650 if_ref(struct ifnet *ifp)
653 /* We don't assert the ifnet list lock here, but arguably should. */
654 refcount_acquire(&ifp->if_refcount);
658 if_rele(struct ifnet *ifp)
661 if (!refcount_release(&ifp->if_refcount))
663 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
667 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
670 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
672 if (ifq->ifq_maxlen == 0)
673 ifq->ifq_maxlen = ifqmaxlen;
676 ifq->altq_disc = NULL;
677 ifq->altq_flags &= ALTQF_CANTCHANGE;
678 ifq->altq_tbr = NULL;
683 ifq_delete(struct ifaltq *ifq)
685 mtx_destroy(&ifq->ifq_mtx);
689 * Perform generic interface initialization tasks and attach the interface
690 * to the list of "active" interfaces. If vmove flag is set on entry
691 * to if_attach_internal(), perform only a limited subset of initialization
692 * tasks, given that we are moving from one vnet to another an ifnet which
693 * has already been fully initialized.
695 * Note that if_detach_internal() removes group membership unconditionally
696 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
697 * Thus, when if_vmove() is applied to a cloned interface, group membership
698 * is lost while a cloned one always joins a group whose name is
699 * ifc->ifc_name. To recover this after if_detach_internal() and
700 * if_attach_internal(), the cloner should be specified to
701 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
702 * attempts to join a group whose name is ifc->ifc_name.
705 * - The decision to return void and thus require this function to
706 * succeed is questionable.
707 * - We should probably do more sanity checking. For instance we don't
708 * do anything to insure if_xname is unique or non-empty.
711 if_attach(struct ifnet *ifp)
714 if_attach_internal(ifp, 0, NULL);
718 * Compute the least common TSO limit.
721 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
724 * 1) If there is no limit currently, take the limit from
725 * the network adapter.
727 * 2) If the network adapter has a limit below the current
730 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
731 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
732 pmax->tsomaxbytes = ifp->if_hw_tsomax;
734 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
735 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
736 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
738 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
739 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
740 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
745 * Update TSO limit of a network adapter.
747 * Returns zero if no change. Else non-zero.
750 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
753 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
754 ifp->if_hw_tsomax = pmax->tsomaxbytes;
757 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
758 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
761 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
762 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
769 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
771 unsigned socksize, ifasize;
772 int namelen, masklen;
773 struct sockaddr_dl *sdl;
776 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
777 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
781 ifp->if_vnet = curvnet;
782 if (ifp->if_home_vnet == NULL)
783 ifp->if_home_vnet = curvnet;
786 if_addgroup(ifp, IFG_ALL);
788 /* Restore group membership for cloned interfaces. */
789 if (vmove && ifc != NULL)
790 if_clone_addgroup(ifp, ifc);
792 getmicrotime(&ifp->if_lastchange);
793 ifp->if_epoch = time_uptime;
795 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
796 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
797 ("transmit and qflush must both either be set or both be NULL"));
798 if (ifp->if_transmit == NULL) {
799 ifp->if_transmit = if_transmit;
800 ifp->if_qflush = if_qflush;
802 if (ifp->if_input == NULL)
803 ifp->if_input = if_input_default;
805 if (ifp->if_requestencap == NULL)
806 ifp->if_requestencap = if_requestencap_default;
810 mac_ifnet_create(ifp);
814 * Create a Link Level name for this device.
816 namelen = strlen(ifp->if_xname);
818 * Always save enough space for any possiable name so we
819 * can do a rename in place later.
821 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
822 socksize = masklen + ifp->if_addrlen;
823 if (socksize < sizeof(*sdl))
824 socksize = sizeof(*sdl);
825 socksize = roundup2(socksize, sizeof(long));
826 ifasize = sizeof(*ifa) + 2 * socksize;
827 ifa = ifa_alloc(ifasize, M_WAITOK);
828 sdl = (struct sockaddr_dl *)(ifa + 1);
829 sdl->sdl_len = socksize;
830 sdl->sdl_family = AF_LINK;
831 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
832 sdl->sdl_nlen = namelen;
833 sdl->sdl_index = ifp->if_index;
834 sdl->sdl_type = ifp->if_type;
837 ifa->ifa_rtrequest = link_rtrequest;
838 ifa->ifa_addr = (struct sockaddr *)sdl;
839 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
840 ifa->ifa_netmask = (struct sockaddr *)sdl;
841 sdl->sdl_len = masklen;
843 sdl->sdl_data[--namelen] = 0xff;
844 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
845 /* Reliably crash if used uninitialized. */
846 ifp->if_broadcastaddr = NULL;
848 if (ifp->if_type == IFT_ETHER) {
849 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
853 #if defined(INET) || defined(INET6)
854 /* Use defaults for TSO, if nothing is set */
855 if (ifp->if_hw_tsomax == 0 &&
856 ifp->if_hw_tsomaxsegcount == 0 &&
857 ifp->if_hw_tsomaxsegsize == 0) {
859 * The TSO defaults needs to be such that an
860 * NFS mbuf list of 35 mbufs totalling just
861 * below 64K works and that a chain of mbufs
862 * can be defragged into at most 32 segments:
864 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
865 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
866 ifp->if_hw_tsomaxsegcount = 35;
867 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
869 /* XXX some drivers set IFCAP_TSO after ethernet attach */
870 if (ifp->if_capabilities & IFCAP_TSO) {
871 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
873 ifp->if_hw_tsomaxsegcount,
874 ifp->if_hw_tsomaxsegsize);
882 * Update the interface index in the link layer address
885 for (ifa = ifp->if_addr; ifa != NULL;
886 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
887 if (ifa->ifa_addr->sa_family == AF_LINK) {
888 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
889 sdl->sdl_index = ifp->if_index;
896 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
898 curvnet->vnet_ifcnt++;
902 if (domain_init_status >= 2)
903 if_attachdomain1(ifp);
905 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
906 if (IS_DEFAULT_VNET(curvnet))
907 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
909 /* Announce the interface. */
910 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
914 if_epochalloc(void *dummy __unused)
917 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
918 net_epoch = epoch_alloc(0);
920 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
921 if_epochalloc, NULL);
924 if_attachdomain(void *dummy)
928 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
929 if_attachdomain1(ifp);
931 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
932 if_attachdomain, NULL);
935 if_attachdomain1(struct ifnet *ifp)
940 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
941 * cannot lock ifp->if_afdata initialization, entirely.
944 if (ifp->if_afdata_initialized >= domain_init_status) {
945 IF_AFDATA_UNLOCK(ifp);
946 log(LOG_WARNING, "%s called more than once on %s\n",
947 __func__, ifp->if_xname);
950 ifp->if_afdata_initialized = domain_init_status;
951 IF_AFDATA_UNLOCK(ifp);
953 /* address family dependent data region */
954 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
955 for (dp = domains; dp; dp = dp->dom_next) {
956 if (dp->dom_ifattach)
957 ifp->if_afdata[dp->dom_family] =
958 (*dp->dom_ifattach)(ifp);
963 * Remove any unicast or broadcast network addresses from an interface.
966 if_purgeaddrs(struct ifnet *ifp)
968 struct ifaddr *ifa, *next;
970 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
971 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
972 if (ifa->ifa_addr->sa_family == AF_LINK)
975 /* XXX: Ugly!! ad hoc just for INET */
976 if (ifa->ifa_addr->sa_family == AF_INET) {
977 struct ifaliasreq ifr;
979 bzero(&ifr, sizeof(ifr));
980 ifr.ifra_addr = *ifa->ifa_addr;
981 if (ifa->ifa_dstaddr)
982 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
983 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
989 if (ifa->ifa_addr->sa_family == AF_INET6) {
991 /* ifp_addrhead is already updated */
996 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
997 IF_ADDR_WUNLOCK(ifp);
1003 * Remove any multicast network addresses from an interface when an ifnet
1007 if_purgemaddrs(struct ifnet *ifp)
1009 struct ifmultiaddr *ifma;
1012 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1013 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1014 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1015 if_delmulti_locked(ifp, ifma, 1);
1017 IF_ADDR_WUNLOCK(ifp);
1021 * Detach an interface, removing it from the list of "active" interfaces.
1022 * If vmove flag is set on entry to if_detach_internal(), perform only a
1023 * limited subset of cleanup tasks, given that we are moving an ifnet from
1024 * one vnet to another, where it must be fully operational.
1026 * XXXRW: There are some significant questions about event ordering, and
1027 * how to prevent things from starting to use the interface during detach.
1030 if_detach(struct ifnet *ifp)
1033 CURVNET_SET_QUIET(ifp->if_vnet);
1034 if_detach_internal(ifp, 0, NULL);
1039 * The vmove flag, if set, indicates that we are called from a callpath
1040 * that is moving an interface to a different vnet instance.
1042 * The shutdown flag, if set, indicates that we are called in the
1043 * process of shutting down a vnet instance. Currently only the
1044 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1045 * on a vnet instance shutdown without this flag being set, e.g., when
1046 * the cloned interfaces are destoyed as first thing of teardown.
1049 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1059 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1060 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1063 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1065 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1072 * While we would want to panic here, we cannot
1073 * guarantee that the interface is indeed still on
1074 * the list given we don't hold locks all the way.
1079 panic("%s: ifp=%p not on the ifnet tailq %p",
1080 __func__, ifp, &V_ifnet);
1082 return; /* XXX this should panic as well? */
1087 * At this point we know the interface still was on the ifnet list
1088 * and we removed it so we are in a stable state.
1091 curvnet->vnet_ifcnt--;
1093 epoch_wait_preempt(net_epoch_preempt);
1095 * In any case (destroy or vmove) detach us from the groups
1096 * and remove/wait for pending events on the taskq.
1097 * XXX-BZ in theory an interface could still enqueue a taskq change?
1101 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1104 * Check if this is a cloned interface or not. Must do even if
1105 * shutting down as a if_vmove_reclaim() would move the ifp and
1106 * the if_clone_addgroup() will have a corrupted string overwise
1107 * from a gibberish pointer.
1109 if (vmove && ifcp != NULL)
1110 *ifcp = if_clone_findifc(ifp);
1116 * On VNET shutdown abort here as the stack teardown will do all
1117 * the work top-down for us.
1121 * In case of a vmove we are done here without error.
1122 * If we would signal an error it would lead to the same
1123 * abort as if we did not find the ifnet anymore.
1124 * if_detach() calls us in void context and does not care
1125 * about an early abort notification, so life is splendid :)
1127 goto finish_vnet_shutdown;
1132 * At this point we are not tearing down a VNET and are either
1133 * going to destroy or vmove the interface and have to cleanup
1138 * Remove routes and flush queues.
1141 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1142 altq_disable(&ifp->if_snd);
1143 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1144 altq_detach(&ifp->if_snd);
1155 * Remove all IPv6 kernel structs related to ifp. This should be done
1156 * before removing routing entries below, since IPv6 interface direct
1157 * routes are expected to be removed by the IPv6-specific kernel API.
1158 * Otherwise, the kernel will detect some inconsistency and bark it.
1162 if_purgemaddrs(ifp);
1164 /* Announce that the interface is gone. */
1165 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1166 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1167 if (IS_DEFAULT_VNET(curvnet))
1168 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1172 * Prevent further calls into the device driver via ifnet.
1177 * Remove link ifaddr pointer and maybe decrement if_index.
1178 * Clean up all addresses.
1180 free(ifp->if_hw_addr, M_IFADDR);
1181 ifp->if_hw_addr = NULL;
1182 ifp->if_addr = NULL;
1184 /* We can now free link ifaddr. */
1186 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1187 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1188 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1189 IF_ADDR_WUNLOCK(ifp);
1192 IF_ADDR_WUNLOCK(ifp);
1195 rt_flushifroutes(ifp);
1198 finish_vnet_shutdown:
1201 * We cannot hold the lock over dom_ifdetach calls as they might
1202 * sleep, for example trying to drain a callout, thus open up the
1203 * theoretical race with re-attaching.
1205 IF_AFDATA_LOCK(ifp);
1206 i = ifp->if_afdata_initialized;
1207 ifp->if_afdata_initialized = 0;
1208 IF_AFDATA_UNLOCK(ifp);
1209 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1210 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1211 (*dp->dom_ifdetach)(ifp,
1212 ifp->if_afdata[dp->dom_family]);
1213 ifp->if_afdata[dp->dom_family] = NULL;
1222 * if_vmove() performs a limited version of if_detach() in current
1223 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1224 * An attempt is made to shrink if_index in current vnet, find an
1225 * unused if_index in target vnet and calls if_grow() if necessary,
1226 * and finally find an unused if_xname for the target vnet.
1229 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1231 struct if_clone *ifc;
1232 u_int bif_dlt, bif_hdrlen;
1237 * if_detach_internal() will call the eventhandler to notify
1238 * interface departure. That will detach if_bpf. We need to
1239 * safe the dlt and hdrlen so we can re-attach it later.
1241 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1244 * Detach from current vnet, but preserve LLADDR info, do not
1245 * mark as dead etc. so that the ifnet can be reattached later.
1246 * If we cannot find it, we lost the race to someone else.
1248 rc = if_detach_internal(ifp, 1, &ifc);
1253 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1254 * the if_index for that vnet if possible.
1256 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1257 * or we'd lock on one vnet and unlock on another.
1260 ifindex_free_locked(ifp->if_index);
1264 * Perform interface-specific reassignment tasks, if provided by
1267 if (ifp->if_reassign != NULL)
1268 ifp->if_reassign(ifp, new_vnet, NULL);
1271 * Switch to the context of the target vnet.
1273 CURVNET_SET_QUIET(new_vnet);
1276 ifp->if_index = ifindex_alloc(&old);
1277 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1279 epoch_wait_preempt(net_epoch_preempt);
1283 ifnet_setbyindex(ifp->if_index, ifp);
1286 if_attach_internal(ifp, 1, ifc);
1288 if (ifp->if_bpf == NULL)
1289 bpfattach(ifp, bif_dlt, bif_hdrlen);
1295 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1298 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1304 /* Try to find the prison within our visibility. */
1305 sx_slock(&allprison_lock);
1306 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1307 sx_sunlock(&allprison_lock);
1310 prison_hold_locked(pr);
1311 mtx_unlock(&pr->pr_mtx);
1313 /* Do not try to move the iface from and to the same prison. */
1314 if (pr->pr_vnet == ifp->if_vnet) {
1319 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1320 /* XXX Lock interfaces to avoid races. */
1321 CURVNET_SET_QUIET(pr->pr_vnet);
1322 difp = ifunit(ifname);
1329 /* Make sure the VNET is stable. */
1330 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1331 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1339 /* Move the interface into the child jail/vnet. */
1340 if_vmove(ifp, pr->pr_vnet);
1342 /* Report the new if_xname back to the userland. */
1343 sprintf(ifname, "%s", ifp->if_xname);
1350 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1353 struct vnet *vnet_dst;
1357 /* Try to find the prison within our visibility. */
1358 sx_slock(&allprison_lock);
1359 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1360 sx_sunlock(&allprison_lock);
1363 prison_hold_locked(pr);
1364 mtx_unlock(&pr->pr_mtx);
1366 /* Make sure the named iface exists in the source prison/vnet. */
1367 CURVNET_SET(pr->pr_vnet);
1368 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1375 /* Do not try to move the iface from and to the same prison. */
1376 vnet_dst = TD_TO_VNET(td);
1377 if (vnet_dst == ifp->if_vnet) {
1383 /* Make sure the VNET is stable. */
1384 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1385 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1392 /* Get interface back from child jail/vnet. */
1393 if_vmove(ifp, vnet_dst);
1396 /* Report the new if_xname back to the userland. */
1397 sprintf(ifname, "%s", ifp->if_xname);
1405 * Add a group to an interface
1408 if_addgroup(struct ifnet *ifp, const char *groupname)
1410 struct ifg_list *ifgl;
1411 struct ifg_group *ifg = NULL;
1412 struct ifg_member *ifgm;
1415 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1416 groupname[strlen(groupname) - 1] <= '9')
1420 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1421 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1426 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1427 M_NOWAIT)) == NULL) {
1432 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1433 M_TEMP, M_NOWAIT)) == NULL) {
1439 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1440 if (!strcmp(ifg->ifg_group, groupname))
1444 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1445 M_TEMP, M_NOWAIT)) == NULL) {
1451 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1452 ifg->ifg_refcnt = 0;
1453 CK_STAILQ_INIT(&ifg->ifg_members);
1454 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1459 ifgl->ifgl_group = ifg;
1460 ifgm->ifgm_ifp = ifp;
1463 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1464 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1465 IF_ADDR_WUNLOCK(ifp);
1470 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1471 EVENTHANDLER_INVOKE(group_change_event, groupname);
1477 * Remove a group from an interface
1480 if_delgroup(struct ifnet *ifp, const char *groupname)
1482 struct ifg_list *ifgl;
1483 struct ifg_member *ifgm;
1487 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1488 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1497 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1498 IF_ADDR_WUNLOCK(ifp);
1500 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1501 if (ifgm->ifgm_ifp == ifp)
1505 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next);
1507 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1508 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1513 epoch_wait_preempt(net_epoch_preempt);
1515 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1516 free(ifgl->ifgl_group, M_TEMP);
1521 EVENTHANDLER_INVOKE(group_change_event, groupname);
1527 * Remove an interface from all groups
1530 if_delgroups(struct ifnet *ifp)
1532 struct ifg_list *ifgl;
1533 struct ifg_member *ifgm;
1534 char groupname[IFNAMSIZ];
1538 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) {
1539 ifgl = CK_STAILQ_FIRST(&ifp->if_groups);
1541 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1544 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1545 IF_ADDR_WUNLOCK(ifp);
1547 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1548 if (ifgm->ifgm_ifp == ifp)
1552 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member,
1555 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1556 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1560 epoch_wait_preempt(net_epoch_preempt);
1563 EVENTHANDLER_INVOKE(group_detach_event,
1565 free(ifgl->ifgl_group, M_TEMP);
1567 EVENTHANDLER_INVOKE(group_change_event, groupname);
1575 ifgr_group_get(void *ifgrp)
1577 union ifgroupreq_union *ifgrup;
1580 #ifdef COMPAT_FREEBSD32
1581 if (SV_CURPROC_FLAG(SV_ILP32))
1582 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1584 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1587 static struct ifg_req *
1588 ifgr_groups_get(void *ifgrp)
1590 union ifgroupreq_union *ifgrup;
1593 #ifdef COMPAT_FREEBSD32
1594 if (SV_CURPROC_FLAG(SV_ILP32))
1595 return ((struct ifg_req *)(uintptr_t)
1596 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1598 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1602 * Stores all groups from an interface in memory pointed to by ifgr.
1605 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1608 struct ifg_list *ifgl;
1609 struct ifg_req ifgrq, *ifgp;
1611 if (ifgr->ifgr_len == 0) {
1613 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1614 ifgr->ifgr_len += sizeof(struct ifg_req);
1615 IF_ADDR_RUNLOCK(ifp);
1619 len = ifgr->ifgr_len;
1620 ifgp = ifgr_groups_get(ifgr);
1623 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1624 if (len < sizeof(ifgrq)) {
1625 IF_ADDR_RUNLOCK(ifp);
1628 bzero(&ifgrq, sizeof ifgrq);
1629 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1630 sizeof(ifgrq.ifgrq_group));
1631 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1632 IF_ADDR_RUNLOCK(ifp);
1635 len -= sizeof(ifgrq);
1638 IF_ADDR_RUNLOCK(ifp);
1644 * Stores all members of a group in memory pointed to by igfr
1647 if_getgroupmembers(struct ifgroupreq *ifgr)
1649 struct ifg_group *ifg;
1650 struct ifg_member *ifgm;
1651 struct ifg_req ifgrq, *ifgp;
1655 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1656 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1663 if (ifgr->ifgr_len == 0) {
1664 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1665 ifgr->ifgr_len += sizeof(ifgrq);
1670 len = ifgr->ifgr_len;
1671 ifgp = ifgr_groups_get(ifgr);
1672 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1673 if (len < sizeof(ifgrq)) {
1677 bzero(&ifgrq, sizeof ifgrq);
1678 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1679 sizeof(ifgrq.ifgrq_member));
1680 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1684 len -= sizeof(ifgrq);
1693 * Return counter values from counter(9)s stored in ifnet.
1696 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1699 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1701 return (counter_u64_fetch(ifp->if_counters[cnt]));
1705 * Increase an ifnet counter. Usually used for counters shared
1706 * between the stack and a driver, but function supports them all.
1709 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1712 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1714 counter_u64_add(ifp->if_counters[cnt], inc);
1718 * Copy data from ifnet to userland API structure if_data.
1721 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1724 ifd->ifi_type = ifp->if_type;
1725 ifd->ifi_physical = 0;
1726 ifd->ifi_addrlen = ifp->if_addrlen;
1727 ifd->ifi_hdrlen = ifp->if_hdrlen;
1728 ifd->ifi_link_state = ifp->if_link_state;
1730 ifd->ifi_datalen = sizeof(struct if_data);
1731 ifd->ifi_mtu = ifp->if_mtu;
1732 ifd->ifi_metric = ifp->if_metric;
1733 ifd->ifi_baudrate = ifp->if_baudrate;
1734 ifd->ifi_hwassist = ifp->if_hwassist;
1735 ifd->ifi_epoch = ifp->if_epoch;
1736 ifd->ifi_lastchange = ifp->if_lastchange;
1738 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1739 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1740 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1741 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1742 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1743 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1744 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1745 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1746 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1747 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1748 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1749 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1753 * Wrapper functions for struct ifnet address list locking macros. These are
1754 * used by kernel modules to avoid encoding programming interface or binary
1755 * interface assumptions that may be violated when kernel-internal locking
1756 * approaches change.
1759 if_addr_rlock(struct ifnet *ifp)
1766 if_addr_runlock(struct ifnet *ifp)
1769 IF_ADDR_RUNLOCK(ifp);
1773 if_maddr_rlock(if_t ifp)
1776 IF_ADDR_RLOCK((struct ifnet *)ifp);
1780 if_maddr_runlock(if_t ifp)
1783 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1787 * Initialization, destruction and refcounting functions for ifaddrs.
1790 ifa_alloc(size_t size, int flags)
1794 KASSERT(size >= sizeof(struct ifaddr),
1795 ("%s: invalid size %zu", __func__, size));
1797 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1801 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1803 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1805 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1807 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1810 refcount_init(&ifa->ifa_refcnt, 1);
1815 /* free(NULL) is okay */
1816 counter_u64_free(ifa->ifa_opackets);
1817 counter_u64_free(ifa->ifa_ipackets);
1818 counter_u64_free(ifa->ifa_obytes);
1819 counter_u64_free(ifa->ifa_ibytes);
1820 free(ifa, M_IFADDR);
1826 ifa_ref(struct ifaddr *ifa)
1829 refcount_acquire(&ifa->ifa_refcnt);
1833 ifa_destroy(epoch_context_t ctx)
1837 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1838 counter_u64_free(ifa->ifa_opackets);
1839 counter_u64_free(ifa->ifa_ipackets);
1840 counter_u64_free(ifa->ifa_obytes);
1841 counter_u64_free(ifa->ifa_ibytes);
1842 free(ifa, M_IFADDR);
1846 ifa_free(struct ifaddr *ifa)
1849 if (refcount_release(&ifa->ifa_refcnt))
1850 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1855 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1856 struct sockaddr *ia)
1859 struct rt_addrinfo info;
1860 struct sockaddr_dl null_sdl;
1865 bzero(&info, sizeof(info));
1866 if (cmd != RTM_DELETE)
1867 info.rti_ifp = V_loif;
1868 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1869 info.rti_info[RTAX_DST] = ia;
1870 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1871 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1873 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1876 !(cmd == RTM_ADD && error == EEXIST) &&
1877 !(cmd == RTM_DELETE && error == ENOENT))
1878 if_printf(ifp, "%s failed: %d\n", otype, error);
1884 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1887 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1891 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1894 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1898 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1901 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1905 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1906 * structs used to represent other address families, it is necessary
1907 * to perform a different comparison.
1910 #define sa_dl_equal(a1, a2) \
1911 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1912 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1913 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1914 CLLADDR((const struct sockaddr_dl *)(a2)), \
1915 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1918 * Locate an interface based on a complete address.
1922 ifa_ifwithaddr(const struct sockaddr *addr)
1928 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1929 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1930 if (ifa->ifa_addr->sa_family != addr->sa_family)
1932 if (sa_equal(addr, ifa->ifa_addr)) {
1935 /* IP6 doesn't have broadcast */
1936 if ((ifp->if_flags & IFF_BROADCAST) &&
1937 ifa->ifa_broadaddr &&
1938 ifa->ifa_broadaddr->sa_len != 0 &&
1939 sa_equal(ifa->ifa_broadaddr, addr)) {
1950 ifa_ifwithaddr_check(const struct sockaddr *addr)
1955 rc = (ifa_ifwithaddr(addr) != NULL);
1961 * Locate an interface based on the broadcast address.
1965 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1971 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1972 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1974 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1975 if (ifa->ifa_addr->sa_family != addr->sa_family)
1977 if ((ifp->if_flags & IFF_BROADCAST) &&
1978 ifa->ifa_broadaddr &&
1979 ifa->ifa_broadaddr->sa_len != 0 &&
1980 sa_equal(ifa->ifa_broadaddr, addr)) {
1991 * Locate the point to point interface with a given destination address.
1995 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2001 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2002 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2004 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2006 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2007 if (ifa->ifa_addr->sa_family != addr->sa_family)
2009 if (ifa->ifa_dstaddr != NULL &&
2010 sa_equal(addr, ifa->ifa_dstaddr)) {
2021 * Find an interface on a specific network. If many, choice
2022 * is most specific found.
2025 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2029 struct ifaddr *ifa_maybe = NULL;
2030 u_int af = addr->sa_family;
2031 const char *addr_data = addr->sa_data, *cplim;
2035 * AF_LINK addresses can be looked up directly by their index number,
2036 * so do that if we can.
2038 if (af == AF_LINK) {
2039 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2040 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2041 return (ifaddr_byindex(sdl->sdl_index));
2045 * Scan though each interface, looking for ones that have addresses
2046 * in this address family and the requested fib. Maintain a reference
2047 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2048 * kept it stable when we move onto the next interface.
2050 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2051 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2053 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2054 const char *cp, *cp2, *cp3;
2056 if (ifa->ifa_addr->sa_family != af)
2058 if (af == AF_INET &&
2059 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2061 * This is a bit broken as it doesn't
2062 * take into account that the remote end may
2063 * be a single node in the network we are
2065 * The trouble is that we don't know the
2066 * netmask for the remote end.
2068 if (ifa->ifa_dstaddr != NULL &&
2069 sa_equal(addr, ifa->ifa_dstaddr)) {
2070 IF_ADDR_RUNLOCK(ifp);
2075 * Scan all the bits in the ifa's address.
2076 * If a bit dissagrees with what we are
2077 * looking for, mask it with the netmask
2078 * to see if it really matters.
2079 * (A byte at a time)
2081 if (ifa->ifa_netmask == 0)
2084 cp2 = ifa->ifa_addr->sa_data;
2085 cp3 = ifa->ifa_netmask->sa_data;
2086 cplim = ifa->ifa_netmask->sa_len
2087 + (char *)ifa->ifa_netmask;
2089 if ((*cp++ ^ *cp2++) & *cp3++)
2090 goto next; /* next address! */
2092 * If the netmask of what we just found
2093 * is more specific than what we had before
2094 * (if we had one), or if the virtual status
2095 * of new prefix is better than of the old one,
2096 * then remember the new one before continuing
2097 * to search for an even better one.
2099 if (ifa_maybe == NULL ||
2100 ifa_preferred(ifa_maybe, ifa) ||
2101 rn_refines((caddr_t)ifa->ifa_netmask,
2102 (caddr_t)ifa_maybe->ifa_netmask)) {
2115 * Find an interface address specific to an interface best matching
2119 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2122 const char *cp, *cp2, *cp3;
2124 struct ifaddr *ifa_maybe = NULL;
2125 u_int af = addr->sa_family;
2130 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2131 if (ifa->ifa_addr->sa_family != af)
2133 if (ifa_maybe == NULL)
2135 if (ifa->ifa_netmask == 0) {
2136 if (sa_equal(addr, ifa->ifa_addr) ||
2137 (ifa->ifa_dstaddr &&
2138 sa_equal(addr, ifa->ifa_dstaddr)))
2142 if (ifp->if_flags & IFF_POINTOPOINT) {
2143 if (sa_equal(addr, ifa->ifa_dstaddr))
2147 cp2 = ifa->ifa_addr->sa_data;
2148 cp3 = ifa->ifa_netmask->sa_data;
2149 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2150 for (; cp3 < cplim; cp3++)
2151 if ((*cp++ ^ *cp2++) & *cp3)
2163 * See whether new ifa is better than current one:
2164 * 1) A non-virtual one is preferred over virtual.
2165 * 2) A virtual in master state preferred over any other state.
2167 * Used in several address selecting functions.
2170 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2173 return (cur->ifa_carp && (!next->ifa_carp ||
2174 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2177 #include <net/if_llatbl.h>
2180 * Default action when installing a route with a Link Level gateway.
2181 * Lookup an appropriate real ifa to point to.
2182 * This should be moved to /sys/net/link.c eventually.
2185 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2187 struct ifaddr *ifa, *oifa;
2188 struct sockaddr *dst;
2191 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2192 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2195 ifa = ifaof_ifpforaddr(dst, ifp);
2199 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2200 ifa->ifa_rtrequest(cmd, rt, info);
2205 struct sockaddr_dl *
2206 link_alloc_sdl(size_t size, int flags)
2209 return (malloc(size, M_TEMP, flags));
2213 link_free_sdl(struct sockaddr *sa)
2219 * Fills in given sdl with interface basic info.
2220 * Returns pointer to filled sdl.
2222 struct sockaddr_dl *
2223 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2225 struct sockaddr_dl *sdl;
2227 sdl = (struct sockaddr_dl *)paddr;
2228 memset(sdl, 0, sizeof(struct sockaddr_dl));
2229 sdl->sdl_len = sizeof(struct sockaddr_dl);
2230 sdl->sdl_family = AF_LINK;
2231 sdl->sdl_index = ifp->if_index;
2232 sdl->sdl_type = iftype;
2238 * Mark an interface down and notify protocols of
2242 if_unroute(struct ifnet *ifp, int flag, int fam)
2246 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2248 ifp->if_flags &= ~flag;
2249 getmicrotime(&ifp->if_lastchange);
2250 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2251 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2252 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2253 ifp->if_qflush(ifp);
2256 (*carp_linkstate_p)(ifp);
2261 * Mark an interface up and notify protocols of
2265 if_route(struct ifnet *ifp, int flag, int fam)
2269 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2271 ifp->if_flags |= flag;
2272 getmicrotime(&ifp->if_lastchange);
2273 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2274 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2275 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2277 (*carp_linkstate_p)(ifp);
2284 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2285 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2286 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2287 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2288 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2289 int (*vlan_setcookie_p)(struct ifnet *, void *);
2290 void *(*vlan_cookie_p)(struct ifnet *);
2293 * Handle a change in the interface link state. To avoid LORs
2294 * between driver lock and upper layer locks, as well as possible
2295 * recursions, we post event to taskqueue, and all job
2296 * is done in static do_link_state_change().
2299 if_link_state_change(struct ifnet *ifp, int link_state)
2301 /* Return if state hasn't changed. */
2302 if (ifp->if_link_state == link_state)
2305 ifp->if_link_state = link_state;
2307 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2311 do_link_state_change(void *arg, int pending)
2313 struct ifnet *ifp = (struct ifnet *)arg;
2314 int link_state = ifp->if_link_state;
2315 CURVNET_SET(ifp->if_vnet);
2317 /* Notify that the link state has changed. */
2319 if (ifp->if_vlantrunk != NULL)
2320 (*vlan_link_state_p)(ifp);
2322 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2323 ifp->if_l2com != NULL)
2324 (*ng_ether_link_state_p)(ifp, link_state);
2326 (*carp_linkstate_p)(ifp);
2328 ifp->if_bridge_linkstate(ifp);
2330 (*lagg_linkstate_p)(ifp, link_state);
2332 if (IS_DEFAULT_VNET(curvnet))
2333 devctl_notify("IFNET", ifp->if_xname,
2334 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2337 if_printf(ifp, "%d link states coalesced\n", pending);
2338 if (log_link_state_change)
2339 if_printf(ifp, "link state changed to %s\n",
2340 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2341 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2346 * Mark an interface down and notify protocols of
2350 if_down(struct ifnet *ifp)
2353 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2354 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2358 * Mark an interface up and notify protocols of
2362 if_up(struct ifnet *ifp)
2365 if_route(ifp, IFF_UP, AF_UNSPEC);
2366 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2370 * Flush an interface queue.
2373 if_qflush(struct ifnet *ifp)
2381 if (ALTQ_IS_ENABLED(ifq))
2385 while ((m = n) != NULL) {
2396 * Map interface name to interface structure pointer, with or without
2397 * returning a reference.
2400 ifunit_ref(const char *name)
2404 IFNET_RLOCK_NOSLEEP();
2405 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2406 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2407 !(ifp->if_flags & IFF_DYING))
2412 IFNET_RUNLOCK_NOSLEEP();
2417 ifunit(const char *name)
2421 IFNET_RLOCK_NOSLEEP();
2422 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2423 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2426 IFNET_RUNLOCK_NOSLEEP();
2431 ifr_buffer_get_buffer(void *data)
2433 union ifreq_union *ifrup;
2436 #ifdef COMPAT_FREEBSD32
2437 if (SV_CURPROC_FLAG(SV_ILP32))
2438 return ((void *)(uintptr_t)
2439 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2441 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2445 ifr_buffer_set_buffer_null(void *data)
2447 union ifreq_union *ifrup;
2450 #ifdef COMPAT_FREEBSD32
2451 if (SV_CURPROC_FLAG(SV_ILP32))
2452 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2455 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2459 ifr_buffer_get_length(void *data)
2461 union ifreq_union *ifrup;
2464 #ifdef COMPAT_FREEBSD32
2465 if (SV_CURPROC_FLAG(SV_ILP32))
2466 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2468 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2472 ifr_buffer_set_length(void *data, size_t len)
2474 union ifreq_union *ifrup;
2477 #ifdef COMPAT_FREEBSD32
2478 if (SV_CURPROC_FLAG(SV_ILP32))
2479 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2482 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2486 ifr_data_get_ptr(void *ifrp)
2488 union ifreq_union *ifrup;
2491 #ifdef COMPAT_FREEBSD32
2492 if (SV_CURPROC_FLAG(SV_ILP32))
2493 return ((void *)(uintptr_t)
2494 ifrup->ifr32.ifr_ifru.ifru_data);
2496 return (ifrup->ifr.ifr_ifru.ifru_data);
2500 * Hardware specific interface ioctls.
2503 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2506 int error = 0, do_ifup = 0;
2507 int new_flags, temp_flags;
2508 size_t namelen, onamelen;
2510 char *descrbuf, *odescrbuf;
2511 char new_name[IFNAMSIZ];
2513 struct sockaddr_dl *sdl;
2515 ifr = (struct ifreq *)data;
2518 ifr->ifr_index = ifp->if_index;
2522 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2523 ifr->ifr_flags = temp_flags & 0xffff;
2524 ifr->ifr_flagshigh = temp_flags >> 16;
2528 ifr->ifr_reqcap = ifp->if_capabilities;
2529 ifr->ifr_curcap = ifp->if_capenable;
2534 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2539 ifr->ifr_metric = ifp->if_metric;
2543 ifr->ifr_mtu = ifp->if_mtu;
2547 /* XXXGL: did this ever worked? */
2553 sx_slock(&ifdescr_sx);
2554 if (ifp->if_description == NULL)
2557 /* space for terminating nul */
2558 descrlen = strlen(ifp->if_description) + 1;
2559 if (ifr_buffer_get_length(ifr) < descrlen)
2560 ifr_buffer_set_buffer_null(ifr);
2562 error = copyout(ifp->if_description,
2563 ifr_buffer_get_buffer(ifr), descrlen);
2564 ifr_buffer_set_length(ifr, descrlen);
2566 sx_sunlock(&ifdescr_sx);
2570 error = priv_check(td, PRIV_NET_SETIFDESCR);
2575 * Copy only (length-1) bytes to make sure that
2576 * if_description is always nul terminated. The
2577 * length parameter is supposed to count the
2578 * terminating nul in.
2580 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2581 return (ENAMETOOLONG);
2582 else if (ifr_buffer_get_length(ifr) == 0)
2585 descrbuf = malloc(ifr_buffer_get_length(ifr),
2586 M_IFDESCR, M_WAITOK | M_ZERO);
2587 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2588 ifr_buffer_get_length(ifr) - 1);
2590 free(descrbuf, M_IFDESCR);
2595 sx_xlock(&ifdescr_sx);
2596 odescrbuf = ifp->if_description;
2597 ifp->if_description = descrbuf;
2598 sx_xunlock(&ifdescr_sx);
2600 getmicrotime(&ifp->if_lastchange);
2601 free(odescrbuf, M_IFDESCR);
2605 ifr->ifr_fib = ifp->if_fib;
2609 error = priv_check(td, PRIV_NET_SETIFFIB);
2612 if (ifr->ifr_fib >= rt_numfibs)
2615 ifp->if_fib = ifr->ifr_fib;
2619 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2623 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2624 * check, so we don't need special handling here yet.
2626 new_flags = (ifr->ifr_flags & 0xffff) |
2627 (ifr->ifr_flagshigh << 16);
2628 if (ifp->if_flags & IFF_UP &&
2629 (new_flags & IFF_UP) == 0) {
2631 } else if (new_flags & IFF_UP &&
2632 (ifp->if_flags & IFF_UP) == 0) {
2635 /* See if permanently promiscuous mode bit is about to flip */
2636 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2637 if (new_flags & IFF_PPROMISC)
2638 ifp->if_flags |= IFF_PROMISC;
2639 else if (ifp->if_pcount == 0)
2640 ifp->if_flags &= ~IFF_PROMISC;
2641 if (log_promisc_mode_change)
2642 if_printf(ifp, "permanently promiscuous mode %s\n",
2643 ((new_flags & IFF_PPROMISC) ?
2644 "enabled" : "disabled"));
2646 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2647 (new_flags &~ IFF_CANTCHANGE);
2648 if (ifp->if_ioctl) {
2649 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2653 getmicrotime(&ifp->if_lastchange);
2657 error = priv_check(td, PRIV_NET_SETIFCAP);
2660 if (ifp->if_ioctl == NULL)
2661 return (EOPNOTSUPP);
2662 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2664 error = (*ifp->if_ioctl)(ifp, cmd, data);
2666 getmicrotime(&ifp->if_lastchange);
2671 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2676 error = priv_check(td, PRIV_NET_SETIFNAME);
2679 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2683 if (new_name[0] == '\0')
2685 if (new_name[IFNAMSIZ-1] != '\0') {
2686 new_name[IFNAMSIZ-1] = '\0';
2687 if (strlen(new_name) == IFNAMSIZ-1)
2690 if (ifunit(new_name) != NULL)
2694 * XXX: Locking. Nothing else seems to lock if_flags,
2695 * and there are numerous other races with the
2696 * ifunit() checks not being atomic with namespace
2697 * changes (renames, vmoves, if_attach, etc).
2699 ifp->if_flags |= IFF_RENAMING;
2701 /* Announce the departure of the interface. */
2702 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2703 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2705 if_printf(ifp, "changing name to '%s'\n", new_name);
2708 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2710 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2711 namelen = strlen(new_name);
2712 onamelen = sdl->sdl_nlen;
2714 * Move the address if needed. This is safe because we
2715 * allocate space for a name of length IFNAMSIZ when we
2716 * create this in if_attach().
2718 if (namelen != onamelen) {
2719 bcopy(sdl->sdl_data + onamelen,
2720 sdl->sdl_data + namelen, sdl->sdl_alen);
2722 bcopy(new_name, sdl->sdl_data, namelen);
2723 sdl->sdl_nlen = namelen;
2724 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2725 bzero(sdl->sdl_data, onamelen);
2726 while (namelen != 0)
2727 sdl->sdl_data[--namelen] = 0xff;
2728 IF_ADDR_WUNLOCK(ifp);
2730 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2731 /* Announce the return of the interface. */
2732 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2734 ifp->if_flags &= ~IFF_RENAMING;
2739 error = priv_check(td, PRIV_NET_SETIFVNET);
2742 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2747 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2750 ifp->if_metric = ifr->ifr_metric;
2751 getmicrotime(&ifp->if_lastchange);
2755 error = priv_check(td, PRIV_NET_SETIFPHYS);
2758 if (ifp->if_ioctl == NULL)
2759 return (EOPNOTSUPP);
2760 error = (*ifp->if_ioctl)(ifp, cmd, data);
2762 getmicrotime(&ifp->if_lastchange);
2767 u_long oldmtu = ifp->if_mtu;
2769 error = priv_check(td, PRIV_NET_SETIFMTU);
2772 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2774 if (ifp->if_ioctl == NULL)
2775 return (EOPNOTSUPP);
2776 error = (*ifp->if_ioctl)(ifp, cmd, data);
2778 getmicrotime(&ifp->if_lastchange);
2781 NETDUMP_REINIT(ifp);
2785 * If the link MTU changed, do network layer specific procedure.
2787 if (ifp->if_mtu != oldmtu) {
2798 if (cmd == SIOCADDMULTI)
2799 error = priv_check(td, PRIV_NET_ADDMULTI);
2801 error = priv_check(td, PRIV_NET_DELMULTI);
2805 /* Don't allow group membership on non-multicast interfaces. */
2806 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2807 return (EOPNOTSUPP);
2809 /* Don't let users screw up protocols' entries. */
2810 if (ifr->ifr_addr.sa_family != AF_LINK)
2813 if (cmd == SIOCADDMULTI) {
2814 struct ifmultiaddr *ifma;
2817 * Userland is only permitted to join groups once
2818 * via the if_addmulti() KPI, because it cannot hold
2819 * struct ifmultiaddr * between calls. It may also
2820 * lose a race while we check if the membership
2824 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2825 IF_ADDR_RUNLOCK(ifp);
2829 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2831 error = if_delmulti(ifp, &ifr->ifr_addr);
2834 getmicrotime(&ifp->if_lastchange);
2837 case SIOCSIFPHYADDR:
2838 case SIOCDIFPHYADDR:
2840 case SIOCSIFPHYADDR_IN6:
2843 case SIOCSIFGENERIC:
2844 error = priv_check(td, PRIV_NET_HWIOCTL);
2847 if (ifp->if_ioctl == NULL)
2848 return (EOPNOTSUPP);
2849 error = (*ifp->if_ioctl)(ifp, cmd, data);
2851 getmicrotime(&ifp->if_lastchange);
2855 case SIOCGIFPSRCADDR:
2856 case SIOCGIFPDSTADDR:
2859 case SIOCGIFGENERIC:
2861 case SIOCGIFRSSHASH:
2862 if (ifp->if_ioctl == NULL)
2863 return (EOPNOTSUPP);
2864 error = (*ifp->if_ioctl)(ifp, cmd, data);
2868 error = priv_check(td, PRIV_NET_SETLLADDR);
2871 error = if_setlladdr(ifp,
2872 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2876 error = if_gethwaddr(ifp, ifr);
2879 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2880 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2883 if ((error = if_addgroup(ifp,
2884 ifgr_group_get((struct ifgroupreq *)data))))
2888 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2889 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2893 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2894 error = priv_check(td, PRIV_NET_DELIFGROUP);
2897 if ((error = if_delgroup(ifp,
2898 ifgr_group_get((struct ifgroupreq *)data))))
2909 #ifdef COMPAT_FREEBSD32
2917 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2920 #ifdef COMPAT_FREEBSD32
2922 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2924 struct ifmediareq32 *ifmr32;
2926 ifmr32 = (struct ifmediareq32 *)data;
2927 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2928 sizeof(ifmr->ifm_name));
2929 ifmr->ifm_current = ifmr32->ifm_current;
2930 ifmr->ifm_mask = ifmr32->ifm_mask;
2931 ifmr->ifm_status = ifmr32->ifm_status;
2932 ifmr->ifm_active = ifmr32->ifm_active;
2933 ifmr->ifm_count = ifmr32->ifm_count;
2934 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2938 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2940 struct ifmediareq32 *ifmr32;
2942 ifmr32 = (struct ifmediareq32 *)data;
2943 ifmr32->ifm_current = ifmr->ifm_current;
2944 ifmr32->ifm_mask = ifmr->ifm_mask;
2945 ifmr32->ifm_status = ifmr->ifm_status;
2946 ifmr32->ifm_active = ifmr->ifm_active;
2947 ifmr32->ifm_count = ifmr->ifm_count;
2955 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2957 #ifdef COMPAT_FREEBSD32
2958 caddr_t saved_data = NULL;
2959 struct ifmediareq ifmr;
2961 struct ifmediareq *ifmrp;
2970 CURVNET_SET(so->so_vnet);
2972 /* Make sure the VNET is stable. */
2973 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2974 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2984 error = ifconf(cmd, data);
2988 #ifdef COMPAT_FREEBSD32
2991 struct ifconf32 *ifc32;
2994 ifc32 = (struct ifconf32 *)data;
2995 ifc.ifc_len = ifc32->ifc_len;
2996 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2998 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3001 ifc32->ifc_len = ifc.ifc_len;
3008 #ifdef COMPAT_FREEBSD32
3010 case SIOCGIFMEDIA32:
3011 case SIOCGIFXMEDIA32:
3013 ifmr_init(ifmrp, data);
3014 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3016 data = (caddr_t)ifmrp;
3020 ifr = (struct ifreq *)data;
3024 error = priv_check(td, PRIV_NET_SETIFVNET);
3026 error = if_vmove_reclaim(td, ifr->ifr_name,
3032 error = priv_check(td, PRIV_NET_IFCREATE);
3034 error = if_clone_create(ifr->ifr_name,
3035 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3036 ifr_data_get_ptr(ifr) : NULL);
3039 error = priv_check(td, PRIV_NET_IFDESTROY);
3041 error = if_clone_destroy(ifr->ifr_name);
3044 case SIOCIFGCLONERS:
3045 error = if_clone_list((struct if_clonereq *)data);
3048 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3049 error = if_getgroupmembers((struct ifgroupreq *)data);
3052 #if defined(INET) || defined(INET6)
3055 if (carp_ioctl_p == NULL)
3056 error = EPROTONOSUPPORT;
3058 error = (*carp_ioctl_p)(ifr, cmd, td);
3063 ifp = ifunit_ref(ifr->ifr_name);
3069 error = ifhwioctl(cmd, ifp, data, td);
3070 if (error != ENOIOCTL)
3073 oif_flags = ifp->if_flags;
3074 if (so->so_proto == NULL) {
3080 * Pass the request on to the socket control method, and if the
3081 * latter returns EOPNOTSUPP, directly to the interface.
3083 * Make an exception for the legacy SIOCSIF* requests. Drivers
3084 * trust SIOCSIFADDR et al to come from an already privileged
3085 * layer, and do not perform any credentials checks or input
3088 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3090 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3091 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3092 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3093 error = (*ifp->if_ioctl)(ifp, cmd, data);
3095 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3097 if (ifp->if_flags & IFF_UP)
3105 #ifdef COMPAT_FREEBSD32
3106 if (ifmrp != NULL) {
3107 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3108 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3111 ifmr_update(ifmrp, data);
3119 * The code common to handling reference counted flags,
3120 * e.g., in ifpromisc() and if_allmulti().
3121 * The "pflag" argument can specify a permanent mode flag to check,
3122 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3124 * Only to be used on stack-owned flags, not driver-owned flags.
3127 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3131 int oldflags, oldcount;
3133 /* Sanity checks to catch programming errors */
3134 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3135 ("%s: setting driver-owned flag %d", __func__, flag));
3138 KASSERT(*refcount >= 0,
3139 ("%s: increment negative refcount %d for flag %d",
3140 __func__, *refcount, flag));
3142 KASSERT(*refcount > 0,
3143 ("%s: decrement non-positive refcount %d for flag %d",
3144 __func__, *refcount, flag));
3146 /* In case this mode is permanent, just touch refcount */
3147 if (ifp->if_flags & pflag) {
3148 *refcount += onswitch ? 1 : -1;
3152 /* Save ifnet parameters for if_ioctl() may fail */
3153 oldcount = *refcount;
3154 oldflags = ifp->if_flags;
3157 * See if we aren't the only and touching refcount is enough.
3158 * Actually toggle interface flag if we are the first or last.
3163 ifp->if_flags |= flag;
3167 ifp->if_flags &= ~flag;
3170 /* Call down the driver since we've changed interface flags */
3171 if (ifp->if_ioctl == NULL) {
3175 ifr.ifr_flags = ifp->if_flags & 0xffff;
3176 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3177 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3180 /* Notify userland that interface flags have changed */
3185 /* Recover after driver error */
3186 *refcount = oldcount;
3187 ifp->if_flags = oldflags;
3192 * Set/clear promiscuous mode on interface ifp based on the truth value
3193 * of pswitch. The calls are reference counted so that only the first
3194 * "on" request actually has an effect, as does the final "off" request.
3195 * Results are undefined if the "off" and "on" requests are not matched.
3198 ifpromisc(struct ifnet *ifp, int pswitch)
3201 int oldflags = ifp->if_flags;
3203 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3204 &ifp->if_pcount, pswitch);
3205 /* If promiscuous mode status has changed, log a message */
3206 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3207 log_promisc_mode_change)
3208 if_printf(ifp, "promiscuous mode %s\n",
3209 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3214 * Return interface configuration
3215 * of system. List may be used
3216 * in later ioctl's (above) to get
3217 * other information.
3221 ifconf(u_long cmd, caddr_t data)
3223 struct ifconf *ifc = (struct ifconf *)data;
3228 int error, full = 0, valid_len, max_len;
3230 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3231 max_len = MAXPHYS - 1;
3233 /* Prevent hostile input from being able to crash the system */
3234 if (ifc->ifc_len <= 0)
3238 if (ifc->ifc_len <= max_len) {
3239 max_len = ifc->ifc_len;
3242 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3247 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3251 * Zero the ifr to make sure we don't disclose the contents
3254 memset(&ifr, 0, sizeof(ifr));
3256 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3257 >= sizeof(ifr.ifr_name)) {
3260 return (ENAMETOOLONG);
3265 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3266 struct sockaddr *sa = ifa->ifa_addr;
3268 if (prison_if(curthread->td_ucred, sa) != 0)
3271 if (sa->sa_len <= sizeof(*sa)) {
3272 if (sa->sa_len < sizeof(*sa)) {
3273 memset(&ifr.ifr_ifru.ifru_addr, 0,
3274 sizeof(ifr.ifr_ifru.ifru_addr));
3275 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3278 ifr.ifr_ifru.ifru_addr = *sa;
3279 sbuf_bcat(sb, &ifr, sizeof(ifr));
3280 max_len += sizeof(ifr);
3283 offsetof(struct ifreq, ifr_addr));
3284 max_len += offsetof(struct ifreq, ifr_addr);
3285 sbuf_bcat(sb, sa, sa->sa_len);
3286 max_len += sa->sa_len;
3289 if (sbuf_error(sb) == 0)
3290 valid_len = sbuf_len(sb);
3292 IF_ADDR_RUNLOCK(ifp);
3294 sbuf_bcat(sb, &ifr, sizeof(ifr));
3295 max_len += sizeof(ifr);
3297 if (sbuf_error(sb) == 0)
3298 valid_len = sbuf_len(sb);
3304 * If we didn't allocate enough space (uncommon), try again. If
3305 * we have already allocated as much space as we are allowed,
3306 * return what we've got.
3308 if (valid_len != max_len && !full) {
3313 ifc->ifc_len = valid_len;
3315 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3321 * Just like ifpromisc(), but for all-multicast-reception mode.
3324 if_allmulti(struct ifnet *ifp, int onswitch)
3327 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3330 struct ifmultiaddr *
3331 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3333 struct ifmultiaddr *ifma;
3335 IF_ADDR_LOCK_ASSERT(ifp);
3337 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3338 if (sa->sa_family == AF_LINK) {
3339 if (sa_dl_equal(ifma->ifma_addr, sa))
3342 if (sa_equal(ifma->ifma_addr, sa))
3351 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3352 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3353 * the ifnet multicast address list here, so the caller must do that and
3354 * other setup work (such as notifying the device driver). The reference
3355 * count is initialized to 1.
3357 static struct ifmultiaddr *
3358 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3361 struct ifmultiaddr *ifma;
3362 struct sockaddr *dupsa;
3364 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3369 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3370 if (dupsa == NULL) {
3371 free(ifma, M_IFMADDR);
3374 bcopy(sa, dupsa, sa->sa_len);
3375 ifma->ifma_addr = dupsa;
3377 ifma->ifma_ifp = ifp;
3378 ifma->ifma_refcount = 1;
3379 ifma->ifma_protospec = NULL;
3382 ifma->ifma_lladdr = NULL;
3386 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3387 if (dupsa == NULL) {
3388 free(ifma->ifma_addr, M_IFMADDR);
3389 free(ifma, M_IFMADDR);
3392 bcopy(llsa, dupsa, llsa->sa_len);
3393 ifma->ifma_lladdr = dupsa;
3399 * if_freemulti: free ifmultiaddr structure and possibly attached related
3400 * addresses. The caller is responsible for implementing reference
3401 * counting, notifying the driver, handling routing messages, and releasing
3402 * any dependent link layer state.
3404 #ifdef MCAST_VERBOSE
3405 extern void kdb_backtrace(void);
3408 if_freemulti_internal(struct ifmultiaddr *ifma)
3411 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3412 ifma->ifma_refcount));
3414 if (ifma->ifma_lladdr != NULL)
3415 free(ifma->ifma_lladdr, M_IFMADDR);
3416 #ifdef MCAST_VERBOSE
3418 printf("%s freeing ifma: %p\n", __func__, ifma);
3420 free(ifma->ifma_addr, M_IFMADDR);
3421 free(ifma, M_IFMADDR);
3425 if_destroymulti(epoch_context_t ctx)
3427 struct ifmultiaddr *ifma;
3429 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3430 if_freemulti_internal(ifma);
3434 if_freemulti(struct ifmultiaddr *ifma)
3436 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3437 ifma->ifma_refcount));
3439 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3444 * Register an additional multicast address with a network interface.
3446 * - If the address is already present, bump the reference count on the
3447 * address and return.
3448 * - If the address is not link-layer, look up a link layer address.
3449 * - Allocate address structures for one or both addresses, and attach to the
3450 * multicast address list on the interface. If automatically adding a link
3451 * layer address, the protocol address will own a reference to the link
3452 * layer address, to be freed when it is freed.
3453 * - Notify the network device driver of an addition to the multicast address
3456 * 'sa' points to caller-owned memory with the desired multicast address.
3458 * 'retifma' will be used to return a pointer to the resulting multicast
3459 * address reference, if desired.
3462 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3463 struct ifmultiaddr **retifma)
3465 struct ifmultiaddr *ifma, *ll_ifma;
3466 struct sockaddr *llsa;
3467 struct sockaddr_dl sdl;
3471 IN_MULTI_LIST_UNLOCK_ASSERT();
3474 IN6_MULTI_LIST_UNLOCK_ASSERT();
3477 * If the address is already present, return a new reference to it;
3478 * otherwise, allocate storage and set up a new address.
3481 ifma = if_findmulti(ifp, sa);
3483 ifma->ifma_refcount++;
3484 if (retifma != NULL)
3486 IF_ADDR_WUNLOCK(ifp);
3491 * The address isn't already present; resolve the protocol address
3492 * into a link layer address, and then look that up, bump its
3493 * refcount or allocate an ifma for that also.
3494 * Most link layer resolving functions returns address data which
3495 * fits inside default sockaddr_dl structure. However callback
3496 * can allocate another sockaddr structure, in that case we need to
3501 if (ifp->if_resolvemulti != NULL) {
3502 /* Provide called function with buffer size information */
3503 sdl.sdl_len = sizeof(sdl);
3504 llsa = (struct sockaddr *)&sdl;
3505 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3511 * Allocate the new address. Don't hook it up yet, as we may also
3512 * need to allocate a link layer multicast address.
3514 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3521 * If a link layer address is found, we'll need to see if it's
3522 * already present in the address list, or allocate is as well.
3523 * When this block finishes, the link layer address will be on the
3527 ll_ifma = if_findmulti(ifp, llsa);
3528 if (ll_ifma == NULL) {
3529 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3530 if (ll_ifma == NULL) {
3531 --ifma->ifma_refcount;
3536 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3539 ll_ifma->ifma_refcount++;
3540 ifma->ifma_llifma = ll_ifma;
3544 * We now have a new multicast address, ifma, and possibly a new or
3545 * referenced link layer address. Add the primary address to the
3546 * ifnet address list.
3548 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3550 if (retifma != NULL)
3554 * Must generate the message while holding the lock so that 'ifma'
3555 * pointer is still valid.
3557 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3558 IF_ADDR_WUNLOCK(ifp);
3561 * We are certain we have added something, so call down to the
3562 * interface to let them know about it.
3564 if (ifp->if_ioctl != NULL) {
3565 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3568 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3569 link_free_sdl(llsa);
3574 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3575 link_free_sdl(llsa);
3578 IF_ADDR_WUNLOCK(ifp);
3583 * Delete a multicast group membership by network-layer group address.
3585 * Returns ENOENT if the entry could not be found. If ifp no longer
3586 * exists, results are undefined. This entry point should only be used
3587 * from subsystems which do appropriate locking to hold ifp for the
3588 * duration of the call.
3589 * Network-layer protocol domains must use if_delmulti_ifma().
3592 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3594 struct ifmultiaddr *ifma;
3599 IFNET_RLOCK_NOSLEEP();
3600 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3605 IFNET_RUNLOCK_NOSLEEP();
3607 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3614 ifma = if_findmulti(ifp, sa);
3616 lastref = if_delmulti_locked(ifp, ifma, 0);
3617 IF_ADDR_WUNLOCK(ifp);
3622 if (lastref && ifp->if_ioctl != NULL) {
3623 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3630 * Delete all multicast group membership for an interface.
3631 * Should be used to quickly flush all multicast filters.
3634 if_delallmulti(struct ifnet *ifp)
3636 struct ifmultiaddr *ifma;
3637 struct ifmultiaddr *next;
3640 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3641 if_delmulti_locked(ifp, ifma, 0);
3642 IF_ADDR_WUNLOCK(ifp);
3646 if_delmulti_ifma(struct ifmultiaddr *ifma)
3648 if_delmulti_ifma_flags(ifma, 0);
3652 * Delete a multicast group membership by group membership pointer.
3653 * Network-layer protocol domains must use this routine.
3655 * It is safe to call this routine if the ifp disappeared.
3658 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3662 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3664 IN_MULTI_LIST_UNLOCK_ASSERT();
3666 ifp = ifma->ifma_ifp;
3669 printf("%s: ifma_ifp seems to be detached\n", __func__);
3673 IFNET_RLOCK_NOSLEEP();
3674 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3678 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3681 IFNET_RUNLOCK_NOSLEEP();
3685 * If and only if the ifnet instance exists: Acquire the address lock.
3690 lastref = if_delmulti_locked(ifp, ifma, flags);
3694 * If and only if the ifnet instance exists:
3695 * Release the address lock.
3696 * If the group was left: update the hardware hash filter.
3698 IF_ADDR_WUNLOCK(ifp);
3699 if (lastref && ifp->if_ioctl != NULL) {
3700 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3706 * Perform deletion of network-layer and/or link-layer multicast address.
3708 * Return 0 if the reference count was decremented.
3709 * Return 1 if the final reference was released, indicating that the
3710 * hardware hash filter should be reprogrammed.
3713 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3715 struct ifmultiaddr *ll_ifma;
3717 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3718 KASSERT(ifma->ifma_ifp == ifp,
3719 ("%s: inconsistent ifp %p", __func__, ifp));
3720 IF_ADDR_WLOCK_ASSERT(ifp);
3723 ifp = ifma->ifma_ifp;
3724 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3727 * If the ifnet is detaching, null out references to ifnet,
3728 * so that upper protocol layers will notice, and not attempt
3729 * to obtain locks for an ifnet which no longer exists. The
3730 * routing socket announcement must happen before the ifnet
3731 * instance is detached from the system.
3735 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3738 * ifp may already be nulled out if we are being reentered
3739 * to delete the ll_ifma.
3742 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3743 ifma->ifma_ifp = NULL;
3747 if (--ifma->ifma_refcount > 0)
3750 if (ifp != NULL && detaching == 0)
3751 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3754 * If this ifma is a network-layer ifma, a link-layer ifma may
3755 * have been associated with it. Release it first if so.
3757 ll_ifma = ifma->ifma_llifma;
3758 if (ll_ifma != NULL) {
3759 KASSERT(ifma->ifma_lladdr != NULL,
3760 ("%s: llifma w/o lladdr", __func__));
3762 ll_ifma->ifma_ifp = NULL; /* XXX */
3763 if (--ll_ifma->ifma_refcount == 0) {
3765 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3768 if_freemulti(ll_ifma);
3773 struct ifmultiaddr *ifmatmp;
3775 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3776 MPASS(ifma != ifmatmp);
3781 * The last reference to this instance of struct ifmultiaddr
3782 * was released; the hardware should be notified of this change.
3788 * Set the link layer address on an interface.
3790 * At this time we only support certain types of interfaces,
3791 * and we don't allow the length of the address to change.
3793 * Set noinline to be dtrace-friendly
3796 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3798 struct sockaddr_dl *sdl;
3811 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3816 if (len != sdl->sdl_alen) { /* don't allow length to change */
3820 switch (ifp->if_type) {
3825 case IFT_IEEE8023ADLAG:
3826 bcopy(lladdr, LLADDR(sdl), len);
3834 * If the interface is already up, we need
3835 * to re-init it in order to reprogram its
3838 if ((ifp->if_flags & IFF_UP) != 0) {
3839 if (ifp->if_ioctl) {
3840 ifp->if_flags &= ~IFF_UP;
3841 ifr.ifr_flags = ifp->if_flags & 0xffff;
3842 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3843 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3844 ifp->if_flags |= IFF_UP;
3845 ifr.ifr_flags = ifp->if_flags & 0xffff;
3846 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3847 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3850 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3857 * Compat function for handling basic encapsulation requests.
3858 * Not converted stacks (FDDI, IB, ..) supports traditional
3859 * output model: ARP (and other similar L2 protocols) are handled
3860 * inside output routine, arpresolve/nd6_resolve() returns MAC
3861 * address instead of full prepend.
3863 * This function creates calculated header==MAC for IPv4/IPv6 and
3864 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3868 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3871 if (req->rtype != IFENCAP_LL)
3872 return (EOPNOTSUPP);
3874 if (req->bufsize < req->lladdr_len)
3877 switch (req->family) {
3882 return (EAFNOSUPPORT);
3885 /* Copy lladdr to storage as is */
3886 memmove(req->buf, req->lladdr, req->lladdr_len);
3887 req->bufsize = req->lladdr_len;
3888 req->lladdr_off = 0;
3894 * Get the link layer address that was read from the hardware at attach.
3896 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3897 * their component interfaces as IFT_IEEE8023ADLAG.
3900 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3903 if (ifp->if_hw_addr == NULL)
3906 switch (ifp->if_type) {
3908 case IFT_IEEE8023ADLAG:
3909 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3917 * The name argument must be a pointer to storage which will last as
3918 * long as the interface does. For physical devices, the result of
3919 * device_get_name(dev) is a good choice and for pseudo-devices a
3920 * static string works well.
3923 if_initname(struct ifnet *ifp, const char *name, int unit)
3925 ifp->if_dname = name;
3926 ifp->if_dunit = unit;
3927 if (unit != IF_DUNIT_NONE)
3928 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3930 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3934 if_printf(struct ifnet *ifp, const char *fmt, ...)
3939 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3941 vlog(LOG_INFO, if_fmt, ap);
3947 if_start(struct ifnet *ifp)
3950 (*(ifp)->if_start)(ifp);
3954 * Backwards compatibility interface for drivers
3955 * that have not implemented it
3958 if_transmit(struct ifnet *ifp, struct mbuf *m)
3962 IFQ_HANDOFF(ifp, m, error);
3967 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3974 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3979 if (_IF_QFULL(ifq)) {
3981 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3986 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3987 if (m->m_flags & (M_BCAST|M_MCAST))
3988 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3989 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3991 _IF_ENQUEUE(ifq, m);
3993 if (ifp != NULL && !active)
3994 (*(ifp)->if_start)(ifp);
3999 if_register_com_alloc(u_char type,
4000 if_com_alloc_t *a, if_com_free_t *f)
4003 KASSERT(if_com_alloc[type] == NULL,
4004 ("if_register_com_alloc: %d already registered", type));
4005 KASSERT(if_com_free[type] == NULL,
4006 ("if_register_com_alloc: %d free already registered", type));
4008 if_com_alloc[type] = a;
4009 if_com_free[type] = f;
4013 if_deregister_com_alloc(u_char type)
4016 KASSERT(if_com_alloc[type] != NULL,
4017 ("if_deregister_com_alloc: %d not registered", type));
4018 KASSERT(if_com_free[type] != NULL,
4019 ("if_deregister_com_alloc: %d free not registered", type));
4020 if_com_alloc[type] = NULL;
4021 if_com_free[type] = NULL;
4024 /* API for driver access to network stack owned ifnet.*/
4026 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4030 oldbrate = ifp->if_baudrate;
4031 ifp->if_baudrate = baudrate;
4036 if_getbaudrate(if_t ifp)
4039 return (((struct ifnet *)ifp)->if_baudrate);
4043 if_setcapabilities(if_t ifp, int capabilities)
4045 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4050 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4052 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4053 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4059 if_getcapabilities(if_t ifp)
4061 return ((struct ifnet *)ifp)->if_capabilities;
4065 if_setcapenable(if_t ifp, int capabilities)
4067 ((struct ifnet *)ifp)->if_capenable = capabilities;
4072 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4075 ((struct ifnet *)ifp)->if_capenable |= setcap;
4077 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4083 if_getdname(if_t ifp)
4085 return ((struct ifnet *)ifp)->if_dname;
4089 if_togglecapenable(if_t ifp, int togglecap)
4091 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4096 if_getcapenable(if_t ifp)
4098 return ((struct ifnet *)ifp)->if_capenable;
4102 * This is largely undesirable because it ties ifnet to a device, but does
4103 * provide flexiblity for an embedded product vendor. Should be used with
4104 * the understanding that it violates the interface boundaries, and should be
4105 * a last resort only.
4108 if_setdev(if_t ifp, void *dev)
4114 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4116 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4117 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4123 if_getdrvflags(if_t ifp)
4125 return ((struct ifnet *)ifp)->if_drv_flags;
4129 if_setdrvflags(if_t ifp, int flags)
4131 ((struct ifnet *)ifp)->if_drv_flags = flags;
4137 if_setflags(if_t ifp, int flags)
4139 ((struct ifnet *)ifp)->if_flags = flags;
4144 if_setflagbits(if_t ifp, int set, int clear)
4146 ((struct ifnet *)ifp)->if_flags |= set;
4147 ((struct ifnet *)ifp)->if_flags &= ~clear;
4153 if_getflags(if_t ifp)
4155 return ((struct ifnet *)ifp)->if_flags;
4159 if_clearhwassist(if_t ifp)
4161 ((struct ifnet *)ifp)->if_hwassist = 0;
4166 if_sethwassistbits(if_t ifp, int toset, int toclear)
4168 ((struct ifnet *)ifp)->if_hwassist |= toset;
4169 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4175 if_sethwassist(if_t ifp, int hwassist_bit)
4177 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4182 if_gethwassist(if_t ifp)
4184 return ((struct ifnet *)ifp)->if_hwassist;
4188 if_setmtu(if_t ifp, int mtu)
4190 ((struct ifnet *)ifp)->if_mtu = mtu;
4197 return ((struct ifnet *)ifp)->if_mtu;
4201 if_getmtu_family(if_t ifp, int family)
4205 for (dp = domains; dp; dp = dp->dom_next) {
4206 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4207 return (dp->dom_ifmtu((struct ifnet *)ifp));
4210 return (((struct ifnet *)ifp)->if_mtu);
4214 if_setsoftc(if_t ifp, void *softc)
4216 ((struct ifnet *)ifp)->if_softc = softc;
4221 if_getsoftc(if_t ifp)
4223 return ((struct ifnet *)ifp)->if_softc;
4227 if_setrcvif(struct mbuf *m, if_t ifp)
4229 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4233 if_setvtag(struct mbuf *m, uint16_t tag)
4235 m->m_pkthdr.ether_vtag = tag;
4239 if_getvtag(struct mbuf *m)
4242 return (m->m_pkthdr.ether_vtag);
4246 if_sendq_empty(if_t ifp)
4248 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4252 if_getifaddr(if_t ifp)
4254 return ((struct ifnet *)ifp)->if_addr;
4258 if_getamcount(if_t ifp)
4260 return ((struct ifnet *)ifp)->if_amcount;
4265 if_setsendqready(if_t ifp)
4267 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4272 if_setsendqlen(if_t ifp, int tx_desc_count)
4274 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4275 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4281 if_vlantrunkinuse(if_t ifp)
4283 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4287 if_input(if_t ifp, struct mbuf* sendmp)
4289 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4295 #ifndef ETH_ADDR_LEN
4296 #define ETH_ADDR_LEN 6
4300 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4302 struct ifmultiaddr *ifma;
4303 uint8_t *lmta = (uint8_t *)mta;
4306 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4307 if (ifma->ifma_addr->sa_family != AF_LINK)
4313 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4314 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4323 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4327 if_maddr_rlock(ifp);
4328 error = if_setupmultiaddr(ifp, mta, cnt, max);
4329 if_maddr_runlock(ifp);
4334 if_multiaddr_count(if_t ifp, int max)
4336 struct ifmultiaddr *ifma;
4340 if_maddr_rlock(ifp);
4341 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4342 if (ifma->ifma_addr->sa_family != AF_LINK)
4348 if_maddr_runlock(ifp);
4353 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4355 struct ifmultiaddr *ifma;
4358 if_maddr_rlock(ifp);
4359 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4360 cnt += filter(arg, ifma, cnt);
4361 if_maddr_runlock(ifp);
4366 if_dequeue(if_t ifp)
4369 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4375 if_sendq_prepend(if_t ifp, struct mbuf *m)
4377 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4382 if_setifheaderlen(if_t ifp, int len)
4384 ((struct ifnet *)ifp)->if_hdrlen = len;
4389 if_getlladdr(if_t ifp)
4391 return (IF_LLADDR((struct ifnet *)ifp));
4395 if_gethandle(u_char type)
4397 return (if_alloc(type));
4401 if_bpfmtap(if_t ifh, struct mbuf *m)
4403 struct ifnet *ifp = (struct ifnet *)ifh;
4409 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4411 struct ifnet *ifp = (struct ifnet *)ifh;
4413 ETHER_BPF_MTAP(ifp, m);
4417 if_vlancap(if_t ifh)
4419 struct ifnet *ifp = (struct ifnet *)ifh;
4420 VLAN_CAPABILITIES(ifp);
4424 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4427 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4432 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4435 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4440 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4443 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4448 if_gethwtsomax(if_t ifp)
4451 return (((struct ifnet *)ifp)->if_hw_tsomax);
4455 if_gethwtsomaxsegcount(if_t ifp)
4458 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4462 if_gethwtsomaxsegsize(if_t ifp)
4465 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4469 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4471 ((struct ifnet *)ifp)->if_init = init_fn;
4475 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4477 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4481 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4483 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4487 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4489 ((struct ifnet *)ifp)->if_transmit = start_fn;
4492 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4494 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4499 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4502 ifp->if_get_counter = fn;
4505 /* Revisit these - These are inline functions originally. */
4507 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4509 return drbr_inuse(ifh, br);
4513 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4515 return drbr_dequeue(ifh, br);
4519 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4521 return drbr_needs_enqueue(ifh, br);
4525 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4527 return drbr_enqueue(ifh, br, m);