xref: /netbsd-src/sys/net/if.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: if.c,v 1.57 2000/03/06 20:49:00 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by William Studnemund and Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the project nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 /*
69  * Copyright (c) 1980, 1986, 1993
70  *	The Regents of the University of California.  All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  * 3. All advertising materials mentioning features or use of this software
81  *    must display the following acknowledgement:
82  *	This product includes software developed by the University of
83  *	California, Berkeley and its contributors.
84  * 4. Neither the name of the University nor the names of its contributors
85  *    may be used to endorse or promote products derived from this software
86  *    without specific prior written permission.
87  *
88  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98  * SUCH DAMAGE.
99  *
100  *	@(#)if.c	8.5 (Berkeley) 1/9/95
101  */
102 
103 #include "opt_inet.h"
104 
105 #include "opt_compat_linux.h"
106 #include "opt_compat_svr4.h"
107 #include "opt_compat_43.h"
108 #include "opt_atalk.h"
109 
110 #include <sys/param.h>
111 #include <sys/mbuf.h>
112 #include <sys/systm.h>
113 #include <sys/proc.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/domain.h>
117 #include <sys/protosw.h>
118 #include <sys/kernel.h>
119 #include <sys/ioctl.h>
120 
121 #include <net/if.h>
122 #include <net/if_dl.h>
123 #include <net/if_types.h>
124 #include <net/radix.h>
125 #include <net/route.h>
126 #ifdef NETATALK
127 #include <netatalk/at_extern.h>
128 #include <netatalk/at.h>
129 #endif
130 
131 #ifdef INET6
132 /*XXX*/
133 #include <netinet/in.h>
134 #endif
135 
136 int	ifqmaxlen = IFQ_MAXLEN;
137 void	if_slowtimo __P((void *arg));
138 
139 #ifdef INET6
140 /*
141  * XXX: declare here to avoid to include many inet6 related files..
142  * should be more generalized?
143  */
144 extern void nd6_setmtu __P((struct ifnet *));
145 #endif
146 
147 int	if_rt_walktree __P((struct radix_node *, void *));
148 
149 /*
150  * Network interface utility routines.
151  *
152  * Routines with ifa_ifwith* names take sockaddr *'s as
153  * parameters.
154  */
155 void
156 ifinit()
157 {
158 
159 	if_slowtimo(NULL);
160 }
161 
162 /*
163  * Null routines used while an interface is going away.  These routines
164  * just return an error.
165  */
166 int	if_nulloutput __P((struct ifnet *, struct mbuf *,
167 	    struct sockaddr *, struct rtentry *));
168 void	if_nullinput __P((struct ifnet *, struct mbuf *));
169 void	if_nullstart __P((struct ifnet *));
170 int	if_nullioctl __P((struct ifnet *, u_long, caddr_t));
171 int	if_nullreset __P((struct ifnet *));
172 void	if_nullwatchdog __P((struct ifnet *));
173 void	if_nulldrain __P((struct ifnet *));
174 
175 int
176 if_nulloutput(ifp, m, so, rt)
177 	struct ifnet *ifp;
178 	struct mbuf *m;
179 	struct sockaddr *so;
180 	struct rtentry *rt;
181 {
182 
183 	return (ENXIO);
184 }
185 
186 void
187 if_nullinput(ifp, m)
188 	struct ifnet *ifp;
189 	struct mbuf *m;
190 {
191 
192 	/* Nothing. */
193 }
194 
195 void
196 if_nullstart(ifp)
197 	struct ifnet *ifp;
198 {
199 
200 	/* Nothing. */
201 }
202 
203 int
204 if_nullioctl(ifp, cmd, data)
205 	struct ifnet *ifp;
206 	u_long cmd;
207 	caddr_t data;
208 {
209 
210 	return (ENXIO);
211 }
212 
213 int
214 if_nullreset(ifp)
215 	struct ifnet *ifp;
216 {
217 
218 	return (ENXIO);
219 }
220 
221 void
222 if_nullwatchdog(ifp)
223 	struct ifnet *ifp;
224 {
225 
226 	/* Nothing. */
227 }
228 
229 void
230 if_nulldrain(ifp)
231 	struct ifnet *ifp;
232 {
233 
234 	/* Nothing. */
235 }
236 
237 int if_index = 0;
238 struct ifaddr **ifnet_addrs = NULL;
239 struct ifnet **ifindex2ifnet = NULL;
240 
241 /*
242  * Attach an interface to the
243  * list of "active" interfaces.
244  */
245 void
246 if_attach(ifp)
247 	struct ifnet *ifp;
248 {
249 	unsigned socksize, ifasize;
250 	int namelen, masklen;
251 	register struct sockaddr_dl *sdl;
252 	register struct ifaddr *ifa;
253 	static size_t if_indexlim = 8;
254 
255 	if (if_index == 0)
256 		TAILQ_INIT(&ifnet);
257 	TAILQ_INIT(&ifp->if_addrlist);
258 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
259 	ifp->if_index = ++if_index;
260 
261 	/*
262 	 * We have some arrays that should be indexed by if_index.
263 	 * since if_index will grow dynamically, they should grow too.
264 	 *	struct ifadd **ifnet_addrs
265 	 *	struct ifnet **ifindex2ifnet
266 	 */
267 	if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
268 	    ifp->if_index >= if_indexlim) {
269 		size_t n;
270 		caddr_t q;
271 
272 		while (ifp->if_index >= if_indexlim)
273 			if_indexlim <<= 1;
274 
275 		/* grow ifnet_addrs */
276 		n = if_indexlim * sizeof(ifa);
277 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
278 		bzero(q, n);
279 		if (ifnet_addrs) {
280 			bcopy((caddr_t)ifnet_addrs, q, n/2);
281 			free((caddr_t)ifnet_addrs, M_IFADDR);
282 		}
283 		ifnet_addrs = (struct ifaddr **)q;
284 
285 		/* grow ifindex2ifnet */
286 		n = if_indexlim * sizeof(struct ifnet *);
287 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
288 		bzero(q, n);
289 		if (ifindex2ifnet) {
290 			bcopy((caddr_t)ifindex2ifnet, q, n/2);
291 			free((caddr_t)ifindex2ifnet, M_IFADDR);
292 		}
293 		ifindex2ifnet = (struct ifnet **)q;
294 	}
295 
296 	ifindex2ifnet[ifp->if_index] = ifp;
297 
298 	/*
299 	 * create a Link Level name for this device
300 	 */
301 	namelen = strlen(ifp->if_xname);
302 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
303 	socksize = masklen + ifp->if_addrlen;
304 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
305 	if (socksize < sizeof(*sdl))
306 		socksize = sizeof(*sdl);
307 	socksize = ROUNDUP(socksize);
308 	ifasize = sizeof(*ifa) + 2 * socksize;
309 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
310 	bzero((caddr_t)ifa, ifasize);
311 	sdl = (struct sockaddr_dl *)(ifa + 1);
312 	sdl->sdl_len = socksize;
313 	sdl->sdl_family = AF_LINK;
314 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
315 	sdl->sdl_nlen = namelen;
316 	sdl->sdl_index = ifp->if_index;
317 	sdl->sdl_type = ifp->if_type;
318 	ifnet_addrs[ifp->if_index] = ifa;
319 	IFAREF(ifa);
320 	ifa->ifa_ifp = ifp;
321 	ifa->ifa_rtrequest = link_rtrequest;
322 	TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
323 	IFAREF(ifa);
324 	ifa->ifa_addr = (struct sockaddr *)sdl;
325 	ifp->if_sadl = sdl;
326 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
327 	ifa->ifa_netmask = (struct sockaddr *)sdl;
328 	sdl->sdl_len = masklen;
329 	while (namelen != 0)
330 		sdl->sdl_data[--namelen] = 0xff;
331 	if (ifp->if_snd.ifq_maxlen == 0)
332 	    ifp->if_snd.ifq_maxlen = ifqmaxlen;
333 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
334 
335 	ifp->if_link_state = LINK_STATE_UNKNOWN;
336 
337 	/* Announce the interface. */
338 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
339 }
340 
341 /*
342  * Deactivate an interface.  This points all of the procedure
343  * handles at error stubs.  May be called from interrupt context.
344  */
345 void
346 if_deactivate(ifp)
347 	struct ifnet *ifp;
348 {
349 	int s;
350 
351 	s = splimp();
352 
353 	ifp->if_output	 = if_nulloutput;
354 	ifp->if_input	 = if_nullinput;
355 	ifp->if_start	 = if_nullstart;
356 	ifp->if_ioctl	 = if_nullioctl;
357 	ifp->if_reset	 = if_nullreset;
358 	ifp->if_watchdog = if_nullwatchdog;
359 	ifp->if_drain	 = if_nulldrain;
360 
361 	/* No more packets may be enqueued. */
362 	ifp->if_snd.ifq_maxlen = 0;
363 
364 	splx(s);
365 }
366 
367 /*
368  * Detach an interface from the list of "active" interfaces,
369  * freeing any resources as we go along.
370  *
371  * NOTE: This routine must be called with a valid thread context,
372  * as it may block.
373  */
374 void
375 if_detach(ifp)
376 	struct ifnet *ifp;
377 {
378 	struct socket so;
379 	struct ifaddr *ifa;
380 #ifdef IFAREF_DEBUG
381 	struct ifaddr *last_ifa = NULL;
382 #endif
383 	struct domain *dp;
384 	struct protosw *pr;
385 	struct radix_node_head *rnh;
386 	int s, i, family, purged;
387 
388 	/*
389 	 * XXX It's kind of lame that we have to have the
390 	 * XXX socket structure...
391 	 */
392 	memset(&so, 0, sizeof(so));
393 
394 	s = splimp();
395 
396 	/*
397 	 * Do an if_down() to give protocols a chance to do something.
398 	 */
399 	if_down(ifp);
400 
401 	/*
402 	 * Rip all the addresses off the interface.  This should make
403 	 * all of the routes go away.
404 	 */
405 	while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
406 		family = ifa->ifa_addr->sa_family;
407 #ifdef IFAREF_DEBUG
408 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
409 		    ifa, family, ifa->ifa_refcnt);
410 		if (last_ifa != NULL && ifa == last_ifa)
411 			panic("if_detach: loop detected");
412 		last_ifa = ifa;
413 #endif
414 		if (family == AF_LINK) {
415 			rtinit(ifa, RTM_DELETE, 0);
416 			TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
417 			IFAFREE(ifa);
418 		} else {
419 			dp = pffinddomain(family);
420 #ifdef DIAGNOSTIC
421 			if (dp == NULL)
422 				panic("if_detach: no domain for AF %d\n",
423 				    family);
424 #endif
425 			purged = 0;
426 			for (pr = dp->dom_protosw;
427 			     pr < dp->dom_protoswNPROTOSW; pr++) {
428 				so.so_proto = pr;
429 				if (pr->pr_usrreq != NULL) {
430 					(void) (*pr->pr_usrreq)(&so,
431 					    PRU_PURGEIF, NULL, NULL,
432 					    (struct mbuf *) ifp, curproc);
433 					purged = 1;
434 				}
435 			}
436 			if (purged == 0) {
437 				/*
438 				 * XXX What's really the best thing to do
439 				 * XXX here?  --thorpej@netbsd.org
440 				 */
441 				printf("if_detach: WARNING: AF %d not purged\n",
442 				    family);
443 			}
444 		}
445 	}
446 
447 	/* Walk the routing table looking for straglers. */
448 	for (i = 0; i <= AF_MAX; i++) {
449 		if ((rnh = rt_tables[i]) != NULL)
450 			(void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
451 	}
452 
453 	IFAFREE(ifnet_addrs[ifp->if_index]);
454 	ifnet_addrs[ifp->if_index] = NULL;
455 
456 	/* Announce that the interface is gone. */
457 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
458 
459 	TAILQ_REMOVE(&ifnet, ifp, if_list);
460 
461 	splx(s);
462 }
463 
464 /*
465  * Callback for a radix tree walk to delete all references to an
466  * ifnet.
467  */
468 int
469 if_rt_walktree(rn, v)
470 	struct radix_node *rn;
471 	void *v;
472 {
473 	struct ifnet *ifp = (struct ifnet *)v;
474 	struct rtentry *rt = (struct rtentry *)rn;
475 	int error;
476 
477 	if (rt->rt_ifp == ifp) {
478 		/* Delete the entry. */
479 		error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
480 		    rt_mask(rt), rt->rt_flags, NULL);
481 		if (error)
482 			printf("%s: warning: unable to delete rtentry @ %p, "
483 			    "error = %d\n", ifp->if_xname, rt, error);
484 	}
485 	return (0);
486 }
487 
488 /*
489  * Locate an interface based on a complete address.
490  */
491 /*ARGSUSED*/
492 struct ifaddr *
493 ifa_ifwithaddr(addr)
494 	register struct sockaddr *addr;
495 {
496 	register struct ifnet *ifp;
497 	register struct ifaddr *ifa;
498 
499 #define	equal(a1, a2) \
500   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
501 
502 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
503 	     ifp = TAILQ_NEXT(ifp, if_list)) {
504 		if (ifp->if_output == if_nulloutput)
505 			continue;
506 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
507 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
508 			if (ifa->ifa_addr->sa_family != addr->sa_family)
509 				continue;
510 			if (equal(addr, ifa->ifa_addr))
511 				return (ifa);
512 			if ((ifp->if_flags & IFF_BROADCAST) &&
513 			    ifa->ifa_broadaddr &&
514 			    /* IP6 doesn't have broadcast */
515 			    ifa->ifa_broadaddr->sa_len != 0 &&
516 			    equal(ifa->ifa_broadaddr, addr))
517 				return (ifa);
518 		}
519 	}
520 	return (NULL);
521 }
522 
523 /*
524  * Locate the point to point interface with a given destination address.
525  */
526 /*ARGSUSED*/
527 struct ifaddr *
528 ifa_ifwithdstaddr(addr)
529 	register struct sockaddr *addr;
530 {
531 	register struct ifnet *ifp;
532 	register struct ifaddr *ifa;
533 
534 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
535 	     ifp = TAILQ_NEXT(ifp, if_list)) {
536 		if (ifp->if_output == if_nulloutput)
537 			continue;
538 		if (ifp->if_flags & IFF_POINTOPOINT) {
539 			for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
540 			     ifa = TAILQ_NEXT(ifa, ifa_list)) {
541 				if (ifa->ifa_addr->sa_family !=
542 				      addr->sa_family ||
543 				    ifa->ifa_dstaddr == NULL)
544 					continue;
545 				if (equal(addr, ifa->ifa_dstaddr))
546 					return (ifa);
547 			}
548 		}
549 	}
550 	return (NULL);
551 }
552 
553 /*
554  * Find an interface on a specific network.  If many, choice
555  * is most specific found.
556  */
557 struct ifaddr *
558 ifa_ifwithnet(addr)
559 	struct sockaddr *addr;
560 {
561 	register struct ifnet *ifp;
562 	register struct ifaddr *ifa;
563 	register struct sockaddr_dl *sdl;
564 	struct ifaddr *ifa_maybe = 0;
565 	u_int af = addr->sa_family;
566 	char *addr_data = addr->sa_data, *cplim;
567 
568 	if (af == AF_LINK) {
569 		sdl = (struct sockaddr_dl *)addr;
570 		if (sdl->sdl_index && sdl->sdl_index <= if_index &&
571 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
572 			return (ifnet_addrs[sdl->sdl_index]);
573 	}
574 #ifdef NETATALK
575 	if (af == AF_APPLETALK) {
576 		for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
577 		     ifp = TAILQ_NEXT(ifp, if_list)) {
578 			if (ifp->if_output == if_nulloutput)
579 				continue;
580 			ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
581 			if (ifa)
582 				return (ifa);
583 		}
584 		return (NULL);
585 	}
586 #endif
587 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
588 	     ifp = TAILQ_NEXT(ifp, if_list)) {
589 		if (ifp->if_output == if_nulloutput)
590 			continue;
591 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
592 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
593 			register char *cp, *cp2, *cp3;
594 
595 			if (ifa->ifa_addr->sa_family != af ||
596 			    ifa->ifa_netmask == 0)
597  next:				continue;
598 			cp = addr_data;
599 			cp2 = ifa->ifa_addr->sa_data;
600 			cp3 = ifa->ifa_netmask->sa_data;
601 			cplim = (char *)ifa->ifa_netmask +
602 			    ifa->ifa_netmask->sa_len;
603 			while (cp3 < cplim) {
604 				if ((*cp++ ^ *cp2++) & *cp3++) {
605 					/* want to continue for() loop */
606 					goto next;
607 				}
608 			}
609 			if (ifa_maybe == 0 ||
610 			    rn_refines((caddr_t)ifa->ifa_netmask,
611 			    (caddr_t)ifa_maybe->ifa_netmask))
612 				ifa_maybe = ifa;
613 		}
614 	}
615 	return (ifa_maybe);
616 }
617 
618 /*
619  * Find the interface of the addresss.
620  */
621 struct ifaddr *
622 ifa_ifwithladdr(addr)
623 	struct sockaddr *addr;
624 {
625 	struct ifaddr *ia;
626 
627 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
628 	    (ia = ifa_ifwithnet(addr)))
629 		return (ia);
630 	return (NULL);
631 }
632 
633 /*
634  * Find an interface using a specific address family
635  */
636 struct ifaddr *
637 ifa_ifwithaf(af)
638 	register int af;
639 {
640 	register struct ifnet *ifp;
641 	register struct ifaddr *ifa;
642 
643 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
644 	     ifp = TAILQ_NEXT(ifp, if_list)) {
645 		if (ifp->if_output == if_nulloutput)
646 			continue;
647 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
648 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
649 			if (ifa->ifa_addr->sa_family == af)
650 				return (ifa);
651 		}
652 	}
653 	return (NULL);
654 }
655 
656 /*
657  * Find an interface address specific to an interface best matching
658  * a given address.
659  */
660 struct ifaddr *
661 ifaof_ifpforaddr(addr, ifp)
662 	struct sockaddr *addr;
663 	register struct ifnet *ifp;
664 {
665 	register struct ifaddr *ifa;
666 	register char *cp, *cp2, *cp3;
667 	register char *cplim;
668 	struct ifaddr *ifa_maybe = 0;
669 	u_int af = addr->sa_family;
670 
671 	if (ifp->if_output == if_nulloutput)
672 		return (NULL);
673 
674 	if (af >= AF_MAX)
675 		return (NULL);
676 
677 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
678 	     ifa = TAILQ_NEXT(ifa, ifa_list)) {
679 		if (ifa->ifa_addr->sa_family != af)
680 			continue;
681 		ifa_maybe = ifa;
682 		if (ifa->ifa_netmask == 0) {
683 			if (equal(addr, ifa->ifa_addr) ||
684 			    (ifa->ifa_dstaddr &&
685 			     equal(addr, ifa->ifa_dstaddr)))
686 				return (ifa);
687 			continue;
688 		}
689 		cp = addr->sa_data;
690 		cp2 = ifa->ifa_addr->sa_data;
691 		cp3 = ifa->ifa_netmask->sa_data;
692 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
693 		for (; cp3 < cplim; cp3++) {
694 			if ((*cp++ ^ *cp2++) & *cp3)
695 				break;
696 		}
697 		if (cp3 == cplim)
698 			return (ifa);
699 	}
700 	return (ifa_maybe);
701 }
702 
703 /*
704  * Default action when installing a route with a Link Level gateway.
705  * Lookup an appropriate real ifa to point to.
706  * This should be moved to /sys/net/link.c eventually.
707  */
708 void
709 link_rtrequest(cmd, rt, sa)
710 	int cmd;
711 	register struct rtentry *rt;
712 	struct sockaddr *sa;
713 {
714 	register struct ifaddr *ifa;
715 	struct sockaddr *dst;
716 	struct ifnet *ifp;
717 
718 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
719 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
720 		return;
721 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
722 		IFAFREE(rt->rt_ifa);
723 		rt->rt_ifa = ifa;
724 		IFAREF(ifa);
725 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
726 			ifa->ifa_rtrequest(cmd, rt, sa);
727 	}
728 }
729 
730 /*
731  * Mark an interface down and notify protocols of
732  * the transition.
733  * NOTE: must be called at splsoftnet or equivalent.
734  */
735 void
736 if_down(ifp)
737 	register struct ifnet *ifp;
738 {
739 	register struct ifaddr *ifa;
740 
741 	ifp->if_flags &= ~IFF_UP;
742 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
743 	     ifa = TAILQ_NEXT(ifa, ifa_list))
744 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
745 	if_qflush(&ifp->if_snd);
746 	rt_ifmsg(ifp);
747 }
748 
749 /*
750  * Mark an interface up and notify protocols of
751  * the transition.
752  * NOTE: must be called at splsoftnet or equivalent.
753  */
754 void
755 if_up(ifp)
756 	register struct ifnet *ifp;
757 {
758 #ifdef notyet
759 	register struct ifaddr *ifa;
760 #endif
761 
762 	ifp->if_flags |= IFF_UP;
763 #ifdef notyet
764 	/* this has no effect on IP, and will kill all ISO connections XXX */
765 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
766 	     ifa = TAILQ_NEXT(ifa, ifa_list))
767 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
768 #endif
769 	rt_ifmsg(ifp);
770 #ifdef INET6
771 	in6_if_up(ifp);
772 #endif
773 }
774 
775 /*
776  * Flush an interface queue.
777  */
778 void
779 if_qflush(ifq)
780 	register struct ifqueue *ifq;
781 {
782 	register struct mbuf *m, *n;
783 
784 	n = ifq->ifq_head;
785 	while ((m = n) != NULL) {
786 		n = m->m_act;
787 		m_freem(m);
788 	}
789 	ifq->ifq_head = 0;
790 	ifq->ifq_tail = 0;
791 	ifq->ifq_len = 0;
792 }
793 
794 /*
795  * Handle interface watchdog timer routines.  Called
796  * from softclock, we decrement timers (if set) and
797  * call the appropriate interface routine on expiration.
798  */
799 void
800 if_slowtimo(arg)
801 	void *arg;
802 {
803 	register struct ifnet *ifp;
804 	int s = splimp();
805 
806 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
807 	     ifp = TAILQ_NEXT(ifp, if_list)) {
808 		if (ifp->if_timer == 0 || --ifp->if_timer)
809 			continue;
810 		if (ifp->if_watchdog)
811 			(*ifp->if_watchdog)(ifp);
812 	}
813 	splx(s);
814 	timeout(if_slowtimo, NULL, hz / IFNET_SLOWHZ);
815 }
816 
817 /*
818  * Map interface name to
819  * interface structure pointer.
820  */
821 struct ifnet *
822 ifunit(name)
823 	const char *name;
824 {
825 	register struct ifnet *ifp;
826 
827 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
828 	     ifp = TAILQ_NEXT(ifp, if_list)) {
829 		if (ifp->if_output == if_nulloutput)
830 			continue;
831 	 	if (strcmp(ifp->if_xname, name) == 0)
832 			return (ifp);
833 	}
834 	return (NULL);
835 }
836 
837 
838 /*
839  * Map interface name in a sockaddr_dl to
840  * interface structure pointer.
841  */
842 struct ifnet *
843 if_withname(sa)
844 	struct sockaddr *sa;
845 {
846 	char ifname[IFNAMSIZ+1];
847 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
848 
849 	if ((sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
850 	     (sdl->sdl_nlen > IFNAMSIZ))
851 		return (NULL);
852 
853 	/*
854 	 * ifunit wants a null-terminated name.  It may not be null-terminated
855 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
856 	 * and there might not be room to put the trailing null anyway, so we
857 	 * make a local copy that we know we can null terminate safely.
858 	 */
859 
860 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
861 	ifname[sdl->sdl_nlen] = '\0';
862 	return ifunit(ifname);
863 }
864 
865 
866 /*
867  * Interface ioctls.
868  */
869 int
870 ifioctl(so, cmd, data, p)
871 	struct socket *so;
872 	u_long cmd;
873 	caddr_t data;
874 	struct proc *p;
875 {
876 	register struct ifnet *ifp;
877 	register struct ifreq *ifr;
878 	int error = 0;
879 	short oif_flags;
880 
881 	switch (cmd) {
882 
883 	case SIOCGIFCONF:
884 	case OSIOCGIFCONF:
885 		return (ifconf(cmd, data));
886 	}
887 	ifr = (struct ifreq *)data;
888 	ifp = ifunit(ifr->ifr_name);
889 	if (ifp == 0)
890 		return (ENXIO);
891 	oif_flags = ifp->if_flags;
892 	switch (cmd) {
893 
894 	case SIOCGIFFLAGS:
895 		ifr->ifr_flags = ifp->if_flags;
896 		break;
897 
898 	case SIOCGIFMETRIC:
899 		ifr->ifr_metric = ifp->if_metric;
900 		break;
901 
902 	case SIOCGIFMTU:
903 		ifr->ifr_mtu = ifp->if_mtu;
904 		break;
905 
906 	case SIOCSIFFLAGS:
907 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
908 			return (error);
909 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
910 			int s = splimp();
911 			if_down(ifp);
912 			splx(s);
913 		}
914 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
915 			int s = splimp();
916 			if_up(ifp);
917 			splx(s);
918 		}
919 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
920 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
921 		if (ifp->if_ioctl)
922 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
923 		break;
924 
925 	case SIOCSIFMETRIC:
926 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
927 			return (error);
928 		ifp->if_metric = ifr->ifr_metric;
929 		break;
930 
931 	case SIOCSIFMTU:
932 	{
933 		u_long oldmtu = ifp->if_mtu;
934 
935 		error = suser(p->p_ucred, &p->p_acflag);
936 		if (error)
937 			return (error);
938 		if (ifp->if_ioctl == NULL)
939 			return (EOPNOTSUPP);
940 		error = (*ifp->if_ioctl)(ifp, cmd, data);
941 
942 		/*
943 		 * If the link MTU changed, do network layer specific procedure.
944 		 */
945 		if (ifp->if_mtu != oldmtu) {
946 #ifdef INET6
947 			nd6_setmtu(ifp);
948 #endif
949 		}
950 		break;
951 	}
952 	case SIOCADDMULTI:
953 	case SIOCDELMULTI:
954 	case SIOCSIFMEDIA:
955 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
956 			return (error);
957 		/* FALLTHROUGH */
958 	case SIOCGIFMEDIA:
959 		if (ifp->if_ioctl == 0)
960 			return (EOPNOTSUPP);
961 		error = (*ifp->if_ioctl)(ifp, cmd, data);
962 		break;
963 
964 	case SIOCSDRVSPEC:
965 		/* XXX:  need to pass proc pointer through to driver... */
966 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
967 			return (error);
968 	/* FALLTHROUGH */
969 	default:
970 		if (so->so_proto == 0)
971 			return (EOPNOTSUPP);
972 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
973 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
974 		    (struct mbuf *)cmd, (struct mbuf *)data,
975 		    (struct mbuf *)ifp, p));
976 #else
977 	    {
978 		int ocmd = cmd;
979 
980 		switch (cmd) {
981 
982 		case SIOCSIFADDR:
983 		case SIOCSIFDSTADDR:
984 		case SIOCSIFBRDADDR:
985 		case SIOCSIFNETMASK:
986 #if BYTE_ORDER != BIG_ENDIAN
987 			if (ifr->ifr_addr.sa_family == 0 &&
988 			    ifr->ifr_addr.sa_len < 16) {
989 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
990 				ifr->ifr_addr.sa_len = 16;
991 			}
992 #else
993 			if (ifr->ifr_addr.sa_len == 0)
994 				ifr->ifr_addr.sa_len = 16;
995 #endif
996 			break;
997 
998 		case OSIOCGIFADDR:
999 			cmd = SIOCGIFADDR;
1000 			break;
1001 
1002 		case OSIOCGIFDSTADDR:
1003 			cmd = SIOCGIFDSTADDR;
1004 			break;
1005 
1006 		case OSIOCGIFBRDADDR:
1007 			cmd = SIOCGIFBRDADDR;
1008 			break;
1009 
1010 		case OSIOCGIFNETMASK:
1011 			cmd = SIOCGIFNETMASK;
1012 		}
1013 
1014 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1015 		    (struct mbuf *)cmd, (struct mbuf *)data,
1016 		    (struct mbuf *)ifp, p));
1017 
1018 		switch (ocmd) {
1019 		case OSIOCGIFADDR:
1020 		case OSIOCGIFDSTADDR:
1021 		case OSIOCGIFBRDADDR:
1022 		case OSIOCGIFNETMASK:
1023 			*(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1024 		}
1025 	    }
1026 #endif /* COMPAT_43 */
1027 		break;
1028 	}
1029 
1030 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1031 #ifdef INET6
1032 		if ((ifp->if_flags & IFF_UP) != 0) {
1033 			int s = splimp();
1034 			in6_if_up(ifp);
1035 			splx(s);
1036 		}
1037 #endif
1038 	}
1039 
1040 	return (error);
1041 }
1042 
1043 /*
1044  * Return interface configuration
1045  * of system.  List may be used
1046  * in later ioctl's (above) to get
1047  * other information.
1048  */
1049 /*ARGSUSED*/
1050 int
1051 ifconf(cmd, data)
1052 	u_long cmd;
1053 	caddr_t data;
1054 {
1055 	register struct ifconf *ifc = (struct ifconf *)data;
1056 	register struct ifnet *ifp;
1057 	register struct ifaddr *ifa;
1058 	struct ifreq ifr, *ifrp;
1059 	int space = ifc->ifc_len, error = 0;
1060 
1061 	ifrp = ifc->ifc_req;
1062 	for (ifp = ifnet.tqh_first;
1063 	    space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
1064 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1065 		if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
1066 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1067 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1068 			    sizeof(ifr));
1069 			if (error)
1070 				break;
1071 			space -= sizeof (ifr), ifrp++;
1072 		} else
1073 		    for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
1074 			register struct sockaddr *sa = ifa->ifa_addr;
1075 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
1076 			if (cmd == OSIOCGIFCONF) {
1077 				struct osockaddr *osa =
1078 					 (struct osockaddr *)&ifr.ifr_addr;
1079 				ifr.ifr_addr = *sa;
1080 				osa->sa_family = sa->sa_family;
1081 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1082 						sizeof (ifr));
1083 				ifrp++;
1084 			} else
1085 #endif
1086 			if (sa->sa_len <= sizeof(*sa)) {
1087 				ifr.ifr_addr = *sa;
1088 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1089 						sizeof (ifr));
1090 				ifrp++;
1091 			} else {
1092 				space -= sa->sa_len - sizeof(*sa);
1093 				if (space < sizeof (ifr))
1094 					break;
1095 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1096 						sizeof (ifr.ifr_name));
1097 				if (error == 0)
1098 				    error = copyout((caddr_t)sa,
1099 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1100 				ifrp = (struct ifreq *)
1101 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1102 			}
1103 			if (error)
1104 				break;
1105 			space -= sizeof (ifr);
1106 		}
1107 	}
1108 	ifc->ifc_len -= space;
1109 	return (error);
1110 }
1111