route.c revision 1.60 1 /* $NetBSD: route.c,v 1.60 2004/04/21 21:03:43 matt 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 <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.60 2004/04/21 21:03:43 matt Exp $");
102
103 #include "opt_ns.h"
104
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/callout.h>
108 #include <sys/proc.h>
109 #include <sys/mbuf.h>
110 #include <sys/socket.h>
111 #include <sys/socketvar.h>
112 #include <sys/domain.h>
113 #include <sys/protosw.h>
114 #include <sys/kernel.h>
115 #include <sys/ioctl.h>
116 #include <sys/pool.h>
117
118 #include <net/if.h>
119 #include <net/route.h>
120 #include <net/raw_cb.h>
121
122 #include <netinet/in.h>
123 #include <netinet/in_var.h>
124
125 #ifdef NS
126 #include <netns/ns.h>
127 #endif
128
129 #define SA(p) ((struct sockaddr *)(p))
130
131 struct route_cb route_cb;
132 struct rtstat rtstat;
133 struct radix_node_head *rt_tables[AF_MAX+1];
134
135 int rttrash; /* routes not in table but not freed */
136 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
137
138 struct pool rtentry_pool; /* pool for rtentry structures */
139 struct pool rttimer_pool; /* pool for rttimer structures */
140
141 struct callout rt_timer_ch; /* callout for rt_timer_timer() */
142
143 static int rtdeletemsg(struct rtentry *);
144 static int rtflushclone1(struct radix_node *, void *);
145 static void rtflushclone(struct radix_node_head *, struct rtentry *);
146
147 void
148 rtable_init(void **table)
149 {
150 struct domain *dom;
151 for (dom = domains; dom; dom = dom->dom_next)
152 if (dom->dom_rtattach)
153 dom->dom_rtattach(&table[dom->dom_family],
154 dom->dom_rtoffset);
155 }
156
157 void
158 route_init(void)
159 {
160
161 pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
162 NULL);
163
164 rn_init(); /* initialize all zeroes, all ones, mask table */
165 rtable_init((void **)rt_tables);
166 }
167
168 /*
169 * Packet routing routines.
170 */
171 void
172 rtalloc(struct route *ro)
173 {
174 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
175 return; /* XXX */
176 ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
177 }
178
179 struct rtentry *
180 rtalloc1(const struct sockaddr *dst, int report)
181 {
182 struct radix_node_head *rnh = rt_tables[dst->sa_family];
183 struct rtentry *rt;
184 struct radix_node *rn;
185 struct rtentry *newrt = 0;
186 struct rt_addrinfo info;
187 int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
188
189 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
190 ((rn->rn_flags & RNF_ROOT) == 0)) {
191 newrt = rt = (struct rtentry *)rn;
192 if (report && (rt->rt_flags & RTF_CLONING)) {
193 err = rtrequest(RTM_RESOLVE, dst, SA(0),
194 SA(0), 0, &newrt);
195 if (err) {
196 newrt = rt;
197 rt->rt_refcnt++;
198 goto miss;
199 }
200 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
201 msgtype = RTM_RESOLVE;
202 goto miss;
203 }
204 /* Inform listeners of the new route */
205 memset(&info, 0, sizeof(info));
206 info.rti_info[RTAX_DST] = rt_key(rt);
207 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
208 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
209 if (rt->rt_ifp != NULL) {
210 info.rti_info[RTAX_IFP] =
211 TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
212 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
213 }
214 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
215 } else
216 rt->rt_refcnt++;
217 } else {
218 rtstat.rts_unreach++;
219 miss: if (report) {
220 memset((caddr_t)&info, 0, sizeof(info));
221 info.rti_info[RTAX_DST] = dst;
222 rt_missmsg(msgtype, &info, 0, err);
223 }
224 }
225 splx(s);
226 return (newrt);
227 }
228
229 void
230 rtfree(struct rtentry *rt)
231 {
232 struct ifaddr *ifa;
233
234 if (rt == 0)
235 panic("rtfree");
236 rt->rt_refcnt--;
237 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
238 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
239 panic ("rtfree 2");
240 rttrash--;
241 if (rt->rt_refcnt < 0) {
242 printf("rtfree: %p not freed (neg refs)\n", rt);
243 return;
244 }
245 rt_timer_remove_all(rt, 0);
246 ifa = rt->rt_ifa;
247 IFAFREE(ifa);
248 Free(rt_key(rt));
249 pool_put(&rtentry_pool, rt);
250 }
251 }
252
253 void
254 ifafree(struct ifaddr *ifa)
255 {
256
257 #ifdef DIAGNOSTIC
258 if (ifa == NULL)
259 panic("ifafree: null ifa");
260 if (ifa->ifa_refcnt != 0)
261 panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
262 #endif
263 #ifdef IFAREF_DEBUG
264 printf("ifafree: freeing ifaddr %p\n", ifa);
265 #endif
266 free(ifa, M_IFADDR);
267 }
268
269 /*
270 * Force a routing table entry to the specified
271 * destination to go through the given gateway.
272 * Normally called as a result of a routing redirect
273 * message from the network layer.
274 *
275 * N.B.: must be called at splsoftnet
276 */
277 void
278 rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
279 const struct sockaddr *netmask, int flags, const struct sockaddr *src,
280 struct rtentry **rtp)
281 {
282 struct rtentry *rt;
283 int error = 0;
284 u_quad_t *stat = 0;
285 struct rt_addrinfo info;
286 struct ifaddr *ifa;
287
288 /* verify the gateway is directly reachable */
289 if ((ifa = ifa_ifwithnet(gateway)) == 0) {
290 error = ENETUNREACH;
291 goto out;
292 }
293 rt = rtalloc1(dst, 0);
294 /*
295 * If the redirect isn't from our current router for this dst,
296 * it's either old or wrong. If it redirects us to ourselves,
297 * we have a routing loop, perhaps as a result of an interface
298 * going down recently.
299 */
300 #define equal(a1, a2) \
301 ((a1)->sa_len == (a2)->sa_len && \
302 bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
303 if (!(flags & RTF_DONE) && rt &&
304 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
305 error = EINVAL;
306 else if (ifa_ifwithaddr(gateway))
307 error = EHOSTUNREACH;
308 if (error)
309 goto done;
310 /*
311 * Create a new entry if we just got back a wildcard entry
312 * or the lookup failed. This is necessary for hosts
313 * which use routing redirects generated by smart gateways
314 * to dynamically build the routing tables.
315 */
316 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
317 goto create;
318 /*
319 * Don't listen to the redirect if it's
320 * for a route to an interface.
321 */
322 if (rt->rt_flags & RTF_GATEWAY) {
323 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
324 /*
325 * Changing from route to net => route to host.
326 * Create new route, rather than smashing route to net.
327 */
328 create:
329 if (rt)
330 rtfree(rt);
331 flags |= RTF_GATEWAY | RTF_DYNAMIC;
332 info.rti_info[RTAX_DST] = dst;
333 info.rti_info[RTAX_GATEWAY] = gateway;
334 info.rti_info[RTAX_NETMASK] = netmask;
335 info.rti_ifa = ifa;
336 info.rti_flags = flags;
337 rt = NULL;
338 error = rtrequest1(RTM_ADD, &info, &rt);
339 if (rt != NULL)
340 flags = rt->rt_flags;
341 stat = &rtstat.rts_dynamic;
342 } else {
343 /*
344 * Smash the current notion of the gateway to
345 * this destination. Should check about netmask!!!
346 */
347 rt->rt_flags |= RTF_MODIFIED;
348 flags |= RTF_MODIFIED;
349 stat = &rtstat.rts_newgateway;
350 rt_setgate(rt, rt_key(rt), gateway);
351 }
352 } else
353 error = EHOSTUNREACH;
354 done:
355 if (rt) {
356 if (rtp && !error)
357 *rtp = rt;
358 else
359 rtfree(rt);
360 }
361 out:
362 if (error)
363 rtstat.rts_badredirect++;
364 else if (stat != NULL)
365 (*stat)++;
366 memset((caddr_t)&info, 0, sizeof(info));
367 info.rti_info[RTAX_DST] = dst;
368 info.rti_info[RTAX_GATEWAY] = gateway;
369 info.rti_info[RTAX_NETMASK] = netmask;
370 info.rti_info[RTAX_AUTHOR] = src;
371 rt_missmsg(RTM_REDIRECT, &info, flags, error);
372 }
373
374 /*
375 * Delete a route and generate a message
376 */
377 static int
378 rtdeletemsg(struct rtentry *rt)
379 {
380 int error;
381 struct rt_addrinfo info;
382
383 /*
384 * Request the new route so that the entry is not actually
385 * deleted. That will allow the information being reported to
386 * be accurate (and consistent with route_output()).
387 */
388 memset((caddr_t)&info, 0, sizeof(info));
389 info.rti_info[RTAX_DST] = rt_key(rt);
390 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
391 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
392 info.rti_flags = rt->rt_flags;
393 error = rtrequest1(RTM_DELETE, &info, &rt);
394
395 rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
396
397 /* Adjust the refcount */
398 if (error == 0 && rt->rt_refcnt <= 0) {
399 rt->rt_refcnt++;
400 rtfree(rt);
401 }
402 return (error);
403 }
404
405 static int
406 rtflushclone1(struct radix_node *rn, void *arg)
407 {
408 struct rtentry *rt, *parent;
409
410 rt = (struct rtentry *)rn;
411 parent = (struct rtentry *)arg;
412 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
413 rtdeletemsg(rt);
414 return 0;
415 }
416
417 static void
418 rtflushclone(struct radix_node_head *rnh, struct rtentry *parent)
419 {
420
421 #ifdef DIAGNOSTIC
422 if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
423 panic("rtflushclone: called with a non-cloning route");
424 if (!rnh->rnh_walktree)
425 panic("rtflushclone: no rnh_walktree");
426 #endif
427 rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
428 }
429
430 /*
431 * Routing table ioctl interface.
432 */
433 int
434 rtioctl(u_long req, caddr_t data, struct proc *p)
435 {
436 return (EOPNOTSUPP);
437 }
438
439 struct ifaddr *
440 ifa_ifwithroute(int flags, const struct sockaddr *dst,
441 const struct sockaddr *gateway)
442 {
443 struct ifaddr *ifa;
444 if ((flags & RTF_GATEWAY) == 0) {
445 /*
446 * If we are adding a route to an interface,
447 * and the interface is a pt to pt link
448 * we should search for the destination
449 * as our clue to the interface. Otherwise
450 * we can use the local address.
451 */
452 ifa = 0;
453 if (flags & RTF_HOST)
454 ifa = ifa_ifwithdstaddr(dst);
455 if (ifa == 0)
456 ifa = ifa_ifwithaddr(gateway);
457 } else {
458 /*
459 * If we are adding a route to a remote net
460 * or host, the gateway may still be on the
461 * other end of a pt to pt link.
462 */
463 ifa = ifa_ifwithdstaddr(gateway);
464 }
465 if (ifa == 0)
466 ifa = ifa_ifwithnet(gateway);
467 if (ifa == 0) {
468 struct rtentry *rt = rtalloc1(dst, 0);
469 if (rt == 0)
470 return (0);
471 rt->rt_refcnt--;
472 if ((ifa = rt->rt_ifa) == 0)
473 return (0);
474 }
475 if (ifa->ifa_addr->sa_family != dst->sa_family) {
476 struct ifaddr *oifa = ifa;
477 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
478 if (ifa == 0)
479 ifa = oifa;
480 }
481 return (ifa);
482 }
483
484 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
485
486 int
487 rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
488 const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
489 {
490 struct rt_addrinfo info;
491
492 memset(&info, 0, sizeof(info));
493 info.rti_flags = flags;
494 info.rti_info[RTAX_DST] = dst;
495 info.rti_info[RTAX_GATEWAY] = gateway;
496 info.rti_info[RTAX_NETMASK] = netmask;
497 return rtrequest1(req, &info, ret_nrt);
498 }
499
500 /*
501 * These (questionable) definitions of apparent local variables apply
502 * to the next function. XXXXXX!!!
503 */
504 #define dst info->rti_info[RTAX_DST]
505 #define gateway info->rti_info[RTAX_GATEWAY]
506 #define netmask info->rti_info[RTAX_NETMASK]
507 #define ifaaddr info->rti_info[RTAX_IFA]
508 #define ifpaddr info->rti_info[RTAX_IFP]
509 #define flags info->rti_flags
510
511 int
512 rt_getifa(struct rt_addrinfo *info)
513 {
514 struct ifaddr *ifa;
515 int error = 0;
516
517 /*
518 * ifp may be specified by sockaddr_dl when protocol address
519 * is ambiguous
520 */
521 if (info->rti_ifp == NULL && ifpaddr != NULL
522 && ifpaddr->sa_family == AF_LINK &&
523 (ifa = ifa_ifwithnet((const struct sockaddr *)ifpaddr)) != NULL)
524 info->rti_ifp = ifa->ifa_ifp;
525 if (info->rti_ifa == NULL && ifaaddr != NULL)
526 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
527 if (info->rti_ifa == NULL) {
528 const struct sockaddr *sa;
529
530 sa = ifaaddr != NULL ? ifaaddr :
531 (gateway != NULL ? gateway : dst);
532 if (sa != NULL && info->rti_ifp != NULL)
533 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
534 else if (dst != NULL && gateway != NULL)
535 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
536 else if (sa != NULL)
537 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
538 }
539 if ((ifa = info->rti_ifa) != NULL) {
540 if (info->rti_ifp == NULL)
541 info->rti_ifp = ifa->ifa_ifp;
542 } else
543 error = ENETUNREACH;
544 return (error);
545 }
546
547 int
548 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
549 {
550 int s = splsoftnet();
551 int error = 0;
552 struct rtentry *rt, *crt;
553 struct radix_node *rn;
554 struct radix_node_head *rnh;
555 struct ifaddr *ifa;
556 struct sockaddr *ndst;
557 #define senderr(x) { error = x ; goto bad; }
558
559 if ((rnh = rt_tables[dst->sa_family]) == 0)
560 senderr(ESRCH);
561 if (flags & RTF_HOST)
562 netmask = 0;
563 switch (req) {
564 case RTM_DELETE:
565 if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == 0)
566 senderr(ESRCH);
567 rt = (struct rtentry *)rn;
568 if ((rt->rt_flags & RTF_CLONING) != 0) {
569 /* clean up any cloned children */
570 rtflushclone(rnh, rt);
571 }
572 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
573 senderr(ESRCH);
574 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
575 panic ("rtrequest delete");
576 rt = (struct rtentry *)rn;
577 if (rt->rt_gwroute) {
578 rt = rt->rt_gwroute; RTFREE(rt);
579 (rt = (struct rtentry *)rn)->rt_gwroute = 0;
580 }
581 if (rt->rt_parent) {
582 rt->rt_parent->rt_refcnt--;
583 rt->rt_parent = NULL;
584 }
585 rt->rt_flags &= ~RTF_UP;
586 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
587 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
588 rttrash++;
589 if (ret_nrt)
590 *ret_nrt = rt;
591 else if (rt->rt_refcnt <= 0) {
592 rt->rt_refcnt++;
593 rtfree(rt);
594 }
595 break;
596
597 case RTM_RESOLVE:
598 if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
599 senderr(EINVAL);
600 if ((rt->rt_flags & RTF_CLONING) == 0)
601 senderr(EINVAL);
602 ifa = rt->rt_ifa;
603 flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
604 flags |= RTF_CLONED;
605 gateway = rt->rt_gateway;
606 if ((netmask = rt->rt_genmask) == 0)
607 flags |= RTF_HOST;
608 goto makeroute;
609
610 case RTM_ADD:
611 if (info->rti_ifa == 0 && (error = rt_getifa(info)))
612 senderr(error);
613 ifa = info->rti_ifa;
614 makeroute:
615 rt = pool_get(&rtentry_pool, PR_NOWAIT);
616 if (rt == 0)
617 senderr(ENOBUFS);
618 Bzero(rt, sizeof(*rt));
619 rt->rt_flags = RTF_UP | flags;
620 LIST_INIT(&rt->rt_timer);
621 if (rt_setgate(rt, dst, gateway)) {
622 pool_put(&rtentry_pool, rt);
623 senderr(ENOBUFS);
624 }
625 ndst = rt_key(rt);
626 if (netmask) {
627 rt_maskedcopy(dst, ndst, netmask);
628 } else
629 Bcopy(dst, ndst, dst->sa_len);
630 IFAREF(ifa);
631 rt->rt_ifa = ifa;
632 rt->rt_ifp = ifa->ifa_ifp;
633 if (req == RTM_RESOLVE) {
634 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
635 rt->rt_parent = *ret_nrt;
636 rt->rt_parent->rt_refcnt++;
637 }
638 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
639 rnh, rt->rt_nodes);
640 if (rn == NULL && (crt = rtalloc1(ndst, 0)) != NULL) {
641 /* overwrite cloned route */
642 if ((crt->rt_flags & RTF_CLONED) != 0) {
643 rtdeletemsg(crt);
644 rn = rnh->rnh_addaddr((caddr_t)ndst,
645 (caddr_t)netmask, rnh, rt->rt_nodes);
646 }
647 RTFREE(crt);
648 }
649 if (rn == 0) {
650 IFAFREE(ifa);
651 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
652 rtfree(rt->rt_parent);
653 if (rt->rt_gwroute)
654 rtfree(rt->rt_gwroute);
655 Free(rt_key(rt));
656 pool_put(&rtentry_pool, rt);
657 senderr(EEXIST);
658 }
659 if (ifa->ifa_rtrequest)
660 ifa->ifa_rtrequest(req, rt, info);
661 if (ret_nrt) {
662 *ret_nrt = rt;
663 rt->rt_refcnt++;
664 }
665 if ((rt->rt_flags & RTF_CLONING) != 0) {
666 /* clean up any cloned children */
667 rtflushclone(rnh, rt);
668 }
669 break;
670 }
671 bad:
672 splx(s);
673 return (error);
674 }
675
676 #undef dst
677 #undef gateway
678 #undef netmask
679 #undef ifaaddr
680 #undef ifpaddr
681 #undef flags
682
683 int
684 rt_setgate( struct rtentry *rt0, const struct sockaddr *dst,
685 const struct sockaddr *gate)
686 {
687 char *new, *old;
688 u_int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
689 struct rtentry *rt = rt0;
690
691 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
692 old = (caddr_t)rt_key(rt);
693 R_Malloc(new, caddr_t, dlen + glen);
694 if (new == 0)
695 return 1;
696 Bzero(new, dlen + glen);
697 rt->rt_nodes->rn_key = new;
698 } else {
699 new = (void *)rt->rt_nodes->rn_key;
700 old = 0;
701 }
702 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
703 if (old) {
704 Bcopy(dst, new, dlen);
705 Free(old);
706 }
707 if (rt->rt_gwroute) {
708 rt = rt->rt_gwroute; RTFREE(rt);
709 rt = rt0; rt->rt_gwroute = 0;
710 }
711 if (rt->rt_flags & RTF_GATEWAY) {
712 rt->rt_gwroute = rtalloc1(gate, 1);
713 /*
714 * If we switched gateways, grab the MTU from the new
715 * gateway route if the current MTU, if the current MTU is
716 * greater than the MTU of gateway.
717 * Note that, if the MTU of gateway is 0, we will reset the
718 * MTU of the route to run PMTUD again from scratch. XXX
719 */
720 if (rt->rt_gwroute
721 && !(rt->rt_rmx.rmx_locks & RTV_MTU)
722 && rt->rt_rmx.rmx_mtu
723 && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
724 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
725 }
726 }
727 return 0;
728 }
729
730 void
731 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
732 const struct sockaddr *netmask)
733 {
734 const u_char *cp1 = (u_char *)src;
735 u_char *cp2 = (u_char *)dst;
736 const u_char *cp3 = (u_char *)netmask;
737 u_char *cplim = cp2 + *cp3;
738 u_char *cplim2 = cp2 + *cp1;
739
740 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
741 cp3 += 2;
742 if (cplim > cplim2)
743 cplim = cplim2;
744 while (cp2 < cplim)
745 *cp2++ = *cp1++ & *cp3++;
746 if (cp2 < cplim2)
747 memset((caddr_t)cp2, 0, (unsigned)(cplim2 - cp2));
748 }
749
750 /*
751 * Set up or tear down a routing table entry, normally
752 * for an interface.
753 */
754 int
755 rtinit(struct ifaddr *ifa, int cmd, int flags)
756 {
757 struct rtentry *rt;
758 struct sockaddr *dst, *odst;
759 struct sockaddr_storage deldst;
760 struct rtentry *nrt = 0;
761 int error;
762 struct rt_addrinfo info;
763
764 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
765 if (cmd == RTM_DELETE) {
766 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
767 /* Delete subnet route for this interface */
768 odst = dst;
769 dst = (struct sockaddr *)&deldst;
770 rt_maskedcopy(odst, dst, ifa->ifa_netmask);
771 }
772 if ((rt = rtalloc1(dst, 0)) != NULL) {
773 rt->rt_refcnt--;
774 if (rt->rt_ifa != ifa)
775 return (flags & RTF_HOST ? EHOSTUNREACH
776 : ENETUNREACH);
777 }
778 }
779 memset(&info, 0, sizeof(info));
780 info.rti_ifa = ifa;
781 info.rti_flags = flags | ifa->ifa_flags;
782 info.rti_info[RTAX_DST] = dst;
783 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
784 /*
785 * XXX here, it seems that we are assuming that ifa_netmask is NULL
786 * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate
787 * variable) when RTF_HOST is 1. still not sure if i can safely
788 * change it to meet bsdi4 behavior.
789 */
790 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
791 error = rtrequest1(cmd, &info, &nrt);
792 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
793 rt_newaddrmsg(cmd, ifa, error, nrt);
794 if (rt->rt_refcnt <= 0) {
795 rt->rt_refcnt++;
796 rtfree(rt);
797 }
798 }
799 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
800 rt->rt_refcnt--;
801 if (rt->rt_ifa != ifa) {
802 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
803 rt->rt_ifa);
804 if (rt->rt_ifa->ifa_rtrequest)
805 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
806 IFAFREE(rt->rt_ifa);
807 rt->rt_ifa = ifa;
808 rt->rt_ifp = ifa->ifa_ifp;
809 IFAREF(ifa);
810 if (ifa->ifa_rtrequest)
811 ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
812 }
813 rt_newaddrmsg(cmd, ifa, error, nrt);
814 }
815 return (error);
816 }
817
818 /*
819 * Route timer routines. These routes allow functions to be called
820 * for various routes at any time. This is useful in supporting
821 * path MTU discovery and redirect route deletion.
822 *
823 * This is similar to some BSDI internal functions, but it provides
824 * for multiple queues for efficiency's sake...
825 */
826
827 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
828 static int rt_init_done = 0;
829
830 #define RTTIMER_CALLOUT(r) do { \
831 if (r->rtt_func != NULL) { \
832 (*r->rtt_func)(r->rtt_rt, r); \
833 } else { \
834 rtrequest((int) RTM_DELETE, \
835 (struct sockaddr *)rt_key(r->rtt_rt), \
836 0, 0, 0, 0); \
837 } \
838 } while (/*CONSTCOND*/0)
839
840 /*
841 * Some subtle order problems with domain initialization mean that
842 * we cannot count on this being run from rt_init before various
843 * protocol initializations are done. Therefore, we make sure
844 * that this is run when the first queue is added...
845 */
846
847 void
848 rt_timer_init(void)
849 {
850 assert(rt_init_done == 0);
851
852 pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
853 NULL);
854
855 LIST_INIT(&rttimer_queue_head);
856 callout_init(&rt_timer_ch);
857 callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
858 rt_init_done = 1;
859 }
860
861 struct rttimer_queue *
862 rt_timer_queue_create(u_int timeout)
863 {
864 struct rttimer_queue *rtq;
865
866 if (rt_init_done == 0)
867 rt_timer_init();
868
869 R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
870 if (rtq == NULL)
871 return (NULL);
872 Bzero(rtq, sizeof *rtq);
873
874 rtq->rtq_timeout = timeout;
875 rtq->rtq_count = 0;
876 TAILQ_INIT(&rtq->rtq_head);
877 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
878
879 return (rtq);
880 }
881
882 void
883 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
884 {
885
886 rtq->rtq_timeout = timeout;
887 }
888
889 void
890 rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
891 {
892 struct rttimer *r;
893
894 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
895 LIST_REMOVE(r, rtt_link);
896 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
897 if (destroy)
898 RTTIMER_CALLOUT(r);
899 pool_put(&rttimer_pool, r);
900 if (rtq->rtq_count > 0)
901 rtq->rtq_count--;
902 else
903 printf("rt_timer_queue_remove_all: "
904 "rtq_count reached 0\n");
905 }
906 }
907
908 void
909 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
910 {
911
912 rt_timer_queue_remove_all(rtq, destroy);
913
914 LIST_REMOVE(rtq, rtq_link);
915
916 /*
917 * Caller is responsible for freeing the rttimer_queue structure.
918 */
919 }
920
921 unsigned long
922 rt_timer_count(struct rttimer_queue *rtq)
923 {
924 return rtq->rtq_count;
925 }
926
927 void
928 rt_timer_remove_all(struct rtentry *rt, int destroy)
929 {
930 struct rttimer *r;
931
932 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
933 LIST_REMOVE(r, rtt_link);
934 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
935 if (destroy)
936 RTTIMER_CALLOUT(r);
937 if (r->rtt_queue->rtq_count > 0)
938 r->rtt_queue->rtq_count--;
939 else
940 printf("rt_timer_remove_all: rtq_count reached 0\n");
941 pool_put(&rttimer_pool, r);
942 }
943 }
944
945 int
946 rt_timer_add(struct rtentry *rt,
947 void (*func)(struct rtentry *, struct rttimer *),
948 struct rttimer_queue *queue)
949 {
950 struct rttimer *r;
951 long current_time;
952 int s;
953
954 s = splclock();
955 current_time = mono_time.tv_sec;
956 splx(s);
957
958 /*
959 * If there's already a timer with this action, destroy it before
960 * we add a new one.
961 */
962 for (r = LIST_FIRST(&rt->rt_timer); r != NULL;
963 r = LIST_NEXT(r, rtt_link)) {
964 if (r->rtt_func == func) {
965 LIST_REMOVE(r, rtt_link);
966 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
967 if (r->rtt_queue->rtq_count > 0)
968 r->rtt_queue->rtq_count--;
969 else
970 printf("rt_timer_add: rtq_count reached 0\n");
971 pool_put(&rttimer_pool, r);
972 break; /* only one per list, so we can quit... */
973 }
974 }
975
976 r = pool_get(&rttimer_pool, PR_NOWAIT);
977 if (r == NULL)
978 return (ENOBUFS);
979 Bzero(r, sizeof(*r));
980
981 r->rtt_rt = rt;
982 r->rtt_time = current_time;
983 r->rtt_func = func;
984 r->rtt_queue = queue;
985 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
986 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
987 r->rtt_queue->rtq_count++;
988
989 return (0);
990 }
991
992 /* ARGSUSED */
993 void
994 rt_timer_timer(void *arg)
995 {
996 struct rttimer_queue *rtq;
997 struct rttimer *r;
998 long current_time;
999 int s;
1000
1001 s = splclock();
1002 current_time = mono_time.tv_sec;
1003 splx(s);
1004
1005 s = splsoftnet();
1006 for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
1007 rtq = LIST_NEXT(rtq, rtq_link)) {
1008 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1009 (r->rtt_time + rtq->rtq_timeout) < current_time) {
1010 LIST_REMOVE(r, rtt_link);
1011 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1012 RTTIMER_CALLOUT(r);
1013 pool_put(&rttimer_pool, r);
1014 if (rtq->rtq_count > 0)
1015 rtq->rtq_count--;
1016 else
1017 printf("rt_timer_timer: rtq_count reached 0\n");
1018 }
1019 }
1020 splx(s);
1021
1022 callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1023 }
1024