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