2013-05-28 21:34:26 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2024-05-22 18:15:14 +03:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
|
2013-05-28 21:34:26 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the TCP/IP network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; Based on the code of 4.4BSD ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
TCP_BIT_SENDALOT = 1 shl 0
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; tcp_output ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket pointer ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success/errorcode ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
proc tcp_output
|
2013-12-13 16:20:53 +04:00
|
|
|
|
|
|
|
locals
|
|
|
|
temp_bits db ?
|
2020-06-08 22:57:03 +03:00
|
|
|
rcv_window dd ?
|
2013-12-13 16:20:53 +04:00
|
|
|
endl
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-12-16 01:27:22 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: socket=%x state=%u\n", eax, [eax + TCP_SOCKET.t_state]
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push eax
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
pop eax
|
|
|
|
|
2013-06-13 12:22:37 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: socket locked\n"
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; We'll detect the length of the data to be transmitted, and flags to be used
|
|
|
|
; If there is some data, or any critical controls to send (SYN / RST), then transmit
|
|
|
|
; Otherwise, investigate further
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_MAX]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
jbe .not_idle
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.t_idle]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.t_rxtcur]
|
|
|
|
jbe .not_idle
|
|
|
|
|
|
|
|
; We have been idle for a while and no ACKS are expected to clock out any data we send..
|
|
|
|
; Slow start to get ack "clock" running again.
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
mov [eax + TCP_SOCKET.SND_CWND], ebx
|
|
|
|
|
|
|
|
.not_idle:
|
|
|
|
.again:
|
2013-12-13 16:20:53 +04:00
|
|
|
mov [temp_bits], 0
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
; Calculate offset
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
sub ebx, [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
|
|
|
|
; Determine window
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
mov ecx, [eax + TCP_SOCKET.SND_WND]
|
|
|
|
cmp ecx, [eax + TCP_SOCKET.SND_CWND]
|
|
|
|
jb @f
|
|
|
|
mov ecx, [eax + TCP_SOCKET.SND_CWND]
|
|
|
|
@@:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
; get flags in dl
|
|
|
|
|
|
|
|
call tcp_outflags
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;------------------------
|
|
|
|
; data being forced out ?
|
|
|
|
|
|
|
|
; If in persist timeout with window of 0, send 1 byte.
|
|
|
|
; Otherwise, if window is small but nonzero, and timer expired,
|
|
|
|
; we will send what we can and go to transmit state
|
|
|
|
|
2017-05-31 20:54:16 +03:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_FORCE
|
|
|
|
jz .no_force
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: forcing data out\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test ecx, ecx
|
|
|
|
jnz .no_zero_window
|
|
|
|
|
|
|
|
cmp ebx, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
jae @f
|
|
|
|
and dl, not (TH_FIN)
|
|
|
|
@@:
|
2015-02-21 18:07:30 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
inc ecx
|
|
|
|
jmp .no_force
|
|
|
|
|
|
|
|
.no_zero_window:
|
2013-06-05 04:53:57 +04:00
|
|
|
and [eax + TCP_SOCKET.timer_flags], not timer_flag_persist
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + TCP_SOCKET.t_rxtshift], 0
|
|
|
|
|
|
|
|
.no_force:
|
|
|
|
|
|
|
|
;--------------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Calculate how much data to send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov esi, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
cmp esi, ecx
|
|
|
|
jb @f
|
|
|
|
mov esi, ecx
|
|
|
|
@@:
|
|
|
|
sub esi, ebx
|
|
|
|
|
|
|
|
;------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; check for window shrink
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
; If FIN has been sent, but not ACKed, but we havent been called to retransmit, esi will be -1
|
2013-05-28 21:34:26 +04:00
|
|
|
; Otherwise, window shrank after we sent into it.
|
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
jge .not_persist
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; enter persist state
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
xor esi, esi
|
|
|
|
|
|
|
|
; If window shrank to 0
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
test ecx, ecx
|
|
|
|
jnz @f
|
|
|
|
|
|
|
|
; cancel pending retransmit
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-06-05 04:53:57 +04:00
|
|
|
and [eax + TCP_SOCKET.timer_flags], not timer_flag_retransmission
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; pull SND_NXT back to (closed) window, We will enter persist state below.
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
pop [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; If window didn't close completely, just wait for an ACK
|
|
|
|
|
|
|
|
.not_persist:
|
|
|
|
|
|
|
|
;---------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Send one segment at a time
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp esi, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
jbe @f
|
|
|
|
mov esi, [eax + TCP_SOCKET.t_maxseg]
|
2013-12-13 16:20:53 +04:00
|
|
|
or [temp_bits], TCP_BIT_SENDALOT
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
|
|
|
;--------------------------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Turn of FIN flag if send buffer not emptied
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov edi, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
add edi, esi
|
|
|
|
sub edi, [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
cmp edi, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
jae @f
|
|
|
|
and dl, not (TH_FIN)
|
|
|
|
@@:
|
|
|
|
|
|
|
|
;-------------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; calculate window advertisement
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
xor ecx, ecx
|
|
|
|
test [eax + SOCKET.state], SS_CANTRCVMORE
|
|
|
|
jnz @f
|
2016-05-01 00:15:55 +03:00
|
|
|
mov ecx, SOCKET_BUFFER_SIZE
|
2013-05-28 21:34:26 +04:00
|
|
|
sub ecx, [eax + STREAM_SOCKET.rcv.size]
|
2020-06-08 22:57:03 +03:00
|
|
|
@@:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;------------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Sender silly window avoidance
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test esi, esi
|
|
|
|
jz .len_zero
|
|
|
|
|
|
|
|
cmp esi, [eax + TCP_SOCKET.t_maxseg]
|
2013-12-20 01:31:51 +04:00
|
|
|
je .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
add ebx, esi ; offset + length
|
|
|
|
cmp ebx, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
jb @f
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_NODELAY
|
2013-12-20 01:31:51 +04:00
|
|
|
jnz .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_MAX]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
2013-12-20 01:31:51 +04:00
|
|
|
je .send
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
2017-05-31 20:54:16 +03:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_FORCE
|
2013-12-20 01:31:51 +04:00
|
|
|
jnz .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.max_sndwnd]
|
|
|
|
shr ebx, 1
|
|
|
|
cmp esi, ebx
|
2013-12-20 01:31:51 +04:00
|
|
|
jae .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_MAX]
|
2013-12-20 01:31:51 +04:00
|
|
|
jb .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.len_zero:
|
|
|
|
|
|
|
|
;----------------------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Check if a window update should be sent
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: window=%d\n", ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; Compare available window to amount of window known to peer (as advertised window less next expected input)
|
|
|
|
; If the difference is at least two max size segments, or at least 50% of the maximum possible window,
|
|
|
|
; Then we want to send a window update to the peer.
|
|
|
|
|
|
|
|
test ecx, ecx
|
|
|
|
jz .no_window
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
mov cl, [eax + TCP_SOCKET.RCV_SCALE]
|
|
|
|
mov ebx, TCP_max_win
|
|
|
|
shl ebx, cl
|
|
|
|
pop ecx
|
2020-06-06 00:05:55 +03:00
|
|
|
sub ebx, [eax + TCP_SOCKET.RCV_ADV]
|
|
|
|
add ebx, [eax + TCP_SOCKET.RCV_NXT]
|
2013-06-13 12:22:37 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp ebx, ecx
|
2020-06-08 22:57:03 +03:00
|
|
|
jl @f
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ebx, ecx
|
|
|
|
@@:
|
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: we can increase window by %d bytes\n", ebx
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov edi, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
shl edi, 1
|
2013-12-20 01:31:51 +04:00
|
|
|
cmp ebx, edi
|
|
|
|
jae .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
cmp ebx, SOCKET_BUFFER_SIZE/2
|
|
|
|
jae .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.no_window:
|
|
|
|
|
|
|
|
;--------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Should a segment be sent?
|
|
|
|
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: Should a segment be sent?\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_ACKNOW ; we need to ACK
|
2013-12-20 01:31:51 +04:00
|
|
|
jnz .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test dl, TH_SYN + TH_RST ; we need to send a SYN or RST
|
2013-12-20 01:31:51 +04:00
|
|
|
jnz .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_UP] ; when urgent pointer is beyond start of send bufer
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
2013-12-20 01:31:51 +04:00
|
|
|
ja .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
; Do we need to send a FIN according to our state?
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
test dl, TH_FIN
|
2016-08-15 21:06:19 +03:00
|
|
|
jz .enter_persist ; no reason to send, enter persist state
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
; Do so if we didnt do it already
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_SENTFIN
|
2013-12-20 01:31:51 +04:00
|
|
|
jz .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
; Or when we need to retransmit the FIN
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
2013-12-20 01:31:51 +04:00
|
|
|
je .send
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;--------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; Enter persist state
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.enter_persist:
|
|
|
|
|
2013-06-04 18:12:37 +04:00
|
|
|
cmp [eax + STREAM_SOCKET.snd.size], 0 ; Data ready to send?
|
2020-06-08 22:57:03 +03:00
|
|
|
je @f
|
|
|
|
test [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission or timer_flag_persist
|
2013-06-04 18:12:37 +04:00
|
|
|
jnz @f
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: Entering persist state\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [eax + TCP_SOCKET.t_rxtshift], 0
|
2015-12-27 18:37:31 +03:00
|
|
|
call tcp_set_persist
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
|
|
|
;----------------------------
|
2016-08-15 21:06:19 +03:00
|
|
|
; No reason to send a segment
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: No reason to send a segment\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2017-05-30 23:33:16 +03:00
|
|
|
xor eax, eax
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
2016-08-15 21:06:19 +03:00
|
|
|
; Send a segment
|
2013-05-28 21:34:26 +04:00
|
|
|
;
|
|
|
|
; eax = socket pointer
|
|
|
|
; esi = data len
|
|
|
|
; dl = flags
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2013-12-20 01:31:51 +04:00
|
|
|
.send:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: socket=%x length=%u flags=%x\n", eax, esi, dl
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push eax ; save socket ptr
|
|
|
|
push esi ; and data length too
|
|
|
|
mov edi, sizeof.TCP_header ; edi will contain headersize
|
|
|
|
|
|
|
|
;------------------------------------
|
|
|
|
; Send options with first SYN segment
|
|
|
|
|
|
|
|
test dl, TH_SYN
|
|
|
|
jz .options_done
|
|
|
|
|
|
|
|
push [eax + TCP_SOCKET.ISS]
|
|
|
|
pop [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_NOOPT
|
|
|
|
jnz .options_done
|
|
|
|
|
|
|
|
mov ecx, 1460 ;;;; FIXME: use routing blablabla to determine MSS
|
|
|
|
or ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16
|
|
|
|
bswap ecx
|
|
|
|
push ecx
|
|
|
|
add di, 4
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: added maxseg option\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE
|
|
|
|
jz .no_scale
|
|
|
|
|
|
|
|
test dl, TH_ACK
|
|
|
|
jz .scale_opt
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE
|
|
|
|
jz .no_scale
|
|
|
|
|
|
|
|
.scale_opt:
|
|
|
|
mov cl, [eax + TCP_SOCKET.request_r_scale]
|
|
|
|
mov ch, TCP_OPT_NOP
|
|
|
|
pushw cx
|
|
|
|
pushw TCP_OPT_WINDOW + 3 shl 8
|
|
|
|
add di, 4
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: added scale option\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.no_scale:
|
|
|
|
.no_syn:
|
|
|
|
|
|
|
|
;------------------------------------
|
|
|
|
; Make the timestamp option if needed
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP
|
|
|
|
jz .no_timestamp
|
|
|
|
|
|
|
|
test dl, TH_RST
|
|
|
|
jnz .no_timestamp
|
|
|
|
|
|
|
|
test dl, TH_ACK
|
|
|
|
jz .timestamp
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
|
|
|
|
jz .no_timestamp
|
|
|
|
|
|
|
|
.timestamp:
|
|
|
|
pushd 0
|
|
|
|
pushd [timer_ticks]
|
|
|
|
pushd TCP_OPT_NOP + TCP_OPT_NOP shl 8 + TCP_OPT_TIMESTAMP shl 16 + 10 shl 24
|
|
|
|
add di, 12
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: added timestamp\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.no_timestamp:
|
|
|
|
|
|
|
|
; <Add additional options here>
|
|
|
|
|
|
|
|
.options_done:
|
|
|
|
|
|
|
|
; eax = socket ptr
|
|
|
|
; edx = flags
|
|
|
|
; edi = header size
|
|
|
|
; esi = data len
|
|
|
|
|
|
|
|
;---------------------------------------------
|
2024-05-28 17:15:26 +03:00
|
|
|
; check if we don't exceed the max segment size
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
add esi, edi ; total TCP segment size
|
|
|
|
cmp esi, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
jbe .no_overflow
|
|
|
|
|
|
|
|
mov esi, [eax + TCP_SOCKET.t_maxseg]
|
2013-12-13 16:20:53 +04:00
|
|
|
or [temp_bits], TCP_BIT_SENDALOT
|
2013-05-28 21:34:26 +04:00
|
|
|
.no_overflow:
|
|
|
|
|
2015-02-21 18:07:30 +03:00
|
|
|
; Update stats
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2015-02-21 18:07:30 +03:00
|
|
|
test esi, esi
|
|
|
|
jz .zero_data
|
|
|
|
|
2017-05-31 20:54:16 +03:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_FORCE
|
|
|
|
jz @f
|
2015-02-21 18:07:30 +03:00
|
|
|
cmp esi, 1
|
|
|
|
jne @f
|
|
|
|
inc [TCPS_sndprobe]
|
|
|
|
jmp .eos
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_MAX]
|
|
|
|
jae @f
|
|
|
|
inc [TCPS_sndrexmitpack]
|
|
|
|
add [TCPS_sndrexmitbyte], esi
|
|
|
|
jmp .eos
|
|
|
|
@@:
|
|
|
|
inc [TCPS_sndpack]
|
|
|
|
add [TCPS_sndbyte], esi
|
|
|
|
jmp .eos
|
|
|
|
|
|
|
|
.zero_data:
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_ACKNOW
|
|
|
|
jz @f
|
|
|
|
inc [TCPS_sndacks]
|
|
|
|
jmp .eos
|
|
|
|
@@:
|
|
|
|
test dl, TH_SYN + TH_FIN + TH_RST
|
|
|
|
jz @f
|
|
|
|
inc [TCPS_sndctrl]
|
|
|
|
jmp .eos
|
|
|
|
@@:
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_UP]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
jb @f
|
|
|
|
inc [TCPS_sndurg]
|
|
|
|
jmp .eos
|
|
|
|
@@:
|
|
|
|
inc [TCPS_sndwinup]
|
|
|
|
|
|
|
|
.eos:
|
|
|
|
|
2017-05-30 23:33:16 +03:00
|
|
|
;---------------------------------------------------
|
2024-05-28 17:15:26 +03:00
|
|
|
; Don't increase sequence number when resending a FIN
|
2017-05-30 23:33:16 +03:00
|
|
|
|
|
|
|
test dl, TH_FIN
|
|
|
|
jz .no_fin_retransmit
|
|
|
|
|
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_SENTFIN
|
|
|
|
jz .no_fin_retransmit
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_MAX]
|
|
|
|
jne .no_fin_retransmit
|
|
|
|
|
|
|
|
dec [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
|
|
|
|
.no_fin_retransmit:
|
|
|
|
|
2013-12-13 16:20:53 +04:00
|
|
|
;----------------------------------------------------
|
|
|
|
; Calculate the receive window.
|
2024-05-28 17:15:26 +03:00
|
|
|
; Don't shrink window, but avoid silly window syndrome
|
2013-12-13 16:20:53 +04:00
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
xor ebx, ebx
|
|
|
|
test [eax + SOCKET.state], SS_CANTRCVMORE
|
|
|
|
jnz @f
|
2016-05-01 00:15:55 +03:00
|
|
|
mov ebx, SOCKET_BUFFER_SIZE
|
2013-12-13 16:20:53 +04:00
|
|
|
sub ebx, [eax + STREAM_SOCKET.rcv.size]
|
|
|
|
|
2016-05-01 00:15:55 +03:00
|
|
|
cmp ebx, SOCKET_BUFFER_SIZE/4
|
2020-06-08 22:57:03 +03:00
|
|
|
jge @f
|
2013-12-13 16:20:53 +04:00
|
|
|
cmp ebx, [eax + TCP_SOCKET.t_maxseg]
|
2020-06-08 22:57:03 +03:00
|
|
|
jge @f
|
2013-12-13 16:20:53 +04:00
|
|
|
xor ebx, ebx
|
|
|
|
@@:
|
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
|
|
|
|
mov cl, [eax + TCP_SOCKET.RCV_SCALE]
|
|
|
|
push eax
|
|
|
|
mov eax, TCP_max_win
|
|
|
|
shl eax, cl
|
|
|
|
cmp ebx, eax
|
|
|
|
jle @f
|
|
|
|
mov ebx, eax
|
2013-12-13 16:20:53 +04:00
|
|
|
@@:
|
2020-06-08 22:57:03 +03:00
|
|
|
pop eax
|
|
|
|
|
2013-12-13 16:20:53 +04:00
|
|
|
|
|
|
|
mov ecx, [eax + TCP_SOCKET.RCV_ADV]
|
|
|
|
sub ecx, [eax + TCP_SOCKET.RCV_NXT]
|
|
|
|
cmp ebx, ecx
|
2017-05-28 19:52:24 +03:00
|
|
|
jg @f
|
2013-12-13 16:20:53 +04:00
|
|
|
mov ebx, ecx
|
|
|
|
@@:
|
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
;; TODO URGENT POINTER
|
|
|
|
|
2016-08-15 20:25:50 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: window=%u\n", ebx
|
2020-06-08 22:57:03 +03:00
|
|
|
mov [rcv_window], ebx
|
2013-12-13 16:20:53 +04:00
|
|
|
|
|
|
|
mov cl, [eax + TCP_SOCKET.RCV_SCALE]
|
|
|
|
shr ebx, cl
|
|
|
|
xchg bl, bh
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
; Start by pushing all TCP header values in reverse order on stack
|
|
|
|
; (essentially, creating the tcp header on the stack!)
|
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
pushw 0 ; UrgentPointer
|
|
|
|
pushw 0 ; Checksum
|
|
|
|
pushw bx ; Window
|
|
|
|
shl edi, 2 ; DataOffset
|
2013-05-28 21:34:26 +04:00
|
|
|
shl dx, 8
|
2016-08-15 21:06:19 +03:00
|
|
|
or dx, di ; Flags
|
2013-05-28 21:34:26 +04:00
|
|
|
pushw dx
|
2016-08-15 21:06:19 +03:00
|
|
|
shr edi, 2 ; DataOffset
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
push [eax + TCP_SOCKET.RCV_NXT] ; AckNumber
|
2013-05-28 21:34:26 +04:00
|
|
|
ntohd [esp]
|
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
push [eax + TCP_SOCKET.SND_NXT] ; SequenceNumber
|
2013-05-28 21:34:26 +04:00
|
|
|
ntohd [esp]
|
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
push [eax + TCP_SOCKET.RemotePort] ; DestinationPort
|
|
|
|
push [eax + TCP_SOCKET.LocalPort] ; SourcePort
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
push edi ; header size
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;---------------------
|
|
|
|
; Create the IP packet
|
|
|
|
|
|
|
|
mov ecx, esi
|
2015-07-20 14:13:42 +03:00
|
|
|
mov ebx, [eax + IP_SOCKET.device]
|
2015-10-11 21:23:40 +03:00
|
|
|
mov edx, [eax + IP_SOCKET.LocalIP] ; source ip
|
|
|
|
mov edi, [eax + IP_SOCKET.RemoteIP] ; dest ip
|
|
|
|
mov al, [eax + IP_SOCKET.ttl]
|
|
|
|
mov ah, IP_PROTO_TCP
|
2015-12-27 18:37:31 +03:00
|
|
|
call ipv4_output
|
2013-05-28 21:34:26 +04:00
|
|
|
jz .ip_error
|
|
|
|
|
2015-02-21 18:07:30 +03:00
|
|
|
;------------------------------------------
|
|
|
|
; Move TCP header from stack to TCP segment
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push ecx
|
|
|
|
mov ecx, [esp + 4]
|
|
|
|
lea esi, [esp + 8]
|
2016-08-15 20:25:50 +03:00
|
|
|
shr ecx, 2 ; count is in bytes, we will work with dwords
|
2013-06-27 03:35:43 +04:00
|
|
|
rep movsd
|
2016-08-15 20:25:50 +03:00
|
|
|
pop ecx ; full TCP packet size
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 20:25:50 +03:00
|
|
|
pop esi ; headersize
|
|
|
|
add esp, esi ; remove it from stack
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 20:25:50 +03:00
|
|
|
push eax ; packet ptr for send proc
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 20:25:50 +03:00
|
|
|
mov edx, edi ; begin of data
|
|
|
|
sub edx, esi ; begin of packet (edi = begin of data)
|
2013-05-28 21:34:26 +04:00
|
|
|
push ecx
|
2016-08-15 20:25:50 +03:00
|
|
|
sub ecx, esi ; data size
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;--------------
|
|
|
|
; Copy the data
|
|
|
|
|
|
|
|
; eax = ptr to ring struct
|
|
|
|
; ecx = buffer size
|
|
|
|
; edi = ptr to buffer
|
|
|
|
|
2015-03-18 00:50:29 +03:00
|
|
|
mov eax, [esp + 12] ; get socket ptr
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push edx
|
|
|
|
push [eax + TCP_SOCKET.SND_NXT] ; we'll need this for timing the transmission
|
|
|
|
test ecx, ecx
|
|
|
|
jz .nodata
|
|
|
|
mov edx, [eax + TCP_SOCKET.SND_NXT]
|
2015-02-21 18:07:30 +03:00
|
|
|
add [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number
|
2013-05-28 21:34:26 +04:00
|
|
|
sub edx, [eax + TCP_SOCKET.SND_UNA] ; offset
|
|
|
|
add eax, STREAM_SOCKET.snd
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_ring_read
|
2013-05-28 21:34:26 +04:00
|
|
|
.nodata:
|
|
|
|
pop edi
|
|
|
|
pop esi ; begin of data
|
|
|
|
pop ecx ; full packet size
|
2015-03-18 00:50:29 +03:00
|
|
|
mov eax, [esp + 8] ; socket ptr
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-08-15 21:06:19 +03:00
|
|
|
;----------------------------
|
|
|
|
; initialize retransmit timer
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;TODO: check t_force and persist
|
|
|
|
|
|
|
|
test [esi + TCP_header.Flags], TH_SYN + TH_FIN ; syn and fin take a sequence number
|
|
|
|
jz @f
|
|
|
|
inc [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
test [esi + TCP_header.Flags], TH_FIN
|
|
|
|
jz @f
|
|
|
|
or [eax + TCP_SOCKET.t_flags], TF_SENTFIN ; if we sent a fin, set the sentfin flag
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov edx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp edx, [eax + TCP_SOCKET.SND_MAX] ; is this a retransmission?
|
|
|
|
jbe @f
|
|
|
|
mov [eax + TCP_SOCKET.SND_MAX], edx ; [eax + TCP_SOCKET.SND_NXT] from before we updated it
|
|
|
|
|
|
|
|
cmp [eax + TCP_SOCKET.t_rtt], 0 ; are we currently timing anything?
|
|
|
|
je @f
|
|
|
|
mov [eax + TCP_SOCKET.t_rtt], 1 ; nope, start transmission timer
|
|
|
|
mov [eax + TCP_SOCKET.t_rtseq], edi
|
2015-02-21 18:07:30 +03:00
|
|
|
inc [TCPS_segstimed]
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
|
|
|
; set retransmission timer if not already set, and not doing an ACK or keepalive probe
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-06-04 18:12:37 +04:00
|
|
|
test [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission
|
|
|
|
jnz .retransmit_set
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp edx, [eax + TCP_SOCKET.SND_UNA] ; edx is still [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
je .retransmit_set
|
|
|
|
|
|
|
|
mov edx, [eax + TCP_SOCKET.t_rxtcur]
|
|
|
|
mov [eax + TCP_SOCKET.timer_retransmission], edx
|
2013-06-05 04:53:57 +04:00
|
|
|
or [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-04 18:12:37 +04:00
|
|
|
test [eax + TCP_SOCKET.timer_flags], timer_flag_persist
|
|
|
|
jz .retransmit_set
|
2013-06-05 04:53:57 +04:00
|
|
|
and [eax + TCP_SOCKET.timer_flags], not timer_flag_persist
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + TCP_SOCKET.t_rxtshift], 0
|
|
|
|
.retransmit_set:
|
|
|
|
|
|
|
|
;--------------------
|
|
|
|
; Create the checksum
|
|
|
|
|
2013-12-20 01:31:51 +04:00
|
|
|
xor dx, dx
|
|
|
|
test [ebx + NET_DEVICE.hwacc], NET_HWACC_TCP_IPv4_OUT
|
|
|
|
jnz .checksum_ok
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
tcp_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP)
|
2013-12-20 01:31:51 +04:00
|
|
|
|
|
|
|
.checksum_ok:
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [esi + TCP_header.Checksum], dx
|
|
|
|
|
|
|
|
;----------------
|
|
|
|
; Send the packet
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: Sending with device %x\n", ebx
|
2013-05-28 21:34:26 +04:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
jnz .send_error
|
|
|
|
|
|
|
|
;---------------
|
|
|
|
; Ok, data sent!
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
pop eax
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call net_ptr_to_num4
|
2013-06-11 15:11:26 +04:00
|
|
|
inc [TCP_segments_tx + edi]
|
2015-02-21 18:07:30 +03:00
|
|
|
inc [TCPS_sndtotal]
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; update advertised receive window
|
2016-08-15 20:25:50 +03:00
|
|
|
|
2020-06-08 22:57:03 +03:00
|
|
|
mov ecx, [rcv_window]
|
2013-05-28 21:34:26 +04:00
|
|
|
test ecx, ecx
|
|
|
|
jz @f
|
|
|
|
add ecx, [eax + TCP_SOCKET.RCV_NXT]
|
|
|
|
cmp ecx, [eax + TCP_SOCKET.RCV_ADV]
|
|
|
|
jbe @f
|
|
|
|
mov [eax + TCP_SOCKET.RCV_ADV], ecx
|
|
|
|
@@:
|
|
|
|
|
|
|
|
; update last ack sent
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push [eax + TCP_SOCKET.RCV_NXT]
|
|
|
|
pop [eax + TCP_SOCKET.last_ack_sent]
|
|
|
|
|
2013-12-13 16:20:53 +04:00
|
|
|
; clear the ACK flags
|
2016-08-15 21:06:19 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
and [eax + TCP_SOCKET.t_flags], not (TF_ACKNOW + TF_DELACK)
|
|
|
|
|
|
|
|
;--------------
|
|
|
|
; unlock socket
|
|
|
|
|
2013-06-13 12:22:37 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: unlocking socket 0x%x\n", eax
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push eax
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
pop eax
|
|
|
|
|
|
|
|
;-----------------------------
|
|
|
|
; Check if we need more output
|
|
|
|
|
2013-12-13 16:20:53 +04:00
|
|
|
test [temp_bits], TCP_BIT_SENDALOT
|
2015-12-27 18:37:31 +03:00
|
|
|
jnz tcp_output.again
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: success!\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
.ip_error:
|
|
|
|
pop ecx
|
|
|
|
add esp, ecx
|
|
|
|
add esp, 4
|
|
|
|
pop eax
|
|
|
|
|
|
|
|
mov [eax + TCP_SOCKET.timer_retransmission], TCP_time_re_min
|
2013-06-05 04:44:59 +04:00
|
|
|
or [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
|
2013-06-05 04:44:59 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "TCP_send: IP error\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
or eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
.send_error:
|
2013-06-13 12:22:37 +04:00
|
|
|
add esp, 4
|
|
|
|
pop eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
|
2013-06-05 04:44:59 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "TCP_send: sending failed\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
or eax, -2
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2024-05-22 18:15:14 +03:00
|
|
|
endp
|