xref: /freebsd/sys/net/if.c (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
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 initalization 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 	int rc;
1025 
1026 	/*
1027 	 * Detach from current vnet, but preserve LLADDR info, do not
1028 	 * mark as dead etc. so that the ifnet can be reattached later.
1029 	 * If we cannot find it, we lost the race to someone else.
1030 	 */
1031 	rc = if_detach_internal(ifp, 1, &ifc);
1032 	if (rc != 0)
1033 		return;
1034 
1035 	/*
1036 	 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1037 	 * the if_index for that vnet if possible.
1038 	 *
1039 	 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1040 	 * or we'd lock on one vnet and unlock on another.
1041 	 */
1042 	IFNET_WLOCK();
1043 	ifindex_free_locked(ifp->if_index);
1044 	IFNET_WUNLOCK();
1045 
1046 	/*
1047 	 * Perform interface-specific reassignment tasks, if provided by
1048 	 * the driver.
1049 	 */
1050 	if (ifp->if_reassign != NULL)
1051 		ifp->if_reassign(ifp, new_vnet, NULL);
1052 
1053 	/*
1054 	 * Switch to the context of the target vnet.
1055 	 */
1056 	CURVNET_SET_QUIET(new_vnet);
1057 
1058 	IFNET_WLOCK();
1059 	ifp->if_index = ifindex_alloc();
1060 	ifnet_setbyindex_locked(ifp->if_index, ifp);
1061 	IFNET_WUNLOCK();
1062 
1063 	if_attach_internal(ifp, 1, ifc);
1064 
1065 	CURVNET_RESTORE();
1066 }
1067 
1068 /*
1069  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1070  */
1071 static int
1072 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1073 {
1074 	struct prison *pr;
1075 	struct ifnet *difp;
1076 
1077 	/* Try to find the prison within our visibility. */
1078 	sx_slock(&allprison_lock);
1079 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1080 	sx_sunlock(&allprison_lock);
1081 	if (pr == NULL)
1082 		return (ENXIO);
1083 	prison_hold_locked(pr);
1084 	mtx_unlock(&pr->pr_mtx);
1085 
1086 	/* Do not try to move the iface from and to the same prison. */
1087 	if (pr->pr_vnet == ifp->if_vnet) {
1088 		prison_free(pr);
1089 		return (EEXIST);
1090 	}
1091 
1092 	/* Make sure the named iface does not exists in the dst. prison/vnet. */
1093 	/* XXX Lock interfaces to avoid races. */
1094 	CURVNET_SET_QUIET(pr->pr_vnet);
1095 	difp = ifunit(ifname);
1096 	CURVNET_RESTORE();
1097 	if (difp != NULL) {
1098 		prison_free(pr);
1099 		return (EEXIST);
1100 	}
1101 
1102 	/* Move the interface into the child jail/vnet. */
1103 	if_vmove(ifp, pr->pr_vnet);
1104 
1105 	/* Report the new if_xname back to the userland. */
1106 	sprintf(ifname, "%s", ifp->if_xname);
1107 
1108 	prison_free(pr);
1109 	return (0);
1110 }
1111 
1112 static int
1113 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1114 {
1115 	struct prison *pr;
1116 	struct vnet *vnet_dst;
1117 	struct ifnet *ifp;
1118 
1119 	/* Try to find the prison within our visibility. */
1120 	sx_slock(&allprison_lock);
1121 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1122 	sx_sunlock(&allprison_lock);
1123 	if (pr == NULL)
1124 		return (ENXIO);
1125 	prison_hold_locked(pr);
1126 	mtx_unlock(&pr->pr_mtx);
1127 
1128 	/* Make sure the named iface exists in the source prison/vnet. */
1129 	CURVNET_SET(pr->pr_vnet);
1130 	ifp = ifunit(ifname);		/* XXX Lock to avoid races. */
1131 	if (ifp == NULL) {
1132 		CURVNET_RESTORE();
1133 		prison_free(pr);
1134 		return (ENXIO);
1135 	}
1136 
1137 	/* Do not try to move the iface from and to the same prison. */
1138 	vnet_dst = TD_TO_VNET(td);
1139 	if (vnet_dst == ifp->if_vnet) {
1140 		CURVNET_RESTORE();
1141 		prison_free(pr);
1142 		return (EEXIST);
1143 	}
1144 
1145 	/* Get interface back from child jail/vnet. */
1146 	if_vmove(ifp, vnet_dst);
1147 	CURVNET_RESTORE();
1148 
1149 	/* Report the new if_xname back to the userland. */
1150 	sprintf(ifname, "%s", ifp->if_xname);
1151 
1152 	prison_free(pr);
1153 	return (0);
1154 }
1155 #endif /* VIMAGE */
1156 
1157 /*
1158  * Add a group to an interface
1159  */
1160 int
1161 if_addgroup(struct ifnet *ifp, const char *groupname)
1162 {
1163 	struct ifg_list		*ifgl;
1164 	struct ifg_group	*ifg = NULL;
1165 	struct ifg_member	*ifgm;
1166 	int 			 new = 0;
1167 
1168 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1169 	    groupname[strlen(groupname) - 1] <= '9')
1170 		return (EINVAL);
1171 
1172 	IFNET_WLOCK();
1173 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1174 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1175 			IFNET_WUNLOCK();
1176 			return (EEXIST);
1177 		}
1178 
1179 	if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1180 	    M_NOWAIT)) == NULL) {
1181 	    	IFNET_WUNLOCK();
1182 		return (ENOMEM);
1183 	}
1184 
1185 	if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1186 	    M_TEMP, M_NOWAIT)) == NULL) {
1187 		free(ifgl, M_TEMP);
1188 		IFNET_WUNLOCK();
1189 		return (ENOMEM);
1190 	}
1191 
1192 	TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1193 		if (!strcmp(ifg->ifg_group, groupname))
1194 			break;
1195 
1196 	if (ifg == NULL) {
1197 		if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1198 		    M_TEMP, M_NOWAIT)) == NULL) {
1199 			free(ifgl, M_TEMP);
1200 			free(ifgm, M_TEMP);
1201 			IFNET_WUNLOCK();
1202 			return (ENOMEM);
1203 		}
1204 		strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1205 		ifg->ifg_refcnt = 0;
1206 		TAILQ_INIT(&ifg->ifg_members);
1207 		TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1208 		new = 1;
1209 	}
1210 
1211 	ifg->ifg_refcnt++;
1212 	ifgl->ifgl_group = ifg;
1213 	ifgm->ifgm_ifp = ifp;
1214 
1215 	IF_ADDR_WLOCK(ifp);
1216 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1217 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1218 	IF_ADDR_WUNLOCK(ifp);
1219 
1220 	IFNET_WUNLOCK();
1221 
1222 	if (new)
1223 		EVENTHANDLER_INVOKE(group_attach_event, ifg);
1224 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1225 
1226 	return (0);
1227 }
1228 
1229 /*
1230  * Remove a group from an interface
1231  */
1232 int
1233 if_delgroup(struct ifnet *ifp, const char *groupname)
1234 {
1235 	struct ifg_list		*ifgl;
1236 	struct ifg_member	*ifgm;
1237 
1238 	IFNET_WLOCK();
1239 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1240 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1241 			break;
1242 	if (ifgl == NULL) {
1243 		IFNET_WUNLOCK();
1244 		return (ENOENT);
1245 	}
1246 
1247 	IF_ADDR_WLOCK(ifp);
1248 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1249 	IF_ADDR_WUNLOCK(ifp);
1250 
1251 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1252 		if (ifgm->ifgm_ifp == ifp)
1253 			break;
1254 
1255 	if (ifgm != NULL) {
1256 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1257 		free(ifgm, M_TEMP);
1258 	}
1259 
1260 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1261 		TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1262 		IFNET_WUNLOCK();
1263 		EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1264 		free(ifgl->ifgl_group, M_TEMP);
1265 	} else
1266 		IFNET_WUNLOCK();
1267 
1268 	free(ifgl, M_TEMP);
1269 
1270 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1271 
1272 	return (0);
1273 }
1274 
1275 /*
1276  * Remove an interface from all groups
1277  */
1278 static void
1279 if_delgroups(struct ifnet *ifp)
1280 {
1281 	struct ifg_list		*ifgl;
1282 	struct ifg_member	*ifgm;
1283 	char groupname[IFNAMSIZ];
1284 
1285 	IFNET_WLOCK();
1286 	while (!TAILQ_EMPTY(&ifp->if_groups)) {
1287 		ifgl = TAILQ_FIRST(&ifp->if_groups);
1288 
1289 		strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1290 
1291 		IF_ADDR_WLOCK(ifp);
1292 		TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1293 		IF_ADDR_WUNLOCK(ifp);
1294 
1295 		TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1296 			if (ifgm->ifgm_ifp == ifp)
1297 				break;
1298 
1299 		if (ifgm != NULL) {
1300 			TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1301 			    ifgm_next);
1302 			free(ifgm, M_TEMP);
1303 		}
1304 
1305 		if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1306 			TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1307 			IFNET_WUNLOCK();
1308 			EVENTHANDLER_INVOKE(group_detach_event,
1309 			    ifgl->ifgl_group);
1310 			free(ifgl->ifgl_group, M_TEMP);
1311 		} else
1312 			IFNET_WUNLOCK();
1313 
1314 		free(ifgl, M_TEMP);
1315 
1316 		EVENTHANDLER_INVOKE(group_change_event, groupname);
1317 
1318 		IFNET_WLOCK();
1319 	}
1320 	IFNET_WUNLOCK();
1321 }
1322 
1323 /*
1324  * Stores all groups from an interface in memory pointed
1325  * to by data
1326  */
1327 static int
1328 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1329 {
1330 	int			 len, error;
1331 	struct ifg_list		*ifgl;
1332 	struct ifg_req		 ifgrq, *ifgp;
1333 	struct ifgroupreq	*ifgr = data;
1334 
1335 	if (ifgr->ifgr_len == 0) {
1336 		IF_ADDR_RLOCK(ifp);
1337 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1338 			ifgr->ifgr_len += sizeof(struct ifg_req);
1339 		IF_ADDR_RUNLOCK(ifp);
1340 		return (0);
1341 	}
1342 
1343 	len = ifgr->ifgr_len;
1344 	ifgp = ifgr->ifgr_groups;
1345 	/* XXX: wire */
1346 	IF_ADDR_RLOCK(ifp);
1347 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1348 		if (len < sizeof(ifgrq)) {
1349 			IF_ADDR_RUNLOCK(ifp);
1350 			return (EINVAL);
1351 		}
1352 		bzero(&ifgrq, sizeof ifgrq);
1353 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1354 		    sizeof(ifgrq.ifgrq_group));
1355 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1356 		    	IF_ADDR_RUNLOCK(ifp);
1357 			return (error);
1358 		}
1359 		len -= sizeof(ifgrq);
1360 		ifgp++;
1361 	}
1362 	IF_ADDR_RUNLOCK(ifp);
1363 
1364 	return (0);
1365 }
1366 
1367 /*
1368  * Stores all members of a group in memory pointed to by data
1369  */
1370 static int
1371 if_getgroupmembers(struct ifgroupreq *data)
1372 {
1373 	struct ifgroupreq	*ifgr = data;
1374 	struct ifg_group	*ifg;
1375 	struct ifg_member	*ifgm;
1376 	struct ifg_req		 ifgrq, *ifgp;
1377 	int			 len, error;
1378 
1379 	IFNET_RLOCK();
1380 	TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1381 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1382 			break;
1383 	if (ifg == NULL) {
1384 		IFNET_RUNLOCK();
1385 		return (ENOENT);
1386 	}
1387 
1388 	if (ifgr->ifgr_len == 0) {
1389 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1390 			ifgr->ifgr_len += sizeof(ifgrq);
1391 		IFNET_RUNLOCK();
1392 		return (0);
1393 	}
1394 
1395 	len = ifgr->ifgr_len;
1396 	ifgp = ifgr->ifgr_groups;
1397 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1398 		if (len < sizeof(ifgrq)) {
1399 			IFNET_RUNLOCK();
1400 			return (EINVAL);
1401 		}
1402 		bzero(&ifgrq, sizeof ifgrq);
1403 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1404 		    sizeof(ifgrq.ifgrq_member));
1405 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1406 			IFNET_RUNLOCK();
1407 			return (error);
1408 		}
1409 		len -= sizeof(ifgrq);
1410 		ifgp++;
1411 	}
1412 	IFNET_RUNLOCK();
1413 
1414 	return (0);
1415 }
1416 
1417 /*
1418  * Return counter values from counter(9)s stored in ifnet.
1419  */
1420 uint64_t
1421 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1422 {
1423 
1424 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1425 
1426 	return (counter_u64_fetch(ifp->if_counters[cnt]));
1427 }
1428 
1429 /*
1430  * Increase an ifnet counter. Usually used for counters shared
1431  * between the stack and a driver, but function supports them all.
1432  */
1433 void
1434 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1435 {
1436 
1437 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1438 
1439 	counter_u64_add(ifp->if_counters[cnt], inc);
1440 }
1441 
1442 /*
1443  * Copy data from ifnet to userland API structure if_data.
1444  */
1445 void
1446 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1447 {
1448 
1449 	ifd->ifi_type = ifp->if_type;
1450 	ifd->ifi_physical = 0;
1451 	ifd->ifi_addrlen = ifp->if_addrlen;
1452 	ifd->ifi_hdrlen = ifp->if_hdrlen;
1453 	ifd->ifi_link_state = ifp->if_link_state;
1454 	ifd->ifi_vhid = 0;
1455 	ifd->ifi_datalen = sizeof(struct if_data);
1456 	ifd->ifi_mtu = ifp->if_mtu;
1457 	ifd->ifi_metric = ifp->if_metric;
1458 	ifd->ifi_baudrate = ifp->if_baudrate;
1459 	ifd->ifi_hwassist = ifp->if_hwassist;
1460 	ifd->ifi_epoch = ifp->if_epoch;
1461 	ifd->ifi_lastchange = ifp->if_lastchange;
1462 
1463 	ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1464 	ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1465 	ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1466 	ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1467 	ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1468 	ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1469 	ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1470 	ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1471 	ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1472 	ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1473 	ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1474 	ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1475 }
1476 
1477 /*
1478  * Wrapper functions for struct ifnet address list locking macros.  These are
1479  * used by kernel modules to avoid encoding programming interface or binary
1480  * interface assumptions that may be violated when kernel-internal locking
1481  * approaches change.
1482  */
1483 void
1484 if_addr_rlock(struct ifnet *ifp)
1485 {
1486 
1487 	IF_ADDR_RLOCK(ifp);
1488 }
1489 
1490 void
1491 if_addr_runlock(struct ifnet *ifp)
1492 {
1493 
1494 	IF_ADDR_RUNLOCK(ifp);
1495 }
1496 
1497 void
1498 if_maddr_rlock(if_t ifp)
1499 {
1500 
1501 	IF_ADDR_RLOCK((struct ifnet *)ifp);
1502 }
1503 
1504 void
1505 if_maddr_runlock(if_t ifp)
1506 {
1507 
1508 	IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1509 }
1510 
1511 /*
1512  * Initialization, destruction and refcounting functions for ifaddrs.
1513  */
1514 struct ifaddr *
1515 ifa_alloc(size_t size, int flags)
1516 {
1517 	struct ifaddr *ifa;
1518 
1519 	KASSERT(size >= sizeof(struct ifaddr),
1520 	    ("%s: invalid size %zu", __func__, size));
1521 
1522 	ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1523 	if (ifa == NULL)
1524 		return (NULL);
1525 
1526 	if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1527 		goto fail;
1528 	if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1529 		goto fail;
1530 	if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1531 		goto fail;
1532 	if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1533 		goto fail;
1534 
1535 	refcount_init(&ifa->ifa_refcnt, 1);
1536 
1537 	return (ifa);
1538 
1539 fail:
1540 	/* free(NULL) is okay */
1541 	counter_u64_free(ifa->ifa_opackets);
1542 	counter_u64_free(ifa->ifa_ipackets);
1543 	counter_u64_free(ifa->ifa_obytes);
1544 	counter_u64_free(ifa->ifa_ibytes);
1545 	free(ifa, M_IFADDR);
1546 
1547 	return (NULL);
1548 }
1549 
1550 void
1551 ifa_ref(struct ifaddr *ifa)
1552 {
1553 
1554 	refcount_acquire(&ifa->ifa_refcnt);
1555 }
1556 
1557 void
1558 ifa_free(struct ifaddr *ifa)
1559 {
1560 
1561 	if (refcount_release(&ifa->ifa_refcnt)) {
1562 		counter_u64_free(ifa->ifa_opackets);
1563 		counter_u64_free(ifa->ifa_ipackets);
1564 		counter_u64_free(ifa->ifa_obytes);
1565 		counter_u64_free(ifa->ifa_ibytes);
1566 		free(ifa, M_IFADDR);
1567 	}
1568 }
1569 
1570 static int
1571 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1572     struct sockaddr *ia)
1573 {
1574 	int error;
1575 	struct rt_addrinfo info;
1576 	struct sockaddr_dl null_sdl;
1577 	struct ifnet *ifp;
1578 
1579 	ifp = ifa->ifa_ifp;
1580 
1581 	bzero(&info, sizeof(info));
1582 	if (cmd != RTM_DELETE)
1583 		info.rti_ifp = V_loif;
1584 	info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1585 	info.rti_info[RTAX_DST] = ia;
1586 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1587 	link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1588 
1589 	error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1590 
1591 	if (error != 0)
1592 		log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1593 		    __func__, otype, if_name(ifp), error);
1594 
1595 	return (error);
1596 }
1597 
1598 int
1599 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1600 {
1601 
1602 	return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1603 }
1604 
1605 int
1606 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1607 {
1608 
1609 	return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1610 }
1611 
1612 int
1613 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1614 {
1615 
1616 	return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1617 }
1618 
1619 /*
1620  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1621  * structs used to represent other address families, it is necessary
1622  * to perform a different comparison.
1623  */
1624 
1625 #define	sa_dl_equal(a1, a2)	\
1626 	((((const struct sockaddr_dl *)(a1))->sdl_len ==		\
1627 	 ((const struct sockaddr_dl *)(a2))->sdl_len) &&		\
1628 	 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)),		\
1629 	       CLLADDR((const struct sockaddr_dl *)(a2)),		\
1630 	       ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1631 
1632 /*
1633  * Locate an interface based on a complete address.
1634  */
1635 /*ARGSUSED*/
1636 static struct ifaddr *
1637 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1638 {
1639 	struct ifnet *ifp;
1640 	struct ifaddr *ifa;
1641 
1642 	IFNET_RLOCK_NOSLEEP();
1643 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1644 		IF_ADDR_RLOCK(ifp);
1645 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1646 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1647 				continue;
1648 			if (sa_equal(addr, ifa->ifa_addr)) {
1649 				if (getref)
1650 					ifa_ref(ifa);
1651 				IF_ADDR_RUNLOCK(ifp);
1652 				goto done;
1653 			}
1654 			/* IP6 doesn't have broadcast */
1655 			if ((ifp->if_flags & IFF_BROADCAST) &&
1656 			    ifa->ifa_broadaddr &&
1657 			    ifa->ifa_broadaddr->sa_len != 0 &&
1658 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1659 				if (getref)
1660 					ifa_ref(ifa);
1661 				IF_ADDR_RUNLOCK(ifp);
1662 				goto done;
1663 			}
1664 		}
1665 		IF_ADDR_RUNLOCK(ifp);
1666 	}
1667 	ifa = NULL;
1668 done:
1669 	IFNET_RUNLOCK_NOSLEEP();
1670 	return (ifa);
1671 }
1672 
1673 struct ifaddr *
1674 ifa_ifwithaddr(const struct sockaddr *addr)
1675 {
1676 
1677 	return (ifa_ifwithaddr_internal(addr, 1));
1678 }
1679 
1680 int
1681 ifa_ifwithaddr_check(const struct sockaddr *addr)
1682 {
1683 
1684 	return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1685 }
1686 
1687 /*
1688  * Locate an interface based on the broadcast address.
1689  */
1690 /* ARGSUSED */
1691 struct ifaddr *
1692 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1693 {
1694 	struct ifnet *ifp;
1695 	struct ifaddr *ifa;
1696 
1697 	IFNET_RLOCK_NOSLEEP();
1698 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1699 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1700 			continue;
1701 		IF_ADDR_RLOCK(ifp);
1702 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1703 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1704 				continue;
1705 			if ((ifp->if_flags & IFF_BROADCAST) &&
1706 			    ifa->ifa_broadaddr &&
1707 			    ifa->ifa_broadaddr->sa_len != 0 &&
1708 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1709 				ifa_ref(ifa);
1710 				IF_ADDR_RUNLOCK(ifp);
1711 				goto done;
1712 			}
1713 		}
1714 		IF_ADDR_RUNLOCK(ifp);
1715 	}
1716 	ifa = NULL;
1717 done:
1718 	IFNET_RUNLOCK_NOSLEEP();
1719 	return (ifa);
1720 }
1721 
1722 /*
1723  * Locate the point to point interface with a given destination address.
1724  */
1725 /*ARGSUSED*/
1726 struct ifaddr *
1727 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1728 {
1729 	struct ifnet *ifp;
1730 	struct ifaddr *ifa;
1731 
1732 	IFNET_RLOCK_NOSLEEP();
1733 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1734 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1735 			continue;
1736 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1737 			continue;
1738 		IF_ADDR_RLOCK(ifp);
1739 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1740 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1741 				continue;
1742 			if (ifa->ifa_dstaddr != NULL &&
1743 			    sa_equal(addr, ifa->ifa_dstaddr)) {
1744 				ifa_ref(ifa);
1745 				IF_ADDR_RUNLOCK(ifp);
1746 				goto done;
1747 			}
1748 		}
1749 		IF_ADDR_RUNLOCK(ifp);
1750 	}
1751 	ifa = NULL;
1752 done:
1753 	IFNET_RUNLOCK_NOSLEEP();
1754 	return (ifa);
1755 }
1756 
1757 /*
1758  * Find an interface on a specific network.  If many, choice
1759  * is most specific found.
1760  */
1761 struct ifaddr *
1762 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1763 {
1764 	struct ifnet *ifp;
1765 	struct ifaddr *ifa;
1766 	struct ifaddr *ifa_maybe = NULL;
1767 	u_int af = addr->sa_family;
1768 	const char *addr_data = addr->sa_data, *cplim;
1769 
1770 	/*
1771 	 * AF_LINK addresses can be looked up directly by their index number,
1772 	 * so do that if we can.
1773 	 */
1774 	if (af == AF_LINK) {
1775 	    const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
1776 	    if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1777 		return (ifaddr_byindex(sdl->sdl_index));
1778 	}
1779 
1780 	/*
1781 	 * Scan though each interface, looking for ones that have addresses
1782 	 * in this address family and the requested fib.  Maintain a reference
1783 	 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1784 	 * kept it stable when we move onto the next interface.
1785 	 */
1786 	IFNET_RLOCK_NOSLEEP();
1787 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1788 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1789 			continue;
1790 		IF_ADDR_RLOCK(ifp);
1791 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1792 			const char *cp, *cp2, *cp3;
1793 
1794 			if (ifa->ifa_addr->sa_family != af)
1795 next:				continue;
1796 			if (af == AF_INET &&
1797 			    ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1798 				/*
1799 				 * This is a bit broken as it doesn't
1800 				 * take into account that the remote end may
1801 				 * be a single node in the network we are
1802 				 * looking for.
1803 				 * The trouble is that we don't know the
1804 				 * netmask for the remote end.
1805 				 */
1806 				if (ifa->ifa_dstaddr != NULL &&
1807 				    sa_equal(addr, ifa->ifa_dstaddr)) {
1808 					ifa_ref(ifa);
1809 					IF_ADDR_RUNLOCK(ifp);
1810 					goto done;
1811 				}
1812 			} else {
1813 				/*
1814 				 * Scan all the bits in the ifa's address.
1815 				 * If a bit dissagrees with what we are
1816 				 * looking for, mask it with the netmask
1817 				 * to see if it really matters.
1818 				 * (A byte at a time)
1819 				 */
1820 				if (ifa->ifa_netmask == 0)
1821 					continue;
1822 				cp = addr_data;
1823 				cp2 = ifa->ifa_addr->sa_data;
1824 				cp3 = ifa->ifa_netmask->sa_data;
1825 				cplim = ifa->ifa_netmask->sa_len
1826 					+ (char *)ifa->ifa_netmask;
1827 				while (cp3 < cplim)
1828 					if ((*cp++ ^ *cp2++) & *cp3++)
1829 						goto next; /* next address! */
1830 				/*
1831 				 * If the netmask of what we just found
1832 				 * is more specific than what we had before
1833 				 * (if we had one), or if the virtual status
1834 				 * of new prefix is better than of the old one,
1835 				 * then remember the new one before continuing
1836 				 * to search for an even better one.
1837 				 */
1838 				if (ifa_maybe == NULL ||
1839 				    ifa_preferred(ifa_maybe, ifa) ||
1840 				    rn_refines((caddr_t)ifa->ifa_netmask,
1841 				    (caddr_t)ifa_maybe->ifa_netmask)) {
1842 					if (ifa_maybe != NULL)
1843 						ifa_free(ifa_maybe);
1844 					ifa_maybe = ifa;
1845 					ifa_ref(ifa_maybe);
1846 				}
1847 			}
1848 		}
1849 		IF_ADDR_RUNLOCK(ifp);
1850 	}
1851 	ifa = ifa_maybe;
1852 	ifa_maybe = NULL;
1853 done:
1854 	IFNET_RUNLOCK_NOSLEEP();
1855 	if (ifa_maybe != NULL)
1856 		ifa_free(ifa_maybe);
1857 	return (ifa);
1858 }
1859 
1860 /*
1861  * Find an interface address specific to an interface best matching
1862  * a given address.
1863  */
1864 struct ifaddr *
1865 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1866 {
1867 	struct ifaddr *ifa;
1868 	const char *cp, *cp2, *cp3;
1869 	char *cplim;
1870 	struct ifaddr *ifa_maybe = NULL;
1871 	u_int af = addr->sa_family;
1872 
1873 	if (af >= AF_MAX)
1874 		return (NULL);
1875 	IF_ADDR_RLOCK(ifp);
1876 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1877 		if (ifa->ifa_addr->sa_family != af)
1878 			continue;
1879 		if (ifa_maybe == NULL)
1880 			ifa_maybe = ifa;
1881 		if (ifa->ifa_netmask == 0) {
1882 			if (sa_equal(addr, ifa->ifa_addr) ||
1883 			    (ifa->ifa_dstaddr &&
1884 			    sa_equal(addr, ifa->ifa_dstaddr)))
1885 				goto done;
1886 			continue;
1887 		}
1888 		if (ifp->if_flags & IFF_POINTOPOINT) {
1889 			if (sa_equal(addr, ifa->ifa_dstaddr))
1890 				goto done;
1891 		} else {
1892 			cp = addr->sa_data;
1893 			cp2 = ifa->ifa_addr->sa_data;
1894 			cp3 = ifa->ifa_netmask->sa_data;
1895 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1896 			for (; cp3 < cplim; cp3++)
1897 				if ((*cp++ ^ *cp2++) & *cp3)
1898 					break;
1899 			if (cp3 == cplim)
1900 				goto done;
1901 		}
1902 	}
1903 	ifa = ifa_maybe;
1904 done:
1905 	if (ifa != NULL)
1906 		ifa_ref(ifa);
1907 	IF_ADDR_RUNLOCK(ifp);
1908 	return (ifa);
1909 }
1910 
1911 /*
1912  * See whether new ifa is better than current one:
1913  * 1) A non-virtual one is preferred over virtual.
1914  * 2) A virtual in master state preferred over any other state.
1915  *
1916  * Used in several address selecting functions.
1917  */
1918 int
1919 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1920 {
1921 
1922 	return (cur->ifa_carp && (!next->ifa_carp ||
1923 	    ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1924 }
1925 
1926 #include <net/if_llatbl.h>
1927 
1928 /*
1929  * Default action when installing a route with a Link Level gateway.
1930  * Lookup an appropriate real ifa to point to.
1931  * This should be moved to /sys/net/link.c eventually.
1932  */
1933 static void
1934 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1935 {
1936 	struct ifaddr *ifa, *oifa;
1937 	struct sockaddr *dst;
1938 	struct ifnet *ifp;
1939 
1940 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1941 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1942 		return;
1943 	ifa = ifaof_ifpforaddr(dst, ifp);
1944 	if (ifa) {
1945 		oifa = rt->rt_ifa;
1946 		rt->rt_ifa = ifa;
1947 		ifa_free(oifa);
1948 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1949 			ifa->ifa_rtrequest(cmd, rt, info);
1950 	}
1951 }
1952 
1953 struct sockaddr_dl *
1954 link_alloc_sdl(size_t size, int flags)
1955 {
1956 
1957 	return (malloc(size, M_TEMP, flags));
1958 }
1959 
1960 void
1961 link_free_sdl(struct sockaddr *sa)
1962 {
1963 	free(sa, M_TEMP);
1964 }
1965 
1966 /*
1967  * Fills in given sdl with interface basic info.
1968  * Returns pointer to filled sdl.
1969  */
1970 struct sockaddr_dl *
1971 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
1972 {
1973 	struct sockaddr_dl *sdl;
1974 
1975 	sdl = (struct sockaddr_dl *)paddr;
1976 	memset(sdl, 0, sizeof(struct sockaddr_dl));
1977 	sdl->sdl_len = sizeof(struct sockaddr_dl);
1978 	sdl->sdl_family = AF_LINK;
1979 	sdl->sdl_index = ifp->if_index;
1980 	sdl->sdl_type = iftype;
1981 
1982 	return (sdl);
1983 }
1984 
1985 /*
1986  * Mark an interface down and notify protocols of
1987  * the transition.
1988  */
1989 static void
1990 if_unroute(struct ifnet *ifp, int flag, int fam)
1991 {
1992 	struct ifaddr *ifa;
1993 
1994 	KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1995 
1996 	ifp->if_flags &= ~flag;
1997 	getmicrotime(&ifp->if_lastchange);
1998 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1999 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2000 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2001 	ifp->if_qflush(ifp);
2002 
2003 	if (ifp->if_carp)
2004 		(*carp_linkstate_p)(ifp);
2005 	rt_ifmsg(ifp);
2006 }
2007 
2008 /*
2009  * Mark an interface up and notify protocols of
2010  * the transition.
2011  */
2012 static void
2013 if_route(struct ifnet *ifp, int flag, int fam)
2014 {
2015 	struct ifaddr *ifa;
2016 
2017 	KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2018 
2019 	ifp->if_flags |= flag;
2020 	getmicrotime(&ifp->if_lastchange);
2021 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2022 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2023 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
2024 	if (ifp->if_carp)
2025 		(*carp_linkstate_p)(ifp);
2026 	rt_ifmsg(ifp);
2027 #ifdef INET6
2028 	in6_if_up(ifp);
2029 #endif
2030 }
2031 
2032 void	(*vlan_link_state_p)(struct ifnet *);	/* XXX: private from if_vlan */
2033 void	(*vlan_trunk_cap_p)(struct ifnet *);		/* XXX: private from if_vlan */
2034 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2035 struct	ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2036 int	(*vlan_tag_p)(struct ifnet *, uint16_t *);
2037 int	(*vlan_setcookie_p)(struct ifnet *, void *);
2038 void	*(*vlan_cookie_p)(struct ifnet *);
2039 
2040 /*
2041  * Handle a change in the interface link state. To avoid LORs
2042  * between driver lock and upper layer locks, as well as possible
2043  * recursions, we post event to taskqueue, and all job
2044  * is done in static do_link_state_change().
2045  */
2046 void
2047 if_link_state_change(struct ifnet *ifp, int link_state)
2048 {
2049 	/* Return if state hasn't changed. */
2050 	if (ifp->if_link_state == link_state)
2051 		return;
2052 
2053 	ifp->if_link_state = link_state;
2054 
2055 	taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2056 }
2057 
2058 static void
2059 do_link_state_change(void *arg, int pending)
2060 {
2061 	struct ifnet *ifp = (struct ifnet *)arg;
2062 	int link_state = ifp->if_link_state;
2063 	CURVNET_SET(ifp->if_vnet);
2064 
2065 	/* Notify that the link state has changed. */
2066 	rt_ifmsg(ifp);
2067 	if (ifp->if_vlantrunk != NULL)
2068 		(*vlan_link_state_p)(ifp);
2069 
2070 	if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2071 	    ifp->if_l2com != NULL)
2072 		(*ng_ether_link_state_p)(ifp, link_state);
2073 	if (ifp->if_carp)
2074 		(*carp_linkstate_p)(ifp);
2075 	if (ifp->if_bridge)
2076 		(*bridge_linkstate_p)(ifp);
2077 	if (ifp->if_lagg)
2078 		(*lagg_linkstate_p)(ifp, link_state);
2079 
2080 	if (IS_DEFAULT_VNET(curvnet))
2081 		devctl_notify("IFNET", ifp->if_xname,
2082 		    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2083 		    NULL);
2084 	if (pending > 1)
2085 		if_printf(ifp, "%d link states coalesced\n", pending);
2086 	if (log_link_state_change)
2087 		log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2088 		    (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2089 	EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2090 	CURVNET_RESTORE();
2091 }
2092 
2093 /*
2094  * Mark an interface down and notify protocols of
2095  * the transition.
2096  */
2097 void
2098 if_down(struct ifnet *ifp)
2099 {
2100 
2101 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
2102 }
2103 
2104 /*
2105  * Mark an interface up and notify protocols of
2106  * the transition.
2107  */
2108 void
2109 if_up(struct ifnet *ifp)
2110 {
2111 
2112 	if_route(ifp, IFF_UP, AF_UNSPEC);
2113 }
2114 
2115 /*
2116  * Flush an interface queue.
2117  */
2118 void
2119 if_qflush(struct ifnet *ifp)
2120 {
2121 	struct mbuf *m, *n;
2122 	struct ifaltq *ifq;
2123 
2124 	ifq = &ifp->if_snd;
2125 	IFQ_LOCK(ifq);
2126 #ifdef ALTQ
2127 	if (ALTQ_IS_ENABLED(ifq))
2128 		ALTQ_PURGE(ifq);
2129 #endif
2130 	n = ifq->ifq_head;
2131 	while ((m = n) != 0) {
2132 		n = m->m_nextpkt;
2133 		m_freem(m);
2134 	}
2135 	ifq->ifq_head = 0;
2136 	ifq->ifq_tail = 0;
2137 	ifq->ifq_len = 0;
2138 	IFQ_UNLOCK(ifq);
2139 }
2140 
2141 /*
2142  * Map interface name to interface structure pointer, with or without
2143  * returning a reference.
2144  */
2145 struct ifnet *
2146 ifunit_ref(const char *name)
2147 {
2148 	struct ifnet *ifp;
2149 
2150 	IFNET_RLOCK_NOSLEEP();
2151 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2152 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2153 		    !(ifp->if_flags & IFF_DYING))
2154 			break;
2155 	}
2156 	if (ifp != NULL)
2157 		if_ref(ifp);
2158 	IFNET_RUNLOCK_NOSLEEP();
2159 	return (ifp);
2160 }
2161 
2162 struct ifnet *
2163 ifunit(const char *name)
2164 {
2165 	struct ifnet *ifp;
2166 
2167 	IFNET_RLOCK_NOSLEEP();
2168 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2169 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2170 			break;
2171 	}
2172 	IFNET_RUNLOCK_NOSLEEP();
2173 	return (ifp);
2174 }
2175 
2176 /*
2177  * Hardware specific interface ioctls.
2178  */
2179 static int
2180 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2181 {
2182 	struct ifreq *ifr;
2183 	int error = 0;
2184 	int new_flags, temp_flags;
2185 	size_t namelen, onamelen;
2186 	size_t descrlen;
2187 	char *descrbuf, *odescrbuf;
2188 	char new_name[IFNAMSIZ];
2189 	struct ifaddr *ifa;
2190 	struct sockaddr_dl *sdl;
2191 
2192 	ifr = (struct ifreq *)data;
2193 	switch (cmd) {
2194 	case SIOCGIFINDEX:
2195 		ifr->ifr_index = ifp->if_index;
2196 		break;
2197 
2198 	case SIOCGIFFLAGS:
2199 		temp_flags = ifp->if_flags | ifp->if_drv_flags;
2200 		ifr->ifr_flags = temp_flags & 0xffff;
2201 		ifr->ifr_flagshigh = temp_flags >> 16;
2202 		break;
2203 
2204 	case SIOCGIFCAP:
2205 		ifr->ifr_reqcap = ifp->if_capabilities;
2206 		ifr->ifr_curcap = ifp->if_capenable;
2207 		break;
2208 
2209 #ifdef MAC
2210 	case SIOCGIFMAC:
2211 		error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2212 		break;
2213 #endif
2214 
2215 	case SIOCGIFMETRIC:
2216 		ifr->ifr_metric = ifp->if_metric;
2217 		break;
2218 
2219 	case SIOCGIFMTU:
2220 		ifr->ifr_mtu = ifp->if_mtu;
2221 		break;
2222 
2223 	case SIOCGIFPHYS:
2224 		/* XXXGL: did this ever worked? */
2225 		ifr->ifr_phys = 0;
2226 		break;
2227 
2228 	case SIOCGIFDESCR:
2229 		error = 0;
2230 		sx_slock(&ifdescr_sx);
2231 		if (ifp->if_description == NULL)
2232 			error = ENOMSG;
2233 		else {
2234 			/* space for terminating nul */
2235 			descrlen = strlen(ifp->if_description) + 1;
2236 			if (ifr->ifr_buffer.length < descrlen)
2237 				ifr->ifr_buffer.buffer = NULL;
2238 			else
2239 				error = copyout(ifp->if_description,
2240 				    ifr->ifr_buffer.buffer, descrlen);
2241 			ifr->ifr_buffer.length = descrlen;
2242 		}
2243 		sx_sunlock(&ifdescr_sx);
2244 		break;
2245 
2246 	case SIOCSIFDESCR:
2247 		error = priv_check(td, PRIV_NET_SETIFDESCR);
2248 		if (error)
2249 			return (error);
2250 
2251 		/*
2252 		 * Copy only (length-1) bytes to make sure that
2253 		 * if_description is always nul terminated.  The
2254 		 * length parameter is supposed to count the
2255 		 * terminating nul in.
2256 		 */
2257 		if (ifr->ifr_buffer.length > ifdescr_maxlen)
2258 			return (ENAMETOOLONG);
2259 		else if (ifr->ifr_buffer.length == 0)
2260 			descrbuf = NULL;
2261 		else {
2262 			descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2263 			    M_WAITOK | M_ZERO);
2264 			error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2265 			    ifr->ifr_buffer.length - 1);
2266 			if (error) {
2267 				free(descrbuf, M_IFDESCR);
2268 				break;
2269 			}
2270 		}
2271 
2272 		sx_xlock(&ifdescr_sx);
2273 		odescrbuf = ifp->if_description;
2274 		ifp->if_description = descrbuf;
2275 		sx_xunlock(&ifdescr_sx);
2276 
2277 		getmicrotime(&ifp->if_lastchange);
2278 		free(odescrbuf, M_IFDESCR);
2279 		break;
2280 
2281 	case SIOCGIFFIB:
2282 		ifr->ifr_fib = ifp->if_fib;
2283 		break;
2284 
2285 	case SIOCSIFFIB:
2286 		error = priv_check(td, PRIV_NET_SETIFFIB);
2287 		if (error)
2288 			return (error);
2289 		if (ifr->ifr_fib >= rt_numfibs)
2290 			return (EINVAL);
2291 
2292 		ifp->if_fib = ifr->ifr_fib;
2293 		break;
2294 
2295 	case SIOCSIFFLAGS:
2296 		error = priv_check(td, PRIV_NET_SETIFFLAGS);
2297 		if (error)
2298 			return (error);
2299 		/*
2300 		 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2301 		 * check, so we don't need special handling here yet.
2302 		 */
2303 		new_flags = (ifr->ifr_flags & 0xffff) |
2304 		    (ifr->ifr_flagshigh << 16);
2305 		if (ifp->if_flags & IFF_UP &&
2306 		    (new_flags & IFF_UP) == 0) {
2307 			if_down(ifp);
2308 		} else if (new_flags & IFF_UP &&
2309 		    (ifp->if_flags & IFF_UP) == 0) {
2310 			if_up(ifp);
2311 		}
2312 		/* See if permanently promiscuous mode bit is about to flip */
2313 		if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2314 			if (new_flags & IFF_PPROMISC)
2315 				ifp->if_flags |= IFF_PROMISC;
2316 			else if (ifp->if_pcount == 0)
2317 				ifp->if_flags &= ~IFF_PROMISC;
2318 			log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2319 			    ifp->if_xname,
2320 			    (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2321 		}
2322 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2323 			(new_flags &~ IFF_CANTCHANGE);
2324 		if (ifp->if_ioctl) {
2325 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
2326 		}
2327 		getmicrotime(&ifp->if_lastchange);
2328 		break;
2329 
2330 	case SIOCSIFCAP:
2331 		error = priv_check(td, PRIV_NET_SETIFCAP);
2332 		if (error)
2333 			return (error);
2334 		if (ifp->if_ioctl == NULL)
2335 			return (EOPNOTSUPP);
2336 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2337 			return (EINVAL);
2338 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2339 		if (error == 0)
2340 			getmicrotime(&ifp->if_lastchange);
2341 		break;
2342 
2343 #ifdef MAC
2344 	case SIOCSIFMAC:
2345 		error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2346 		break;
2347 #endif
2348 
2349 	case SIOCSIFNAME:
2350 		error = priv_check(td, PRIV_NET_SETIFNAME);
2351 		if (error)
2352 			return (error);
2353 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2354 		if (error != 0)
2355 			return (error);
2356 		if (new_name[0] == '\0')
2357 			return (EINVAL);
2358 		if (ifunit(new_name) != NULL)
2359 			return (EEXIST);
2360 
2361 		/*
2362 		 * XXX: Locking.  Nothing else seems to lock if_flags,
2363 		 * and there are numerous other races with the
2364 		 * ifunit() checks not being atomic with namespace
2365 		 * changes (renames, vmoves, if_attach, etc).
2366 		 */
2367 		ifp->if_flags |= IFF_RENAMING;
2368 
2369 		/* Announce the departure of the interface. */
2370 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2371 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2372 
2373 		log(LOG_INFO, "%s: changing name to '%s'\n",
2374 		    ifp->if_xname, new_name);
2375 
2376 		IF_ADDR_WLOCK(ifp);
2377 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2378 		ifa = ifp->if_addr;
2379 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2380 		namelen = strlen(new_name);
2381 		onamelen = sdl->sdl_nlen;
2382 		/*
2383 		 * Move the address if needed.  This is safe because we
2384 		 * allocate space for a name of length IFNAMSIZ when we
2385 		 * create this in if_attach().
2386 		 */
2387 		if (namelen != onamelen) {
2388 			bcopy(sdl->sdl_data + onamelen,
2389 			    sdl->sdl_data + namelen, sdl->sdl_alen);
2390 		}
2391 		bcopy(new_name, sdl->sdl_data, namelen);
2392 		sdl->sdl_nlen = namelen;
2393 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2394 		bzero(sdl->sdl_data, onamelen);
2395 		while (namelen != 0)
2396 			sdl->sdl_data[--namelen] = 0xff;
2397 		IF_ADDR_WUNLOCK(ifp);
2398 
2399 		EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2400 		/* Announce the return of the interface. */
2401 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2402 
2403 		ifp->if_flags &= ~IFF_RENAMING;
2404 		break;
2405 
2406 #ifdef VIMAGE
2407 	case SIOCSIFVNET:
2408 		error = priv_check(td, PRIV_NET_SETIFVNET);
2409 		if (error)
2410 			return (error);
2411 		error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2412 		break;
2413 #endif
2414 
2415 	case SIOCSIFMETRIC:
2416 		error = priv_check(td, PRIV_NET_SETIFMETRIC);
2417 		if (error)
2418 			return (error);
2419 		ifp->if_metric = ifr->ifr_metric;
2420 		getmicrotime(&ifp->if_lastchange);
2421 		break;
2422 
2423 	case SIOCSIFPHYS:
2424 		error = priv_check(td, PRIV_NET_SETIFPHYS);
2425 		if (error)
2426 			return (error);
2427 		if (ifp->if_ioctl == NULL)
2428 			return (EOPNOTSUPP);
2429 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2430 		if (error == 0)
2431 			getmicrotime(&ifp->if_lastchange);
2432 		break;
2433 
2434 	case SIOCSIFMTU:
2435 	{
2436 		u_long oldmtu = ifp->if_mtu;
2437 
2438 		error = priv_check(td, PRIV_NET_SETIFMTU);
2439 		if (error)
2440 			return (error);
2441 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2442 			return (EINVAL);
2443 		if (ifp->if_ioctl == NULL)
2444 			return (EOPNOTSUPP);
2445 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2446 		if (error == 0) {
2447 			getmicrotime(&ifp->if_lastchange);
2448 			rt_ifmsg(ifp);
2449 		}
2450 		/*
2451 		 * If the link MTU changed, do network layer specific procedure.
2452 		 */
2453 		if (ifp->if_mtu != oldmtu) {
2454 #ifdef INET6
2455 			nd6_setmtu(ifp);
2456 #endif
2457 			rt_updatemtu(ifp);
2458 		}
2459 		break;
2460 	}
2461 
2462 	case SIOCADDMULTI:
2463 	case SIOCDELMULTI:
2464 		if (cmd == SIOCADDMULTI)
2465 			error = priv_check(td, PRIV_NET_ADDMULTI);
2466 		else
2467 			error = priv_check(td, PRIV_NET_DELMULTI);
2468 		if (error)
2469 			return (error);
2470 
2471 		/* Don't allow group membership on non-multicast interfaces. */
2472 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
2473 			return (EOPNOTSUPP);
2474 
2475 		/* Don't let users screw up protocols' entries. */
2476 		if (ifr->ifr_addr.sa_family != AF_LINK)
2477 			return (EINVAL);
2478 
2479 		if (cmd == SIOCADDMULTI) {
2480 			struct ifmultiaddr *ifma;
2481 
2482 			/*
2483 			 * Userland is only permitted to join groups once
2484 			 * via the if_addmulti() KPI, because it cannot hold
2485 			 * struct ifmultiaddr * between calls. It may also
2486 			 * lose a race while we check if the membership
2487 			 * already exists.
2488 			 */
2489 			IF_ADDR_RLOCK(ifp);
2490 			ifma = if_findmulti(ifp, &ifr->ifr_addr);
2491 			IF_ADDR_RUNLOCK(ifp);
2492 			if (ifma != NULL)
2493 				error = EADDRINUSE;
2494 			else
2495 				error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2496 		} else {
2497 			error = if_delmulti(ifp, &ifr->ifr_addr);
2498 		}
2499 		if (error == 0)
2500 			getmicrotime(&ifp->if_lastchange);
2501 		break;
2502 
2503 	case SIOCSIFPHYADDR:
2504 	case SIOCDIFPHYADDR:
2505 #ifdef INET6
2506 	case SIOCSIFPHYADDR_IN6:
2507 #endif
2508 	case SIOCSIFMEDIA:
2509 	case SIOCSIFGENERIC:
2510 		error = priv_check(td, PRIV_NET_HWIOCTL);
2511 		if (error)
2512 			return (error);
2513 		if (ifp->if_ioctl == NULL)
2514 			return (EOPNOTSUPP);
2515 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2516 		if (error == 0)
2517 			getmicrotime(&ifp->if_lastchange);
2518 		break;
2519 
2520 	case SIOCGIFSTATUS:
2521 	case SIOCGIFPSRCADDR:
2522 	case SIOCGIFPDSTADDR:
2523 	case SIOCGIFMEDIA:
2524 	case SIOCGIFXMEDIA:
2525 	case SIOCGIFGENERIC:
2526 		if (ifp->if_ioctl == NULL)
2527 			return (EOPNOTSUPP);
2528 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2529 		break;
2530 
2531 	case SIOCSIFLLADDR:
2532 		error = priv_check(td, PRIV_NET_SETLLADDR);
2533 		if (error)
2534 			return (error);
2535 		error = if_setlladdr(ifp,
2536 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2537 		break;
2538 
2539 	case SIOCAIFGROUP:
2540 	{
2541 		struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2542 
2543 		error = priv_check(td, PRIV_NET_ADDIFGROUP);
2544 		if (error)
2545 			return (error);
2546 		if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2547 			return (error);
2548 		break;
2549 	}
2550 
2551 	case SIOCGIFGROUP:
2552 		if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2553 			return (error);
2554 		break;
2555 
2556 	case SIOCDIFGROUP:
2557 	{
2558 		struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2559 
2560 		error = priv_check(td, PRIV_NET_DELIFGROUP);
2561 		if (error)
2562 			return (error);
2563 		if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2564 			return (error);
2565 		break;
2566 	}
2567 
2568 	default:
2569 		error = ENOIOCTL;
2570 		break;
2571 	}
2572 	return (error);
2573 }
2574 
2575 #ifdef COMPAT_FREEBSD32
2576 struct ifconf32 {
2577 	int32_t	ifc_len;
2578 	union {
2579 		uint32_t	ifcu_buf;
2580 		uint32_t	ifcu_req;
2581 	} ifc_ifcu;
2582 };
2583 #define	SIOCGIFCONF32	_IOWR('i', 36, struct ifconf32)
2584 #endif
2585 
2586 /*
2587  * Interface ioctls.
2588  */
2589 int
2590 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2591 {
2592 	struct ifnet *ifp;
2593 	struct ifreq *ifr;
2594 	int error;
2595 	int oif_flags;
2596 
2597 	CURVNET_SET(so->so_vnet);
2598 	switch (cmd) {
2599 	case SIOCGIFCONF:
2600 		error = ifconf(cmd, data);
2601 		CURVNET_RESTORE();
2602 		return (error);
2603 
2604 #ifdef COMPAT_FREEBSD32
2605 	case SIOCGIFCONF32:
2606 		{
2607 			struct ifconf32 *ifc32;
2608 			struct ifconf ifc;
2609 
2610 			ifc32 = (struct ifconf32 *)data;
2611 			ifc.ifc_len = ifc32->ifc_len;
2612 			ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2613 
2614 			error = ifconf(SIOCGIFCONF, (void *)&ifc);
2615 			CURVNET_RESTORE();
2616 			if (error == 0)
2617 				ifc32->ifc_len = ifc.ifc_len;
2618 			return (error);
2619 		}
2620 #endif
2621 	}
2622 	ifr = (struct ifreq *)data;
2623 
2624 	switch (cmd) {
2625 #ifdef VIMAGE
2626 	case SIOCSIFRVNET:
2627 		error = priv_check(td, PRIV_NET_SETIFVNET);
2628 		if (error == 0)
2629 			error = if_vmove_reclaim(td, ifr->ifr_name,
2630 			    ifr->ifr_jid);
2631 		CURVNET_RESTORE();
2632 		return (error);
2633 #endif
2634 	case SIOCIFCREATE:
2635 	case SIOCIFCREATE2:
2636 		error = priv_check(td, PRIV_NET_IFCREATE);
2637 		if (error == 0)
2638 			error = if_clone_create(ifr->ifr_name,
2639 			    sizeof(ifr->ifr_name),
2640 			    cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2641 		CURVNET_RESTORE();
2642 		return (error);
2643 	case SIOCIFDESTROY:
2644 		error = priv_check(td, PRIV_NET_IFDESTROY);
2645 		if (error == 0)
2646 			error = if_clone_destroy(ifr->ifr_name);
2647 		CURVNET_RESTORE();
2648 		return (error);
2649 
2650 	case SIOCIFGCLONERS:
2651 		error = if_clone_list((struct if_clonereq *)data);
2652 		CURVNET_RESTORE();
2653 		return (error);
2654 	case SIOCGIFGMEMB:
2655 		error = if_getgroupmembers((struct ifgroupreq *)data);
2656 		CURVNET_RESTORE();
2657 		return (error);
2658 #if defined(INET) || defined(INET6)
2659 	case SIOCSVH:
2660 	case SIOCGVH:
2661 		if (carp_ioctl_p == NULL)
2662 			error = EPROTONOSUPPORT;
2663 		else
2664 			error = (*carp_ioctl_p)(ifr, cmd, td);
2665 		CURVNET_RESTORE();
2666 		return (error);
2667 #endif
2668 	}
2669 
2670 	ifp = ifunit_ref(ifr->ifr_name);
2671 	if (ifp == NULL) {
2672 		CURVNET_RESTORE();
2673 		return (ENXIO);
2674 	}
2675 
2676 	error = ifhwioctl(cmd, ifp, data, td);
2677 	if (error != ENOIOCTL) {
2678 		if_rele(ifp);
2679 		CURVNET_RESTORE();
2680 		return (error);
2681 	}
2682 
2683 	oif_flags = ifp->if_flags;
2684 	if (so->so_proto == NULL) {
2685 		if_rele(ifp);
2686 		CURVNET_RESTORE();
2687 		return (EOPNOTSUPP);
2688 	}
2689 
2690 	/*
2691 	 * Pass the request on to the socket control method, and if the
2692 	 * latter returns EOPNOTSUPP, directly to the interface.
2693 	 *
2694 	 * Make an exception for the legacy SIOCSIF* requests.  Drivers
2695 	 * trust SIOCSIFADDR et al to come from an already privileged
2696 	 * layer, and do not perform any credentials checks or input
2697 	 * validation.
2698 	 */
2699 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2700 	    ifp, td));
2701 	if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2702 	    cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2703 	    cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2704 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2705 
2706 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2707 #ifdef INET6
2708 		if (ifp->if_flags & IFF_UP)
2709 			in6_if_up(ifp);
2710 #endif
2711 	}
2712 	if_rele(ifp);
2713 	CURVNET_RESTORE();
2714 	return (error);
2715 }
2716 
2717 /*
2718  * The code common to handling reference counted flags,
2719  * e.g., in ifpromisc() and if_allmulti().
2720  * The "pflag" argument can specify a permanent mode flag to check,
2721  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2722  *
2723  * Only to be used on stack-owned flags, not driver-owned flags.
2724  */
2725 static int
2726 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2727 {
2728 	struct ifreq ifr;
2729 	int error;
2730 	int oldflags, oldcount;
2731 
2732 	/* Sanity checks to catch programming errors */
2733 	KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2734 	    ("%s: setting driver-owned flag %d", __func__, flag));
2735 
2736 	if (onswitch)
2737 		KASSERT(*refcount >= 0,
2738 		    ("%s: increment negative refcount %d for flag %d",
2739 		    __func__, *refcount, flag));
2740 	else
2741 		KASSERT(*refcount > 0,
2742 		    ("%s: decrement non-positive refcount %d for flag %d",
2743 		    __func__, *refcount, flag));
2744 
2745 	/* In case this mode is permanent, just touch refcount */
2746 	if (ifp->if_flags & pflag) {
2747 		*refcount += onswitch ? 1 : -1;
2748 		return (0);
2749 	}
2750 
2751 	/* Save ifnet parameters for if_ioctl() may fail */
2752 	oldcount = *refcount;
2753 	oldflags = ifp->if_flags;
2754 
2755 	/*
2756 	 * See if we aren't the only and touching refcount is enough.
2757 	 * Actually toggle interface flag if we are the first or last.
2758 	 */
2759 	if (onswitch) {
2760 		if ((*refcount)++)
2761 			return (0);
2762 		ifp->if_flags |= flag;
2763 	} else {
2764 		if (--(*refcount))
2765 			return (0);
2766 		ifp->if_flags &= ~flag;
2767 	}
2768 
2769 	/* Call down the driver since we've changed interface flags */
2770 	if (ifp->if_ioctl == NULL) {
2771 		error = EOPNOTSUPP;
2772 		goto recover;
2773 	}
2774 	ifr.ifr_flags = ifp->if_flags & 0xffff;
2775 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
2776 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2777 	if (error)
2778 		goto recover;
2779 	/* Notify userland that interface flags have changed */
2780 	rt_ifmsg(ifp);
2781 	return (0);
2782 
2783 recover:
2784 	/* Recover after driver error */
2785 	*refcount = oldcount;
2786 	ifp->if_flags = oldflags;
2787 	return (error);
2788 }
2789 
2790 /*
2791  * Set/clear promiscuous mode on interface ifp based on the truth value
2792  * of pswitch.  The calls are reference counted so that only the first
2793  * "on" request actually has an effect, as does the final "off" request.
2794  * Results are undefined if the "off" and "on" requests are not matched.
2795  */
2796 int
2797 ifpromisc(struct ifnet *ifp, int pswitch)
2798 {
2799 	int error;
2800 	int oldflags = ifp->if_flags;
2801 
2802 	error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2803 			   &ifp->if_pcount, pswitch);
2804 	/* If promiscuous mode status has changed, log a message */
2805 	if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2806 		log(LOG_INFO, "%s: promiscuous mode %s\n",
2807 		    ifp->if_xname,
2808 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2809 	return (error);
2810 }
2811 
2812 /*
2813  * Return interface configuration
2814  * of system.  List may be used
2815  * in later ioctl's (above) to get
2816  * other information.
2817  */
2818 /*ARGSUSED*/
2819 static int
2820 ifconf(u_long cmd, caddr_t data)
2821 {
2822 	struct ifconf *ifc = (struct ifconf *)data;
2823 	struct ifnet *ifp;
2824 	struct ifaddr *ifa;
2825 	struct ifreq ifr;
2826 	struct sbuf *sb;
2827 	int error, full = 0, valid_len, max_len;
2828 
2829 	/* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2830 	max_len = MAXPHYS - 1;
2831 
2832 	/* Prevent hostile input from being able to crash the system */
2833 	if (ifc->ifc_len <= 0)
2834 		return (EINVAL);
2835 
2836 again:
2837 	if (ifc->ifc_len <= max_len) {
2838 		max_len = ifc->ifc_len;
2839 		full = 1;
2840 	}
2841 	sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2842 	max_len = 0;
2843 	valid_len = 0;
2844 
2845 	IFNET_RLOCK();
2846 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2847 		int addrs;
2848 
2849 		/*
2850 		 * Zero the ifr_name buffer to make sure we don't
2851 		 * disclose the contents of the stack.
2852 		 */
2853 		memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2854 
2855 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2856 		    >= sizeof(ifr.ifr_name)) {
2857 			sbuf_delete(sb);
2858 			IFNET_RUNLOCK();
2859 			return (ENAMETOOLONG);
2860 		}
2861 
2862 		addrs = 0;
2863 		IF_ADDR_RLOCK(ifp);
2864 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2865 			struct sockaddr *sa = ifa->ifa_addr;
2866 
2867 			if (prison_if(curthread->td_ucred, sa) != 0)
2868 				continue;
2869 			addrs++;
2870 			if (sa->sa_len <= sizeof(*sa)) {
2871 				ifr.ifr_addr = *sa;
2872 				sbuf_bcat(sb, &ifr, sizeof(ifr));
2873 				max_len += sizeof(ifr);
2874 			} else {
2875 				sbuf_bcat(sb, &ifr,
2876 				    offsetof(struct ifreq, ifr_addr));
2877 				max_len += offsetof(struct ifreq, ifr_addr);
2878 				sbuf_bcat(sb, sa, sa->sa_len);
2879 				max_len += sa->sa_len;
2880 			}
2881 
2882 			if (sbuf_error(sb) == 0)
2883 				valid_len = sbuf_len(sb);
2884 		}
2885 		IF_ADDR_RUNLOCK(ifp);
2886 		if (addrs == 0) {
2887 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2888 			sbuf_bcat(sb, &ifr, sizeof(ifr));
2889 			max_len += sizeof(ifr);
2890 
2891 			if (sbuf_error(sb) == 0)
2892 				valid_len = sbuf_len(sb);
2893 		}
2894 	}
2895 	IFNET_RUNLOCK();
2896 
2897 	/*
2898 	 * If we didn't allocate enough space (uncommon), try again.  If
2899 	 * we have already allocated as much space as we are allowed,
2900 	 * return what we've got.
2901 	 */
2902 	if (valid_len != max_len && !full) {
2903 		sbuf_delete(sb);
2904 		goto again;
2905 	}
2906 
2907 	ifc->ifc_len = valid_len;
2908 	sbuf_finish(sb);
2909 	error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2910 	sbuf_delete(sb);
2911 	return (error);
2912 }
2913 
2914 /*
2915  * Just like ifpromisc(), but for all-multicast-reception mode.
2916  */
2917 int
2918 if_allmulti(struct ifnet *ifp, int onswitch)
2919 {
2920 
2921 	return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2922 }
2923 
2924 struct ifmultiaddr *
2925 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
2926 {
2927 	struct ifmultiaddr *ifma;
2928 
2929 	IF_ADDR_LOCK_ASSERT(ifp);
2930 
2931 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2932 		if (sa->sa_family == AF_LINK) {
2933 			if (sa_dl_equal(ifma->ifma_addr, sa))
2934 				break;
2935 		} else {
2936 			if (sa_equal(ifma->ifma_addr, sa))
2937 				break;
2938 		}
2939 	}
2940 
2941 	return ifma;
2942 }
2943 
2944 /*
2945  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
2946  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
2947  * the ifnet multicast address list here, so the caller must do that and
2948  * other setup work (such as notifying the device driver).  The reference
2949  * count is initialized to 1.
2950  */
2951 static struct ifmultiaddr *
2952 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2953     int mflags)
2954 {
2955 	struct ifmultiaddr *ifma;
2956 	struct sockaddr *dupsa;
2957 
2958 	ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2959 	    M_ZERO);
2960 	if (ifma == NULL)
2961 		return (NULL);
2962 
2963 	dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2964 	if (dupsa == NULL) {
2965 		free(ifma, M_IFMADDR);
2966 		return (NULL);
2967 	}
2968 	bcopy(sa, dupsa, sa->sa_len);
2969 	ifma->ifma_addr = dupsa;
2970 
2971 	ifma->ifma_ifp = ifp;
2972 	ifma->ifma_refcount = 1;
2973 	ifma->ifma_protospec = NULL;
2974 
2975 	if (llsa == NULL) {
2976 		ifma->ifma_lladdr = NULL;
2977 		return (ifma);
2978 	}
2979 
2980 	dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2981 	if (dupsa == NULL) {
2982 		free(ifma->ifma_addr, M_IFMADDR);
2983 		free(ifma, M_IFMADDR);
2984 		return (NULL);
2985 	}
2986 	bcopy(llsa, dupsa, llsa->sa_len);
2987 	ifma->ifma_lladdr = dupsa;
2988 
2989 	return (ifma);
2990 }
2991 
2992 /*
2993  * if_freemulti: free ifmultiaddr structure and possibly attached related
2994  * addresses.  The caller is responsible for implementing reference
2995  * counting, notifying the driver, handling routing messages, and releasing
2996  * any dependent link layer state.
2997  */
2998 static void
2999 if_freemulti(struct ifmultiaddr *ifma)
3000 {
3001 
3002 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3003 	    ifma->ifma_refcount));
3004 
3005 	if (ifma->ifma_lladdr != NULL)
3006 		free(ifma->ifma_lladdr, M_IFMADDR);
3007 	free(ifma->ifma_addr, M_IFMADDR);
3008 	free(ifma, M_IFMADDR);
3009 }
3010 
3011 /*
3012  * Register an additional multicast address with a network interface.
3013  *
3014  * - If the address is already present, bump the reference count on the
3015  *   address and return.
3016  * - If the address is not link-layer, look up a link layer address.
3017  * - Allocate address structures for one or both addresses, and attach to the
3018  *   multicast address list on the interface.  If automatically adding a link
3019  *   layer address, the protocol address will own a reference to the link
3020  *   layer address, to be freed when it is freed.
3021  * - Notify the network device driver of an addition to the multicast address
3022  *   list.
3023  *
3024  * 'sa' points to caller-owned memory with the desired multicast address.
3025  *
3026  * 'retifma' will be used to return a pointer to the resulting multicast
3027  * address reference, if desired.
3028  */
3029 int
3030 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3031     struct ifmultiaddr **retifma)
3032 {
3033 	struct ifmultiaddr *ifma, *ll_ifma;
3034 	struct sockaddr *llsa;
3035 	struct sockaddr_dl sdl;
3036 	int error;
3037 
3038 	/*
3039 	 * If the address is already present, return a new reference to it;
3040 	 * otherwise, allocate storage and set up a new address.
3041 	 */
3042 	IF_ADDR_WLOCK(ifp);
3043 	ifma = if_findmulti(ifp, sa);
3044 	if (ifma != NULL) {
3045 		ifma->ifma_refcount++;
3046 		if (retifma != NULL)
3047 			*retifma = ifma;
3048 		IF_ADDR_WUNLOCK(ifp);
3049 		return (0);
3050 	}
3051 
3052 	/*
3053 	 * The address isn't already present; resolve the protocol address
3054 	 * into a link layer address, and then look that up, bump its
3055 	 * refcount or allocate an ifma for that also.
3056 	 * Most link layer resolving functions returns address data which
3057 	 * fits inside default sockaddr_dl structure. However callback
3058 	 * can allocate another sockaddr structure, in that case we need to
3059 	 * free it later.
3060 	 */
3061 	llsa = NULL;
3062 	ll_ifma = NULL;
3063 	if (ifp->if_resolvemulti != NULL) {
3064 		/* Provide called function with buffer size information */
3065 		sdl.sdl_len = sizeof(sdl);
3066 		llsa = (struct sockaddr *)&sdl;
3067 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
3068 		if (error)
3069 			goto unlock_out;
3070 	}
3071 
3072 	/*
3073 	 * Allocate the new address.  Don't hook it up yet, as we may also
3074 	 * need to allocate a link layer multicast address.
3075 	 */
3076 	ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3077 	if (ifma == NULL) {
3078 		error = ENOMEM;
3079 		goto free_llsa_out;
3080 	}
3081 
3082 	/*
3083 	 * If a link layer address is found, we'll need to see if it's
3084 	 * already present in the address list, or allocate is as well.
3085 	 * When this block finishes, the link layer address will be on the
3086 	 * list.
3087 	 */
3088 	if (llsa != NULL) {
3089 		ll_ifma = if_findmulti(ifp, llsa);
3090 		if (ll_ifma == NULL) {
3091 			ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3092 			if (ll_ifma == NULL) {
3093 				--ifma->ifma_refcount;
3094 				if_freemulti(ifma);
3095 				error = ENOMEM;
3096 				goto free_llsa_out;
3097 			}
3098 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3099 			    ifma_link);
3100 		} else
3101 			ll_ifma->ifma_refcount++;
3102 		ifma->ifma_llifma = ll_ifma;
3103 	}
3104 
3105 	/*
3106 	 * We now have a new multicast address, ifma, and possibly a new or
3107 	 * referenced link layer address.  Add the primary address to the
3108 	 * ifnet address list.
3109 	 */
3110 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3111 
3112 	if (retifma != NULL)
3113 		*retifma = ifma;
3114 
3115 	/*
3116 	 * Must generate the message while holding the lock so that 'ifma'
3117 	 * pointer is still valid.
3118 	 */
3119 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3120 	IF_ADDR_WUNLOCK(ifp);
3121 
3122 	/*
3123 	 * We are certain we have added something, so call down to the
3124 	 * interface to let them know about it.
3125 	 */
3126 	if (ifp->if_ioctl != NULL) {
3127 		(void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3128 	}
3129 
3130 	if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3131 		link_free_sdl(llsa);
3132 
3133 	return (0);
3134 
3135 free_llsa_out:
3136 	if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3137 		link_free_sdl(llsa);
3138 
3139 unlock_out:
3140 	IF_ADDR_WUNLOCK(ifp);
3141 	return (error);
3142 }
3143 
3144 /*
3145  * Delete a multicast group membership by network-layer group address.
3146  *
3147  * Returns ENOENT if the entry could not be found. If ifp no longer
3148  * exists, results are undefined. This entry point should only be used
3149  * from subsystems which do appropriate locking to hold ifp for the
3150  * duration of the call.
3151  * Network-layer protocol domains must use if_delmulti_ifma().
3152  */
3153 int
3154 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3155 {
3156 	struct ifmultiaddr *ifma;
3157 	int lastref;
3158 #ifdef INVARIANTS
3159 	struct ifnet *oifp;
3160 
3161 	IFNET_RLOCK_NOSLEEP();
3162 	TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3163 		if (ifp == oifp)
3164 			break;
3165 	if (ifp != oifp)
3166 		ifp = NULL;
3167 	IFNET_RUNLOCK_NOSLEEP();
3168 
3169 	KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3170 #endif
3171 	if (ifp == NULL)
3172 		return (ENOENT);
3173 
3174 	IF_ADDR_WLOCK(ifp);
3175 	lastref = 0;
3176 	ifma = if_findmulti(ifp, sa);
3177 	if (ifma != NULL)
3178 		lastref = if_delmulti_locked(ifp, ifma, 0);
3179 	IF_ADDR_WUNLOCK(ifp);
3180 
3181 	if (ifma == NULL)
3182 		return (ENOENT);
3183 
3184 	if (lastref && ifp->if_ioctl != NULL) {
3185 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3186 	}
3187 
3188 	return (0);
3189 }
3190 
3191 /*
3192  * Delete all multicast group membership for an interface.
3193  * Should be used to quickly flush all multicast filters.
3194  */
3195 void
3196 if_delallmulti(struct ifnet *ifp)
3197 {
3198 	struct ifmultiaddr *ifma;
3199 	struct ifmultiaddr *next;
3200 
3201 	IF_ADDR_WLOCK(ifp);
3202 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3203 		if_delmulti_locked(ifp, ifma, 0);
3204 	IF_ADDR_WUNLOCK(ifp);
3205 }
3206 
3207 /*
3208  * Delete a multicast group membership by group membership pointer.
3209  * Network-layer protocol domains must use this routine.
3210  *
3211  * It is safe to call this routine if the ifp disappeared.
3212  */
3213 void
3214 if_delmulti_ifma(struct ifmultiaddr *ifma)
3215 {
3216 	struct ifnet *ifp;
3217 	int lastref;
3218 
3219 	ifp = ifma->ifma_ifp;
3220 #ifdef DIAGNOSTIC
3221 	if (ifp == NULL) {
3222 		printf("%s: ifma_ifp seems to be detached\n", __func__);
3223 	} else {
3224 		struct ifnet *oifp;
3225 
3226 		IFNET_RLOCK_NOSLEEP();
3227 		TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3228 			if (ifp == oifp)
3229 				break;
3230 		if (ifp != oifp) {
3231 			printf("%s: ifnet %p disappeared\n", __func__, ifp);
3232 			ifp = NULL;
3233 		}
3234 		IFNET_RUNLOCK_NOSLEEP();
3235 	}
3236 #endif
3237 	/*
3238 	 * If and only if the ifnet instance exists: Acquire the address lock.
3239 	 */
3240 	if (ifp != NULL)
3241 		IF_ADDR_WLOCK(ifp);
3242 
3243 	lastref = if_delmulti_locked(ifp, ifma, 0);
3244 
3245 	if (ifp != NULL) {
3246 		/*
3247 		 * If and only if the ifnet instance exists:
3248 		 *  Release the address lock.
3249 		 *  If the group was left: update the hardware hash filter.
3250 		 */
3251 		IF_ADDR_WUNLOCK(ifp);
3252 		if (lastref && ifp->if_ioctl != NULL) {
3253 			(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3254 		}
3255 	}
3256 }
3257 
3258 /*
3259  * Perform deletion of network-layer and/or link-layer multicast address.
3260  *
3261  * Return 0 if the reference count was decremented.
3262  * Return 1 if the final reference was released, indicating that the
3263  * hardware hash filter should be reprogrammed.
3264  */
3265 static int
3266 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3267 {
3268 	struct ifmultiaddr *ll_ifma;
3269 
3270 	if (ifp != NULL && ifma->ifma_ifp != NULL) {
3271 		KASSERT(ifma->ifma_ifp == ifp,
3272 		    ("%s: inconsistent ifp %p", __func__, ifp));
3273 		IF_ADDR_WLOCK_ASSERT(ifp);
3274 	}
3275 
3276 	ifp = ifma->ifma_ifp;
3277 
3278 	/*
3279 	 * If the ifnet is detaching, null out references to ifnet,
3280 	 * so that upper protocol layers will notice, and not attempt
3281 	 * to obtain locks for an ifnet which no longer exists. The
3282 	 * routing socket announcement must happen before the ifnet
3283 	 * instance is detached from the system.
3284 	 */
3285 	if (detaching) {
3286 #ifdef DIAGNOSTIC
3287 		printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3288 #endif
3289 		/*
3290 		 * ifp may already be nulled out if we are being reentered
3291 		 * to delete the ll_ifma.
3292 		 */
3293 		if (ifp != NULL) {
3294 			rt_newmaddrmsg(RTM_DELMADDR, ifma);
3295 			ifma->ifma_ifp = NULL;
3296 		}
3297 	}
3298 
3299 	if (--ifma->ifma_refcount > 0)
3300 		return 0;
3301 
3302 	/*
3303 	 * If this ifma is a network-layer ifma, a link-layer ifma may
3304 	 * have been associated with it. Release it first if so.
3305 	 */
3306 	ll_ifma = ifma->ifma_llifma;
3307 	if (ll_ifma != NULL) {
3308 		KASSERT(ifma->ifma_lladdr != NULL,
3309 		    ("%s: llifma w/o lladdr", __func__));
3310 		if (detaching)
3311 			ll_ifma->ifma_ifp = NULL;	/* XXX */
3312 		if (--ll_ifma->ifma_refcount == 0) {
3313 			if (ifp != NULL) {
3314 				TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3315 				    ifma_link);
3316 			}
3317 			if_freemulti(ll_ifma);
3318 		}
3319 	}
3320 
3321 	if (ifp != NULL)
3322 		TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3323 
3324 	if_freemulti(ifma);
3325 
3326 	/*
3327 	 * The last reference to this instance of struct ifmultiaddr
3328 	 * was released; the hardware should be notified of this change.
3329 	 */
3330 	return 1;
3331 }
3332 
3333 /*
3334  * Set the link layer address on an interface.
3335  *
3336  * At this time we only support certain types of interfaces,
3337  * and we don't allow the length of the address to change.
3338  *
3339  * Set noinline to be dtrace-friendly
3340  */
3341 __noinline int
3342 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3343 {
3344 	struct sockaddr_dl *sdl;
3345 	struct ifaddr *ifa;
3346 	struct ifreq ifr;
3347 
3348 	IF_ADDR_RLOCK(ifp);
3349 	ifa = ifp->if_addr;
3350 	if (ifa == NULL) {
3351 		IF_ADDR_RUNLOCK(ifp);
3352 		return (EINVAL);
3353 	}
3354 	ifa_ref(ifa);
3355 	IF_ADDR_RUNLOCK(ifp);
3356 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3357 	if (sdl == NULL) {
3358 		ifa_free(ifa);
3359 		return (EINVAL);
3360 	}
3361 	if (len != sdl->sdl_alen) {	/* don't allow length to change */
3362 		ifa_free(ifa);
3363 		return (EINVAL);
3364 	}
3365 	switch (ifp->if_type) {
3366 	case IFT_ETHER:
3367 	case IFT_FDDI:
3368 	case IFT_XETHER:
3369 	case IFT_ISO88025:
3370 	case IFT_L2VLAN:
3371 	case IFT_BRIDGE:
3372 	case IFT_ARCNET:
3373 	case IFT_IEEE8023ADLAG:
3374 	case IFT_IEEE80211:
3375 		bcopy(lladdr, LLADDR(sdl), len);
3376 		ifa_free(ifa);
3377 		break;
3378 	default:
3379 		ifa_free(ifa);
3380 		return (ENODEV);
3381 	}
3382 
3383 	/*
3384 	 * If the interface is already up, we need
3385 	 * to re-init it in order to reprogram its
3386 	 * address filter.
3387 	 */
3388 	if ((ifp->if_flags & IFF_UP) != 0) {
3389 		if (ifp->if_ioctl) {
3390 			ifp->if_flags &= ~IFF_UP;
3391 			ifr.ifr_flags = ifp->if_flags & 0xffff;
3392 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
3393 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3394 			ifp->if_flags |= IFF_UP;
3395 			ifr.ifr_flags = ifp->if_flags & 0xffff;
3396 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
3397 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3398 		}
3399 	}
3400 	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3401 	return (0);
3402 }
3403 
3404 /*
3405  * Compat function for handling basic encapsulation requests.
3406  * Not converted stacks (FDDI, IB, ..) supports traditional
3407  * output model: ARP (and other similar L2 protocols) are handled
3408  * inside output routine, arpresolve/nd6_resolve() returns MAC
3409  * address instead of full prepend.
3410  *
3411  * This function creates calculated header==MAC for IPv4/IPv6 and
3412  * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3413  * address families.
3414  */
3415 static int
3416 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3417 {
3418 
3419 	if (req->rtype != IFENCAP_LL)
3420 		return (EOPNOTSUPP);
3421 
3422 	if (req->bufsize < req->lladdr_len)
3423 		return (ENOMEM);
3424 
3425 	switch (req->family) {
3426 	case AF_INET:
3427 	case AF_INET6:
3428 		break;
3429 	default:
3430 		return (EAFNOSUPPORT);
3431 	}
3432 
3433 	/* Copy lladdr to storage as is */
3434 	memmove(req->buf, req->lladdr, req->lladdr_len);
3435 	req->bufsize = req->lladdr_len;
3436 	req->lladdr_off = 0;
3437 
3438 	return (0);
3439 }
3440 
3441 /*
3442  * The name argument must be a pointer to storage which will last as
3443  * long as the interface does.  For physical devices, the result of
3444  * device_get_name(dev) is a good choice and for pseudo-devices a
3445  * static string works well.
3446  */
3447 void
3448 if_initname(struct ifnet *ifp, const char *name, int unit)
3449 {
3450 	ifp->if_dname = name;
3451 	ifp->if_dunit = unit;
3452 	if (unit != IF_DUNIT_NONE)
3453 		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3454 	else
3455 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
3456 }
3457 
3458 int
3459 if_printf(struct ifnet *ifp, const char * fmt, ...)
3460 {
3461 	va_list ap;
3462 	int retval;
3463 
3464 	retval = printf("%s: ", ifp->if_xname);
3465 	va_start(ap, fmt);
3466 	retval += vprintf(fmt, ap);
3467 	va_end(ap);
3468 	return (retval);
3469 }
3470 
3471 void
3472 if_start(struct ifnet *ifp)
3473 {
3474 
3475 	(*(ifp)->if_start)(ifp);
3476 }
3477 
3478 /*
3479  * Backwards compatibility interface for drivers
3480  * that have not implemented it
3481  */
3482 static int
3483 if_transmit(struct ifnet *ifp, struct mbuf *m)
3484 {
3485 	int error;
3486 
3487 	IFQ_HANDOFF(ifp, m, error);
3488 	return (error);
3489 }
3490 
3491 static void
3492 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3493 {
3494 
3495 	m_freem(m);
3496 }
3497 
3498 int
3499 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3500 {
3501 	int active = 0;
3502 
3503 	IF_LOCK(ifq);
3504 	if (_IF_QFULL(ifq)) {
3505 		IF_UNLOCK(ifq);
3506 		if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3507 		m_freem(m);
3508 		return (0);
3509 	}
3510 	if (ifp != NULL) {
3511 		if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3512 		if (m->m_flags & (M_BCAST|M_MCAST))
3513 			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3514 		active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3515 	}
3516 	_IF_ENQUEUE(ifq, m);
3517 	IF_UNLOCK(ifq);
3518 	if (ifp != NULL && !active)
3519 		(*(ifp)->if_start)(ifp);
3520 	return (1);
3521 }
3522 
3523 void
3524 if_register_com_alloc(u_char type,
3525     if_com_alloc_t *a, if_com_free_t *f)
3526 {
3527 
3528 	KASSERT(if_com_alloc[type] == NULL,
3529 	    ("if_register_com_alloc: %d already registered", type));
3530 	KASSERT(if_com_free[type] == NULL,
3531 	    ("if_register_com_alloc: %d free already registered", type));
3532 
3533 	if_com_alloc[type] = a;
3534 	if_com_free[type] = f;
3535 }
3536 
3537 void
3538 if_deregister_com_alloc(u_char type)
3539 {
3540 
3541 	KASSERT(if_com_alloc[type] != NULL,
3542 	    ("if_deregister_com_alloc: %d not registered", type));
3543 	KASSERT(if_com_free[type] != NULL,
3544 	    ("if_deregister_com_alloc: %d free not registered", type));
3545 	if_com_alloc[type] = NULL;
3546 	if_com_free[type] = NULL;
3547 }
3548 
3549 /* API for driver access to network stack owned ifnet.*/
3550 uint64_t
3551 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3552 {
3553 	uint64_t oldbrate;
3554 
3555 	oldbrate = ifp->if_baudrate;
3556 	ifp->if_baudrate = baudrate;
3557 	return (oldbrate);
3558 }
3559 
3560 uint64_t
3561 if_getbaudrate(if_t ifp)
3562 {
3563 
3564 	return (((struct ifnet *)ifp)->if_baudrate);
3565 }
3566 
3567 int
3568 if_setcapabilities(if_t ifp, int capabilities)
3569 {
3570 	((struct ifnet *)ifp)->if_capabilities = capabilities;
3571 	return (0);
3572 }
3573 
3574 int
3575 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3576 {
3577 	((struct ifnet *)ifp)->if_capabilities |= setbit;
3578 	((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3579 
3580 	return (0);
3581 }
3582 
3583 int
3584 if_getcapabilities(if_t ifp)
3585 {
3586 	return ((struct ifnet *)ifp)->if_capabilities;
3587 }
3588 
3589 int
3590 if_setcapenable(if_t ifp, int capabilities)
3591 {
3592 	((struct ifnet *)ifp)->if_capenable = capabilities;
3593 	return (0);
3594 }
3595 
3596 int
3597 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3598 {
3599 	if(setcap)
3600 		((struct ifnet *)ifp)->if_capenable |= setcap;
3601 	if(clearcap)
3602 		((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3603 
3604 	return (0);
3605 }
3606 
3607 const char *
3608 if_getdname(if_t ifp)
3609 {
3610 	return ((struct ifnet *)ifp)->if_dname;
3611 }
3612 
3613 int
3614 if_togglecapenable(if_t ifp, int togglecap)
3615 {
3616 	((struct ifnet *)ifp)->if_capenable ^= togglecap;
3617 	return (0);
3618 }
3619 
3620 int
3621 if_getcapenable(if_t ifp)
3622 {
3623 	return ((struct ifnet *)ifp)->if_capenable;
3624 }
3625 
3626 /*
3627  * This is largely undesirable because it ties ifnet to a device, but does
3628  * provide flexiblity for an embedded product vendor. Should be used with
3629  * the understanding that it violates the interface boundaries, and should be
3630  * a last resort only.
3631  */
3632 int
3633 if_setdev(if_t ifp, void *dev)
3634 {
3635 	return (0);
3636 }
3637 
3638 int
3639 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3640 {
3641 	((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3642 	((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3643 
3644 	return (0);
3645 }
3646 
3647 int
3648 if_getdrvflags(if_t ifp)
3649 {
3650 	return ((struct ifnet *)ifp)->if_drv_flags;
3651 }
3652 
3653 int
3654 if_setdrvflags(if_t ifp, int flags)
3655 {
3656 	((struct ifnet *)ifp)->if_drv_flags = flags;
3657 	return (0);
3658 }
3659 
3660 
3661 int
3662 if_setflags(if_t ifp, int flags)
3663 {
3664 	((struct ifnet *)ifp)->if_flags = flags;
3665 	return (0);
3666 }
3667 
3668 int
3669 if_setflagbits(if_t ifp, int set, int clear)
3670 {
3671 	((struct ifnet *)ifp)->if_flags |= set;
3672 	((struct ifnet *)ifp)->if_flags &= ~clear;
3673 
3674 	return (0);
3675 }
3676 
3677 int
3678 if_getflags(if_t ifp)
3679 {
3680 	return ((struct ifnet *)ifp)->if_flags;
3681 }
3682 
3683 int
3684 if_clearhwassist(if_t ifp)
3685 {
3686 	((struct ifnet *)ifp)->if_hwassist = 0;
3687 	return (0);
3688 }
3689 
3690 int
3691 if_sethwassistbits(if_t ifp, int toset, int toclear)
3692 {
3693 	((struct ifnet *)ifp)->if_hwassist |= toset;
3694 	((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3695 
3696 	return (0);
3697 }
3698 
3699 int
3700 if_sethwassist(if_t ifp, int hwassist_bit)
3701 {
3702 	((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3703 	return (0);
3704 }
3705 
3706 int
3707 if_gethwassist(if_t ifp)
3708 {
3709 	return ((struct ifnet *)ifp)->if_hwassist;
3710 }
3711 
3712 int
3713 if_setmtu(if_t ifp, int mtu)
3714 {
3715 	((struct ifnet *)ifp)->if_mtu = mtu;
3716 	return (0);
3717 }
3718 
3719 int
3720 if_getmtu(if_t ifp)
3721 {
3722 	return ((struct ifnet *)ifp)->if_mtu;
3723 }
3724 
3725 int
3726 if_getmtu_family(if_t ifp, int family)
3727 {
3728 	struct domain *dp;
3729 
3730 	for (dp = domains; dp; dp = dp->dom_next) {
3731 		if (dp->dom_family == family && dp->dom_ifmtu != NULL)
3732 			return (dp->dom_ifmtu((struct ifnet *)ifp));
3733 	}
3734 
3735 	return (((struct ifnet *)ifp)->if_mtu);
3736 }
3737 
3738 int
3739 if_setsoftc(if_t ifp, void *softc)
3740 {
3741 	((struct ifnet *)ifp)->if_softc = softc;
3742 	return (0);
3743 }
3744 
3745 void *
3746 if_getsoftc(if_t ifp)
3747 {
3748 	return ((struct ifnet *)ifp)->if_softc;
3749 }
3750 
3751 void
3752 if_setrcvif(struct mbuf *m, if_t ifp)
3753 {
3754 	m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3755 }
3756 
3757 void
3758 if_setvtag(struct mbuf *m, uint16_t tag)
3759 {
3760 	m->m_pkthdr.ether_vtag = tag;
3761 }
3762 
3763 uint16_t
3764 if_getvtag(struct mbuf *m)
3765 {
3766 
3767 	return (m->m_pkthdr.ether_vtag);
3768 }
3769 
3770 int
3771 if_sendq_empty(if_t ifp)
3772 {
3773 	return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3774 }
3775 
3776 struct ifaddr *
3777 if_getifaddr(if_t ifp)
3778 {
3779 	return ((struct ifnet *)ifp)->if_addr;
3780 }
3781 
3782 int
3783 if_getamcount(if_t ifp)
3784 {
3785 	return ((struct ifnet *)ifp)->if_amcount;
3786 }
3787 
3788 
3789 int
3790 if_setsendqready(if_t ifp)
3791 {
3792 	IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3793 	return (0);
3794 }
3795 
3796 int
3797 if_setsendqlen(if_t ifp, int tx_desc_count)
3798 {
3799 	IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3800 	((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3801 
3802 	return (0);
3803 }
3804 
3805 int
3806 if_vlantrunkinuse(if_t ifp)
3807 {
3808 	return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3809 }
3810 
3811 int
3812 if_input(if_t ifp, struct mbuf* sendmp)
3813 {
3814 	(*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3815 	return (0);
3816 
3817 }
3818 
3819 /* XXX */
3820 #ifndef ETH_ADDR_LEN
3821 #define ETH_ADDR_LEN 6
3822 #endif
3823 
3824 int
3825 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3826 {
3827 	struct ifmultiaddr *ifma;
3828 	uint8_t *lmta = (uint8_t *)mta;
3829 	int mcnt = 0;
3830 
3831 	TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3832 		if (ifma->ifma_addr->sa_family != AF_LINK)
3833 			continue;
3834 
3835 		if (mcnt == max)
3836 			break;
3837 
3838 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3839 		    &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
3840 		mcnt++;
3841 	}
3842 	*cnt = mcnt;
3843 
3844 	return (0);
3845 }
3846 
3847 int
3848 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
3849 {
3850 	int error;
3851 
3852 	if_maddr_rlock(ifp);
3853 	error = if_setupmultiaddr(ifp, mta, cnt, max);
3854 	if_maddr_runlock(ifp);
3855 	return (error);
3856 }
3857 
3858 int
3859 if_multiaddr_count(if_t ifp, int max)
3860 {
3861 	struct ifmultiaddr *ifma;
3862 	int count;
3863 
3864 	count = 0;
3865 	if_maddr_rlock(ifp);
3866 	TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3867 		if (ifma->ifma_addr->sa_family != AF_LINK)
3868 			continue;
3869 		count++;
3870 		if (count == max)
3871 			break;
3872 	}
3873 	if_maddr_runlock(ifp);
3874 	return (count);
3875 }
3876 
3877 struct mbuf *
3878 if_dequeue(if_t ifp)
3879 {
3880 	struct mbuf *m;
3881 	IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
3882 
3883 	return (m);
3884 }
3885 
3886 int
3887 if_sendq_prepend(if_t ifp, struct mbuf *m)
3888 {
3889 	IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
3890 	return (0);
3891 }
3892 
3893 int
3894 if_setifheaderlen(if_t ifp, int len)
3895 {
3896 	((struct ifnet *)ifp)->if_hdrlen = len;
3897 	return (0);
3898 }
3899 
3900 caddr_t
3901 if_getlladdr(if_t ifp)
3902 {
3903 	return (IF_LLADDR((struct ifnet *)ifp));
3904 }
3905 
3906 void *
3907 if_gethandle(u_char type)
3908 {
3909 	return (if_alloc(type));
3910 }
3911 
3912 void
3913 if_bpfmtap(if_t ifh, struct mbuf *m)
3914 {
3915 	struct ifnet *ifp = (struct ifnet *)ifh;
3916 
3917 	BPF_MTAP(ifp, m);
3918 }
3919 
3920 void
3921 if_etherbpfmtap(if_t ifh, struct mbuf *m)
3922 {
3923 	struct ifnet *ifp = (struct ifnet *)ifh;
3924 
3925 	ETHER_BPF_MTAP(ifp, m);
3926 }
3927 
3928 void
3929 if_vlancap(if_t ifh)
3930 {
3931 	struct ifnet *ifp = (struct ifnet *)ifh;
3932 	VLAN_CAPABILITIES(ifp);
3933 }
3934 
3935 void
3936 if_setinitfn(if_t ifp, void (*init_fn)(void *))
3937 {
3938 	((struct ifnet *)ifp)->if_init = init_fn;
3939 }
3940 
3941 void
3942 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
3943 {
3944 	((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
3945 }
3946 
3947 void
3948 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
3949 {
3950 	((struct ifnet *)ifp)->if_start = (void *)start_fn;
3951 }
3952 
3953 void
3954 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
3955 {
3956 	((struct ifnet *)ifp)->if_transmit = start_fn;
3957 }
3958 
3959 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
3960 {
3961 	((struct ifnet *)ifp)->if_qflush = flush_fn;
3962 
3963 }
3964 
3965 void
3966 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
3967 {
3968 
3969 	ifp->if_get_counter = fn;
3970 }
3971 
3972 /* Revisit these - These are inline functions originally. */
3973 int
3974 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
3975 {
3976 	return drbr_inuse(ifh, br);
3977 }
3978 
3979 struct mbuf*
3980 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
3981 {
3982 	return drbr_dequeue(ifh, br);
3983 }
3984 
3985 int
3986 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
3987 {
3988 	return drbr_needs_enqueue(ifh, br);
3989 }
3990 
3991 int
3992 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
3993 {
3994 	return drbr_enqueue(ifh, br, m);
3995 
3996 }
3997