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