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