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