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