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