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) 1980, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)route.c 8.3 (Berkeley) 1/9/95
66 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
67 * $DragonFly: src/sys/net/route.c,v 1.34 2008/06/05 15:29:47 sephe Exp $
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/domain.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/globaldata.h>
81 #include <sys/thread.h>
84 #include <net/route.h>
85 #include <net/netisr.h>
87 #include <netinet/in.h>
88 #include <net/ip_mroute/ip_mroute.h>
90 #include <sys/thread2.h>
91 #include <sys/msgport2.h>
92 #include <net/netmsg2.h>
94 static struct rtstatistics rtstatistics_percpu
[MAXCPU
];
96 #define rtstat rtstatistics_percpu[mycpuid]
98 #define rtstat rtstatistics_percpu[0]
101 struct radix_node_head
*rt_tables
[MAXCPU
][AF_MAX
+1];
102 struct lwkt_port
*rt_ports
[MAXCPU
];
104 static void rt_maskedcopy (struct sockaddr
*, struct sockaddr
*,
106 static void rtable_init(void);
107 static void rtable_service_loop(void *dummy
);
108 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo
*,
109 struct rtentry
*, void *);
112 static void rtredirect_msghandler(struct netmsg
*netmsg
);
113 static void rtrequest1_msghandler(struct netmsg
*netmsg
);
116 SYSCTL_NODE(_net
, OID_AUTO
, route
, CTLFLAG_RW
, 0, "Routing");
119 static int route_debug
= 1;
120 SYSCTL_INT(_net_route
, OID_AUTO
, route_debug
, CTLFLAG_RW
,
121 &route_debug
, 0, "");
125 * Initialize the route table(s) for protocol domains and
126 * create a helper thread which will be responsible for updating
127 * route table entries on each cpu.
135 for (cpu
= 0; cpu
< ncpus
; ++cpu
)
136 bzero(&rtstatistics_percpu
[cpu
], sizeof(struct rtstatistics
));
137 rn_init(); /* initialize all zeroes, all ones, mask table */
139 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
140 lwkt_migratecpu(cpu
);
142 lwkt_create(rtable_service_loop
, NULL
, &rtd
, NULL
,
143 TDF_STOPREQ
, cpu
, "rtable_cpu %d", cpu
);
144 rt_ports
[cpu
] = &rtd
->td_msgport
;
147 lwkt_migratecpu(origcpu
);
155 SLIST_FOREACH(dom
, &domains
, dom_next
) {
156 if (dom
->dom_rtattach
) {
158 (void **)&rt_tables
[mycpuid
][dom
->dom_family
],
165 * Our per-cpu table management protocol thread. All route table operations
166 * are sequentially chained through all cpus starting at cpu #0 in order to
167 * maintain duplicate route tables on each cpu. Having a spearate route
168 * table management thread allows the protocol and interrupt threads to
169 * issue route table changes.
172 rtable_service_loop(void *dummy __unused
)
174 struct netmsg
*netmsg
;
175 thread_t td
= curthread
;
177 while ((netmsg
= lwkt_waitport(&td
->td_msgport
, 0)) != NULL
) {
178 netmsg
->nm_dispatch(netmsg
);
183 * Routing statistics.
187 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS
)
191 for (cpu
= 0; cpu
< ncpus
; ++cpu
) {
192 if ((error
= SYSCTL_OUT(req
, &rtstatistics_percpu
[cpu
],
193 sizeof(struct rtstatistics
))))
195 if ((error
= SYSCTL_IN(req
, &rtstatistics_percpu
[cpu
],
196 sizeof(struct rtstatistics
))))
202 SYSCTL_PROC(_net_route
, OID_AUTO
, stats
, (CTLTYPE_OPAQUE
|CTLFLAG_RW
),
203 0, 0, sysctl_rtstatistics
, "S,rtstatistics", "Routing statistics");
205 SYSCTL_STRUCT(_net_route
, OID_AUTO
, stats
, CTLFLAG_RW
, &rtstat
, rtstatistics
,
206 "Routing statistics");
210 * Packet routing routines.
214 * Look up and fill in the "ro_rt" rtentry field in a route structure given
215 * an address in the "ro_dst" field. Always send a report on a miss and
216 * always clone routes.
219 rtalloc(struct route
*ro
)
221 rtalloc_ign(ro
, 0UL);
225 * Look up and fill in the "ro_rt" rtentry field in a route structure given
226 * an address in the "ro_dst" field. Always send a report on a miss and
227 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
231 rtalloc_ign(struct route
*ro
, u_long ignoreflags
)
233 if (ro
->ro_rt
!= NULL
) {
234 if (ro
->ro_rt
->rt_ifp
!= NULL
&& ro
->ro_rt
->rt_flags
& RTF_UP
)
239 ro
->ro_rt
= _rtlookup(&ro
->ro_dst
, RTL_REPORTMSG
, ignoreflags
);
243 * Look up the route that matches the given "dst" address.
245 * Route lookup can have the side-effect of creating and returning
246 * a cloned route instead when "dst" matches a cloning route and the
247 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
249 * Any route returned has its reference count incremented.
252 _rtlookup(struct sockaddr
*dst
, boolean_t generate_report
, u_long ignore
)
254 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
261 * Look up route in the radix tree.
263 rt
= (struct rtentry
*) rnh
->rnh_matchaddr((char *)dst
, rnh
);
268 * Handle cloning routes.
270 if ((rt
->rt_flags
& ~ignore
& (RTF_CLONING
| RTF_PRCLONING
)) != 0) {
271 struct rtentry
*clonedroute
;
274 clonedroute
= rt
; /* copy in/copy out parameter */
275 error
= rtrequest(RTM_RESOLVE
, dst
, NULL
, NULL
, 0,
276 &clonedroute
); /* clone the route */
277 if (error
!= 0) { /* cloning failed */
279 rt_dstmsg(RTM_MISS
, dst
, error
);
281 return (rt
); /* return the uncloned route */
283 if (generate_report
) {
284 if (clonedroute
->rt_flags
& RTF_XRESOLVE
)
285 rt_dstmsg(RTM_RESOLVE
, dst
, 0);
287 rt_rtmsg(RTM_ADD
, clonedroute
,
288 clonedroute
->rt_ifp
, 0);
290 return (clonedroute
); /* return cloned route */
294 * Increment the reference count of the matched route and return.
300 rtstat
.rts_unreach
++;
302 rt_dstmsg(RTM_MISS
, dst
, 0);
307 rtfree(struct rtentry
*rt
)
309 KASSERT(rt
->rt_refcnt
> 0, ("rtfree: rt_refcnt %ld", rt
->rt_refcnt
));
312 if (rt
->rt_refcnt
== 0) {
313 struct radix_node_head
*rnh
=
314 rt_tables
[mycpuid
][rt_key(rt
)->sa_family
];
317 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
318 if (!(rt
->rt_flags
& RTF_UP
)) {
319 /* deallocate route */
320 if (rt
->rt_ifa
!= NULL
)
322 if (rt
->rt_parent
!= NULL
)
323 RTFREE(rt
->rt_parent
); /* recursive call! */
331 rtredirect_oncpu(struct sockaddr
*dst
, struct sockaddr
*gateway
,
332 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
334 struct rtentry
*rt
= NULL
;
335 struct rt_addrinfo rtinfo
;
340 /* verify the gateway is directly reachable */
341 if ((ifa
= ifa_ifwithnet(gateway
)) == NULL
) {
347 * If the redirect isn't from our current router for this destination,
348 * it's either old or wrong.
350 if (!(flags
& RTF_DONE
) && /* XXX JH */
351 (rt
= rtpurelookup(dst
)) != NULL
&&
352 (!sa_equal(src
, rt
->rt_gateway
) || rt
->rt_ifa
!= ifa
)) {
358 * If it redirects us to ourselves, we have a routing loop,
359 * perhaps as a result of an interface going down recently.
361 if (ifa_ifwithaddr(gateway
)) {
362 error
= EHOSTUNREACH
;
367 * Create a new entry if the lookup failed or if we got back
368 * a wildcard entry for the default route. This is necessary
369 * for hosts which use routing redirects generated by smart
370 * gateways to dynamically build the routing tables.
374 if ((rt_mask(rt
) != NULL
&& rt_mask(rt
)->sa_len
< 2)) {
379 /* Ignore redirects for directly connected hosts. */
380 if (!(rt
->rt_flags
& RTF_GATEWAY
)) {
381 error
= EHOSTUNREACH
;
385 if (!(rt
->rt_flags
& RTF_HOST
) && (flags
& RTF_HOST
)) {
387 * Changing from a network route to a host route.
388 * Create a new host route rather than smashing the
392 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
393 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
394 rtinfo
.rti_info
[RTAX_DST
] = dst
;
395 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
396 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
397 rtinfo
.rti_flags
= flags
;
398 rtinfo
.rti_ifa
= ifa
;
399 rt
= NULL
; /* copy-in/copy-out parameter */
400 error
= rtrequest1(RTM_ADD
, &rtinfo
, &rt
);
402 flags
= rt
->rt_flags
;
403 stat
= &rtstat
.rts_dynamic
;
406 * Smash the current notion of the gateway to this destination.
407 * Should check about netmask!!!
409 rt
->rt_flags
|= RTF_MODIFIED
;
410 flags
|= RTF_MODIFIED
;
411 rt_setgate(rt
, rt_key(rt
), gateway
);
413 stat
= &rtstat
.rts_newgateway
;
421 rtstat
.rts_badredirect
++;
422 else if (stat
!= NULL
)
430 struct netmsg_rtredirect
{
431 struct netmsg netmsg
;
432 struct sockaddr
*dst
;
433 struct sockaddr
*gateway
;
434 struct sockaddr
*netmask
;
436 struct sockaddr
*src
;
442 * Force a routing table entry to the specified
443 * destination to go through the given gateway.
444 * Normally called as a result of a routing redirect
445 * message from the network layer.
447 * N.B.: must be called at splnet
450 rtredirect(struct sockaddr
*dst
, struct sockaddr
*gateway
,
451 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
453 struct rt_addrinfo rtinfo
;
456 struct netmsg_rtredirect msg
;
458 netmsg_init(&msg
.netmsg
, &curthread
->td_msgport
, 0,
459 rtredirect_msghandler
);
461 msg
.gateway
= gateway
;
462 msg
.netmask
= netmask
;
465 error
= lwkt_domsg(rtable_portfn(0), &msg
.netmsg
.nm_lmsg
, 0);
467 error
= rtredirect_oncpu(dst
, gateway
, netmask
, flags
, src
);
469 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
470 rtinfo
.rti_info
[RTAX_DST
] = dst
;
471 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
472 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
473 rtinfo
.rti_info
[RTAX_AUTHOR
] = src
;
474 rt_missmsg(RTM_REDIRECT
, &rtinfo
, flags
, error
);
480 rtredirect_msghandler(struct netmsg
*netmsg
)
482 struct netmsg_rtredirect
*msg
= (void *)netmsg
;
485 rtredirect_oncpu(msg
->dst
, msg
->gateway
, msg
->netmask
,
486 msg
->flags
, msg
->src
);
487 nextcpu
= mycpuid
+ 1;
489 lwkt_forwardmsg(rtable_portfn(nextcpu
), &netmsg
->nm_lmsg
);
491 lwkt_replymsg(&netmsg
->nm_lmsg
, 0);
497 * Routing table ioctl interface.
500 rtioctl(u_long req
, caddr_t data
, struct ucred
*cred
)
503 /* Multicast goop, grrr... */
504 return mrt_ioctl
? mrt_ioctl(req
, data
) : EOPNOTSUPP
;
511 ifa_ifwithroute(int flags
, struct sockaddr
*dst
, struct sockaddr
*gateway
)
515 if (!(flags
& RTF_GATEWAY
)) {
517 * If we are adding a route to an interface,
518 * and the interface is a point-to-point link,
519 * we should search for the destination
520 * as our clue to the interface. Otherwise
521 * we can use the local address.
524 if (flags
& RTF_HOST
) {
525 ifa
= ifa_ifwithdstaddr(dst
);
528 ifa
= ifa_ifwithaddr(gateway
);
531 * If we are adding a route to a remote net
532 * or host, the gateway may still be on the
533 * other end of a pt to pt link.
535 ifa
= ifa_ifwithdstaddr(gateway
);
538 ifa
= ifa_ifwithnet(gateway
);
542 rt
= rtpurelookup(gateway
);
546 if ((ifa
= rt
->rt_ifa
) == NULL
)
549 if (ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
550 struct ifaddr
*oldifa
= ifa
;
552 ifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
559 static int rt_fixdelete (struct radix_node
*, void *);
560 static int rt_fixchange (struct radix_node
*, void *);
564 struct radix_node_head
*rnh
;
568 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
571 rt_getifa(struct rt_addrinfo
*rtinfo
)
573 struct sockaddr
*gateway
= rtinfo
->rti_info
[RTAX_GATEWAY
];
574 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
575 struct sockaddr
*ifaaddr
= rtinfo
->rti_info
[RTAX_IFA
];
576 int flags
= rtinfo
->rti_flags
;
579 * ifp may be specified by sockaddr_dl
580 * when protocol address is ambiguous.
582 if (rtinfo
->rti_ifp
== NULL
) {
583 struct sockaddr
*ifpaddr
;
585 ifpaddr
= rtinfo
->rti_info
[RTAX_IFP
];
586 if (ifpaddr
!= NULL
&& ifpaddr
->sa_family
== AF_LINK
) {
589 ifa
= ifa_ifwithnet(ifpaddr
);
591 rtinfo
->rti_ifp
= ifa
->ifa_ifp
;
595 if (rtinfo
->rti_ifa
== NULL
&& ifaaddr
!= NULL
)
596 rtinfo
->rti_ifa
= ifa_ifwithaddr(ifaaddr
);
597 if (rtinfo
->rti_ifa
== NULL
) {
600 sa
= ifaaddr
!= NULL
? ifaaddr
:
601 (gateway
!= NULL
? gateway
: dst
);
602 if (sa
!= NULL
&& rtinfo
->rti_ifp
!= NULL
)
603 rtinfo
->rti_ifa
= ifaof_ifpforaddr(sa
, rtinfo
->rti_ifp
);
604 else if (dst
!= NULL
&& gateway
!= NULL
)
605 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, dst
, gateway
);
607 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, sa
, sa
);
609 if (rtinfo
->rti_ifa
== NULL
)
610 return (ENETUNREACH
);
612 if (rtinfo
->rti_ifp
== NULL
)
613 rtinfo
->rti_ifp
= rtinfo
->rti_ifa
->ifa_ifp
;
618 * Do appropriate manipulations of a routing tree given
619 * all the bits of info needed
624 struct sockaddr
*dst
,
625 struct sockaddr
*gateway
,
626 struct sockaddr
*netmask
,
628 struct rtentry
**ret_nrt
)
630 struct rt_addrinfo rtinfo
;
632 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
633 rtinfo
.rti_info
[RTAX_DST
] = dst
;
634 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
635 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
636 rtinfo
.rti_flags
= flags
;
637 return rtrequest1(req
, &rtinfo
, ret_nrt
);
643 struct sockaddr
*dst
,
644 struct sockaddr
*gateway
,
645 struct sockaddr
*netmask
,
648 struct rt_addrinfo rtinfo
;
650 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
651 rtinfo
.rti_info
[RTAX_DST
] = dst
;
652 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
653 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
654 rtinfo
.rti_flags
= flags
;
655 return rtrequest1_global(req
, &rtinfo
, NULL
, NULL
);
661 struct netmsg netmsg
;
663 struct rt_addrinfo
*rtinfo
;
664 rtrequest1_callback_func_t callback
;
671 rtrequest1_global(int req
, struct rt_addrinfo
*rtinfo
,
672 rtrequest1_callback_func_t callback
, void *arg
)
676 struct netmsg_rtq msg
;
678 netmsg_init(&msg
.netmsg
, &curthread
->td_msgport
, 0,
679 rtrequest1_msghandler
);
680 msg
.netmsg
.nm_lmsg
.ms_error
= -1;
683 msg
.callback
= callback
;
685 error
= lwkt_domsg(rtable_portfn(0), &msg
.netmsg
.nm_lmsg
, 0);
687 struct rtentry
*rt
= NULL
;
689 error
= rtrequest1(req
, rtinfo
, &rt
);
693 callback(req
, error
, rtinfo
, rt
, arg
);
699 * Handle a route table request on the current cpu. Since the route table's
700 * are supposed to be identical on each cpu, an error occuring later in the
701 * message chain is considered system-fatal.
706 rtrequest1_msghandler(struct netmsg
*netmsg
)
708 struct netmsg_rtq
*msg
= (void *)netmsg
;
709 struct rtentry
*rt
= NULL
;
713 error
= rtrequest1(msg
->req
, msg
->rtinfo
, &rt
);
717 msg
->callback(msg
->req
, error
, msg
->rtinfo
, rt
, msg
->arg
);
720 * RTM_DELETE's are propogated even if an error occurs, since a
721 * cloned route might be undergoing deletion and cloned routes
722 * are not necessarily replicated. An overall error is returned
723 * only if no cpus have the route in question.
725 if (msg
->netmsg
.nm_lmsg
.ms_error
< 0 || error
== 0)
726 msg
->netmsg
.nm_lmsg
.ms_error
= error
;
728 nextcpu
= mycpuid
+ 1;
729 if (error
&& msg
->req
!= RTM_DELETE
) {
731 panic("rtrequest1_msghandler: rtrequest table "
732 "error was not on cpu #0: %p", msg
->rtinfo
);
734 lwkt_replymsg(&msg
->netmsg
.nm_lmsg
, error
);
735 } else if (nextcpu
< ncpus
) {
736 lwkt_forwardmsg(rtable_portfn(nextcpu
), &msg
->netmsg
.nm_lmsg
);
738 lwkt_replymsg(&msg
->netmsg
.nm_lmsg
,
739 msg
->netmsg
.nm_lmsg
.ms_error
);
746 rtrequest1(int req
, struct rt_addrinfo
*rtinfo
, struct rtentry
**ret_nrt
)
748 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
750 struct radix_node
*rn
;
751 struct radix_node_head
*rnh
;
753 struct sockaddr
*ndst
;
756 #define gotoerr(x) { error = x ; goto bad; }
760 rt_addrinfo_print(req
, rtinfo
);
765 * Find the correct routing tree to use for this Address Family
767 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
)
768 gotoerr(EAFNOSUPPORT
);
771 * If we are adding a host route then we don't want to put
772 * a netmask in the tree, nor do we want to clone it.
774 if (rtinfo
->rti_flags
& RTF_HOST
) {
775 rtinfo
->rti_info
[RTAX_NETMASK
] = NULL
;
776 rtinfo
->rti_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
781 /* Remove the item from the tree. */
782 rn
= rnh
->rnh_deladdr((char *)rtinfo
->rti_info
[RTAX_DST
],
783 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
787 KASSERT(!(rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)),
788 ("rnh_deladdr returned flags 0x%x", rn
->rn_flags
));
789 rt
= (struct rtentry
*)rn
;
791 /* ref to prevent a deletion race */
794 /* Free any routes cloned from this one. */
795 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
796 rt_mask(rt
) != NULL
) {
797 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
802 if (rt
->rt_gwroute
!= NULL
) {
803 RTFREE(rt
->rt_gwroute
);
804 rt
->rt_gwroute
= NULL
;
808 * NB: RTF_UP must be set during the search above,
809 * because we might delete the last ref, causing
810 * rt to get freed prematurely.
812 rt
->rt_flags
&= ~RTF_UP
;
816 rt_print(rtinfo
, rt
);
819 /* Give the protocol a chance to keep things in sync. */
820 if ((ifa
= rt
->rt_ifa
) && ifa
->ifa_rtrequest
)
821 ifa
->ifa_rtrequest(RTM_DELETE
, rt
, rtinfo
);
824 * If the caller wants it, then it can have it,
825 * but it's up to it to free the rtentry as we won't be
828 KASSERT(rt
->rt_refcnt
>= 0,
829 ("rtrequest1(DELETE): refcnt %ld", rt
->rt_refcnt
));
830 if (ret_nrt
!= NULL
) {
831 /* leave ref intact for return */
834 /* deref / attempt to destroy */
840 if (ret_nrt
== NULL
|| (rt
= *ret_nrt
) == NULL
)
844 rt
->rt_flags
& ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
845 rtinfo
->rti_flags
|= RTF_WASCLONED
;
846 rtinfo
->rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
847 if ((rtinfo
->rti_info
[RTAX_NETMASK
] = rt
->rt_genmask
) == NULL
)
848 rtinfo
->rti_flags
|= RTF_HOST
;
852 KASSERT(!(rtinfo
->rti_flags
& RTF_GATEWAY
) ||
853 rtinfo
->rti_info
[RTAX_GATEWAY
] != NULL
,
854 ("rtrequest: GATEWAY but no gateway"));
856 if (rtinfo
->rti_ifa
== NULL
&& (error
= rt_getifa(rtinfo
)))
858 ifa
= rtinfo
->rti_ifa
;
860 R_Malloc(rt
, struct rtentry
*, sizeof(struct rtentry
));
863 bzero(rt
, sizeof(struct rtentry
));
864 rt
->rt_flags
= RTF_UP
| rtinfo
->rti_flags
;
865 rt
->rt_cpuid
= mycpuid
;
866 error
= rt_setgate(rt
, dst
, rtinfo
->rti_info
[RTAX_GATEWAY
]);
873 if (rtinfo
->rti_info
[RTAX_NETMASK
] != NULL
)
874 rt_maskedcopy(dst
, ndst
,
875 rtinfo
->rti_info
[RTAX_NETMASK
]);
877 bcopy(dst
, ndst
, dst
->sa_len
);
880 * Note that we now have a reference to the ifa.
881 * This moved from below so that rnh->rnh_addaddr() can
882 * examine the ifa and ifa->ifa_ifp if it so desires.
886 rt
->rt_ifp
= ifa
->ifa_ifp
;
887 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
889 rn
= rnh
->rnh_addaddr((char *)ndst
,
890 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
893 struct rtentry
*oldrt
;
896 * We already have one of these in the tree.
897 * We do a special hack: if the old route was
898 * cloned, then we blow it away and try
899 * re-inserting the new one.
901 oldrt
= rtpurelookup(ndst
);
904 if (oldrt
->rt_flags
& RTF_WASCLONED
) {
905 rtrequest(RTM_DELETE
, rt_key(oldrt
),
908 oldrt
->rt_flags
, NULL
);
909 rn
= rnh
->rnh_addaddr((char *)ndst
,
911 rtinfo
->rti_info
[RTAX_NETMASK
],
918 * If it still failed to go into the tree,
919 * then un-make it (this should be a function).
922 if (rt
->rt_gwroute
!= NULL
)
923 rtfree(rt
->rt_gwroute
);
931 * If we got here from RESOLVE, then we are cloning
932 * so clone the rest, and note that we
933 * are a clone (and increment the parent's references)
935 if (req
== RTM_RESOLVE
) {
936 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
; /* copy metrics */
937 rt
->rt_rmx
.rmx_pksent
= 0; /* reset packet counter */
938 if ((*ret_nrt
)->rt_flags
&
939 (RTF_CLONING
| RTF_PRCLONING
)) {
940 rt
->rt_parent
= *ret_nrt
;
941 (*ret_nrt
)->rt_refcnt
++;
946 * if this protocol has something to add to this then
947 * allow it to do that as well.
949 if (ifa
->ifa_rtrequest
!= NULL
)
950 ifa
->ifa_rtrequest(req
, rt
, rtinfo
);
953 * We repeat the same procedure from rt_setgate() here because
954 * it doesn't fire when we call it there because the node
955 * hasn't been added to the tree yet.
957 if (req
== RTM_ADD
&& !(rt
->rt_flags
& RTF_HOST
) &&
958 rt_mask(rt
) != NULL
) {
959 struct rtfc_arg arg
= { rt
, rnh
};
961 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
968 rt_print(rtinfo
, rt
);
971 * Return the resulting rtentry,
972 * increasing the number of references by one.
974 if (ret_nrt
!= NULL
) {
986 kprintf("rti %p failed error %d\n", rtinfo
, error
);
988 kprintf("rti %p succeeded\n", rtinfo
);
996 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
997 * (i.e., the routes related to it by the operation of cloning). This
998 * routine is iterated over all potential former-child-routes by way of
999 * rnh->rnh_walktree_from() above, and those that actually are children of
1000 * the late parent (passed in as VP here) are themselves deleted.
1003 rt_fixdelete(struct radix_node
*rn
, void *vp
)
1005 struct rtentry
*rt
= (struct rtentry
*)rn
;
1006 struct rtentry
*rt0
= vp
;
1008 if (rt
->rt_parent
== rt0
&&
1009 !(rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1010 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1011 rt
->rt_flags
, NULL
);
1017 * This routine is called from rt_setgate() to do the analogous thing for
1018 * adds and changes. There is the added complication in this case of a
1019 * middle insert; i.e., insertion of a new network route between an older
1020 * network route and (cloned) host routes. For this reason, a simple check
1021 * of rt->rt_parent is insufficient; each candidate route must be tested
1022 * against the (mask, value) of the new route (passed as before in vp)
1023 * to see if the new route matches it.
1025 * XXX - it may be possible to do fixdelete() for changes and reserve this
1026 * routine just for adds. I'm not sure why I thought it was necessary to do
1030 static int rtfcdebug
= 0;
1034 rt_fixchange(struct radix_node
*rn
, void *vp
)
1036 struct rtentry
*rt
= (struct rtentry
*)rn
;
1037 struct rtfc_arg
*ap
= vp
;
1038 struct rtentry
*rt0
= ap
->rt0
;
1039 struct radix_node_head
*rnh
= ap
->rnh
;
1040 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
1045 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt
, rt0
);
1048 if (rt
->rt_parent
== NULL
||
1049 (rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1051 if (rtfcdebug
) kprintf("no parent, pinned or cloning\n");
1056 if (rt
->rt_parent
== rt0
) {
1058 if (rtfcdebug
) kprintf("parent match\n");
1060 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1061 rt
->rt_flags
, NULL
);
1065 * There probably is a function somewhere which does this...
1066 * if not, there should be.
1068 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
1070 xk1
= (u_char
*)rt_key(rt0
);
1071 xm1
= (u_char
*)rt_mask(rt0
);
1072 xk2
= (u_char
*)rt_key(rt
);
1074 /* avoid applying a less specific route */
1075 xmp
= (u_char
*)rt_mask(rt
->rt_parent
);
1076 mlen
= rt_key(rt
->rt_parent
)->sa_len
;
1077 if (mlen
> rt_key(rt0
)->sa_len
) {
1080 kprintf("rt_fixchange: inserting a less "
1081 "specific route\n");
1085 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
1086 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
]) {
1089 kprintf("rt_fixchange: inserting a less "
1090 "specific route\n");
1096 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
1097 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
1099 if (rtfcdebug
) kprintf("no match\n");
1106 * OK, this node is a clone, and matches the node currently being
1107 * changed/added under the node's mask. So, get rid of it.
1110 if (rtfcdebug
) kprintf("deleting\n");
1112 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1113 rt
->rt_flags
, NULL
);
1116 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1119 rt_setgate(struct rtentry
*rt0
, struct sockaddr
*dst
, struct sockaddr
*gate
)
1121 char *space
, *oldspace
;
1122 int dlen
= ROUNDUP(dst
->sa_len
), glen
= ROUNDUP(gate
->sa_len
);
1123 struct rtentry
*rt
= rt0
;
1124 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
1127 * A host route with the destination equal to the gateway
1128 * will interfere with keeping LLINFO in the routing
1129 * table, so disallow it.
1131 if (((rt0
->rt_flags
& (RTF_HOST
| RTF_GATEWAY
| RTF_LLINFO
)) ==
1132 (RTF_HOST
| RTF_GATEWAY
)) &&
1133 dst
->sa_len
== gate
->sa_len
&&
1134 sa_equal(dst
, gate
)) {
1136 * The route might already exist if this is an RTM_CHANGE
1137 * or a routing redirect, so try to delete it.
1139 if (rt_key(rt0
) != NULL
)
1140 rtrequest(RTM_DELETE
, rt_key(rt0
), rt0
->rt_gateway
,
1141 rt_mask(rt0
), rt0
->rt_flags
, NULL
);
1142 return EADDRNOTAVAIL
;
1146 * Both dst and gateway are stored in the same malloc'ed chunk
1147 * (If I ever get my hands on....)
1148 * if we need to malloc a new chunk, then keep the old one around
1149 * till we don't need it any more.
1151 if (rt
->rt_gateway
== NULL
|| glen
> ROUNDUP(rt
->rt_gateway
->sa_len
)) {
1152 oldspace
= (char *)rt_key(rt
);
1153 R_Malloc(space
, char *, dlen
+ glen
);
1156 rt
->rt_nodes
->rn_key
= space
;
1158 space
= (char *)rt_key(rt
); /* Just use the old space. */
1162 /* Set the gateway value. */
1163 rt
->rt_gateway
= (struct sockaddr
*)(space
+ dlen
);
1164 bcopy(gate
, rt
->rt_gateway
, glen
);
1166 if (oldspace
!= NULL
) {
1168 * If we allocated a new chunk, preserve the original dst.
1169 * This way, rt_setgate() really just sets the gate
1170 * and leaves the dst field alone.
1172 bcopy(dst
, space
, dlen
);
1177 * If there is already a gwroute, it's now almost definitely wrong
1180 if (rt
->rt_gwroute
!= NULL
) {
1181 RTFREE(rt
->rt_gwroute
);
1182 rt
->rt_gwroute
= NULL
;
1184 if (rt
->rt_flags
& RTF_GATEWAY
) {
1186 * Cloning loop avoidance: In the presence of
1187 * protocol-cloning and bad configuration, it is
1188 * possible to get stuck in bottomless mutual recursion
1189 * (rtrequest rt_setgate rtlookup). We avoid this
1190 * by not allowing protocol-cloning to operate for
1191 * gateways (which is probably the correct choice
1192 * anyway), and avoid the resulting reference loops
1193 * by disallowing any route to run through itself as
1194 * a gateway. This is obviously mandatory when we
1195 * get rt->rt_output().
1197 * This breaks TTCP for hosts outside the gateway! XXX JH
1199 rt
->rt_gwroute
= _rtlookup(gate
, RTL_REPORTMSG
, RTF_PRCLONING
);
1200 if (rt
->rt_gwroute
== rt
) {
1201 rt
->rt_gwroute
= NULL
;
1203 return EDQUOT
; /* failure */
1208 * This isn't going to do anything useful for host routes, so
1209 * don't bother. Also make sure we have a reasonable mask
1210 * (we don't yet have one during adds).
1212 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != NULL
) {
1213 struct rtfc_arg arg
= { rt
, rnh
};
1215 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
1216 (char *)rt_mask(rt
),
1217 rt_fixchange
, &arg
);
1225 struct sockaddr
*src
,
1226 struct sockaddr
*dst
,
1227 struct sockaddr
*netmask
)
1229 u_char
*cp1
= (u_char
*)src
;
1230 u_char
*cp2
= (u_char
*)dst
;
1231 u_char
*cp3
= (u_char
*)netmask
;
1232 u_char
*cplim
= cp2
+ *cp3
;
1233 u_char
*cplim2
= cp2
+ *cp1
;
1235 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
1240 *cp2
++ = *cp1
++ & *cp3
++;
1242 bzero(cp2
, cplim2
- cp2
);
1246 rt_llroute(struct sockaddr
*dst
, struct rtentry
*rt0
, struct rtentry
**drt
)
1248 struct rtentry
*up_rt
, *rt
;
1250 if (!(rt0
->rt_flags
& RTF_UP
)) {
1251 up_rt
= rtlookup(dst
);
1253 return (EHOSTUNREACH
);
1257 if (up_rt
->rt_flags
& RTF_GATEWAY
) {
1258 if (up_rt
->rt_gwroute
== NULL
) {
1259 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1260 if (up_rt
->rt_gwroute
== NULL
)
1261 return (EHOSTUNREACH
);
1262 } else if (!(up_rt
->rt_gwroute
->rt_flags
& RTF_UP
)) {
1263 rtfree(up_rt
->rt_gwroute
);
1264 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1265 if (up_rt
->rt_gwroute
== NULL
)
1266 return (EHOSTUNREACH
);
1268 rt
= up_rt
->rt_gwroute
;
1271 if (rt
->rt_flags
& RTF_REJECT
&&
1272 (rt
->rt_rmx
.rmx_expire
== 0 || /* rt doesn't expire */
1273 time_second
< rt
->rt_rmx
.rmx_expire
)) /* rt not expired */
1274 return (rt
->rt_flags
& RTF_HOST
? EHOSTDOWN
: EHOSTUNREACH
);
1282 * Print out a route table entry
1285 rt_print(struct rt_addrinfo
*rtinfo
, struct rtentry
*rn
)
1287 kprintf("rti %p cpu %d route %p flags %08lx: ",
1288 rtinfo
, mycpuid
, rn
, rn
->rt_flags
);
1289 sockaddr_print(rt_key(rn
));
1291 sockaddr_print(rt_mask(rn
));
1293 sockaddr_print(rn
->rt_gateway
);
1294 kprintf(" ifc \"%s\"", rn
->rt_ifp
? rn
->rt_ifp
->if_dname
: "?");
1295 kprintf(" ifa %p\n", rn
->rt_ifa
);
1299 rt_addrinfo_print(int cmd
, struct rt_addrinfo
*rti
)
1305 if (cmd
== RTM_DELETE
&& route_debug
> 1)
1306 db_print_backtrace();
1320 kprintf("C%02d ", cmd
);
1323 kprintf("rti %p cpu %d ", rti
, mycpuid
);
1324 for (i
= 0; i
< rti
->rti_addrs
; ++i
) {
1325 if (rti
->rti_info
[i
] == NULL
)
1355 kprintf("(?%02d ", i
);
1358 sockaddr_print(rti
->rti_info
[i
]);
1366 sockaddr_print(struct sockaddr
*sa
)
1368 struct sockaddr_in
*sa4
;
1369 struct sockaddr_in6
*sa6
;
1378 len
= sa
->sa_len
- offsetof(struct sockaddr
, sa_data
[0]);
1380 switch(sa
->sa_family
) {
1384 switch(sa
->sa_family
) {
1386 sa4
= (struct sockaddr_in
*)sa
;
1387 kprintf("INET %d %d.%d.%d.%d",
1388 ntohs(sa4
->sin_port
),
1389 (ntohl(sa4
->sin_addr
.s_addr
) >> 24) & 255,
1390 (ntohl(sa4
->sin_addr
.s_addr
) >> 16) & 255,
1391 (ntohl(sa4
->sin_addr
.s_addr
) >> 8) & 255,
1392 (ntohl(sa4
->sin_addr
.s_addr
) >> 0) & 255
1396 sa6
= (struct sockaddr_in6
*)sa
;
1397 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1398 ntohs(sa6
->sin6_port
),
1399 sa6
->sin6_addr
.s6_addr16
[0],
1400 sa6
->sin6_addr
.s6_addr16
[1],
1401 sa6
->sin6_addr
.s6_addr16
[2],
1402 sa6
->sin6_addr
.s6_addr16
[3],
1403 sa6
->sin6_addr
.s6_addr16
[4],
1404 sa6
->sin6_addr
.s6_addr16
[5],
1405 sa6
->sin6_addr
.s6_addr16
[6],
1406 sa6
->sin6_addr
.s6_addr16
[7]
1410 kprintf("AF%d ", sa
->sa_family
);
1411 while (len
> 0 && sa
->sa_data
[len
-1] == 0)
1414 for (i
= 0; i
< len
; ++i
) {
1417 kprintf("%d", (unsigned char)sa
->sa_data
[i
]);
1427 * Set up a routing table entry, normally for an interface.
1430 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
1432 struct sockaddr
*dst
, *deldst
, *netmask
;
1433 struct mbuf
*m
= NULL
;
1434 struct radix_node_head
*rnh
;
1435 struct radix_node
*rn
;
1436 struct rt_addrinfo rtinfo
;
1439 if (flags
& RTF_HOST
) {
1440 dst
= ifa
->ifa_dstaddr
;
1443 dst
= ifa
->ifa_addr
;
1444 netmask
= ifa
->ifa_netmask
;
1447 * If it's a delete, check that if it exists, it's on the correct
1448 * interface or we might scrub a route to another ifa which would
1449 * be confusing at best and possibly worse.
1451 if (cmd
== RTM_DELETE
) {
1453 * It's a delete, so it should already exist..
1454 * If it's a net, mask off the host bits
1455 * (Assuming we have a mask)
1457 if (netmask
!= NULL
) {
1458 m
= m_get(MB_DONTWAIT
, MT_SONAME
);
1462 deldst
= mtod(m
, struct sockaddr
*);
1463 rt_maskedcopy(dst
, deldst
, netmask
);
1467 * Look up an rtentry that is in the routing tree and
1468 * contains the correct info.
1470 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
||
1471 (rn
= rnh
->rnh_lookup((char *)dst
,
1472 (char *)netmask
, rnh
)) == NULL
||
1473 ((struct rtentry
*)rn
)->rt_ifa
!= ifa
||
1474 !sa_equal((struct sockaddr
*)rn
->rn_key
, dst
)) {
1477 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1483 * One would think that as we are deleting, and we know
1484 * it doesn't exist, we could just return at this point
1485 * with an "ELSE" clause, but apparently not..
1487 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1492 * Do the actual request
1494 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1495 rtinfo
.rti_info
[RTAX_DST
] = dst
;
1496 rtinfo
.rti_info
[RTAX_GATEWAY
] = ifa
->ifa_addr
;
1497 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
1498 rtinfo
.rti_flags
= flags
| ifa
->ifa_flags
;
1499 rtinfo
.rti_ifa
= ifa
;
1500 error
= rtrequest1_global(cmd
, &rtinfo
, rtinit_rtrequest_callback
, ifa
);
1507 rtinit_rtrequest_callback(int cmd
, int error
,
1508 struct rt_addrinfo
*rtinfo
, struct rtentry
*rt
,
1511 struct ifaddr
*ifa
= arg
;
1513 if (error
== 0 && rt
) {
1516 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
1519 if (cmd
== RTM_DELETE
) {
1520 if (rt
->rt_refcnt
== 0) {
1528 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1529 SYSINIT(route
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_THIRD
, route_init
, 0);