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