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