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