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. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)route.c 8.3 (Berkeley) 1/9/95
62 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
72 #include <sys/socket.h>
73 #include <sys/domain.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/globaldata.h>
77 #include <sys/thread.h>
80 #include <net/route.h>
81 #include <net/netisr.h>
83 #include <netinet/in.h>
84 #include <net/ip_mroute/ip_mroute.h>
86 #include <sys/thread2.h>
87 #include <sys/msgport2.h>
88 #include <net/netmsg2.h>
89 #include <net/netisr2.h>
92 #include <netproto/mpls/mpls.h>
95 static struct rtstatistics rtstatistics_percpu
[MAXCPU
];
96 #define rtstat rtstatistics_percpu[mycpuid]
98 struct radix_node_head
*rt_tables
[MAXCPU
][AF_MAX
+1];
100 static void rt_maskedcopy (struct sockaddr
*, struct sockaddr
*,
102 static void rtable_init(void);
103 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo
*,
104 struct rtentry
*, void *);
106 static void rtredirect_msghandler(netmsg_t msg
);
107 static void rtrequest1_msghandler(netmsg_t msg
);
108 static void rtsearch_msghandler(netmsg_t msg
);
109 static void rtmask_add_msghandler(netmsg_t msg
);
111 static int rt_setshims(struct rtentry
*, struct sockaddr
**);
113 SYSCTL_NODE(_net
, OID_AUTO
, route
, CTLFLAG_RW
, 0, "Routing");
116 static int route_debug
= 1;
117 SYSCTL_INT(_net_route
, OID_AUTO
, route_debug
, CTLFLAG_RW
,
118 &route_debug
, 0, "");
121 int route_assert_owner_access
= 1;
122 SYSCTL_INT(_net_route
, OID_AUTO
, assert_owner_access
, CTLFLAG_RW
,
123 &route_assert_owner_access
, 0, "");
125 u_long route_kmalloc_limit
= 0;
126 TUNABLE_ULONG("net.route.kmalloc_limit", &route_kmalloc_limit
);
129 * Initialize the route table(s) for protocol domains and
130 * create a helper thread which will be responsible for updating
131 * route table entries on each cpu.
138 for (cpu
= 0; cpu
< ncpus
; ++cpu
)
139 bzero(&rtstatistics_percpu
[cpu
], sizeof(struct rtstatistics
));
140 rn_init(); /* initialize all zeroes, all ones, mask table */
141 rtable_init(); /* call dom_rtattach() on each cpu */
143 if (route_kmalloc_limit
)
144 kmalloc_raise_limit(M_RTABLE
, route_kmalloc_limit
);
148 rtable_init_oncpu(netmsg_t msg
)
153 SLIST_FOREACH(dom
, &domains
, dom_next
) {
154 if (dom
->dom_rtattach
) {
156 (void **)&rt_tables
[cpu
][dom
->dom_family
],
160 ifnet_forwardmsg(&msg
->lmsg
, cpu
+ 1);
166 struct netmsg_base msg
;
168 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
, 0, rtable_init_oncpu
);
169 ifnet_domsg(&msg
.lmsg
, 0);
173 * Routing statistics.
176 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS
)
180 for (cpu
= 0; cpu
< ncpus
; ++cpu
) {
181 if ((error
= SYSCTL_OUT(req
, &rtstatistics_percpu
[cpu
],
182 sizeof(struct rtstatistics
))))
184 if ((error
= SYSCTL_IN(req
, &rtstatistics_percpu
[cpu
],
185 sizeof(struct rtstatistics
))))
191 SYSCTL_PROC(_net_route
, OID_AUTO
, stats
, (CTLTYPE_OPAQUE
|CTLFLAG_RW
),
192 0, 0, sysctl_rtstatistics
, "S,rtstatistics", "Routing statistics");
195 * Packet routing routines.
199 * Look up and fill in the "ro_rt" rtentry field in a route structure given
200 * an address in the "ro_dst" field. Always send a report on a miss and
201 * always clone routes.
204 rtalloc(struct route
*ro
)
206 rtalloc_ign(ro
, 0UL);
210 * Look up and fill in the "ro_rt" rtentry field in a route structure given
211 * an address in the "ro_dst" field. Always send a report on a miss and
212 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
216 rtalloc_ign(struct route
*ro
, u_long ignoreflags
)
218 if (ro
->ro_rt
!= NULL
) {
219 if (ro
->ro_rt
->rt_ifp
!= NULL
&& ro
->ro_rt
->rt_flags
& RTF_UP
)
224 ro
->ro_rt
= _rtlookup(&ro
->ro_dst
, RTL_REPORTMSG
, ignoreflags
);
228 * Look up the route that matches the given "dst" address.
230 * Route lookup can have the side-effect of creating and returning
231 * a cloned route instead when "dst" matches a cloning route and the
232 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
234 * Any route returned has its reference count incremented.
237 _rtlookup(struct sockaddr
*dst
, boolean_t generate_report
, u_long ignore
)
239 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
246 * Look up route in the radix tree.
248 rt
= (struct rtentry
*) rnh
->rnh_matchaddr((char *)dst
, rnh
);
253 * Handle cloning routes.
255 if ((rt
->rt_flags
& ~ignore
& (RTF_CLONING
| RTF_PRCLONING
)) != 0) {
256 struct rtentry
*clonedroute
;
259 clonedroute
= rt
; /* copy in/copy out parameter */
260 error
= rtrequest(RTM_RESOLVE
, dst
, NULL
, NULL
, 0,
261 &clonedroute
); /* clone the route */
262 if (error
!= 0) { /* cloning failed */
264 rt_dstmsg(RTM_MISS
, dst
, error
);
266 return (rt
); /* return the uncloned route */
268 if (generate_report
) {
269 if (clonedroute
->rt_flags
& RTF_XRESOLVE
)
270 rt_dstmsg(RTM_RESOLVE
, dst
, 0);
272 rt_rtmsg(RTM_ADD
, clonedroute
,
273 clonedroute
->rt_ifp
, 0);
275 return (clonedroute
); /* return cloned route */
279 * Increment the reference count of the matched route and return.
285 rtstat
.rts_unreach
++;
287 rt_dstmsg(RTM_MISS
, dst
, 0);
292 rtfree(struct rtentry
*rt
)
294 if (rt
->rt_cpuid
== mycpuid
)
301 rtfree_oncpu(struct rtentry
*rt
)
303 KKASSERT(rt
->rt_cpuid
== mycpuid
);
304 KASSERT(rt
->rt_refcnt
> 0, ("rtfree: rt_refcnt %ld", rt
->rt_refcnt
));
307 if (rt
->rt_refcnt
== 0) {
308 struct radix_node_head
*rnh
=
309 rt_tables
[mycpuid
][rt_key(rt
)->sa_family
];
312 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
313 if (!(rt
->rt_flags
& RTF_UP
)) {
314 /* deallocate route */
315 if (rt
->rt_ifa
!= NULL
)
317 if (rt
->rt_parent
!= NULL
)
318 RTFREE(rt
->rt_parent
); /* recursive call! */
326 rtfree_remote_dispatch(netmsg_t msg
)
328 struct lwkt_msg
*lmsg
= &msg
->lmsg
;
329 struct rtentry
*rt
= lmsg
->u
.ms_resultp
;
332 lwkt_replymsg(lmsg
, 0);
336 rtfree_remote(struct rtentry
*rt
)
338 struct netmsg_base
*msg
;
339 struct lwkt_msg
*lmsg
;
341 KKASSERT(rt
->rt_cpuid
!= mycpuid
);
343 if (route_assert_owner_access
) {
344 panic("rt remote free rt_cpuid %d, mycpuid %d",
345 rt
->rt_cpuid
, mycpuid
);
347 kprintf("rt remote free rt_cpuid %d, mycpuid %d\n",
348 rt
->rt_cpuid
, mycpuid
);
352 msg
= kmalloc(sizeof(*msg
), M_LWKTMSG
, M_INTWAIT
);
353 netmsg_init(msg
, NULL
, &netisr_afree_rport
, 0, rtfree_remote_dispatch
);
355 lmsg
->u
.ms_resultp
= rt
;
357 lwkt_sendmsg(netisr_cpuport(rt
->rt_cpuid
), lmsg
);
361 rtredirect_oncpu(struct sockaddr
*dst
, struct sockaddr
*gateway
,
362 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
364 struct rtentry
*rt
= NULL
;
365 struct rt_addrinfo rtinfo
;
370 /* verify the gateway is directly reachable */
371 if ((ifa
= ifa_ifwithnet(gateway
)) == NULL
) {
377 * If the redirect isn't from our current router for this destination,
378 * it's either old or wrong.
380 if (!(flags
& RTF_DONE
) && /* XXX JH */
381 (rt
= rtpurelookup(dst
)) != NULL
&&
382 (!sa_equal(src
, rt
->rt_gateway
) || rt
->rt_ifa
!= ifa
)) {
388 * If it redirects us to ourselves, we have a routing loop,
389 * perhaps as a result of an interface going down recently.
391 if (ifa_ifwithaddr(gateway
)) {
392 error
= EHOSTUNREACH
;
397 * Create a new entry if the lookup failed or if we got back
398 * a wildcard entry for the default route. This is necessary
399 * for hosts which use routing redirects generated by smart
400 * gateways to dynamically build the routing tables.
404 if ((rt_mask(rt
) != NULL
&& rt_mask(rt
)->sa_len
< 2)) {
409 /* Ignore redirects for directly connected hosts. */
410 if (!(rt
->rt_flags
& RTF_GATEWAY
)) {
411 error
= EHOSTUNREACH
;
415 if (!(rt
->rt_flags
& RTF_HOST
) && (flags
& RTF_HOST
)) {
417 * Changing from a network route to a host route.
418 * Create a new host route rather than smashing the
422 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
423 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
424 rtinfo
.rti_info
[RTAX_DST
] = dst
;
425 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
426 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
427 rtinfo
.rti_flags
= flags
;
428 rtinfo
.rti_ifa
= ifa
;
429 rt
= NULL
; /* copy-in/copy-out parameter */
430 error
= rtrequest1(RTM_ADD
, &rtinfo
, &rt
);
432 flags
= rt
->rt_flags
;
433 stat
= &rtstat
.rts_dynamic
;
436 * Smash the current notion of the gateway to this destination.
437 * Should check about netmask!!!
439 rt
->rt_flags
|= RTF_MODIFIED
;
440 flags
|= RTF_MODIFIED
;
442 /* We only need to report rtmsg on CPU0 */
443 rt_setgate(rt
, rt_key(rt
), gateway
,
444 mycpuid
== 0 ? RTL_REPORTMSG
: RTL_DONTREPORT
);
446 stat
= &rtstat
.rts_newgateway
;
454 rtstat
.rts_badredirect
++;
455 else if (stat
!= NULL
)
461 struct netmsg_rtredirect
{
462 struct netmsg_base base
;
463 struct sockaddr
*dst
;
464 struct sockaddr
*gateway
;
465 struct sockaddr
*netmask
;
467 struct sockaddr
*src
;
471 * Force a routing table entry to the specified
472 * destination to go through the given gateway.
473 * Normally called as a result of a routing redirect
474 * message from the network layer.
476 * N.B.: must be called at splnet
479 rtredirect(struct sockaddr
*dst
, struct sockaddr
*gateway
,
480 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
482 struct rt_addrinfo rtinfo
;
484 struct netmsg_rtredirect msg
;
486 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
487 0, rtredirect_msghandler
);
489 msg
.gateway
= gateway
;
490 msg
.netmask
= netmask
;
493 error
= rt_domsg_global(&msg
.base
);
494 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
495 rtinfo
.rti_info
[RTAX_DST
] = dst
;
496 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
497 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
498 rtinfo
.rti_info
[RTAX_AUTHOR
] = src
;
499 rt_missmsg(RTM_REDIRECT
, &rtinfo
, flags
, error
);
503 rtredirect_msghandler(netmsg_t msg
)
505 struct netmsg_rtredirect
*rmsg
= (void *)msg
;
508 rtredirect_oncpu(rmsg
->dst
, rmsg
->gateway
, rmsg
->netmask
,
509 rmsg
->flags
, rmsg
->src
);
510 nextcpu
= mycpuid
+ 1;
512 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &msg
->lmsg
);
514 lwkt_replymsg(&msg
->lmsg
, 0);
518 * Routing table ioctl interface.
521 rtioctl(u_long req
, caddr_t data
, struct ucred
*cred
)
524 /* Multicast goop, grrr... */
525 return mrt_ioctl
? mrt_ioctl(req
, data
) : EOPNOTSUPP
;
532 ifa_ifwithroute(int flags
, struct sockaddr
*dst
, struct sockaddr
*gateway
)
536 if (!(flags
& RTF_GATEWAY
)) {
538 * If we are adding a route to an interface,
539 * and the interface is a point-to-point link,
540 * we should search for the destination
541 * as our clue to the interface. Otherwise
542 * we can use the local address.
545 if (flags
& RTF_HOST
) {
546 ifa
= ifa_ifwithdstaddr(dst
);
549 ifa
= ifa_ifwithaddr(gateway
);
552 * If we are adding a route to a remote net
553 * or host, the gateway may still be on the
554 * other end of a pt to pt link.
556 ifa
= ifa_ifwithdstaddr(gateway
);
559 ifa
= ifa_ifwithnet(gateway
);
563 rt
= rtpurelookup(gateway
);
567 if ((ifa
= rt
->rt_ifa
) == NULL
)
570 if (ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
571 struct ifaddr
*oldifa
= ifa
;
573 ifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
580 static int rt_fixdelete (struct radix_node
*, void *);
581 static int rt_fixchange (struct radix_node
*, void *);
585 struct radix_node_head
*rnh
;
589 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
592 rt_getifa(struct rt_addrinfo
*rtinfo
)
594 struct sockaddr
*gateway
= rtinfo
->rti_info
[RTAX_GATEWAY
];
595 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
596 struct sockaddr
*ifaaddr
= rtinfo
->rti_info
[RTAX_IFA
];
597 int flags
= rtinfo
->rti_flags
;
600 * ifp may be specified by sockaddr_dl
601 * when protocol address is ambiguous.
603 if (rtinfo
->rti_ifp
== NULL
) {
604 struct sockaddr
*ifpaddr
;
606 ifpaddr
= rtinfo
->rti_info
[RTAX_IFP
];
607 if (ifpaddr
!= NULL
&& ifpaddr
->sa_family
== AF_LINK
) {
610 ifa
= ifa_ifwithnet(ifpaddr
);
612 rtinfo
->rti_ifp
= ifa
->ifa_ifp
;
616 if (rtinfo
->rti_ifa
== NULL
&& ifaaddr
!= NULL
)
617 rtinfo
->rti_ifa
= ifa_ifwithaddr(ifaaddr
);
618 if (rtinfo
->rti_ifa
== NULL
) {
621 sa
= ifaaddr
!= NULL
? ifaaddr
:
622 (gateway
!= NULL
? gateway
: dst
);
623 if (sa
!= NULL
&& rtinfo
->rti_ifp
!= NULL
)
624 rtinfo
->rti_ifa
= ifaof_ifpforaddr(sa
, rtinfo
->rti_ifp
);
625 else if (dst
!= NULL
&& gateway
!= NULL
)
626 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, dst
, gateway
);
628 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, sa
, sa
);
630 if (rtinfo
->rti_ifa
== NULL
)
631 return (ENETUNREACH
);
633 if (rtinfo
->rti_ifp
== NULL
)
634 rtinfo
->rti_ifp
= rtinfo
->rti_ifa
->ifa_ifp
;
639 * Do appropriate manipulations of a routing tree given
640 * all the bits of info needed
645 struct sockaddr
*dst
,
646 struct sockaddr
*gateway
,
647 struct sockaddr
*netmask
,
649 struct rtentry
**ret_nrt
)
651 struct rt_addrinfo rtinfo
;
653 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
654 rtinfo
.rti_info
[RTAX_DST
] = dst
;
655 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
656 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
657 rtinfo
.rti_flags
= flags
;
658 return rtrequest1(req
, &rtinfo
, ret_nrt
);
664 struct sockaddr
*dst
,
665 struct sockaddr
*gateway
,
666 struct sockaddr
*netmask
,
669 struct rt_addrinfo rtinfo
;
671 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
672 rtinfo
.rti_info
[RTAX_DST
] = dst
;
673 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
674 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
675 rtinfo
.rti_flags
= flags
;
676 return rtrequest1_global(req
, &rtinfo
, NULL
, NULL
, RTREQ_PRIO_NORM
);
680 struct netmsg_base base
;
682 struct rt_addrinfo
*rtinfo
;
683 rtrequest1_callback_func_t callback
;
688 rtrequest1_global(int req
, struct rt_addrinfo
*rtinfo
,
689 rtrequest1_callback_func_t callback
, void *arg
, boolean_t req_prio
)
691 int error
, flags
= 0;
692 struct netmsg_rtq msg
;
695 flags
= MSGF_PRIORITY
;
696 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, flags
,
697 rtrequest1_msghandler
);
698 msg
.base
.lmsg
.ms_error
= -1;
701 msg
.callback
= callback
;
703 error
= rt_domsg_global(&msg
.base
);
708 * Handle a route table request on the current cpu. Since the route table's
709 * are supposed to be identical on each cpu, an error occuring later in the
710 * message chain is considered system-fatal.
713 rtrequest1_msghandler(netmsg_t msg
)
715 struct netmsg_rtq
*rmsg
= (void *)msg
;
716 struct rt_addrinfo rtinfo
;
717 struct rtentry
*rt
= NULL
;
722 * Copy the rtinfo. We need to make sure that the original
723 * rtinfo, which is setup by the caller, in the netmsg will
724 * _not_ be changed; else the next CPU on the netmsg forwarding
725 * path will see a different rtinfo than what this CPU has seen.
727 rtinfo
= *rmsg
->rtinfo
;
729 error
= rtrequest1(rmsg
->req
, &rtinfo
, &rt
);
733 rmsg
->callback(rmsg
->req
, error
, &rtinfo
, rt
, rmsg
->arg
);
736 * RTM_DELETE's are propogated even if an error occurs, since a
737 * cloned route might be undergoing deletion and cloned routes
738 * are not necessarily replicated. An overall error is returned
739 * only if no cpus have the route in question.
741 if (rmsg
->base
.lmsg
.ms_error
< 0 || error
== 0)
742 rmsg
->base
.lmsg
.ms_error
= error
;
744 nextcpu
= mycpuid
+ 1;
745 if (error
&& rmsg
->req
!= RTM_DELETE
) {
747 panic("rtrequest1_msghandler: rtrequest table "
748 "error was cpu%d, err %d\n", mycpuid
, error
);
750 lwkt_replymsg(&rmsg
->base
.lmsg
, error
);
751 } else if (nextcpu
< ncpus
) {
752 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
754 lwkt_replymsg(&rmsg
->base
.lmsg
, rmsg
->base
.lmsg
.ms_error
);
759 rtrequest1(int req
, struct rt_addrinfo
*rtinfo
, struct rtentry
**ret_nrt
)
761 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
763 struct radix_node
*rn
;
764 struct radix_node_head
*rnh
;
766 struct sockaddr
*ndst
;
770 #define gotoerr(x) { error = x ; goto bad; }
774 rt_addrinfo_print(req
, rtinfo
);
779 * Find the correct routing tree to use for this Address Family
781 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
)
782 gotoerr(EAFNOSUPPORT
);
785 * If we are adding a host route then we don't want to put
786 * a netmask in the tree, nor do we want to clone it.
788 if (rtinfo
->rti_flags
& RTF_HOST
) {
789 rtinfo
->rti_info
[RTAX_NETMASK
] = NULL
;
790 rtinfo
->rti_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
795 /* Remove the item from the tree. */
796 rn
= rnh
->rnh_deladdr((char *)rtinfo
->rti_info
[RTAX_DST
],
797 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
801 KASSERT(!(rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)),
802 ("rnh_deladdr returned flags 0x%x", rn
->rn_flags
));
803 rt
= (struct rtentry
*)rn
;
805 /* ref to prevent a deletion race */
808 /* Free any routes cloned from this one. */
809 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
810 rt_mask(rt
) != NULL
) {
811 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
816 if (rt
->rt_gwroute
!= NULL
) {
817 RTFREE(rt
->rt_gwroute
);
818 rt
->rt_gwroute
= NULL
;
822 * NB: RTF_UP must be set during the search above,
823 * because we might delete the last ref, causing
824 * rt to get freed prematurely.
826 rt
->rt_flags
&= ~RTF_UP
;
830 rt_print(rtinfo
, rt
);
833 /* Give the protocol a chance to keep things in sync. */
834 if ((ifa
= rt
->rt_ifa
) && ifa
->ifa_rtrequest
)
835 ifa
->ifa_rtrequest(RTM_DELETE
, rt
);
838 * If the caller wants it, then it can have it,
839 * but it's up to it to free the rtentry as we won't be
842 KASSERT(rt
->rt_refcnt
>= 0,
843 ("rtrequest1(DELETE): refcnt %ld", rt
->rt_refcnt
));
844 if (ret_nrt
!= NULL
) {
845 /* leave ref intact for return */
848 /* deref / attempt to destroy */
854 if (ret_nrt
== NULL
|| (rt
= *ret_nrt
) == NULL
)
857 KASSERT(rt
->rt_cpuid
== mycpuid
,
858 ("rt resolve rt_cpuid %d, mycpuid %d",
859 rt
->rt_cpuid
, mycpuid
));
863 rt
->rt_flags
& ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
864 rtinfo
->rti_flags
|= RTF_WASCLONED
;
865 rtinfo
->rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
866 if ((rtinfo
->rti_info
[RTAX_NETMASK
] = rt
->rt_genmask
) == NULL
)
867 rtinfo
->rti_flags
|= RTF_HOST
;
868 rtinfo
->rti_info
[RTAX_MPLS1
] = rt
->rt_shim
[0];
869 rtinfo
->rti_info
[RTAX_MPLS2
] = rt
->rt_shim
[1];
870 rtinfo
->rti_info
[RTAX_MPLS3
] = rt
->rt_shim
[2];
874 KASSERT(!(rtinfo
->rti_flags
& RTF_GATEWAY
) ||
875 rtinfo
->rti_info
[RTAX_GATEWAY
] != NULL
,
876 ("rtrequest: GATEWAY but no gateway"));
878 if (rtinfo
->rti_ifa
== NULL
&& (error
= rt_getifa(rtinfo
)))
880 ifa
= rtinfo
->rti_ifa
;
882 R_Malloc(rt
, struct rtentry
*, sizeof(struct rtentry
));
884 if (req
== RTM_ADD
) {
885 kprintf("rtrequest1: alloc rtentry failed on "
890 bzero(rt
, sizeof(struct rtentry
));
891 rt
->rt_flags
= RTF_UP
| rtinfo
->rti_flags
;
892 rt
->rt_cpuid
= mycpuid
;
894 if (mycpuid
!= 0 && req
== RTM_ADD
) {
895 /* For RTM_ADD, we have already sent rtmsg on CPU0. */
896 reportmsg
= RTL_DONTREPORT
;
899 * For RTM_ADD, we only send rtmsg on CPU0.
900 * For RTM_RESOLVE, we always send rtmsg. XXX
902 reportmsg
= RTL_REPORTMSG
;
904 error
= rt_setgate(rt
, dst
, rtinfo
->rti_info
[RTAX_GATEWAY
],
912 if (rtinfo
->rti_info
[RTAX_NETMASK
] != NULL
)
913 rt_maskedcopy(dst
, ndst
,
914 rtinfo
->rti_info
[RTAX_NETMASK
]);
916 bcopy(dst
, ndst
, dst
->sa_len
);
918 if (rtinfo
->rti_info
[RTAX_MPLS1
] != NULL
)
919 rt_setshims(rt
, rtinfo
->rti_info
);
922 * Note that we now have a reference to the ifa.
923 * This moved from below so that rnh->rnh_addaddr() can
924 * examine the ifa and ifa->ifa_ifp if it so desires.
928 rt
->rt_ifp
= ifa
->ifa_ifp
;
929 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
931 rn
= rnh
->rnh_addaddr((char *)ndst
,
932 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
935 struct rtentry
*oldrt
;
938 * We already have one of these in the tree.
939 * We do a special hack: if the old route was
940 * cloned, then we blow it away and try
941 * re-inserting the new one.
943 oldrt
= rtpurelookup(ndst
);
946 if (oldrt
->rt_flags
& RTF_WASCLONED
) {
947 rtrequest(RTM_DELETE
, rt_key(oldrt
),
950 oldrt
->rt_flags
, NULL
);
951 rn
= rnh
->rnh_addaddr((char *)ndst
,
953 rtinfo
->rti_info
[RTAX_NETMASK
],
960 * If it still failed to go into the tree,
961 * then un-make it (this should be a function).
964 if (rt
->rt_gwroute
!= NULL
)
965 rtfree(rt
->rt_gwroute
);
973 * If we got here from RESOLVE, then we are cloning
974 * so clone the rest, and note that we
975 * are a clone (and increment the parent's references)
977 if (req
== RTM_RESOLVE
) {
978 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
; /* copy metrics */
979 rt
->rt_rmx
.rmx_pksent
= 0; /* reset packet counter */
980 if ((*ret_nrt
)->rt_flags
&
981 (RTF_CLONING
| RTF_PRCLONING
)) {
982 rt
->rt_parent
= *ret_nrt
;
983 (*ret_nrt
)->rt_refcnt
++;
988 * if this protocol has something to add to this then
989 * allow it to do that as well.
991 if (ifa
->ifa_rtrequest
!= NULL
)
992 ifa
->ifa_rtrequest(req
, rt
);
995 * We repeat the same procedure from rt_setgate() here because
996 * it doesn't fire when we call it there because the node
997 * hasn't been added to the tree yet.
999 if (req
== RTM_ADD
&& !(rt
->rt_flags
& RTF_HOST
) &&
1000 rt_mask(rt
) != NULL
) {
1001 struct rtfc_arg arg
= { rt
, rnh
};
1003 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
1004 (char *)rt_mask(rt
),
1005 rt_fixchange
, &arg
);
1010 rt_print(rtinfo
, rt
);
1013 * Return the resulting rtentry,
1014 * increasing the number of references by one.
1016 if (ret_nrt
!= NULL
) {
1028 kprintf("rti %p failed error %d\n", rtinfo
, error
);
1030 kprintf("rti %p succeeded\n", rtinfo
);
1038 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1039 * (i.e., the routes related to it by the operation of cloning). This
1040 * routine is iterated over all potential former-child-routes by way of
1041 * rnh->rnh_walktree_from() above, and those that actually are children of
1042 * the late parent (passed in as VP here) are themselves deleted.
1045 rt_fixdelete(struct radix_node
*rn
, void *vp
)
1047 struct rtentry
*rt
= (struct rtentry
*)rn
;
1048 struct rtentry
*rt0
= vp
;
1050 if (rt
->rt_parent
== rt0
&&
1051 !(rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1052 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1053 rt
->rt_flags
, NULL
);
1059 * This routine is called from rt_setgate() to do the analogous thing for
1060 * adds and changes. There is the added complication in this case of a
1061 * middle insert; i.e., insertion of a new network route between an older
1062 * network route and (cloned) host routes. For this reason, a simple check
1063 * of rt->rt_parent is insufficient; each candidate route must be tested
1064 * against the (mask, value) of the new route (passed as before in vp)
1065 * to see if the new route matches it.
1067 * XXX - it may be possible to do fixdelete() for changes and reserve this
1068 * routine just for adds. I'm not sure why I thought it was necessary to do
1072 static int rtfcdebug
= 0;
1076 rt_fixchange(struct radix_node
*rn
, void *vp
)
1078 struct rtentry
*rt
= (struct rtentry
*)rn
;
1079 struct rtfc_arg
*ap
= vp
;
1080 struct rtentry
*rt0
= ap
->rt0
;
1081 struct radix_node_head
*rnh
= ap
->rnh
;
1082 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
1087 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt
, rt0
);
1090 if (rt
->rt_parent
== NULL
||
1091 (rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1093 if (rtfcdebug
) kprintf("no parent, pinned or cloning\n");
1098 if (rt
->rt_parent
== rt0
) {
1100 if (rtfcdebug
) kprintf("parent match\n");
1102 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1103 rt
->rt_flags
, NULL
);
1107 * There probably is a function somewhere which does this...
1108 * if not, there should be.
1110 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
1112 xk1
= (u_char
*)rt_key(rt0
);
1113 xm1
= (u_char
*)rt_mask(rt0
);
1114 xk2
= (u_char
*)rt_key(rt
);
1116 /* avoid applying a less specific route */
1117 xmp
= (u_char
*)rt_mask(rt
->rt_parent
);
1118 mlen
= rt_key(rt
->rt_parent
)->sa_len
;
1119 if (mlen
> rt_key(rt0
)->sa_len
) {
1122 kprintf("rt_fixchange: inserting a less "
1123 "specific route\n");
1127 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
1128 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
]) {
1131 kprintf("rt_fixchange: inserting a less "
1132 "specific route\n");
1138 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
1139 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
1141 if (rtfcdebug
) kprintf("no match\n");
1148 * OK, this node is a clone, and matches the node currently being
1149 * changed/added under the node's mask. So, get rid of it.
1152 if (rtfcdebug
) kprintf("deleting\n");
1154 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1155 rt
->rt_flags
, NULL
);
1159 rt_setgate(struct rtentry
*rt0
, struct sockaddr
*dst
, struct sockaddr
*gate
,
1160 boolean_t generate_report
)
1162 char *space
, *oldspace
;
1163 int dlen
= RT_ROUNDUP(dst
->sa_len
), glen
= RT_ROUNDUP(gate
->sa_len
);
1164 struct rtentry
*rt
= rt0
;
1165 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
1168 * A host route with the destination equal to the gateway
1169 * will interfere with keeping LLINFO in the routing
1170 * table, so disallow it.
1172 if (((rt0
->rt_flags
& (RTF_HOST
| RTF_GATEWAY
| RTF_LLINFO
)) ==
1173 (RTF_HOST
| RTF_GATEWAY
)) &&
1174 dst
->sa_len
== gate
->sa_len
&&
1175 sa_equal(dst
, gate
)) {
1177 * The route might already exist if this is an RTM_CHANGE
1178 * or a routing redirect, so try to delete it.
1180 if (rt_key(rt0
) != NULL
)
1181 rtrequest(RTM_DELETE
, rt_key(rt0
), rt0
->rt_gateway
,
1182 rt_mask(rt0
), rt0
->rt_flags
, NULL
);
1183 return EADDRNOTAVAIL
;
1187 * Both dst and gateway are stored in the same malloc'ed chunk
1188 * (If I ever get my hands on....)
1189 * if we need to malloc a new chunk, then keep the old one around
1190 * till we don't need it any more.
1192 if (rt
->rt_gateway
== NULL
||
1193 glen
> RT_ROUNDUP(rt
->rt_gateway
->sa_len
)) {
1194 oldspace
= (char *)rt_key(rt
);
1195 R_Malloc(space
, char *, dlen
+ glen
);
1198 rt
->rt_nodes
->rn_key
= space
;
1200 space
= (char *)rt_key(rt
); /* Just use the old space. */
1204 /* Set the gateway value. */
1205 rt
->rt_gateway
= (struct sockaddr
*)(space
+ dlen
);
1206 bcopy(gate
, rt
->rt_gateway
, glen
);
1208 if (oldspace
!= NULL
) {
1210 * If we allocated a new chunk, preserve the original dst.
1211 * This way, rt_setgate() really just sets the gate
1212 * and leaves the dst field alone.
1214 bcopy(dst
, space
, dlen
);
1219 * If there is already a gwroute, it's now almost definitely wrong
1222 if (rt
->rt_gwroute
!= NULL
) {
1223 RTFREE(rt
->rt_gwroute
);
1224 rt
->rt_gwroute
= NULL
;
1226 if (rt
->rt_flags
& RTF_GATEWAY
) {
1228 * Cloning loop avoidance: In the presence of
1229 * protocol-cloning and bad configuration, it is
1230 * possible to get stuck in bottomless mutual recursion
1231 * (rtrequest rt_setgate rtlookup). We avoid this
1232 * by not allowing protocol-cloning to operate for
1233 * gateways (which is probably the correct choice
1234 * anyway), and avoid the resulting reference loops
1235 * by disallowing any route to run through itself as
1236 * a gateway. This is obviously mandatory when we
1237 * get rt->rt_output().
1239 * This breaks TTCP for hosts outside the gateway! XXX JH
1241 rt
->rt_gwroute
= _rtlookup(gate
, generate_report
,
1243 if (rt
->rt_gwroute
== rt
) {
1244 rt
->rt_gwroute
= NULL
;
1246 return EDQUOT
; /* failure */
1251 * This isn't going to do anything useful for host routes, so
1252 * don't bother. Also make sure we have a reasonable mask
1253 * (we don't yet have one during adds).
1255 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != NULL
) {
1256 struct rtfc_arg arg
= { rt
, rnh
};
1258 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
1259 (char *)rt_mask(rt
),
1260 rt_fixchange
, &arg
);
1268 struct sockaddr
*src
,
1269 struct sockaddr
*dst
,
1270 struct sockaddr
*netmask
)
1272 u_char
*cp1
= (u_char
*)src
;
1273 u_char
*cp2
= (u_char
*)dst
;
1274 u_char
*cp3
= (u_char
*)netmask
;
1275 u_char
*cplim
= cp2
+ *cp3
;
1276 u_char
*cplim2
= cp2
+ *cp1
;
1278 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
1283 *cp2
++ = *cp1
++ & *cp3
++;
1285 bzero(cp2
, cplim2
- cp2
);
1289 rt_llroute(struct sockaddr
*dst
, struct rtentry
*rt0
, struct rtentry
**drt
)
1291 struct rtentry
*up_rt
, *rt
;
1293 if (!(rt0
->rt_flags
& RTF_UP
)) {
1294 up_rt
= rtlookup(dst
);
1296 return (EHOSTUNREACH
);
1300 if (up_rt
->rt_flags
& RTF_GATEWAY
) {
1301 if (up_rt
->rt_gwroute
== NULL
) {
1302 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1303 if (up_rt
->rt_gwroute
== NULL
)
1304 return (EHOSTUNREACH
);
1305 } else if (!(up_rt
->rt_gwroute
->rt_flags
& RTF_UP
)) {
1306 rtfree(up_rt
->rt_gwroute
);
1307 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1308 if (up_rt
->rt_gwroute
== NULL
)
1309 return (EHOSTUNREACH
);
1311 rt
= up_rt
->rt_gwroute
;
1314 if (rt
->rt_flags
& RTF_REJECT
&&
1315 (rt
->rt_rmx
.rmx_expire
== 0 || /* rt doesn't expire */
1316 time_uptime
< rt
->rt_rmx
.rmx_expire
)) /* rt not expired */
1317 return (rt
->rt_flags
& RTF_HOST
? EHOSTDOWN
: EHOSTUNREACH
);
1323 rt_setshims(struct rtentry
*rt
, struct sockaddr
**rt_shim
){
1326 for (i
=0; i
<3; i
++) {
1327 struct sockaddr
*shim
= rt_shim
[RTAX_MPLS1
+ i
];
1333 shimlen
= RT_ROUNDUP(shim
->sa_len
);
1334 R_Malloc(rt
->rt_shim
[i
], struct sockaddr
*, shimlen
);
1335 bcopy(shim
, rt
->rt_shim
[i
], shimlen
);
1344 * Print out a route table entry
1347 rt_print(struct rt_addrinfo
*rtinfo
, struct rtentry
*rn
)
1349 kprintf("rti %p cpu %d route %p flags %08lx: ",
1350 rtinfo
, mycpuid
, rn
, rn
->rt_flags
);
1351 sockaddr_print(rt_key(rn
));
1353 sockaddr_print(rt_mask(rn
));
1355 sockaddr_print(rn
->rt_gateway
);
1356 kprintf(" ifc \"%s\"", rn
->rt_ifp
? rn
->rt_ifp
->if_dname
: "?");
1357 kprintf(" ifa %p\n", rn
->rt_ifa
);
1361 rt_addrinfo_print(int cmd
, struct rt_addrinfo
*rti
)
1367 if (cmd
== RTM_DELETE
&& route_debug
> 1)
1368 print_backtrace(-1);
1382 kprintf("C%02d ", cmd
);
1385 kprintf("rti %p cpu %d ", rti
, mycpuid
);
1386 for (i
= 0; i
< rti
->rti_addrs
; ++i
) {
1387 if (rti
->rti_info
[i
] == NULL
)
1417 kprintf("(?%02d ", i
);
1420 sockaddr_print(rti
->rti_info
[i
]);
1428 sockaddr_print(struct sockaddr
*sa
)
1430 struct sockaddr_in
*sa4
;
1431 struct sockaddr_in6
*sa6
;
1440 len
= sa
->sa_len
- offsetof(struct sockaddr
, sa_data
[0]);
1442 switch(sa
->sa_family
) {
1446 switch(sa
->sa_family
) {
1448 sa4
= (struct sockaddr_in
*)sa
;
1449 kprintf("INET %d %d.%d.%d.%d",
1450 ntohs(sa4
->sin_port
),
1451 (ntohl(sa4
->sin_addr
.s_addr
) >> 24) & 255,
1452 (ntohl(sa4
->sin_addr
.s_addr
) >> 16) & 255,
1453 (ntohl(sa4
->sin_addr
.s_addr
) >> 8) & 255,
1454 (ntohl(sa4
->sin_addr
.s_addr
) >> 0) & 255
1458 sa6
= (struct sockaddr_in6
*)sa
;
1459 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1460 ntohs(sa6
->sin6_port
),
1461 sa6
->sin6_addr
.s6_addr16
[0],
1462 sa6
->sin6_addr
.s6_addr16
[1],
1463 sa6
->sin6_addr
.s6_addr16
[2],
1464 sa6
->sin6_addr
.s6_addr16
[3],
1465 sa6
->sin6_addr
.s6_addr16
[4],
1466 sa6
->sin6_addr
.s6_addr16
[5],
1467 sa6
->sin6_addr
.s6_addr16
[6],
1468 sa6
->sin6_addr
.s6_addr16
[7]
1472 kprintf("AF%d ", sa
->sa_family
);
1473 while (len
> 0 && sa
->sa_data
[len
-1] == 0)
1476 for (i
= 0; i
< len
; ++i
) {
1479 kprintf("%d", (unsigned char)sa
->sa_data
[i
]);
1489 * Set up a routing table entry, normally for an interface.
1492 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
1494 struct sockaddr
*dst
, *deldst
, *netmask
;
1495 struct mbuf
*m
= NULL
;
1496 struct radix_node_head
*rnh
;
1497 struct radix_node
*rn
;
1498 struct rt_addrinfo rtinfo
;
1501 if (flags
& RTF_HOST
) {
1502 dst
= ifa
->ifa_dstaddr
;
1505 dst
= ifa
->ifa_addr
;
1506 netmask
= ifa
->ifa_netmask
;
1509 * If it's a delete, check that if it exists, it's on the correct
1510 * interface or we might scrub a route to another ifa which would
1511 * be confusing at best and possibly worse.
1513 if (cmd
== RTM_DELETE
) {
1515 * It's a delete, so it should already exist..
1516 * If it's a net, mask off the host bits
1517 * (Assuming we have a mask)
1519 if (netmask
!= NULL
) {
1520 m
= m_get(MB_DONTWAIT
, MT_SONAME
);
1524 deldst
= mtod(m
, struct sockaddr
*);
1525 rt_maskedcopy(dst
, deldst
, netmask
);
1529 * Look up an rtentry that is in the routing tree and
1530 * contains the correct info.
1532 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
||
1533 (rn
= rnh
->rnh_lookup((char *)dst
,
1534 (char *)netmask
, rnh
)) == NULL
||
1535 ((struct rtentry
*)rn
)->rt_ifa
!= ifa
||
1536 !sa_equal((struct sockaddr
*)rn
->rn_key
, dst
)) {
1539 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1545 * One would think that as we are deleting, and we know
1546 * it doesn't exist, we could just return at this point
1547 * with an "ELSE" clause, but apparently not..
1549 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1554 * Do the actual request
1556 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1557 rtinfo
.rti_info
[RTAX_DST
] = dst
;
1558 rtinfo
.rti_info
[RTAX_GATEWAY
] = ifa
->ifa_addr
;
1559 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
1560 rtinfo
.rti_flags
= flags
| ifa
->ifa_flags
;
1561 rtinfo
.rti_ifa
= ifa
;
1562 error
= rtrequest1_global(cmd
, &rtinfo
, rtinit_rtrequest_callback
, ifa
,
1570 rtinit_rtrequest_callback(int cmd
, int error
,
1571 struct rt_addrinfo
*rtinfo
, struct rtentry
*rt
,
1574 struct ifaddr
*ifa
= arg
;
1576 if (error
== 0 && rt
) {
1579 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
1582 if (cmd
== RTM_DELETE
) {
1583 if (rt
->rt_refcnt
== 0) {
1592 struct netmsg_base base
;
1594 struct rt_addrinfo
*rtinfo
;
1595 rtsearch_callback_func_t callback
;
1597 boolean_t exact_match
;
1602 rtsearch_global(int req
, struct rt_addrinfo
*rtinfo
,
1603 rtsearch_callback_func_t callback
, void *arg
, boolean_t exact_match
,
1606 struct netmsg_rts msg
;
1610 flags
= MSGF_PRIORITY
;
1611 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, flags
,
1612 rtsearch_msghandler
);
1614 msg
.rtinfo
= rtinfo
;
1615 msg
.callback
= callback
;
1617 msg
.exact_match
= exact_match
;
1619 return rt_domsg_global(&msg
.base
);
1623 rtsearch_msghandler(netmsg_t msg
)
1625 struct netmsg_rts
*rmsg
= (void *)msg
;
1626 struct rt_addrinfo rtinfo
;
1627 struct radix_node_head
*rnh
;
1632 * Copy the rtinfo. We need to make sure that the original
1633 * rtinfo, which is setup by the caller, in the netmsg will
1634 * _not_ be changed; else the next CPU on the netmsg forwarding
1635 * path will see a different rtinfo than what this CPU has seen.
1637 rtinfo
= *rmsg
->rtinfo
;
1640 * Find the correct routing tree to use for this Address Family
1642 if ((rnh
= rt_tables
[mycpuid
][rtinfo
.rti_dst
->sa_family
]) == NULL
) {
1644 panic("partially initialized routing tables");
1645 lwkt_replymsg(&rmsg
->base
.lmsg
, EAFNOSUPPORT
);
1650 * Correct rtinfo for the host route searching.
1652 if (rtinfo
.rti_flags
& RTF_HOST
) {
1653 rtinfo
.rti_netmask
= NULL
;
1654 rtinfo
.rti_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
1657 rt
= (struct rtentry
*)
1658 rnh
->rnh_lookup((char *)rtinfo
.rti_dst
,
1659 (char *)rtinfo
.rti_netmask
, rnh
);
1662 * If we are asked to do the "exact match", we need to make sure
1663 * that host route searching got a host route while a network
1664 * route searching got a network route.
1666 if (rt
!= NULL
&& rmsg
->exact_match
&&
1667 ((rt
->rt_flags
^ rtinfo
.rti_flags
) & RTF_HOST
))
1672 * No matching routes have been found, don't count this
1673 * as a critical error (here, we set 'error' to 0), just
1674 * keep moving on, since at least prcloned routes are not
1675 * duplicated onto each CPU.
1682 error
= rmsg
->callback(rmsg
->req
, &rtinfo
, rt
, rmsg
->arg
,
1686 if (error
== EJUSTRETURN
) {
1687 lwkt_replymsg(&rmsg
->base
.lmsg
, 0);
1692 nextcpu
= mycpuid
+ 1;
1694 KKASSERT(rmsg
->found_cnt
> 0);
1697 * Under following cases, unrecoverable error has
1699 * o Request is RTM_GET
1700 * o The first time that we find the route, but the
1701 * modification fails.
1703 if (rmsg
->req
!= RTM_GET
&& rmsg
->found_cnt
> 1) {
1704 panic("rtsearch_msghandler: unrecoverable error "
1707 lwkt_replymsg(&rmsg
->base
.lmsg
, error
);
1708 } else if (nextcpu
< ncpus
) {
1709 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
1711 if (rmsg
->found_cnt
== 0) {
1712 /* The requested route was never seen ... */
1715 lwkt_replymsg(&rmsg
->base
.lmsg
, error
);
1720 rtmask_add_global(struct sockaddr
*mask
, boolean_t req_prio
)
1722 struct netmsg_base msg
;
1726 flags
= MSGF_PRIORITY
;
1727 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
, flags
,
1728 rtmask_add_msghandler
);
1729 msg
.lmsg
.u
.ms_resultp
= mask
;
1731 return rt_domsg_global(&msg
);
1735 _rtmask_lookup(struct sockaddr
*mask
, boolean_t search
)
1737 struct radix_node
*n
;
1739 #define clen(s) (*(u_char *)(s))
1740 n
= rn_addmask((char *)mask
, search
, 1, rn_cpumaskhead(mycpuid
));
1742 mask
->sa_len
>= clen(n
->rn_key
) &&
1743 bcmp((char *)mask
+ 1,
1744 (char *)n
->rn_key
+ 1, clen(n
->rn_key
) - 1) == 0) {
1745 return (struct sockaddr
*)n
->rn_key
;
1753 rtmask_add_msghandler(netmsg_t msg
)
1755 struct lwkt_msg
*lmsg
= &msg
->lmsg
;
1756 struct sockaddr
*mask
= lmsg
->u
.ms_resultp
;
1757 int error
= 0, nextcpu
;
1759 if (rtmask_lookup(mask
) == NULL
)
1762 nextcpu
= mycpuid
+ 1;
1763 if (!error
&& nextcpu
< ncpus
)
1764 lwkt_forwardmsg(netisr_cpuport(nextcpu
), lmsg
);
1766 lwkt_replymsg(lmsg
, error
);
1769 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1770 SYSINIT(route
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_THIRD
, route_init
, 0);
1772 struct rtchange_arg
{
1773 struct ifaddr
*old_ifa
;
1774 struct ifaddr
*new_ifa
;
1780 rtchange_ifa(struct rtentry
*rt
, struct rtchange_arg
*ap
)
1782 if (rt
->rt_ifa
->ifa_rtrequest
!= NULL
)
1783 rt
->rt_ifa
->ifa_rtrequest(RTM_DELETE
, rt
);
1784 IFAFREE(rt
->rt_ifa
);
1786 IFAREF(ap
->new_ifa
);
1787 rt
->rt_ifa
= ap
->new_ifa
;
1788 rt
->rt_ifp
= ap
->new_ifa
->ifa_ifp
;
1789 if (rt
->rt_ifa
->ifa_rtrequest
!= NULL
)
1790 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
);
1796 rtchange_callback(struct radix_node
*rn
, void *xap
)
1798 struct rtchange_arg
*ap
= xap
;
1799 struct rtentry
*rt
= (struct rtentry
*)rn
;
1801 if (rt
->rt_ifa
== ap
->old_ifa
) {
1802 if (rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) {
1804 * We could saw the branch off when we are
1805 * still sitting on it, if the ifa_rtrequest
1806 * DEL/ADD are called directly from here.
1811 rtchange_ifa(rt
, ap
);
1816 struct netmsg_rtchange
{
1817 struct netmsg_base base
;
1818 struct ifaddr
*old_ifa
;
1819 struct ifaddr
*new_ifa
;
1824 rtchange_dispatch(netmsg_t msg
)
1826 struct netmsg_rtchange
*rmsg
= (void *)msg
;
1827 struct radix_node_head
*rnh
;
1828 struct rtchange_arg arg
;
1833 memset(&arg
, 0, sizeof(arg
));
1834 arg
.old_ifa
= rmsg
->old_ifa
;
1835 arg
.new_ifa
= rmsg
->new_ifa
;
1837 rnh
= rt_tables
[cpu
][AF_INET
];
1841 KKASSERT(arg
.rt
== NULL
);
1842 error
= rnh
->rnh_walktree(rnh
, rtchange_callback
, &arg
);
1843 if (arg
.rt
!= NULL
) {
1848 rtchange_ifa(rt
, &arg
);
1857 if (nextcpu
< ncpus
)
1858 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
1860 lwkt_replymsg(&rmsg
->base
.lmsg
, 0);
1864 rtchange(struct ifaddr
*old_ifa
, struct ifaddr
*new_ifa
)
1866 struct netmsg_rtchange msg
;
1869 * XXX individual requests are not independantly chained,
1870 * which means that the per-cpu route tables will not be
1871 * consistent in the middle of the operation. If routes
1872 * related to the interface are manipulated while we are
1873 * doing this the inconsistancy could trigger a panic.
1875 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, MSGF_PRIORITY
,
1877 msg
.old_ifa
= old_ifa
;
1878 msg
.new_ifa
= new_ifa
;
1880 rt_domsg_global(&msg
.base
);
1883 old_ifa
->ifa_flags
&= ~IFA_ROUTE
;
1884 new_ifa
->ifa_flags
|= IFA_ROUTE
;
1892 rt_domsg_global(struct netmsg_base
*nmsg
)
1894 ASSERT_CANDOMSG_NETISR0(curthread
);
1895 return lwkt_domsg(netisr_cpuport(0), &nmsg
->lmsg
, 0);