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