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