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