2010-10-01 13:21:55 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2012-03-15 16:41:29 +04:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
|
2010-10-01 13:21:55 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; TCP.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; TCP Processes for Menuet OS TCP/IP stack ;;
|
|
|
|
;; ;;
|
|
|
|
;; Copyright 2002 Mike Hibbett, mikeh@oceanfree.net ;;
|
|
|
|
;; ;;
|
|
|
|
;; See file COPYING for details ;;
|
|
|
|
;; v0.6 : Added reset handling in the established state ;;
|
|
|
|
;; Added a timer per socket to allow delays when ;;
|
|
|
|
;; rx window gets below 1KB ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
$Revision$
|
|
|
|
|
|
|
|
|
|
|
|
; TCP TCB states
|
2012-03-08 12:33:38 +04:00
|
|
|
TCB_LISTEN equ 1
|
|
|
|
TCB_SYN_SENT equ 2
|
|
|
|
TCB_SYN_RECEIVED equ 3
|
|
|
|
TCB_ESTABLISHED equ 4
|
|
|
|
TCB_FIN_WAIT_1 equ 5
|
|
|
|
TCB_FIN_WAIT_2 equ 6
|
|
|
|
TCB_CLOSE_WAIT equ 7
|
|
|
|
TCB_CLOSING equ 8
|
|
|
|
TCB_LAST_ACK equ 9
|
|
|
|
TCB_TIMED_WAIT equ 10
|
|
|
|
TCB_CLOSED equ 11
|
|
|
|
|
|
|
|
TH_FIN = 0x01
|
|
|
|
TH_SYN = 0x02
|
|
|
|
TH_RST = 0x04
|
2010-10-01 13:21:55 +04:00
|
|
|
TH_PUSH = 0x08
|
2012-03-08 12:33:38 +04:00
|
|
|
TH_ACK = 0x10
|
|
|
|
TH_URG = 0x20
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
TWOMSL equ 10 ; # of secs to wait before closing socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
TCP_RETRIES equ 5 ; Number of times to resend a packet
|
|
|
|
TCP_TIMEOUT equ 20 ; resend if not replied to in x hs
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
;*******************************************************************
|
|
|
|
; Interface
|
|
|
|
;
|
|
|
|
; tcp_tx_handler Handles the TCP transmit queue
|
|
|
|
; tcp_rx The protocol handler for received data
|
|
|
|
; buildTCPPacket fills in the packet headers and data
|
|
|
|
; tcpStateMachine Main state machine for received TCP packets
|
|
|
|
; tcp_tcb_handler 1s timer, to erase tcb's in TIME_WAIT state
|
|
|
|
;
|
|
|
|
;*******************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
; TCP Payload ( Data field in IP datagram )
|
|
|
|
;
|
|
|
|
; 0 1 2 3
|
|
|
|
; 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
;20 | Source Port | Destination Port |
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
;24 | Sequence Number |
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
;28 | Acknowledgment Number |
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
;32 | Data | |U|A|P|R|S|F| |
|
|
|
|
; | Offset| Reserved |R|C|S|S|Y|I| Window |
|
|
|
|
; | | |G|K|H|T|N|N| |
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
;36 | Checksum | Urgent Pointer |
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
;40 | Options | Padding |
|
|
|
|
; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
; | data
|
|
|
|
|
|
|
|
|
|
|
|
struc TCP_PACKET
|
2012-03-08 12:33:38 +04:00
|
|
|
{ .SourcePort dw ? ;+00
|
2010-10-01 13:21:55 +04:00
|
|
|
.DestinationPort dw ? ;+02
|
|
|
|
.SequenceNumber dd ? ;+04
|
2012-03-08 12:33:38 +04:00
|
|
|
.AckNumber dd ? ;+08
|
|
|
|
.DataOffset db ? ;+12 - DataOffset[0-3 bits] and Reserved[4-7]
|
|
|
|
.Flags db ? ;+13 - Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN
|
|
|
|
.Window dw ? ;+14
|
|
|
|
.Checksum dw ? ;+16
|
2010-10-01 13:21:55 +04:00
|
|
|
.UrgentPointer dw ? ;+18
|
2012-03-08 12:33:38 +04:00
|
|
|
.Options rb 3 ;+20
|
|
|
|
.Padding db ? ;+23
|
|
|
|
.Data db ? ;+24
|
2010-10-01 13:21:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual at 0
|
|
|
|
TCP_PACKET TCP_PACKET
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; tcp_tcb_handler
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; Handles sockets in the timewait state, closing them
|
|
|
|
; when the TCB timer expires
|
|
|
|
;
|
|
|
|
;***************************************************************************
|
|
|
|
|
|
|
|
proc tcp_tcb_handler stdcall uses ebx
|
2012-03-08 12:33:38 +04:00
|
|
|
; scan through all the sockets, decrementing active timers
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, net_sockets
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [ebx + SOCKET.NextPtr], 0
|
|
|
|
je .exit
|
|
|
|
;DEBUGF 1, "K : sockets:\n"
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket:
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
or ebx, ebx
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
;DEBUGF 1, "K : %x-%x: %x-%x-%x-%u\n", [ebx + SOCKET.PID]:2, [ebx + SOCKET.Number]:2, [ebx + SOCKET.LocalPort]:4, [ebx + SOCKET.RemoteIP], [ebx + SOCKET.RemotePort]:4, [ebx + SOCKET.TCBState]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [ebx + SOCKET.TCBTimer], 0
|
|
|
|
jne .decrement_tcb
|
|
|
|
cmp [ebx + SOCKET.wndsizeTimer], 0
|
|
|
|
jne .decrement_wnd
|
|
|
|
jmp .next_socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.decrement_tcb:
|
2012-03-08 12:33:38 +04:00
|
|
|
; decrement it, delete socket if TCB timer = 0 & socket in timewait state
|
|
|
|
dec [ebx + SOCKET.TCBTimer]
|
|
|
|
jnz .next_socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [ebx + SOCKET.TCBState], TCB_TIMED_WAIT
|
|
|
|
jne .next_socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
push [ebx + SOCKET.PrevPtr]
|
|
|
|
stdcall net_socket_free, ebx
|
|
|
|
pop ebx
|
|
|
|
jmp .next_socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.decrement_wnd:
|
2012-03-08 12:33:38 +04:00
|
|
|
; TODO - prove it works!
|
|
|
|
dec [ebx + SOCKET.wndsizeTimer]
|
|
|
|
jmp .next_socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; tcp_tx_handler
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; Handles queued TCP data
|
|
|
|
; This is a kernel function, called by stack_handler
|
|
|
|
;
|
|
|
|
;***************************************************************************
|
|
|
|
|
|
|
|
proc tcp_tx_handler stdcall
|
|
|
|
; decrement all resend buffers timers. If they
|
|
|
|
; expire, queue them for sending, and restart the timer.
|
|
|
|
; If the retries counter reach 0, delete the entry
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov esi, resendQ
|
|
|
|
mov ecx, 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_resendq:
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp ecx, NUMRESENDENTRIES
|
|
|
|
je .exit ; None left
|
|
|
|
cmp dword[esi + 4], 0
|
|
|
|
jne @f ; found one
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@: ; we have one. decrement it's timer by 1
|
2012-03-08 12:33:38 +04:00
|
|
|
dec word[esi + 2]
|
|
|
|
jz @f
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq ; Timer not zero, so move on
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@:
|
2012-03-08 12:33:38 +04:00
|
|
|
xor ebx, ebx
|
|
|
|
; restart timer, and decrement retries
|
|
|
|
; After the first resend, back of on next, by a factor of 5
|
|
|
|
mov [esi + 2], word TCP_TIMEOUT * 5
|
|
|
|
dec byte[esi + 1]
|
|
|
|
jnz @f
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; retries now 0, so delete from queue
|
|
|
|
xchg [esi + 4], ebx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@: ; resend packet
|
2012-03-08 12:33:38 +04:00
|
|
|
pushad
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, EMPTY_QUEUE
|
|
|
|
call dequeue
|
|
|
|
cmp ax, NO_BUFFER
|
|
|
|
jne .tth004z
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; TODO - try again in 10ms.
|
|
|
|
test ebx, ebx
|
|
|
|
jnz @f
|
|
|
|
mov [esi + 4], ebx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@: ; Mark it to expire in 10ms - 1 tick
|
2012-03-08 12:33:38 +04:00
|
|
|
mov byte[esi + 1], 1
|
|
|
|
mov word[esi + 2], 1
|
|
|
|
jmp .tth005
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.tth004z:
|
2012-03-08 12:33:38 +04:00
|
|
|
; we have a buffer # in ax
|
|
|
|
push eax ecx
|
|
|
|
mov ecx, IPBUFFSIZE
|
|
|
|
mul ecx
|
|
|
|
add eax, IPbuffs
|
|
|
|
|
|
|
|
; we have the buffer address in eax
|
|
|
|
mov edi, eax
|
|
|
|
pop ecx
|
|
|
|
; Now get buffer location, and copy buffer across. argh! more copying,,
|
|
|
|
imul esi, ecx, IPBUFFSIZE
|
|
|
|
add esi, resendBuffer
|
|
|
|
|
|
|
|
; we have resend buffer location in esi
|
|
|
|
mov ecx, IPBUFFSIZE
|
|
|
|
|
|
|
|
; copy data across
|
|
|
|
push edi
|
|
|
|
cld
|
|
|
|
rep movsb
|
|
|
|
pop edi
|
|
|
|
|
|
|
|
; queue packet
|
|
|
|
mov eax, NET1OUT_QUEUE
|
|
|
|
mov edx, [stack_ip]
|
|
|
|
cmp edx, [edi + IP_PACKET.DestinationAddress]
|
|
|
|
jne .not_local
|
|
|
|
mov eax, IPIN_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.not_local:
|
2012-03-08 12:33:38 +04:00
|
|
|
pop ebx
|
|
|
|
call queue
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.tth005:
|
2012-03-08 12:33:38 +04:00
|
|
|
popad
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; tcp_rx
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; TCP protocol handler
|
|
|
|
; This is a kernel function, called by ip_rx
|
|
|
|
; IP buffer address given in edx
|
|
|
|
; IP buffer number in eax
|
|
|
|
; Free up (or re-use) IP buffer when finished
|
|
|
|
;
|
|
|
|
;***************************************************************************
|
|
|
|
|
|
|
|
proc tcp_rx stdcall uses ebx
|
2012-03-08 12:33:38 +04:00
|
|
|
; The process is as follows.
|
|
|
|
; Look for a socket with matching remote IP, remote port, local port
|
|
|
|
; if not found, then
|
|
|
|
; look for remote IP + local port match ( where sockets remote port = 0)
|
|
|
|
; if not found, then
|
|
|
|
; look for a socket where local socket port == IP packets remote port
|
|
|
|
; where sockets remote port, remote IP = 0
|
|
|
|
; discard if not found
|
|
|
|
; Call sockets tcbStateMachine, with pointer to packet.
|
|
|
|
; the state machine will not delete the packet, so do that here.
|
|
|
|
|
|
|
|
push eax
|
|
|
|
|
|
|
|
; Look for a socket where
|
|
|
|
; IP Packet TCP Destination Port = local Port
|
|
|
|
; IP Packet SA = Remote IP
|
|
|
|
; IP Packet TCP Source Port = remote Port
|
|
|
|
|
|
|
|
mov ebx, net_sockets
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket.1:
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
or ebx, ebx
|
|
|
|
jz .next_socket.1.exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 1.dport: %x - %x\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [ebx + SOCKET.LocalPort]:4
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ax, [edx + 20 + TCP_PACKET.DestinationPort] ; get the dest. port from the TCP hdr
|
|
|
|
cmp [ebx + SOCKET.LocalPort], ax ; get the dest. port from the TCP hdr
|
|
|
|
jne .next_socket.1 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 1.addr: %x - %x\n", [edx + IP_PACKET.SourceAddress], [ebx + SOCKET.RemoteIP]
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, [edx + IP_PACKET.SourceAddress] ; get the source IP Addr from the IP hdr
|
|
|
|
cmp [ebx + SOCKET.RemoteIP], eax ; compare with socket's remote IP
|
|
|
|
jne .next_socket.1 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 1.sport: %x - %x\n", [edx + 20 + TCP_PACKET.SourcePort]:4, [ebx + SOCKET.RemotePort]:4
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ax, [edx + 20 + TCP_PACKET.SourcePort] ; get the source port from the TCP hdr
|
|
|
|
cmp [ebx + SOCKET.RemotePort], ax ; compare with socket's remote port
|
|
|
|
jne .next_socket.1 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; We have a complete match - use this socket
|
|
|
|
jmp .change_state
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket.1.exit:
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; If we got here, there was no match
|
|
|
|
; Look for a socket where
|
|
|
|
; IP Packet TCP Destination Port = local Port
|
|
|
|
; IP Packet SA = Remote IP
|
|
|
|
; socket remote Port = 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, net_sockets
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket.2:
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
or ebx, ebx
|
|
|
|
jz .next_socket.2.exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 2.dport: %x - %x\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [ebx + SOCKET.LocalPort]:4
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ax, [edx + 20 + TCP_PACKET.DestinationPort] ; get the dest. port from the TCP hdr
|
|
|
|
cmp [ebx + SOCKET.LocalPort], ax ; compare with socket's local port
|
|
|
|
jne .next_socket.2 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 2.addr: %x - %x\n", [edx + IP_PACKET.SourceAddress], [ebx + SOCKET.RemoteIP]
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, [edx + IP_PACKET.SourceAddress] ; get the source IP Addr from the IP hdr
|
|
|
|
cmp [ebx + SOCKET.RemoteIP], eax ; compare with socket's remote IP
|
|
|
|
jne .next_socket.2 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 2.sport: 0000 - %x\n", [ebx + SOCKET.RemotePort]:4
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [ebx + SOCKET.RemotePort], 0 ; only match a remote socket of 0
|
|
|
|
jne .next_socket.2 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; We have a complete match - use this socket
|
|
|
|
jmp .change_state
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket.2.exit:
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; If we got here, there was no match
|
|
|
|
; Look for a socket where
|
|
|
|
; IP Packet TCP Destination Port = local Port
|
|
|
|
; socket Remote IP = 0
|
|
|
|
; socket remote Port = 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, net_sockets
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket.3:
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
or ebx, ebx
|
|
|
|
jz .next_socket.3.exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 3.dport: %x - %x\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [ebx + SOCKET.LocalPort]:4
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ax, [edx + 20 + TCP_PACKET.DestinationPort] ; get destination port from the TCP hdr
|
|
|
|
cmp [ebx + SOCKET.LocalPort], ax ; compare with socket's local port
|
|
|
|
jne .next_socket.3 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 3.addr: 00000000 - %x\n", [ebx + SOCKET.RemoteIP]
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [ebx + SOCKET.RemoteIP], 0 ; only match a socket remote IP of 0
|
|
|
|
jne .next_socket.3 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
; DEBUGF 1, "K : tcp_rx - 3.sport: 0000 - %x\n", [ebx + SOCKET.RemotePort]:4
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [ebx + SOCKET.RemotePort], 0 ; only match a remote socket of 0
|
|
|
|
jne .next_socket.3 ; different - try next socket
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; We have a complete match - use this socket
|
|
|
|
jmp .change_state
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_socket.3.exit:
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; If we got here, we need to reject the packet
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
DEBUGF 1, "K : tcp_rx - dumped\n"
|
|
|
|
DEBUGF 1, "K : --------: %x-%x-%x (flags: %x)\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [edx + IP_PACKET.SourceAddress], [edx + 20 + TCP_PACKET.SourcePort]:4, [edx + 20 + TCP_PACKET.Flags]:2
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
inc [dumped_rx_count]
|
|
|
|
jmp .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.change_state:
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; We have a valid socket/TCB, so call the TCB State Machine for that skt.
|
|
|
|
; socket is pointed to by ebx
|
|
|
|
; IP packet is pointed to by edx
|
|
|
|
; IP buffer number is on stack ( it will be popped at the end)
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
stdcall tcpStateMachine, ebx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
pop eax
|
|
|
|
call freeBuff
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; buildTCPPacket
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; builds an IP Packet with TCP data fully populated for transmission
|
|
|
|
; You may destroy any and all registers
|
|
|
|
; TCP control flags specified in bl
|
|
|
|
; This TCB is in [sktAddr]
|
|
|
|
; User data pointed to by esi
|
|
|
|
; Data length in ecx
|
|
|
|
; Transmit buffer number in eax
|
|
|
|
;
|
|
|
|
;***************************************************************************
|
|
|
|
|
|
|
|
proc build_tcp_packet stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
push ecx ; Save data length
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; convert buffer pointer eax to the absolute address
|
|
|
|
mov ecx, IPBUFFSIZE
|
|
|
|
mul ecx
|
|
|
|
add eax, IPbuffs
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov edx, eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [edx + 20 + TCP_PACKET.Flags], bl ; TCP flags
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ebx, [sockAddr]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; So, ebx holds the socket ptr, edx holds the IPbuffer ptr
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Fill in the IP header ( some data is in the socket descriptor)
|
|
|
|
mov eax, [ebx + SOCKET.LocalIP]
|
|
|
|
mov [edx + IP_PACKET.SourceAddress], eax
|
|
|
|
mov eax, [ebx + SOCKET.RemoteIP]
|
|
|
|
mov [edx + IP_PACKET.DestinationAddress], eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [edx + IP_PACKET.VersionAndIHL], 0x45
|
|
|
|
mov [edx + IP_PACKET.TypeOfService], 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
pop eax ; Get the TCP data length
|
|
|
|
push eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
add eax, 20 + 20 ; add IP header and TCP header lengths
|
|
|
|
rol ax, 8
|
|
|
|
mov [edx + IP_PACKET.TotalLength], ax
|
|
|
|
mov [edx + IP_PACKET.Identification], 0
|
|
|
|
mov [edx + IP_PACKET.FlagsAndFragmentOffset], 0x0040
|
|
|
|
mov [edx + IP_PACKET.TimeToLive], 0x20
|
|
|
|
mov [edx + IP_PACKET.Protocol], PROTOCOL_TCP
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Checksum left unfilled
|
|
|
|
mov [edx + IP_PACKET.HeaderChecksum], 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Fill in the TCP header (some data is in the socket descriptor)
|
|
|
|
mov ax, [ebx + SOCKET.LocalPort]
|
|
|
|
mov [edx + 20 + TCP_PACKET.SourcePort], ax ; Local Port
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov ax, [ebx + SOCKET.RemotePort]
|
|
|
|
mov [edx + 20 + TCP_PACKET.DestinationPort], ax ; desitination Port
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Checksum left unfilled
|
|
|
|
mov [edx + 20 + TCP_PACKET.Checksum], 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; sequence number
|
|
|
|
mov eax, [ebx + SOCKET.SND_NXT]
|
|
|
|
mov [edx + 20 + TCP_PACKET.SequenceNumber], eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; ack number
|
|
|
|
mov eax, [ebx + SOCKET.RCV_NXT]
|
|
|
|
mov [edx + 20 + TCP_PACKET.AckNumber], eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; window ( 0x2000 is default ).I could accept 4KB, fa0, ( skt buffer size)
|
|
|
|
; 768 bytes seems better
|
|
|
|
mov [edx + 20 + TCP_PACKET.Window], 0x0003
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Urgent pointer (0)
|
|
|
|
mov [edx + 20 + TCP_PACKET.UrgentPointer], 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; data offset ( 0x50 )
|
|
|
|
mov [edx + 20 + TCP_PACKET.DataOffset], 0x50
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
pop ecx ; count of bytes to send
|
|
|
|
mov ebx, ecx ; need the length later
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp ebx, 0
|
|
|
|
jz @f
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov edi, edx
|
|
|
|
add edi, 40
|
|
|
|
cld
|
|
|
|
rep movsb ; copy the data across
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@: ; we have edx as IPbuffer ptr.
|
2012-03-08 12:33:38 +04:00
|
|
|
; Fill in the TCP checksum
|
|
|
|
; First, fill in pseudoheader
|
|
|
|
mov eax, [edx + IP_PACKET.SourceAddress]
|
|
|
|
mov [pseudoHeader], eax
|
|
|
|
mov eax, [edx + IP_PACKET.DestinationAddress]
|
|
|
|
mov [pseudoHeader + 4], eax
|
|
|
|
mov word[pseudoHeader + 8], PROTOCOL_TCP shl 8 + 0
|
|
|
|
add ebx, 20
|
|
|
|
mov [pseudoHeader + 10], bh
|
|
|
|
mov [pseudoHeader + 11], bl
|
|
|
|
|
|
|
|
mov eax, pseudoHeader
|
|
|
|
mov [checkAdd1], eax
|
|
|
|
mov word[checkSize1], 12
|
|
|
|
mov eax, edx
|
|
|
|
add eax, 20
|
|
|
|
mov [checkAdd2], eax
|
|
|
|
mov eax, ebx
|
|
|
|
mov [checkSize2], ax
|
|
|
|
|
|
|
|
call checksum
|
|
|
|
|
|
|
|
; store it in the TCP checksum ( in the correct order! )
|
|
|
|
mov ax, [checkResult]
|
|
|
|
rol ax, 8
|
|
|
|
mov [edx + 20 + TCP_PACKET.Checksum], ax
|
|
|
|
|
|
|
|
; Fill in the IP header checksum
|
|
|
|
GET_IHL eax, edx ; get IP-Header length
|
|
|
|
stdcall checksum_jb, edx, eax ; buf_ptr, buf_size
|
|
|
|
rol ax, 8
|
|
|
|
mov [edx + IP_PACKET.HeaderChecksum], ax
|
|
|
|
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
; Increments the 32 bit value pointed to by esi in internet order
|
|
|
|
proc inc_inet_esi stdcall
|
2012-03-08 12:33:38 +04:00
|
|
|
push eax
|
|
|
|
mov eax, [esi]
|
|
|
|
bswap eax
|
|
|
|
inc eax
|
|
|
|
bswap eax
|
|
|
|
mov [esi], eax
|
|
|
|
pop eax
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
; Increments the 32 bit value pointed to by esi in internet order
|
|
|
|
; by the value in ecx
|
|
|
|
proc add_inet_esi stdcall
|
2012-03-08 12:33:38 +04:00
|
|
|
push eax
|
|
|
|
mov eax, [esi]
|
|
|
|
bswap eax
|
|
|
|
add eax, ecx
|
|
|
|
bswap eax
|
|
|
|
mov [esi], eax
|
|
|
|
pop eax
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
iglobal
|
|
|
|
TCBStateHandler dd \
|
|
|
|
stateTCB_LISTEN, \
|
|
|
|
stateTCB_SYN_SENT, \
|
|
|
|
stateTCB_SYN_RECEIVED, \
|
|
|
|
stateTCB_ESTABLISHED, \
|
|
|
|
stateTCB_FIN_WAIT_1, \
|
|
|
|
stateTCB_FIN_WAIT_2, \
|
|
|
|
stateTCB_CLOSE_WAIT, \
|
|
|
|
stateTCB_CLOSING, \
|
|
|
|
stateTCB_LAST_ACK, \
|
|
|
|
stateTCB_TIME_WAIT, \
|
|
|
|
stateTCB_CLOSED
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; tcpStateMachine
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; TCP state machine
|
|
|
|
; This is a kernel function, called by tcp_rx
|
|
|
|
;
|
|
|
|
; IP buffer address given in edx
|
|
|
|
; Socket/TCB address in ebx
|
|
|
|
;
|
|
|
|
; The IP buffer will be released by the caller
|
|
|
|
;***************************************************************************
|
|
|
|
|
|
|
|
proc tcpStateMachine stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; as a packet has been received, update the TCB timer
|
|
|
|
mov [ebx + SOCKET.TCBTimer], TWOMSL
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; If the received packet has an ACK bit set,
|
|
|
|
; remove any packets in the resend queue that this
|
|
|
|
; received packet acknowledges
|
|
|
|
pushad
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_ACK
|
|
|
|
jz .call_handler ; No ACK, so no data yet
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; get skt number in eax
|
|
|
|
stdcall net_socket_addr_to_num, ebx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; The ack number is in [edx + 28], inet format
|
|
|
|
; skt in eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov esi, resendQ
|
|
|
|
xor ecx, ecx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_resendq:
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp ecx, NUMRESENDENTRIES
|
|
|
|
je .call_handler ; None left
|
|
|
|
cmp [esi + 4], eax
|
|
|
|
je @f ; found one
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
|
|
|
|
|
|
|
@@: ; Can we delete this buffer?
|
|
|
|
|
|
|
|
; If yes, goto @@. No, goto .next_resendq
|
|
|
|
; Get packet data address
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
; Now get buffer location, and copy buffer across. argh! more copying,,
|
|
|
|
imul edi, ecx, IPBUFFSIZE
|
|
|
|
add edi, resendBuffer
|
|
|
|
|
|
|
|
; we have dest buffer location in edi. incoming packet in edx.
|
|
|
|
; Get this packets sequence number
|
|
|
|
; preserve al, ecx, esi, edx
|
|
|
|
mov ecx, [edi + 20 + TCP_PACKET.SequenceNumber]
|
|
|
|
bswap ecx
|
|
|
|
movzx ebx, word[edi + 2]
|
|
|
|
xchg bl, bh
|
|
|
|
sub ebx, 40
|
|
|
|
add ecx, ebx ; ecx is now seq# of last byte +1, intel format
|
|
|
|
|
|
|
|
; get recievd ack #, in intel format
|
|
|
|
mov ebx, [edx + 20 + TCP_PACKET.AckNumber]
|
|
|
|
bswap ebx
|
|
|
|
|
|
|
|
cmp ebx, ecx ; Finally. ecx = rx'ed ack. ebx = last byte in que
|
|
|
|
; DANGER! need to handle case that we have just
|
|
|
|
; passed the 2**32, and wrapped round!
|
|
|
|
pop ecx
|
|
|
|
jae @f ; if rx > old, delete old
|
|
|
|
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
|
|
|
|
|
|
|
@@:
|
|
|
|
mov dword[esi + 4], 0
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.call_handler:
|
2012-03-08 12:33:38 +04:00
|
|
|
popad
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Call handler for given TCB state
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, [ebx + SOCKET.TCBState]
|
|
|
|
cmp eax, TCB_LISTEN
|
|
|
|
jb .exit
|
|
|
|
cmp eax, TCB_CLOSED
|
|
|
|
ja .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
stdcall [TCBStateHandler + (eax - 1) * 4], [sockAddr]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; signal_network_event
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; Signals about network event to socket owner
|
2012-03-08 12:33:38 +04:00
|
|
|
; This is a kernel function, called from TCP handler
|
2010-10-01 13:21:55 +04:00
|
|
|
;
|
|
|
|
; Socket/TCB address in ebx
|
|
|
|
;***************************************************************************
|
|
|
|
proc signal_network_event
|
2012-03-08 12:33:38 +04:00
|
|
|
push ecx esi eax
|
|
|
|
mov eax, [ebx + SOCKET.PID]
|
|
|
|
mov ecx, 1
|
|
|
|
mov esi, TASK_DATA + TASKDATA.pid
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_pid:
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp [esi], eax
|
|
|
|
je .found_pid
|
|
|
|
inc ecx
|
|
|
|
add esi, 0x20
|
|
|
|
cmp ecx, [TASK_COUNT]
|
|
|
|
jbe .next_pid
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.found_pid:
|
2012-03-08 12:33:38 +04:00
|
|
|
shl ecx, 8
|
|
|
|
or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
|
|
|
|
pop eax esi ecx
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
proc stateTCB_LISTEN stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; In this case, we are expecting a SYN packet
|
|
|
|
; For now, if the packet is a SYN, process it, and send a response
|
|
|
|
; If not, ignore it
|
|
|
|
|
|
|
|
; Look at control flags
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_SYN
|
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; We have a SYN. update the socket with this IP packets details,
|
|
|
|
; And send a response
|
|
|
|
|
|
|
|
mov eax, [edx + IP_PACKET.SourceAddress]
|
|
|
|
mov [ebx + SOCKET.RemoteIP], eax
|
|
|
|
mov ax, [edx + 20 + TCP_PACKET.SourcePort]
|
|
|
|
mov [ebx + SOCKET.RemotePort], ax
|
|
|
|
mov eax, [edx + 20 + TCP_PACKET.SequenceNumber]
|
|
|
|
mov [ebx + SOCKET.IRS], eax
|
|
|
|
mov [ebx + SOCKET.RCV_NXT], eax
|
|
|
|
lea esi, [ebx + SOCKET.RCV_NXT]
|
|
|
|
call inc_inet_esi ; RCV.NXT
|
|
|
|
mov eax, [ebx + SOCKET.ISS]
|
|
|
|
mov [ebx + SOCKET.SND_NXT], eax
|
|
|
|
|
|
|
|
; Now construct the response, and queue for sending by IP
|
|
|
|
mov eax, EMPTY_QUEUE
|
|
|
|
call dequeue
|
|
|
|
cmp ax, NO_BUFFER
|
|
|
|
je .exit
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
push eax
|
|
|
|
mov bl, TH_SYN + TH_ACK
|
|
|
|
xor ecx, ecx
|
|
|
|
xor esi, esi
|
|
|
|
stdcall build_tcp_packet, [sockAddr]
|
|
|
|
|
|
|
|
mov eax, NET1OUT_QUEUE
|
|
|
|
mov edx, [stack_ip]
|
|
|
|
mov ecx, [sockAddr]
|
|
|
|
cmp edx, [ecx + SOCKET.RemoteIP]
|
|
|
|
jne .not_local
|
|
|
|
mov eax, IPIN_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.not_local:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send it.
|
|
|
|
pop ebx
|
|
|
|
call queue
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
pop ebx
|
|
|
|
mov esi, [sockAddr]
|
|
|
|
mov [esi + SOCKET.TCBState], TCB_SYN_RECEIVED
|
|
|
|
call signal_network_event
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; increment SND.NXT in socket
|
|
|
|
add esi, SOCKET.SND_NXT
|
|
|
|
call inc_inet_esi
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_SYN_SENT stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; We are awaiting an ACK to our SYN, with a SYM
|
|
|
|
; Look at control flags - expecting an ACK
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov al, [edx + 20 + TCP_PACKET.Flags]
|
|
|
|
and al, TH_SYN + TH_ACK
|
|
|
|
cmp al, TH_SYN + TH_ACK
|
|
|
|
je .syn_ack
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
test al, TH_SYN
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.TCBState], TCB_SYN_RECEIVED
|
|
|
|
push TH_SYN + TH_ACK
|
|
|
|
jmp .send
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.syn_ack:
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.TCBState], TCB_ESTABLISHED
|
|
|
|
push TH_ACK
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.send:
|
2012-03-08 12:33:38 +04:00
|
|
|
call signal_network_event
|
|
|
|
; Store the recv.nxt field
|
|
|
|
mov eax, [edx + 20 + TCP_PACKET.SequenceNumber]
|
|
|
|
|
|
|
|
; Update our recv.nxt field
|
|
|
|
mov [ebx + SOCKET.RCV_NXT], eax
|
|
|
|
lea esi, [ebx + SOCKET.RCV_NXT]
|
|
|
|
call inc_inet_esi
|
|
|
|
|
|
|
|
; Send an ACK
|
|
|
|
; Now construct the response, and queue for sending by IP
|
|
|
|
mov eax, EMPTY_QUEUE
|
|
|
|
call dequeue
|
|
|
|
cmp ax, NO_BUFFER
|
|
|
|
pop ebx
|
|
|
|
je .exit
|
|
|
|
|
|
|
|
push eax
|
|
|
|
|
|
|
|
xor ecx, ecx
|
|
|
|
xor esi, esi
|
|
|
|
stdcall build_tcp_packet, [sockAddr]
|
|
|
|
|
|
|
|
mov eax, NET1OUT_QUEUE
|
|
|
|
mov edx, [stack_ip]
|
|
|
|
mov ecx, [sockAddr]
|
|
|
|
cmp edx, [ecx + SOCKET.RemoteIP]
|
|
|
|
jne .not_local
|
|
|
|
mov eax, IPIN_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.not_local:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send it.
|
|
|
|
pop ebx
|
|
|
|
call queue
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_SYN_RECEIVED stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; In this case, we are expecting an ACK packet
|
|
|
|
; For now, if the packet is an ACK, process it,
|
|
|
|
; If not, ignore it
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_RST
|
|
|
|
jz .check_ack
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
push [ebx + SOCKET.OrigRemotePort] [ebx + SOCKET.OrigRemoteIP]
|
|
|
|
pop [ebx + SOCKET.RemoteIP] [ebx + SOCKET.RemotePort]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.TCBState], TCB_LISTEN
|
|
|
|
jmp .signal
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.check_ack:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Look at control flags - expecting an ACK
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_ACK
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.TCBState], TCB_ESTABLISHED
|
2010-10-01 13:21:55 +04:00
|
|
|
.signal:
|
2012-03-08 12:33:38 +04:00
|
|
|
call signal_network_event
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_ESTABLISHED stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; Here we are expecting data, or a request to close
|
|
|
|
; OR both...
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Ignore all packets with sequnce number other than next expected
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; recv.nxt is in dword [edx+24], in inet format
|
|
|
|
; recv seq is in [sktAddr]+56, in inet format
|
|
|
|
; just do a comparision
|
|
|
|
mov eax, [ebx + SOCKET.RCV_NXT]
|
|
|
|
cmp eax, [edx + 20 + TCP_PACKET.SequenceNumber]
|
|
|
|
jne .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Did we receive a FIN or RST?
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_FIN+TH_RST
|
|
|
|
jz .check_ack
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; It was a fin or reset.
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Remove resend entries from the queue - I dont want to send any more data
|
|
|
|
pushad
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; get skt #
|
|
|
|
stdcall net_socket_addr_to_num, ebx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov esi, resendQ
|
|
|
|
mov ecx, 0
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.next_resendq:
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp ecx, NUMRESENDENTRIES
|
|
|
|
je .last_resendq ; None left
|
|
|
|
cmp [esi + 4], eax
|
|
|
|
je @f ; found one
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
|
|
|
|
|
|
|
@@:
|
|
|
|
mov dword[esi + 4], 0
|
|
|
|
inc ecx
|
|
|
|
add esi, 8
|
|
|
|
jmp .next_resendq
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.last_resendq:
|
2012-03-08 12:33:38 +04:00
|
|
|
popad
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@: ; Send an ACK to that fin, and enter closewait state
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.TCBState], TCB_CLOSE_WAIT
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_RST
|
|
|
|
je @f
|
|
|
|
mov [ebx + SOCKET.TCBState], TCB_CLOSED
|
|
|
|
@@:
|
|
|
|
call signal_network_event
|
|
|
|
lea esi, [ebx + SOCKET.RCV_NXT]
|
|
|
|
mov eax, [esi] ; save original
|
|
|
|
call inc_inet_esi
|
|
|
|
;; jmp ste_ack - NO, there may be data
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.check_ack:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Check that we received an ACK
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_ACK
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; TODO - done, I think!
|
|
|
|
; First, look at the incoming window. If this is less than or equal to 1024,
|
|
|
|
; Set the socket window timer to 1. This will stop an additional packets being queued.
|
|
|
|
; ** I may need to tweak this value, since I do not know how many packets are already queued
|
|
|
|
mov cx, [edx + 20 + TCP_PACKET.Window]
|
|
|
|
xchg cl, ch
|
|
|
|
cmp cx, 1024
|
|
|
|
ja @f
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.wndsizeTimer], 1
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
@@: ; OK, here is the deal
|
|
|
|
|
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Read the data bytes, store in socket buffer
|
|
|
|
movzx ecx, [edx + IP_PACKET.TotalLength]
|
|
|
|
xchg cl, ch
|
|
|
|
sub ecx, 40 ; Discard 40 bytes of header
|
|
|
|
ja .data ; Read data, if any
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; If we had received a fin, we need to ACK it.
|
|
|
|
cmp [ebx + SOCKET.TCBState], TCB_CLOSE_WAIT
|
|
|
|
je .ack
|
|
|
|
jmp .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.data:
|
2012-03-08 12:33:38 +04:00
|
|
|
push ecx
|
|
|
|
push ecx edx
|
|
|
|
lea ecx, [ebx+SOCKET.mutex]
|
|
|
|
call mutex_lock
|
2011-09-19 00:51:00 +04:00
|
|
|
pop edx ecx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
push ebx
|
|
|
|
mov eax, [ebx + SOCKET.rxDataCount]
|
|
|
|
add eax, ecx
|
|
|
|
cmp eax, SOCKETBUFFSIZE - SOCKETHEADERSIZE
|
|
|
|
ja .overflow
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.rxDataCount], eax ; increment the count of bytes in buffer
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; point to the location to store the data
|
|
|
|
lea edi, [ebx + eax + SOCKETHEADERSIZE]
|
|
|
|
sub edi, ecx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
add edx, 40 ; edx now points to the data
|
|
|
|
mov esi, edx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cld
|
|
|
|
rep movsb ; copy the data across
|
2011-08-27 15:24:26 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; flag an event to the application
|
|
|
|
pop ebx
|
|
|
|
call signal_network_event
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
pop ecx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Update our recv.nxt field
|
|
|
|
lea esi, [ebx + SOCKET.RCV_NXT]
|
|
|
|
call add_inet_esi
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.ack:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send an ACK
|
|
|
|
; Now construct the response, and queue for sending by IP
|
|
|
|
mov eax, EMPTY_QUEUE
|
|
|
|
call dequeue
|
|
|
|
cmp ax, NO_BUFFER
|
|
|
|
je .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
push eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov bl, TH_ACK
|
|
|
|
xor ecx, ecx
|
|
|
|
xor esi, esi
|
|
|
|
stdcall build_tcp_packet, [sockAddr]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, NET1OUT_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov edx, [stack_ip]
|
|
|
|
mov ecx, [sockAddr]
|
|
|
|
cmp edx, [ecx + SOCKET.RemoteIP]
|
|
|
|
jne .not_local
|
|
|
|
mov eax, IPIN_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.not_local:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send it.
|
|
|
|
pop ebx
|
|
|
|
call queue
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
.overflow:
|
2012-03-08 12:33:38 +04:00
|
|
|
; no place in buffer
|
|
|
|
; so simply restore stack and exit
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
pop eax ecx
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_FIN_WAIT_1 stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; We can either receive an ACK of a fin, or a fin
|
|
|
|
mov al, [edx + 20 + TCP_PACKET.Flags]
|
|
|
|
and al, TH_FIN + TH_ACK
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
cmp al, TH_ACK
|
|
|
|
jne @f
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; It was an ACK
|
|
|
|
mov [ebx + SOCKET.TCBState], TCB_FIN_WAIT_2
|
|
|
|
jmp .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
@@:
|
|
|
|
mov [ebx + SOCKET.TCBState], TCB_CLOSING
|
|
|
|
cmp al, TH_FIN
|
|
|
|
je @f
|
|
|
|
mov [ebx + SOCKET.TCBState], TCB_TIMED_WAIT
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
@@:
|
|
|
|
lea esi, [ebx + SOCKET.RCV_NXT]
|
|
|
|
call inc_inet_esi
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send an ACK
|
|
|
|
mov eax, EMPTY_QUEUE
|
|
|
|
call dequeue
|
|
|
|
cmp ax, NO_BUFFER
|
|
|
|
je .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
push eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov bl, TH_ACK
|
|
|
|
xor ecx, ecx
|
|
|
|
xor esi, esi
|
|
|
|
stdcall build_tcp_packet, [sockAddr]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, NET1OUT_QUEUE
|
|
|
|
mov edx, [stack_ip]
|
|
|
|
mov ecx, [sockAddr]
|
|
|
|
cmp edx, [ecx + SOCKET.RemoteIP]
|
|
|
|
jne .not_local
|
|
|
|
mov eax, IPIN_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.not_local:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send it.
|
|
|
|
pop ebx
|
|
|
|
call queue
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_FIN_WAIT_2 stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_FIN
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Change state, as we have a fin
|
|
|
|
mov [ebx + SOCKET.TCBState], TCB_TIMED_WAIT
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
lea esi, [ebx + SOCKET.RCV_NXT]
|
|
|
|
call inc_inet_esi
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send an ACK
|
|
|
|
mov eax, EMPTY_QUEUE
|
|
|
|
call dequeue
|
|
|
|
cmp ax, NO_BUFFER
|
|
|
|
je .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
push eax
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov bl, TH_ACK
|
|
|
|
xor ecx, ecx
|
|
|
|
xor esi, esi
|
|
|
|
stdcall build_tcp_packet, [sockAddr]
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov eax, NET1OUT_QUEUE
|
|
|
|
mov edx, [stack_ip]
|
|
|
|
mov ecx, [sockAddr]
|
|
|
|
cmp edx, [ecx + SOCKET.RemoteIP]
|
|
|
|
jne .not_local
|
|
|
|
mov eax, IPIN_QUEUE
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.not_local:
|
2012-03-08 12:33:38 +04:00
|
|
|
; Send it.
|
|
|
|
pop ebx
|
|
|
|
call queue
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_CLOSE_WAIT stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; Intentionally left empty
|
|
|
|
; socket_close_tcp handles this
|
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_CLOSING stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; We can either receive an ACK of a fin, or a fin
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_ACK
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
mov [ebx + SOCKET.TCBState], TCB_TIMED_WAIT
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_LAST_ACK stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
; Look at control flags - expecting an ACK
|
|
|
|
test [edx + 20 + TCP_PACKET.Flags], TH_ACK
|
|
|
|
jz .exit
|
2010-10-01 13:21:55 +04:00
|
|
|
|
2012-03-08 12:33:38 +04:00
|
|
|
; delete the socket
|
|
|
|
stdcall net_socket_free, ebx
|
2010-10-01 13:21:55 +04:00
|
|
|
|
|
|
|
.exit:
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_TIME_WAIT stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
proc stateTCB_CLOSED stdcall, sockAddr:DWORD
|
2012-03-08 12:33:38 +04:00
|
|
|
ret
|
2010-10-01 13:21:55 +04:00
|
|
|
endp
|