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