2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
35 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
45 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
53 #include <sys/refcount.h>
54 #include <sys/module.h>
55 #include <sys/rwlock.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/taskqueue.h>
61 #include <sys/domain.h>
65 #include <machine/stdarg.h>
69 #include <net/ethernet.h>
71 #include <net/if_arp.h>
72 #include <net/if_clone.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 #include <net/if_media.h>
77 #include <net/if_vlan_var.h>
78 #include <net/radix.h>
79 #include <net/route.h>
82 #if defined(INET) || defined(INET6)
83 #include <net/ethernet.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_carp.h>
89 #include <netinet/if_ether.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/in6_ifattach.h>
95 #endif /* INET || INET6 */
97 #include <security/mac/mac_framework.h>
100 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
101 * and ifr_ifru when it is used in SIOCGIFCONF.
103 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
104 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
106 #ifdef COMPAT_FREEBSD32
107 #include <sys/mount.h>
108 #include <compat/freebsd32/freebsd32.h>
110 struct ifreq_buffer32 {
111 uint32_t length; /* (size_t) */
112 uint32_t buffer; /* (void *) */
116 * Interface request structure used for socket
117 * ioctl's. All interface ioctl's must have parameter
118 * definitions which begin with ifr_name. The
119 * remainder may be interface specific.
122 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
124 struct sockaddr ifru_addr;
125 struct sockaddr ifru_dstaddr;
126 struct sockaddr ifru_broadaddr;
127 struct ifreq_buffer32 ifru_buffer;
138 u_char ifru_vlan_pcp;
141 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
142 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
143 __offsetof(struct ifreq32, ifr_ifru));
145 struct ifgroupreq32 {
146 char ifgr_name[IFNAMSIZ];
149 char ifgru_group[IFNAMSIZ];
150 uint32_t ifgru_groups;
154 struct ifmediareq32 {
155 char ifm_name[IFNAMSIZ];
161 uint32_t ifm_ulist; /* (int *) */
163 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
164 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
166 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
167 case _IOC_NEWTYPE((cmd), struct ifgroupreq32):
168 #else /* !COMPAT_FREEBSD32 */
169 #define _CASE_IOC_IFGROUPREQ_32(cmd)
170 #endif /* !COMPAT_FREEBSD32 */
172 #define CASE_IOC_IFGROUPREQ(cmd) \
173 _CASE_IOC_IFGROUPREQ_32(cmd) \
178 #ifdef COMPAT_FREEBSD32
179 struct ifreq32 ifr32;
183 union ifgroupreq_union {
184 struct ifgroupreq ifgr;
185 #ifdef COMPAT_FREEBSD32
186 struct ifgroupreq32 ifgr32;
190 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
191 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
193 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
194 &ifqmaxlen, 0, "max send queue size");
196 /* Log link state change events */
197 static int log_link_state_change = 1;
199 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
200 &log_link_state_change, 0,
201 "log interface link state change events");
203 /* Log promiscuous mode change events */
204 static int log_promisc_mode_change = 1;
206 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
207 &log_promisc_mode_change, 1,
208 "log promiscuous mode change events");
210 /* Interface description */
211 static unsigned int ifdescr_maxlen = 1024;
212 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
214 "administrative maximum length for interface description");
216 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
218 /* global sx for non-critical path ifdescr */
219 static struct sx ifdescr_sx;
220 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
222 void (*bridge_linkstate_p)(struct ifnet *ifp);
223 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
224 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
225 /* These are external hooks for CARP. */
226 void (*carp_linkstate_p)(struct ifnet *ifp);
227 void (*carp_demote_adj_p)(int, char *);
228 int (*carp_master_p)(struct ifaddr *);
229 #if defined(INET) || defined(INET6)
230 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
231 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
232 const struct sockaddr *sa);
233 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
234 int (*carp_attach_p)(struct ifaddr *, int);
235 void (*carp_detach_p)(struct ifaddr *, bool);
238 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
241 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
242 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
243 const struct in6_addr *taddr);
246 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
249 * XXX: Style; these should be sorted alphabetically, and unprototyped
250 * static functions should be prototyped. Currently they are sorted by
253 static void if_attachdomain(void *);
254 static void if_attachdomain1(struct ifnet *);
255 static int ifconf(u_long, caddr_t);
256 static void if_freemulti(struct ifmultiaddr *);
257 static void if_grow(void);
258 static void if_input_default(struct ifnet *, struct mbuf *);
259 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
260 static void if_route(struct ifnet *, int flag, int fam);
261 static int if_setflag(struct ifnet *, int, int, int *, int);
262 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
263 static void if_unroute(struct ifnet *, int flag, int fam);
264 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
265 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
266 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
267 static void do_link_state_change(void *, int);
268 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
269 static int if_getgroupmembers(struct ifgroupreq *);
270 static void if_delgroups(struct ifnet *);
271 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
272 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
274 static void if_vmove(struct ifnet *, struct vnet *);
279 * XXX: declare here to avoid to include many inet6 related files..
280 * should be more generalized?
282 extern void nd6_setmtu(struct ifnet *);
285 /* ipsec helper hooks */
286 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
287 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
289 VNET_DEFINE(int, if_index);
290 int ifqmaxlen = IFQ_MAXLEN;
291 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
292 VNET_DEFINE(struct ifgrouphead, ifg_head);
294 static VNET_DEFINE(int, if_indexlim) = 8;
296 /* Table of ifnet by index. */
297 VNET_DEFINE(struct ifnet **, ifindex_table);
299 #define V_if_indexlim VNET(if_indexlim)
300 #define V_ifindex_table VNET(ifindex_table)
303 * The global network interface list (V_ifnet) and related state (such as
304 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
305 * an rwlock. Either may be acquired shared to stablize the list, but both
306 * must be acquired writable to modify the list. This model allows us to
307 * both stablize the interface list during interrupt thread processing, but
308 * also to stablize it over long-running ioctls, without introducing priority
309 * inversions and deadlocks.
311 struct rwlock ifnet_rwlock;
312 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
313 struct sx ifnet_sxlock;
314 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
317 * The allocation of network interfaces is a rather non-atomic affair; we
318 * need to select an index before we are ready to expose the interface for
319 * use, so will use this pointer value to indicate reservation.
321 #define IFNET_HOLD (void *)(uintptr_t)(-1)
323 static if_com_alloc_t *if_com_alloc[256];
324 static if_com_free_t *if_com_free[256];
326 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
327 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
328 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
331 ifnet_byindex_locked(u_short idx)
334 if (idx > V_if_index)
336 if (V_ifindex_table[idx] == IFNET_HOLD)
338 return (V_ifindex_table[idx]);
342 ifnet_byindex(u_short idx)
346 IFNET_RLOCK_NOSLEEP();
347 ifp = ifnet_byindex_locked(idx);
348 IFNET_RUNLOCK_NOSLEEP();
353 ifnet_byindex_ref(u_short idx)
357 IFNET_RLOCK_NOSLEEP();
358 ifp = ifnet_byindex_locked(idx);
359 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
360 IFNET_RUNLOCK_NOSLEEP();
364 IFNET_RUNLOCK_NOSLEEP();
369 * Allocate an ifindex array entry; return 0 on success or an error on
377 IFNET_WLOCK_ASSERT();
380 * Try to find an empty slot below V_if_index. If we fail, take the
383 for (idx = 1; idx <= V_if_index; idx++) {
384 if (V_ifindex_table[idx] == NULL)
388 /* Catch if_index overflow. */
389 if (idx >= V_if_indexlim) {
393 if (idx > V_if_index)
399 ifindex_free_locked(u_short idx)
402 IFNET_WLOCK_ASSERT();
404 V_ifindex_table[idx] = NULL;
405 while (V_if_index > 0 &&
406 V_ifindex_table[V_if_index] == NULL)
411 ifindex_free(u_short idx)
415 ifindex_free_locked(idx);
420 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
423 IFNET_WLOCK_ASSERT();
425 V_ifindex_table[idx] = ifp;
429 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
433 ifnet_setbyindex_locked(idx, ifp);
438 ifaddr_byindex(u_short idx)
441 struct ifaddr *ifa = NULL;
443 IFNET_RLOCK_NOSLEEP();
444 ifp = ifnet_byindex_locked(idx);
445 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
447 IFNET_RUNLOCK_NOSLEEP();
452 * Network interface utility routines.
454 * Routines with ifa_ifwith* names take sockaddr *'s as
459 vnet_if_init(const void *unused __unused)
462 TAILQ_INIT(&V_ifnet);
463 TAILQ_INIT(&V_ifg_head);
465 if_grow(); /* create initial table */
467 vnet_if_clone_init();
469 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
474 vnet_if_uninit(const void *unused __unused)
477 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
478 "not empty", __func__, __LINE__, &V_ifnet));
479 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
480 "not empty", __func__, __LINE__, &V_ifg_head));
482 free((caddr_t)V_ifindex_table, M_IFNET);
484 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
485 vnet_if_uninit, NULL);
488 vnet_if_return(const void *unused __unused)
490 struct ifnet *ifp, *nifp;
492 /* Return all inherited interfaces to their parent vnets. */
493 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
494 if (ifp->if_home_vnet != ifp->if_vnet)
495 if_vmove(ifp, ifp->if_home_vnet);
498 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
499 vnet_if_return, NULL);
509 IFNET_WLOCK_ASSERT();
510 oldlim = V_if_indexlim;
512 n = (oldlim << 1) * sizeof(*e);
513 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
515 if (V_if_indexlim != oldlim) {
519 if (V_ifindex_table != NULL) {
520 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
521 free((caddr_t)V_ifindex_table, M_IFNET);
528 * Allocate a struct ifnet and an index for an interface. A layer 2
529 * common structure will also be allocated if an allocation routine is
530 * registered for the passed type.
533 if_alloc(u_char type)
538 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
540 idx = ifindex_alloc();
541 ifnet_setbyindex_locked(idx, IFNET_HOLD);
545 ifp->if_alloctype = type;
547 ifp->if_vnet = curvnet;
549 if (if_com_alloc[type] != NULL) {
550 ifp->if_l2com = if_com_alloc[type](type, ifp);
551 if (ifp->if_l2com == NULL) {
558 IF_ADDR_LOCK_INIT(ifp);
559 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
560 ifp->if_afdata_initialized = 0;
561 IF_AFDATA_LOCK_INIT(ifp);
562 TAILQ_INIT(&ifp->if_addrhead);
563 TAILQ_INIT(&ifp->if_multiaddrs);
564 TAILQ_INIT(&ifp->if_groups);
568 ifq_init(&ifp->if_snd, ifp);
570 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
571 for (int i = 0; i < IFCOUNTERS; i++)
572 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
573 ifp->if_get_counter = if_get_counter_default;
574 ifp->if_pcp = IFNET_PCP_NONE;
575 ifnet_setbyindex(ifp->if_index, ifp);
580 * Do the actual work of freeing a struct ifnet, and layer 2 common
581 * structure. This call is made when the last reference to an
582 * interface is released.
585 if_free_internal(struct ifnet *ifp)
588 KASSERT((ifp->if_flags & IFF_DYING),
589 ("if_free_internal: interface not dying"));
591 if (if_com_free[ifp->if_alloctype] != NULL)
592 if_com_free[ifp->if_alloctype](ifp->if_l2com,
596 mac_ifnet_destroy(ifp);
598 if (ifp->if_description != NULL)
599 free(ifp->if_description, M_IFDESCR);
600 IF_AFDATA_DESTROY(ifp);
601 IF_ADDR_LOCK_DESTROY(ifp);
602 ifq_delete(&ifp->if_snd);
604 for (int i = 0; i < IFCOUNTERS; i++)
605 counter_u64_free(ifp->if_counters[i]);
611 * Deregister an interface and free the associated storage.
614 if_free(struct ifnet *ifp)
617 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
619 CURVNET_SET_QUIET(ifp->if_vnet);
621 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
622 ("%s: freeing unallocated ifnet", ifp->if_xname));
624 ifindex_free_locked(ifp->if_index);
627 if (refcount_release(&ifp->if_refcount))
628 if_free_internal(ifp);
633 * Interfaces to keep an ifnet type-stable despite the possibility of the
634 * driver calling if_free(). If there are additional references, we defer
635 * freeing the underlying data structure.
638 if_ref(struct ifnet *ifp)
641 /* We don't assert the ifnet list lock here, but arguably should. */
642 refcount_acquire(&ifp->if_refcount);
646 if_rele(struct ifnet *ifp)
649 if (!refcount_release(&ifp->if_refcount))
651 if_free_internal(ifp);
655 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
658 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
660 if (ifq->ifq_maxlen == 0)
661 ifq->ifq_maxlen = ifqmaxlen;
664 ifq->altq_disc = NULL;
665 ifq->altq_flags &= ALTQF_CANTCHANGE;
666 ifq->altq_tbr = NULL;
671 ifq_delete(struct ifaltq *ifq)
673 mtx_destroy(&ifq->ifq_mtx);
677 * Perform generic interface initialization tasks and attach the interface
678 * to the list of "active" interfaces. If vmove flag is set on entry
679 * to if_attach_internal(), perform only a limited subset of initialization
680 * tasks, given that we are moving from one vnet to another an ifnet which
681 * has already been fully initialized.
683 * Note that if_detach_internal() removes group membership unconditionally
684 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
685 * Thus, when if_vmove() is applied to a cloned interface, group membership
686 * is lost while a cloned one always joins a group whose name is
687 * ifc->ifc_name. To recover this after if_detach_internal() and
688 * if_attach_internal(), the cloner should be specified to
689 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
690 * attempts to join a group whose name is ifc->ifc_name.
693 * - The decision to return void and thus require this function to
694 * succeed is questionable.
695 * - We should probably do more sanity checking. For instance we don't
696 * do anything to insure if_xname is unique or non-empty.
699 if_attach(struct ifnet *ifp)
702 if_attach_internal(ifp, 0, NULL);
706 * Compute the least common TSO limit.
709 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
712 * 1) If there is no limit currently, take the limit from
713 * the network adapter.
715 * 2) If the network adapter has a limit below the current
718 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
719 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
720 pmax->tsomaxbytes = ifp->if_hw_tsomax;
722 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
723 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
724 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
726 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
727 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
728 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
733 * Update TSO limit of a network adapter.
735 * Returns zero if no change. Else non-zero.
738 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
741 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
742 ifp->if_hw_tsomax = pmax->tsomaxbytes;
745 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
746 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
749 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
750 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
757 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
759 unsigned socksize, ifasize;
760 int namelen, masklen;
761 struct sockaddr_dl *sdl;
764 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
765 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
769 ifp->if_vnet = curvnet;
770 if (ifp->if_home_vnet == NULL)
771 ifp->if_home_vnet = curvnet;
774 if_addgroup(ifp, IFG_ALL);
776 /* Restore group membership for cloned interfaces. */
777 if (vmove && ifc != NULL)
778 if_clone_addgroup(ifp, ifc);
780 getmicrotime(&ifp->if_lastchange);
781 ifp->if_epoch = time_uptime;
783 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
784 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
785 ("transmit and qflush must both either be set or both be NULL"));
786 if (ifp->if_transmit == NULL) {
787 ifp->if_transmit = if_transmit;
788 ifp->if_qflush = if_qflush;
790 if (ifp->if_input == NULL)
791 ifp->if_input = if_input_default;
793 if (ifp->if_requestencap == NULL)
794 ifp->if_requestencap = if_requestencap_default;
798 mac_ifnet_create(ifp);
802 * Create a Link Level name for this device.
804 namelen = strlen(ifp->if_xname);
806 * Always save enough space for any possiable name so we
807 * can do a rename in place later.
809 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
810 socksize = masklen + ifp->if_addrlen;
811 if (socksize < sizeof(*sdl))
812 socksize = sizeof(*sdl);
813 socksize = roundup2(socksize, sizeof(long));
814 ifasize = sizeof(*ifa) + 2 * socksize;
815 ifa = ifa_alloc(ifasize, M_WAITOK);
816 sdl = (struct sockaddr_dl *)(ifa + 1);
817 sdl->sdl_len = socksize;
818 sdl->sdl_family = AF_LINK;
819 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
820 sdl->sdl_nlen = namelen;
821 sdl->sdl_index = ifp->if_index;
822 sdl->sdl_type = ifp->if_type;
825 ifa->ifa_rtrequest = link_rtrequest;
826 ifa->ifa_addr = (struct sockaddr *)sdl;
827 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
828 ifa->ifa_netmask = (struct sockaddr *)sdl;
829 sdl->sdl_len = masklen;
831 sdl->sdl_data[--namelen] = 0xff;
832 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
833 /* Reliably crash if used uninitialized. */
834 ifp->if_broadcastaddr = NULL;
836 if (ifp->if_type == IFT_ETHER) {
837 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
841 #if defined(INET) || defined(INET6)
842 /* Use defaults for TSO, if nothing is set */
843 if (ifp->if_hw_tsomax == 0 &&
844 ifp->if_hw_tsomaxsegcount == 0 &&
845 ifp->if_hw_tsomaxsegsize == 0) {
847 * The TSO defaults needs to be such that an
848 * NFS mbuf list of 35 mbufs totalling just
849 * below 64K works and that a chain of mbufs
850 * can be defragged into at most 32 segments:
852 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
853 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
854 ifp->if_hw_tsomaxsegcount = 35;
855 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
857 /* XXX some drivers set IFCAP_TSO after ethernet attach */
858 if (ifp->if_capabilities & IFCAP_TSO) {
859 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
861 ifp->if_hw_tsomaxsegcount,
862 ifp->if_hw_tsomaxsegsize);
870 * Update the interface index in the link layer address
873 for (ifa = ifp->if_addr; ifa != NULL;
874 ifa = TAILQ_NEXT(ifa, ifa_link)) {
875 if (ifa->ifa_addr->sa_family == AF_LINK) {
876 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
877 sdl->sdl_index = ifp->if_index;
884 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
886 curvnet->vnet_ifcnt++;
890 if (domain_init_status >= 2)
891 if_attachdomain1(ifp);
893 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
894 if (IS_DEFAULT_VNET(curvnet))
895 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
897 /* Announce the interface. */
898 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
902 if_attachdomain(void *dummy)
906 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
907 if_attachdomain1(ifp);
909 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
910 if_attachdomain, NULL);
913 if_attachdomain1(struct ifnet *ifp)
918 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
919 * cannot lock ifp->if_afdata initialization, entirely.
922 if (ifp->if_afdata_initialized >= domain_init_status) {
923 IF_AFDATA_UNLOCK(ifp);
924 log(LOG_WARNING, "%s called more than once on %s\n",
925 __func__, ifp->if_xname);
928 ifp->if_afdata_initialized = domain_init_status;
929 IF_AFDATA_UNLOCK(ifp);
931 /* address family dependent data region */
932 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
933 for (dp = domains; dp; dp = dp->dom_next) {
934 if (dp->dom_ifattach)
935 ifp->if_afdata[dp->dom_family] =
936 (*dp->dom_ifattach)(ifp);
941 * Remove any unicast or broadcast network addresses from an interface.
944 if_purgeaddrs(struct ifnet *ifp)
946 struct ifaddr *ifa, *next;
948 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
949 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
950 if (ifa->ifa_addr->sa_family == AF_LINK)
953 /* XXX: Ugly!! ad hoc just for INET */
954 if (ifa->ifa_addr->sa_family == AF_INET) {
955 struct ifaliasreq ifr;
957 bzero(&ifr, sizeof(ifr));
958 ifr.ifra_addr = *ifa->ifa_addr;
959 if (ifa->ifa_dstaddr)
960 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
961 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
967 if (ifa->ifa_addr->sa_family == AF_INET6) {
969 /* ifp_addrhead is already updated */
974 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
975 IF_ADDR_WUNLOCK(ifp);
981 * Remove any multicast network addresses from an interface when an ifnet
985 if_purgemaddrs(struct ifnet *ifp)
987 struct ifmultiaddr *ifma;
990 while (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
991 ifma = TAILQ_FIRST(&ifp->if_multiaddrs);
992 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
993 if_delmulti_locked(ifp, ifma, 1);
995 IF_ADDR_WUNLOCK(ifp);
999 * Detach an interface, removing it from the list of "active" interfaces.
1000 * If vmove flag is set on entry to if_detach_internal(), perform only a
1001 * limited subset of cleanup tasks, given that we are moving an ifnet from
1002 * one vnet to another, where it must be fully operational.
1004 * XXXRW: There are some significant questions about event ordering, and
1005 * how to prevent things from starting to use the interface during detach.
1008 if_detach(struct ifnet *ifp)
1011 CURVNET_SET_QUIET(ifp->if_vnet);
1012 if_detach_internal(ifp, 0, NULL);
1017 * The vmove flag, if set, indicates that we are called from a callpath
1018 * that is moving an interface to a different vnet instance.
1020 * The shutdown flag, if set, indicates that we are called in the
1021 * process of shutting down a vnet instance. Currently only the
1022 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1023 * on a vnet instance shutdown without this flag being set, e.g., when
1024 * the cloned interfaces are destoyed as first thing of teardown.
1027 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1037 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1038 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1041 TAILQ_FOREACH(iter, &V_ifnet, if_link)
1043 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
1050 * While we would want to panic here, we cannot
1051 * guarantee that the interface is indeed still on
1052 * the list given we don't hold locks all the way.
1057 panic("%s: ifp=%p not on the ifnet tailq %p",
1058 __func__, ifp, &V_ifnet);
1060 return; /* XXX this should panic as well? */
1065 * At this point we know the interface still was on the ifnet list
1066 * and we removed it so we are in a stable state.
1069 curvnet->vnet_ifcnt--;
1073 * In any case (destroy or vmove) detach us from the groups
1074 * and remove/wait for pending events on the taskq.
1075 * XXX-BZ in theory an interface could still enqueue a taskq change?
1079 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1082 * Check if this is a cloned interface or not. Must do even if
1083 * shutting down as a if_vmove_reclaim() would move the ifp and
1084 * the if_clone_addgroup() will have a corrupted string overwise
1085 * from a gibberish pointer.
1087 if (vmove && ifcp != NULL)
1088 *ifcp = if_clone_findifc(ifp);
1094 * On VNET shutdown abort here as the stack teardown will do all
1095 * the work top-down for us.
1099 * In case of a vmove we are done here without error.
1100 * If we would signal an error it would lead to the same
1101 * abort as if we did not find the ifnet anymore.
1102 * if_detach() calls us in void context and does not care
1103 * about an early abort notification, so life is splendid :)
1105 goto finish_vnet_shutdown;
1110 * At this point we are not tearing down a VNET and are either
1111 * going to destroy or vmove the interface and have to cleanup
1116 * Remove routes and flush queues.
1119 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1120 altq_disable(&ifp->if_snd);
1121 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1122 altq_detach(&ifp->if_snd);
1133 * Remove all IPv6 kernel structs related to ifp. This should be done
1134 * before removing routing entries below, since IPv6 interface direct
1135 * routes are expected to be removed by the IPv6-specific kernel API.
1136 * Otherwise, the kernel will detect some inconsistency and bark it.
1140 if_purgemaddrs(ifp);
1142 /* Announce that the interface is gone. */
1143 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1144 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1145 if (IS_DEFAULT_VNET(curvnet))
1146 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1150 * Prevent further calls into the device driver via ifnet.
1155 * Remove link ifaddr pointer and maybe decrement if_index.
1156 * Clean up all addresses.
1158 free(ifp->if_hw_addr, M_IFADDR);
1159 ifp->if_hw_addr = NULL;
1160 ifp->if_addr = NULL;
1162 /* We can now free link ifaddr. */
1164 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1165 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1166 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1167 IF_ADDR_WUNLOCK(ifp);
1170 IF_ADDR_WUNLOCK(ifp);
1173 rt_flushifroutes(ifp);
1176 finish_vnet_shutdown:
1179 * We cannot hold the lock over dom_ifdetach calls as they might
1180 * sleep, for example trying to drain a callout, thus open up the
1181 * theoretical race with re-attaching.
1183 IF_AFDATA_LOCK(ifp);
1184 i = ifp->if_afdata_initialized;
1185 ifp->if_afdata_initialized = 0;
1186 IF_AFDATA_UNLOCK(ifp);
1187 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1188 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1189 (*dp->dom_ifdetach)(ifp,
1190 ifp->if_afdata[dp->dom_family]);
1191 ifp->if_afdata[dp->dom_family] = NULL;
1200 * if_vmove() performs a limited version of if_detach() in current
1201 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1202 * An attempt is made to shrink if_index in current vnet, find an
1203 * unused if_index in target vnet and calls if_grow() if necessary,
1204 * and finally find an unused if_xname for the target vnet.
1207 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1209 struct if_clone *ifc;
1210 u_int bif_dlt, bif_hdrlen;
1214 * if_detach_internal() will call the eventhandler to notify
1215 * interface departure. That will detach if_bpf. We need to
1216 * safe the dlt and hdrlen so we can re-attach it later.
1218 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1221 * Detach from current vnet, but preserve LLADDR info, do not
1222 * mark as dead etc. so that the ifnet can be reattached later.
1223 * If we cannot find it, we lost the race to someone else.
1225 rc = if_detach_internal(ifp, 1, &ifc);
1230 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1231 * the if_index for that vnet if possible.
1233 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1234 * or we'd lock on one vnet and unlock on another.
1237 ifindex_free_locked(ifp->if_index);
1241 * Perform interface-specific reassignment tasks, if provided by
1244 if (ifp->if_reassign != NULL)
1245 ifp->if_reassign(ifp, new_vnet, NULL);
1248 * Switch to the context of the target vnet.
1250 CURVNET_SET_QUIET(new_vnet);
1253 ifp->if_index = ifindex_alloc();
1254 ifnet_setbyindex_locked(ifp->if_index, ifp);
1257 if_attach_internal(ifp, 1, ifc);
1259 if (ifp->if_bpf == NULL)
1260 bpfattach(ifp, bif_dlt, bif_hdrlen);
1266 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1269 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1275 /* Try to find the prison within our visibility. */
1276 sx_slock(&allprison_lock);
1277 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1278 sx_sunlock(&allprison_lock);
1281 prison_hold_locked(pr);
1282 mtx_unlock(&pr->pr_mtx);
1284 /* Do not try to move the iface from and to the same prison. */
1285 if (pr->pr_vnet == ifp->if_vnet) {
1290 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1291 /* XXX Lock interfaces to avoid races. */
1292 CURVNET_SET_QUIET(pr->pr_vnet);
1293 difp = ifunit(ifname);
1300 /* Make sure the VNET is stable. */
1301 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1302 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1310 /* Move the interface into the child jail/vnet. */
1311 if_vmove(ifp, pr->pr_vnet);
1313 /* Report the new if_xname back to the userland. */
1314 sprintf(ifname, "%s", ifp->if_xname);
1321 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1324 struct vnet *vnet_dst;
1328 /* Try to find the prison within our visibility. */
1329 sx_slock(&allprison_lock);
1330 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1331 sx_sunlock(&allprison_lock);
1334 prison_hold_locked(pr);
1335 mtx_unlock(&pr->pr_mtx);
1337 /* Make sure the named iface exists in the source prison/vnet. */
1338 CURVNET_SET(pr->pr_vnet);
1339 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1346 /* Do not try to move the iface from and to the same prison. */
1347 vnet_dst = TD_TO_VNET(td);
1348 if (vnet_dst == ifp->if_vnet) {
1354 /* Make sure the VNET is stable. */
1355 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1356 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1363 /* Get interface back from child jail/vnet. */
1364 if_vmove(ifp, vnet_dst);
1367 /* Report the new if_xname back to the userland. */
1368 sprintf(ifname, "%s", ifp->if_xname);
1376 * Add a group to an interface
1379 if_addgroup(struct ifnet *ifp, const char *groupname)
1381 struct ifg_list *ifgl;
1382 struct ifg_group *ifg = NULL;
1383 struct ifg_member *ifgm;
1386 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1387 groupname[strlen(groupname) - 1] <= '9')
1391 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1392 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1397 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1398 M_NOWAIT)) == NULL) {
1403 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1404 M_TEMP, M_NOWAIT)) == NULL) {
1410 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1411 if (!strcmp(ifg->ifg_group, groupname))
1415 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1416 M_TEMP, M_NOWAIT)) == NULL) {
1422 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1423 ifg->ifg_refcnt = 0;
1424 TAILQ_INIT(&ifg->ifg_members);
1425 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1430 ifgl->ifgl_group = ifg;
1431 ifgm->ifgm_ifp = ifp;
1434 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1435 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1436 IF_ADDR_WUNLOCK(ifp);
1441 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1442 EVENTHANDLER_INVOKE(group_change_event, groupname);
1448 * Remove a group from an interface
1451 if_delgroup(struct ifnet *ifp, const char *groupname)
1453 struct ifg_list *ifgl;
1454 struct ifg_member *ifgm;
1457 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1458 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1466 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1467 IF_ADDR_WUNLOCK(ifp);
1469 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1470 if (ifgm->ifgm_ifp == ifp)
1474 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1478 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1479 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1481 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1482 free(ifgl->ifgl_group, M_TEMP);
1488 EVENTHANDLER_INVOKE(group_change_event, groupname);
1494 * Remove an interface from all groups
1497 if_delgroups(struct ifnet *ifp)
1499 struct ifg_list *ifgl;
1500 struct ifg_member *ifgm;
1501 char groupname[IFNAMSIZ];
1504 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1505 ifgl = TAILQ_FIRST(&ifp->if_groups);
1507 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1510 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1511 IF_ADDR_WUNLOCK(ifp);
1513 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1514 if (ifgm->ifgm_ifp == ifp)
1518 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1523 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1524 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1526 EVENTHANDLER_INVOKE(group_detach_event,
1528 free(ifgl->ifgl_group, M_TEMP);
1534 EVENTHANDLER_INVOKE(group_change_event, groupname);
1542 ifgr_group_get(void *ifgrp)
1544 union ifgroupreq_union *ifgrup;
1547 #ifdef COMPAT_FREEBSD32
1548 if (SV_CURPROC_FLAG(SV_ILP32))
1549 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1551 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1554 static struct ifg_req *
1555 ifgr_groups_get(void *ifgrp)
1557 union ifgroupreq_union *ifgrup;
1560 #ifdef COMPAT_FREEBSD32
1561 if (SV_CURPROC_FLAG(SV_ILP32))
1562 return ((struct ifg_req *)(uintptr_t)
1563 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1565 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1569 * Stores all groups from an interface in memory pointed to by ifgr.
1572 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1575 struct ifg_list *ifgl;
1576 struct ifg_req ifgrq, *ifgp;
1578 if (ifgr->ifgr_len == 0) {
1580 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1581 ifgr->ifgr_len += sizeof(struct ifg_req);
1582 IF_ADDR_RUNLOCK(ifp);
1586 len = ifgr->ifgr_len;
1587 ifgp = ifgr_groups_get(ifgr);
1590 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1591 if (len < sizeof(ifgrq)) {
1592 IF_ADDR_RUNLOCK(ifp);
1595 bzero(&ifgrq, sizeof ifgrq);
1596 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1597 sizeof(ifgrq.ifgrq_group));
1598 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1599 IF_ADDR_RUNLOCK(ifp);
1602 len -= sizeof(ifgrq);
1605 IF_ADDR_RUNLOCK(ifp);
1611 * Stores all members of a group in memory pointed to by igfr
1614 if_getgroupmembers(struct ifgroupreq *ifgr)
1616 struct ifg_group *ifg;
1617 struct ifg_member *ifgm;
1618 struct ifg_req ifgrq, *ifgp;
1622 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1623 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1630 if (ifgr->ifgr_len == 0) {
1631 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1632 ifgr->ifgr_len += sizeof(ifgrq);
1637 len = ifgr->ifgr_len;
1638 ifgp = ifgr_groups_get(ifgr);
1639 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1640 if (len < sizeof(ifgrq)) {
1644 bzero(&ifgrq, sizeof ifgrq);
1645 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1646 sizeof(ifgrq.ifgrq_member));
1647 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1651 len -= sizeof(ifgrq);
1660 * Return counter values from counter(9)s stored in ifnet.
1663 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1666 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1668 return (counter_u64_fetch(ifp->if_counters[cnt]));
1672 * Increase an ifnet counter. Usually used for counters shared
1673 * between the stack and a driver, but function supports them all.
1676 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1679 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1681 counter_u64_add(ifp->if_counters[cnt], inc);
1685 * Copy data from ifnet to userland API structure if_data.
1688 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1691 ifd->ifi_type = ifp->if_type;
1692 ifd->ifi_physical = 0;
1693 ifd->ifi_addrlen = ifp->if_addrlen;
1694 ifd->ifi_hdrlen = ifp->if_hdrlen;
1695 ifd->ifi_link_state = ifp->if_link_state;
1697 ifd->ifi_datalen = sizeof(struct if_data);
1698 ifd->ifi_mtu = ifp->if_mtu;
1699 ifd->ifi_metric = ifp->if_metric;
1700 ifd->ifi_baudrate = ifp->if_baudrate;
1701 ifd->ifi_hwassist = ifp->if_hwassist;
1702 ifd->ifi_epoch = ifp->if_epoch;
1703 ifd->ifi_lastchange = ifp->if_lastchange;
1705 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1706 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1707 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1708 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1709 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1710 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1711 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1712 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1713 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1714 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1715 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1716 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1720 * Wrapper functions for struct ifnet address list locking macros. These are
1721 * used by kernel modules to avoid encoding programming interface or binary
1722 * interface assumptions that may be violated when kernel-internal locking
1723 * approaches change.
1726 if_addr_rlock(struct ifnet *ifp)
1733 if_addr_runlock(struct ifnet *ifp)
1736 IF_ADDR_RUNLOCK(ifp);
1740 if_maddr_rlock(if_t ifp)
1743 IF_ADDR_RLOCK((struct ifnet *)ifp);
1747 if_maddr_runlock(if_t ifp)
1750 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1754 * Initialization, destruction and refcounting functions for ifaddrs.
1757 ifa_alloc(size_t size, int flags)
1761 KASSERT(size >= sizeof(struct ifaddr),
1762 ("%s: invalid size %zu", __func__, size));
1764 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1768 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1770 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1772 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1774 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1777 refcount_init(&ifa->ifa_refcnt, 1);
1782 /* free(NULL) is okay */
1783 counter_u64_free(ifa->ifa_opackets);
1784 counter_u64_free(ifa->ifa_ipackets);
1785 counter_u64_free(ifa->ifa_obytes);
1786 counter_u64_free(ifa->ifa_ibytes);
1787 free(ifa, M_IFADDR);
1793 ifa_ref(struct ifaddr *ifa)
1796 refcount_acquire(&ifa->ifa_refcnt);
1800 ifa_free(struct ifaddr *ifa)
1803 if (refcount_release(&ifa->ifa_refcnt)) {
1804 counter_u64_free(ifa->ifa_opackets);
1805 counter_u64_free(ifa->ifa_ipackets);
1806 counter_u64_free(ifa->ifa_obytes);
1807 counter_u64_free(ifa->ifa_ibytes);
1808 free(ifa, M_IFADDR);
1813 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1814 struct sockaddr *ia)
1817 struct rt_addrinfo info;
1818 struct sockaddr_dl null_sdl;
1823 bzero(&info, sizeof(info));
1824 if (cmd != RTM_DELETE)
1825 info.rti_ifp = V_loif;
1826 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1827 info.rti_info[RTAX_DST] = ia;
1828 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1829 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1831 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1834 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1835 __func__, otype, if_name(ifp), error);
1841 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1844 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1848 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1851 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1855 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1858 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1862 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1863 * structs used to represent other address families, it is necessary
1864 * to perform a different comparison.
1867 #define sa_dl_equal(a1, a2) \
1868 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1869 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1870 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1871 CLLADDR((const struct sockaddr_dl *)(a2)), \
1872 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1875 * Locate an interface based on a complete address.
1878 static struct ifaddr *
1879 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1884 IFNET_RLOCK_NOSLEEP();
1885 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1887 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1888 if (ifa->ifa_addr->sa_family != addr->sa_family)
1890 if (sa_equal(addr, ifa->ifa_addr)) {
1893 IF_ADDR_RUNLOCK(ifp);
1896 /* IP6 doesn't have broadcast */
1897 if ((ifp->if_flags & IFF_BROADCAST) &&
1898 ifa->ifa_broadaddr &&
1899 ifa->ifa_broadaddr->sa_len != 0 &&
1900 sa_equal(ifa->ifa_broadaddr, addr)) {
1903 IF_ADDR_RUNLOCK(ifp);
1907 IF_ADDR_RUNLOCK(ifp);
1911 IFNET_RUNLOCK_NOSLEEP();
1916 ifa_ifwithaddr(const struct sockaddr *addr)
1919 return (ifa_ifwithaddr_internal(addr, 1));
1923 ifa_ifwithaddr_check(const struct sockaddr *addr)
1926 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1930 * Locate an interface based on the broadcast address.
1934 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1939 IFNET_RLOCK_NOSLEEP();
1940 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1941 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1944 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1945 if (ifa->ifa_addr->sa_family != addr->sa_family)
1947 if ((ifp->if_flags & IFF_BROADCAST) &&
1948 ifa->ifa_broadaddr &&
1949 ifa->ifa_broadaddr->sa_len != 0 &&
1950 sa_equal(ifa->ifa_broadaddr, addr)) {
1952 IF_ADDR_RUNLOCK(ifp);
1956 IF_ADDR_RUNLOCK(ifp);
1960 IFNET_RUNLOCK_NOSLEEP();
1965 * Locate the point to point interface with a given destination address.
1969 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1974 IFNET_RLOCK_NOSLEEP();
1975 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1976 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1978 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1981 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1982 if (ifa->ifa_addr->sa_family != addr->sa_family)
1984 if (ifa->ifa_dstaddr != NULL &&
1985 sa_equal(addr, ifa->ifa_dstaddr)) {
1987 IF_ADDR_RUNLOCK(ifp);
1991 IF_ADDR_RUNLOCK(ifp);
1995 IFNET_RUNLOCK_NOSLEEP();
2000 * Find an interface on a specific network. If many, choice
2001 * is most specific found.
2004 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2008 struct ifaddr *ifa_maybe = NULL;
2009 u_int af = addr->sa_family;
2010 const char *addr_data = addr->sa_data, *cplim;
2013 * AF_LINK addresses can be looked up directly by their index number,
2014 * so do that if we can.
2016 if (af == AF_LINK) {
2017 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2018 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2019 return (ifaddr_byindex(sdl->sdl_index));
2023 * Scan though each interface, looking for ones that have addresses
2024 * in this address family and the requested fib. Maintain a reference
2025 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2026 * kept it stable when we move onto the next interface.
2028 IFNET_RLOCK_NOSLEEP();
2029 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2030 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2033 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2034 const char *cp, *cp2, *cp3;
2036 if (ifa->ifa_addr->sa_family != af)
2038 if (af == AF_INET &&
2039 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2041 * This is a bit broken as it doesn't
2042 * take into account that the remote end may
2043 * be a single node in the network we are
2045 * The trouble is that we don't know the
2046 * netmask for the remote end.
2048 if (ifa->ifa_dstaddr != NULL &&
2049 sa_equal(addr, ifa->ifa_dstaddr)) {
2051 IF_ADDR_RUNLOCK(ifp);
2056 * Scan all the bits in the ifa's address.
2057 * If a bit dissagrees with what we are
2058 * looking for, mask it with the netmask
2059 * to see if it really matters.
2060 * (A byte at a time)
2062 if (ifa->ifa_netmask == 0)
2065 cp2 = ifa->ifa_addr->sa_data;
2066 cp3 = ifa->ifa_netmask->sa_data;
2067 cplim = ifa->ifa_netmask->sa_len
2068 + (char *)ifa->ifa_netmask;
2070 if ((*cp++ ^ *cp2++) & *cp3++)
2071 goto next; /* next address! */
2073 * If the netmask of what we just found
2074 * is more specific than what we had before
2075 * (if we had one), or if the virtual status
2076 * of new prefix is better than of the old one,
2077 * then remember the new one before continuing
2078 * to search for an even better one.
2080 if (ifa_maybe == NULL ||
2081 ifa_preferred(ifa_maybe, ifa) ||
2082 rn_refines((caddr_t)ifa->ifa_netmask,
2083 (caddr_t)ifa_maybe->ifa_netmask)) {
2084 if (ifa_maybe != NULL)
2085 ifa_free(ifa_maybe);
2091 IF_ADDR_RUNLOCK(ifp);
2096 IFNET_RUNLOCK_NOSLEEP();
2097 if (ifa_maybe != NULL)
2098 ifa_free(ifa_maybe);
2103 * Find an interface address specific to an interface best matching
2107 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2110 const char *cp, *cp2, *cp3;
2112 struct ifaddr *ifa_maybe = NULL;
2113 u_int af = addr->sa_family;
2118 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2119 if (ifa->ifa_addr->sa_family != af)
2121 if (ifa_maybe == NULL)
2123 if (ifa->ifa_netmask == 0) {
2124 if (sa_equal(addr, ifa->ifa_addr) ||
2125 (ifa->ifa_dstaddr &&
2126 sa_equal(addr, ifa->ifa_dstaddr)))
2130 if (ifp->if_flags & IFF_POINTOPOINT) {
2131 if (sa_equal(addr, ifa->ifa_dstaddr))
2135 cp2 = ifa->ifa_addr->sa_data;
2136 cp3 = ifa->ifa_netmask->sa_data;
2137 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2138 for (; cp3 < cplim; cp3++)
2139 if ((*cp++ ^ *cp2++) & *cp3)
2149 IF_ADDR_RUNLOCK(ifp);
2154 * See whether new ifa is better than current one:
2155 * 1) A non-virtual one is preferred over virtual.
2156 * 2) A virtual in master state preferred over any other state.
2158 * Used in several address selecting functions.
2161 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2164 return (cur->ifa_carp && (!next->ifa_carp ||
2165 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2168 #include <net/if_llatbl.h>
2171 * Default action when installing a route with a Link Level gateway.
2172 * Lookup an appropriate real ifa to point to.
2173 * This should be moved to /sys/net/link.c eventually.
2176 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2178 struct ifaddr *ifa, *oifa;
2179 struct sockaddr *dst;
2182 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2183 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2185 ifa = ifaof_ifpforaddr(dst, ifp);
2190 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2191 ifa->ifa_rtrequest(cmd, rt, info);
2195 struct sockaddr_dl *
2196 link_alloc_sdl(size_t size, int flags)
2199 return (malloc(size, M_TEMP, flags));
2203 link_free_sdl(struct sockaddr *sa)
2209 * Fills in given sdl with interface basic info.
2210 * Returns pointer to filled sdl.
2212 struct sockaddr_dl *
2213 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2215 struct sockaddr_dl *sdl;
2217 sdl = (struct sockaddr_dl *)paddr;
2218 memset(sdl, 0, sizeof(struct sockaddr_dl));
2219 sdl->sdl_len = sizeof(struct sockaddr_dl);
2220 sdl->sdl_family = AF_LINK;
2221 sdl->sdl_index = ifp->if_index;
2222 sdl->sdl_type = iftype;
2228 * Mark an interface down and notify protocols of
2232 if_unroute(struct ifnet *ifp, int flag, int fam)
2236 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2238 ifp->if_flags &= ~flag;
2239 getmicrotime(&ifp->if_lastchange);
2240 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2241 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2242 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2243 ifp->if_qflush(ifp);
2246 (*carp_linkstate_p)(ifp);
2251 * Mark an interface up and notify protocols of
2255 if_route(struct ifnet *ifp, int flag, int fam)
2259 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2261 ifp->if_flags |= flag;
2262 getmicrotime(&ifp->if_lastchange);
2263 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2264 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2265 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2267 (*carp_linkstate_p)(ifp);
2274 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2275 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2276 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2277 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2278 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2279 int (*vlan_setcookie_p)(struct ifnet *, void *);
2280 void *(*vlan_cookie_p)(struct ifnet *);
2283 * Handle a change in the interface link state. To avoid LORs
2284 * between driver lock and upper layer locks, as well as possible
2285 * recursions, we post event to taskqueue, and all job
2286 * is done in static do_link_state_change().
2289 if_link_state_change(struct ifnet *ifp, int link_state)
2291 /* Return if state hasn't changed. */
2292 if (ifp->if_link_state == link_state)
2295 ifp->if_link_state = link_state;
2297 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2301 do_link_state_change(void *arg, int pending)
2303 struct ifnet *ifp = (struct ifnet *)arg;
2304 int link_state = ifp->if_link_state;
2305 CURVNET_SET(ifp->if_vnet);
2307 /* Notify that the link state has changed. */
2309 if (ifp->if_vlantrunk != NULL)
2310 (*vlan_link_state_p)(ifp);
2312 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2313 ifp->if_l2com != NULL)
2314 (*ng_ether_link_state_p)(ifp, link_state);
2316 (*carp_linkstate_p)(ifp);
2318 (*bridge_linkstate_p)(ifp);
2320 (*lagg_linkstate_p)(ifp, link_state);
2322 if (IS_DEFAULT_VNET(curvnet))
2323 devctl_notify("IFNET", ifp->if_xname,
2324 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2327 if_printf(ifp, "%d link states coalesced\n", pending);
2328 if (log_link_state_change)
2329 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2330 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2331 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2336 * Mark an interface down and notify protocols of
2340 if_down(struct ifnet *ifp)
2343 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2344 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2348 * Mark an interface up and notify protocols of
2352 if_up(struct ifnet *ifp)
2355 if_route(ifp, IFF_UP, AF_UNSPEC);
2356 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2360 * Flush an interface queue.
2363 if_qflush(struct ifnet *ifp)
2371 if (ALTQ_IS_ENABLED(ifq))
2375 while ((m = n) != NULL) {
2386 * Map interface name to interface structure pointer, with or without
2387 * returning a reference.
2390 ifunit_ref(const char *name)
2394 IFNET_RLOCK_NOSLEEP();
2395 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2396 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2397 !(ifp->if_flags & IFF_DYING))
2402 IFNET_RUNLOCK_NOSLEEP();
2407 ifunit(const char *name)
2411 IFNET_RLOCK_NOSLEEP();
2412 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2413 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2416 IFNET_RUNLOCK_NOSLEEP();
2421 ifr_buffer_get_buffer(void *data)
2423 union ifreq_union *ifrup;
2426 #ifdef COMPAT_FREEBSD32
2427 if (SV_CURPROC_FLAG(SV_ILP32))
2428 return ((void *)(uintptr_t)
2429 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2431 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2435 ifr_buffer_set_buffer_null(void *data)
2437 union ifreq_union *ifrup;
2440 #ifdef COMPAT_FREEBSD32
2441 if (SV_CURPROC_FLAG(SV_ILP32))
2442 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2445 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2449 ifr_buffer_get_length(void *data)
2451 union ifreq_union *ifrup;
2454 #ifdef COMPAT_FREEBSD32
2455 if (SV_CURPROC_FLAG(SV_ILP32))
2456 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2458 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2462 ifr_buffer_set_length(void *data, size_t len)
2464 union ifreq_union *ifrup;
2467 #ifdef COMPAT_FREEBSD32
2468 if (SV_CURPROC_FLAG(SV_ILP32))
2469 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2472 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2476 ifr_data_get_ptr(void *ifrp)
2478 union ifreq_union *ifrup;
2481 #ifdef COMPAT_FREEBSD32
2482 if (SV_CURPROC_FLAG(SV_ILP32))
2483 return ((void *)(uintptr_t)
2484 ifrup->ifr32.ifr_ifru.ifru_data);
2486 return (ifrup->ifr.ifr_ifru.ifru_data);
2490 * Hardware specific interface ioctls.
2493 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2496 int error = 0, do_ifup = 0;
2497 int new_flags, temp_flags;
2498 size_t namelen, onamelen;
2500 char *descrbuf, *odescrbuf;
2501 char new_name[IFNAMSIZ];
2503 struct sockaddr_dl *sdl;
2505 ifr = (struct ifreq *)data;
2508 ifr->ifr_index = ifp->if_index;
2512 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2513 ifr->ifr_flags = temp_flags & 0xffff;
2514 ifr->ifr_flagshigh = temp_flags >> 16;
2518 ifr->ifr_reqcap = ifp->if_capabilities;
2519 ifr->ifr_curcap = ifp->if_capenable;
2524 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2529 ifr->ifr_metric = ifp->if_metric;
2533 ifr->ifr_mtu = ifp->if_mtu;
2537 /* XXXGL: did this ever worked? */
2543 sx_slock(&ifdescr_sx);
2544 if (ifp->if_description == NULL)
2547 /* space for terminating nul */
2548 descrlen = strlen(ifp->if_description) + 1;
2549 if (ifr_buffer_get_length(ifr) < descrlen)
2550 ifr_buffer_set_buffer_null(ifr);
2552 error = copyout(ifp->if_description,
2553 ifr_buffer_get_buffer(ifr), descrlen);
2554 ifr_buffer_set_length(ifr, descrlen);
2556 sx_sunlock(&ifdescr_sx);
2560 error = priv_check(td, PRIV_NET_SETIFDESCR);
2565 * Copy only (length-1) bytes to make sure that
2566 * if_description is always nul terminated. The
2567 * length parameter is supposed to count the
2568 * terminating nul in.
2570 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2571 return (ENAMETOOLONG);
2572 else if (ifr_buffer_get_length(ifr) == 0)
2575 descrbuf = malloc(ifr_buffer_get_length(ifr),
2576 M_IFDESCR, M_WAITOK | M_ZERO);
2577 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2578 ifr_buffer_get_length(ifr) - 1);
2580 free(descrbuf, M_IFDESCR);
2585 sx_xlock(&ifdescr_sx);
2586 odescrbuf = ifp->if_description;
2587 ifp->if_description = descrbuf;
2588 sx_xunlock(&ifdescr_sx);
2590 getmicrotime(&ifp->if_lastchange);
2591 free(odescrbuf, M_IFDESCR);
2595 ifr->ifr_fib = ifp->if_fib;
2599 error = priv_check(td, PRIV_NET_SETIFFIB);
2602 if (ifr->ifr_fib >= rt_numfibs)
2605 ifp->if_fib = ifr->ifr_fib;
2609 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2613 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2614 * check, so we don't need special handling here yet.
2616 new_flags = (ifr->ifr_flags & 0xffff) |
2617 (ifr->ifr_flagshigh << 16);
2618 if (ifp->if_flags & IFF_UP &&
2619 (new_flags & IFF_UP) == 0) {
2621 } else if (new_flags & IFF_UP &&
2622 (ifp->if_flags & IFF_UP) == 0) {
2625 /* See if permanently promiscuous mode bit is about to flip */
2626 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2627 if (new_flags & IFF_PPROMISC)
2628 ifp->if_flags |= IFF_PROMISC;
2629 else if (ifp->if_pcount == 0)
2630 ifp->if_flags &= ~IFF_PROMISC;
2631 if (log_promisc_mode_change)
2632 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2634 ((new_flags & IFF_PPROMISC) ?
2635 "enabled" : "disabled"));
2637 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2638 (new_flags &~ IFF_CANTCHANGE);
2639 if (ifp->if_ioctl) {
2640 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2644 getmicrotime(&ifp->if_lastchange);
2648 error = priv_check(td, PRIV_NET_SETIFCAP);
2651 if (ifp->if_ioctl == NULL)
2652 return (EOPNOTSUPP);
2653 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2655 error = (*ifp->if_ioctl)(ifp, cmd, data);
2657 getmicrotime(&ifp->if_lastchange);
2662 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2667 error = priv_check(td, PRIV_NET_SETIFNAME);
2670 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2674 if (new_name[0] == '\0')
2676 if (new_name[IFNAMSIZ-1] != '\0') {
2677 new_name[IFNAMSIZ-1] = '\0';
2678 if (strlen(new_name) == IFNAMSIZ-1)
2681 if (ifunit(new_name) != NULL)
2685 * XXX: Locking. Nothing else seems to lock if_flags,
2686 * and there are numerous other races with the
2687 * ifunit() checks not being atomic with namespace
2688 * changes (renames, vmoves, if_attach, etc).
2690 ifp->if_flags |= IFF_RENAMING;
2692 /* Announce the departure of the interface. */
2693 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2694 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2696 log(LOG_INFO, "%s: changing name to '%s'\n",
2697 ifp->if_xname, new_name);
2700 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2702 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2703 namelen = strlen(new_name);
2704 onamelen = sdl->sdl_nlen;
2706 * Move the address if needed. This is safe because we
2707 * allocate space for a name of length IFNAMSIZ when we
2708 * create this in if_attach().
2710 if (namelen != onamelen) {
2711 bcopy(sdl->sdl_data + onamelen,
2712 sdl->sdl_data + namelen, sdl->sdl_alen);
2714 bcopy(new_name, sdl->sdl_data, namelen);
2715 sdl->sdl_nlen = namelen;
2716 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2717 bzero(sdl->sdl_data, onamelen);
2718 while (namelen != 0)
2719 sdl->sdl_data[--namelen] = 0xff;
2720 IF_ADDR_WUNLOCK(ifp);
2722 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2723 /* Announce the return of the interface. */
2724 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2726 ifp->if_flags &= ~IFF_RENAMING;
2731 error = priv_check(td, PRIV_NET_SETIFVNET);
2734 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2739 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2742 ifp->if_metric = ifr->ifr_metric;
2743 getmicrotime(&ifp->if_lastchange);
2747 error = priv_check(td, PRIV_NET_SETIFPHYS);
2750 if (ifp->if_ioctl == NULL)
2751 return (EOPNOTSUPP);
2752 error = (*ifp->if_ioctl)(ifp, cmd, data);
2754 getmicrotime(&ifp->if_lastchange);
2759 u_long oldmtu = ifp->if_mtu;
2761 error = priv_check(td, PRIV_NET_SETIFMTU);
2764 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2766 if (ifp->if_ioctl == NULL)
2767 return (EOPNOTSUPP);
2768 error = (*ifp->if_ioctl)(ifp, cmd, data);
2770 getmicrotime(&ifp->if_lastchange);
2774 * If the link MTU changed, do network layer specific procedure.
2776 if (ifp->if_mtu != oldmtu) {
2787 if (cmd == SIOCADDMULTI)
2788 error = priv_check(td, PRIV_NET_ADDMULTI);
2790 error = priv_check(td, PRIV_NET_DELMULTI);
2794 /* Don't allow group membership on non-multicast interfaces. */
2795 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2796 return (EOPNOTSUPP);
2798 /* Don't let users screw up protocols' entries. */
2799 if (ifr->ifr_addr.sa_family != AF_LINK)
2802 if (cmd == SIOCADDMULTI) {
2803 struct ifmultiaddr *ifma;
2806 * Userland is only permitted to join groups once
2807 * via the if_addmulti() KPI, because it cannot hold
2808 * struct ifmultiaddr * between calls. It may also
2809 * lose a race while we check if the membership
2813 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2814 IF_ADDR_RUNLOCK(ifp);
2818 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2820 error = if_delmulti(ifp, &ifr->ifr_addr);
2823 getmicrotime(&ifp->if_lastchange);
2826 case SIOCSIFPHYADDR:
2827 case SIOCDIFPHYADDR:
2829 case SIOCSIFPHYADDR_IN6:
2832 case SIOCSIFGENERIC:
2833 error = priv_check(td, PRIV_NET_HWIOCTL);
2836 if (ifp->if_ioctl == NULL)
2837 return (EOPNOTSUPP);
2838 error = (*ifp->if_ioctl)(ifp, cmd, data);
2840 getmicrotime(&ifp->if_lastchange);
2844 case SIOCGIFPSRCADDR:
2845 case SIOCGIFPDSTADDR:
2848 case SIOCGIFGENERIC:
2850 case SIOCGIFRSSHASH:
2851 if (ifp->if_ioctl == NULL)
2852 return (EOPNOTSUPP);
2853 error = (*ifp->if_ioctl)(ifp, cmd, data);
2857 error = priv_check(td, PRIV_NET_SETLLADDR);
2860 error = if_setlladdr(ifp,
2861 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2865 error = if_gethwaddr(ifp, ifr);
2868 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2869 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2872 if ((error = if_addgroup(ifp,
2873 ifgr_group_get((struct ifgroupreq *)data))))
2877 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2878 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2882 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2883 error = priv_check(td, PRIV_NET_DELIFGROUP);
2886 if ((error = if_delgroup(ifp,
2887 ifgr_group_get((struct ifgroupreq *)data))))
2898 #ifdef COMPAT_FREEBSD32
2906 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2909 #ifdef COMPAT_FREEBSD32
2911 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2913 struct ifmediareq32 *ifmr32;
2915 ifmr32 = (struct ifmediareq32 *)data;
2916 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2917 sizeof(ifmr->ifm_name));
2918 ifmr->ifm_current = ifmr32->ifm_current;
2919 ifmr->ifm_mask = ifmr32->ifm_mask;
2920 ifmr->ifm_status = ifmr32->ifm_status;
2921 ifmr->ifm_active = ifmr32->ifm_active;
2922 ifmr->ifm_count = ifmr32->ifm_count;
2923 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2927 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2929 struct ifmediareq32 *ifmr32;
2931 ifmr32 = (struct ifmediareq32 *)data;
2932 ifmr32->ifm_current = ifmr->ifm_current;
2933 ifmr32->ifm_mask = ifmr->ifm_mask;
2934 ifmr32->ifm_status = ifmr->ifm_status;
2935 ifmr32->ifm_active = ifmr->ifm_active;
2936 ifmr32->ifm_count = ifmr->ifm_count;
2944 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2946 #ifdef COMPAT_FREEBSD32
2948 struct ifmediareq ifmr;
2950 struct ifmediareq *ifmrp;
2959 CURVNET_SET(so->so_vnet);
2961 /* Make sure the VNET is stable. */
2962 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2963 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2973 error = ifconf(cmd, data);
2977 #ifdef COMPAT_FREEBSD32
2980 struct ifconf32 *ifc32;
2983 ifc32 = (struct ifconf32 *)data;
2984 ifc.ifc_len = ifc32->ifc_len;
2985 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2987 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2990 ifc32->ifc_len = ifc.ifc_len;
2997 #ifdef COMPAT_FREEBSD32
2999 case SIOCGIFMEDIA32:
3000 case SIOCGIFXMEDIA32:
3002 ifmr_init(ifmrp, data);
3003 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3005 data = (caddr_t)ifmrp;
3009 ifr = (struct ifreq *)data;
3013 error = priv_check(td, PRIV_NET_SETIFVNET);
3015 error = if_vmove_reclaim(td, ifr->ifr_name,
3021 error = priv_check(td, PRIV_NET_IFCREATE);
3023 error = if_clone_create(ifr->ifr_name,
3024 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3025 ifr_data_get_ptr(ifr) : NULL);
3028 error = priv_check(td, PRIV_NET_IFDESTROY);
3030 error = if_clone_destroy(ifr->ifr_name);
3033 case SIOCIFGCLONERS:
3034 error = if_clone_list((struct if_clonereq *)data);
3037 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3038 error = if_getgroupmembers((struct ifgroupreq *)data);
3041 #if defined(INET) || defined(INET6)
3044 if (carp_ioctl_p == NULL)
3045 error = EPROTONOSUPPORT;
3047 error = (*carp_ioctl_p)(ifr, cmd, td);
3052 ifp = ifunit_ref(ifr->ifr_name);
3058 error = ifhwioctl(cmd, ifp, data, td);
3059 if (error != ENOIOCTL)
3062 oif_flags = ifp->if_flags;
3063 if (so->so_proto == NULL) {
3069 * Pass the request on to the socket control method, and if the
3070 * latter returns EOPNOTSUPP, directly to the interface.
3072 * Make an exception for the legacy SIOCSIF* requests. Drivers
3073 * trust SIOCSIFADDR et al to come from an already privileged
3074 * layer, and do not perform any credentials checks or input
3077 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3079 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3080 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3081 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3082 error = (*ifp->if_ioctl)(ifp, cmd, data);
3084 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3086 if (ifp->if_flags & IFF_UP)
3094 #ifdef COMPAT_FREEBSD32
3095 if (ifmrp != NULL) {
3096 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3097 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3100 ifmr_update(ifmrp, data);
3108 * The code common to handling reference counted flags,
3109 * e.g., in ifpromisc() and if_allmulti().
3110 * The "pflag" argument can specify a permanent mode flag to check,
3111 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3113 * Only to be used on stack-owned flags, not driver-owned flags.
3116 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3120 int oldflags, oldcount;
3122 /* Sanity checks to catch programming errors */
3123 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3124 ("%s: setting driver-owned flag %d", __func__, flag));
3127 KASSERT(*refcount >= 0,
3128 ("%s: increment negative refcount %d for flag %d",
3129 __func__, *refcount, flag));
3131 KASSERT(*refcount > 0,
3132 ("%s: decrement non-positive refcount %d for flag %d",
3133 __func__, *refcount, flag));
3135 /* In case this mode is permanent, just touch refcount */
3136 if (ifp->if_flags & pflag) {
3137 *refcount += onswitch ? 1 : -1;
3141 /* Save ifnet parameters for if_ioctl() may fail */
3142 oldcount = *refcount;
3143 oldflags = ifp->if_flags;
3146 * See if we aren't the only and touching refcount is enough.
3147 * Actually toggle interface flag if we are the first or last.
3152 ifp->if_flags |= flag;
3156 ifp->if_flags &= ~flag;
3159 /* Call down the driver since we've changed interface flags */
3160 if (ifp->if_ioctl == NULL) {
3164 ifr.ifr_flags = ifp->if_flags & 0xffff;
3165 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3166 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3169 /* Notify userland that interface flags have changed */
3174 /* Recover after driver error */
3175 *refcount = oldcount;
3176 ifp->if_flags = oldflags;
3181 * Set/clear promiscuous mode on interface ifp based on the truth value
3182 * of pswitch. The calls are reference counted so that only the first
3183 * "on" request actually has an effect, as does the final "off" request.
3184 * Results are undefined if the "off" and "on" requests are not matched.
3187 ifpromisc(struct ifnet *ifp, int pswitch)
3190 int oldflags = ifp->if_flags;
3192 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3193 &ifp->if_pcount, pswitch);
3194 /* If promiscuous mode status has changed, log a message */
3195 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3196 log_promisc_mode_change)
3197 log(LOG_INFO, "%s: promiscuous mode %s\n",
3199 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3204 * Return interface configuration
3205 * of system. List may be used
3206 * in later ioctl's (above) to get
3207 * other information.
3211 ifconf(u_long cmd, caddr_t data)
3213 struct ifconf *ifc = (struct ifconf *)data;
3218 int error, full = 0, valid_len, max_len;
3220 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3221 max_len = MAXPHYS - 1;
3223 /* Prevent hostile input from being able to crash the system */
3224 if (ifc->ifc_len <= 0)
3228 if (ifc->ifc_len <= max_len) {
3229 max_len = ifc->ifc_len;
3232 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3237 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3241 * Zero the ifr to make sure we don't disclose the contents
3244 memset(&ifr, 0, sizeof(ifr));
3246 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3247 >= sizeof(ifr.ifr_name)) {
3250 return (ENAMETOOLONG);
3255 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3256 struct sockaddr *sa = ifa->ifa_addr;
3258 if (prison_if(curthread->td_ucred, sa) != 0)
3261 if (sa->sa_len <= sizeof(*sa)) {
3262 if (sa->sa_len < sizeof(*sa)) {
3263 memset(&ifr.ifr_ifru.ifru_addr, 0,
3264 sizeof(ifr.ifr_ifru.ifru_addr));
3265 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3268 ifr.ifr_ifru.ifru_addr = *sa;
3269 sbuf_bcat(sb, &ifr, sizeof(ifr));
3270 max_len += sizeof(ifr);
3273 offsetof(struct ifreq, ifr_addr));
3274 max_len += offsetof(struct ifreq, ifr_addr);
3275 sbuf_bcat(sb, sa, sa->sa_len);
3276 max_len += sa->sa_len;
3279 if (sbuf_error(sb) == 0)
3280 valid_len = sbuf_len(sb);
3282 IF_ADDR_RUNLOCK(ifp);
3284 sbuf_bcat(sb, &ifr, sizeof(ifr));
3285 max_len += sizeof(ifr);
3287 if (sbuf_error(sb) == 0)
3288 valid_len = sbuf_len(sb);
3294 * If we didn't allocate enough space (uncommon), try again. If
3295 * we have already allocated as much space as we are allowed,
3296 * return what we've got.
3298 if (valid_len != max_len && !full) {
3303 ifc->ifc_len = valid_len;
3305 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3311 * Just like ifpromisc(), but for all-multicast-reception mode.
3314 if_allmulti(struct ifnet *ifp, int onswitch)
3317 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3320 struct ifmultiaddr *
3321 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3323 struct ifmultiaddr *ifma;
3325 IF_ADDR_LOCK_ASSERT(ifp);
3327 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3328 if (sa->sa_family == AF_LINK) {
3329 if (sa_dl_equal(ifma->ifma_addr, sa))
3332 if (sa_equal(ifma->ifma_addr, sa))
3341 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3342 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3343 * the ifnet multicast address list here, so the caller must do that and
3344 * other setup work (such as notifying the device driver). The reference
3345 * count is initialized to 1.
3347 static struct ifmultiaddr *
3348 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3351 struct ifmultiaddr *ifma;
3352 struct sockaddr *dupsa;
3354 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3359 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3360 if (dupsa == NULL) {
3361 free(ifma, M_IFMADDR);
3364 bcopy(sa, dupsa, sa->sa_len);
3365 ifma->ifma_addr = dupsa;
3367 ifma->ifma_ifp = ifp;
3368 ifma->ifma_refcount = 1;
3369 ifma->ifma_protospec = NULL;
3372 ifma->ifma_lladdr = NULL;
3376 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3377 if (dupsa == NULL) {
3378 free(ifma->ifma_addr, M_IFMADDR);
3379 free(ifma, M_IFMADDR);
3382 bcopy(llsa, dupsa, llsa->sa_len);
3383 ifma->ifma_lladdr = dupsa;
3389 * if_freemulti: free ifmultiaddr structure and possibly attached related
3390 * addresses. The caller is responsible for implementing reference
3391 * counting, notifying the driver, handling routing messages, and releasing
3392 * any dependent link layer state.
3395 if_freemulti(struct ifmultiaddr *ifma)
3398 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3399 ifma->ifma_refcount));
3401 if (ifma->ifma_lladdr != NULL)
3402 free(ifma->ifma_lladdr, M_IFMADDR);
3403 free(ifma->ifma_addr, M_IFMADDR);
3404 free(ifma, M_IFMADDR);
3408 * Register an additional multicast address with a network interface.
3410 * - If the address is already present, bump the reference count on the
3411 * address and return.
3412 * - If the address is not link-layer, look up a link layer address.
3413 * - Allocate address structures for one or both addresses, and attach to the
3414 * multicast address list on the interface. If automatically adding a link
3415 * layer address, the protocol address will own a reference to the link
3416 * layer address, to be freed when it is freed.
3417 * - Notify the network device driver of an addition to the multicast address
3420 * 'sa' points to caller-owned memory with the desired multicast address.
3422 * 'retifma' will be used to return a pointer to the resulting multicast
3423 * address reference, if desired.
3426 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3427 struct ifmultiaddr **retifma)
3429 struct ifmultiaddr *ifma, *ll_ifma;
3430 struct sockaddr *llsa;
3431 struct sockaddr_dl sdl;
3435 IN_MULTI_LIST_UNLOCK_ASSERT();
3438 IN6_MULTI_LIST_UNLOCK_ASSERT();
3441 * If the address is already present, return a new reference to it;
3442 * otherwise, allocate storage and set up a new address.
3445 ifma = if_findmulti(ifp, sa);
3447 ifma->ifma_refcount++;
3448 if (retifma != NULL)
3450 IF_ADDR_WUNLOCK(ifp);
3455 * The address isn't already present; resolve the protocol address
3456 * into a link layer address, and then look that up, bump its
3457 * refcount or allocate an ifma for that also.
3458 * Most link layer resolving functions returns address data which
3459 * fits inside default sockaddr_dl structure. However callback
3460 * can allocate another sockaddr structure, in that case we need to
3465 if (ifp->if_resolvemulti != NULL) {
3466 /* Provide called function with buffer size information */
3467 sdl.sdl_len = sizeof(sdl);
3468 llsa = (struct sockaddr *)&sdl;
3469 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3475 * Allocate the new address. Don't hook it up yet, as we may also
3476 * need to allocate a link layer multicast address.
3478 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3485 * If a link layer address is found, we'll need to see if it's
3486 * already present in the address list, or allocate is as well.
3487 * When this block finishes, the link layer address will be on the
3491 ll_ifma = if_findmulti(ifp, llsa);
3492 if (ll_ifma == NULL) {
3493 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3494 if (ll_ifma == NULL) {
3495 --ifma->ifma_refcount;
3500 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3503 ll_ifma->ifma_refcount++;
3504 ifma->ifma_llifma = ll_ifma;
3508 * We now have a new multicast address, ifma, and possibly a new or
3509 * referenced link layer address. Add the primary address to the
3510 * ifnet address list.
3512 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3514 if (retifma != NULL)
3518 * Must generate the message while holding the lock so that 'ifma'
3519 * pointer is still valid.
3521 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3522 IF_ADDR_WUNLOCK(ifp);
3525 * We are certain we have added something, so call down to the
3526 * interface to let them know about it.
3528 if (ifp->if_ioctl != NULL) {
3529 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3532 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3533 link_free_sdl(llsa);
3538 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3539 link_free_sdl(llsa);
3542 IF_ADDR_WUNLOCK(ifp);
3547 * Delete a multicast group membership by network-layer group address.
3549 * Returns ENOENT if the entry could not be found. If ifp no longer
3550 * exists, results are undefined. This entry point should only be used
3551 * from subsystems which do appropriate locking to hold ifp for the
3552 * duration of the call.
3553 * Network-layer protocol domains must use if_delmulti_ifma().
3556 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3558 struct ifmultiaddr *ifma;
3563 IFNET_RLOCK_NOSLEEP();
3564 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3569 IFNET_RUNLOCK_NOSLEEP();
3571 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3578 ifma = if_findmulti(ifp, sa);
3580 lastref = if_delmulti_locked(ifp, ifma, 0);
3581 IF_ADDR_WUNLOCK(ifp);
3586 if (lastref && ifp->if_ioctl != NULL) {
3587 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3594 * Delete all multicast group membership for an interface.
3595 * Should be used to quickly flush all multicast filters.
3598 if_delallmulti(struct ifnet *ifp)
3600 struct ifmultiaddr *ifma;
3601 struct ifmultiaddr *next;
3604 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3605 if_delmulti_locked(ifp, ifma, 0);
3606 IF_ADDR_WUNLOCK(ifp);
3610 * Delete a multicast group membership by group membership pointer.
3611 * Network-layer protocol domains must use this routine.
3613 * It is safe to call this routine if the ifp disappeared.
3616 if_delmulti_ifma(struct ifmultiaddr *ifma)
3622 IN_MULTI_LIST_UNLOCK_ASSERT();
3624 ifp = ifma->ifma_ifp;
3627 printf("%s: ifma_ifp seems to be detached\n", __func__);
3631 IFNET_RLOCK_NOSLEEP();
3632 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3636 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3639 IFNET_RUNLOCK_NOSLEEP();
3643 * If and only if the ifnet instance exists: Acquire the address lock.
3648 lastref = if_delmulti_locked(ifp, ifma, 0);
3652 * If and only if the ifnet instance exists:
3653 * Release the address lock.
3654 * If the group was left: update the hardware hash filter.
3656 IF_ADDR_WUNLOCK(ifp);
3657 if (lastref && ifp->if_ioctl != NULL) {
3658 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3664 * Perform deletion of network-layer and/or link-layer multicast address.
3666 * Return 0 if the reference count was decremented.
3667 * Return 1 if the final reference was released, indicating that the
3668 * hardware hash filter should be reprogrammed.
3671 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3673 struct ifmultiaddr *ll_ifma;
3675 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3676 KASSERT(ifma->ifma_ifp == ifp,
3677 ("%s: inconsistent ifp %p", __func__, ifp));
3678 IF_ADDR_WLOCK_ASSERT(ifp);
3681 ifp = ifma->ifma_ifp;
3684 * If the ifnet is detaching, null out references to ifnet,
3685 * so that upper protocol layers will notice, and not attempt
3686 * to obtain locks for an ifnet which no longer exists. The
3687 * routing socket announcement must happen before the ifnet
3688 * instance is detached from the system.
3692 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3695 * ifp may already be nulled out if we are being reentered
3696 * to delete the ll_ifma.
3699 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3700 ifma->ifma_ifp = NULL;
3704 if (--ifma->ifma_refcount > 0)
3708 * If this ifma is a network-layer ifma, a link-layer ifma may
3709 * have been associated with it. Release it first if so.
3711 ll_ifma = ifma->ifma_llifma;
3712 if (ll_ifma != NULL) {
3713 KASSERT(ifma->ifma_lladdr != NULL,
3714 ("%s: llifma w/o lladdr", __func__));
3716 ll_ifma->ifma_ifp = NULL; /* XXX */
3717 if (--ll_ifma->ifma_refcount == 0) {
3719 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3722 if_freemulti(ll_ifma);
3725 if (ifp != NULL && detaching == 0)
3726 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3731 * The last reference to this instance of struct ifmultiaddr
3732 * was released; the hardware should be notified of this change.
3738 * Set the link layer address on an interface.
3740 * At this time we only support certain types of interfaces,
3741 * and we don't allow the length of the address to change.
3743 * Set noinline to be dtrace-friendly
3746 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3748 struct sockaddr_dl *sdl;
3755 IF_ADDR_RUNLOCK(ifp);
3759 IF_ADDR_RUNLOCK(ifp);
3760 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3765 if (len != sdl->sdl_alen) { /* don't allow length to change */
3769 switch (ifp->if_type) {
3774 case IFT_IEEE8023ADLAG:
3775 bcopy(lladdr, LLADDR(sdl), len);
3784 * If the interface is already up, we need
3785 * to re-init it in order to reprogram its
3788 if ((ifp->if_flags & IFF_UP) != 0) {
3789 if (ifp->if_ioctl) {
3790 ifp->if_flags &= ~IFF_UP;
3791 ifr.ifr_flags = ifp->if_flags & 0xffff;
3792 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3793 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3794 ifp->if_flags |= IFF_UP;
3795 ifr.ifr_flags = ifp->if_flags & 0xffff;
3796 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3797 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3800 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3805 * Compat function for handling basic encapsulation requests.
3806 * Not converted stacks (FDDI, IB, ..) supports traditional
3807 * output model: ARP (and other similar L2 protocols) are handled
3808 * inside output routine, arpresolve/nd6_resolve() returns MAC
3809 * address instead of full prepend.
3811 * This function creates calculated header==MAC for IPv4/IPv6 and
3812 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3816 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3819 if (req->rtype != IFENCAP_LL)
3820 return (EOPNOTSUPP);
3822 if (req->bufsize < req->lladdr_len)
3825 switch (req->family) {
3830 return (EAFNOSUPPORT);
3833 /* Copy lladdr to storage as is */
3834 memmove(req->buf, req->lladdr, req->lladdr_len);
3835 req->bufsize = req->lladdr_len;
3836 req->lladdr_off = 0;
3842 * Get the link layer address that was read from the hardware at attach.
3844 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3845 * their component interfaces as IFT_IEEE8023ADLAG.
3848 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3851 if (ifp->if_hw_addr == NULL)
3854 switch (ifp->if_type) {
3856 case IFT_IEEE8023ADLAG:
3857 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3865 * The name argument must be a pointer to storage which will last as
3866 * long as the interface does. For physical devices, the result of
3867 * device_get_name(dev) is a good choice and for pseudo-devices a
3868 * static string works well.
3871 if_initname(struct ifnet *ifp, const char *name, int unit)
3873 ifp->if_dname = name;
3874 ifp->if_dunit = unit;
3875 if (unit != IF_DUNIT_NONE)
3876 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3878 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3882 if_printf(struct ifnet *ifp, const char * fmt, ...)
3887 retval = printf("%s: ", ifp->if_xname);
3889 retval += vprintf(fmt, ap);
3895 if_start(struct ifnet *ifp)
3898 (*(ifp)->if_start)(ifp);
3902 * Backwards compatibility interface for drivers
3903 * that have not implemented it
3906 if_transmit(struct ifnet *ifp, struct mbuf *m)
3910 IFQ_HANDOFF(ifp, m, error);
3915 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3922 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3927 if (_IF_QFULL(ifq)) {
3929 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3934 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3935 if (m->m_flags & (M_BCAST|M_MCAST))
3936 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3937 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3939 _IF_ENQUEUE(ifq, m);
3941 if (ifp != NULL && !active)
3942 (*(ifp)->if_start)(ifp);
3947 if_register_com_alloc(u_char type,
3948 if_com_alloc_t *a, if_com_free_t *f)
3951 KASSERT(if_com_alloc[type] == NULL,
3952 ("if_register_com_alloc: %d already registered", type));
3953 KASSERT(if_com_free[type] == NULL,
3954 ("if_register_com_alloc: %d free already registered", type));
3956 if_com_alloc[type] = a;
3957 if_com_free[type] = f;
3961 if_deregister_com_alloc(u_char type)
3964 KASSERT(if_com_alloc[type] != NULL,
3965 ("if_deregister_com_alloc: %d not registered", type));
3966 KASSERT(if_com_free[type] != NULL,
3967 ("if_deregister_com_alloc: %d free not registered", type));
3968 if_com_alloc[type] = NULL;
3969 if_com_free[type] = NULL;
3972 /* API for driver access to network stack owned ifnet.*/
3974 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3978 oldbrate = ifp->if_baudrate;
3979 ifp->if_baudrate = baudrate;
3984 if_getbaudrate(if_t ifp)
3987 return (((struct ifnet *)ifp)->if_baudrate);
3991 if_setcapabilities(if_t ifp, int capabilities)
3993 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3998 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4000 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4001 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4007 if_getcapabilities(if_t ifp)
4009 return ((struct ifnet *)ifp)->if_capabilities;
4013 if_setcapenable(if_t ifp, int capabilities)
4015 ((struct ifnet *)ifp)->if_capenable = capabilities;
4020 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4023 ((struct ifnet *)ifp)->if_capenable |= setcap;
4025 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4031 if_getdname(if_t ifp)
4033 return ((struct ifnet *)ifp)->if_dname;
4037 if_togglecapenable(if_t ifp, int togglecap)
4039 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4044 if_getcapenable(if_t ifp)
4046 return ((struct ifnet *)ifp)->if_capenable;
4050 * This is largely undesirable because it ties ifnet to a device, but does
4051 * provide flexiblity for an embedded product vendor. Should be used with
4052 * the understanding that it violates the interface boundaries, and should be
4053 * a last resort only.
4056 if_setdev(if_t ifp, void *dev)
4062 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4064 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4065 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4071 if_getdrvflags(if_t ifp)
4073 return ((struct ifnet *)ifp)->if_drv_flags;
4077 if_setdrvflags(if_t ifp, int flags)
4079 ((struct ifnet *)ifp)->if_drv_flags = flags;
4085 if_setflags(if_t ifp, int flags)
4087 ((struct ifnet *)ifp)->if_flags = flags;
4092 if_setflagbits(if_t ifp, int set, int clear)
4094 ((struct ifnet *)ifp)->if_flags |= set;
4095 ((struct ifnet *)ifp)->if_flags &= ~clear;
4101 if_getflags(if_t ifp)
4103 return ((struct ifnet *)ifp)->if_flags;
4107 if_clearhwassist(if_t ifp)
4109 ((struct ifnet *)ifp)->if_hwassist = 0;
4114 if_sethwassistbits(if_t ifp, int toset, int toclear)
4116 ((struct ifnet *)ifp)->if_hwassist |= toset;
4117 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4123 if_sethwassist(if_t ifp, int hwassist_bit)
4125 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4130 if_gethwassist(if_t ifp)
4132 return ((struct ifnet *)ifp)->if_hwassist;
4136 if_setmtu(if_t ifp, int mtu)
4138 ((struct ifnet *)ifp)->if_mtu = mtu;
4145 return ((struct ifnet *)ifp)->if_mtu;
4149 if_getmtu_family(if_t ifp, int family)
4153 for (dp = domains; dp; dp = dp->dom_next) {
4154 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4155 return (dp->dom_ifmtu((struct ifnet *)ifp));
4158 return (((struct ifnet *)ifp)->if_mtu);
4162 if_setsoftc(if_t ifp, void *softc)
4164 ((struct ifnet *)ifp)->if_softc = softc;
4169 if_getsoftc(if_t ifp)
4171 return ((struct ifnet *)ifp)->if_softc;
4175 if_setrcvif(struct mbuf *m, if_t ifp)
4177 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4181 if_setvtag(struct mbuf *m, uint16_t tag)
4183 m->m_pkthdr.ether_vtag = tag;
4187 if_getvtag(struct mbuf *m)
4190 return (m->m_pkthdr.ether_vtag);
4194 if_sendq_empty(if_t ifp)
4196 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4200 if_getifaddr(if_t ifp)
4202 return ((struct ifnet *)ifp)->if_addr;
4206 if_getamcount(if_t ifp)
4208 return ((struct ifnet *)ifp)->if_amcount;
4213 if_setsendqready(if_t ifp)
4215 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4220 if_setsendqlen(if_t ifp, int tx_desc_count)
4222 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4223 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4229 if_vlantrunkinuse(if_t ifp)
4231 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4235 if_input(if_t ifp, struct mbuf* sendmp)
4237 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4243 #ifndef ETH_ADDR_LEN
4244 #define ETH_ADDR_LEN 6
4248 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4250 struct ifmultiaddr *ifma;
4251 uint8_t *lmta = (uint8_t *)mta;
4254 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4255 if (ifma->ifma_addr->sa_family != AF_LINK)
4261 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4262 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4271 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4275 if_maddr_rlock(ifp);
4276 error = if_setupmultiaddr(ifp, mta, cnt, max);
4277 if_maddr_runlock(ifp);
4282 if_multiaddr_count(if_t ifp, int max)
4284 struct ifmultiaddr *ifma;
4288 if_maddr_rlock(ifp);
4289 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4290 if (ifma->ifma_addr->sa_family != AF_LINK)
4296 if_maddr_runlock(ifp);
4301 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4303 struct ifmultiaddr *ifma;
4306 if_maddr_rlock(ifp);
4307 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4308 cnt += filter(arg, ifma, cnt);
4309 if_maddr_runlock(ifp);
4314 if_dequeue(if_t ifp)
4317 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4323 if_sendq_prepend(if_t ifp, struct mbuf *m)
4325 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4330 if_setifheaderlen(if_t ifp, int len)
4332 ((struct ifnet *)ifp)->if_hdrlen = len;
4337 if_getlladdr(if_t ifp)
4339 return (IF_LLADDR((struct ifnet *)ifp));
4343 if_gethandle(u_char type)
4345 return (if_alloc(type));
4349 if_bpfmtap(if_t ifh, struct mbuf *m)
4351 struct ifnet *ifp = (struct ifnet *)ifh;
4357 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4359 struct ifnet *ifp = (struct ifnet *)ifh;
4361 ETHER_BPF_MTAP(ifp, m);
4365 if_vlancap(if_t ifh)
4367 struct ifnet *ifp = (struct ifnet *)ifh;
4368 VLAN_CAPABILITIES(ifp);
4372 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4375 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4380 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4383 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4388 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4391 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4396 if_gethwtsomax(if_t ifp)
4399 return (((struct ifnet *)ifp)->if_hw_tsomax);
4403 if_gethwtsomaxsegcount(if_t ifp)
4406 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4410 if_gethwtsomaxsegsize(if_t ifp)
4413 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4417 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4419 ((struct ifnet *)ifp)->if_init = init_fn;
4423 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4425 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4429 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4431 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4435 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4437 ((struct ifnet *)ifp)->if_transmit = start_fn;
4440 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4442 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4447 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4450 ifp->if_get_counter = fn;
4453 /* Revisit these - These are inline functions originally. */
4455 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4457 return drbr_inuse(ifh, br);
4461 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4463 return drbr_dequeue(ifh, br);
4467 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4469 return drbr_needs_enqueue(ifh, br);
4473 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4475 return drbr_enqueue(ifh, br, m);