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