2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
44 #include <sys/domainset.h>
47 #include <sys/epoch.h>
49 #include <sys/systm.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/protosw.h>
55 #include <sys/kernel.h>
57 #include <sys/refcount.h>
58 #include <sys/module.h>
59 #include <sys/rwlock.h>
60 #include <sys/sockio.h>
61 #include <sys/syslog.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/taskqueue.h>
65 #include <sys/domain.h>
69 #include <machine/stdarg.h>
73 #include <net/ethernet.h>
75 #include <net/if_arp.h>
76 #include <net/if_clone.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/if_var.h>
80 #include <net/if_media.h>
81 #include <net/if_vlan_var.h>
82 #include <net/radix.h>
83 #include <net/route.h>
84 #include <net/route/route_ctl.h>
87 #if defined(INET) || defined(INET6)
88 #include <net/ethernet.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_carp.h>
94 #include <net/debugnet.h>
95 #include <netinet/if_ether.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet6/in6_ifattach.h>
101 #endif /* INET || INET6 */
103 #include <security/mac/mac_framework.h>
106 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
107 * and ifr_ifru when it is used in SIOCGIFCONF.
109 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
110 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
112 __read_mostly epoch_t net_epoch_preempt;
113 #ifdef COMPAT_FREEBSD32
114 #include <sys/mount.h>
115 #include <compat/freebsd32/freebsd32.h>
117 struct ifreq_buffer32 {
118 uint32_t length; /* (size_t) */
119 uint32_t buffer; /* (void *) */
123 * Interface request structure used for socket
124 * ioctl's. All interface ioctl's must have parameter
125 * definitions which begin with ifr_name. The
126 * remainder may be interface specific.
129 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
131 struct sockaddr ifru_addr;
132 struct sockaddr ifru_dstaddr;
133 struct sockaddr ifru_broadaddr;
134 struct ifreq_buffer32 ifru_buffer;
145 u_char ifru_vlan_pcp;
148 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
149 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
150 __offsetof(struct ifreq32, ifr_ifru));
159 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
162 char ifd_name[IFNAMSIZ];
167 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
168 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
170 struct ifgroupreq32 {
171 char ifgr_name[IFNAMSIZ];
174 char ifgru_group[IFNAMSIZ];
175 uint32_t ifgru_groups;
178 #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
179 #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
180 #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
181 #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
183 struct ifmediareq32 {
184 char ifm_name[IFNAMSIZ];
190 uint32_t ifm_ulist; /* (int *) */
192 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
193 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
194 #endif /* COMPAT_FREEBSD32 */
198 #ifdef COMPAT_FREEBSD32
199 struct ifreq32 ifr32;
203 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
205 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
206 "Generic link-management");
208 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
209 &ifqmaxlen, 0, "max send queue size");
211 /* Log link state change events */
212 static int log_link_state_change = 1;
214 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
215 &log_link_state_change, 0,
216 "log interface link state change events");
218 /* Log promiscuous mode change events */
219 static int log_promisc_mode_change = 1;
221 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
222 &log_promisc_mode_change, 1,
223 "log promiscuous mode change events");
225 /* Interface description */
226 static unsigned int ifdescr_maxlen = 1024;
227 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
229 "administrative maximum length for interface description");
231 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
233 /* global sx for non-critical path ifdescr */
234 static struct sx ifdescr_sx;
235 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
237 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
238 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
239 /* These are external hooks for CARP. */
240 void (*carp_linkstate_p)(struct ifnet *ifp);
241 void (*carp_demote_adj_p)(int, char *);
242 int (*carp_master_p)(struct ifaddr *);
243 #if defined(INET) || defined(INET6)
244 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
245 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
246 const struct sockaddr *sa);
247 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
248 int (*carp_attach_p)(struct ifaddr *, int);
249 void (*carp_detach_p)(struct ifaddr *, bool);
252 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
255 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
256 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
257 const struct in6_addr *taddr);
260 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
263 * XXX: Style; these should be sorted alphabetically, and unprototyped
264 * static functions should be prototyped. Currently they are sorted by
267 static void if_attachdomain(void *);
268 static void if_attachdomain1(struct ifnet *);
269 static int ifconf(u_long, caddr_t);
270 static void *if_grow(void);
271 static void if_input_default(struct ifnet *, struct mbuf *);
272 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
273 static void if_route(struct ifnet *, int flag, int fam);
274 static int if_setflag(struct ifnet *, int, int, int *, int);
275 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
276 static void if_unroute(struct ifnet *, int flag, int fam);
277 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
278 static void do_link_state_change(void *, int);
279 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
280 static int if_getgroupmembers(struct ifgroupreq *);
281 static void if_delgroups(struct ifnet *);
282 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
283 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
284 static void if_siocaddmulti(void *, int);
285 static void if_link_ifnet(struct ifnet *);
286 static bool if_unlink_ifnet(struct ifnet *, bool);
288 static int if_vmove(struct ifnet *, struct vnet *);
293 * XXX: declare here to avoid to include many inet6 related files..
294 * should be more generalized?
296 extern void nd6_setmtu(struct ifnet *);
299 /* ipsec helper hooks */
300 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
301 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
303 VNET_DEFINE(int, if_index);
304 int ifqmaxlen = IFQ_MAXLEN;
305 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
306 VNET_DEFINE(struct ifgrouphead, ifg_head);
308 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
310 /* Table of ifnet by index. */
311 VNET_DEFINE(struct ifnet **, ifindex_table);
313 #define V_if_indexlim VNET(if_indexlim)
314 #define V_ifindex_table VNET(ifindex_table)
317 * The global network interface list (V_ifnet) and related state (such as
318 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
319 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
321 struct sx ifnet_sxlock;
322 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
324 struct sx ifnet_detach_sxlock;
325 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
329 * The allocation of network interfaces is a rather non-atomic affair; we
330 * need to select an index before we are ready to expose the interface for
331 * use, so will use this pointer value to indicate reservation.
333 #define IFNET_HOLD (void *)(uintptr_t)(-1)
336 #define VNET_IS_SHUTTING_DOWN(_vnet) \
337 ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
340 static if_com_alloc_t *if_com_alloc[256];
341 static if_com_free_t *if_com_free[256];
343 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
344 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
345 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
348 ifnet_byindex(u_short idx)
352 if (__predict_false(idx > V_if_index))
355 ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx);
356 return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp);
360 ifnet_byindex_ref(u_short idx)
366 ifp = ifnet_byindex(idx);
367 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
369 if (!if_try_ref(ifp))
375 * Allocate an ifindex array entry; return 0 on success or an error on
379 ifindex_alloc(void **old)
383 IFNET_WLOCK_ASSERT();
385 * Try to find an empty slot below V_if_index. If we fail, take the
388 for (idx = 1; idx <= V_if_index; idx++) {
389 if (V_ifindex_table[idx] == NULL)
393 /* Catch if_index overflow. */
394 if (idx >= V_if_indexlim) {
398 if (idx > V_if_index)
404 ifindex_free_locked(u_short idx)
407 IFNET_WLOCK_ASSERT();
409 V_ifindex_table[idx] = NULL;
410 while (V_if_index > 0 &&
411 V_ifindex_table[V_if_index] == NULL)
416 ifindex_free(u_short idx)
420 ifindex_free_locked(idx);
425 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
428 V_ifindex_table[idx] = ifp;
432 ifaddr_byindex(u_short idx)
435 struct ifaddr *ifa = NULL;
439 ifp = ifnet_byindex(idx);
440 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
446 * Network interface utility routines.
448 * Routines with ifa_ifwith* names take sockaddr *'s as
453 vnet_if_init(const void *unused __unused)
457 CK_STAILQ_INIT(&V_ifnet);
458 CK_STAILQ_INIT(&V_ifg_head);
460 old = if_grow(); /* create initial table */
462 epoch_wait_preempt(net_epoch_preempt);
464 vnet_if_clone_init();
466 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
471 vnet_if_uninit(const void *unused __unused)
474 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
475 "not empty", __func__, __LINE__, &V_ifnet));
476 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
477 "not empty", __func__, __LINE__, &V_ifg_head));
479 free((caddr_t)V_ifindex_table, M_IFNET);
481 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
482 vnet_if_uninit, NULL);
486 if_link_ifnet(struct ifnet *ifp)
490 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
492 curvnet->vnet_ifcnt++;
498 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
504 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
506 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
508 ifp->if_flags |= IFF_DYING;
513 curvnet->vnet_ifcnt--;
522 vnet_if_return(const void *unused __unused)
524 struct ifnet *ifp, *nifp;
525 struct ifnet **pending;
531 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
532 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
533 * if_detach_internal(), which waits for NET_EPOCH callbacks to
534 * complete. We can't do that from within NET_EPOCH.
536 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
537 * read/write lock. We cannot hold the lock as we call if_vmove()
538 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
543 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
544 M_IFNET, M_WAITOK | M_ZERO);
546 /* Return all inherited interfaces to their parent vnets. */
547 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
548 if (ifp->if_home_vnet != ifp->if_vnet) {
549 found = if_unlink_ifnet(ifp, true);
557 for (int j = 0; j < i; j++) {
558 if_vmove(pending[j], pending[j]->if_home_vnet);
561 free(pending, M_IFNET);
563 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
564 vnet_if_return, NULL);
576 IFNET_WLOCK_ASSERT();
577 oldlim = V_if_indexlim;
579 n = (oldlim << 1) * sizeof(*e);
580 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
582 if (V_if_indexlim != oldlim) {
586 if (V_ifindex_table != NULL) {
587 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
588 old = V_ifindex_table;
596 * Allocate a struct ifnet and an index for an interface. A layer 2
597 * common structure will also be allocated if an allocation routine is
598 * registered for the passed type.
601 if_alloc_domain(u_char type, int numa_domain)
607 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
608 if (numa_domain == IF_NODOM)
609 ifp = malloc(sizeof(struct ifnet), M_IFNET,
612 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
613 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
616 idx = ifindex_alloc(&old);
617 if (__predict_false(idx == USHRT_MAX)) {
619 epoch_wait_preempt(net_epoch_preempt);
623 ifnet_setbyindex(idx, IFNET_HOLD);
627 ifp->if_alloctype = type;
628 ifp->if_numa_domain = numa_domain;
630 ifp->if_vnet = curvnet;
632 if (if_com_alloc[type] != NULL) {
633 ifp->if_l2com = if_com_alloc[type](type, ifp);
634 if (ifp->if_l2com == NULL) {
641 IF_ADDR_LOCK_INIT(ifp);
642 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
643 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
644 ifp->if_afdata_initialized = 0;
645 IF_AFDATA_LOCK_INIT(ifp);
646 CK_STAILQ_INIT(&ifp->if_addrhead);
647 CK_STAILQ_INIT(&ifp->if_multiaddrs);
648 CK_STAILQ_INIT(&ifp->if_groups);
652 ifq_init(&ifp->if_snd, ifp);
654 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
655 for (int i = 0; i < IFCOUNTERS; i++)
656 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
657 ifp->if_get_counter = if_get_counter_default;
658 ifp->if_pcp = IFNET_PCP_NONE;
659 ifnet_setbyindex(ifp->if_index, ifp);
664 if_alloc_dev(u_char type, device_t dev)
668 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
669 return (if_alloc_domain(type, IF_NODOM));
670 return (if_alloc_domain(type, numa_domain));
674 if_alloc(u_char type)
677 return (if_alloc_domain(type, IF_NODOM));
680 * Do the actual work of freeing a struct ifnet, and layer 2 common
681 * structure. This call is made when the last reference to an
682 * interface is released.
685 if_free_internal(struct ifnet *ifp)
688 KASSERT((ifp->if_flags & IFF_DYING),
689 ("if_free_internal: interface not dying"));
691 if (if_com_free[ifp->if_alloctype] != NULL)
692 if_com_free[ifp->if_alloctype](ifp->if_l2com,
696 mac_ifnet_destroy(ifp);
698 IF_AFDATA_DESTROY(ifp);
699 IF_ADDR_LOCK_DESTROY(ifp);
700 ifq_delete(&ifp->if_snd);
702 for (int i = 0; i < IFCOUNTERS; i++)
703 counter_u64_free(ifp->if_counters[i]);
705 free(ifp->if_description, M_IFDESCR);
706 free(ifp->if_hw_addr, M_IFADDR);
711 if_destroy(epoch_context_t ctx)
715 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
716 if_free_internal(ifp);
720 * Deregister an interface and free the associated storage.
723 if_free(struct ifnet *ifp)
726 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
728 CURVNET_SET_QUIET(ifp->if_vnet);
730 KASSERT(ifp == ifnet_byindex(ifp->if_index),
731 ("%s: freeing unallocated ifnet", ifp->if_xname));
733 ifindex_free_locked(ifp->if_index);
736 if (refcount_release(&ifp->if_refcount))
737 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
742 * Interfaces to keep an ifnet type-stable despite the possibility of the
743 * driver calling if_free(). If there are additional references, we defer
744 * freeing the underlying data structure.
747 if_ref(struct ifnet *ifp)
751 /* We don't assert the ifnet list lock here, but arguably should. */
752 old = refcount_acquire(&ifp->if_refcount);
753 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
757 if_try_ref(struct ifnet *ifp)
760 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
764 if_rele(struct ifnet *ifp)
767 if (!refcount_release(&ifp->if_refcount))
769 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
773 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
776 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
778 if (ifq->ifq_maxlen == 0)
779 ifq->ifq_maxlen = ifqmaxlen;
782 ifq->altq_disc = NULL;
783 ifq->altq_flags &= ALTQF_CANTCHANGE;
784 ifq->altq_tbr = NULL;
789 ifq_delete(struct ifaltq *ifq)
791 mtx_destroy(&ifq->ifq_mtx);
795 * Perform generic interface initialization tasks and attach the interface
796 * to the list of "active" interfaces. If vmove flag is set on entry
797 * to if_attach_internal(), perform only a limited subset of initialization
798 * tasks, given that we are moving from one vnet to another an ifnet which
799 * has already been fully initialized.
801 * Note that if_detach_internal() removes group membership unconditionally
802 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
803 * Thus, when if_vmove() is applied to a cloned interface, group membership
804 * is lost while a cloned one always joins a group whose name is
805 * ifc->ifc_name. To recover this after if_detach_internal() and
806 * if_attach_internal(), the cloner should be specified to
807 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
808 * attempts to join a group whose name is ifc->ifc_name.
811 * - The decision to return void and thus require this function to
812 * succeed is questionable.
813 * - We should probably do more sanity checking. For instance we don't
814 * do anything to insure if_xname is unique or non-empty.
817 if_attach(struct ifnet *ifp)
820 if_attach_internal(ifp, 0, NULL);
824 * Compute the least common TSO limit.
827 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
830 * 1) If there is no limit currently, take the limit from
831 * the network adapter.
833 * 2) If the network adapter has a limit below the current
836 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
837 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
838 pmax->tsomaxbytes = ifp->if_hw_tsomax;
840 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
841 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
842 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
844 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
845 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
846 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
851 * Update TSO limit of a network adapter.
853 * Returns zero if no change. Else non-zero.
856 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
859 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
860 ifp->if_hw_tsomax = pmax->tsomaxbytes;
863 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
864 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
867 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
868 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
875 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
877 unsigned socksize, ifasize;
878 int namelen, masklen;
879 struct sockaddr_dl *sdl;
882 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
883 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
887 ifp->if_vnet = curvnet;
888 if (ifp->if_home_vnet == NULL)
889 ifp->if_home_vnet = curvnet;
892 if_addgroup(ifp, IFG_ALL);
894 /* Restore group membership for cloned interfaces. */
895 if (vmove && ifc != NULL)
896 if_clone_addgroup(ifp, ifc);
898 getmicrotime(&ifp->if_lastchange);
899 ifp->if_epoch = time_uptime;
901 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
902 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
903 ("transmit and qflush must both either be set or both be NULL"));
904 if (ifp->if_transmit == NULL) {
905 ifp->if_transmit = if_transmit;
906 ifp->if_qflush = if_qflush;
908 if (ifp->if_input == NULL)
909 ifp->if_input = if_input_default;
911 if (ifp->if_requestencap == NULL)
912 ifp->if_requestencap = if_requestencap_default;
916 mac_ifnet_create(ifp);
920 * Create a Link Level name for this device.
922 namelen = strlen(ifp->if_xname);
924 * Always save enough space for any possiable name so we
925 * can do a rename in place later.
927 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
928 socksize = masklen + ifp->if_addrlen;
929 if (socksize < sizeof(*sdl))
930 socksize = sizeof(*sdl);
931 socksize = roundup2(socksize, sizeof(long));
932 ifasize = sizeof(*ifa) + 2 * socksize;
933 ifa = ifa_alloc(ifasize, M_WAITOK);
934 sdl = (struct sockaddr_dl *)(ifa + 1);
935 sdl->sdl_len = socksize;
936 sdl->sdl_family = AF_LINK;
937 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
938 sdl->sdl_nlen = namelen;
939 sdl->sdl_index = ifp->if_index;
940 sdl->sdl_type = ifp->if_type;
943 ifa->ifa_addr = (struct sockaddr *)sdl;
944 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
945 ifa->ifa_netmask = (struct sockaddr *)sdl;
946 sdl->sdl_len = masklen;
948 sdl->sdl_data[--namelen] = 0xff;
949 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
950 /* Reliably crash if used uninitialized. */
951 ifp->if_broadcastaddr = NULL;
953 if (ifp->if_type == IFT_ETHER) {
954 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
958 #if defined(INET) || defined(INET6)
959 /* Use defaults for TSO, if nothing is set */
960 if (ifp->if_hw_tsomax == 0 &&
961 ifp->if_hw_tsomaxsegcount == 0 &&
962 ifp->if_hw_tsomaxsegsize == 0) {
964 * The TSO defaults needs to be such that an
965 * NFS mbuf list of 35 mbufs totalling just
966 * below 64K works and that a chain of mbufs
967 * can be defragged into at most 32 segments:
969 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
970 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
971 ifp->if_hw_tsomaxsegcount = 35;
972 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
974 /* XXX some drivers set IFCAP_TSO after ethernet attach */
975 if (ifp->if_capabilities & IFCAP_TSO) {
976 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
978 ifp->if_hw_tsomaxsegcount,
979 ifp->if_hw_tsomaxsegsize);
987 * Update the interface index in the link layer address
990 for (ifa = ifp->if_addr; ifa != NULL;
991 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
992 if (ifa->ifa_addr->sa_family == AF_LINK) {
993 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
994 sdl->sdl_index = ifp->if_index;
1002 if (domain_init_status >= 2)
1003 if_attachdomain1(ifp);
1005 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1006 if (IS_DEFAULT_VNET(curvnet))
1007 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
1009 /* Announce the interface. */
1010 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1014 if_epochalloc(void *dummy __unused)
1017 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
1019 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
1022 if_attachdomain(void *dummy)
1026 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
1027 if_attachdomain1(ifp);
1029 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
1030 if_attachdomain, NULL);
1033 if_attachdomain1(struct ifnet *ifp)
1038 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1039 * cannot lock ifp->if_afdata initialization, entirely.
1041 IF_AFDATA_LOCK(ifp);
1042 if (ifp->if_afdata_initialized >= domain_init_status) {
1043 IF_AFDATA_UNLOCK(ifp);
1044 log(LOG_WARNING, "%s called more than once on %s\n",
1045 __func__, ifp->if_xname);
1048 ifp->if_afdata_initialized = domain_init_status;
1049 IF_AFDATA_UNLOCK(ifp);
1051 /* address family dependent data region */
1052 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1053 for (dp = domains; dp; dp = dp->dom_next) {
1054 if (dp->dom_ifattach)
1055 ifp->if_afdata[dp->dom_family] =
1056 (*dp->dom_ifattach)(ifp);
1061 * Remove any unicast or broadcast network addresses from an interface.
1064 if_purgeaddrs(struct ifnet *ifp)
1069 struct epoch_tracker et;
1071 NET_EPOCH_ENTER(et);
1072 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1073 if (ifa->ifa_addr->sa_family != AF_LINK)
1081 /* XXX: Ugly!! ad hoc just for INET */
1082 if (ifa->ifa_addr->sa_family == AF_INET) {
1083 struct ifaliasreq ifr;
1085 bzero(&ifr, sizeof(ifr));
1086 ifr.ifra_addr = *ifa->ifa_addr;
1087 if (ifa->ifa_dstaddr)
1088 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1089 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1095 if (ifa->ifa_addr->sa_family == AF_INET6) {
1096 in6_purgeifaddr((struct in6_ifaddr *)ifa);
1097 /* ifp_addrhead is already updated */
1102 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1103 IF_ADDR_WUNLOCK(ifp);
1109 * Remove any multicast network addresses from an interface when an ifnet
1113 if_purgemaddrs(struct ifnet *ifp)
1115 struct ifmultiaddr *ifma;
1118 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1119 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1120 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1121 if_delmulti_locked(ifp, ifma, 1);
1123 IF_ADDR_WUNLOCK(ifp);
1127 * Detach an interface, removing it from the list of "active" interfaces.
1128 * If vmove flag is set on entry to if_detach_internal(), perform only a
1129 * limited subset of cleanup tasks, given that we are moving an ifnet from
1130 * one vnet to another, where it must be fully operational.
1132 * XXXRW: There are some significant questions about event ordering, and
1133 * how to prevent things from starting to use the interface during detach.
1136 if_detach(struct ifnet *ifp)
1140 CURVNET_SET_QUIET(ifp->if_vnet);
1141 found = if_unlink_ifnet(ifp, false);
1143 sx_xlock(&ifnet_detach_sxlock);
1144 if_detach_internal(ifp, 0, NULL);
1145 sx_xunlock(&ifnet_detach_sxlock);
1151 * The vmove flag, if set, indicates that we are called from a callpath
1152 * that is moving an interface to a different vnet instance.
1154 * The shutdown flag, if set, indicates that we are called in the
1155 * process of shutting down a vnet instance. Currently only the
1156 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1157 * on a vnet instance shutdown without this flag being set, e.g., when
1158 * the cloned interfaces are destoyed as first thing of teardown.
1161 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1169 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1173 * At this point we know the interface still was on the ifnet list
1174 * and we removed it so we are in a stable state.
1176 epoch_wait_preempt(net_epoch_preempt);
1179 * Ensure all pending EPOCH(9) callbacks have been executed. This
1180 * fixes issues about late destruction of multicast options
1181 * which lead to leave group calls, which in turn access the
1182 * belonging ifnet structure:
1184 epoch_drain_callbacks(net_epoch_preempt);
1187 * In any case (destroy or vmove) detach us from the groups
1188 * and remove/wait for pending events on the taskq.
1189 * XXX-BZ in theory an interface could still enqueue a taskq change?
1193 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1194 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1197 * Check if this is a cloned interface or not. Must do even if
1198 * shutting down as a if_vmove_reclaim() would move the ifp and
1199 * the if_clone_addgroup() will have a corrupted string overwise
1200 * from a gibberish pointer.
1202 if (vmove && ifcp != NULL)
1203 *ifcp = if_clone_findifc(ifp);
1209 * On VNET shutdown abort here as the stack teardown will do all
1210 * the work top-down for us.
1213 /* Give interface users the chance to clean up. */
1214 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1217 * In case of a vmove we are done here without error.
1218 * If we would signal an error it would lead to the same
1219 * abort as if we did not find the ifnet anymore.
1220 * if_detach() calls us in void context and does not care
1221 * about an early abort notification, so life is splendid :)
1223 goto finish_vnet_shutdown;
1228 * At this point we are not tearing down a VNET and are either
1229 * going to destroy or vmove the interface and have to cleanup
1234 * Remove routes and flush queues.
1237 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1238 altq_disable(&ifp->if_snd);
1239 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1240 altq_detach(&ifp->if_snd);
1251 * Remove all IPv6 kernel structs related to ifp. This should be done
1252 * before removing routing entries below, since IPv6 interface direct
1253 * routes are expected to be removed by the IPv6-specific kernel API.
1254 * Otherwise, the kernel will detect some inconsistency and bark it.
1258 if_purgemaddrs(ifp);
1260 /* Announce that the interface is gone. */
1261 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1262 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1263 if (IS_DEFAULT_VNET(curvnet))
1264 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1268 * Prevent further calls into the device driver via ifnet.
1273 * Clean up all addresses.
1276 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1277 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1278 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1279 IF_ADDR_WUNLOCK(ifp);
1282 IF_ADDR_WUNLOCK(ifp);
1285 rt_flushifroutes(ifp);
1288 finish_vnet_shutdown:
1291 * We cannot hold the lock over dom_ifdetach calls as they might
1292 * sleep, for example trying to drain a callout, thus open up the
1293 * theoretical race with re-attaching.
1295 IF_AFDATA_LOCK(ifp);
1296 i = ifp->if_afdata_initialized;
1297 ifp->if_afdata_initialized = 0;
1298 IF_AFDATA_UNLOCK(ifp);
1299 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1300 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1301 (*dp->dom_ifdetach)(ifp,
1302 ifp->if_afdata[dp->dom_family]);
1303 ifp->if_afdata[dp->dom_family] = NULL;
1312 * if_vmove() performs a limited version of if_detach() in current
1313 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1314 * An attempt is made to shrink if_index in current vnet, find an
1315 * unused if_index in target vnet and calls if_grow() if necessary,
1316 * and finally find an unused if_xname for the target vnet.
1319 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1321 struct if_clone *ifc;
1323 u_int bif_dlt, bif_hdrlen;
1330 * if_detach_internal() will call the eventhandler to notify
1331 * interface departure. That will detach if_bpf. We need to
1332 * safe the dlt and hdrlen so we can re-attach it later.
1334 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1338 * Detach from current vnet, but preserve LLADDR info, do not
1339 * mark as dead etc. so that the ifnet can be reattached later.
1340 * If we cannot find it, we lost the race to someone else.
1342 rc = if_detach_internal(ifp, 1, &ifc);
1347 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1348 * the if_index for that vnet if possible.
1350 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1351 * or we'd lock on one vnet and unlock on another.
1354 ifindex_free_locked(ifp->if_index);
1358 * Perform interface-specific reassignment tasks, if provided by
1361 if (ifp->if_reassign != NULL)
1362 ifp->if_reassign(ifp, new_vnet, NULL);
1365 * Switch to the context of the target vnet.
1367 CURVNET_SET_QUIET(new_vnet);
1370 ifp->if_index = ifindex_alloc(&old);
1371 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1373 epoch_wait_preempt(net_epoch_preempt);
1377 ifnet_setbyindex(ifp->if_index, ifp);
1380 if_attach_internal(ifp, 1, ifc);
1383 if (ifp->if_bpf == NULL)
1384 bpfattach(ifp, bif_dlt, bif_hdrlen);
1392 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1395 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1403 /* Try to find the prison within our visibility. */
1404 sx_slock(&allprison_lock);
1405 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1406 sx_sunlock(&allprison_lock);
1409 prison_hold_locked(pr);
1410 mtx_unlock(&pr->pr_mtx);
1412 /* Do not try to move the iface from and to the same prison. */
1413 if (pr->pr_vnet == ifp->if_vnet) {
1418 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1419 /* XXX Lock interfaces to avoid races. */
1420 CURVNET_SET_QUIET(pr->pr_vnet);
1421 difp = ifunit(ifname);
1428 /* Make sure the VNET is stable. */
1429 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1437 found = if_unlink_ifnet(ifp, true);
1440 /* Move the interface into the child jail/vnet. */
1441 error = if_vmove(ifp, pr->pr_vnet);
1443 /* Report the new if_xname back to the userland on success. */
1445 sprintf(ifname, "%s", ifp->if_xname);
1452 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1455 struct vnet *vnet_dst;
1460 /* Try to find the prison within our visibility. */
1461 sx_slock(&allprison_lock);
1462 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1463 sx_sunlock(&allprison_lock);
1466 prison_hold_locked(pr);
1467 mtx_unlock(&pr->pr_mtx);
1469 /* Make sure the named iface exists in the source prison/vnet. */
1470 CURVNET_SET(pr->pr_vnet);
1471 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1478 /* Do not try to move the iface from and to the same prison. */
1479 vnet_dst = TD_TO_VNET(td);
1480 if (vnet_dst == ifp->if_vnet) {
1486 /* Make sure the VNET is stable. */
1487 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1494 /* Get interface back from child jail/vnet. */
1495 found = if_unlink_ifnet(ifp, true);
1497 error = if_vmove(ifp, vnet_dst);
1500 /* Report the new if_xname back to the userland on success. */
1502 sprintf(ifname, "%s", ifp->if_xname);
1510 * Add a group to an interface
1513 if_addgroup(struct ifnet *ifp, const char *groupname)
1515 struct ifg_list *ifgl;
1516 struct ifg_group *ifg = NULL;
1517 struct ifg_member *ifgm;
1520 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1521 groupname[strlen(groupname) - 1] <= '9')
1525 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1526 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1531 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1536 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1542 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1543 if (!strcmp(ifg->ifg_group, groupname))
1547 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1553 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1554 ifg->ifg_refcnt = 0;
1555 CK_STAILQ_INIT(&ifg->ifg_members);
1556 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1561 ifgl->ifgl_group = ifg;
1562 ifgm->ifgm_ifp = ifp;
1565 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1566 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1567 IF_ADDR_WUNLOCK(ifp);
1572 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1573 EVENTHANDLER_INVOKE(group_change_event, groupname);
1579 * Helper function to remove a group out of an interface. Expects the global
1580 * ifnet lock to be write-locked, and drops it before returning.
1583 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1584 const char *groupname)
1586 struct ifg_member *ifgm;
1589 IFNET_WLOCK_ASSERT();
1592 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1593 IF_ADDR_WUNLOCK(ifp);
1595 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1596 if (ifgm->ifgm_ifp == ifp) {
1597 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1598 ifg_member, ifgm_next);
1603 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1604 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1612 epoch_wait_preempt(net_epoch_preempt);
1614 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1615 free(ifgl->ifgl_group, M_TEMP);
1620 EVENTHANDLER_INVOKE(group_change_event, groupname);
1624 * Remove a group from an interface
1627 if_delgroup(struct ifnet *ifp, const char *groupname)
1629 struct ifg_list *ifgl;
1632 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1633 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1640 _if_delgroup_locked(ifp, ifgl, groupname);
1646 * Remove an interface from all groups
1649 if_delgroups(struct ifnet *ifp)
1651 struct ifg_list *ifgl;
1652 char groupname[IFNAMSIZ];
1655 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1656 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1657 _if_delgroup_locked(ifp, ifgl, groupname);
1664 * Stores all groups from an interface in memory pointed to by ifgr.
1667 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1670 struct ifg_list *ifgl;
1671 struct ifg_req ifgrq, *ifgp;
1675 if (ifgr->ifgr_len == 0) {
1676 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1677 ifgr->ifgr_len += sizeof(struct ifg_req);
1681 len = ifgr->ifgr_len;
1682 ifgp = ifgr->ifgr_groups;
1684 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1685 if (len < sizeof(ifgrq))
1687 bzero(&ifgrq, sizeof ifgrq);
1688 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1689 sizeof(ifgrq.ifgrq_group));
1690 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1692 len -= sizeof(ifgrq);
1700 * Stores all members of a group in memory pointed to by igfr
1703 if_getgroupmembers(struct ifgroupreq *ifgr)
1705 struct ifg_group *ifg;
1706 struct ifg_member *ifgm;
1707 struct ifg_req ifgrq, *ifgp;
1711 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1712 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1719 if (ifgr->ifgr_len == 0) {
1720 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1721 ifgr->ifgr_len += sizeof(ifgrq);
1726 len = ifgr->ifgr_len;
1727 ifgp = ifgr->ifgr_groups;
1728 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1729 if (len < sizeof(ifgrq)) {
1733 bzero(&ifgrq, sizeof ifgrq);
1734 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1735 sizeof(ifgrq.ifgrq_member));
1736 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1740 len -= sizeof(ifgrq);
1749 * Return counter values from counter(9)s stored in ifnet.
1752 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1755 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1757 return (counter_u64_fetch(ifp->if_counters[cnt]));
1761 * Increase an ifnet counter. Usually used for counters shared
1762 * between the stack and a driver, but function supports them all.
1765 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1768 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1770 counter_u64_add(ifp->if_counters[cnt], inc);
1774 * Copy data from ifnet to userland API structure if_data.
1777 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1780 ifd->ifi_type = ifp->if_type;
1781 ifd->ifi_physical = 0;
1782 ifd->ifi_addrlen = ifp->if_addrlen;
1783 ifd->ifi_hdrlen = ifp->if_hdrlen;
1784 ifd->ifi_link_state = ifp->if_link_state;
1786 ifd->ifi_datalen = sizeof(struct if_data);
1787 ifd->ifi_mtu = ifp->if_mtu;
1788 ifd->ifi_metric = ifp->if_metric;
1789 ifd->ifi_baudrate = ifp->if_baudrate;
1790 ifd->ifi_hwassist = ifp->if_hwassist;
1791 ifd->ifi_epoch = ifp->if_epoch;
1792 ifd->ifi_lastchange = ifp->if_lastchange;
1794 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1795 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1796 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1797 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1798 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1799 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1800 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1801 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1802 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1803 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1804 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1805 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1809 * Initialization, destruction and refcounting functions for ifaddrs.
1812 ifa_alloc(size_t size, int flags)
1816 KASSERT(size >= sizeof(struct ifaddr),
1817 ("%s: invalid size %zu", __func__, size));
1819 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1823 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1825 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1827 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1829 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1832 refcount_init(&ifa->ifa_refcnt, 1);
1837 /* free(NULL) is okay */
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);
1848 ifa_ref(struct ifaddr *ifa)
1852 old = refcount_acquire(&ifa->ifa_refcnt);
1853 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1857 ifa_try_ref(struct ifaddr *ifa)
1861 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1865 ifa_destroy(epoch_context_t ctx)
1869 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1870 counter_u64_free(ifa->ifa_opackets);
1871 counter_u64_free(ifa->ifa_ipackets);
1872 counter_u64_free(ifa->ifa_obytes);
1873 counter_u64_free(ifa->ifa_ibytes);
1874 free(ifa, M_IFADDR);
1878 ifa_free(struct ifaddr *ifa)
1881 if (refcount_release(&ifa->ifa_refcnt))
1882 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1886 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1887 * structs used to represent other address families, it is necessary
1888 * to perform a different comparison.
1891 #define sa_dl_equal(a1, a2) \
1892 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1893 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1894 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1895 CLLADDR((const struct sockaddr_dl *)(a2)), \
1896 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1899 * Locate an interface based on a complete address.
1903 ifa_ifwithaddr(const struct sockaddr *addr)
1910 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1911 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1912 if (ifa->ifa_addr->sa_family != addr->sa_family)
1914 if (sa_equal(addr, ifa->ifa_addr)) {
1917 /* IP6 doesn't have broadcast */
1918 if ((ifp->if_flags & IFF_BROADCAST) &&
1919 ifa->ifa_broadaddr &&
1920 ifa->ifa_broadaddr->sa_len != 0 &&
1921 sa_equal(ifa->ifa_broadaddr, addr)) {
1932 ifa_ifwithaddr_check(const struct sockaddr *addr)
1934 struct epoch_tracker et;
1937 NET_EPOCH_ENTER(et);
1938 rc = (ifa_ifwithaddr(addr) != NULL);
1944 * Locate an interface based on the broadcast address.
1948 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1954 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1955 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1957 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1958 if (ifa->ifa_addr->sa_family != addr->sa_family)
1960 if ((ifp->if_flags & IFF_BROADCAST) &&
1961 ifa->ifa_broadaddr &&
1962 ifa->ifa_broadaddr->sa_len != 0 &&
1963 sa_equal(ifa->ifa_broadaddr, addr)) {
1974 * Locate the point to point interface with a given destination address.
1978 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1984 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1985 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1987 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1989 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1990 if (ifa->ifa_addr->sa_family != addr->sa_family)
1992 if (ifa->ifa_dstaddr != NULL &&
1993 sa_equal(addr, ifa->ifa_dstaddr)) {
2004 * Find an interface on a specific network. If many, choice
2005 * is most specific found.
2008 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2012 struct ifaddr *ifa_maybe = NULL;
2013 u_int af = addr->sa_family;
2014 const char *addr_data = addr->sa_data, *cplim;
2018 * AF_LINK addresses can be looked up directly by their index number,
2019 * so do that if we can.
2021 if (af == AF_LINK) {
2022 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2023 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2024 return (ifaddr_byindex(sdl->sdl_index));
2028 * Scan though each interface, looking for ones that have addresses
2029 * in this address family and the requested fib.
2031 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2032 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2034 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2035 const char *cp, *cp2, *cp3;
2037 if (ifa->ifa_addr->sa_family != af)
2039 if (af == AF_INET &&
2040 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2042 * This is a bit broken as it doesn't
2043 * take into account that the remote end may
2044 * be a single node in the network we are
2046 * The trouble is that we don't know the
2047 * netmask for the remote end.
2049 if (ifa->ifa_dstaddr != NULL &&
2050 sa_equal(addr, ifa->ifa_dstaddr)) {
2055 * Scan all the bits in the ifa's address.
2056 * If a bit dissagrees with what we are
2057 * looking for, mask it with the netmask
2058 * to see if it really matters.
2059 * (A byte at a time)
2061 if (ifa->ifa_netmask == 0)
2064 cp2 = ifa->ifa_addr->sa_data;
2065 cp3 = ifa->ifa_netmask->sa_data;
2066 cplim = ifa->ifa_netmask->sa_len
2067 + (char *)ifa->ifa_netmask;
2069 if ((*cp++ ^ *cp2++) & *cp3++)
2070 goto next; /* next address! */
2072 * If the netmask of what we just found
2073 * is more specific than what we had before
2074 * (if we had one), or if the virtual status
2075 * of new prefix is better than of the old one,
2076 * then remember the new one before continuing
2077 * to search for an even better one.
2079 if (ifa_maybe == NULL ||
2080 ifa_preferred(ifa_maybe, ifa) ||
2081 rn_refines((caddr_t)ifa->ifa_netmask,
2082 (caddr_t)ifa_maybe->ifa_netmask)) {
2095 * Find an interface address specific to an interface best matching
2099 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2102 const char *cp, *cp2, *cp3;
2104 struct ifaddr *ifa_maybe = NULL;
2105 u_int af = addr->sa_family;
2111 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2112 if (ifa->ifa_addr->sa_family != af)
2114 if (ifa_maybe == NULL)
2116 if (ifa->ifa_netmask == 0) {
2117 if (sa_equal(addr, ifa->ifa_addr) ||
2118 (ifa->ifa_dstaddr &&
2119 sa_equal(addr, ifa->ifa_dstaddr)))
2123 if (ifp->if_flags & IFF_POINTOPOINT) {
2124 if (sa_equal(addr, ifa->ifa_dstaddr))
2128 cp2 = ifa->ifa_addr->sa_data;
2129 cp3 = ifa->ifa_netmask->sa_data;
2130 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2131 for (; cp3 < cplim; cp3++)
2132 if ((*cp++ ^ *cp2++) & *cp3)
2144 * See whether new ifa is better than current one:
2145 * 1) A non-virtual one is preferred over virtual.
2146 * 2) A virtual in master state preferred over any other state.
2148 * Used in several address selecting functions.
2151 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2154 return (cur->ifa_carp && (!next->ifa_carp ||
2155 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2158 struct sockaddr_dl *
2159 link_alloc_sdl(size_t size, int flags)
2162 return (malloc(size, M_TEMP, flags));
2166 link_free_sdl(struct sockaddr *sa)
2172 * Fills in given sdl with interface basic info.
2173 * Returns pointer to filled sdl.
2175 struct sockaddr_dl *
2176 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2178 struct sockaddr_dl *sdl;
2180 sdl = (struct sockaddr_dl *)paddr;
2181 memset(sdl, 0, sizeof(struct sockaddr_dl));
2182 sdl->sdl_len = sizeof(struct sockaddr_dl);
2183 sdl->sdl_family = AF_LINK;
2184 sdl->sdl_index = ifp->if_index;
2185 sdl->sdl_type = iftype;
2191 * Mark an interface down and notify protocols of
2195 if_unroute(struct ifnet *ifp, int flag, int fam)
2199 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2201 ifp->if_flags &= ~flag;
2202 getmicrotime(&ifp->if_lastchange);
2203 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2204 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2205 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2206 ifp->if_qflush(ifp);
2209 (*carp_linkstate_p)(ifp);
2214 * Mark an interface up and notify protocols of
2218 if_route(struct ifnet *ifp, int flag, int fam)
2222 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2224 ifp->if_flags |= flag;
2225 getmicrotime(&ifp->if_lastchange);
2226 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2227 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2228 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2230 (*carp_linkstate_p)(ifp);
2237 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2238 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2239 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2240 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2241 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2242 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2243 int (*vlan_setcookie_p)(struct ifnet *, void *);
2244 void *(*vlan_cookie_p)(struct ifnet *);
2247 * Handle a change in the interface link state. To avoid LORs
2248 * between driver lock and upper layer locks, as well as possible
2249 * recursions, we post event to taskqueue, and all job
2250 * is done in static do_link_state_change().
2253 if_link_state_change(struct ifnet *ifp, int link_state)
2255 /* Return if state hasn't changed. */
2256 if (ifp->if_link_state == link_state)
2259 ifp->if_link_state = link_state;
2261 /* XXXGL: reference ifp? */
2262 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2266 do_link_state_change(void *arg, int pending)
2272 link_state = ifp->if_link_state;
2274 CURVNET_SET(ifp->if_vnet);
2276 if (ifp->if_vlantrunk != NULL)
2277 (*vlan_link_state_p)(ifp);
2279 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2280 ifp->if_l2com != NULL)
2281 (*ng_ether_link_state_p)(ifp, link_state);
2283 (*carp_linkstate_p)(ifp);
2285 ifp->if_bridge_linkstate(ifp);
2287 (*lagg_linkstate_p)(ifp, link_state);
2289 if (IS_DEFAULT_VNET(curvnet))
2290 devctl_notify("IFNET", ifp->if_xname,
2291 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2294 if_printf(ifp, "%d link states coalesced\n", pending);
2295 if (log_link_state_change)
2296 if_printf(ifp, "link state changed to %s\n",
2297 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2298 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2303 * Mark an interface down and notify protocols of
2307 if_down(struct ifnet *ifp)
2310 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2311 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2315 * Mark an interface up and notify protocols of
2319 if_up(struct ifnet *ifp)
2322 if_route(ifp, IFF_UP, AF_UNSPEC);
2323 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2327 * Flush an interface queue.
2330 if_qflush(struct ifnet *ifp)
2338 if (ALTQ_IS_ENABLED(ifq))
2342 while ((m = n) != NULL) {
2353 * Map interface name to interface structure pointer, with or without
2354 * returning a reference.
2357 ifunit_ref(const char *name)
2359 struct epoch_tracker et;
2362 NET_EPOCH_ENTER(et);
2363 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2364 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2365 !(ifp->if_flags & IFF_DYING))
2375 ifunit(const char *name)
2377 struct epoch_tracker et;
2380 NET_EPOCH_ENTER(et);
2381 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2382 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2390 ifr_buffer_get_buffer(void *data)
2392 union ifreq_union *ifrup;
2395 #ifdef COMPAT_FREEBSD32
2396 if (SV_CURPROC_FLAG(SV_ILP32))
2397 return ((void *)(uintptr_t)
2398 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2400 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2404 ifr_buffer_set_buffer_null(void *data)
2406 union ifreq_union *ifrup;
2409 #ifdef COMPAT_FREEBSD32
2410 if (SV_CURPROC_FLAG(SV_ILP32))
2411 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2414 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2418 ifr_buffer_get_length(void *data)
2420 union ifreq_union *ifrup;
2423 #ifdef COMPAT_FREEBSD32
2424 if (SV_CURPROC_FLAG(SV_ILP32))
2425 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2427 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2431 ifr_buffer_set_length(void *data, size_t len)
2433 union ifreq_union *ifrup;
2436 #ifdef COMPAT_FREEBSD32
2437 if (SV_CURPROC_FLAG(SV_ILP32))
2438 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2441 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2445 ifr_data_get_ptr(void *ifrp)
2447 union ifreq_union *ifrup;
2450 #ifdef COMPAT_FREEBSD32
2451 if (SV_CURPROC_FLAG(SV_ILP32))
2452 return ((void *)(uintptr_t)
2453 ifrup->ifr32.ifr_ifru.ifru_data);
2455 return (ifrup->ifr.ifr_ifru.ifru_data);
2459 * Hardware specific interface ioctls.
2462 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2465 int error = 0, do_ifup = 0;
2466 int new_flags, temp_flags;
2467 size_t namelen, onamelen;
2469 char *descrbuf, *odescrbuf;
2470 char new_name[IFNAMSIZ];
2471 char old_name[IFNAMSIZ], strbuf[IFNAMSIZ + 8];
2473 struct sockaddr_dl *sdl;
2475 ifr = (struct ifreq *)data;
2478 ifr->ifr_index = ifp->if_index;
2482 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2483 ifr->ifr_flags = temp_flags & 0xffff;
2484 ifr->ifr_flagshigh = temp_flags >> 16;
2488 ifr->ifr_reqcap = ifp->if_capabilities;
2489 ifr->ifr_curcap = ifp->if_capenable;
2496 /* Ensure uninitialised padding is not leaked. */
2497 memset(&ifd, 0, sizeof(ifd));
2499 if_data_copy(ifp, &ifd);
2500 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2506 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2511 ifr->ifr_metric = ifp->if_metric;
2515 ifr->ifr_mtu = ifp->if_mtu;
2519 /* XXXGL: did this ever worked? */
2525 sx_slock(&ifdescr_sx);
2526 if (ifp->if_description == NULL)
2529 /* space for terminating nul */
2530 descrlen = strlen(ifp->if_description) + 1;
2531 if (ifr_buffer_get_length(ifr) < descrlen)
2532 ifr_buffer_set_buffer_null(ifr);
2534 error = copyout(ifp->if_description,
2535 ifr_buffer_get_buffer(ifr), descrlen);
2536 ifr_buffer_set_length(ifr, descrlen);
2538 sx_sunlock(&ifdescr_sx);
2542 error = priv_check(td, PRIV_NET_SETIFDESCR);
2547 * Copy only (length-1) bytes to make sure that
2548 * if_description is always nul terminated. The
2549 * length parameter is supposed to count the
2550 * terminating nul in.
2552 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2553 return (ENAMETOOLONG);
2554 else if (ifr_buffer_get_length(ifr) == 0)
2557 descrbuf = malloc(ifr_buffer_get_length(ifr),
2558 M_IFDESCR, M_WAITOK | M_ZERO);
2559 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2560 ifr_buffer_get_length(ifr) - 1);
2562 free(descrbuf, M_IFDESCR);
2567 sx_xlock(&ifdescr_sx);
2568 odescrbuf = ifp->if_description;
2569 ifp->if_description = descrbuf;
2570 sx_xunlock(&ifdescr_sx);
2572 getmicrotime(&ifp->if_lastchange);
2573 free(odescrbuf, M_IFDESCR);
2577 ifr->ifr_fib = ifp->if_fib;
2581 error = priv_check(td, PRIV_NET_SETIFFIB);
2584 if (ifr->ifr_fib >= rt_numfibs)
2587 ifp->if_fib = ifr->ifr_fib;
2591 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2595 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2596 * check, so we don't need special handling here yet.
2598 new_flags = (ifr->ifr_flags & 0xffff) |
2599 (ifr->ifr_flagshigh << 16);
2600 if (ifp->if_flags & IFF_UP &&
2601 (new_flags & IFF_UP) == 0) {
2603 } else if (new_flags & IFF_UP &&
2604 (ifp->if_flags & IFF_UP) == 0) {
2607 /* See if permanently promiscuous mode bit is about to flip */
2608 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2609 if (new_flags & IFF_PPROMISC)
2610 ifp->if_flags |= IFF_PROMISC;
2611 else if (ifp->if_pcount == 0)
2612 ifp->if_flags &= ~IFF_PROMISC;
2613 if (log_promisc_mode_change)
2614 if_printf(ifp, "permanently promiscuous mode %s\n",
2615 ((new_flags & IFF_PPROMISC) ?
2616 "enabled" : "disabled"));
2618 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2619 (new_flags &~ IFF_CANTCHANGE);
2620 if (ifp->if_ioctl) {
2621 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2625 getmicrotime(&ifp->if_lastchange);
2629 error = priv_check(td, PRIV_NET_SETIFCAP);
2632 if (ifp->if_ioctl == NULL)
2633 return (EOPNOTSUPP);
2634 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2636 error = (*ifp->if_ioctl)(ifp, cmd, data);
2638 getmicrotime(&ifp->if_lastchange);
2643 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2648 error = priv_check(td, PRIV_NET_SETIFNAME);
2651 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2655 if (new_name[0] == '\0')
2657 if (strcmp(new_name, ifp->if_xname) == 0)
2659 if (ifunit(new_name) != NULL)
2663 * XXX: Locking. Nothing else seems to lock if_flags,
2664 * and there are numerous other races with the
2665 * ifunit() checks not being atomic with namespace
2666 * changes (renames, vmoves, if_attach, etc).
2668 ifp->if_flags |= IFF_RENAMING;
2670 /* Announce the departure of the interface. */
2671 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2672 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2674 if_printf(ifp, "changing name to '%s'\n", new_name);
2677 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
2678 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2680 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2681 namelen = strlen(new_name);
2682 onamelen = sdl->sdl_nlen;
2684 * Move the address if needed. This is safe because we
2685 * allocate space for a name of length IFNAMSIZ when we
2686 * create this in if_attach().
2688 if (namelen != onamelen) {
2689 bcopy(sdl->sdl_data + onamelen,
2690 sdl->sdl_data + namelen, sdl->sdl_alen);
2692 bcopy(new_name, sdl->sdl_data, namelen);
2693 sdl->sdl_nlen = namelen;
2694 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2695 bzero(sdl->sdl_data, onamelen);
2696 while (namelen != 0)
2697 sdl->sdl_data[--namelen] = 0xff;
2698 IF_ADDR_WUNLOCK(ifp);
2700 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2701 /* Announce the return of the interface. */
2702 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2704 ifp->if_flags &= ~IFF_RENAMING;
2706 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
2707 devctl_notify("IFNET", old_name, "RENAME", strbuf);
2712 error = priv_check(td, PRIV_NET_SETIFVNET);
2715 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2720 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2723 ifp->if_metric = ifr->ifr_metric;
2724 getmicrotime(&ifp->if_lastchange);
2728 error = priv_check(td, PRIV_NET_SETIFPHYS);
2731 if (ifp->if_ioctl == NULL)
2732 return (EOPNOTSUPP);
2733 error = (*ifp->if_ioctl)(ifp, cmd, data);
2735 getmicrotime(&ifp->if_lastchange);
2740 u_long oldmtu = ifp->if_mtu;
2742 error = priv_check(td, PRIV_NET_SETIFMTU);
2745 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2747 if (ifp->if_ioctl == NULL)
2748 return (EOPNOTSUPP);
2749 /* Disallow MTU changes on bridge member interfaces. */
2751 return (EOPNOTSUPP);
2752 error = (*ifp->if_ioctl)(ifp, cmd, data);
2754 getmicrotime(&ifp->if_lastchange);
2757 DEBUGNET_NOTIFY_MTU(ifp);
2761 * If the link MTU changed, do network layer specific procedure.
2763 if (ifp->if_mtu != oldmtu) {
2774 if (cmd == SIOCADDMULTI)
2775 error = priv_check(td, PRIV_NET_ADDMULTI);
2777 error = priv_check(td, PRIV_NET_DELMULTI);
2781 /* Don't allow group membership on non-multicast interfaces. */
2782 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2783 return (EOPNOTSUPP);
2785 /* Don't let users screw up protocols' entries. */
2786 if (ifr->ifr_addr.sa_family != AF_LINK)
2789 if (cmd == SIOCADDMULTI) {
2790 struct epoch_tracker et;
2791 struct ifmultiaddr *ifma;
2794 * Userland is only permitted to join groups once
2795 * via the if_addmulti() KPI, because it cannot hold
2796 * struct ifmultiaddr * between calls. It may also
2797 * lose a race while we check if the membership
2800 NET_EPOCH_ENTER(et);
2801 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2806 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2808 error = if_delmulti(ifp, &ifr->ifr_addr);
2811 getmicrotime(&ifp->if_lastchange);
2814 case SIOCSIFPHYADDR:
2815 case SIOCDIFPHYADDR:
2817 case SIOCSIFPHYADDR_IN6:
2820 case SIOCSIFGENERIC:
2821 error = priv_check(td, PRIV_NET_HWIOCTL);
2824 if (ifp->if_ioctl == NULL)
2825 return (EOPNOTSUPP);
2826 error = (*ifp->if_ioctl)(ifp, cmd, data);
2828 getmicrotime(&ifp->if_lastchange);
2832 case SIOCGIFPSRCADDR:
2833 case SIOCGIFPDSTADDR:
2836 case SIOCGIFGENERIC:
2838 case SIOCGIFRSSHASH:
2839 case SIOCGIFDOWNREASON:
2840 if (ifp->if_ioctl == NULL)
2841 return (EOPNOTSUPP);
2842 error = (*ifp->if_ioctl)(ifp, cmd, data);
2846 error = priv_check(td, PRIV_NET_SETLLADDR);
2849 error = if_setlladdr(ifp,
2850 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2854 error = if_gethwaddr(ifp, ifr);
2858 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2861 error = if_addgroup(ifp,
2862 ((struct ifgroupreq *)data)->ifgr_group);
2869 struct epoch_tracker et;
2871 NET_EPOCH_ENTER(et);
2872 error = if_getgroup((struct ifgroupreq *)data, ifp);
2878 error = priv_check(td, PRIV_NET_DELIFGROUP);
2881 error = if_delgroup(ifp,
2882 ((struct ifgroupreq *)data)->ifgr_group);
2898 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2900 #ifdef COMPAT_FREEBSD32
2904 struct ifgroupreq ifgr;
2905 struct ifmediareq ifmr;
2908 struct ifconf32 *ifc32;
2909 struct ifdrv32 *ifd32;
2910 struct ifgroupreq32 *ifgr32;
2911 struct ifmediareq32 *ifmr32;
2921 CURVNET_SET(so->so_vnet);
2923 /* Make sure the VNET is stable. */
2924 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2931 #ifdef COMPAT_FREEBSD32
2935 ifc32 = (struct ifconf32 *)data;
2936 thunk.ifc.ifc_len = ifc32->ifc_len;
2937 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2938 data = (caddr_t)&thunk.ifc;
2941 case SIOCGDRVSPEC32:
2942 case SIOCSDRVSPEC32:
2943 ifd32 = (struct ifdrv32 *)data;
2944 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2945 sizeof(thunk.ifd.ifd_name));
2946 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2947 thunk.ifd.ifd_len = ifd32->ifd_len;
2948 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2949 data = (caddr_t)&thunk.ifd;
2950 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2952 case SIOCAIFGROUP32:
2953 case SIOCGIFGROUP32:
2954 case SIOCDIFGROUP32:
2955 case SIOCGIFGMEMB32:
2956 ifgr32 = (struct ifgroupreq32 *)data;
2957 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2958 sizeof(thunk.ifgr.ifgr_name));
2959 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2961 case SIOCAIFGROUP32:
2962 case SIOCDIFGROUP32:
2963 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2964 sizeof(thunk.ifgr.ifgr_group));
2966 case SIOCGIFGROUP32:
2967 case SIOCGIFGMEMB32:
2968 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2971 data = (caddr_t)&thunk.ifgr;
2972 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2974 case SIOCGIFMEDIA32:
2975 case SIOCGIFXMEDIA32:
2976 ifmr32 = (struct ifmediareq32 *)data;
2977 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2978 sizeof(thunk.ifmr.ifm_name));
2979 thunk.ifmr.ifm_current = ifmr32->ifm_current;
2980 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
2981 thunk.ifmr.ifm_status = ifmr32->ifm_status;
2982 thunk.ifmr.ifm_active = ifmr32->ifm_active;
2983 thunk.ifmr.ifm_count = ifmr32->ifm_count;
2984 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
2985 data = (caddr_t)&thunk.ifmr;
2986 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2993 error = ifconf(cmd, data);
2997 ifr = (struct ifreq *)data;
3001 error = priv_check(td, PRIV_NET_SETIFVNET);
3003 error = if_vmove_reclaim(td, ifr->ifr_name,
3009 error = priv_check(td, PRIV_NET_IFCREATE);
3011 error = if_clone_create(ifr->ifr_name,
3012 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3013 ifr_data_get_ptr(ifr) : NULL);
3016 error = priv_check(td, PRIV_NET_IFDESTROY);
3019 sx_xlock(&ifnet_detach_sxlock);
3020 error = if_clone_destroy(ifr->ifr_name);
3021 sx_xunlock(&ifnet_detach_sxlock);
3025 case SIOCIFGCLONERS:
3026 error = if_clone_list((struct if_clonereq *)data);
3030 error = if_getgroupmembers((struct ifgroupreq *)data);
3033 #if defined(INET) || defined(INET6)
3036 if (carp_ioctl_p == NULL)
3037 error = EPROTONOSUPPORT;
3039 error = (*carp_ioctl_p)(ifr, cmd, td);
3044 ifp = ifunit_ref(ifr->ifr_name);
3050 error = ifhwioctl(cmd, ifp, data, td);
3051 if (error != ENOIOCTL)
3054 oif_flags = ifp->if_flags;
3055 if (so->so_proto == NULL) {
3061 * Pass the request on to the socket control method, and if the
3062 * latter returns EOPNOTSUPP, directly to the interface.
3064 * Make an exception for the legacy SIOCSIF* requests. Drivers
3065 * trust SIOCSIFADDR et al to come from an already privileged
3066 * layer, and do not perform any credentials checks or input
3069 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3071 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3072 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3073 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3074 error = (*ifp->if_ioctl)(ifp, cmd, data);
3076 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3078 if (ifp->if_flags & IFF_UP)
3087 #ifdef COMPAT_FREEBSD32
3090 switch (saved_cmd) {
3092 ifc32->ifc_len = thunk.ifc.ifc_len;
3094 case SIOCGDRVSPEC32:
3096 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3097 * the struct so just assert that ifd_len (the only
3098 * field it might make sense to update) hasn't
3101 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3102 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3103 thunk.ifd.ifd_len));
3105 case SIOCGIFGROUP32:
3106 case SIOCGIFGMEMB32:
3107 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3109 case SIOCGIFMEDIA32:
3110 case SIOCGIFXMEDIA32:
3111 ifmr32->ifm_current = thunk.ifmr.ifm_current;
3112 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3113 ifmr32->ifm_status = thunk.ifmr.ifm_status;
3114 ifmr32->ifm_active = thunk.ifmr.ifm_active;
3115 ifmr32->ifm_count = thunk.ifmr.ifm_count;
3123 * The code common to handling reference counted flags,
3124 * e.g., in ifpromisc() and if_allmulti().
3125 * The "pflag" argument can specify a permanent mode flag to check,
3126 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3128 * Only to be used on stack-owned flags, not driver-owned flags.
3131 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3135 int oldflags, oldcount;
3137 /* Sanity checks to catch programming errors */
3138 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3139 ("%s: setting driver-owned flag %d", __func__, flag));
3142 KASSERT(*refcount >= 0,
3143 ("%s: increment negative refcount %d for flag %d",
3144 __func__, *refcount, flag));
3146 KASSERT(*refcount > 0,
3147 ("%s: decrement non-positive refcount %d for flag %d",
3148 __func__, *refcount, flag));
3150 /* In case this mode is permanent, just touch refcount */
3151 if (ifp->if_flags & pflag) {
3152 *refcount += onswitch ? 1 : -1;
3156 /* Save ifnet parameters for if_ioctl() may fail */
3157 oldcount = *refcount;
3158 oldflags = ifp->if_flags;
3161 * See if we aren't the only and touching refcount is enough.
3162 * Actually toggle interface flag if we are the first or last.
3167 ifp->if_flags |= flag;
3171 ifp->if_flags &= ~flag;
3174 /* Call down the driver since we've changed interface flags */
3175 if (ifp->if_ioctl == NULL) {
3179 ifr.ifr_flags = ifp->if_flags & 0xffff;
3180 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3181 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3184 /* Notify userland that interface flags have changed */
3189 /* Recover after driver error */
3190 *refcount = oldcount;
3191 ifp->if_flags = oldflags;
3196 * Set/clear promiscuous mode on interface ifp based on the truth value
3197 * of pswitch. The calls are reference counted so that only the first
3198 * "on" request actually has an effect, as does the final "off" request.
3199 * Results are undefined if the "off" and "on" requests are not matched.
3202 ifpromisc(struct ifnet *ifp, int pswitch)
3205 int oldflags = ifp->if_flags;
3207 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3208 &ifp->if_pcount, pswitch);
3209 /* If promiscuous mode status has changed, log a message */
3210 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3211 log_promisc_mode_change)
3212 if_printf(ifp, "promiscuous mode %s\n",
3213 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3218 * Return interface configuration
3219 * of system. List may be used
3220 * in later ioctl's (above) to get
3221 * other information.
3225 ifconf(u_long cmd, caddr_t data)
3227 struct ifconf *ifc = (struct ifconf *)data;
3232 int error, full = 0, valid_len, max_len;
3234 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3235 max_len = maxphys - 1;
3237 /* Prevent hostile input from being able to crash the system */
3238 if (ifc->ifc_len <= 0)
3242 if (ifc->ifc_len <= max_len) {
3243 max_len = ifc->ifc_len;
3246 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3251 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3252 struct epoch_tracker et;
3256 * Zero the ifr to make sure we don't disclose the contents
3259 memset(&ifr, 0, sizeof(ifr));
3261 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3262 >= sizeof(ifr.ifr_name)) {
3265 return (ENAMETOOLONG);
3269 NET_EPOCH_ENTER(et);
3270 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3271 struct sockaddr *sa = ifa->ifa_addr;
3273 if (prison_if(curthread->td_ucred, sa) != 0)
3276 if (sa->sa_len <= sizeof(*sa)) {
3277 if (sa->sa_len < sizeof(*sa)) {
3278 memset(&ifr.ifr_ifru.ifru_addr, 0,
3279 sizeof(ifr.ifr_ifru.ifru_addr));
3280 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3283 ifr.ifr_ifru.ifru_addr = *sa;
3284 sbuf_bcat(sb, &ifr, sizeof(ifr));
3285 max_len += sizeof(ifr);
3288 offsetof(struct ifreq, ifr_addr));
3289 max_len += offsetof(struct ifreq, ifr_addr);
3290 sbuf_bcat(sb, sa, sa->sa_len);
3291 max_len += sa->sa_len;
3294 if (sbuf_error(sb) == 0)
3295 valid_len = sbuf_len(sb);
3299 sbuf_bcat(sb, &ifr, sizeof(ifr));
3300 max_len += sizeof(ifr);
3302 if (sbuf_error(sb) == 0)
3303 valid_len = sbuf_len(sb);
3309 * If we didn't allocate enough space (uncommon), try again. If
3310 * we have already allocated as much space as we are allowed,
3311 * return what we've got.
3313 if (valid_len != max_len && !full) {
3318 ifc->ifc_len = valid_len;
3320 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3326 * Just like ifpromisc(), but for all-multicast-reception mode.
3329 if_allmulti(struct ifnet *ifp, int onswitch)
3332 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3335 struct ifmultiaddr *
3336 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3338 struct ifmultiaddr *ifma;
3340 IF_ADDR_LOCK_ASSERT(ifp);
3342 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3343 if (sa->sa_family == AF_LINK) {
3344 if (sa_dl_equal(ifma->ifma_addr, sa))
3347 if (sa_equal(ifma->ifma_addr, sa))
3356 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3357 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3358 * the ifnet multicast address list here, so the caller must do that and
3359 * other setup work (such as notifying the device driver). The reference
3360 * count is initialized to 1.
3362 static struct ifmultiaddr *
3363 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3366 struct ifmultiaddr *ifma;
3367 struct sockaddr *dupsa;
3369 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3374 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3375 if (dupsa == NULL) {
3376 free(ifma, M_IFMADDR);
3379 bcopy(sa, dupsa, sa->sa_len);
3380 ifma->ifma_addr = dupsa;
3382 ifma->ifma_ifp = ifp;
3383 ifma->ifma_refcount = 1;
3384 ifma->ifma_protospec = NULL;
3387 ifma->ifma_lladdr = NULL;
3391 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3392 if (dupsa == NULL) {
3393 free(ifma->ifma_addr, M_IFMADDR);
3394 free(ifma, M_IFMADDR);
3397 bcopy(llsa, dupsa, llsa->sa_len);
3398 ifma->ifma_lladdr = dupsa;
3404 * if_freemulti: free ifmultiaddr structure and possibly attached related
3405 * addresses. The caller is responsible for implementing reference
3406 * counting, notifying the driver, handling routing messages, and releasing
3407 * any dependent link layer state.
3409 #ifdef MCAST_VERBOSE
3410 extern void kdb_backtrace(void);
3413 if_freemulti_internal(struct ifmultiaddr *ifma)
3416 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3417 ifma->ifma_refcount));
3419 if (ifma->ifma_lladdr != NULL)
3420 free(ifma->ifma_lladdr, M_IFMADDR);
3421 #ifdef MCAST_VERBOSE
3423 printf("%s freeing ifma: %p\n", __func__, ifma);
3425 free(ifma->ifma_addr, M_IFMADDR);
3426 free(ifma, M_IFMADDR);
3430 if_destroymulti(epoch_context_t ctx)
3432 struct ifmultiaddr *ifma;
3434 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3435 if_freemulti_internal(ifma);
3439 if_freemulti(struct ifmultiaddr *ifma)
3441 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3442 ifma->ifma_refcount));
3444 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3448 * Register an additional multicast address with a network interface.
3450 * - If the address is already present, bump the reference count on the
3451 * address and return.
3452 * - If the address is not link-layer, look up a link layer address.
3453 * - Allocate address structures for one or both addresses, and attach to the
3454 * multicast address list on the interface. If automatically adding a link
3455 * layer address, the protocol address will own a reference to the link
3456 * layer address, to be freed when it is freed.
3457 * - Notify the network device driver of an addition to the multicast address
3460 * 'sa' points to caller-owned memory with the desired multicast address.
3462 * 'retifma' will be used to return a pointer to the resulting multicast
3463 * address reference, if desired.
3466 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3467 struct ifmultiaddr **retifma)
3469 struct ifmultiaddr *ifma, *ll_ifma;
3470 struct sockaddr *llsa;
3471 struct sockaddr_dl sdl;
3475 IN_MULTI_LIST_UNLOCK_ASSERT();
3478 IN6_MULTI_LIST_UNLOCK_ASSERT();
3481 * If the address is already present, return a new reference to it;
3482 * otherwise, allocate storage and set up a new address.
3485 ifma = if_findmulti(ifp, sa);
3487 ifma->ifma_refcount++;
3488 if (retifma != NULL)
3490 IF_ADDR_WUNLOCK(ifp);
3495 * The address isn't already present; resolve the protocol address
3496 * into a link layer address, and then look that up, bump its
3497 * refcount or allocate an ifma for that also.
3498 * Most link layer resolving functions returns address data which
3499 * fits inside default sockaddr_dl structure. However callback
3500 * can allocate another sockaddr structure, in that case we need to
3505 if (ifp->if_resolvemulti != NULL) {
3506 /* Provide called function with buffer size information */
3507 sdl.sdl_len = sizeof(sdl);
3508 llsa = (struct sockaddr *)&sdl;
3509 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3515 * Allocate the new address. Don't hook it up yet, as we may also
3516 * need to allocate a link layer multicast address.
3518 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3525 * If a link layer address is found, we'll need to see if it's
3526 * already present in the address list, or allocate is as well.
3527 * When this block finishes, the link layer address will be on the
3531 ll_ifma = if_findmulti(ifp, llsa);
3532 if (ll_ifma == NULL) {
3533 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3534 if (ll_ifma == NULL) {
3535 --ifma->ifma_refcount;
3540 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3541 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3544 ll_ifma->ifma_refcount++;
3545 ifma->ifma_llifma = ll_ifma;
3549 * We now have a new multicast address, ifma, and possibly a new or
3550 * referenced link layer address. Add the primary address to the
3551 * ifnet address list.
3553 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3554 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3556 if (retifma != NULL)
3560 * Must generate the message while holding the lock so that 'ifma'
3561 * pointer is still valid.
3563 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3564 IF_ADDR_WUNLOCK(ifp);
3567 * We are certain we have added something, so call down to the
3568 * interface to let them know about it.
3570 if (ifp->if_ioctl != NULL) {
3571 if (THREAD_CAN_SLEEP())
3572 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3574 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3577 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3578 link_free_sdl(llsa);
3583 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3584 link_free_sdl(llsa);
3587 IF_ADDR_WUNLOCK(ifp);
3592 if_siocaddmulti(void *arg, int pending)
3599 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3601 CURVNET_SET(ifp->if_vnet);
3602 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3607 * Delete a multicast group membership by network-layer group address.
3609 * Returns ENOENT if the entry could not be found. If ifp no longer
3610 * exists, results are undefined. This entry point should only be used
3611 * from subsystems which do appropriate locking to hold ifp for the
3612 * duration of the call.
3613 * Network-layer protocol domains must use if_delmulti_ifma().
3616 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3618 struct ifmultiaddr *ifma;
3621 KASSERT(ifp, ("%s: NULL ifp", __func__));
3625 ifma = if_findmulti(ifp, sa);
3627 lastref = if_delmulti_locked(ifp, ifma, 0);
3628 IF_ADDR_WUNLOCK(ifp);
3633 if (lastref && ifp->if_ioctl != NULL) {
3634 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3641 * Delete all multicast group membership for an interface.
3642 * Should be used to quickly flush all multicast filters.
3645 if_delallmulti(struct ifnet *ifp)
3647 struct ifmultiaddr *ifma;
3648 struct ifmultiaddr *next;
3651 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3652 if_delmulti_locked(ifp, ifma, 0);
3653 IF_ADDR_WUNLOCK(ifp);
3657 if_delmulti_ifma(struct ifmultiaddr *ifma)
3659 if_delmulti_ifma_flags(ifma, 0);
3663 * Delete a multicast group membership by group membership pointer.
3664 * Network-layer protocol domains must use this routine.
3666 * It is safe to call this routine if the ifp disappeared.
3669 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3673 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3675 IN_MULTI_LIST_UNLOCK_ASSERT();
3677 ifp = ifma->ifma_ifp;
3680 printf("%s: ifma_ifp seems to be detached\n", __func__);
3682 struct epoch_tracker et;
3685 NET_EPOCH_ENTER(et);
3686 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3695 * If and only if the ifnet instance exists: Acquire the address lock.
3700 lastref = if_delmulti_locked(ifp, ifma, flags);
3704 * If and only if the ifnet instance exists:
3705 * Release the address lock.
3706 * If the group was left: update the hardware hash filter.
3708 IF_ADDR_WUNLOCK(ifp);
3709 if (lastref && ifp->if_ioctl != NULL) {
3710 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3716 * Perform deletion of network-layer and/or link-layer multicast address.
3718 * Return 0 if the reference count was decremented.
3719 * Return 1 if the final reference was released, indicating that the
3720 * hardware hash filter should be reprogrammed.
3723 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3725 struct ifmultiaddr *ll_ifma;
3727 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3728 KASSERT(ifma->ifma_ifp == ifp,
3729 ("%s: inconsistent ifp %p", __func__, ifp));
3730 IF_ADDR_WLOCK_ASSERT(ifp);
3733 ifp = ifma->ifma_ifp;
3734 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3737 * If the ifnet is detaching, null out references to ifnet,
3738 * so that upper protocol layers will notice, and not attempt
3739 * to obtain locks for an ifnet which no longer exists. The
3740 * routing socket announcement must happen before the ifnet
3741 * instance is detached from the system.
3745 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3748 * ifp may already be nulled out if we are being reentered
3749 * to delete the ll_ifma.
3752 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3753 ifma->ifma_ifp = NULL;
3757 if (--ifma->ifma_refcount > 0)
3760 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3761 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3762 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3765 * If this ifma is a network-layer ifma, a link-layer ifma may
3766 * have been associated with it. Release it first if so.
3768 ll_ifma = ifma->ifma_llifma;
3769 if (ll_ifma != NULL) {
3770 KASSERT(ifma->ifma_lladdr != NULL,
3771 ("%s: llifma w/o lladdr", __func__));
3773 ll_ifma->ifma_ifp = NULL; /* XXX */
3774 if (--ll_ifma->ifma_refcount == 0) {
3776 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3777 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3779 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3782 if_freemulti(ll_ifma);
3787 struct ifmultiaddr *ifmatmp;
3789 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3790 MPASS(ifma != ifmatmp);
3795 * The last reference to this instance of struct ifmultiaddr
3796 * was released; the hardware should be notified of this change.
3802 * Set the link layer address on an interface.
3804 * At this time we only support certain types of interfaces,
3805 * and we don't allow the length of the address to change.
3807 * Set noinline to be dtrace-friendly
3810 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3812 struct sockaddr_dl *sdl;
3820 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3824 if (len != sdl->sdl_alen) /* don't allow length to change */
3827 switch (ifp->if_type) {
3832 case IFT_IEEE8023ADLAG:
3833 bcopy(lladdr, LLADDR(sdl), len);
3840 * If the interface is already up, we need
3841 * to re-init it in order to reprogram its
3844 if ((ifp->if_flags & IFF_UP) != 0) {
3845 if (ifp->if_ioctl) {
3846 ifp->if_flags &= ~IFF_UP;
3847 ifr.ifr_flags = ifp->if_flags & 0xffff;
3848 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3849 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3850 ifp->if_flags |= IFF_UP;
3851 ifr.ifr_flags = ifp->if_flags & 0xffff;
3852 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3853 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3856 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3862 * Compat function for handling basic encapsulation requests.
3863 * Not converted stacks (FDDI, IB, ..) supports traditional
3864 * output model: ARP (and other similar L2 protocols) are handled
3865 * inside output routine, arpresolve/nd6_resolve() returns MAC
3866 * address instead of full prepend.
3868 * This function creates calculated header==MAC for IPv4/IPv6 and
3869 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3873 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3876 if (req->rtype != IFENCAP_LL)
3877 return (EOPNOTSUPP);
3879 if (req->bufsize < req->lladdr_len)
3882 switch (req->family) {
3887 return (EAFNOSUPPORT);
3890 /* Copy lladdr to storage as is */
3891 memmove(req->buf, req->lladdr, req->lladdr_len);
3892 req->bufsize = req->lladdr_len;
3893 req->lladdr_off = 0;
3899 * Tunnel interfaces can nest, also they may cause infinite recursion
3900 * calls when misconfigured. We'll prevent this by detecting loops.
3901 * High nesting level may cause stack exhaustion. We'll prevent this
3902 * by introducing upper limit.
3904 * Return 0, if tunnel nesting count is equal or less than limit.
3907 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3915 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3916 if (*(struct ifnet **)(mtag + 1) == ifp) {
3917 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3922 if (count > limit) {
3924 "%s: if_output recursively called too many times(%d)\n",
3925 if_name(ifp), count);
3928 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3931 *(struct ifnet **)(mtag + 1) = ifp;
3932 m_tag_prepend(m, mtag);
3937 * Get the link layer address that was read from the hardware at attach.
3939 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3940 * their component interfaces as IFT_IEEE8023ADLAG.
3943 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3946 if (ifp->if_hw_addr == NULL)
3949 switch (ifp->if_type) {
3951 case IFT_IEEE8023ADLAG:
3952 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3960 * The name argument must be a pointer to storage which will last as
3961 * long as the interface does. For physical devices, the result of
3962 * device_get_name(dev) is a good choice and for pseudo-devices a
3963 * static string works well.
3966 if_initname(struct ifnet *ifp, const char *name, int unit)
3968 ifp->if_dname = name;
3969 ifp->if_dunit = unit;
3970 if (unit != IF_DUNIT_NONE)
3971 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3973 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3977 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
3981 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3982 vlog(pri, if_fmt, ap);
3988 if_printf(struct ifnet *ifp, const char *fmt, ...)
3993 if_vlog(ifp, LOG_INFO, fmt, ap);
3999 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
4004 if_vlog(ifp, pri, fmt, ap);
4010 if_start(struct ifnet *ifp)
4013 (*(ifp)->if_start)(ifp);
4017 * Backwards compatibility interface for drivers
4018 * that have not implemented it
4021 if_transmit(struct ifnet *ifp, struct mbuf *m)
4025 IFQ_HANDOFF(ifp, m, error);
4030 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4037 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4042 if (_IF_QFULL(ifq)) {
4044 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4049 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4050 if (m->m_flags & (M_BCAST|M_MCAST))
4051 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4052 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4054 _IF_ENQUEUE(ifq, m);
4056 if (ifp != NULL && !active)
4057 (*(ifp)->if_start)(ifp);
4062 if_register_com_alloc(u_char type,
4063 if_com_alloc_t *a, if_com_free_t *f)
4066 KASSERT(if_com_alloc[type] == NULL,
4067 ("if_register_com_alloc: %d already registered", type));
4068 KASSERT(if_com_free[type] == NULL,
4069 ("if_register_com_alloc: %d free already registered", type));
4071 if_com_alloc[type] = a;
4072 if_com_free[type] = f;
4076 if_deregister_com_alloc(u_char type)
4079 KASSERT(if_com_alloc[type] != NULL,
4080 ("if_deregister_com_alloc: %d not registered", type));
4081 KASSERT(if_com_free[type] != NULL,
4082 ("if_deregister_com_alloc: %d free not registered", type));
4085 * Ensure all pending EPOCH(9) callbacks have been executed. This
4086 * fixes issues about late invocation of if_destroy(), which leads
4087 * to memory leak from if_com_alloc[type] allocated if_l2com.
4089 epoch_drain_callbacks(net_epoch_preempt);
4091 if_com_alloc[type] = NULL;
4092 if_com_free[type] = NULL;
4095 /* API for driver access to network stack owned ifnet.*/
4097 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4101 oldbrate = ifp->if_baudrate;
4102 ifp->if_baudrate = baudrate;
4107 if_getbaudrate(if_t ifp)
4110 return (((struct ifnet *)ifp)->if_baudrate);
4114 if_setcapabilities(if_t ifp, int capabilities)
4116 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4121 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4123 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4124 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4130 if_getcapabilities(if_t ifp)
4132 return ((struct ifnet *)ifp)->if_capabilities;
4136 if_setcapenable(if_t ifp, int capabilities)
4138 ((struct ifnet *)ifp)->if_capenable = capabilities;
4143 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4146 ((struct ifnet *)ifp)->if_capenable |= setcap;
4148 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4154 if_getdname(if_t ifp)
4156 return ((struct ifnet *)ifp)->if_dname;
4160 if_togglecapenable(if_t ifp, int togglecap)
4162 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4167 if_getcapenable(if_t ifp)
4169 return ((struct ifnet *)ifp)->if_capenable;
4173 * This is largely undesirable because it ties ifnet to a device, but does
4174 * provide flexiblity for an embedded product vendor. Should be used with
4175 * the understanding that it violates the interface boundaries, and should be
4176 * a last resort only.
4179 if_setdev(if_t ifp, void *dev)
4185 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4187 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4188 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4194 if_getdrvflags(if_t ifp)
4196 return ((struct ifnet *)ifp)->if_drv_flags;
4200 if_setdrvflags(if_t ifp, int flags)
4202 ((struct ifnet *)ifp)->if_drv_flags = flags;
4207 if_setflags(if_t ifp, int flags)
4210 ifp->if_flags = flags;
4215 if_setflagbits(if_t ifp, int set, int clear)
4217 ((struct ifnet *)ifp)->if_flags |= set;
4218 ((struct ifnet *)ifp)->if_flags &= ~clear;
4224 if_getflags(if_t ifp)
4226 return ((struct ifnet *)ifp)->if_flags;
4230 if_clearhwassist(if_t ifp)
4232 ((struct ifnet *)ifp)->if_hwassist = 0;
4237 if_sethwassistbits(if_t ifp, int toset, int toclear)
4239 ((struct ifnet *)ifp)->if_hwassist |= toset;
4240 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4246 if_sethwassist(if_t ifp, int hwassist_bit)
4248 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4253 if_gethwassist(if_t ifp)
4255 return ((struct ifnet *)ifp)->if_hwassist;
4259 if_setmtu(if_t ifp, int mtu)
4261 ((struct ifnet *)ifp)->if_mtu = mtu;
4268 return ((struct ifnet *)ifp)->if_mtu;
4272 if_getmtu_family(if_t ifp, int family)
4276 for (dp = domains; dp; dp = dp->dom_next) {
4277 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4278 return (dp->dom_ifmtu((struct ifnet *)ifp));
4281 return (((struct ifnet *)ifp)->if_mtu);
4285 * Methods for drivers to access interface unicast and multicast
4286 * link level addresses. Driver shall not know 'struct ifaddr' neither
4287 * 'struct ifmultiaddr'.
4290 if_lladdr_count(if_t ifp)
4292 struct epoch_tracker et;
4297 NET_EPOCH_ENTER(et);
4298 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4299 if (ifa->ifa_addr->sa_family == AF_LINK)
4307 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4309 struct epoch_tracker et;
4316 NET_EPOCH_ENTER(et);
4317 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4318 if (ifa->ifa_addr->sa_family != AF_LINK)
4320 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4329 if_llmaddr_count(if_t ifp)
4331 struct epoch_tracker et;
4332 struct ifmultiaddr *ifma;
4336 NET_EPOCH_ENTER(et);
4337 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4338 if (ifma->ifma_addr->sa_family == AF_LINK)
4346 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4348 struct epoch_tracker et;
4349 struct ifmultiaddr *ifma;
4355 NET_EPOCH_ENTER(et);
4356 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4357 if (ifma->ifma_addr->sa_family != AF_LINK)
4359 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4368 if_setsoftc(if_t ifp, void *softc)
4370 ((struct ifnet *)ifp)->if_softc = softc;
4375 if_getsoftc(if_t ifp)
4377 return ((struct ifnet *)ifp)->if_softc;
4381 if_setrcvif(struct mbuf *m, if_t ifp)
4384 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4385 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4389 if_setvtag(struct mbuf *m, uint16_t tag)
4391 m->m_pkthdr.ether_vtag = tag;
4395 if_getvtag(struct mbuf *m)
4398 return (m->m_pkthdr.ether_vtag);
4402 if_sendq_empty(if_t ifp)
4404 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4408 if_getifaddr(if_t ifp)
4410 return ((struct ifnet *)ifp)->if_addr;
4414 if_getamcount(if_t ifp)
4416 return ((struct ifnet *)ifp)->if_amcount;
4420 if_setsendqready(if_t ifp)
4422 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4427 if_setsendqlen(if_t ifp, int tx_desc_count)
4429 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4430 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4436 if_vlantrunkinuse(if_t ifp)
4438 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4442 if_input(if_t ifp, struct mbuf* sendmp)
4444 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4450 if_dequeue(if_t ifp)
4453 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4459 if_sendq_prepend(if_t ifp, struct mbuf *m)
4461 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4466 if_setifheaderlen(if_t ifp, int len)
4468 ((struct ifnet *)ifp)->if_hdrlen = len;
4473 if_getlladdr(if_t ifp)
4475 return (IF_LLADDR((struct ifnet *)ifp));
4479 if_gethandle(u_char type)
4481 return (if_alloc(type));
4485 if_bpfmtap(if_t ifh, struct mbuf *m)
4487 struct ifnet *ifp = (struct ifnet *)ifh;
4493 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4495 struct ifnet *ifp = (struct ifnet *)ifh;
4497 ETHER_BPF_MTAP(ifp, m);
4501 if_vlancap(if_t ifh)
4503 struct ifnet *ifp = (struct ifnet *)ifh;
4504 VLAN_CAPABILITIES(ifp);
4508 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4511 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4516 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4519 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4524 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4527 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4532 if_gethwtsomax(if_t ifp)
4535 return (((struct ifnet *)ifp)->if_hw_tsomax);
4539 if_gethwtsomaxsegcount(if_t ifp)
4542 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4546 if_gethwtsomaxsegsize(if_t ifp)
4549 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4553 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4555 ((struct ifnet *)ifp)->if_init = init_fn;
4559 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4561 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4565 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4567 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4571 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4573 ((struct ifnet *)ifp)->if_transmit = start_fn;
4576 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4578 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4583 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4586 ifp->if_get_counter = fn;
4589 /* Revisit these - These are inline functions originally. */
4591 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4593 return drbr_inuse(ifh, br);
4597 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4599 return drbr_dequeue(ifh, br);
4603 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4605 return drbr_needs_enqueue(ifh, br);
4609 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4611 return drbr_enqueue(ifh, br, m);