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