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/if_var.h>
81 #include <net/route.h>
82 #include <net/netisr.h>
84 #include <netinet/in.h>
85 #include <net/ip_mroute/ip_mroute.h>
87 #include <sys/thread2.h>
88 #include <sys/msgport2.h>
89 #include <net/netmsg2.h>
90 #include <net/netisr2.h>
93 #include <netproto/mpls/mpls.h>
96 static struct rtstatistics rtstatistics_percpu
[MAXCPU
] __cachealign
;
97 #define rtstat rtstatistics_percpu[mycpuid]
99 struct radix_node_head
*rt_tables
[MAXCPU
][AF_MAX
+1];
101 static void rt_maskedcopy (struct sockaddr
*, struct sockaddr
*,
103 static void rtable_init(void);
104 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo
*,
105 struct rtentry
*, void *);
107 static void rtredirect_msghandler(netmsg_t msg
);
108 static void rtrequest1_msghandler(netmsg_t msg
);
109 static void rtsearch_msghandler(netmsg_t msg
);
110 static void rtmask_add_msghandler(netmsg_t msg
);
112 static int rt_setshims(struct rtentry
*, struct sockaddr
**);
114 SYSCTL_NODE(_net
, OID_AUTO
, route
, CTLFLAG_RW
, 0, "Routing");
117 static int route_debug
= 1;
118 SYSCTL_INT(_net_route
, OID_AUTO
, route_debug
, CTLFLAG_RW
,
119 &route_debug
, 0, "");
122 u_long route_kmalloc_limit
= 0;
123 TUNABLE_ULONG("net.route.kmalloc_limit", &route_kmalloc_limit
);
126 * Initialize the route table(s) for protocol domains and
127 * create a helper thread which will be responsible for updating
128 * route table entries on each cpu.
135 if (route_kmalloc_limit
)
136 kmalloc_raise_limit(M_RTABLE
, route_kmalloc_limit
);
138 for (cpu
= 0; cpu
< netisr_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 */
145 rtable_init_oncpu(netmsg_t msg
)
150 ASSERT_NETISR_NCPUS(cpu
);
152 SLIST_FOREACH(dom
, &domains
, dom_next
) {
153 if (dom
->dom_rtattach
) {
155 (void **)&rt_tables
[cpu
][dom
->dom_family
],
159 netisr_forwardmsg(&msg
->base
, cpu
+ 1);
165 struct netmsg_base msg
;
167 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
, 0, rtable_init_oncpu
);
168 netisr_domsg_global(&msg
);
172 * Routing statistics.
175 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS
)
179 for (cpu
= 0; cpu
< netisr_ncpus
; ++cpu
) {
180 if ((error
= SYSCTL_OUT(req
, &rtstatistics_percpu
[cpu
],
181 sizeof(struct rtstatistics
))))
183 if ((error
= SYSCTL_IN(req
, &rtstatistics_percpu
[cpu
],
184 sizeof(struct rtstatistics
))))
190 SYSCTL_PROC(_net_route
, OID_AUTO
, stats
, (CTLTYPE_OPAQUE
|CTLFLAG_RW
),
191 0, 0, sysctl_rtstatistics
, "S,rtstatistics", "Routing statistics");
194 * Packet routing routines.
198 * Look up and fill in the "ro_rt" rtentry field in a route structure given
199 * an address in the "ro_dst" field. Always send a report on a miss and
200 * always clone routes.
203 rtalloc(struct route
*ro
)
205 rtalloc_ign(ro
, 0UL);
209 * Look up and fill in the "ro_rt" rtentry field in a route structure given
210 * an address in the "ro_dst" field. Always send a report on a miss and
211 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
215 rtalloc_ign(struct route
*ro
, u_long ignoreflags
)
217 if (ro
->ro_rt
!= NULL
) {
218 if (ro
->ro_rt
->rt_ifp
!= NULL
&& ro
->ro_rt
->rt_flags
& RTF_UP
)
223 ro
->ro_rt
= _rtlookup(&ro
->ro_dst
, ignoreflags
);
227 * Look up the route that matches the given "dst" address.
229 * Route lookup can have the side-effect of creating and returning
230 * a cloned route instead when "dst" matches a cloning route and the
231 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
233 * Any route returned has its reference count incremented.
236 _rtlookup(struct sockaddr
*dst
, u_long ignore
)
238 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
241 ASSERT_NETISR_NCPUS(mycpuid
);
247 * Look up route in the radix tree.
249 rt
= (struct rtentry
*) rnh
->rnh_matchaddr((char *)dst
, rnh
);
254 * Handle cloning routes.
256 if ((rt
->rt_flags
& ~ignore
& (RTF_CLONING
| RTF_PRCLONING
)) != 0) {
257 struct rtentry
*clonedroute
;
260 clonedroute
= rt
; /* copy in/copy out parameter */
261 error
= rtrequest(RTM_RESOLVE
, dst
, NULL
, NULL
, 0,
262 &clonedroute
); /* clone the route */
263 if (error
!= 0) { /* cloning failed */
264 rt_dstmsg(RTM_MISS
, dst
, error
);
266 return (rt
); /* return the uncloned route */
268 if (clonedroute
->rt_flags
& RTF_XRESOLVE
)
269 rt_dstmsg(RTM_RESOLVE
, dst
, 0);
270 return (clonedroute
); /* return cloned route */
274 * Increment the reference count of the matched route and return.
280 rtstat
.rts_unreach
++;
281 rt_dstmsg(RTM_MISS
, dst
, 0);
286 rtfree(struct rtentry
*rt
)
289 ASSERT_NETISR_NCPUS(rt
->rt_cpuid
);
290 KASSERT(rt
->rt_refcnt
> 0, ("rtfree: rt_refcnt %ld", rt
->rt_refcnt
));
293 if (rt
->rt_refcnt
== 0) {
294 struct radix_node_head
*rnh
=
295 rt_tables
[mycpuid
][rt_key(rt
)->sa_family
];
298 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
299 if (!(rt
->rt_flags
& RTF_UP
)) {
300 /* deallocate route */
301 if (rt
->rt_ifa
!= NULL
)
303 if (rt
->rt_parent
!= NULL
)
304 RTFREE(rt
->rt_parent
); /* recursive call! */
312 rtfree_async_dispatch(netmsg_t msg
)
314 struct rtentry
*rt
= msg
->lmsg
.u
.ms_resultp
;
317 netisr_replymsg(&msg
->base
, 0);
321 rtfree_async(struct rtentry
*rt
)
323 struct netmsg_base
*msg
;
325 if (IN_NETISR_NCPUS(rt
->rt_cpuid
)) {
330 KASSERT(rt
->rt_refcnt
> 0,
331 ("rtfree_async: rt_refcnt %ld", rt
->rt_refcnt
));
333 msg
= kmalloc(sizeof(*msg
), M_LWKTMSG
, M_INTWAIT
);
334 netmsg_init(msg
, NULL
, &netisr_afree_rport
, 0, rtfree_async_dispatch
);
335 msg
->lmsg
.u
.ms_resultp
= rt
;
337 netisr_sendmsg(msg
, rt
->rt_cpuid
);
341 rtredirect_oncpu(struct sockaddr
*dst
, struct sockaddr
*gateway
,
342 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
344 struct rtentry
*rt
= NULL
;
345 struct rt_addrinfo rtinfo
;
350 ASSERT_NETISR_NCPUS(mycpuid
);
352 /* verify the gateway is directly reachable */
353 if ((ifa
= ifa_ifwithnet(gateway
)) == NULL
) {
359 * If the redirect isn't from our current router for this destination,
360 * it's either old or wrong.
362 if (!(flags
& RTF_DONE
) && /* XXX JH */
363 (rt
= rtpurelookup(dst
)) != NULL
&&
364 (!sa_equal(src
, rt
->rt_gateway
) || rt
->rt_ifa
!= ifa
)) {
370 * If it redirects us to ourselves, we have a routing loop,
371 * perhaps as a result of an interface going down recently.
373 if (ifa_ifwithaddr(gateway
)) {
374 error
= EHOSTUNREACH
;
379 * Create a new entry if the lookup failed or if we got back
380 * a wildcard entry for the default route. This is necessary
381 * for hosts which use routing redirects generated by smart
382 * gateways to dynamically build the routing tables.
386 if ((rt_mask(rt
) != NULL
&& rt_mask(rt
)->sa_len
< 2)) {
391 /* Ignore redirects for directly connected hosts. */
392 if (!(rt
->rt_flags
& RTF_GATEWAY
)) {
393 error
= EHOSTUNREACH
;
397 if (!(rt
->rt_flags
& RTF_HOST
) && (flags
& RTF_HOST
)) {
399 * Changing from a network route to a host route.
400 * Create a new host route rather than smashing the
404 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
405 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
406 rtinfo
.rti_info
[RTAX_DST
] = dst
;
407 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
408 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
409 rtinfo
.rti_flags
= flags
;
410 rtinfo
.rti_ifa
= ifa
;
411 rt
= NULL
; /* copy-in/copy-out parameter */
412 error
= rtrequest1(RTM_ADD
, &rtinfo
, &rt
);
414 flags
= rt
->rt_flags
;
415 stat
= &rtstat
.rts_dynamic
;
418 * Smash the current notion of the gateway to this destination.
419 * Should check about netmask!!!
421 rt
->rt_flags
|= RTF_MODIFIED
;
422 flags
|= RTF_MODIFIED
;
424 /* We only need to report rtmsg on CPU0 */
425 rt_setgate(rt
, rt_key(rt
), gateway
);
427 rt_rtmsg(RTM_CHANGE
, rt
, rt
->rt_ifp
, 0);
429 stat
= &rtstat
.rts_newgateway
;
437 rtstat
.rts_badredirect
++;
438 else if (stat
!= NULL
)
444 struct netmsg_rtredirect
{
445 struct netmsg_base base
;
446 struct sockaddr
*dst
;
447 struct sockaddr
*gateway
;
448 struct sockaddr
*netmask
;
450 struct sockaddr
*src
;
454 * Force a routing table entry to the specified
455 * destination to go through the given gateway.
456 * Normally called as a result of a routing redirect
457 * message from the network layer.
460 rtredirect(struct sockaddr
*dst
, struct sockaddr
*gateway
,
461 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
)
463 struct rt_addrinfo rtinfo
;
465 struct netmsg_rtredirect msg
;
467 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
468 0, rtredirect_msghandler
);
470 msg
.gateway
= gateway
;
471 msg
.netmask
= netmask
;
474 error
= netisr_domsg_global(&msg
.base
);
476 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
477 rtinfo
.rti_info
[RTAX_DST
] = dst
;
478 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
479 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
480 rtinfo
.rti_info
[RTAX_AUTHOR
] = src
;
481 rt_missmsg(RTM_REDIRECT
, &rtinfo
, flags
, error
);
485 rtredirect_msghandler(netmsg_t msg
)
487 struct netmsg_rtredirect
*rmsg
= (void *)msg
;
489 rtredirect_oncpu(rmsg
->dst
, rmsg
->gateway
, rmsg
->netmask
,
490 rmsg
->flags
, rmsg
->src
);
491 netisr_forwardmsg(&msg
->base
, mycpuid
+ 1);
495 * Routing table ioctl interface.
498 rtioctl(u_long req
, caddr_t data
, struct ucred
*cred
)
501 /* Multicast goop, grrr... */
502 return mrt_ioctl
? mrt_ioctl(req
, data
) : EOPNOTSUPP
;
509 ifa_ifwithroute(int flags
, struct sockaddr
*dst
, struct sockaddr
*gateway
)
513 if (!(flags
& RTF_GATEWAY
)) {
515 * If we are adding a route to an interface,
516 * and the interface is a point-to-point link,
517 * we should search for the destination
518 * as our clue to the interface. Otherwise
519 * we can use the local address.
522 if (flags
& RTF_HOST
) {
523 ifa
= ifa_ifwithdstaddr(dst
);
526 ifa
= ifa_ifwithaddr(gateway
);
529 * If we are adding a route to a remote net
530 * or host, the gateway may still be on the
531 * other end of a pt to pt link.
533 ifa
= ifa_ifwithdstaddr(gateway
);
536 ifa
= ifa_ifwithnet(gateway
);
540 rt
= rtpurelookup(gateway
);
544 if ((ifa
= rt
->rt_ifa
) == NULL
)
547 if (ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
548 struct ifaddr
*oldifa
= ifa
;
550 ifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
557 static int rt_fixdelete (struct radix_node
*, void *);
558 static int rt_fixchange (struct radix_node
*, void *);
562 struct radix_node_head
*rnh
;
566 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
569 rt_getifa(struct rt_addrinfo
*rtinfo
)
571 struct sockaddr
*gateway
= rtinfo
->rti_info
[RTAX_GATEWAY
];
572 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
573 struct sockaddr
*ifaaddr
= rtinfo
->rti_info
[RTAX_IFA
];
574 int flags
= rtinfo
->rti_flags
;
577 * ifp may be specified by sockaddr_dl
578 * when protocol address is ambiguous.
580 if (rtinfo
->rti_ifp
== NULL
) {
581 struct sockaddr
*ifpaddr
;
583 ifpaddr
= rtinfo
->rti_info
[RTAX_IFP
];
584 if (ifpaddr
!= NULL
&& ifpaddr
->sa_family
== AF_LINK
) {
587 ifa
= ifa_ifwithnet(ifpaddr
);
589 rtinfo
->rti_ifp
= ifa
->ifa_ifp
;
593 if (rtinfo
->rti_ifa
== NULL
&& ifaaddr
!= NULL
)
594 rtinfo
->rti_ifa
= ifa_ifwithaddr(ifaaddr
);
595 if (rtinfo
->rti_ifa
== NULL
) {
598 sa
= ifaaddr
!= NULL
? ifaaddr
:
599 (gateway
!= NULL
? gateway
: dst
);
600 if (sa
!= NULL
&& rtinfo
->rti_ifp
!= NULL
)
601 rtinfo
->rti_ifa
= ifaof_ifpforaddr(sa
, rtinfo
->rti_ifp
);
602 else if (dst
!= NULL
&& gateway
!= NULL
)
603 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, dst
, gateway
);
605 rtinfo
->rti_ifa
= ifa_ifwithroute(flags
, sa
, sa
);
607 if (rtinfo
->rti_ifa
== NULL
)
608 return (ENETUNREACH
);
610 if (rtinfo
->rti_ifp
== NULL
)
611 rtinfo
->rti_ifp
= rtinfo
->rti_ifa
->ifa_ifp
;
616 * Do appropriate manipulations of a routing tree given
617 * all the bits of info needed
622 struct sockaddr
*dst
,
623 struct sockaddr
*gateway
,
624 struct sockaddr
*netmask
,
626 struct rtentry
**ret_nrt
)
628 struct rt_addrinfo rtinfo
;
630 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
631 rtinfo
.rti_info
[RTAX_DST
] = dst
;
632 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
633 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
634 rtinfo
.rti_flags
= flags
;
635 return rtrequest1(req
, &rtinfo
, ret_nrt
);
641 struct sockaddr
*dst
,
642 struct sockaddr
*gateway
,
643 struct sockaddr
*netmask
,
646 struct rt_addrinfo rtinfo
;
648 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
649 rtinfo
.rti_info
[RTAX_DST
] = dst
;
650 rtinfo
.rti_info
[RTAX_GATEWAY
] = gateway
;
651 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
652 rtinfo
.rti_flags
= flags
;
653 return rtrequest1_global(req
, &rtinfo
, NULL
, NULL
, RTREQ_PRIO_NORM
);
657 struct netmsg_base base
;
659 struct rt_addrinfo
*rtinfo
;
660 rtrequest1_callback_func_t callback
;
665 rtrequest1_global(int req
, struct rt_addrinfo
*rtinfo
,
666 rtrequest1_callback_func_t callback
, void *arg
, boolean_t req_prio
)
668 struct netmsg_rtq msg
;
672 flags
= MSGF_PRIORITY
;
673 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, flags
,
674 rtrequest1_msghandler
);
675 msg
.base
.lmsg
.ms_error
= -1;
678 msg
.callback
= callback
;
680 return (netisr_domsg_global(&msg
.base
));
684 * Handle a route table request on the current cpu. Since the route table's
685 * are supposed to be identical on each cpu, an error occuring later in the
686 * message chain is considered system-fatal.
689 rtrequest1_msghandler(netmsg_t msg
)
691 struct netmsg_rtq
*rmsg
= (void *)msg
;
692 struct rt_addrinfo rtinfo
;
693 struct rtentry
*rt
= NULL
;
697 * Copy the rtinfo. We need to make sure that the original
698 * rtinfo, which is setup by the caller, in the netmsg will
699 * _not_ be changed; else the next CPU on the netmsg forwarding
700 * path will see a different rtinfo than what this CPU has seen.
702 rtinfo
= *rmsg
->rtinfo
;
704 error
= rtrequest1(rmsg
->req
, &rtinfo
, &rt
);
708 rmsg
->callback(rmsg
->req
, error
, &rtinfo
, rt
, rmsg
->arg
);
711 * RTM_DELETE's are propogated even if an error occurs, since a
712 * cloned route might be undergoing deletion and cloned routes
713 * are not necessarily replicated. An overall error is returned
714 * only if no cpus have the route in question.
716 if (rmsg
->base
.lmsg
.ms_error
< 0 || error
== 0)
717 rmsg
->base
.lmsg
.ms_error
= error
;
719 if (error
&& rmsg
->req
!= RTM_DELETE
) {
721 panic("rtrequest1_msghandler: rtrequest table req %d, "
722 "failed on cpu%d, error %d\n",
723 rmsg
->req
, mycpuid
, error
);
725 netisr_replymsg(&rmsg
->base
, error
);
727 netisr_forwardmsg_error(&rmsg
->base
, mycpuid
+ 1,
728 rmsg
->base
.lmsg
.ms_error
);
733 rtrequest1(int req
, struct rt_addrinfo
*rtinfo
, struct rtentry
**ret_nrt
)
735 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
737 struct radix_node
*rn
;
738 struct radix_node_head
*rnh
;
740 struct sockaddr
*ndst
;
743 ASSERT_NETISR_NCPUS(mycpuid
);
745 #define gotoerr(x) { error = x ; goto bad; }
749 rt_addrinfo_print(req
, rtinfo
);
754 * Find the correct routing tree to use for this Address Family
756 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
)
757 gotoerr(EAFNOSUPPORT
);
760 * If we are adding a host route then we don't want to put
761 * a netmask in the tree, nor do we want to clone it.
763 if (rtinfo
->rti_flags
& RTF_HOST
) {
764 rtinfo
->rti_info
[RTAX_NETMASK
] = NULL
;
765 rtinfo
->rti_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
770 /* Remove the item from the tree. */
771 rn
= rnh
->rnh_deladdr((char *)rtinfo
->rti_info
[RTAX_DST
],
772 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
776 KASSERT(!(rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)),
777 ("rnh_deladdr returned flags 0x%x", rn
->rn_flags
));
778 rt
= (struct rtentry
*)rn
;
780 /* ref to prevent a deletion race */
783 /* Free any routes cloned from this one. */
784 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
785 rt_mask(rt
) != NULL
) {
786 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
791 if (rt
->rt_gwroute
!= NULL
) {
792 RTFREE(rt
->rt_gwroute
);
793 rt
->rt_gwroute
= NULL
;
797 * NB: RTF_UP must be set during the search above,
798 * because we might delete the last ref, causing
799 * rt to get freed prematurely.
801 rt
->rt_flags
&= ~RTF_UP
;
805 rt_print(rtinfo
, rt
);
808 /* Give the protocol a chance to keep things in sync. */
809 if ((ifa
= rt
->rt_ifa
) && ifa
->ifa_rtrequest
)
810 ifa
->ifa_rtrequest(RTM_DELETE
, rt
);
813 * If the caller wants it, then it can have it,
814 * but it's up to it to free the rtentry as we won't be
817 KASSERT(rt
->rt_refcnt
>= 0,
818 ("rtrequest1(DELETE): refcnt %ld", rt
->rt_refcnt
));
819 if (ret_nrt
!= NULL
) {
820 /* leave ref intact for return */
823 /* deref / attempt to destroy */
829 if (ret_nrt
== NULL
|| (rt
= *ret_nrt
) == NULL
)
832 if (!(rt
->rt_ifp
->if_flags
& IFF_UP
))
835 KASSERT(rt
->rt_cpuid
== mycpuid
,
836 ("rt resolve rt_cpuid %d, mycpuid %d",
837 rt
->rt_cpuid
, mycpuid
));
841 rt
->rt_flags
& ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
842 rtinfo
->rti_flags
|= RTF_WASCLONED
;
843 rtinfo
->rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
844 if ((rtinfo
->rti_info
[RTAX_NETMASK
] = rt
->rt_genmask
) == NULL
)
845 rtinfo
->rti_flags
|= RTF_HOST
;
846 rtinfo
->rti_info
[RTAX_MPLS1
] = rt
->rt_shim
[0];
847 rtinfo
->rti_info
[RTAX_MPLS2
] = rt
->rt_shim
[1];
848 rtinfo
->rti_info
[RTAX_MPLS3
] = rt
->rt_shim
[2];
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
));
862 if (req
== RTM_ADD
) {
863 kprintf("rtrequest1: alloc rtentry failed on "
868 bzero(rt
, sizeof(struct rtentry
));
869 rt
->rt_flags
= RTF_UP
| rtinfo
->rti_flags
;
870 rt
->rt_cpuid
= mycpuid
;
872 error
= rt_setgate(rt
, dst
, rtinfo
->rti_info
[RTAX_GATEWAY
]);
879 if (rtinfo
->rti_info
[RTAX_NETMASK
] != NULL
)
880 rt_maskedcopy(dst
, ndst
,
881 rtinfo
->rti_info
[RTAX_NETMASK
]);
883 bcopy(dst
, ndst
, dst
->sa_len
);
885 if (rtinfo
->rti_info
[RTAX_MPLS1
] != NULL
)
886 rt_setshims(rt
, rtinfo
->rti_info
);
889 * Note that we now have a reference to the ifa.
890 * This moved from below so that rnh->rnh_addaddr() can
891 * examine the ifa and ifa->ifa_ifp if it so desires.
895 rt
->rt_ifp
= ifa
->ifa_ifp
;
896 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
898 rn
= rnh
->rnh_addaddr((char *)ndst
,
899 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
902 struct rtentry
*oldrt
;
905 * We already have one of these in the tree.
906 * We do a special hack: if the old route was
907 * cloned, then we blow it away and try
908 * re-inserting the new one.
910 oldrt
= rtpurelookup(ndst
);
913 if (oldrt
->rt_flags
& RTF_WASCLONED
) {
914 rtrequest(RTM_DELETE
, rt_key(oldrt
),
917 oldrt
->rt_flags
, NULL
);
918 rn
= rnh
->rnh_addaddr((char *)ndst
,
920 rtinfo
->rti_info
[RTAX_NETMASK
],
925 /* NOTE: rt_ifa may have been changed */
929 * If it still failed to go into the tree,
930 * then un-make it (this should be a function).
933 if (rt
->rt_gwroute
!= NULL
)
934 rtfree(rt
->rt_gwroute
);
942 * If we got here from RESOLVE, then we are cloning
943 * so clone the rest, and note that we
944 * are a clone (and increment the parent's references)
946 if (req
== RTM_RESOLVE
) {
947 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
; /* copy metrics */
948 rt
->rt_rmx
.rmx_pksent
= 0; /* reset packet counter */
949 if ((*ret_nrt
)->rt_flags
&
950 (RTF_CLONING
| RTF_PRCLONING
)) {
951 rt
->rt_parent
= *ret_nrt
;
952 (*ret_nrt
)->rt_refcnt
++;
957 * if this protocol has something to add to this then
958 * allow it to do that as well.
960 if (ifa
->ifa_rtrequest
!= NULL
)
961 ifa
->ifa_rtrequest(req
, rt
);
964 * We repeat the same procedure from rt_setgate() here because
965 * it doesn't fire when we call it there because the node
966 * hasn't been added to the tree yet.
968 if (req
== RTM_ADD
&& !(rt
->rt_flags
& RTF_HOST
) &&
969 rt_mask(rt
) != NULL
) {
970 struct rtfc_arg arg
= { rt
, rnh
};
972 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
979 rt_print(rtinfo
, rt
);
982 * Return the resulting rtentry,
983 * increasing the number of references by one.
985 if (ret_nrt
!= NULL
) {
991 /* Get the item from the tree. */
992 rn
= rnh
->rnh_lookup((char *)rtinfo
->rti_info
[RTAX_DST
],
993 (char *)rtinfo
->rti_info
[RTAX_NETMASK
],
997 if (ret_nrt
!= NULL
) {
998 rt
= (struct rtentry
*)rn
;
1010 kprintf("rti %p failed error %d\n", rtinfo
, error
);
1012 kprintf("rti %p succeeded\n", rtinfo
);
1020 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1021 * (i.e., the routes related to it by the operation of cloning). This
1022 * routine is iterated over all potential former-child-routes by way of
1023 * rnh->rnh_walktree_from() above, and those that actually are children of
1024 * the late parent (passed in as VP here) are themselves deleted.
1027 rt_fixdelete(struct radix_node
*rn
, void *vp
)
1029 struct rtentry
*rt
= (struct rtentry
*)rn
;
1030 struct rtentry
*rt0
= vp
;
1032 if (rt
->rt_parent
== rt0
&&
1033 !(rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1034 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1035 rt
->rt_flags
, NULL
);
1041 * This routine is called from rt_setgate() to do the analogous thing for
1042 * adds and changes. There is the added complication in this case of a
1043 * middle insert; i.e., insertion of a new network route between an older
1044 * network route and (cloned) host routes. For this reason, a simple check
1045 * of rt->rt_parent is insufficient; each candidate route must be tested
1046 * against the (mask, value) of the new route (passed as before in vp)
1047 * to see if the new route matches it.
1049 * XXX - it may be possible to do fixdelete() for changes and reserve this
1050 * routine just for adds. I'm not sure why I thought it was necessary to do
1054 static int rtfcdebug
= 0;
1058 rt_fixchange(struct radix_node
*rn
, void *vp
)
1060 struct rtentry
*rt
= (struct rtentry
*)rn
;
1061 struct rtfc_arg
*ap
= vp
;
1062 struct rtentry
*rt0
= ap
->rt0
;
1063 struct radix_node_head
*rnh
= ap
->rnh
;
1064 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
1069 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt
, rt0
);
1072 if (rt
->rt_parent
== NULL
||
1073 (rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1075 if (rtfcdebug
) kprintf("no parent, pinned or cloning\n");
1080 if (rt
->rt_parent
== rt0
) {
1082 if (rtfcdebug
) kprintf("parent match\n");
1084 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1085 rt
->rt_flags
, NULL
);
1089 * There probably is a function somewhere which does this...
1090 * if not, there should be.
1092 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
1094 xk1
= (u_char
*)rt_key(rt0
);
1095 xm1
= (u_char
*)rt_mask(rt0
);
1096 xk2
= (u_char
*)rt_key(rt
);
1098 /* avoid applying a less specific route */
1099 xmp
= (u_char
*)rt_mask(rt
->rt_parent
);
1100 mlen
= rt_key(rt
->rt_parent
)->sa_len
;
1101 if (mlen
> rt_key(rt0
)->sa_len
) {
1104 kprintf("rt_fixchange: inserting a less "
1105 "specific route\n");
1109 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
1110 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
]) {
1113 kprintf("rt_fixchange: inserting a less "
1114 "specific route\n");
1120 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
1121 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
1123 if (rtfcdebug
) kprintf("no match\n");
1130 * OK, this node is a clone, and matches the node currently being
1131 * changed/added under the node's mask. So, get rid of it.
1134 if (rtfcdebug
) kprintf("deleting\n");
1136 return rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
1137 rt
->rt_flags
, NULL
);
1141 rt_setgate(struct rtentry
*rt0
, struct sockaddr
*dst
, struct sockaddr
*gate
)
1143 char *space
, *oldspace
;
1144 int dlen
= RT_ROUNDUP(dst
->sa_len
), glen
= RT_ROUNDUP(gate
->sa_len
);
1145 struct rtentry
*rt
= rt0
;
1146 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][dst
->sa_family
];
1148 ASSERT_NETISR_NCPUS(mycpuid
);
1151 * A host route with the destination equal to the gateway
1152 * will interfere with keeping LLINFO in the routing
1153 * table, so disallow it.
1155 if (((rt0
->rt_flags
& (RTF_HOST
| RTF_GATEWAY
| RTF_LLINFO
)) ==
1156 (RTF_HOST
| RTF_GATEWAY
)) &&
1157 dst
->sa_len
== gate
->sa_len
&&
1158 sa_equal(dst
, gate
)) {
1160 * The route might already exist if this is an RTM_CHANGE
1161 * or a routing redirect, so try to delete it.
1163 if (rt_key(rt0
) != NULL
)
1164 rtrequest(RTM_DELETE
, rt_key(rt0
), rt0
->rt_gateway
,
1165 rt_mask(rt0
), rt0
->rt_flags
, NULL
);
1166 return EADDRNOTAVAIL
;
1170 * Both dst and gateway are stored in the same malloc'ed chunk
1171 * (If I ever get my hands on....)
1172 * if we need to malloc a new chunk, then keep the old one around
1173 * till we don't need it any more.
1175 if (rt
->rt_gateway
== NULL
||
1176 glen
> RT_ROUNDUP(rt
->rt_gateway
->sa_len
)) {
1177 oldspace
= (char *)rt_key(rt
);
1178 R_Malloc(space
, char *, dlen
+ glen
);
1181 rt
->rt_nodes
->rn_key
= space
;
1183 space
= (char *)rt_key(rt
); /* Just use the old space. */
1187 /* Set the gateway value. */
1188 rt
->rt_gateway
= (struct sockaddr
*)(space
+ dlen
);
1189 bcopy(gate
, rt
->rt_gateway
, glen
);
1191 if (oldspace
!= NULL
) {
1193 * If we allocated a new chunk, preserve the original dst.
1194 * This way, rt_setgate() really just sets the gate
1195 * and leaves the dst field alone.
1197 bcopy(dst
, space
, dlen
);
1202 * If there is already a gwroute, it's now almost definitely wrong
1205 if (rt
->rt_gwroute
!= NULL
) {
1206 RTFREE(rt
->rt_gwroute
);
1207 rt
->rt_gwroute
= NULL
;
1209 if (rt
->rt_flags
& RTF_GATEWAY
) {
1211 * Cloning loop avoidance: In the presence of
1212 * protocol-cloning and bad configuration, it is
1213 * possible to get stuck in bottomless mutual recursion
1214 * (rtrequest rt_setgate rtlookup). We avoid this
1215 * by not allowing protocol-cloning to operate for
1216 * gateways (which is probably the correct choice
1217 * anyway), and avoid the resulting reference loops
1218 * by disallowing any route to run through itself as
1219 * a gateway. This is obviously mandatory when we
1220 * get rt->rt_output().
1222 * This breaks TTCP for hosts outside the gateway! XXX JH
1224 rt
->rt_gwroute
= _rtlookup(gate
, RTF_PRCLONING
);
1225 if (rt
->rt_gwroute
== rt
) {
1226 rt
->rt_gwroute
= NULL
;
1228 return EDQUOT
; /* failure */
1233 * This isn't going to do anything useful for host routes, so
1234 * don't bother. Also make sure we have a reasonable mask
1235 * (we don't yet have one during adds).
1237 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != NULL
) {
1238 struct rtfc_arg arg
= { rt
, rnh
};
1240 rnh
->rnh_walktree_from(rnh
, (char *)rt_key(rt
),
1241 (char *)rt_mask(rt
),
1242 rt_fixchange
, &arg
);
1250 struct sockaddr
*src
,
1251 struct sockaddr
*dst
,
1252 struct sockaddr
*netmask
)
1254 u_char
*cp1
= (u_char
*)src
;
1255 u_char
*cp2
= (u_char
*)dst
;
1256 u_char
*cp3
= (u_char
*)netmask
;
1257 u_char
*cplim
= cp2
+ *cp3
;
1258 u_char
*cplim2
= cp2
+ *cp1
;
1260 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
1265 *cp2
++ = *cp1
++ & *cp3
++;
1267 bzero(cp2
, cplim2
- cp2
);
1271 rt_llroute(struct sockaddr
*dst
, struct rtentry
*rt0
, struct rtentry
**drt
)
1273 struct rtentry
*up_rt
, *rt
;
1275 ASSERT_NETISR_NCPUS(mycpuid
);
1277 if (!(rt0
->rt_flags
& RTF_UP
)) {
1278 up_rt
= rtlookup(dst
);
1280 return (EHOSTUNREACH
);
1284 if (up_rt
->rt_flags
& RTF_GATEWAY
) {
1285 if (up_rt
->rt_gwroute
== NULL
) {
1286 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1287 if (up_rt
->rt_gwroute
== NULL
)
1288 return (EHOSTUNREACH
);
1289 } else if (!(up_rt
->rt_gwroute
->rt_flags
& RTF_UP
)) {
1290 rtfree(up_rt
->rt_gwroute
);
1291 up_rt
->rt_gwroute
= rtlookup(up_rt
->rt_gateway
);
1292 if (up_rt
->rt_gwroute
== NULL
)
1293 return (EHOSTUNREACH
);
1295 rt
= up_rt
->rt_gwroute
;
1298 if (rt
->rt_flags
& RTF_REJECT
&&
1299 (rt
->rt_rmx
.rmx_expire
== 0 || /* rt doesn't expire */
1300 time_uptime
< rt
->rt_rmx
.rmx_expire
)) /* rt not expired */
1301 return (rt
->rt_flags
& RTF_HOST
? EHOSTDOWN
: EHOSTUNREACH
);
1306 struct rt_purgecloned_arg
{
1312 rt_purgecloned_callback(struct radix_node
*rn
, void *xap
)
1314 struct rtentry
*rt
= (struct rtentry
*)rn
;
1315 struct rt_purgecloned_arg
*arg
= xap
;
1317 if (rt
->rt_ifp
== arg
->ifp
&& rt
->rt_flags
& RTF_WASCLONED
)
1318 rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
1323 rt_purgecloned(struct ifnet
*ifp
, int af
)
1325 struct radix_node_head
*rnh
;
1326 struct rt_purgecloned_arg arg
= {
1333 if ((rnh
= rt_tables
[mycpuid
][af
]) != NULL
)
1334 rnh
->rnh_walktree(rnh
, rt_purgecloned_callback
, &arg
);
1338 rt_setshims(struct rtentry
*rt
, struct sockaddr
**rt_shim
){
1341 for (i
=0; i
<3; i
++) {
1342 struct sockaddr
*shim
= rt_shim
[RTAX_MPLS1
+ i
];
1348 shimlen
= RT_ROUNDUP(shim
->sa_len
);
1349 R_Malloc(rt
->rt_shim
[i
], struct sockaddr
*, shimlen
);
1350 bcopy(shim
, rt
->rt_shim
[i
], shimlen
);
1359 * Print out a route table entry
1362 rt_print(struct rt_addrinfo
*rtinfo
, struct rtentry
*rn
)
1364 kprintf("rti %p cpu %d route %p flags %08lx: ",
1365 rtinfo
, mycpuid
, rn
, rn
->rt_flags
);
1366 sockaddr_print(rt_key(rn
));
1368 sockaddr_print(rt_mask(rn
));
1370 sockaddr_print(rn
->rt_gateway
);
1371 kprintf(" ifc \"%s\"", rn
->rt_ifp
? rn
->rt_ifp
->if_dname
: "?");
1372 kprintf(" ifa %p\n", rn
->rt_ifa
);
1376 rt_addrinfo_print(int cmd
, struct rt_addrinfo
*rti
)
1382 if (cmd
== RTM_DELETE
&& route_debug
> 1)
1383 print_backtrace(-1);
1397 kprintf("C%02d ", cmd
);
1400 kprintf("rti %p cpu %d ", rti
, mycpuid
);
1401 for (i
= 0; i
< rti
->rti_addrs
; ++i
) {
1402 if (rti
->rti_info
[i
] == NULL
)
1432 kprintf("(?%02d ", i
);
1435 sockaddr_print(rti
->rti_info
[i
]);
1443 sockaddr_print(const struct sockaddr
*sa
)
1445 const struct sockaddr_in
*sa4
;
1446 const struct sockaddr_in6
*sa6
;
1455 len
= sa
->sa_len
- offsetof(struct sockaddr
, sa_data
[0]);
1457 switch(sa
->sa_family
) {
1461 switch(sa
->sa_family
) {
1463 sa4
= (const struct sockaddr_in
*)sa
;
1464 kprintf("INET %d %d.%d.%d.%d",
1465 ntohs(sa4
->sin_port
),
1466 (ntohl(sa4
->sin_addr
.s_addr
) >> 24) & 255,
1467 (ntohl(sa4
->sin_addr
.s_addr
) >> 16) & 255,
1468 (ntohl(sa4
->sin_addr
.s_addr
) >> 8) & 255,
1469 (ntohl(sa4
->sin_addr
.s_addr
) >> 0) & 255
1473 sa6
= (const struct sockaddr_in6
*)sa
;
1474 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1475 ntohs(sa6
->sin6_port
),
1476 ntohs(sa6
->sin6_addr
.s6_addr16
[0]),
1477 ntohs(sa6
->sin6_addr
.s6_addr16
[1]),
1478 ntohs(sa6
->sin6_addr
.s6_addr16
[2]),
1479 ntohs(sa6
->sin6_addr
.s6_addr16
[3]),
1480 ntohs(sa6
->sin6_addr
.s6_addr16
[4]),
1481 ntohs(sa6
->sin6_addr
.s6_addr16
[5]),
1482 ntohs(sa6
->sin6_addr
.s6_addr16
[6]),
1483 ntohs(sa6
->sin6_addr
.s6_addr16
[7])
1487 kprintf("AF%d ", sa
->sa_family
);
1488 while (len
> 0 && sa
->sa_data
[len
-1] == 0)
1491 for (i
= 0; i
< len
; ++i
) {
1494 kprintf("%d", (unsigned char)sa
->sa_data
[i
]);
1504 * Set up a routing table entry, normally for an interface.
1507 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
1509 struct sockaddr
*dst
, *deldst
, *netmask
;
1510 struct mbuf
*m
= NULL
;
1511 struct radix_node_head
*rnh
;
1512 struct radix_node
*rn
;
1513 struct rt_addrinfo rtinfo
;
1518 if (flags
& RTF_HOST
) {
1519 dst
= ifa
->ifa_dstaddr
;
1522 dst
= ifa
->ifa_addr
;
1523 netmask
= ifa
->ifa_netmask
;
1526 * If it's a delete, check that if it exists, it's on the correct
1527 * interface or we might scrub a route to another ifa which would
1528 * be confusing at best and possibly worse.
1530 if (cmd
== RTM_DELETE
) {
1532 * It's a delete, so it should already exist..
1533 * If it's a net, mask off the host bits
1534 * (Assuming we have a mask)
1536 if (netmask
!= NULL
) {
1537 m
= m_get(M_NOWAIT
, MT_SONAME
);
1541 deldst
= mtod(m
, struct sockaddr
*);
1542 rt_maskedcopy(dst
, deldst
, netmask
);
1546 * Look up an rtentry that is in the routing tree and
1547 * contains the correct info.
1549 if ((rnh
= rt_tables
[mycpuid
][dst
->sa_family
]) == NULL
||
1550 (rn
= rnh
->rnh_lookup((char *)dst
,
1551 (char *)netmask
, rnh
)) == NULL
||
1552 ((struct rtentry
*)rn
)->rt_ifa
!= ifa
||
1553 !sa_equal((struct sockaddr
*)rn
->rn_key
, dst
)) {
1556 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1562 * One would think that as we are deleting, and we know
1563 * it doesn't exist, we could just return at this point
1564 * with an "ELSE" clause, but apparently not..
1566 return (flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1571 * Do the actual request
1573 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1574 rtinfo
.rti_info
[RTAX_DST
] = dst
;
1575 rtinfo
.rti_info
[RTAX_GATEWAY
] = ifa
->ifa_addr
;
1576 rtinfo
.rti_info
[RTAX_NETMASK
] = netmask
;
1577 rtinfo
.rti_flags
= flags
| ifa
->ifa_flags
;
1578 rtinfo
.rti_ifa
= ifa
;
1579 error
= rtrequest1_global(cmd
, &rtinfo
, rtinit_rtrequest_callback
, ifa
,
1587 rtinit_rtrequest_callback(int cmd
, int error
,
1588 struct rt_addrinfo
*rtinfo
, struct rtentry
*rt
,
1591 struct ifaddr
*ifa
= arg
;
1593 if (error
== 0 && rt
) {
1595 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
1596 if (cmd
== RTM_DELETE
) {
1597 if (rt
->rt_refcnt
== 0) {
1606 struct netmsg_base base
;
1608 struct rt_addrinfo
*rtinfo
;
1609 rtsearch_callback_func_t callback
;
1611 boolean_t exact_match
;
1616 rtsearch_global(int req
, struct rt_addrinfo
*rtinfo
,
1617 rtsearch_callback_func_t callback
, void *arg
, boolean_t exact_match
,
1620 struct netmsg_rts msg
;
1624 flags
= MSGF_PRIORITY
;
1625 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, flags
,
1626 rtsearch_msghandler
);
1628 msg
.rtinfo
= rtinfo
;
1629 msg
.callback
= callback
;
1631 msg
.exact_match
= exact_match
;
1633 return (netisr_domsg_global(&msg
.base
));
1637 rtsearch_msghandler(netmsg_t msg
)
1639 struct netmsg_rts
*rmsg
= (void *)msg
;
1640 struct rt_addrinfo rtinfo
;
1641 struct radix_node_head
*rnh
;
1645 ASSERT_NETISR_NCPUS(mycpuid
);
1648 * Copy the rtinfo. We need to make sure that the original
1649 * rtinfo, which is setup by the caller, in the netmsg will
1650 * _not_ be changed; else the next CPU on the netmsg forwarding
1651 * path will see a different rtinfo than what this CPU has seen.
1653 rtinfo
= *rmsg
->rtinfo
;
1656 * Find the correct routing tree to use for this Address Family
1658 if ((rnh
= rt_tables
[mycpuid
][rtinfo
.rti_dst
->sa_family
]) == NULL
) {
1660 panic("partially initialized routing tables");
1661 netisr_replymsg(&rmsg
->base
, EAFNOSUPPORT
);
1666 * Correct rtinfo for the host route searching.
1668 if (rtinfo
.rti_flags
& RTF_HOST
) {
1669 rtinfo
.rti_netmask
= NULL
;
1670 rtinfo
.rti_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
1673 rt
= (struct rtentry
*)
1674 rnh
->rnh_lookup((char *)rtinfo
.rti_dst
,
1675 (char *)rtinfo
.rti_netmask
, rnh
);
1678 * If we are asked to do the "exact match", we need to make sure
1679 * that host route searching got a host route while a network
1680 * route searching got a network route.
1682 if (rt
!= NULL
&& rmsg
->exact_match
&&
1683 ((rt
->rt_flags
^ rtinfo
.rti_flags
) & RTF_HOST
))
1688 * No matching routes have been found, don't count this
1689 * as a critical error (here, we set 'error' to 0), just
1690 * keep moving on, since at least prcloned routes are not
1691 * duplicated onto each CPU.
1698 error
= rmsg
->callback(rmsg
->req
, &rtinfo
, rt
, rmsg
->arg
,
1702 if (error
== EJUSTRETURN
) {
1703 netisr_replymsg(&rmsg
->base
, 0);
1709 KKASSERT(rmsg
->found_cnt
> 0);
1712 * Under following cases, unrecoverable error has
1714 * o Request is RTM_GET
1715 * o The first time that we find the route, but the
1716 * modification fails.
1718 if (rmsg
->req
!= RTM_GET
&& rmsg
->found_cnt
> 1) {
1719 panic("rtsearch_msghandler: unrecoverable error "
1722 netisr_replymsg(&rmsg
->base
, error
);
1724 if (rmsg
->found_cnt
== 0) {
1725 /* The requested route has not been seen ... */
1728 netisr_forwardmsg_error(&rmsg
->base
, mycpuid
+ 1, error
);
1733 rtmask_add_global(struct sockaddr
*mask
, boolean_t req_prio
)
1735 struct netmsg_base msg
;
1739 flags
= MSGF_PRIORITY
;
1740 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
, flags
,
1741 rtmask_add_msghandler
);
1742 msg
.lmsg
.u
.ms_resultp
= mask
;
1744 return (netisr_domsg_global(&msg
));
1748 _rtmask_lookup(struct sockaddr
*mask
, boolean_t search
)
1750 struct radix_node
*n
;
1752 #define clen(s) (*(u_char *)(s))
1753 n
= rn_addmask((char *)mask
, search
, 1, rn_cpumaskhead(mycpuid
));
1755 mask
->sa_len
>= clen(n
->rn_key
) &&
1756 bcmp((char *)mask
+ 1,
1757 (char *)n
->rn_key
+ 1, clen(n
->rn_key
) - 1) == 0) {
1758 return (struct sockaddr
*)n
->rn_key
;
1766 rtmask_add_msghandler(netmsg_t msg
)
1768 struct sockaddr
*mask
= msg
->lmsg
.u
.ms_resultp
;
1770 ASSERT_NETISR_NCPUS(mycpuid
);
1772 if (rtmask_lookup(mask
) == NULL
) {
1773 netisr_replymsg(&msg
->base
, ENOBUFS
);
1776 netisr_forwardmsg(&msg
->base
, mycpuid
+ 1);
1779 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1780 SYSINIT(route
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_THIRD
, route_init
, 0);
1782 struct rtchange_arg
{
1783 struct ifaddr
*old_ifa
;
1784 struct ifaddr
*new_ifa
;
1790 rtchange_ifa(struct rtentry
*rt
, struct rtchange_arg
*ap
)
1792 if (rt
->rt_ifa
->ifa_rtrequest
!= NULL
)
1793 rt
->rt_ifa
->ifa_rtrequest(RTM_DELETE
, rt
);
1794 IFAFREE(rt
->rt_ifa
);
1796 IFAREF(ap
->new_ifa
);
1797 rt
->rt_ifa
= ap
->new_ifa
;
1798 rt
->rt_ifp
= ap
->new_ifa
->ifa_ifp
;
1799 if (rt
->rt_ifa
->ifa_rtrequest
!= NULL
)
1800 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
);
1806 rtchange_callback(struct radix_node
*rn
, void *xap
)
1808 struct rtchange_arg
*ap
= xap
;
1809 struct rtentry
*rt
= (struct rtentry
*)rn
;
1811 if (rt
->rt_ifa
== ap
->old_ifa
) {
1812 if (rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) {
1814 * We could saw the branch off when we are
1815 * still sitting on it, if the ifa_rtrequest
1816 * DEL/ADD are called directly from here.
1821 rtchange_ifa(rt
, ap
);
1826 struct netmsg_rtchange
{
1827 struct netmsg_base base
;
1828 struct ifaddr
*old_ifa
;
1829 struct ifaddr
*new_ifa
;
1834 rtchange_dispatch(netmsg_t msg
)
1836 struct netmsg_rtchange
*rmsg
= (void *)msg
;
1837 struct radix_node_head
*rnh
;
1838 struct rtchange_arg arg
;
1842 ASSERT_NETISR_NCPUS(cpu
);
1844 memset(&arg
, 0, sizeof(arg
));
1845 arg
.old_ifa
= rmsg
->old_ifa
;
1846 arg
.new_ifa
= rmsg
->new_ifa
;
1848 rnh
= rt_tables
[cpu
][AF_INET
];
1852 KKASSERT(arg
.rt
== NULL
);
1853 error
= rnh
->rnh_walktree(rnh
, rtchange_callback
, &arg
);
1854 if (arg
.rt
!= NULL
) {
1859 rtchange_ifa(rt
, &arg
);
1867 netisr_forwardmsg(&rmsg
->base
, cpu
+ 1);
1871 rtchange(struct ifaddr
*old_ifa
, struct ifaddr
*new_ifa
)
1873 struct netmsg_rtchange msg
;
1876 * XXX individual requests are not independantly chained,
1877 * which means that the per-cpu route tables will not be
1878 * consistent in the middle of the operation. If routes
1879 * related to the interface are manipulated while we are
1880 * doing this the inconsistancy could trigger a panic.
1882 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, MSGF_PRIORITY
,
1884 msg
.old_ifa
= old_ifa
;
1885 msg
.new_ifa
= new_ifa
;
1887 netisr_domsg_global(&msg
.base
);
1890 old_ifa
->ifa_flags
&= ~IFA_ROUTE
;
1891 new_ifa
->ifa_flags
|= IFA_ROUTE
;