xref: /freebsd/sys/net/if.c (revision 3fe92528afe8313fecf48822dde74bad5e380f48)
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_mac.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/mac.h>
43 #include <sys/malloc.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/mbuf.h>
47 #include <sys/systm.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/sockio.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 #include <sys/domain.h>
58 #include <sys/jail.h>
59 #include <machine/stdarg.h>
60 
61 #include <net/if.h>
62 #include <net/if_clone.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_var.h>
66 #include <net/radix.h>
67 #include <net/route.h>
68 
69 #if defined(INET) || defined(INET6)
70 /*XXX*/
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #ifdef INET6
74 #include <netinet6/in6_var.h>
75 #include <netinet6/in6_ifattach.h>
76 #endif
77 #endif
78 #ifdef INET
79 #include <netinet/if_ether.h>
80 #endif
81 #ifdef DEV_CARP
82 #include <netinet/ip_carp.h>
83 #endif
84 
85 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
86 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
87 
88 /* Log link state change events */
89 static int log_link_state_change = 1;
90 
91 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
92 	&log_link_state_change, 0,
93 	"log interface link state change events");
94 
95 void	(*bstp_linkstate_p)(struct ifnet *ifp, int state);
96 void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
97 
98 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
99 
100 static void	if_attachdomain(void *);
101 static void	if_attachdomain1(struct ifnet *);
102 static int	ifconf(u_long, caddr_t);
103 static void	if_grow(void);
104 static void	if_init(void *);
105 static void	if_check(void *);
106 static void	if_qflush(struct ifaltq *);
107 static void	if_route(struct ifnet *, int flag, int fam);
108 static int	if_setflag(struct ifnet *, int, int, int *, int);
109 static void	if_slowtimo(void *);
110 static void	if_unroute(struct ifnet *, int flag, int fam);
111 static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
112 static int	if_rtdel(struct radix_node *, void *);
113 static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
114 static void	if_start_deferred(void *context, int pending);
115 static void	do_link_state_change(void *, int);
116 static int	if_getgroup(struct ifgroupreq *, struct ifnet *);
117 static int	if_getgroupmembers(struct ifgroupreq *);
118 #ifdef INET6
119 /*
120  * XXX: declare here to avoid to include many inet6 related files..
121  * should be more generalized?
122  */
123 extern void	nd6_setmtu(struct ifnet *);
124 #endif
125 
126 int	if_index = 0;
127 struct	ifindex_entry *ifindex_table = NULL;
128 int	ifqmaxlen = IFQ_MAXLEN;
129 struct	ifnethead ifnet;	/* depend on static init XXX */
130 struct	ifgrouphead ifg_head;
131 struct	mtx ifnet_lock;
132 static	if_com_alloc_t *if_com_alloc[256];
133 static	if_com_free_t *if_com_free[256];
134 
135 static int	if_indexlim = 8;
136 static struct	knlist ifklist;
137 
138 static void	filt_netdetach(struct knote *kn);
139 static int	filt_netdev(struct knote *kn, long hint);
140 
141 static struct filterops netdev_filtops =
142     { 1, NULL, filt_netdetach, filt_netdev };
143 
144 /*
145  * System initialization
146  */
147 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
148 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
149 
150 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
151 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
152 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
153 
154 static d_open_t		netopen;
155 static d_close_t	netclose;
156 static d_ioctl_t	netioctl;
157 static d_kqfilter_t	netkqfilter;
158 
159 static struct cdevsw net_cdevsw = {
160 	.d_version =	D_VERSION,
161 	.d_flags =	D_NEEDGIANT,
162 	.d_open =	netopen,
163 	.d_close =	netclose,
164 	.d_ioctl =	netioctl,
165 	.d_name =	"net",
166 	.d_kqfilter =	netkqfilter,
167 };
168 
169 static int
170 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
171 {
172 	return (0);
173 }
174 
175 static int
176 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
177 {
178 	return (0);
179 }
180 
181 static int
182 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
183 {
184 	struct ifnet *ifp;
185 	int error, idx;
186 
187 	/* only support interface specific ioctls */
188 	if (IOCGROUP(cmd) != 'i')
189 		return (EOPNOTSUPP);
190 	idx = minor(dev);
191 	if (idx == 0) {
192 		/*
193 		 * special network device, not interface.
194 		 */
195 		if (cmd == SIOCGIFCONF)
196 			return (ifconf(cmd, data));	/* XXX remove cmd */
197 #ifdef __amd64__
198 		if (cmd == SIOCGIFCONF32)
199 			return (ifconf(cmd, data));	/* XXX remove cmd */
200 #endif
201 		return (EOPNOTSUPP);
202 	}
203 
204 	ifp = ifnet_byindex(idx);
205 	if (ifp == NULL)
206 		return (ENXIO);
207 
208 	error = ifhwioctl(cmd, ifp, data, td);
209 	if (error == ENOIOCTL)
210 		error = EOPNOTSUPP;
211 	return (error);
212 }
213 
214 static int
215 netkqfilter(struct cdev *dev, struct knote *kn)
216 {
217 	struct knlist *klist;
218 	struct ifnet *ifp;
219 	int idx;
220 
221 	switch (kn->kn_filter) {
222 	case EVFILT_NETDEV:
223 		kn->kn_fop = &netdev_filtops;
224 		break;
225 	default:
226 		return (EINVAL);
227 	}
228 
229 	idx = minor(dev);
230 	if (idx == 0) {
231 		klist = &ifklist;
232 	} else {
233 		ifp = ifnet_byindex(idx);
234 		if (ifp == NULL)
235 			return (1);
236 		klist = &ifp->if_klist;
237 	}
238 
239 	kn->kn_hook = (caddr_t)klist;
240 
241 	knlist_add(klist, kn, 0);
242 
243 	return (0);
244 }
245 
246 static void
247 filt_netdetach(struct knote *kn)
248 {
249 	struct knlist *klist = (struct knlist *)kn->kn_hook;
250 
251 	knlist_remove(klist, kn, 0);
252 }
253 
254 static int
255 filt_netdev(struct knote *kn, long hint)
256 {
257 	struct knlist *klist = (struct knlist *)kn->kn_hook;
258 
259 	/*
260 	 * Currently NOTE_EXIT is abused to indicate device detach.
261 	 */
262 	if (hint == NOTE_EXIT) {
263 		kn->kn_data = NOTE_LINKINV;
264 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
265 		knlist_remove_inevent(klist, kn);
266 		return (1);
267 	}
268 	if (hint != 0)
269 		kn->kn_data = hint;			/* current status */
270 	if (kn->kn_sfflags & hint)
271 		kn->kn_fflags |= hint;
272 	return (kn->kn_fflags != 0);
273 }
274 
275 /*
276  * Network interface utility routines.
277  *
278  * Routines with ifa_ifwith* names take sockaddr *'s as
279  * parameters.
280  */
281 /* ARGSUSED*/
282 static void
283 if_init(void *dummy __unused)
284 {
285 
286 	IFNET_LOCK_INIT();
287 	TAILQ_INIT(&ifnet);
288 	TAILQ_INIT(&ifg_head);
289 	knlist_init(&ifklist, NULL, NULL, NULL, NULL);
290 	if_grow();				/* create initial table */
291 	ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
292 	    UID_ROOT, GID_WHEEL, 0600, "network");
293 	if_clone_init();
294 }
295 
296 static void
297 if_grow(void)
298 {
299 	u_int n;
300 	struct ifindex_entry *e;
301 
302 	if_indexlim <<= 1;
303 	n = if_indexlim * sizeof(*e);
304 	e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
305 	if (ifindex_table != NULL) {
306 		memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
307 		free((caddr_t)ifindex_table, M_IFNET);
308 	}
309 	ifindex_table = e;
310 }
311 
312 /* ARGSUSED*/
313 static void
314 if_check(void *dummy __unused)
315 {
316 	struct ifnet *ifp;
317 	int s;
318 
319 	s = splimp();
320 	IFNET_RLOCK();	/* could sleep on rare error; mostly okay XXX */
321 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
322 		if (ifp->if_snd.ifq_maxlen == 0) {
323 			if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
324 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
325 		}
326 		if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
327 			if_printf(ifp,
328 			    "XXX: driver didn't initialize queue mtx\n");
329 			mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
330 			    MTX_NETWORK_LOCK, MTX_DEF);
331 		}
332 	}
333 	IFNET_RUNLOCK();
334 	splx(s);
335 	if_slowtimo(0);
336 }
337 
338 /*
339  * Allocate a struct ifnet and in index for an interface.
340  */
341 struct ifnet*
342 if_alloc(u_char type)
343 {
344 	struct ifnet *ifp;
345 
346 	ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
347 
348 	/*
349 	 * Try to find an empty slot below if_index.  If we fail, take
350 	 * the next slot.
351 	 *
352 	 * XXX: should be locked!
353 	 */
354 	for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
355 		if (ifnet_byindex(ifp->if_index) == NULL)
356 			break;
357 	}
358 	/* Catch if_index overflow. */
359 	if (ifp->if_index < 1) {
360 		free(ifp, M_IFNET);
361 		return (NULL);
362 	}
363 	if (ifp->if_index > if_index)
364 		if_index = ifp->if_index;
365 	if (if_index >= if_indexlim)
366 		if_grow();
367 	ifnet_byindex(ifp->if_index) = ifp;
368 
369 	ifp->if_type = type;
370 
371 	if (if_com_alloc[type] != NULL) {
372 		ifp->if_l2com = if_com_alloc[type](type, ifp);
373 		if (ifp->if_l2com == NULL) {
374 			free(ifp, M_IFNET);
375 			return (NULL);
376 		}
377 	}
378 	IF_ADDR_LOCK_INIT(ifp);
379 
380 	return (ifp);
381 }
382 
383 void
384 if_free(struct ifnet *ifp)
385 {
386 
387 	/* Do not add code to this function!  Add it to if_free_type(). */
388 	if_free_type(ifp, ifp->if_type);
389 }
390 
391 void
392 if_free_type(struct ifnet *ifp, u_char type)
393 {
394 
395 	if (ifp != ifnet_byindex(ifp->if_index)) {
396 		if_printf(ifp, "%s: value was not if_alloced, skipping\n",
397 		    __func__);
398 		return;
399 	}
400 
401 	IF_ADDR_LOCK_DESTROY(ifp);
402 
403 	ifnet_byindex(ifp->if_index) = NULL;
404 
405 	/* XXX: should be locked with if_findindex() */
406 	while (if_index > 0 && ifnet_byindex(if_index) == NULL)
407 		if_index--;
408 
409 	if (if_com_free[type] != NULL)
410 		if_com_free[type](ifp->if_l2com, type);
411 
412 	free(ifp, M_IFNET);
413 };
414 
415 /*
416  * Attach an interface to the
417  * list of "active" interfaces.
418  */
419 void
420 if_attach(struct ifnet *ifp)
421 {
422 	unsigned socksize, ifasize;
423 	int namelen, masklen;
424 	struct sockaddr_dl *sdl;
425 	struct ifaddr *ifa;
426 
427 	if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
428 		panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
429 		    ifp->if_xname);
430 
431 	TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
432 	TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
433 	IF_AFDATA_LOCK_INIT(ifp);
434 	ifp->if_afdata_initialized = 0;
435 	/*
436 	 * XXX -
437 	 * The old code would work if the interface passed a pre-existing
438 	 * chain of ifaddrs to this code.  We don't trust our callers to
439 	 * properly initialize the tailq, however, so we no longer allow
440 	 * this unlikely case.
441 	 */
442 	TAILQ_INIT(&ifp->if_addrhead);
443 	TAILQ_INIT(&ifp->if_prefixhead);
444 	TAILQ_INIT(&ifp->if_multiaddrs);
445 	TAILQ_INIT(&ifp->if_groups);
446 
447 	if_addgroup(ifp, IFG_ALL);
448 
449 	knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
450 	getmicrotime(&ifp->if_lastchange);
451 	ifp->if_data.ifi_epoch = time_uptime;
452 	ifp->if_data.ifi_datalen = sizeof(struct if_data);
453 
454 #ifdef MAC
455 	mac_init_ifnet(ifp);
456 	mac_create_ifnet(ifp);
457 #endif
458 
459 	ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
460 	    unit2minor(ifp->if_index),
461 	    UID_ROOT, GID_WHEEL, 0600, "%s/%s",
462 	    net_cdevsw.d_name, ifp->if_xname);
463 	make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
464 	    net_cdevsw.d_name, ifp->if_index);
465 
466 	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
467 
468 	/*
469 	 * create a Link Level name for this device
470 	 */
471 	namelen = strlen(ifp->if_xname);
472 	/*
473 	 * Always save enough space for any possiable name so we can do
474 	 * a rename in place later.
475 	 */
476 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
477 	socksize = masklen + ifp->if_addrlen;
478 	if (socksize < sizeof(*sdl))
479 		socksize = sizeof(*sdl);
480 	socksize = roundup2(socksize, sizeof(long));
481 	ifasize = sizeof(*ifa) + 2 * socksize;
482 	ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
483 	IFA_LOCK_INIT(ifa);
484 	sdl = (struct sockaddr_dl *)(ifa + 1);
485 	sdl->sdl_len = socksize;
486 	sdl->sdl_family = AF_LINK;
487 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
488 	sdl->sdl_nlen = namelen;
489 	sdl->sdl_index = ifp->if_index;
490 	sdl->sdl_type = ifp->if_type;
491 	ifp->if_addr = ifa;
492 	ifa->ifa_ifp = ifp;
493 	ifa->ifa_rtrequest = link_rtrequest;
494 	ifa->ifa_addr = (struct sockaddr *)sdl;
495 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
496 	ifa->ifa_netmask = (struct sockaddr *)sdl;
497 	sdl->sdl_len = masklen;
498 	while (namelen != 0)
499 		sdl->sdl_data[--namelen] = 0xff;
500 	ifa->ifa_refcnt = 1;
501 	TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
502 	ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
503 	ifp->if_snd.altq_type = 0;
504 	ifp->if_snd.altq_disc = NULL;
505 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
506 	ifp->if_snd.altq_tbr  = NULL;
507 	ifp->if_snd.altq_ifp  = ifp;
508 
509 	IFNET_WLOCK();
510 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
511 	IFNET_WUNLOCK();
512 
513 	if (domain_init_status >= 2)
514 		if_attachdomain1(ifp);
515 
516 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
517 	devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
518 
519 	/* Announce the interface. */
520 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
521 }
522 
523 static void
524 if_attachdomain(void *dummy)
525 {
526 	struct ifnet *ifp;
527 	int s;
528 
529 	s = splnet();
530 	TAILQ_FOREACH(ifp, &ifnet, if_link)
531 		if_attachdomain1(ifp);
532 	splx(s);
533 }
534 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
535     if_attachdomain, NULL);
536 
537 static void
538 if_attachdomain1(struct ifnet *ifp)
539 {
540 	struct domain *dp;
541 	int s;
542 
543 	s = splnet();
544 
545 	/*
546 	 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
547 	 * cannot lock ifp->if_afdata initialization, entirely.
548 	 */
549 	if (IF_AFDATA_TRYLOCK(ifp) == 0) {
550 		splx(s);
551 		return;
552 	}
553 	if (ifp->if_afdata_initialized >= domain_init_status) {
554 		IF_AFDATA_UNLOCK(ifp);
555 		splx(s);
556 		printf("if_attachdomain called more than once on %s\n",
557 		    ifp->if_xname);
558 		return;
559 	}
560 	ifp->if_afdata_initialized = domain_init_status;
561 	IF_AFDATA_UNLOCK(ifp);
562 
563 	/* address family dependent data region */
564 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
565 	for (dp = domains; dp; dp = dp->dom_next) {
566 		if (dp->dom_ifattach)
567 			ifp->if_afdata[dp->dom_family] =
568 			    (*dp->dom_ifattach)(ifp);
569 	}
570 
571 	splx(s);
572 }
573 
574 /*
575  * Remove any network addresses from an interface.
576  */
577 
578 void
579 if_purgeaddrs(struct ifnet *ifp)
580 {
581 	struct ifaddr *ifa, *next;
582 
583 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
584 		if (ifa->ifa_addr->sa_family == AF_LINK)
585 			continue;
586 #ifdef INET
587 		/* XXX: Ugly!! ad hoc just for INET */
588 		if (ifa->ifa_addr->sa_family == AF_INET) {
589 			struct ifaliasreq ifr;
590 
591 			bzero(&ifr, sizeof(ifr));
592 			ifr.ifra_addr = *ifa->ifa_addr;
593 			if (ifa->ifa_dstaddr)
594 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
595 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
596 			    NULL) == 0)
597 				continue;
598 		}
599 #endif /* INET */
600 #ifdef INET6
601 		if (ifa->ifa_addr->sa_family == AF_INET6) {
602 			in6_purgeaddr(ifa);
603 			/* ifp_addrhead is already updated */
604 			continue;
605 		}
606 #endif /* INET6 */
607 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
608 		IFAFREE(ifa);
609 	}
610 }
611 
612 /*
613  * Detach an interface, removing it from the
614  * list of "active" interfaces.
615  *
616  * XXXRW: There are some significant questions about event ordering, and
617  * how to prevent things from starting to use the interface during detach.
618  */
619 void
620 if_detach(struct ifnet *ifp)
621 {
622 	struct ifaddr *ifa;
623 	struct radix_node_head	*rnh;
624 	int s;
625 	int i;
626 	struct domain *dp;
627  	struct ifnet *iter;
628  	int found = 0;
629 
630 	IFNET_WLOCK();
631 	TAILQ_FOREACH(iter, &ifnet, if_link)
632 		if (iter == ifp) {
633 			TAILQ_REMOVE(&ifnet, ifp, if_link);
634 			found = 1;
635 			break;
636 		}
637 	IFNET_WUNLOCK();
638 	if (!found)
639 		return;
640 
641 	/*
642 	 * Remove/wait for pending events.
643 	 */
644 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
645 
646 	/*
647 	 * Remove routes and flush queues.
648 	 */
649 	s = splnet();
650 	if_down(ifp);
651 #ifdef ALTQ
652 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
653 		altq_disable(&ifp->if_snd);
654 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
655 		altq_detach(&ifp->if_snd);
656 #endif
657 
658 	if_purgeaddrs(ifp);
659 
660 #ifdef INET
661 	in_ifdetach(ifp);
662 #endif
663 
664 #ifdef INET6
665 	/*
666 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
667 	 * before removing routing entries below, since IPv6 interface direct
668 	 * routes are expected to be removed by the IPv6-specific kernel API.
669 	 * Otherwise, the kernel will detect some inconsistency and bark it.
670 	 */
671 	in6_ifdetach(ifp);
672 #endif
673 	/*
674 	 * Remove link ifaddr pointer and maybe decrement if_index.
675 	 * Clean up all addresses.
676 	 */
677 	ifp->if_addr = NULL;
678 	destroy_dev(ifdev_byindex(ifp->if_index));
679 	ifdev_byindex(ifp->if_index) = NULL;
680 
681 	/* We can now free link ifaddr. */
682 	if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
683 		ifa = TAILQ_FIRST(&ifp->if_addrhead);
684 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
685 		IFAFREE(ifa);
686 	}
687 
688 	/*
689 	 * Delete all remaining routes using this interface
690 	 * Unfortuneatly the only way to do this is to slog through
691 	 * the entire routing table looking for routes which point
692 	 * to this interface...oh well...
693 	 */
694 	for (i = 1; i <= AF_MAX; i++) {
695 		if ((rnh = rt_tables[i]) == NULL)
696 			continue;
697 		RADIX_NODE_HEAD_LOCK(rnh);
698 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
699 		RADIX_NODE_HEAD_UNLOCK(rnh);
700 	}
701 
702 	/* Announce that the interface is gone. */
703 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
704 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
705 	devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
706 
707 	IF_AFDATA_LOCK(ifp);
708 	for (dp = domains; dp; dp = dp->dom_next) {
709 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
710 			(*dp->dom_ifdetach)(ifp,
711 			    ifp->if_afdata[dp->dom_family]);
712 	}
713 	IF_AFDATA_UNLOCK(ifp);
714 
715 #ifdef MAC
716 	mac_destroy_ifnet(ifp);
717 #endif /* MAC */
718 	KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
719 	knlist_clear(&ifp->if_klist, 0);
720 	knlist_destroy(&ifp->if_klist);
721 	mtx_destroy(&ifp->if_snd.ifq_mtx);
722 	IF_AFDATA_DESTROY(ifp);
723 	splx(s);
724 }
725 
726 /*
727  * Add a group to an interface
728  */
729 int
730 if_addgroup(struct ifnet *ifp, const char *groupname)
731 {
732 	struct ifg_list		*ifgl;
733 	struct ifg_group	*ifg = NULL;
734 	struct ifg_member	*ifgm;
735 
736 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
737 	    groupname[strlen(groupname) - 1] <= '9')
738 		return (EINVAL);
739 
740 	IFNET_WLOCK();
741 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
742 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
743 			IFNET_WUNLOCK();
744 			return (EEXIST);
745 		}
746 
747 	if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
748 	    M_NOWAIT)) == NULL) {
749 	    	IFNET_WUNLOCK();
750 		return (ENOMEM);
751 	}
752 
753 	if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
754 	    M_TEMP, M_NOWAIT)) == NULL) {
755 		free(ifgl, M_TEMP);
756 		IFNET_WUNLOCK();
757 		return (ENOMEM);
758 	}
759 
760 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
761 		if (!strcmp(ifg->ifg_group, groupname))
762 			break;
763 
764 	if (ifg == NULL) {
765 		if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
766 		    M_TEMP, M_NOWAIT)) == NULL) {
767 			free(ifgl, M_TEMP);
768 			free(ifgm, M_TEMP);
769 			IFNET_WUNLOCK();
770 			return (ENOMEM);
771 		}
772 		strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
773 		ifg->ifg_refcnt = 0;
774 		TAILQ_INIT(&ifg->ifg_members);
775 		EVENTHANDLER_INVOKE(group_attach_event, ifg);
776 		TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
777 	}
778 
779 	ifg->ifg_refcnt++;
780 	ifgl->ifgl_group = ifg;
781 	ifgm->ifgm_ifp = ifp;
782 
783 	IF_ADDR_LOCK(ifp);
784 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
785 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
786 	IF_ADDR_UNLOCK(ifp);
787 
788 	IFNET_WUNLOCK();
789 
790 	EVENTHANDLER_INVOKE(group_change_event, groupname);
791 
792 	return (0);
793 }
794 
795 /*
796  * Remove a group from an interface
797  */
798 int
799 if_delgroup(struct ifnet *ifp, const char *groupname)
800 {
801 	struct ifg_list		*ifgl;
802 	struct ifg_member	*ifgm;
803 
804 	IFNET_WLOCK();
805 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
806 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
807 			break;
808 	if (ifgl == NULL) {
809 		IFNET_WUNLOCK();
810 		return (ENOENT);
811 	}
812 
813 	IF_ADDR_LOCK(ifp);
814 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
815 	IF_ADDR_UNLOCK(ifp);
816 
817 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
818 		if (ifgm->ifgm_ifp == ifp)
819 			break;
820 
821 	if (ifgm != NULL) {
822 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
823 		free(ifgm, M_TEMP);
824 	}
825 
826 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
827 		TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
828 		EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
829 		free(ifgl->ifgl_group, M_TEMP);
830 	}
831 	IFNET_WUNLOCK();
832 
833 	free(ifgl, M_TEMP);
834 
835 	EVENTHANDLER_INVOKE(group_change_event, groupname);
836 
837 	return (0);
838 }
839 
840 /*
841  * Stores all groups from an interface in memory pointed
842  * to by data
843  */
844 static int
845 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
846 {
847 	int			 len, error;
848 	struct ifg_list		*ifgl;
849 	struct ifg_req		 ifgrq, *ifgp;
850 	struct ifgroupreq	*ifgr = data;
851 
852 	if (ifgr->ifgr_len == 0) {
853 		IF_ADDR_LOCK(ifp);
854 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
855 			ifgr->ifgr_len += sizeof(struct ifg_req);
856 		IF_ADDR_UNLOCK(ifp);
857 		return (0);
858 	}
859 
860 	len = ifgr->ifgr_len;
861 	ifgp = ifgr->ifgr_groups;
862 	/* XXX: wire */
863 	IF_ADDR_LOCK(ifp);
864 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
865 		if (len < sizeof(ifgrq)) {
866 			IF_ADDR_UNLOCK(ifp);
867 			return (EINVAL);
868 		}
869 		bzero(&ifgrq, sizeof ifgrq);
870 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
871 		    sizeof(ifgrq.ifgrq_group));
872 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
873 		    	IF_ADDR_UNLOCK(ifp);
874 			return (error);
875 		}
876 		len -= sizeof(ifgrq);
877 		ifgp++;
878 	}
879 	IF_ADDR_UNLOCK(ifp);
880 
881 	return (0);
882 }
883 
884 /*
885  * Stores all members of a group in memory pointed to by data
886  */
887 static int
888 if_getgroupmembers(struct ifgroupreq *data)
889 {
890 	struct ifgroupreq	*ifgr = data;
891 	struct ifg_group	*ifg;
892 	struct ifg_member	*ifgm;
893 	struct ifg_req		 ifgrq, *ifgp;
894 	int			 len, error;
895 
896 	IFNET_RLOCK();
897 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
898 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
899 			break;
900 	if (ifg == NULL) {
901 		IFNET_RUNLOCK();
902 		return (ENOENT);
903 	}
904 
905 	if (ifgr->ifgr_len == 0) {
906 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
907 			ifgr->ifgr_len += sizeof(ifgrq);
908 		IFNET_RUNLOCK();
909 		return (0);
910 	}
911 
912 	len = ifgr->ifgr_len;
913 	ifgp = ifgr->ifgr_groups;
914 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
915 		if (len < sizeof(ifgrq)) {
916 			IFNET_RUNLOCK();
917 			return (EINVAL);
918 		}
919 		bzero(&ifgrq, sizeof ifgrq);
920 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
921 		    sizeof(ifgrq.ifgrq_member));
922 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
923 			IFNET_RUNLOCK();
924 			return (error);
925 		}
926 		len -= sizeof(ifgrq);
927 		ifgp++;
928 	}
929 	IFNET_RUNLOCK();
930 
931 	return (0);
932 }
933 
934 /*
935  * Delete Routes for a Network Interface
936  *
937  * Called for each routing entry via the rnh->rnh_walktree() call above
938  * to delete all route entries referencing a detaching network interface.
939  *
940  * Arguments:
941  *	rn	pointer to node in the routing table
942  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
943  *
944  * Returns:
945  *	0	successful
946  *	errno	failed - reason indicated
947  *
948  */
949 static int
950 if_rtdel(struct radix_node *rn, void *arg)
951 {
952 	struct rtentry	*rt = (struct rtentry *)rn;
953 	struct ifnet	*ifp = arg;
954 	int		err;
955 
956 	if (rt->rt_ifp == ifp) {
957 
958 		/*
959 		 * Protect (sorta) against walktree recursion problems
960 		 * with cloned routes
961 		 */
962 		if ((rt->rt_flags & RTF_UP) == 0)
963 			return (0);
964 
965 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
966 				rt_mask(rt), rt->rt_flags,
967 				(struct rtentry **) NULL);
968 		if (err) {
969 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
970 		}
971 	}
972 
973 	return (0);
974 }
975 
976 #define	sa_equal(a1, a2)	(bcmp((a1), (a2), ((a1))->sa_len) == 0)
977 
978 /*
979  * Locate an interface based on a complete address.
980  */
981 /*ARGSUSED*/
982 struct ifaddr *
983 ifa_ifwithaddr(struct sockaddr *addr)
984 {
985 	struct ifnet *ifp;
986 	struct ifaddr *ifa;
987 
988 	IFNET_RLOCK();
989 	TAILQ_FOREACH(ifp, &ifnet, if_link)
990 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
991 			if (ifa->ifa_addr->sa_family != addr->sa_family)
992 				continue;
993 			if (sa_equal(addr, ifa->ifa_addr))
994 				goto done;
995 			/* IP6 doesn't have broadcast */
996 			if ((ifp->if_flags & IFF_BROADCAST) &&
997 			    ifa->ifa_broadaddr &&
998 			    ifa->ifa_broadaddr->sa_len != 0 &&
999 			    sa_equal(ifa->ifa_broadaddr, addr))
1000 				goto done;
1001 		}
1002 	ifa = NULL;
1003 done:
1004 	IFNET_RUNLOCK();
1005 	return (ifa);
1006 }
1007 
1008 /*
1009  * Locate an interface based on the broadcast address.
1010  */
1011 /* ARGSUSED */
1012 struct ifaddr *
1013 ifa_ifwithbroadaddr(struct sockaddr *addr)
1014 {
1015 	struct ifnet *ifp;
1016 	struct ifaddr *ifa;
1017 
1018 	IFNET_RLOCK();
1019 	TAILQ_FOREACH(ifp, &ifnet, if_link)
1020 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1021 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1022 				continue;
1023 			if ((ifp->if_flags & IFF_BROADCAST) &&
1024 			    ifa->ifa_broadaddr &&
1025 			    ifa->ifa_broadaddr->sa_len != 0 &&
1026 			    sa_equal(ifa->ifa_broadaddr, addr))
1027 				goto done;
1028 		}
1029 	ifa = NULL;
1030 done:
1031 	IFNET_RUNLOCK();
1032 	return (ifa);
1033 }
1034 
1035 /*
1036  * Locate the point to point interface with a given destination address.
1037  */
1038 /*ARGSUSED*/
1039 struct ifaddr *
1040 ifa_ifwithdstaddr(struct sockaddr *addr)
1041 {
1042 	struct ifnet *ifp;
1043 	struct ifaddr *ifa;
1044 
1045 	IFNET_RLOCK();
1046 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1047 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1048 			continue;
1049 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1050 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1051 				continue;
1052 			if (ifa->ifa_dstaddr &&
1053 			    sa_equal(addr, ifa->ifa_dstaddr))
1054 				goto done;
1055 		}
1056 	}
1057 	ifa = NULL;
1058 done:
1059 	IFNET_RUNLOCK();
1060 	return (ifa);
1061 }
1062 
1063 /*
1064  * Find an interface on a specific network.  If many, choice
1065  * is most specific found.
1066  */
1067 struct ifaddr *
1068 ifa_ifwithnet(struct sockaddr *addr)
1069 {
1070 	struct ifnet *ifp;
1071 	struct ifaddr *ifa;
1072 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1073 	u_int af = addr->sa_family;
1074 	char *addr_data = addr->sa_data, *cplim;
1075 
1076 	/*
1077 	 * AF_LINK addresses can be looked up directly by their index number,
1078 	 * so do that if we can.
1079 	 */
1080 	if (af == AF_LINK) {
1081 	    struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1082 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
1083 		return (ifaddr_byindex(sdl->sdl_index));
1084 	}
1085 
1086 	/*
1087 	 * Scan though each interface, looking for ones that have
1088 	 * addresses in this address family.
1089 	 */
1090 	IFNET_RLOCK();
1091 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1092 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1093 			char *cp, *cp2, *cp3;
1094 
1095 			if (ifa->ifa_addr->sa_family != af)
1096 next:				continue;
1097 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1098 				/*
1099 				 * This is a bit broken as it doesn't
1100 				 * take into account that the remote end may
1101 				 * be a single node in the network we are
1102 				 * looking for.
1103 				 * The trouble is that we don't know the
1104 				 * netmask for the remote end.
1105 				 */
1106 				if (ifa->ifa_dstaddr != 0 &&
1107 				    sa_equal(addr, ifa->ifa_dstaddr))
1108 					goto done;
1109 			} else {
1110 				/*
1111 				 * if we have a special address handler,
1112 				 * then use it instead of the generic one.
1113 				 */
1114 				if (ifa->ifa_claim_addr) {
1115 					if ((*ifa->ifa_claim_addr)(ifa, addr))
1116 						goto done;
1117 					continue;
1118 				}
1119 
1120 				/*
1121 				 * Scan all the bits in the ifa's address.
1122 				 * If a bit dissagrees with what we are
1123 				 * looking for, mask it with the netmask
1124 				 * to see if it really matters.
1125 				 * (A byte at a time)
1126 				 */
1127 				if (ifa->ifa_netmask == 0)
1128 					continue;
1129 				cp = addr_data;
1130 				cp2 = ifa->ifa_addr->sa_data;
1131 				cp3 = ifa->ifa_netmask->sa_data;
1132 				cplim = ifa->ifa_netmask->sa_len
1133 					+ (char *)ifa->ifa_netmask;
1134 				while (cp3 < cplim)
1135 					if ((*cp++ ^ *cp2++) & *cp3++)
1136 						goto next; /* next address! */
1137 				/*
1138 				 * If the netmask of what we just found
1139 				 * is more specific than what we had before
1140 				 * (if we had one) then remember the new one
1141 				 * before continuing to search
1142 				 * for an even better one.
1143 				 */
1144 				if (ifa_maybe == 0 ||
1145 				    rn_refines((caddr_t)ifa->ifa_netmask,
1146 				    (caddr_t)ifa_maybe->ifa_netmask))
1147 					ifa_maybe = ifa;
1148 			}
1149 		}
1150 	}
1151 	ifa = ifa_maybe;
1152 done:
1153 	IFNET_RUNLOCK();
1154 	return (ifa);
1155 }
1156 
1157 /*
1158  * Find an interface address specific to an interface best matching
1159  * a given address.
1160  */
1161 struct ifaddr *
1162 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1163 {
1164 	struct ifaddr *ifa;
1165 	char *cp, *cp2, *cp3;
1166 	char *cplim;
1167 	struct ifaddr *ifa_maybe = 0;
1168 	u_int af = addr->sa_family;
1169 
1170 	if (af >= AF_MAX)
1171 		return (0);
1172 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1173 		if (ifa->ifa_addr->sa_family != af)
1174 			continue;
1175 		if (ifa_maybe == 0)
1176 			ifa_maybe = ifa;
1177 		if (ifa->ifa_netmask == 0) {
1178 			if (sa_equal(addr, ifa->ifa_addr) ||
1179 			    (ifa->ifa_dstaddr &&
1180 			    sa_equal(addr, ifa->ifa_dstaddr)))
1181 				goto done;
1182 			continue;
1183 		}
1184 		if (ifp->if_flags & IFF_POINTOPOINT) {
1185 			if (sa_equal(addr, ifa->ifa_dstaddr))
1186 				goto done;
1187 		} else {
1188 			cp = addr->sa_data;
1189 			cp2 = ifa->ifa_addr->sa_data;
1190 			cp3 = ifa->ifa_netmask->sa_data;
1191 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1192 			for (; cp3 < cplim; cp3++)
1193 				if ((*cp++ ^ *cp2++) & *cp3)
1194 					break;
1195 			if (cp3 == cplim)
1196 				goto done;
1197 		}
1198 	}
1199 	ifa = ifa_maybe;
1200 done:
1201 	return (ifa);
1202 }
1203 
1204 #include <net/route.h>
1205 
1206 /*
1207  * Default action when installing a route with a Link Level gateway.
1208  * Lookup an appropriate real ifa to point to.
1209  * This should be moved to /sys/net/link.c eventually.
1210  */
1211 static void
1212 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1213 {
1214 	struct ifaddr *ifa, *oifa;
1215 	struct sockaddr *dst;
1216 	struct ifnet *ifp;
1217 
1218 	RT_LOCK_ASSERT(rt);
1219 
1220 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1221 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1222 		return;
1223 	ifa = ifaof_ifpforaddr(dst, ifp);
1224 	if (ifa) {
1225 		IFAREF(ifa);		/* XXX */
1226 		oifa = rt->rt_ifa;
1227 		rt->rt_ifa = ifa;
1228 		IFAFREE(oifa);
1229 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1230 			ifa->ifa_rtrequest(cmd, rt, info);
1231 	}
1232 }
1233 
1234 /*
1235  * Mark an interface down and notify protocols of
1236  * the transition.
1237  * NOTE: must be called at splnet or eqivalent.
1238  */
1239 static void
1240 if_unroute(struct ifnet *ifp, int flag, int fam)
1241 {
1242 	struct ifaddr *ifa;
1243 
1244 	KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1245 
1246 	ifp->if_flags &= ~flag;
1247 	getmicrotime(&ifp->if_lastchange);
1248 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1249 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1250 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1251 	if_qflush(&ifp->if_snd);
1252 #ifdef DEV_CARP
1253 	if (ifp->if_carp)
1254 		carp_carpdev_state(ifp->if_carp);
1255 #endif
1256 	rt_ifmsg(ifp);
1257 }
1258 
1259 /*
1260  * Mark an interface up and notify protocols of
1261  * the transition.
1262  * NOTE: must be called at splnet or eqivalent.
1263  */
1264 static void
1265 if_route(struct ifnet *ifp, int flag, int fam)
1266 {
1267 	struct ifaddr *ifa;
1268 
1269 	KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1270 
1271 	ifp->if_flags |= flag;
1272 	getmicrotime(&ifp->if_lastchange);
1273 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1274 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1275 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
1276 #ifdef DEV_CARP
1277 	if (ifp->if_carp)
1278 		carp_carpdev_state(ifp->if_carp);
1279 #endif
1280 	rt_ifmsg(ifp);
1281 #ifdef INET6
1282 	in6_if_up(ifp);
1283 #endif
1284 }
1285 
1286 void	(*vlan_link_state_p)(struct ifnet *, int);	/* XXX: private from if_vlan */
1287 void	(*vlan_trunk_cap_p)(struct ifnet *);		/* XXX: private from if_vlan */
1288 
1289 /*
1290  * Handle a change in the interface link state. To avoid LORs
1291  * between driver lock and upper layer locks, as well as possible
1292  * recursions, we post event to taskqueue, and all job
1293  * is done in static do_link_state_change().
1294  */
1295 void
1296 if_link_state_change(struct ifnet *ifp, int link_state)
1297 {
1298 	/* Return if state hasn't changed. */
1299 	if (ifp->if_link_state == link_state)
1300 		return;
1301 
1302 	ifp->if_link_state = link_state;
1303 
1304 	taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1305 }
1306 
1307 static void
1308 do_link_state_change(void *arg, int pending)
1309 {
1310 	struct ifnet *ifp = (struct ifnet *)arg;
1311 	int link_state = ifp->if_link_state;
1312 	int link;
1313 
1314 	/* Notify that the link state has changed. */
1315 	rt_ifmsg(ifp);
1316 	if (link_state == LINK_STATE_UP)
1317 		link = NOTE_LINKUP;
1318 	else if (link_state == LINK_STATE_DOWN)
1319 		link = NOTE_LINKDOWN;
1320 	else
1321 		link = NOTE_LINKINV;
1322 	KNOTE_UNLOCKED(&ifp->if_klist, link);
1323 	if (ifp->if_vlantrunk != NULL)
1324 		(*vlan_link_state_p)(ifp, link);
1325 
1326 	if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1327 	    IFP2AC(ifp)->ac_netgraph != NULL)
1328 		(*ng_ether_link_state_p)(ifp, link_state);
1329 #ifdef DEV_CARP
1330 	if (ifp->if_carp)
1331 		carp_carpdev_state(ifp->if_carp);
1332 #endif
1333 	if (ifp->if_bridge) {
1334 		KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1335 		(*bstp_linkstate_p)(ifp, link_state);
1336 	}
1337 
1338 	devctl_notify("IFNET", ifp->if_xname,
1339 	    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1340 	if (pending > 1)
1341 		if_printf(ifp, "%d link states coalesced\n", pending);
1342 	if (log_link_state_change)
1343 		log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1344 		    (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1345 }
1346 
1347 /*
1348  * Mark an interface down and notify protocols of
1349  * the transition.
1350  * NOTE: must be called at splnet or eqivalent.
1351  */
1352 void
1353 if_down(struct ifnet *ifp)
1354 {
1355 
1356 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1357 }
1358 
1359 /*
1360  * Mark an interface up and notify protocols of
1361  * the transition.
1362  * NOTE: must be called at splnet or eqivalent.
1363  */
1364 void
1365 if_up(struct ifnet *ifp)
1366 {
1367 
1368 	if_route(ifp, IFF_UP, AF_UNSPEC);
1369 }
1370 
1371 /*
1372  * Flush an interface queue.
1373  */
1374 static void
1375 if_qflush(struct ifaltq *ifq)
1376 {
1377 	struct mbuf *m, *n;
1378 
1379 	IFQ_LOCK(ifq);
1380 #ifdef ALTQ
1381 	if (ALTQ_IS_ENABLED(ifq))
1382 		ALTQ_PURGE(ifq);
1383 #endif
1384 	n = ifq->ifq_head;
1385 	while ((m = n) != 0) {
1386 		n = m->m_act;
1387 		m_freem(m);
1388 	}
1389 	ifq->ifq_head = 0;
1390 	ifq->ifq_tail = 0;
1391 	ifq->ifq_len = 0;
1392 	IFQ_UNLOCK(ifq);
1393 }
1394 
1395 /*
1396  * Handle interface watchdog timer routines.  Called
1397  * from softclock, we decrement timers (if set) and
1398  * call the appropriate interface routine on expiration.
1399  *
1400  * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1401  * holding Giant.  If we switch to an MPSAFE callout, we likely need to grab
1402  * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1403  */
1404 static void
1405 if_slowtimo(void *arg)
1406 {
1407 	struct ifnet *ifp;
1408 	int s = splimp();
1409 
1410 	IFNET_RLOCK();
1411 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1412 		if (ifp->if_timer == 0 || --ifp->if_timer)
1413 			continue;
1414 		if (ifp->if_watchdog)
1415 			(*ifp->if_watchdog)(ifp);
1416 	}
1417 	IFNET_RUNLOCK();
1418 	splx(s);
1419 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1420 }
1421 
1422 /*
1423  * Map interface name to
1424  * interface structure pointer.
1425  */
1426 struct ifnet *
1427 ifunit(const char *name)
1428 {
1429 	struct ifnet *ifp;
1430 
1431 	IFNET_RLOCK();
1432 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1433 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1434 			break;
1435 	}
1436 	IFNET_RUNLOCK();
1437 	return (ifp);
1438 }
1439 
1440 /*
1441  * Hardware specific interface ioctls.
1442  */
1443 static int
1444 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1445 {
1446 	struct ifreq *ifr;
1447 	struct ifstat *ifs;
1448 	int error = 0;
1449 	int new_flags, temp_flags;
1450 	size_t namelen, onamelen;
1451 	char new_name[IFNAMSIZ];
1452 	struct ifaddr *ifa;
1453 	struct sockaddr_dl *sdl;
1454 
1455 	ifr = (struct ifreq *)data;
1456 	switch (cmd) {
1457 	case SIOCGIFINDEX:
1458 		ifr->ifr_index = ifp->if_index;
1459 		break;
1460 
1461 	case SIOCGIFFLAGS:
1462 		temp_flags = ifp->if_flags | ifp->if_drv_flags;
1463 		ifr->ifr_flags = temp_flags & 0xffff;
1464 		ifr->ifr_flagshigh = temp_flags >> 16;
1465 		break;
1466 
1467 	case SIOCGIFCAP:
1468 		ifr->ifr_reqcap = ifp->if_capabilities;
1469 		ifr->ifr_curcap = ifp->if_capenable;
1470 		break;
1471 
1472 #ifdef MAC
1473 	case SIOCGIFMAC:
1474 		error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1475 		break;
1476 #endif
1477 
1478 	case SIOCGIFMETRIC:
1479 		ifr->ifr_metric = ifp->if_metric;
1480 		break;
1481 
1482 	case SIOCGIFMTU:
1483 		ifr->ifr_mtu = ifp->if_mtu;
1484 		break;
1485 
1486 	case SIOCGIFPHYS:
1487 		ifr->ifr_phys = ifp->if_physical;
1488 		break;
1489 
1490 	case SIOCSIFFLAGS:
1491 		error = suser(td);
1492 		if (error)
1493 			return (error);
1494 		/*
1495 		 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1496 		 * check, so we don't need special handling here yet.
1497 		 */
1498 		new_flags = (ifr->ifr_flags & 0xffff) |
1499 		    (ifr->ifr_flagshigh << 16);
1500 		if (ifp->if_flags & IFF_SMART) {
1501 			/* Smart drivers twiddle their own routes */
1502 		} else if (ifp->if_flags & IFF_UP &&
1503 		    (new_flags & IFF_UP) == 0) {
1504 			int s = splimp();
1505 			if_down(ifp);
1506 			splx(s);
1507 		} else if (new_flags & IFF_UP &&
1508 		    (ifp->if_flags & IFF_UP) == 0) {
1509 			int s = splimp();
1510 			if_up(ifp);
1511 			splx(s);
1512 		}
1513 		/* See if permanently promiscuous mode bit is about to flip */
1514 		if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1515 			if (new_flags & IFF_PPROMISC)
1516 				ifp->if_flags |= IFF_PROMISC;
1517 			else if (ifp->if_pcount == 0)
1518 				ifp->if_flags &= ~IFF_PROMISC;
1519 			log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1520 			    ifp->if_xname,
1521 			    (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1522 		}
1523 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1524 			(new_flags &~ IFF_CANTCHANGE);
1525 		if (ifp->if_ioctl) {
1526 			IFF_LOCKGIANT(ifp);
1527 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1528 			IFF_UNLOCKGIANT(ifp);
1529 		}
1530 		getmicrotime(&ifp->if_lastchange);
1531 		break;
1532 
1533 	case SIOCSIFCAP:
1534 		error = suser(td);
1535 		if (error)
1536 			return (error);
1537 		if (ifp->if_ioctl == NULL)
1538 			return (EOPNOTSUPP);
1539 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1540 			return (EINVAL);
1541 		IFF_LOCKGIANT(ifp);
1542 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1543 		IFF_UNLOCKGIANT(ifp);
1544 		if (error == 0)
1545 			getmicrotime(&ifp->if_lastchange);
1546 		break;
1547 
1548 #ifdef MAC
1549 	case SIOCSIFMAC:
1550 		error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1551 		break;
1552 #endif
1553 
1554 	case SIOCSIFNAME:
1555 		error = suser(td);
1556 		if (error != 0)
1557 			return (error);
1558 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1559 		if (error != 0)
1560 			return (error);
1561 		if (new_name[0] == '\0')
1562 			return (EINVAL);
1563 		if (ifunit(new_name) != NULL)
1564 			return (EEXIST);
1565 
1566 		/* Announce the departure of the interface. */
1567 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1568 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1569 
1570 		log(LOG_INFO, "%s: changing name to '%s'\n",
1571 		    ifp->if_xname, new_name);
1572 
1573 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1574 		ifa = ifp->if_addr;
1575 		IFA_LOCK(ifa);
1576 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1577 		namelen = strlen(new_name);
1578 		onamelen = sdl->sdl_nlen;
1579 		/*
1580 		 * Move the address if needed.  This is safe because we
1581 		 * allocate space for a name of length IFNAMSIZ when we
1582 		 * create this in if_attach().
1583 		 */
1584 		if (namelen != onamelen) {
1585 			bcopy(sdl->sdl_data + onamelen,
1586 			    sdl->sdl_data + namelen, sdl->sdl_alen);
1587 		}
1588 		bcopy(new_name, sdl->sdl_data, namelen);
1589 		sdl->sdl_nlen = namelen;
1590 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1591 		bzero(sdl->sdl_data, onamelen);
1592 		while (namelen != 0)
1593 			sdl->sdl_data[--namelen] = 0xff;
1594 		IFA_UNLOCK(ifa);
1595 
1596 		EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1597 		/* Announce the return of the interface. */
1598 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1599 		break;
1600 
1601 	case SIOCSIFMETRIC:
1602 		error = suser(td);
1603 		if (error)
1604 			return (error);
1605 		ifp->if_metric = ifr->ifr_metric;
1606 		getmicrotime(&ifp->if_lastchange);
1607 		break;
1608 
1609 	case SIOCSIFPHYS:
1610 		error = suser(td);
1611 		if (error)
1612 			return (error);
1613 		if (ifp->if_ioctl == NULL)
1614 			return (EOPNOTSUPP);
1615 		IFF_LOCKGIANT(ifp);
1616 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1617 		IFF_UNLOCKGIANT(ifp);
1618 		if (error == 0)
1619 			getmicrotime(&ifp->if_lastchange);
1620 		break;
1621 
1622 	case SIOCSIFMTU:
1623 	{
1624 		u_long oldmtu = ifp->if_mtu;
1625 
1626 		error = suser(td);
1627 		if (error)
1628 			return (error);
1629 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1630 			return (EINVAL);
1631 		if (ifp->if_ioctl == NULL)
1632 			return (EOPNOTSUPP);
1633 		IFF_LOCKGIANT(ifp);
1634 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1635 		IFF_UNLOCKGIANT(ifp);
1636 		if (error == 0) {
1637 			getmicrotime(&ifp->if_lastchange);
1638 			rt_ifmsg(ifp);
1639 		}
1640 		/*
1641 		 * If the link MTU changed, do network layer specific procedure.
1642 		 */
1643 		if (ifp->if_mtu != oldmtu) {
1644 #ifdef INET6
1645 			nd6_setmtu(ifp);
1646 #endif
1647 		}
1648 		break;
1649 	}
1650 
1651 	case SIOCADDMULTI:
1652 	case SIOCDELMULTI:
1653 		error = suser(td);
1654 		if (error)
1655 			return (error);
1656 
1657 		/* Don't allow group membership on non-multicast interfaces. */
1658 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1659 			return (EOPNOTSUPP);
1660 
1661 		/* Don't let users screw up protocols' entries. */
1662 		if (ifr->ifr_addr.sa_family != AF_LINK)
1663 			return (EINVAL);
1664 
1665 		if (cmd == SIOCADDMULTI) {
1666 			struct ifmultiaddr *ifma;
1667 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1668 		} else {
1669 			error = if_delmulti(ifp, &ifr->ifr_addr);
1670 		}
1671 		if (error == 0)
1672 			getmicrotime(&ifp->if_lastchange);
1673 		break;
1674 
1675 	case SIOCSIFPHYADDR:
1676 	case SIOCDIFPHYADDR:
1677 #ifdef INET6
1678 	case SIOCSIFPHYADDR_IN6:
1679 #endif
1680 	case SIOCSLIFPHYADDR:
1681 	case SIOCSIFMEDIA:
1682 	case SIOCSIFGENERIC:
1683 		error = suser(td);
1684 		if (error)
1685 			return (error);
1686 		if (ifp->if_ioctl == NULL)
1687 			return (EOPNOTSUPP);
1688 		IFF_LOCKGIANT(ifp);
1689 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1690 		IFF_UNLOCKGIANT(ifp);
1691 		if (error == 0)
1692 			getmicrotime(&ifp->if_lastchange);
1693 		break;
1694 
1695 	case SIOCGIFSTATUS:
1696 		ifs = (struct ifstat *)data;
1697 		ifs->ascii[0] = '\0';
1698 
1699 	case SIOCGIFPSRCADDR:
1700 	case SIOCGIFPDSTADDR:
1701 	case SIOCGLIFPHYADDR:
1702 	case SIOCGIFMEDIA:
1703 	case SIOCGIFGENERIC:
1704 		if (ifp->if_ioctl == NULL)
1705 			return (EOPNOTSUPP);
1706 		IFF_LOCKGIANT(ifp);
1707 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1708 		IFF_UNLOCKGIANT(ifp);
1709 		break;
1710 
1711 	case SIOCSIFLLADDR:
1712 		error = suser(td);
1713 		if (error)
1714 			return (error);
1715 		error = if_setlladdr(ifp,
1716 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1717 		break;
1718 
1719 	case SIOCAIFGROUP:
1720 	{
1721 		struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1722 
1723 		error = suser(td);
1724 		if (error)
1725 			return (error);
1726 		if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1727 			return (error);
1728 		break;
1729 	}
1730 
1731 	case SIOCGIFGROUP:
1732 		if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1733 			return (error);
1734 		break;
1735 
1736 	case SIOCDIFGROUP:
1737 	{
1738 		struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1739 
1740 		error = suser(td);
1741 		if (error)
1742 			return (error);
1743 		if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1744 			return (error);
1745 		break;
1746 	}
1747 
1748 	default:
1749 		error = ENOIOCTL;
1750 		break;
1751 	}
1752 	return (error);
1753 }
1754 
1755 /*
1756  * Interface ioctls.
1757  */
1758 int
1759 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1760 {
1761 	struct ifnet *ifp;
1762 	struct ifreq *ifr;
1763 	int error;
1764 	int oif_flags;
1765 
1766 	switch (cmd) {
1767 	case SIOCGIFCONF:
1768 	case OSIOCGIFCONF:
1769 #ifdef __amd64__
1770 	case SIOCGIFCONF32:
1771 #endif
1772 		return (ifconf(cmd, data));
1773 	}
1774 	ifr = (struct ifreq *)data;
1775 
1776 	switch (cmd) {
1777 	case SIOCIFCREATE:
1778 	case SIOCIFCREATE2:
1779 		if ((error = suser(td)) != 0)
1780 			return (error);
1781 		return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1782 			cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1783 	case SIOCIFDESTROY:
1784 		if ((error = suser(td)) != 0)
1785 			return (error);
1786 		return if_clone_destroy(ifr->ifr_name);
1787 
1788 	case SIOCIFGCLONERS:
1789 		return (if_clone_list((struct if_clonereq *)data));
1790 	case SIOCGIFGMEMB:
1791 		return (if_getgroupmembers((struct ifgroupreq *)data));
1792 	}
1793 
1794 	ifp = ifunit(ifr->ifr_name);
1795 	if (ifp == 0)
1796 		return (ENXIO);
1797 
1798 	error = ifhwioctl(cmd, ifp, data, td);
1799 	if (error != ENOIOCTL)
1800 		return (error);
1801 
1802 	oif_flags = ifp->if_flags;
1803 	if (so->so_proto == 0)
1804 		return (EOPNOTSUPP);
1805 #ifndef COMPAT_43
1806 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1807 								 data,
1808 								 ifp, td));
1809 #else
1810 	{
1811 		int ocmd = cmd;
1812 
1813 		switch (cmd) {
1814 
1815 		case SIOCSIFDSTADDR:
1816 		case SIOCSIFADDR:
1817 		case SIOCSIFBRDADDR:
1818 		case SIOCSIFNETMASK:
1819 #if BYTE_ORDER != BIG_ENDIAN
1820 			if (ifr->ifr_addr.sa_family == 0 &&
1821 			    ifr->ifr_addr.sa_len < 16) {
1822 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1823 				ifr->ifr_addr.sa_len = 16;
1824 			}
1825 #else
1826 			if (ifr->ifr_addr.sa_len == 0)
1827 				ifr->ifr_addr.sa_len = 16;
1828 #endif
1829 			break;
1830 
1831 		case OSIOCGIFADDR:
1832 			cmd = SIOCGIFADDR;
1833 			break;
1834 
1835 		case OSIOCGIFDSTADDR:
1836 			cmd = SIOCGIFDSTADDR;
1837 			break;
1838 
1839 		case OSIOCGIFBRDADDR:
1840 			cmd = SIOCGIFBRDADDR;
1841 			break;
1842 
1843 		case OSIOCGIFNETMASK:
1844 			cmd = SIOCGIFNETMASK;
1845 		}
1846 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
1847 								   cmd,
1848 								   data,
1849 								   ifp, td));
1850 		switch (ocmd) {
1851 
1852 		case OSIOCGIFADDR:
1853 		case OSIOCGIFDSTADDR:
1854 		case OSIOCGIFBRDADDR:
1855 		case OSIOCGIFNETMASK:
1856 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1857 
1858 		}
1859 	}
1860 #endif /* COMPAT_43 */
1861 
1862 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1863 #ifdef INET6
1864 		DELAY(100);/* XXX: temporary workaround for fxp issue*/
1865 		if (ifp->if_flags & IFF_UP) {
1866 			int s = splimp();
1867 			in6_if_up(ifp);
1868 			splx(s);
1869 		}
1870 #endif
1871 	}
1872 	return (error);
1873 }
1874 
1875 /*
1876  * The code common to handling reference counted flags,
1877  * e.g., in ifpromisc() and if_allmulti().
1878  * The "pflag" argument can specify a permanent mode flag to check,
1879  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
1880  *
1881  * Only to be used on stack-owned flags, not driver-owned flags.
1882  */
1883 static int
1884 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
1885 {
1886 	struct ifreq ifr;
1887 	int error;
1888 	int oldflags, oldcount;
1889 
1890 	/* Sanity checks to catch programming errors */
1891 	KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
1892 	    ("%s: setting driver-owned flag %d", __func__, flag));
1893 
1894 	if (onswitch)
1895 		KASSERT(*refcount >= 0,
1896 		    ("%s: increment negative refcount %d for flag %d",
1897 		    __func__, *refcount, flag));
1898 	else
1899 		KASSERT(*refcount > 0,
1900 		    ("%s: decrement non-positive refcount %d for flag %d",
1901 		    __func__, *refcount, flag));
1902 
1903 	/* In case this mode is permanent, just touch refcount */
1904 	if (ifp->if_flags & pflag) {
1905 		*refcount += onswitch ? 1 : -1;
1906 		return (0);
1907 	}
1908 
1909 	/* Save ifnet parameters for if_ioctl() may fail */
1910 	oldcount = *refcount;
1911 	oldflags = ifp->if_flags;
1912 
1913 	/*
1914 	 * See if we aren't the only and touching refcount is enough.
1915 	 * Actually toggle interface flag if we are the first or last.
1916 	 */
1917 	if (onswitch) {
1918 		if ((*refcount)++)
1919 			return (0);
1920 		ifp->if_flags |= flag;
1921 	} else {
1922 		if (--(*refcount))
1923 			return (0);
1924 		ifp->if_flags &= ~flag;
1925 	}
1926 
1927 	/* Call down the driver since we've changed interface flags */
1928 	if (ifp->if_ioctl == NULL) {
1929 		error = EOPNOTSUPP;
1930 		goto recover;
1931 	}
1932 	ifr.ifr_flags = ifp->if_flags & 0xffff;
1933 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
1934 	IFF_LOCKGIANT(ifp);
1935 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1936 	IFF_UNLOCKGIANT(ifp);
1937 	if (error)
1938 		goto recover;
1939 	/* Notify userland that interface flags have changed */
1940 	rt_ifmsg(ifp);
1941 	return (0);
1942 
1943 recover:
1944 	/* Recover after driver error */
1945 	*refcount = oldcount;
1946 	ifp->if_flags = oldflags;
1947 	return (error);
1948 }
1949 
1950 /*
1951  * Set/clear promiscuous mode on interface ifp based on the truth value
1952  * of pswitch.  The calls are reference counted so that only the first
1953  * "on" request actually has an effect, as does the final "off" request.
1954  * Results are undefined if the "off" and "on" requests are not matched.
1955  */
1956 int
1957 ifpromisc(struct ifnet *ifp, int pswitch)
1958 {
1959 	int error;
1960 	int oldflags = ifp->if_flags;
1961 
1962 	error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
1963 			   &ifp->if_pcount, pswitch);
1964 	/* If promiscuous mode status has changed, log a message */
1965 	if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
1966 		log(LOG_INFO, "%s: promiscuous mode %s\n",
1967 		    ifp->if_xname,
1968 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1969 	return (error);
1970 }
1971 
1972 /*
1973  * Return interface configuration
1974  * of system.  List may be used
1975  * in later ioctl's (above) to get
1976  * other information.
1977  */
1978 /*ARGSUSED*/
1979 static int
1980 ifconf(u_long cmd, caddr_t data)
1981 {
1982 	struct ifconf *ifc = (struct ifconf *)data;
1983 #ifdef __amd64__
1984 	struct ifconf32 *ifc32 = (struct ifconf32 *)data;
1985 	struct ifconf ifc_swab;
1986 #endif
1987 	struct ifnet *ifp;
1988 	struct ifaddr *ifa;
1989 	struct ifreq ifr;
1990 	struct sbuf *sb;
1991 	int error, full = 0, valid_len, max_len;
1992 
1993 #ifdef __amd64__
1994 	if (cmd == SIOCGIFCONF32) {
1995 		ifc_swab.ifc_len = ifc32->ifc_len;
1996 		ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
1997 		ifc = &ifc_swab;
1998 	}
1999 #endif
2000 	/* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2001 	max_len = MAXPHYS - 1;
2002 
2003 	/* Prevent hostile input from being able to crash the system */
2004 	if (ifc->ifc_len <= 0)
2005 		return (EINVAL);
2006 
2007 again:
2008 	if (ifc->ifc_len <= max_len) {
2009 		max_len = ifc->ifc_len;
2010 		full = 1;
2011 	}
2012 	sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2013 	max_len = 0;
2014 	valid_len = 0;
2015 
2016 	IFNET_RLOCK();		/* could sleep XXX */
2017 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
2018 		int addrs;
2019 
2020 		/*
2021 		 * Zero the ifr_name buffer to make sure we don't
2022 		 * disclose the contents of the stack.
2023 		 */
2024 		memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2025 
2026 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2027 		    >= sizeof(ifr.ifr_name)) {
2028 			sbuf_delete(sb);
2029 			IFNET_RUNLOCK();
2030 			return (ENAMETOOLONG);
2031 		}
2032 
2033 		addrs = 0;
2034 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2035 			struct sockaddr *sa = ifa->ifa_addr;
2036 
2037 			if (jailed(curthread->td_ucred) &&
2038 			    prison_if(curthread->td_ucred, sa))
2039 				continue;
2040 			addrs++;
2041 #ifdef COMPAT_43
2042 			if (cmd == OSIOCGIFCONF) {
2043 				struct osockaddr *osa =
2044 					 (struct osockaddr *)&ifr.ifr_addr;
2045 				ifr.ifr_addr = *sa;
2046 				osa->sa_family = sa->sa_family;
2047 				sbuf_bcat(sb, &ifr, sizeof(ifr));
2048 				max_len += sizeof(ifr);
2049 			} else
2050 #endif
2051 			if (sa->sa_len <= sizeof(*sa)) {
2052 				ifr.ifr_addr = *sa;
2053 				sbuf_bcat(sb, &ifr, sizeof(ifr));
2054 				max_len += sizeof(ifr);
2055 			} else {
2056 				sbuf_bcat(sb, &ifr,
2057 				    offsetof(struct ifreq, ifr_addr));
2058 				max_len += offsetof(struct ifreq, ifr_addr);
2059 				sbuf_bcat(sb, sa, sa->sa_len);
2060 				max_len += sa->sa_len;
2061 			}
2062 
2063 			if (!sbuf_overflowed(sb))
2064 				valid_len = sbuf_len(sb);
2065 		}
2066 		if (addrs == 0) {
2067 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2068 			sbuf_bcat(sb, &ifr, sizeof(ifr));
2069 			max_len += sizeof(ifr);
2070 
2071 			if (!sbuf_overflowed(sb))
2072 				valid_len = sbuf_len(sb);
2073 		}
2074 	}
2075 	IFNET_RUNLOCK();
2076 
2077 	/*
2078 	 * If we didn't allocate enough space (uncommon), try again.  If
2079 	 * we have already allocated as much space as we are allowed,
2080 	 * return what we've got.
2081 	 */
2082 	if (valid_len != max_len && !full) {
2083 		sbuf_delete(sb);
2084 		goto again;
2085 	}
2086 
2087 	ifc->ifc_len = valid_len;
2088 #ifdef __amd64__
2089 	if (cmd == SIOCGIFCONF32)
2090 		ifc32->ifc_len = valid_len;
2091 #endif
2092 	sbuf_finish(sb);
2093 	error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2094 	sbuf_delete(sb);
2095 	return (error);
2096 }
2097 
2098 /*
2099  * Just like ifpromisc(), but for all-multicast-reception mode.
2100  */
2101 int
2102 if_allmulti(struct ifnet *ifp, int onswitch)
2103 {
2104 
2105 	return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2106 }
2107 
2108 static struct ifmultiaddr *
2109 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2110 {
2111 	struct ifmultiaddr *ifma;
2112 
2113 	IF_ADDR_LOCK_ASSERT(ifp);
2114 
2115 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2116 		if (sa_equal(ifma->ifma_addr, sa))
2117 			break;
2118 	}
2119 
2120 	return ifma;
2121 }
2122 
2123 /*
2124  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
2125  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
2126  * the ifnet multicast address list here, so the caller must do that and
2127  * other setup work (such as notifying the device driver).  The reference
2128  * count is initialized to 1.
2129  */
2130 static struct ifmultiaddr *
2131 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2132     int mflags)
2133 {
2134 	struct ifmultiaddr *ifma;
2135 	struct sockaddr *dupsa;
2136 
2137 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
2138 	    M_ZERO);
2139 	if (ifma == NULL)
2140 		return (NULL);
2141 
2142 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
2143 	if (dupsa == NULL) {
2144 		FREE(ifma, M_IFMADDR);
2145 		return (NULL);
2146 	}
2147 	bcopy(sa, dupsa, sa->sa_len);
2148 	ifma->ifma_addr = dupsa;
2149 
2150 	ifma->ifma_ifp = ifp;
2151 	ifma->ifma_refcount = 1;
2152 	ifma->ifma_protospec = NULL;
2153 
2154 	if (llsa == NULL) {
2155 		ifma->ifma_lladdr = NULL;
2156 		return (ifma);
2157 	}
2158 
2159 	MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
2160 	if (dupsa == NULL) {
2161 		FREE(ifma->ifma_addr, M_IFMADDR);
2162 		FREE(ifma, M_IFMADDR);
2163 		return (NULL);
2164 	}
2165 	bcopy(llsa, dupsa, llsa->sa_len);
2166 	ifma->ifma_lladdr = dupsa;
2167 
2168 	return (ifma);
2169 }
2170 
2171 /*
2172  * if_freemulti: free ifmultiaddr structure and possibly attached related
2173  * addresses.  The caller is responsible for implementing reference
2174  * counting, notifying the driver, handling routing messages, and releasing
2175  * any dependent link layer state.
2176  */
2177 static void
2178 if_freemulti(struct ifmultiaddr *ifma)
2179 {
2180 
2181 	KASSERT(ifma->ifma_refcount == 1, ("if_freemulti: refcount %d",
2182 	    ifma->ifma_refcount));
2183 	KASSERT(ifma->ifma_protospec == NULL,
2184 	    ("if_freemulti: protospec not NULL"));
2185 
2186 	if (ifma->ifma_lladdr != NULL)
2187 		FREE(ifma->ifma_lladdr, M_IFMADDR);
2188 	FREE(ifma->ifma_addr, M_IFMADDR);
2189 	FREE(ifma, M_IFMADDR);
2190 }
2191 
2192 /*
2193  * Register an additional multicast address with a network interface.
2194  *
2195  * - If the address is already present, bump the reference count on the
2196  *   address and return.
2197  * - If the address is not link-layer, look up a link layer address.
2198  * - Allocate address structures for one or both addresses, and attach to the
2199  *   multicast address list on the interface.  If automatically adding a link
2200  *   layer address, the protocol address will own a reference to the link
2201  *   layer address, to be freed when it is freed.
2202  * - Notify the network device driver of an addition to the multicast address
2203  *   list.
2204  *
2205  * 'sa' points to caller-owned memory with the desired multicast address.
2206  *
2207  * 'retifma' will be used to return a pointer to the resulting multicast
2208  * address reference, if desired.
2209  */
2210 int
2211 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2212     struct ifmultiaddr **retifma)
2213 {
2214 	struct ifmultiaddr *ifma, *ll_ifma;
2215 	struct sockaddr *llsa;
2216 	int error;
2217 
2218 	/*
2219 	 * If the address is already present, return a new reference to it;
2220 	 * otherwise, allocate storage and set up a new address.
2221 	 */
2222 	IF_ADDR_LOCK(ifp);
2223 	ifma = if_findmulti(ifp, sa);
2224 	if (ifma != NULL) {
2225 		ifma->ifma_refcount++;
2226 		if (retifma != NULL)
2227 			*retifma = ifma;
2228 		IF_ADDR_UNLOCK(ifp);
2229 		return (0);
2230 	}
2231 
2232 	/*
2233 	 * The address isn't already present; resolve the protocol address
2234 	 * into a link layer address, and then look that up, bump its
2235 	 * refcount or allocate an ifma for that also.  If 'llsa' was
2236 	 * returned, we will need to free it later.
2237 	 */
2238 	llsa = NULL;
2239 	ll_ifma = NULL;
2240 	if (ifp->if_resolvemulti != NULL) {
2241 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
2242 		if (error)
2243 			goto unlock_out;
2244 	}
2245 
2246 	/*
2247 	 * Allocate the new address.  Don't hook it up yet, as we may also
2248 	 * need to allocate a link layer multicast address.
2249 	 */
2250 	ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2251 	if (ifma == NULL) {
2252 		error = ENOMEM;
2253 		goto free_llsa_out;
2254 	}
2255 
2256 	/*
2257 	 * If a link layer address is found, we'll need to see if it's
2258 	 * already present in the address list, or allocate is as well.
2259 	 * When this block finishes, the link layer address will be on the
2260 	 * list.
2261 	 */
2262 	if (llsa != NULL) {
2263 		ll_ifma = if_findmulti(ifp, llsa);
2264 		if (ll_ifma == NULL) {
2265 			ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2266 			if (ll_ifma == NULL) {
2267 				if_freemulti(ifma);
2268 				error = ENOMEM;
2269 				goto free_llsa_out;
2270 			}
2271 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2272 			    ifma_link);
2273 		} else
2274 			ll_ifma->ifma_refcount++;
2275 	}
2276 
2277 	/*
2278 	 * We now have a new multicast address, ifma, and possibly a new or
2279 	 * referenced link layer address.  Add the primary address to the
2280 	 * ifnet address list.
2281 	 */
2282 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2283 
2284 	if (retifma != NULL)
2285 		*retifma = ifma;
2286 
2287 	/*
2288 	 * Must generate the message while holding the lock so that 'ifma'
2289 	 * pointer is still valid.
2290 	 *
2291 	 * XXXRW: How come we don't announce ll_ifma?
2292 	 */
2293 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2294 	IF_ADDR_UNLOCK(ifp);
2295 
2296 	/*
2297 	 * We are certain we have added something, so call down to the
2298 	 * interface to let them know about it.
2299 	 */
2300 	if (ifp->if_ioctl != NULL) {
2301 		IFF_LOCKGIANT(ifp);
2302 		(void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2303 		IFF_UNLOCKGIANT(ifp);
2304 	}
2305 
2306 	if (llsa != NULL)
2307 		FREE(llsa, M_IFMADDR);
2308 
2309 	return (0);
2310 
2311 free_llsa_out:
2312 	if (llsa != NULL)
2313 		FREE(llsa, M_IFMADDR);
2314 
2315 unlock_out:
2316 	IF_ADDR_UNLOCK(ifp);
2317 	return (error);
2318 }
2319 
2320 /*
2321  * Remove a reference to a multicast address on this interface.  Yell
2322  * if the request does not match an existing membership.
2323  */
2324 int
2325 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2326 {
2327 	struct ifmultiaddr *ifma, *ll_ifma;
2328 
2329 	IF_ADDR_LOCK(ifp);
2330 	ifma = if_findmulti(ifp, sa);
2331 	if (ifma == NULL) {
2332 		IF_ADDR_UNLOCK(ifp);
2333 		return ENOENT;
2334 	}
2335 
2336 	if (ifma->ifma_refcount > 1) {
2337 		ifma->ifma_refcount--;
2338 		IF_ADDR_UNLOCK(ifp);
2339 		return 0;
2340 	}
2341 
2342 	sa = ifma->ifma_lladdr;
2343 	if (sa != NULL)
2344 		ll_ifma = if_findmulti(ifp, sa);
2345 	else
2346 		ll_ifma = NULL;
2347 
2348 	/*
2349 	 * XXXRW: How come we don't announce ll_ifma?
2350 	 */
2351 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
2352 
2353 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2354 	if_freemulti(ifma);
2355 
2356 	if (ll_ifma != NULL) {
2357 		if (ll_ifma->ifma_refcount == 1) {
2358 			TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifma_link);
2359 			if_freemulti(ll_ifma);
2360 		} else
2361 			ll_ifma->ifma_refcount--;
2362 	}
2363 	IF_ADDR_UNLOCK(ifp);
2364 
2365 	/*
2366 	 * Make sure the interface driver is notified
2367 	 * in the case of a link layer mcast group being left.
2368 	 */
2369 	if (ifp->if_ioctl) {
2370 		IFF_LOCKGIANT(ifp);
2371 		(void) (*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2372 		IFF_UNLOCKGIANT(ifp);
2373 	}
2374 
2375 	return 0;
2376 }
2377 
2378 /*
2379  * Set the link layer address on an interface.
2380  *
2381  * At this time we only support certain types of interfaces,
2382  * and we don't allow the length of the address to change.
2383  */
2384 int
2385 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2386 {
2387 	struct sockaddr_dl *sdl;
2388 	struct ifaddr *ifa;
2389 	struct ifreq ifr;
2390 
2391 	ifa = ifp->if_addr;
2392 	if (ifa == NULL)
2393 		return (EINVAL);
2394 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2395 	if (sdl == NULL)
2396 		return (EINVAL);
2397 	if (len != sdl->sdl_alen)	/* don't allow length to change */
2398 		return (EINVAL);
2399 	switch (ifp->if_type) {
2400 	case IFT_ETHER:
2401 	case IFT_FDDI:
2402 	case IFT_XETHER:
2403 	case IFT_ISO88025:
2404 	case IFT_L2VLAN:
2405 	case IFT_BRIDGE:
2406 	case IFT_ARCNET:
2407 		bcopy(lladdr, LLADDR(sdl), len);
2408 		break;
2409 	default:
2410 		return (ENODEV);
2411 	}
2412 	/*
2413 	 * If the interface is already up, we need
2414 	 * to re-init it in order to reprogram its
2415 	 * address filter.
2416 	 */
2417 	if ((ifp->if_flags & IFF_UP) != 0) {
2418 		if (ifp->if_ioctl) {
2419 			IFF_LOCKGIANT(ifp);
2420 			ifp->if_flags &= ~IFF_UP;
2421 			ifr.ifr_flags = ifp->if_flags & 0xffff;
2422 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
2423 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2424 			ifp->if_flags |= IFF_UP;
2425 			ifr.ifr_flags = ifp->if_flags & 0xffff;
2426 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
2427 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2428 			IFF_UNLOCKGIANT(ifp);
2429 		}
2430 #ifdef INET
2431 		/*
2432 		 * Also send gratuitous ARPs to notify other nodes about
2433 		 * the address change.
2434 		 */
2435 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2436 			if (ifa->ifa_addr->sa_family == AF_INET)
2437 				arp_ifinit(ifp, ifa);
2438 		}
2439 #endif
2440 	}
2441 	return (0);
2442 }
2443 
2444 /*
2445  * The name argument must be a pointer to storage which will last as
2446  * long as the interface does.  For physical devices, the result of
2447  * device_get_name(dev) is a good choice and for pseudo-devices a
2448  * static string works well.
2449  */
2450 void
2451 if_initname(struct ifnet *ifp, const char *name, int unit)
2452 {
2453 	ifp->if_dname = name;
2454 	ifp->if_dunit = unit;
2455 	if (unit != IF_DUNIT_NONE)
2456 		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2457 	else
2458 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
2459 }
2460 
2461 int
2462 if_printf(struct ifnet *ifp, const char * fmt, ...)
2463 {
2464 	va_list ap;
2465 	int retval;
2466 
2467 	retval = printf("%s: ", ifp->if_xname);
2468 	va_start(ap, fmt);
2469 	retval += vprintf(fmt, ap);
2470 	va_end(ap);
2471 	return (retval);
2472 }
2473 
2474 /*
2475  * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2476  * be called without Giant.  However, we often can't acquire the Giant lock
2477  * at those points; instead, we run it via a task queue that holds Giant via
2478  * if_start_deferred.
2479  *
2480  * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2481  * outstanding if_start_deferred() tasks that will run after the free.  This
2482  * probably means waiting in if_detach().
2483  */
2484 void
2485 if_start(struct ifnet *ifp)
2486 {
2487 
2488 	NET_ASSERT_GIANT();
2489 
2490 	if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
2491 		if (mtx_owned(&Giant))
2492 			(*(ifp)->if_start)(ifp);
2493 		else
2494 			taskqueue_enqueue(taskqueue_swi_giant,
2495 			    &ifp->if_starttask);
2496 	} else
2497 		(*(ifp)->if_start)(ifp);
2498 }
2499 
2500 static void
2501 if_start_deferred(void *context, int pending)
2502 {
2503 	struct ifnet *ifp;
2504 
2505 	/*
2506 	 * This code must be entered with Giant, and should never run if
2507 	 * we're not running with debug.mpsafenet.
2508 	 */
2509 	KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
2510 	GIANT_REQUIRED;
2511 
2512 	ifp = context;
2513 	(ifp->if_start)(ifp);
2514 }
2515 
2516 int
2517 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2518 {
2519 	int active = 0;
2520 
2521 	IF_LOCK(ifq);
2522 	if (_IF_QFULL(ifq)) {
2523 		_IF_DROP(ifq);
2524 		IF_UNLOCK(ifq);
2525 		m_freem(m);
2526 		return (0);
2527 	}
2528 	if (ifp != NULL) {
2529 		ifp->if_obytes += m->m_pkthdr.len + adjust;
2530 		if (m->m_flags & (M_BCAST|M_MCAST))
2531 			ifp->if_omcasts++;
2532 		active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2533 	}
2534 	_IF_ENQUEUE(ifq, m);
2535 	IF_UNLOCK(ifq);
2536 	if (ifp != NULL && !active)
2537 		if_start(ifp);
2538 	return (1);
2539 }
2540 
2541 void
2542 if_register_com_alloc(u_char type,
2543     if_com_alloc_t *a, if_com_free_t *f)
2544 {
2545 
2546 	KASSERT(if_com_alloc[type] == NULL,
2547 	    ("if_register_com_alloc: %d already registered", type));
2548 	KASSERT(if_com_free[type] == NULL,
2549 	    ("if_register_com_alloc: %d free already registered", type));
2550 
2551 	if_com_alloc[type] = a;
2552 	if_com_free[type] = f;
2553 }
2554 
2555 void
2556 if_deregister_com_alloc(u_char type)
2557 {
2558 
2559 	KASSERT(if_com_alloc[type] != NULL,
2560 	    ("if_deregister_com_alloc: %d not registered", type));
2561 	KASSERT(if_com_free[type] != NULL,
2562 	    ("if_deregister_com_alloc: %d free not registered", type));
2563 	if_com_alloc[type] = NULL;
2564 	if_com_free[type] = NULL;
2565 }
2566