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