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