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