xref: /netbsd-src/sys/net/route.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: route.c,v 1.47 2001/07/26 05:47:37 itojun Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Kevin M. Lahey of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the project nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  */
68 
69 /*
70  * Copyright (c) 1980, 1986, 1991, 1993
71  *	The Regents of the University of California.  All rights reserved.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  * 1. Redistributions of source code must retain the above copyright
77  *    notice, this list of conditions and the following disclaimer.
78  * 2. Redistributions in binary form must reproduce the above copyright
79  *    notice, this list of conditions and the following disclaimer in the
80  *    documentation and/or other materials provided with the distribution.
81  * 3. All advertising materials mentioning features or use of this software
82  *    must display the following acknowledgement:
83  *	This product includes software developed by the University of
84  *	California, Berkeley and its contributors.
85  * 4. Neither the name of the University nor the names of its contributors
86  *    may be used to endorse or promote products derived from this software
87  *    without specific prior written permission.
88  *
89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  *
101  *	@(#)route.c	8.3 (Berkeley) 1/9/95
102  */
103 
104 #include "opt_ns.h"
105 
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/callout.h>
109 #include <sys/proc.h>
110 #include <sys/mbuf.h>
111 #include <sys/socket.h>
112 #include <sys/socketvar.h>
113 #include <sys/domain.h>
114 #include <sys/protosw.h>
115 #include <sys/kernel.h>
116 #include <sys/ioctl.h>
117 #include <sys/pool.h>
118 
119 #include <net/if.h>
120 #include <net/route.h>
121 #include <net/raw_cb.h>
122 
123 #include <netinet/in.h>
124 #include <netinet/in_var.h>
125 
126 #ifdef NS
127 #include <netns/ns.h>
128 #endif
129 
130 #define	SA(p) ((struct sockaddr *)(p))
131 
132 int	rttrash;		/* routes not in table but not freed */
133 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
134 
135 struct pool rtentry_pool;	/* pool for rtentry structures */
136 struct pool rttimer_pool;	/* pool for rttimer structures */
137 
138 struct callout rt_timer_ch; /* callout for rt_timer_timer() */
139 
140 static int rtdeletemsg __P((struct rtentry *));
141 static int rtflushclone1 __P((struct radix_node *, void *));
142 static void rtflushclone __P((struct radix_node_head *, struct rtentry *));
143 
144 void
145 rtable_init(table)
146 	void **table;
147 {
148 	struct domain *dom;
149 	for (dom = domains; dom; dom = dom->dom_next)
150 		if (dom->dom_rtattach)
151 			dom->dom_rtattach(&table[dom->dom_family],
152 			    dom->dom_rtoffset);
153 }
154 
155 void
156 route_init()
157 {
158 
159 	pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
160 	    0, NULL, NULL, M_RTABLE);
161 
162 	rn_init();	/* initialize all zeroes, all ones, mask table */
163 	rtable_init((void **)rt_tables);
164 }
165 
166 /*
167  * Packet routing routines.
168  */
169 void
170 rtalloc(ro)
171 	struct route *ro;
172 {
173 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
174 		return;				 /* XXX */
175 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
176 }
177 
178 struct rtentry *
179 rtalloc1(dst, report)
180 	struct sockaddr *dst;
181 	int report;
182 {
183 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
184 	struct rtentry *rt;
185 	struct radix_node *rn;
186 	struct rtentry *newrt = 0;
187 	struct rt_addrinfo info;
188 	int  s = splsoftnet(), err = 0, msgtype = RTM_MISS;
189 
190 	if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
191 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
192 		newrt = rt = (struct rtentry *)rn;
193 		if (report && (rt->rt_flags & RTF_CLONING)) {
194 			err = rtrequest(RTM_RESOLVE, dst, SA(0),
195 					      SA(0), 0, &newrt);
196 			if (err) {
197 				newrt = rt;
198 				rt->rt_refcnt++;
199 				goto miss;
200 			}
201 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
202 				msgtype = RTM_RESOLVE;
203 				goto miss;
204 			}
205 			/* Inform listeners of the new route */
206 			memset(&info, 0, sizeof(info));
207 			info.rti_info[RTAX_DST] = rt_key(rt);
208 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
209 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
210 			if (rt->rt_ifp != NULL) {
211 				info.rti_info[RTAX_IFP] =
212 				    rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
213 				info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
214 			}
215 			rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
216 		} else
217 			rt->rt_refcnt++;
218 	} else {
219 		rtstat.rts_unreach++;
220 	miss:	if (report) {
221 			memset((caddr_t)&info, 0, sizeof(info));
222 			info.rti_info[RTAX_DST] = dst;
223 			rt_missmsg(msgtype, &info, 0, err);
224 		}
225 	}
226 	splx(s);
227 	return (newrt);
228 }
229 
230 void
231 rtfree(rt)
232 	struct rtentry *rt;
233 {
234 	struct ifaddr *ifa;
235 
236 	if (rt == 0)
237 		panic("rtfree");
238 	rt->rt_refcnt--;
239 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
240 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
241 			panic ("rtfree 2");
242 		rttrash--;
243 		if (rt->rt_refcnt < 0) {
244 			printf("rtfree: %p not freed (neg refs)\n", rt);
245 			return;
246 		}
247 		rt_timer_remove_all(rt);
248 		ifa = rt->rt_ifa;
249 		IFAFREE(ifa);
250 		Free(rt_key(rt));
251 		pool_put(&rtentry_pool, rt);
252 	}
253 }
254 
255 void
256 ifafree(ifa)
257 	struct ifaddr *ifa;
258 {
259 
260 #ifdef DIAGNOSTIC
261 	if (ifa == NULL)
262 		panic("ifafree: null ifa");
263 	if (ifa->ifa_refcnt != 0)
264 		panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
265 #endif
266 #ifdef IFAREF_DEBUG
267 	printf("ifafree: freeing ifaddr %p\n", ifa);
268 #endif
269 	free(ifa, M_IFADDR);
270 }
271 
272 /*
273  * Force a routing table entry to the specified
274  * destination to go through the given gateway.
275  * Normally called as a result of a routing redirect
276  * message from the network layer.
277  *
278  * N.B.: must be called at splsoftnet
279  */
280 void
281 rtredirect(dst, gateway, netmask, flags, src, rtp)
282 	struct sockaddr *dst, *gateway, *netmask, *src;
283 	int flags;
284 	struct rtentry **rtp;
285 {
286 	struct rtentry *rt;
287 	int error = 0;
288 	u_quad_t *stat = 0;
289 	struct rt_addrinfo info;
290 	struct ifaddr *ifa;
291 
292 	/* verify the gateway is directly reachable */
293 	if ((ifa = ifa_ifwithnet(gateway)) == 0) {
294 		error = ENETUNREACH;
295 		goto out;
296 	}
297 	rt = rtalloc1(dst, 0);
298 	/*
299 	 * If the redirect isn't from our current router for this dst,
300 	 * it's either old or wrong.  If it redirects us to ourselves,
301 	 * we have a routing loop, perhaps as a result of an interface
302 	 * going down recently.
303 	 */
304 #define	equal(a1, a2) \
305 	((a1)->sa_len == (a2)->sa_len && \
306 	 bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
307 	if (!(flags & RTF_DONE) && rt &&
308 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
309 		error = EINVAL;
310 	else if (ifa_ifwithaddr(gateway))
311 		error = EHOSTUNREACH;
312 	if (error)
313 		goto done;
314 	/*
315 	 * Create a new entry if we just got back a wildcard entry
316 	 * or the lookup failed.  This is necessary for hosts
317 	 * which use routing redirects generated by smart gateways
318 	 * to dynamically build the routing tables.
319 	 */
320 	if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
321 		goto create;
322 	/*
323 	 * Don't listen to the redirect if it's
324 	 * for a route to an interface.
325 	 */
326 	if (rt->rt_flags & RTF_GATEWAY) {
327 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
328 			/*
329 			 * Changing from route to net => route to host.
330 			 * Create new route, rather than smashing route to net.
331 			 */
332 		create:
333 			if (rt)
334 				rtfree(rt);
335 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
336 			info.rti_info[RTAX_DST] = dst;
337 			info.rti_info[RTAX_GATEWAY] = gateway;
338 			info.rti_info[RTAX_NETMASK] = netmask;
339 			info.rti_ifa = ifa;
340 			info.rti_flags = flags;
341 			rt = NULL;
342 			error = rtrequest1(RTM_ADD, &info, &rt);
343 			if (rt != NULL)
344 				flags = rt->rt_flags;
345 			stat = &rtstat.rts_dynamic;
346 		} else {
347 			/*
348 			 * Smash the current notion of the gateway to
349 			 * this destination.  Should check about netmask!!!
350 			 */
351 			rt->rt_flags |= RTF_MODIFIED;
352 			flags |= RTF_MODIFIED;
353 			stat = &rtstat.rts_newgateway;
354 			rt_setgate(rt, rt_key(rt), gateway);
355 		}
356 	} else
357 		error = EHOSTUNREACH;
358 done:
359 	if (rt) {
360 		if (rtp && !error)
361 			*rtp = rt;
362 		else
363 			rtfree(rt);
364 	}
365 out:
366 	if (error)
367 		rtstat.rts_badredirect++;
368 	else if (stat != NULL)
369 		(*stat)++;
370 	memset((caddr_t)&info, 0, sizeof(info));
371 	info.rti_info[RTAX_DST] = dst;
372 	info.rti_info[RTAX_GATEWAY] = gateway;
373 	info.rti_info[RTAX_NETMASK] = netmask;
374 	info.rti_info[RTAX_AUTHOR] = src;
375 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
376 }
377 
378 /*
379  * Delete a route and generate a message
380  */
381 static int
382 rtdeletemsg(rt)
383 	struct rtentry *rt;
384 {
385 	int error;
386 	struct rt_addrinfo info;
387 
388 	/*
389 	 * Request the new route so that the entry is not actually
390 	 * deleted.  That will allow the information being reported to
391 	 * be accurate (and consistent with route_output()).
392 	 */
393 	memset((caddr_t)&info, 0, sizeof(info));
394 	info.rti_info[RTAX_DST] = rt_key(rt);
395 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
396 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
397 	info.rti_flags = rt->rt_flags;
398 	error = rtrequest1(RTM_DELETE, &info, &rt);
399 
400 	rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
401 
402 	/* Adjust the refcount */
403 	if (error == 0 && rt->rt_refcnt <= 0) {
404 		rt->rt_refcnt++;
405 		rtfree(rt);
406 	}
407 	return (error);
408 }
409 
410 static int
411 rtflushclone1(rn, arg)
412 	struct radix_node *rn;
413 	void *arg;
414 {
415 	struct rtentry *rt, *parent;
416 
417 	rt = (struct rtentry *)rn;
418 	parent = (struct rtentry *)arg;
419 	if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
420 		rtdeletemsg(rt);
421 	return 0;
422 }
423 
424 static void
425 rtflushclone(rnh, parent)
426 	struct radix_node_head *rnh;
427 	struct rtentry *parent;
428 {
429 
430 #ifdef DIAGNOSTIC
431 	if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
432 		panic("rtflushclone: called with a non-cloning route");
433 	if (!rnh->rnh_walktree)
434 		panic("rtflushclone: no rnh_walktree");
435 #endif
436 	rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
437 }
438 
439 /*
440  * Routing table ioctl interface.
441  */
442 int
443 rtioctl(req, data, p)
444 	u_long req;
445 	caddr_t data;
446 	struct proc *p;
447 {
448 	return (EOPNOTSUPP);
449 }
450 
451 struct ifaddr *
452 ifa_ifwithroute(flags, dst, gateway)
453 	int flags;
454 	struct sockaddr	*dst, *gateway;
455 {
456 	struct ifaddr *ifa;
457 	if ((flags & RTF_GATEWAY) == 0) {
458 		/*
459 		 * If we are adding a route to an interface,
460 		 * and the interface is a pt to pt link
461 		 * we should search for the destination
462 		 * as our clue to the interface.  Otherwise
463 		 * we can use the local address.
464 		 */
465 		ifa = 0;
466 		if (flags & RTF_HOST)
467 			ifa = ifa_ifwithdstaddr(dst);
468 		if (ifa == 0)
469 			ifa = ifa_ifwithaddr(gateway);
470 	} else {
471 		/*
472 		 * If we are adding a route to a remote net
473 		 * or host, the gateway may still be on the
474 		 * other end of a pt to pt link.
475 		 */
476 		ifa = ifa_ifwithdstaddr(gateway);
477 	}
478 	if (ifa == 0)
479 		ifa = ifa_ifwithnet(gateway);
480 	if (ifa == 0) {
481 		struct rtentry *rt = rtalloc1(dst, 0);
482 		if (rt == 0)
483 			return (0);
484 		rt->rt_refcnt--;
485 		if ((ifa = rt->rt_ifa) == 0)
486 			return (0);
487 	}
488 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
489 		struct ifaddr *oifa = ifa;
490 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
491 		if (ifa == 0)
492 			ifa = oifa;
493 	}
494 	return (ifa);
495 }
496 
497 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
498 
499 int
500 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
501 	int req, flags;
502 	struct sockaddr *dst, *gateway, *netmask;
503 	struct rtentry **ret_nrt;
504 {
505 	struct rt_addrinfo info;
506 
507 	memset(&info, 0, sizeof(info));
508 	info.rti_flags = flags;
509 	info.rti_info[RTAX_DST] = dst;
510 	info.rti_info[RTAX_GATEWAY] = gateway;
511 	info.rti_info[RTAX_NETMASK] = netmask;
512 	return rtrequest1(req, &info, ret_nrt);
513 }
514 
515 /*
516  * These (questionable) definitions of apparent local variables apply
517  * to the next function.  XXXXXX!!!
518  */
519 #define dst	info->rti_info[RTAX_DST]
520 #define gateway	info->rti_info[RTAX_GATEWAY]
521 #define netmask	info->rti_info[RTAX_NETMASK]
522 #define ifaaddr	info->rti_info[RTAX_IFA]
523 #define ifpaddr	info->rti_info[RTAX_IFP]
524 #define flags	info->rti_flags
525 
526 int
527 rt_getifa(info)
528 	struct rt_addrinfo *info;
529 {
530 	struct ifaddr *ifa;
531 	int error = 0;
532 
533 	/*
534 	 * ifp may be specified by sockaddr_dl when protocol address
535 	 * is ambiguous
536 	 */
537 	if (info->rti_ifp == NULL && ifpaddr != NULL
538 	    && ifpaddr->sa_family == AF_LINK &&
539 	    (ifa = ifa_ifwithnet((struct sockaddr *)ifpaddr)) != NULL)
540 		info->rti_ifp = ifa->ifa_ifp;
541 	if (info->rti_ifa == NULL && ifaaddr != NULL)
542 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
543 	if (info->rti_ifa == NULL) {
544 		struct sockaddr *sa;
545 
546 		sa = ifaaddr != NULL ? ifaaddr :
547 		    (gateway != NULL ? gateway : dst);
548 		if (sa != NULL && info->rti_ifp != NULL)
549 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
550 		else if (dst != NULL && gateway != NULL)
551 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
552 		else if (sa != NULL)
553 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
554 	}
555 	if ((ifa = info->rti_ifa) != NULL) {
556 		if (info->rti_ifp == NULL)
557 			info->rti_ifp = ifa->ifa_ifp;
558 	} else
559 		error = ENETUNREACH;
560 	return (error);
561 }
562 
563 int
564 rtrequest1(req, info, ret_nrt)
565 	int req;
566 	struct rt_addrinfo *info;
567 	struct rtentry **ret_nrt;
568 {
569 	int s = splsoftnet(); int error = 0;
570 	struct rtentry *rt, *crt;
571 	struct radix_node *rn;
572 	struct radix_node_head *rnh;
573 	struct ifaddr *ifa;
574 	struct sockaddr *ndst;
575 #define senderr(x) { error = x ; goto bad; }
576 
577 	if ((rnh = rt_tables[dst->sa_family]) == 0)
578 		senderr(ESRCH);
579 	if (flags & RTF_HOST)
580 		netmask = 0;
581 	switch (req) {
582 	case RTM_DELETE:
583 		if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == 0)
584 			senderr(ESRCH);
585 		rt = (struct rtentry *)rn;
586 		if ((rt->rt_flags & RTF_CLONING) != 0) {
587 			/* clean up any cloned children */
588 			rtflushclone(rnh, rt);
589 		}
590 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
591 			senderr(ESRCH);
592 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
593 			panic ("rtrequest delete");
594 		rt = (struct rtentry *)rn;
595 		if (rt->rt_gwroute) {
596 			rt = rt->rt_gwroute; RTFREE(rt);
597 			(rt = (struct rtentry *)rn)->rt_gwroute = 0;
598 		}
599 		rt->rt_flags &= ~RTF_UP;
600 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
601 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
602 		rttrash++;
603 		if (ret_nrt)
604 			*ret_nrt = rt;
605 		else if (rt->rt_refcnt <= 0) {
606 			rt->rt_refcnt++;
607 			rtfree(rt);
608 		}
609 		break;
610 
611 	case RTM_RESOLVE:
612 		if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
613 			senderr(EINVAL);
614 		if ((rt->rt_flags & RTF_CLONING) == 0)
615 			senderr(EINVAL);
616 		ifa = rt->rt_ifa;
617 		flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
618 		flags |= RTF_CLONED;
619 		gateway = rt->rt_gateway;
620 		if ((netmask = rt->rt_genmask) == 0)
621 			flags |= RTF_HOST;
622 		goto makeroute;
623 
624 	case RTM_ADD:
625 		if (info->rti_ifa == 0 && (error = rt_getifa(info)))
626 			senderr(error);
627 		ifa = info->rti_ifa;
628 	makeroute:
629 		rt = pool_get(&rtentry_pool, PR_NOWAIT);
630 		if (rt == 0)
631 			senderr(ENOBUFS);
632 		Bzero(rt, sizeof(*rt));
633 		rt->rt_flags = RTF_UP | flags;
634 		LIST_INIT(&rt->rt_timer);
635 		if (rt_setgate(rt, dst, gateway)) {
636 			pool_put(&rtentry_pool, rt);
637 			senderr(ENOBUFS);
638 		}
639 		ndst = rt_key(rt);
640 		if (netmask) {
641 			rt_maskedcopy(dst, ndst, netmask);
642 		} else
643 			Bcopy(dst, ndst, dst->sa_len);
644 		IFAREF(ifa);
645 		rt->rt_ifa = ifa;
646 		rt->rt_ifp = ifa->ifa_ifp;
647 		if (req == RTM_RESOLVE) {
648 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
649 			rt->rt_parent = *ret_nrt;
650 			rt->rt_parent->rt_refcnt++;
651 		}
652 		rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
653 		    rnh, rt->rt_nodes);
654 		if (rn == NULL && (crt = rtalloc1(ndst, 0)) != NULL) {
655 			/* overwrite cloned route */
656 			if ((crt->rt_flags & RTF_CLONED) != 0) {
657 				rtdeletemsg(crt);
658 				rn = rnh->rnh_addaddr((caddr_t)ndst,
659 				    (caddr_t)netmask, rnh, rt->rt_nodes);
660 			}
661 			RTFREE(crt);
662 		}
663 		if (rn == 0) {
664 			IFAFREE(ifa);
665 			if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
666 				rtfree(rt->rt_parent);
667 			if (rt->rt_gwroute)
668 				rtfree(rt->rt_gwroute);
669 			Free(rt_key(rt));
670 			pool_put(&rtentry_pool, rt);
671 			senderr(EEXIST);
672 		}
673 		if (ifa->ifa_rtrequest)
674 			ifa->ifa_rtrequest(req, rt, info);
675 		if (ret_nrt) {
676 			*ret_nrt = rt;
677 			rt->rt_refcnt++;
678 		}
679 		if ((rt->rt_flags & RTF_CLONING) != 0) {
680 			/* clean up any cloned children */
681 			rtflushclone(rnh, rt);
682 		}
683 		break;
684 	}
685 bad:
686 	splx(s);
687 	return (error);
688 }
689 
690 #undef dst
691 #undef gateway
692 #undef netmask
693 #undef ifaaddr
694 #undef ifpaddr
695 #undef flags
696 
697 int
698 rt_setgate(rt0, dst, gate)
699 	struct rtentry *rt0;
700 	struct sockaddr *dst, *gate;
701 {
702 	caddr_t new, old;
703 	int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
704 	struct rtentry *rt = rt0;
705 
706 	if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
707 		old = (caddr_t)rt_key(rt);
708 		R_Malloc(new, caddr_t, dlen + glen);
709 		if (new == 0)
710 			return 1;
711 		Bzero(new, dlen + glen);
712 		rt->rt_nodes->rn_key = new;
713 	} else {
714 		new = rt->rt_nodes->rn_key;
715 		old = 0;
716 	}
717 	Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
718 	if (old) {
719 		Bcopy(dst, new, dlen);
720 		Free(old);
721 	}
722 	if (rt->rt_gwroute) {
723 		rt = rt->rt_gwroute; RTFREE(rt);
724 		rt = rt0; rt->rt_gwroute = 0;
725 	}
726 	if (rt->rt_flags & RTF_GATEWAY) {
727 		rt->rt_gwroute = rtalloc1(gate, 1);
728 		/*
729 		 * If we switched gateways, grab the MTU from the new
730 		 * gateway route if the current MTU, if the current MTU is
731 		 * greater than the MTU of gateway.
732 		 * Note that, if the MTU of gateway is 0, we will reset the
733 		 * MTU of the route to run PMTUD again from scratch. XXX
734 		 */
735 		if (rt->rt_gwroute
736 		    && !(rt->rt_rmx.rmx_locks & RTV_MTU)
737 		    && rt->rt_rmx.rmx_mtu
738 		    && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
739 			rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
740 		}
741 	}
742 	return 0;
743 }
744 
745 void
746 rt_maskedcopy(src, dst, netmask)
747 	struct sockaddr *src, *dst, *netmask;
748 {
749 	u_char *cp1 = (u_char *)src;
750 	u_char *cp2 = (u_char *)dst;
751 	u_char *cp3 = (u_char *)netmask;
752 	u_char *cplim = cp2 + *cp3;
753 	u_char *cplim2 = cp2 + *cp1;
754 
755 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
756 	cp3 += 2;
757 	if (cplim > cplim2)
758 		cplim = cplim2;
759 	while (cp2 < cplim)
760 		*cp2++ = *cp1++ & *cp3++;
761 	if (cp2 < cplim2)
762 		memset((caddr_t)cp2, 0, (unsigned)(cplim2 - cp2));
763 }
764 
765 /*
766  * Set up or tear down a routing table entry, normally
767  * for an interface.
768  */
769 int
770 rtinit(ifa, cmd, flags)
771 	struct ifaddr *ifa;
772 	int cmd, flags;
773 {
774 	struct rtentry *rt;
775 	struct sockaddr *dst, *odst;
776 	struct sockaddr_storage deldst;
777 	struct rtentry *nrt = 0;
778 	int error;
779 	struct rt_addrinfo info;
780 
781 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
782 	if (cmd == RTM_DELETE) {
783 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
784 			/* Delete subnet route for this interface */
785 			odst = dst;
786 			dst = (struct sockaddr *)&deldst;
787 			rt_maskedcopy(odst, dst, ifa->ifa_netmask);
788 		}
789 		if ((rt = rtalloc1(dst, 0)) != NULL) {
790 			rt->rt_refcnt--;
791 			if (rt->rt_ifa != ifa)
792 				return (flags & RTF_HOST ? EHOSTUNREACH
793 							: ENETUNREACH);
794 		}
795 	}
796 	memset(&info, 0, sizeof(info));
797 	info.rti_ifa = ifa;
798 	info.rti_flags = flags | ifa->ifa_flags;
799 	info.rti_info[RTAX_DST] = dst;
800 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
801 	/*
802 	 * XXX here, it seems that we are assuming that ifa_netmask is NULL
803 	 * for RTF_HOST.  bsdi4 passes NULL explicitly (via intermediate
804 	 * variable) when RTF_HOST is 1.  still not sure if i can safely
805 	 * change it to meet bsdi4 behavior.
806 	 */
807 	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
808 	error = rtrequest1(cmd, &info, &nrt);
809 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
810 		rt_newaddrmsg(cmd, ifa, error, nrt);
811 		if (rt->rt_refcnt <= 0) {
812 			rt->rt_refcnt++;
813 			rtfree(rt);
814 		}
815 	}
816 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
817 		rt->rt_refcnt--;
818 		if (rt->rt_ifa != ifa) {
819 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
820 				rt->rt_ifa);
821 			if (rt->rt_ifa->ifa_rtrequest)
822 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
823 			IFAFREE(rt->rt_ifa);
824 			rt->rt_ifa = ifa;
825 			rt->rt_ifp = ifa->ifa_ifp;
826 			IFAREF(ifa);
827 			if (ifa->ifa_rtrequest)
828 				ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
829 		}
830 		rt_newaddrmsg(cmd, ifa, error, nrt);
831 	}
832 	return (error);
833 }
834 
835 /*
836  * Route timer routines.  These routes allow functions to be called
837  * for various routes at any time.  This is useful in supporting
838  * path MTU discovery and redirect route deletion.
839  *
840  * This is similar to some BSDI internal functions, but it provides
841  * for multiple queues for efficiency's sake...
842  */
843 
844 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
845 static int rt_init_done = 0;
846 
847 #define RTTIMER_CALLOUT(r)	{				\
848 	if (r->rtt_func != NULL) {				\
849 		(*r->rtt_func)(r->rtt_rt, r);			\
850 	} else {						\
851 		rtrequest((int) RTM_DELETE,			\
852 			  (struct sockaddr *)rt_key(r->rtt_rt),	\
853 			  0, 0, 0, 0);				\
854 	}							\
855 }
856 
857 /*
858  * Some subtle order problems with domain initialization mean that
859  * we cannot count on this being run from rt_init before various
860  * protocol initializations are done.  Therefore, we make sure
861  * that this is run when the first queue is added...
862  */
863 
864 void
865 rt_timer_init()
866 {
867 	assert(rt_init_done == 0);
868 
869 	pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
870 	    0, NULL, NULL, M_RTABLE);
871 
872 	LIST_INIT(&rttimer_queue_head);
873 	callout_init(&rt_timer_ch);
874 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
875 	rt_init_done = 1;
876 }
877 
878 struct rttimer_queue *
879 rt_timer_queue_create(timeout)
880 	u_int	timeout;
881 {
882 	struct rttimer_queue *rtq;
883 
884 	if (rt_init_done == 0)
885 		rt_timer_init();
886 
887 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
888 	if (rtq == NULL)
889 		return (NULL);
890 	Bzero(rtq, sizeof *rtq);
891 
892 	rtq->rtq_timeout = timeout;
893 	rtq->rtq_count = 0;
894 	TAILQ_INIT(&rtq->rtq_head);
895 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
896 
897 	return (rtq);
898 }
899 
900 void
901 rt_timer_queue_change(rtq, timeout)
902 	struct rttimer_queue *rtq;
903 	long timeout;
904 {
905 
906 	rtq->rtq_timeout = timeout;
907 }
908 
909 void
910 rt_timer_queue_destroy(rtq, destroy)
911 	struct rttimer_queue *rtq;
912 	int destroy;
913 {
914 	struct rttimer *r;
915 
916 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
917 		LIST_REMOVE(r, rtt_link);
918 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
919 		if (destroy)
920 			RTTIMER_CALLOUT(r);
921 		pool_put(&rttimer_pool, r);
922 		if (rtq->rtq_count > 0)
923 			rtq->rtq_count--;
924 		else
925 			printf("rt_timer_queue_destroy: rtq_count reached 0\n");
926 	}
927 
928 	LIST_REMOVE(rtq, rtq_link);
929 
930 	/*
931 	 * Caller is responsible for freeing the rttimer_queue structure.
932 	 */
933 }
934 
935 unsigned long
936 rt_timer_count(rtq)
937 	struct rttimer_queue *rtq;
938 {
939 
940 	return rtq->rtq_count;
941 }
942 
943 void
944 rt_timer_remove_all(rt)
945 	struct rtentry *rt;
946 {
947 	struct rttimer *r;
948 
949 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
950 		LIST_REMOVE(r, rtt_link);
951 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
952 		if (r->rtt_queue->rtq_count > 0)
953 			r->rtt_queue->rtq_count--;
954 		else
955 			printf("rt_timer_remove_all: rtq_count reached 0\n");
956 		pool_put(&rttimer_pool, r);
957 	}
958 }
959 
960 int
961 rt_timer_add(rt, func, queue)
962 	struct rtentry *rt;
963 	void(*func) __P((struct rtentry *, struct rttimer *));
964 	struct rttimer_queue *queue;
965 {
966 	struct rttimer *r;
967 	long current_time;
968 	int s;
969 
970 	s = splclock();
971 	current_time = mono_time.tv_sec;
972 	splx(s);
973 
974 	/*
975 	 * If there's already a timer with this action, destroy it before
976 	 * we add a new one.
977 	 */
978 	for (r = LIST_FIRST(&rt->rt_timer); r != NULL;
979 	     r = LIST_NEXT(r, rtt_link)) {
980 		if (r->rtt_func == func) {
981 			LIST_REMOVE(r, rtt_link);
982 			TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
983 			if (r->rtt_queue->rtq_count > 0)
984 				r->rtt_queue->rtq_count--;
985 			else
986 				printf("rt_timer_add: rtq_count reached 0\n");
987 			pool_put(&rttimer_pool, r);
988 			break;  /* only one per list, so we can quit... */
989 		}
990 	}
991 
992 	r = pool_get(&rttimer_pool, PR_NOWAIT);
993 	if (r == NULL)
994 		return (ENOBUFS);
995 	Bzero(r, sizeof(*r));
996 
997 	r->rtt_rt = rt;
998 	r->rtt_time = current_time;
999 	r->rtt_func = func;
1000 	r->rtt_queue = queue;
1001 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1002 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1003 	r->rtt_queue->rtq_count++;
1004 
1005 	return (0);
1006 }
1007 
1008 /* ARGSUSED */
1009 void
1010 rt_timer_timer(arg)
1011 	void *arg;
1012 {
1013 	struct rttimer_queue *rtq;
1014 	struct rttimer *r;
1015 	long current_time;
1016 	int s;
1017 
1018 	s = splclock();
1019 	current_time = mono_time.tv_sec;
1020 	splx(s);
1021 
1022 	s = splsoftnet();
1023 	for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
1024 	     rtq = LIST_NEXT(rtq, rtq_link)) {
1025 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1026 		    (r->rtt_time + rtq->rtq_timeout) < current_time) {
1027 			LIST_REMOVE(r, rtt_link);
1028 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1029 			RTTIMER_CALLOUT(r);
1030 			pool_put(&rttimer_pool, r);
1031 			if (rtq->rtq_count > 0)
1032 				rtq->rtq_count--;
1033 			else
1034 				printf("rt_timer_timer: rtq_count reached 0\n");
1035 		}
1036 	}
1037 	splx(s);
1038 
1039 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1040 }
1041