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