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