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