2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2015-01-08 21:10:22 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the TCP/IP network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
2014-09-19 21:02:34 +02:00
|
|
|
;; Based on the algorithms used in 4.4BSD ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; ;;
|
|
|
|
;; 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
|
|
|
|
2015-12-16 22:20:53 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; TCP_input: Add a segment to the incoming TCP queue. ;
|
|
|
|
; ;
|
|
|
|
; IN: [esp] = ptr to buffer ;
|
|
|
|
; ebx = ptr to device struct ;
|
|
|
|
; ecx = TCP segment size ;
|
|
|
|
; edx = ptr to IPv4 header ;
|
|
|
|
; esi = ptr to TCP segment ;
|
|
|
|
; edi = interface number*4 ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
tcp_input:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; record the current time
|
2015-03-17 22:50:29 +01:00
|
|
|
push [timer_ticks] ; in 1/100 seconds
|
2015-10-11 20:23:40 +02:00
|
|
|
push ebx ecx esi edx ; mind the order (see TCP_queue_entry struct)
|
2013-05-28 19:34:26 +02:00
|
|
|
mov esi, esp
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
push edi
|
2013-05-28 19:34:26 +02:00
|
|
|
add_to_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .fail
|
2015-10-11 20:23:40 +02:00
|
|
|
pop edi
|
2013-05-28 19:34:26 +02:00
|
|
|
add esp, sizeof.TCP_queue_entry
|
|
|
|
|
2013-06-11 13:11:26 +02:00
|
|
|
inc [TCP_segments_rx + edi]
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
xor edx, edx
|
|
|
|
mov eax, [TCP_input_event]
|
|
|
|
mov ebx, [eax + EVENT.id]
|
|
|
|
xor esi, esi
|
|
|
|
call raise_event
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
2015-10-11 20:23:40 +02:00
|
|
|
pop edi
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP incoming queue is full, discarding packet!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_ptr_to_num4
|
2013-06-11 13:11:26 +02:00
|
|
|
inc [TCP_segments_missed + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 19:43:07 +02:00
|
|
|
add esp, sizeof.TCP_queue_entry - 4
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
proc tcp_process_input
|
2013-12-10 22:08:29 +01:00
|
|
|
|
|
|
|
locals
|
|
|
|
dataoffset dd ?
|
2013-12-12 12:32:03 +01:00
|
|
|
timestamp dd ?
|
2013-12-13 13:20:53 +01:00
|
|
|
temp_bits db ?
|
2013-12-10 22:08:29 +01:00
|
|
|
endl
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
xor esi, esi
|
|
|
|
mov ecx, MANUAL_DESTROY
|
|
|
|
call create_event
|
|
|
|
mov [TCP_input_event], eax
|
|
|
|
|
|
|
|
.wait:
|
|
|
|
mov eax, [TCP_input_event]
|
|
|
|
mov ebx, [eax + EVENT.id]
|
|
|
|
call wait_event
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
get_from_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .wait
|
|
|
|
|
|
|
|
push [esi + TCP_queue_entry.timestamp]
|
2013-12-12 12:32:03 +01:00
|
|
|
pop [timestamp]
|
2013-05-28 19:34:26 +02:00
|
|
|
push [esi + TCP_queue_entry.buffer_ptr]
|
|
|
|
|
|
|
|
mov ebx, [esi + TCP_queue_entry.device_ptr]
|
|
|
|
mov ecx, [esi + TCP_queue_entry.segment_size]
|
2015-10-11 20:23:40 +02:00
|
|
|
mov edi, [esi + TCP_queue_entry.ip_ptr] ; ptr to ipv4 header
|
2013-05-28 19:34:26 +02:00
|
|
|
mov esi, [esi + TCP_queue_entry.segment_ptr] ; change esi last
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: size=%u time=%d\n", ecx, [timer_ticks]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov edx, esi
|
|
|
|
|
|
|
|
cmp ebx, LOOPBACK_DEVICE
|
|
|
|
je .checksum_ok
|
|
|
|
|
|
|
|
; re-calculate the checksum (if not already done by hw)
|
2013-12-19 22:39:14 +01:00
|
|
|
test [ebx + NET_DEVICE.hwacc], NET_HWACC_TCP_IPv4_IN
|
|
|
|
jnz .checksum_ok
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push ecx esi
|
|
|
|
pushw [esi + TCP_header.Checksum]
|
|
|
|
mov [esi + TCP_header.Checksum], 0
|
2015-12-27 16:37:31 +01:00
|
|
|
tcp_checksum (edi+IPv4_header.SourceAddress), (edi+IPv4_header.DestinationAddress)
|
2015-10-11 20:23:40 +02:00
|
|
|
pop cx ; previous checksum
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp cx, dx
|
|
|
|
pop edx ecx
|
|
|
|
jne .drop_no_socket
|
|
|
|
.checksum_ok:
|
|
|
|
|
|
|
|
; Verify the data offset
|
|
|
|
movzx eax, [edx + TCP_header.DataOffset]
|
2013-12-10 22:08:29 +01:00
|
|
|
and al, 0xf0 ; Calculate TCP segment header size (throwing away unused reserved bits in TCP header)
|
|
|
|
shr al, 2
|
|
|
|
cmp al, sizeof.TCP_header ; Now see if it's at least the size of a standard TCP header
|
|
|
|
jb .drop_no_socket ; If not, drop the packet
|
|
|
|
mov [dataoffset], eax
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
sub ecx, eax ; substract TCP header size from total segment size
|
|
|
|
jb .drop_no_socket ; If total segment size is less then the advertised header size, drop packet
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: %u bytes of data\n", ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;-------------------------------------------
|
|
|
|
; Convert Big-endian values to little endian
|
|
|
|
|
|
|
|
ntohd [edx + TCP_header.SequenceNumber]
|
|
|
|
ntohd [edx + TCP_header.AckNumber]
|
|
|
|
|
|
|
|
ntohw [edx + TCP_header.Window]
|
|
|
|
ntohw [edx + TCP_header.UrgentPointer]
|
|
|
|
|
|
|
|
;------------------------
|
|
|
|
; Find the socket pointer
|
|
|
|
|
|
|
|
; IP Packet TCP Destination Port = local Port
|
|
|
|
; (IP Packet SenderAddress = Remote IP) OR (Remote IP = 0)
|
|
|
|
; (IP Packet TCP Source Port = remote Port) OR (remote Port = 0)
|
|
|
|
|
|
|
|
.findpcb:
|
2013-06-12 12:21:41 +02:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ebx, net_sockets
|
|
|
|
mov si, [edx + TCP_header.DestinationPort]
|
|
|
|
|
|
|
|
.socket_loop:
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
or ebx, ebx
|
2013-06-12 12:21:41 +02:00
|
|
|
jz .no_socket ;respond_seg_reset
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp [ebx + SOCKET.Domain], AF_INET4
|
|
|
|
jne .socket_loop
|
|
|
|
|
|
|
|
cmp [ebx + SOCKET.Protocol], IP_PROTO_TCP
|
|
|
|
jne .socket_loop
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.LocalPort], si
|
|
|
|
jne .socket_loop
|
|
|
|
|
|
|
|
mov eax, [ebx + IP_SOCKET.RemoteIP]
|
2015-10-11 20:23:40 +02:00
|
|
|
cmp eax, [edi + IPv4_header.SourceAddress]
|
2013-05-28 19:34:26 +02:00
|
|
|
je @f
|
|
|
|
test eax, eax
|
|
|
|
jnz .socket_loop
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov ax, [ebx + TCP_SOCKET.RemotePort]
|
|
|
|
cmp [edx + TCP_header.SourcePort], ax
|
|
|
|
je .found_socket
|
|
|
|
test ax, ax
|
|
|
|
jnz .socket_loop
|
|
|
|
.found_socket: ; ebx now contains the socketpointer
|
2013-06-12 12:21:41 +02:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: socket ptr=%x state=%u flags=%x\n", ebx, [ebx + TCP_SOCKET.t_state], [edx + TCP_header.Flags]:2
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;----------------------------
|
|
|
|
; Check if socket isnt closed
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
|
|
|
|
je .drop_no_socket
|
|
|
|
|
|
|
|
;----------------
|
|
|
|
; Lock the socket
|
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: socket locked\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;---------------------------
|
|
|
|
; disable all temporary bits
|
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
mov [temp_bits], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;---------------------------------------
|
|
|
|
; unscale the window into a 32 bit value
|
|
|
|
|
|
|
|
movzx eax, [edx + TCP_header.Window]
|
|
|
|
push ecx
|
|
|
|
mov cl, [ebx + TCP_SOCKET.SND_SCALE]
|
|
|
|
shl eax, cl
|
|
|
|
mov dword [edx + TCP_header.Window], eax ; word after window is checksum, we dont need checksum anymore
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
;---------------------------------------
|
|
|
|
; Are we accepting incoming connections?
|
|
|
|
|
|
|
|
test [ebx + SOCKET.options], SO_ACCEPTCON
|
|
|
|
jz .no_accept
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Accepting new connection\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
push ecx edx esi edi
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_fork
|
2013-05-28 19:34:26 +02:00
|
|
|
pop edi esi edx ecx
|
|
|
|
|
|
|
|
test eax, eax
|
|
|
|
jz .drop_no_socket
|
|
|
|
|
|
|
|
mov ebx, eax
|
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
mov [temp_bits], TCP_BIT_DROPSOCKET
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
push [edi + IPv4_header.DestinationAddress]
|
2013-05-28 19:34:26 +02:00
|
|
|
pop [ebx + IP_SOCKET.LocalIP]
|
|
|
|
|
|
|
|
push [edx + TCP_header.DestinationPort]
|
|
|
|
pop [ebx + TCP_SOCKET.LocalPort]
|
|
|
|
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_LISTEN
|
|
|
|
.no_accept:
|
|
|
|
|
|
|
|
|
|
|
|
;-------------------------------------
|
|
|
|
; Reset idle timer and keepalive timer
|
|
|
|
|
|
|
|
mov [ebx + TCP_SOCKET.t_idle], 0
|
|
|
|
mov [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_idle
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;--------------------
|
|
|
|
; Process TCP options
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
;;; FIXME: for LISTEN, options should be called after we determined route, we need it for MSS
|
|
|
|
;;; cmp [ebx + TCP_SOCKET.t_state], TCPS_LISTEN ; no options when in listen state
|
|
|
|
;;; jz .not_uni_xfer ; also no header prediction
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
push ecx
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
mov ecx, [dataoffset]
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ecx, sizeof.TCP_header ; Does header contain any options?
|
|
|
|
je .no_options
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Segment has options\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
add ecx, edx
|
|
|
|
lea esi, [edx + sizeof.TCP_header]
|
|
|
|
|
|
|
|
.opt_loop:
|
|
|
|
cmp esi, ecx ; are we scanning outside of header?
|
|
|
|
jae .no_options
|
|
|
|
lodsb
|
|
|
|
cmp al, TCP_OPT_EOL ; end of option list?
|
|
|
|
je .no_options
|
|
|
|
cmp al, TCP_OPT_NOP
|
|
|
|
je .opt_loop
|
|
|
|
cmp al, TCP_OPT_MAXSEG
|
|
|
|
je .opt_maxseg
|
|
|
|
cmp al, TCP_OPT_WINDOW
|
|
|
|
je .opt_window
|
|
|
|
cmp al, TCP_OPT_SACK_PERMIT
|
|
|
|
je .opt_sack_permit
|
|
|
|
; cmp al, TCP_OPT_SACK
|
|
|
|
; je .opt_sack
|
|
|
|
cmp al, TCP_OPT_TIMESTAMP
|
|
|
|
je .opt_timestamp
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: unknown option:%u\n", al
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .no_options ; If we reach here, some unknown options were received, skip them all!
|
|
|
|
|
|
|
|
.opt_maxseg:
|
|
|
|
lodsb
|
|
|
|
cmp al, 4
|
|
|
|
jne .no_options ; error occured, ignore all options!
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz @f
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
lodsw
|
|
|
|
rol ax, 8
|
2013-12-10 22:08:29 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Maxseg=%u\n", eax
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_mss
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
jmp .opt_loop
|
|
|
|
|
|
|
|
|
|
|
|
.opt_window:
|
|
|
|
lodsb
|
|
|
|
cmp al, 3
|
|
|
|
jne .no_options
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz @f
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Got window scale option\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE
|
|
|
|
|
|
|
|
lodsb
|
|
|
|
mov [ebx + TCP_SOCKET.SND_SCALE], al
|
|
|
|
;;;;; TODO
|
|
|
|
|
|
|
|
@@:
|
|
|
|
jmp .opt_loop
|
|
|
|
|
|
|
|
|
|
|
|
.opt_sack_permit:
|
|
|
|
lodsb
|
|
|
|
cmp al, 2
|
|
|
|
jne .no_options
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz @f
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Selective Acknowledgement permitted\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_SACK_PERMIT
|
|
|
|
|
|
|
|
@@:
|
|
|
|
jmp .opt_loop
|
|
|
|
|
|
|
|
|
|
|
|
.opt_timestamp:
|
|
|
|
lodsb
|
|
|
|
cmp al, 10 ; length must be 10
|
|
|
|
jne .no_options
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Got timestamp option\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz @f
|
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
|
|
|
|
@@:
|
|
|
|
|
|
|
|
lodsd
|
2013-12-12 12:32:03 +01:00
|
|
|
bswap eax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.ts_val], eax
|
|
|
|
lodsd ; timestamp echo reply
|
|
|
|
mov [ebx + TCP_SOCKET.ts_ecr], eax
|
2013-12-13 13:20:53 +01:00
|
|
|
or [temp_bits], TCP_BIT_TIMESTAMP
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Since we have a timestamp, lets do the paws test right away!
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
|
|
|
jnz .no_paws
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.ts_recent]
|
|
|
|
test eax, eax
|
|
|
|
jz .no_paws
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.ts_val]
|
2013-12-12 12:32:03 +01:00
|
|
|
jbe .no_paws
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: PAWS: detected an old segment\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-12 12:32:03 +01:00
|
|
|
mov eax, [timestamp]
|
2013-05-28 19:34:26 +02:00
|
|
|
sub eax, [ebx + TCP_SOCKET.ts_recent_age]
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
cmp eax, TCP_PAWS_IDLE
|
2015-02-21 16:07:30 +01:00
|
|
|
jle .paws_drop
|
2013-05-28 19:34:26 +02:00
|
|
|
push ecx
|
|
|
|
mov [ebx + TCP_SOCKET.ts_recent], 0 ; timestamp was invalid, fix it.
|
|
|
|
.no_paws:
|
|
|
|
jmp .opt_loop
|
|
|
|
|
2015-02-21 16:07:30 +01:00
|
|
|
.paws_drop:
|
|
|
|
inc [TCPS_rcvduppack] ; update stats
|
|
|
|
add [TCPS_rcvdupbyte], ecx
|
|
|
|
inc [TCPS_pawsdrop]
|
|
|
|
jmp .drop_after_ack
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_options:
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------------
|
|
|
|
; Time to do some header prediction (Original Principle by Van Jacobson)
|
|
|
|
|
|
|
|
; There are two common cases for an uni-directional data transfer.
|
|
|
|
;
|
|
|
|
; General rule: the packets has no control flags, is in-sequence,
|
|
|
|
; window width didnt change and we're not retransmitting.
|
|
|
|
;
|
|
|
|
; Second rules:
|
|
|
|
; - If the length is 0 and the ACK moved forward, we're the sender side of the transfer.
|
|
|
|
; In this case we'll free the ACK'ed data and notify higher levels that we have free space in buffer
|
|
|
|
;
|
|
|
|
; - If the length is not 0 and the ACK didn't move, we're the receiver side of the transfer.
|
|
|
|
; If the packets are in order (data queue is empty), add the data to the socket buffer and request a delayed ACK
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
|
|
|
|
jnz .not_uni_xfer
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN + TH_FIN + TH_RST + TH_URG
|
|
|
|
jnz .not_uni_xfer
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz .not_uni_xfer
|
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.RCV_NXT]
|
|
|
|
jne .not_uni_xfer
|
|
|
|
|
|
|
|
mov eax, dword [edx + TCP_header.Window]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_WND]
|
|
|
|
jne .not_uni_xfer
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_MAX]
|
|
|
|
jne .not_uni_xfer
|
|
|
|
|
|
|
|
;---------------------------------------
|
|
|
|
; check if we are sender in the uni-xfer
|
|
|
|
|
|
|
|
; If the following 4 conditions are all true, this segment is a pure ACK.
|
|
|
|
;
|
|
|
|
; - The segment contains no data.
|
|
|
|
test ecx, ecx
|
|
|
|
jnz .not_sender
|
|
|
|
|
|
|
|
; - The congestion window is greater than or equal to the current send window.
|
|
|
|
; This test is true only if the window is fully open, that is, the connection is not in the middle of slow start or congestion avoidance.
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_CWND]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_WND]
|
|
|
|
jb .not_uni_xfer
|
|
|
|
|
|
|
|
; - The acknowledgment field in the segment is less than or equal to the maximum sequence number sent.
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_MAX]
|
|
|
|
ja .not_uni_xfer
|
|
|
|
|
|
|
|
; - The acknowledgment field in the segment is greater than the largest unacknowledged sequence number.
|
|
|
|
sub eax, [ebx + TCP_SOCKET.SND_UNA]
|
|
|
|
jbe .not_uni_xfer
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction: we are sender\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;---------------------------------
|
|
|
|
; Packet is a pure ACK, process it
|
|
|
|
|
|
|
|
; Delete acknowledged bytes from send buffer
|
|
|
|
pusha
|
|
|
|
mov ecx, eax
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.snd]
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_free
|
2013-05-28 19:34:26 +02:00
|
|
|
popa
|
|
|
|
|
|
|
|
; Update RTT estimators
|
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
test [temp_bits], TCP_BIT_TIMESTAMP
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .no_timestamp_rtt
|
2013-12-12 12:32:03 +01:00
|
|
|
mov eax, [timestamp]
|
2013-05-28 19:34:26 +02:00
|
|
|
sub eax, [ebx + TCP_SOCKET.ts_ecr]
|
|
|
|
inc eax
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_xmit_timer
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .rtt_done
|
|
|
|
.no_timestamp_rtt:
|
2015-12-16 22:20:53 +01:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp [ebx + TCP_SOCKET.t_rtt], 0
|
|
|
|
je .rtt_done
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.t_rtseq]
|
|
|
|
jbe .rtt_done
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_rtt]
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_xmit_timer
|
2013-05-28 19:34:26 +02:00
|
|
|
.rtt_done:
|
|
|
|
|
|
|
|
; update window pointers
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
mov [ebx + TCP_SOCKET.SND_UNA], eax
|
|
|
|
|
|
|
|
; Stop retransmit timer
|
2013-06-04 16:12:37 +02:00
|
|
|
and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Unlock the socket
|
|
|
|
pusha
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
|
|
|
; Awaken waiting processes
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_notify
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Generate more output
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_output
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
jmp .drop_no_socket
|
|
|
|
|
|
|
|
;-------------------------------------------------
|
|
|
|
; maybe we are the receiver in the uni-xfer then..
|
|
|
|
|
|
|
|
.not_sender:
|
|
|
|
; - The amount of data in the segment is greater than 0 (data count is in ecx)
|
|
|
|
|
|
|
|
; - The acknowledgment field equals the largest unacknowledged sequence number. This means no data is acknowledged by this segment.
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_UNA]
|
|
|
|
jne .not_uni_xfer
|
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
; - The reassembly list of out-of-order segments for the connection is empty.
|
|
|
|
cmp [ebx + TCP_SOCKET.seg_next], 0
|
|
|
|
jne .not_uni_xfer
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Complete processing of received data
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction: we are receiving %u bytes\n", ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
mov esi, [dataoffset]
|
2013-05-28 19:34:26 +02:00
|
|
|
add esi, edx
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.rcv]
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_write ; Add the data to the socket buffer
|
2013-12-12 12:32:03 +01:00
|
|
|
add [ebx + TCP_SOCKET.RCV_NXT], ecx ; Update sequence number with number of bytes we have copied
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_notify
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_DELACK ; Set delayed ack flag
|
|
|
|
|
|
|
|
jmp .drop
|
|
|
|
|
|
|
|
;--------------------------------------------------
|
|
|
|
; Header prediction failed, do it the slow way
|
|
|
|
|
|
|
|
.not_uni_xfer:
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction failed\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Calculate receive window size
|
|
|
|
push edx
|
2016-04-30 23:15:55 +02:00
|
|
|
mov eax, SOCKET_BUFFER_SIZE
|
2013-05-28 19:34:26 +02:00
|
|
|
sub eax, [ebx + STREAM_SOCKET.rcv.size]
|
2013-12-15 22:27:22 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Space in receive buffer=%d\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov edx, [ebx + TCP_SOCKET.RCV_ADV]
|
|
|
|
sub edx, [ebx + TCP_SOCKET.RCV_NXT]
|
2013-12-15 22:27:22 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Current advertised window=%d\n", edx
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, edx
|
|
|
|
jg @f
|
|
|
|
mov eax, edx
|
|
|
|
@@:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Receive window size=%d\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.RCV_WND], eax
|
|
|
|
pop edx
|
|
|
|
|
|
|
|
; If we are in listen or syn_sent state, go to that specific code right away
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_LISTEN
|
|
|
|
je .LISTEN
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_SYN_SENT
|
|
|
|
je .SYN_SENT
|
|
|
|
|
|
|
|
;----------------------------
|
|
|
|
; trim any data not in window
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
; 1. Check for duplicate data at beginning of segment
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
; Calculate number of bytes we need to drop
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [ebx + TCP_SOCKET.RCV_NXT]
|
|
|
|
sub eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
jle .no_duplicate
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: %u bytes duplicate data!\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz .no_dup_syn
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: got duplicate syn\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
and [edx + TCP_header.Flags], not (TH_SYN)
|
|
|
|
inc [edx + TCP_header.SequenceNumber]
|
|
|
|
|
|
|
|
cmp [edx + TCP_header.UrgentPointer], 1
|
|
|
|
jbe @f
|
|
|
|
dec [edx + TCP_header.UrgentPointer]
|
|
|
|
jmp .dup_syn
|
|
|
|
@@:
|
|
|
|
and [edx + TCP_header.Flags], not (TH_URG)
|
|
|
|
.dup_syn:
|
|
|
|
dec eax
|
|
|
|
.no_dup_syn:
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
; 2. Check for entire duplicate segment
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, ecx ; eax holds number of bytes to drop, ecx is data size
|
|
|
|
jb .duplicate
|
|
|
|
jnz @f
|
|
|
|
test [edx + TCP_header.Flags], TH_FIN
|
|
|
|
jnz .duplicate
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; Any valid FIN must be to the left of the window.
|
|
|
|
; At this point the FIN must be out of sequence or a duplicate, drop it
|
|
|
|
and [edx + TCP_header.Flags], not TH_FIN
|
|
|
|
|
|
|
|
; send an ACK and resynchronize and drop any data.
|
|
|
|
; But keep on processing for RST or ACK
|
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
|
|
|
|
mov eax, ecx
|
2013-12-10 22:08:29 +01:00
|
|
|
|
2015-02-21 16:07:30 +01:00
|
|
|
inc [TCPS_rcvpartduppack]
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
;;; TODO: update stats
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
; Remove duplicate data and update urgent offset
|
|
|
|
|
|
|
|
.duplicate:
|
2013-12-19 22:31:51 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: trimming duplicate data\n"
|
2013-12-10 22:08:29 +01:00
|
|
|
|
|
|
|
; Trim data from left side of window
|
|
|
|
add [dataoffset], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
add [edx + TCP_header.SequenceNumber], eax
|
|
|
|
sub ecx, eax
|
|
|
|
|
|
|
|
sub [edx + TCP_header.UrgentPointer], ax
|
|
|
|
jg @f
|
|
|
|
and [edx + TCP_header.Flags], not (TH_URG)
|
|
|
|
mov [edx + TCP_header.UrgentPointer], 0
|
|
|
|
@@:
|
|
|
|
|
|
|
|
;--------------------------------------------------
|
2013-12-10 22:08:29 +01:00
|
|
|
; Handle data that arrives after process terminates
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.no_duplicate:
|
2013-12-10 22:08:29 +01:00
|
|
|
cmp [ebx + SOCKET.PID], 0 ;;; TODO: use socket flags instead??
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .not_terminated
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
|
|
|
|
jbe .not_terminated
|
|
|
|
test ecx, ecx
|
|
|
|
jz .not_terminated
|
|
|
|
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_close
|
2015-02-21 16:07:30 +01:00
|
|
|
inc [TCPS_rcvafterclose]
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .respond_seg_reset
|
|
|
|
|
|
|
|
;----------------------------------------
|
2013-12-10 22:08:29 +01:00
|
|
|
; Remove data beyond right edge of window
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.not_terminated:
|
|
|
|
mov eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
add eax, ecx
|
|
|
|
sub eax, [ebx + TCP_SOCKET.RCV_NXT]
|
|
|
|
sub eax, [ebx + TCP_SOCKET.RCV_WND] ; eax now holds the number of bytes to drop
|
|
|
|
jle .no_excess_data
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "%d bytes beyond right edge of window\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;;; TODO: update stats
|
|
|
|
cmp eax, ecx
|
|
|
|
jl .dont_drop_all
|
|
|
|
; If a new connection request is received while in TIME_WAIT, drop the old connection and start over,
|
|
|
|
; if the sequence numbers are above the previous ones
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz .no_new_request
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
|
|
|
|
jne .no_new_request
|
|
|
|
; mov edx, [ebx + TCP_SOCKET.RCV_NXT]
|
|
|
|
; cmp edx, [edx + TCP_header.SequenceNumber]
|
|
|
|
; add edx, 64000 ; TCP_ISSINCR FIXME
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_close
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .findpcb ; FIXME: skip code for unscaling window, ...
|
|
|
|
.no_new_request:
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
; If window is closed, we can only take segments at window edge, and have to drop data and PUSH from
|
2013-05-28 19:34:26 +02:00
|
|
|
; incoming segments. Continue processing, but remember to ACK. Otherwise drop segment and ACK
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.RCV_WND], 0
|
|
|
|
jne .drop_after_ack
|
2013-12-10 22:08:29 +01:00
|
|
|
mov esi, [edx + TCP_header.SequenceNumber]
|
|
|
|
cmp esi, [ebx + TCP_SOCKET.RCV_NXT]
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .drop_after_ack
|
|
|
|
|
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
|
|
|
|
;;; TODO: update stats
|
|
|
|
.dont_drop_all:
|
|
|
|
;;; TODO: update stats
|
2013-12-13 13:20:53 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Trimming %u bytes from the right of the window\n"
|
2013-12-10 22:08:29 +01:00
|
|
|
sub ecx, eax ; remove data from the right side of window (decrease data length)
|
2013-12-13 13:20:53 +01:00
|
|
|
and [edx + TCP_header.Flags], not (TH_PUSH or TH_FIN)
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_excess_data:
|
|
|
|
|
|
|
|
;-----------------
|
2013-12-10 22:08:29 +01:00
|
|
|
; Record timestamp
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; If last ACK falls within this segments sequence numbers, record its timestamp
|
2013-12-13 13:20:53 +01:00
|
|
|
test [temp_bits], TCP_BIT_TIMESTAMP
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .no_timestamp
|
|
|
|
mov eax, [ebx + TCP_SOCKET.last_ack_sent]
|
|
|
|
sub eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
jb .no_timestamp
|
|
|
|
test [ebx + TCP_header.Flags], TH_SYN or TH_FIN ; syn and fin occupy one byte
|
|
|
|
jz @f
|
|
|
|
dec eax
|
|
|
|
@@:
|
|
|
|
sub eax, ecx
|
|
|
|
jae .no_timestamp
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Recording timestamp\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-12 12:32:03 +01:00
|
|
|
mov eax, [timestamp]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.ts_recent_age], eax
|
|
|
|
mov eax, [ebx + TCP_SOCKET.ts_val]
|
|
|
|
mov [ebx + TCP_SOCKET.ts_recent], eax
|
|
|
|
.no_timestamp:
|
|
|
|
|
|
|
|
;------------------
|
|
|
|
; Process RST flags
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
|
|
|
jz .no_rst
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Got an RST flag\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_state]
|
|
|
|
shl eax, 2
|
|
|
|
jmp dword [eax + .rst_sw_list]
|
|
|
|
|
|
|
|
.rst_sw_list:
|
|
|
|
dd .no_rst ; TCPS_CLOSED
|
|
|
|
dd .no_rst ; TCPS_LISTEN
|
|
|
|
dd .no_rst ; TCPS_SYN_SENT
|
|
|
|
dd .econnrefused ; TCPS_SYN_RECEIVED
|
|
|
|
dd .econnreset ; TCPS_ESTABLISHED
|
|
|
|
dd .econnreset ; TCPS_CLOSE_WAIT
|
|
|
|
dd .econnreset ; TCPS_FIN_WAIT_1
|
|
|
|
dd .rst_close ; TCPS_CLOSING
|
|
|
|
dd .rst_close ; TCPS_LAST_ACK
|
|
|
|
dd .econnreset ; TCPS_FIN_WAIT_2
|
|
|
|
dd .rst_close ; TCPS_TIMED_WAIT
|
|
|
|
|
|
|
|
.econnrefused:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Connection refused\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov [ebx + SOCKET.errorcode], ECONNREFUSED
|
|
|
|
jmp .close
|
|
|
|
|
|
|
|
.econnreset:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Connection reset\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov [ebx + SOCKET.errorcode], ECONNRESET
|
|
|
|
|
|
|
|
.close:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Closing connection\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
|
|
|
|
;;; TODO: update stats (tcp drops)
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_close
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_no_socket
|
|
|
|
|
|
|
|
.rst_close:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Closing with reset\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_close
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_no_socket
|
|
|
|
|
|
|
|
.no_rst:
|
|
|
|
|
|
|
|
;--------------------------------------
|
|
|
|
; handle SYN-full and ACK-less segments
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz .not_syn_full
|
|
|
|
|
|
|
|
mov eax, ebx
|
|
|
|
mov ebx, ECONNRESET
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_drop
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_with_reset
|
|
|
|
.not_syn_full:
|
|
|
|
|
|
|
|
;---------------
|
|
|
|
; ACK processing
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz .drop
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
|
|
|
|
jb .ack_processed ; states: closed, listen, syn_sent
|
|
|
|
ja .no_syn_rcv ; established, fin_wait_1, fin_wait_2, close_wait, closing, last_ack, time_wait
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_received\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp [ebx + TCP_SOCKET.SND_UNA], eax
|
|
|
|
ja .drop_with_reset
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_MAX]
|
|
|
|
ja .drop_with_reset
|
|
|
|
|
|
|
|
;;; TODO: update stats
|
|
|
|
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_is_connected
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
|
|
|
|
|
|
|
|
; Do window scaling?
|
|
|
|
|
|
|
|
test [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE
|
|
|
|
jz @f
|
|
|
|
test [ebx + TCP_SOCKET.t_flags], TF_REQ_SCALE
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
push word [ebx + TCP_SOCKET.requested_s_scale] ; Set send and receive scale factors to the received values
|
|
|
|
pop word [ebx + TCP_SOCKET.SND_SCALE]
|
|
|
|
@@:
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_reassemble
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
dec eax
|
|
|
|
mov [ebx + TCP_SOCKET.SND_WL1], eax
|
|
|
|
|
|
|
|
.no_syn_rcv:
|
|
|
|
|
|
|
|
;-------------------------
|
|
|
|
; check for duplicate ACKs
|
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_UNA]
|
|
|
|
ja .not_dup_ack
|
|
|
|
|
|
|
|
test ecx, ecx
|
|
|
|
jnz .reset_dupacks
|
|
|
|
|
|
|
|
mov eax, dword [edx + TCP_header.Window]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_WND]
|
|
|
|
jne .reset_dupacks
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Processing duplicate ACK\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; If we have outstanding data, other than a window probe, this is a completely duplicate ACK
|
|
|
|
; (window info didnt change) The ACK is the biggest we've seen and we've seen exactly our rexmt threshold of them,
|
|
|
|
; assume a packet has been dropped and retransmit it. Kludge snd_nxt & the congestion window so we send only this one packet.
|
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
test [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission
|
|
|
|
jz @f
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_UNA]
|
|
|
|
je .dup_ack
|
|
|
|
|
|
|
|
@@:
|
|
|
|
mov [ebx + TCP_SOCKET.t_dupacks], 0
|
|
|
|
jmp .not_dup_ack
|
|
|
|
|
|
|
|
.dup_ack:
|
|
|
|
inc [ebx + TCP_SOCKET.t_dupacks]
|
|
|
|
cmp [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
|
|
|
|
jne .no_re_xmit
|
|
|
|
|
|
|
|
push [ebx + TCP_SOCKET.SND_NXT] ; >>>>
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_WND]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_CWND]
|
2013-11-26 18:00:13 +01:00
|
|
|
jbe @f
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_CWND]
|
|
|
|
@@:
|
2013-05-28 19:34:26 +02:00
|
|
|
shr eax, 1
|
|
|
|
push edx
|
|
|
|
xor edx, edx
|
|
|
|
div [ebx + TCP_SOCKET.t_maxseg]
|
|
|
|
cmp eax, 2
|
|
|
|
ja @f
|
|
|
|
xor eax, eax
|
|
|
|
mov al, 2
|
|
|
|
@@:
|
|
|
|
mul [ebx + TCP_SOCKET.t_maxseg]
|
|
|
|
pop edx
|
|
|
|
mov [ebx + TCP_SOCKET.SND_SSTHRESH], eax
|
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission ; turn off retransmission timer
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.t_rtt], 0
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
mov [ebx + TCP_SOCKET.SND_NXT], eax
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_maxseg]
|
|
|
|
mov [ebx + TCP_SOCKET.SND_CWND], eax
|
|
|
|
|
|
|
|
; Unlock the socket
|
|
|
|
push ebx
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
|
|
|
|
; retransmit missing segment
|
|
|
|
mov eax, [esp]
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_output
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Lock the socket again
|
|
|
|
mov ecx, [esp]
|
|
|
|
add ecx, SOCKET.mutex
|
|
|
|
call mutex_lock
|
2013-06-27 01:35:43 +02:00
|
|
|
pop ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Continue processing
|
|
|
|
xor edx, edx
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_maxseg]
|
|
|
|
mul [ebx + TCP_SOCKET.t_dupacks]
|
|
|
|
add eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
|
|
|
|
mov [ebx + TCP_SOCKET.SND_CWND], eax
|
|
|
|
|
|
|
|
pop eax ; <<<<
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_NXT]
|
|
|
|
jb @f
|
|
|
|
mov [ebx + TCP_SOCKET.SND_NXT], eax
|
|
|
|
@@:
|
|
|
|
|
|
|
|
jmp .drop
|
|
|
|
|
|
|
|
|
|
|
|
.no_re_xmit:
|
|
|
|
jbe .not_dup_ack
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Increasing congestion window\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_maxseg]
|
|
|
|
add [ebx + TCP_SOCKET.SND_CWND], eax
|
|
|
|
|
|
|
|
; Unlock the socket
|
|
|
|
push ebx
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
|
|
|
|
; retransmit missing segment
|
|
|
|
mov eax, [esp]
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_output
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Lock the socket again
|
|
|
|
mov ecx, [esp]
|
|
|
|
add ecx, SOCKET.mutex
|
|
|
|
call mutex_lock
|
|
|
|
pop ebx
|
|
|
|
|
|
|
|
jmp .drop
|
|
|
|
|
|
|
|
|
|
|
|
.not_dup_ack:
|
|
|
|
|
|
|
|
;-------------------------------------------------
|
|
|
|
; If the congestion window was inflated to account
|
|
|
|
; for the other side's cached packets, retract it
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_CWND]
|
|
|
|
ja @f
|
|
|
|
cmp [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
|
|
|
|
jbe @f
|
|
|
|
mov [ebx + TCP_SOCKET.SND_CWND], eax
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov [ebx + TCP_SOCKET.t_dupacks], 0
|
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_MAX]
|
|
|
|
jbe @f
|
2015-02-21 16:07:30 +01:00
|
|
|
inc [TCPS_rcvacktoomuch]
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_after_ack
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov edi, [edx + TCP_header.AckNumber]
|
|
|
|
sub edi, [ebx + TCP_SOCKET.SND_UNA] ; now we got the number of acked bytes in edi
|
2015-02-21 16:07:30 +01:00
|
|
|
inc [TCPS_rcvackpack]
|
|
|
|
add [TCPS_rcvackbyte], edi
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: acceptable ACK for %u bytes\n", edi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;------------------------------------------
|
2013-12-13 13:20:53 +01:00
|
|
|
; RTT measurements and retransmission timer
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; If we have a timestamp, update smoothed RTT
|
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
test [temp_bits], TCP_BIT_TIMESTAMP
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .timestamp_not_present
|
2013-12-12 12:32:03 +01:00
|
|
|
mov eax, [timestamp]
|
2013-05-28 19:34:26 +02:00
|
|
|
sub eax, [ebx + TCP_SOCKET.ts_ecr]
|
|
|
|
inc eax
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_xmit_timer
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .rtt_done_
|
|
|
|
|
|
|
|
; If no timestamp but transmit timer is running and timed sequence number was acked,
|
|
|
|
; update smoothed RTT. Since we now have an RTT measurement, cancel the timer backoff
|
|
|
|
; (Phil Karn's retransmit algo)
|
|
|
|
; Recompute the initial retransmit timer
|
|
|
|
|
|
|
|
.timestamp_not_present:
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.t_rtseq]
|
|
|
|
jbe .rtt_done_
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_rtt]
|
|
|
|
test eax, eax
|
|
|
|
jz .rtt_done_
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_xmit_timer
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.rtt_done_:
|
|
|
|
|
|
|
|
; If all outstanding data is acked, stop retransmit timer and remember to restart (more output or persist)
|
|
|
|
; If there is more data to be acked, restart retransmit timer, using current (possible backed-off) value.
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_MAX]
|
|
|
|
cmp eax, [edx + TCP_header.AckNumber]
|
|
|
|
jne .more_data
|
2013-06-04 16:12:37 +02:00
|
|
|
and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission
|
2013-12-13 13:20:53 +01:00
|
|
|
or [temp_bits], TCP_BIT_NEEDOUTPUT
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .no_restart
|
|
|
|
.more_data:
|
2013-06-04 16:12:37 +02:00
|
|
|
test [ebx + TCP_SOCKET.timer_flags], timer_flag_persist
|
|
|
|
jnz .no_restart
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_rxtcur]
|
|
|
|
mov [ebx + TCP_SOCKET.timer_retransmission], eax
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_restart:
|
|
|
|
|
|
|
|
|
|
|
|
;-------------------------------------------
|
|
|
|
; Open congestion window in response to ACKs
|
|
|
|
|
|
|
|
mov esi, [ebx + TCP_SOCKET.SND_CWND]
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_maxseg]
|
|
|
|
|
|
|
|
cmp esi, [ebx + TCP_SOCKET.SND_SSTHRESH]
|
|
|
|
jbe @f
|
|
|
|
push edx
|
|
|
|
push eax
|
|
|
|
mul eax
|
|
|
|
div esi
|
|
|
|
pop edx
|
|
|
|
shr edx, 3
|
|
|
|
add eax, edx
|
|
|
|
pop edx
|
|
|
|
@@:
|
|
|
|
|
|
|
|
add esi, eax
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
mov cl, [ebx + TCP_SOCKET.SND_SCALE]
|
|
|
|
mov eax, TCP_max_win
|
|
|
|
shl eax, cl
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
cmp esi, eax
|
2013-11-26 18:00:13 +01:00
|
|
|
jbe @f
|
|
|
|
mov esi, eax
|
|
|
|
@@:
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.SND_CWND], esi
|
|
|
|
|
|
|
|
;------------------------------------------
|
|
|
|
; Remove acknowledged data from send buffer
|
|
|
|
|
|
|
|
cmp edi, [ebx + STREAM_SOCKET.snd.size]
|
|
|
|
jbe .finiacked
|
|
|
|
|
|
|
|
push ecx edx ebx
|
|
|
|
mov ecx, [ebx + STREAM_SOCKET.snd.size]
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.snd]
|
|
|
|
sub [ebx + TCP_SOCKET.SND_WND], ecx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_free
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ebx edx ecx
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is acked\n"
|
2014-09-19 21:02:34 +02:00
|
|
|
or [temp_bits], TCP_BIT_FIN_IS_ACKED
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .wakeup
|
|
|
|
|
|
|
|
.finiacked:
|
|
|
|
|
|
|
|
push ecx edx ebx
|
|
|
|
mov ecx, edi
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.snd]
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_free
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ebx
|
|
|
|
sub [ebx + TCP_SOCKET.SND_WND], ecx
|
|
|
|
pop edx ecx
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is not acked\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;----------------------------------------
|
|
|
|
; Wake up process waiting on send buffer
|
|
|
|
|
|
|
|
.wakeup:
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_notify
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Update TCPS
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
mov [ebx + TCP_SOCKET.SND_UNA], eax
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_NXT]
|
|
|
|
jb @f
|
|
|
|
mov [ebx + TCP_SOCKET.SND_NXT], eax
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; General ACK handling complete
|
|
|
|
; Now do the state-specific ones
|
|
|
|
; Carry flag is set when our FIN is acked
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_state]
|
|
|
|
jmp dword [eax*4 + .ACK_sw_list]
|
|
|
|
|
|
|
|
.ACK_sw_list:
|
|
|
|
dd .ack_processed ; TCPS_CLOSED
|
|
|
|
dd .ack_processed ; TCPS_LISTEN
|
|
|
|
dd .ack_processed ; TCPS_SYN_SENT
|
|
|
|
dd .ack_processed ; TCPS_SYN_RECEIVED
|
|
|
|
dd .ack_processed ; TCPS_ESTABLISHED
|
|
|
|
dd .ack_processed ; TCPS_CLOSE_WAIT
|
|
|
|
dd .ack_fw1 ; TCPS_FIN_WAIT_1
|
|
|
|
dd .ack_c ; TCPS_CLOSING
|
|
|
|
dd .ack_la ; TCPS_LAST_ACK
|
|
|
|
dd .ack_processed ; TCPS_FIN_WAIT_2
|
|
|
|
dd .ack_tw ; TCPS_TIMED_WAIT
|
|
|
|
|
|
|
|
|
|
|
|
.ack_fw1:
|
2014-09-19 21:02:34 +02:00
|
|
|
test [temp_bits], TCP_BIT_FIN_IS_ACKED
|
|
|
|
jz .ack_processed
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [ebx + SOCKET.state], SS_CANTRCVMORE
|
|
|
|
jnz @f
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_is_disconnected
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
|
|
|
|
jmp .ack_processed
|
|
|
|
|
|
|
|
.ack_c:
|
2014-09-19 21:02:34 +02:00
|
|
|
test [temp_bits], TCP_BIT_FIN_IS_ACKED
|
|
|
|
jz .ack_processed
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_cancel_timers
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_is_disconnected
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .ack_processed
|
|
|
|
|
|
|
|
.ack_la:
|
2014-09-19 21:02:34 +02:00
|
|
|
test [temp_bits], TCP_BIT_FIN_IS_ACKED
|
|
|
|
jz .ack_processed
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-13 10:22:37 +02:00
|
|
|
push ebx
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
pop ebx
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_close
|
2013-12-15 22:27:22 +01:00
|
|
|
jmp .drop_no_socket
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.ack_tw:
|
|
|
|
mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_after_ack
|
|
|
|
|
|
|
|
.reset_dupacks: ; We got a new ACK, reset duplicate ACK counter
|
|
|
|
mov [ebx + TCP_SOCKET.t_dupacks], 0
|
|
|
|
jmp .ack_processed
|
|
|
|
|
|
|
|
;-------
|
|
|
|
; LISTEN
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.LISTEN:
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: state=listen\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
|
|
|
jnz .drop
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jnz .drop_with_reset
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz .drop
|
|
|
|
|
2015-02-21 16:07:30 +01:00
|
|
|
inc [TCPS_accepts] ; update stats
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;;; TODO: check if it's a broadcast or multicast, and drop if so
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
push [edi + IPv4_header.SourceAddress]
|
2013-05-28 19:34:26 +02:00
|
|
|
pop [ebx + IP_SOCKET.RemoteIP]
|
|
|
|
|
|
|
|
push [edx + TCP_header.SourcePort]
|
|
|
|
pop [ebx + TCP_SOCKET.RemotePort]
|
|
|
|
|
|
|
|
push [edx + TCP_header.SequenceNumber]
|
|
|
|
pop [ebx + TCP_SOCKET.IRS]
|
|
|
|
|
|
|
|
mov eax, [TCP_sequence_num]
|
|
|
|
add [TCP_sequence_num], 64000 / 2
|
|
|
|
mov [ebx + TCP_SOCKET.ISS], eax
|
|
|
|
mov [ebx + TCP_SOCKET.SND_NXT], eax
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
tcp_sendseqinit ebx
|
|
|
|
tcp_rcvseqinit ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
|
2013-12-13 13:20:53 +01:00
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval ;;;; macro
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.snd]
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_create
|
2014-11-01 19:27:28 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .drop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.rcv]
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_create
|
2014-11-01 19:27:28 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .drop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
and [temp_bits], not TCP_BIT_DROPSOCKET
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-17 20:17:17 +02:00
|
|
|
pusha
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_notify
|
2013-06-17 20:17:17 +02:00
|
|
|
popa
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
jmp .trim
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;------------
|
|
|
|
; Active Open
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.SYN_SENT:
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_sent\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.ISS]
|
|
|
|
jbe .drop_with_reset
|
|
|
|
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_MAX]
|
|
|
|
ja .drop_with_reset
|
|
|
|
@@:
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz .drop
|
|
|
|
|
|
|
|
mov eax, ebx
|
|
|
|
mov ebx, ECONNREFUSED
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_drop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
jmp .drop
|
|
|
|
@@:
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jz .drop
|
|
|
|
|
|
|
|
; at this point, segment seems to be valid
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz .no_syn_ack
|
|
|
|
|
|
|
|
; now, process received SYN in response to an active open
|
|
|
|
|
|
|
|
mov eax, [edx + TCP_header.AckNumber]
|
|
|
|
mov [ebx + TCP_SOCKET.SND_UNA], eax
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_NXT]
|
|
|
|
jbe @f
|
|
|
|
mov [ebx + TCP_SOCKET.SND_NXT], eax
|
|
|
|
@@:
|
|
|
|
|
|
|
|
.no_syn_ack:
|
2013-06-04 16:12:37 +02:00
|
|
|
and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission ; disable retransmission timer
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push [edx + TCP_header.SequenceNumber]
|
|
|
|
pop [ebx + TCP_SOCKET.IRS]
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
tcp_rcvseqinit ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_UNA]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.ISS]
|
|
|
|
jbe .simultaneous_open
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz .simultaneous_open
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: active open\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;;; TODO: update stats
|
|
|
|
|
|
|
|
; set socket state to connected
|
2013-06-17 20:17:17 +02:00
|
|
|
push eax
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_is_connected
|
2013-06-17 20:17:17 +02:00
|
|
|
pop eax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
|
|
|
|
|
|
|
|
; Do window scaling on this connection ?
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_flags]
|
|
|
|
and eax, TF_REQ_SCALE or TF_RCVD_SCALE
|
|
|
|
cmp eax, TF_REQ_SCALE or TF_RCVD_SCALE
|
|
|
|
jne .no_scaling
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
mov ax, word[ebx + TCP_SOCKET.requested_s_scale]
|
|
|
|
mov word[ebx + TCP_SOCKET.SND_SCALE], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_scaling:
|
|
|
|
|
|
|
|
;;; TODO: reassemble packets queue
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_rtt]
|
|
|
|
test eax, eax
|
2014-09-19 21:02:34 +02:00
|
|
|
je .trim
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_xmit_timer
|
2014-09-19 21:02:34 +02:00
|
|
|
jmp .trim
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.simultaneous_open:
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: simultaneous open\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
; We have received a syn but no ACK, so we are having a simultaneous open..
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
|
|
|
|
|
|
|
|
;-------------------------------------
|
|
|
|
; Common processing for receipt of SYN
|
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
.trim:
|
2013-05-28 19:34:26 +02:00
|
|
|
inc [edx + TCP_header.SequenceNumber]
|
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
; Drop any received data that doesnt fit in the receive window.
|
|
|
|
cmp ecx, [ebx + TCP_SOCKET.RCV_WND]
|
|
|
|
jbe .dont_trim
|
|
|
|
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: received data does not fit in window, trimming %u bytes\n", eax
|
|
|
|
mov ecx, [ebx + TCP_SOCKET.RCV_WND]
|
|
|
|
and [edx + TCP_header.Flags], not (TH_FIN)
|
|
|
|
;;; TODO: update stats
|
|
|
|
|
|
|
|
.dont_trim:
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
mov [ebx + TCP_SOCKET.RCV_UP], eax
|
|
|
|
dec eax
|
|
|
|
mov [ebx + TCP_SOCKET.SND_WL1], eax
|
|
|
|
|
|
|
|
.ack_processed:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: ACK processed\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;----------------------------------------------
|
|
|
|
; check if we need to update window information
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jz .no_window_update
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_WL1]
|
|
|
|
cmp eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
jb .update_window
|
|
|
|
ja @f
|
|
|
|
|
|
|
|
mov eax, [ebx + TCP_SOCKET.SND_WL2]
|
|
|
|
cmp eax, [edx + TCP_header.AckNumber]
|
|
|
|
jb .update_window
|
|
|
|
ja .no_window_update
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov eax, dword [edx + TCP_header.Window]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.SND_WND]
|
|
|
|
jbe .no_window_update
|
|
|
|
|
|
|
|
.update_window:
|
|
|
|
|
|
|
|
;;; TODO: update stats (Keep track of pure window updates)
|
|
|
|
|
|
|
|
mov eax, dword [edx + TCP_header.Window]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.max_sndwnd]
|
|
|
|
jbe @f
|
|
|
|
mov [ebx + TCP_SOCKET.max_sndwnd], eax
|
|
|
|
@@:
|
|
|
|
mov [ebx + TCP_SOCKET.SND_WND], eax
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Updating window to %u\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push [edx + TCP_header.SequenceNumber]
|
|
|
|
pop [ebx + TCP_SOCKET.SND_WL1]
|
|
|
|
|
|
|
|
push [edx + TCP_header.AckNumber]
|
|
|
|
pop [ebx + TCP_SOCKET.SND_WL2]
|
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
or [temp_bits], TCP_BIT_NEEDOUTPUT
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.no_window_update:
|
|
|
|
|
|
|
|
;-----------------
|
|
|
|
; process URG flag
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_URG
|
|
|
|
jz .not_urgent
|
|
|
|
|
|
|
|
cmp [edx + TCP_header.UrgentPointer], 0
|
|
|
|
jz .not_urgent
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
|
|
|
|
je .not_urgent
|
|
|
|
|
|
|
|
; Ignore bogus urgent offsets
|
|
|
|
|
|
|
|
movzx eax, [edx + TCP_header.UrgentPointer]
|
|
|
|
add eax, [ebx + STREAM_SOCKET.rcv.size]
|
2016-04-30 23:15:55 +02:00
|
|
|
cmp eax, SOCKET_BUFFER_SIZE
|
2013-05-28 19:34:26 +02:00
|
|
|
jbe .not_urgent
|
|
|
|
|
|
|
|
mov [edx + TCP_header.UrgentPointer], 0
|
|
|
|
and [edx + TCP_header.Flags], not (TH_URG)
|
|
|
|
jmp .do_data
|
|
|
|
|
|
|
|
.not_urgent:
|
|
|
|
|
|
|
|
; processing of received urgent pointer
|
|
|
|
|
|
|
|
;;; TODO (1051-1093)
|
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------
|
|
|
|
; process the data in the segment (1094)
|
|
|
|
|
|
|
|
.do_data:
|
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
|
|
|
|
jae .final_processing
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_FIN
|
|
|
|
jnz @f
|
|
|
|
|
|
|
|
test ecx, ecx
|
2013-07-04 12:15:03 +02:00
|
|
|
jz .final_processing
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
|
|
|
|
; The segment is in order?
|
|
|
|
mov eax, [edx + TCP_header.SequenceNumber]
|
|
|
|
cmp eax, [ebx + TCP_SOCKET.RCV_NXT]
|
|
|
|
jne .out_of_order
|
|
|
|
|
|
|
|
; The reassembly queue is empty?
|
|
|
|
cmp [ebx + TCP_SOCKET.seg_next], 0
|
|
|
|
jne .out_of_order
|
|
|
|
|
|
|
|
; The connection is established?
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
|
|
|
|
jne .out_of_order
|
|
|
|
|
|
|
|
; Ok, lets do this.. Set delayed ACK flag and copy data into socket buffer
|
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_DELACK
|
|
|
|
|
|
|
|
pusha
|
2013-12-10 22:08:29 +01:00
|
|
|
mov esi, [dataoffset]
|
2013-05-28 19:34:26 +02:00
|
|
|
add esi, edx
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.rcv]
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_ring_write ; Add the data to the socket buffer
|
2013-05-28 19:34:26 +02:00
|
|
|
add [ebx + TCP_SOCKET.RCV_NXT], ecx ; Update sequence number with number of bytes we have copied
|
|
|
|
popa
|
|
|
|
|
|
|
|
; Wake up the sleeping process
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_notify
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
jmp .data_done
|
|
|
|
|
|
|
|
.out_of_order:
|
2013-12-10 22:08:29 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP data is out of order!\nSequencenumber is %u, we expected %u.\n", \
|
|
|
|
[edx + TCP_header.SequenceNumber], [ebx + TCP_SOCKET.RCV_NXT]
|
2013-08-07 15:13:29 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; Uh-oh, some data is out of order, lets call TCP reassemble for help
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_reassemble
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-10 22:08:29 +01:00
|
|
|
; Generate ACK immediately, to let the other end know that a segment was received out of order,
|
|
|
|
; and to tell it what sequence number is expected. This aids the fast-retransmit algorithm.
|
2013-05-28 19:34:26 +02:00
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
|
|
|
|
.data_done:
|
|
|
|
|
|
|
|
;---------------
|
|
|
|
; FIN processing
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_FIN
|
|
|
|
jz .final_processing
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Processing FIN\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
|
|
|
|
jae .not_first_fin
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: First FIN for this connection\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_cant_recv_more
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-12-13 13:20:53 +01:00
|
|
|
or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
|
2013-05-28 19:34:26 +02:00
|
|
|
inc [ebx + TCP_SOCKET.RCV_NXT]
|
|
|
|
|
|
|
|
.not_first_fin:
|
|
|
|
mov eax, [ebx + TCP_SOCKET.t_state]
|
|
|
|
shl eax, 2
|
|
|
|
jmp dword [eax + .FIN_sw_list]
|
|
|
|
|
|
|
|
.FIN_sw_list:
|
|
|
|
dd .final_processing ; TCPS_CLOSED
|
|
|
|
dd .final_processing ; TCPS_LISTEN
|
|
|
|
dd .final_processing ; TCPS_SYN_SENT
|
|
|
|
dd .fin_syn_est ; TCPS_SYN_RECEIVED
|
|
|
|
dd .fin_syn_est ; TCPS_ESTABLISHED
|
|
|
|
dd .final_processing ; TCPS_CLOSE_WAIT
|
|
|
|
dd .fin_wait1 ; TCPS_FIN_WAIT_1
|
|
|
|
dd .final_processing ; TCPS_CLOSING
|
|
|
|
dd .final_processing ; TCPS_LAST_ACK
|
|
|
|
dd .fin_wait2 ; TCPS_FIN_WAIT_2
|
|
|
|
dd .fin_timed ; TCPS_TIMED_WAIT
|
|
|
|
|
|
|
|
.fin_syn_est:
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
|
|
|
|
jmp .final_processing
|
|
|
|
|
|
|
|
.fin_wait1:
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSING
|
|
|
|
jmp .final_processing
|
|
|
|
|
|
|
|
.fin_wait2:
|
|
|
|
mov [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_cancel_timers
|
|
|
|
call socket_is_disconnected
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.fin_timed:
|
|
|
|
mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
|
2013-06-04 16:12:37 +02:00
|
|
|
or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
|
2013-12-13 13:20:53 +01:00
|
|
|
|
|
|
|
;-----------------
|
|
|
|
; Final processing
|
|
|
|
|
|
|
|
.final_processing:
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Final processing\n"
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
pop eax
|
|
|
|
|
|
|
|
test [temp_bits], TCP_BIT_NEEDOUTPUT
|
|
|
|
jnz .need_output
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_ACKNOW
|
2014-09-19 21:02:34 +02:00
|
|
|
jz .done
|
2013-12-13 13:20:53 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: ACK now!\n"
|
|
|
|
|
|
|
|
.need_output:
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: need output\n"
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_output
|
2013-12-13 13:20:53 +01:00
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
.done:
|
2013-12-13 13:20:53 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: dumping\n"
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2013-12-13 13:20:53 +01:00
|
|
|
jmp .loop
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------
|
|
|
|
; Drop the segment
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
.drop_after_ack:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Drop after ACK\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push edx ebx
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
pop eax edx
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
2014-09-19 21:02:34 +02:00
|
|
|
jnz .done
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
or [eax + TCP_SOCKET.t_flags], TF_ACKNOW
|
|
|
|
jmp .need_output
|
|
|
|
|
|
|
|
.drop_with_reset:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Drop with reset\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push ebx edx
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
pop edx ebx
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
2014-09-19 21:02:34 +02:00
|
|
|
jnz .done
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
; TODO: if its a multicast/broadcast, also drop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jnz .respond_ack
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jnz .respond_syn
|
2014-09-19 21:02:34 +02:00
|
|
|
jmp .done
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;---------
|
|
|
|
; Respond
|
|
|
|
|
|
|
|
.respond_ack:
|
|
|
|
push ebx
|
|
|
|
mov cl, TH_RST
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_respond
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ebx
|
|
|
|
jmp .destroy_new_socket
|
|
|
|
|
|
|
|
.respond_syn:
|
|
|
|
push ebx
|
|
|
|
mov cl, TH_RST + TH_ACK
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_respond
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ebx
|
|
|
|
jmp .destroy_new_socket
|
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
;-----------------------------------------
|
|
|
|
; The connection has no associated socket
|
2013-06-12 12:21:41 +02:00
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
.no_socket:
|
2013-06-12 12:21:41 +02:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.respond_seg_reset:
|
|
|
|
test [edx + TCP_header.Flags], TH_RST
|
|
|
|
jnz .drop_no_socket
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
; TODO: if its a multicast/broadcast, also drop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_ACK
|
|
|
|
jnz .respond_seg_ack
|
|
|
|
|
|
|
|
test [edx + TCP_header.Flags], TH_SYN
|
|
|
|
jnz .respond_seg_syn
|
|
|
|
|
|
|
|
jmp .drop_no_socket
|
|
|
|
|
|
|
|
.respond_seg_ack:
|
|
|
|
mov cl, TH_RST
|
2015-10-11 20:23:40 +02:00
|
|
|
xor ebx, ebx ; FIXME: find a way to get the receiving device ptr
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_respond_segment
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_no_socket
|
|
|
|
|
|
|
|
.respond_seg_syn:
|
|
|
|
mov cl, TH_RST + TH_ACK
|
2015-10-11 20:23:40 +02:00
|
|
|
xor ebx, ebx ; FIXME: find a way to get the receiving device ptr
|
2015-12-27 16:37:31 +01:00
|
|
|
call tcp_respond_segment
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .drop_no_socket
|
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
;------------------------------------------------
|
|
|
|
; Unlock socket mutex and prepare to drop segment
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.drop:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Dropping segment\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
;--------------------------------------------
|
|
|
|
; Destroy the newly created socket if needed
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.destroy_new_socket:
|
2013-12-13 13:20:53 +01:00
|
|
|
test [temp_bits], TCP_BIT_DROPSOCKET
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .drop_no_socket
|
|
|
|
|
|
|
|
mov eax, ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
call socket_free
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-09-19 21:02:34 +02:00
|
|
|
;------------------
|
|
|
|
; Drop the segment
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.drop_no_socket:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Drop (no socket)\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .loop
|
2013-12-10 22:08:29 +01:00
|
|
|
|
|
|
|
endp
|