route.c revision 1.145 1 /* $NetBSD: route.c,v 1.145 2015/06/08 08:21:49 roy 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_inet.h"
94 #include "opt_route.h"
95
96 #include <sys/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.145 2015/06/08 08:21:49 roy Exp $");
98
99 #include <sys/param.h>
100 #ifdef RTFLUSH_DEBUG
101 #include <sys/sysctl.h>
102 #endif
103 #include <sys/systm.h>
104 #include <sys/callout.h>
105 #include <sys/proc.h>
106 #include <sys/mbuf.h>
107 #include <sys/socket.h>
108 #include <sys/socketvar.h>
109 #include <sys/domain.h>
110 #include <sys/protosw.h>
111 #include <sys/kernel.h>
112 #include <sys/ioctl.h>
113 #include <sys/pool.h>
114 #include <sys/kauth.h>
115
116 #include <net/if.h>
117 #include <net/if_dl.h>
118 #include <net/route.h>
119
120 #include <netinet/in.h>
121 #include <netinet/in_var.h>
122
123 #ifdef RTFLUSH_DEBUG
124 #define rtcache_debug() __predict_false(_rtcache_debug)
125 #else /* RTFLUSH_DEBUG */
126 #define rtcache_debug() 0
127 #endif /* RTFLUSH_DEBUG */
128
129 struct rtstat rtstat;
130
131 int rttrash; /* routes not in table but not freed */
132
133 struct pool rtentry_pool;
134 struct pool rttimer_pool;
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 kauth_listener_t route_listener;
143
144 static int rtdeletemsg(struct rtentry *);
145 static int rtflushclone1(struct rtentry *, void *);
146 static void rtflushclone(sa_family_t family, struct rtentry *);
147 static void rtflushall(int);
148
149 static void rt_maskedcopy(const struct sockaddr *,
150 struct sockaddr *, const struct sockaddr *);
151
152 static void rtcache_clear(struct route *);
153 static void rtcache_invalidate(struct dom_rtlist *);
154
155 #ifdef RTFLUSH_DEBUG
156 static void sysctl_net_rtcache_setup(struct sysctllog **);
157 static void
158 sysctl_net_rtcache_setup(struct sysctllog **clog)
159 {
160 const struct sysctlnode *rnode;
161
162 if (sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT,
163 CTLTYPE_NODE,
164 "rtcache", SYSCTL_DESCR("Route cache related settings"),
165 NULL, 0, NULL, 0, CTL_NET, 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 static inline void
176 rt_destroy(struct rtentry *rt)
177 {
178 if (rt->_rt_key != NULL)
179 sockaddr_free(rt->_rt_key);
180 if (rt->rt_gateway != NULL)
181 sockaddr_free(rt->rt_gateway);
182 if (rt_gettag(rt) != NULL)
183 sockaddr_free(rt_gettag(rt));
184 rt->_rt_key = rt->rt_gateway = rt->rt_tag = NULL;
185 }
186
187 static inline const struct sockaddr *
188 rt_setkey(struct rtentry *rt, const struct sockaddr *key, int flags)
189 {
190 if (rt->_rt_key == key)
191 goto out;
192
193 if (rt->_rt_key != NULL)
194 sockaddr_free(rt->_rt_key);
195 rt->_rt_key = sockaddr_dup(key, flags);
196 out:
197 rt->rt_nodes->rn_key = (const char *)rt->_rt_key;
198 return rt->_rt_key;
199 }
200
201 struct ifaddr *
202 rt_get_ifa(struct rtentry *rt)
203 {
204 struct ifaddr *ifa;
205
206 if ((ifa = rt->rt_ifa) == NULL)
207 return ifa;
208 else if (ifa->ifa_getifa == NULL)
209 return ifa;
210 #if 0
211 else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
212 return ifa;
213 #endif
214 else {
215 ifa = (*ifa->ifa_getifa)(ifa, rt_getkey(rt));
216 if (ifa == NULL)
217 return NULL;
218 rt_replace_ifa(rt, ifa);
219 return ifa;
220 }
221 }
222
223 static void
224 rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
225 {
226 rt->rt_ifa = ifa;
227 if (ifa->ifa_seqno != NULL)
228 rt->rt_ifa_seqno = *ifa->ifa_seqno;
229 }
230
231 /*
232 * Is this route the connected route for the ifa?
233 */
234 static int
235 rt_ifa_connected(const struct rtentry *rt, const struct ifaddr *ifa)
236 {
237 const struct sockaddr *key, *dst, *odst;
238 struct sockaddr_storage maskeddst;
239
240 key = rt_getkey(rt);
241 dst = rt->rt_flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
242 if (dst == NULL ||
243 dst->sa_family != key->sa_family ||
244 dst->sa_len != key->sa_len)
245 return 0;
246 if ((rt->rt_flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
247 odst = dst;
248 dst = (struct sockaddr *)&maskeddst;
249 rt_maskedcopy(odst, (struct sockaddr *)&maskeddst,
250 ifa->ifa_netmask);
251 }
252 return (memcmp(dst, key, dst->sa_len) == 0);
253 }
254
255 void
256 rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
257 {
258 if (rt->rt_ifa &&
259 rt->rt_ifa != ifa &&
260 rt->rt_ifa->ifa_flags & IFA_ROUTE &&
261 rt_ifa_connected(rt, rt->rt_ifa))
262 {
263 RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
264 "replace deleted IFA_ROUTE\n",
265 (void *)rt->_rt_key, (void *)rt->rt_ifa);
266 rt->rt_ifa->ifa_flags &= ~IFA_ROUTE;
267 if (rt_ifa_connected(rt, ifa)) {
268 RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
269 "replace added IFA_ROUTE\n",
270 (void *)rt->_rt_key, (void *)ifa);
271 ifa->ifa_flags |= IFA_ROUTE;
272 }
273 }
274
275 ifaref(ifa);
276 ifafree(rt->rt_ifa);
277 rt_set_ifa1(rt, ifa);
278 }
279
280 static void
281 rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
282 {
283 ifaref(ifa);
284 rt_set_ifa1(rt, ifa);
285 }
286
287 static int
288 route_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
289 void *arg0, void *arg1, void *arg2, void *arg3)
290 {
291 struct rt_msghdr *rtm;
292 int result;
293
294 result = KAUTH_RESULT_DEFER;
295 rtm = arg1;
296
297 if (action != KAUTH_NETWORK_ROUTE)
298 return result;
299
300 if (rtm->rtm_type == RTM_GET)
301 result = KAUTH_RESULT_ALLOW;
302
303 return result;
304 }
305
306 void
307 rt_init(void)
308 {
309
310 #ifdef RTFLUSH_DEBUG
311 sysctl_net_rtcache_setup(NULL);
312 #endif
313
314 pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
315 NULL, IPL_SOFTNET);
316 pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
317 NULL, IPL_SOFTNET);
318
319 rn_init(); /* initialize all zeroes, all ones, mask table */
320 rtbl_init();
321
322 route_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
323 route_listener_cb, NULL);
324 }
325
326 static void
327 rtflushall(int family)
328 {
329 struct domain *dom;
330
331 if (rtcache_debug())
332 printf("%s: enter\n", __func__);
333
334 if ((dom = pffinddomain(family)) == NULL)
335 return;
336
337 rtcache_invalidate(&dom->dom_rtcache);
338 }
339
340 static void
341 rtcache(struct route *ro)
342 {
343 struct domain *dom;
344
345 rtcache_invariants(ro);
346 KASSERT(ro->_ro_rt != NULL);
347 KASSERT(ro->ro_invalid == false);
348 KASSERT(rtcache_getdst(ro) != NULL);
349
350 if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) == NULL)
351 return;
352
353 LIST_INSERT_HEAD(&dom->dom_rtcache, ro, ro_rtcache_next);
354 rtcache_invariants(ro);
355 }
356
357 /*
358 * Packet routing routines.
359 */
360 struct rtentry *
361 rtalloc1(const struct sockaddr *dst, int report)
362 {
363 rtbl_t *rtbl = rt_gettable(dst->sa_family);
364 struct rtentry *rt;
365 struct rtentry *newrt = NULL;
366 struct rt_addrinfo info;
367 int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
368
369 if (rtbl != NULL && (rt = rt_matchaddr(rtbl, dst)) != NULL) {
370 newrt = rt;
371 if (report && (rt->rt_flags & RTF_CLONING)) {
372 err = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
373 &newrt);
374 if (err) {
375 newrt = rt;
376 rt->rt_refcnt++;
377 goto miss;
378 }
379 KASSERT(newrt != NULL);
380 rt = newrt;
381 if (rt->rt_flags & RTF_XRESOLVE) {
382 msgtype = RTM_RESOLVE;
383 goto miss;
384 }
385 /* Inform listeners of the new route */
386 memset(&info, 0, sizeof(info));
387 info.rti_info[RTAX_DST] = rt_getkey(rt);
388 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
389 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
390 if (rt->rt_ifp != NULL) {
391 info.rti_info[RTAX_IFP] =
392 rt->rt_ifp->if_dl->ifa_addr;
393 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
394 }
395 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
396 } else
397 rt->rt_refcnt++;
398 } else {
399 rtstat.rts_unreach++;
400 miss: if (report) {
401 memset((void *)&info, 0, sizeof(info));
402 info.rti_info[RTAX_DST] = dst;
403 rt_missmsg(msgtype, &info, 0, err);
404 }
405 }
406 splx(s);
407 return newrt;
408 }
409
410 void
411 rtfree(struct rtentry *rt)
412 {
413 struct ifaddr *ifa;
414
415 KASSERT(rt != NULL);
416 KASSERT(rt->rt_refcnt > 0);
417
418 rt->rt_refcnt--;
419 if (rt->rt_refcnt == 0 && (rt->rt_flags & RTF_UP) == 0) {
420 rt_assert_inactive(rt);
421 rttrash--;
422 rt_timer_remove_all(rt, 0);
423 ifa = rt->rt_ifa;
424 rt->rt_ifa = NULL;
425 ifafree(ifa);
426 rt->rt_ifp = NULL;
427 rt_destroy(rt);
428 pool_put(&rtentry_pool, rt);
429 }
430 }
431
432 /*
433 * Force a routing table entry to the specified
434 * destination to go through the given gateway.
435 * Normally called as a result of a routing redirect
436 * message from the network layer.
437 *
438 * N.B.: must be called at splsoftnet
439 */
440 void
441 rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
442 const struct sockaddr *netmask, int flags, const struct sockaddr *src,
443 struct rtentry **rtp)
444 {
445 struct rtentry *rt;
446 int error = 0;
447 uint64_t *stat = NULL;
448 struct rt_addrinfo info;
449 struct ifaddr *ifa;
450
451 /* verify the gateway is directly reachable */
452 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
453 error = ENETUNREACH;
454 goto out;
455 }
456 rt = rtalloc1(dst, 0);
457 /*
458 * If the redirect isn't from our current router for this dst,
459 * it's either old or wrong. If it redirects us to ourselves,
460 * we have a routing loop, perhaps as a result of an interface
461 * going down recently.
462 */
463 if (!(flags & RTF_DONE) && rt &&
464 (sockaddr_cmp(src, rt->rt_gateway) != 0 || rt->rt_ifa != ifa))
465 error = EINVAL;
466 else if (ifa_ifwithaddr(gateway))
467 error = EHOSTUNREACH;
468 if (error)
469 goto done;
470 /*
471 * Create a new entry if we just got back a wildcard entry
472 * or the lookup failed. This is necessary for hosts
473 * which use routing redirects generated by smart gateways
474 * to dynamically build the routing tables.
475 */
476 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
477 goto create;
478 /*
479 * Don't listen to the redirect if it's
480 * for a route to an interface.
481 */
482 if (rt->rt_flags & RTF_GATEWAY) {
483 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
484 /*
485 * Changing from route to net => route to host.
486 * Create new route, rather than smashing route to net.
487 */
488 create:
489 if (rt != NULL)
490 rtfree(rt);
491 flags |= RTF_GATEWAY | RTF_DYNAMIC;
492 memset(&info, 0, sizeof(info));
493 info.rti_info[RTAX_DST] = dst;
494 info.rti_info[RTAX_GATEWAY] = gateway;
495 info.rti_info[RTAX_NETMASK] = netmask;
496 info.rti_ifa = ifa;
497 info.rti_flags = flags;
498 rt = NULL;
499 error = rtrequest1(RTM_ADD, &info, &rt);
500 if (rt != NULL)
501 flags = rt->rt_flags;
502 stat = &rtstat.rts_dynamic;
503 } else {
504 /*
505 * Smash the current notion of the gateway to
506 * this destination. Should check about netmask!!!
507 */
508 rt->rt_flags |= RTF_MODIFIED;
509 flags |= RTF_MODIFIED;
510 stat = &rtstat.rts_newgateway;
511 rt_setgate(rt, gateway);
512 }
513 } else
514 error = EHOSTUNREACH;
515 done:
516 if (rt) {
517 if (rtp != NULL && !error)
518 *rtp = rt;
519 else
520 rtfree(rt);
521 }
522 out:
523 if (error)
524 rtstat.rts_badredirect++;
525 else if (stat != NULL)
526 (*stat)++;
527 memset(&info, 0, sizeof(info));
528 info.rti_info[RTAX_DST] = dst;
529 info.rti_info[RTAX_GATEWAY] = gateway;
530 info.rti_info[RTAX_NETMASK] = netmask;
531 info.rti_info[RTAX_AUTHOR] = src;
532 rt_missmsg(RTM_REDIRECT, &info, flags, error);
533 }
534
535 /*
536 * Delete a route and generate a message
537 */
538 static int
539 rtdeletemsg(struct rtentry *rt)
540 {
541 int error;
542 struct rt_addrinfo info;
543
544 /*
545 * Request the new route so that the entry is not actually
546 * deleted. That will allow the information being reported to
547 * be accurate (and consistent with route_output()).
548 */
549 memset(&info, 0, sizeof(info));
550 info.rti_info[RTAX_DST] = rt_getkey(rt);
551 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
552 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
553 info.rti_flags = rt->rt_flags;
554 error = rtrequest1(RTM_DELETE, &info, &rt);
555
556 rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
557
558 /* Adjust the refcount */
559 if (error == 0 && rt->rt_refcnt <= 0) {
560 rt->rt_refcnt++;
561 rtfree(rt);
562 }
563 return error;
564 }
565
566 static int
567 rtflushclone1(struct rtentry *rt, void *arg)
568 {
569 struct rtentry *parent;
570
571 parent = (struct rtentry *)arg;
572 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
573 rtdeletemsg(rt);
574 return 0;
575 }
576
577 static void
578 rtflushclone(sa_family_t family, struct rtentry *parent)
579 {
580
581 #ifdef DIAGNOSTIC
582 if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
583 panic("rtflushclone: called with a non-cloning route");
584 #endif
585 rt_walktree(family, rtflushclone1, (void *)parent);
586 }
587
588 struct ifaddr *
589 ifa_ifwithroute(int flags, const struct sockaddr *dst,
590 const struct sockaddr *gateway)
591 {
592 struct ifaddr *ifa;
593 if ((flags & RTF_GATEWAY) == 0) {
594 /*
595 * If we are adding a route to an interface,
596 * and the interface is a pt to pt link
597 * we should search for the destination
598 * as our clue to the interface. Otherwise
599 * we can use the local address.
600 */
601 ifa = NULL;
602 if ((flags & RTF_HOST) && gateway->sa_family != AF_LINK)
603 ifa = ifa_ifwithdstaddr(dst);
604 if (ifa == NULL)
605 ifa = ifa_ifwithaddr(gateway);
606 } else {
607 /*
608 * If we are adding a route to a remote net
609 * or host, the gateway may still be on the
610 * other end of a pt to pt link.
611 */
612 ifa = ifa_ifwithdstaddr(gateway);
613 }
614 if (ifa == NULL)
615 ifa = ifa_ifwithnet(gateway);
616 if (ifa == NULL) {
617 struct rtentry *rt = rtalloc1(dst, 0);
618 if (rt == NULL)
619 return NULL;
620 rt->rt_refcnt--;
621 if ((ifa = rt->rt_ifa) == NULL)
622 return NULL;
623 }
624 if (ifa->ifa_addr->sa_family != dst->sa_family) {
625 struct ifaddr *oifa = ifa;
626 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
627 if (ifa == NULL)
628 ifa = oifa;
629 }
630 return ifa;
631 }
632
633 int
634 rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
635 const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
636 {
637 struct rt_addrinfo info;
638
639 memset(&info, 0, sizeof(info));
640 info.rti_flags = flags;
641 info.rti_info[RTAX_DST] = dst;
642 info.rti_info[RTAX_GATEWAY] = gateway;
643 info.rti_info[RTAX_NETMASK] = netmask;
644 return rtrequest1(req, &info, ret_nrt);
645 }
646
647 int
648 rt_getifa(struct rt_addrinfo *info)
649 {
650 struct ifaddr *ifa;
651 const struct sockaddr *dst = info->rti_info[RTAX_DST];
652 const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
653 const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
654 const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
655 int flags = info->rti_flags;
656
657 /*
658 * ifp may be specified by sockaddr_dl when protocol address
659 * is ambiguous
660 */
661 if (info->rti_ifp == NULL && ifpaddr != NULL
662 && ifpaddr->sa_family == AF_LINK &&
663 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
664 info->rti_ifp = ifa->ifa_ifp;
665 if (info->rti_ifa == NULL && ifaaddr != NULL)
666 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
667 if (info->rti_ifa == NULL) {
668 const struct sockaddr *sa;
669
670 sa = ifaaddr != NULL ? ifaaddr :
671 (gateway != NULL ? gateway : dst);
672 if (sa != NULL && info->rti_ifp != NULL)
673 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
674 else if (dst != NULL && gateway != NULL)
675 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
676 else if (sa != NULL)
677 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
678 }
679 if ((ifa = info->rti_ifa) == NULL)
680 return ENETUNREACH;
681 if (ifa->ifa_getifa != NULL) {
682 info->rti_ifa = ifa = (*ifa->ifa_getifa)(ifa, dst);
683 if (ifa == NULL)
684 return ENETUNREACH;
685 }
686 if (info->rti_ifp == NULL)
687 info->rti_ifp = ifa->ifa_ifp;
688 return 0;
689 }
690
691 int
692 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
693 {
694 int s = splsoftnet();
695 int error = 0, rc;
696 struct rtentry *rt, *crt;
697 rtbl_t *rtbl;
698 struct ifaddr *ifa, *ifa2;
699 struct sockaddr_storage maskeddst;
700 const struct sockaddr *dst = info->rti_info[RTAX_DST];
701 const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
702 const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
703 int flags = info->rti_flags;
704 #define senderr(x) { error = x ; goto bad; }
705
706 if ((rtbl = rt_gettable(dst->sa_family)) == NULL)
707 senderr(ESRCH);
708 if (flags & RTF_HOST)
709 netmask = NULL;
710 switch (req) {
711 case RTM_DELETE:
712 if (netmask) {
713 rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
714 netmask);
715 dst = (struct sockaddr *)&maskeddst;
716 }
717 if ((rt = rt_lookup(rtbl, dst, netmask)) == NULL)
718 senderr(ESRCH);
719 if ((rt->rt_flags & RTF_CLONING) != 0) {
720 /* clean up any cloned children */
721 rtflushclone(dst->sa_family, rt);
722 }
723 if ((rt = rt_deladdr(rtbl, dst, netmask)) == NULL)
724 senderr(ESRCH);
725 if (rt->rt_gwroute) {
726 rtfree(rt->rt_gwroute);
727 rt->rt_gwroute = NULL;
728 }
729 if (rt->rt_parent) {
730 rt->rt_parent->rt_refcnt--;
731 rt->rt_parent = NULL;
732 }
733 rt->rt_flags &= ~RTF_UP;
734 if ((ifa = rt->rt_ifa)) {
735 if (ifa->ifa_flags & IFA_ROUTE &&
736 rt_ifa_connected(rt, ifa)) {
737 RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
738 "deleted IFA_ROUTE\n",
739 (void *)rt->_rt_key, (void *)ifa);
740 ifa->ifa_flags &= ~IFA_ROUTE;
741 }
742 if (ifa->ifa_rtrequest)
743 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
744 }
745 rttrash++;
746 if (ret_nrt)
747 *ret_nrt = rt;
748 else if (rt->rt_refcnt <= 0) {
749 rt->rt_refcnt++;
750 rtfree(rt);
751 }
752 break;
753
754 case RTM_RESOLVE:
755 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
756 senderr(EINVAL);
757 if ((rt->rt_flags & RTF_CLONING) == 0)
758 senderr(EINVAL);
759 ifa = rt->rt_ifa;
760 flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
761 flags |= RTF_CLONED;
762 gateway = rt->rt_gateway;
763 flags |= RTF_HOST;
764 goto makeroute;
765
766 case RTM_ADD:
767 if (info->rti_ifa == NULL && (error = rt_getifa(info)))
768 senderr(error);
769 ifa = info->rti_ifa;
770 makeroute:
771 /* Already at splsoftnet() so pool_get/pool_put are safe */
772 rt = pool_get(&rtentry_pool, PR_NOWAIT);
773 if (rt == NULL)
774 senderr(ENOBUFS);
775 memset(rt, 0, sizeof(*rt));
776 rt->rt_flags = RTF_UP | flags;
777 LIST_INIT(&rt->rt_timer);
778 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
779 if (rt_setkey(rt, dst, M_NOWAIT) == NULL ||
780 rt_setgate(rt, gateway) != 0) {
781 pool_put(&rtentry_pool, rt);
782 senderr(ENOBUFS);
783 }
784 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
785 if (netmask) {
786 rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
787 netmask);
788 rt_setkey(rt, (struct sockaddr *)&maskeddst, M_NOWAIT);
789 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
790 } else {
791 rt_setkey(rt, dst, M_NOWAIT);
792 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
793 }
794 rt_set_ifa(rt, ifa);
795 if (info->rti_info[RTAX_TAG] != NULL)
796 rt_settag(rt, info->rti_info[RTAX_TAG]);
797 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
798 if (info->rti_info[RTAX_IFP] != NULL &&
799 (ifa2 = ifa_ifwithnet(info->rti_info[RTAX_IFP])) != NULL &&
800 ifa2->ifa_ifp != NULL)
801 rt->rt_ifp = ifa2->ifa_ifp;
802 else
803 rt->rt_ifp = ifa->ifa_ifp;
804 if (req == RTM_RESOLVE) {
805 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
806 rt->rt_parent = *ret_nrt;
807 rt->rt_parent->rt_refcnt++;
808 }
809 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
810 rc = rt_addaddr(rtbl, rt, netmask);
811 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
812 if (rc != 0 && (crt = rtalloc1(rt_getkey(rt), 0)) != NULL) {
813 /* overwrite cloned route */
814 if ((crt->rt_flags & RTF_CLONED) != 0) {
815 rtdeletemsg(crt);
816 rc = rt_addaddr(rtbl, rt, netmask);
817 }
818 rtfree(crt);
819 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
820 }
821 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
822 if (rc != 0) {
823 ifafree(ifa);
824 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
825 rtfree(rt->rt_parent);
826 if (rt->rt_gwroute)
827 rtfree(rt->rt_gwroute);
828 rt_destroy(rt);
829 pool_put(&rtentry_pool, rt);
830 senderr(rc);
831 }
832 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
833 if (ifa->ifa_rtrequest)
834 ifa->ifa_rtrequest(req, rt, info);
835 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
836 if (ret_nrt) {
837 *ret_nrt = rt;
838 rt->rt_refcnt++;
839 }
840 if ((rt->rt_flags & RTF_CLONING) != 0) {
841 /* clean up any cloned children */
842 rtflushclone(dst->sa_family, rt);
843 }
844 rtflushall(dst->sa_family);
845 break;
846 case RTM_GET:
847 if (netmask != NULL) {
848 rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
849 netmask);
850 dst = (struct sockaddr *)&maskeddst;
851 }
852 if ((rt = rt_lookup(rtbl, dst, netmask)) == NULL)
853 senderr(ESRCH);
854 if (ret_nrt != NULL) {
855 *ret_nrt = rt;
856 rt->rt_refcnt++;
857 }
858 break;
859 }
860 bad:
861 splx(s);
862 return error;
863 }
864
865 int
866 rt_setgate(struct rtentry *rt, const struct sockaddr *gate)
867 {
868 KASSERT(rt != rt->rt_gwroute);
869
870 KASSERT(rt->_rt_key != NULL);
871 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
872
873 if (rt->rt_gwroute) {
874 rtfree(rt->rt_gwroute);
875 rt->rt_gwroute = NULL;
876 }
877 KASSERT(rt->_rt_key != NULL);
878 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
879 if (rt->rt_gateway != NULL)
880 sockaddr_free(rt->rt_gateway);
881 KASSERT(rt->_rt_key != NULL);
882 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
883 if ((rt->rt_gateway = sockaddr_dup(gate, M_ZERO | M_NOWAIT)) == NULL)
884 return ENOMEM;
885 KASSERT(rt->_rt_key != NULL);
886 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
887
888 if (rt->rt_flags & RTF_GATEWAY) {
889 KASSERT(rt->_rt_key != NULL);
890 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
891 rt->rt_gwroute = rtalloc1(gate, 1);
892 /*
893 * If we switched gateways, grab the MTU from the new
894 * gateway route if the current MTU, if the current MTU is
895 * greater than the MTU of gateway.
896 * Note that, if the MTU of gateway is 0, we will reset the
897 * MTU of the route to run PMTUD again from scratch. XXX
898 */
899 KASSERT(rt->_rt_key != NULL);
900 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
901 if (rt->rt_gwroute
902 && !(rt->rt_rmx.rmx_locks & RTV_MTU)
903 && rt->rt_rmx.rmx_mtu
904 && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
905 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
906 }
907 }
908 KASSERT(rt->_rt_key != NULL);
909 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
910 return 0;
911 }
912
913 static void
914 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
915 const struct sockaddr *netmask)
916 {
917 const char *netmaskp = &netmask->sa_data[0],
918 *srcp = &src->sa_data[0];
919 char *dstp = &dst->sa_data[0];
920 const char *maskend = (char *)dst + MIN(netmask->sa_len, src->sa_len);
921 const char *srcend = (char *)dst + src->sa_len;
922
923 dst->sa_len = src->sa_len;
924 dst->sa_family = src->sa_family;
925
926 while (dstp < maskend)
927 *dstp++ = *srcp++ & *netmaskp++;
928 if (dstp < srcend)
929 memset(dstp, 0, (size_t)(srcend - dstp));
930 }
931
932 /*
933 * Inform the routing socket of a route change.
934 */
935 void
936 rt_newmsg(int cmd, struct rtentry *rt)
937 {
938 struct rt_addrinfo info;
939
940 memset((void *)&info, 0, sizeof(info));
941 info.rti_info[RTAX_DST] = rt_getkey(rt);
942 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
943 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
944 if (rt->rt_ifp) {
945 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
946 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
947 }
948
949 rt_missmsg(cmd, &info, rt->rt_flags, 0);
950 }
951
952 /*
953 * Set up or tear down a routing table entry, normally
954 * for an interface.
955 */
956 int
957 rtinit(struct ifaddr *ifa, int cmd, int flags)
958 {
959 struct rtentry *rt;
960 struct sockaddr *dst, *odst;
961 struct sockaddr_storage maskeddst;
962 struct rtentry *nrt = NULL;
963 int error;
964 struct rt_addrinfo info;
965 struct sockaddr_dl *sdl;
966 const struct sockaddr_dl *ifsdl;
967
968 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
969 if (cmd == RTM_DELETE) {
970 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
971 /* Delete subnet route for this interface */
972 odst = dst;
973 dst = (struct sockaddr *)&maskeddst;
974 rt_maskedcopy(odst, dst, ifa->ifa_netmask);
975 }
976 if ((rt = rtalloc1(dst, 0)) != NULL) {
977 rt->rt_refcnt--;
978 if (rt->rt_ifa != ifa)
979 return (flags & RTF_HOST) ? EHOSTUNREACH
980 : ENETUNREACH;
981 }
982 }
983 memset(&info, 0, sizeof(info));
984 info.rti_ifa = ifa;
985 info.rti_flags = flags | ifa->ifa_flags;
986 info.rti_info[RTAX_DST] = dst;
987 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
988 /*
989 * XXX here, it seems that we are assuming that ifa_netmask is NULL
990 * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate
991 * variable) when RTF_HOST is 1. still not sure if i can safely
992 * change it to meet bsdi4 behavior.
993 */
994 if (cmd != RTM_LLINFO_UPD)
995 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
996 error = rtrequest1((cmd == RTM_LLINFO_UPD) ? RTM_GET : cmd, &info,
997 &nrt);
998 if (error != 0 || (rt = nrt) == NULL)
999 ;
1000 else switch (cmd) {
1001 case RTM_DELETE:
1002 rt_newmsg(cmd, nrt);
1003 if (rt->rt_refcnt <= 0) {
1004 rt->rt_refcnt++;
1005 rtfree(rt);
1006 }
1007 break;
1008 case RTM_LLINFO_UPD:
1009 rt->rt_refcnt--;
1010 RT_DPRINTF("%s: updating%s\n", __func__,
1011 ((rt->rt_flags & RTF_LLINFO) == 0) ? " (no llinfo)" : "");
1012
1013 ifsdl = ifa->ifa_ifp->if_sadl;
1014
1015 if ((rt->rt_flags & RTF_LLINFO) != 0 &&
1016 (sdl = satosdl(rt->rt_gateway)) != NULL &&
1017 sdl->sdl_family == AF_LINK &&
1018 sockaddr_dl_setaddr(sdl, sdl->sdl_len, CLLADDR(ifsdl),
1019 ifa->ifa_ifp->if_addrlen) == NULL) {
1020 error = EINVAL;
1021 break;
1022 }
1023
1024 if (cmd == RTM_LLINFO_UPD && ifa->ifa_rtrequest != NULL)
1025 ifa->ifa_rtrequest(RTM_LLINFO_UPD, rt, &info);
1026 rt_newmsg(RTM_CHANGE, nrt);
1027 break;
1028 case RTM_ADD:
1029 rt->rt_refcnt--;
1030 if (rt->rt_ifa != ifa) {
1031 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
1032 rt->rt_ifa);
1033 if (rt->rt_ifa->ifa_rtrequest != NULL) {
1034 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
1035 &info);
1036 }
1037 rt_replace_ifa(rt, ifa);
1038 rt->rt_ifp = ifa->ifa_ifp;
1039 if (ifa->ifa_rtrequest != NULL)
1040 ifa->ifa_rtrequest(RTM_ADD, rt, &info);
1041 }
1042 rt_newmsg(cmd, nrt);
1043 break;
1044 }
1045 return error;
1046 }
1047
1048 static const struct in_addr inmask32 = {.s_addr = INADDR_BROADCAST};
1049
1050 /* Subroutine for rt_ifa_addlocal() and rt_ifa_remlocal() */
1051 static int
1052 rt_ifa_localrequest(int cmd, struct ifaddr *ifa)
1053 {
1054 struct sockaddr *all1_sa;
1055 struct sockaddr_in all1_sin;
1056 #ifdef INET6
1057 struct sockaddr_in6 all1_sin6;
1058 #endif
1059 struct rtentry *nrt = NULL;
1060 int flags, e;
1061
1062 switch(ifa->ifa_addr->sa_family) {
1063 case AF_INET:
1064 sockaddr_in_init(&all1_sin, &inmask32, 0);
1065 all1_sa = (struct sockaddr *)&all1_sin;
1066 break;
1067 #ifdef INET6
1068 case AF_INET6:
1069 sockaddr_in6_init(&all1_sin6, &in6mask128, 0, 0, 0);
1070 all1_sa = (struct sockaddr *)&all1_sin6;
1071 break;
1072 #endif
1073 default:
1074 return 0;
1075 }
1076
1077 flags = RTF_UP | RTF_HOST | RTF_LOCAL;
1078 if (!(ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)))
1079 flags |= RTF_LLINFO;
1080 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr, all1_sa, flags, &nrt);
1081
1082 /* Make sure rt_ifa be equal to IFA, the second argument of the
1083 * function. */
1084 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa)
1085 rt_replace_ifa(nrt, ifa);
1086
1087 rt_newaddrmsg(cmd, ifa, e, nrt);
1088 if (nrt) {
1089 if (cmd == RTM_DELETE) {
1090 if (nrt->rt_refcnt <= 0) {
1091 /* XXX: we should free the entry ourselves. */
1092 nrt->rt_refcnt++;
1093 rtfree(nrt);
1094 }
1095 } else {
1096 /* the cmd must be RTM_ADD here */
1097 nrt->rt_refcnt--;
1098 }
1099 }
1100 return e;
1101 }
1102
1103 /*
1104 * Create a local route entry for the address.
1105 * Announce the addition of the address and the route to the routing socket.
1106 */
1107 int
1108 rt_ifa_addlocal(struct ifaddr *ifa)
1109 {
1110 struct rtentry *rt;
1111 int e;
1112
1113 /* If there is no loopback entry, allocate one. */
1114 rt = rtalloc1(ifa->ifa_addr, 0);
1115 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
1116 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1117 e = rt_ifa_localrequest(RTM_ADD, ifa);
1118 else {
1119 e = 0;
1120 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1121 }
1122 if (rt != NULL)
1123 rt->rt_refcnt--;
1124 return e;
1125 }
1126
1127 /*
1128 * Remove the local route entry for the address.
1129 * Announce the removal of the address and the route to the routing socket.
1130 */
1131 int
1132 rt_ifa_remlocal(struct ifaddr *ifa, struct ifaddr *alt_ifa)
1133 {
1134 struct rtentry *rt;
1135 int e = 0;
1136
1137 rt = rtalloc1(ifa->ifa_addr, 0);
1138
1139 /*
1140 * Before deleting, check if a corresponding loopbacked
1141 * host route surely exists. With this check, we can avoid
1142 * deleting an interface direct route whose destination is
1143 * the same as the address being removed. This can happen
1144 * when removing a subnet-router anycast address on an
1145 * interface attached to a shared medium.
1146 */
1147 if (rt != NULL &&
1148 (rt->rt_flags & RTF_HOST) &&
1149 (rt->rt_ifp->if_flags & IFF_LOOPBACK))
1150 {
1151 /* If we cannot replace the route's ifaddr with the equivalent
1152 * ifaddr of another interface, I believe it is safest to
1153 * delete the route.
1154 */
1155 if (alt_ifa == NULL)
1156 e = rt_ifa_localrequest(RTM_DELETE, ifa);
1157 else {
1158 rt_replace_ifa(rt, alt_ifa);
1159 rt_newmsg(RTM_CHANGE, rt);
1160 }
1161 } else
1162 rt_newaddrmsg(RTM_DELADDR, ifa, 0, NULL);
1163 if (rt != NULL)
1164 rt->rt_refcnt--;
1165 return e;
1166 }
1167
1168 /*
1169 * Route timer routines. These routes allow functions to be called
1170 * for various routes at any time. This is useful in supporting
1171 * path MTU discovery and redirect route deletion.
1172 *
1173 * This is similar to some BSDI internal functions, but it provides
1174 * for multiple queues for efficiency's sake...
1175 */
1176
1177 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
1178 static int rt_init_done = 0;
1179
1180 #define RTTIMER_CALLOUT(r) do { \
1181 if (r->rtt_func != NULL) { \
1182 (*r->rtt_func)(r->rtt_rt, r); \
1183 } else { \
1184 rtrequest((int) RTM_DELETE, \
1185 rt_getkey(r->rtt_rt), \
1186 0, 0, 0, 0); \
1187 } \
1188 } while (/*CONSTCOND*/0)
1189
1190 /*
1191 * Some subtle order problems with domain initialization mean that
1192 * we cannot count on this being run from rt_init before various
1193 * protocol initializations are done. Therefore, we make sure
1194 * that this is run when the first queue is added...
1195 */
1196
1197 void
1198 rt_timer_init(void)
1199 {
1200 assert(rt_init_done == 0);
1201
1202 LIST_INIT(&rttimer_queue_head);
1203 callout_init(&rt_timer_ch, 0);
1204 callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1205 rt_init_done = 1;
1206 }
1207
1208 struct rttimer_queue *
1209 rt_timer_queue_create(u_int timeout)
1210 {
1211 struct rttimer_queue *rtq;
1212
1213 if (rt_init_done == 0)
1214 rt_timer_init();
1215
1216 R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
1217 if (rtq == NULL)
1218 return NULL;
1219 memset(rtq, 0, sizeof(*rtq));
1220
1221 rtq->rtq_timeout = timeout;
1222 TAILQ_INIT(&rtq->rtq_head);
1223 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
1224
1225 return rtq;
1226 }
1227
1228 void
1229 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
1230 {
1231
1232 rtq->rtq_timeout = timeout;
1233 }
1234
1235 void
1236 rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
1237 {
1238 struct rttimer *r;
1239
1240 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
1241 LIST_REMOVE(r, rtt_link);
1242 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1243 if (destroy)
1244 RTTIMER_CALLOUT(r);
1245 /* we are already at splsoftnet */
1246 pool_put(&rttimer_pool, r);
1247 if (rtq->rtq_count > 0)
1248 rtq->rtq_count--;
1249 else
1250 printf("rt_timer_queue_remove_all: "
1251 "rtq_count reached 0\n");
1252 }
1253 }
1254
1255 void
1256 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
1257 {
1258
1259 rt_timer_queue_remove_all(rtq, destroy);
1260
1261 LIST_REMOVE(rtq, rtq_link);
1262
1263 /*
1264 * Caller is responsible for freeing the rttimer_queue structure.
1265 */
1266 }
1267
1268 unsigned long
1269 rt_timer_count(struct rttimer_queue *rtq)
1270 {
1271 return rtq->rtq_count;
1272 }
1273
1274 void
1275 rt_timer_remove_all(struct rtentry *rt, int destroy)
1276 {
1277 struct rttimer *r;
1278
1279 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
1280 LIST_REMOVE(r, rtt_link);
1281 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1282 if (destroy)
1283 RTTIMER_CALLOUT(r);
1284 if (r->rtt_queue->rtq_count > 0)
1285 r->rtt_queue->rtq_count--;
1286 else
1287 printf("rt_timer_remove_all: rtq_count reached 0\n");
1288 /* we are already at splsoftnet */
1289 pool_put(&rttimer_pool, r);
1290 }
1291 }
1292
1293 int
1294 rt_timer_add(struct rtentry *rt,
1295 void (*func)(struct rtentry *, struct rttimer *),
1296 struct rttimer_queue *queue)
1297 {
1298 struct rttimer *r;
1299 int s;
1300
1301 /*
1302 * If there's already a timer with this action, destroy it before
1303 * we add a new one.
1304 */
1305 LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
1306 if (r->rtt_func == func)
1307 break;
1308 }
1309 if (r != NULL) {
1310 LIST_REMOVE(r, rtt_link);
1311 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1312 if (r->rtt_queue->rtq_count > 0)
1313 r->rtt_queue->rtq_count--;
1314 else
1315 printf("rt_timer_add: rtq_count reached 0\n");
1316 } else {
1317 s = splsoftnet();
1318 r = pool_get(&rttimer_pool, PR_NOWAIT);
1319 splx(s);
1320 if (r == NULL)
1321 return ENOBUFS;
1322 }
1323
1324 memset(r, 0, sizeof(*r));
1325
1326 r->rtt_rt = rt;
1327 r->rtt_time = time_uptime;
1328 r->rtt_func = func;
1329 r->rtt_queue = queue;
1330 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1331 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1332 r->rtt_queue->rtq_count++;
1333
1334 return 0;
1335 }
1336
1337 /* ARGSUSED */
1338 void
1339 rt_timer_timer(void *arg)
1340 {
1341 struct rttimer_queue *rtq;
1342 struct rttimer *r;
1343 int s;
1344
1345 s = splsoftnet();
1346 LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
1347 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1348 (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
1349 LIST_REMOVE(r, rtt_link);
1350 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1351 RTTIMER_CALLOUT(r);
1352 pool_put(&rttimer_pool, r);
1353 if (rtq->rtq_count > 0)
1354 rtq->rtq_count--;
1355 else
1356 printf("rt_timer_timer: rtq_count reached 0\n");
1357 }
1358 }
1359 splx(s);
1360
1361 callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1362 }
1363
1364 static struct rtentry *
1365 _rtcache_init(struct route *ro, int flag)
1366 {
1367 rtcache_invariants(ro);
1368 KASSERT(ro->_ro_rt == NULL);
1369
1370 if (rtcache_getdst(ro) == NULL)
1371 return NULL;
1372 ro->ro_invalid = false;
1373 if ((ro->_ro_rt = rtalloc1(rtcache_getdst(ro), flag)) != NULL)
1374 rtcache(ro);
1375
1376 rtcache_invariants(ro);
1377 return ro->_ro_rt;
1378 }
1379
1380 struct rtentry *
1381 rtcache_init(struct route *ro)
1382 {
1383 return _rtcache_init(ro, 1);
1384 }
1385
1386 struct rtentry *
1387 rtcache_init_noclone(struct route *ro)
1388 {
1389 return _rtcache_init(ro, 0);
1390 }
1391
1392 struct rtentry *
1393 rtcache_update(struct route *ro, int clone)
1394 {
1395 rtcache_clear(ro);
1396 return _rtcache_init(ro, clone);
1397 }
1398
1399 void
1400 rtcache_copy(struct route *new_ro, const struct route *old_ro)
1401 {
1402 struct rtentry *rt;
1403
1404 KASSERT(new_ro != old_ro);
1405 rtcache_invariants(new_ro);
1406 rtcache_invariants(old_ro);
1407
1408 if ((rt = rtcache_validate(old_ro)) != NULL)
1409 rt->rt_refcnt++;
1410
1411 if (rtcache_getdst(old_ro) == NULL ||
1412 rtcache_setdst(new_ro, rtcache_getdst(old_ro)) != 0)
1413 return;
1414
1415 new_ro->ro_invalid = false;
1416 if ((new_ro->_ro_rt = rt) != NULL)
1417 rtcache(new_ro);
1418 rtcache_invariants(new_ro);
1419 }
1420
1421 static struct dom_rtlist invalid_routes = LIST_HEAD_INITIALIZER(dom_rtlist);
1422
1423 static void
1424 rtcache_invalidate(struct dom_rtlist *rtlist)
1425 {
1426 struct route *ro;
1427
1428 while ((ro = LIST_FIRST(rtlist)) != NULL) {
1429 rtcache_invariants(ro);
1430 KASSERT(ro->_ro_rt != NULL);
1431 ro->ro_invalid = true;
1432 LIST_REMOVE(ro, ro_rtcache_next);
1433 LIST_INSERT_HEAD(&invalid_routes, ro, ro_rtcache_next);
1434 rtcache_invariants(ro);
1435 }
1436 }
1437
1438 static void
1439 rtcache_clear(struct route *ro)
1440 {
1441 rtcache_invariants(ro);
1442 if (ro->_ro_rt == NULL)
1443 return;
1444
1445 LIST_REMOVE(ro, ro_rtcache_next);
1446
1447 rtfree(ro->_ro_rt);
1448 ro->_ro_rt = NULL;
1449 ro->ro_invalid = false;
1450 rtcache_invariants(ro);
1451 }
1452
1453 struct rtentry *
1454 rtcache_lookup2(struct route *ro, const struct sockaddr *dst, int clone,
1455 int *hitp)
1456 {
1457 const struct sockaddr *odst;
1458 struct rtentry *rt = NULL;
1459
1460 odst = rtcache_getdst(ro);
1461 if (odst == NULL)
1462 goto miss;
1463
1464 if (sockaddr_cmp(odst, dst) != 0) {
1465 rtcache_free(ro);
1466 goto miss;
1467 }
1468
1469 rt = rtcache_validate(ro);
1470 if (rt == NULL) {
1471 rtcache_clear(ro);
1472 goto miss;
1473 }
1474
1475 *hitp = 1;
1476 rtcache_invariants(ro);
1477
1478 return rt;
1479 miss:
1480 *hitp = 0;
1481 if (rtcache_setdst(ro, dst) == 0)
1482 rt = _rtcache_init(ro, clone);
1483
1484 rtcache_invariants(ro);
1485
1486 return rt;
1487 }
1488
1489 void
1490 rtcache_free(struct route *ro)
1491 {
1492 rtcache_clear(ro);
1493 if (ro->ro_sa != NULL) {
1494 sockaddr_free(ro->ro_sa);
1495 ro->ro_sa = NULL;
1496 }
1497 rtcache_invariants(ro);
1498 }
1499
1500 int
1501 rtcache_setdst(struct route *ro, const struct sockaddr *sa)
1502 {
1503 KASSERT(sa != NULL);
1504
1505 rtcache_invariants(ro);
1506 if (ro->ro_sa != NULL) {
1507 if (ro->ro_sa->sa_family == sa->sa_family) {
1508 rtcache_clear(ro);
1509 sockaddr_copy(ro->ro_sa, ro->ro_sa->sa_len, sa);
1510 rtcache_invariants(ro);
1511 return 0;
1512 }
1513 /* free ro_sa, wrong family */
1514 rtcache_free(ro);
1515 }
1516
1517 KASSERT(ro->_ro_rt == NULL);
1518
1519 if ((ro->ro_sa = sockaddr_dup(sa, M_ZERO | M_NOWAIT)) == NULL) {
1520 rtcache_invariants(ro);
1521 return ENOMEM;
1522 }
1523 rtcache_invariants(ro);
1524 return 0;
1525 }
1526
1527 const struct sockaddr *
1528 rt_settag(struct rtentry *rt, const struct sockaddr *tag)
1529 {
1530 if (rt->rt_tag != tag) {
1531 if (rt->rt_tag != NULL)
1532 sockaddr_free(rt->rt_tag);
1533 rt->rt_tag = sockaddr_dup(tag, M_ZERO | M_NOWAIT);
1534 }
1535 return rt->rt_tag;
1536 }
1537
1538 struct sockaddr *
1539 rt_gettag(struct rtentry *rt)
1540 {
1541 return rt->rt_tag;
1542 }
1543