-
Notifications
You must be signed in to change notification settings - Fork 2
/
arp.s
264 lines (242 loc) · 4.39 KB
/
arp.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
include "zombie.def"
export arp_init
export arp_in
export arp_resolve
export arp_setbroad
;;; ARP local database
;;; todo: timestamp needed?
;;; 0 1b - used marker, 0 = unused
;;; 1 6b - mac address field
;;; 7 4b - ip address field
;;; 11b - size of record
.area .data
ARPMAX equ 8
;; add some predefined entries for broadcasts
adb
rmb 11*ARPMAX
adb_end rmb 2 ; storage for pointer to itselft, (PIC)
outbuf rmb 2 ; tmp storage for outgoing buffer
.area .code
mirror .db 1
.dw $ffff,$ffff,$ffff
.db 255,255,255,255
.db 1
.dw $ffff,$ffff,$ffff
broad .db 192,168,42,255 ; todo: set arp table for local broacast
mirrore
;;; set the broadcast ip to eth mapping
arp_setbroad:
ldd ,x++
std adb+broad-mirror,pcr
ldd ,x++
std adb+broad-mirror+2,pcr
rts
;;; initialize the arp sub-system
arp_init:
leax adb_end,pcr ; so we can compare ptr to end
stx adb_end,pcr ; easily in PIC.
leay adb,pcr ; copy prebuilt broadcast entries
leau mirror,pcr
ldb #mirrore-mirror
lbsr memcpy
ldb #11*(ARPMAX-2)
lbsr memclr
rts
;;; processes incoming arp things
;;; X = ptr to layer 3
arp_in:
ldd ,x ; get hardware type
cmpd #1 ; is ethernet?
lbne ip_drop
ldd 2,x ; get protocol type
cmpd #$800 ; is ip?
lbne ip_drop
;; todo check protocol/hardware address lengths?
;; or can we rely on above type checks?
ldd 6,x
cmpd #1 ; request?
beq request_in
cmpb #2 ; reply?
beq reply_in
lbra ip_drop
request_in:
leay 24,x ; get target ip address
leau ipaddr,pcr
lbsr ip_cmp ; compare to ours
lbne ip_drop
inc 7,x ; convert packet to reply
;; copy sender to target
leay 18,x
leau 8,x
ldb #10
lbsr memcpy
;; copy sender mac to our dmac
leay dmac,pcr
leau 8,x
ldb #6
lbsr memcpy
;; send reply
ldd mac,pcr
std 8,x ; fill in our mac
ldd mac+2,pcr
std 10,x
ldd mac+4,pcr
std 12,x
ldd ipaddr,pcr ; fill in our IP
std 14,x
ldd ipaddr+2,pcr
std 16,x
ldd #$806
std type,pcr
ldd #28 ; size of arp
lbsr eth_out
lbra ip_drop
reply_in:
pshs x ; save packet ptr
leay 14,x ; compare received ip
bsr lookup ; find match in database
bcc found@
bsr findnew ; find empty entry
bcc found@
bsr purge
found@ tfr x,y
clr ,y ; mark record as used
inc ,y+ ;
puls x
leau 8,x
ldb #10 ; copy eth and ip addresses
lbsr memcpy ; to new record
lbra ip_drop
;;; purge an old record in databse
;;; returns: X = record ptr
;;; todo: actually purge something
purge:
rts
;;; find an ip address in arp database
;;; takes: y = ptr to ip
;;; returns: x = ptr to record
;;; returns: C set on error
lookup:
;; start by looking up in database
leax adb,pcr
a@ tst ,x
beq next@
leau 7,x
lbsr ip_cmp
beq found@
next@ leax 11,x
cmpx adb_end,pcr
bne a@
;; not found
coma
rts
found@ clra
rts
;;; find an unused entry in arp database
findnew:
leax adb,pcr
a@ tst ,x
beq found@
leax 11,x
cmpx adb_end,pcr
bne a@
coma
rts
found@ clra
rts
;;; attempt resolve ip address
;;; takes: x - ptr to sending pdu
;;; returns C set if arp request sent
;;; todo: some of this could be speed up by precalculating things at bind?
arp_resolve:
pshs d,x,y,u
stx outbuf,pcr
;; if not IP don't resolve
ldd type,pcr
cmpd #$800
bne ok@
;; if multicast IP then translate
ldb dipaddr,pcr
andb #$f0
cmpb #$e0
beq multicast
;; if not local use gateway mac
ldd dipaddr,pcr
anda ipmask,pcr
andb ipmask+1,pcr
cmpd ipnet,pcr
bne use_gateway
ldd dipaddr+2,pcr
anda ipmask+2,pcr
andb ipmask+3,pcr
cmpd ipnet+2,pcr
bne use_gateway
;; find in table
leay dipaddr,pcr
lbsr lookup
bcc found@
;; else send request
leau dipaddr,pcr
lbsr arp_send
bra out@
found@ leau 1,x
b@ leay dmac,pcr
ldb #6
lbsr memcpy
ok@ clra
out@ puls d,x,y,u,pc
export use_gateway
use_gateway
leay gateway,pcr
lbsr lookup
bcc found@
;; not found! arp for the gateway IP then...
c@ leau gateway,pcr
lbsr arp_send
bra out@
multicast:
ldd #$0100
std dmac,pcr
ldd #$5e00
std dmac+2,pcr
ldd #$00fb
std dmac+4,pcr
bra ok@
;;; send an arp
;;; takes U = ptr ipaddr to lookup
arp_send
ldx outbuf,pcr ; fixme: hinky... this is called on *output*
leax 5+14,x
pshs x
ldd #1
std ,x++
ldd #$800
std ,x++
ldd #$0604
std ,x++
ldd #1
std ,x++
leay 16,x
ldb #4
lbsr memcpy
;; fill out sender
leay ,x
leau mac,pcr
ldb #6
lbsr memcpy
leay 6,x
leau ipaddr,pcr
ldb #4
lbsr memcpy
;; fill out target
leay 10,x
ldb #6
lbsr memclr
;; send
ldd #$806
std type,pcr
puls x
ldd #28
lbsr eth_out
coma
rts