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