xref: /freebsd/sys/net/if.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
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 	/* Announce the interface. */
435 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
436 }
437 
438 /*
439  * Detach an interface, removing it from the
440  * list of "active" interfaces.
441  */
442 void
443 if_detach(ifp)
444 	struct ifnet *ifp;
445 {
446 	struct ifaddr *ifa;
447 	struct radix_node_head	*rnh;
448 	int s;
449 	int i;
450 
451 	/*
452 	 * Remove routes and flush queues.
453 	 */
454 	s = splnet();
455 	if_down(ifp);
456 
457 	/*
458 	 * Remove address from ifindex_table[] and maybe decrement if_index.
459 	 * Clean up all addresses.
460 	 */
461 	ifaddr_byindex(ifp->if_index) = NULL;
462 	destroy_dev(ifdev_byindex(ifp->if_index));
463 	ifdev_byindex(ifp->if_index) = NULL;
464 
465 	while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
466 		if_index--;
467 
468 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
469 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
470 #ifdef INET
471 		/* XXX: Ugly!! ad hoc just for INET */
472 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
473 			struct ifaliasreq ifr;
474 
475 			bzero(&ifr, sizeof(ifr));
476 			ifr.ifra_addr = *ifa->ifa_addr;
477 			if (ifa->ifa_dstaddr)
478 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
479 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
480 			    NULL) == 0)
481 				continue;
482 		}
483 #endif /* INET */
484 #ifdef INET6
485 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
486 			in6_purgeaddr(ifa);
487 			/* ifp_addrhead is already updated */
488 			continue;
489 		}
490 #endif /* INET6 */
491 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
492 		IFAFREE(ifa);
493 	}
494 
495 #ifdef INET6
496 	/*
497 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
498 	 * before removing routing entries below, since IPv6 interface direct
499 	 * routes are expected to be removed by the IPv6-specific kernel API.
500 	 * Otherwise, the kernel will detect some inconsistency and bark it.
501 	 */
502 	in6_ifdetach(ifp);
503 #endif
504 
505 	/*
506 	 * Delete all remaining routes using this interface
507 	 * Unfortuneatly the only way to do this is to slog through
508 	 * the entire routing table looking for routes which point
509 	 * to this interface...oh well...
510 	 */
511 	for (i = 1; i <= AF_MAX; i++) {
512 		if ((rnh = rt_tables[i]) == NULL)
513 			continue;
514 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
515 	}
516 
517 	/* Announce that the interface is gone. */
518 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
519 
520 	KNOTE(&ifp->if_klist, NOTE_EXIT);
521 	TAILQ_REMOVE(&ifnet, ifp, if_link);
522 	mtx_destroy(&ifp->if_snd.ifq_mtx);
523 	splx(s);
524 }
525 
526 /*
527  * Delete Routes for a Network Interface
528  *
529  * Called for each routing entry via the rnh->rnh_walktree() call above
530  * to delete all route entries referencing a detaching network interface.
531  *
532  * Arguments:
533  *	rn	pointer to node in the routing table
534  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
535  *
536  * Returns:
537  *	0	successful
538  *	errno	failed - reason indicated
539  *
540  */
541 static int
542 if_rtdel(rn, arg)
543 	struct radix_node	*rn;
544 	void			*arg;
545 {
546 	struct rtentry	*rt = (struct rtentry *)rn;
547 	struct ifnet	*ifp = arg;
548 	int		err;
549 
550 	if (rt->rt_ifp == ifp) {
551 
552 		/*
553 		 * Protect (sorta) against walktree recursion problems
554 		 * with cloned routes
555 		 */
556 		if ((rt->rt_flags & RTF_UP) == 0)
557 			return (0);
558 
559 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
560 				rt_mask(rt), rt->rt_flags,
561 				(struct rtentry **) NULL);
562 		if (err) {
563 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
564 		}
565 	}
566 
567 	return (0);
568 }
569 
570 /*
571  * Create a clone network interface.
572  */
573 int
574 if_clone_create(name, len)
575 	char *name;
576 	int len;
577 {
578 	struct if_clone *ifc;
579 	char *dp;
580 	int wildcard;
581 	int unit;
582 	int err;
583 
584 	ifc = if_clone_lookup(name, &unit);
585 	if (ifc == NULL)
586 		return (EINVAL);
587 
588 	if (ifunit(name) != NULL)
589 		return (EEXIST);
590 
591 	wildcard = (unit < 0);
592 
593 	err = (*ifc->ifc_create)(ifc, &unit);
594 	if (err != 0)
595 		return (err);
596 
597 	/* In the wildcard case, we need to update the name. */
598 	if (wildcard) {
599 		for (dp = name; *dp != '\0'; dp++);
600 		if (snprintf(dp, len - (dp-name), "%d", unit) >
601 		    len - (dp-name) - 1) {
602 			/*
603 			 * This can only be a programmer error and
604 			 * there's no straightforward way to recover if
605 			 * it happens.
606 			 */
607 			panic("if_clone_create(): interface name too long");
608 		}
609 
610 	}
611 
612 	return (0);
613 }
614 
615 /*
616  * Destroy a clone network interface.
617  */
618 int
619 if_clone_destroy(name)
620 	const char *name;
621 {
622 	struct if_clone *ifc;
623 	struct ifnet *ifp;
624 
625 	ifc = if_clone_lookup(name, NULL);
626 	if (ifc == NULL)
627 		return (EINVAL);
628 
629 	ifp = ifunit(name);
630 	if (ifp == NULL)
631 		return (ENXIO);
632 
633 	if (ifc->ifc_destroy == NULL)
634 		return (EOPNOTSUPP);
635 
636 	(*ifc->ifc_destroy)(ifp);
637 	return (0);
638 }
639 
640 /*
641  * Look up a network interface cloner.
642  */
643 static struct if_clone *
644 if_clone_lookup(name, unitp)
645 	const char *name;
646 	int *unitp;
647 {
648 	struct if_clone *ifc;
649 	const char *cp;
650 	int i;
651 
652 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
653 		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
654 			if (ifc->ifc_name[i] != *cp)
655 				goto next_ifc;
656 		}
657 		goto found_name;
658  next_ifc:
659 		ifc = LIST_NEXT(ifc, ifc_list);
660 	}
661 
662 	/* No match. */
663 	return ((struct if_clone *)NULL);
664 
665  found_name:
666 	if (*cp == '\0') {
667 		i = -1;
668 	} else {
669 		for (i = 0; *cp != '\0'; cp++) {
670 			if (*cp < '0' || *cp > '9') {
671 				/* Bogus unit number. */
672 				return (NULL);
673 			}
674 			i = (i * 10) + (*cp - '0');
675 		}
676 	}
677 
678 	if (unitp != NULL)
679 		*unitp = i;
680 	return (ifc);
681 }
682 
683 /*
684  * Register a network interface cloner.
685  */
686 void
687 if_clone_attach(ifc)
688 	struct if_clone *ifc;
689 {
690 
691 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
692 	if_cloners_count++;
693 }
694 
695 /*
696  * Unregister a network interface cloner.
697  */
698 void
699 if_clone_detach(ifc)
700 	struct if_clone *ifc;
701 {
702 
703 	LIST_REMOVE(ifc, ifc_list);
704 	if_cloners_count--;
705 }
706 
707 /*
708  * Provide list of interface cloners to userspace.
709  */
710 static int
711 if_clone_list(ifcr)
712 	struct if_clonereq *ifcr;
713 {
714 	char outbuf[IFNAMSIZ], *dst;
715 	struct if_clone *ifc;
716 	int count, error = 0;
717 
718 	ifcr->ifcr_total = if_cloners_count;
719 	if ((dst = ifcr->ifcr_buffer) == NULL) {
720 		/* Just asking how many there are. */
721 		return (0);
722 	}
723 
724 	if (ifcr->ifcr_count < 0)
725 		return (EINVAL);
726 
727 	count = (if_cloners_count < ifcr->ifcr_count) ?
728 	    if_cloners_count : ifcr->ifcr_count;
729 
730 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
731 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
732 		strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
733 		outbuf[IFNAMSIZ - 1] = '\0';	/* sanity */
734 		error = copyout(outbuf, dst, IFNAMSIZ);
735 		if (error)
736 			break;
737 	}
738 
739 	return (error);
740 }
741 
742 /*
743  * Locate an interface based on a complete address.
744  */
745 /*ARGSUSED*/
746 struct ifaddr *
747 ifa_ifwithaddr(addr)
748 	struct sockaddr *addr;
749 {
750 	struct ifnet *ifp;
751 	struct ifaddr *ifa;
752 
753 #define	equal(a1, a2) \
754   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
755 	TAILQ_FOREACH(ifp, &ifnet, if_link)
756 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
757 			if (ifa->ifa_addr->sa_family != addr->sa_family)
758 				continue;
759 			if (equal(addr, ifa->ifa_addr))
760 				goto done;
761 			/* IP6 doesn't have broadcast */
762 			if ((ifp->if_flags & IFF_BROADCAST) &&
763 			    ifa->ifa_broadaddr &&
764 			    ifa->ifa_broadaddr->sa_len != 0 &&
765 			    equal(ifa->ifa_broadaddr, addr))
766 				goto done;
767 		}
768 	ifa = NULL;
769 done:
770 	return (ifa);
771 }
772 
773 /*
774  * Locate the point to point interface with a given destination address.
775  */
776 /*ARGSUSED*/
777 struct ifaddr *
778 ifa_ifwithdstaddr(addr)
779 	struct sockaddr *addr;
780 {
781 	struct ifnet *ifp;
782 	struct ifaddr *ifa;
783 
784 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
785 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
786 			continue;
787 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
788 			if (ifa->ifa_addr->sa_family != addr->sa_family)
789 				continue;
790 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
791 				goto done;
792 		}
793 	}
794 	ifa = NULL;
795 done:
796 	return (ifa);
797 }
798 
799 /*
800  * Find an interface on a specific network.  If many, choice
801  * is most specific found.
802  */
803 struct ifaddr *
804 ifa_ifwithnet(addr)
805 	struct sockaddr *addr;
806 {
807 	register struct ifnet *ifp;
808 	register struct ifaddr *ifa;
809 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
810 	u_int af = addr->sa_family;
811 	char *addr_data = addr->sa_data, *cplim;
812 
813 	/*
814 	 * AF_LINK addresses can be looked up directly by their index number,
815 	 * so do that if we can.
816 	 */
817 	if (af == AF_LINK) {
818 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
819 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
820 		return (ifaddr_byindex(sdl->sdl_index));
821 	}
822 
823 	/*
824 	 * Scan though each interface, looking for ones that have
825 	 * addresses in this address family.
826 	 */
827 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
828 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
829 			register char *cp, *cp2, *cp3;
830 
831 			if (ifa->ifa_addr->sa_family != af)
832 next:				continue;
833 			if (
834 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
835 			    addr->sa_family != AF_INET6 &&
836 #endif
837 			    ifp->if_flags & IFF_POINTOPOINT) {
838 				/*
839 				 * This is a bit broken as it doesn't
840 				 * take into account that the remote end may
841 				 * be a single node in the network we are
842 				 * looking for.
843 				 * The trouble is that we don't know the
844 				 * netmask for the remote end.
845 				 */
846 				if (ifa->ifa_dstaddr != 0
847 				    && equal(addr, ifa->ifa_dstaddr))
848 					goto done;
849 			} else {
850 				/*
851 				 * if we have a special address handler,
852 				 * then use it instead of the generic one.
853 				 */
854 	          		if (ifa->ifa_claim_addr) {
855 					if ((*ifa->ifa_claim_addr)(ifa, addr))
856 						goto done;
857 					continue;
858 				}
859 
860 				/*
861 				 * Scan all the bits in the ifa's address.
862 				 * If a bit dissagrees with what we are
863 				 * looking for, mask it with the netmask
864 				 * to see if it really matters.
865 				 * (A byte at a time)
866 				 */
867 				if (ifa->ifa_netmask == 0)
868 					continue;
869 				cp = addr_data;
870 				cp2 = ifa->ifa_addr->sa_data;
871 				cp3 = ifa->ifa_netmask->sa_data;
872 				cplim = ifa->ifa_netmask->sa_len
873 					+ (char *)ifa->ifa_netmask;
874 				while (cp3 < cplim)
875 					if ((*cp++ ^ *cp2++) & *cp3++)
876 						goto next; /* next address! */
877 				/*
878 				 * If the netmask of what we just found
879 				 * is more specific than what we had before
880 				 * (if we had one) then remember the new one
881 				 * before continuing to search
882 				 * for an even better one.
883 				 */
884 				if (ifa_maybe == 0 ||
885 				    rn_refines((caddr_t)ifa->ifa_netmask,
886 				    (caddr_t)ifa_maybe->ifa_netmask))
887 					ifa_maybe = ifa;
888 			}
889 		}
890 	}
891 	ifa = ifa_maybe;
892 done:
893 	return (ifa);
894 }
895 
896 /*
897  * Find an interface address specific to an interface best matching
898  * a given address.
899  */
900 struct ifaddr *
901 ifaof_ifpforaddr(addr, ifp)
902 	struct sockaddr *addr;
903 	register struct ifnet *ifp;
904 {
905 	register struct ifaddr *ifa;
906 	register char *cp, *cp2, *cp3;
907 	register char *cplim;
908 	struct ifaddr *ifa_maybe = 0;
909 	u_int af = addr->sa_family;
910 
911 	if (af >= AF_MAX)
912 		return (0);
913 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
914 		if (ifa->ifa_addr->sa_family != af)
915 			continue;
916 		if (ifa_maybe == 0)
917 			ifa_maybe = ifa;
918 		if (ifa->ifa_netmask == 0) {
919 			if (equal(addr, ifa->ifa_addr) ||
920 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
921 				goto done;
922 			continue;
923 		}
924 		if (ifp->if_flags & IFF_POINTOPOINT) {
925 			if (equal(addr, ifa->ifa_dstaddr))
926 				goto done;
927 		} else {
928 			cp = addr->sa_data;
929 			cp2 = ifa->ifa_addr->sa_data;
930 			cp3 = ifa->ifa_netmask->sa_data;
931 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
932 			for (; cp3 < cplim; cp3++)
933 				if ((*cp++ ^ *cp2++) & *cp3)
934 					break;
935 			if (cp3 == cplim)
936 				goto done;
937 		}
938 	}
939 	ifa = ifa_maybe;
940 done:
941 	return (ifa);
942 }
943 
944 #include <net/route.h>
945 
946 /*
947  * Default action when installing a route with a Link Level gateway.
948  * Lookup an appropriate real ifa to point to.
949  * This should be moved to /sys/net/link.c eventually.
950  */
951 static void
952 link_rtrequest(cmd, rt, info)
953 	int cmd;
954 	register struct rtentry *rt;
955 	struct rt_addrinfo *info;
956 {
957 	register struct ifaddr *ifa;
958 	struct sockaddr *dst;
959 	struct ifnet *ifp;
960 
961 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
962 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
963 		return;
964 	ifa = ifaof_ifpforaddr(dst, ifp);
965 	if (ifa) {
966 		IFAFREE(rt->rt_ifa);
967 		rt->rt_ifa = ifa;
968 		ifa->ifa_refcnt++;
969 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
970 			ifa->ifa_rtrequest(cmd, rt, info);
971 	}
972 }
973 
974 /*
975  * Mark an interface down and notify protocols of
976  * the transition.
977  * NOTE: must be called at splnet or eqivalent.
978  */
979 void
980 if_unroute(ifp, flag, fam)
981 	register struct ifnet *ifp;
982 	int flag, fam;
983 {
984 	register struct ifaddr *ifa;
985 
986 	ifp->if_flags &= ~flag;
987 	getmicrotime(&ifp->if_lastchange);
988 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
989 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
990 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
991 	if_qflush(&ifp->if_snd);
992 	rt_ifmsg(ifp);
993 }
994 
995 /*
996  * Mark an interface up and notify protocols of
997  * the transition.
998  * NOTE: must be called at splnet or eqivalent.
999  */
1000 void
1001 if_route(ifp, flag, fam)
1002 	register struct ifnet *ifp;
1003 	int flag, fam;
1004 {
1005 	register struct ifaddr *ifa;
1006 
1007 	ifp->if_flags |= flag;
1008 	getmicrotime(&ifp->if_lastchange);
1009 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1010 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1011 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
1012 	rt_ifmsg(ifp);
1013 #ifdef INET6
1014 	in6_if_up(ifp);
1015 #endif
1016 }
1017 
1018 /*
1019  * Mark an interface down and notify protocols of
1020  * the transition.
1021  * NOTE: must be called at splnet or eqivalent.
1022  */
1023 void
1024 if_down(ifp)
1025 	register struct ifnet *ifp;
1026 {
1027 
1028 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1029 }
1030 
1031 /*
1032  * Mark an interface up and notify protocols of
1033  * the transition.
1034  * NOTE: must be called at splnet or eqivalent.
1035  */
1036 void
1037 if_up(ifp)
1038 	register struct ifnet *ifp;
1039 {
1040 
1041 	if_route(ifp, IFF_UP, AF_UNSPEC);
1042 }
1043 
1044 /*
1045  * Flush an interface queue.
1046  */
1047 static void
1048 if_qflush(ifq)
1049 	register struct ifqueue *ifq;
1050 {
1051 	register struct mbuf *m, *n;
1052 
1053 	n = ifq->ifq_head;
1054 	while ((m = n) != 0) {
1055 		n = m->m_act;
1056 		m_freem(m);
1057 	}
1058 	ifq->ifq_head = 0;
1059 	ifq->ifq_tail = 0;
1060 	ifq->ifq_len = 0;
1061 }
1062 
1063 /*
1064  * Handle interface watchdog timer routines.  Called
1065  * from softclock, we decrement timers (if set) and
1066  * call the appropriate interface routine on expiration.
1067  */
1068 static void
1069 if_slowtimo(arg)
1070 	void *arg;
1071 {
1072 	register struct ifnet *ifp;
1073 	int s = splimp();
1074 
1075 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1076 		if (ifp->if_timer == 0 || --ifp->if_timer)
1077 			continue;
1078 		if (ifp->if_watchdog)
1079 			(*ifp->if_watchdog)(ifp);
1080 	}
1081 	splx(s);
1082 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1083 }
1084 
1085 /*
1086  * Map interface name to
1087  * interface structure pointer.
1088  */
1089 struct ifnet *
1090 ifunit(const char *name)
1091 {
1092 	char namebuf[IFNAMSIZ + 1];
1093 	struct ifnet *ifp;
1094 	dev_t dev;
1095 
1096 	/*
1097 	 * Now search all the interfaces for this name/number
1098 	 */
1099 
1100 	/*
1101 	 * XXX
1102 	 * Devices should really be known as /dev/fooN, not /dev/net/fooN.
1103 	 */
1104 	snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name);
1105 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1106 		dev = ifdev_byindex(ifp->if_index);
1107 		if (strcmp(devtoname(dev), namebuf) == 0)
1108 			break;
1109 		if (dev_named(dev, name))
1110 			break;
1111 	}
1112 	return (ifp);
1113 }
1114 
1115 /*
1116  * Map interface name in a sockaddr_dl to
1117  * interface structure pointer.
1118  */
1119 struct ifnet *
1120 if_withname(sa)
1121 	struct sockaddr *sa;
1122 {
1123 	char ifname[IFNAMSIZ+1];
1124 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1125 
1126 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1127 	     (sdl->sdl_nlen > IFNAMSIZ) )
1128 		return NULL;
1129 
1130 	/*
1131 	 * ifunit wants a null-terminated name.  It may not be null-terminated
1132 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
1133 	 * and there might not be room to put the trailing null anyway, so we
1134 	 * make a local copy that we know we can null terminate safely.
1135 	 */
1136 
1137 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1138 	ifname[sdl->sdl_nlen] = '\0';
1139 	return ifunit(ifname);
1140 }
1141 
1142 /*
1143  * Hardware specific interface ioctls.
1144  */
1145 static int
1146 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1147 {
1148 	struct ifreq *ifr;
1149 	struct ifstat *ifs;
1150 	int error = 0;
1151 
1152 	ifr = (struct ifreq *)data;
1153 	switch (cmd) {
1154 	case SIOCGIFINDEX:
1155 		ifr->ifr_index = ifp->if_index;
1156 		break;
1157 
1158 	case SIOCGIFFLAGS:
1159 		ifr->ifr_flags = ifp->if_flags;
1160 		break;
1161 
1162 	case SIOCGIFCAP:
1163 		ifr->ifr_reqcap = ifp->if_capabilities;
1164 		ifr->ifr_curcap = ifp->if_capenable;
1165 		break;
1166 
1167 	case SIOCGIFMETRIC:
1168 		ifr->ifr_metric = ifp->if_metric;
1169 		break;
1170 
1171 	case SIOCGIFMTU:
1172 		ifr->ifr_mtu = ifp->if_mtu;
1173 		break;
1174 
1175 	case SIOCGIFPHYS:
1176 		ifr->ifr_phys = ifp->if_physical;
1177 		break;
1178 
1179 	case SIOCSIFFLAGS:
1180 		error = suser_td(td);
1181 		if (error)
1182 			return (error);
1183 		ifr->ifr_prevflags = ifp->if_flags;
1184 		if (ifp->if_flags & IFF_SMART) {
1185 			/* Smart drivers twiddle their own routes */
1186 		} else if (ifp->if_flags & IFF_UP &&
1187 		    (ifr->ifr_flags & IFF_UP) == 0) {
1188 			int s = splimp();
1189 			if_down(ifp);
1190 			splx(s);
1191 		} else if (ifr->ifr_flags & IFF_UP &&
1192 		    (ifp->if_flags & IFF_UP) == 0) {
1193 			int s = splimp();
1194 			if_up(ifp);
1195 			splx(s);
1196 		}
1197 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1198 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1199 		if (ifp->if_ioctl)
1200 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1201 		getmicrotime(&ifp->if_lastchange);
1202 		break;
1203 
1204 	case SIOCSIFCAP:
1205 		error = suser_td(td);
1206 		if (error)
1207 			return (error);
1208 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1209 			return (EINVAL);
1210 		(void) (*ifp->if_ioctl)(ifp, cmd, data);
1211 		break;
1212 
1213 	case SIOCSIFMETRIC:
1214 		error = suser_td(td);
1215 		if (error)
1216 			return (error);
1217 		ifp->if_metric = ifr->ifr_metric;
1218 		getmicrotime(&ifp->if_lastchange);
1219 		break;
1220 
1221 	case SIOCSIFPHYS:
1222 		error = suser_td(td);
1223 		if (error)
1224 			return error;
1225 		if (!ifp->if_ioctl)
1226 		        return EOPNOTSUPP;
1227 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1228 		if (error == 0)
1229 			getmicrotime(&ifp->if_lastchange);
1230 		return(error);
1231 
1232 	case SIOCSIFMTU:
1233 	{
1234 		u_long oldmtu = ifp->if_mtu;
1235 
1236 		error = suser_td(td);
1237 		if (error)
1238 			return (error);
1239 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1240 			return (EINVAL);
1241 		if (ifp->if_ioctl == NULL)
1242 			return (EOPNOTSUPP);
1243 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1244 		if (error == 0) {
1245 			getmicrotime(&ifp->if_lastchange);
1246 			rt_ifmsg(ifp);
1247 		}
1248 		/*
1249 		 * If the link MTU changed, do network layer specific procedure.
1250 		 */
1251 		if (ifp->if_mtu != oldmtu) {
1252 #ifdef INET6
1253 			nd6_setmtu(ifp);
1254 #endif
1255 		}
1256 		break;
1257 	}
1258 
1259 	case SIOCADDMULTI:
1260 	case SIOCDELMULTI:
1261 		error = suser_td(td);
1262 		if (error)
1263 			return (error);
1264 
1265 		/* Don't allow group membership on non-multicast interfaces. */
1266 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1267 			return (EOPNOTSUPP);
1268 
1269 		/* Don't let users screw up protocols' entries. */
1270 		if (ifr->ifr_addr.sa_family != AF_LINK)
1271 			return (EINVAL);
1272 
1273 		if (cmd == SIOCADDMULTI) {
1274 			struct ifmultiaddr *ifma;
1275 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1276 		} else {
1277 			error = if_delmulti(ifp, &ifr->ifr_addr);
1278 		}
1279 		if (error == 0)
1280 			getmicrotime(&ifp->if_lastchange);
1281 		break;
1282 
1283 	case SIOCSIFPHYADDR:
1284 	case SIOCDIFPHYADDR:
1285 #ifdef INET6
1286 	case SIOCSIFPHYADDR_IN6:
1287 #endif
1288 	case SIOCSLIFPHYADDR:
1289         case SIOCSIFMEDIA:
1290 	case SIOCSIFGENERIC:
1291 		error = suser_td(td);
1292 		if (error)
1293 			return (error);
1294 		if (ifp->if_ioctl == NULL)
1295 			return (EOPNOTSUPP);
1296 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1297 		if (error == 0)
1298 			getmicrotime(&ifp->if_lastchange);
1299 		break;
1300 
1301 	case SIOCGIFSTATUS:
1302 		ifs = (struct ifstat *)data;
1303 		ifs->ascii[0] = '\0';
1304 
1305 	case SIOCGIFPSRCADDR:
1306 	case SIOCGIFPDSTADDR:
1307 	case SIOCGLIFPHYADDR:
1308 	case SIOCGIFMEDIA:
1309 	case SIOCGIFGENERIC:
1310 		if (ifp->if_ioctl == 0)
1311 			return (EOPNOTSUPP);
1312 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1313 		break;
1314 
1315 	case SIOCSIFLLADDR:
1316 		error = suser_td(td);
1317 		if (error)
1318 			return (error);
1319 		error = if_setlladdr(ifp,
1320 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1321 		break;
1322 
1323 	default:
1324 		error = ENOIOCTL;
1325 		break;
1326 	}
1327 	return (error);
1328 }
1329 
1330 /*
1331  * Interface ioctls.
1332  */
1333 int
1334 ifioctl(so, cmd, data, td)
1335 	struct socket *so;
1336 	u_long cmd;
1337 	caddr_t data;
1338 	struct thread *td;
1339 {
1340 	struct ifnet *ifp;
1341 	struct ifreq *ifr;
1342 	int error;
1343 	short oif_flags;
1344 
1345 	switch (cmd) {
1346 	case SIOCGIFCONF:
1347 	case OSIOCGIFCONF:
1348 		return (ifconf(cmd, data));
1349 	}
1350 	ifr = (struct ifreq *)data;
1351 
1352 	switch (cmd) {
1353 	case SIOCIFCREATE:
1354 	case SIOCIFDESTROY:
1355 		if ((error = suser_td(td)) != 0)
1356 			return (error);
1357 		return ((cmd == SIOCIFCREATE) ?
1358 			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1359 			if_clone_destroy(ifr->ifr_name));
1360 
1361 	case SIOCIFGCLONERS:
1362 		return (if_clone_list((struct if_clonereq *)data));
1363 	}
1364 
1365 	ifp = ifunit(ifr->ifr_name);
1366 	if (ifp == 0)
1367 		return (ENXIO);
1368 
1369 	error = ifhwioctl(cmd, ifp, data, td);
1370 	if (error != ENOIOCTL)
1371 		return (error);
1372 
1373 	oif_flags = ifp->if_flags;
1374 	if (so->so_proto == 0)
1375 		return (EOPNOTSUPP);
1376 #ifndef COMPAT_43
1377 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1378 								 data,
1379 								 ifp, td));
1380 #else
1381 	{
1382 		int ocmd = cmd;
1383 
1384 		switch (cmd) {
1385 
1386 		case SIOCSIFDSTADDR:
1387 		case SIOCSIFADDR:
1388 		case SIOCSIFBRDADDR:
1389 		case SIOCSIFNETMASK:
1390 #if BYTE_ORDER != BIG_ENDIAN
1391 			if (ifr->ifr_addr.sa_family == 0 &&
1392 			    ifr->ifr_addr.sa_len < 16) {
1393 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1394 				ifr->ifr_addr.sa_len = 16;
1395 			}
1396 #else
1397 			if (ifr->ifr_addr.sa_len == 0)
1398 				ifr->ifr_addr.sa_len = 16;
1399 #endif
1400 			break;
1401 
1402 		case OSIOCGIFADDR:
1403 			cmd = SIOCGIFADDR;
1404 			break;
1405 
1406 		case OSIOCGIFDSTADDR:
1407 			cmd = SIOCGIFDSTADDR;
1408 			break;
1409 
1410 		case OSIOCGIFBRDADDR:
1411 			cmd = SIOCGIFBRDADDR;
1412 			break;
1413 
1414 		case OSIOCGIFNETMASK:
1415 			cmd = SIOCGIFNETMASK;
1416 		}
1417 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
1418 								   cmd,
1419 								   data,
1420 								   ifp, td));
1421 		switch (ocmd) {
1422 
1423 		case OSIOCGIFADDR:
1424 		case OSIOCGIFDSTADDR:
1425 		case OSIOCGIFBRDADDR:
1426 		case OSIOCGIFNETMASK:
1427 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1428 
1429 		}
1430 	}
1431 #endif /* COMPAT_43 */
1432 
1433 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1434 #ifdef INET6
1435 		DELAY(100);/* XXX: temporal workaround for fxp issue*/
1436 		if (ifp->if_flags & IFF_UP) {
1437 			int s = splimp();
1438 			in6_if_up(ifp);
1439 			splx(s);
1440 		}
1441 #endif
1442 	}
1443 	return (error);
1444 }
1445 
1446 /*
1447  * Set/clear promiscuous mode on interface ifp based on the truth value
1448  * of pswitch.  The calls are reference counted so that only the first
1449  * "on" request actually has an effect, as does the final "off" request.
1450  * Results are undefined if the "off" and "on" requests are not matched.
1451  */
1452 int
1453 ifpromisc(ifp, pswitch)
1454 	struct ifnet *ifp;
1455 	int pswitch;
1456 {
1457 	struct ifreq ifr;
1458 	int error;
1459 	int oldflags, oldpcount;
1460 
1461 	oldpcount = ifp->if_pcount;
1462 	oldflags = ifp->if_flags;
1463 	if (pswitch) {
1464 		/*
1465 		 * If the device is not configured up, we cannot put it in
1466 		 * promiscuous mode.
1467 		 */
1468 		if ((ifp->if_flags & IFF_UP) == 0)
1469 			return (ENETDOWN);
1470 		if (ifp->if_pcount++ != 0)
1471 			return (0);
1472 		ifp->if_flags |= IFF_PROMISC;
1473 	} else {
1474 		if (--ifp->if_pcount > 0)
1475 			return (0);
1476 		ifp->if_flags &= ~IFF_PROMISC;
1477 	}
1478 	ifr.ifr_flags = ifp->if_flags;
1479 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1480 	if (error == 0) {
1481 		log(LOG_INFO, "%s%d: promiscuous mode %s\n",
1482 		    ifp->if_name, ifp->if_unit,
1483 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1484 		rt_ifmsg(ifp);
1485 	} else {
1486 		ifp->if_pcount = oldpcount;
1487 		ifp->if_flags = oldflags;
1488 	}
1489 	return error;
1490 }
1491 
1492 /*
1493  * Return interface configuration
1494  * of system.  List may be used
1495  * in later ioctl's (above) to get
1496  * other information.
1497  */
1498 /*ARGSUSED*/
1499 static int
1500 ifconf(cmd, data)
1501 	u_long cmd;
1502 	caddr_t data;
1503 {
1504 	struct ifconf *ifc = (struct ifconf *)data;
1505 	struct ifnet *ifp;
1506 	struct ifaddr *ifa;
1507 	struct ifreq ifr, *ifrp;
1508 	int space = ifc->ifc_len, error = 0;
1509 
1510 	ifrp = ifc->ifc_req;
1511 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1512 		char workbuf[64];
1513 		int ifnlen, addrs;
1514 
1515 		if (space < sizeof(ifr))
1516 			break;
1517 		ifnlen = snprintf(workbuf, sizeof(workbuf),
1518 		    "%s%d", ifp->if_name, ifp->if_unit);
1519 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
1520 			error = ENAMETOOLONG;
1521 			break;
1522 		} else {
1523 			strcpy(ifr.ifr_name, workbuf);
1524 		}
1525 
1526 		addrs = 0;
1527 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1528 			struct sockaddr *sa = ifa->ifa_addr;
1529 
1530 			if (space < sizeof(ifr))
1531 				break;
1532 			if (jailed(curproc->p_ucred) &&
1533 			    prison_if(curproc->p_ucred, sa))
1534 				continue;
1535 			addrs++;
1536 #ifdef COMPAT_43
1537 			if (cmd == OSIOCGIFCONF) {
1538 				struct osockaddr *osa =
1539 					 (struct osockaddr *)&ifr.ifr_addr;
1540 				ifr.ifr_addr = *sa;
1541 				osa->sa_family = sa->sa_family;
1542 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1543 						sizeof (ifr));
1544 				ifrp++;
1545 			} else
1546 #endif
1547 			if (sa->sa_len <= sizeof(*sa)) {
1548 				ifr.ifr_addr = *sa;
1549 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1550 						sizeof (ifr));
1551 				ifrp++;
1552 			} else {
1553 				if (space < sizeof (ifr) + sa->sa_len -
1554 					    sizeof(*sa))
1555 					break;
1556 				space -= sa->sa_len - sizeof(*sa);
1557 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1558 						sizeof (ifr.ifr_name));
1559 				if (error == 0)
1560 				    error = copyout((caddr_t)sa,
1561 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1562 				ifrp = (struct ifreq *)
1563 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1564 			}
1565 			if (error)
1566 				break;
1567 			space -= sizeof (ifr);
1568 		}
1569 		if (error)
1570 			break;
1571 		if (!addrs) {
1572 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1573 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1574 			    sizeof (ifr));
1575 			if (error)
1576 				break;
1577 			space -= sizeof (ifr);
1578 			ifrp++;
1579 		}
1580 	}
1581 	ifc->ifc_len -= space;
1582 	return (error);
1583 }
1584 
1585 /*
1586  * Just like if_promisc(), but for all-multicast-reception mode.
1587  */
1588 int
1589 if_allmulti(ifp, onswitch)
1590 	struct ifnet *ifp;
1591 	int onswitch;
1592 {
1593 	int error = 0;
1594 	int s = splimp();
1595 
1596 	if (onswitch) {
1597 		if (ifp->if_amcount++ == 0) {
1598 			ifp->if_flags |= IFF_ALLMULTI;
1599 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1600 		}
1601 	} else {
1602 		if (ifp->if_amcount > 1) {
1603 			ifp->if_amcount--;
1604 		} else {
1605 			ifp->if_amcount = 0;
1606 			ifp->if_flags &= ~IFF_ALLMULTI;
1607 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1608 		}
1609 	}
1610 	splx(s);
1611 
1612 	if (error == 0)
1613 		rt_ifmsg(ifp);
1614 	return error;
1615 }
1616 
1617 /*
1618  * Add a multicast listenership to the interface in question.
1619  * The link layer provides a routine which converts
1620  */
1621 int
1622 if_addmulti(ifp, sa, retifma)
1623 	struct ifnet *ifp;	/* interface to manipulate */
1624 	struct sockaddr *sa;	/* address to add */
1625 	struct ifmultiaddr **retifma;
1626 {
1627 	struct sockaddr *llsa, *dupsa;
1628 	int error, s;
1629 	struct ifmultiaddr *ifma;
1630 
1631 	/*
1632 	 * If the matching multicast address already exists
1633 	 * then don't add a new one, just add a reference
1634 	 */
1635 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1636 		if (equal(sa, ifma->ifma_addr)) {
1637 			ifma->ifma_refcount++;
1638 			if (retifma)
1639 				*retifma = ifma;
1640 			return 0;
1641 		}
1642 	}
1643 
1644 	/*
1645 	 * Give the link layer a chance to accept/reject it, and also
1646 	 * find out which AF_LINK address this maps to, if it isn't one
1647 	 * already.
1648 	 */
1649 	if (ifp->if_resolvemulti) {
1650 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
1651 		if (error) return error;
1652 	} else {
1653 		llsa = 0;
1654 	}
1655 
1656 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1657 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1658 	bcopy(sa, dupsa, sa->sa_len);
1659 
1660 	ifma->ifma_addr = dupsa;
1661 	ifma->ifma_lladdr = llsa;
1662 	ifma->ifma_ifp = ifp;
1663 	ifma->ifma_refcount = 1;
1664 	ifma->ifma_protospec = 0;
1665 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1666 
1667 	/*
1668 	 * Some network interfaces can scan the address list at
1669 	 * interrupt time; lock them out.
1670 	 */
1671 	s = splimp();
1672 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1673 	splx(s);
1674 	*retifma = ifma;
1675 
1676 	if (llsa != 0) {
1677 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1678 			if (equal(ifma->ifma_addr, llsa))
1679 				break;
1680 		}
1681 		if (ifma) {
1682 			ifma->ifma_refcount++;
1683 		} else {
1684 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1685 			       M_IFMADDR, M_WAITOK);
1686 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1687 			       M_IFMADDR, M_WAITOK);
1688 			bcopy(llsa, dupsa, llsa->sa_len);
1689 			ifma->ifma_addr = dupsa;
1690 			ifma->ifma_ifp = ifp;
1691 			ifma->ifma_refcount = 1;
1692 			s = splimp();
1693 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1694 			splx(s);
1695 		}
1696 	}
1697 	/*
1698 	 * We are certain we have added something, so call down to the
1699 	 * interface to let them know about it.
1700 	 */
1701 	s = splimp();
1702 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1703 	splx(s);
1704 
1705 	return 0;
1706 }
1707 
1708 /*
1709  * Remove a reference to a multicast address on this interface.  Yell
1710  * if the request does not match an existing membership.
1711  */
1712 int
1713 if_delmulti(ifp, sa)
1714 	struct ifnet *ifp;
1715 	struct sockaddr *sa;
1716 {
1717 	struct ifmultiaddr *ifma;
1718 	int s;
1719 
1720 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1721 		if (equal(sa, ifma->ifma_addr))
1722 			break;
1723 	if (ifma == 0)
1724 		return ENOENT;
1725 
1726 	if (ifma->ifma_refcount > 1) {
1727 		ifma->ifma_refcount--;
1728 		return 0;
1729 	}
1730 
1731 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1732 	sa = ifma->ifma_lladdr;
1733 	s = splimp();
1734 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1735 	/*
1736 	 * Make sure the interface driver is notified
1737 	 * in the case of a link layer mcast group being left.
1738 	 */
1739 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1740 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1741 	splx(s);
1742 	free(ifma->ifma_addr, M_IFMADDR);
1743 	free(ifma, M_IFMADDR);
1744 	if (sa == 0)
1745 		return 0;
1746 
1747 	/*
1748 	 * Now look for the link-layer address which corresponds to
1749 	 * this network address.  It had been squirreled away in
1750 	 * ifma->ifma_lladdr for this purpose (so we don't have
1751 	 * to call ifp->if_resolvemulti() again), and we saved that
1752 	 * value in sa above.  If some nasty deleted the
1753 	 * link-layer address out from underneath us, we can deal because
1754 	 * the address we stored was is not the same as the one which was
1755 	 * in the record for the link-layer address.  (So we don't complain
1756 	 * in that case.)
1757 	 */
1758 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1759 		if (equal(sa, ifma->ifma_addr))
1760 			break;
1761 	if (ifma == 0)
1762 		return 0;
1763 
1764 	if (ifma->ifma_refcount > 1) {
1765 		ifma->ifma_refcount--;
1766 		return 0;
1767 	}
1768 
1769 	s = splimp();
1770 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1771 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1772 	splx(s);
1773 	free(ifma->ifma_addr, M_IFMADDR);
1774 	free(sa, M_IFMADDR);
1775 	free(ifma, M_IFMADDR);
1776 
1777 	return 0;
1778 }
1779 
1780 /*
1781  * Set the link layer address on an interface.
1782  *
1783  * At this time we only support certain types of interfaces,
1784  * and we don't allow the length of the address to change.
1785  */
1786 int
1787 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1788 {
1789 	struct sockaddr_dl *sdl;
1790 	struct ifaddr *ifa;
1791 
1792 	ifa = ifaddr_byindex(ifp->if_index);
1793 	if (ifa == NULL)
1794 		return (EINVAL);
1795 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1796 	if (sdl == NULL)
1797 		return (EINVAL);
1798 	if (len != sdl->sdl_alen)	/* don't allow length to change */
1799 		return (EINVAL);
1800 	switch (ifp->if_type) {
1801 	case IFT_ETHER:			/* these types use struct arpcom */
1802 	case IFT_FDDI:
1803 	case IFT_XETHER:
1804 	case IFT_ISO88025:
1805 	case IFT_L2VLAN:
1806 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1807 		bcopy(lladdr, LLADDR(sdl), len);
1808 		break;
1809 	default:
1810 		return (ENODEV);
1811 	}
1812 	/*
1813 	 * If the interface is already up, we need
1814 	 * to re-init it in order to reprogram its
1815 	 * address filter.
1816 	 */
1817 	if ((ifp->if_flags & IFF_UP) != 0) {
1818 		ifp->if_flags &= ~IFF_UP;
1819 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1820 		ifp->if_flags |= IFF_UP;
1821 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1822 #ifdef INET
1823 		/*
1824 		 * Also send gratuitous ARPs to notify other nodes about
1825 		 * the address change.
1826 		 */
1827 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1828 			if (ifa->ifa_addr != NULL &&
1829 			    ifa->ifa_addr->sa_family == AF_INET)
1830 				arp_ifinit((struct arpcom *)ifp, ifa);
1831 		}
1832 #endif
1833 	}
1834 	return (0);
1835 }
1836 
1837 struct ifmultiaddr *
1838 ifmaof_ifpforaddr(sa, ifp)
1839 	struct sockaddr *sa;
1840 	struct ifnet *ifp;
1841 {
1842 	struct ifmultiaddr *ifma;
1843 
1844 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1845 		if (equal(ifma->ifma_addr, sa))
1846 			break;
1847 
1848 	return ifma;
1849 }
1850 
1851 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1852 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1853