2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 2004, 2005 Jeffrey M. Hsu. All rights reserved.
36 * License terms: all terms for the DragonFly license above plus the following:
38 * 4. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
41 * This product includes software developed by Jeffrey M. Hsu
42 * for the DragonFly Project.
44 * This requirement may be waived with permission from Jeffrey Hsu.
45 * Permission will be granted to any DragonFly user for free.
46 * This requirement will sunset and may be removed on Jan 31, 2006,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1980, 1986, 1991, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)route.c 8.3 (Berkeley) 1/9/95
84 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
85 * $DragonFly: src/sys/net/route.c,v 1.27 2006/12/23 00:44:54 swildner Exp $
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/malloc.h>
94 #include <sys/socket.h>
95 #include <sys/domain.h>
96 #include <sys/kernel.h>
97 #include <sys/sysctl.h>
98 #include <sys/globaldata.h>
99 #include <sys/thread.h>
100 #include <sys/thread2.h>
101 #include <sys/msgport2.h>
104 #include <net/route.h>
105 #include <net/netisr.h>
107 #include <netinet/in.h>
108 #include <net/ip_mroute/ip_mroute.h>
110 static struct rtstatistics rtstatistics_percpu
[MAXCPU
];
112 #define rtstat rtstatistics_percpu[mycpuid]
114 #define rtstat rtstatistics_percpu[0]
117 struct radix_node_head
*rt_tables
[MAXCPU
][AF_MAX
+1];
118 struct lwkt_port
*rt_ports
[MAXCPU
];
120 static void rt_maskedcopy (struct sockaddr
*, struct sockaddr
*,
122 static void rtable_init(void);
123 static void rtable_service_loop(void *dummy
);
124 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo
*,
125 struct rtentry
*, void *);
128 static int rtredirect_msghandler(struct lwkt_msg
*lmsg
);
129 static int rtrequest1_msghandler(struct lwkt_msg
*lmsg
);
132 SYSCTL_NODE(_net
, OID_AUTO
, route
, CTLFLAG_RW
, 0, "Routing");
135 static int route_debug
= 1;
136 SYSCTL_INT(_net_route
, OID_AUTO
, route_debug
, CTLFLAG_RW
,
137 &route_debug
, 0, "");
141 * Initialize the route table(s) for protocol domains and
142 * create a helper thread which will be responsible for updating
143 * route table entries on each cpu.
151 for (cpu
= 0; cpu
< ncpus
; ++cpu
)
152 bzero(&rtstatistics_percpu
[cpu
], sizeof(struct rtstatistics
));
153 rn_init(); /* initialize all zeroes, all ones, mask table */
155 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
156 lwkt_migratecpu(cpu
);
158 lwkt_create(rtable_service_loop
, NULL
, &rtd
, NULL
,
159 TDF_STOPREQ
, cpu
, "rtable_cpu %d", cpu
);
160 rt_ports
[cpu
] = &rtd
->td_msgport
;
163 lwkt_migratecpu(origcpu
);
171 SLIST_FOREACH(dom
, &domains
, dom_next
) {
172 if (dom
->dom_rtattach
) {
174 (void **)&rt_tables
[mycpuid
][dom
->dom_family
],
181 * Our per-cpu table management protocol thread. All route table operations
182 * are chained through all cpus in order starting at cpu #0 in order to
183 * maintain duplicate route tables on each cpu. Having a spearate route
184 * table management thread allows the protocol and interrupt threads to
185 * issue route table changes.
188 rtable_service_loop(void *dummy __unused
)
190 struct lwkt_msg
*lmsg
;
191 thread_t td
= curthread
;
193 while ((lmsg
= lwkt_waitport(&td
->td_msgport
, NULL
)) != NULL
) {
194 lmsg
->ms_cmd
.cm_func(lmsg
);
199 * Routing statistics.
203 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS
)
207 for (cpu
= 0; cpu
< ncpus
; ++cpu
) {
208 if ((error
= SYSCTL_OUT(req
, &rtstatistics_percpu
[cpu
],
209 sizeof(struct rtstatistics
))))
211 if ((error
= SYSCTL_IN(req
, &rtstatistics_percpu
[cpu
],
212 sizeof(struct rtstatistics
))))
218 SYSCTL_PROC(_net_route
, OID_AUTO
, stats
, (CTLTYPE_OPAQUE
|CTLFLAG_RW
),
219 0, 0, sysctl_rtstatistics
, "S,rtstatistics", "Routing statistics");
221 SYSCTL_STRUCT(_net_route
, OID_AUTO
, stats
, CTLFLAG_RW
, &rtstat
, rtstatistics
,
222 "Routing statistics");
226 * Packet routing routines.
230 * Look up and fill in the "ro_rt" rtentry field in a route structure given
231 * an address in the "ro_dst" field. Always send a report on a miss and
232 * always clone routes.
235 rtalloc(struct route
*ro
)
237 rtalloc_ign(ro
, 0UL);
241 * Look up and fill in the "ro_rt" rtentry field in a route structure given
242 * an address in the "ro_dst" field. Always send a report on a miss and
243 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
247 rtalloc_ign(struct route
*ro
, u_long ignoreflags
)
249 if (ro
->ro_rt
!= NULL
) {
250 if (ro
->ro_rt
->rt_ifp
!= NULL
&& ro
->ro_rt
->rt_flags
& RTF_UP
)
255 ro
->ro_rt
= _rtlookup(&ro
->ro_dst
, RTL_REPORTMSG
, ignoreflags
);
259 * Look up the route that matches the given "dst" address.
261 * Route lookup can have the side-effect of creating and returning
262 * a cloned route instead when "dst" matches a cloning route and the
263 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
265 * Any route returned has its reference count incremented.
268 _rtlookup(struct sockaddr
*dst
, boolean_t generate_report
, u_long ignore
)
270 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
277 * Look up route in the radix tree.
279 rt
= (struct rtentry
*) rnh
->rnh_matchaddr((char *)dst
, rnh
);
284 * Handle cloning routes.
286 if ((rt
->rt_flags
& ~ignore
& (RTF_CLONING
| RTF_PRCLONING
)) != 0) {
287 struct rtentry
*clonedroute
;
290 clonedroute
= rt
; /* copy in/copy out parameter */
291 error
= rtrequest(RTM_RESOLVE
, dst
, NULL
, NULL
, 0,
292 &clonedroute
); /* clone the route */
293 if (error
!= 0) { /* cloning failed */
295 rt_dstmsg(RTM_MISS
, dst
, error
);
297 return (rt
); /* return the uncloned route */
299 if (generate_report
) {
300 if (clonedroute
->rt_flags
& RTF_XRESOLVE
)
301 rt_dstmsg(RTM_RESOLVE
, dst
, 0);
303 rt_rtmsg(RTM_ADD
, clonedroute
,
304 clonedroute
->rt_ifp
, 0);
306 return (clonedroute
); /* return cloned route */
310 * Increment the reference count of the matched route and return.
316 rtstat
.rts_unreach
++;
318 rt_dstmsg(RTM_MISS
, dst
, 0);
323 rtfree(struct rtentry
*rt
)
325 KASSERT(rt
->rt_refcnt
> 0, ("rtfree: rt_refcnt %ld", rt
->rt_refcnt
));
328 if (rt
->rt_refcnt
== 0) {
329 struct radix_node_head
*rnh
=
330 rt_tables
[mycpuid
][rt_key(rt
)->sa_family
];
333 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
334 if (!(rt
->rt_flags
& RTF_UP
)) {
335 /* deallocate route */
336 if (rt
->rt_ifa
!= NULL
)
338 if (rt
->rt_parent
!= NULL
)
339 RTFREE(rt
->rt_parent
); /* recursive call! */
347 rtredirect_oncpu(struct sockaddr
*dst
, struct sockaddr
*gateway
,
348 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
350 struct rtentry
*rt
= NULL
;
351 struct rt_addrinfo rtinfo
;
356 /* verify the gateway is directly reachable */
357 if ((ifa
= ifa_ifwithnet(gateway
)) == NULL
) {
363 * If the redirect isn't from our current router for this destination,
364 * it's either old or wrong.
366 if (!(flags
& RTF_DONE
) && /* XXX JH */
367 (rt
= rtpurelookup(dst
)) != NULL
&&
368 (!sa_equal(src
, rt
->rt_gateway
) || rt
->rt_ifa
!= ifa
)) {
374 * If it redirects us to ourselves, we have a routing loop,
375 * perhaps as a result of an interface going down recently.
377 if (ifa_ifwithaddr(gateway
)) {
378 error
= EHOSTUNREACH
;
383 * Create a new entry if the lookup failed or if we got back
384 * a wildcard entry for the default route. This is necessary
385 * for hosts which use routing redirects generated by smart
386 * gateways to dynamically build the routing tables.
390 if ((rt_mask(rt
) != NULL
&& rt_mask(rt
)->sa_len
< 2)) {
395 /* Ignore redirects for directly connected hosts. */
396 if (!(rt
->rt_flags
& RTF_GATEWAY
)) {
397 error
= EHOSTUNREACH
;
401 if (!(rt
->rt_flags
& RTF_HOST
) && (flags
& RTF_HOST
)) {
403 * Changing from a network route to a host route.
404 * Create a new host route rather than smashing the
408 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
409 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
410 rtinfo
.rti_info
[RTAX_DST
] = dst
;
411 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
412 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
413 rtinfo
.rti_flags
= flags
;
414 rtinfo
.rti_ifa
= ifa
;
415 rt
= NULL
; /* copy-in/copy-out parameter */
416 error
= rtrequest1(RTM_ADD
, &rtinfo
, &rt
);
418 flags
= rt
->rt_flags
;
419 stat
= &rtstat
.rts_dynamic
;
422 * Smash the current notion of the gateway to this destination.
423 * Should check about netmask!!!
425 rt
->rt_flags
|= RTF_MODIFIED
;
426 flags
|= RTF_MODIFIED
;
427 rt_setgate(rt
, rt_key(rt
), gateway
);
429 stat
= &rtstat
.rts_newgateway
;
437 rtstat
.rts_badredirect
++;
438 else if (stat
!= NULL
)
446 struct netmsg_rtredirect
{
447 struct lwkt_msg lmsg
;
448 struct sockaddr
*dst
;
449 struct sockaddr
*gateway
;
450 struct sockaddr
*netmask
;
452 struct sockaddr
*src
;
458 * Force a routing table entry to the specified
459 * destination to go through the given gateway.
460 * Normally called as a result of a routing redirect
461 * message from the network layer.
463 * N.B.: must be called at splnet
466 rtredirect(struct sockaddr
*dst
, struct sockaddr
*gateway
,
467 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
469 struct rt_addrinfo rtinfo
;
472 struct netmsg_rtredirect msg
;
474 lwkt_initmsg(&msg
.lmsg
, &curthread
->td_msgport
, 0,
475 lwkt_cmd_func(rtredirect_msghandler
), lwkt_cmd_op_none
);
477 msg
.gateway
= gateway
;
478 msg
.netmask
= netmask
;
481 error
= lwkt_domsg(rtable_portfn(0), &msg
.lmsg
);
483 error
= rtredirect_oncpu(dst
, gateway
, netmask
, flags
, src
);
485 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
486 rtinfo
.rti_info
[RTAX_DST
] = dst
;
487 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
488 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
489 rtinfo
.rti_info
[RTAX_AUTHOR
] = src
;
490 rt_missmsg(RTM_REDIRECT
, &rtinfo
, flags
, error
);
496 rtredirect_msghandler(struct lwkt_msg
*lmsg
)
498 struct netmsg_rtredirect
*msg
= (void *)lmsg
;
501 rtredirect_oncpu(msg
->dst
, msg
->gateway
, msg
->netmask
,
502 msg
->flags
, msg
->src
);
503 nextcpu
= mycpuid
+ 1;
505 lwkt_forwardmsg(rtable_portfn(nextcpu
), &msg
->lmsg
);
507 lwkt_replymsg(&msg
->lmsg
, 0);
514 * Routing table ioctl interface.
517 rtioctl(u_long req
, caddr_t data
, struct ucred
*cred
)
520 /* Multicast goop, grrr... */
521 return mrt_ioctl
? mrt_ioctl(req
, data
) : EOPNOTSUPP
;
528 ifa_ifwithroute(int flags
, struct sockaddr
*dst
, struct sockaddr
*gateway
)
532 if (!(flags
& RTF_GATEWAY
)) {
534 * If we are adding a route to an interface,
535 * and the interface is a point-to-point link,
536 * we should search for the destination
537 * as our clue to the interface. Otherwise
538 * we can use the local address.
541 if (flags
& RTF_HOST
) {
542 ifa
= ifa_ifwithdstaddr(dst
);
545 ifa
= ifa_ifwithaddr(gateway
);
548 * If we are adding a route to a remote net
549 * or host, the gateway may still be on the
550 * other end of a pt to pt link.
552 ifa
= ifa_ifwithdstaddr(gateway
);
555 ifa
= ifa_ifwithnet(gateway
);
559 rt
= rtpurelookup(gateway
);
563 if ((ifa
= rt
->rt_ifa
) == NULL
)
566 if (ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
567 struct ifaddr
*oldifa
= ifa
;
569 ifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
576 static int rt_fixdelete (struct radix_node
*, void *);
577 static int rt_fixchange (struct radix_node
*, void *);
581 struct radix_node_head
*rnh
;
585 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
588 rt_getifa(struct rt_addrinfo
*rtinfo
)
590 struct sockaddr
*gateway
= rtinfo
->rti_info
[RTAX_GATEWAY
];
591 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
592 struct sockaddr
*ifaaddr
= rtinfo
->rti_info
[RTAX_IFA
];
593 int flags
= rtinfo
->rti_flags
;
596 * ifp may be specified by sockaddr_dl
597 * when protocol address is ambiguous.
599 if (rtinfo
->rti_ifp
== NULL
) {
600 struct sockaddr
*ifpaddr
;
602 ifpaddr
= rtinfo
->rti_info
[RTAX_IFP
];
603 if (ifpaddr
!= NULL
&& ifpaddr
->sa_family
== AF_LINK
) {
606 ifa
= ifa_ifwithnet(ifpaddr
);
608 rtinfo
->rti_ifp
= ifa
->ifa_ifp
;
612 if (rtinfo
->rti_ifa
== NULL
&& ifaaddr
!= NULL
)
613 rtinfo
->rti_ifa
= ifa_ifwithaddr(ifaaddr
);
614 if (rtinfo
->rti_ifa
== NULL
) {
617 sa
= ifaaddr
!= NULL
? ifaaddr
:
618 (gateway
!= NULL
? gateway
: dst
);
619 if (sa
!= NULL
&& rtinfo
->rti_ifp
!= NULL
)
620 rtinfo
->rti_ifa
= ifaof_ifpforaddr(sa
, rtinfo
->rti_ifp
);
621 else if (dst
!= NULL
&& gateway
!= NULL
)
622 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, dst
, gateway
);
624 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, sa
, sa
);
626 if (rtinfo
->rti_ifa
== NULL
)
627 return (ENETUNREACH
);
629 if (rtinfo
->rti_ifp
== NULL
)
630 rtinfo
->rti_ifp
= rtinfo
->rti_ifa
->ifa_ifp
;
635 * Do appropriate manipulations of a routing tree given
636 * all the bits of info needed
641 struct sockaddr
*dst
,
642 struct sockaddr
*gateway
,
643 struct sockaddr
*netmask
,
645 struct rtentry
**ret_nrt
)
647 struct rt_addrinfo rtinfo
;
649 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
650 rtinfo
.rti_info
[RTAX_DST
] = dst
;
651 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
652 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
653 rtinfo
.rti_flags
= flags
;
654 return rtrequest1(req
, &rtinfo
, ret_nrt
);
660 struct sockaddr
*dst
,
661 struct sockaddr
*gateway
,
662 struct sockaddr
*netmask
,
665 struct rt_addrinfo rtinfo
;
667 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
668 rtinfo
.rti_info
[RTAX_DST
] = dst
;
669 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
670 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
671 rtinfo
.rti_flags
= flags
;
672 return rtrequest1_global(req
, &rtinfo
, NULL
, NULL
);
678 struct lwkt_msg lmsg
;
680 struct rt_addrinfo
*rtinfo
;
681 rtrequest1_callback_func_t callback
;
688 rtrequest1_global(int req
, struct rt_addrinfo
*rtinfo
,
689 rtrequest1_callback_func_t callback
, void *arg
)
693 struct netmsg_rtq msg
;
695 lwkt_initmsg(&msg
.lmsg
, &curthread
->td_msgport
, 0,
696 lwkt_cmd_func(rtrequest1_msghandler
), lwkt_cmd_op_none
);
697 msg
.lmsg
.ms_error
= -1;
700 msg
.callback
= callback
;
702 error
= lwkt_domsg(rtable_portfn(0), &msg
.lmsg
);
704 struct rtentry
*rt
= NULL
;
706 error
= rtrequest1(req
, rtinfo
, &rt
);
710 callback(req
, error
, rtinfo
, rt
, arg
);
716 * Handle a route table request on the current cpu. Since the route table's
717 * are supposed to be identical on each cpu, an error occuring later in the
718 * message chain is considered system-fatal.
723 rtrequest1_msghandler(struct lwkt_msg
*lmsg
)
725 struct netmsg_rtq
*msg
= (void *)lmsg
;
726 struct rtentry
*rt
= NULL
;
730 error
= rtrequest1(msg
->req
, msg
->rtinfo
, &rt
);
734 msg
->callback(msg
->req
, error
, msg
->rtinfo
, rt
, msg
->arg
);
737 * RTM_DELETE's are propogated even if an error occurs, since a
738 * cloned route might be undergoing deletion and cloned routes
739 * are not necessarily replicated. An overall error is returned
740 * only if no cpus have the route in question.
742 if (msg
->lmsg
.ms_error
< 0 || error
== 0)
743 msg
->lmsg
.ms_error
= error
;
745 nextcpu
= mycpuid
+ 1;
746 if (error
&& msg
->req
!= RTM_DELETE
) {
748 panic("rtrequest1_msghandler: rtrequest table "
749 "error was not on cpu #0: %p", msg
->rtinfo
);
751 lwkt_replymsg(&msg
->lmsg
, error
);
752 } else if (nextcpu
< ncpus
) {
753 lwkt_forwardmsg(rtable_portfn(nextcpu
), &msg
->lmsg
);
755 lwkt_replymsg(&msg
->lmsg
, msg
->lmsg
.ms_error
);
763 rtrequest1(int req
, struct rt_addrinfo
*rtinfo
, struct rtentry
**ret_nrt
)
765 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
767 struct radix_node
*rn
;
768 struct radix_node_head
*rnh
;
770 struct sockaddr
*ndst
;
773 #define gotoerr(x) { error = x ; goto bad; }
777 rt_addrinfo_print(req
, rtinfo
);
782 * Find the correct routing tree to use for this Address Family
784 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
)
785 gotoerr(EAFNOSUPPORT
);
788 * If we are adding a host route then we don't want to put
789 * a netmask in the tree, nor do we want to clone it.
791 if (rtinfo
->rti_flags
& RTF_HOST
) {
792 rtinfo
->rti_info
[RTAX_NETMASK
] = NULL
;
793 rtinfo
->rti_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
798 /* Remove the item from the tree. */
799 rn
= rnh
->rnh_deladdr((char *)rtinfo
->rti_info
[RTAX_DST
],
800 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
804 KASSERT(!(rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)),
805 ("rnh_deladdr returned flags 0x%x", rn
->rn_flags
));
806 rt
= (struct rtentry
*)rn
;
808 /* ref to prevent a deletion race */
811 /* Free any routes cloned from this one. */
812 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
813 rt_mask(rt
) != NULL
) {
814 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
819 if (rt
->rt_gwroute
!= NULL
) {
820 RTFREE(rt
->rt_gwroute
);
821 rt
->rt_gwroute
= NULL
;
825 * NB: RTF_UP must be set during the search above,
826 * because we might delete the last ref, causing
827 * rt to get freed prematurely.
829 rt
->rt_flags
&= ~RTF_UP
;
833 rt_print(rtinfo
, rt
);
836 /* Give the protocol a chance to keep things in sync. */
837 if ((ifa
= rt
->rt_ifa
) && ifa
->ifa_rtrequest
)
838 ifa
->ifa_rtrequest(RTM_DELETE
, rt
, rtinfo
);
841 * If the caller wants it, then it can have it,
842 * but it's up to it to free the rtentry as we won't be
845 KASSERT(rt
->rt_refcnt
>= 0,
846 ("rtrequest1(DELETE): refcnt %ld", rt
->rt_refcnt
));
847 if (ret_nrt
!= NULL
) {
848 /* leave ref intact for return */
851 /* deref / attempt to destroy */
857 if (ret_nrt
== NULL
|| (rt
= *ret_nrt
) == NULL
)
861 rt
->rt_flags
& ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
862 rtinfo
->rti_flags
|= RTF_WASCLONED
;
863 rtinfo
->rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
864 if ((rtinfo
->rti_info
[RTAX_NETMASK
] = rt
->rt_genmask
) == NULL
)
865 rtinfo
->rti_flags
|= RTF_HOST
;
869 KASSERT(!(rtinfo
->rti_flags
& RTF_GATEWAY
) ||
870 rtinfo
->rti_info
[RTAX_GATEWAY
] != NULL
,
871 ("rtrequest: GATEWAY but no gateway"));
873 if (rtinfo
->rti_ifa
== NULL
&& (error
= rt_getifa(rtinfo
)))
875 ifa
= rtinfo
->rti_ifa
;
877 R_Malloc(rt
, struct rtentry
*, sizeof(struct rtentry
));
880 bzero(rt
, sizeof(struct rtentry
));
881 rt
->rt_flags
= RTF_UP
| rtinfo
->rti_flags
;
882 error
= rt_setgate(rt
, dst
, rtinfo
->rti_info
[RTAX_GATEWAY
]);
889 if (rtinfo
->rti_info
[RTAX_NETMASK
] != NULL
)
890 rt_maskedcopy(dst
, ndst
,
891 rtinfo
->rti_info
[RTAX_NETMASK
]);
893 bcopy(dst
, ndst
, dst
->sa_len
);
896 * Note that we now have a reference to the ifa.
897 * This moved from below so that rnh->rnh_addaddr() can
898 * examine the ifa and ifa->ifa_ifp if it so desires.
902 rt
->rt_ifp
= ifa
->ifa_ifp
;
903 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
905 rn
= rnh
->rnh_addaddr((char *)ndst
,
906 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
909 struct rtentry
*oldrt
;
912 * We already have one of these in the tree.
913 * We do a special hack: if the old route was
914 * cloned, then we blow it away and try
915 * re-inserting the new one.
917 oldrt
= rtpurelookup(ndst
);
920 if (oldrt
->rt_flags
& RTF_WASCLONED
) {
921 rtrequest(RTM_DELETE
, rt_key(oldrt
),
924 oldrt
->rt_flags
, NULL
);
925 rn
= rnh
->rnh_addaddr((char *)ndst
,
927 rtinfo
->rti_info
[RTAX_NETMASK
],
934 * If it still failed to go into the tree,
935 * then un-make it (this should be a function).
938 if (rt
->rt_gwroute
!= NULL
)
939 rtfree(rt
->rt_gwroute
);
947 * If we got here from RESOLVE, then we are cloning
948 * so clone the rest, and note that we
949 * are a clone (and increment the parent's references)
951 if (req
== RTM_RESOLVE
) {
952 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
; /* copy metrics */
953 rt
->rt_rmx
.rmx_pksent
= 0; /* reset packet counter */
954 if ((*ret_nrt
)->rt_flags
&
955 (RTF_CLONING
| RTF_PRCLONING
)) {
956 rt
->rt_parent
= *ret_nrt
;
957 (*ret_nrt
)->rt_refcnt
++;
962 * if this protocol has something to add to this then
963 * allow it to do that as well.
965 if (ifa
->ifa_rtrequest
!= NULL
)
966 ifa
->ifa_rtrequest(req
, rt
, rtinfo
);
969 * We repeat the same procedure from rt_setgate() here because
970 * it doesn't fire when we call it there because the node
971 * hasn't been added to the tree yet.
973 if (req
== RTM_ADD
&& !(rt
->rt_flags
& RTF_HOST
) &&
974 rt_mask(rt
) != NULL
) {
975 struct rtfc_arg arg
= { rt
, rnh
};
977 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
984 rt_print(rtinfo
, rt
);
987 * Return the resulting rtentry,
988 * increasing the number of references by one.
990 if (ret_nrt
!= NULL
) {
1002 kprintf("rti %p failed error %d\n", rtinfo
, error
);
1004 kprintf("rti %p succeeded\n", rtinfo
);
1012 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1013 * (i.e., the routes related to it by the operation of cloning). This
1014 * routine is iterated over all potential former-child-routes by way of
1015 * rnh->rnh_walktree_from() above, and those that actually are children of
1016 * the late parent (passed in as VP here) are themselves deleted.
1019 rt_fixdelete(struct radix_node
*rn
, void *vp
)
1021 struct rtentry
*rt
= (struct rtentry
*)rn
;
1022 struct rtentry
*rt0
= vp
;
1024 if (rt
->rt_parent
== rt0
&&
1025 !(rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1026 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1027 rt
->rt_flags
, NULL
);
1033 * This routine is called from rt_setgate() to do the analogous thing for
1034 * adds and changes. There is the added complication in this case of a
1035 * middle insert; i.e., insertion of a new network route between an older
1036 * network route and (cloned) host routes. For this reason, a simple check
1037 * of rt->rt_parent is insufficient; each candidate route must be tested
1038 * against the (mask, value) of the new route (passed as before in vp)
1039 * to see if the new route matches it.
1041 * XXX - it may be possible to do fixdelete() for changes and reserve this
1042 * routine just for adds. I'm not sure why I thought it was necessary to do
1046 static int rtfcdebug
= 0;
1050 rt_fixchange(struct radix_node
*rn
, void *vp
)
1052 struct rtentry
*rt
= (struct rtentry
*)rn
;
1053 struct rtfc_arg
*ap
= vp
;
1054 struct rtentry
*rt0
= ap
->rt0
;
1055 struct radix_node_head
*rnh
= ap
->rnh
;
1056 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
1061 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt
, rt0
);
1064 if (rt
->rt_parent
== NULL
||
1065 (rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1067 if (rtfcdebug
) kprintf("no parent, pinned or cloning\n");
1072 if (rt
->rt_parent
== rt0
) {
1074 if (rtfcdebug
) kprintf("parent match\n");
1076 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1077 rt
->rt_flags
, NULL
);
1081 * There probably is a function somewhere which does this...
1082 * if not, there should be.
1084 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
1086 xk1
= (u_char
*)rt_key(rt0
);
1087 xm1
= (u_char
*)rt_mask(rt0
);
1088 xk2
= (u_char
*)rt_key(rt
);
1090 /* avoid applying a less specific route */
1091 xmp
= (u_char
*)rt_mask(rt
->rt_parent
);
1092 mlen
= rt_key(rt
->rt_parent
)->sa_len
;
1093 if (mlen
> rt_key(rt0
)->sa_len
) {
1096 kprintf("rt_fixchange: inserting a less "
1097 "specific route\n");
1101 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
1102 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
]) {
1105 kprintf("rt_fixchange: inserting a less "
1106 "specific route\n");
1112 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
1113 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
1115 if (rtfcdebug
) kprintf("no match\n");
1122 * OK, this node is a clone, and matches the node currently being
1123 * changed/added under the node's mask. So, get rid of it.
1126 if (rtfcdebug
) kprintf("deleting\n");
1128 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1129 rt
->rt_flags
, NULL
);
1132 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1135 rt_setgate(struct rtentry
*rt0
, struct sockaddr
*dst
, struct sockaddr
*gate
)
1137 char *space
, *oldspace
;
1138 int dlen
= ROUNDUP(dst
->sa_len
), glen
= ROUNDUP(gate
->sa_len
);
1139 struct rtentry
*rt
= rt0
;
1140 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
1143 * A host route with the destination equal to the gateway
1144 * will interfere with keeping LLINFO in the routing
1145 * table, so disallow it.
1147 if (((rt0
->rt_flags
& (RTF_HOST
| RTF_GATEWAY
| RTF_LLINFO
)) ==
1148 (RTF_HOST
| RTF_GATEWAY
)) &&
1149 dst
->sa_len
== gate
->sa_len
&&
1150 sa_equal(dst
, gate
)) {
1152 * The route might already exist if this is an RTM_CHANGE
1153 * or a routing redirect, so try to delete it.
1155 if (rt_key(rt0
) != NULL
)
1156 rtrequest(RTM_DELETE
, rt_key(rt0
), rt0
->rt_gateway
,
1157 rt_mask(rt0
), rt0
->rt_flags
, NULL
);
1158 return EADDRNOTAVAIL
;
1162 * Both dst and gateway are stored in the same malloc'ed chunk
1163 * (If I ever get my hands on....)
1164 * if we need to malloc a new chunk, then keep the old one around
1165 * till we don't need it any more.
1167 if (rt
->rt_gateway
== NULL
|| glen
> ROUNDUP(rt
->rt_gateway
->sa_len
)) {
1168 oldspace
= (char *)rt_key(rt
);
1169 R_Malloc(space
, char *, dlen
+ glen
);
1172 rt
->rt_nodes
->rn_key
= space
;
1174 space
= (char *)rt_key(rt
); /* Just use the old space. */
1178 /* Set the gateway value. */
1179 rt
->rt_gateway
= (struct sockaddr
*)(space
+ dlen
);
1180 bcopy(gate
, rt
->rt_gateway
, glen
);
1182 if (oldspace
!= NULL
) {
1184 * If we allocated a new chunk, preserve the original dst.
1185 * This way, rt_setgate() really just sets the gate
1186 * and leaves the dst field alone.
1188 bcopy(dst
, space
, dlen
);
1193 * If there is already a gwroute, it's now almost definitely wrong
1196 if (rt
->rt_gwroute
!= NULL
) {
1197 RTFREE(rt
->rt_gwroute
);
1198 rt
->rt_gwroute
= NULL
;
1200 if (rt
->rt_flags
& RTF_GATEWAY
) {
1202 * Cloning loop avoidance: In the presence of
1203 * protocol-cloning and bad configuration, it is
1204 * possible to get stuck in bottomless mutual recursion
1205 * (rtrequest rt_setgate rtlookup). We avoid this
1206 * by not allowing protocol-cloning to operate for
1207 * gateways (which is probably the correct choice
1208 * anyway), and avoid the resulting reference loops
1209 * by disallowing any route to run through itself as
1210 * a gateway. This is obviously mandatory when we
1211 * get rt->rt_output().
1213 * This breaks TTCP for hosts outside the gateway! XXX JH
1215 rt
->rt_gwroute
= _rtlookup(gate
, RTL_REPORTMSG
, RTF_PRCLONING
);
1216 if (rt
->rt_gwroute
== rt
) {
1217 rt
->rt_gwroute
= NULL
;
1219 return EDQUOT
; /* failure */
1224 * This isn't going to do anything useful for host routes, so
1225 * don't bother. Also make sure we have a reasonable mask
1226 * (we don't yet have one during adds).
1228 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != NULL
) {
1229 struct rtfc_arg arg
= { rt
, rnh
};
1231 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
1232 (char *)rt_mask(rt
),
1233 rt_fixchange
, &arg
);
1241 struct sockaddr
*src
,
1242 struct sockaddr
*dst
,
1243 struct sockaddr
*netmask
)
1245 u_char
*cp1
= (u_char
*)src
;
1246 u_char
*cp2
= (u_char
*)dst
;
1247 u_char
*cp3
= (u_char
*)netmask
;
1248 u_char
*cplim
= cp2
+ *cp3
;
1249 u_char
*cplim2
= cp2
+ *cp1
;
1251 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
1256 *cp2
++ = *cp1
++ & *cp3
++;
1258 bzero(cp2
, cplim2
- cp2
);
1262 rt_llroute(struct sockaddr
*dst
, struct rtentry
*rt0
, struct rtentry
**drt
)
1264 struct rtentry
*up_rt
, *rt
;
1266 if (!(rt0
->rt_flags
& RTF_UP
)) {
1267 up_rt
= rtlookup(dst
);
1269 return (EHOSTUNREACH
);
1273 if (up_rt
->rt_flags
& RTF_GATEWAY
) {
1274 if (up_rt
->rt_gwroute
== NULL
) {
1275 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1276 if (up_rt
->rt_gwroute
== NULL
)
1277 return (EHOSTUNREACH
);
1278 } else if (!(up_rt
->rt_gwroute
->rt_flags
& RTF_UP
)) {
1279 rtfree(up_rt
->rt_gwroute
);
1280 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1281 if (up_rt
->rt_gwroute
== NULL
)
1282 return (EHOSTUNREACH
);
1284 rt
= up_rt
->rt_gwroute
;
1287 if (rt
->rt_flags
& RTF_REJECT
&&
1288 (rt
->rt_rmx
.rmx_expire
== 0 || /* rt doesn't expire */
1289 time_second
< rt
->rt_rmx
.rmx_expire
)) /* rt not expired */
1290 return (rt
->rt_flags
& RTF_HOST
? EHOSTDOWN
: EHOSTUNREACH
);
1298 * Print out a route table entry
1301 rt_print(struct rt_addrinfo
*rtinfo
, struct rtentry
*rn
)
1303 kprintf("rti %p cpu %d route %p flags %08lx: ",
1304 rtinfo
, mycpuid
, rn
, rn
->rt_flags
);
1305 sockaddr_print(rt_key(rn
));
1307 sockaddr_print(rt_mask(rn
));
1309 sockaddr_print(rn
->rt_gateway
);
1310 kprintf(" ifc \"%s\"", rn
->rt_ifp
? rn
->rt_ifp
->if_dname
: "?");
1311 kprintf(" ifa %p\n", rn
->rt_ifa
);
1315 rt_addrinfo_print(int cmd
, struct rt_addrinfo
*rti
)
1321 if (cmd
== RTM_DELETE
&& route_debug
> 1)
1322 db_print_backtrace();
1336 kprintf("C%02d ", cmd
);
1339 kprintf("rti %p cpu %d ", rti
, mycpuid
);
1340 for (i
= 0; i
< rti
->rti_addrs
; ++i
) {
1341 if (rti
->rti_info
[i
] == NULL
)
1371 kprintf("(?%02d ", i
);
1374 sockaddr_print(rti
->rti_info
[i
]);
1382 sockaddr_print(struct sockaddr
*sa
)
1384 struct sockaddr_in
*sa4
;
1385 struct sockaddr_in6
*sa6
;
1394 len
= sa
->sa_len
- offsetof(struct sockaddr
, sa_data
[0]);
1396 switch(sa
->sa_family
) {
1400 switch(sa
->sa_family
) {
1402 sa4
= (struct sockaddr_in
*)sa
;
1403 kprintf("INET %d %d.%d.%d.%d",
1404 ntohs(sa4
->sin_port
),
1405 (ntohl(sa4
->sin_addr
.s_addr
) >> 24) & 255,
1406 (ntohl(sa4
->sin_addr
.s_addr
) >> 16) & 255,
1407 (ntohl(sa4
->sin_addr
.s_addr
) >> 8) & 255,
1408 (ntohl(sa4
->sin_addr
.s_addr
) >> 0) & 255
1412 sa6
= (struct sockaddr_in6
*)sa
;
1413 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1414 ntohs(sa6
->sin6_port
),
1415 sa6
->sin6_addr
.s6_addr16
[0],
1416 sa6
->sin6_addr
.s6_addr16
[1],
1417 sa6
->sin6_addr
.s6_addr16
[2],
1418 sa6
->sin6_addr
.s6_addr16
[3],
1419 sa6
->sin6_addr
.s6_addr16
[4],
1420 sa6
->sin6_addr
.s6_addr16
[5],
1421 sa6
->sin6_addr
.s6_addr16
[6],
1422 sa6
->sin6_addr
.s6_addr16
[7]
1426 kprintf("AF%d ", sa
->sa_family
);
1427 while (len
> 0 && sa
->sa_data
[len
-1] == 0)
1430 for (i
= 0; i
< len
; ++i
) {
1433 kprintf("%d", (unsigned char)sa
->sa_data
[i
]);
1443 * Set up a routing table entry, normally for an interface.
1446 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
1448 struct sockaddr
*dst
, *deldst
, *netmask
;
1449 struct mbuf
*m
= NULL
;
1450 struct radix_node_head
*rnh
;
1451 struct radix_node
*rn
;
1452 struct rt_addrinfo rtinfo
;
1455 if (flags
& RTF_HOST
) {
1456 dst
= ifa
->ifa_dstaddr
;
1459 dst
= ifa
->ifa_addr
;
1460 netmask
= ifa
->ifa_netmask
;
1463 * If it's a delete, check that if it exists, it's on the correct
1464 * interface or we might scrub a route to another ifa which would
1465 * be confusing at best and possibly worse.
1467 if (cmd
== RTM_DELETE
) {
1469 * It's a delete, so it should already exist..
1470 * If it's a net, mask off the host bits
1471 * (Assuming we have a mask)
1473 if (netmask
!= NULL
) {
1474 m
= m_get(MB_DONTWAIT
, MT_SONAME
);
1477 deldst
= mtod(m
, struct sockaddr
*);
1478 rt_maskedcopy(dst
, deldst
, netmask
);
1482 * Look up an rtentry that is in the routing tree and
1483 * contains the correct info.
1485 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
||
1486 (rn
= rnh
->rnh_lookup((char *)dst
,
1487 (char *)netmask
, rnh
)) == NULL
||
1488 ((struct rtentry
*)rn
)->rt_ifa
!= ifa
||
1489 !sa_equal((struct sockaddr
*)rn
->rn_key
, dst
)) {
1492 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1498 * One would think that as we are deleting, and we know
1499 * it doesn't exist, we could just return at this point
1500 * with an "ELSE" clause, but apparently not..
1502 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1507 * Do the actual request
1509 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1510 rtinfo
.rti_info
[RTAX_DST
] = dst
;
1511 rtinfo
.rti_info
[RTAX_GATEWAY
] = ifa
->ifa_addr
;
1512 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
1513 rtinfo
.rti_flags
= flags
| ifa
->ifa_flags
;
1514 rtinfo
.rti_ifa
= ifa
;
1515 error
= rtrequest1_global(cmd
, &rtinfo
, rtinit_rtrequest_callback
, ifa
);
1522 rtinit_rtrequest_callback(int cmd
, int error
,
1523 struct rt_addrinfo
*rtinfo
, struct rtentry
*rt
,
1526 struct ifaddr
*ifa
= arg
;
1528 if (error
== 0 && rt
) {
1531 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
1534 if (cmd
== RTM_DELETE
) {
1535 if (rt
->rt_refcnt
== 0) {
1543 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1544 SYSINIT(route
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_THIRD
, route_init
, 0);