route.c revision 1.91 1 /* $NetBSD: route.c,v 1.91 2007/05/06 02:17:54 dyoung 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. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 *
97 * @(#)route.c 8.3 (Berkeley) 1/9/95
98 */
99
100 #include "opt_route.h"
101
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.91 2007/05/06 02:17:54 dyoung Exp $");
104
105 #include <sys/param.h>
106 #include <sys/sysctl.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 RTFLUSH_DEBUG
127 #define rtcache_debug() __predict_false(_rtcache_debug)
128 #else /* RTFLUSH_DEBUG */
129 #define rtcache_debug() 0
130 #endif /* RTFLUSH_DEBUG */
131
132 struct route_cb route_cb;
133 struct rtstat rtstat;
134 struct radix_node_head *rt_tables[AF_MAX+1];
135
136 int rttrash; /* routes not in table but not freed */
137 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
138
139 POOL_INIT(rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl", NULL,
140 IPL_SOFTNET);
141 POOL_INIT(rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl", NULL,
142 IPL_SOFTNET);
143
144 struct callout rt_timer_ch; /* callout for rt_timer_timer() */
145
146 #ifdef RTFLUSH_DEBUG
147 static int _rtcache_debug = 0;
148 #endif /* RTFLUSH_DEBUG */
149
150 static int rtdeletemsg(struct rtentry *);
151 static int rtflushclone1(struct radix_node *, void *);
152 static void rtflushclone(struct radix_node_head *, struct rtentry *);
153
154 #ifdef RTFLUSH_DEBUG
155 SYSCTL_SETUP(sysctl_net_rtcache_setup, "sysctl net.rtcache.debug setup")
156 {
157 const struct sysctlnode *rnode;
158
159 /* XXX do not duplicate */
160 if (sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT,
161 CTLTYPE_NODE, "net", NULL, NULL, 0, NULL, 0, CTL_NET, CTL_EOL) != 0)
162 return;
163 if (sysctl_createv(clog, 0, &rnode, &rnode, CTLFLAG_PERMANENT,
164 CTLTYPE_NODE,
165 "rtcache", SYSCTL_DESCR("Route cache related settings"),
166 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
167 return;
168 if (sysctl_createv(clog, 0, &rnode, &rnode,
169 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
170 "debug", SYSCTL_DESCR("Debug route caches"),
171 NULL, 0, &_rtcache_debug, 0, CTL_CREATE, CTL_EOL) != 0)
172 return;
173 }
174 #endif /* RTFLUSH_DEBUG */
175
176 struct ifaddr *
177 rt_get_ifa(struct rtentry *rt)
178 {
179 struct ifaddr *ifa;
180
181 if ((ifa = rt->rt_ifa) == NULL)
182 return ifa;
183 else if (ifa->ifa_getifa == NULL)
184 return ifa;
185 #if 0
186 else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
187 return ifa;
188 #endif
189 else {
190 ifa = (*ifa->ifa_getifa)(ifa, rt_key(rt));
191 rt_replace_ifa(rt, ifa);
192 return ifa;
193 }
194 }
195
196 static void
197 rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
198 {
199 rt->rt_ifa = ifa;
200 if (ifa->ifa_seqno != NULL)
201 rt->rt_ifa_seqno = *ifa->ifa_seqno;
202 }
203
204 void
205 rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
206 {
207 IFAREF(ifa);
208 IFAFREE(rt->rt_ifa);
209 rt_set_ifa1(rt, ifa);
210 }
211
212 static void
213 rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
214 {
215 IFAREF(ifa);
216 rt_set_ifa1(rt, ifa);
217 }
218
219 void
220 rtable_init(void **table)
221 {
222 struct domain *dom;
223 DOMAIN_FOREACH(dom)
224 if (dom->dom_rtattach)
225 dom->dom_rtattach(&table[dom->dom_family],
226 dom->dom_rtoffset);
227 }
228
229 void
230 route_init(void)
231 {
232
233 rn_init(); /* initialize all zeroes, all ones, mask table */
234 rtable_init((void **)rt_tables);
235 }
236
237 void
238 rtflushall(int family)
239 {
240 int s;
241 struct domain *dom;
242 struct route *ro;
243
244 if (rtcache_debug())
245 printf("%s: enter\n", __func__);
246
247 if ((dom = pffinddomain(family)) == NULL)
248 return;
249
250 s = splnet();
251 while ((ro = LIST_FIRST(&dom->dom_rtcache)) != NULL) {
252 KASSERT(ro->ro_rt != NULL);
253 rtcache_clear(ro);
254 }
255 splx(s);
256 }
257
258 void
259 rtflush(struct route *ro)
260 {
261 KASSERT(ro->ro_rt != NULL);
262 KASSERT(rtcache_getdst(ro) != NULL);
263
264 RTFREE(ro->ro_rt);
265 ro->ro_rt = NULL;
266
267 LIST_REMOVE(ro, ro_rtcache_next);
268
269 #if 0
270 if (rtcache_debug()) {
271 printf("%s: flushing %s\n", __func__,
272 inet_ntoa((satocsin(rtcache_getdst(ro)))->sin_addr));
273 }
274 #endif
275 }
276
277 void
278 rtcache(struct route *ro)
279 {
280 struct domain *dom;
281
282 KASSERT(ro->ro_rt != NULL);
283 KASSERT(rtcache_getdst(ro) != NULL);
284
285 if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) == NULL)
286 return;
287
288 LIST_INSERT_HEAD(&dom->dom_rtcache, ro, ro_rtcache_next);
289 }
290
291 /*
292 * Packet routing routines.
293 */
294 void
295 rtalloc(struct route *ro)
296 {
297 if (ro->ro_rt != NULL) {
298 if (ro->ro_rt->rt_ifp != NULL &&
299 (ro->ro_rt->rt_flags & RTF_UP) != 0)
300 return;
301 rtflush(ro);
302 }
303 if (rtcache_getdst(ro) == NULL ||
304 (ro->ro_rt = rtalloc1(rtcache_getdst(ro), 1)) == NULL)
305 return;
306 rtcache(ro);
307 }
308
309 struct rtentry *
310 rtalloc1(const struct sockaddr *dst, int report)
311 {
312 struct radix_node_head *rnh = rt_tables[dst->sa_family];
313 struct rtentry *rt;
314 struct radix_node *rn;
315 struct rtentry *newrt = NULL;
316 struct rt_addrinfo info;
317 int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
318
319 if (rnh && (rn = rnh->rnh_matchaddr(dst, rnh)) &&
320 ((rn->rn_flags & RNF_ROOT) == 0)) {
321 newrt = rt = (struct rtentry *)rn;
322 if (report && (rt->rt_flags & RTF_CLONING)) {
323 err = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
324 &newrt);
325 if (err) {
326 newrt = rt;
327 rt->rt_refcnt++;
328 goto miss;
329 }
330 KASSERT(newrt != NULL);
331 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
332 msgtype = RTM_RESOLVE;
333 goto miss;
334 }
335 /* Inform listeners of the new route */
336 memset(&info, 0, sizeof(info));
337 info.rti_info[RTAX_DST] = rt_key(rt);
338 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
339 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
340 if (rt->rt_ifp != NULL) {
341 info.rti_info[RTAX_IFP] =
342 TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
343 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
344 }
345 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
346 } else
347 rt->rt_refcnt++;
348 } else {
349 rtstat.rts_unreach++;
350 miss: if (report) {
351 memset((void *)&info, 0, sizeof(info));
352 info.rti_info[RTAX_DST] = dst;
353 rt_missmsg(msgtype, &info, 0, err);
354 }
355 }
356 splx(s);
357 return (newrt);
358 }
359
360 void
361 rtfree(struct rtentry *rt)
362 {
363 struct ifaddr *ifa;
364
365 if (rt == NULL)
366 panic("rtfree");
367 rt->rt_refcnt--;
368 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
369 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
370 panic ("rtfree 2");
371 rttrash--;
372 if (rt->rt_refcnt < 0) {
373 printf("rtfree: %p not freed (neg refs)\n", rt);
374 return;
375 }
376 rt_timer_remove_all(rt, 0);
377 ifa = rt->rt_ifa;
378 rt->rt_ifa = NULL;
379 IFAFREE(ifa);
380 rt->rt_ifp = NULL;
381 Free(rt_key(rt));
382 pool_put(&rtentry_pool, rt);
383 }
384 }
385
386 void
387 ifafree(struct ifaddr *ifa)
388 {
389
390 #ifdef DIAGNOSTIC
391 if (ifa == NULL)
392 panic("ifafree: null ifa");
393 if (ifa->ifa_refcnt != 0)
394 panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
395 #endif
396 #ifdef IFAREF_DEBUG
397 printf("ifafree: freeing ifaddr %p\n", ifa);
398 #endif
399 free(ifa, M_IFADDR);
400 }
401
402 /*
403 * Force a routing table entry to the specified
404 * destination to go through the given gateway.
405 * Normally called as a result of a routing redirect
406 * message from the network layer.
407 *
408 * N.B.: must be called at splsoftnet
409 */
410 void
411 rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
412 const struct sockaddr *netmask, int flags, const struct sockaddr *src,
413 struct rtentry **rtp)
414 {
415 struct rtentry *rt;
416 int error = 0;
417 u_quad_t *stat = NULL;
418 struct rt_addrinfo info;
419 struct ifaddr *ifa;
420
421 /* verify the gateway is directly reachable */
422 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
423 error = ENETUNREACH;
424 goto out;
425 }
426 rt = rtalloc1(dst, 0);
427 /*
428 * If the redirect isn't from our current router for this dst,
429 * it's either old or wrong. If it redirects us to ourselves,
430 * we have a routing loop, perhaps as a result of an interface
431 * going down recently.
432 */
433 #define equal(a1, a2) \
434 ((a1)->sa_len == (a2)->sa_len && \
435 memcmp((a1), (a2), (a1)->sa_len) == 0)
436 if (!(flags & RTF_DONE) && rt &&
437 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
438 error = EINVAL;
439 else if (ifa_ifwithaddr(gateway))
440 error = EHOSTUNREACH;
441 if (error)
442 goto done;
443 /*
444 * Create a new entry if we just got back a wildcard entry
445 * or the lookup failed. This is necessary for hosts
446 * which use routing redirects generated by smart gateways
447 * to dynamically build the routing tables.
448 */
449 if ((rt == NULL) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
450 goto create;
451 /*
452 * Don't listen to the redirect if it's
453 * for a route to an interface.
454 */
455 if (rt->rt_flags & RTF_GATEWAY) {
456 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
457 /*
458 * Changing from route to net => route to host.
459 * Create new route, rather than smashing route to net.
460 */
461 create:
462 if (rt)
463 rtfree(rt);
464 flags |= RTF_GATEWAY | RTF_DYNAMIC;
465 info.rti_info[RTAX_DST] = dst;
466 info.rti_info[RTAX_GATEWAY] = gateway;
467 info.rti_info[RTAX_NETMASK] = netmask;
468 info.rti_ifa = ifa;
469 info.rti_flags = flags;
470 rt = NULL;
471 error = rtrequest1(RTM_ADD, &info, &rt);
472 if (rt != NULL)
473 flags = rt->rt_flags;
474 stat = &rtstat.rts_dynamic;
475 } else {
476 /*
477 * Smash the current notion of the gateway to
478 * this destination. Should check about netmask!!!
479 */
480 rt->rt_flags |= RTF_MODIFIED;
481 flags |= RTF_MODIFIED;
482 stat = &rtstat.rts_newgateway;
483 rt_setgate(rt, rt_key(rt), gateway);
484 }
485 } else
486 error = EHOSTUNREACH;
487 done:
488 if (rt) {
489 if (rtp && !error)
490 *rtp = rt;
491 else
492 rtfree(rt);
493 }
494 out:
495 if (error)
496 rtstat.rts_badredirect++;
497 else if (stat != NULL)
498 (*stat)++;
499 memset((void *)&info, 0, sizeof(info));
500 info.rti_info[RTAX_DST] = dst;
501 info.rti_info[RTAX_GATEWAY] = gateway;
502 info.rti_info[RTAX_NETMASK] = netmask;
503 info.rti_info[RTAX_AUTHOR] = src;
504 rt_missmsg(RTM_REDIRECT, &info, flags, error);
505 }
506
507 /*
508 * Delete a route and generate a message
509 */
510 static int
511 rtdeletemsg(struct rtentry *rt)
512 {
513 int error;
514 struct rt_addrinfo info;
515
516 /*
517 * Request the new route so that the entry is not actually
518 * deleted. That will allow the information being reported to
519 * be accurate (and consistent with route_output()).
520 */
521 memset((void *)&info, 0, sizeof(info));
522 info.rti_info[RTAX_DST] = rt_key(rt);
523 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
524 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
525 info.rti_flags = rt->rt_flags;
526 error = rtrequest1(RTM_DELETE, &info, &rt);
527
528 rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
529
530 /* Adjust the refcount */
531 if (error == 0 && rt->rt_refcnt <= 0) {
532 rt->rt_refcnt++;
533 rtfree(rt);
534 }
535 return (error);
536 }
537
538 static int
539 rtflushclone1(struct radix_node *rn, void *arg)
540 {
541 struct rtentry *rt, *parent;
542
543 rt = (struct rtentry *)rn;
544 parent = (struct rtentry *)arg;
545 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
546 rtdeletemsg(rt);
547 return 0;
548 }
549
550 static void
551 rtflushclone(struct radix_node_head *rnh, struct rtentry *parent)
552 {
553
554 #ifdef DIAGNOSTIC
555 if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
556 panic("rtflushclone: called with a non-cloning route");
557 if (!rnh->rnh_walktree)
558 panic("rtflushclone: no rnh_walktree");
559 #endif
560 rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
561 }
562
563 /*
564 * Routing table ioctl interface.
565 */
566 int
567 rtioctl(u_long req, void *data, struct lwp *l)
568 {
569 return (EOPNOTSUPP);
570 }
571
572 struct ifaddr *
573 ifa_ifwithroute(int flags, const struct sockaddr *dst,
574 const struct sockaddr *gateway)
575 {
576 struct ifaddr *ifa;
577 if ((flags & RTF_GATEWAY) == 0) {
578 /*
579 * If we are adding a route to an interface,
580 * and the interface is a pt to pt link
581 * we should search for the destination
582 * as our clue to the interface. Otherwise
583 * we can use the local address.
584 */
585 ifa = NULL;
586 if (flags & RTF_HOST)
587 ifa = ifa_ifwithdstaddr(dst);
588 if (ifa == NULL)
589 ifa = ifa_ifwithaddr(gateway);
590 } else {
591 /*
592 * If we are adding a route to a remote net
593 * or host, the gateway may still be on the
594 * other end of a pt to pt link.
595 */
596 ifa = ifa_ifwithdstaddr(gateway);
597 }
598 if (ifa == NULL)
599 ifa = ifa_ifwithnet(gateway);
600 if (ifa == NULL) {
601 struct rtentry *rt = rtalloc1(dst, 0);
602 if (rt == NULL)
603 return NULL;
604 rt->rt_refcnt--;
605 if ((ifa = rt->rt_ifa) == NULL)
606 return NULL;
607 }
608 if (ifa->ifa_addr->sa_family != dst->sa_family) {
609 struct ifaddr *oifa = ifa;
610 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
611 if (ifa == 0)
612 ifa = oifa;
613 }
614 return (ifa);
615 }
616
617 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
618
619 int
620 rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
621 const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
622 {
623 struct rt_addrinfo info;
624
625 memset(&info, 0, sizeof(info));
626 info.rti_flags = flags;
627 info.rti_info[RTAX_DST] = dst;
628 info.rti_info[RTAX_GATEWAY] = gateway;
629 info.rti_info[RTAX_NETMASK] = netmask;
630 return rtrequest1(req, &info, ret_nrt);
631 }
632
633 int
634 rt_getifa(struct rt_addrinfo *info)
635 {
636 struct ifaddr *ifa;
637 const struct sockaddr *dst = info->rti_info[RTAX_DST];
638 const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
639 const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
640 const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
641 int flags = info->rti_flags;
642
643 /*
644 * ifp may be specified by sockaddr_dl when protocol address
645 * is ambiguous
646 */
647 if (info->rti_ifp == NULL && ifpaddr != NULL
648 && ifpaddr->sa_family == AF_LINK &&
649 (ifa = ifa_ifwithnet((const struct sockaddr *)ifpaddr)) != NULL)
650 info->rti_ifp = ifa->ifa_ifp;
651 if (info->rti_ifa == NULL && ifaaddr != NULL)
652 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
653 if (info->rti_ifa == NULL) {
654 const struct sockaddr *sa;
655
656 sa = ifaaddr != NULL ? ifaaddr :
657 (gateway != NULL ? gateway : dst);
658 if (sa != NULL && info->rti_ifp != NULL)
659 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
660 else if (dst != NULL && gateway != NULL)
661 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
662 else if (sa != NULL)
663 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
664 }
665 if ((ifa = info->rti_ifa) == NULL)
666 return ENETUNREACH;
667 if (ifa->ifa_getifa != NULL)
668 info->rti_ifa = ifa = (*ifa->ifa_getifa)(ifa, dst);
669 if (info->rti_ifp == NULL)
670 info->rti_ifp = ifa->ifa_ifp;
671 return 0;
672 }
673
674 int
675 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
676 {
677 int s = splsoftnet();
678 int error = 0;
679 struct rtentry *rt, *crt;
680 struct radix_node *rn;
681 struct radix_node_head *rnh;
682 struct ifaddr *ifa;
683 struct sockaddr_storage deldst;
684 const struct sockaddr *dst = info->rti_info[RTAX_DST];
685 const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
686 const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
687 int flags = info->rti_flags;
688 #define senderr(x) { error = x ; goto bad; }
689
690 if ((rnh = rt_tables[dst->sa_family]) == NULL)
691 senderr(ESRCH);
692 if (flags & RTF_HOST)
693 netmask = NULL;
694 switch (req) {
695 case RTM_DELETE:
696 if (netmask) {
697 rt_maskedcopy(dst, (struct sockaddr *)&deldst, netmask);
698 dst = (struct sockaddr *)&deldst;
699 }
700 if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL)
701 senderr(ESRCH);
702 rt = (struct rtentry *)rn;
703 if ((rt->rt_flags & RTF_CLONING) != 0) {
704 /* clean up any cloned children */
705 rtflushclone(rnh, rt);
706 }
707 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL)
708 senderr(ESRCH);
709 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
710 panic ("rtrequest delete");
711 rt = (struct rtentry *)rn;
712 if (rt->rt_gwroute) {
713 RTFREE(rt->rt_gwroute);
714 rt->rt_gwroute = NULL;
715 }
716 if (rt->rt_parent) {
717 rt->rt_parent->rt_refcnt--;
718 rt->rt_parent = NULL;
719 }
720 rt->rt_flags &= ~RTF_UP;
721 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
722 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
723 rttrash++;
724 if (ret_nrt)
725 *ret_nrt = rt;
726 else if (rt->rt_refcnt <= 0) {
727 rt->rt_refcnt++;
728 rtfree(rt);
729 }
730 break;
731
732 case RTM_RESOLVE:
733 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
734 senderr(EINVAL);
735 if ((rt->rt_flags & RTF_CLONING) == 0)
736 senderr(EINVAL);
737 ifa = rt->rt_ifa;
738 flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
739 flags |= RTF_CLONED;
740 gateway = rt->rt_gateway;
741 if ((netmask = rt->rt_genmask) == NULL)
742 flags |= RTF_HOST;
743 goto makeroute;
744
745 case RTM_ADD:
746 if (info->rti_ifa == NULL && (error = rt_getifa(info)))
747 senderr(error);
748 ifa = info->rti_ifa;
749 makeroute:
750 /* Already at splsoftnet() so pool_get/pool_put are safe */
751 rt = pool_get(&rtentry_pool, PR_NOWAIT);
752 if (rt == NULL)
753 senderr(ENOBUFS);
754 Bzero(rt, sizeof(*rt));
755 rt->rt_flags = RTF_UP | flags;
756 LIST_INIT(&rt->rt_timer);
757 if (rt_setgate(rt, dst, gateway)) {
758 pool_put(&rtentry_pool, rt);
759 senderr(ENOBUFS);
760 }
761 if (netmask) {
762 rt_maskedcopy(dst, rt_key(rt), netmask);
763 } else
764 Bcopy(dst, rt_key(rt), dst->sa_len);
765 rt_set_ifa(rt, ifa);
766 rt->rt_ifp = ifa->ifa_ifp;
767 if (req == RTM_RESOLVE) {
768 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
769 rt->rt_parent = *ret_nrt;
770 rt->rt_parent->rt_refcnt++;
771 }
772 rn = rnh->rnh_addaddr(rt_key(rt), netmask, rnh, rt->rt_nodes);
773 if (rn == NULL && (crt = rtalloc1(rt_key(rt), 0)) != NULL) {
774 /* overwrite cloned route */
775 if ((crt->rt_flags & RTF_CLONED) != 0) {
776 rtdeletemsg(crt);
777 rn = rnh->rnh_addaddr(rt_key(rt),
778 netmask, rnh, rt->rt_nodes);
779 }
780 RTFREE(crt);
781 }
782 if (rn == NULL) {
783 IFAFREE(ifa);
784 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
785 rtfree(rt->rt_parent);
786 if (rt->rt_gwroute)
787 rtfree(rt->rt_gwroute);
788 Free(rt_key(rt));
789 pool_put(&rtentry_pool, rt);
790 senderr(EEXIST);
791 }
792 if (ifa->ifa_rtrequest)
793 ifa->ifa_rtrequest(req, rt, info);
794 if (ret_nrt) {
795 *ret_nrt = rt;
796 rt->rt_refcnt++;
797 }
798 if ((rt->rt_flags & RTF_CLONING) != 0) {
799 /* clean up any cloned children */
800 rtflushclone(rnh, rt);
801 }
802 rtflushall(dst->sa_family);
803 break;
804 }
805 bad:
806 splx(s);
807 return (error);
808 }
809
810 int
811 rt_setgate( struct rtentry *rt0, const struct sockaddr *dst,
812 const struct sockaddr *gate)
813 {
814 char *new, *old;
815 u_int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
816 struct rtentry *rt = rt0;
817
818 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
819 old = (void *)rt_key(rt);
820 R_Malloc(new, void *, dlen + glen);
821 if (new == NULL)
822 return 1;
823 Bzero(new, dlen + glen);
824 rt->rt_nodes->rn_key = new;
825 } else {
826 new = __UNCONST(rt->rt_nodes->rn_key); /*XXXUNCONST*/
827 old = NULL;
828 }
829 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
830 if (old) {
831 Bcopy(dst, new, dlen);
832 Free(old);
833 }
834 if (rt->rt_gwroute) {
835 RTFREE(rt->rt_gwroute);
836 rt->rt_gwroute = NULL;
837 }
838 if (rt->rt_flags & RTF_GATEWAY) {
839 rt->rt_gwroute = rtalloc1(gate, 1);
840 /*
841 * If we switched gateways, grab the MTU from the new
842 * gateway route if the current MTU, if the current MTU is
843 * greater than the MTU of gateway.
844 * Note that, if the MTU of gateway is 0, we will reset the
845 * MTU of the route to run PMTUD again from scratch. XXX
846 */
847 if (rt->rt_gwroute
848 && !(rt->rt_rmx.rmx_locks & RTV_MTU)
849 && rt->rt_rmx.rmx_mtu
850 && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
851 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
852 }
853 }
854 return 0;
855 }
856
857 void
858 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
859 const struct sockaddr *netmask)
860 {
861 const u_char *cp1 = (const u_char *)src;
862 u_char *cp2 = (u_char *)dst;
863 const u_char *cp3 = (const u_char *)netmask;
864 u_char *cplim = cp2 + *cp3;
865 u_char *cplim2 = cp2 + *cp1;
866
867 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
868 cp3 += 2;
869 if (cplim > cplim2)
870 cplim = cplim2;
871 while (cp2 < cplim)
872 *cp2++ = *cp1++ & *cp3++;
873 if (cp2 < cplim2)
874 memset(cp2, 0, (unsigned)(cplim2 - cp2));
875 }
876
877 /*
878 * Set up or tear down a routing table entry, normally
879 * for an interface.
880 */
881 int
882 rtinit(struct ifaddr *ifa, int cmd, int flags)
883 {
884 struct rtentry *rt;
885 struct sockaddr *dst, *odst;
886 struct sockaddr_storage deldst;
887 struct rtentry *nrt = NULL;
888 int error;
889 struct rt_addrinfo info;
890
891 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
892 if (cmd == RTM_DELETE) {
893 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
894 /* Delete subnet route for this interface */
895 odst = dst;
896 dst = (struct sockaddr *)&deldst;
897 rt_maskedcopy(odst, dst, ifa->ifa_netmask);
898 }
899 if ((rt = rtalloc1(dst, 0)) != NULL) {
900 rt->rt_refcnt--;
901 if (rt->rt_ifa != ifa)
902 return (flags & RTF_HOST) ? EHOSTUNREACH
903 : ENETUNREACH;
904 }
905 }
906 memset(&info, 0, sizeof(info));
907 info.rti_ifa = ifa;
908 info.rti_flags = flags | ifa->ifa_flags;
909 info.rti_info[RTAX_DST] = dst;
910 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
911 /*
912 * XXX here, it seems that we are assuming that ifa_netmask is NULL
913 * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate
914 * variable) when RTF_HOST is 1. still not sure if i can safely
915 * change it to meet bsdi4 behavior.
916 */
917 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
918 error = rtrequest1(cmd, &info, &nrt);
919 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
920 rt_newaddrmsg(cmd, ifa, error, nrt);
921 if (rt->rt_refcnt <= 0) {
922 rt->rt_refcnt++;
923 rtfree(rt);
924 }
925 }
926 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
927 rt->rt_refcnt--;
928 if (rt->rt_ifa != ifa) {
929 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
930 rt->rt_ifa);
931 if (rt->rt_ifa->ifa_rtrequest)
932 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
933 rt_replace_ifa(rt, ifa);
934 rt->rt_ifp = ifa->ifa_ifp;
935 if (ifa->ifa_rtrequest)
936 ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
937 }
938 rt_newaddrmsg(cmd, ifa, error, nrt);
939 }
940 return error;
941 }
942
943 /*
944 * Route timer routines. These routes allow functions to be called
945 * for various routes at any time. This is useful in supporting
946 * path MTU discovery and redirect route deletion.
947 *
948 * This is similar to some BSDI internal functions, but it provides
949 * for multiple queues for efficiency's sake...
950 */
951
952 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
953 static int rt_init_done = 0;
954
955 #define RTTIMER_CALLOUT(r) do { \
956 if (r->rtt_func != NULL) { \
957 (*r->rtt_func)(r->rtt_rt, r); \
958 } else { \
959 rtrequest((int) RTM_DELETE, \
960 (struct sockaddr *)rt_key(r->rtt_rt), \
961 0, 0, 0, 0); \
962 } \
963 } while (/*CONSTCOND*/0)
964
965 /*
966 * Some subtle order problems with domain initialization mean that
967 * we cannot count on this being run from rt_init before various
968 * protocol initializations are done. Therefore, we make sure
969 * that this is run when the first queue is added...
970 */
971
972 void
973 rt_timer_init(void)
974 {
975 assert(rt_init_done == 0);
976
977 LIST_INIT(&rttimer_queue_head);
978 callout_init(&rt_timer_ch);
979 callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
980 rt_init_done = 1;
981 }
982
983 struct rttimer_queue *
984 rt_timer_queue_create(u_int timeout)
985 {
986 struct rttimer_queue *rtq;
987
988 if (rt_init_done == 0)
989 rt_timer_init();
990
991 R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
992 if (rtq == NULL)
993 return NULL;
994 Bzero(rtq, sizeof *rtq);
995
996 rtq->rtq_timeout = timeout;
997 rtq->rtq_count = 0;
998 TAILQ_INIT(&rtq->rtq_head);
999 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
1000
1001 return rtq;
1002 }
1003
1004 void
1005 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
1006 {
1007
1008 rtq->rtq_timeout = timeout;
1009 }
1010
1011 void
1012 rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
1013 {
1014 struct rttimer *r;
1015
1016 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
1017 LIST_REMOVE(r, rtt_link);
1018 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1019 if (destroy)
1020 RTTIMER_CALLOUT(r);
1021 /* we are already at splsoftnet */
1022 pool_put(&rttimer_pool, r);
1023 if (rtq->rtq_count > 0)
1024 rtq->rtq_count--;
1025 else
1026 printf("rt_timer_queue_remove_all: "
1027 "rtq_count reached 0\n");
1028 }
1029 }
1030
1031 void
1032 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
1033 {
1034
1035 rt_timer_queue_remove_all(rtq, destroy);
1036
1037 LIST_REMOVE(rtq, rtq_link);
1038
1039 /*
1040 * Caller is responsible for freeing the rttimer_queue structure.
1041 */
1042 }
1043
1044 unsigned long
1045 rt_timer_count(struct rttimer_queue *rtq)
1046 {
1047 return rtq->rtq_count;
1048 }
1049
1050 void
1051 rt_timer_remove_all(struct rtentry *rt, int destroy)
1052 {
1053 struct rttimer *r;
1054
1055 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
1056 LIST_REMOVE(r, rtt_link);
1057 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1058 if (destroy)
1059 RTTIMER_CALLOUT(r);
1060 if (r->rtt_queue->rtq_count > 0)
1061 r->rtt_queue->rtq_count--;
1062 else
1063 printf("rt_timer_remove_all: rtq_count reached 0\n");
1064 /* we are already at splsoftnet */
1065 pool_put(&rttimer_pool, r);
1066 }
1067 }
1068
1069 int
1070 rt_timer_add(struct rtentry *rt,
1071 void (*func)(struct rtentry *, struct rttimer *),
1072 struct rttimer_queue *queue)
1073 {
1074 struct rttimer *r;
1075 int s;
1076
1077 /*
1078 * If there's already a timer with this action, destroy it before
1079 * we add a new one.
1080 */
1081 LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
1082 if (r->rtt_func == func)
1083 break;
1084 }
1085 if (r != NULL) {
1086 LIST_REMOVE(r, rtt_link);
1087 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1088 if (r->rtt_queue->rtq_count > 0)
1089 r->rtt_queue->rtq_count--;
1090 else
1091 printf("rt_timer_add: rtq_count reached 0\n");
1092 } else {
1093 s = splsoftnet();
1094 r = pool_get(&rttimer_pool, PR_NOWAIT);
1095 splx(s);
1096 if (r == NULL)
1097 return ENOBUFS;
1098 }
1099
1100 memset(r, 0, sizeof(*r));
1101
1102 r->rtt_rt = rt;
1103 r->rtt_time = time_uptime;
1104 r->rtt_func = func;
1105 r->rtt_queue = queue;
1106 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1107 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1108 r->rtt_queue->rtq_count++;
1109
1110 return (0);
1111 }
1112
1113 /* ARGSUSED */
1114 void
1115 rt_timer_timer(void *arg)
1116 {
1117 struct rttimer_queue *rtq;
1118 struct rttimer *r;
1119 int s;
1120
1121 s = splsoftnet();
1122 LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
1123 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1124 (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
1125 LIST_REMOVE(r, rtt_link);
1126 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1127 RTTIMER_CALLOUT(r);
1128 pool_put(&rttimer_pool, r);
1129 if (rtq->rtq_count > 0)
1130 rtq->rtq_count--;
1131 else
1132 printf("rt_timer_timer: rtq_count reached 0\n");
1133 }
1134 }
1135 splx(s);
1136
1137 callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1138 }
1139
1140 #ifdef RTCACHE_DEBUG
1141 #ifndef RTCACHE_DEBUG_SIZE
1142 #define RTCACHE_DEBUG_SIZE (1024 * 1024)
1143 #endif
1144 static const char *cache_caller[RTCACHE_DEBUG_SIZE];
1145 static struct route *cache_entry[RTCACHE_DEBUG_SIZE];
1146 size_t cache_cur;
1147 #endif
1148
1149 #ifdef RTCACHE_DEBUG
1150 static void
1151 _rtcache_init_debug(const char *caller, struct route *ro, int flag)
1152 #else
1153 static void
1154 _rtcache_init(struct route *ro, int flag)
1155 #endif
1156 {
1157 #ifdef RTCACHE_DEBUG
1158 size_t i;
1159 for (i = 0; i < cache_cur; ++i) {
1160 if (cache_entry[i] == ro)
1161 panic("Reinit of route %p, initialised from %s", ro, cache_caller[i]);
1162 }
1163 #endif
1164
1165 if (rtcache_getdst(ro) == NULL)
1166 return;
1167 ro->ro_rt = rtalloc1(rtcache_getdst(ro), flag);
1168 if (ro->ro_rt != NULL) {
1169 #ifdef RTCACHE_DEBUG
1170 if (cache_cur == RTCACHE_DEBUG_SIZE)
1171 panic("Route cache debug overflow");
1172 cache_caller[cache_cur] = caller;
1173 cache_entry[cache_cur] = ro;
1174 ++cache_cur;
1175 #endif
1176 rtcache(ro);
1177 }
1178 }
1179
1180 #ifdef RTCACHE_DEBUG
1181 void
1182 rtcache_init_debug(const char *caller, struct route *ro)
1183 {
1184 _rtcache_init_debug(caller, ro, 1);
1185 }
1186
1187 void
1188 rtcache_init_noclone_debug(const char *caller, struct route *ro)
1189 {
1190 _rtcache_init_debug(caller, ro, 0);
1191 }
1192
1193 void
1194 rtcache_update(struct route *ro, int clone)
1195 {
1196 rtcache_clear(ro);
1197 _rtcache_init_debug(__func__, ro, clone);
1198 }
1199 #else
1200 void
1201 rtcache_init(struct route *ro)
1202 {
1203 _rtcache_init(ro, 1);
1204 }
1205
1206 void
1207 rtcache_init_noclone(struct route *ro)
1208 {
1209 _rtcache_init(ro, 0);
1210 }
1211
1212 void
1213 rtcache_update(struct route *ro, int clone)
1214 {
1215 rtcache_clear(ro);
1216 _rtcache_init(ro, clone);
1217 }
1218 #endif
1219
1220 #ifdef RTCACHE_DEBUG
1221 void
1222 rtcache_copy_debug(const char *caller, struct route *new_ro, const struct route *old_ro)
1223 #else
1224 void
1225 rtcache_copy(struct route *new_ro, const struct route *old_ro)
1226 #endif
1227 {
1228 /* XXX i doubt this DTRT any longer --dyoung */
1229 #ifdef RTCACHE_DEBUG
1230 size_t i;
1231
1232 for (i = 0; i < cache_cur; ++i) {
1233 if (cache_entry[i] == new_ro)
1234 panic("Copy to initalised route %p (before %s)", new_ro, cache_caller[i]);
1235 }
1236 #endif
1237
1238 if (rtcache_getdst(old_ro) == NULL ||
1239 rtcache_setdst(new_ro, rtcache_getdst(old_ro)) != 0)
1240 return;
1241 new_ro->ro_rt = old_ro->ro_rt;
1242 if (new_ro->ro_rt != NULL) {
1243 #ifdef RTCACHE_DEBUG
1244 if (cache_cur == RTCACHE_DEBUG_SIZE)
1245 panic("Route cache debug overflow");
1246 cache_caller[cache_cur] = caller;
1247 cache_entry[cache_cur] = new_ro;
1248 ++cache_cur;
1249 #endif
1250 rtcache(new_ro);
1251 ++new_ro->ro_rt->rt_refcnt;
1252 }
1253 }
1254
1255 void
1256 rtcache_clear(struct route *ro)
1257 {
1258 #ifdef RTCACHE_DEBUG
1259 size_t j, i = cache_cur;
1260 for (i = j = 0; i < cache_cur; ++i, ++j) {
1261 if (cache_entry[i] == ro) {
1262 if (ro->ro_rt == NULL)
1263 panic("Route cache manipulated (allocated by %s)", cache_caller[i]);
1264 --j;
1265 } else {
1266 cache_caller[j] = cache_caller[i];
1267 cache_entry[j] = cache_entry[i];
1268 }
1269 }
1270 if (ro->ro_rt != NULL) {
1271 if (i != j + 1)
1272 panic("Wrong entries after rtcache_free: %zu (expected %zu)", j, i - 1);
1273 --cache_cur;
1274 }
1275 #endif
1276
1277 if (ro->ro_rt != NULL)
1278 rtflush(ro);
1279 ro->ro_rt = NULL;
1280 }
1281
1282 struct rtentry *
1283 rtcache_lookup2(struct route *ro, const struct sockaddr *dst, int clone,
1284 int *hitp)
1285 {
1286 const struct sockaddr *odst;
1287
1288 odst = rtcache_getdst(ro);
1289
1290 if (odst == NULL)
1291 ;
1292 else if (sockaddr_cmp(odst, dst) != 0)
1293 rtcache_free(ro);
1294 else if (rtcache_down(ro))
1295 rtcache_clear(ro);
1296
1297 if (ro->ro_rt == NULL) {
1298 *hitp = 0;
1299 rtcache_setdst(ro, dst);
1300 _rtcache_init(ro, clone);
1301 } else
1302 *hitp = 1;
1303
1304 return ro->ro_rt;
1305 }
1306
1307 void
1308 rtcache_free(struct route *ro)
1309 {
1310 rtcache_clear(ro);
1311 if (ro->ro_sa != NULL) {
1312 sockaddr_free(ro->ro_sa);
1313 ro->ro_sa = NULL;
1314 }
1315 }
1316
1317 int
1318 rtcache_setdst(struct route *ro, const struct sockaddr *sa)
1319 {
1320 KASSERT(sa != NULL);
1321
1322 if (ro->ro_sa != NULL && ro->ro_sa->sa_family == sa->sa_family) {
1323 rtcache_clear(ro);
1324 sockaddr_copy(ro->ro_sa, sa);
1325 return 0;
1326 } else if (ro->ro_sa != NULL)
1327 rtcache_free(ro); /* free ro_sa, wrong family */
1328
1329 if ((ro->ro_sa = sockaddr_dup(sa, PR_NOWAIT)) == NULL)
1330 return ENOMEM;
1331 return 0;
1332 }
1333