Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: route.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)route.fth 1.1 04/09/08 | |
43 | purpose: Routing table management | |
44 | copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | \ RFC 1122: Requirements for Internet Hosts -- Communication Layers | |
48 | ||
49 | \ Routing table entries are keyed on destination host addresses. Each | |
50 | \ routing table entry (other than the default route entry) contains the | |
51 | \ destination IP address and the IP address of the corresponding | |
52 | \ next-hop router. | |
53 | ||
54 | headerless | |
55 | ||
56 | d# 32 constant RT_TABLE_SIZE \ Routing table size | |
57 | ||
58 | struct | |
59 | /ip-addr field >rt-dest \ Destination IP | |
60 | /ip-addr field >rt-gateway \ Next hop | |
61 | /c field >rt-flags \ Flags | |
62 | constant /route-entry | |
63 | ||
64 | 1 constant RT_DEFAULT \ Default route | |
65 | 2 constant RT_HOST \ Route to host | |
66 | ||
67 | 0 instance value routing-table \ Routing table | |
68 | 0 instance value rt-next \ Next free routing table entry | |
69 | ||
70 | : index>route-entry ( index -- adr ) /route-entry * routing-table + ; | |
71 | ||
72 | \ Initialize routing table data structures. | |
73 | : init-routing-table ( -- ) | |
74 | RT_TABLE_SIZE /route-entry * dup alloc-mem ( n adr ) | |
75 | tuck swap erase to routing-table ( ) | |
76 | 0 to rt-next ( ) | |
77 | ; | |
78 | ||
79 | \ Free routing table structures. | |
80 | : free-routing-table ( -- ) | |
81 | routing-table RT_TABLE_SIZE /route-entry * free-mem | |
82 | 0 to routing-table | |
83 | ; | |
84 | ||
85 | \ Check for routing entry match. | |
86 | : rtentry-match? ( dest-ip rtflags rtentry -- flag ) | |
87 | tuck >rt-flags c@ = if ( ip entry ) | |
88 | >rt-dest ip= ( flag ) | |
89 | else ( ip entry ) | |
90 | 2drop false ( false ) | |
91 | then ( flag ) | |
92 | ; | |
93 | ||
94 | \ Get specified route. | |
95 | : find-route-entry ( dest-ip rtflags -- rtentry ) | |
96 | rt-next 0 ?do ( ip flags ) | |
97 | i index>route-entry 3dup rtentry-match? if ( ip flags entry ) | |
98 | nip nip unloop exit ( entry ) | |
99 | then drop ( ip flags ) | |
100 | loop 2drop 0 ( 0 ) | |
101 | ; | |
102 | ||
103 | \ Set routing table entry fields. | |
104 | : set-route-entry ( dest-ip gateway-ip rtflags rtentry -- ) | |
105 | tuck >rt-flags c! ( ip gateway entry ) | |
106 | tuck >rt-gateway copy-ip-addr ( ip entry ) | |
107 | >rt-dest copy-ip-addr ( ) | |
108 | ; | |
109 | ||
110 | \ Add a route to the routing table. | |
111 | : route-add ( dest-ip gateway-ip flags -- ) | |
112 | rt-next RT_TABLE_SIZE = if ( ip gateway flags ) | |
113 | 3drop ." Routing Table Full" cr ( ) | |
114 | else ( ip gateway flags ) | |
115 | rt-next index>route-entry set-route-entry ( ) | |
116 | rt-next 1+ to rt-next ( ) | |
117 | then ( ) | |
118 | ; | |
119 | ||
120 | \ Update a routing table entry. Called by ICMP redirect processing. | |
121 | : route-update ( dest-ip gateway-ip flags -- ) | |
122 | 2 pick over find-route-entry ?dup if ( ip gateway flags entry ) | |
123 | set-route-entry ( ) | |
124 | else ( ip gateway flags ) | |
125 | 3drop ( ) | |
126 | then ( ) | |
127 | ; | |
128 | ||
129 | \ Get route to specified destination. If a routing table entry for the | |
130 | \ specified destination is not found, select the default router as the | |
131 | \ next-hop, but build an entry for this destination. If the default | |
132 | \ router is not the best next-hop, the route entry will be updated by | |
133 | \ the incoming ICMP redirect message. | |
134 | ||
135 | : route-get ( dest-ip -- gateway-ip | 0 ) | |
136 | dup RT_HOST find-route-entry ?dup if ( ip entry ) | |
137 | nip >rt-gateway ( gateway ) | |
138 | else ( ip ) | |
139 | inaddr-any RT_DEFAULT find-route-entry dup if ( ip entry ) | |
140 | >rt-gateway 2dup RT_HOST route-add ( ip gateway ) | |
141 | then nip ( gateway | 0 ) | |
142 | then ( gateway | 0 ) | |
143 | ; | |
144 | ||
145 | \ If the destination is not on the directly connected network, consult | |
146 | \ the routing table to get the next-hop router. | |
147 | : ipdest>nexthop ( dest-ip -- nexthop | 0 ) | |
148 | dup ip=localaddr? 0= if route-get then | |
149 | ; | |
150 | ||
151 | [ifdef] DEBUG | |
152 | : show-routing-table ( -- ) | |
153 | rt-next 0 ?do | |
154 | i index>route-entry | |
155 | dup >rt-dest .ipaddr 2 spaces | |
156 | dup >rt-gateway .ipaddr 2 spaces | |
157 | >rt-flags c@ RT_DEFAULT = if ." Default" else ." Host" then cr | |
158 | loop | |
159 | ; | |
160 | [then] | |
161 | ||
162 | headers |