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