xref: /freebsd/sys/net/if.c (revision 10729d0ce11c9ac5077d158bb372f9c39053f168)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_bpf.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_ddb.h"
37 
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/epoch.h>
47 #include <sys/mbuf.h>
48 #include <sys/systm.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/nv.h>
59 #include <sys/rwlock.h>
60 #include <sys/sockio.h>
61 #include <sys/stdarg.h>
62 #include <sys/syslog.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/taskqueue.h>
66 #include <sys/domain.h>
67 #include <sys/jail.h>
68 #include <sys/priv.h>
69 
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73 
74 #include <vm/uma.h>
75 
76 #include <net/bpf.h>
77 #include <net/if.h>
78 #include <net/if_arp.h>
79 #include <net/if_clone.h>
80 #include <net/if_dl.h>
81 #include <net/if_strings.h>
82 #include <net/if_types.h>
83 #include <net/if_var.h>
84 #include <net/if_vf_status.h>
85 #include <net/if_media.h>
86 #include <net/if_mib.h>
87 #include <net/if_private.h>
88 #include <net/if_vlan_var.h>
89 #include <net/radix.h>
90 #include <net/route.h>
91 #include <net/route/route_ctl.h>
92 #include <net/vnet.h>
93 
94 #if defined(INET) || defined(INET6)
95 #include <net/ethernet.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_carp.h>
100 #ifdef INET
101 #include <net/debugnet.h>
102 #include <netinet/if_ether.h>
103 #endif /* INET */
104 #ifdef INET6
105 #include <netinet6/in6_var.h>
106 #endif /* INET6 */
107 #endif /* INET || INET6 */
108 
109 #include <security/mac/mac_framework.h>
110 
111 /*
112  * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
113  * and ifr_ifru when it is used in SIOCGIFCONF.
114  */
115 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
116     offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
117 
118 __read_mostly epoch_t net_epoch_preempt;
119 #ifdef COMPAT_FREEBSD32
120 #include <sys/mount.h>
121 #include <compat/freebsd32/freebsd32.h>
122 
123 struct ifreq_buffer32 {
124 	uint32_t	length;		/* (size_t) */
125 	uint32_t	buffer;		/* (void *) */
126 };
127 
128 /*
129  * Interface request structure used for socket
130  * ioctl's.  All interface ioctl's must have parameter
131  * definitions which begin with ifr_name.  The
132  * remainder may be interface specific.
133  */
134 struct ifreq32 {
135 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
136 	union {
137 		struct sockaddr	ifru_addr;
138 		struct sockaddr	ifru_dstaddr;
139 		struct sockaddr	ifru_broadaddr;
140 		struct ifreq_buffer32 ifru_buffer;
141 		short		ifru_flags[2];
142 		short		ifru_index;
143 		int		ifru_jid;
144 		int		ifru_metric;
145 		int		ifru_mtu;
146 		int		ifru_phys;
147 		int		ifru_media;
148 		uint32_t	ifru_data;
149 		int		ifru_cap[2];
150 		u_int		ifru_fib;
151 		u_char		ifru_vlan_pcp;
152 	} ifr_ifru;
153 };
154 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
155 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
156     __offsetof(struct ifreq32, ifr_ifru));
157 
158 struct ifconf32 {
159 	int32_t	ifc_len;
160 	union {
161 		uint32_t	ifcu_buf;
162 		uint32_t	ifcu_req;
163 	} ifc_ifcu;
164 };
165 #define	SIOCGIFCONF32	_IOWR('i', 36, struct ifconf32)
166 
167 struct ifdrv32 {
168 	char		ifd_name[IFNAMSIZ];
169 	uint32_t	ifd_cmd;
170 	uint32_t	ifd_len;
171 	uint32_t	ifd_data;
172 };
173 #define SIOCSDRVSPEC32	_IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
174 #define SIOCGDRVSPEC32	_IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
175 
176 struct ifgroupreq32 {
177 	char	ifgr_name[IFNAMSIZ];
178 	u_int	ifgr_len;
179 	union {
180 		char		ifgru_group[IFNAMSIZ];
181 		uint32_t	ifgru_groups;
182 	} ifgr_ifgru;
183 };
184 #define	SIOCAIFGROUP32	_IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
185 #define	SIOCGIFGROUP32	_IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
186 #define	SIOCDIFGROUP32	_IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
187 #define	SIOCGIFGMEMB32	_IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
188 
189 struct ifmediareq32 {
190 	char		ifm_name[IFNAMSIZ];
191 	int		ifm_current;
192 	int		ifm_mask;
193 	int		ifm_status;
194 	int		ifm_active;
195 	int		ifm_count;
196 	uint32_t	ifm_ulist;	/* (int *) */
197 };
198 #define	SIOCGIFMEDIA32	_IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
199 #define	SIOCGIFXMEDIA32	_IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
200 #endif /* COMPAT_FREEBSD32 */
201 
202 union ifreq_union {
203 	struct ifreq	ifr;
204 #ifdef COMPAT_FREEBSD32
205 	struct ifreq32	ifr32;
206 #endif
207 };
208 
209 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
210     "Link layers");
211 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
212     "Generic link-management");
213 
214 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
215     &ifqmaxlen, 0, "max send queue size");
216 
217 /* Log link state change events */
218 static int log_link_state_change = 1;
219 
220 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
221 	&log_link_state_change, 0,
222 	"log interface link state change events");
223 
224 /* Log promiscuous mode change events */
225 static int log_promisc_mode_change = 1;
226 
227 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
228 	&log_promisc_mode_change, 1,
229 	"log promiscuous mode change events");
230 
231 /* Interface description */
232 static unsigned int ifdescr_maxlen = 1024;
233 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
234 	&ifdescr_maxlen, 0,
235 	"administrative maximum length for interface description");
236 
237 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
238 
239 /* global sx for non-critical path ifdescr */
240 static struct sx ifdescr_sx;
241 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
242 
243 void	(*lagg_linkstate_p)(struct ifnet *ifp, int state);
244 /* These are external hooks for CARP. */
245 void	(*carp_linkstate_p)(struct ifnet *ifp);
246 void	(*carp_demote_adj_p)(int, char *);
247 int	(*carp_master_p)(struct ifaddr *);
248 #if defined(INET) || defined(INET6)
249 int	(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
250 int	(*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
251     const struct sockaddr *sa);
252 int	(*carp_attach_p)(struct ifaddr *, int);
253 void	(*carp_detach_p)(struct ifaddr *, bool);
254 #endif
255 #ifdef INET
256 int	(*carp_iamatch_p)(struct ifaddr *, uint8_t **);
257 #endif
258 #ifdef INET6
259 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
260 caddr_t	(*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
261     const struct in6_addr *taddr);
262 #endif
263 
264 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
265 
266 /*
267  * XXX: Style; these should be sorted alphabetically, and unprototyped
268  * static functions should be prototyped. Currently they are sorted by
269  * declaration order.
270  */
271 static int	ifconf(u_long, caddr_t);
272 static void	if_input_default(struct ifnet *, struct mbuf *);
273 static int	if_requestencap_default(struct ifnet *, struct if_encap_req *);
274 static int	if_setflag(struct ifnet *, int, int, int *, int);
275 static int	if_transmit_default(struct ifnet *ifp, struct mbuf *m);
276 static int	if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
277 static void	do_link_state_change(void *, int);
278 static int	if_getgroup(struct ifgroupreq *, struct ifnet *);
279 static int	if_getgroupmembers(struct ifgroupreq *);
280 static void	if_delgroups(struct ifnet *);
281 static void	if_attach_internal(struct ifnet *, bool);
282 static void	if_detach_internal(struct ifnet *, bool);
283 static void	if_siocaddmulti(void *, int);
284 static void	if_link_ifnet(struct ifnet *);
285 static bool	if_unlink_ifnet(struct ifnet *, bool);
286 #ifdef VIMAGE
287 static void	if_vmove(struct ifnet *, struct vnet *);
288 #endif
289 
290 #ifdef INET6
291 /*
292  * XXX: declare here to avoid to include many inet6 related files..
293  * should be more generalized?
294  */
295 extern void	nd6_setmtu(struct ifnet *);
296 #endif
297 
298 /* ipsec helper hooks */
299 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
300 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
301 
302 int	ifqmaxlen = IFQ_MAXLEN;
303 VNET_DEFINE(struct ifnethead, ifnet);	/* depend on static init XXX */
304 VNET_DEFINE(struct ifgrouphead, ifg_head);
305 
306 /* Table of ifnet by index. */
307 static int if_index;
308 static int if_indexlim = 8;
309 static struct ifindex_entry {
310 	struct ifnet	*ife_ifnet;
311 	uint16_t	ife_gencnt;
312 } *ifindex_table;
313 
314 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
315     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
316     "Variables global to all interfaces");
317 static int
318 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
319 {
320 	int rv = 0;
321 
322 	IFNET_RLOCK();
323 	for (int i = 1; i <= if_index; i++)
324 		if (ifindex_table[i].ife_ifnet != NULL &&
325 		    ifindex_table[i].ife_ifnet->if_vnet == curvnet)
326 			rv = i;
327 	IFNET_RUNLOCK();
328 
329 	return (sysctl_handle_int(oidp, &rv, 0, req));
330 }
331 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
332     CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
333     "Maximum known interface index");
334 
335 /*
336  * The global network interface list (V_ifnet) and related state (such as
337  * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
338  * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
339  */
340 struct sx ifnet_sxlock;
341 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
342 
343 struct sx ifnet_detach_sxlock;
344 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
345     SX_RECURSE);
346 
347 static	if_com_alloc_t *if_com_alloc[256];
348 static	if_com_free_t *if_com_free[256];
349 
350 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
351 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
352 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
353 
354 struct ifnet *
355 ifnet_byindex(u_int idx)
356 {
357 	struct ifnet *ifp;
358 
359 	NET_EPOCH_ASSERT();
360 
361 	if (__predict_false(idx > if_index))
362 		return (NULL);
363 
364 	ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
365 
366 	if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
367 		ifp = NULL;
368 
369 	return (ifp);
370 }
371 
372 struct ifnet *
373 ifnet_byindex_ref(u_int idx)
374 {
375 	struct ifnet *ifp;
376 
377 	ifp = ifnet_byindex(idx);
378 	if (ifp == NULL || (ifp->if_flags & IFF_DYING))
379 		return (NULL);
380 	if (!if_try_ref(ifp))
381 		return (NULL);
382 	return (ifp);
383 }
384 
385 struct ifnet *
386 ifnet_byindexgen(uint16_t idx, uint16_t gen)
387 {
388 	struct ifnet *ifp;
389 
390 	NET_EPOCH_ASSERT();
391 
392 	if (__predict_false(idx > if_index))
393 		return (NULL);
394 
395 	ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
396 
397 	if (ifindex_table[idx].ife_gencnt == gen)
398 		return (ifp);
399 	else
400 		return (NULL);
401 }
402 
403 /*
404  * Network interface utility routines.
405  *
406  * Routines with ifa_ifwith* names take sockaddr *'s as
407  * parameters.
408  */
409 
410 static void
411 if_init_idxtable(void *arg __unused)
412 {
413 
414 	ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
415 	    M_IFNET, M_WAITOK | M_ZERO);
416 }
417 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
418 
419 static void
420 vnet_if_init(const void *unused __unused)
421 {
422 
423 	CK_STAILQ_INIT(&V_ifnet);
424 	CK_STAILQ_INIT(&V_ifg_head);
425 }
426 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
427     NULL);
428 
429 static void
430 if_link_ifnet(struct ifnet *ifp)
431 {
432 	IFNET_WLOCK();
433 
434 	MPASS(refcount_load(&ifp->if_refcount) > 0);
435 	MPASS(ifp->if_vnet == curvnet);
436 	MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
437 
438 	CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
439 #ifdef VIMAGE
440 	curvnet->vnet_ifcnt++;
441 #endif
442 	IFNET_WUNLOCK();
443 }
444 
445 static bool
446 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
447 {
448 	struct ifnet *iter;
449 	int found = 0;
450 
451 	sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
452 	IFNET_WLOCK();
453 	CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
454 		if (iter == ifp) {
455 			MPASS(refcount_load(&ifp->if_refcount) > 0);
456 			MPASS(ifp->if_vnet == curvnet);
457 			MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
458 
459 			CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
460 #ifdef VIMAGE
461 			curvnet->vnet_ifcnt--;
462 #endif
463 			if (!vmove)
464 				ifp->if_flags |= IFF_DYING;
465 			found = 1;
466 			break;
467 		}
468 	IFNET_WUNLOCK();
469 
470 	return (found);
471 }
472 
473 #ifdef VIMAGE
474 static void
475 vnet_if_return(const void *unused __unused)
476 {
477 	struct ifnet *ifp, *nifp;
478 	struct ifnet **pending;
479 	int found __diagused;
480 	int i;
481 
482 	i = 0;
483 
484 	/* The lock has already been aquired in vnet_destroy() */
485 	sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
486 	/*
487 	 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
488 	 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
489 	 * if_detach_internal(), which waits for NET_EPOCH callbacks to
490 	 * complete. We can't do that from within NET_EPOCH.
491 	 *
492 	 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
493 	 * read/write lock. We cannot hold the lock as we call if_vmove()
494 	 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
495 	 * ctx lock.
496 	 */
497 	IFNET_WLOCK();
498 
499 	pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
500 	    M_IFNET, M_WAITOK | M_ZERO);
501 
502 	/* Return all inherited interfaces to their parent vnets. */
503 	CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
504 		if (ifp->if_home_vnet != ifp->if_vnet) {
505 			found = if_unlink_ifnet(ifp, true);
506 			MPASS(found);
507 
508 			pending[i++] = ifp;
509 		}
510 	}
511 	IFNET_WUNLOCK();
512 
513 	for (int j = 0; j < i; j++) {
514 		if_vmove(pending[j], pending[j]->if_home_vnet);
515 	}
516 
517 	free(pending, M_IFNET);
518 }
519 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
520     vnet_if_return, NULL);
521 #endif
522 
523 /*
524  * Allocate a struct ifnet and an index for an interface.  A layer 2
525  * common structure will also be allocated if an allocation routine is
526  * registered for the passed type.
527  */
528 static struct ifnet *
529 if_alloc_domain(u_char type, int numa_domain)
530 {
531 	struct ifnet *ifp;
532 	u_short idx;
533 
534 	KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
535 	if (numa_domain == IF_NODOM)
536 		ifp = malloc(sizeof(struct ifnet), M_IFNET,
537 		    M_WAITOK | M_ZERO);
538 	else
539 		ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
540 		    DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
541 	ifp->if_type = type;
542 	ifp->if_alloctype = type;
543 	ifp->if_numa_domain = numa_domain;
544 #ifdef VIMAGE
545 	ifp->if_vnet = curvnet;
546 #endif
547 	if (if_com_alloc[type] != NULL) {
548 		ifp->if_l2com = if_com_alloc[type](type, ifp);
549 		KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
550 		    type));
551 	}
552 
553 	IF_ADDR_LOCK_INIT(ifp);
554 	TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
555 	TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
556 	CK_STAILQ_INIT(&ifp->if_addrhead);
557 	CK_STAILQ_INIT(&ifp->if_multiaddrs);
558 	CK_STAILQ_INIT(&ifp->if_groups);
559 #ifdef MAC
560 	mac_ifnet_init(ifp);
561 #endif
562 	ifq_init(&ifp->if_snd, ifp);
563 
564 	refcount_init(&ifp->if_refcount, 1);	/* Index reference. */
565 	for (int i = 0; i < IFCOUNTERS; i++)
566 		ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
567 	ifp->if_get_counter = if_get_counter_default;
568 	ifp->if_pcp = IFNET_PCP_NONE;
569 
570 	/* Allocate an ifindex array entry. */
571 	IFNET_WLOCK();
572 	/*
573 	 * Try to find an empty slot below if_index.  If we fail, take the
574 	 * next slot.
575 	 */
576 	for (idx = 1; idx <= if_index; idx++) {
577 		if (ifindex_table[idx].ife_ifnet == NULL)
578 			break;
579 	}
580 
581 	/* Catch if_index overflow. */
582 	if (idx >= if_indexlim) {
583 		struct ifindex_entry *new, *old;
584 		int newlim;
585 
586 		newlim = if_indexlim * 2;
587 		new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
588 		memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
589 		old = ifindex_table;
590 		ck_pr_store_ptr(&ifindex_table, new);
591 		if_indexlim = newlim;
592 		NET_EPOCH_WAIT();
593 		free(old, M_IFNET);
594 	}
595 	if (idx > if_index)
596 		if_index = idx;
597 
598 	ifp->if_index = idx;
599 	ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
600 	ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
601 	IFNET_WUNLOCK();
602 
603 	return (ifp);
604 }
605 
606 struct ifnet *
607 if_alloc_dev(u_char type, device_t dev)
608 {
609 	int numa_domain;
610 
611 	if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
612 		return (if_alloc_domain(type, IF_NODOM));
613 	return (if_alloc_domain(type, numa_domain));
614 }
615 
616 struct ifnet *
617 if_alloc(u_char type)
618 {
619 
620 	return (if_alloc_domain(type, IF_NODOM));
621 }
622 /*
623  * Do the actual work of freeing a struct ifnet, and layer 2 common
624  * structure.  This call is made when the network epoch guarantees
625  * us that nobody holds a pointer to the interface.
626  */
627 static void
628 if_free_deferred(epoch_context_t ctx)
629 {
630 	struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
631 
632 	KASSERT((ifp->if_flags & IFF_DYING),
633 	    ("%s: interface not dying", __func__));
634 
635 	if (if_com_free[ifp->if_alloctype] != NULL)
636 		if_com_free[ifp->if_alloctype](ifp->if_l2com,
637 		    ifp->if_alloctype);
638 
639 #ifdef MAC
640 	mac_ifnet_destroy(ifp);
641 #endif /* MAC */
642 	IF_ADDR_LOCK_DESTROY(ifp);
643 	ifq_delete(&ifp->if_snd);
644 
645 	for (int i = 0; i < IFCOUNTERS; i++)
646 		counter_u64_free(ifp->if_counters[i]);
647 
648 	if_freedescr(ifp->if_description);
649 	free(ifp->if_hw_addr, M_IFADDR);
650 	free(ifp, M_IFNET);
651 }
652 
653 /*
654  * Deregister an interface and free the associated storage.
655  */
656 void
657 if_free(struct ifnet *ifp)
658 {
659 
660 	ifp->if_flags |= IFF_DYING;			/* XXX: Locking */
661 
662 	/*
663 	 * XXXGL: An interface index is really an alias to ifp pointer.
664 	 * Why would we clear the alias now, and not in the deferred
665 	 * context?  Indeed there is nothing wrong with some network
666 	 * thread obtaining ifp via ifnet_byindex() inside the network
667 	 * epoch and then dereferencing ifp while we perform if_free(),
668 	 * and after if_free() finished, too.
669 	 *
670 	 * This early index freeing was important back when ifindex was
671 	 * virtualized and interface would outlive the vnet.
672 	 */
673 	IFNET_WLOCK();
674 	MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
675 	ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
676 	ifindex_table[ifp->if_index].ife_gencnt++;
677 	while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
678 		if_index--;
679 	IFNET_WUNLOCK();
680 
681 	if (refcount_release(&ifp->if_refcount))
682 		NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
683 }
684 
685 /*
686  * Interfaces to keep an ifnet type-stable despite the possibility of the
687  * driver calling if_free().  If there are additional references, we defer
688  * freeing the underlying data structure.
689  */
690 void
691 if_ref(struct ifnet *ifp)
692 {
693 	u_int old __diagused;
694 
695 	/* We don't assert the ifnet list lock here, but arguably should. */
696 	old = refcount_acquire(&ifp->if_refcount);
697 	KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
698 }
699 
700 bool
701 if_try_ref(struct ifnet *ifp)
702 {
703 	NET_EPOCH_ASSERT();
704 	return (refcount_acquire_if_not_zero(&ifp->if_refcount));
705 }
706 
707 void
708 if_rele(struct ifnet *ifp)
709 {
710 
711 	if (!refcount_release(&ifp->if_refcount))
712 		return;
713 	NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
714 }
715 
716 void
717 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
718 {
719 
720 	mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
721 
722 	if (ifq->ifq_maxlen == 0)
723 		ifq->ifq_maxlen = ifqmaxlen;
724 
725 	ifq->altq_type = 0;
726 	ifq->altq_disc = NULL;
727 	ifq->altq_flags &= ALTQF_CANTCHANGE;
728 	ifq->altq_tbr  = NULL;
729 	ifq->altq_ifp  = ifp;
730 }
731 
732 void
733 ifq_delete(struct ifaltq *ifq)
734 {
735 	mtx_destroy(&ifq->ifq_mtx);
736 }
737 
738 /*
739  * Perform generic interface initialization tasks and attach the interface
740  * to the list of "active" interfaces.  If vmove flag is set on entry
741  * to if_attach_internal(), perform only a limited subset of initialization
742  * tasks, given that we are moving from one vnet to another an ifnet which
743  * has already been fully initialized.
744  *
745  * Note that if_detach_internal() removes group membership unconditionally
746  * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
747  * Thus, when if_vmove() is applied to a cloned interface, group membership
748  * is lost while a cloned one always joins a group whose name is
749  * ifc->ifc_name.  To recover this after if_detach_internal() and
750  * if_attach_internal(), the cloner should be specified to
751  * if_attach_internal() via ifc.  If it is non-NULL, if_attach_internal()
752  * attempts to join a group whose name is ifc->ifc_name.
753  *
754  * XXX:
755  *  - The decision to return void and thus require this function to
756  *    succeed is questionable.
757  *  - We should probably do more sanity checking.  For instance we don't
758  *    do anything to insure if_xname is unique or non-empty.
759  */
760 void
761 if_attach(struct ifnet *ifp)
762 {
763 
764 	if_attach_internal(ifp, false);
765 }
766 
767 /*
768  * Compute the least common TSO limit.
769  */
770 void
771 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
772 {
773 	/*
774 	 * 1) If there is no limit currently, take the limit from
775 	 * the network adapter.
776 	 *
777 	 * 2) If the network adapter has a limit below the current
778 	 * limit, apply it.
779 	 */
780 	if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
781 	    ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
782 		pmax->tsomaxbytes = ifp->if_hw_tsomax;
783 	}
784 	if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
785 	    ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
786 		pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
787 	}
788 	if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
789 	    ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
790 		pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
791 	}
792 }
793 
794 /*
795  * Update TSO limit of a network adapter.
796  *
797  * Returns zero if no change. Else non-zero.
798  */
799 int
800 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
801 {
802 	int retval = 0;
803 	if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
804 		ifp->if_hw_tsomax = pmax->tsomaxbytes;
805 		retval++;
806 	}
807 	if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
808 		ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
809 		retval++;
810 	}
811 	if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
812 		ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
813 		retval++;
814 	}
815 	return (retval);
816 }
817 
818 static void
819 if_attach_internal(struct ifnet *ifp, bool vmove)
820 {
821 	unsigned socksize, ifasize;
822 	int namelen, masklen;
823 	struct sockaddr_dl *sdl;
824 	struct ifaddr *ifa;
825 
826 	MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
827 
828 #ifdef VIMAGE
829 	CURVNET_ASSERT_SET();
830 	ifp->if_vnet = curvnet;
831 	if (ifp->if_home_vnet == NULL)
832 		ifp->if_home_vnet = curvnet;
833 #endif
834 
835 	if_addgroup(ifp, IFG_ALL);
836 
837 #ifdef VIMAGE
838 	/* Restore group membership for cloned interface. */
839 	if (vmove)
840 		if_clone_restoregroup(ifp);
841 #endif
842 
843 	getmicrotime(&ifp->if_lastchange);
844 	ifp->if_epoch = time_uptime;
845 
846 	KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
847 	    (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
848 	    ("transmit and qflush must both either be set or both be NULL"));
849 	if (ifp->if_transmit == NULL) {
850 		ifp->if_transmit = if_transmit_default;
851 		ifp->if_qflush = if_qflush;
852 	}
853 	if (ifp->if_input == NULL)
854 		ifp->if_input = if_input_default;
855 
856 	if (ifp->if_requestencap == NULL)
857 		ifp->if_requestencap = if_requestencap_default;
858 
859 	if (!vmove) {
860 #ifdef MAC
861 		mac_ifnet_create(ifp);
862 #endif
863 
864 		/*
865 		 * Create a Link Level name for this device.
866 		 */
867 		namelen = strlen(ifp->if_xname);
868 		/*
869 		 * Always save enough space for any possible name so we
870 		 * can do a rename in place later.
871 		 */
872 		masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
873 		socksize = masklen + ifp->if_addrlen;
874 		if (socksize < sizeof(*sdl))
875 			socksize = sizeof(*sdl);
876 		socksize = roundup2(socksize, sizeof(long));
877 		ifasize = sizeof(*ifa) + 2 * socksize;
878 		ifa = ifa_alloc(ifasize, M_WAITOK);
879 		sdl = (struct sockaddr_dl *)(ifa + 1);
880 		sdl->sdl_len = socksize;
881 		sdl->sdl_family = AF_LINK;
882 		bcopy(ifp->if_xname, sdl->sdl_data, namelen);
883 		sdl->sdl_nlen = namelen;
884 		sdl->sdl_index = ifp->if_index;
885 		sdl->sdl_type = ifp->if_type;
886 		ifp->if_addr = ifa;
887 		ifa->ifa_ifp = ifp;
888 		ifa->ifa_addr = (struct sockaddr *)sdl;
889 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
890 		ifa->ifa_netmask = (struct sockaddr *)sdl;
891 		sdl->sdl_len = masklen;
892 		while (namelen != 0)
893 			sdl->sdl_data[--namelen] = 0xff;
894 		CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
895 		/* Reliably crash if used uninitialized. */
896 		ifp->if_broadcastaddr = NULL;
897 
898 		if (ifp->if_type == IFT_ETHER) {
899 			ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
900 			    M_WAITOK | M_ZERO);
901 		}
902 
903 #if defined(INET) || defined(INET6)
904 		/* Use defaults for TSO, if nothing is set */
905 		if (ifp->if_hw_tsomax == 0 &&
906 		    ifp->if_hw_tsomaxsegcount == 0 &&
907 		    ifp->if_hw_tsomaxsegsize == 0) {
908 			/*
909 			 * The TSO defaults needs to be such that an
910 			 * NFS mbuf list of 35 mbufs totalling just
911 			 * below 64K works and that a chain of mbufs
912 			 * can be defragged into at most 32 segments:
913 			 */
914 			ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
915 			    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
916 			ifp->if_hw_tsomaxsegcount = 35;
917 			ifp->if_hw_tsomaxsegsize = 2048;	/* 2K */
918 
919 			/* XXX some drivers set IFCAP_TSO after ethernet attach */
920 			if (ifp->if_capabilities & IFCAP_TSO) {
921 				if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
922 				    ifp->if_hw_tsomax,
923 				    ifp->if_hw_tsomaxsegcount,
924 				    ifp->if_hw_tsomaxsegsize);
925 			}
926 		}
927 #endif
928 	}
929 
930 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
931 	if_link_ifnet(ifp);
932 	EVENTHANDLER_INVOKE(ifnet_attached_event, ifp);
933 	if (IS_DEFAULT_VNET(curvnet))
934 		devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
935 }
936 
937 static void
938 if_epochalloc(void *dummy __unused)
939 {
940 
941 	net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
942 }
943 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
944 
945 /*
946  * Remove any unicast or broadcast network addresses from an interface.
947  */
948 void
949 if_purgeaddrs(struct ifnet *ifp)
950 {
951 	struct ifaddr *ifa;
952 
953 #ifdef INET6
954 	/*
955 	 * Need to leave multicast addresses of proxy NDP llentries
956 	 * before in6_purgeifaddr() because the llentries are keys
957 	 * for in6_multi objects of proxy NDP entries.
958 	 * in6_purgeifaddr()s clean up llentries including proxy NDPs
959 	 * then we would lose the keys if they are called earlier.
960 	 */
961 	in6_purge_proxy_ndp(ifp);
962 #endif
963 	while (1) {
964 		struct epoch_tracker et;
965 
966 		NET_EPOCH_ENTER(et);
967 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
968 			if (ifa->ifa_addr->sa_family != AF_LINK)
969 				break;
970 		}
971 		NET_EPOCH_EXIT(et);
972 
973 		if (ifa == NULL)
974 			break;
975 #ifdef INET
976 		/* XXX: Ugly!! ad hoc just for INET */
977 		if (ifa->ifa_addr->sa_family == AF_INET) {
978 			struct ifreq ifr;
979 
980 			bzero(&ifr, sizeof(ifr));
981 			ifr.ifr_addr = *ifa->ifa_addr;
982 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
983 			    NULL) == 0)
984 				continue;
985 		}
986 #endif /* INET */
987 #ifdef INET6
988 		if (ifa->ifa_addr->sa_family == AF_INET6) {
989 			in6_purgeifaddr((struct in6_ifaddr *)ifa);
990 			/* ifp_addrhead is already updated */
991 			continue;
992 		}
993 #endif /* INET6 */
994 		IF_ADDR_WLOCK(ifp);
995 		CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
996 		IF_ADDR_WUNLOCK(ifp);
997 		ifa_free(ifa);
998 	}
999 }
1000 
1001 /*
1002  * Remove any multicast network addresses from an interface when an ifnet
1003  * is going away.
1004  */
1005 static void
1006 if_purgemaddrs(struct ifnet *ifp)
1007 {
1008 	struct ifmultiaddr *ifma;
1009 
1010 	IF_ADDR_WLOCK(ifp);
1011 	while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1012 		ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1013 		CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1014 		if_delmulti_locked(ifp, ifma, 1);
1015 	}
1016 	IF_ADDR_WUNLOCK(ifp);
1017 }
1018 
1019 /*
1020  * Detach an interface, removing it from the list of "active" interfaces.
1021  * If vmove flag is set on entry to if_detach_internal(), perform only a
1022  * limited subset of cleanup tasks, given that we are moving an ifnet from
1023  * one vnet to another, where it must be fully operational.
1024  *
1025  * XXXRW: There are some significant questions about event ordering, and
1026  * how to prevent things from starting to use the interface during detach.
1027  */
1028 void
1029 if_detach(struct ifnet *ifp)
1030 {
1031 	bool found;
1032 
1033 	/*
1034 	 * The driver private data holds a strong reference to the ifnet, and
1035 	 * it is actually the "owner", hence this routine shall never fail.
1036 	 *
1037 	 * Ideally we can loop retrying when we lose race with other threads
1038 	 * those run if_unlink_ifnet(). For simplicity, use ifnet_detach_sxlock
1039 	 * to serialize all the detach / vmove operations.
1040 	 */
1041 	sx_xlock(&ifnet_detach_sxlock);
1042 	CURVNET_SET_QUIET(ifp->if_vnet);
1043 	found = if_unlink_ifnet(ifp, false);
1044 	if (! found)
1045 		panic("%s: interface is not on the active list",
1046 		    ifp->if_xname);
1047 	if_detach_internal(ifp, false);
1048 	CURVNET_RESTORE();
1049 	sx_xunlock(&ifnet_detach_sxlock);
1050 }
1051 
1052 /*
1053  * The vmove flag, if set, indicates that we are called from a callpath
1054  * that is moving an interface to a different vnet instance.
1055  *
1056  * The shutdown flag, if set, indicates that we are called in the
1057  * process of shutting down a vnet instance.  Currently only the
1058  * vnet_if_return SYSUNINIT function sets it.  Note: we can be called
1059  * on a vnet instance shutdown without this flag being set, e.g., when
1060  * the cloned interfaces are destoyed as first thing of teardown.
1061  */
1062 static void
1063 if_detach_internal(struct ifnet *ifp, bool vmove)
1064 {
1065 	struct ifaddr *ifa;
1066 #ifdef VIMAGE
1067 	bool shutdown;
1068 
1069 	shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1070 #endif
1071 
1072 	sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
1073 
1074 	/*
1075 	 * At this point we know the interface still was on the ifnet list
1076 	 * and we removed it so we are in a stable state.
1077 	 */
1078 	NET_EPOCH_WAIT();
1079 
1080 	/*
1081 	 * Ensure all pending EPOCH(9) callbacks have been executed. This
1082 	 * fixes issues about late destruction of multicast options
1083 	 * which lead to leave group calls, which in turn access the
1084 	 * belonging ifnet structure:
1085 	 */
1086 	NET_EPOCH_DRAIN_CALLBACKS();
1087 
1088 	/*
1089 	 * In any case (destroy or vmove) detach us from the groups
1090 	 * and remove/wait for pending events on the taskq.
1091 	 * XXX-BZ in theory an interface could still enqueue a taskq change?
1092 	 */
1093 	if_delgroups(ifp);
1094 
1095 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1096 	taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1097 
1098 	if_down(ifp);
1099 
1100 #ifdef VIMAGE
1101 	/*
1102 	 * On VNET shutdown abort here as the stack teardown will do all
1103 	 * the work top-down for us.
1104 	 */
1105 	if (shutdown) {
1106 		/* Give interface users the chance to clean up. */
1107 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1108 
1109 		/*
1110 		 * In case of a vmove we are done here without error.
1111 		 * If we would signal an error it would lead to the same
1112 		 * abort as if we did not find the ifnet anymore.
1113 		 * if_detach() calls us in void context and does not care
1114 		 * about an early abort notification, so life is splendid :)
1115 		 */
1116 		return;
1117 	}
1118 #endif
1119 
1120 	/*
1121 	 * At this point we are not tearing down a VNET and are either
1122 	 * going to destroy or vmove the interface and have to cleanup
1123 	 * accordingly.
1124 	 */
1125 
1126 	/*
1127 	 * Remove routes and flush queues.
1128 	 */
1129 #ifdef ALTQ
1130 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
1131 		altq_disable(&ifp->if_snd);
1132 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1133 		altq_detach(&ifp->if_snd);
1134 #endif
1135 
1136 	rt_flushifroutes(ifp);
1137 
1138 	if_purgeaddrs(ifp);
1139 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1140 	if_purgemaddrs(ifp);
1141 	if (IS_DEFAULT_VNET(curvnet))
1142 		devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1143 
1144 	if (!vmove) {
1145 		/*
1146 		 * Prevent further calls into the device driver via ifnet.
1147 		 */
1148 		if_dead(ifp);
1149 
1150 		/*
1151 		 * Clean up all addresses.
1152 		 */
1153 		IF_ADDR_WLOCK(ifp);
1154 		if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1155 			ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1156 			CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1157 			IF_ADDR_WUNLOCK(ifp);
1158 			ifa_free(ifa);
1159 		} else
1160 			IF_ADDR_WUNLOCK(ifp);
1161 	}
1162 }
1163 
1164 #ifdef VIMAGE
1165 /*
1166  * if_vmove() performs a limited version of if_detach() in current
1167  * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1168  */
1169 static void
1170 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1171 {
1172 #ifdef DEV_BPF
1173 	/*
1174 	 * Detach BPF file descriptors from its interface.
1175 	 */
1176 	bpf_ifdetach(ifp);
1177 #endif
1178 
1179 	/*
1180 	 * Detach from current vnet, but preserve LLADDR info, do not
1181 	 * mark as dead etc. so that the ifnet can be reattached later.
1182 	 */
1183 	if_detach_internal(ifp, true);
1184 
1185 	/*
1186 	 * Perform interface-specific reassignment tasks, if provided by
1187 	 * the driver.
1188 	 */
1189 	if (ifp->if_reassign != NULL)
1190 		ifp->if_reassign(ifp, new_vnet, NULL);
1191 
1192 	/*
1193 	 * Switch to the context of the target vnet.
1194 	 */
1195 	CURVNET_SET_QUIET(new_vnet);
1196 	if_attach_internal(ifp, true);
1197 	bpf_vmove(ifp->if_bpf);
1198 	CURVNET_RESTORE();
1199 }
1200 
1201 /*
1202  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1203  */
1204 static int
1205 if_vmove_loan(struct thread *td, char *ifname, int jid)
1206 {
1207 	struct prison *pr;
1208 	struct ifnet *ifp, *difp;
1209 	bool found;
1210 
1211 	MPASS(curthread == td);
1212 	MPASS(curvnet == TD_TO_VNET(td));
1213 
1214 	/*
1215 	 * We check the existence of the interface, and will later try to
1216 	 * unlink it from the "active" list, so it is sufficient to only
1217 	 * hold a weak reference to it.
1218 	 * Be aware that it is unsafe to access any member of it, until it
1219 	 * is proven to be safe to ( say it was on the "active" list ).
1220 	 */
1221 	ifp = ifunit(ifname);
1222 	if (ifp == NULL)
1223 		return (ENXIO);
1224 
1225 	/* Try to find the prison within our visibility. */
1226 	sx_slock(&allprison_lock);
1227 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1228 	sx_sunlock(&allprison_lock);
1229 	if (pr == NULL)
1230 		return (ENXIO);
1231 	/* Do not try to move the iface from and to the same vnet. */
1232 	if (pr->pr_vnet == TD_TO_VNET(td)) {
1233 		mtx_unlock(&pr->pr_mtx);
1234 		return (EEXIST);
1235 	}
1236 	prison_hold_locked(pr);
1237 	mtx_unlock(&pr->pr_mtx);
1238 
1239 	/* Make sure the named iface does not exists in the dst. prison/vnet. */
1240 	/* XXX Lock interfaces to avoid races. */
1241 	CURVNET_SET_QUIET(pr->pr_vnet);
1242 	difp = ifunit(ifname);
1243 	CURVNET_RESTORE();
1244 	if (difp != NULL) {
1245 		prison_free(pr);
1246 		return (EEXIST);
1247 	}
1248 	sx_xlock(&ifnet_detach_sxlock);
1249 
1250 	found = if_unlink_ifnet(ifp, true);
1251 	if (! found) {
1252 		sx_xunlock(&ifnet_detach_sxlock);
1253 		prison_free(pr);
1254 		return (ENODEV);
1255 	}
1256 
1257 	/* Move the interface into the child jail/vnet. */
1258 	if_vmove(ifp, pr->pr_vnet);
1259 
1260 	/* Report the new if_xname back to the userland. */
1261 	sprintf(ifname, "%s", ifp->if_xname);
1262 
1263 	sx_xunlock(&ifnet_detach_sxlock);
1264 
1265 	prison_free(pr);
1266 	return (0);
1267 }
1268 
1269 static int
1270 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1271 {
1272 	struct prison *pr;
1273 	struct vnet *vnet_dst;
1274 	struct ifnet *ifp;
1275 	int found;
1276 
1277 	/* Try to find the prison within our visibility. */
1278 	sx_slock(&allprison_lock);
1279 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1280 	sx_sunlock(&allprison_lock);
1281 	if (pr == NULL)
1282 		return (ENXIO);
1283 	prison_hold_locked(pr);
1284 	mtx_unlock(&pr->pr_mtx);
1285 
1286 	/* Make sure the named iface exists in the source prison/vnet. */
1287 	CURVNET_SET(pr->pr_vnet);
1288 	ifp = ifunit(ifname);
1289 	if (ifp == NULL) {
1290 		CURVNET_RESTORE();
1291 		prison_free(pr);
1292 		return (ENXIO);
1293 	}
1294 
1295 	/* Do not try to move the iface from and to the same vnet. */
1296 	vnet_dst = TD_TO_VNET(td);
1297 	if (vnet_dst == pr->pr_vnet) {
1298 		CURVNET_RESTORE();
1299 		prison_free(pr);
1300 		return (EEXIST);
1301 	}
1302 
1303 	/* Get interface back from child jail/vnet. */
1304 	sx_xlock(&ifnet_detach_sxlock);
1305 	found = if_unlink_ifnet(ifp, true);
1306 	if (! found) {
1307 		sx_xunlock(&ifnet_detach_sxlock);
1308 		CURVNET_RESTORE();
1309 		prison_free(pr);
1310 		return (ENODEV);
1311 	}
1312 	if_vmove(ifp, vnet_dst);
1313 	sx_xunlock(&ifnet_detach_sxlock);
1314 	CURVNET_RESTORE();
1315 
1316 	/* Report the new if_xname back to the userland. */
1317 	sprintf(ifname, "%s", ifp->if_xname);
1318 
1319 	prison_free(pr);
1320 	return (0);
1321 }
1322 #endif /* VIMAGE */
1323 
1324 /*
1325  * Add a group to an interface
1326  */
1327 int
1328 if_addgroup(struct ifnet *ifp, const char *groupname)
1329 {
1330 	struct ifg_list		*ifgl;
1331 	struct ifg_group	*ifg = NULL;
1332 	struct ifg_member	*ifgm;
1333 	int 			 new = 0;
1334 
1335 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1336 	    groupname[strlen(groupname) - 1] <= '9')
1337 		return (EINVAL);
1338 
1339 	IFNET_WLOCK();
1340 	CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1341 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1342 			IFNET_WUNLOCK();
1343 			return (EEXIST);
1344 		}
1345 
1346 	if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1347 	    	IFNET_WUNLOCK();
1348 		return (ENOMEM);
1349 	}
1350 
1351 	if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1352 		free(ifgl, M_TEMP);
1353 		IFNET_WUNLOCK();
1354 		return (ENOMEM);
1355 	}
1356 
1357 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1358 		if (!strcmp(ifg->ifg_group, groupname))
1359 			break;
1360 
1361 	if (ifg == NULL) {
1362 		if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1363 			free(ifgl, M_TEMP);
1364 			free(ifgm, M_TEMP);
1365 			IFNET_WUNLOCK();
1366 			return (ENOMEM);
1367 		}
1368 		strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1369 		ifg->ifg_refcnt = 0;
1370 		CK_STAILQ_INIT(&ifg->ifg_members);
1371 		CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1372 		new = 1;
1373 	}
1374 
1375 	ifg->ifg_refcnt++;
1376 	ifgl->ifgl_group = ifg;
1377 	ifgm->ifgm_ifp = ifp;
1378 
1379 	CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1380 	CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1381 	IFNET_WUNLOCK();
1382 
1383 	if (new)
1384 		EVENTHANDLER_INVOKE(group_attach_event, ifg);
1385 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1386 
1387 	return (0);
1388 }
1389 
1390 /*
1391  * Helper function to remove a group out of an interface.  Expects the global
1392  * ifnet lock to be write-locked, and drops it before returning.
1393  */
1394 static void
1395 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1396     const char *groupname)
1397 {
1398 	struct ifg_member *ifgm;
1399 	bool freeifgl;
1400 
1401 	IFNET_WLOCK_ASSERT();
1402 
1403 	CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1404 
1405 	CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1406 		if (ifgm->ifgm_ifp == ifp) {
1407 			CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1408 			    ifg_member, ifgm_next);
1409 			break;
1410 		}
1411 	}
1412 
1413 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1414 		CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1415 		    ifg_next);
1416 		freeifgl = true;
1417 	} else {
1418 		freeifgl = false;
1419 	}
1420 	IFNET_WUNLOCK();
1421 
1422 	NET_EPOCH_WAIT();
1423 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1424 	if (freeifgl) {
1425 		EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1426 		free(ifgl->ifgl_group, M_TEMP);
1427 	}
1428 	free(ifgm, M_TEMP);
1429 	free(ifgl, M_TEMP);
1430 }
1431 
1432 /*
1433  * Remove a group from an interface
1434  */
1435 int
1436 if_delgroup(struct ifnet *ifp, const char *groupname)
1437 {
1438 	struct ifg_list *ifgl;
1439 
1440 	IFNET_WLOCK();
1441 	CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1442 		if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1443 			break;
1444 	if (ifgl == NULL) {
1445 		IFNET_WUNLOCK();
1446 		return (ENOENT);
1447 	}
1448 
1449 	_if_delgroup_locked(ifp, ifgl, groupname);
1450 
1451 	return (0);
1452 }
1453 
1454 /*
1455  * Remove an interface from all groups
1456  */
1457 static void
1458 if_delgroups(struct ifnet *ifp)
1459 {
1460 	struct ifg_list *ifgl;
1461 	char groupname[IFNAMSIZ];
1462 
1463 	IFNET_WLOCK();
1464 	while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1465 		strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1466 		_if_delgroup_locked(ifp, ifgl, groupname);
1467 		IFNET_WLOCK();
1468 	}
1469 	IFNET_WUNLOCK();
1470 }
1471 
1472 /*
1473  * XXX: This KPI should not expose ifg_group. therefore the current
1474  * implementation is questionable and may change in the future.
1475  */
1476 int
1477 if_foreach_group(struct ifnet *ifp, if_foreach_group_cb_t cb, void *cb_arg)
1478 {
1479 	struct ifg_list *ifgl;
1480 	int error;
1481 
1482 	MPASS(cb);
1483 
1484 	error = 0;
1485 	IFNET_RLOCK();
1486 	CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1487 		error = cb(ifgl->ifgl_group, cb_arg);
1488 		if (error != 0)
1489 			break;
1490 	}
1491 	IFNET_RUNLOCK();
1492 
1493 	return (error);
1494 }
1495 
1496 /*
1497  * Stores all groups from an interface in memory pointed to by ifgr.
1498  */
1499 static int
1500 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1501 {
1502 	struct ifg_list *ifgl;
1503 	struct ifg_req ifgrq, *ifgp;
1504 	int len, error;
1505 
1506 	IFNET_RLOCK();
1507 	if (ifgr->ifgr_len == 0) {
1508 		CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1509 			ifgr->ifgr_len += sizeof(struct ifg_req);
1510 		error = 0;
1511 	} else {
1512 		len = ifgr->ifgr_len;
1513 		ifgp = ifgr->ifgr_groups;
1514 		CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1515 			if (len < sizeof(ifgrq)) {
1516 				error = EINVAL;
1517 				break;
1518 			}
1519 			bzero(&ifgrq, sizeof ifgrq);
1520 			strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1521 			    sizeof(ifgrq.ifgrq_group));
1522 			if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1523 				break;
1524 			len -= sizeof(ifgrq);
1525 			ifgp++;
1526 		}
1527 	}
1528 	IFNET_RUNLOCK();
1529 
1530 	return (error);
1531 }
1532 
1533 /*
1534  * Stores all members of a group in memory pointed to by igfr
1535  */
1536 static int
1537 if_getgroupmembers(struct ifgroupreq *ifgr)
1538 {
1539 	struct ifg_group	*ifg;
1540 	struct ifg_member	*ifgm;
1541 	struct ifg_req		 ifgrq, *ifgp;
1542 	int			 len, error;
1543 
1544 	IFNET_RLOCK();
1545 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1546 		if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1547 			break;
1548 	if (ifg == NULL) {
1549 		IFNET_RUNLOCK();
1550 		return (ENOENT);
1551 	}
1552 
1553 	if (ifgr->ifgr_len == 0) {
1554 		CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1555 			ifgr->ifgr_len += sizeof(ifgrq);
1556 		IFNET_RUNLOCK();
1557 		return (0);
1558 	}
1559 
1560 	len = ifgr->ifgr_len;
1561 	ifgp = ifgr->ifgr_groups;
1562 	CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1563 		if (len < sizeof(ifgrq)) {
1564 			IFNET_RUNLOCK();
1565 			return (EINVAL);
1566 		}
1567 		bzero(&ifgrq, sizeof ifgrq);
1568 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1569 		    sizeof(ifgrq.ifgrq_member));
1570 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1571 			IFNET_RUNLOCK();
1572 			return (error);
1573 		}
1574 		len -= sizeof(ifgrq);
1575 		ifgp++;
1576 	}
1577 	IFNET_RUNLOCK();
1578 
1579 	return (0);
1580 }
1581 
1582 /*
1583  * Return counter values from counter(9)s stored in ifnet.
1584  */
1585 uint64_t
1586 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1587 {
1588 
1589 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1590 
1591 	return (counter_u64_fetch(ifp->if_counters[cnt]));
1592 }
1593 
1594 /*
1595  * Increase an ifnet counter. Usually used for counters shared
1596  * between the stack and a driver, but function supports them all.
1597  */
1598 void
1599 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1600 {
1601 
1602 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1603 
1604 	counter_u64_add(ifp->if_counters[cnt], inc);
1605 }
1606 
1607 /*
1608  * Copy data from ifnet to userland API structure if_data.
1609  */
1610 void
1611 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1612 {
1613 
1614 	ifd->ifi_type = ifp->if_type;
1615 	ifd->ifi_physical = 0;
1616 	ifd->ifi_addrlen = ifp->if_addrlen;
1617 	ifd->ifi_hdrlen = ifp->if_hdrlen;
1618 	ifd->ifi_link_state = ifp->if_link_state;
1619 	ifd->ifi_vhid = 0;
1620 	ifd->ifi_datalen = sizeof(struct if_data);
1621 	ifd->ifi_mtu = ifp->if_mtu;
1622 	ifd->ifi_metric = ifp->if_metric;
1623 	ifd->ifi_baudrate = ifp->if_baudrate;
1624 	ifd->ifi_hwassist = ifp->if_hwassist;
1625 	ifd->ifi_epoch = ifp->if_epoch;
1626 	ifd->ifi_lastchange = ifp->if_lastchange;
1627 
1628 	ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1629 	ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1630 	ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1631 	ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1632 	ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1633 	ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1634 	ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1635 	ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1636 	ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1637 	ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1638 	ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1639 	ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1640 }
1641 
1642 /*
1643  * Initialization, destruction and refcounting functions for ifaddrs.
1644  */
1645 struct ifaddr *
1646 ifa_alloc(size_t size, int flags)
1647 {
1648 	struct ifaddr *ifa;
1649 
1650 	KASSERT(size >= sizeof(struct ifaddr),
1651 	    ("%s: invalid size %zu", __func__, size));
1652 
1653 	ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1654 	if (ifa == NULL)
1655 		return (NULL);
1656 
1657 	if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1658 		goto fail;
1659 	if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1660 		goto fail;
1661 	if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1662 		goto fail;
1663 	if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1664 		goto fail;
1665 
1666 	refcount_init(&ifa->ifa_refcnt, 1);
1667 
1668 	return (ifa);
1669 
1670 fail:
1671 	/* free(NULL) is okay */
1672 	counter_u64_free(ifa->ifa_opackets);
1673 	counter_u64_free(ifa->ifa_ipackets);
1674 	counter_u64_free(ifa->ifa_obytes);
1675 	counter_u64_free(ifa->ifa_ibytes);
1676 	free(ifa, M_IFADDR);
1677 
1678 	return (NULL);
1679 }
1680 
1681 void
1682 ifa_ref(struct ifaddr *ifa)
1683 {
1684 	u_int old __diagused;
1685 
1686 	old = refcount_acquire(&ifa->ifa_refcnt);
1687 	KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1688 }
1689 
1690 int
1691 ifa_try_ref(struct ifaddr *ifa)
1692 {
1693 
1694 	NET_EPOCH_ASSERT();
1695 	return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1696 }
1697 
1698 static void
1699 ifa_destroy(epoch_context_t ctx)
1700 {
1701 	struct ifaddr *ifa;
1702 
1703 	ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1704 	counter_u64_free(ifa->ifa_opackets);
1705 	counter_u64_free(ifa->ifa_ipackets);
1706 	counter_u64_free(ifa->ifa_obytes);
1707 	counter_u64_free(ifa->ifa_ibytes);
1708 	free(ifa, M_IFADDR);
1709 }
1710 
1711 void
1712 ifa_free(struct ifaddr *ifa)
1713 {
1714 
1715 	if (refcount_release(&ifa->ifa_refcnt))
1716 		NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1717 }
1718 
1719 /*
1720  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1721  * structs used to represent other address families, it is necessary
1722  * to perform a different comparison.
1723  */
1724 static bool
1725 sa_dl_equal(const struct sockaddr *a, const struct sockaddr *b)
1726 {
1727 	const struct sockaddr_dl *sdl1 = (const struct sockaddr_dl *)a;
1728 	const struct sockaddr_dl *sdl2 = (const struct sockaddr_dl *)b;
1729 
1730 	return (sdl1->sdl_len == sdl2->sdl_len &&
1731 	    bcmp(sdl1->sdl_data + sdl1->sdl_nlen,
1732 	    sdl2->sdl_data + sdl2->sdl_nlen, sdl1->sdl_alen) == 0);
1733 }
1734 
1735 /*
1736  * Locate an interface on the specified fib based on a complete address.
1737  */
1738 struct ifaddr *
1739 ifa_ifwithaddr_fib(const struct sockaddr *addr, int fibnum)
1740 {
1741 	struct ifnet *ifp;
1742 	struct ifaddr *ifa;
1743 
1744 	NET_EPOCH_ASSERT();
1745 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1746 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1747 			continue;
1748 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1749 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1750 				continue;
1751 			if (sa_equal(addr, ifa->ifa_addr)) {
1752 				goto done;
1753 			}
1754 			/* IP6 doesn't have broadcast */
1755 			if ((ifp->if_flags & IFF_BROADCAST) &&
1756 			    ifa->ifa_broadaddr &&
1757 			    ifa->ifa_broadaddr->sa_len != 0 &&
1758 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1759 				goto done;
1760 			}
1761 		}
1762 	}
1763 	ifa = NULL;
1764 done:
1765 	return (ifa);
1766 }
1767 
1768 /*
1769  * Locate an interface based on a complete address.
1770  */
1771 struct ifaddr *
1772 ifa_ifwithaddr(const struct sockaddr *addr)
1773 {
1774 
1775 	return (ifa_ifwithaddr_fib(addr, RT_ALL_FIBS));
1776 }
1777 
1778 int
1779 ifa_ifwithaddr_fib_check(const struct sockaddr *addr, int fibnum)
1780 {
1781 	struct epoch_tracker et;
1782 	int rc;
1783 
1784 	NET_EPOCH_ENTER(et);
1785 	rc = (ifa_ifwithaddr_fib(addr, fibnum) != NULL);
1786 	NET_EPOCH_EXIT(et);
1787 	return (rc);
1788 }
1789 
1790 int
1791 ifa_ifwithaddr_check(const struct sockaddr *addr)
1792 {
1793 
1794 	return (ifa_ifwithaddr_fib_check(addr, RT_ALL_FIBS));
1795 }
1796 
1797 /*
1798  * Locate an interface based on the broadcast address.
1799  */
1800 /* ARGSUSED */
1801 struct ifaddr *
1802 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1803 {
1804 	struct ifnet *ifp;
1805 	struct ifaddr *ifa;
1806 
1807 	NET_EPOCH_ASSERT();
1808 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1809 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1810 			continue;
1811 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1812 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1813 				continue;
1814 			if ((ifp->if_flags & IFF_BROADCAST) &&
1815 			    ifa->ifa_broadaddr &&
1816 			    ifa->ifa_broadaddr->sa_len != 0 &&
1817 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1818 				goto done;
1819 			}
1820 		}
1821 	}
1822 	ifa = NULL;
1823 done:
1824 	return (ifa);
1825 }
1826 
1827 /*
1828  * Locate the point to point interface with a given destination address.
1829  */
1830 /*ARGSUSED*/
1831 struct ifaddr *
1832 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1833 {
1834 	struct ifnet *ifp;
1835 	struct ifaddr *ifa;
1836 
1837 	NET_EPOCH_ASSERT();
1838 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1839 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1840 			continue;
1841 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1842 			continue;
1843 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1844 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1845 				continue;
1846 			if (ifa->ifa_dstaddr != NULL &&
1847 			    sa_equal(addr, ifa->ifa_dstaddr)) {
1848 				goto done;
1849 			}
1850 		}
1851 	}
1852 	ifa = NULL;
1853 done:
1854 	return (ifa);
1855 }
1856 
1857 /*
1858  * Find an interface on a specific network.  If many, choice
1859  * is most specific found.
1860  */
1861 struct ifaddr *
1862 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1863 {
1864 	struct ifnet *ifp;
1865 	struct ifaddr *ifa;
1866 	struct ifaddr *ifa_maybe = NULL;
1867 	u_int af = addr->sa_family;
1868 	const char *addr_data = addr->sa_data, *cplim;
1869 
1870 	NET_EPOCH_ASSERT();
1871 	/*
1872 	 * AF_LINK addresses can be looked up directly by their index number,
1873 	 * so do that if we can.
1874 	 */
1875 	if (af == AF_LINK) {
1876 		ifp = ifnet_byindex(
1877 		    ((const struct sockaddr_dl *)addr)->sdl_index);
1878 		return (ifp ? ifp->if_addr : NULL);
1879 	}
1880 
1881 	/*
1882 	 * Scan though each interface, looking for ones that have addresses
1883 	 * in this address family and the requested fib.
1884 	 */
1885 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1886 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1887 			continue;
1888 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1889 			const char *cp, *cp2, *cp3;
1890 
1891 			if (ifa->ifa_addr->sa_family != af)
1892 next:				continue;
1893 			if (af == AF_INET &&
1894 			    ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1895 				/*
1896 				 * This is a bit broken as it doesn't
1897 				 * take into account that the remote end may
1898 				 * be a single node in the network we are
1899 				 * looking for.
1900 				 * The trouble is that we don't know the
1901 				 * netmask for the remote end.
1902 				 */
1903 				if (ifa->ifa_dstaddr != NULL &&
1904 				    sa_equal(addr, ifa->ifa_dstaddr)) {
1905 					goto done;
1906 				}
1907 			} else {
1908 				/*
1909 				 * Scan all the bits in the ifa's address.
1910 				 * If a bit dissagrees with what we are
1911 				 * looking for, mask it with the netmask
1912 				 * to see if it really matters.
1913 				 * (A byte at a time)
1914 				 */
1915 				if (ifa->ifa_netmask == 0)
1916 					continue;
1917 				cp = addr_data;
1918 				cp2 = ifa->ifa_addr->sa_data;
1919 				cp3 = ifa->ifa_netmask->sa_data;
1920 				cplim = ifa->ifa_netmask->sa_len
1921 					+ (char *)ifa->ifa_netmask;
1922 				while (cp3 < cplim)
1923 					if ((*cp++ ^ *cp2++) & *cp3++)
1924 						goto next; /* next address! */
1925 				/*
1926 				 * If the netmask of what we just found
1927 				 * is more specific than what we had before
1928 				 * (if we had one), or if the virtual status
1929 				 * of new prefix is better than of the old one,
1930 				 * then remember the new one before continuing
1931 				 * to search for an even better one.
1932 				 */
1933 				if (ifa_maybe == NULL ||
1934 				    ifa_preferred(ifa_maybe, ifa) ||
1935 				    rn_refines((caddr_t)ifa->ifa_netmask,
1936 				    (caddr_t)ifa_maybe->ifa_netmask)) {
1937 					ifa_maybe = ifa;
1938 				}
1939 			}
1940 		}
1941 	}
1942 	ifa = ifa_maybe;
1943 	ifa_maybe = NULL;
1944 done:
1945 	return (ifa);
1946 }
1947 
1948 /*
1949  * Find an interface address specific to an interface best matching
1950  * a given address.
1951  */
1952 struct ifaddr *
1953 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1954 {
1955 	struct ifaddr *ifa;
1956 	const char *cp, *cp2, *cp3;
1957 	char *cplim;
1958 	struct ifaddr *ifa_maybe = NULL;
1959 	u_int af = addr->sa_family;
1960 
1961 	if (af >= AF_MAX)
1962 		return (NULL);
1963 
1964 	NET_EPOCH_ASSERT();
1965 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1966 		if (ifa->ifa_addr->sa_family != af)
1967 			continue;
1968 		if (ifa_maybe == NULL)
1969 			ifa_maybe = ifa;
1970 		if (ifa->ifa_netmask == 0) {
1971 			if (sa_equal(addr, ifa->ifa_addr) ||
1972 			    (ifa->ifa_dstaddr &&
1973 			    sa_equal(addr, ifa->ifa_dstaddr)))
1974 				goto done;
1975 			continue;
1976 		}
1977 		if (ifp->if_flags & IFF_POINTOPOINT) {
1978 			if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
1979 				goto done;
1980 		} else {
1981 			cp = addr->sa_data;
1982 			cp2 = ifa->ifa_addr->sa_data;
1983 			cp3 = ifa->ifa_netmask->sa_data;
1984 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1985 			for (; cp3 < cplim; cp3++)
1986 				if ((*cp++ ^ *cp2++) & *cp3)
1987 					break;
1988 			if (cp3 == cplim)
1989 				goto done;
1990 		}
1991 	}
1992 	ifa = ifa_maybe;
1993 done:
1994 	return (ifa);
1995 }
1996 
1997 /*
1998  * See whether new ifa is better than current one:
1999  * 1) A non-virtual one is preferred over virtual.
2000  * 2) A virtual in master state preferred over any other state.
2001  *
2002  * Used in several address selecting functions.
2003  */
2004 int
2005 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2006 {
2007 
2008 	return (cur->ifa_carp && (!next->ifa_carp ||
2009 	    ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2010 }
2011 
2012 struct sockaddr_dl *
2013 link_alloc_sdl(size_t size, int flags)
2014 {
2015 
2016 	return (malloc(size, M_TEMP, flags));
2017 }
2018 
2019 void
2020 link_free_sdl(struct sockaddr *sa)
2021 {
2022 	free(sa, M_TEMP);
2023 }
2024 
2025 /*
2026  * Fills in given sdl with interface basic info.
2027  * Returns pointer to filled sdl.
2028  */
2029 struct sockaddr_dl *
2030 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2031 {
2032 	struct sockaddr_dl *sdl;
2033 
2034 	sdl = (struct sockaddr_dl *)paddr;
2035 	memset(sdl, 0, sizeof(struct sockaddr_dl));
2036 	sdl->sdl_len = sizeof(struct sockaddr_dl);
2037 	sdl->sdl_family = AF_LINK;
2038 	sdl->sdl_index = ifp->if_index;
2039 	sdl->sdl_type = iftype;
2040 
2041 	return (sdl);
2042 }
2043 
2044 void	(*vlan_link_state_p)(struct ifnet *);	/* XXX: private from if_vlan */
2045 void	(*vlan_trunk_cap_p)(struct ifnet *);		/* XXX: private from if_vlan */
2046 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2047 struct	ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2048 int	(*vlan_tag_p)(struct ifnet *, uint16_t *);
2049 int	(*vlan_pcp_p)(struct ifnet *, uint16_t *);
2050 int	(*vlan_setcookie_p)(struct ifnet *, void *);
2051 void	*(*vlan_cookie_p)(struct ifnet *);
2052 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
2053 
2054 /*
2055  * Handle a change in the interface link state. To avoid LORs
2056  * between driver lock and upper layer locks, as well as possible
2057  * recursions, we post event to taskqueue, and all job
2058  * is done in static do_link_state_change().
2059  */
2060 void
2061 if_link_state_change(struct ifnet *ifp, int link_state)
2062 {
2063 	/* Return if state hasn't changed. */
2064 	if (ifp->if_link_state == link_state)
2065 		return;
2066 
2067 	ifp->if_link_state = link_state;
2068 
2069 	/* XXXGL: reference ifp? */
2070 	taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2071 }
2072 
2073 static void
2074 do_link_state_change(void *arg, int pending)
2075 {
2076 	struct ifnet *ifp;
2077 	int link_state;
2078 
2079 	ifp = arg;
2080 	link_state = ifp->if_link_state;
2081 
2082 	CURVNET_SET(ifp->if_vnet);
2083 	rt_ifmsg(ifp, 0);
2084 	if (ifp->if_vlantrunk != NULL)
2085 		(*vlan_link_state_p)(ifp);
2086 	if (ifp->if_carp)
2087 		(*carp_linkstate_p)(ifp);
2088 	if (ifp->if_bridge)
2089 		ifp->if_bridge_linkstate(ifp);
2090 	if (ifp->if_lagg)
2091 		(*lagg_linkstate_p)(ifp, link_state);
2092 
2093 	if (IS_DEFAULT_VNET(curvnet))
2094 		devctl_notify("IFNET", ifp->if_xname,
2095 		    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2096 		    NULL);
2097 	if (pending > 1)
2098 		if_printf(ifp, "%d link states coalesced\n", pending);
2099 	if (log_link_state_change)
2100 		if_printf(ifp, "link state changed to %s\n",
2101 		    (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2102 	EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2103 	CURVNET_RESTORE();
2104 }
2105 
2106 /*
2107  * Mark an interface down and notify protocols of
2108  * the transition.
2109  */
2110 void
2111 if_down(struct ifnet *ifp)
2112 {
2113 
2114 	EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2115 
2116 	ifp->if_flags &= ~IFF_UP;
2117 	getmicrotime(&ifp->if_lastchange);
2118 	ifp->if_qflush(ifp);
2119 
2120 	if (ifp->if_carp)
2121 		(*carp_linkstate_p)(ifp);
2122 	rt_ifmsg(ifp, IFF_UP);
2123 }
2124 
2125 /*
2126  * Mark an interface up and notify protocols of
2127  * the transition.
2128  */
2129 void
2130 if_up(struct ifnet *ifp)
2131 {
2132 
2133 	ifp->if_flags |= IFF_UP;
2134 	getmicrotime(&ifp->if_lastchange);
2135 	if (ifp->if_carp)
2136 		(*carp_linkstate_p)(ifp);
2137 	rt_ifmsg(ifp, IFF_UP);
2138 	EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2139 }
2140 
2141 /*
2142  * Flush an interface queue.
2143  */
2144 void
2145 if_qflush(struct ifnet *ifp)
2146 {
2147 	struct mbuf *m, *n;
2148 	struct ifaltq *ifq;
2149 
2150 	ifq = &ifp->if_snd;
2151 	IFQ_LOCK(ifq);
2152 #ifdef ALTQ
2153 	if (ALTQ_IS_ENABLED(ifq))
2154 		ALTQ_PURGE(ifq);
2155 #endif
2156 	n = ifq->ifq_head;
2157 	while ((m = n) != NULL) {
2158 		n = m->m_nextpkt;
2159 		m_freem(m);
2160 	}
2161 	ifq->ifq_head = 0;
2162 	ifq->ifq_tail = 0;
2163 	ifq->ifq_len = 0;
2164 	IFQ_UNLOCK(ifq);
2165 }
2166 
2167 /*
2168  * Map interface name to interface structure pointer, with or without
2169  * returning a reference.
2170  */
2171 struct ifnet *
2172 ifunit_ref(const char *name)
2173 {
2174 	struct epoch_tracker et;
2175 	struct ifnet *ifp;
2176 
2177 	NET_EPOCH_ENTER(et);
2178 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2179 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2180 		    !(ifp->if_flags & IFF_DYING)) {
2181 			MPASS(ifp->if_vnet == curvnet);
2182 			MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2183 			if_ref(ifp);
2184 			break;
2185 		}
2186 	}
2187 	NET_EPOCH_EXIT(et);
2188 	return (ifp);
2189 }
2190 
2191 struct ifnet *
2192 ifunit(const char *name)
2193 {
2194 	struct epoch_tracker et;
2195 	struct ifnet *ifp;
2196 
2197 	NET_EPOCH_ENTER(et);
2198 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2199 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) {
2200 			MPASS(refcount_load(&ifp->if_refcount) > 0);
2201 			MPASS(ifp->if_vnet == curvnet);
2202 			MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2203 			break;
2204 		}
2205 	}
2206 	NET_EPOCH_EXIT(et);
2207 	return (ifp);
2208 }
2209 
2210 void *
2211 ifr_buffer_get_buffer(void *data)
2212 {
2213 	union ifreq_union *ifrup;
2214 
2215 	ifrup = data;
2216 #ifdef COMPAT_FREEBSD32
2217 	if (SV_CURPROC_FLAG(SV_ILP32))
2218 		return ((void *)(uintptr_t)
2219 		    ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2220 #endif
2221 	return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2222 }
2223 
2224 static void
2225 ifr_buffer_set_buffer_null(void *data)
2226 {
2227 	union ifreq_union *ifrup;
2228 
2229 	ifrup = data;
2230 #ifdef COMPAT_FREEBSD32
2231 	if (SV_CURPROC_FLAG(SV_ILP32))
2232 		ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2233 	else
2234 #endif
2235 		ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2236 }
2237 
2238 size_t
2239 ifr_buffer_get_length(void *data)
2240 {
2241 	union ifreq_union *ifrup;
2242 
2243 	ifrup = data;
2244 #ifdef COMPAT_FREEBSD32
2245 	if (SV_CURPROC_FLAG(SV_ILP32))
2246 		return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2247 #endif
2248 	return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2249 }
2250 
2251 static void
2252 ifr_buffer_set_length(void *data, size_t len)
2253 {
2254 	union ifreq_union *ifrup;
2255 
2256 	ifrup = data;
2257 #ifdef COMPAT_FREEBSD32
2258 	if (SV_CURPROC_FLAG(SV_ILP32))
2259 		ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2260 	else
2261 #endif
2262 		ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2263 }
2264 
2265 void *
2266 ifr_data_get_ptr(void *ifrp)
2267 {
2268 	union ifreq_union *ifrup;
2269 
2270 	ifrup = ifrp;
2271 #ifdef COMPAT_FREEBSD32
2272 	if (SV_CURPROC_FLAG(SV_ILP32))
2273 		return ((void *)(uintptr_t)
2274 		    ifrup->ifr32.ifr_ifru.ifru_data);
2275 #endif
2276 		return (ifrup->ifr.ifr_ifru.ifru_data);
2277 }
2278 
2279 struct ifcap_nv_bit_name {
2280 	uint64_t cap_bit;
2281 	const char *cap_name;
2282 };
2283 #define CAPNV(x) {.cap_bit = IFCAP_##x, \
2284     .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
2285 const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
2286 	CAPNV(RXCSUM),
2287 	CAPNV(TXCSUM),
2288 	CAPNV(NETCONS),
2289 	CAPNV(VLAN_MTU),
2290 	CAPNV(VLAN_HWTAGGING),
2291 	CAPNV(JUMBO_MTU),
2292 	CAPNV(POLLING),
2293 	CAPNV(VLAN_HWCSUM),
2294 	CAPNV(TSO4),
2295 	CAPNV(TSO6),
2296 	CAPNV(LRO),
2297 	CAPNV(WOL_UCAST),
2298 	CAPNV(WOL_MCAST),
2299 	CAPNV(WOL_MAGIC),
2300 	CAPNV(TOE4),
2301 	CAPNV(TOE6),
2302 	CAPNV(VLAN_HWFILTER),
2303 	CAPNV(VLAN_HWTSO),
2304 	CAPNV(LINKSTATE),
2305 	CAPNV(NETMAP),
2306 	CAPNV(RXCSUM_IPV6),
2307 	CAPNV(TXCSUM_IPV6),
2308 	CAPNV(HWSTATS),
2309 	CAPNV(TXRTLMT),
2310 	CAPNV(HWRXTSTMP),
2311 	CAPNV(MEXTPG),
2312 	CAPNV(TXTLS4),
2313 	CAPNV(TXTLS6),
2314 	CAPNV(VXLAN_HWCSUM),
2315 	CAPNV(VXLAN_HWTSO),
2316 	CAPNV(TXTLS_RTLMT),
2317 	{0, NULL}
2318 };
2319 #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
2320     .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
2321 const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
2322 	CAP2NV(RXTLS4),
2323 	CAP2NV(RXTLS6),
2324 	CAP2NV(IPSEC_OFFLOAD),
2325 	CAP2NV(GENEVE_HWCSUM),
2326 	CAP2NV(GENEVE_HWTSO),
2327 	{0, NULL}
2328 };
2329 #undef CAPNV
2330 #undef CAP2NV
2331 
2332 int
2333 if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
2334     const struct ifcap_nv_bit_name *nn, bool all)
2335 {
2336 	int i, res;
2337 
2338 	res = 0;
2339 	for (i = 0; nn[i].cap_name != NULL; i++) {
2340 		if (nvlist_exists_bool(nv, nn[i].cap_name)) {
2341 			if (all || nvlist_get_bool(nv, nn[i].cap_name))
2342 				res |= nn[i].cap_bit;
2343 		} else {
2344 			res |= *old_cap & nn[i].cap_bit;
2345 		}
2346 	}
2347 	return (res);
2348 }
2349 
2350 void
2351 if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
2352     int ifr_cap, int ifr_req)
2353 {
2354 	int i;
2355 
2356 	for (i = 0; nn[i].cap_name != NULL; i++) {
2357 		if ((nn[i].cap_bit & ifr_cap) != 0) {
2358 			nvlist_add_bool(nv, nn[i].cap_name,
2359 			    (nn[i].cap_bit & ifr_req) != 0);
2360 		}
2361 	}
2362 }
2363 
2364 struct if_vf_status *
2365 if_vf_status_alloc(uint32_t num_vfs)
2366 {
2367 	struct if_vf_status *status;
2368 	size_t size;
2369 
2370 	KASSERT(num_vfs <= IFVF_MAX_VFS,
2371 	    ("invalid VF count %u", num_vfs));
2372 	size = sizeof(struct if_vf_status) +
2373 	    num_vfs * sizeof(struct if_vf_info);
2374 	status = malloc(size, M_IFNET, M_WAITOK | M_ZERO);
2375 	status->num_vfs = num_vfs;
2376 	return (status);
2377 }
2378 
2379 void
2380 if_vf_status_free(struct if_vf_status *status)
2381 {
2382 	struct if_vf_ext_field *field;
2383 	struct if_vf_extension *extension;
2384 	uint32_t i, j;
2385 
2386 	KASSERT(status != NULL, ("NULL VF status"));
2387 	for (i = 0; i < status->num_vfs; i++) {
2388 		for (j = 0; j < status->vfs[i].num_extensions; j++) {
2389 			extension = &status->vfs[i].extensions[j];
2390 			for (uint32_t k = 0; k < extension->num_fields; k++) {
2391 				field = &extension->fields[k];
2392 				if (field->type == IFVF_EXT_STRING)
2393 					free(field->value.string, M_IFNET);
2394 				else if (field->type == IFVF_EXT_BINARY)
2395 					free(field->value.binary.data, M_IFNET);
2396 			}
2397 			free(extension->fields, M_IFNET);
2398 		}
2399 		free(status->vfs[i].extensions, M_IFNET);
2400 	}
2401 
2402 	free(status, M_IFNET);
2403 }
2404 
2405 struct if_vf_extension *
2406 if_vf_status_add_extension(struct if_vf_info *vf, const char *name,
2407     uint32_t version, uint32_t num_fields)
2408 {
2409 	struct if_vf_extension *extensions, *extension;
2410 	uint32_t count;
2411 
2412 	KASSERT(vf != NULL, ("NULL VF information"));
2413 	KASSERT(name != NULL, ("VF extension without a name"));
2414 	KASSERT(vf->num_extensions < IFVF_MAX_EXTENSIONS,
2415 	    ("too many VF extensions"));
2416 	KASSERT(num_fields > 0 && num_fields <= IFVF_MAX_EXTENSION_FIELDS,
2417 	    ("invalid VF extension field count %u", num_fields));
2418 	count = vf->num_extensions + 1;
2419 	extensions = mallocarray(count, sizeof(*extensions), M_IFNET,
2420 	    M_WAITOK | M_ZERO);
2421 	if (vf->num_extensions != 0) {
2422 		memcpy(extensions, vf->extensions,
2423 		    vf->num_extensions * sizeof(*extensions));
2424 		free(vf->extensions, M_IFNET);
2425 	}
2426 	vf->extensions = extensions;
2427 	vf->num_extensions = count;
2428 	extension = &extensions[count - 1];
2429 	extension->name = name;
2430 	extension->version = version;
2431 	extension->num_fields = num_fields;
2432 	extension->fields = mallocarray(num_fields, sizeof(*extension->fields),
2433 	    M_IFNET, M_WAITOK | M_ZERO);
2434 	return (extension);
2435 }
2436 
2437 static struct if_vf_ext_field *
2438 if_vf_extension_field(struct if_vf_extension *extension, uint32_t index,
2439     const char *name, enum if_vf_ext_type type)
2440 {
2441 	struct if_vf_ext_field *field;
2442 
2443 	KASSERT(extension != NULL && index < extension->num_fields,
2444 	    ("invalid VF extension field"));
2445 	KASSERT(name != NULL, ("VF extension field without a name"));
2446 	field = &extension->fields[index];
2447 	KASSERT(field->type == 0, ("VF extension field initialized twice"));
2448 	field->name = name;
2449 	field->type = type;
2450 	return (field);
2451 }
2452 
2453 void
2454 if_vf_extension_set_bool(struct if_vf_extension *extension, uint32_t index,
2455     const char *name, bool value)
2456 {
2457 	struct if_vf_ext_field *field;
2458 
2459 	field = if_vf_extension_field(extension, index, name, IFVF_EXT_BOOL);
2460 	field->value.boolean = value;
2461 }
2462 
2463 void
2464 if_vf_extension_set_number(struct if_vf_extension *extension, uint32_t index,
2465     const char *name, uint64_t value)
2466 {
2467 	struct if_vf_ext_field *field;
2468 
2469 	field = if_vf_extension_field(extension, index, name, IFVF_EXT_NUMBER);
2470 	field->value.number = value;
2471 }
2472 
2473 void
2474 if_vf_extension_set_string(struct if_vf_extension *extension, uint32_t index,
2475     const char *name, const char *value)
2476 {
2477 	struct if_vf_ext_field *field;
2478 
2479 	KASSERT(value != NULL, ("NULL VF extension string"));
2480 	field = if_vf_extension_field(extension, index, name, IFVF_EXT_STRING);
2481 	field->value.string = strdup(value, M_IFNET);
2482 }
2483 
2484 void
2485 if_vf_extension_set_binary(struct if_vf_extension *extension, uint32_t index,
2486     const char *name, const void *value, uint32_t length)
2487 {
2488 	struct if_vf_ext_field *field;
2489 
2490 	KASSERT(value != NULL && length != 0, ("empty VF extension binary"));
2491 	field = if_vf_extension_field(extension, index, name, IFVF_EXT_BINARY);
2492 	field->value.binary.data = malloc(length, M_IFNET, M_WAITOK);
2493 	memcpy(field->value.binary.data, value, length);
2494 	field->value.binary.length = length;
2495 }
2496 
2497 int
2498 if_get_vf_status(if_t ifp, struct if_vf_status **statusp)
2499 {
2500 	struct if_vf_status *status;
2501 	int error;
2502 
2503 	KASSERT(statusp != NULL, ("NULL VF status output"));
2504 	if (ifp->if_vf_status == NULL)
2505 		return (EOPNOTSUPP);
2506 	status = NULL;
2507 	error = ifp->if_vf_status(ifp, &status);
2508 	KASSERT((error == 0) == (status != NULL),
2509 	    ("VF status provider returned error %d with status %p", error,
2510 	    status));
2511 	if (error != 0) {
2512 		if (status != NULL)
2513 			if_vf_status_free(status);
2514 		return (error);
2515 	}
2516 	if (status == NULL)
2517 		return (EBADMSG);
2518 	*statusp = status;
2519 	return (0);
2520 }
2521 
2522 /*
2523  * Hardware specific interface ioctls.
2524  */
2525 int
2526 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2527 {
2528 	struct ifreq *ifr;
2529 	int error = 0, do_ifup = 0;
2530 	int new_flags, temp_flags;
2531 	size_t descrlen, nvbuflen;
2532 	char *descrbuf;
2533 	char new_name[IFNAMSIZ];
2534 	void *buf;
2535 	nvlist_t *nvcap;
2536 	struct siocsifcapnv_driver_data drv_ioctl_data;
2537 
2538 	ifr = (struct ifreq *)data;
2539 	switch (cmd) {
2540 	case SIOCGIFINDEX:
2541 		ifr->ifr_index = ifp->if_index;
2542 		break;
2543 
2544 	case SIOCGIFFLAGS:
2545 		temp_flags = ifp->if_flags | ifp->if_drv_flags;
2546 		ifr->ifr_flags = temp_flags & 0xffff;
2547 		ifr->ifr_flagshigh = temp_flags >> 16;
2548 		break;
2549 
2550 	case SIOCGIFCAP:
2551 		ifr->ifr_reqcap = ifp->if_capabilities;
2552 		ifr->ifr_curcap = ifp->if_capenable;
2553 		break;
2554 
2555 	case SIOCGIFCAPNV:
2556 		if ((ifp->if_capabilities & IFCAP_NV) == 0) {
2557 			error = EINVAL;
2558 			break;
2559 		}
2560 		buf = NULL;
2561 		nvcap = nvlist_create(0);
2562 		for (;;) {
2563 			if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
2564 			    ifp->if_capabilities, ifp->if_capenable);
2565 			if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
2566 			    ifp->if_capabilities2, ifp->if_capenable2);
2567 			error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
2568 			    __DECONST(caddr_t, nvcap));
2569 			if (error != 0) {
2570 				if_printf(ifp,
2571 			    "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
2572 				    error);
2573 				break;
2574 			}
2575 			buf = nvlist_pack(nvcap, &nvbuflen);
2576 			if (buf == NULL) {
2577 				error = nvlist_error(nvcap);
2578 				if (error == 0)
2579 					error = EDOOFUS;
2580 				break;
2581 			}
2582 			if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
2583 				ifr->ifr_cap_nv.length = nvbuflen;
2584 				ifr->ifr_cap_nv.buffer = NULL;
2585 				error = EFBIG;
2586 				break;
2587 			}
2588 			ifr->ifr_cap_nv.length = nvbuflen;
2589 			error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
2590 			break;
2591 		}
2592 		free(buf, M_NVLIST);
2593 		nvlist_destroy(nvcap);
2594 		break;
2595 
2596 	case SIOCGIFDATA:
2597 	{
2598 		struct if_data ifd;
2599 
2600 		/* Ensure uninitialised padding is not leaked. */
2601 		memset(&ifd, 0, sizeof(ifd));
2602 
2603 		if_data_copy(ifp, &ifd);
2604 		error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2605 		break;
2606 	}
2607 
2608 #ifdef MAC
2609 	case SIOCGIFMAC:
2610 		error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2611 		break;
2612 #endif
2613 
2614 	case SIOCGIFMETRIC:
2615 		ifr->ifr_metric = ifp->if_metric;
2616 		break;
2617 
2618 	case SIOCGIFMTU:
2619 		ifr->ifr_mtu = ifp->if_mtu;
2620 		break;
2621 
2622 	case SIOCGIFPHYS:
2623 		/* XXXGL: did this ever worked? */
2624 		ifr->ifr_phys = 0;
2625 		break;
2626 
2627 	case SIOCGIFDESCR:
2628 		error = 0;
2629 		sx_slock(&ifdescr_sx);
2630 		if (ifp->if_description == NULL)
2631 			error = ENOMSG;
2632 		else {
2633 			/* space for terminating nul */
2634 			descrlen = strlen(ifp->if_description) + 1;
2635 			if (ifr_buffer_get_length(ifr) < descrlen)
2636 				ifr_buffer_set_buffer_null(ifr);
2637 			else
2638 				error = copyout(ifp->if_description,
2639 				    ifr_buffer_get_buffer(ifr), descrlen);
2640 			ifr_buffer_set_length(ifr, descrlen);
2641 		}
2642 		sx_sunlock(&ifdescr_sx);
2643 		break;
2644 
2645 	case SIOCSIFDESCR:
2646 		error = priv_check(td, PRIV_NET_SETIFDESCR);
2647 		if (error)
2648 			return (error);
2649 
2650 		/*
2651 		 * Copy only (length-1) bytes to make sure that
2652 		 * if_description is always nul terminated.  The
2653 		 * length parameter is supposed to count the
2654 		 * terminating nul in.
2655 		 */
2656 		if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2657 			return (ENAMETOOLONG);
2658 		else if (ifr_buffer_get_length(ifr) == 0)
2659 			descrbuf = NULL;
2660 		else {
2661 			descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
2662 			error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2663 			    ifr_buffer_get_length(ifr) - 1);
2664 			if (error) {
2665 				if_freedescr(descrbuf);
2666 				break;
2667 			}
2668 		}
2669 
2670 		if_setdescr(ifp, descrbuf);
2671 		getmicrotime(&ifp->if_lastchange);
2672 		break;
2673 
2674 	case SIOCGIFFIB:
2675 		ifr->ifr_fib = ifp->if_fib;
2676 		break;
2677 
2678 	case SIOCSIFFIB:
2679 		error = priv_check(td, PRIV_NET_SETIFFIB);
2680 		if (error)
2681 			return (error);
2682 		if (ifr->ifr_fib >= rt_numfibs)
2683 			return (EINVAL);
2684 
2685 		ifp->if_fib = ifr->ifr_fib;
2686 		break;
2687 
2688 	case SIOCSIFFLAGS:
2689 		error = priv_check(td, PRIV_NET_SETIFFLAGS);
2690 		if (error)
2691 			return (error);
2692 		/*
2693 		 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2694 		 * check, so we don't need special handling here yet.
2695 		 */
2696 		new_flags = (ifr->ifr_flags & 0xffff) |
2697 		    (ifr->ifr_flagshigh << 16);
2698 		if (ifp->if_flags & IFF_UP &&
2699 		    (new_flags & IFF_UP) == 0) {
2700 			if_down(ifp);
2701 		} else if (new_flags & IFF_UP &&
2702 		    (ifp->if_flags & IFF_UP) == 0) {
2703 			do_ifup = 1;
2704 		}
2705 
2706 		/*
2707 		 * See if the promiscuous mode or allmulti bits are about to
2708 		 * flip.  They require special handling because in-kernel
2709 		 * consumers may indepdently toggle them.
2710 		 */
2711 		if_setppromisc(ifp, new_flags & IFF_PPROMISC);
2712 		if ((ifp->if_flags ^ new_flags) & IFF_PALLMULTI) {
2713 			if (new_flags & IFF_PALLMULTI)
2714 				ifp->if_flags |= IFF_ALLMULTI;
2715 			else if (ifp->if_amcount == 0)
2716 				ifp->if_flags &= ~IFF_ALLMULTI;
2717 		}
2718 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2719 			(new_flags &~ IFF_CANTCHANGE);
2720 		if (ifp->if_ioctl) {
2721 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
2722 		}
2723 		if (do_ifup)
2724 			if_up(ifp);
2725 		getmicrotime(&ifp->if_lastchange);
2726 		break;
2727 
2728 	case SIOCSIFCAP:
2729 		error = priv_check(td, PRIV_NET_SETIFCAP);
2730 		if (error != 0)
2731 			return (error);
2732 		if (ifp->if_ioctl == NULL)
2733 			return (EOPNOTSUPP);
2734 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2735 			return (EINVAL);
2736 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2737 		if (error == 0)
2738 			getmicrotime(&ifp->if_lastchange);
2739 		break;
2740 
2741 	case SIOCSIFCAPNV:
2742 		error = priv_check(td, PRIV_NET_SETIFCAP);
2743 		if (error != 0)
2744 			return (error);
2745 		if (ifp->if_ioctl == NULL)
2746 			return (EOPNOTSUPP);
2747 		if ((ifp->if_capabilities & IFCAP_NV) == 0)
2748 			return (EINVAL);
2749 		if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
2750 			return (EINVAL);
2751 		nvcap = NULL;
2752 		buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
2753 		for (;;) {
2754 			error = copyin(ifr->ifr_cap_nv.buffer, buf,
2755 			    ifr->ifr_cap_nv.length);
2756 			if (error != 0)
2757 				break;
2758 			nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
2759 			if (nvcap == NULL) {
2760 				error = EINVAL;
2761 				break;
2762 			}
2763 			drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
2764 			    &ifp->if_capenable, ifcap_nv_bit_names, false);
2765 			if ((drv_ioctl_data.reqcap &
2766 			    ~ifp->if_capabilities) != 0) {
2767 				error = EINVAL;
2768 				break;
2769 			}
2770 			drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
2771 			    &ifp->if_capenable2, ifcap2_nv_bit_names, false);
2772 			if ((drv_ioctl_data.reqcap2 &
2773 			    ~ifp->if_capabilities2) != 0) {
2774 				error = EINVAL;
2775 				break;
2776 			}
2777 			drv_ioctl_data.nvcap = nvcap;
2778 			error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
2779 			    (caddr_t)&drv_ioctl_data);
2780 			break;
2781 		}
2782 		nvlist_destroy(nvcap);
2783 		free(buf, M_TEMP);
2784 		if (error == 0)
2785 			getmicrotime(&ifp->if_lastchange);
2786 		break;
2787 
2788 #ifdef MAC
2789 	case SIOCSIFMAC:
2790 		error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2791 		break;
2792 #endif
2793 
2794 	case SIOCSIFNAME:
2795 		error = priv_check(td, PRIV_NET_SETIFNAME);
2796 		if (error)
2797 			return (error);
2798 		error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2799 		    NULL);
2800 		if (error != 0)
2801 			return (error);
2802 		error = if_rename(ifp, new_name);
2803 		break;
2804 
2805 	case SIOCSIFMETRIC:
2806 		error = priv_check(td, PRIV_NET_SETIFMETRIC);
2807 		if (error)
2808 			return (error);
2809 		ifp->if_metric = ifr->ifr_metric;
2810 		getmicrotime(&ifp->if_lastchange);
2811 		break;
2812 
2813 	case SIOCSIFPHYS:
2814 		error = priv_check(td, PRIV_NET_SETIFPHYS);
2815 		if (error)
2816 			return (error);
2817 		if (ifp->if_ioctl == NULL)
2818 			return (EOPNOTSUPP);
2819 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2820 		if (error == 0)
2821 			getmicrotime(&ifp->if_lastchange);
2822 		break;
2823 
2824 	case SIOCSIFMTU:
2825 	{
2826 		u_long oldmtu = ifp->if_mtu;
2827 
2828 		error = priv_check(td, PRIV_NET_SETIFMTU);
2829 		if (error)
2830 			return (error);
2831 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2832 			return (EINVAL);
2833 		if (ifp->if_ioctl == NULL)
2834 			return (EOPNOTSUPP);
2835 		/* Disallow MTU changes on bridge member interfaces. */
2836 		if (ifp->if_bridge)
2837 			return (EOPNOTSUPP);
2838 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2839 		if (error == 0) {
2840 			getmicrotime(&ifp->if_lastchange);
2841 			rt_ifmsg(ifp, 0);
2842 #ifdef INET
2843 			DEBUGNET_NOTIFY_MTU(ifp);
2844 #endif
2845 		}
2846 		/*
2847 		 * If the link MTU changed, do network layer specific procedure.
2848 		 */
2849 		if (ifp->if_mtu != oldmtu)
2850 			if_notifymtu(ifp);
2851 		break;
2852 	}
2853 
2854 	case SIOCADDMULTI:
2855 	case SIOCDELMULTI:
2856 	{
2857 		struct sockaddr_dl sa_dl;
2858 
2859 		if (cmd == SIOCADDMULTI)
2860 			error = priv_check(td, PRIV_NET_ADDMULTI);
2861 		else
2862 			error = priv_check(td, PRIV_NET_DELMULTI);
2863 		if (error)
2864 			return (error);
2865 
2866 		/* Don't allow group membership on non-multicast interfaces. */
2867 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
2868 			return (EOPNOTSUPP);
2869 
2870 		/*
2871 		 * The sockaddr embedded in the ifreq is not large enough to
2872 		 * hold a full sockaddr_dl, but existing callers of these ioctls
2873 		 * will set sa_len = sizeof(struct sockaddr_dl) anyway.  Bounce
2874 		 * the sockaddr into a sockaddr_dl on the stack to avoid
2875 		 * potential out-of-bounds accesses.
2876 		 */
2877 		if (ifr->ifr_addr.sa_family != AF_LINK)
2878 			return (EINVAL);
2879 		memset(&sa_dl, 0, sizeof(sa_dl));
2880 		memcpy(&sa_dl, &ifr->ifr_addr,
2881 		    MIN(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
2882 		sa_dl.sdl_family = AF_LINK;
2883 		sa_dl.sdl_len = MIN(ifr->ifr_addr.sa_len, sizeof(sa_dl));
2884 		if (sa_dl.sdl_nlen + sa_dl.sdl_alen + sa_dl.sdl_slen >
2885 		    sizeof(ifr->ifr_addr) -
2886 		    offsetof(struct sockaddr_dl, sdl_data) ||
2887 		    sa_dl.sdl_nlen > IFNAMSIZ)
2888 			return (EINVAL);
2889 
2890 		if (cmd == SIOCADDMULTI) {
2891 			struct epoch_tracker et;
2892 			struct ifmultiaddr *ifma;
2893 
2894 			/*
2895 			 * Userland is only permitted to join groups once
2896 			 * via the if_addmulti() KPI, because it cannot hold
2897 			 * struct ifmultiaddr * between calls. It may also
2898 			 * lose a race while we check if the membership
2899 			 * already exists.
2900 			 */
2901 			NET_EPOCH_ENTER(et);
2902 			ifma = if_findmulti(ifp, (struct sockaddr *)&sa_dl);
2903 			NET_EPOCH_EXIT(et);
2904 			if (ifma != NULL)
2905 				error = EADDRINUSE;
2906 			else
2907 				error = if_addmulti(ifp,
2908 				    (struct sockaddr *)&sa_dl, &ifma);
2909 		} else {
2910 			error = if_delmulti(ifp, (struct sockaddr *)&sa_dl);
2911 		}
2912 		if (error == 0)
2913 			getmicrotime(&ifp->if_lastchange);
2914 		break;
2915 	}
2916 
2917 	case SIOCSIFPHYADDR:
2918 	case SIOCDIFPHYADDR:
2919 #ifdef INET6
2920 	case SIOCSIFPHYADDR_IN6:
2921 #endif
2922 	case SIOCSIFMEDIA:
2923 	case SIOCSIFGENERIC:
2924 		error = priv_check(td, PRIV_NET_HWIOCTL);
2925 		if (error)
2926 			return (error);
2927 		if (ifp->if_ioctl == NULL)
2928 			return (EOPNOTSUPP);
2929 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2930 		if (error == 0)
2931 			getmicrotime(&ifp->if_lastchange);
2932 		break;
2933 
2934 	case SIOCGIFSTATUS:
2935 	case SIOCGIFPSRCADDR:
2936 	case SIOCGIFPDSTADDR:
2937 	case SIOCGIFMEDIA:
2938 	case SIOCGIFXMEDIA:
2939 	case SIOCGIFGENERIC:
2940 	case SIOCGIFRSSKEY:
2941 	case SIOCGIFRSSHASH:
2942 	case SIOCGIFDOWNREASON:
2943 		if (ifp->if_ioctl == NULL)
2944 			return (EOPNOTSUPP);
2945 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2946 		break;
2947 
2948 	case SIOCSIFLLADDR:
2949 		error = priv_check(td, PRIV_NET_SETLLADDR);
2950 		if (error)
2951 			return (error);
2952 		error = if_setlladdr(ifp,
2953 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2954 		break;
2955 
2956 	case SIOCGHWADDR:
2957 		error = if_gethwaddr(ifp, ifr);
2958 		break;
2959 
2960 	case SIOCAIFGROUP:
2961 	{
2962 		const char *groupname;
2963 
2964 		error = priv_check(td, PRIV_NET_ADDIFGROUP);
2965 		if (error)
2966 			return (error);
2967 		groupname = ((struct ifgroupreq *)data)->ifgr_group;
2968 		if (strnlen(groupname, IFNAMSIZ) == IFNAMSIZ)
2969 			return (EINVAL);
2970 		error = if_addgroup(ifp, groupname);
2971 		if (error != 0)
2972 			return (error);
2973 		break;
2974 	}
2975 	case SIOCGIFGROUP:
2976 		error = if_getgroup((struct ifgroupreq *)data, ifp);
2977 		break;
2978 
2979 	case SIOCDIFGROUP:
2980 	{
2981 		const char *groupname;
2982 
2983 		error = priv_check(td, PRIV_NET_DELIFGROUP);
2984 		if (error)
2985 			return (error);
2986 		groupname = ((struct ifgroupreq *)data)->ifgr_group;
2987 		if (strnlen(groupname, IFNAMSIZ) == IFNAMSIZ)
2988 			return (EINVAL);
2989 		error = if_delgroup(ifp, groupname);
2990 		if (error != 0)
2991 			return (error);
2992 		break;
2993 	}
2994 	default:
2995 		error = ENOIOCTL;
2996 		break;
2997 	}
2998 	return (error);
2999 }
3000 
3001 /*
3002  * Interface ioctls.
3003  */
3004 int
3005 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
3006 {
3007 #ifdef COMPAT_FREEBSD32
3008 	union {
3009 		struct ifconf ifc;
3010 		struct ifdrv ifd;
3011 		struct ifgroupreq ifgr;
3012 		struct ifmediareq ifmr;
3013 	} thunk;
3014 	u_long saved_cmd;
3015 	struct ifconf32 *ifc32;
3016 	struct ifdrv32 *ifd32;
3017 	struct ifgroupreq32 *ifgr32;
3018 	struct ifmediareq32 *ifmr32;
3019 #endif
3020 	struct ifnet *ifp;
3021 	struct ifreq *ifr;
3022 	int error;
3023 	int oif_flags;
3024 #ifdef VIMAGE
3025 	bool shutdown;
3026 #endif
3027 
3028 	CURVNET_SET(so->so_vnet);
3029 #ifdef VIMAGE
3030 	/* Make sure the VNET is stable. */
3031 	shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
3032 	if (shutdown) {
3033 		CURVNET_RESTORE();
3034 		return (EBUSY);
3035 	}
3036 #endif
3037 
3038 #ifdef COMPAT_FREEBSD32
3039 	saved_cmd = cmd;
3040 	switch (cmd) {
3041 	case SIOCGIFCONF32:
3042 		ifc32 = (struct ifconf32 *)data;
3043 		thunk.ifc.ifc_len = ifc32->ifc_len;
3044 		thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
3045 		data = (caddr_t)&thunk.ifc;
3046 		cmd = SIOCGIFCONF;
3047 		break;
3048 	case SIOCGDRVSPEC32:
3049 	case SIOCSDRVSPEC32:
3050 		ifd32 = (struct ifdrv32 *)data;
3051 		memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
3052 		    sizeof(thunk.ifd.ifd_name));
3053 		thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
3054 		thunk.ifd.ifd_len = ifd32->ifd_len;
3055 		thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
3056 		data = (caddr_t)&thunk.ifd;
3057 		cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
3058 		break;
3059 	case SIOCAIFGROUP32:
3060 	case SIOCGIFGROUP32:
3061 	case SIOCDIFGROUP32:
3062 	case SIOCGIFGMEMB32:
3063 		ifgr32 = (struct ifgroupreq32 *)data;
3064 		memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
3065 		    sizeof(thunk.ifgr.ifgr_name));
3066 		thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
3067 		switch (cmd) {
3068 		case SIOCAIFGROUP32:
3069 		case SIOCDIFGROUP32:
3070 			memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
3071 			    sizeof(thunk.ifgr.ifgr_group));
3072 			break;
3073 		case SIOCGIFGROUP32:
3074 		case SIOCGIFGMEMB32:
3075 			thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
3076 			break;
3077 		}
3078 		data = (caddr_t)&thunk.ifgr;
3079 		cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
3080 		break;
3081 	case SIOCGIFMEDIA32:
3082 	case SIOCGIFXMEDIA32:
3083 		ifmr32 = (struct ifmediareq32 *)data;
3084 		memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
3085 		    sizeof(thunk.ifmr.ifm_name));
3086 		thunk.ifmr.ifm_current = ifmr32->ifm_current;
3087 		thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
3088 		thunk.ifmr.ifm_status = ifmr32->ifm_status;
3089 		thunk.ifmr.ifm_active = ifmr32->ifm_active;
3090 		thunk.ifmr.ifm_count = ifmr32->ifm_count;
3091 		thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
3092 		data = (caddr_t)&thunk.ifmr;
3093 		cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3094 		break;
3095 	}
3096 #endif
3097 
3098 	switch (cmd) {
3099 	case SIOCGIFCONF:
3100 		error = ifconf(cmd, data);
3101 		goto out_noref;
3102 	}
3103 
3104 	ifr = (struct ifreq *)data;
3105 	switch (cmd) {
3106 #ifdef VIMAGE
3107 	case SIOCSIFVNET:
3108 		error = priv_check(td, PRIV_NET_SETIFVNET);
3109 		if (error == 0)
3110 			error = if_vmove_loan(td, ifr->ifr_name, ifr->ifr_jid);
3111 		goto out_noref;
3112 
3113 	case SIOCSIFRVNET:
3114 		error = priv_check(td, PRIV_NET_SETIFVNET);
3115 		if (error == 0)
3116 			error = if_vmove_reclaim(td, ifr->ifr_name,
3117 			    ifr->ifr_jid);
3118 		goto out_noref;
3119 #endif
3120 	case SIOCIFCREATE:
3121 	case SIOCIFCREATE2:
3122 		error = priv_check(td, PRIV_NET_IFCREATE);
3123 		if (error == 0)
3124 			error = if_clone_create(ifr->ifr_name,
3125 			    sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3126 			    ifr_data_get_ptr(ifr) : NULL);
3127 		goto out_noref;
3128 	case SIOCIFDESTROY:
3129 		error = priv_check(td, PRIV_NET_IFDESTROY);
3130 
3131 		if (error == 0)
3132 			error = if_clone_destroy(ifr->ifr_name);
3133 		goto out_noref;
3134 
3135 	case SIOCIFGCLONERS:
3136 		error = if_clone_list((struct if_clonereq *)data);
3137 		goto out_noref;
3138 
3139 	case SIOCGIFGMEMB:
3140 	{
3141 		struct ifgroupreq *req;
3142 
3143 		req = (struct ifgroupreq *)data;
3144 		if (strnlen(req->ifgr_name, IFNAMSIZ) == IFNAMSIZ) {
3145 			error = EINVAL;
3146 			goto out_noref;
3147 		}
3148 		error = if_getgroupmembers(req);
3149 		goto out_noref;
3150 	}
3151 	}
3152 
3153 	ifp = ifunit_ref(ifr->ifr_name);
3154 	if (ifp == NULL) {
3155 		error = ENXIO;
3156 		goto out_noref;
3157 	}
3158 
3159 	error = ifhwioctl(cmd, ifp, data, td);
3160 	if (error != ENOIOCTL)
3161 		goto out_ref;
3162 
3163 	oif_flags = ifp->if_flags;
3164 	if (so->so_proto == NULL) {
3165 		error = EOPNOTSUPP;
3166 		goto out_ref;
3167 	}
3168 
3169 	/*
3170 	 * Pass the request on to the socket control method, and if the
3171 	 * latter returns EOPNOTSUPP, directly to the interface.
3172 	 *
3173 	 * Make an exception for the legacy SIOCSIF* requests.  Drivers
3174 	 * trust SIOCSIFADDR et al to come from an already privileged
3175 	 * layer, and do not perform any credentials checks or input
3176 	 * validation.
3177 	 */
3178 	error = so->so_proto->pr_control(so, cmd, data, ifp, td);
3179 	if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3180 	    cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3181 	    cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3182 		error = (*ifp->if_ioctl)(ifp, cmd, data);
3183 
3184 	if (!(oif_flags & IFF_UP) && (ifp->if_flags & IFF_UP))
3185 		if_up(ifp);
3186 out_ref:
3187 	if_rele(ifp);
3188 out_noref:
3189 	CURVNET_RESTORE();
3190 #ifdef COMPAT_FREEBSD32
3191 	if (error != 0)
3192 		return (error);
3193 	switch (saved_cmd) {
3194 	case SIOCGIFCONF32:
3195 		ifc32->ifc_len = thunk.ifc.ifc_len;
3196 		break;
3197 	case SIOCGDRVSPEC32:
3198 		/*
3199 		 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3200 		 * the struct so just assert that ifd_len (the only
3201 		 * field it might make sense to update) hasn't
3202 		 * changed.
3203 		 */
3204 		KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3205 		    ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3206 			thunk.ifd.ifd_len));
3207 		break;
3208 	case SIOCGIFGROUP32:
3209 	case SIOCGIFGMEMB32:
3210 		ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3211 		break;
3212 	case SIOCGIFMEDIA32:
3213 	case SIOCGIFXMEDIA32:
3214 		ifmr32->ifm_current = thunk.ifmr.ifm_current;
3215 		ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3216 		ifmr32->ifm_status = thunk.ifmr.ifm_status;
3217 		ifmr32->ifm_active = thunk.ifmr.ifm_active;
3218 		ifmr32->ifm_count = thunk.ifmr.ifm_count;
3219 		break;
3220 	}
3221 #endif
3222 	return (error);
3223 }
3224 
3225 int
3226 if_rename(struct ifnet *ifp, char *new_name)
3227 {
3228 	struct ifaddr *ifa;
3229 	struct sockaddr_dl *sdl;
3230 	size_t namelen, onamelen;
3231 	char old_name[IFNAMSIZ];
3232 	char strbuf[IFNAMSIZ + 8];
3233 
3234 	if (new_name[0] == '\0')
3235 		return (EINVAL);
3236 	if (strcmp(new_name, ifp->if_xname) == 0)
3237 		return (0);
3238 	if (ifunit(new_name) != NULL)
3239 		return (EEXIST);
3240 
3241 	if_printf(ifp, "changing name to '%s'\n", new_name);
3242 
3243 	IF_ADDR_WLOCK(ifp);
3244 	strlcpy(old_name, ifp->if_xname, sizeof(old_name));
3245 	strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
3246 	ifa = ifp->if_addr;
3247 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3248 	namelen = strlen(new_name);
3249 	onamelen = sdl->sdl_nlen;
3250 	/*
3251 	 * Move the address if needed.  This is safe because we
3252 	 * allocate space for a name of length IFNAMSIZ when we
3253 	 * create this in if_attach().
3254 	 */
3255 	if (namelen != onamelen) {
3256 		bcopy(sdl->sdl_data + onamelen,
3257 		    sdl->sdl_data + namelen, sdl->sdl_alen);
3258 	}
3259 	bcopy(new_name, sdl->sdl_data, namelen);
3260 	sdl->sdl_nlen = namelen;
3261 	sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
3262 	bzero(sdl->sdl_data, onamelen);
3263 	while (namelen != 0)
3264 		sdl->sdl_data[--namelen] = 0xff;
3265 	IF_ADDR_WUNLOCK(ifp);
3266 
3267 	EVENTHANDLER_INVOKE(ifnet_rename_event, ifp, old_name);
3268 
3269 	snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
3270 	devctl_notify("IFNET", old_name, "RENAME", strbuf);
3271 
3272 	return (0);
3273 }
3274 
3275 /*
3276  * The code common to handling reference counted flags,
3277  * e.g., in ifpromisc() and if_allmulti().
3278  * The "pflag" argument can specify a permanent mode flag to check,
3279  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3280  *
3281  * Only to be used on stack-owned flags, not driver-owned flags.
3282  */
3283 static int
3284 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3285 {
3286 	struct ifreq ifr;
3287 	int error;
3288 	int oldflags, oldcount;
3289 
3290 	/* Sanity checks to catch programming errors */
3291 	KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3292 	    ("%s: setting driver-owned flag %d", __func__, flag));
3293 
3294 	if (onswitch)
3295 		KASSERT(*refcount >= 0,
3296 		    ("%s: increment negative refcount %d for flag %d",
3297 		    __func__, *refcount, flag));
3298 	else
3299 		KASSERT(*refcount > 0,
3300 		    ("%s: decrement non-positive refcount %d for flag %d",
3301 		    __func__, *refcount, flag));
3302 
3303 	/* In case this mode is permanent, just touch refcount */
3304 	if (ifp->if_flags & pflag) {
3305 		*refcount += onswitch ? 1 : -1;
3306 		return (0);
3307 	}
3308 
3309 	/* Save ifnet parameters for if_ioctl() may fail */
3310 	oldcount = *refcount;
3311 	oldflags = ifp->if_flags;
3312 
3313 	/*
3314 	 * See if we aren't the only and touching refcount is enough.
3315 	 * Actually toggle interface flag if we are the first or last.
3316 	 */
3317 	if (onswitch) {
3318 		if ((*refcount)++)
3319 			return (0);
3320 		ifp->if_flags |= flag;
3321 	} else {
3322 		if (--(*refcount))
3323 			return (0);
3324 		ifp->if_flags &= ~flag;
3325 	}
3326 
3327 	/* Call down the driver since we've changed interface flags */
3328 	if (ifp->if_ioctl == NULL) {
3329 		error = EOPNOTSUPP;
3330 		goto recover;
3331 	}
3332 	ifr.ifr_flags = ifp->if_flags & 0xffff;
3333 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
3334 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3335 	if (error)
3336 		goto recover;
3337 	/* Notify userland that interface flags have changed */
3338 	rt_ifmsg(ifp, flag);
3339 	return (0);
3340 
3341 recover:
3342 	/* Recover after driver error */
3343 	*refcount = oldcount;
3344 	ifp->if_flags = oldflags;
3345 	return (error);
3346 }
3347 
3348 /*
3349  * Set/clear promiscuous mode on interface ifp based on the truth value
3350  * of pswitch.  The calls are reference counted so that only the first
3351  * "on" request actually has an effect, as does the final "off" request.
3352  * Results are undefined if the "off" and "on" requests are not matched.
3353  */
3354 int
3355 ifpromisc(struct ifnet *ifp, int pswitch)
3356 {
3357 	int error;
3358 	int oldflags = ifp->if_flags;
3359 
3360 	error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3361 			   &ifp->if_pcount, pswitch);
3362 	/* If promiscuous mode status has changed, log a message */
3363 	if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3364             log_promisc_mode_change)
3365 		if_printf(ifp, "promiscuous mode %s\n",
3366 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3367 	return (error);
3368 }
3369 
3370 /*
3371  * Return interface configuration
3372  * of system.  List may be used
3373  * in later ioctl's (above) to get
3374  * other information.
3375  */
3376 /*ARGSUSED*/
3377 static int
3378 ifconf(u_long cmd, caddr_t data)
3379 {
3380 	struct ifconf *ifc = (struct ifconf *)data;
3381 	struct ifnet *ifp;
3382 	struct ifaddr *ifa;
3383 	struct ifreq ifr;
3384 	struct sbuf *sb;
3385 	int error, full = 0, valid_len, max_len;
3386 
3387 	/* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3388 	max_len = maxphys - 1;
3389 
3390 	/* Prevent hostile input from being able to crash the system */
3391 	if (ifc->ifc_len <= 0)
3392 		return (EINVAL);
3393 
3394 again:
3395 	if (ifc->ifc_len <= max_len) {
3396 		max_len = ifc->ifc_len;
3397 		full = 1;
3398 	}
3399 	sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3400 	max_len = 0;
3401 	valid_len = 0;
3402 
3403 	IFNET_RLOCK();
3404 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3405 		struct epoch_tracker et;
3406 		int addrs;
3407 
3408 		/*
3409 		 * Zero the ifr to make sure we don't disclose the contents
3410 		 * of the stack.
3411 		 */
3412 		memset(&ifr, 0, sizeof(ifr));
3413 
3414 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3415 		    >= sizeof(ifr.ifr_name)) {
3416 			sbuf_delete(sb);
3417 			IFNET_RUNLOCK();
3418 			return (ENAMETOOLONG);
3419 		}
3420 
3421 		addrs = 0;
3422 		NET_EPOCH_ENTER(et);
3423 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3424 			struct sockaddr *sa = ifa->ifa_addr;
3425 
3426 			if (prison_if(curthread->td_ucred, sa) != 0)
3427 				continue;
3428 			addrs++;
3429 			if (sa->sa_len <= sizeof(*sa)) {
3430 				if (sa->sa_len < sizeof(*sa)) {
3431 					memset(&ifr.ifr_ifru.ifru_addr, 0,
3432 					    sizeof(ifr.ifr_ifru.ifru_addr));
3433 					memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3434 					    sa->sa_len);
3435 				} else
3436 					ifr.ifr_ifru.ifru_addr = *sa;
3437 				sbuf_bcat(sb, &ifr, sizeof(ifr));
3438 				max_len += sizeof(ifr);
3439 			} else {
3440 				sbuf_bcat(sb, &ifr,
3441 				    offsetof(struct ifreq, ifr_addr));
3442 				max_len += offsetof(struct ifreq, ifr_addr);
3443 				sbuf_bcat(sb, sa, sa->sa_len);
3444 				max_len += sa->sa_len;
3445 			}
3446 
3447 			if (sbuf_error(sb) == 0)
3448 				valid_len = sbuf_len(sb);
3449 		}
3450 		NET_EPOCH_EXIT(et);
3451 		if (addrs == 0) {
3452 			sbuf_bcat(sb, &ifr, sizeof(ifr));
3453 			max_len += sizeof(ifr);
3454 
3455 			if (sbuf_error(sb) == 0)
3456 				valid_len = sbuf_len(sb);
3457 		}
3458 	}
3459 	IFNET_RUNLOCK();
3460 
3461 	/*
3462 	 * If we didn't allocate enough space (uncommon), try again.  If
3463 	 * we have already allocated as much space as we are allowed,
3464 	 * return what we've got.
3465 	 */
3466 	if (valid_len != max_len && !full) {
3467 		sbuf_delete(sb);
3468 		goto again;
3469 	}
3470 
3471 	ifc->ifc_len = valid_len;
3472 	sbuf_finish(sb);
3473 	error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3474 	sbuf_delete(sb);
3475 	return (error);
3476 }
3477 
3478 /*
3479  * Just like ifpromisc(), but for all-multicast-reception mode.
3480  */
3481 int
3482 if_allmulti(struct ifnet *ifp, int onswitch)
3483 {
3484 
3485 	return (if_setflag(ifp, IFF_ALLMULTI, IFF_PALLMULTI, &ifp->if_amcount,
3486 	    onswitch));
3487 }
3488 
3489 struct ifmultiaddr *
3490 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3491 {
3492 	struct ifmultiaddr *ifma;
3493 
3494 	IF_ADDR_LOCK_ASSERT(ifp);
3495 
3496 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3497 		if (sa->sa_family == AF_LINK) {
3498 			if (sa_dl_equal(ifma->ifma_addr, sa))
3499 				break;
3500 		} else {
3501 			if (sa_equal(ifma->ifma_addr, sa))
3502 				break;
3503 		}
3504 	}
3505 
3506 	return ifma;
3507 }
3508 
3509 /*
3510  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
3511  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
3512  * the ifnet multicast address list here, so the caller must do that and
3513  * other setup work (such as notifying the device driver).  The reference
3514  * count is initialized to 1.
3515  */
3516 static struct ifmultiaddr *
3517 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3518     int mflags)
3519 {
3520 	struct ifmultiaddr *ifma;
3521 	struct sockaddr *dupsa;
3522 
3523 	ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3524 	    M_ZERO);
3525 	if (ifma == NULL)
3526 		return (NULL);
3527 
3528 	dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3529 	if (dupsa == NULL) {
3530 		free(ifma, M_IFMADDR);
3531 		return (NULL);
3532 	}
3533 	bcopy(sa, dupsa, sa->sa_len);
3534 	ifma->ifma_addr = dupsa;
3535 
3536 	ifma->ifma_ifp = ifp;
3537 	ifma->ifma_refcount = 1;
3538 	ifma->ifma_protospec = NULL;
3539 
3540 	if (llsa == NULL) {
3541 		ifma->ifma_lladdr = NULL;
3542 		return (ifma);
3543 	}
3544 
3545 	dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3546 	if (dupsa == NULL) {
3547 		free(ifma->ifma_addr, M_IFMADDR);
3548 		free(ifma, M_IFMADDR);
3549 		return (NULL);
3550 	}
3551 	bcopy(llsa, dupsa, llsa->sa_len);
3552 	ifma->ifma_lladdr = dupsa;
3553 
3554 	return (ifma);
3555 }
3556 
3557 /*
3558  * if_freemulti: free ifmultiaddr structure and possibly attached related
3559  * addresses.  The caller is responsible for implementing reference
3560  * counting, notifying the driver, handling routing messages, and releasing
3561  * any dependent link layer state.
3562  */
3563 #ifdef MCAST_VERBOSE
3564 extern void kdb_backtrace(void);
3565 #endif
3566 static void
3567 if_freemulti_internal(struct ifmultiaddr *ifma)
3568 {
3569 
3570 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3571 	    ifma->ifma_refcount));
3572 
3573 	if (ifma->ifma_lladdr != NULL)
3574 		free(ifma->ifma_lladdr, M_IFMADDR);
3575 #ifdef MCAST_VERBOSE
3576 	kdb_backtrace();
3577 	printf("%s freeing ifma: %p\n", __func__, ifma);
3578 #endif
3579 	free(ifma->ifma_addr, M_IFMADDR);
3580 	free(ifma, M_IFMADDR);
3581 }
3582 
3583 static void
3584 if_destroymulti(epoch_context_t ctx)
3585 {
3586 	struct ifmultiaddr *ifma;
3587 
3588 	ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3589 	if_freemulti_internal(ifma);
3590 }
3591 
3592 void
3593 if_freemulti(struct ifmultiaddr *ifma)
3594 {
3595 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3596 	    ifma->ifma_refcount));
3597 
3598 	NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3599 }
3600 
3601 /*
3602  * Register an additional multicast address with a network interface.
3603  *
3604  * - If the address is already present, bump the reference count on the
3605  *   address and return.
3606  * - If the address is not link-layer, look up a link layer address.
3607  * - Allocate address structures for one or both addresses, and attach to the
3608  *   multicast address list on the interface.  If automatically adding a link
3609  *   layer address, the protocol address will own a reference to the link
3610  *   layer address, to be freed when it is freed.
3611  * - Notify the network device driver of an addition to the multicast address
3612  *   list.
3613  *
3614  * 'sa' points to caller-owned memory with the desired multicast address.
3615  *
3616  * 'retifma' will be used to return a pointer to the resulting multicast
3617  * address reference, if desired.
3618  */
3619 int
3620 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3621     struct ifmultiaddr **retifma)
3622 {
3623 	struct ifmultiaddr *ifma, *ll_ifma;
3624 	struct sockaddr *llsa;
3625 	struct sockaddr_dl sdl;
3626 	int error;
3627 
3628 #ifdef INET
3629 	IN_MULTI_LIST_UNLOCK_ASSERT();
3630 #endif
3631 #ifdef INET6
3632 	IN6_MULTI_LIST_UNLOCK_ASSERT();
3633 #endif
3634 	/*
3635 	 * If the address is already present, return a new reference to it;
3636 	 * otherwise, allocate storage and set up a new address.
3637 	 */
3638 	IF_ADDR_WLOCK(ifp);
3639 	ifma = if_findmulti(ifp, sa);
3640 	if (ifma != NULL) {
3641 		ifma->ifma_refcount++;
3642 		if (retifma != NULL)
3643 			*retifma = ifma;
3644 		IF_ADDR_WUNLOCK(ifp);
3645 		return (0);
3646 	}
3647 
3648 	/*
3649 	 * The address isn't already present; resolve the protocol address
3650 	 * into a link layer address, and then look that up, bump its
3651 	 * refcount or allocate an ifma for that also.
3652 	 * Most link layer resolving functions returns address data which
3653 	 * fits inside default sockaddr_dl structure. However callback
3654 	 * can allocate another sockaddr structure, in that case we need to
3655 	 * free it later.
3656 	 */
3657 	llsa = NULL;
3658 	ll_ifma = NULL;
3659 	if (ifp->if_resolvemulti != NULL) {
3660 		/* Provide called function with buffer size information */
3661 		sdl.sdl_len = sizeof(sdl);
3662 		llsa = (struct sockaddr *)&sdl;
3663 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
3664 		if (error)
3665 			goto unlock_out;
3666 	}
3667 
3668 	/*
3669 	 * Allocate the new address.  Don't hook it up yet, as we may also
3670 	 * need to allocate a link layer multicast address.
3671 	 */
3672 	ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3673 	if (ifma == NULL) {
3674 		error = ENOMEM;
3675 		goto free_llsa_out;
3676 	}
3677 
3678 	/*
3679 	 * If a link layer address is found, we'll need to see if it's
3680 	 * already present in the address list, or allocate is as well.
3681 	 * When this block finishes, the link layer address will be on the
3682 	 * list.
3683 	 */
3684 	if (llsa != NULL) {
3685 		ll_ifma = if_findmulti(ifp, llsa);
3686 		if (ll_ifma == NULL) {
3687 			ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3688 			if (ll_ifma == NULL) {
3689 				--ifma->ifma_refcount;
3690 				if_freemulti(ifma);
3691 				error = ENOMEM;
3692 				goto free_llsa_out;
3693 			}
3694 			ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3695 			CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3696 			    ifma_link);
3697 		} else
3698 			ll_ifma->ifma_refcount++;
3699 		ifma->ifma_llifma = ll_ifma;
3700 	}
3701 
3702 	/*
3703 	 * We now have a new multicast address, ifma, and possibly a new or
3704 	 * referenced link layer address.  Add the primary address to the
3705 	 * ifnet address list.
3706 	 */
3707 	ifma->ifma_flags |= IFMA_F_ENQUEUED;
3708 	CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3709 
3710 	if (retifma != NULL)
3711 		*retifma = ifma;
3712 
3713 	/*
3714 	 * Must generate the message while holding the lock so that 'ifma'
3715 	 * pointer is still valid.
3716 	 */
3717 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3718 	IF_ADDR_WUNLOCK(ifp);
3719 
3720 	/*
3721 	 * We are certain we have added something, so call down to the
3722 	 * interface to let them know about it.
3723 	 */
3724 	if (ifp->if_ioctl != NULL) {
3725 		if (THREAD_CAN_SLEEP())
3726 			(void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3727 		else
3728 			taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3729 	}
3730 
3731 	if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3732 		link_free_sdl(llsa);
3733 
3734 	return (0);
3735 
3736 free_llsa_out:
3737 	if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3738 		link_free_sdl(llsa);
3739 
3740 unlock_out:
3741 	IF_ADDR_WUNLOCK(ifp);
3742 	return (error);
3743 }
3744 
3745 static void
3746 if_siocaddmulti(void *arg, int pending)
3747 {
3748 	struct ifnet *ifp;
3749 
3750 	ifp = arg;
3751 #ifdef DIAGNOSTIC
3752 	if (pending > 1)
3753 		if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3754 #endif
3755 	CURVNET_SET(ifp->if_vnet);
3756 	(void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3757 	CURVNET_RESTORE();
3758 }
3759 
3760 /*
3761  * Delete a multicast group membership by network-layer group address.
3762  *
3763  * Returns ENOENT if the entry could not be found. If ifp no longer
3764  * exists, results are undefined. This entry point should only be used
3765  * from subsystems which do appropriate locking to hold ifp for the
3766  * duration of the call.
3767  * Network-layer protocol domains must use if_delmulti_ifma().
3768  */
3769 int
3770 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3771 {
3772 	struct ifmultiaddr *ifma;
3773 	int lastref;
3774 
3775 	KASSERT(ifp, ("%s: NULL ifp", __func__));
3776 
3777 	IF_ADDR_WLOCK(ifp);
3778 	lastref = 0;
3779 	ifma = if_findmulti(ifp, sa);
3780 	if (ifma != NULL)
3781 		lastref = if_delmulti_locked(ifp, ifma, 0);
3782 	IF_ADDR_WUNLOCK(ifp);
3783 
3784 	if (ifma == NULL)
3785 		return (ENOENT);
3786 
3787 	if (lastref && ifp->if_ioctl != NULL) {
3788 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3789 	}
3790 
3791 	return (0);
3792 }
3793 
3794 /*
3795  * Delete all multicast group membership for an interface.
3796  * Should be used to quickly flush all multicast filters.
3797  */
3798 void
3799 if_delallmulti(struct ifnet *ifp)
3800 {
3801 	struct ifmultiaddr *ifma;
3802 	struct ifmultiaddr *next;
3803 
3804 	IF_ADDR_WLOCK(ifp);
3805 	CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3806 		if_delmulti_locked(ifp, ifma, 0);
3807 	IF_ADDR_WUNLOCK(ifp);
3808 }
3809 
3810 void
3811 if_delmulti_ifma(struct ifmultiaddr *ifma)
3812 {
3813 	if_delmulti_ifma_flags(ifma, 0);
3814 }
3815 
3816 /*
3817  * Delete a multicast group membership by group membership pointer.
3818  * Network-layer protocol domains must use this routine.
3819  *
3820  * It is safe to call this routine if the ifp disappeared.
3821  */
3822 void
3823 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3824 {
3825 	struct ifnet *ifp;
3826 	int lastref;
3827 	MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3828 #ifdef INET
3829 	IN_MULTI_LIST_UNLOCK_ASSERT();
3830 #endif
3831 	ifp = ifma->ifma_ifp;
3832 #ifdef DIAGNOSTIC
3833 	if (ifp == NULL) {
3834 		printf("%s: ifma_ifp seems to be detached\n", __func__);
3835 	} else {
3836 		struct epoch_tracker et;
3837 		struct ifnet *oifp;
3838 
3839 		NET_EPOCH_ENTER(et);
3840 		CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3841 			if (ifp == oifp)
3842 				break;
3843 		NET_EPOCH_EXIT(et);
3844 		if (ifp != oifp)
3845 			ifp = NULL;
3846 	}
3847 #endif
3848 	/*
3849 	 * If and only if the ifnet instance exists: Acquire the address lock.
3850 	 */
3851 	if (ifp != NULL)
3852 		IF_ADDR_WLOCK(ifp);
3853 
3854 	lastref = if_delmulti_locked(ifp, ifma, flags);
3855 
3856 	if (ifp != NULL) {
3857 		/*
3858 		 * If and only if the ifnet instance exists:
3859 		 *  Release the address lock.
3860 		 *  If the group was left: update the hardware hash filter.
3861 		 */
3862 		IF_ADDR_WUNLOCK(ifp);
3863 		if (lastref && ifp->if_ioctl != NULL) {
3864 			(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3865 		}
3866 	}
3867 }
3868 
3869 /*
3870  * Perform deletion of network-layer and/or link-layer multicast address.
3871  *
3872  * Return 0 if the reference count was decremented.
3873  * Return 1 if the final reference was released, indicating that the
3874  * hardware hash filter should be reprogrammed.
3875  */
3876 static int
3877 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3878 {
3879 	struct ifmultiaddr *ll_ifma;
3880 
3881 	if (ifp != NULL && ifma->ifma_ifp != NULL) {
3882 		KASSERT(ifma->ifma_ifp == ifp,
3883 		    ("%s: inconsistent ifp %p", __func__, ifp));
3884 		IF_ADDR_WLOCK_ASSERT(ifp);
3885 	}
3886 
3887 	ifp = ifma->ifma_ifp;
3888 	MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3889 
3890 	/*
3891 	 * If the ifnet is detaching, null out references to ifnet,
3892 	 * so that upper protocol layers will notice, and not attempt
3893 	 * to obtain locks for an ifnet which no longer exists. The
3894 	 * routing socket announcement must happen before the ifnet
3895 	 * instance is detached from the system.
3896 	 */
3897 	if (detaching) {
3898 #ifdef DIAGNOSTIC
3899 		printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3900 #endif
3901 		/*
3902 		 * ifp may already be nulled out if we are being reentered
3903 		 * to delete the ll_ifma.
3904 		 */
3905 		if (ifp != NULL) {
3906 			rt_newmaddrmsg(RTM_DELMADDR, ifma);
3907 			ifma->ifma_ifp = NULL;
3908 		}
3909 	}
3910 
3911 	if (--ifma->ifma_refcount > 0)
3912 		return 0;
3913 
3914 	if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3915 		CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3916 		ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3917 	}
3918 	/*
3919 	 * If this ifma is a network-layer ifma, a link-layer ifma may
3920 	 * have been associated with it. Release it first if so.
3921 	 */
3922 	ll_ifma = ifma->ifma_llifma;
3923 	if (ll_ifma != NULL) {
3924 		KASSERT(ifma->ifma_lladdr != NULL,
3925 		    ("%s: llifma w/o lladdr", __func__));
3926 		if (detaching)
3927 			ll_ifma->ifma_ifp = NULL;	/* XXX */
3928 		if (--ll_ifma->ifma_refcount == 0) {
3929 			if (ifp != NULL) {
3930 				if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3931 					CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3932 						ifma_link);
3933 					ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3934 				}
3935 			}
3936 			if_freemulti(ll_ifma);
3937 		}
3938 	}
3939 #ifdef INVARIANTS
3940 	if (ifp) {
3941 		struct ifmultiaddr *ifmatmp;
3942 
3943 		CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3944 			MPASS(ifma != ifmatmp);
3945 	}
3946 #endif
3947 	if_freemulti(ifma);
3948 	/*
3949 	 * The last reference to this instance of struct ifmultiaddr
3950 	 * was released; the hardware should be notified of this change.
3951 	 */
3952 	return 1;
3953 }
3954 
3955 /*
3956  * Set the link layer address on an interface.
3957  *
3958  * At this time we only support certain types of interfaces,
3959  * and we don't allow the length of the address to change.
3960  *
3961  * Set noinline to be dtrace-friendly
3962  */
3963 __noinline int
3964 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3965 {
3966 	struct sockaddr_dl *sdl;
3967 	struct ifaddr *ifa;
3968 	struct ifreq ifr;
3969 
3970 	ifa = ifp->if_addr;
3971 	if (ifa == NULL)
3972 		return (EINVAL);
3973 
3974 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3975 	if (sdl == NULL)
3976 		return (EINVAL);
3977 
3978 	if (len != sdl->sdl_alen)	/* don't allow length to change */
3979 		return (EINVAL);
3980 
3981 	switch (ifp->if_type) {
3982 	case IFT_ETHER:
3983 	case IFT_XETHER:
3984 	case IFT_L2VLAN:
3985 	case IFT_BRIDGE:
3986 	case IFT_IEEE8023ADLAG:
3987 		bcopy(lladdr, LLADDR(sdl), len);
3988 		break;
3989 	default:
3990 		return (ENODEV);
3991 	}
3992 
3993 	/*
3994 	 * If the interface is already up, we need
3995 	 * to re-init it in order to reprogram its
3996 	 * address filter.
3997 	 */
3998 	if ((ifp->if_flags & IFF_UP) != 0) {
3999 		if (ifp->if_ioctl) {
4000 			ifp->if_flags &= ~IFF_UP;
4001 			ifr.ifr_flags = ifp->if_flags & 0xffff;
4002 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
4003 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
4004 			ifp->if_flags |= IFF_UP;
4005 			ifr.ifr_flags = ifp->if_flags & 0xffff;
4006 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
4007 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
4008 		}
4009 	}
4010 	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
4011 
4012 	return (0);
4013 }
4014 
4015 /*
4016  * Compat function for handling basic encapsulation requests.
4017  * Not converted stacks (FDDI, IB, ..) supports traditional
4018  * output model: ARP (and other similar L2 protocols) are handled
4019  * inside output routine, arpresolve/nd6_resolve() returns MAC
4020  * address instead of full prepend.
4021  *
4022  * This function creates calculated header==MAC for IPv4/IPv6 and
4023  * returns EAFNOSUPPORT (which is then handled in ARP code) for other
4024  * address families.
4025  */
4026 static int
4027 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
4028 {
4029 	if (req->rtype != IFENCAP_LL)
4030 		return (EOPNOTSUPP);
4031 
4032 	if (req->bufsize < req->lladdr_len)
4033 		return (ENOMEM);
4034 
4035 	switch (req->family) {
4036 	case AF_INET:
4037 	case AF_INET6:
4038 		break;
4039 	default:
4040 		return (EAFNOSUPPORT);
4041 	}
4042 
4043 	/* Copy lladdr to storage as is */
4044 	memmove(req->buf, req->lladdr, req->lladdr_len);
4045 	req->bufsize = req->lladdr_len;
4046 	req->lladdr_off = 0;
4047 
4048 	return (0);
4049 }
4050 
4051 /*
4052  * Tunnel interfaces can nest, also they may cause infinite recursion
4053  * calls when misconfigured. We'll prevent this by detecting loops.
4054  * High nesting level may cause stack exhaustion. We'll prevent this
4055  * by introducing upper limit.
4056  *
4057  * Return 0, if tunnel nesting count is equal or less than limit.
4058  */
4059 int
4060 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
4061     int limit)
4062 {
4063 	struct m_tag *mtag;
4064 	int count;
4065 
4066 	count = 1;
4067 	mtag = NULL;
4068 	while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
4069 		if (*(struct ifnet **)(mtag + 1) == ifp) {
4070 			log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
4071 			return (EIO);
4072 		}
4073 		count++;
4074 	}
4075 	if (count > limit) {
4076 		log(LOG_NOTICE,
4077 		    "%s: if_output recursively called too many times(%d)\n",
4078 		    if_name(ifp), count);
4079 		return (EIO);
4080 	}
4081 	mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
4082 	if (mtag == NULL)
4083 		return (ENOMEM);
4084 	*(struct ifnet **)(mtag + 1) = ifp;
4085 	m_tag_prepend(m, mtag);
4086 	return (0);
4087 }
4088 
4089 /*
4090  * Get the link layer address that was read from the hardware at attach.
4091  *
4092  * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
4093  * their component interfaces as IFT_IEEE8023ADLAG.
4094  */
4095 int
4096 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
4097 {
4098 	if (ifp->if_hw_addr == NULL)
4099 		return (ENODEV);
4100 
4101 	switch (ifp->if_type) {
4102 	case IFT_ETHER:
4103 	case IFT_IEEE8023ADLAG:
4104 		bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
4105 		return (0);
4106 	default:
4107 		return (ENODEV);
4108 	}
4109 }
4110 
4111 /*
4112  * The name argument must be a pointer to storage which will last as
4113  * long as the interface does.  For physical devices, the result of
4114  * device_get_name(dev) is a good choice and for pseudo-devices a
4115  * static string works well.
4116  */
4117 void
4118 if_initname(struct ifnet *ifp, const char *name, int unit)
4119 {
4120 	ifp->if_dname = name;
4121 	ifp->if_dunit = unit;
4122 	if (unit != IF_DUNIT_NONE)
4123 		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4124 	else
4125 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
4126 }
4127 
4128 static int
4129 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
4130 {
4131 	char if_fmt[256];
4132 
4133 	snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4134 	vlog(pri, if_fmt, ap);
4135 	return (0);
4136 }
4137 
4138 
4139 int
4140 if_printf(struct ifnet *ifp, const char *fmt, ...)
4141 {
4142 	va_list ap;
4143 
4144 	va_start(ap, fmt);
4145 	if_vlog(ifp, LOG_INFO, fmt, ap);
4146 	va_end(ap);
4147 	return (0);
4148 }
4149 
4150 int
4151 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
4152 {
4153 	va_list ap;
4154 
4155 	va_start(ap, fmt);
4156 	if_vlog(ifp, pri, fmt, ap);
4157 	va_end(ap);
4158 	return (0);
4159 }
4160 
4161 void
4162 if_start(struct ifnet *ifp)
4163 {
4164 
4165 	(*(ifp)->if_start)(ifp);
4166 }
4167 
4168 /*
4169  * Backwards compatibility interface for drivers
4170  * that have not implemented it
4171  */
4172 static int
4173 if_transmit_default(struct ifnet *ifp, struct mbuf *m)
4174 {
4175 	int error;
4176 
4177 	IFQ_HANDOFF(ifp, m, error);
4178 	return (error);
4179 }
4180 
4181 static void
4182 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4183 {
4184 	m_freem(m);
4185 }
4186 
4187 int
4188 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4189 {
4190 	int active = 0;
4191 
4192 	IF_LOCK(ifq);
4193 	if (_IF_QFULL(ifq)) {
4194 		IF_UNLOCK(ifq);
4195 		if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4196 		m_freem(m);
4197 		return (0);
4198 	}
4199 	if (ifp != NULL) {
4200 		if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4201 		if (m->m_flags & (M_BCAST|M_MCAST))
4202 			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4203 		active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4204 	}
4205 	_IF_ENQUEUE(ifq, m);
4206 	IF_UNLOCK(ifq);
4207 	if (ifp != NULL && !active)
4208 		(*(ifp)->if_start)(ifp);
4209 	return (1);
4210 }
4211 
4212 void
4213 if_register_com_alloc(u_char type,
4214     if_com_alloc_t *a, if_com_free_t *f)
4215 {
4216 
4217 	KASSERT(if_com_alloc[type] == NULL,
4218 	    ("if_register_com_alloc: %d already registered", type));
4219 	KASSERT(if_com_free[type] == NULL,
4220 	    ("if_register_com_alloc: %d free already registered", type));
4221 
4222 	if_com_alloc[type] = a;
4223 	if_com_free[type] = f;
4224 }
4225 
4226 void
4227 if_deregister_com_alloc(u_char type)
4228 {
4229 
4230 	KASSERT(if_com_alloc[type] != NULL,
4231 	    ("if_deregister_com_alloc: %d not registered", type));
4232 	KASSERT(if_com_free[type] != NULL,
4233 	    ("if_deregister_com_alloc: %d free not registered", type));
4234 
4235 	/*
4236 	 * Ensure all pending EPOCH(9) callbacks have been executed. This
4237 	 * fixes issues about late invocation of if_destroy(), which leads
4238 	 * to memory leak from if_com_alloc[type] allocated if_l2com.
4239 	 */
4240 	NET_EPOCH_DRAIN_CALLBACKS();
4241 
4242 	if_com_alloc[type] = NULL;
4243 	if_com_free[type] = NULL;
4244 }
4245 
4246 /* API for driver access to network stack owned ifnet.*/
4247 uint64_t
4248 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4249 {
4250 	uint64_t oldbrate;
4251 
4252 	oldbrate = ifp->if_baudrate;
4253 	ifp->if_baudrate = baudrate;
4254 	return (oldbrate);
4255 }
4256 
4257 uint64_t
4258 if_getbaudrate(const if_t ifp)
4259 {
4260 	return (ifp->if_baudrate);
4261 }
4262 
4263 int
4264 if_setcapabilities(if_t ifp, int capabilities)
4265 {
4266 	ifp->if_capabilities = capabilities;
4267 	return (0);
4268 }
4269 
4270 int
4271 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4272 {
4273 	ifp->if_capabilities &= ~clearbit;
4274 	ifp->if_capabilities |= setbit;
4275 	return (0);
4276 }
4277 
4278 int
4279 if_getcapabilities(const if_t ifp)
4280 {
4281 	return (ifp->if_capabilities);
4282 }
4283 
4284 int
4285 if_setcapenable(if_t ifp, int capabilities)
4286 {
4287 	ifp->if_capenable = capabilities;
4288 	return (0);
4289 }
4290 
4291 int
4292 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4293 {
4294 	ifp->if_capenable &= ~clearcap;
4295 	ifp->if_capenable |= setcap;
4296 	return (0);
4297 }
4298 
4299 int
4300 if_setcapabilities2(if_t ifp, int capabilities)
4301 {
4302 	ifp->if_capabilities2 = capabilities;
4303 	return (0);
4304 }
4305 
4306 int
4307 if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
4308 {
4309 	ifp->if_capabilities2 &= ~clearbit;
4310 	ifp->if_capabilities2 |= setbit;
4311 	return (0);
4312 }
4313 
4314 int
4315 if_getcapabilities2(const if_t ifp)
4316 {
4317 	return (ifp->if_capabilities2);
4318 }
4319 
4320 int
4321 if_setcapenable2(if_t ifp, int capabilities2)
4322 {
4323 	ifp->if_capenable2 = capabilities2;
4324 	return (0);
4325 }
4326 
4327 int
4328 if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
4329 {
4330 	ifp->if_capenable2 &= ~clearcap;
4331 	ifp->if_capenable2 |= setcap;
4332 	return (0);
4333 }
4334 
4335 const char *
4336 if_getdname(const if_t ifp)
4337 {
4338 	return (ifp->if_dname);
4339 }
4340 
4341 void
4342 if_setdname(if_t ifp, const char *dname)
4343 {
4344 	ifp->if_dname = dname;
4345 }
4346 
4347 const char *
4348 if_name(if_t ifp)
4349 {
4350 	return (ifp->if_xname);
4351 }
4352 
4353 int
4354 if_setname(if_t ifp, const char *name)
4355 {
4356 	if (strlen(name) > sizeof(ifp->if_xname) - 1)
4357 		return (ENAMETOOLONG);
4358 	strcpy(ifp->if_xname, name);
4359 
4360 	return (0);
4361 }
4362 
4363 int
4364 if_togglecapenable(if_t ifp, int togglecap)
4365 {
4366 	ifp->if_capenable ^= togglecap;
4367 	return (0);
4368 }
4369 
4370 int
4371 if_getcapenable(const if_t ifp)
4372 {
4373 	return (ifp->if_capenable);
4374 }
4375 
4376 int
4377 if_togglecapenable2(if_t ifp, int togglecap)
4378 {
4379 	ifp->if_capenable2 ^= togglecap;
4380 	return (0);
4381 }
4382 
4383 int
4384 if_getcapenable2(const if_t ifp)
4385 {
4386 	return (ifp->if_capenable2);
4387 }
4388 
4389 int
4390 if_getdunit(const if_t ifp)
4391 {
4392 	return (ifp->if_dunit);
4393 }
4394 
4395 int
4396 if_getindex(const if_t ifp)
4397 {
4398 	return (ifp->if_index);
4399 }
4400 
4401 int
4402 if_getidxgen(const if_t ifp)
4403 {
4404 	return (ifp->if_idxgen);
4405 }
4406 
4407 const char *
4408 if_getdescr(if_t ifp)
4409 {
4410 	return (ifp->if_description);
4411 }
4412 
4413 void
4414 if_setdescr(if_t ifp, char *descrbuf)
4415 {
4416 	sx_xlock(&ifdescr_sx);
4417 	char *odescrbuf = ifp->if_description;
4418 	ifp->if_description = descrbuf;
4419 	sx_xunlock(&ifdescr_sx);
4420 
4421 	if_freedescr(odescrbuf);
4422 }
4423 
4424 char *
4425 if_allocdescr(size_t sz, int malloc_flag)
4426 {
4427 	malloc_flag &= (M_WAITOK | M_NOWAIT);
4428 	return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4429 }
4430 
4431 void
4432 if_freedescr(char *descrbuf)
4433 {
4434 	free(descrbuf, M_IFDESCR);
4435 }
4436 
4437 int
4438 if_getalloctype(const if_t ifp)
4439 {
4440 	return (ifp->if_alloctype);
4441 }
4442 
4443 void
4444 if_setlastchange(if_t ifp)
4445 {
4446 	getmicrotime(&ifp->if_lastchange);
4447 }
4448 
4449 /*
4450  * This is largely undesirable because it ties ifnet to a device, but does
4451  * provide flexiblity for an embedded product vendor. Should be used with
4452  * the understanding that it violates the interface boundaries, and should be
4453  * a last resort only.
4454  */
4455 int
4456 if_setdev(if_t ifp, void *dev)
4457 {
4458 	return (0);
4459 }
4460 
4461 int
4462 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4463 {
4464 	ifp->if_drv_flags &= ~clear_flags;
4465 	ifp->if_drv_flags |= set_flags;
4466 
4467 	return (0);
4468 }
4469 
4470 int
4471 if_getdrvflags(const if_t ifp)
4472 {
4473 	return (ifp->if_drv_flags);
4474 }
4475 
4476 int
4477 if_setdrvflags(if_t ifp, int flags)
4478 {
4479 	ifp->if_drv_flags = flags;
4480 	return (0);
4481 }
4482 
4483 int
4484 if_setflags(if_t ifp, int flags)
4485 {
4486 	ifp->if_flags = flags;
4487 	return (0);
4488 }
4489 
4490 int
4491 if_setflagbits(if_t ifp, int set, int clear)
4492 {
4493 	ifp->if_flags &= ~clear;
4494 	ifp->if_flags |= set;
4495 	return (0);
4496 }
4497 
4498 int
4499 if_getflags(const if_t ifp)
4500 {
4501 	return (ifp->if_flags);
4502 }
4503 
4504 int
4505 if_clearhwassist(if_t ifp)
4506 {
4507 	ifp->if_hwassist = 0;
4508 	return (0);
4509 }
4510 
4511 int
4512 if_sethwassistbits(if_t ifp, int toset, int toclear)
4513 {
4514 	ifp->if_hwassist &= ~toclear;
4515 	ifp->if_hwassist |= toset;
4516 
4517 	return (0);
4518 }
4519 
4520 int
4521 if_sethwassist(if_t ifp, int hwassist_bit)
4522 {
4523 	ifp->if_hwassist = hwassist_bit;
4524 	return (0);
4525 }
4526 
4527 int
4528 if_gethwassist(const if_t ifp)
4529 {
4530 	return (ifp->if_hwassist);
4531 }
4532 
4533 int
4534 if_togglehwassist(if_t ifp, int toggle_bits)
4535 {
4536 	ifp->if_hwassist ^= toggle_bits;
4537 	return (0);
4538 }
4539 
4540 int
4541 if_setmtu(if_t ifp, int mtu)
4542 {
4543 	ifp->if_mtu = mtu;
4544 	return (0);
4545 }
4546 
4547 void
4548 if_notifymtu(if_t ifp)
4549 {
4550 #ifdef INET6
4551 	nd6_setmtu(ifp);
4552 #endif
4553 	rt_updatemtu(ifp);
4554 }
4555 
4556 int
4557 if_getmtu(const if_t ifp)
4558 {
4559 	return (ifp->if_mtu);
4560 }
4561 
4562 void
4563 if_setppromisc(if_t ifp, bool ppromisc)
4564 {
4565 	int new_flags;
4566 
4567 	if (ppromisc)
4568 		new_flags = ifp->if_flags | IFF_PPROMISC;
4569 	else
4570 		new_flags = ifp->if_flags & ~IFF_PPROMISC;
4571 	if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
4572 		if (new_flags & IFF_PPROMISC)
4573 			new_flags |= IFF_PROMISC;
4574 		/*
4575 		 * Only unset IFF_PROMISC if there are no more consumers of
4576 		 * promiscuity, i.e. the ifp->if_pcount refcount is 0.
4577 		 */
4578 		else if (ifp->if_pcount == 0)
4579 			new_flags &= ~IFF_PROMISC;
4580 		if (log_promisc_mode_change)
4581                         if_printf(ifp, "permanently promiscuous mode %s\n",
4582                             ((new_flags & IFF_PPROMISC) ?
4583                              "enabled" : "disabled"));
4584 	}
4585 	ifp->if_flags = new_flags;
4586 }
4587 
4588 /*
4589  * Methods for drivers to access interface unicast and multicast
4590  * link level addresses.  Driver shall not know 'struct ifaddr' neither
4591  * 'struct ifmultiaddr'.
4592  */
4593 u_int
4594 if_lladdr_count(if_t ifp)
4595 {
4596 	struct epoch_tracker et;
4597 	struct ifaddr *ifa;
4598 	u_int count;
4599 
4600 	count = 0;
4601 	NET_EPOCH_ENTER(et);
4602 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4603 		if (ifa->ifa_addr->sa_family == AF_LINK)
4604 			count++;
4605 	NET_EPOCH_EXIT(et);
4606 
4607 	return (count);
4608 }
4609 
4610 int
4611 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4612 {
4613 	if_t ifp;
4614 	int error;
4615 
4616 	NET_EPOCH_ASSERT();
4617 	MPASS(cb);
4618 
4619 	error = 0;
4620 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4621 		error = cb(ifp, cb_arg);
4622 		if (error != 0)
4623 			break;
4624 	}
4625 
4626 	return (error);
4627 }
4628 
4629 /*
4630  * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4631  * Stops iteration if @cb returns non-zero error code.
4632  * Returns the last error code from @cb.
4633  * @match_cb: optional match callback limiting the iteration to only matched interfaces
4634  * @match_arg: argument to pass to @match_cb
4635  * @cb: iteration callback
4636  * @cb_arg: argument to pass to @cb
4637  */
4638 int
4639 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4640     void *cb_arg)
4641 {
4642 	int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4643 	struct ifnet **match_array = NULL;
4644 	int error = 0;
4645 
4646 	MPASS(cb);
4647 
4648 	while (true) {
4649 		struct ifnet **new_array;
4650 		int new_size = array_size;
4651 		struct epoch_tracker et;
4652 		struct ifnet *ifp;
4653 
4654 		while (new_size < match_count)
4655 			new_size *= 2;
4656 		new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4657 		if (match_array != NULL)
4658 			memcpy(new_array, match_array, array_size * sizeof(void *));
4659 		free(match_array, M_TEMP);
4660 		match_array = new_array;
4661 		array_size = new_size;
4662 
4663 		match_count = 0;
4664 		NET_EPOCH_ENTER(et);
4665 		CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4666 			if (match_cb != NULL && !match_cb(ifp, match_arg))
4667 				continue;
4668 			if (match_count < array_size) {
4669 				if (if_try_ref(ifp))
4670 					match_array[match_count++] = ifp;
4671 			} else
4672 				match_count++;
4673 		}
4674 		NET_EPOCH_EXIT(et);
4675 
4676 		if (match_count > array_size) {
4677 			for (int i = 0; i < array_size; i++)
4678 				if_rele(match_array[i]);
4679 			continue;
4680 		} else {
4681 			for (int i = 0; i < match_count; i++) {
4682 				if (error == 0)
4683 					error = cb(match_array[i], cb_arg);
4684 				if_rele(match_array[i]);
4685 			}
4686 			free(match_array, M_TEMP);
4687 			break;
4688 		}
4689 	}
4690 
4691 	return (error);
4692 }
4693 
4694 
4695 /*
4696  * Uses just 1 pointer of the 4 available in the public struct.
4697  */
4698 if_t
4699 if_iter_start(struct if_iter *iter)
4700 {
4701 	if_t ifp;
4702 
4703 	NET_EPOCH_ASSERT();
4704 
4705 	bzero(iter, sizeof(*iter));
4706 	ifp = CK_STAILQ_FIRST(&V_ifnet);
4707 	if (ifp != NULL)
4708 		iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4709 	else
4710 		iter->context[0] = NULL;
4711 	return (ifp);
4712 }
4713 
4714 if_t
4715 if_iter_next(struct if_iter *iter)
4716 {
4717 	if_t cur_ifp = iter->context[0];
4718 
4719 	if (cur_ifp != NULL)
4720 		iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4721 	return (cur_ifp);
4722 }
4723 
4724 void
4725 if_iter_finish(struct if_iter *iter)
4726 {
4727 	/* Nothing to do here for now. */
4728 }
4729 
4730 u_int
4731 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4732 {
4733 	struct epoch_tracker et;
4734 	struct ifaddr *ifa;
4735 	u_int count;
4736 
4737 	MPASS(cb);
4738 
4739 	count = 0;
4740 	NET_EPOCH_ENTER(et);
4741 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4742 		if (ifa->ifa_addr->sa_family != AF_LINK)
4743 			continue;
4744 		count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4745 		    count);
4746 	}
4747 	NET_EPOCH_EXIT(et);
4748 
4749 	return (count);
4750 }
4751 
4752 u_int
4753 if_llmaddr_count(if_t ifp)
4754 {
4755 	struct epoch_tracker et;
4756 	struct ifmultiaddr *ifma;
4757 	int count;
4758 
4759 	count = 0;
4760 	NET_EPOCH_ENTER(et);
4761 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4762 		if (ifma->ifma_addr->sa_family == AF_LINK)
4763 			count++;
4764 	NET_EPOCH_EXIT(et);
4765 
4766 	return (count);
4767 }
4768 
4769 bool
4770 if_maddr_empty(if_t ifp)
4771 {
4772 
4773 	return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4774 }
4775 
4776 u_int
4777 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4778 {
4779 	struct epoch_tracker et;
4780 	struct ifmultiaddr *ifma;
4781 	u_int count;
4782 
4783 	MPASS(cb);
4784 
4785 	count = 0;
4786 	NET_EPOCH_ENTER(et);
4787 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4788 		if (ifma->ifma_addr->sa_family != AF_LINK)
4789 			continue;
4790 		count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4791 		    count);
4792 	}
4793 	NET_EPOCH_EXIT(et);
4794 
4795 	return (count);
4796 }
4797 
4798 u_int
4799 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4800 {
4801 	struct epoch_tracker et;
4802 	struct ifaddr *ifa;
4803 	u_int count;
4804 
4805 	MPASS(cb);
4806 
4807 	count = 0;
4808 	NET_EPOCH_ENTER(et);
4809 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4810 		if (ifa->ifa_addr->sa_family != type)
4811 			continue;
4812 		count += (*cb)(cb_arg, ifa, count);
4813 	}
4814 	NET_EPOCH_EXIT(et);
4815 
4816 	return (count);
4817 }
4818 
4819 struct ifaddr *
4820 ifa_iter_start(if_t ifp, struct ifa_iter *iter)
4821 {
4822 	struct ifaddr *ifa;
4823 
4824 	NET_EPOCH_ASSERT();
4825 
4826 	bzero(iter, sizeof(*iter));
4827 	ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
4828 	if (ifa != NULL)
4829 		iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4830 	else
4831 		iter->context[0] = NULL;
4832 	return (ifa);
4833 }
4834 
4835 struct ifaddr *
4836 ifa_iter_next(struct ifa_iter *iter)
4837 {
4838 	struct ifaddr *ifa = iter->context[0];
4839 
4840 	if (ifa != NULL)
4841 		iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4842 	return (ifa);
4843 }
4844 
4845 void
4846 ifa_iter_finish(struct ifa_iter *iter)
4847 {
4848 	/* Nothing to do here for now. */
4849 }
4850 
4851 int
4852 if_setsoftc(if_t ifp, void *softc)
4853 {
4854 	ifp->if_softc = softc;
4855 	return (0);
4856 }
4857 
4858 void *
4859 if_getsoftc(const if_t ifp)
4860 {
4861 	return (ifp->if_softc);
4862 }
4863 
4864 void
4865 if_setrcvif(struct mbuf *m, if_t ifp)
4866 {
4867 
4868 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4869 	m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4870 }
4871 
4872 void
4873 if_setvtag(struct mbuf *m, uint16_t tag)
4874 {
4875 	m->m_pkthdr.ether_vtag = tag;
4876 }
4877 
4878 uint16_t
4879 if_getvtag(struct mbuf *m)
4880 {
4881 	return (m->m_pkthdr.ether_vtag);
4882 }
4883 
4884 int
4885 if_sendq_empty(if_t ifp)
4886 {
4887 	return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4888 }
4889 
4890 struct ifaddr *
4891 if_getifaddr(const if_t ifp)
4892 {
4893 	return (ifp->if_addr);
4894 }
4895 
4896 int
4897 if_setsendqready(if_t ifp)
4898 {
4899 	IFQ_SET_READY(&ifp->if_snd);
4900 	return (0);
4901 }
4902 
4903 int
4904 if_setsendqlen(if_t ifp, int tx_desc_count)
4905 {
4906 	IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4907 	ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4908 	return (0);
4909 }
4910 
4911 void
4912 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4913 {
4914 	ifp->if_netmap = na;
4915 }
4916 
4917 struct netmap_adapter *
4918 if_getnetmapadapter(if_t ifp)
4919 {
4920 	return (ifp->if_netmap);
4921 }
4922 
4923 int
4924 if_vlantrunkinuse(if_t ifp)
4925 {
4926 	return (ifp->if_vlantrunk != NULL);
4927 }
4928 
4929 void
4930 if_init(if_t ifp, void *ctx)
4931 {
4932 	(*ifp->if_init)(ctx);
4933 }
4934 
4935 void
4936 if_input(if_t ifp, struct mbuf* sendmp)
4937 {
4938 	(*ifp->if_input)(ifp, sendmp);
4939 }
4940 
4941 int
4942 if_transmit(if_t ifp, struct mbuf *m)
4943 {
4944 	return ((*ifp->if_transmit)(ifp, m));
4945 }
4946 
4947 int
4948 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4949 {
4950 	if (ifp->if_resolvemulti == NULL)
4951 		return (EOPNOTSUPP);
4952 
4953 	return (ifp->if_resolvemulti(ifp, srcs, dst));
4954 }
4955 
4956 int
4957 if_ioctl(if_t ifp, u_long cmd, void *data)
4958 {
4959 	if (ifp->if_ioctl == NULL)
4960 		return (EOPNOTSUPP);
4961 
4962 	return (ifp->if_ioctl(ifp, cmd, data));
4963 }
4964 
4965 struct mbuf *
4966 if_dequeue(if_t ifp)
4967 {
4968 	struct mbuf *m;
4969 
4970 	IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4971 	return (m);
4972 }
4973 
4974 int
4975 if_sendq_prepend(if_t ifp, struct mbuf *m)
4976 {
4977 	IFQ_DRV_PREPEND(&ifp->if_snd, m);
4978 	return (0);
4979 }
4980 
4981 int
4982 if_setifheaderlen(if_t ifp, int len)
4983 {
4984 	ifp->if_hdrlen = len;
4985 	return (0);
4986 }
4987 
4988 char *
4989 if_getlladdr(const if_t ifp)
4990 {
4991 	return (IF_LLADDR(ifp));
4992 }
4993 
4994 void *
4995 if_gethandle(u_char type)
4996 {
4997 	return (if_alloc(type));
4998 }
4999 
5000 void
5001 if_vlancap(if_t ifp)
5002 {
5003 	VLAN_CAPABILITIES(ifp);
5004 }
5005 
5006 int
5007 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
5008 {
5009 	ifp->if_hw_tsomax = if_hw_tsomax;
5010         return (0);
5011 }
5012 
5013 int
5014 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
5015 {
5016 	ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
5017         return (0);
5018 }
5019 
5020 int
5021 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
5022 {
5023 	ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
5024         return (0);
5025 }
5026 
5027 u_int
5028 if_gethwtsomax(const if_t ifp)
5029 {
5030 	return (ifp->if_hw_tsomax);
5031 }
5032 
5033 u_int
5034 if_gethwtsomaxsegcount(const if_t ifp)
5035 {
5036 	return (ifp->if_hw_tsomaxsegcount);
5037 }
5038 
5039 u_int
5040 if_gethwtsomaxsegsize(const if_t ifp)
5041 {
5042 	return (ifp->if_hw_tsomaxsegsize);
5043 }
5044 
5045 void
5046 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
5047 {
5048 	ifp->if_init = init_fn;
5049 }
5050 
5051 void
5052 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
5053 {
5054 	ifp->if_input = input_fn;
5055 }
5056 
5057 if_input_fn_t
5058 if_getinputfn(if_t ifp)
5059 {
5060 	return (ifp->if_input);
5061 }
5062 
5063 void
5064 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
5065 {
5066 	ifp->if_ioctl = ioctl_fn;
5067 }
5068 
5069 void
5070 if_setvfstatusfn(if_t ifp, if_vf_status_fn_t vf_status_fn)
5071 {
5072 	ifp->if_vf_status = vf_status_fn;
5073 }
5074 
5075 void
5076 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
5077 {
5078 	ifp->if_output = output_fn;
5079 }
5080 
5081 void
5082 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
5083 {
5084 	ifp->if_start = start_fn;
5085 }
5086 
5087 if_start_fn_t
5088 if_getstartfn(if_t ifp)
5089 {
5090 	return (ifp->if_start);
5091 }
5092 
5093 void
5094 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
5095 {
5096 	ifp->if_transmit = start_fn;
5097 }
5098 
5099 if_transmit_fn_t
5100 if_gettransmitfn(if_t ifp)
5101 {
5102 	return (ifp->if_transmit);
5103 }
5104 
5105 void
5106 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
5107 {
5108 	ifp->if_qflush = flush_fn;
5109 }
5110 
5111 if_qflush_fn_t
5112 if_getqflushfn(if_t ifp)
5113 {
5114 	return (ifp->if_qflush);
5115 }
5116 
5117 void
5118 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
5119 {
5120 	ifp->if_snd_tag_alloc = alloc_fn;
5121 }
5122 
5123 int
5124 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
5125     struct m_snd_tag **mstp)
5126 {
5127 	if (ifp->if_snd_tag_alloc == NULL)
5128 		return (EOPNOTSUPP);
5129 	return (ifp->if_snd_tag_alloc(ifp, params, mstp));
5130 }
5131 
5132 void
5133 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
5134 {
5135 	ifp->if_get_counter = fn;
5136 }
5137 
5138 void
5139 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
5140 {
5141 	ifp->if_reassign = fn;
5142 }
5143 
5144 void
5145 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
5146 {
5147 	ifp->if_ratelimit_query = fn;
5148 }
5149 
5150 void
5151 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
5152 {
5153 	ifp->if_debugnet_methods = m;
5154 }
5155 
5156 struct label *
5157 if_getmaclabel(if_t ifp)
5158 {
5159 	return (ifp->if_label);
5160 }
5161 
5162 void
5163 if_setmaclabel(if_t ifp, struct label *label)
5164 {
5165 	ifp->if_label = label;
5166 }
5167 
5168 int
5169 if_gettype(if_t ifp)
5170 {
5171 	return (ifp->if_type);
5172 }
5173 
5174 void *
5175 if_getllsoftc(if_t ifp)
5176 {
5177 	return (ifp->if_llsoftc);
5178 }
5179 
5180 void
5181 if_setllsoftc(if_t ifp, void *llsoftc)
5182 {
5183 	ifp->if_llsoftc = llsoftc;
5184 };
5185 
5186 int
5187 if_getlinkstate(if_t ifp)
5188 {
5189 	return (ifp->if_link_state);
5190 }
5191 
5192 const uint8_t *
5193 if_getbroadcastaddr(if_t ifp)
5194 {
5195 	return (ifp->if_broadcastaddr);
5196 }
5197 
5198 void
5199 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
5200 {
5201 	ifp->if_broadcastaddr = addr;
5202 }
5203 
5204 int
5205 if_getnumadomain(if_t ifp)
5206 {
5207 	return (ifp->if_numa_domain);
5208 }
5209 
5210 uint64_t
5211 if_getcounter(if_t ifp, ift_counter counter)
5212 {
5213 	return (ifp->if_get_counter(ifp, counter));
5214 }
5215 
5216 bool
5217 if_altq_is_enabled(if_t ifp)
5218 {
5219 	return (ALTQ_IS_ENABLED(&ifp->if_snd));
5220 }
5221 
5222 struct vnet *
5223 if_getvnet(if_t ifp)
5224 {
5225 	return (ifp->if_vnet);
5226 }
5227 
5228 struct in_ifinfo *
5229 if_getinet(if_t ifp)
5230 {
5231 	return (ifp->if_inet);
5232 }
5233 
5234 struct in6_ifextra *
5235 if_getinet6(if_t ifp)
5236 {
5237 	return (ifp->if_inet6);
5238 }
5239 
5240 u_int
5241 if_getfib(if_t ifp)
5242 {
5243 	return (ifp->if_fib);
5244 }
5245 
5246 uint8_t
5247 if_getaddrlen(if_t ifp)
5248 {
5249 	return (ifp->if_addrlen);
5250 }
5251 
5252 struct bpf_if *
5253 if_getbpf(if_t ifp)
5254 {
5255 	return (ifp->if_bpf);
5256 }
5257 
5258 struct ifvlantrunk *
5259 if_getvlantrunk(if_t ifp)
5260 {
5261 	return (ifp->if_vlantrunk);
5262 }
5263 
5264 uint8_t
5265 if_getpcp(if_t ifp)
5266 {
5267 	return (ifp->if_pcp);
5268 }
5269 
5270 void *
5271 if_getl2com(if_t ifp)
5272 {
5273 	return (ifp->if_l2com);
5274 }
5275 
5276 void
5277 if_setipsec_accel_methods(if_t ifp, const struct if_ipsec_accel_methods *m)
5278 {
5279 	ifp->if_ipsec_accel_m = m;
5280 }
5281 
5282 #ifdef DDB
5283 static void
5284 if_show_ifnet(struct ifnet *ifp)
5285 {
5286 	if (ifp == NULL)
5287 		return;
5288 	db_printf("%s:\n", ifp->if_xname);
5289 #define	IF_DB_PRINTF(f, e)	db_printf("   %s = " f "\n", #e, ifp->e);
5290 	IF_DB_PRINTF("%s", if_dname);
5291 	IF_DB_PRINTF("%d", if_dunit);
5292 	IF_DB_PRINTF("%s", if_description);
5293 	IF_DB_PRINTF("%u", if_index);
5294 	IF_DB_PRINTF("%d", if_idxgen);
5295 	IF_DB_PRINTF("%u", if_refcount);
5296 	IF_DB_PRINTF("%p", if_softc);
5297 	IF_DB_PRINTF("%p", if_l2com);
5298 	IF_DB_PRINTF("%p", if_llsoftc);
5299 	IF_DB_PRINTF("%d", if_amcount);
5300 	IF_DB_PRINTF("%p", if_addr);
5301 	IF_DB_PRINTF("%p", if_broadcastaddr);
5302 	IF_DB_PRINTF("%u", if_fib);
5303 	IF_DB_PRINTF("%p", if_vnet);
5304 	IF_DB_PRINTF("%p", if_home_vnet);
5305 	IF_DB_PRINTF("%p", if_vlantrunk);
5306 	IF_DB_PRINTF("%p", if_bpf);
5307 	IF_DB_PRINTF("%u", if_pcount);
5308 	IF_DB_PRINTF("%p", if_bridge);
5309 	IF_DB_PRINTF("%p", if_lagg);
5310 	IF_DB_PRINTF("%p", if_pf_kif);
5311 	IF_DB_PRINTF("%p", if_carp);
5312 	IF_DB_PRINTF("%p", if_label);
5313 	IF_DB_PRINTF("%p", if_netmap);
5314 	IF_DB_PRINTF("0x%08x", if_flags);
5315 	IF_DB_PRINTF("0x%08x", if_drv_flags);
5316 	IF_DB_PRINTF("0x%08x", if_capabilities);
5317 	IF_DB_PRINTF("0x%08x", if_capenable);
5318 	IF_DB_PRINTF("%p", if_snd.ifq_head);
5319 	IF_DB_PRINTF("%p", if_snd.ifq_tail);
5320 	IF_DB_PRINTF("%d", if_snd.ifq_len);
5321 	IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5322 	IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5323 	IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5324 	IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5325 	IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5326 	IF_DB_PRINTF("%d", if_snd.altq_type);
5327 	IF_DB_PRINTF("%x", if_snd.altq_flags);
5328 #undef IF_DB_PRINTF
5329 }
5330 
5331 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5332 {
5333 	if (!have_addr) {
5334 		db_printf("usage: show ifnet <struct ifnet *>\n");
5335 		return;
5336 	}
5337 
5338 	if_show_ifnet((struct ifnet *)addr);
5339 }
5340 
5341 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5342 {
5343 	struct ifnet *ifp;
5344 	u_short idx;
5345 
5346 	for (idx = 1; idx <= if_index; idx++) {
5347 		ifp = ifindex_table[idx].ife_ifnet;
5348 		if (ifp == NULL)
5349 			continue;
5350 		db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);
5351 		if (db_pager_quit)
5352 			break;
5353 	}
5354 }
5355 #endif	/* DDB */
5356