Rev 1187 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1187 | Rev 1196 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; ARP.INC ;; |
6 | ;; ARP.INC ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Address Resolution Protocol ;; |
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
9 | ;; ;; |
9 | ;; ;; |
10 | ;; This file contains the following: ;; |
- | |
11 | ;; arp_table_manager - Manages an ARPTable ;; |
10 | ;; Based on the work of [Johnny_B] and [smb] ;; |
12 | ;; arp_request - Sends an ARP request on the ethernet ;; |
- | |
13 | ;; arp_handler - Called when an ARP packet is received ;; |
- | |
14 | ;; ;; |
11 | ;; ;; |
15 | ;; Changes history: ;; |
- | |
16 | ;; 22.09.2003 - [Mike Hibbett] : mikeh@oceanfree.net ;; |
- | |
17 | ;; 11.11.2006 - [Johnny_B] and [smb] ;; |
12 | ;; Written by hidnplayr@kolibrios.org ;; |
18 | ;; ;; |
13 | ;; ;; |
- | 14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
|
- | 15 | ;; Version 2, June 1991 ;; |
|
- | 16 | ;; ;; |
|
19 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
- | 18 | ||
Line 20... | Line 19... | ||
20 | 19 | ||
Line 21... | Line 20... | ||
21 | $Revision: 983 $ |
20 | $Revision: 983 $ |
22 | 21 | ||
23 | 22 | ||
24 | ARP_NO_ENTRY equ 0 |
23 | ARP_NO_ENTRY equ 0 |
Line -... | Line 24... | ||
- | 24 | ARP_VALID_MAPPING equ 1 |
|
- | 25 | ARP_AWAITING_RESPONSE equ 2 |
|
- | 26 | ARP_RESPONSE_TIMEOUT equ 3 |
|
25 | ARP_VALID_MAPPING equ 1 |
27 | |
Line 26... | Line 28... | ||
26 | ARP_AWAITING_RESPONSE equ 2 |
28 | ARP_REQUEST_TTL = 20 ; in seconds |
27 | ARP_RESPONSE_TIMEOUT equ 3 |
29 | ARP_ENTRY_TTL = 30 ; in seconds |
Line 77... | Line 79... | ||
77 | 79 | ||
Line -... | Line 80... | ||
- | 80 | endg |
|
- | 81 | ||
- | 82 | ||
- | 83 | ||
- | 84 | ;----------------------------------------------------------------- |
|
- | 85 | ; |
|
- | 86 | ; ARP_init |
|
- | 87 | ; |
|
- | 88 | ; This function resets all ARP variables |
|
- | 89 | ; |
|
Line -... | Line 90... | ||
- | 90 | ; IN: / |
|
78 | endg |
91 | ; OUT: / |
Line 79... | Line 92... | ||
79 | 92 | ; |
|
Line 80... | Line 93... | ||
80 | 93 | ;----------------------------------------------------------------- |
|
Line 91... | Line 104... | ||
91 | rep stosd |
104 | rep stosd |
Line 92... | Line 105... | ||
92 | 105 | ||
Line 93... | Line -... | ||
93 | ret |
- | |
- | 106 | ret |
|
94 | 107 | ||
95 | - | ||
96 | 108 | ||
97 | - | ||
98 | ;*************************************************************************** |
109 | ;----------------------------------------------------------------- |
99 | ; Function |
- | |
100 | ; arp_table_manager [by Johnny_B] |
110 | ; |
101 | ; |
111 | ; ARP_IP_to_MAC |
102 | ; Description |
112 | ; |
103 | ; Does a most required operations with ARP-table |
113 | ; This function resets all ARP variables |
104 | ; IN: |
114 | ; |
105 | ; Operation: see Opcode's constants below |
- | |
106 | ; Index: Index of entry in the ARP-table |
115 | ; IN: eax = IPv4 address |
107 | ; Extra: Extra parameter for some Opcodes |
116 | ; OUT: eax = -1 on error, else eax = first two bytes of mac |
108 | ; OUT: |
- | |
109 | ; EAX = Returned value depends on opcodes, more detailed see below |
- | |
110 | ; |
- | |
111 | ;*************************************************************************** |
- | |
112 | ;Opcode's constants |
- | |
113 | ARP_TABLE_ADD equ 1 |
- | |
114 | ARP_TABLE_IP_TO_MAC equ 5 |
- | |
115 | 117 | ; ( high 16 bits are zero) |
|
Line 116... | Line 118... | ||
116 | ;Index's constants |
118 | ; ebx = last four bytes of mac ; TODO: special eax value for 'request send' |
117 | EXTRA_IS_ARP_PACKET_PTR equ 0 ;if Extra contain pointer to ARP_Packet |
- | |
118 | EXTRA_IS_ARP_ENTRY_PTR equ -1 ;if Extra contain pointer to ARP_ENTRY |
- | |
119 | - | ||
120 | align 4 |
- | |
121 | proc arp_table_manager stdcall uses ebx esi edi ecx edx, Opcode:DWORD,Index:DWORD,Extra:DWORD |
- | |
122 | - | ||
123 | mov ebx, ARPTable ;ARPTable base |
- | |
124 | mov ecx, dword[NumARP] ;ARP-entries counter |
- | |
125 | - | ||
126 | mov eax, dword[Opcode] |
- | |
127 | - | ||
128 | - | ||
129 | DEBUGF 1,"ARP table manager opcode:%u numARP:%u\n",eax,ecx |
- | |
130 | - | ||
131 | cmp eax, ARP_TABLE_ADD |
- | |
132 | je .add |
- | |
133 | - | ||
134 | cmp eax, ARP_TABLE_IP_TO_MAC |
- | |
135 | je .ip_to_mac |
- | |
136 | - | ||
137 | jmp .exit ;if unknown opcode |
- | |
138 | - | ||
139 | - | ||
140 | ;;BEGIN ADD |
- | |
141 | ;;Description: it adds an entry in the table. If ARP-table already |
- | |
142 | ;; contains same IP, it will be updated. |
- | |
143 | ;;IN: Operation: ARP_TABLE_ADD |
- | |
144 | ;; Index: specifies what contains Extra-parameter |
- | |
145 | ;; Extra: if Index==EXTRA_IS_ARP_Packet_PTR, |
- | |
146 | ;; then Extra contains pointer to ARP_Packet, |
- | |
147 | ;; otherwise Extra contains pointer to ARP_ENTRY |
- | |
148 | ;;OUT: |
- | |
149 | ;; EAX=index of entry, that has been added |
- | |
150 | ;; |
- | |
151 | .add: |
- | |
152 | - | ||
153 | DEBUGF 1,"1" |
- | |
154 | - | ||
155 | sub esp, ARP_ENTRY.size ;Allocate ARP_ENTRY_SIZE byte in stack |
- | |
156 | - | ||
157 | mov esi, [Extra] ;pointer |
- | |
158 | mov edi, [Index] ;opcode |
- | |
159 | - | ||
160 | cmp edi, EXTRA_IS_ARP_PACKET_PTR |
- | |
161 | je .ARP_Packet_to_entry ;if Extra contain ptr to ARP_Packet and we have to form arp-entry |
- | |
162 | ;else it contain ptr to arp-entry |
- | |
163 | - | ||
164 | DEBUGF 1,"2" |
- | |
165 | - | ||
166 | cld |
- | |
167 | ; esi already has been loaded |
- | |
168 | mov edi, esp ;ebx + eax=ARPTable_base + ARP-entry_base(where we will add) |
- | |
169 | mov ecx,ARP_ENTRY.size/2 ;ARP_ENTRY_SIZE must be even number!!! |
- | |
170 | rep movsw ;copy |
- | |
171 | jmp .search |
- | |
172 | - | ||
173 | .ARP_Packet_to_entry: |
- | |
174 | - | ||
175 | DEBUGF 1,"3" |
- | |
176 | mov edx, dword[esi + ARP_Packet.SenderIP] ;esi=base of ARP_Packet |
- | |
177 | mov [esp + ARP_ENTRY.IP], edx |
- | |
178 | - | ||
179 | cld |
- | |
180 | lea esi, [esi + ARP_Packet.SenderMAC] |
- | |
181 | lea edi, [esp + ARP_ENTRY.MAC] |
- | |
182 | movsd |
- | |
183 | movsw |
- | |
184 | mov word[esp + ARP_ENTRY.Status], ARP_VALID_MAPPING ; specify the type - a valid entry |
- | |
185 | mov word[esp + ARP_ENTRY.TTL], 0x0E10 ; = 1 hour |
- | |
186 | - | ||
187 | .search: |
- | |
188 | - | ||
189 | DEBUGF 1,"4" |
- | |
190 | mov edx, dword[esp + ARP_ENTRY.IP] ;edx=IP-address, which we'll search |
- | |
191 | mov ecx, dword[NumARP] ;ecx=ARP-entries counter |
- | |
192 | jecxz .add_to_end ;if ARP-entries number == 0 |
- | |
193 | imul eax, ecx, ARP_ENTRY.size ;eax=current table size(in bytes) |
- | |
194 | @@: |
- | |
195 | sub eax, ARP_ENTRY.size |
- | |
196 | cmp dword[ebx + eax + ARP_ENTRY.IP], edx |
119 | ; |
197 | loopnz @b |
- | |
198 | ; jz .replace ; found, replace existing entry, ptr to it is in eax |
- | |
199 | - | ||
200 | .add_to_end: |
- | |
201 | ; |
- | |
202 | ; DEBUGF 1,"5\n" |
- | |
203 | ; ;else add to end |
- | |
204 | ; or eax,-1 ;set eax=0xFFFFFFFF if adding is impossible |
- | |
205 | ; mov ecx, dword[NumARP] |
- | |
206 | ; cmp ecx, ARP_TABLE_SIZE |
- | |
207 | ; je .add_exit ;if arp-entries number is equal to arp-table maxsize |
- | |
208 | - | ||
209 | ; imul eax, dword[NumARP], ARP_ENTRY.size ;eax=ptr to end of ARPTable |
- | |
210 | ; inc dword [NumARP] ;increase ARP-entries counter |
- | |
211 | - | ||
212 | ; .replace: |
- | |
213 | DEBUGF 1,"Updating ARP entry: %x-%x-%x-%x-%x-%x = %u.%u.%u.%u to slot:%u\n",\ |
- | |
214 | [esp + ARP_ENTRY.MAC]:2,[esp + ARP_ENTRY.MAC+1]:2,[esp + ARP_ENTRY.MAC+2]:2,[esp + ARP_ENTRY.MAC+3]:2,[esp + ARP_ENTRY.MAC+4]:2,[esp + ARP_ENTRY.MAC+5]:2,\ |
- | |
215 | [esp + ARP_ENTRY.IP]:1,[esp + ARP_ENTRY.IP+1]:1,[esp + ARP_ENTRY.IP+2]:1,[esp + ARP_ENTRY.IP+3]:1,eax |
- | |
216 | - | ||
217 | cld |
- | |
218 | mov esi, esp ;esp=base of ARP-entry, that will be added |
- | |
219 | lea edi, [ebx + eax] ;ebx + eax=ARPTable_base + ARP-entry_base(where we will add) |
- | |
220 | mov ecx,ARP_ENTRY.size/2 ;ARP_ENTRY_SIZE must be even number!!! |
- | |
221 | rep movsw |
- | |
222 | - | ||
223 | mov ecx, ARP_ENTRY.size |
- | |
224 | xor edx, edx ;"div" takes operand from EDX:EAX |
- | |
225 | div ecx ;eax=index of entry, which has been added |
- | |
226 | - | ||
227 | - | ||
228 | - | ||
229 | .add_exit: |
- | |
230 | - | ||
231 | add esp, ARP_ENTRY.size ;free stack |
- | |
232 | jmp .exit |
- | |
233 | ;;END ADD |
- | |
234 | - | ||
235 | - | ||
236 | - | ||
237 | ;;BEGIN IP_TO_MAC |
- | |
238 | ;;Description: it gets an IP from Index, scans each entry in the table and writes |
- | |
239 | ;; MAC, that relates to specified IP, into buffer specified in Extra. |
- | |
240 | ;; And if it cannot find an IP-address in the table, it does an ARP-request of that. |
- | |
241 | ;;IN: Operation: ARP_TABLE_IP_TO_MAC |
- | |
242 | ;; Index: IP that should be transformed into MAC |
- | |
243 | ;; Extra: pointer to buffer where will be written the MAC-address. |
- | |
244 | ;;OUT: |
- | |
245 | ;; EAX=ARP table entry status code. |
- | |
246 | ;; If EAX==ARP_NO_ENTRY, IP isn't found in the table and we have sent the request. |
- | |
247 | ;; If EAX==ARP_AWAITING_RESPONSE, we wait the response from remote system. |
- | |
248 | ;; If EAX==ARP_RESPONSE_TIMEOUT, remote system not responds too long. |
- | |
249 | ;; If EAX==ARP_VALID_MAPPING, all is ok, we've got a true MAC. |
- | |
250 | ;; |
- | |
251 | ;; If MAC will equal to a zero, in the buffer. It means, that IP-address was not yet |
- | |
252 | ;; resolved, or that doesn't exist. I recommend you, to do at most 3-5 calls of this |
- | |
253 | ;; function with 1sec delay. sure, only if it not return a valid MAC after a first call. |
- | |
254 | ;; |
- | |
255 | .ip_to_mac: |
- | |
256 | - | ||
257 | DEBUGF 1,"Trying to find MAC for %u.%u.%u.%u\n",[Index]:1,[Index+1]:1,[Index+2]:1,[Index+3]:1 |
- | |
258 | - | ||
259 | xor eax, eax |
- | |
Line -... | Line 120... | ||
- | 120 | ;----------------------------------------------------------------- |
|
Line 260... | Line 121... | ||
260 | mov edi, dword[Extra] |
121 | |
261 | cld |
122 | align 4 |
262 | stosd |
123 | ARP_IP_to_MAC: |
263 | stosw |
124 | |
264 | 125 | DEBUGF 1,"ARP_IP_to_MAC\n" |
|
265 | 126 | ||
Line -... | Line 127... | ||
- | 127 | ; first, check destination IP to see if it is on 'this' network. |
|
Line 266... | Line -... | ||
266 | ; first, check destination IP to see if it is on 'this' network. |
- | |
267 | ; The test is: |
128 | ; The test is: |
268 | ; if ( destIP & subnet_mask == stack_ip & subnet_mask ) |
- | |
269 | ; destination is local |
- | |
270 | ; else |
- | |
271 | ; destination is remote, so pass to gateway |
129 | ; if ( destIP & subnet_mask == stack_ip & subnet_mask ) |
272 | 130 | ; destination is local |
|
273 | 131 | ; else |
|
274 | ;;; TODO: get device number ! (in edx) |
132 | ; destination is remote, so pass to gateway |
275 | xor edx, edx |
133 | |
- | 134 | xor edx, edx ; TODO: find device num in edx |
|
276 | 135 | ||
277 | mov eax, [Index] ;eax=required IP |
- | |
278 | mov esi, eax |
136 | mov ebx, [IP_LIST+edx] |
279 | and esi, [SUBNET_LIST+edx] |
- | |
Line -... | Line 137... | ||
- | 137 | and ebx, [SUBNET_LIST+edx] |
|
- | 138 | mov ecx, eax |
|
280 | mov ecx, [IP_LIST+edx] |
139 | and ecx, [SUBNET_LIST+edx] |
- | 140 | cmp ecx, ebx |
|
281 | and ecx, [SUBNET_LIST+edx] |
141 | je .local |
- | 142 | ||
- | 143 | mov eax, [GATEWAY_LIST+edx] |
|
- | 144 | DEBUGF 1,"requested IP is not on subnet, using gateway\n" |
|
282 | cmp esi, ecx |
145 | |
- | 146 | .local: |
|
- | 147 | ; try to find it on the list |
|
Line 283... | Line -... | ||
283 | je @f ;if we and target IP are located in the same network |
- | |
284 | mov eax, [GATEWAY_LIST+edx] |
148 | mov ecx, [NumARP] |
Line 285... | Line -... | ||
285 | mov [Index], eax |
- | |
286 | DEBUGF 1,"IP is not on subnet, using %u.%u.%u.%u instead\n",[Index]:1,[Index+1]:1,[Index+2]:1,[Index+3]:1 |
- | |
287 | @@: |
149 | jz .not_in_list |
- | 150 | mov esi, ARPTable + ARP_ENTRY.IP |
|
288 | 151 | .scan_loop: |
|
289 | cmp dword[NumARP], 0 |
- | |
Line 290... | Line 152... | ||
290 | je .ip_to_mac_send_request ;if ARP-table not contain an entries, we have to request IP. |
152 | scasd |
291 | ;EAX will be containing a zero, it's equal to ARP_NO_ENTRY |
153 | jz .found_it |
- | 154 | add esi, ARP_ENTRY.size - 4 |
|
- | 155 | loop .scan_loop |
|
- | 156 | .not_in_list: |
|
- | 157 | ||
Line 292... | Line 158... | ||
292 | 158 | DEBUGF 1,"IP not found on list, preparing for ARP request\n" |
|
293 | mov ecx, dword[NumARP] |
- | |
- | 159 | ||
Line 294... | Line -... | ||
294 | imul esi, ecx, ARP_ENTRY.size ;esi=current ARP-table size |
- | |
295 | 160 | ; if not, reserve an entry in list and send an ARP request packet |
|
296 | @@: |
- | |
297 | sub esi, ARP_ENTRY.size |
- | |
298 | cmp [ebx + esi + ARP_ENTRY.IP], eax ; ebx=ARPTable base |
- | |
299 | loopnz @b ; Return back if non match |
- | |
300 | jnz .ip_to_mac_send_request ; and request IP->MAC if none found in the table |
161 | |
Line 301... | Line -... | ||
301 | - | ||
302 | ; Return the entry status in eax |
- | |
303 | movzx eax, word[ebx + esi + ARP_ENTRY.Status] |
162 | push eax |
304 | - | ||
305 | DEBUGF 1,"MAC found: %x-%x-%x-%x-%x-%x status:%x in slot:%u\n",\ |
- | |
Line -... | Line 163... | ||
- | 163 | ||
306 | [ebx + esi + ARP_ENTRY.MAC]:2,[ebx + esi + ARP_ENTRY.MAC+1]:2,[ebx + esi + ARP_ENTRY.MAC+2]:2,[ebx + esi + ARP_ENTRY.MAC+3]:2,[ebx + esi + ARP_ENTRY.MAC+4]:2,[ebx + esi + ARP_ENTRY.MAC+5]:2, ax, esi |
164 | push word ARP_REQUEST_TTL |
307 | 165 | push word ARP_AWAITING_RESPONSE |
|
308 | ; esi holds index |
166 | push dword 0 |
Line 309... | Line 167... | ||
309 | cld |
167 | push word 0 |
Line 310... | Line 168... | ||
310 | lea esi, [ebx + esi + ARP_ENTRY.MAC] |
168 | push eax |
- | 169 | call ARP_add_entry |
|
- | 170 | ||
311 | mov edi, [Extra] ;edi=ptr to buffer for write MAC |
171 | cmp eax, -1 |
312 | movsd |
- | |
Line 313... | Line 172... | ||
313 | movsw |
172 | je .full |
314 | jmp .exit |
- | |
315 | - | ||
316 | .ip_to_mac_send_request: |
- | |
317 | ;;; TODO: get device number ! (in edx) |
- | |
318 | xor edx, edx |
- | |
319 | mov edx, [ETH_DRV_LIST + 4*edx] |
- | |
320 | lea ecx, [edx + ETH_DEVICE.mac] |
- | |
321 | - | ||
322 | stdcall arp_request,[Index],[IP_LIST+edx],ecx ;TargetIP,SenderIP_ptr,SenderMAC_ptr |
- | |
323 | mov eax, ARP_NO_ENTRY |
- | |
324 | jmp .exit |
- | |
325 | 173 | ||
326 | ;;END IP_TO_MAC |
174 | pop eax |
327 | 175 | call ARP_create_request |
|
328 | .exit: |
- | |
329 | ret |
- | |
330 | endp |
- | |
331 | - | ||
332 | - | ||
333 | ;*************************************************************************** |
- | |
334 | ; Function |
- | |
335 | ; arp_request [by Johnny_B] |
- | |
336 | ; |
- | |
337 | ; Description |
- | |
338 | ; Sends an ARP request on the ethernet |
- | |
339 | ; IN: |
176 | |
340 | ; TargetIP : requested IP address |
177 | ret |
341 | ; SenderIP_ptr : POINTER to sender's IP address(our system's address) |
- | |
342 | ; SenderMAC_ptr : POINTER to sender's MAC address(our system's address) |
- | |
343 | ; OUT: |
- | |
344 | ; EAX=0 (if all is ok), otherwise EAX is not defined |
- | |
345 | ; |
- | |
346 | ; EBX,ESI,EDI will be saved |
- | |
347 | ; |
- | |
348 | ;*************************************************************************** |
- | |
349 | proc arp_request stdcall uses ebx esi edi,\ |
178 | |
350 | TargetIP:DWORD, SenderIP_ptr:DWORD, SenderMAC_ptr:DWORD |
- | |
351 | - | ||
352 | DEBUGF 1,"Create ARP request\n" |
- | |
353 | - | ||
354 | - | ||
355 | stdcall kernel_alloc, 60 ; minimum eth packet size |
- | |
356 | test eax, eax |
179 | .found_it: |
357 | jz .exit |
- | |
358 | 180 | DEBUGF 1,"Found MAC! (%u-%u-%u-%u-%u-%u)\n",[esi+0]:2,[esi+1]:2,[esi+2]:2,[esi+3]:2,[esi+4]:2,[esi+5]:2 |
|
359 | mov ebx, eax |
- | |
360 | - | ||
361 | mov word [ebx + ETH_FRAME.Data + ARP_Packet.HardwareType], 0x0100 ;Ethernet |
- | |
362 | mov word [ebx + ETH_FRAME.Data + ARP_Packet.ProtocolType], 0x0008 ;IP |
- | |
363 | mov byte [ebx + ETH_FRAME.Data + ARP_Packet.HardwareSize], 0x06 ;MAC-addr length |
- | |
364 | mov byte [ebx + ETH_FRAME.Data + ARP_Packet.ProtocolSize], 0x04 ;IP-addr length |
- | |
365 | mov word [ebx + ETH_FRAME.Data + ARP_Packet.Opcode], 0x0100 ;Request |
- | |
366 | - | ||
367 | DEBUGF 1,"1" |
- | |
368 | - | ||
369 | cld |
- | |
370 | mov esi, [SenderMAC_ptr] |
- | |
371 | lea edi, [ebx + ETH_FRAME.Data + ARP_Packet.SenderMAC] ;Our MAC-addr |
- | |
372 | movsd |
- | |
373 | movsw |
- | |
374 | - | ||
Line 375... | Line -... | ||
375 | DEBUGF 1,"2" |
- | |
376 | - | ||
Line -... | Line 181... | ||
- | 181 | movzx eax, word [esi] |
|
- | 182 | mov ebx, [esi+2] |
|
Line 377... | Line 183... | ||
377 | mov esi, [SenderIP_ptr] |
183 | |
Line 378... | Line 184... | ||
378 | mov [ebx + ETH_FRAME.Data + ARP_Packet.SenderIP], esi ;Our IP-addr |
184 | ret |
379 | ; movsd |
- | |
380 | - | ||
381 | DEBUGF 1,"3" |
- | |
Line 382... | Line 185... | ||
382 | 185 | ||
- | 186 | .full: |
|
- | 187 | add esp, 4 |
|
Line -... | Line 188... | ||
- | 188 | mov eax, -1 |
|
- | 189 | ret |
|
- | 190 | ||
- | 191 | ||
- | 192 | ;--------------------------------------------------------------------------- |
|
383 | lea edi, [ebx + ETH_FRAME.Data + ARP_Packet.TargetMAC] ; Required MAC-addr |
193 | ; |
- | 194 | ; ARP_create_packet |
|
- | 195 | ; |
|
384 | xor eax, eax |
196 | ; IN: ip in eax |
Line -... | Line 197... | ||
- | 197 | ; |
|
385 | stosd |
198 | ; OUT: / |
- | 199 | ; |
|
- | 200 | ;--------------------------------------------------------------------------- |
|
- | 201 | ||
Line 386... | Line 202... | ||
386 | stosw |
202 | |
- | 203 | align 4 |
|
387 | 204 | ARP_create_request: |
|
388 | DEBUGF 1,"4" |
205 | |
Line -... | Line 206... | ||
- | 206 | DEBUGF 1,"Create ARP Packet\n" |
|
389 | 207 | ||
Line 390... | Line 208... | ||
390 | lea edi, [ebx + ETH_FRAME.DstMAC] |
208 | call IPv4_dest_to_dev |
391 | stosd |
- | |
392 | stosw |
209 | |
393 | - | ||
394 | DEBUGF 1,"5" |
- | |
395 | 210 | push eax ; DestIP |
|
Line -... | Line 211... | ||
- | 211 | mov eax, [IP_LIST+4*edi] ; senderIP |
|
396 | mov esi, [TargetIP] |
212 | push eax |
397 | mov dword [ebx + ETH_FRAME.Data + ARP_Packet.TargetIP], esi ;Required IP-addr(we get it as function parameter) |
- | |
Line 398... | Line -... | ||
398 | - | ||
399 | 213 | ||
Line 400... | Line -... | ||
400 | DEBUGF 1,"6" |
- | |
401 | - | ||
402 | mov esi, [SenderMAC_ptr] |
- | |
403 | lea edi, [ebx + ETH_FRAME.SrcMAC] |
214 | mov edi, [ETH_DRV_LIST + 4*edi] |
Line 404... | Line -... | ||
404 | movsd |
- | |
405 | movsw |
215 | lea eax, [edi + ETH_DEVICE.mac] |
Line 406... | Line -... | ||
406 | - | ||
407 | DEBUGF 1,"7" |
- | |
- | 216 | mov ebx, ETH_BROADCAST |
|
Line 408... | Line 217... | ||
408 | 217 | mov ecx, 60 ; minimum packet size |
|
409 | mov ax , ETHER_ARP |
218 | mov edx, edi ;;; |
410 | stosw |
219 | mov di , ETHER_ARP |
- | 220 | call ETH_create_Packet |
|
411 | 221 | cmp edi, -1 |
|
412 | DEBUGF 1,"8" |
- | |
413 | - | ||
414 | ;;; TODO: get device number in edx !! |
- | |
Line 415... | Line 222... | ||
415 | xor edx, edx |
222 | je .exit |
416 | shl edx, 2 |
223 | |
Line 510... | Line 317... | ||
510 | 317 | ||
Line 511... | Line 318... | ||
511 | .exit: |
318 | .exit: |
Line -... | Line 319... | ||
- | 319 | ||
- | 320 | ret |
|
- | 321 | ||
- | 322 | ;--------------------------------------------------------------------------- |
|
- | 323 | ; |
|
- | 324 | ; ARP_add_entry (or update) |
|
- | 325 | ; |
|
- | 326 | ; IN: arp entry in stack: esp .IP |
|
- | 327 | ; esp+4 .MAC |
|
- | 328 | ; esp+10 .Status |
|
- | 329 | ; esp+12 .TTL |
|
- | 330 | ; esp+14 |
|
- | 331 | ; |
|
- | 332 | ; OUT: eax = entry #, -1 on error |
|
- | 333 | ; |
|
- | 334 | ;--------------------------------------------------------------------------- |
|
- | 335 | ||
- | 336 | ; TODO: use a mutex |
|
- | 337 | ||
- | 338 | align 4 |
|
- | 339 | ARP_add_entry: |
|
- | 340 | ||
- | 341 | mov ecx, [NumARP] |
|
- | 342 | test ecx, ecx |
|
- | 343 | jz .add |
|
- | 344 | ||
- | 345 | mov eax, dword[esp + ARP_ENTRY.MAC] |
|
- | 346 | mov bx , word[esp + ARP_ENTRY.MAC + 4] |
|
- | 347 | mov esi, ARPTable |
|
- | 348 | ||
- | 349 | .loop: |
|
- | 350 | cmp dword [esi + ARP_ENTRY.MAC], eax |
|
- | 351 | jne .maybe_next |
|
- | 352 | cmp word [esi + ARP_ENTRY.MAC + 4], bx |
|
- | 353 | jne .maybe_next |
|
- | 354 | ||
- | 355 | cmp dword[esi + ARP_ENTRY.TTL], 0xFFFF ; static entry |
|
- | 356 | jne .notstatic |
|
- | 357 | cmp dword[esp + ARP_ENTRY.TTL], 0xFFFF |
|
- | 358 | jne .exit |
|
- | 359 | .notstatic: |
|
- | 360 | ||
- | 361 | mov ebx, [NumARP] |
|
- | 362 | xchg ebx, ecx |
|
- | 363 | sub ecx, ebx |
|
- | 364 | jmp .add |
|
- | 365 | ||
- | 366 | .maybe_next: |
|
- | 367 | add esi, ARP_ENTRY.size |
|
- | 368 | loop .loop |
|
- | 369 | ||
- | 370 | mov ecx, [NumARP] |
|
- | 371 | cmp ecx, ARP_TABLE_SIZE |
|
- | 372 | jge .full |
|
- | 373 | ||
- | 374 | .add: |
|
- | 375 | ||
- | 376 | push ecx |
|
- | 377 | imul ecx, ARP_ENTRY.size |
|
- | 378 | lea edi, [ecx + ARPTable] |
|
- | 379 | lea esi, [esp + 4] |
|
- | 380 | mov ecx, ARP_ENTRY.size/2 |
|
- | 381 | repz movsw |
|
- | 382 | ||
- | 383 | inc [NumARP] |
|
- | 384 | pop eax |
|
- | 385 | ||
- | 386 | .exit: |
|
- | 387 | ||
- | 388 | add esp, 14 |
|
- | 389 | ret |
|
- | 390 | ||
- | 391 | .full: |
|
- | 392 | ||
- | 393 | mov eax, -1 |
|
Line 512... | Line 394... | ||
512 | 394 | jmp .exit |
|
513 | ret |
395 | |
514 | 396 | ||
515 | 397 | ||
Line 535... | Line 417... | ||
535 | 417 | ||
536 | shr ecx,1 ;ecx/2 => ARP_ENTRY_SIZE MUST BE EVEN NUMBER! |
418 | shr ecx,1 ;ecx/2 => ARP_ENTRY_SIZE MUST BE EVEN NUMBER! |
537 | cld |
419 | cld |
Line 538... | Line 420... | ||
538 | rep movsw |
420 | rep movsw |
539 | 421 | ||
Line 540... | Line -... | ||
540 | dec dword[NumARP] ;decrease arp-entries counter |
- | |
541 | ret |
422 | dec [NumARP] ;decrease arp-entries counter |
542 | 423 | ret |
|
543 | 424 | ||
544 | 425 | ||
545 | 426 | ||
Line 555... | Line 436... | ||
555 | ; size of buffer in [esp+4] |
436 | ; size of buffer in [esp+4] |
556 | ; packet size (without ethernet header) in ecx |
437 | ; packet size (without ethernet header) in ecx |
557 | ; OUT: / |
438 | ; OUT: / |
558 | ; |
439 | ; |
559 | ;----------------------------------------------------- |
440 | ;----------------------------------------------------- |
- | 441 | ||
560 | align 4 |
442 | align 4 |
561 | ARP_Handler: |
443 | ARP_handler: |
Line 562... | Line 444... | ||
562 | 444 | ||
563 | DEBUGF 1,"ARP_Handler - start\n" |
445 | DEBUGF 1,"ARP_Handler - start\n" |
564 | cmp ecx, 28 |
446 | cmp ecx, 28 |
Line -... | Line 447... | ||
- | 447 | jl .exit |
|
- | 448 | ||
- | 449 | cmp word [edx + ARP_Packet.Opcode], ARP_REP_OPCODE ; Is this a reply packet? |
|
- | 450 | jne .maybe_request |
|
565 | jl .exit |
451 | |
- | 452 | mov ecx, [NumARP] |
|
- | 453 | test ecx, ecx |
|
- | 454 | jz .exit |
|
566 | 455 | ||
567 | ; Is this a REQUEST? |
456 | mov eax, [esp] |
- | 457 | mov eax, [eax + ARP_Packet.SenderIP] |
|
- | 458 | mov esi, ARPTable+ARP_ENTRY.IP |
|
- | 459 | ||
- | 460 | .loop: |
|
- | 461 | scasd |
|
- | 462 | jz .gotit |
|
- | 463 | add esi, ARP_ENTRY.size-4 |
|
- | 464 | loop .loop |
|
- | 465 | ||
- | 466 | jmp .exit |
|
568 | ; Is this a request for My Host IP |
467 | |
- | 468 | .gotit: |
|
- | 469 | cmp [esi-4+ARP_ENTRY.Status], 0x0300 ;if it is a static entry, dont touch it |
|
- | 470 | je .exit |
|
- | 471 | ||
569 | ; Yes - So construct a response message. |
472 | mov [esi-4+ARP_ENTRY.Status], ARP_VALID_MAPPING |
- | 473 | mov [esi+ARP_ENTRY.TTL-4], ARP_ENTRY_TTL |
|
- | 474 | ||
570 | ; Send this message to the ethernet card for transmission |
475 | mov ebx, [esp] |
571 | 476 | mov eax, dword [ebx + ARP_Packet.SenderMAC] |
|
572 | ; push ebx edx |
477 | mov dword [esi+ARP_ENTRY.MAC-4], eax |
Line -... | Line 478... | ||
- | 478 | mov ax , word [ebx + ARP_Packet.SenderMAC + 4] |
|
- | 479 | mov word [esi+ARP_ENTRY.MAC-4+4], ax |
|
- | 480 | ||
- | 481 | jmp .exit |
|
- | 482 | ||
- | 483 | ||
- | 484 | ;------ |
|
573 | stdcall arp_table_manager, ARP_TABLE_ADD, EXTRA_IS_ARP_PACKET_PTR, edx |
485 | |
574 | ; pop edx ebx |
486 | |
Line 575... | Line 487... | ||
575 | 487 | .maybe_request: |
|
576 | cmp word [edx + ARP_Packet.Opcode], ARP_REQ_OPCODE ; Is this a request packet? |
488 | cmp word [edx + ARP_Packet.Opcode], ARP_REQ_OPCODE ; Is this a request packet? |
Line 589... | Line 501... | ||
589 | cmp eax, [edx + ARP_Packet.TargetIP] ; Is it looking for my IP address? |
501 | cmp eax, [edx + ARP_Packet.TargetIP] ; Is it looking for my IP address? |
590 | jnz .exit |
502 | jnz .exit |
591 | push eax |
503 | push eax |
592 | push edi |
504 | push edi |
Line 593... | Line -... | ||
593 | - | ||
594 | ; DEBUGF 1,"ETH_ARP_Handler - request for %u.%u.%u.%u\n",[edi+0]:1,[edi+1]:1,[edi+2]:1,[edi+3]:1 |
- | |
595 | 505 | ||
596 | ; OK, it is a request for one of our MAC addresses. Build the frame and send it |
506 | ; OK, it is a request for one of our MAC addresses. Build the frame and send it |
Line 597... | Line 507... | ||
597 | ; We can reuse the buffer. |
507 | ; We can reuse the buffer. (faster then using ARP_create_packet) |
598 | 508 | ||
599 | cld |
509 | cld |
600 | lea esi, [edx + ARP_Packet.SenderMAC] |
510 | lea esi, [edx + ARP_Packet.SenderMAC] |
Line 621... | Line 531... | ||
621 | movsd |
531 | movsd |
622 | movsw |
532 | movsw |
623 | lea esi, [edx + ARP_Packet.SenderMAC] |
533 | lea esi, [edx + ARP_Packet.SenderMAC] |
624 | movsd |
534 | movsd |
625 | movsw |
535 | movsw |
626 | mov ax , ETHER_ARP |
536 | ; mov ax , ETHER_ARP |
627 | stosw |
537 | ; stosw |
Line 628... | Line 538... | ||
628 | 538 | ||
Line 629... | Line 539... | ||
629 | jmp ETH_Sender ; And send it! |
539 | jmp ETH_Sender ; And send it! |
630 | 540 | ||
Line 670... | Line 580... | ||
670 | jz .write ; 4 |
580 | jz .write ; 4 |
671 | dec bl |
581 | dec bl |
672 | jz .remove ; 5 |
582 | jz .remove ; 5 |
673 | dec bl |
583 | dec bl |
Line 674... | Line -... | ||
674 | - | ||
675 | 584 | ||
676 | .error: |
585 | .error: |
677 | mov eax, -1 |
586 | mov eax, -1 |
Line 678... | Line 587... | ||
678 | ret |
587 | ret |
Line 695... | Line 604... | ||
695 | ; TODO: write code |
604 | ; TODO: write code |
696 | ret |
605 | ret |
Line 697... | Line 606... | ||
697 | 606 | ||
698 | .write: |
607 | .write: |
- | 608 | ; TODO: write code |
|
699 | ; TODO: write code |
609 | ; call ARP_write_entry |
Line 700... | Line 610... | ||
700 | ret |
610 | ret |
- | 611 | ||
701 | 612 | .remove: |
|
702 | .remove: |
613 | mov esi, eax |