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