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