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