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