2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
35 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
44 #include <sys/epoch.h>
46 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
54 #include <sys/refcount.h>
55 #include <sys/module.h>
56 #include <sys/rwlock.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/taskqueue.h>
62 #include <sys/domain.h>
66 #include <machine/stdarg.h>
70 #include <net/ethernet.h>
72 #include <net/if_arp.h>
73 #include <net/if_clone.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_var.h>
77 #include <net/if_media.h>
78 #include <net/if_vlan_var.h>
79 #include <net/radix.h>
80 #include <net/route.h>
83 #if defined(INET) || defined(INET6)
84 #include <net/ethernet.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_carp.h>
90 #include <netinet/if_ether.h>
91 #include <netinet/netdump/netdump.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet6/in6_ifattach.h>
97 #endif /* INET || INET6 */
99 #include <security/mac/mac_framework.h>
102 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
103 * and ifr_ifru when it is used in SIOCGIFCONF.
105 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
106 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
108 __read_mostly epoch_t net_epoch_preempt;
109 __read_mostly epoch_t net_epoch;
110 #ifdef COMPAT_FREEBSD32
111 #include <sys/mount.h>
112 #include <compat/freebsd32/freebsd32.h>
114 struct ifreq_buffer32 {
115 uint32_t length; /* (size_t) */
116 uint32_t buffer; /* (void *) */
120 * Interface request structure used for socket
121 * ioctl's. All interface ioctl's must have parameter
122 * definitions which begin with ifr_name. The
123 * remainder may be interface specific.
126 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
128 struct sockaddr ifru_addr;
129 struct sockaddr ifru_dstaddr;
130 struct sockaddr ifru_broadaddr;
131 struct ifreq_buffer32 ifru_buffer;
142 u_char ifru_vlan_pcp;
145 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
146 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
147 __offsetof(struct ifreq32, ifr_ifru));
149 struct ifgroupreq32 {
150 char ifgr_name[IFNAMSIZ];
153 char ifgru_group[IFNAMSIZ];
154 uint32_t ifgru_groups;
158 struct ifmediareq32 {
159 char ifm_name[IFNAMSIZ];
165 uint32_t ifm_ulist; /* (int *) */
167 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
168 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
170 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
171 case _IOC_NEWTYPE((cmd), struct ifgroupreq32):
172 #else /* !COMPAT_FREEBSD32 */
173 #define _CASE_IOC_IFGROUPREQ_32(cmd)
174 #endif /* !COMPAT_FREEBSD32 */
176 #define CASE_IOC_IFGROUPREQ(cmd) \
177 _CASE_IOC_IFGROUPREQ_32(cmd) \
182 #ifdef COMPAT_FREEBSD32
183 struct ifreq32 ifr32;
187 union ifgroupreq_union {
188 struct ifgroupreq ifgr;
189 #ifdef COMPAT_FREEBSD32
190 struct ifgroupreq32 ifgr32;
194 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
195 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
197 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
198 &ifqmaxlen, 0, "max send queue size");
200 /* Log link state change events */
201 static int log_link_state_change = 1;
203 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
204 &log_link_state_change, 0,
205 "log interface link state change events");
207 /* Log promiscuous mode change events */
208 static int log_promisc_mode_change = 1;
210 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
211 &log_promisc_mode_change, 1,
212 "log promiscuous mode change events");
214 /* Interface description */
215 static unsigned int ifdescr_maxlen = 1024;
216 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
218 "administrative maximum length for interface description");
220 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
222 /* global sx for non-critical path ifdescr */
223 static struct sx ifdescr_sx;
224 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
226 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
227 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
228 /* These are external hooks for CARP. */
229 void (*carp_linkstate_p)(struct ifnet *ifp);
230 void (*carp_demote_adj_p)(int, char *);
231 int (*carp_master_p)(struct ifaddr *);
232 #if defined(INET) || defined(INET6)
233 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
234 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
235 const struct sockaddr *sa);
236 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
237 int (*carp_attach_p)(struct ifaddr *, int);
238 void (*carp_detach_p)(struct ifaddr *, bool);
241 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
244 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
245 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
246 const struct in6_addr *taddr);
249 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
252 * XXX: Style; these should be sorted alphabetically, and unprototyped
253 * static functions should be prototyped. Currently they are sorted by
256 static void if_attachdomain(void *);
257 static void if_attachdomain1(struct ifnet *);
258 static int ifconf(u_long, caddr_t);
259 static void *if_grow(void);
260 static void if_input_default(struct ifnet *, struct mbuf *);
261 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
262 static void if_route(struct ifnet *, int flag, int fam);
263 static int if_setflag(struct ifnet *, int, int, int *, int);
264 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
265 static void if_unroute(struct ifnet *, int flag, int fam);
266 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
267 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
268 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
269 static void do_link_state_change(void *, int);
270 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
271 static int if_getgroupmembers(struct ifgroupreq *);
272 static void if_delgroups(struct ifnet *);
273 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
274 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
276 static void if_vmove(struct ifnet *, struct vnet *);
281 * XXX: declare here to avoid to include many inet6 related files..
282 * should be more generalized?
284 extern void nd6_setmtu(struct ifnet *);
287 /* ipsec helper hooks */
288 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
289 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
291 VNET_DEFINE(int, if_index);
292 int ifqmaxlen = IFQ_MAXLEN;
293 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
294 VNET_DEFINE(struct ifgrouphead, ifg_head);
296 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
298 /* Table of ifnet by index. */
299 VNET_DEFINE(struct ifnet **, ifindex_table);
301 #define V_if_indexlim VNET(if_indexlim)
302 #define V_ifindex_table VNET(ifindex_table)
305 * The global network interface list (V_ifnet) and related state (such as
306 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
307 * an rwlock. Either may be acquired shared to stablize the list, but both
308 * must be acquired writable to modify the list. This model allows us to
309 * both stablize the interface list during interrupt thread processing, but
310 * also to stablize it over long-running ioctls, without introducing priority
311 * inversions and deadlocks.
313 struct rwlock ifnet_rwlock;
314 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
315 struct sx ifnet_sxlock;
316 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
319 * The allocation of network interfaces is a rather non-atomic affair; we
320 * need to select an index before we are ready to expose the interface for
321 * use, so will use this pointer value to indicate reservation.
323 #define IFNET_HOLD (void *)(uintptr_t)(-1)
325 static if_com_alloc_t *if_com_alloc[256];
326 static if_com_free_t *if_com_free[256];
328 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
329 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
330 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
333 ifnet_byindex_locked(u_short idx)
336 if (idx > V_if_index)
338 if (V_ifindex_table[idx] == IFNET_HOLD)
340 return (V_ifindex_table[idx]);
344 ifnet_byindex(u_short idx)
348 ifp = ifnet_byindex_locked(idx);
353 ifnet_byindex_ref(u_short idx)
357 IFNET_RLOCK_NOSLEEP();
358 ifp = ifnet_byindex_locked(idx);
359 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
360 IFNET_RUNLOCK_NOSLEEP();
364 IFNET_RUNLOCK_NOSLEEP();
369 * Allocate an ifindex array entry; return 0 on success or an error on
373 ifindex_alloc(void **old)
377 IFNET_WLOCK_ASSERT();
379 * Try to find an empty slot below V_if_index. If we fail, take the
382 for (idx = 1; idx <= V_if_index; idx++) {
383 if (V_ifindex_table[idx] == NULL)
387 /* Catch if_index overflow. */
388 if (idx >= V_if_indexlim) {
392 if (idx > V_if_index)
398 ifindex_free_locked(u_short idx)
401 IFNET_WLOCK_ASSERT();
403 V_ifindex_table[idx] = NULL;
404 while (V_if_index > 0 &&
405 V_ifindex_table[V_if_index] == NULL)
410 ifindex_free(u_short idx)
414 ifindex_free_locked(idx);
419 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
422 V_ifindex_table[idx] = ifp;
426 ifaddr_byindex(u_short idx)
429 struct ifaddr *ifa = NULL;
431 IFNET_RLOCK_NOSLEEP();
432 ifp = ifnet_byindex_locked(idx);
433 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
435 IFNET_RUNLOCK_NOSLEEP();
440 * Network interface utility routines.
442 * Routines with ifa_ifwith* names take sockaddr *'s as
447 vnet_if_init(const void *unused __unused)
451 CK_STAILQ_INIT(&V_ifnet);
452 CK_STAILQ_INIT(&V_ifg_head);
454 old = if_grow(); /* create initial table */
456 epoch_wait_preempt(net_epoch_preempt);
458 vnet_if_clone_init();
460 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
465 vnet_if_uninit(const void *unused __unused)
468 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
469 "not empty", __func__, __LINE__, &V_ifnet));
470 VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
471 "not empty", __func__, __LINE__, &V_ifg_head));
473 free((caddr_t)V_ifindex_table, M_IFNET);
475 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
476 vnet_if_uninit, NULL);
479 vnet_if_return(const void *unused __unused)
481 struct ifnet *ifp, *nifp;
483 /* Return all inherited interfaces to their parent vnets. */
484 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
485 if (ifp->if_home_vnet != ifp->if_vnet)
486 if_vmove(ifp, ifp->if_home_vnet);
489 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
490 vnet_if_return, NULL);
503 IFNET_WLOCK_ASSERT();
504 oldlim = V_if_indexlim;
506 n = (oldlim << 1) * sizeof(*e);
507 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
509 if (V_if_indexlim != oldlim) {
513 if (V_ifindex_table != NULL) {
514 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
515 old = V_ifindex_table;
523 * Allocate a struct ifnet and an index for an interface. A layer 2
524 * common structure will also be allocated if an allocation routine is
525 * registered for the passed type.
528 if_alloc(u_char type)
534 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
537 idx = ifindex_alloc(&old);
538 if (__predict_false(idx == USHRT_MAX)) {
540 epoch_wait_preempt(net_epoch_preempt);
544 ifnet_setbyindex(idx, IFNET_HOLD);
548 ifp->if_alloctype = type;
550 ifp->if_vnet = curvnet;
552 if (if_com_alloc[type] != NULL) {
553 ifp->if_l2com = if_com_alloc[type](type, ifp);
554 if (ifp->if_l2com == NULL) {
561 IF_ADDR_LOCK_INIT(ifp);
562 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
563 ifp->if_afdata_initialized = 0;
564 IF_AFDATA_LOCK_INIT(ifp);
565 CK_STAILQ_INIT(&ifp->if_addrhead);
566 CK_STAILQ_INIT(&ifp->if_multiaddrs);
567 CK_STAILQ_INIT(&ifp->if_groups);
571 ifq_init(&ifp->if_snd, ifp);
573 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
574 for (int i = 0; i < IFCOUNTERS; i++)
575 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
576 ifp->if_get_counter = if_get_counter_default;
577 ifp->if_pcp = IFNET_PCP_NONE;
578 ifnet_setbyindex(ifp->if_index, ifp);
583 * Do the actual work of freeing a struct ifnet, and layer 2 common
584 * structure. This call is made when the last reference to an
585 * interface is released.
588 if_free_internal(struct ifnet *ifp)
591 KASSERT((ifp->if_flags & IFF_DYING),
592 ("if_free_internal: interface not dying"));
594 if (if_com_free[ifp->if_alloctype] != NULL)
595 if_com_free[ifp->if_alloctype](ifp->if_l2com,
599 mac_ifnet_destroy(ifp);
601 if (ifp->if_description != NULL)
602 free(ifp->if_description, M_IFDESCR);
603 IF_AFDATA_DESTROY(ifp);
604 IF_ADDR_LOCK_DESTROY(ifp);
605 ifq_delete(&ifp->if_snd);
607 for (int i = 0; i < IFCOUNTERS; i++)
608 counter_u64_free(ifp->if_counters[i]);
614 if_destroy(epoch_context_t ctx)
618 ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
619 if_free_internal(ifp);
623 * Deregister an interface and free the associated storage.
626 if_free(struct ifnet *ifp)
629 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
631 CURVNET_SET_QUIET(ifp->if_vnet);
633 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
634 ("%s: freeing unallocated ifnet", ifp->if_xname));
636 ifindex_free_locked(ifp->if_index);
639 if (refcount_release(&ifp->if_refcount))
640 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
645 * Interfaces to keep an ifnet type-stable despite the possibility of the
646 * driver calling if_free(). If there are additional references, we defer
647 * freeing the underlying data structure.
650 if_ref(struct ifnet *ifp)
653 /* We don't assert the ifnet list lock here, but arguably should. */
654 refcount_acquire(&ifp->if_refcount);
658 if_rele(struct ifnet *ifp)
661 if (!refcount_release(&ifp->if_refcount))
663 epoch_call(net_epoch_preempt, &ifp->if_epoch_ctx, if_destroy);
667 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
670 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
672 if (ifq->ifq_maxlen == 0)
673 ifq->ifq_maxlen = ifqmaxlen;
676 ifq->altq_disc = NULL;
677 ifq->altq_flags &= ALTQF_CANTCHANGE;
678 ifq->altq_tbr = NULL;
683 ifq_delete(struct ifaltq *ifq)
685 mtx_destroy(&ifq->ifq_mtx);
689 * Perform generic interface initialization tasks and attach the interface
690 * to the list of "active" interfaces. If vmove flag is set on entry
691 * to if_attach_internal(), perform only a limited subset of initialization
692 * tasks, given that we are moving from one vnet to another an ifnet which
693 * has already been fully initialized.
695 * Note that if_detach_internal() removes group membership unconditionally
696 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
697 * Thus, when if_vmove() is applied to a cloned interface, group membership
698 * is lost while a cloned one always joins a group whose name is
699 * ifc->ifc_name. To recover this after if_detach_internal() and
700 * if_attach_internal(), the cloner should be specified to
701 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
702 * attempts to join a group whose name is ifc->ifc_name.
705 * - The decision to return void and thus require this function to
706 * succeed is questionable.
707 * - We should probably do more sanity checking. For instance we don't
708 * do anything to insure if_xname is unique or non-empty.
711 if_attach(struct ifnet *ifp)
714 if_attach_internal(ifp, 0, NULL);
718 * Compute the least common TSO limit.
721 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
724 * 1) If there is no limit currently, take the limit from
725 * the network adapter.
727 * 2) If the network adapter has a limit below the current
730 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
731 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
732 pmax->tsomaxbytes = ifp->if_hw_tsomax;
734 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
735 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
736 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
738 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
739 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
740 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
745 * Update TSO limit of a network adapter.
747 * Returns zero if no change. Else non-zero.
750 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
753 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
754 ifp->if_hw_tsomax = pmax->tsomaxbytes;
757 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
758 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
761 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
762 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
769 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
771 unsigned socksize, ifasize;
772 int namelen, masklen;
773 struct sockaddr_dl *sdl;
776 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
777 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
781 ifp->if_vnet = curvnet;
782 if (ifp->if_home_vnet == NULL)
783 ifp->if_home_vnet = curvnet;
786 if_addgroup(ifp, IFG_ALL);
788 /* Restore group membership for cloned interfaces. */
789 if (vmove && ifc != NULL)
790 if_clone_addgroup(ifp, ifc);
792 getmicrotime(&ifp->if_lastchange);
793 ifp->if_epoch = time_uptime;
795 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
796 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
797 ("transmit and qflush must both either be set or both be NULL"));
798 if (ifp->if_transmit == NULL) {
799 ifp->if_transmit = if_transmit;
800 ifp->if_qflush = if_qflush;
802 if (ifp->if_input == NULL)
803 ifp->if_input = if_input_default;
805 if (ifp->if_requestencap == NULL)
806 ifp->if_requestencap = if_requestencap_default;
810 mac_ifnet_create(ifp);
814 * Create a Link Level name for this device.
816 namelen = strlen(ifp->if_xname);
818 * Always save enough space for any possiable name so we
819 * can do a rename in place later.
821 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
822 socksize = masklen + ifp->if_addrlen;
823 if (socksize < sizeof(*sdl))
824 socksize = sizeof(*sdl);
825 socksize = roundup2(socksize, sizeof(long));
826 ifasize = sizeof(*ifa) + 2 * socksize;
827 ifa = ifa_alloc(ifasize, M_WAITOK);
828 sdl = (struct sockaddr_dl *)(ifa + 1);
829 sdl->sdl_len = socksize;
830 sdl->sdl_family = AF_LINK;
831 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
832 sdl->sdl_nlen = namelen;
833 sdl->sdl_index = ifp->if_index;
834 sdl->sdl_type = ifp->if_type;
837 ifa->ifa_rtrequest = link_rtrequest;
838 ifa->ifa_addr = (struct sockaddr *)sdl;
839 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
840 ifa->ifa_netmask = (struct sockaddr *)sdl;
841 sdl->sdl_len = masklen;
843 sdl->sdl_data[--namelen] = 0xff;
844 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
845 /* Reliably crash if used uninitialized. */
846 ifp->if_broadcastaddr = NULL;
848 if (ifp->if_type == IFT_ETHER) {
849 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
853 #if defined(INET) || defined(INET6)
854 /* Use defaults for TSO, if nothing is set */
855 if (ifp->if_hw_tsomax == 0 &&
856 ifp->if_hw_tsomaxsegcount == 0 &&
857 ifp->if_hw_tsomaxsegsize == 0) {
859 * The TSO defaults needs to be such that an
860 * NFS mbuf list of 35 mbufs totalling just
861 * below 64K works and that a chain of mbufs
862 * can be defragged into at most 32 segments:
864 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
865 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
866 ifp->if_hw_tsomaxsegcount = 35;
867 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
869 /* XXX some drivers set IFCAP_TSO after ethernet attach */
870 if (ifp->if_capabilities & IFCAP_TSO) {
871 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
873 ifp->if_hw_tsomaxsegcount,
874 ifp->if_hw_tsomaxsegsize);
882 * Update the interface index in the link layer address
885 for (ifa = ifp->if_addr; ifa != NULL;
886 ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
887 if (ifa->ifa_addr->sa_family == AF_LINK) {
888 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
889 sdl->sdl_index = ifp->if_index;
896 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
898 curvnet->vnet_ifcnt++;
902 if (domain_init_status >= 2)
903 if_attachdomain1(ifp);
905 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
906 if (IS_DEFAULT_VNET(curvnet))
907 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
909 /* Announce the interface. */
910 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
914 if_epochalloc(void *dummy __unused)
917 net_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
918 net_epoch = epoch_alloc(0);
920 SYSINIT(ifepochalloc, SI_SUB_TASKQ + 1, SI_ORDER_ANY,
921 if_epochalloc, NULL);
924 if_attachdomain(void *dummy)
928 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
929 if_attachdomain1(ifp);
931 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
932 if_attachdomain, NULL);
935 if_attachdomain1(struct ifnet *ifp)
940 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
941 * cannot lock ifp->if_afdata initialization, entirely.
944 if (ifp->if_afdata_initialized >= domain_init_status) {
945 IF_AFDATA_UNLOCK(ifp);
946 log(LOG_WARNING, "%s called more than once on %s\n",
947 __func__, ifp->if_xname);
950 ifp->if_afdata_initialized = domain_init_status;
951 IF_AFDATA_UNLOCK(ifp);
953 /* address family dependent data region */
954 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
955 for (dp = domains; dp; dp = dp->dom_next) {
956 if (dp->dom_ifattach)
957 ifp->if_afdata[dp->dom_family] =
958 (*dp->dom_ifattach)(ifp);
963 * Remove any unicast or broadcast network addresses from an interface.
966 if_purgeaddrs(struct ifnet *ifp)
968 struct ifaddr *ifa, *next;
971 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
972 if (ifa->ifa_addr->sa_family == AF_LINK)
975 /* XXX: Ugly!! ad hoc just for INET */
976 if (ifa->ifa_addr->sa_family == AF_INET) {
977 struct ifaliasreq ifr;
979 bzero(&ifr, sizeof(ifr));
980 ifr.ifra_addr = *ifa->ifa_addr;
981 if (ifa->ifa_dstaddr)
982 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
983 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
989 if (ifa->ifa_addr->sa_family == AF_INET6) {
991 /* ifp_addrhead is already updated */
996 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
997 IF_ADDR_WUNLOCK(ifp);
1004 * Remove any multicast network addresses from an interface when an ifnet
1008 if_purgemaddrs(struct ifnet *ifp)
1010 struct ifmultiaddr *ifma;
1013 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1014 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1015 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1016 if_delmulti_locked(ifp, ifma, 1);
1018 IF_ADDR_WUNLOCK(ifp);
1022 * Detach an interface, removing it from the list of "active" interfaces.
1023 * If vmove flag is set on entry to if_detach_internal(), perform only a
1024 * limited subset of cleanup tasks, given that we are moving an ifnet from
1025 * one vnet to another, where it must be fully operational.
1027 * XXXRW: There are some significant questions about event ordering, and
1028 * how to prevent things from starting to use the interface during detach.
1031 if_detach(struct ifnet *ifp)
1034 CURVNET_SET_QUIET(ifp->if_vnet);
1035 if_detach_internal(ifp, 0, NULL);
1040 * The vmove flag, if set, indicates that we are called from a callpath
1041 * that is moving an interface to a different vnet instance.
1043 * The shutdown flag, if set, indicates that we are called in the
1044 * process of shutting down a vnet instance. Currently only the
1045 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1046 * on a vnet instance shutdown without this flag being set, e.g., when
1047 * the cloned interfaces are destoyed as first thing of teardown.
1050 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1060 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1061 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1064 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
1066 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
1073 * While we would want to panic here, we cannot
1074 * guarantee that the interface is indeed still on
1075 * the list given we don't hold locks all the way.
1080 panic("%s: ifp=%p not on the ifnet tailq %p",
1081 __func__, ifp, &V_ifnet);
1083 return; /* XXX this should panic as well? */
1088 * At this point we know the interface still was on the ifnet list
1089 * and we removed it so we are in a stable state.
1092 curvnet->vnet_ifcnt--;
1094 epoch_wait_preempt(net_epoch_preempt);
1096 * In any case (destroy or vmove) detach us from the groups
1097 * and remove/wait for pending events on the taskq.
1098 * XXX-BZ in theory an interface could still enqueue a taskq change?
1102 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1105 * Check if this is a cloned interface or not. Must do even if
1106 * shutting down as a if_vmove_reclaim() would move the ifp and
1107 * the if_clone_addgroup() will have a corrupted string overwise
1108 * from a gibberish pointer.
1110 if (vmove && ifcp != NULL)
1111 *ifcp = if_clone_findifc(ifp);
1117 * On VNET shutdown abort here as the stack teardown will do all
1118 * the work top-down for us.
1122 * In case of a vmove we are done here without error.
1123 * If we would signal an error it would lead to the same
1124 * abort as if we did not find the ifnet anymore.
1125 * if_detach() calls us in void context and does not care
1126 * about an early abort notification, so life is splendid :)
1128 goto finish_vnet_shutdown;
1133 * At this point we are not tearing down a VNET and are either
1134 * going to destroy or vmove the interface and have to cleanup
1139 * Remove routes and flush queues.
1142 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1143 altq_disable(&ifp->if_snd);
1144 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1145 altq_detach(&ifp->if_snd);
1156 * Remove all IPv6 kernel structs related to ifp. This should be done
1157 * before removing routing entries below, since IPv6 interface direct
1158 * routes are expected to be removed by the IPv6-specific kernel API.
1159 * Otherwise, the kernel will detect some inconsistency and bark it.
1163 if_purgemaddrs(ifp);
1165 /* Announce that the interface is gone. */
1166 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1167 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1168 if (IS_DEFAULT_VNET(curvnet))
1169 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1173 * Prevent further calls into the device driver via ifnet.
1178 * Remove link ifaddr pointer and maybe decrement if_index.
1179 * Clean up all addresses.
1181 free(ifp->if_hw_addr, M_IFADDR);
1182 ifp->if_hw_addr = NULL;
1183 ifp->if_addr = NULL;
1185 /* We can now free link ifaddr. */
1187 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1188 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1189 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1190 IF_ADDR_WUNLOCK(ifp);
1193 IF_ADDR_WUNLOCK(ifp);
1196 rt_flushifroutes(ifp);
1199 finish_vnet_shutdown:
1202 * We cannot hold the lock over dom_ifdetach calls as they might
1203 * sleep, for example trying to drain a callout, thus open up the
1204 * theoretical race with re-attaching.
1206 IF_AFDATA_LOCK(ifp);
1207 i = ifp->if_afdata_initialized;
1208 ifp->if_afdata_initialized = 0;
1209 IF_AFDATA_UNLOCK(ifp);
1210 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1211 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1212 (*dp->dom_ifdetach)(ifp,
1213 ifp->if_afdata[dp->dom_family]);
1214 ifp->if_afdata[dp->dom_family] = NULL;
1223 * if_vmove() performs a limited version of if_detach() in current
1224 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1225 * An attempt is made to shrink if_index in current vnet, find an
1226 * unused if_index in target vnet and calls if_grow() if necessary,
1227 * and finally find an unused if_xname for the target vnet.
1230 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1232 struct if_clone *ifc;
1233 u_int bif_dlt, bif_hdrlen;
1238 * if_detach_internal() will call the eventhandler to notify
1239 * interface departure. That will detach if_bpf. We need to
1240 * safe the dlt and hdrlen so we can re-attach it later.
1242 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1245 * Detach from current vnet, but preserve LLADDR info, do not
1246 * mark as dead etc. so that the ifnet can be reattached later.
1247 * If we cannot find it, we lost the race to someone else.
1249 rc = if_detach_internal(ifp, 1, &ifc);
1254 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1255 * the if_index for that vnet if possible.
1257 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1258 * or we'd lock on one vnet and unlock on another.
1261 ifindex_free_locked(ifp->if_index);
1265 * Perform interface-specific reassignment tasks, if provided by
1268 if (ifp->if_reassign != NULL)
1269 ifp->if_reassign(ifp, new_vnet, NULL);
1272 * Switch to the context of the target vnet.
1274 CURVNET_SET_QUIET(new_vnet);
1277 ifp->if_index = ifindex_alloc(&old);
1278 if (__predict_false(ifp->if_index == USHRT_MAX)) {
1280 epoch_wait_preempt(net_epoch_preempt);
1284 ifnet_setbyindex(ifp->if_index, ifp);
1287 if_attach_internal(ifp, 1, ifc);
1289 if (ifp->if_bpf == NULL)
1290 bpfattach(ifp, bif_dlt, bif_hdrlen);
1296 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1299 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1305 /* Try to find the prison within our visibility. */
1306 sx_slock(&allprison_lock);
1307 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1308 sx_sunlock(&allprison_lock);
1311 prison_hold_locked(pr);
1312 mtx_unlock(&pr->pr_mtx);
1314 /* Do not try to move the iface from and to the same prison. */
1315 if (pr->pr_vnet == ifp->if_vnet) {
1320 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1321 /* XXX Lock interfaces to avoid races. */
1322 CURVNET_SET_QUIET(pr->pr_vnet);
1323 difp = ifunit(ifname);
1330 /* Make sure the VNET is stable. */
1331 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1332 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1340 /* Move the interface into the child jail/vnet. */
1341 if_vmove(ifp, pr->pr_vnet);
1343 /* Report the new if_xname back to the userland. */
1344 sprintf(ifname, "%s", ifp->if_xname);
1351 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1354 struct vnet *vnet_dst;
1358 /* Try to find the prison within our visibility. */
1359 sx_slock(&allprison_lock);
1360 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1361 sx_sunlock(&allprison_lock);
1364 prison_hold_locked(pr);
1365 mtx_unlock(&pr->pr_mtx);
1367 /* Make sure the named iface exists in the source prison/vnet. */
1368 CURVNET_SET(pr->pr_vnet);
1369 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1376 /* Do not try to move the iface from and to the same prison. */
1377 vnet_dst = TD_TO_VNET(td);
1378 if (vnet_dst == ifp->if_vnet) {
1384 /* Make sure the VNET is stable. */
1385 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1386 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1393 /* Get interface back from child jail/vnet. */
1394 if_vmove(ifp, vnet_dst);
1397 /* Report the new if_xname back to the userland. */
1398 sprintf(ifname, "%s", ifp->if_xname);
1406 * Add a group to an interface
1409 if_addgroup(struct ifnet *ifp, const char *groupname)
1411 struct ifg_list *ifgl;
1412 struct ifg_group *ifg = NULL;
1413 struct ifg_member *ifgm;
1416 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1417 groupname[strlen(groupname) - 1] <= '9')
1421 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1422 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1427 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1428 M_NOWAIT)) == NULL) {
1433 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1434 M_TEMP, M_NOWAIT)) == NULL) {
1440 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1441 if (!strcmp(ifg->ifg_group, groupname))
1445 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1446 M_TEMP, M_NOWAIT)) == NULL) {
1452 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1453 ifg->ifg_refcnt = 0;
1454 CK_STAILQ_INIT(&ifg->ifg_members);
1455 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1460 ifgl->ifgl_group = ifg;
1461 ifgm->ifgm_ifp = ifp;
1464 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1465 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1466 IF_ADDR_WUNLOCK(ifp);
1471 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1472 EVENTHANDLER_INVOKE(group_change_event, groupname);
1478 * Remove a group from an interface
1481 if_delgroup(struct ifnet *ifp, const char *groupname)
1483 struct ifg_list *ifgl;
1484 struct ifg_member *ifgm;
1488 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1489 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1498 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1499 IF_ADDR_WUNLOCK(ifp);
1501 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1502 if (ifgm->ifgm_ifp == ifp)
1506 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member, ifgm_next);
1508 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1509 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1514 epoch_wait_preempt(net_epoch_preempt);
1516 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1517 free(ifgl->ifgl_group, M_TEMP);
1522 EVENTHANDLER_INVOKE(group_change_event, groupname);
1528 * Remove an interface from all groups
1531 if_delgroups(struct ifnet *ifp)
1533 struct ifg_list *ifgl;
1534 struct ifg_member *ifgm;
1535 char groupname[IFNAMSIZ];
1539 while (!CK_STAILQ_EMPTY(&ifp->if_groups)) {
1540 ifgl = CK_STAILQ_FIRST(&ifp->if_groups);
1542 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1545 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1546 IF_ADDR_WUNLOCK(ifp);
1548 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1549 if (ifgm->ifgm_ifp == ifp)
1553 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifg_member,
1556 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1557 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group, ifg_next);
1562 epoch_wait_preempt(net_epoch_preempt);
1565 EVENTHANDLER_INVOKE(group_detach_event,
1567 free(ifgl->ifgl_group, M_TEMP);
1569 EVENTHANDLER_INVOKE(group_change_event, groupname);
1577 ifgr_group_get(void *ifgrp)
1579 union ifgroupreq_union *ifgrup;
1582 #ifdef COMPAT_FREEBSD32
1583 if (SV_CURPROC_FLAG(SV_ILP32))
1584 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1586 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1589 static struct ifg_req *
1590 ifgr_groups_get(void *ifgrp)
1592 union ifgroupreq_union *ifgrup;
1595 #ifdef COMPAT_FREEBSD32
1596 if (SV_CURPROC_FLAG(SV_ILP32))
1597 return ((struct ifg_req *)(uintptr_t)
1598 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1600 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1604 * Stores all groups from an interface in memory pointed to by ifgr.
1607 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1610 struct ifg_list *ifgl;
1611 struct ifg_req ifgrq, *ifgp;
1613 if (ifgr->ifgr_len == 0) {
1615 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1616 ifgr->ifgr_len += sizeof(struct ifg_req);
1617 IF_ADDR_RUNLOCK(ifp);
1621 len = ifgr->ifgr_len;
1622 ifgp = ifgr_groups_get(ifgr);
1625 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1626 if (len < sizeof(ifgrq)) {
1627 IF_ADDR_RUNLOCK(ifp);
1630 bzero(&ifgrq, sizeof ifgrq);
1631 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1632 sizeof(ifgrq.ifgrq_group));
1633 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1634 IF_ADDR_RUNLOCK(ifp);
1637 len -= sizeof(ifgrq);
1640 IF_ADDR_RUNLOCK(ifp);
1646 * Stores all members of a group in memory pointed to by igfr
1649 if_getgroupmembers(struct ifgroupreq *ifgr)
1651 struct ifg_group *ifg;
1652 struct ifg_member *ifgm;
1653 struct ifg_req ifgrq, *ifgp;
1657 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1658 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1665 if (ifgr->ifgr_len == 0) {
1666 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1667 ifgr->ifgr_len += sizeof(ifgrq);
1672 len = ifgr->ifgr_len;
1673 ifgp = ifgr_groups_get(ifgr);
1674 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1675 if (len < sizeof(ifgrq)) {
1679 bzero(&ifgrq, sizeof ifgrq);
1680 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1681 sizeof(ifgrq.ifgrq_member));
1682 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1686 len -= sizeof(ifgrq);
1695 * Return counter values from counter(9)s stored in ifnet.
1698 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1701 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1703 return (counter_u64_fetch(ifp->if_counters[cnt]));
1707 * Increase an ifnet counter. Usually used for counters shared
1708 * between the stack and a driver, but function supports them all.
1711 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1714 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1716 counter_u64_add(ifp->if_counters[cnt], inc);
1720 * Copy data from ifnet to userland API structure if_data.
1723 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1726 ifd->ifi_type = ifp->if_type;
1727 ifd->ifi_physical = 0;
1728 ifd->ifi_addrlen = ifp->if_addrlen;
1729 ifd->ifi_hdrlen = ifp->if_hdrlen;
1730 ifd->ifi_link_state = ifp->if_link_state;
1732 ifd->ifi_datalen = sizeof(struct if_data);
1733 ifd->ifi_mtu = ifp->if_mtu;
1734 ifd->ifi_metric = ifp->if_metric;
1735 ifd->ifi_baudrate = ifp->if_baudrate;
1736 ifd->ifi_hwassist = ifp->if_hwassist;
1737 ifd->ifi_epoch = ifp->if_epoch;
1738 ifd->ifi_lastchange = ifp->if_lastchange;
1740 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1741 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1742 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1743 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1744 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1745 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1746 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1747 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1748 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1749 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1750 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1751 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1755 * Wrapper functions for struct ifnet address list locking macros. These are
1756 * used by kernel modules to avoid encoding programming interface or binary
1757 * interface assumptions that may be violated when kernel-internal locking
1758 * approaches change.
1761 if_addr_rlock(struct ifnet *ifp)
1763 MPASS(*(uint64_t *)&ifp->if_addr_et == 0);
1764 epoch_enter_preempt(net_epoch_preempt, &ifp->if_addr_et);
1768 if_addr_runlock(struct ifnet *ifp)
1770 epoch_exit_preempt(net_epoch_preempt, &ifp->if_addr_et);
1772 bzero(&ifp->if_addr_et, sizeof(struct epoch_tracker));
1777 if_maddr_rlock(if_t ifp)
1780 MPASS(*(uint64_t *)&ifp->if_maddr_et == 0);
1781 epoch_enter_preempt(net_epoch_preempt, &ifp->if_maddr_et);
1785 if_maddr_runlock(if_t ifp)
1788 epoch_exit_preempt(net_epoch_preempt, &ifp->if_maddr_et);
1790 bzero(&ifp->if_maddr_et, sizeof(struct epoch_tracker));
1795 * Initialization, destruction and refcounting functions for ifaddrs.
1798 ifa_alloc(size_t size, int flags)
1802 KASSERT(size >= sizeof(struct ifaddr),
1803 ("%s: invalid size %zu", __func__, size));
1805 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1809 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1811 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1813 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1815 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1818 refcount_init(&ifa->ifa_refcnt, 1);
1823 /* free(NULL) is okay */
1824 counter_u64_free(ifa->ifa_opackets);
1825 counter_u64_free(ifa->ifa_ipackets);
1826 counter_u64_free(ifa->ifa_obytes);
1827 counter_u64_free(ifa->ifa_ibytes);
1828 free(ifa, M_IFADDR);
1834 ifa_ref(struct ifaddr *ifa)
1837 refcount_acquire(&ifa->ifa_refcnt);
1841 ifa_destroy(epoch_context_t ctx)
1845 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1846 counter_u64_free(ifa->ifa_opackets);
1847 counter_u64_free(ifa->ifa_ipackets);
1848 counter_u64_free(ifa->ifa_obytes);
1849 counter_u64_free(ifa->ifa_ibytes);
1850 free(ifa, M_IFADDR);
1854 ifa_free(struct ifaddr *ifa)
1857 if (refcount_release(&ifa->ifa_refcnt))
1858 epoch_call(net_epoch_preempt, &ifa->ifa_epoch_ctx, ifa_destroy);
1863 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1864 struct sockaddr *ia)
1867 struct rt_addrinfo info;
1868 struct sockaddr_dl null_sdl;
1873 bzero(&info, sizeof(info));
1874 if (cmd != RTM_DELETE)
1875 info.rti_ifp = V_loif;
1876 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1877 info.rti_info[RTAX_DST] = ia;
1878 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1879 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1881 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1884 !(cmd == RTM_ADD && error == EEXIST) &&
1885 !(cmd == RTM_DELETE && error == ENOENT))
1886 if_printf(ifp, "%s failed: %d\n", otype, error);
1892 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1895 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1899 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1902 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1906 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1909 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1913 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1914 * structs used to represent other address families, it is necessary
1915 * to perform a different comparison.
1918 #define sa_dl_equal(a1, a2) \
1919 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1920 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1921 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1922 CLLADDR((const struct sockaddr_dl *)(a2)), \
1923 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1926 * Locate an interface based on a complete address.
1930 ifa_ifwithaddr(const struct sockaddr *addr)
1935 MPASS(in_epoch(net_epoch_preempt));
1936 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1937 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1938 if (ifa->ifa_addr->sa_family != addr->sa_family)
1940 if (sa_equal(addr, ifa->ifa_addr)) {
1943 /* IP6 doesn't have broadcast */
1944 if ((ifp->if_flags & IFF_BROADCAST) &&
1945 ifa->ifa_broadaddr &&
1946 ifa->ifa_broadaddr->sa_len != 0 &&
1947 sa_equal(ifa->ifa_broadaddr, addr)) {
1958 ifa_ifwithaddr_check(const struct sockaddr *addr)
1963 rc = (ifa_ifwithaddr(addr) != NULL);
1969 * Locate an interface based on the broadcast address.
1973 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1978 MPASS(in_epoch(net_epoch_preempt));
1979 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1980 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1982 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1983 if (ifa->ifa_addr->sa_family != addr->sa_family)
1985 if ((ifp->if_flags & IFF_BROADCAST) &&
1986 ifa->ifa_broadaddr &&
1987 ifa->ifa_broadaddr->sa_len != 0 &&
1988 sa_equal(ifa->ifa_broadaddr, addr)) {
1999 * Locate the point to point interface with a given destination address.
2003 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
2008 MPASS(in_epoch(net_epoch_preempt));
2009 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2010 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2012 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2014 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2015 if (ifa->ifa_addr->sa_family != addr->sa_family)
2017 if (ifa->ifa_dstaddr != NULL &&
2018 sa_equal(addr, ifa->ifa_dstaddr)) {
2029 * Find an interface on a specific network. If many, choice
2030 * is most specific found.
2033 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2037 struct ifaddr *ifa_maybe = NULL;
2038 u_int af = addr->sa_family;
2039 const char *addr_data = addr->sa_data, *cplim;
2041 MPASS(in_epoch(net_epoch_preempt));
2043 * AF_LINK addresses can be looked up directly by their index number,
2044 * so do that if we can.
2046 if (af == AF_LINK) {
2047 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2048 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2049 return (ifaddr_byindex(sdl->sdl_index));
2053 * Scan though each interface, looking for ones that have addresses
2054 * in this address family and the requested fib. Maintain a reference
2055 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2056 * kept it stable when we move onto the next interface.
2058 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2059 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2061 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2062 const char *cp, *cp2, *cp3;
2064 if (ifa->ifa_addr->sa_family != af)
2066 if (af == AF_INET &&
2067 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2069 * This is a bit broken as it doesn't
2070 * take into account that the remote end may
2071 * be a single node in the network we are
2073 * The trouble is that we don't know the
2074 * netmask for the remote end.
2076 if (ifa->ifa_dstaddr != NULL &&
2077 sa_equal(addr, ifa->ifa_dstaddr)) {
2082 * Scan all the bits in the ifa's address.
2083 * If a bit dissagrees with what we are
2084 * looking for, mask it with the netmask
2085 * to see if it really matters.
2086 * (A byte at a time)
2088 if (ifa->ifa_netmask == 0)
2091 cp2 = ifa->ifa_addr->sa_data;
2092 cp3 = ifa->ifa_netmask->sa_data;
2093 cplim = ifa->ifa_netmask->sa_len
2094 + (char *)ifa->ifa_netmask;
2096 if ((*cp++ ^ *cp2++) & *cp3++)
2097 goto next; /* next address! */
2099 * If the netmask of what we just found
2100 * is more specific than what we had before
2101 * (if we had one), or if the virtual status
2102 * of new prefix is better than of the old one,
2103 * then remember the new one before continuing
2104 * to search for an even better one.
2106 if (ifa_maybe == NULL ||
2107 ifa_preferred(ifa_maybe, ifa) ||
2108 rn_refines((caddr_t)ifa->ifa_netmask,
2109 (caddr_t)ifa_maybe->ifa_netmask)) {
2122 * Find an interface address specific to an interface best matching
2126 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2129 const char *cp, *cp2, *cp3;
2131 struct ifaddr *ifa_maybe = NULL;
2132 u_int af = addr->sa_family;
2137 MPASS(in_epoch(net_epoch_preempt));
2138 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2139 if (ifa->ifa_addr->sa_family != af)
2141 if (ifa_maybe == NULL)
2143 if (ifa->ifa_netmask == 0) {
2144 if (sa_equal(addr, ifa->ifa_addr) ||
2145 (ifa->ifa_dstaddr &&
2146 sa_equal(addr, ifa->ifa_dstaddr)))
2150 if (ifp->if_flags & IFF_POINTOPOINT) {
2151 if (sa_equal(addr, ifa->ifa_dstaddr))
2155 cp2 = ifa->ifa_addr->sa_data;
2156 cp3 = ifa->ifa_netmask->sa_data;
2157 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2158 for (; cp3 < cplim; cp3++)
2159 if ((*cp++ ^ *cp2++) & *cp3)
2171 * See whether new ifa is better than current one:
2172 * 1) A non-virtual one is preferred over virtual.
2173 * 2) A virtual in master state preferred over any other state.
2175 * Used in several address selecting functions.
2178 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2181 return (cur->ifa_carp && (!next->ifa_carp ||
2182 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2185 #include <net/if_llatbl.h>
2188 * Default action when installing a route with a Link Level gateway.
2189 * Lookup an appropriate real ifa to point to.
2190 * This should be moved to /sys/net/link.c eventually.
2193 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2195 struct ifaddr *ifa, *oifa;
2196 struct sockaddr *dst;
2199 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2200 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2203 ifa = ifaof_ifpforaddr(dst, ifp);
2211 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2212 ifa->ifa_rtrequest(cmd, rt, info);
2217 struct sockaddr_dl *
2218 link_alloc_sdl(size_t size, int flags)
2221 return (malloc(size, M_TEMP, flags));
2225 link_free_sdl(struct sockaddr *sa)
2231 * Fills in given sdl with interface basic info.
2232 * Returns pointer to filled sdl.
2234 struct sockaddr_dl *
2235 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2237 struct sockaddr_dl *sdl;
2239 sdl = (struct sockaddr_dl *)paddr;
2240 memset(sdl, 0, sizeof(struct sockaddr_dl));
2241 sdl->sdl_len = sizeof(struct sockaddr_dl);
2242 sdl->sdl_family = AF_LINK;
2243 sdl->sdl_index = ifp->if_index;
2244 sdl->sdl_type = iftype;
2250 * Mark an interface down and notify protocols of
2254 if_unroute(struct ifnet *ifp, int flag, int fam)
2258 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2260 ifp->if_flags &= ~flag;
2261 getmicrotime(&ifp->if_lastchange);
2262 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2263 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2264 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2265 ifp->if_qflush(ifp);
2268 (*carp_linkstate_p)(ifp);
2273 * Mark an interface up and notify protocols of
2277 if_route(struct ifnet *ifp, int flag, int fam)
2281 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2283 ifp->if_flags |= flag;
2284 getmicrotime(&ifp->if_lastchange);
2285 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2286 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2287 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2289 (*carp_linkstate_p)(ifp);
2296 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2297 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2298 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2299 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2300 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2301 int (*vlan_setcookie_p)(struct ifnet *, void *);
2302 void *(*vlan_cookie_p)(struct ifnet *);
2305 * Handle a change in the interface link state. To avoid LORs
2306 * between driver lock and upper layer locks, as well as possible
2307 * recursions, we post event to taskqueue, and all job
2308 * is done in static do_link_state_change().
2311 if_link_state_change(struct ifnet *ifp, int link_state)
2313 /* Return if state hasn't changed. */
2314 if (ifp->if_link_state == link_state)
2317 ifp->if_link_state = link_state;
2319 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2323 do_link_state_change(void *arg, int pending)
2325 struct ifnet *ifp = (struct ifnet *)arg;
2326 int link_state = ifp->if_link_state;
2327 CURVNET_SET(ifp->if_vnet);
2329 /* Notify that the link state has changed. */
2331 if (ifp->if_vlantrunk != NULL)
2332 (*vlan_link_state_p)(ifp);
2334 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2335 ifp->if_l2com != NULL)
2336 (*ng_ether_link_state_p)(ifp, link_state);
2338 (*carp_linkstate_p)(ifp);
2340 ifp->if_bridge_linkstate(ifp);
2342 (*lagg_linkstate_p)(ifp, link_state);
2344 if (IS_DEFAULT_VNET(curvnet))
2345 devctl_notify("IFNET", ifp->if_xname,
2346 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2349 if_printf(ifp, "%d link states coalesced\n", pending);
2350 if (log_link_state_change)
2351 if_printf(ifp, "link state changed to %s\n",
2352 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2353 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2358 * Mark an interface down and notify protocols of
2362 if_down(struct ifnet *ifp)
2365 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2366 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2370 * Mark an interface up and notify protocols of
2374 if_up(struct ifnet *ifp)
2377 if_route(ifp, IFF_UP, AF_UNSPEC);
2378 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2382 * Flush an interface queue.
2385 if_qflush(struct ifnet *ifp)
2393 if (ALTQ_IS_ENABLED(ifq))
2397 while ((m = n) != NULL) {
2408 * Map interface name to interface structure pointer, with or without
2409 * returning a reference.
2412 ifunit_ref(const char *name)
2416 IFNET_RLOCK_NOSLEEP();
2417 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2418 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2419 !(ifp->if_flags & IFF_DYING))
2424 IFNET_RUNLOCK_NOSLEEP();
2429 ifunit(const char *name)
2433 IFNET_RLOCK_NOSLEEP();
2434 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2435 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2438 IFNET_RUNLOCK_NOSLEEP();
2443 ifr_buffer_get_buffer(void *data)
2445 union ifreq_union *ifrup;
2448 #ifdef COMPAT_FREEBSD32
2449 if (SV_CURPROC_FLAG(SV_ILP32))
2450 return ((void *)(uintptr_t)
2451 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2453 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2457 ifr_buffer_set_buffer_null(void *data)
2459 union ifreq_union *ifrup;
2462 #ifdef COMPAT_FREEBSD32
2463 if (SV_CURPROC_FLAG(SV_ILP32))
2464 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2467 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2471 ifr_buffer_get_length(void *data)
2473 union ifreq_union *ifrup;
2476 #ifdef COMPAT_FREEBSD32
2477 if (SV_CURPROC_FLAG(SV_ILP32))
2478 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2480 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2484 ifr_buffer_set_length(void *data, size_t len)
2486 union ifreq_union *ifrup;
2489 #ifdef COMPAT_FREEBSD32
2490 if (SV_CURPROC_FLAG(SV_ILP32))
2491 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2494 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2498 ifr_data_get_ptr(void *ifrp)
2500 union ifreq_union *ifrup;
2503 #ifdef COMPAT_FREEBSD32
2504 if (SV_CURPROC_FLAG(SV_ILP32))
2505 return ((void *)(uintptr_t)
2506 ifrup->ifr32.ifr_ifru.ifru_data);
2508 return (ifrup->ifr.ifr_ifru.ifru_data);
2512 * Hardware specific interface ioctls.
2515 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2518 int error = 0, do_ifup = 0;
2519 int new_flags, temp_flags;
2520 size_t namelen, onamelen;
2522 char *descrbuf, *odescrbuf;
2523 char new_name[IFNAMSIZ];
2525 struct sockaddr_dl *sdl;
2527 ifr = (struct ifreq *)data;
2530 ifr->ifr_index = ifp->if_index;
2534 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2535 ifr->ifr_flags = temp_flags & 0xffff;
2536 ifr->ifr_flagshigh = temp_flags >> 16;
2540 ifr->ifr_reqcap = ifp->if_capabilities;
2541 ifr->ifr_curcap = ifp->if_capenable;
2546 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2551 ifr->ifr_metric = ifp->if_metric;
2555 ifr->ifr_mtu = ifp->if_mtu;
2559 /* XXXGL: did this ever worked? */
2565 sx_slock(&ifdescr_sx);
2566 if (ifp->if_description == NULL)
2569 /* space for terminating nul */
2570 descrlen = strlen(ifp->if_description) + 1;
2571 if (ifr_buffer_get_length(ifr) < descrlen)
2572 ifr_buffer_set_buffer_null(ifr);
2574 error = copyout(ifp->if_description,
2575 ifr_buffer_get_buffer(ifr), descrlen);
2576 ifr_buffer_set_length(ifr, descrlen);
2578 sx_sunlock(&ifdescr_sx);
2582 error = priv_check(td, PRIV_NET_SETIFDESCR);
2587 * Copy only (length-1) bytes to make sure that
2588 * if_description is always nul terminated. The
2589 * length parameter is supposed to count the
2590 * terminating nul in.
2592 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2593 return (ENAMETOOLONG);
2594 else if (ifr_buffer_get_length(ifr) == 0)
2597 descrbuf = malloc(ifr_buffer_get_length(ifr),
2598 M_IFDESCR, M_WAITOK | M_ZERO);
2599 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2600 ifr_buffer_get_length(ifr) - 1);
2602 free(descrbuf, M_IFDESCR);
2607 sx_xlock(&ifdescr_sx);
2608 odescrbuf = ifp->if_description;
2609 ifp->if_description = descrbuf;
2610 sx_xunlock(&ifdescr_sx);
2612 getmicrotime(&ifp->if_lastchange);
2613 free(odescrbuf, M_IFDESCR);
2617 ifr->ifr_fib = ifp->if_fib;
2621 error = priv_check(td, PRIV_NET_SETIFFIB);
2624 if (ifr->ifr_fib >= rt_numfibs)
2627 ifp->if_fib = ifr->ifr_fib;
2631 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2635 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2636 * check, so we don't need special handling here yet.
2638 new_flags = (ifr->ifr_flags & 0xffff) |
2639 (ifr->ifr_flagshigh << 16);
2640 if (ifp->if_flags & IFF_UP &&
2641 (new_flags & IFF_UP) == 0) {
2643 } else if (new_flags & IFF_UP &&
2644 (ifp->if_flags & IFF_UP) == 0) {
2647 /* See if permanently promiscuous mode bit is about to flip */
2648 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2649 if (new_flags & IFF_PPROMISC)
2650 ifp->if_flags |= IFF_PROMISC;
2651 else if (ifp->if_pcount == 0)
2652 ifp->if_flags &= ~IFF_PROMISC;
2653 if (log_promisc_mode_change)
2654 if_printf(ifp, "permanently promiscuous mode %s\n",
2655 ((new_flags & IFF_PPROMISC) ?
2656 "enabled" : "disabled"));
2658 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2659 (new_flags &~ IFF_CANTCHANGE);
2660 if (ifp->if_ioctl) {
2661 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2665 getmicrotime(&ifp->if_lastchange);
2669 error = priv_check(td, PRIV_NET_SETIFCAP);
2672 if (ifp->if_ioctl == NULL)
2673 return (EOPNOTSUPP);
2674 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2676 error = (*ifp->if_ioctl)(ifp, cmd, data);
2678 getmicrotime(&ifp->if_lastchange);
2683 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2688 error = priv_check(td, PRIV_NET_SETIFNAME);
2691 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2695 if (new_name[0] == '\0')
2697 if (new_name[IFNAMSIZ-1] != '\0') {
2698 new_name[IFNAMSIZ-1] = '\0';
2699 if (strlen(new_name) == IFNAMSIZ-1)
2702 if (ifunit(new_name) != NULL)
2706 * XXX: Locking. Nothing else seems to lock if_flags,
2707 * and there are numerous other races with the
2708 * ifunit() checks not being atomic with namespace
2709 * changes (renames, vmoves, if_attach, etc).
2711 ifp->if_flags |= IFF_RENAMING;
2713 /* Announce the departure of the interface. */
2714 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2715 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2717 if_printf(ifp, "changing name to '%s'\n", new_name);
2720 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2722 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2723 namelen = strlen(new_name);
2724 onamelen = sdl->sdl_nlen;
2726 * Move the address if needed. This is safe because we
2727 * allocate space for a name of length IFNAMSIZ when we
2728 * create this in if_attach().
2730 if (namelen != onamelen) {
2731 bcopy(sdl->sdl_data + onamelen,
2732 sdl->sdl_data + namelen, sdl->sdl_alen);
2734 bcopy(new_name, sdl->sdl_data, namelen);
2735 sdl->sdl_nlen = namelen;
2736 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2737 bzero(sdl->sdl_data, onamelen);
2738 while (namelen != 0)
2739 sdl->sdl_data[--namelen] = 0xff;
2740 IF_ADDR_WUNLOCK(ifp);
2742 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2743 /* Announce the return of the interface. */
2744 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2746 ifp->if_flags &= ~IFF_RENAMING;
2751 error = priv_check(td, PRIV_NET_SETIFVNET);
2754 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2759 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2762 ifp->if_metric = ifr->ifr_metric;
2763 getmicrotime(&ifp->if_lastchange);
2767 error = priv_check(td, PRIV_NET_SETIFPHYS);
2770 if (ifp->if_ioctl == NULL)
2771 return (EOPNOTSUPP);
2772 error = (*ifp->if_ioctl)(ifp, cmd, data);
2774 getmicrotime(&ifp->if_lastchange);
2779 u_long oldmtu = ifp->if_mtu;
2781 error = priv_check(td, PRIV_NET_SETIFMTU);
2784 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2786 if (ifp->if_ioctl == NULL)
2787 return (EOPNOTSUPP);
2788 error = (*ifp->if_ioctl)(ifp, cmd, data);
2790 getmicrotime(&ifp->if_lastchange);
2793 NETDUMP_REINIT(ifp);
2797 * If the link MTU changed, do network layer specific procedure.
2799 if (ifp->if_mtu != oldmtu) {
2810 if (cmd == SIOCADDMULTI)
2811 error = priv_check(td, PRIV_NET_ADDMULTI);
2813 error = priv_check(td, PRIV_NET_DELMULTI);
2817 /* Don't allow group membership on non-multicast interfaces. */
2818 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2819 return (EOPNOTSUPP);
2821 /* Don't let users screw up protocols' entries. */
2822 if (ifr->ifr_addr.sa_family != AF_LINK)
2825 if (cmd == SIOCADDMULTI) {
2826 struct ifmultiaddr *ifma;
2829 * Userland is only permitted to join groups once
2830 * via the if_addmulti() KPI, because it cannot hold
2831 * struct ifmultiaddr * between calls. It may also
2832 * lose a race while we check if the membership
2836 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2837 IF_ADDR_RUNLOCK(ifp);
2841 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2843 error = if_delmulti(ifp, &ifr->ifr_addr);
2846 getmicrotime(&ifp->if_lastchange);
2849 case SIOCSIFPHYADDR:
2850 case SIOCDIFPHYADDR:
2852 case SIOCSIFPHYADDR_IN6:
2855 case SIOCSIFGENERIC:
2856 error = priv_check(td, PRIV_NET_HWIOCTL);
2859 if (ifp->if_ioctl == NULL)
2860 return (EOPNOTSUPP);
2861 error = (*ifp->if_ioctl)(ifp, cmd, data);
2863 getmicrotime(&ifp->if_lastchange);
2867 case SIOCGIFPSRCADDR:
2868 case SIOCGIFPDSTADDR:
2871 case SIOCGIFGENERIC:
2873 case SIOCGIFRSSHASH:
2874 if (ifp->if_ioctl == NULL)
2875 return (EOPNOTSUPP);
2876 error = (*ifp->if_ioctl)(ifp, cmd, data);
2880 error = priv_check(td, PRIV_NET_SETLLADDR);
2883 error = if_setlladdr(ifp,
2884 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2888 error = if_gethwaddr(ifp, ifr);
2891 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2892 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2895 if ((error = if_addgroup(ifp,
2896 ifgr_group_get((struct ifgroupreq *)data))))
2900 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2901 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2905 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2906 error = priv_check(td, PRIV_NET_DELIFGROUP);
2909 if ((error = if_delgroup(ifp,
2910 ifgr_group_get((struct ifgroupreq *)data))))
2921 #ifdef COMPAT_FREEBSD32
2929 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2932 #ifdef COMPAT_FREEBSD32
2934 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2936 struct ifmediareq32 *ifmr32;
2938 ifmr32 = (struct ifmediareq32 *)data;
2939 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2940 sizeof(ifmr->ifm_name));
2941 ifmr->ifm_current = ifmr32->ifm_current;
2942 ifmr->ifm_mask = ifmr32->ifm_mask;
2943 ifmr->ifm_status = ifmr32->ifm_status;
2944 ifmr->ifm_active = ifmr32->ifm_active;
2945 ifmr->ifm_count = ifmr32->ifm_count;
2946 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2950 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2952 struct ifmediareq32 *ifmr32;
2954 ifmr32 = (struct ifmediareq32 *)data;
2955 ifmr32->ifm_current = ifmr->ifm_current;
2956 ifmr32->ifm_mask = ifmr->ifm_mask;
2957 ifmr32->ifm_status = ifmr->ifm_status;
2958 ifmr32->ifm_active = ifmr->ifm_active;
2959 ifmr32->ifm_count = ifmr->ifm_count;
2967 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2969 #ifdef COMPAT_FREEBSD32
2970 caddr_t saved_data = NULL;
2971 struct ifmediareq ifmr;
2972 struct ifmediareq *ifmrp;
2982 CURVNET_SET(so->so_vnet);
2984 /* Make sure the VNET is stable. */
2985 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2986 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2996 error = ifconf(cmd, data);
3000 #ifdef COMPAT_FREEBSD32
3003 struct ifconf32 *ifc32;
3006 ifc32 = (struct ifconf32 *)data;
3007 ifc.ifc_len = ifc32->ifc_len;
3008 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3010 error = ifconf(SIOCGIFCONF, (void *)&ifc);
3013 ifc32->ifc_len = ifc.ifc_len;
3019 #ifdef COMPAT_FREEBSD32
3022 case SIOCGIFMEDIA32:
3023 case SIOCGIFXMEDIA32:
3025 ifmr_init(ifmrp, data);
3026 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3028 data = (caddr_t)ifmrp;
3032 ifr = (struct ifreq *)data;
3036 error = priv_check(td, PRIV_NET_SETIFVNET);
3038 error = if_vmove_reclaim(td, ifr->ifr_name,
3044 error = priv_check(td, PRIV_NET_IFCREATE);
3046 error = if_clone_create(ifr->ifr_name,
3047 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3048 ifr_data_get_ptr(ifr) : NULL);
3051 error = priv_check(td, PRIV_NET_IFDESTROY);
3053 error = if_clone_destroy(ifr->ifr_name);
3056 case SIOCIFGCLONERS:
3057 error = if_clone_list((struct if_clonereq *)data);
3060 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3061 error = if_getgroupmembers((struct ifgroupreq *)data);
3064 #if defined(INET) || defined(INET6)
3067 if (carp_ioctl_p == NULL)
3068 error = EPROTONOSUPPORT;
3070 error = (*carp_ioctl_p)(ifr, cmd, td);
3075 ifp = ifunit_ref(ifr->ifr_name);
3081 error = ifhwioctl(cmd, ifp, data, td);
3082 if (error != ENOIOCTL)
3085 oif_flags = ifp->if_flags;
3086 if (so->so_proto == NULL) {
3092 * Pass the request on to the socket control method, and if the
3093 * latter returns EOPNOTSUPP, directly to the interface.
3095 * Make an exception for the legacy SIOCSIF* requests. Drivers
3096 * trust SIOCSIFADDR et al to come from an already privileged
3097 * layer, and do not perform any credentials checks or input
3100 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3102 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3103 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3104 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3105 error = (*ifp->if_ioctl)(ifp, cmd, data);
3107 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3109 if (ifp->if_flags & IFF_UP)
3117 #ifdef COMPAT_FREEBSD32
3118 if (ifmrp != NULL) {
3119 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3120 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3123 ifmr_update(ifmrp, data);
3131 * The code common to handling reference counted flags,
3132 * e.g., in ifpromisc() and if_allmulti().
3133 * The "pflag" argument can specify a permanent mode flag to check,
3134 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3136 * Only to be used on stack-owned flags, not driver-owned flags.
3139 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3143 int oldflags, oldcount;
3145 /* Sanity checks to catch programming errors */
3146 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3147 ("%s: setting driver-owned flag %d", __func__, flag));
3150 KASSERT(*refcount >= 0,
3151 ("%s: increment negative refcount %d for flag %d",
3152 __func__, *refcount, flag));
3154 KASSERT(*refcount > 0,
3155 ("%s: decrement non-positive refcount %d for flag %d",
3156 __func__, *refcount, flag));
3158 /* In case this mode is permanent, just touch refcount */
3159 if (ifp->if_flags & pflag) {
3160 *refcount += onswitch ? 1 : -1;
3164 /* Save ifnet parameters for if_ioctl() may fail */
3165 oldcount = *refcount;
3166 oldflags = ifp->if_flags;
3169 * See if we aren't the only and touching refcount is enough.
3170 * Actually toggle interface flag if we are the first or last.
3175 ifp->if_flags |= flag;
3179 ifp->if_flags &= ~flag;
3182 /* Call down the driver since we've changed interface flags */
3183 if (ifp->if_ioctl == NULL) {
3187 ifr.ifr_flags = ifp->if_flags & 0xffff;
3188 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3189 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3192 /* Notify userland that interface flags have changed */
3197 /* Recover after driver error */
3198 *refcount = oldcount;
3199 ifp->if_flags = oldflags;
3204 * Set/clear promiscuous mode on interface ifp based on the truth value
3205 * of pswitch. The calls are reference counted so that only the first
3206 * "on" request actually has an effect, as does the final "off" request.
3207 * Results are undefined if the "off" and "on" requests are not matched.
3210 ifpromisc(struct ifnet *ifp, int pswitch)
3213 int oldflags = ifp->if_flags;
3215 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3216 &ifp->if_pcount, pswitch);
3217 /* If promiscuous mode status has changed, log a message */
3218 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3219 log_promisc_mode_change)
3220 if_printf(ifp, "promiscuous mode %s\n",
3221 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3226 * Return interface configuration
3227 * of system. List may be used
3228 * in later ioctl's (above) to get
3229 * other information.
3233 ifconf(u_long cmd, caddr_t data)
3235 struct ifconf *ifc = (struct ifconf *)data;
3240 int error, full = 0, valid_len, max_len;
3242 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3243 max_len = MAXPHYS - 1;
3245 /* Prevent hostile input from being able to crash the system */
3246 if (ifc->ifc_len <= 0)
3250 if (ifc->ifc_len <= max_len) {
3251 max_len = ifc->ifc_len;
3254 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3259 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3263 * Zero the ifr to make sure we don't disclose the contents
3266 memset(&ifr, 0, sizeof(ifr));
3268 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3269 >= sizeof(ifr.ifr_name)) {
3272 return (ENAMETOOLONG);
3277 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3278 struct sockaddr *sa = ifa->ifa_addr;
3280 if (prison_if(curthread->td_ucred, sa) != 0)
3283 if (sa->sa_len <= sizeof(*sa)) {
3284 if (sa->sa_len < sizeof(*sa)) {
3285 memset(&ifr.ifr_ifru.ifru_addr, 0,
3286 sizeof(ifr.ifr_ifru.ifru_addr));
3287 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3290 ifr.ifr_ifru.ifru_addr = *sa;
3291 sbuf_bcat(sb, &ifr, sizeof(ifr));
3292 max_len += sizeof(ifr);
3295 offsetof(struct ifreq, ifr_addr));
3296 max_len += offsetof(struct ifreq, ifr_addr);
3297 sbuf_bcat(sb, sa, sa->sa_len);
3298 max_len += sa->sa_len;
3301 if (sbuf_error(sb) == 0)
3302 valid_len = sbuf_len(sb);
3304 IF_ADDR_RUNLOCK(ifp);
3306 sbuf_bcat(sb, &ifr, sizeof(ifr));
3307 max_len += sizeof(ifr);
3309 if (sbuf_error(sb) == 0)
3310 valid_len = sbuf_len(sb);
3316 * If we didn't allocate enough space (uncommon), try again. If
3317 * we have already allocated as much space as we are allowed,
3318 * return what we've got.
3320 if (valid_len != max_len && !full) {
3325 ifc->ifc_len = valid_len;
3327 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3333 * Just like ifpromisc(), but for all-multicast-reception mode.
3336 if_allmulti(struct ifnet *ifp, int onswitch)
3339 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3342 struct ifmultiaddr *
3343 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3345 struct ifmultiaddr *ifma;
3347 IF_ADDR_LOCK_ASSERT(ifp);
3349 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3350 if (sa->sa_family == AF_LINK) {
3351 if (sa_dl_equal(ifma->ifma_addr, sa))
3354 if (sa_equal(ifma->ifma_addr, sa))
3363 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3364 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3365 * the ifnet multicast address list here, so the caller must do that and
3366 * other setup work (such as notifying the device driver). The reference
3367 * count is initialized to 1.
3369 static struct ifmultiaddr *
3370 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3373 struct ifmultiaddr *ifma;
3374 struct sockaddr *dupsa;
3376 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3381 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3382 if (dupsa == NULL) {
3383 free(ifma, M_IFMADDR);
3386 bcopy(sa, dupsa, sa->sa_len);
3387 ifma->ifma_addr = dupsa;
3389 ifma->ifma_ifp = ifp;
3390 ifma->ifma_refcount = 1;
3391 ifma->ifma_protospec = NULL;
3394 ifma->ifma_lladdr = NULL;
3398 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3399 if (dupsa == NULL) {
3400 free(ifma->ifma_addr, M_IFMADDR);
3401 free(ifma, M_IFMADDR);
3404 bcopy(llsa, dupsa, llsa->sa_len);
3405 ifma->ifma_lladdr = dupsa;
3411 * if_freemulti: free ifmultiaddr structure and possibly attached related
3412 * addresses. The caller is responsible for implementing reference
3413 * counting, notifying the driver, handling routing messages, and releasing
3414 * any dependent link layer state.
3416 #ifdef MCAST_VERBOSE
3417 extern void kdb_backtrace(void);
3420 if_freemulti_internal(struct ifmultiaddr *ifma)
3423 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3424 ifma->ifma_refcount));
3426 if (ifma->ifma_lladdr != NULL)
3427 free(ifma->ifma_lladdr, M_IFMADDR);
3428 #ifdef MCAST_VERBOSE
3430 printf("%s freeing ifma: %p\n", __func__, ifma);
3432 free(ifma->ifma_addr, M_IFMADDR);
3433 free(ifma, M_IFMADDR);
3437 if_destroymulti(epoch_context_t ctx)
3439 struct ifmultiaddr *ifma;
3441 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3442 if_freemulti_internal(ifma);
3446 if_freemulti(struct ifmultiaddr *ifma)
3448 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3449 ifma->ifma_refcount));
3451 epoch_call(net_epoch_preempt, &ifma->ifma_epoch_ctx, if_destroymulti);
3456 * Register an additional multicast address with a network interface.
3458 * - If the address is already present, bump the reference count on the
3459 * address and return.
3460 * - If the address is not link-layer, look up a link layer address.
3461 * - Allocate address structures for one or both addresses, and attach to the
3462 * multicast address list on the interface. If automatically adding a link
3463 * layer address, the protocol address will own a reference to the link
3464 * layer address, to be freed when it is freed.
3465 * - Notify the network device driver of an addition to the multicast address
3468 * 'sa' points to caller-owned memory with the desired multicast address.
3470 * 'retifma' will be used to return a pointer to the resulting multicast
3471 * address reference, if desired.
3474 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3475 struct ifmultiaddr **retifma)
3477 struct ifmultiaddr *ifma, *ll_ifma;
3478 struct sockaddr *llsa;
3479 struct sockaddr_dl sdl;
3483 IN_MULTI_LIST_UNLOCK_ASSERT();
3486 IN6_MULTI_LIST_UNLOCK_ASSERT();
3489 * If the address is already present, return a new reference to it;
3490 * otherwise, allocate storage and set up a new address.
3493 ifma = if_findmulti(ifp, sa);
3495 ifma->ifma_refcount++;
3496 if (retifma != NULL)
3498 IF_ADDR_WUNLOCK(ifp);
3503 * The address isn't already present; resolve the protocol address
3504 * into a link layer address, and then look that up, bump its
3505 * refcount or allocate an ifma for that also.
3506 * Most link layer resolving functions returns address data which
3507 * fits inside default sockaddr_dl structure. However callback
3508 * can allocate another sockaddr structure, in that case we need to
3513 if (ifp->if_resolvemulti != NULL) {
3514 /* Provide called function with buffer size information */
3515 sdl.sdl_len = sizeof(sdl);
3516 llsa = (struct sockaddr *)&sdl;
3517 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3523 * Allocate the new address. Don't hook it up yet, as we may also
3524 * need to allocate a link layer multicast address.
3526 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3533 * If a link layer address is found, we'll need to see if it's
3534 * already present in the address list, or allocate is as well.
3535 * When this block finishes, the link layer address will be on the
3539 ll_ifma = if_findmulti(ifp, llsa);
3540 if (ll_ifma == NULL) {
3541 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3542 if (ll_ifma == NULL) {
3543 --ifma->ifma_refcount;
3548 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3551 ll_ifma->ifma_refcount++;
3552 ifma->ifma_llifma = ll_ifma;
3556 * We now have a new multicast address, ifma, and possibly a new or
3557 * referenced link layer address. Add the primary address to the
3558 * ifnet address list.
3560 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3562 if (retifma != NULL)
3566 * Must generate the message while holding the lock so that 'ifma'
3567 * pointer is still valid.
3569 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3570 IF_ADDR_WUNLOCK(ifp);
3573 * We are certain we have added something, so call down to the
3574 * interface to let them know about it.
3576 if (ifp->if_ioctl != NULL) {
3577 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3580 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3581 link_free_sdl(llsa);
3586 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3587 link_free_sdl(llsa);
3590 IF_ADDR_WUNLOCK(ifp);
3595 * Delete a multicast group membership by network-layer group address.
3597 * Returns ENOENT if the entry could not be found. If ifp no longer
3598 * exists, results are undefined. This entry point should only be used
3599 * from subsystems which do appropriate locking to hold ifp for the
3600 * duration of the call.
3601 * Network-layer protocol domains must use if_delmulti_ifma().
3604 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3606 struct ifmultiaddr *ifma;
3611 IFNET_RLOCK_NOSLEEP();
3612 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3617 IFNET_RUNLOCK_NOSLEEP();
3619 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3626 ifma = if_findmulti(ifp, sa);
3628 lastref = if_delmulti_locked(ifp, ifma, 0);
3629 IF_ADDR_WUNLOCK(ifp);
3634 if (lastref && ifp->if_ioctl != NULL) {
3635 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3642 * Delete all multicast group membership for an interface.
3643 * Should be used to quickly flush all multicast filters.
3646 if_delallmulti(struct ifnet *ifp)
3648 struct ifmultiaddr *ifma;
3649 struct ifmultiaddr *next;
3652 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3653 if_delmulti_locked(ifp, ifma, 0);
3654 IF_ADDR_WUNLOCK(ifp);
3658 if_delmulti_ifma(struct ifmultiaddr *ifma)
3660 if_delmulti_ifma_flags(ifma, 0);
3664 * Delete a multicast group membership by group membership pointer.
3665 * Network-layer protocol domains must use this routine.
3667 * It is safe to call this routine if the ifp disappeared.
3670 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3674 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3676 IN_MULTI_LIST_UNLOCK_ASSERT();
3678 ifp = ifma->ifma_ifp;
3681 printf("%s: ifma_ifp seems to be detached\n", __func__);
3685 IFNET_RLOCK_NOSLEEP();
3686 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3691 IFNET_RUNLOCK_NOSLEEP();
3695 * If and only if the ifnet instance exists: Acquire the address lock.
3700 lastref = if_delmulti_locked(ifp, ifma, flags);
3704 * If and only if the ifnet instance exists:
3705 * Release the address lock.
3706 * If the group was left: update the hardware hash filter.
3708 IF_ADDR_WUNLOCK(ifp);
3709 if (lastref && ifp->if_ioctl != NULL) {
3710 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3716 * Perform deletion of network-layer and/or link-layer multicast address.
3718 * Return 0 if the reference count was decremented.
3719 * Return 1 if the final reference was released, indicating that the
3720 * hardware hash filter should be reprogrammed.
3723 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3725 struct ifmultiaddr *ll_ifma;
3727 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3728 KASSERT(ifma->ifma_ifp == ifp,
3729 ("%s: inconsistent ifp %p", __func__, ifp));
3730 IF_ADDR_WLOCK_ASSERT(ifp);
3733 ifp = ifma->ifma_ifp;
3734 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3737 * If the ifnet is detaching, null out references to ifnet,
3738 * so that upper protocol layers will notice, and not attempt
3739 * to obtain locks for an ifnet which no longer exists. The
3740 * routing socket announcement must happen before the ifnet
3741 * instance is detached from the system.
3745 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3748 * ifp may already be nulled out if we are being reentered
3749 * to delete the ll_ifma.
3752 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3753 ifma->ifma_ifp = NULL;
3757 if (--ifma->ifma_refcount > 0)
3760 if (ifp != NULL && detaching == 0)
3761 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3764 * If this ifma is a network-layer ifma, a link-layer ifma may
3765 * have been associated with it. Release it first if so.
3767 ll_ifma = ifma->ifma_llifma;
3768 if (ll_ifma != NULL) {
3769 KASSERT(ifma->ifma_lladdr != NULL,
3770 ("%s: llifma w/o lladdr", __func__));
3772 ll_ifma->ifma_ifp = NULL; /* XXX */
3773 if (--ll_ifma->ifma_refcount == 0) {
3775 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3778 if_freemulti(ll_ifma);
3783 struct ifmultiaddr *ifmatmp;
3785 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3786 MPASS(ifma != ifmatmp);
3791 * The last reference to this instance of struct ifmultiaddr
3792 * was released; the hardware should be notified of this change.
3798 * Set the link layer address on an interface.
3800 * At this time we only support certain types of interfaces,
3801 * and we don't allow the length of the address to change.
3803 * Set noinline to be dtrace-friendly
3806 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3808 struct sockaddr_dl *sdl;
3821 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3826 if (len != sdl->sdl_alen) { /* don't allow length to change */
3830 switch (ifp->if_type) {
3835 case IFT_IEEE8023ADLAG:
3836 bcopy(lladdr, LLADDR(sdl), len);
3844 * If the interface is already up, we need
3845 * to re-init it in order to reprogram its
3849 if ((ifp->if_flags & IFF_UP) != 0) {
3850 if (ifp->if_ioctl) {
3851 ifp->if_flags &= ~IFF_UP;
3852 ifr.ifr_flags = ifp->if_flags & 0xffff;
3853 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3854 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3855 ifp->if_flags |= IFF_UP;
3856 ifr.ifr_flags = ifp->if_flags & 0xffff;
3857 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3858 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3861 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3869 * Compat function for handling basic encapsulation requests.
3870 * Not converted stacks (FDDI, IB, ..) supports traditional
3871 * output model: ARP (and other similar L2 protocols) are handled
3872 * inside output routine, arpresolve/nd6_resolve() returns MAC
3873 * address instead of full prepend.
3875 * This function creates calculated header==MAC for IPv4/IPv6 and
3876 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3880 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3883 if (req->rtype != IFENCAP_LL)
3884 return (EOPNOTSUPP);
3886 if (req->bufsize < req->lladdr_len)
3889 switch (req->family) {
3894 return (EAFNOSUPPORT);
3897 /* Copy lladdr to storage as is */
3898 memmove(req->buf, req->lladdr, req->lladdr_len);
3899 req->bufsize = req->lladdr_len;
3900 req->lladdr_off = 0;
3906 * Tunnel interfaces can nest, also they may cause infinite recursion
3907 * calls when misconfigured. We'll prevent this by detecting loops.
3908 * High nesting level may cause stack exhaustion. We'll prevent this
3909 * by introducing upper limit.
3911 * Return 0, if tunnel nesting count is equal or less than limit.
3914 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3922 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3923 if (*(struct ifnet **)(mtag + 1) == ifp) {
3924 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3929 if (count > limit) {
3931 "%s: if_output recursively called too many times(%d)\n",
3932 if_name(ifp), count);
3935 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3938 *(struct ifnet **)(mtag + 1) = ifp;
3939 m_tag_prepend(m, mtag);
3944 * Get the link layer address that was read from the hardware at attach.
3946 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3947 * their component interfaces as IFT_IEEE8023ADLAG.
3950 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3953 if (ifp->if_hw_addr == NULL)
3956 switch (ifp->if_type) {
3958 case IFT_IEEE8023ADLAG:
3959 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3967 * The name argument must be a pointer to storage which will last as
3968 * long as the interface does. For physical devices, the result of
3969 * device_get_name(dev) is a good choice and for pseudo-devices a
3970 * static string works well.
3973 if_initname(struct ifnet *ifp, const char *name, int unit)
3975 ifp->if_dname = name;
3976 ifp->if_dunit = unit;
3977 if (unit != IF_DUNIT_NONE)
3978 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3980 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3984 if_printf(struct ifnet *ifp, const char *fmt, ...)
3989 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3991 vlog(LOG_INFO, if_fmt, ap);
3997 if_start(struct ifnet *ifp)
4000 (*(ifp)->if_start)(ifp);
4004 * Backwards compatibility interface for drivers
4005 * that have not implemented it
4008 if_transmit(struct ifnet *ifp, struct mbuf *m)
4012 IFQ_HANDOFF(ifp, m, error);
4017 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4024 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4029 if (_IF_QFULL(ifq)) {
4031 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4036 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4037 if (m->m_flags & (M_BCAST|M_MCAST))
4038 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4039 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4041 _IF_ENQUEUE(ifq, m);
4043 if (ifp != NULL && !active)
4044 (*(ifp)->if_start)(ifp);
4049 if_register_com_alloc(u_char type,
4050 if_com_alloc_t *a, if_com_free_t *f)
4053 KASSERT(if_com_alloc[type] == NULL,
4054 ("if_register_com_alloc: %d already registered", type));
4055 KASSERT(if_com_free[type] == NULL,
4056 ("if_register_com_alloc: %d free already registered", type));
4058 if_com_alloc[type] = a;
4059 if_com_free[type] = f;
4063 if_deregister_com_alloc(u_char type)
4066 KASSERT(if_com_alloc[type] != NULL,
4067 ("if_deregister_com_alloc: %d not registered", type));
4068 KASSERT(if_com_free[type] != NULL,
4069 ("if_deregister_com_alloc: %d free not registered", type));
4070 if_com_alloc[type] = NULL;
4071 if_com_free[type] = NULL;
4074 /* API for driver access to network stack owned ifnet.*/
4076 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4080 oldbrate = ifp->if_baudrate;
4081 ifp->if_baudrate = baudrate;
4086 if_getbaudrate(if_t ifp)
4089 return (((struct ifnet *)ifp)->if_baudrate);
4093 if_setcapabilities(if_t ifp, int capabilities)
4095 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4100 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4102 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4103 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4109 if_getcapabilities(if_t ifp)
4111 return ((struct ifnet *)ifp)->if_capabilities;
4115 if_setcapenable(if_t ifp, int capabilities)
4117 ((struct ifnet *)ifp)->if_capenable = capabilities;
4122 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4125 ((struct ifnet *)ifp)->if_capenable |= setcap;
4127 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4133 if_getdname(if_t ifp)
4135 return ((struct ifnet *)ifp)->if_dname;
4139 if_togglecapenable(if_t ifp, int togglecap)
4141 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4146 if_getcapenable(if_t ifp)
4148 return ((struct ifnet *)ifp)->if_capenable;
4152 * This is largely undesirable because it ties ifnet to a device, but does
4153 * provide flexiblity for an embedded product vendor. Should be used with
4154 * the understanding that it violates the interface boundaries, and should be
4155 * a last resort only.
4158 if_setdev(if_t ifp, void *dev)
4164 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4166 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4167 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4173 if_getdrvflags(if_t ifp)
4175 return ((struct ifnet *)ifp)->if_drv_flags;
4179 if_setdrvflags(if_t ifp, int flags)
4181 ((struct ifnet *)ifp)->if_drv_flags = flags;
4187 if_setflags(if_t ifp, int flags)
4189 ((struct ifnet *)ifp)->if_flags = flags;
4194 if_setflagbits(if_t ifp, int set, int clear)
4196 ((struct ifnet *)ifp)->if_flags |= set;
4197 ((struct ifnet *)ifp)->if_flags &= ~clear;
4203 if_getflags(if_t ifp)
4205 return ((struct ifnet *)ifp)->if_flags;
4209 if_clearhwassist(if_t ifp)
4211 ((struct ifnet *)ifp)->if_hwassist = 0;
4216 if_sethwassistbits(if_t ifp, int toset, int toclear)
4218 ((struct ifnet *)ifp)->if_hwassist |= toset;
4219 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4225 if_sethwassist(if_t ifp, int hwassist_bit)
4227 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4232 if_gethwassist(if_t ifp)
4234 return ((struct ifnet *)ifp)->if_hwassist;
4238 if_setmtu(if_t ifp, int mtu)
4240 ((struct ifnet *)ifp)->if_mtu = mtu;
4247 return ((struct ifnet *)ifp)->if_mtu;
4251 if_getmtu_family(if_t ifp, int family)
4255 for (dp = domains; dp; dp = dp->dom_next) {
4256 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4257 return (dp->dom_ifmtu((struct ifnet *)ifp));
4260 return (((struct ifnet *)ifp)->if_mtu);
4264 if_setsoftc(if_t ifp, void *softc)
4266 ((struct ifnet *)ifp)->if_softc = softc;
4271 if_getsoftc(if_t ifp)
4273 return ((struct ifnet *)ifp)->if_softc;
4277 if_setrcvif(struct mbuf *m, if_t ifp)
4279 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4283 if_setvtag(struct mbuf *m, uint16_t tag)
4285 m->m_pkthdr.ether_vtag = tag;
4289 if_getvtag(struct mbuf *m)
4292 return (m->m_pkthdr.ether_vtag);
4296 if_sendq_empty(if_t ifp)
4298 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4302 if_getifaddr(if_t ifp)
4304 return ((struct ifnet *)ifp)->if_addr;
4308 if_getamcount(if_t ifp)
4310 return ((struct ifnet *)ifp)->if_amcount;
4315 if_setsendqready(if_t ifp)
4317 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4322 if_setsendqlen(if_t ifp, int tx_desc_count)
4324 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4325 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4331 if_vlantrunkinuse(if_t ifp)
4333 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4337 if_input(if_t ifp, struct mbuf* sendmp)
4339 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4345 #ifndef ETH_ADDR_LEN
4346 #define ETH_ADDR_LEN 6
4350 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4352 struct ifmultiaddr *ifma;
4353 uint8_t *lmta = (uint8_t *)mta;
4356 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4357 if (ifma->ifma_addr->sa_family != AF_LINK)
4363 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4364 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4373 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4377 if_maddr_rlock(ifp);
4378 error = if_setupmultiaddr(ifp, mta, cnt, max);
4379 if_maddr_runlock(ifp);
4384 if_multiaddr_count(if_t ifp, int max)
4386 struct ifmultiaddr *ifma;
4390 if_maddr_rlock(ifp);
4391 CK_STAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4392 if (ifma->ifma_addr->sa_family != AF_LINK)
4398 if_maddr_runlock(ifp);
4403 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4405 struct ifmultiaddr *ifma;
4408 if_maddr_rlock(ifp);
4409 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4410 cnt += filter(arg, ifma, cnt);
4411 if_maddr_runlock(ifp);
4416 if_dequeue(if_t ifp)
4419 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4425 if_sendq_prepend(if_t ifp, struct mbuf *m)
4427 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4432 if_setifheaderlen(if_t ifp, int len)
4434 ((struct ifnet *)ifp)->if_hdrlen = len;
4439 if_getlladdr(if_t ifp)
4441 return (IF_LLADDR((struct ifnet *)ifp));
4445 if_gethandle(u_char type)
4447 return (if_alloc(type));
4451 if_bpfmtap(if_t ifh, struct mbuf *m)
4453 struct ifnet *ifp = (struct ifnet *)ifh;
4459 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4461 struct ifnet *ifp = (struct ifnet *)ifh;
4463 ETHER_BPF_MTAP(ifp, m);
4467 if_vlancap(if_t ifh)
4469 struct ifnet *ifp = (struct ifnet *)ifh;
4470 VLAN_CAPABILITIES(ifp);
4474 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4477 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4482 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4485 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4490 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4493 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4498 if_gethwtsomax(if_t ifp)
4501 return (((struct ifnet *)ifp)->if_hw_tsomax);
4505 if_gethwtsomaxsegcount(if_t ifp)
4508 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4512 if_gethwtsomaxsegsize(if_t ifp)
4515 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4519 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4521 ((struct ifnet *)ifp)->if_init = init_fn;
4525 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4527 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4531 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4533 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4537 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4539 ((struct ifnet *)ifp)->if_transmit = start_fn;
4542 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4544 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4549 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4552 ifp->if_get_counter = fn;
4555 /* Revisit these - These are inline functions originally. */
4557 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4559 return drbr_inuse(ifh, br);
4563 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4565 return drbr_dequeue(ifh, br);
4569 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4571 return drbr_needs_enqueue(ifh, br);
4575 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4577 return drbr_enqueue(ifh, br, m);