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