route.c revision 1.29 1 /* $NetBSD: route.c,v 1.29 1999/10/09 18:55:30 sommerfeld 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/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 int rttrash; /* routes not in table but not freed */
132 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
133
134 struct pool rtentry_pool; /* pool for rtentry structures */
135 struct pool rttimer_pool; /* pool for rttimer structures */
136
137 void
138 rtable_init(table)
139 void **table;
140 {
141 struct domain *dom;
142 for (dom = domains; dom; dom = dom->dom_next)
143 if (dom->dom_rtattach)
144 dom->dom_rtattach(&table[dom->dom_family],
145 dom->dom_rtoffset);
146 }
147
148 void
149 route_init()
150 {
151
152 pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
153 0, NULL, NULL, M_RTABLE);
154
155 rn_init(); /* initialize all zeroes, all ones, mask table */
156 rtable_init((void **)rt_tables);
157 }
158
159 /*
160 * Packet routing routines.
161 */
162 void
163 rtalloc(ro)
164 register struct route *ro;
165 {
166 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
167 return; /* XXX */
168 ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
169 }
170
171 struct rtentry *
172 rtalloc1(dst, report)
173 register struct sockaddr *dst;
174 int report;
175 {
176 register struct radix_node_head *rnh = rt_tables[dst->sa_family];
177 register struct rtentry *rt;
178 register struct radix_node *rn;
179 struct rtentry *newrt = 0;
180 struct rt_addrinfo info;
181 int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
182
183 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)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, SA(0),
188 SA(0), 0, &newrt);
189 if (err) {
190 newrt = rt;
191 rt->rt_refcnt++;
192 goto miss;
193 }
194 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
195 msgtype = RTM_RESOLVE;
196 goto miss;
197 }
198 } else
199 rt->rt_refcnt++;
200 } else {
201 rtstat.rts_unreach++;
202 miss: if (report) {
203 bzero((caddr_t)&info, sizeof(info));
204 info.rti_info[RTAX_DST] = dst;
205 rt_missmsg(msgtype, &info, 0, err);
206 }
207 }
208 splx(s);
209 return (newrt);
210 }
211
212 void
213 rtfree(rt)
214 register struct rtentry *rt;
215 {
216 register struct ifaddr *ifa;
217
218 if (rt == 0)
219 panic("rtfree");
220 rt->rt_refcnt--;
221 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
222 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
223 panic ("rtfree 2");
224 rttrash--;
225 if (rt->rt_refcnt < 0) {
226 printf("rtfree: %p not freed (neg refs)\n", rt);
227 return;
228 }
229 rt_timer_remove_all(rt);
230 ifa = rt->rt_ifa;
231 IFAFREE(ifa);
232 Free(rt_key(rt));
233 pool_put(&rtentry_pool, rt);
234 }
235 }
236
237 void
238 ifafree(ifa)
239 register struct ifaddr *ifa;
240 {
241 if (ifa == NULL)
242 panic("ifafree");
243 if (ifa->ifa_refcnt == 0)
244 free(ifa, M_IFADDR);
245 else
246 ifa->ifa_refcnt--;
247 }
248
249 /*
250 * Force a routing table entry to the specified
251 * destination to go through the given gateway.
252 * Normally called as a result of a routing redirect
253 * message from the network layer.
254 *
255 * N.B.: must be called at splsoftnet
256 */
257 void
258 rtredirect(dst, gateway, netmask, flags, src, rtp)
259 struct sockaddr *dst, *gateway, *netmask, *src;
260 int flags;
261 struct rtentry **rtp;
262 {
263 register struct rtentry *rt;
264 int error = 0;
265 short *stat = 0;
266 struct rt_addrinfo info;
267 struct ifaddr *ifa;
268
269 /* verify the gateway is directly reachable */
270 if ((ifa = ifa_ifwithnet(gateway)) == 0) {
271 error = ENETUNREACH;
272 goto out;
273 }
274 rt = rtalloc1(dst, 0);
275 /*
276 * If the redirect isn't from our current router for this dst,
277 * it's either old or wrong. If it redirects us to ourselves,
278 * we have a routing loop, perhaps as a result of an interface
279 * going down recently.
280 */
281 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
282 if (!(flags & RTF_DONE) && rt &&
283 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
284 error = EINVAL;
285 else if (ifa_ifwithaddr(gateway))
286 error = EHOSTUNREACH;
287 if (error)
288 goto done;
289 /*
290 * Create a new entry if we just got back a wildcard entry
291 * or the the lookup failed. This is necessary for hosts
292 * which use routing redirects generated by smart gateways
293 * to dynamically build the routing tables.
294 */
295 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
296 goto create;
297 /*
298 * Don't listen to the redirect if it's
299 * for a route to an interface.
300 */
301 if (rt->rt_flags & RTF_GATEWAY) {
302 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
303 /*
304 * Changing from route to net => route to host.
305 * Create new route, rather than smashing route to net.
306 */
307 create:
308 flags |= RTF_GATEWAY | RTF_DYNAMIC;
309 error = rtrequest((int)RTM_ADD, dst, gateway,
310 netmask, flags,
311 (struct rtentry **)0);
312 stat = &rtstat.rts_dynamic;
313 } else {
314 /*
315 * Smash the current notion of the gateway to
316 * this destination. Should check about netmask!!!
317 */
318 rt->rt_flags |= RTF_MODIFIED;
319 flags |= RTF_MODIFIED;
320 stat = &rtstat.rts_newgateway;
321 rt_setgate(rt, rt_key(rt), gateway);
322 }
323 } else
324 error = EHOSTUNREACH;
325 done:
326 if (rt) {
327 if (rtp && !error)
328 *rtp = rt;
329 else
330 rtfree(rt);
331 }
332 out:
333 if (error)
334 rtstat.rts_badredirect++;
335 else if (stat != NULL)
336 (*stat)++;
337 bzero((caddr_t)&info, sizeof(info));
338 info.rti_info[RTAX_DST] = dst;
339 info.rti_info[RTAX_GATEWAY] = gateway;
340 info.rti_info[RTAX_NETMASK] = netmask;
341 info.rti_info[RTAX_AUTHOR] = src;
342 rt_missmsg(RTM_REDIRECT, &info, flags, error);
343 }
344
345 /*
346 * Routing table ioctl interface.
347 */
348 int
349 rtioctl(req, data, p)
350 u_long req;
351 caddr_t data;
352 struct proc *p;
353 {
354 return (EOPNOTSUPP);
355 }
356
357 struct ifaddr *
358 ifa_ifwithroute(flags, dst, gateway)
359 int flags;
360 struct sockaddr *dst, *gateway;
361 {
362 register struct ifaddr *ifa;
363 if ((flags & RTF_GATEWAY) == 0) {
364 /*
365 * If we are adding a route to an interface,
366 * and the interface is a pt to pt link
367 * we should search for the destination
368 * as our clue to the interface. Otherwise
369 * we can use the local address.
370 */
371 ifa = 0;
372 if (flags & RTF_HOST)
373 ifa = ifa_ifwithdstaddr(dst);
374 if (ifa == 0)
375 ifa = ifa_ifwithaddr(gateway);
376 } else {
377 /*
378 * If we are adding a route to a remote net
379 * or host, the gateway may still be on the
380 * other end of a pt to pt link.
381 */
382 ifa = ifa_ifwithdstaddr(gateway);
383 }
384 if (ifa == 0)
385 ifa = ifa_ifwithnet(gateway);
386 if (ifa == 0) {
387 struct rtentry *rt = rtalloc1(dst, 0);
388 if (rt == 0)
389 return (0);
390 rt->rt_refcnt--;
391 if ((ifa = rt->rt_ifa) == 0)
392 return (0);
393 }
394 if (ifa->ifa_addr->sa_family != dst->sa_family) {
395 struct ifaddr *oifa = ifa;
396 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
397 if (ifa == 0)
398 ifa = oifa;
399 }
400 return (ifa);
401 }
402
403 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
404
405 int
406 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
407 int req, flags;
408 struct sockaddr *dst, *gateway, *netmask;
409 struct rtentry **ret_nrt;
410 {
411 int s = splsoftnet(); int error = 0;
412 register struct rtentry *rt;
413 register struct radix_node *rn;
414 register struct radix_node_head *rnh;
415 struct ifaddr *ifa;
416 struct sockaddr *ndst;
417 #define senderr(x) { error = x ; goto bad; }
418
419 if ((rnh = rt_tables[dst->sa_family]) == 0)
420 senderr(ESRCH);
421 if (flags & RTF_HOST)
422 netmask = 0;
423 switch (req) {
424 case RTM_DELETE:
425 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
426 senderr(ESRCH);
427 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
428 panic ("rtrequest delete");
429 rt = (struct rtentry *)rn;
430 if (rt->rt_gwroute) {
431 rt = rt->rt_gwroute; RTFREE(rt);
432 (rt = (struct rtentry *)rn)->rt_gwroute = 0;
433 }
434 rt->rt_flags &= ~RTF_UP;
435 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
436 ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
437 rttrash++;
438 if (ret_nrt)
439 *ret_nrt = rt;
440 else if (rt->rt_refcnt <= 0) {
441 rt->rt_refcnt++;
442 rtfree(rt);
443 }
444 break;
445
446 case RTM_RESOLVE:
447 if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
448 senderr(EINVAL);
449 ifa = rt->rt_ifa;
450 flags = rt->rt_flags & ~RTF_CLONING;
451 gateway = rt->rt_gateway;
452 if ((netmask = rt->rt_genmask) == 0)
453 flags |= RTF_HOST;
454 goto makeroute;
455
456 case RTM_ADD:
457 if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
458 senderr(ENETUNREACH);
459
460 /* The interface found in the previous statement may
461 * be overridden later by rt_setif. See the code
462 * for case RTM_ADD in rtsock.c:route_output.
463 */
464 makeroute:
465 rt = pool_get(&rtentry_pool, PR_NOWAIT);
466 if (rt == 0)
467 senderr(ENOBUFS);
468 Bzero(rt, sizeof(*rt));
469 rt->rt_flags = RTF_UP | flags;
470 LIST_INIT(&rt->rt_timer);
471 if (rt_setgate(rt, dst, gateway)) {
472 pool_put(&rtentry_pool, rt);
473 senderr(ENOBUFS);
474 }
475 ndst = rt_key(rt);
476 if (netmask) {
477 rt_maskedcopy(dst, ndst, netmask);
478 } else
479 Bcopy(dst, ndst, dst->sa_len);
480 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
481 rnh, rt->rt_nodes);
482 if (rn == 0) {
483 if (rt->rt_gwroute)
484 rtfree(rt->rt_gwroute);
485 Free(rt_key(rt));
486 pool_put(&rtentry_pool, rt);
487 senderr(EEXIST);
488 }
489 ifa->ifa_refcnt++;
490 rt->rt_ifa = ifa;
491 rt->rt_ifp = ifa->ifa_ifp;
492 if (req == RTM_RESOLVE) {
493 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
494 } else if (rt->rt_rmx.rmx_mtu == 0
495 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { /* XXX */
496 if (rt->rt_gwroute != NULL) {
497 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
498 } else {
499 rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu;
500 }
501 }
502 if (ifa->ifa_rtrequest)
503 ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
504 if (ret_nrt) {
505 *ret_nrt = rt;
506 rt->rt_refcnt++;
507 }
508 break;
509 }
510 bad:
511 splx(s);
512 return (error);
513 }
514
515 int
516 rt_setgate(rt0, dst, gate)
517 struct rtentry *rt0;
518 struct sockaddr *dst, *gate;
519 {
520 caddr_t new, old;
521 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
522 register struct rtentry *rt = rt0;
523
524 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
525 old = (caddr_t)rt_key(rt);
526 R_Malloc(new, caddr_t, dlen + glen);
527 if (new == 0)
528 return 1;
529 rt->rt_nodes->rn_key = new;
530 } else {
531 new = rt->rt_nodes->rn_key;
532 old = 0;
533 }
534 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
535 if (old) {
536 Bcopy(dst, new, dlen);
537 Free(old);
538 }
539 if (rt->rt_gwroute) {
540 rt = rt->rt_gwroute; RTFREE(rt);
541 rt = rt0; rt->rt_gwroute = 0;
542 }
543 if (rt->rt_flags & RTF_GATEWAY) {
544 rt->rt_gwroute = rtalloc1(gate, 1);
545 /*
546 * If we switched gateways, grab the MTU from the new
547 * gateway route if the current MTU is 0 or greater
548 * than the MTU of gateway.
549 */
550 if (rt->rt_gwroute
551 && !(rt->rt_rmx.rmx_locks & RTV_MTU)
552 && (rt->rt_rmx.rmx_mtu == 0
553 || rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu)) { /* XXX */
554 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
555 }
556 }
557 return 0;
558 }
559
560 void
561 rt_maskedcopy(src, dst, netmask)
562 struct sockaddr *src, *dst, *netmask;
563 {
564 register u_char *cp1 = (u_char *)src;
565 register u_char *cp2 = (u_char *)dst;
566 register u_char *cp3 = (u_char *)netmask;
567 u_char *cplim = cp2 + *cp3;
568 u_char *cplim2 = cp2 + *cp1;
569
570 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
571 cp3 += 2;
572 if (cplim > cplim2)
573 cplim = cplim2;
574 while (cp2 < cplim)
575 *cp2++ = *cp1++ & *cp3++;
576 if (cp2 < cplim2)
577 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
578 }
579
580 /*
581 * Set up or tear down a routing table entry, normally
582 * for an interface.
583 */
584 int
585 rtinit(ifa, cmd, flags)
586 register struct ifaddr *ifa;
587 int cmd, flags;
588 {
589 register struct rtentry *rt;
590 register struct sockaddr *dst, *odst;
591 struct sockaddr_storage deldst;
592 struct rtentry *nrt = 0;
593 int error;
594
595 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
596 if (cmd == RTM_DELETE) {
597 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
598 /* Delete subnet route for this interface */
599 odst = dst;
600 dst = (struct sockaddr *)&deldst;
601 rt_maskedcopy(odst, dst, ifa->ifa_netmask);
602 }
603 if ((rt = rtalloc1(dst, 0)) != NULL) {
604 rt->rt_refcnt--;
605 if (rt->rt_ifa != ifa)
606 return (flags & RTF_HOST ? EHOSTUNREACH
607 : ENETUNREACH);
608 }
609 }
610 error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
611 flags | ifa->ifa_flags, &nrt);
612 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
613 rt_newaddrmsg(cmd, ifa, error, nrt);
614 if (rt->rt_refcnt <= 0) {
615 rt->rt_refcnt++;
616 rtfree(rt);
617 }
618 }
619 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
620 rt->rt_refcnt--;
621 if (rt->rt_ifa != ifa) {
622 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
623 rt->rt_ifa);
624 if (rt->rt_ifa->ifa_rtrequest)
625 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
626 IFAFREE(rt->rt_ifa);
627 rt->rt_ifa = ifa;
628 rt->rt_ifp = ifa->ifa_ifp;
629 rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu; /*XXX*/
630 ifa->ifa_refcnt++;
631 if (ifa->ifa_rtrequest)
632 ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
633 }
634 rt_newaddrmsg(cmd, ifa, error, nrt);
635 }
636 return (error);
637 }
638
639 /*
640 * Route timer routines. These routes allow functions to be called
641 * for various routes at any time. This is useful in supporting
642 * path MTU discovery and redirect route deletion.
643 *
644 * This is similar to some BSDI internal functions, but it provides
645 * for multiple queues for efficiency's sake...
646 */
647
648 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
649 static int rt_init_done = 0;
650
651 #define RTTIMER_CALLOUT(r) { \
652 if (r->rtt_func != NULL) { \
653 (*r->rtt_func)(r->rtt_rt, r); \
654 } else { \
655 rtrequest((int) RTM_DELETE, \
656 (struct sockaddr *)rt_key(r->rtt_rt), \
657 0, 0, 0, 0); \
658 } \
659 }
660
661 /*
662 * Some subtle order problems with domain initialization mean that
663 * we cannot count on this being run from rt_init before various
664 * protocol initializations are done. Therefore, we make sure
665 * that this is run when the first queue is added...
666 */
667
668 void
669 rt_timer_init()
670 {
671 assert(rt_init_done == 0);
672
673 pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
674 0, NULL, NULL, M_RTABLE);
675
676 LIST_INIT(&rttimer_queue_head);
677 timeout(rt_timer_timer, NULL, hz); /* every second */
678 rt_init_done = 1;
679 }
680
681 struct rttimer_queue *
682 rt_timer_queue_create(timeout)
683 u_int timeout;
684 {
685 struct rttimer_queue *rtq;
686
687 if (rt_init_done == 0)
688 rt_timer_init();
689
690 R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
691 if (rtq == NULL)
692 return (NULL);
693
694 rtq->rtq_timeout = timeout;
695 TAILQ_INIT(&rtq->rtq_head);
696 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
697
698 return (rtq);
699 }
700
701 void
702 rt_timer_queue_change(rtq, timeout)
703 struct rttimer_queue *rtq;
704 long timeout;
705 {
706
707 rtq->rtq_timeout = timeout;
708 }
709
710
711 void
712 rt_timer_queue_destroy(rtq, destroy)
713 struct rttimer_queue *rtq;
714 int destroy;
715 {
716 struct rttimer *r;
717
718 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
719 LIST_REMOVE(r, rtt_link);
720 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
721 if (destroy)
722 RTTIMER_CALLOUT(r);
723 pool_put(&rttimer_pool, r);
724 }
725
726 LIST_REMOVE(rtq, rtq_link);
727
728 /*
729 * Caller is responsible for freeing the rttimer_queue structure.
730 */
731 }
732
733 void
734 rt_timer_remove_all(rt)
735 struct rtentry *rt;
736 {
737 struct rttimer *r;
738
739 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
740 LIST_REMOVE(r, rtt_link);
741 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
742 pool_put(&rttimer_pool, r);
743 }
744 }
745
746 int
747 rt_timer_add(rt, func, queue)
748 struct rtentry *rt;
749 void(*func) __P((struct rtentry *, struct rttimer *));
750 struct rttimer_queue *queue;
751 {
752 struct rttimer *r;
753 long current_time;
754 int s;
755
756 s = splclock();
757 current_time = mono_time.tv_sec;
758 splx(s);
759
760 /*
761 * If there's already a timer with this action, destroy it before
762 * we add a new one.
763 */
764 for (r = LIST_FIRST(&rt->rt_timer); r != NULL;
765 r = LIST_NEXT(r, rtt_link)) {
766 if (r->rtt_func == func) {
767 LIST_REMOVE(r, rtt_link);
768 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
769 pool_put(&rttimer_pool, r);
770 break; /* only one per list, so we can quit... */
771 }
772 }
773
774 r = pool_get(&rttimer_pool, PR_NOWAIT);
775 if (r == NULL)
776 return (ENOBUFS);
777
778 r->rtt_rt = rt;
779 r->rtt_time = current_time;
780 r->rtt_func = func;
781 r->rtt_queue = queue;
782 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
783 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
784
785 return (0);
786 }
787
788 /* ARGSUSED */
789 void
790 rt_timer_timer(arg)
791 void *arg;
792 {
793 struct rttimer_queue *rtq;
794 struct rttimer *r;
795 long current_time;
796 int s;
797
798 s = splclock();
799 current_time = mono_time.tv_sec;
800 splx(s);
801
802 s = splsoftnet();
803 for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
804 rtq = LIST_NEXT(rtq, rtq_link)) {
805 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
806 (r->rtt_time + rtq->rtq_timeout) < current_time) {
807 LIST_REMOVE(r, rtt_link);
808 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
809 RTTIMER_CALLOUT(r);
810 pool_put(&rttimer_pool, r);
811 }
812 }
813 splx(s);
814
815 timeout(rt_timer_timer, NULL, hz); /* every second */
816 }
817