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