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