Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: arp.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: @(#)arp.fth 2.16 02/08/22 | |
43 | purpose: Address Resolution Protocol (ARP) and Reverse ARP (RARP) | |
44 | copyright: Copyright 1990-2002 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | \ Address Resolution Protocol (ARP) | |
48 | \ Given the local Ethernet address, finds a server's Ethernet address | |
49 | \ | |
50 | \ Reverse Address Resolution Protocol (RARP) | |
51 | \ Given the local Ethernet address, finds corresponding Internet address | |
52 | \ | |
53 | \ These protocols are specific to both Ethernet and Internet, since | |
54 | \ their purpose is to relate corresponding addresses from the two | |
55 | \ families. | |
56 | ||
57 | decimal | |
58 | ||
59 | headerless | |
60 | h# 806 constant ARP_TYPE | |
61 | h# 8035 constant RARP_TYPE | |
62 | ||
63 | \ Request structure shared between ARP and RARP | |
64 | ||
65 | struct ( arp-packet) | |
66 | 2 field >arp-hw \ set to 1 for ethernet | |
67 | 2 field >arp-protocol \ set to IP_TYPE | |
68 | 1 field >arp-hwlen \ set to 6 for ethernet | |
69 | 1 field >arp-protolen \ set to 4 for IP | |
70 | 2 field >arp-opcode \ 1 arp req., 2 arp reply, 3 rarp req., 4 rarp reply | |
71 | 6 field >arp-sha \ sender hardware address | |
72 | 4 field >arp-spa \ sender protocol address | |
73 | 6 field >arp-tha \ target hardware address | |
74 | 4 field >arp-tpa \ target protocol address | |
75 | constant /arp-packet | |
76 | ||
77 | : arp-hw ( -- adr ) active-struct@ >arp-hw ; | |
78 | : arp-protocol ( -- adr ) active-struct@ >arp-protocol ; | |
79 | : arp-hwlen ( -- adr ) active-struct@ >arp-hwlen ; | |
80 | : arp-protolen ( -- adr ) active-struct@ >arp-protolen ; | |
81 | : arp-opcode ( -- adr ) active-struct@ >arp-opcode ; | |
82 | : arp-sha ( -- adr ) active-struct@ >arp-sha ; | |
83 | : arp-spa ( -- adr ) active-struct@ >arp-spa ; | |
84 | : arp-tha ( -- adr ) active-struct@ >arp-tha ; | |
85 | : arp-tpa ( -- adr ) active-struct@ >arp-tpa ; | |
86 | ||
87 | 1 constant ARP_REQ | |
88 | 2 constant ARP_REPLY | |
89 | 3 constant RARP_REQ | |
90 | 4 constant RARP_REPLY | |
91 | ||
92 | /ether-header /arp-packet + constant /ether+arp | |
93 | ||
94 | \ Initial timeout for ARP/RARP packets set to 1 second | |
95 | d# 1000 instance value arp-timeout-msecs | |
96 | ||
97 | \ Common ARP/RARP request packet constructor | |
98 | : send-arp/rarp-packet ( target-ip target-en my-ip my-en req-type en-type -- ok? ) | |
99 | *buffer @ active-struct ! \ Set the Ethernet header | |
100 | my-en-addr en-source-addr 6 cmove | |
101 | broadcast-en-addr en-dest-addr 6 cmove | |
102 | ( ... en-type ) en-type be-w! | |
103 | ||
104 | /ether-header active-struct +! \ Set the ARP/RARP protocol packet | |
105 | 1 arp-hw be-w! | |
106 | IP_TYPE arp-protocol be-w! | |
107 | 6 arp-hwlen c! | |
108 | 4 arp-protolen c! | |
109 | ( ... req-type ) arp-opcode be-w! | |
110 | ( ... my-en-addr ) arp-sha 6 cmove | |
111 | ( ... my-ip-addr ) arp-spa 4 cmove | |
112 | ( ... target-en ) arp-tha 6 cmove | |
113 | ( target-ip ) arp-tpa 4 cmove | |
114 | ||
115 | /ether+arp transmit | |
116 | ; | |
117 | ||
118 | instance variable arp-timeout | |
119 | ||
120 | \ Use exponential backoff (with maximum timeout of 32 seconds) between retries | |
121 | : arp-backoff ( -- ) arp-timeout @ 2* d# 32000 min arp-timeout ! ; | |
122 | ||
123 | : arpcom ( target-ip target-en my-ip my-en req-type en-type -- ok? ) | |
124 | send-arp/rarp-packet dup 0= if | |
125 | ." ARP/RARP send failed. Check Ethernet cable and transceiver." cr | |
126 | exit | |
127 | then | |
128 | arp-timeout @ set-timeout | |
129 | ; | |
130 | ||
131 | : decode-arp-packet ( -- ) | |
132 | arp-sha his-en-addr 6 cmove \ grab his ethernet address | |
133 | ; | |
134 | ||
135 | : .arp/rarp-timeout ( -- ) | |
136 | ." Timeout waiting for ARP/RARP packet" cr | |
137 | ; | |
138 | ||
139 | : try-arp ( -- ) | |
140 | his-ip-addr his-en-addr my-ip-addr my-en-addr ARP_REQ ARP_TYPE | |
141 | arpcom if | |
142 | begin | |
143 | ARP_TYPE receive-ethernet-packet 0<> | |
144 | while ( adr len ) | |
145 | drop ( adr ) | |
146 | /ether-header + active-struct ! | |
147 | arp-tpa my-ip-addr ip= if \ Addressed to me | |
148 | arp-opcode be-w@ ARP_REPLY = if \ ARP reply | |
149 | decode-arp-packet exit | |
150 | then | |
151 | then | |
152 | repeat ( ) | |
153 | .arp/rarp-timeout arp-backoff | |
154 | then | |
155 | ; | |
156 | ||
157 | headers | |
158 | ||
159 | : do-arp ( -- ) | |
160 | arp-timeout-msecs arp-timeout ! | |
161 | reserve-buffer | |
162 | begin his-en-addr broadcast-en-addr? while try-arp repeat | |
163 | release-buffer | |
164 | ; | |
165 | ||
166 | headerless | |
167 | ||
168 | \ Handle incoming arp packets if we know our address | |
169 | : arp-response ( adr len -- ) | |
170 | /ether+arp < if drop exit then \ Packet length filter | |
171 | active-struct ! | |
172 | en-type be-w@ ARP_TYPE <> if exit then \ Packet type filter | |
173 | /ether-header active-struct +! \ Select ARP structure | |
174 | arp-protocol be-w@ IP_TYPE <> if exit then \ Type filter | |
175 | ||
176 | \ sunmon code locks onto his Ethernet address here, but I don't | |
177 | \ think that is necessary, because we should only be ARP'ed if we | |
178 | \ haven't already found the correct IP address. | |
179 | ||
180 | arp-opcode be-w@ ARP_REQ <> if exit then \ Type filter | |
181 | arp-tpa my-ip-addr ip= 0= if exit then \ For somebody else? | |
182 | ||
183 | \ All the checks have succeeded, so we can send the reply | |
184 | ARP_REPLY arp-opcode be-w! | |
185 | arp-sha arp-tha 6 cmove | |
186 | arp-sha | |
187 | /ether-header negate active-struct +! | |
188 | en-dest-addr 6 cmove | |
189 | my-en-addr en-source-addr 6 cmove | |
190 | /ether-header active-struct +! | |
191 | my-en-addr arp-sha 6 cmove ( adr ) | |
192 | arp-spa arp-tpa 4 cmove | |
193 | my-ip-addr arp-spa 4 cmove | |
194 | ||
195 | active-struct@ /ether-header - /ether+arp " write" $call-parent drop | |
196 | ; | |
197 | ' arp-response is handle-broadcast-packet | |
198 | ||
199 | : decode-rarp-packet ( -- ) | |
200 | arp-tpa my-ip-addr 4 cmove \ grab my IP address | |
201 | arp-spa his-ip-addr 4 cmove \ grab his IP address | |
202 | arp-sha his-en-addr 6 cmove \ grab his ethernet address | |
203 | ; | |
204 | ||
205 | : try-rarp ( -- ) | |
206 | broadcast-ip-addr my-en-addr my-ip-addr my-en-addr RARP_REQ RARP_TYPE | |
207 | arpcom if | |
208 | begin | |
209 | RARP_TYPE receive-ethernet-packet 0<> | |
210 | while ( adr len ) | |
211 | drop ( len ) | |
212 | /ether-header + active-struct ! | |
213 | arp-tha my-en-addr 6 comp 0= \ Addressed to me | |
214 | arp-opcode be-w@ RARP_REPLY = and if \ RARP reply | |
215 | decode-rarp-packet exit | |
216 | then | |
217 | repeat ( ) | |
218 | .arp/rarp-timeout arp-backoff | |
219 | then | |
220 | ; | |
221 | ||
222 | headers | |
223 | ||
224 | : do-rarp ( -- ) | |
225 | arp-timeout-msecs arp-timeout ! | |
226 | reserve-buffer | |
227 | begin my-ip-addr l@ 0= while try-rarp repeat | |
228 | release-buffer | |
229 | ; |