2 * Copyright (c) 1980, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)route.c 8.2 (Berkeley) 11/15/93
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/ioctl.h>
48 #include <net/route.h>
49 #include <net/raw_cb.h>
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
54 #include <netinet/ip_mroute.h>
58 #define SA(p) ((struct sockaddr *)(p))
60 int rttrash
; /* routes not in table but not freed */
61 struct sockaddr wildcard
; /* zero valued cookie for wildcard searches */
68 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
69 if (dom
->dom_rtattach
)
70 dom
->dom_rtattach(&table
[dom
->dom_family
],
77 rn_init(); /* initialize all zeroes, all ones, mask table */
78 rtable_init((void **)rt_tables
);
82 * Packet routing routines.
86 register struct route
*ro
;
88 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
&& (ro
->ro_rt
->rt_flags
& RTF_UP
))
90 ro
->ro_rt
= rtalloc1(&ro
->ro_dst
, 1, 0UL);
94 rtalloc_ign(ro
, ignore
)
95 register struct route
*ro
;
98 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
&& (ro
->ro_rt
->rt_flags
& RTF_UP
))
100 ro
->ro_rt
= rtalloc1(&ro
->ro_dst
, 1, ignore
);
104 rtalloc1(dst
, report
, ignflags
)
105 register struct sockaddr
*dst
;
109 register struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
110 register struct rtentry
*rt
;
111 register struct radix_node
*rn
;
112 struct rtentry
*newrt
= 0;
113 struct rt_addrinfo info
;
115 int s
= splnet(), err
= 0, msgtype
= RTM_MISS
;
117 if (rnh
&& (rn
= rnh
->rnh_matchaddr((caddr_t
)dst
, rnh
)) &&
118 ((rn
->rn_flags
& RNF_ROOT
) == 0)) {
119 newrt
= rt
= (struct rtentry
*)rn
;
120 nflags
= rt
->rt_flags
& ~ignflags
;
121 if (report
&& (nflags
& (RTF_CLONING
| RTF_PRCLONING
))) {
122 err
= rtrequest(RTM_RESOLVE
, dst
, SA(0),
129 if ((rt
= newrt
) && (rt
->rt_flags
& RTF_XRESOLVE
)) {
130 msgtype
= RTM_RESOLVE
;
136 rtstat
.rts_unreach
++;
138 bzero((caddr_t
)&info
, sizeof(info
));
139 info
.rti_info
[RTAX_DST
] = dst
;
140 rt_missmsg(msgtype
, &info
, 0, err
);
149 register struct rtentry
*rt
;
151 register struct radix_node_head
*rnh
=
152 rt_tables
[rt_key(rt
)->sa_family
];
153 register struct ifaddr
*ifa
;
155 if (rt
== 0 || rnh
== 0)
158 if(rnh
->rnh_close
&& rt
->rt_refcnt
== 0) {
159 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
161 if (rt
->rt_refcnt
<= 0 && (rt
->rt_flags
& RTF_UP
) == 0) {
162 if (rt
->rt_nodes
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
))
165 if (rt
->rt_refcnt
< 0) {
166 printf("rtfree: %p not freed (neg refs)\n", rt
);
172 RTFREE(rt
->rt_parent
);
181 register struct ifaddr
*ifa
;
185 if (ifa
->ifa_refcnt
== 0)
192 * Force a routing table entry to the specified
193 * destination to go through the given gateway.
194 * Normally called as a result of a routing redirect
195 * message from the network layer.
197 * N.B.: must be called at splnet
201 rtredirect(dst
, gateway
, netmask
, flags
, src
, rtp
)
202 struct sockaddr
*dst
, *gateway
, *netmask
, *src
;
204 struct rtentry
**rtp
;
206 register struct rtentry
*rt
;
209 struct rt_addrinfo info
;
212 /* verify the gateway is directly reachable */
213 if ((ifa
= ifa_ifwithnet(gateway
)) == 0) {
217 rt
= rtalloc1(dst
, 0, 0UL);
219 * If the redirect isn't from our current router for this dst,
220 * it's either old or wrong. If it redirects us to ourselves,
221 * we have a routing loop, perhaps as a result of an interface
222 * going down recently.
224 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
225 if (!(flags
& RTF_DONE
) && rt
&&
226 (!equal(src
, rt
->rt_gateway
) || rt
->rt_ifa
!= ifa
))
228 else if (ifa_ifwithaddr(gateway
)) {
229 OS_DbgPrint(OSK_MID_TRACE
,("EHOSTUNREACH\n"));
230 error
= EHOSTUNREACH
;
235 * Create a new entry if we just got back a wildcard entry
236 * or the the lookup failed. This is necessary for hosts
237 * which use routing redirects generated by smart gateways
238 * to dynamically build the routing tables.
240 if ((rt
== 0) || (rt_mask(rt
) && rt_mask(rt
)->sa_len
< 2))
243 * Don't listen to the redirect if it's
244 * for a route to an interface.
246 if (rt
->rt_flags
& RTF_GATEWAY
) {
247 if (((rt
->rt_flags
& RTF_HOST
) == 0) && (flags
& RTF_HOST
)) {
249 * Changing from route to net => route to host.
250 * Create new route, rather than smashing route to net.
253 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
254 error
= rtrequest((int)RTM_ADD
, dst
, gateway
,
256 (struct rtentry
**)0);
257 stat
= &rtstat
.rts_dynamic
;
260 * Smash the current notion of the gateway to
261 * this destination. Should check about netmask!!!
263 rt
->rt_flags
|= RTF_MODIFIED
;
264 flags
|= RTF_MODIFIED
;
265 stat
= &rtstat
.rts_newgateway
;
266 rt_setgate(rt
, rt_key(rt
), gateway
);
269 OS_DbgPrint(OSK_MID_TRACE
,("EHOSTUNREACH\n"));
270 error
= EHOSTUNREACH
;
281 rtstat
.rts_badredirect
++;
282 else if (stat
!= NULL
)
284 bzero((caddr_t
)&info
, sizeof(info
));
285 info
.rti_info
[RTAX_DST
] = dst
;
286 info
.rti_info
[RTAX_GATEWAY
] = gateway
;
287 info
.rti_info
[RTAX_NETMASK
] = netmask
;
288 info
.rti_info
[RTAX_AUTHOR
] = src
;
289 rt_missmsg(RTM_REDIRECT
, &info
, flags
, error
);
293 * Routing table ioctl interface.
296 rtioctl(req
, data
, p
)
302 /* Multicast goop, grrr... */
303 return mrt_ioctl(req
, data
, p
);
310 ifa_ifwithroute(flags
, dst
, gateway
)
312 struct sockaddr
*dst
, *gateway
;
314 register struct ifaddr
*ifa
;
316 OS_DbgPrint(OSK_MID_TRACE
,("Called: flags %\n", flags
));
317 OskitDumpBuffer( (void *)dst
, sizeof(*dst
) );
318 OskitDumpBuffer( (void *)gateway
, sizeof(*gateway
) );
320 if ((flags
& RTF_GATEWAY
) == 0) {
322 * If we are adding a route to an interface,
323 * and the interface is a pt to pt link
324 * we should search for the destination
325 * as our clue to the interface. Otherwise
326 * we can use the local address.
329 if (flags
& RTF_HOST
) {
330 ifa
= ifa_ifwithdstaddr(dst
);
333 ifa
= ifa_ifwithaddr(gateway
);
336 * If we are adding a route to a remote net
337 * or host, the gateway may still be on the
338 * other end of a pt to pt link.
340 ifa
= ifa_ifwithdstaddr(gateway
);
343 ifa
= ifa_ifwithnet(gateway
);
345 struct rtentry
*rt
= rtalloc1(dst
, 0, 0UL);
349 if ((ifa
= rt
->rt_ifa
) == 0)
353 if (ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
354 struct ifaddr
*oifa
= ifa
;
355 ifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
361 OS_DbgPrint(OSK_MID_TRACE
,("Leaving: %x\n"));
366 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
369 static int rt_fixdelete(struct radix_node
*, void *);
371 static int rt_fixchange(struct radix_node
*, void *);
375 struct radix_node_head
*rnh
;
379 rtrequest(req
, dst
, gateway
, netmask
, flags
, ret_nrt
)
381 struct sockaddr
*dst
, *gateway
, *netmask
;
382 struct rtentry
**ret_nrt
;
384 int s
= splnet(); int error
= 0;
385 register struct rtentry
*rt
;
386 register struct radix_node
*rn
;
387 register struct radix_node_head
*rnh
;
389 struct sockaddr
*ndst
;
391 u_long prflags
= 0UL;
393 #define senderr(x) { error = x ; goto bad; }
395 if ((rnh
= rt_tables
[dst
->sa_family
]) == 0)
397 if (flags
& RTF_HOST
)
402 if ((rn
= rnh
->rnh_deladdr(dst
, netmask
, rnh
)) == 0)
404 if (rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
))
405 panic ("rtrequest delete");
406 rt
= (struct rtentry
*)rn
;
409 * Now search what's left of the subtree for any cloned
410 * routes which might have been formed from this node.
412 if ((rt
->rt_flags
& RTF_PRCLONING
) && netmask
) {
413 rnh
->rnh_walktree_from(rnh
, dst
, netmask
,
417 if (rt
->rt_gwroute
) {
418 rt
= rt
->rt_gwroute
; RTFREE(rt
);
419 (rt
= (struct rtentry
*)rn
)->rt_gwroute
= 0;
423 * NB: RTF_UP must be set during the search above,
424 * because we might delete the last ref, causing
425 * rt to get freed prematurely.
427 rt
->rt_flags
&= ~RTF_UP
;
429 if ((ifa
= rt
->rt_ifa
) && ifa
->ifa_rtrequest
)
430 ifa
->ifa_rtrequest(RTM_DELETE
, rt
, SA(0));
434 else if (rt
->rt_refcnt
<= 0) {
442 if (ret_nrt
== 0 || (rt
= *ret_nrt
) == 0)
445 flags
= rt
->rt_flags
&
446 ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
447 flags
|= RTF_WASCLONED
;
448 gateway
= rt
->rt_gateway
;
449 if ((netmask
= rt
->rt_genmask
) == 0)
455 if ((flags
& RTF_GATEWAY
) && !gateway
)
456 panic("rtrequest: GATEWAY but no gateway");
458 if ((ifa
= ifa_ifwithroute(flags
, dst
, gateway
)) == 0)
459 senderr(ENETUNREACH
);
463 R_Malloc(rt
, struct rtentry
*, sizeof(*rt
));
466 Bzero(rt
, sizeof(*rt
));
467 rt
->rt_flags
= RTF_UP
| flags
;
468 if (rt_setgate(rt
, dst
, gateway
)) {
474 rt_maskedcopy(dst
, ndst
, netmask
);
476 Bcopy(dst
, ndst
, dst
->sa_len
);
479 * This moved from below so that rnh->rnh_addaddr() can
480 * examine the ifa and ifp if it so desires.
485 rt
->rt_ifp
= ifa
->ifa_ifp
;
488 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
, (caddr_t
)netmask
,
493 * Uh-oh, we already have one of these in the tree.
494 * We do a special hack: if the route that's already
495 * there was generated by the protocol-cloning
496 * mechanism, then we just blow it away and retry
497 * the insertion of the new one.
499 rt2
= rtalloc1(dst
, 0, RTF_PRCLONING
);
500 if (rt2
&& rt2
->rt_parent
) {
501 rtrequest(RTM_DELETE
,
502 (struct sockaddr
*)rt_key(rt2
),
504 rt_mask(rt2
), rt2
->rt_flags
, 0);
506 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
,
516 rtfree(rt
->rt_gwroute
);
526 if (req
== RTM_RESOLVE
) {
527 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
; /* copy metrics */
528 if ((*ret_nrt
)->rt_flags
& RTF_PRCLONING
) {
529 rt
->rt_parent
= (*ret_nrt
);
530 (*ret_nrt
)->rt_refcnt
++;
534 if (ifa
->ifa_rtrequest
)
535 ifa
->ifa_rtrequest(req
, rt
, SA(ret_nrt
? *ret_nrt
: 0));
538 * We repeat the same procedure from rt_setgate() here because
539 * it doesn't fire when we call it there because the node
540 * hasn't been added to the tree yet.
542 if (!(rt
->rt_flags
& RTF_HOST
)) {
546 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
562 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
563 * (i.e., the routes related to it by the operation of cloning). This
564 * routine is iterated over all potential former-child-routes by way of
565 * rnh->rnh_walktree_from() above, and those that actually are children of
566 * the late parent (passed in as VP here) are themselves deleted.
570 rt_fixdelete(struct radix_node
*rn
, void *vp
)
572 struct rtentry
*rt
= (struct rtentry
*)rn
;
573 struct rtentry
*rt0
= vp
;
575 if (rt
->rt_parent
== rt0
&& !(rt
->rt_flags
& RTF_PINNED
)) {
576 return rtrequest(RTM_DELETE
, rt_key(rt
),
577 (struct sockaddr
*)0, rt_mask(rt
),
578 rt
->rt_flags
, (struct rtentry
**)0);
585 * This routine is called from rt_setgate() to do the analogous thing for
586 * adds and changes. There is the added complication in this case of a
587 * middle insert; i.e., insertion of a new network route between an older
588 * network route and (cloned) host routes. For this reason, a simple check
589 * of rt->rt_parent is insufficient; each candidate route must be tested
590 * against the (mask, value) of the new route (passed as before in vp)
591 * to see if the new route matches it. Unfortunately, this has the obnoxious
592 * property of also triggering for insertion /above/ a pre-existing network
593 * route and clones. Sigh. This may be fixed some day.
595 * XXX - it may be possible to do fixdelete() for changes and reserve this
596 * routine just for adds. I'm not sure why I thought it was necessary to do
604 rt_fixchange(struct radix_node
*rn
, void *vp
)
606 struct rtentry
*rt
= (struct rtentry
*)rn
;
607 struct rtfc_arg
*ap
= vp
;
608 struct rtentry
*rt0
= ap
->rt0
;
609 struct radix_node_head
*rnh
= ap
->rnh
;
610 u_char
*xk1
, *xm1
, *xk2
;
615 printf("rt_fixchange: rt %p, rt0 %p\n", rt
, rt0
);
618 if (!rt
->rt_parent
|| (rt
->rt_flags
& RTF_PINNED
)) {
620 if(rtfcdebug
) printf("no parent or pinned\n");
625 if (rt
->rt_parent
== rt0
) {
627 if(rtfcdebug
) printf("parent match\n");
629 return rtrequest(RTM_DELETE
, rt_key(rt
),
630 (struct sockaddr
*)0, rt_mask(rt
),
631 rt
->rt_flags
, (struct rtentry
**)0);
635 * There probably is a function somewhere which does this...
636 * if not, there should be.
638 len
= imin(((struct sockaddr
*)rt_key(rt0
))->sa_len
,
639 ((struct sockaddr
*)rt_key(rt
))->sa_len
);
641 xk1
= (u_char
*)rt_key(rt0
);
642 xm1
= (u_char
*)rt_mask(rt0
);
643 xk2
= (u_char
*)rt_key(rt
);
645 for (i
= rnh
->rnh_treetop
->rn_off
; i
< len
; i
++) {
646 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
648 if(rtfcdebug
) printf("no match\n");
655 * OK, this node is a clone, and matches the node currently being
656 * changed/added under the node's mask. So, get rid of it.
659 if(rtfcdebug
) printf("deleting\n");
661 return rtrequest(RTM_DELETE
, rt_key(rt
), (struct sockaddr
*)0,
662 rt_mask(rt
), rt
->rt_flags
, (struct rtentry
**)0);
666 rt_setgate(rt0
, dst
, gate
)
668 struct sockaddr
*dst
, *gate
;
671 int dlen
= ROUNDUP(dst
->sa_len
), glen
= ROUNDUP(gate
->sa_len
);
672 register struct rtentry
*rt
= rt0
;
673 struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
675 if (rt
->rt_gateway
== 0 || glen
> ROUNDUP(rt
->rt_gateway
->sa_len
)) {
676 old
= (caddr_t
)rt_key(rt
);
677 R_Malloc(new, caddr_t
, dlen
+ glen
);
680 rt
->rt_nodes
->rn_key
= new;
682 new = rt
->rt_nodes
->rn_key
;
685 Bcopy(gate
, (rt
->rt_gateway
= (struct sockaddr
*)(new + dlen
)), glen
);
687 Bcopy(dst
, new, dlen
);
690 if (rt
->rt_gwroute
) {
691 rt
= rt
->rt_gwroute
; RTFREE(rt
);
692 rt
= rt0
; rt
->rt_gwroute
= 0;
695 * Cloning loop avoidance:
696 * In the presence of protocol-cloning and bad configuration,
697 * it is possible to get stuck in bottomless mutual recursion
698 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing
699 * protocol-cloning to operate for gateways (which is probably the
700 * correct choice anyway), and avoid the resulting reference loops
701 * by disallowing any route to run through itself as a gateway.
702 * This is obviuosly mandatory when we get rt->rt_output().
704 if (rt
->rt_flags
& RTF_GATEWAY
) {
705 rt
->rt_gwroute
= rtalloc1(gate
, 1, RTF_PRCLONING
);
706 if (rt
->rt_gwroute
== rt
) {
707 RTFREE(rt
->rt_gwroute
);
709 return 1; /* failure */
714 * This isn't going to do anything useful for host routes, so
715 * don't bother. Also make sure we have a reasonable mask
716 * (we don't yet have one during adds).
718 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != 0) {
722 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
730 rt_maskedcopy(src
, dst
, netmask
)
731 struct sockaddr
*src
, *dst
, *netmask
;
733 register u_char
*cp1
= (u_char
*)src
;
734 register u_char
*cp2
= (u_char
*)dst
;
735 register u_char
*cp3
= (u_char
*)netmask
;
736 u_char
*cplim
= cp2
+ *cp3
;
737 u_char
*cplim2
= cp2
+ *cp1
;
739 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
744 *cp2
++ = *cp1
++ & *cp3
++;
746 bzero((caddr_t
)cp2
, (unsigned)(cplim2
- cp2
));
750 * Set up a routing table entry, normally
754 rtinit(ifa
, cmd
, flags
)
755 register struct ifaddr
*ifa
;
758 int error
= EADDRNOTAVAIL
;
760 register struct rtentry
*rt
;
761 register struct sockaddr
*dst
;
762 register struct sockaddr
*deldst
;
764 struct rtentry
*nrt
= 0;
766 dst
= flags
& RTF_HOST
? ifa
->ifa_dstaddr
: ifa
->ifa_addr
;
767 if (cmd
== RTM_DELETE
) {
768 if ((flags
& RTF_HOST
) == 0 && ifa
->ifa_netmask
) {
769 m
= m_get(M_WAIT
, MT_SONAME
);
770 deldst
= mtod(m
, struct sockaddr
*);
771 rt_maskedcopy(dst
, deldst
, ifa
->ifa_netmask
);
774 rt
= rtalloc1(dst
, 0, 0UL);
777 if (rt
->rt_ifa
!= ifa
) {
780 return (flags
& RTF_HOST
? EHOSTUNREACH
785 error
= rtrequest(cmd
, dst
, ifa
->ifa_addr
, ifa
->ifa_netmask
,
786 flags
| ifa
->ifa_flags
, &nrt
);
789 if (cmd
== RTM_DELETE
&& error
== 0 && (rt
= nrt
)) {
790 rt_newaddrmsg(cmd
, ifa
, error
, nrt
);
791 if (rt
->rt_refcnt
<= 0) {
796 if (cmd
== RTM_ADD
&& error
== 0 && (rt
= nrt
)) {
798 if (rt
->rt_ifa
!= ifa
) {
799 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa
,
801 if (rt
->rt_ifa
->ifa_rtrequest
)
802 rt
->rt_ifa
->ifa_rtrequest(RTM_DELETE
, rt
, SA(0));
805 rt
->rt_ifp
= ifa
->ifa_ifp
;
807 if (ifa
->ifa_rtrequest
)
808 ifa
->ifa_rtrequest(RTM_ADD
, rt
, SA(0));
810 rt_newaddrmsg(cmd
, ifa
, error
, nrt
);