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