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