* remove "\r" nonsense
[mascara-docs.git] / amd64 / bareMetalOS-0.5.3 / os / ipv4 / icmp.asm
blob2cca75c0cd54986537d9826ec729144b54f9b50f
1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2012 Return Infinity -- see LICENSE.TXT
5 ; ICMP (Internet Control Message Protocol)
6 ; =============================================================================
8 align 16
9 db 'DEBUG: IPv4 ICMP'
10 align 16
13 ; -----------------------------------------------------------------------------
14 ; os_icmp_handler -- Handle an incoming ICMP packet; Called by Network interrupt
15 ; IN: RCX = packet length
16 ; RSI = location of received ICMP packet
17 os_icmp_handler:
18 push rsi
19 push rax
21 ; Check if reply or request
24 os_icmp_handler_request:
25 ; Swap the MAC addresses
26 mov rax, [rsi] ; Grab the Destination MAC as 8 bytes even though the MAC is 6 bytes
27 mov ax, [rsi+0x0C] ; Store the EtherType in the low 16-bits of RAX
28 push rax ; Save the new Source MAC (with EtherType) to the stack
29 mov rax, [rsi+0x06] ; Grab the Source MAC as 8 bytes (the last two bytes will be overwritten)
30 mov [rsi], rax ; Store the new Destination MAC in the packet
31 pop rax ; Restore the new Source MAC + EtherType
32 mov [rsi+0x06], rax ; Write it to the packet
34 ; Swap the IP addresses
35 mov eax, [rsi+0x1A] ; Grab the Source IP
36 push rax
37 mov eax, [rsi+0x1E] ; Grab the Destination IP
38 mov dword [rsi+0x1A], eax ; Overwrite the 'old' Source with the 'new' Source
39 pop rax
40 mov dword [rsi+0x1E], eax ; Overwrite the 'old' Destination with the 'new' Destination
42 ; Set to Echo Reply
43 mov byte [rsi+0x22], 0x00 ; Set to 0 for Echo Reply (Was originally 8 for Echo Request)
45 ; Adjust the checksum
46 mov ax, [rsi+0x24]
47 add ax, 8 ; Add 8 since we removed 8 (by clearing the Echo Request)
48 mov word [rsi+0x24], ax
50 call os_ethernet_tx_raw ; Send the packet
52 pop rax
53 pop rsi
54 ret
55 ; -----------------------------------------------------------------------------
58 ; =============================================================================
59 ; EOF