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 ; =============================================================================
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
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
37 mov eax, [rsi
+0x1E] ; Grab the Destination IP
38 mov dword [rsi
+0x1A], eax ; Overwrite the 'old' Source with the 'new' Source
40 mov dword [rsi
+0x1E], eax ; Overwrite the 'old' Destination with the 'new' Destination
43 mov byte [rsi
+0x22], 0x00 ; Set to 0 for Echo Reply (Was originally 8 for Echo Request)
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
55 ; -----------------------------------------------------------------------------
58 ; =============================================================================