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