xref: /freebsd/sys/net/if.c (revision 6e0da4f753ed6b5d26395001a6194b4fdea70177)
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 
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/conf.h>
41 #include <sys/mac.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/proc.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
52 #include <sys/sockio.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
55 #include <sys/taskqueue.h>
56 #include <sys/domain.h>
57 #include <sys/jail.h>
58 #include <machine/stdarg.h>
59 
60 #include <net/if.h>
61 #include <net/if_arp.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 
82 void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
83 
84 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
85 
86 static void	if_attachdomain(void *);
87 static void	if_attachdomain1(struct ifnet *);
88 static int	ifconf(u_long, caddr_t);
89 static void	if_grow(void);
90 static void	if_init(void *);
91 static void	if_check(void *);
92 static int	if_findindex(struct ifnet *);
93 static void	if_qflush(struct ifaltq *);
94 static void	if_route(struct ifnet *, int flag, int fam);
95 static void	if_slowtimo(void *);
96 static void	if_unroute(struct ifnet *, int flag, int fam);
97 static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
98 static int	if_rtdel(struct radix_node *, void *);
99 static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
100 static void	if_start_deferred(void *context, int pending);
101 #ifdef INET6
102 /*
103  * XXX: declare here to avoid to include many inet6 related files..
104  * should be more generalized?
105  */
106 extern void	nd6_setmtu(struct ifnet *);
107 #endif
108 
109 int	if_index = 0;
110 struct	ifindex_entry *ifindex_table = NULL;
111 int	ifqmaxlen = IFQ_MAXLEN;
112 struct	ifnethead ifnet;	/* depend on static init XXX */
113 struct	mtx ifnet_lock;
114 
115 static int	if_indexlim = 8;
116 static struct	knlist ifklist;
117 
118 static void	filt_netdetach(struct knote *kn);
119 static int	filt_netdev(struct knote *kn, long hint);
120 
121 static struct filterops netdev_filtops =
122     { 1, NULL, filt_netdetach, filt_netdev };
123 
124 /*
125  * System initialization
126  */
127 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
128 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
129 
130 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
131 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
132 
133 static d_open_t		netopen;
134 static d_close_t	netclose;
135 static d_ioctl_t	netioctl;
136 static d_kqfilter_t	netkqfilter;
137 
138 static struct cdevsw net_cdevsw = {
139 	.d_version =	D_VERSION,
140 	.d_flags =	D_NEEDGIANT,
141 	.d_open =	netopen,
142 	.d_close =	netclose,
143 	.d_ioctl =	netioctl,
144 	.d_name =	"net",
145 	.d_kqfilter =	netkqfilter,
146 };
147 
148 static int
149 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
150 {
151 	return (0);
152 }
153 
154 static int
155 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
156 {
157 	return (0);
158 }
159 
160 static int
161 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
162 {
163 	struct ifnet *ifp;
164 	int error, idx;
165 
166 	/* only support interface specific ioctls */
167 	if (IOCGROUP(cmd) != 'i')
168 		return (EOPNOTSUPP);
169 	idx = minor(dev);
170 	if (idx == 0) {
171 		/*
172 		 * special network device, not interface.
173 		 */
174 		if (cmd == SIOCGIFCONF)
175 			return (ifconf(cmd, data));	/* XXX remove cmd */
176 		return (EOPNOTSUPP);
177 	}
178 
179 	ifp = ifnet_byindex(idx);
180 	if (ifp == NULL)
181 		return (ENXIO);
182 
183 	error = ifhwioctl(cmd, ifp, data, td);
184 	if (error == ENOIOCTL)
185 		error = EOPNOTSUPP;
186 	return (error);
187 }
188 
189 static int
190 netkqfilter(struct cdev *dev, struct knote *kn)
191 {
192 	struct knlist *klist;
193 	struct ifnet *ifp;
194 	int idx;
195 
196 	switch (kn->kn_filter) {
197 	case EVFILT_NETDEV:
198 		kn->kn_fop = &netdev_filtops;
199 		break;
200 	default:
201 		return (1);
202 	}
203 
204 	idx = minor(dev);
205 	if (idx == 0) {
206 		klist = &ifklist;
207 	} else {
208 		ifp = ifnet_byindex(idx);
209 		if (ifp == NULL)
210 			return (1);
211 		klist = &ifp->if_klist;
212 	}
213 
214 	kn->kn_hook = (caddr_t)klist;
215 
216 	knlist_add(klist, kn, 0);
217 
218 	return (0);
219 }
220 
221 static void
222 filt_netdetach(struct knote *kn)
223 {
224 	struct knlist *klist = (struct knlist *)kn->kn_hook;
225 
226 	knlist_remove(klist, kn, 0);
227 }
228 
229 static int
230 filt_netdev(struct knote *kn, long hint)
231 {
232 	struct knlist *klist = (struct knlist *)kn->kn_hook;
233 
234 	/*
235 	 * Currently NOTE_EXIT is abused to indicate device detach.
236 	 */
237 	if (hint == NOTE_EXIT) {
238 		kn->kn_data = NOTE_LINKINV;
239 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
240 		knlist_remove_inevent(klist, kn);
241 		return (1);
242 	}
243 	if (hint != 0)
244 		kn->kn_data = hint;			/* current status */
245 	if (kn->kn_sfflags & hint)
246 		kn->kn_fflags |= hint;
247 	return (kn->kn_fflags != 0);
248 }
249 
250 /*
251  * Network interface utility routines.
252  *
253  * Routines with ifa_ifwith* names take sockaddr *'s as
254  * parameters.
255  */
256 /* ARGSUSED*/
257 static void
258 if_init(void *dummy __unused)
259 {
260 
261 	IFNET_LOCK_INIT();
262 	TAILQ_INIT(&ifnet);
263 	knlist_init(&ifklist, NULL);
264 	if_grow();				/* create initial table */
265 	ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
266 	    UID_ROOT, GID_WHEEL, 0600, "network");
267 	if_clone_init();
268 }
269 
270 static void
271 if_grow(void)
272 {
273 	u_int n;
274 	struct ifindex_entry *e;
275 
276 	if_indexlim <<= 1;
277 	n = if_indexlim * sizeof(*e);
278 	e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
279 	if (ifindex_table != NULL) {
280 		memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
281 		free((caddr_t)ifindex_table, M_IFADDR);
282 	}
283 	ifindex_table = e;
284 }
285 
286 /* ARGSUSED*/
287 static void
288 if_check(void *dummy __unused)
289 {
290 	struct ifnet *ifp;
291 	int s;
292 
293 	s = splimp();
294 	IFNET_RLOCK();	/* could sleep on rare error; mostly okay XXX */
295 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
296 		if (ifp->if_snd.ifq_maxlen == 0) {
297 			if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
298 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
299 		}
300 		if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
301 			if_printf(ifp,
302 			    "XXX: driver didn't initialize queue mtx\n");
303 			mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
304 			    MTX_NETWORK_LOCK, MTX_DEF);
305 		}
306 	}
307 	IFNET_RUNLOCK();
308 	splx(s);
309 	if_slowtimo(0);
310 }
311 
312 static int
313 if_findindex(struct ifnet *ifp)
314 {
315 	int i, unit;
316 	char eaddr[18], devname[32];
317 	const char *name, *p;
318 
319 	switch (ifp->if_type) {
320 	case IFT_ETHER:			/* these types use struct arpcom */
321 	case IFT_FDDI:
322 	case IFT_XETHER:
323 	case IFT_ISO88025:
324 	case IFT_L2VLAN:
325 		snprintf(eaddr, 18, "%6D", IFP2AC(ifp)->ac_enaddr, ":");
326 		break;
327 	default:
328 		eaddr[0] = '\0';
329 		break;
330 	}
331 	strlcpy(devname, ifp->if_xname, sizeof(devname));
332 	name = net_cdevsw.d_name;
333 	i = 0;
334 	while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
335 		if (resource_string_value(name, unit, "ether", &p) == 0)
336 			if (strcmp(p, eaddr) == 0)
337 				goto found;
338 		if (resource_string_value(name, unit, "dev", &p) == 0)
339 			if (strcmp(p, devname) == 0)
340 				goto found;
341 	}
342 	unit = 0;
343 found:
344 	if (unit != 0) {
345 		if (ifaddr_byindex(unit) == NULL)
346 			return (unit);
347 		printf("%s%d in use, cannot hardwire it to %s.\n",
348 		    name, unit, devname);
349 	}
350 	for (unit = 1; ; unit++) {
351 		if (unit <= if_index && ifaddr_byindex(unit) != NULL)
352 			continue;
353 		if (resource_string_value(name, unit, "ether", &p) == 0 ||
354 		    resource_string_value(name, unit, "dev", &p) == 0)
355 			continue;
356 		break;
357 	}
358 	return (unit);
359 }
360 
361 /*
362  * Attach an interface to the
363  * list of "active" interfaces.
364  */
365 void
366 if_attach(struct ifnet *ifp)
367 {
368 	unsigned socksize, ifasize;
369 	int namelen, masklen;
370 	struct sockaddr_dl *sdl;
371 	struct ifaddr *ifa;
372 
373 	TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
374 	IF_AFDATA_LOCK_INIT(ifp);
375 	ifp->if_afdata_initialized = 0;
376 	IFNET_WLOCK();
377 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
378 	IFNET_WUNLOCK();
379 	/*
380 	 * XXX -
381 	 * The old code would work if the interface passed a pre-existing
382 	 * chain of ifaddrs to this code.  We don't trust our callers to
383 	 * properly initialize the tailq, however, so we no longer allow
384 	 * this unlikely case.
385 	 */
386 	TAILQ_INIT(&ifp->if_addrhead);
387 	TAILQ_INIT(&ifp->if_prefixhead);
388 	TAILQ_INIT(&ifp->if_multiaddrs);
389 	knlist_init(&ifp->if_klist, NULL);
390 	getmicrotime(&ifp->if_lastchange);
391 	ifp->if_data.ifi_epoch = time_second;
392 
393 #ifdef MAC
394 	mac_init_ifnet(ifp);
395 	mac_create_ifnet(ifp);
396 #endif
397 
398 	ifp->if_index = if_findindex(ifp);
399 	if (ifp->if_index > if_index)
400 		if_index = ifp->if_index;
401 	if (if_index >= if_indexlim)
402 		if_grow();
403 	ifp->if_data.ifi_datalen = sizeof(struct if_data);
404 
405 	ifnet_byindex(ifp->if_index) = ifp;
406 	ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
407 	    unit2minor(ifp->if_index),
408 	    UID_ROOT, GID_WHEEL, 0600, "%s/%s",
409 	    net_cdevsw.d_name, ifp->if_xname);
410 	make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
411 	    net_cdevsw.d_name, ifp->if_index);
412 
413 	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
414 
415 	/*
416 	 * create a Link Level name for this device
417 	 */
418 	namelen = strlen(ifp->if_xname);
419 	/*
420 	 * Always save enough space for any possiable name so we can do
421 	 * a rename in place later.
422 	 */
423 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
424 	socksize = masklen + ifp->if_addrlen;
425 	if (socksize < sizeof(*sdl))
426 		socksize = sizeof(*sdl);
427 	socksize = roundup2(socksize, sizeof(long));
428 	ifasize = sizeof(*ifa) + 2 * socksize;
429 	ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
430 	IFA_LOCK_INIT(ifa);
431 	sdl = (struct sockaddr_dl *)(ifa + 1);
432 	sdl->sdl_len = socksize;
433 	sdl->sdl_family = AF_LINK;
434 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
435 	sdl->sdl_nlen = namelen;
436 	sdl->sdl_index = ifp->if_index;
437 	sdl->sdl_type = ifp->if_type;
438 	ifaddr_byindex(ifp->if_index) = ifa;
439 	ifa->ifa_ifp = ifp;
440 	ifa->ifa_rtrequest = link_rtrequest;
441 	ifa->ifa_addr = (struct sockaddr *)sdl;
442 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
443 	ifa->ifa_netmask = (struct sockaddr *)sdl;
444 	sdl->sdl_len = masklen;
445 	while (namelen != 0)
446 		sdl->sdl_data[--namelen] = 0xff;
447 	ifa->ifa_refcnt = 1;
448 	TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
449 	ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
450 	ifp->if_snd.altq_type = 0;
451 	ifp->if_snd.altq_disc = NULL;
452 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
453 	ifp->if_snd.altq_tbr  = NULL;
454 	ifp->if_snd.altq_ifp  = ifp;
455 
456 	if (domain_init_status >= 2)
457 		if_attachdomain1(ifp);
458 
459 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
460 
461 	/* Announce the interface. */
462 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
463 }
464 
465 static void
466 if_attachdomain(void *dummy)
467 {
468 	struct ifnet *ifp;
469 	int s;
470 
471 	s = splnet();
472 	TAILQ_FOREACH(ifp, &ifnet, if_link)
473 		if_attachdomain1(ifp);
474 	splx(s);
475 }
476 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
477     if_attachdomain, NULL);
478 
479 static void
480 if_attachdomain1(struct ifnet *ifp)
481 {
482 	struct domain *dp;
483 	int s;
484 
485 	s = splnet();
486 
487 	/*
488 	 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
489 	 * cannot lock ifp->if_afdata initialization, entirely.
490 	 */
491 	if (IF_AFDATA_TRYLOCK(ifp) == 0) {
492 		splx(s);
493 		return;
494 	}
495 	if (ifp->if_afdata_initialized >= domain_init_status) {
496 		IF_AFDATA_UNLOCK(ifp);
497 		splx(s);
498 		printf("if_attachdomain called more than once on %s\n",
499 		    ifp->if_xname);
500 		return;
501 	}
502 	ifp->if_afdata_initialized = domain_init_status;
503 	IF_AFDATA_UNLOCK(ifp);
504 
505 	/* address family dependent data region */
506 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
507 	for (dp = domains; dp; dp = dp->dom_next) {
508 		if (dp->dom_ifattach)
509 			ifp->if_afdata[dp->dom_family] =
510 			    (*dp->dom_ifattach)(ifp);
511 	}
512 
513 	splx(s);
514 }
515 
516 /*
517  * Detach an interface, removing it from the
518  * list of "active" interfaces.
519  */
520 void
521 if_detach(struct ifnet *ifp)
522 {
523 	struct ifaddr *ifa, *next;
524 	struct radix_node_head	*rnh;
525 	int s;
526 	int i;
527 	struct domain *dp;
528  	struct ifnet *iter;
529  	int found;
530 
531 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
532 	/*
533 	 * Remove routes and flush queues.
534 	 */
535 	s = splnet();
536 	if_down(ifp);
537 #ifdef ALTQ
538 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
539 		altq_disable(&ifp->if_snd);
540 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
541 		altq_detach(&ifp->if_snd);
542 #endif
543 
544 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; ifa = next) {
545 		next = TAILQ_NEXT(ifa, ifa_link);
546 
547 		if (ifa->ifa_addr->sa_family == AF_LINK)
548 			continue;
549 #ifdef INET
550 		/* XXX: Ugly!! ad hoc just for INET */
551 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
552 			struct ifaliasreq ifr;
553 
554 			bzero(&ifr, sizeof(ifr));
555 			ifr.ifra_addr = *ifa->ifa_addr;
556 			if (ifa->ifa_dstaddr)
557 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
558 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
559 			    NULL) == 0)
560 				continue;
561 		}
562 #endif /* INET */
563 #ifdef INET6
564 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
565 			in6_purgeaddr(ifa);
566 			/* ifp_addrhead is already updated */
567 			continue;
568 		}
569 #endif /* INET6 */
570 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
571 		IFAFREE(ifa);
572 	}
573 
574 #ifdef INET6
575 	/*
576 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
577 	 * before removing routing entries below, since IPv6 interface direct
578 	 * routes are expected to be removed by the IPv6-specific kernel API.
579 	 * Otherwise, the kernel will detect some inconsistency and bark it.
580 	 */
581 	in6_ifdetach(ifp);
582 #endif
583 	/*
584 	 * Remove address from ifindex_table[] and maybe decrement if_index.
585 	 * Clean up all addresses.
586 	 */
587 	ifnet_byindex(ifp->if_index) = NULL;
588 	ifaddr_byindex(ifp->if_index) = NULL;
589 	destroy_dev(ifdev_byindex(ifp->if_index));
590 	ifdev_byindex(ifp->if_index) = NULL;
591 
592 	while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
593 		if_index--;
594 
595 
596 	/* We can now free link ifaddr. */
597 	if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
598 		ifa = TAILQ_FIRST(&ifp->if_addrhead);
599 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
600 		IFAFREE(ifa);
601 	}
602 
603 	/*
604 	 * Delete all remaining routes using this interface
605 	 * Unfortuneatly the only way to do this is to slog through
606 	 * the entire routing table looking for routes which point
607 	 * to this interface...oh well...
608 	 */
609 	for (i = 1; i <= AF_MAX; i++) {
610 		if ((rnh = rt_tables[i]) == NULL)
611 			continue;
612 		RADIX_NODE_HEAD_LOCK(rnh);
613 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
614 		RADIX_NODE_HEAD_UNLOCK(rnh);
615 	}
616 
617 	/* Announce that the interface is gone. */
618 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
619 
620 	IF_AFDATA_LOCK(ifp);
621 	for (dp = domains; dp; dp = dp->dom_next) {
622 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
623 			(*dp->dom_ifdetach)(ifp,
624 			    ifp->if_afdata[dp->dom_family]);
625 	}
626 	IF_AFDATA_UNLOCK(ifp);
627 
628 #ifdef MAC
629 	mac_destroy_ifnet(ifp);
630 #endif /* MAC */
631 	KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
632 	knlist_clear(&ifp->if_klist, 0);
633 	knlist_destroy(&ifp->if_klist);
634 	IFNET_WLOCK();
635  	found = 0;
636  	TAILQ_FOREACH(iter, &ifnet, if_link)
637  		if (iter == ifp) {
638  			found = 1;
639  			break;
640  		}
641  	if (found)
642  		TAILQ_REMOVE(&ifnet, ifp, if_link);
643 	IFNET_WUNLOCK();
644 	mtx_destroy(&ifp->if_snd.ifq_mtx);
645 	IF_AFDATA_DESTROY(ifp);
646 	splx(s);
647 }
648 
649 /*
650  * Delete Routes for a Network Interface
651  *
652  * Called for each routing entry via the rnh->rnh_walktree() call above
653  * to delete all route entries referencing a detaching network interface.
654  *
655  * Arguments:
656  *	rn	pointer to node in the routing table
657  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
658  *
659  * Returns:
660  *	0	successful
661  *	errno	failed - reason indicated
662  *
663  */
664 static int
665 if_rtdel(struct radix_node *rn, void *arg)
666 {
667 	struct rtentry	*rt = (struct rtentry *)rn;
668 	struct ifnet	*ifp = arg;
669 	int		err;
670 
671 	if (rt->rt_ifp == ifp) {
672 
673 		/*
674 		 * Protect (sorta) against walktree recursion problems
675 		 * with cloned routes
676 		 */
677 		if ((rt->rt_flags & RTF_UP) == 0)
678 			return (0);
679 
680 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
681 				rt_mask(rt), rt->rt_flags,
682 				(struct rtentry **) NULL);
683 		if (err) {
684 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
685 		}
686 	}
687 
688 	return (0);
689 }
690 
691 #define	equal(a1, a2)	(bcmp((a1), (a2), ((a1))->sa_len) == 0)
692 
693 /*
694  * Locate an interface based on a complete address.
695  */
696 /*ARGSUSED*/
697 struct ifaddr *
698 ifa_ifwithaddr(struct sockaddr *addr)
699 {
700 	struct ifnet *ifp;
701 	struct ifaddr *ifa;
702 
703 	IFNET_RLOCK();
704 	TAILQ_FOREACH(ifp, &ifnet, if_link)
705 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
706 			if (ifa->ifa_addr->sa_family != addr->sa_family)
707 				continue;
708 			if (equal(addr, ifa->ifa_addr))
709 				goto done;
710 			/* IP6 doesn't have broadcast */
711 			if ((ifp->if_flags & IFF_BROADCAST) &&
712 			    ifa->ifa_broadaddr &&
713 			    ifa->ifa_broadaddr->sa_len != 0 &&
714 			    equal(ifa->ifa_broadaddr, addr))
715 				goto done;
716 		}
717 	ifa = NULL;
718 done:
719 	IFNET_RUNLOCK();
720 	return (ifa);
721 }
722 
723 /*
724  * Locate the point to point interface with a given destination address.
725  */
726 /*ARGSUSED*/
727 struct ifaddr *
728 ifa_ifwithdstaddr(struct sockaddr *addr)
729 {
730 	struct ifnet *ifp;
731 	struct ifaddr *ifa;
732 
733 	IFNET_RLOCK();
734 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
735 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
736 			continue;
737 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
738 			if (ifa->ifa_addr->sa_family != addr->sa_family)
739 				continue;
740 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
741 				goto done;
742 		}
743 	}
744 	ifa = NULL;
745 done:
746 	IFNET_RUNLOCK();
747 	return (ifa);
748 }
749 
750 /*
751  * Find an interface on a specific network.  If many, choice
752  * is most specific found.
753  */
754 struct ifaddr *
755 ifa_ifwithnet(struct sockaddr *addr)
756 {
757 	struct ifnet *ifp;
758 	struct ifaddr *ifa;
759 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
760 	u_int af = addr->sa_family;
761 	char *addr_data = addr->sa_data, *cplim;
762 
763 	/*
764 	 * AF_LINK addresses can be looked up directly by their index number,
765 	 * so do that if we can.
766 	 */
767 	if (af == AF_LINK) {
768 	    struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
769 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
770 		return (ifaddr_byindex(sdl->sdl_index));
771 	}
772 
773 	/*
774 	 * Scan though each interface, looking for ones that have
775 	 * addresses in this address family.
776 	 */
777 	IFNET_RLOCK();
778 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
779 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
780 			char *cp, *cp2, *cp3;
781 
782 			if (ifa->ifa_addr->sa_family != af)
783 next:				continue;
784 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
785 				/*
786 				 * This is a bit broken as it doesn't
787 				 * take into account that the remote end may
788 				 * be a single node in the network we are
789 				 * looking for.
790 				 * The trouble is that we don't know the
791 				 * netmask for the remote end.
792 				 */
793 				if (ifa->ifa_dstaddr != 0
794 				    && equal(addr, ifa->ifa_dstaddr))
795 					goto done;
796 			} else {
797 				/*
798 				 * if we have a special address handler,
799 				 * then use it instead of the generic one.
800 				 */
801 				if (ifa->ifa_claim_addr) {
802 					if ((*ifa->ifa_claim_addr)(ifa, addr))
803 						goto done;
804 					continue;
805 				}
806 
807 				/*
808 				 * Scan all the bits in the ifa's address.
809 				 * If a bit dissagrees with what we are
810 				 * looking for, mask it with the netmask
811 				 * to see if it really matters.
812 				 * (A byte at a time)
813 				 */
814 				if (ifa->ifa_netmask == 0)
815 					continue;
816 				cp = addr_data;
817 				cp2 = ifa->ifa_addr->sa_data;
818 				cp3 = ifa->ifa_netmask->sa_data;
819 				cplim = ifa->ifa_netmask->sa_len
820 					+ (char *)ifa->ifa_netmask;
821 				while (cp3 < cplim)
822 					if ((*cp++ ^ *cp2++) & *cp3++)
823 						goto next; /* next address! */
824 				/*
825 				 * If the netmask of what we just found
826 				 * is more specific than what we had before
827 				 * (if we had one) then remember the new one
828 				 * before continuing to search
829 				 * for an even better one.
830 				 */
831 				if (ifa_maybe == 0 ||
832 				    rn_refines((caddr_t)ifa->ifa_netmask,
833 				    (caddr_t)ifa_maybe->ifa_netmask))
834 					ifa_maybe = ifa;
835 			}
836 		}
837 	}
838 	ifa = ifa_maybe;
839 done:
840 	IFNET_RUNLOCK();
841 	return (ifa);
842 }
843 
844 /*
845  * Find an interface address specific to an interface best matching
846  * a given address.
847  */
848 struct ifaddr *
849 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
850 {
851 	struct ifaddr *ifa;
852 	char *cp, *cp2, *cp3;
853 	char *cplim;
854 	struct ifaddr *ifa_maybe = 0;
855 	u_int af = addr->sa_family;
856 
857 	if (af >= AF_MAX)
858 		return (0);
859 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
860 		if (ifa->ifa_addr->sa_family != af)
861 			continue;
862 		if (ifa_maybe == 0)
863 			ifa_maybe = ifa;
864 		if (ifa->ifa_netmask == 0) {
865 			if (equal(addr, ifa->ifa_addr) ||
866 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
867 				goto done;
868 			continue;
869 		}
870 		if (ifp->if_flags & IFF_POINTOPOINT) {
871 			if (equal(addr, ifa->ifa_dstaddr))
872 				goto done;
873 		} else {
874 			cp = addr->sa_data;
875 			cp2 = ifa->ifa_addr->sa_data;
876 			cp3 = ifa->ifa_netmask->sa_data;
877 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
878 			for (; cp3 < cplim; cp3++)
879 				if ((*cp++ ^ *cp2++) & *cp3)
880 					break;
881 			if (cp3 == cplim)
882 				goto done;
883 		}
884 	}
885 	ifa = ifa_maybe;
886 done:
887 	return (ifa);
888 }
889 
890 #include <net/route.h>
891 
892 /*
893  * Default action when installing a route with a Link Level gateway.
894  * Lookup an appropriate real ifa to point to.
895  * This should be moved to /sys/net/link.c eventually.
896  */
897 static void
898 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
899 {
900 	struct ifaddr *ifa, *oifa;
901 	struct sockaddr *dst;
902 	struct ifnet *ifp;
903 
904 	RT_LOCK_ASSERT(rt);
905 
906 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
907 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
908 		return;
909 	ifa = ifaof_ifpforaddr(dst, ifp);
910 	if (ifa) {
911 		IFAREF(ifa);		/* XXX */
912 		oifa = rt->rt_ifa;
913 		rt->rt_ifa = ifa;
914 		IFAFREE(oifa);
915 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
916 			ifa->ifa_rtrequest(cmd, rt, info);
917 	}
918 }
919 
920 /*
921  * Mark an interface down and notify protocols of
922  * the transition.
923  * NOTE: must be called at splnet or eqivalent.
924  */
925 static void
926 if_unroute(struct ifnet *ifp, int flag, int fam)
927 {
928 	struct ifaddr *ifa;
929 
930 	ifp->if_flags &= ~flag;
931 	getmicrotime(&ifp->if_lastchange);
932 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
933 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
934 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
935 	if_qflush(&ifp->if_snd);
936 	rt_ifmsg(ifp);
937 }
938 
939 /*
940  * Mark an interface up and notify protocols of
941  * the transition.
942  * NOTE: must be called at splnet or eqivalent.
943  */
944 static void
945 if_route(struct ifnet *ifp, int flag, int fam)
946 {
947 	struct ifaddr *ifa;
948 
949 	ifp->if_flags |= flag;
950 	getmicrotime(&ifp->if_lastchange);
951 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
952 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
953 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
954 	rt_ifmsg(ifp);
955 #ifdef INET6
956 	in6_if_up(ifp);
957 #endif
958 }
959 
960 void	(*vlan_link_state_p)(struct ifnet *, int);	/* XXX: private from if_vlan */
961 
962 /*
963  * Handle a change in the interface link state.
964  */
965 void
966 if_link_state_change(struct ifnet *ifp, int link_state)
967 {
968 	int link;
969 
970 	/* Notify that the link state has changed. */
971 	if (ifp->if_link_state != link_state) {
972 		ifp->if_link_state = link_state;
973 		rt_ifmsg(ifp);
974 		if (link_state == LINK_STATE_UP)
975 			link = NOTE_LINKUP;
976 		else if (link_state == LINK_STATE_DOWN)
977 			link = NOTE_LINKDOWN;
978 		else
979 			link = NOTE_LINKINV;
980 		KNOTE_UNLOCKED(&ifp->if_klist, link);
981 		if (ifp->if_nvlans != 0)
982 			(*vlan_link_state_p)(ifp, link);
983 
984 		if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
985 		    IFP2AC(ifp)->ac_netgraph != NULL)
986 			(*ng_ether_link_state_p)(ifp, link_state);
987 	}
988 }
989 
990 /*
991  * Mark an interface down and notify protocols of
992  * the transition.
993  * NOTE: must be called at splnet or eqivalent.
994  */
995 void
996 if_down(struct ifnet *ifp)
997 {
998 
999 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1000 }
1001 
1002 /*
1003  * Mark an interface up and notify protocols of
1004  * the transition.
1005  * NOTE: must be called at splnet or eqivalent.
1006  */
1007 void
1008 if_up(struct ifnet *ifp)
1009 {
1010 
1011 	if_route(ifp, IFF_UP, AF_UNSPEC);
1012 }
1013 
1014 /*
1015  * Flush an interface queue.
1016  */
1017 static void
1018 if_qflush(struct ifaltq *ifq)
1019 {
1020 	struct mbuf *m, *n;
1021 
1022 	IFQ_LOCK(ifq);
1023 #ifdef ALTQ
1024 	if (ALTQ_IS_ENABLED(ifq))
1025 		ALTQ_PURGE(ifq);
1026 #endif
1027 	n = ifq->ifq_head;
1028 	while ((m = n) != 0) {
1029 		n = m->m_act;
1030 		m_freem(m);
1031 	}
1032 	ifq->ifq_head = 0;
1033 	ifq->ifq_tail = 0;
1034 	ifq->ifq_len = 0;
1035 	IFQ_UNLOCK(ifq);
1036 }
1037 
1038 /*
1039  * Handle interface watchdog timer routines.  Called
1040  * from softclock, we decrement timers (if set) and
1041  * call the appropriate interface routine on expiration.
1042  *
1043  * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1044  * holding Giant.  If we switch to an MPSAFE callout, we likely need to grab
1045  * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1046  */
1047 static void
1048 if_slowtimo(void *arg)
1049 {
1050 	struct ifnet *ifp;
1051 	int s = splimp();
1052 
1053 	IFNET_RLOCK();
1054 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1055 		if (ifp->if_timer == 0 || --ifp->if_timer)
1056 			continue;
1057 		if (ifp->if_watchdog)
1058 			(*ifp->if_watchdog)(ifp);
1059 	}
1060 	IFNET_RUNLOCK();
1061 	splx(s);
1062 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1063 }
1064 
1065 /*
1066  * Map interface name to
1067  * interface structure pointer.
1068  */
1069 struct ifnet *
1070 ifunit(const char *name)
1071 {
1072 	struct ifnet *ifp;
1073 
1074 	IFNET_RLOCK();
1075 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1076 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1077 			break;
1078 	}
1079 	IFNET_RUNLOCK();
1080 	return (ifp);
1081 }
1082 
1083 /*
1084  * Hardware specific interface ioctls.
1085  */
1086 static int
1087 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1088 {
1089 	struct ifreq *ifr;
1090 	struct ifstat *ifs;
1091 	int error = 0;
1092 	int new_flags;
1093 	size_t namelen, onamelen;
1094 	char new_name[IFNAMSIZ];
1095 	struct ifaddr *ifa;
1096 	struct sockaddr_dl *sdl;
1097 
1098 	ifr = (struct ifreq *)data;
1099 	switch (cmd) {
1100 	case SIOCGIFINDEX:
1101 		ifr->ifr_index = ifp->if_index;
1102 		break;
1103 
1104 	case SIOCGIFFLAGS:
1105 		ifr->ifr_flags = ifp->if_flags & 0xffff;
1106 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1107 		break;
1108 
1109 	case SIOCGIFCAP:
1110 		ifr->ifr_reqcap = ifp->if_capabilities;
1111 		ifr->ifr_curcap = ifp->if_capenable;
1112 		break;
1113 
1114 #ifdef MAC
1115 	case SIOCGIFMAC:
1116 		error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1117 		break;
1118 #endif
1119 
1120 	case SIOCGIFMETRIC:
1121 		ifr->ifr_metric = ifp->if_metric;
1122 		break;
1123 
1124 	case SIOCGIFMTU:
1125 		ifr->ifr_mtu = ifp->if_mtu;
1126 		break;
1127 
1128 	case SIOCGIFPHYS:
1129 		ifr->ifr_phys = ifp->if_physical;
1130 		break;
1131 
1132 	case SIOCSIFFLAGS:
1133 		error = suser(td);
1134 		if (error)
1135 			return (error);
1136 		new_flags = (ifr->ifr_flags & 0xffff) |
1137 		    (ifr->ifr_flagshigh << 16);
1138 		if (ifp->if_flags & IFF_SMART) {
1139 			/* Smart drivers twiddle their own routes */
1140 		} else if (ifp->if_flags & IFF_UP &&
1141 		    (new_flags & IFF_UP) == 0) {
1142 			int s = splimp();
1143 			if_down(ifp);
1144 			splx(s);
1145 		} else if (new_flags & IFF_UP &&
1146 		    (ifp->if_flags & IFF_UP) == 0) {
1147 			int s = splimp();
1148 			if_up(ifp);
1149 			splx(s);
1150 		}
1151 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1152 			(new_flags &~ IFF_CANTCHANGE);
1153 		if (new_flags & IFF_PPROMISC) {
1154 			/* Permanently promiscuous mode requested */
1155 			ifp->if_flags |= IFF_PROMISC;
1156 		} else if (ifp->if_pcount == 0) {
1157 			ifp->if_flags &= ~IFF_PROMISC;
1158 		}
1159 		if (ifp->if_ioctl) {
1160 			IFF_LOCKGIANT(ifp);
1161 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1162 			IFF_UNLOCKGIANT(ifp);
1163 		}
1164 		getmicrotime(&ifp->if_lastchange);
1165 		break;
1166 
1167 	case SIOCSIFCAP:
1168 		error = suser(td);
1169 		if (error)
1170 			return (error);
1171 		if (ifp->if_ioctl == NULL)
1172 			return (EOPNOTSUPP);
1173 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1174 			return (EINVAL);
1175 		IFF_LOCKGIANT(ifp);
1176 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1177 		IFF_UNLOCKGIANT(ifp);
1178 		if (error == 0)
1179 			getmicrotime(&ifp->if_lastchange);
1180 		break;
1181 
1182 #ifdef MAC
1183 	case SIOCSIFMAC:
1184 		error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1185 		break;
1186 #endif
1187 
1188 	case SIOCSIFNAME:
1189 		error = suser(td);
1190 		if (error != 0)
1191 			return (error);
1192 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1193 		if (error != 0)
1194 			return (error);
1195 		if (new_name[0] == '\0')
1196 			return (EINVAL);
1197 		if (ifunit(new_name) != NULL)
1198 			return (EEXIST);
1199 
1200 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1201 		/* Announce the departure of the interface. */
1202 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1203 
1204 		log(LOG_INFO, "%s: changing name to '%s'\n",
1205 		    ifp->if_xname, new_name);
1206 
1207 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1208 		ifa = ifaddr_byindex(ifp->if_index);
1209 		IFA_LOCK(ifa);
1210 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1211 		namelen = strlen(new_name);
1212 		onamelen = sdl->sdl_nlen;
1213 		/*
1214 		 * Move the address if needed.  This is safe because we
1215 		 * allocate space for a name of length IFNAMSIZ when we
1216 		 * create this in if_attach().
1217 		 */
1218 		if (namelen != onamelen) {
1219 			bcopy(sdl->sdl_data + onamelen,
1220 			    sdl->sdl_data + namelen, sdl->sdl_alen);
1221 		}
1222 		bcopy(new_name, sdl->sdl_data, namelen);
1223 		sdl->sdl_nlen = namelen;
1224 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1225 		bzero(sdl->sdl_data, onamelen);
1226 		while (namelen != 0)
1227 			sdl->sdl_data[--namelen] = 0xff;
1228 		IFA_UNLOCK(ifa);
1229 
1230 		EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1231 		/* Announce the return of the interface. */
1232 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1233 		break;
1234 
1235 	case SIOCSIFMETRIC:
1236 		error = suser(td);
1237 		if (error)
1238 			return (error);
1239 		ifp->if_metric = ifr->ifr_metric;
1240 		getmicrotime(&ifp->if_lastchange);
1241 		break;
1242 
1243 	case SIOCSIFPHYS:
1244 		error = suser(td);
1245 		if (error)
1246 			return (error);
1247 		if (ifp->if_ioctl == NULL)
1248 			return (EOPNOTSUPP);
1249 		IFF_LOCKGIANT(ifp);
1250 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1251 		IFF_UNLOCKGIANT(ifp);
1252 		if (error == 0)
1253 			getmicrotime(&ifp->if_lastchange);
1254 		break;
1255 
1256 	case SIOCSIFMTU:
1257 	{
1258 		u_long oldmtu = ifp->if_mtu;
1259 
1260 		error = suser(td);
1261 		if (error)
1262 			return (error);
1263 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1264 			return (EINVAL);
1265 		if (ifp->if_ioctl == NULL)
1266 			return (EOPNOTSUPP);
1267 		IFF_LOCKGIANT(ifp);
1268 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1269 		IFF_UNLOCKGIANT(ifp);
1270 		if (error == 0) {
1271 			getmicrotime(&ifp->if_lastchange);
1272 			rt_ifmsg(ifp);
1273 		}
1274 		/*
1275 		 * If the link MTU changed, do network layer specific procedure.
1276 		 */
1277 		if (ifp->if_mtu != oldmtu) {
1278 #ifdef INET6
1279 			nd6_setmtu(ifp);
1280 #endif
1281 		}
1282 		break;
1283 	}
1284 
1285 	case SIOCADDMULTI:
1286 	case SIOCDELMULTI:
1287 		error = suser(td);
1288 		if (error)
1289 			return (error);
1290 
1291 		/* Don't allow group membership on non-multicast interfaces. */
1292 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1293 			return (EOPNOTSUPP);
1294 
1295 		/* Don't let users screw up protocols' entries. */
1296 		if (ifr->ifr_addr.sa_family != AF_LINK)
1297 			return (EINVAL);
1298 
1299 		if (cmd == SIOCADDMULTI) {
1300 			struct ifmultiaddr *ifma;
1301 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1302 		} else {
1303 			error = if_delmulti(ifp, &ifr->ifr_addr);
1304 		}
1305 		if (error == 0)
1306 			getmicrotime(&ifp->if_lastchange);
1307 		break;
1308 
1309 	case SIOCSIFPHYADDR:
1310 	case SIOCDIFPHYADDR:
1311 #ifdef INET6
1312 	case SIOCSIFPHYADDR_IN6:
1313 #endif
1314 	case SIOCSLIFPHYADDR:
1315 	case SIOCSIFMEDIA:
1316 	case SIOCSIFGENERIC:
1317 		error = suser(td);
1318 		if (error)
1319 			return (error);
1320 		if (ifp->if_ioctl == NULL)
1321 			return (EOPNOTSUPP);
1322 		IFF_LOCKGIANT(ifp);
1323 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1324 		IFF_UNLOCKGIANT(ifp);
1325 		if (error == 0)
1326 			getmicrotime(&ifp->if_lastchange);
1327 		break;
1328 
1329 	case SIOCGIFSTATUS:
1330 		ifs = (struct ifstat *)data;
1331 		ifs->ascii[0] = '\0';
1332 
1333 	case SIOCGIFPSRCADDR:
1334 	case SIOCGIFPDSTADDR:
1335 	case SIOCGLIFPHYADDR:
1336 	case SIOCGIFMEDIA:
1337 	case SIOCGIFGENERIC:
1338 		if (ifp->if_ioctl == NULL)
1339 			return (EOPNOTSUPP);
1340 		IFF_LOCKGIANT(ifp);
1341 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1342 		IFF_UNLOCKGIANT(ifp);
1343 		break;
1344 
1345 	case SIOCSIFLLADDR:
1346 		error = suser(td);
1347 		if (error)
1348 			return (error);
1349 		error = if_setlladdr(ifp,
1350 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1351 		break;
1352 
1353 	default:
1354 		error = ENOIOCTL;
1355 		break;
1356 	}
1357 	return (error);
1358 }
1359 
1360 /*
1361  * Interface ioctls.
1362  */
1363 int
1364 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1365 {
1366 	struct ifnet *ifp;
1367 	struct ifreq *ifr;
1368 	int error;
1369 	int oif_flags;
1370 
1371 	switch (cmd) {
1372 	case SIOCGIFCONF:
1373 	case OSIOCGIFCONF:
1374 		return (ifconf(cmd, data));
1375 	}
1376 	ifr = (struct ifreq *)data;
1377 
1378 	switch (cmd) {
1379 	case SIOCIFCREATE:
1380 	case SIOCIFDESTROY:
1381 		if ((error = suser(td)) != 0)
1382 			return (error);
1383 		return ((cmd == SIOCIFCREATE) ?
1384 			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1385 			if_clone_destroy(ifr->ifr_name));
1386 
1387 	case SIOCIFGCLONERS:
1388 		return (if_clone_list((struct if_clonereq *)data));
1389 	}
1390 
1391 	ifp = ifunit(ifr->ifr_name);
1392 	if (ifp == 0)
1393 		return (ENXIO);
1394 
1395 	error = ifhwioctl(cmd, ifp, data, td);
1396 	if (error != ENOIOCTL)
1397 		return (error);
1398 
1399 	oif_flags = ifp->if_flags;
1400 	if (so->so_proto == 0)
1401 		return (EOPNOTSUPP);
1402 #ifndef COMPAT_43
1403 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1404 								 data,
1405 								 ifp, td));
1406 #else
1407 	{
1408 		int ocmd = cmd;
1409 
1410 		switch (cmd) {
1411 
1412 		case SIOCSIFDSTADDR:
1413 		case SIOCSIFADDR:
1414 		case SIOCSIFBRDADDR:
1415 		case SIOCSIFNETMASK:
1416 #if BYTE_ORDER != BIG_ENDIAN
1417 			if (ifr->ifr_addr.sa_family == 0 &&
1418 			    ifr->ifr_addr.sa_len < 16) {
1419 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1420 				ifr->ifr_addr.sa_len = 16;
1421 			}
1422 #else
1423 			if (ifr->ifr_addr.sa_len == 0)
1424 				ifr->ifr_addr.sa_len = 16;
1425 #endif
1426 			break;
1427 
1428 		case OSIOCGIFADDR:
1429 			cmd = SIOCGIFADDR;
1430 			break;
1431 
1432 		case OSIOCGIFDSTADDR:
1433 			cmd = SIOCGIFDSTADDR;
1434 			break;
1435 
1436 		case OSIOCGIFBRDADDR:
1437 			cmd = SIOCGIFBRDADDR;
1438 			break;
1439 
1440 		case OSIOCGIFNETMASK:
1441 			cmd = SIOCGIFNETMASK;
1442 		}
1443 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
1444 								   cmd,
1445 								   data,
1446 								   ifp, td));
1447 		switch (ocmd) {
1448 
1449 		case OSIOCGIFADDR:
1450 		case OSIOCGIFDSTADDR:
1451 		case OSIOCGIFBRDADDR:
1452 		case OSIOCGIFNETMASK:
1453 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1454 
1455 		}
1456 	}
1457 #endif /* COMPAT_43 */
1458 
1459 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1460 #ifdef INET6
1461 		DELAY(100);/* XXX: temporary workaround for fxp issue*/
1462 		if (ifp->if_flags & IFF_UP) {
1463 			int s = splimp();
1464 			in6_if_up(ifp);
1465 			splx(s);
1466 		}
1467 #endif
1468 	}
1469 	return (error);
1470 }
1471 
1472 /*
1473  * Set/clear promiscuous mode on interface ifp based on the truth value
1474  * of pswitch.  The calls are reference counted so that only the first
1475  * "on" request actually has an effect, as does the final "off" request.
1476  * Results are undefined if the "off" and "on" requests are not matched.
1477  */
1478 int
1479 ifpromisc(struct ifnet *ifp, int pswitch)
1480 {
1481 	struct ifreq ifr;
1482 	int error;
1483 	int oldflags, oldpcount;
1484 
1485 	oldpcount = ifp->if_pcount;
1486 	oldflags = ifp->if_flags;
1487 	if (ifp->if_flags & IFF_PPROMISC) {
1488 		/* Do nothing if device is in permanently promiscuous mode */
1489 		ifp->if_pcount += pswitch ? 1 : -1;
1490 		return (0);
1491 	}
1492 	if (pswitch) {
1493 		/*
1494 		 * If the device is not configured up, we cannot put it in
1495 		 * promiscuous mode.
1496 		 */
1497 		if ((ifp->if_flags & IFF_UP) == 0)
1498 			return (ENETDOWN);
1499 		if (ifp->if_pcount++ != 0)
1500 			return (0);
1501 		ifp->if_flags |= IFF_PROMISC;
1502 	} else {
1503 		if (--ifp->if_pcount > 0)
1504 			return (0);
1505 		ifp->if_flags &= ~IFF_PROMISC;
1506 	}
1507 	ifr.ifr_flags = ifp->if_flags & 0xffff;
1508 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
1509 	IFF_LOCKGIANT(ifp);
1510 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1511 	IFF_UNLOCKGIANT(ifp);
1512 	if (error == 0) {
1513 		log(LOG_INFO, "%s: promiscuous mode %s\n",
1514 		    ifp->if_xname,
1515 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1516 		rt_ifmsg(ifp);
1517 	} else {
1518 		ifp->if_pcount = oldpcount;
1519 		ifp->if_flags = oldflags;
1520 	}
1521 	return error;
1522 }
1523 
1524 /*
1525  * Return interface configuration
1526  * of system.  List may be used
1527  * in later ioctl's (above) to get
1528  * other information.
1529  */
1530 /*ARGSUSED*/
1531 static int
1532 ifconf(u_long cmd, caddr_t data)
1533 {
1534 	struct ifconf *ifc = (struct ifconf *)data;
1535 	struct ifnet *ifp;
1536 	struct ifaddr *ifa;
1537 	struct ifreq ifr;
1538 	struct sbuf *sb;
1539 	int error, full = 0, valid_len, max_len;
1540 
1541 	/* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
1542 	max_len = MAXPHYS - 1;
1543 
1544 again:
1545 	if (ifc->ifc_len <= max_len) {
1546 		max_len = ifc->ifc_len;
1547 		full = 1;
1548 	}
1549 	sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
1550 	max_len = 0;
1551 	valid_len = 0;
1552 
1553 	IFNET_RLOCK();		/* could sleep XXX */
1554 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1555 		int addrs;
1556 
1557 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1558 		    >= sizeof(ifr.ifr_name))
1559 			return (ENAMETOOLONG);
1560 
1561 		addrs = 0;
1562 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1563 			struct sockaddr *sa = ifa->ifa_addr;
1564 
1565 			if (jailed(curthread->td_ucred) &&
1566 			    prison_if(curthread->td_ucred, sa))
1567 				continue;
1568 			addrs++;
1569 #ifdef COMPAT_43
1570 			if (cmd == OSIOCGIFCONF) {
1571 				struct osockaddr *osa =
1572 					 (struct osockaddr *)&ifr.ifr_addr;
1573 				ifr.ifr_addr = *sa;
1574 				osa->sa_family = sa->sa_family;
1575 				sbuf_bcat(sb, &ifr, sizeof(ifr));
1576 				max_len += sizeof(ifr);
1577 			} else
1578 #endif
1579 			if (sa->sa_len <= sizeof(*sa)) {
1580 				ifr.ifr_addr = *sa;
1581 				sbuf_bcat(sb, &ifr, sizeof(ifr));
1582 				max_len += sizeof(ifr);
1583 			} else {
1584 				sbuf_bcat(sb, &ifr,
1585 				    offsetof(struct ifreq, ifr_addr));
1586 				max_len += offsetof(struct ifreq, ifr_addr);
1587 				sbuf_bcat(sb, sa, sa->sa_len);
1588 				max_len += sa->sa_len;
1589 			}
1590 
1591 			if (!sbuf_overflowed(sb))
1592 				valid_len = sbuf_len(sb);
1593 		}
1594 		if (addrs == 0) {
1595 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1596 			sbuf_bcat(sb, &ifr, sizeof(ifr));
1597 			max_len += sizeof(ifr);
1598 
1599 			if (!sbuf_overflowed(sb))
1600 				valid_len = sbuf_len(sb);
1601 		}
1602 	}
1603 	IFNET_RUNLOCK();
1604 
1605 	/*
1606 	 * If we didn't allocate enough space (uncommon), try again.  If
1607 	 * we have already allocated as much space as we are allowed,
1608 	 * return what we've got.
1609 	 */
1610 	if (valid_len != max_len && !full) {
1611 		sbuf_delete(sb);
1612 		goto again;
1613 	}
1614 
1615 	ifc->ifc_len = valid_len;
1616 	sbuf_finish(sb);
1617 	error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
1618 	sbuf_delete(sb);
1619 	return (error);
1620 }
1621 
1622 /*
1623  * Just like if_promisc(), but for all-multicast-reception mode.
1624  */
1625 int
1626 if_allmulti(struct ifnet *ifp, int onswitch)
1627 {
1628 	int error = 0;
1629 	int s = splimp();
1630 	struct ifreq ifr;
1631 
1632 	if (onswitch) {
1633 		if (ifp->if_amcount++ == 0) {
1634 			ifp->if_flags |= IFF_ALLMULTI;
1635 			ifr.ifr_flags = ifp->if_flags & 0xffff;
1636 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1637 			IFF_LOCKGIANT(ifp);
1638 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1639 			IFF_UNLOCKGIANT(ifp);
1640 		}
1641 	} else {
1642 		if (ifp->if_amcount > 1) {
1643 			ifp->if_amcount--;
1644 		} else {
1645 			ifp->if_amcount = 0;
1646 			ifp->if_flags &= ~IFF_ALLMULTI;
1647 			ifr.ifr_flags = ifp->if_flags & 0xffff;;
1648 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1649 			IFF_LOCKGIANT(ifp);
1650 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1651 			IFF_UNLOCKGIANT(ifp);
1652 		}
1653 	}
1654 	splx(s);
1655 
1656 	if (error == 0)
1657 		rt_ifmsg(ifp);
1658 	return error;
1659 }
1660 
1661 /*
1662  * Add a multicast listenership to the interface in question.
1663  * The link layer provides a routine which converts
1664  */
1665 int
1666 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, struct ifmultiaddr **retifma)
1667 {
1668 	struct sockaddr *llsa, *dupsa;
1669 	int error, s;
1670 	struct ifmultiaddr *ifma;
1671 
1672 	/*
1673 	 * If the matching multicast address already exists
1674 	 * then don't add a new one, just add a reference
1675 	 */
1676 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1677 		if (equal(sa, ifma->ifma_addr)) {
1678 			ifma->ifma_refcount++;
1679 			if (retifma)
1680 				*retifma = ifma;
1681 			return 0;
1682 		}
1683 	}
1684 
1685 	/*
1686 	 * Give the link layer a chance to accept/reject it, and also
1687 	 * find out which AF_LINK address this maps to, if it isn't one
1688 	 * already.
1689 	 */
1690 	if (ifp->if_resolvemulti) {
1691 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
1692 		if (error) return error;
1693 	} else {
1694 		llsa = 0;
1695 	}
1696 
1697 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1698 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1699 	bcopy(sa, dupsa, sa->sa_len);
1700 
1701 	ifma->ifma_addr = dupsa;
1702 	ifma->ifma_lladdr = llsa;
1703 	ifma->ifma_ifp = ifp;
1704 	ifma->ifma_refcount = 1;
1705 	ifma->ifma_protospec = 0;
1706 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1707 
1708 	/*
1709 	 * Some network interfaces can scan the address list at
1710 	 * interrupt time; lock them out.
1711 	 */
1712 	s = splimp();
1713 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1714 	splx(s);
1715 	if (retifma != NULL)
1716 		*retifma = ifma;
1717 
1718 	if (llsa != 0) {
1719 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1720 			if (equal(ifma->ifma_addr, llsa))
1721 				break;
1722 		}
1723 		if (ifma) {
1724 			ifma->ifma_refcount++;
1725 		} else {
1726 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1727 			       M_IFMADDR, M_WAITOK);
1728 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1729 			       M_IFMADDR, M_WAITOK);
1730 			bcopy(llsa, dupsa, llsa->sa_len);
1731 			ifma->ifma_addr = dupsa;
1732 			ifma->ifma_lladdr = NULL;
1733 			ifma->ifma_ifp = ifp;
1734 			ifma->ifma_refcount = 1;
1735 			ifma->ifma_protospec = 0;
1736 			s = splimp();
1737 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1738 			splx(s);
1739 		}
1740 	}
1741 	/*
1742 	 * We are certain we have added something, so call down to the
1743 	 * interface to let them know about it.
1744 	 */
1745 	s = splimp();
1746 	IFF_LOCKGIANT(ifp);
1747 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1748 	IFF_UNLOCKGIANT(ifp);
1749 	splx(s);
1750 
1751 	return 0;
1752 }
1753 
1754 /*
1755  * Remove a reference to a multicast address on this interface.  Yell
1756  * if the request does not match an existing membership.
1757  */
1758 int
1759 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1760 {
1761 	struct ifmultiaddr *ifma;
1762 	int s;
1763 
1764 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1765 		if (equal(sa, ifma->ifma_addr))
1766 			break;
1767 	if (ifma == 0)
1768 		return ENOENT;
1769 
1770 	if (ifma->ifma_refcount > 1) {
1771 		ifma->ifma_refcount--;
1772 		return 0;
1773 	}
1774 
1775 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1776 	sa = ifma->ifma_lladdr;
1777 	s = splimp();
1778 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1779 	/*
1780 	 * Make sure the interface driver is notified
1781 	 * in the case of a link layer mcast group being left.
1782 	 */
1783 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1784 		IFF_LOCKGIANT(ifp);
1785 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1786 		IFF_UNLOCKGIANT(ifp);
1787 	}
1788 	splx(s);
1789 	free(ifma->ifma_addr, M_IFMADDR);
1790 	free(ifma, M_IFMADDR);
1791 	if (sa == 0)
1792 		return 0;
1793 
1794 	/*
1795 	 * Now look for the link-layer address which corresponds to
1796 	 * this network address.  It had been squirreled away in
1797 	 * ifma->ifma_lladdr for this purpose (so we don't have
1798 	 * to call ifp->if_resolvemulti() again), and we saved that
1799 	 * value in sa above.  If some nasty deleted the
1800 	 * link-layer address out from underneath us, we can deal because
1801 	 * the address we stored was is not the same as the one which was
1802 	 * in the record for the link-layer address.  (So we don't complain
1803 	 * in that case.)
1804 	 */
1805 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1806 		if (equal(sa, ifma->ifma_addr))
1807 			break;
1808 	if (ifma == 0)
1809 		return 0;
1810 
1811 	if (ifma->ifma_refcount > 1) {
1812 		ifma->ifma_refcount--;
1813 		return 0;
1814 	}
1815 
1816 	s = splimp();
1817 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1818 	IFF_LOCKGIANT(ifp);
1819 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1820 	IFF_UNLOCKGIANT(ifp);
1821 	splx(s);
1822 	free(ifma->ifma_addr, M_IFMADDR);
1823 	free(sa, M_IFMADDR);
1824 	free(ifma, M_IFMADDR);
1825 
1826 	return 0;
1827 }
1828 
1829 /*
1830  * Set the link layer address on an interface.
1831  *
1832  * At this time we only support certain types of interfaces,
1833  * and we don't allow the length of the address to change.
1834  */
1835 int
1836 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1837 {
1838 	struct sockaddr_dl *sdl;
1839 	struct ifaddr *ifa;
1840 	struct ifreq ifr;
1841 
1842 	ifa = ifaddr_byindex(ifp->if_index);
1843 	if (ifa == NULL)
1844 		return (EINVAL);
1845 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1846 	if (sdl == NULL)
1847 		return (EINVAL);
1848 	if (len != sdl->sdl_alen)	/* don't allow length to change */
1849 		return (EINVAL);
1850 	switch (ifp->if_type) {
1851 	case IFT_ETHER:			/* these types use struct arpcom */
1852 	case IFT_FDDI:
1853 	case IFT_XETHER:
1854 	case IFT_ISO88025:
1855 	case IFT_L2VLAN:
1856 		bcopy(lladdr, IFP2AC(ifp)->ac_enaddr, len);
1857 		/*
1858 		 * XXX We also need to store the lladdr in LLADDR(sdl),
1859 		 * which is done below. This is a pain because we must
1860 		 * remember to keep the info in sync.
1861 		 */
1862 		/* FALLTHROUGH */
1863 	case IFT_ARCNET:
1864 		bcopy(lladdr, LLADDR(sdl), len);
1865 		break;
1866 	default:
1867 		return (ENODEV);
1868 	}
1869 	/*
1870 	 * If the interface is already up, we need
1871 	 * to re-init it in order to reprogram its
1872 	 * address filter.
1873 	 */
1874 	if ((ifp->if_flags & IFF_UP) != 0) {
1875 		IFF_LOCKGIANT(ifp);
1876 		ifp->if_flags &= ~IFF_UP;
1877 		ifr.ifr_flags = ifp->if_flags & 0xffff;
1878 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1879 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1880 		ifp->if_flags |= IFF_UP;
1881 		ifr.ifr_flags = ifp->if_flags & 0xffff;
1882 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1883 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1884 		IFF_UNLOCKGIANT(ifp);
1885 #ifdef INET
1886 		/*
1887 		 * Also send gratuitous ARPs to notify other nodes about
1888 		 * the address change.
1889 		 */
1890 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1891 			if (ifa->ifa_addr != NULL &&
1892 			    ifa->ifa_addr->sa_family == AF_INET)
1893 				arp_ifinit(ifp, ifa);
1894 		}
1895 #endif
1896 	}
1897 	return (0);
1898 }
1899 
1900 struct ifmultiaddr *
1901 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1902 {
1903 	struct ifmultiaddr *ifma;
1904 
1905 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1906 		if (equal(ifma->ifma_addr, sa))
1907 			break;
1908 
1909 	return ifma;
1910 }
1911 
1912 /*
1913  * The name argument must be a pointer to storage which will last as
1914  * long as the interface does.  For physical devices, the result of
1915  * device_get_name(dev) is a good choice and for pseudo-devices a
1916  * static string works well.
1917  */
1918 void
1919 if_initname(struct ifnet *ifp, const char *name, int unit)
1920 {
1921 	ifp->if_dname = name;
1922 	ifp->if_dunit = unit;
1923 	if (unit != IF_DUNIT_NONE)
1924 		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1925 	else
1926 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
1927 }
1928 
1929 int
1930 if_printf(struct ifnet *ifp, const char * fmt, ...)
1931 {
1932 	va_list ap;
1933 	int retval;
1934 
1935 	retval = printf("%s: ", ifp->if_xname);
1936 	va_start(ap, fmt);
1937 	retval += vprintf(fmt, ap);
1938 	va_end(ap);
1939 	return (retval);
1940 }
1941 
1942 /*
1943  * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
1944  * be called without Giant.  However, we often can't acquire the Giant lock
1945  * at those points; instead, we run it via a task queue that holds Giant via
1946  * if_start_deferred.
1947  *
1948  * XXXRW: We need to make sure that the ifnet isn't fully detached until any
1949  * outstanding if_start_deferred() tasks that will run after the free.  This
1950  * probably means waiting in if_detach().
1951  */
1952 void
1953 if_start(struct ifnet *ifp)
1954 {
1955 
1956 	NET_ASSERT_GIANT();
1957 
1958 	if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
1959 		if (mtx_owned(&Giant))
1960 			(*(ifp)->if_start)(ifp);
1961 		else
1962 			taskqueue_enqueue(taskqueue_swi_giant,
1963 			    &ifp->if_starttask);
1964 	} else
1965 		(*(ifp)->if_start)(ifp);
1966 }
1967 
1968 static void
1969 if_start_deferred(void *context, int pending)
1970 {
1971 	struct ifnet *ifp;
1972 
1973 	/*
1974 	 * This code must be entered with Giant, and should never run if
1975 	 * we're not running with debug.mpsafenet.
1976 	 */
1977 	KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
1978 	GIANT_REQUIRED;
1979 
1980 	ifp = (struct ifnet *)context;
1981 	(ifp->if_start)(ifp);
1982 }
1983 
1984 int
1985 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
1986 {
1987 	int active = 0;
1988 
1989 	IF_LOCK(ifq);
1990 	if (_IF_QFULL(ifq)) {
1991 		_IF_DROP(ifq);
1992 		IF_UNLOCK(ifq);
1993 		m_freem(m);
1994 		return (0);
1995 	}
1996 	if (ifp != NULL) {
1997 		ifp->if_obytes += m->m_pkthdr.len + adjust;
1998 		if (m->m_flags & (M_BCAST|M_MCAST))
1999 			ifp->if_omcasts++;
2000 		active = ifp->if_flags & IFF_OACTIVE;
2001 	}
2002 	_IF_ENQUEUE(ifq, m);
2003 	IF_UNLOCK(ifq);
2004 	if (ifp != NULL && !active)
2005 		if_start(ifp);
2006 	return (1);
2007 }
2008 
2009 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
2010 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
2011