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