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