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