2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2016-02-01 20:43:10 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; IPv4.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the TCP/IP network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Based on the work of [Johnny_B] and [smb] ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-04-17 23:19:45 +02:00
|
|
|
$Revision$
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-19 17:50:16 +02:00
|
|
|
IPv4_MAX_FRAGMENTS = 64
|
2013-12-19 22:31:51 +01:00
|
|
|
IPv4_MAX_ROUTES = 64
|
|
|
|
|
|
|
|
IPv4_ROUTE_FLAG_UP = 1 shl 0
|
|
|
|
IPv4_ROUTE_FLAG_GATEWAY = 1 shl 1
|
|
|
|
IPv4_ROUTE_FLAG_HOST = 1 shl 2
|
|
|
|
IPv4_ROUTE_FLAG_D = 1 shl 3 ; Route was created by a redirect
|
|
|
|
IPv4_ROUTE_FLAG_M = 1 shl 4 ; Route was modified by a redirect
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
struct IPv4_header
|
|
|
|
|
|
|
|
VersionAndIHL db ? ; Version[0-3 bits] and IHL(header length)[4-7 bits]
|
|
|
|
TypeOfService db ? ; precedence [7-5] minimize delay [4], maximize throughput [3], maximize riliability [2] minimize momentary cost [1] and zero [0]
|
|
|
|
TotalLength dw ?
|
|
|
|
Identification dw ?
|
|
|
|
FlagsAndFragmentOffset dw ? ; Flags[0-2] and FragmentOffset[3-15]
|
|
|
|
TimeToLive db ? ;
|
|
|
|
Protocol db ?
|
|
|
|
HeaderChecksum dw ?
|
|
|
|
SourceAddress dd ?
|
|
|
|
DestinationAddress dd ?
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
struct IPv4_FRAGMENT_slot
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ttl dw ? ; Time to live for this entry, 0 for empty slot's
|
|
|
|
id dw ? ; Identification field from IP header
|
|
|
|
SrcIP dd ? ; .. from IP header
|
|
|
|
DstIP dd ? ; .. from IP header
|
|
|
|
ptr dd ? ; Pointer to first packet
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
struct IPv4_FRAGMENT_entry ; This structure will replace the ethernet header in fragmented ip packets
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
PrevPtr dd ? ; Pointer to previous fragment entry (-1 for first packet)
|
|
|
|
NextPtr dd ? ; Pointer to next fragment entry (-1 for last packet)
|
|
|
|
Owner dd ? ; Pointer to structure of driver
|
|
|
|
rb 2 ; to match ethernet header size ;;; FIXME
|
|
|
|
; Ip header begins here (we will need the IP header to re-construct the complete packet)
|
|
|
|
ends
|
|
|
|
|
2013-12-19 22:31:51 +01:00
|
|
|
struct IPv4_ROUTE
|
|
|
|
|
|
|
|
Destination dd ?
|
|
|
|
Gateway dd ?
|
|
|
|
Flags dd ?
|
|
|
|
Use dd ?
|
|
|
|
Interface dd ?
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
uglobal
|
2013-06-24 13:39:05 +02:00
|
|
|
align 4
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
IP_LIST rd NET_DEVICES_MAX
|
|
|
|
SUBNET_LIST rd NET_DEVICES_MAX
|
|
|
|
DNS_LIST rd NET_DEVICES_MAX
|
|
|
|
GATEWAY_LIST rd NET_DEVICES_MAX
|
|
|
|
BROADCAST_LIST rd NET_DEVICES_MAX
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
IPv4_packets_tx rd NET_DEVICES_MAX
|
|
|
|
IPv4_packets_rx rd NET_DEVICES_MAX
|
|
|
|
IPv4_packets_dumped rd NET_DEVICES_MAX
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
IPv4_FRAGMENT_LIST rb IPv4_MAX_FRAGMENTS * sizeof.IPv4_FRAGMENT_slot
|
2013-06-24 13:39:05 +02:00
|
|
|
|
2013-12-19 22:31:51 +01:00
|
|
|
IPv4_ROUTES rd IPv4_MAX_ROUTES * sizeof.IPv4_ROUTE
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
endg
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_init: Resets all IPv4 variables ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
macro ipv4_init {
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
mov edi, IP_LIST
|
2013-10-19 17:47:58 +02:00
|
|
|
mov ecx, 7*NET_DEVICES_MAX + (sizeof.IPv4_FRAGMENT_slot*IPv4_MAX_FRAGMENTS)/4
|
2013-06-27 01:35:43 +02:00
|
|
|
rep stosd
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; Decrease TimeToLive of all fragment slots ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
macro ipv4_decrease_fragment_ttls {
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
local .loop, .next
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
mov esi, IPv4_FRAGMENT_LIST
|
|
|
|
mov ecx, IPv4_MAX_FRAGMENTS
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp [esi + IPv4_FRAGMENT_slot.ttl], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
je .next
|
2013-10-19 17:47:58 +02:00
|
|
|
dec [esi + IPv4_FRAGMENT_slot.ttl]
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .died
|
|
|
|
.next:
|
2013-10-19 17:47:58 +02:00
|
|
|
add esi, sizeof.IPv4_FRAGMENT_slot
|
2013-05-28 19:34:26 +02:00
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
jmp .done
|
|
|
|
|
|
|
|
.died:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4 Fragment slot timed-out!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
;;; TODO: clear all entry's of timed-out slot
|
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.done:
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
macro ipv4_checksum ptr {
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; This is the fast procedure to create or check an IP header without options
|
|
|
|
; To create a new checksum, the checksum field must be set to 0 before computation
|
|
|
|
; To check an existing checksum, leave the checksum as is, and it will be 0 after this procedure, if it was correct
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
xor ebx, ebx
|
|
|
|
add bl, [ptr+1]
|
|
|
|
adc bh, [ptr+0]
|
|
|
|
|
|
|
|
adc bl, [ptr+3]
|
|
|
|
adc bh, [ptr+2]
|
|
|
|
|
|
|
|
adc bl, [ptr+5]
|
|
|
|
adc bh, [ptr+4]
|
|
|
|
|
|
|
|
adc bl, [ptr+7]
|
|
|
|
adc bh, [ptr+6]
|
|
|
|
|
|
|
|
adc bl, [ptr+9]
|
|
|
|
adc bh, [ptr+8]
|
|
|
|
|
|
|
|
; we skip 11th and 12th byte, they are the checksum bytes and should be 0 for re-calculation
|
|
|
|
|
|
|
|
adc bl, [ptr+13]
|
|
|
|
adc bh, [ptr+12]
|
|
|
|
|
|
|
|
adc bl, [ptr+15]
|
|
|
|
adc bh, [ptr+14]
|
|
|
|
|
|
|
|
adc bl, [ptr+17]
|
|
|
|
adc bh, [ptr+16]
|
|
|
|
|
|
|
|
adc bl, [ptr+19]
|
|
|
|
adc bh, [ptr+18]
|
|
|
|
|
|
|
|
adc ebx, 0
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
mov ecx, ebx
|
|
|
|
shr ecx, 16
|
|
|
|
and ebx, 0xffff
|
|
|
|
add ebx, ecx
|
|
|
|
|
|
|
|
mov ecx, ebx
|
|
|
|
shr ecx, 16
|
|
|
|
add ebx, ecx
|
|
|
|
|
|
|
|
not bx
|
|
|
|
jnz .not_zero
|
|
|
|
dec bx
|
|
|
|
.not_zero:
|
|
|
|
xchg bl, bh
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
neg word [ptr+10] ; zero will stay zero so we just get the checksum
|
|
|
|
add word [ptr+10], bx ; , else we will get (new checksum - old checksum) in the end, wich should be 0 :)
|
|
|
|
pop ebx
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_input: Check if IPv4 Packet isnt damaged and call ;
|
|
|
|
; appropriate handler. (TCP/UDP/ICMP/..) ;
|
|
|
|
; We will also re-construct fragmented packets. ;
|
|
|
|
; ;
|
|
|
|
; IN: Pointer to buffer in [esp] ;
|
|
|
|
; pointer to device struct in ebx ;
|
|
|
|
; pointer to IPv4 header in edx ;
|
|
|
|
; size of IPv4 packet in ecx ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_input:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: packet from %u.%u.%u.%u ",\
|
2013-05-28 19:34:26 +02:00
|
|
|
[edx + IPv4_header.SourceAddress + 0]:1,[edx + IPv4_header.SourceAddress + 1]:1,\
|
|
|
|
[edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1
|
2015-03-17 22:50:29 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "to %u.%u.%u.%u\n",\
|
2013-05-28 19:34:26 +02:00
|
|
|
[edx + IPv4_header.DestinationAddress + 0]:1,[edx + IPv4_header.DestinationAddress + 1]:1,\
|
|
|
|
[edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_ptr_to_num4
|
2015-10-11 20:23:40 +02:00
|
|
|
cmp edi, -1
|
|
|
|
je .invalid_device
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;-------------------------------
|
|
|
|
; re-calculate the checksum
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_checksum edx
|
2013-05-28 19:34:26 +02:00
|
|
|
jnz .dump ; if checksum isn't valid then dump packet
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Checksum ok\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
;--------------------------------
|
|
|
|
; Check if destination IP matches
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; local ip (Using RFC1122 strong end system model)
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [edx + IPv4_header.DestinationAddress]
|
|
|
|
cmp eax, [IP_LIST + edi]
|
|
|
|
je .ip_ok
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; network layer broadcast
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, [BROADCAST_LIST + edi]
|
|
|
|
je .ip_ok
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; physical layer broadcast (255.255.255.255)
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, 0xffffffff
|
|
|
|
je .ip_ok
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; multicast (224.0.0.0/4 = 224.0.0.0 to 239.255.255.255)
|
2013-05-28 19:34:26 +02:00
|
|
|
and eax, 0x0fffffff
|
|
|
|
cmp eax, 224
|
|
|
|
je .ip_ok
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; maybe we just dont have an IP yet and should accept everything on the IP level
|
2014-06-15 14:00:30 +02:00
|
|
|
cmp [IP_LIST + edi], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
je .ip_ok
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; or it's just not meant for us.. :(
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Destination address does not match!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .dump
|
|
|
|
|
|
|
|
;------------------------
|
|
|
|
; Now we can update stats
|
|
|
|
|
|
|
|
.ip_ok:
|
2013-10-19 17:47:58 +02:00
|
|
|
inc [IPv4_packets_rx + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;----------------------------------
|
|
|
|
; Check if the packet is fragmented
|
|
|
|
|
|
|
|
test [edx + IPv4_header.FlagsAndFragmentOffset], 1 shl 5 ; Is 'more fragments' flag set ?
|
|
|
|
jnz .has_fragments ; If so, we definately have a fragmented packet
|
|
|
|
|
|
|
|
test [edx + IPv4_header.FlagsAndFragmentOffset], 0xff1f ; If flag is not set, but there is a fragment offset, the packet is last in series of fragmented packets
|
|
|
|
jnz .is_last_fragment
|
|
|
|
|
|
|
|
;-------------------------------------------------------------------
|
|
|
|
; No, it's just a regular IP packet, pass it to the higher protocols
|
|
|
|
|
|
|
|
.handle_it: ; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
|
|
|
|
|
|
|
|
movzx esi, [edx + IPv4_header.VersionAndIHL] ; Calculate Header length by using IHL field
|
|
|
|
and esi, 0x0000000f ;
|
|
|
|
shl esi, 2 ;
|
|
|
|
|
|
|
|
movzx ecx, [edx + IPv4_header.TotalLength] ; Calculate length of encapsulated Packet
|
|
|
|
xchg cl, ch ;
|
|
|
|
sub ecx, esi ;
|
|
|
|
|
|
|
|
mov al, [edx + IPv4_header.Protocol]
|
|
|
|
add esi, edx ; make esi ptr to data
|
|
|
|
|
|
|
|
cmp al, IP_PROTO_TCP
|
2015-12-27 16:37:31 +01:00
|
|
|
je tcp_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp al, IP_PROTO_UDP
|
2015-12-27 16:37:31 +01:00
|
|
|
je udp_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp al, IP_PROTO_ICMP
|
2015-12-27 16:37:31 +01:00
|
|
|
je icmp_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
;-------------------------------
|
|
|
|
; Look for a matching RAW socket
|
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
|
|
|
add ecx, esi
|
|
|
|
sub ecx, edx
|
|
|
|
mov esi, edx
|
|
|
|
movzx edx, al
|
|
|
|
mov eax, net_sockets
|
|
|
|
.next_socket:
|
|
|
|
mov eax, [eax + SOCKET.NextPtr]
|
|
|
|
or eax, eax
|
|
|
|
jz .dump_unlock
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], edx
|
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: found matching RAW socket: 0x%x\n", eax
|
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
jmp socket_input
|
2015-10-11 20:23:40 +02:00
|
|
|
|
|
|
|
.dump_unlock:
|
|
|
|
|
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: unknown protocol %u\n", al
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.dump:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: dumping\n"
|
2015-10-11 20:23:40 +02:00
|
|
|
inc [IPv4_packets_dumped + edi]
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2015-10-11 20:23:40 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.invalid_device:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_input: packet originated from invalid device\n"
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;---------------------------
|
|
|
|
; Fragmented packet handler
|
|
|
|
|
|
|
|
|
|
|
|
.has_fragments:
|
|
|
|
movzx eax, [edx + IPv4_header.FlagsAndFragmentOffset]
|
|
|
|
xchg al, ah
|
|
|
|
shl ax, 3
|
|
|
|
|
2013-11-19 21:40:18 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: fragmented packet offset=%u id=%x ptr=0x%x\n", ax, [edx + IPv4_header.Identification]:4, edx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test ax, ax ; Is this the first packet of the fragment?
|
|
|
|
jz .is_first_fragment
|
|
|
|
|
|
|
|
|
|
|
|
;-------------------------------------------------------
|
|
|
|
; We have a fragmented IP packet, but it's not the first
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Middle fragment packet received!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call ipv4_find_fragment_slot
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp esi, -1
|
|
|
|
je .dump
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [esi + IPv4_FRAGMENT_slot.ttl], 15 ; Reset the ttl
|
|
|
|
mov esi, [esi + IPv4_FRAGMENT_slot.ptr]
|
2013-05-28 19:34:26 +02:00
|
|
|
or edi, -1
|
|
|
|
.find_last_entry: ; The following routine will try to find the last entry
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp edi, [esi + IPv4_FRAGMENT_entry.PrevPtr]
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .destroy_slot ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
|
|
|
|
mov edi, esi
|
2013-10-19 17:47:58 +02:00
|
|
|
mov esi, [esi + IPv4_FRAGMENT_entry.NextPtr]
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp esi, -1
|
|
|
|
jne .find_last_entry
|
|
|
|
; We found the last entry (pointer is now in edi)
|
|
|
|
; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
|
|
|
|
|
|
|
|
pop eax ; pointer to packet
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [edi + IPv4_FRAGMENT_entry.NextPtr], eax ; update pointer of previous entry to the new entry
|
|
|
|
mov [eax + IPv4_FRAGMENT_entry.NextPtr], -1
|
|
|
|
mov [eax + IPv4_FRAGMENT_entry.PrevPtr], edi
|
|
|
|
mov [eax + IPv4_FRAGMENT_entry.Owner], ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;------------------------------------
|
|
|
|
; We have received the first fragment
|
|
|
|
|
|
|
|
.is_first_fragment:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: First fragment packet received!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
; try to locate a free slot..
|
2013-10-19 17:47:58 +02:00
|
|
|
mov ecx, IPv4_MAX_FRAGMENTS
|
|
|
|
mov esi, IPv4_FRAGMENT_LIST
|
2013-05-28 19:34:26 +02:00
|
|
|
.find_free_slot:
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp word [esi + IPv4_FRAGMENT_slot.ttl], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
je .found_free_slot
|
2013-10-19 17:47:58 +02:00
|
|
|
add esi, sizeof.IPv4_FRAGMENT_slot
|
2013-05-28 19:34:26 +02:00
|
|
|
loop .find_free_slot
|
|
|
|
jmp .dump ; If no free slot was found, dump the packet
|
|
|
|
|
|
|
|
.found_free_slot: ; We found a free slot, let's fill in the FRAGMENT_slot structure
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [esi + IPv4_FRAGMENT_slot.ttl], 15 ; RFC recommends 15 secs as ttl
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ax, [edx + IPv4_header.Identification]
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [esi + IPv4_FRAGMENT_slot.id], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [edx + IPv4_header.SourceAddress]
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [esi + IPv4_FRAGMENT_slot.SrcIP], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [edx + IPv4_header.DestinationAddress]
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [esi + IPv4_FRAGMENT_slot.DstIP], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
pop eax
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [esi + IPv4_FRAGMENT_slot.ptr], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [eax + IPv4_FRAGMENT_entry.NextPtr], -1
|
|
|
|
mov [eax + IPv4_FRAGMENT_entry.PrevPtr], -1
|
|
|
|
mov [eax + IPv4_FRAGMENT_entry.Owner], ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------
|
|
|
|
; We have received the last fragment
|
|
|
|
|
|
|
|
.is_last_fragment:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Last fragment packet received!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call ipv4_find_fragment_slot
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp esi, -1
|
|
|
|
je .dump
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
mov esi, [esi + IPv4_FRAGMENT_slot.ptr] ; We found the first entry, let's calculate total size of the packet in eax, so we can allocate a buffer
|
2013-05-28 19:34:26 +02:00
|
|
|
push esi
|
|
|
|
xor eax, eax
|
|
|
|
or edi, -1
|
|
|
|
|
|
|
|
.count_bytes:
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp [esi + IPv4_FRAGMENT_entry.PrevPtr], edi
|
|
|
|
jne .destroy_slot_pop ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
|
|
|
|
mov cx, [esi + sizeof.IPv4_FRAGMENT_entry + IPv4_header.TotalLength] ; Add total length
|
2013-05-28 19:34:26 +02:00
|
|
|
xchg cl, ch
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Packet size=%u\n", cx
|
2013-05-28 19:34:26 +02:00
|
|
|
add ax, cx
|
2013-10-19 17:47:58 +02:00
|
|
|
movzx cx, [esi + sizeof.IPv4_FRAGMENT_entry + IPv4_header.VersionAndIHL] ; Sub Header length
|
2013-05-28 19:34:26 +02:00
|
|
|
and cx, 0x000F
|
|
|
|
shl cx, 2
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Header size=%u\n", cx
|
2013-05-28 19:34:26 +02:00
|
|
|
sub ax, cx
|
|
|
|
mov edi, esi
|
2013-10-19 17:47:58 +02:00
|
|
|
mov esi, [esi + IPv4_FRAGMENT_entry.NextPtr]
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp esi, -1
|
|
|
|
jne .count_bytes
|
|
|
|
|
|
|
|
mov esi, [esp+4]
|
2013-10-19 17:47:58 +02:00
|
|
|
mov [edi + IPv4_FRAGMENT_entry.NextPtr], esi ; Add this packet to the chain, this simplifies the following code
|
|
|
|
mov [esi + IPv4_FRAGMENT_entry.NextPtr], -1
|
|
|
|
mov [esi + IPv4_FRAGMENT_entry.PrevPtr], edi
|
|
|
|
mov [esi + IPv4_FRAGMENT_entry.Owner], ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
mov cx, [edx + IPv4_header.TotalLength] ; Note: This time we dont substract Header length
|
2013-05-28 19:34:26 +02:00
|
|
|
xchg cl, ch
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Packet size=%u\n", cx
|
2013-05-28 19:34:26 +02:00
|
|
|
add ax, cx
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Total Received data size=%u\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push eax
|
|
|
|
mov ax, [edx + IPv4_header.FlagsAndFragmentOffset]
|
|
|
|
xchg al, ah
|
|
|
|
shl ax, 3
|
|
|
|
add cx, ax
|
|
|
|
pop eax
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Total Fragment size=%u\n", ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp ax, cx
|
|
|
|
jne .destroy_slot_pop
|
|
|
|
|
|
|
|
push eax
|
|
|
|
push eax
|
|
|
|
call kernel_alloc
|
|
|
|
test eax, eax
|
|
|
|
je .destroy_slot_pop ; If we dont have enough space to allocate the buffer, discard all packets in slot
|
|
|
|
mov edx, [esp+4] ; Get pointer to first fragment entry back in edx
|
|
|
|
|
|
|
|
.rebuild_packet_loop:
|
2013-10-19 17:47:58 +02:00
|
|
|
movzx ecx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.FlagsAndFragmentOffset] ; Calculate the fragment offset
|
2013-05-28 19:34:26 +02:00
|
|
|
xchg cl, ch ; intel byte order
|
|
|
|
shl cx, 3 ; multiply by 8 and clear first 3 bits
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Fragment offset=%u\n", cx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
lea edi, [eax + ecx] ; Notice that edi will be equal to eax for first fragment
|
2013-11-19 21:40:18 +01:00
|
|
|
movzx ebx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.VersionAndIHL] ; Find header size (in ebx) of fragment
|
2013-05-28 19:34:26 +02:00
|
|
|
and bx, 0x000F ;
|
|
|
|
shl bx, 2 ;
|
|
|
|
|
2013-11-19 21:40:18 +01:00
|
|
|
lea esi, [edx + sizeof.IPv4_FRAGMENT_entry] ; Set esi to the correct begin of fragment
|
|
|
|
movzx ecx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.TotalLength] ; Calculate total length of fragment
|
2013-05-28 19:34:26 +02:00
|
|
|
xchg cl, ch ; intel byte order
|
|
|
|
|
|
|
|
cmp edi, eax ; Is this packet the first fragment ?
|
|
|
|
je .first_fragment
|
|
|
|
sub cx, bx ; If not, dont copy the header
|
|
|
|
add esi, ebx ;
|
|
|
|
.first_fragment:
|
|
|
|
|
2013-11-19 21:40:18 +01:00
|
|
|
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Copying %u bytes from 0x%x to 0x%x\n", ecx, esi, edi
|
2013-05-28 19:34:26 +02:00
|
|
|
push cx ; First copy dword-wise, then byte-wise
|
|
|
|
shr cx, 2 ;
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsd ;
|
2013-05-28 19:34:26 +02:00
|
|
|
pop cx ;
|
|
|
|
and cx, 3 ;
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsb ;
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push eax
|
2013-11-19 22:01:57 +01:00
|
|
|
push [edx + IPv4_FRAGMENT_entry.Owner] ; we need to remeber the owner, in case this is the last packet
|
|
|
|
push [edx + IPv4_FRAGMENT_entry.NextPtr] ; Set edx to the next pointer
|
2013-05-28 19:34:26 +02:00
|
|
|
push edx ; Push pointer to fragment onto stack
|
2013-11-19 21:40:18 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Next Fragment: 0x%x\n", edx
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free ; free the previous fragment buffer (this uses the value from stack)
|
2013-11-19 21:40:18 +01:00
|
|
|
pop edx ebx eax
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp edx, -1 ; Check if it is last fragment in chain
|
|
|
|
jne .rebuild_packet_loop
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
xchg cl, ch
|
|
|
|
mov edx, eax
|
|
|
|
mov [edx + IPv4_header.TotalLength], cx
|
2013-11-19 22:01:57 +01:00
|
|
|
add esp, 12
|
2013-05-28 19:34:26 +02:00
|
|
|
xchg cl, ch
|
2013-11-19 22:01:57 +01:00
|
|
|
push ecx edx ; size and pointer
|
|
|
|
jmp .handle_it ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.destroy_slot_pop:
|
|
|
|
add esp, 4
|
|
|
|
.destroy_slot:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: Destroy fragment slot!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
; TODO!
|
|
|
|
jmp .dump
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_find_fragment_slot ;
|
|
|
|
; ;
|
|
|
|
; IN: pointer to fragmented packet in edx ;
|
|
|
|
; ;
|
|
|
|
; OUT: pointer to slot in esi, -1 on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_find_fragment_slot:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;;; TODO: the RFC says we should check protocol number too
|
|
|
|
|
|
|
|
push eax ebx ecx edx
|
|
|
|
mov ax, [edx + IPv4_header.Identification]
|
2013-10-19 17:47:58 +02:00
|
|
|
mov ecx, IPv4_MAX_FRAGMENTS
|
|
|
|
mov esi, IPv4_FRAGMENT_LIST
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ebx, [edx + IPv4_header.SourceAddress]
|
|
|
|
mov edx, [edx + IPv4_header.DestinationAddress]
|
|
|
|
.find_slot:
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp [esi + IPv4_FRAGMENT_slot.id], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .try_next
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp [esi + IPv4_FRAGMENT_slot.SrcIP], ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .try_next
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp [esi + IPv4_FRAGMENT_slot.DstIP], edx
|
2013-05-28 19:34:26 +02:00
|
|
|
je .found_slot
|
|
|
|
.try_next:
|
2013-10-19 17:47:58 +02:00
|
|
|
add esi, sizeof.IPv4_FRAGMENT_slot
|
2013-05-28 19:34:26 +02:00
|
|
|
loop .find_slot
|
|
|
|
|
|
|
|
or esi, -1
|
|
|
|
.found_slot:
|
|
|
|
pop edx ecx ebx eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;------------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_output ;
|
|
|
|
; ;
|
|
|
|
; IN: al = protocol ;
|
|
|
|
; ah = TTL ;
|
|
|
|
; ebx = device ptr (or 0 to let IP layer decide) ;
|
|
|
|
; ecx = data length ;
|
|
|
|
; edx = Source IP ;
|
|
|
|
; edi = Destination IP ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = pointer to buffer start ;
|
|
|
|
; eax = 0 on error ;
|
|
|
|
; ebx = device ptr (send packet through this device) ;
|
|
|
|
; ecx = data length ;
|
|
|
|
; edx = size of complete frame ;
|
|
|
|
; edi = start of IPv4 payload ;
|
|
|
|
; ;
|
|
|
|
;------------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_output:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2016-08-15 19:27:45 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_output: size=%u ip=0x%x\n", ecx, edi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp ecx, 65500 ; Max IPv4 packet size
|
|
|
|
ja .too_large
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
push ecx ax edi
|
|
|
|
mov eax, edi
|
2015-12-27 16:37:31 +01:00
|
|
|
call ipv4_route ; outputs device number in edi, dest ip in eax, source IP in edx
|
2016-02-01 20:43:10 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .no_route
|
2013-10-19 17:47:58 +02:00
|
|
|
push edx
|
2013-06-06 08:08:45 +02:00
|
|
|
test edi, edi
|
|
|
|
jz .loopback
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_ip_to_mac
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, 0xffff0000 ; error bits
|
|
|
|
jnz .arp_error
|
|
|
|
push ebx ; push the mac onto the stack
|
|
|
|
push ax
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
inc [IPv4_packets_tx + edi] ; update stats
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-02 14:48:32 +02:00
|
|
|
mov ax, ETHER_PROTO_IPv4
|
2013-06-10 22:19:12 +02:00
|
|
|
mov ebx, [NET_DRV_LIST + edi]
|
2013-10-19 17:47:58 +02:00
|
|
|
mov ecx, [esp + 6 + 8 + 2]
|
2013-05-28 19:34:26 +02:00
|
|
|
add ecx, sizeof.IPv4_header
|
2014-08-02 14:48:32 +02:00
|
|
|
mov edx, esp
|
2015-12-27 16:37:31 +01:00
|
|
|
call eth_output
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .eth_error
|
|
|
|
add esp, 6 ; pop the mac out of the stack
|
|
|
|
|
|
|
|
.continue:
|
|
|
|
xchg cl, ch ; internet byte order
|
|
|
|
mov [edi + IPv4_header.VersionAndIHL], 0x45 ; IPv4, normal length (no Optional header)
|
|
|
|
mov [edi + IPv4_header.TypeOfService], 0 ; nothing special, just plain ip packet
|
|
|
|
mov [edi + IPv4_header.TotalLength], cx
|
|
|
|
mov [edi + IPv4_header.Identification], 0 ; fragment id: FIXME
|
|
|
|
mov [edi + IPv4_header.FlagsAndFragmentOffset], 0
|
2013-10-19 17:47:58 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [edi + IPv4_header.HeaderChecksum], 0
|
|
|
|
popd [edi + IPv4_header.SourceAddress]
|
|
|
|
popd [edi + IPv4_header.DestinationAddress]
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
pop word[edi + IPv4_header.TimeToLive] ; ttl shl 8 + protocol
|
|
|
|
; [edi + IPv4_header.Protocol]
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_checksum edi
|
2013-05-28 19:34:26 +02:00
|
|
|
add edi, sizeof.IPv4_header
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_output: success!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.eth_error:
|
2013-06-05 02:53:57 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: ethernet error\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
add esp, 3*4+2+6
|
2014-08-02 14:48:32 +02:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
2016-02-01 20:43:10 +01:00
|
|
|
.no_route:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: No route to host!\n"
|
|
|
|
add esp, 2*4+2
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.arp_error:
|
2013-06-05 02:53:57 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: ARP error=%x\n", eax
|
2015-10-11 20:23:40 +02:00
|
|
|
add esp, 4
|
|
|
|
pop eax
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: ip=0x%x\n", eax
|
|
|
|
add esp, 4+2
|
2014-08-02 14:48:32 +02:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.too_large:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: Packet too large!\n"
|
2014-08-02 14:48:32 +02:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.loopback:
|
2015-03-17 22:50:29 +01:00
|
|
|
mov dword [esp], eax ; set source IP to dest IP
|
2013-06-06 08:08:45 +02:00
|
|
|
mov ecx, [esp + 10]
|
2013-05-28 19:34:26 +02:00
|
|
|
add ecx, sizeof.IPv4_header
|
2013-06-04 16:12:37 +02:00
|
|
|
mov edi, AF_INET4
|
2015-12-27 16:37:31 +01:00
|
|
|
call loop_output
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;------------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_output_raw ;
|
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ecx = data length ;
|
|
|
|
; esi = data ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = -1 on error ;
|
|
|
|
; ;
|
|
|
|
;------------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_output_raw:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"IPv4_output_raw: size=%u ptr=%x socket=%x\n", ecx, esi, eax
|
|
|
|
|
|
|
|
sub esp, 8
|
|
|
|
push esi eax
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call ipv4_route
|
|
|
|
call arp_ip_to_mac
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test eax, 0xffff0000 ; error bits
|
|
|
|
jnz .arp_error
|
|
|
|
|
|
|
|
push ebx ; push the mac
|
|
|
|
push ax
|
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
inc [IPv4_packets_tx + 4*edi]
|
2014-08-02 14:48:32 +02:00
|
|
|
mov ax, ETHER_PROTO_IPv4
|
2013-06-05 02:21:20 +02:00
|
|
|
mov ebx, [NET_DRV_LIST + 4*edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ecx, [esp + 6 + 4]
|
|
|
|
add ecx, sizeof.IPv4_header
|
2014-08-02 14:48:32 +02:00
|
|
|
mov edx, esp
|
2015-12-27 16:37:31 +01:00
|
|
|
call eth_output
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .error
|
|
|
|
add esp, 6 ; pop the mac
|
|
|
|
|
|
|
|
mov dword[esp+4+4], edx
|
|
|
|
mov dword[esp+4+4+4], eax
|
|
|
|
|
|
|
|
pop eax esi
|
2015-10-11 20:23:40 +02:00
|
|
|
;; TODO: check socket options if we should add header, or just compute checksum
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push edi ecx
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsb
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx edi
|
|
|
|
|
|
|
|
; [edi + IPv4_header.VersionAndIHL] ; IPv4, normal length (no Optional header)
|
|
|
|
; [edi + IPv4_header.TypeOfService] ; nothing special, just plain ip packet
|
|
|
|
; [edi + IPv4_header.TotalLength]
|
|
|
|
; [edi + IPv4_header.TotalLength] ; internet byte order
|
|
|
|
; [edi + IPv4_header.FlagsAndFragmentOffset]
|
|
|
|
|
|
|
|
mov [edi + IPv4_header.HeaderChecksum], 0
|
|
|
|
|
|
|
|
; [edi + IPv4_header.TimeToLive] ; ttl shl 8 + protocol
|
|
|
|
; [edi + IPv4_header.Protocol]
|
|
|
|
; [edi + IPv4_header.Identification] ; fragment id
|
|
|
|
; [edi + IPv4_header.SourceAddress]
|
|
|
|
; [edi + IPv4_header.DestinationAddress]
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_checksum edi ;;;; todo: checksum for IP packet with options!
|
2013-05-28 19:34:26 +02:00
|
|
|
add edi, sizeof.IPv4_header
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_output_raw: device=%x\n", ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2015-10-11 20:23:40 +02:00
|
|
|
add esp, 6+8+4+4
|
|
|
|
mov ebx, ENOBUFS ; FIXME: NOBUFS or MSGSIZE error
|
|
|
|
or eax, -1
|
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.arp_error:
|
|
|
|
add esp, 8+4+4
|
2015-10-11 20:23:40 +02:00
|
|
|
mov ebx, ENOTCONN
|
2014-08-02 14:48:32 +02:00
|
|
|
or eax, -1
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_fragment ;
|
|
|
|
; ;
|
|
|
|
; IN: [esp] = ptr to packet buffer to fragment ;
|
|
|
|
; edi = ptrr to ip header in that buffer ;
|
|
|
|
; ebx = device ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
proc ipv4_fragment stdcall buffer
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
locals
|
|
|
|
offset dd ?
|
|
|
|
headerlength dd ?
|
|
|
|
headerptr dd ?
|
|
|
|
dataptr dd ?
|
|
|
|
remaining dd ?
|
|
|
|
segmentsize dd ?
|
|
|
|
endl
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_fragment\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; We must be able to put at least 8 bytes per segment
|
|
|
|
movzx eax, byte[edi] ; IHL
|
|
|
|
and eax, 0xf
|
|
|
|
shl eax, 2
|
|
|
|
mov [headerlength], eax
|
|
|
|
add eax, 8
|
|
|
|
mov ecx, [ebx + NET_DEVICE.mtu]
|
|
|
|
and ecx, not 11b
|
|
|
|
cmp ecx, eax
|
|
|
|
jb .fail
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
mov [edi + IPv4_header.HeaderChecksum], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
mov [segmentsize], ecx
|
|
|
|
mov [headerptr], edi
|
|
|
|
movzx ecx, [edi + IPv4_header.TotalLength]
|
|
|
|
xchg cl, ch
|
|
|
|
sub ecx, [headerlength]
|
|
|
|
mov [remaining], ecx
|
|
|
|
mov [offset], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
add edi, [headerlength]
|
|
|
|
mov [dataptr], edi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
.loop:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: new fragment"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
mov ecx, [segmentsize]
|
|
|
|
cmp ecx, [remaining]
|
|
|
|
jbe @f
|
|
|
|
mov ecx, [remaining]
|
|
|
|
@@:
|
|
|
|
|
2014-08-02 14:48:32 +02:00
|
|
|
mov ax, ETHER_PROTO_IPv4
|
2015-10-11 20:23:40 +02:00
|
|
|
mov edx, [esp]
|
|
|
|
add edx, [edx + NET_BUFF.offset]
|
|
|
|
; add edx, ETH_header.DstMAC ; = 0
|
2013-05-28 19:34:26 +02:00
|
|
|
call ETH_output
|
2015-10-11 20:23:40 +02:00
|
|
|
jz .fail
|
|
|
|
|
|
|
|
push edi
|
|
|
|
mov edx, ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; copy header
|
2015-10-11 20:23:40 +02:00
|
|
|
mov esi, [headerptr]
|
|
|
|
mov ecx, [headerlength]
|
|
|
|
shr ecx, 2
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsd
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; copy data
|
2015-10-11 20:23:40 +02:00
|
|
|
mov esi, [dataptr]
|
|
|
|
add esi, [offset]
|
|
|
|
mov ecx, edx
|
|
|
|
sub ecx, [headerlength]
|
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
|
|
|
pop edi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; now, correct header
|
2015-10-11 20:23:40 +02:00
|
|
|
; packet length
|
|
|
|
mov ax, dx
|
|
|
|
xchg al, ah
|
|
|
|
mov [edi + IPv4_header.TotalLength], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; offset
|
|
|
|
mov eax, [offset]
|
|
|
|
xchg al, ah
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
sub edx, [headerlength]
|
|
|
|
sub [remaining], edx
|
|
|
|
je @f
|
|
|
|
jb .fail
|
|
|
|
or ah, 1 shl 2 ; more fragments
|
|
|
|
add [offset], edx
|
|
|
|
@@:
|
|
|
|
mov [edi + IPv4_header.FlagsAndFragmentOffset], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; Send the fragment
|
2013-05-28 19:34:26 +02:00
|
|
|
IPv4_checksum edi
|
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
cmp [remaining], 0
|
|
|
|
jne .loop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
call NET_BUFF_free
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
.fail:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "Ipv4_fragment: failed\n"
|
2015-03-17 22:50:29 +01:00
|
|
|
call NET_BUFF_free
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
endp
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_route ;
|
|
|
|
; ;
|
|
|
|
; IN: eax = Destination IP ;
|
|
|
|
; ebx = outgoing device / 0 ;
|
|
|
|
; edx = Source IP ;
|
|
|
|
; ;
|
2016-02-01 20:43:10 +01:00
|
|
|
; OUT: eax = Destination IP (may be gateway), 0 on error ;
|
2015-12-27 16:37:31 +01:00
|
|
|
; edx = Source IP ;
|
|
|
|
; edi = device number*4 ;
|
|
|
|
; ;
|
|
|
|
; DESTROYED: ;
|
|
|
|
; ecx ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2016-02-01 20:43:10 +01:00
|
|
|
ipv4_route:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-07-20 13:13:42 +02:00
|
|
|
test ebx, ebx
|
|
|
|
jnz .got_device
|
|
|
|
|
2016-02-14 20:35:20 +01:00
|
|
|
; Broadcast does not need gateway
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, 0xffffffff
|
|
|
|
je .broadcast
|
|
|
|
|
|
|
|
xor edi, edi
|
|
|
|
.loop:
|
2013-10-19 17:47:58 +02:00
|
|
|
mov ebx, [IP_LIST + edi]
|
|
|
|
and ebx, [SUBNET_LIST + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .next
|
2013-10-19 17:47:58 +02:00
|
|
|
mov ecx, eax
|
|
|
|
and ecx, [SUBNET_LIST + edi]
|
|
|
|
cmp ebx, ecx
|
|
|
|
je .got_it
|
2013-05-28 19:34:26 +02:00
|
|
|
.next:
|
|
|
|
add edi, 4
|
2013-10-19 17:47:58 +02:00
|
|
|
cmp edi, 4*NET_DEVICES_MAX
|
|
|
|
jb .loop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-19 17:47:58 +02:00
|
|
|
mov eax, [GATEWAY_LIST + 4] ; TODO: let user (or a user space daemon) configure default route
|
2013-05-28 19:34:26 +02:00
|
|
|
.broadcast:
|
2013-10-19 17:47:58 +02:00
|
|
|
mov edi, 4 ; TODO: same as above
|
|
|
|
.got_it:
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_route: %u\n", edi
|
|
|
|
test edx, edx
|
|
|
|
jnz @f
|
|
|
|
mov edx, [IP_LIST + edi]
|
|
|
|
@@:
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
2015-07-20 13:13:42 +02:00
|
|
|
.got_device:
|
|
|
|
; Validate device ptr and convert to device number
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_ptr_to_num4
|
2015-07-20 13:13:42 +02:00
|
|
|
cmp edi, -1
|
|
|
|
je .fail
|
|
|
|
|
|
|
|
mov edx, [IP_LIST + edi] ; Source IP
|
|
|
|
|
2016-02-14 20:35:20 +01:00
|
|
|
; Broadcast does not need gateway
|
|
|
|
cmp eax, 0xffffffff
|
|
|
|
je @f
|
|
|
|
|
2015-07-20 13:13:42 +02:00
|
|
|
; Check if we should route to gateway or not
|
|
|
|
mov ebx, [IP_LIST + edi]
|
|
|
|
and ebx, [SUBNET_LIST + edi]
|
|
|
|
mov ecx, eax
|
|
|
|
and ecx, [SUBNET_LIST + edi]
|
2016-08-15 19:27:45 +02:00
|
|
|
cmp ecx, ebx
|
2015-07-20 13:13:42 +02:00
|
|
|
je @f
|
|
|
|
mov eax, [GATEWAY_LIST + edi]
|
|
|
|
@@:
|
2016-08-15 19:27:45 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_route: %u\n", edi
|
2015-07-20 13:13:42 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
2016-08-15 19:27:45 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "IPv4_route failed\n"
|
2016-02-01 20:43:10 +01:00
|
|
|
xor eax, eax
|
2015-07-20 13:13:42 +02:00
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_get_frgmnt_num ;
|
|
|
|
; ;
|
|
|
|
; IN: / ;
|
|
|
|
; ;
|
|
|
|
; OUT: ax = fragment number ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_get_frgmnt_num:
|
2013-05-28 19:34:26 +02:00
|
|
|
xor ax, ax ;;; TODO: replace this with real code
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_connect ;
|
|
|
|
; ;
|
|
|
|
; IN: eax = socket pointer ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = error code on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-10-15 12:35:08 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_connect:
|
2013-10-15 12:35:08 +02:00
|
|
|
|
2013-10-15 18:18:58 +02:00
|
|
|
push eax edx
|
2013-10-15 12:35:08 +02:00
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
2013-10-15 18:18:58 +02:00
|
|
|
pop edx eax
|
2013-10-15 12:35:08 +02:00
|
|
|
|
|
|
|
; Fill in local IP
|
|
|
|
cmp [eax + IP_SOCKET.LocalIP], 0
|
|
|
|
jne @f
|
|
|
|
push [IP_LIST + 4] ; FIXME: use correct local IP
|
|
|
|
pop [eax + IP_SOCKET.LocalIP]
|
|
|
|
|
|
|
|
; Fill in remote IP
|
|
|
|
pushd [edx + 4]
|
|
|
|
pop [eax + IP_SOCKET.RemoteIP]
|
|
|
|
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; ipv4_API: Part of system function 76. ;
|
|
|
|
; ;
|
|
|
|
; IN: bl = subfunction number ;
|
|
|
|
; bh = device number ;
|
|
|
|
; ecx, edx, .. depends on subfunction ;
|
|
|
|
; ;
|
|
|
|
; OUT: depends on subfunction ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_api:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
|
|
|
and ebx, 0x000000ff
|
|
|
|
cmp ebx, .number
|
|
|
|
ja .error
|
|
|
|
jmp dword [.table + 4*ebx]
|
|
|
|
|
|
|
|
.table:
|
|
|
|
dd .packets_tx ; 0
|
|
|
|
dd .packets_rx ; 1
|
|
|
|
dd .read_ip ; 2
|
|
|
|
dd .write_ip ; 3
|
|
|
|
dd .read_dns ; 4
|
|
|
|
dd .write_dns ; 5
|
|
|
|
dd .read_subnet ; 6
|
|
|
|
dd .write_subnet ; 7
|
|
|
|
dd .read_gateway ; 8
|
|
|
|
dd .write_gateway ; 9
|
|
|
|
.number = ($ - .table) / 4 - 1
|
|
|
|
|
|
|
|
.error:
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx:
|
2013-10-19 17:47:58 +02:00
|
|
|
mov eax, [IPv4_packets_tx + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx:
|
2013-10-19 17:47:58 +02:00
|
|
|
mov eax, [IPv4_packets_rx + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.read_ip:
|
|
|
|
mov eax, [IP_LIST + eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.write_ip:
|
|
|
|
mov [IP_LIST + eax], ecx
|
|
|
|
mov edi, eax ; device number, we'll need it for ARP
|
|
|
|
|
|
|
|
; pre-calculate the local broadcast address
|
|
|
|
mov ebx, [SUBNET_LIST + eax]
|
|
|
|
not ebx
|
|
|
|
or ebx, ecx
|
|
|
|
mov [BROADCAST_LIST + eax], ebx
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
mov ebx, [NET_DRV_LIST + eax]
|
|
|
|
mov eax, [IP_LIST + eax]
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_output_request ; now send a gratuitous ARP
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_send_event
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.read_dns:
|
|
|
|
mov eax, [DNS_LIST + eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.write_dns:
|
|
|
|
mov [DNS_LIST + eax], ecx
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_send_event
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.read_subnet:
|
|
|
|
mov eax, [SUBNET_LIST + eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.write_subnet:
|
|
|
|
mov [SUBNET_LIST + eax], ecx
|
|
|
|
|
|
|
|
; pre-calculate the local broadcast address
|
|
|
|
mov ebx, [IP_LIST + eax]
|
|
|
|
not ecx
|
|
|
|
or ecx, ebx
|
|
|
|
mov [BROADCAST_LIST + eax], ecx
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_send_event
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.read_gateway:
|
|
|
|
mov eax, [GATEWAY_LIST + eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.write_gateway:
|
|
|
|
mov [GATEWAY_LIST + eax], ecx
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_send_event
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|