2011-01-24 23:01:54 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2012-02-21 09:44:51 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
|
2011-01-24 23:01:54 +01: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 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
$Revision$
|
|
|
|
|
2011-01-08 15:59:21 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; TCP_output
|
|
|
|
;
|
|
|
|
; IN: eax = socket pointer
|
|
|
|
;
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
TCP_output:
|
|
|
|
|
2012-08-28 00:47:10 +02:00
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_output: socket=%x\n", eax
|
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
2011-01-08 15:59:21 +01: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
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_MAX]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
2012-08-28 21:02:56 +02:00
|
|
|
jbe .not_idle
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.t_idle]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.t_rxtcur]
|
|
|
|
jbe .not_idle
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
; 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.
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
mov [eax + TCP_SOCKET.SND_CWND], ebx
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.not_idle:
|
|
|
|
.again:
|
2012-08-27 15:55:15 +02:00
|
|
|
mov [eax + TCP_SOCKET.temp_bits], 0
|
2012-07-26 16:54:57 +02:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT] ; calculate offset (71)
|
|
|
|
sub ebx, [eax + TCP_SOCKET.SND_UNA] ;
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ecx, [eax + TCP_SOCKET.SND_WND] ; determine window
|
|
|
|
cmp ecx, [eax + TCP_SOCKET.SND_CWND] ;
|
|
|
|
jb @f ;
|
|
|
|
mov ecx, [eax + TCP_SOCKET.SND_CWND] ;
|
|
|
|
@@: ;
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
call TCP_outflags ; flags in dl
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
;------------------------
|
|
|
|
; data being forced out ?
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
; 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
|
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
cmp [eax + TCP_SOCKET.t_force], 0
|
|
|
|
je .no_force
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test ecx, ecx
|
|
|
|
jnz .no_zero_window
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
cmp ebx, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
jae @f
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
and dl, not (TH_FIN)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
@@:
|
2012-02-25 13:03:22 +01:00
|
|
|
inc ecx
|
|
|
|
jmp .no_force
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.no_zero_window:
|
2012-02-25 13:03:22 +01:00
|
|
|
mov [eax + TCP_SOCKET.timer_persist], 0
|
|
|
|
mov [eax + TCP_SOCKET.t_rxtshift], 0
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
.no_force:
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;--------------------------------
|
|
|
|
; Calculate how much data to send (106)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov esi, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
cmp esi, ecx
|
|
|
|
jb @f
|
|
|
|
mov esi, ecx
|
2011-01-08 15:59:21 +01:00
|
|
|
@@:
|
2012-02-25 13:03:22 +01:00
|
|
|
sub esi, ebx
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-04-14 20:20:01 +02:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;------------------------
|
|
|
|
; check for window shrink (107)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
; If FIN has been set, but not ACKed, but we havent been called to retransmit, esi will be -1
|
2011-01-08 15:59:21 +01:00
|
|
|
; Otherwise, window shrank after we sent into it.
|
|
|
|
|
2012-04-14 20:20:01 +02:00
|
|
|
jae .not_persist
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
; enter persist state
|
2012-02-25 13:03:22 +01:00
|
|
|
xor esi, esi
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
; If window shrank to 0
|
2012-02-25 13:03:22 +01:00
|
|
|
test ecx, ecx
|
|
|
|
jnz @f
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
; cancel pending retransmit
|
2012-02-25 13:03:22 +01:00
|
|
|
mov [eax + TCP_SOCKET.timer_retransmission], 0
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
; pull SND_NXT back to (closed) window, We will enter persist state below.
|
2012-02-25 13:03:22 +01:00
|
|
|
push [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
pop [eax + TCP_SOCKET.SND_NXT]
|
2011-01-08 15:59:21 +01:00
|
|
|
@@:
|
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
; If window didn't close completely, just wait for an ACK
|
|
|
|
|
2012-04-14 20:20:01 +02:00
|
|
|
.not_persist:
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;---------------------------
|
|
|
|
; Send one segment at a time (124)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
cmp esi, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
jbe @f
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov esi, [eax + TCP_SOCKET.t_maxseg]
|
2012-08-27 15:55:15 +02:00
|
|
|
or [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
|
2011-01-08 15:59:21 +01:00
|
|
|
@@:
|
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;--------------------------------------------
|
|
|
|
; Turn of FIN flag if send buffer not emptied (128)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov edi, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
add edi, esi
|
|
|
|
sub edi, [eax + TCP_SOCKET.SND_UNA]
|
2012-07-26 16:54:57 +02:00
|
|
|
cmp edi, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
jae @f
|
2012-02-25 13:03:22 +01:00
|
|
|
and dl, not (TH_FIN)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
@@:
|
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;-------------------------------
|
|
|
|
; calculate window advertisement (130)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ecx, SOCKET_MAXDATA
|
|
|
|
sub ecx, [eax + STREAM_SOCKET.rcv.size]
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
;------------------------------
|
2011-01-20 23:29:47 +01:00
|
|
|
; Sender silly window avoidance (131)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test esi, esi
|
|
|
|
jz .len_zero
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
cmp esi, [eax + TCP_SOCKET.t_maxseg]
|
2012-07-24 22:29:46 +02:00
|
|
|
je TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
add ebx, esi ; offset + length
|
|
|
|
cmp ebx, [eax + STREAM_SOCKET.snd.size]
|
|
|
|
jb @f
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_NODELAY
|
2012-07-26 16:54:57 +02:00
|
|
|
jnz TCP_send
|
|
|
|
|
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_MAX]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
|
|
|
je TCP_send
|
2012-02-21 09:44:51 +01:00
|
|
|
@@:
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_force], -1 ;;;
|
2012-07-24 22:29:46 +02:00
|
|
|
jnz TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.max_sndwnd]
|
|
|
|
shr ebx, 1
|
|
|
|
cmp esi, ebx
|
2012-07-24 22:29:46 +02:00
|
|
|
jae TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_MAX]
|
2012-07-24 22:29:46 +02:00
|
|
|
jb TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
.len_zero:
|
|
|
|
|
2011-01-08 15:59:21 +01:00
|
|
|
;----------------------------------------
|
2011-01-20 23:29:47 +01:00
|
|
|
; Check if a window update should be sent (154)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
DEBUGF 1,"TCP_output: window=%d\n", ecx
|
|
|
|
|
|
|
|
; 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.
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test ecx, ecx
|
|
|
|
jz .no_window
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push ecx
|
|
|
|
mov cl, [eax + TCP_SOCKET.RCV_SCALE]
|
|
|
|
mov ebx, TCP_max_win
|
|
|
|
shl ebx, cl
|
|
|
|
pop ecx
|
|
|
|
cmp ebx, ecx
|
2012-08-28 21:02:56 +02:00
|
|
|
jb @f
|
|
|
|
mov ebx, ecx
|
|
|
|
@@:
|
|
|
|
sub ebx, [eax + TCP_SOCKET.RCV_ADV]
|
|
|
|
add ebx, [eax + TCP_SOCKET.RCV_NXT]
|
2012-02-21 09:44:51 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
mov edi, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
shl edi, 1
|
2012-02-21 09:44:51 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
; cmp ebx, edi
|
|
|
|
; jae TCP_send
|
2012-02-21 09:44:51 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
; cmp ebx, [eax + TCP_SOCKET.] ;;; TODO: check with receive buffer high water mark
|
|
|
|
; jae TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.no_window:
|
|
|
|
|
|
|
|
;--------------------------
|
2011-01-20 23:29:47 +01:00
|
|
|
; Should a segment be sent? (174)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
DEBUGF 1,"TCP_output: 174\n"
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_ACKNOW ; we need to ACK
|
2012-07-24 22:29:46 +02:00
|
|
|
jnz TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test dl, TH_SYN + TH_RST ; we need to send a SYN or RST
|
2012-07-24 22:29:46 +02:00
|
|
|
jnz TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_UP] ; when urgent pointer is beyond start of send bufer
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
2012-07-24 22:29:46 +02:00
|
|
|
ja TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test dl, TH_FIN
|
|
|
|
jz .enter_persist ; no reason to send, enter persist state
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
; FIN was set, only send if not already sent, or on retransmit
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_SENTFIN
|
2012-07-26 16:54:57 +02:00
|
|
|
jz TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ebx, [eax + TCP_SOCKET.SND_NXT]
|
|
|
|
cmp ebx, [eax + TCP_SOCKET.SND_UNA]
|
2012-07-24 22:29:46 +02:00
|
|
|
je TCP_send
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
;--------------------
|
2011-01-20 23:29:47 +01:00
|
|
|
; Enter persist state (191)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.enter_persist:
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
cmp [eax + STREAM_SOCKET.snd.size], 0 ; Data ready to send?
|
|
|
|
jne @f
|
|
|
|
cmp [eax + TCP_SOCKET.timer_retransmission], 0
|
|
|
|
jne @f
|
|
|
|
cmp [eax + TCP_SOCKET.timer_persist], 0 ; Persist timer already expired?
|
|
|
|
jne @f
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_output: Entering persist state\n"
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov [eax + TCP_SOCKET.t_rxtshift], 0
|
2012-08-28 22:48:45 +02:00
|
|
|
call TCP_set_persist
|
2012-02-21 09:44:51 +01:00
|
|
|
@@:
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
;----------------------------
|
|
|
|
; No reason to send a segment (219)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_output: No reason to send a segment\n"
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-28 00:47:10 +02:00
|
|
|
popf
|
2012-02-25 13:03:22 +01:00
|
|
|
ret
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-08 15:59:21 +01:00
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
2011-01-20 23:29:47 +01:00
|
|
|
; Send a segment (222)
|
2011-01-08 15:59:21 +01:00
|
|
|
;
|
|
|
|
; eax = socket pointer
|
2011-01-20 23:29:47 +01:00
|
|
|
; esi = data len
|
2011-01-08 15:59:21 +01:00
|
|
|
; dl = flags
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2012-07-24 22:29:46 +02:00
|
|
|
align 4
|
|
|
|
TCP_send:
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_send: socket=%x length=%u flags=%x\n", eax, esi, dl
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
push eax ; save socket ptr
|
2012-08-27 15:55:15 +02:00
|
|
|
push esi ; and data length too
|
2012-02-25 13:03:22 +01:00
|
|
|
mov edi, sizeof.TCP_header ; edi will contain headersize
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
;------------------------------------
|
|
|
|
; Send options with first SYN segment
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test dl, TH_SYN
|
|
|
|
jz .options_done
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push [eax + TCP_SOCKET.ISS]
|
|
|
|
pop [eax + TCP_SOCKET.SND_NXT]
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_NOOPT
|
|
|
|
jnz .options_done
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
mov ecx, 1460 ;;;; FIXME: use routing blablabla to determine MSS
|
2012-02-25 13:03:22 +01:00
|
|
|
or ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16
|
|
|
|
bswap ecx
|
|
|
|
push ecx
|
|
|
|
add di, 4
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
DEBUGF 1,"TCP_send: added maxseg option\n"
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE
|
2012-08-28 21:30:47 +02:00
|
|
|
jz .no_scale
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test dl, TH_ACK
|
2012-08-28 21:30:47 +02:00
|
|
|
jz .scale_opt
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE
|
2012-08-28 21:30:47 +02:00
|
|
|
jz .no_scale
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.scale_opt:
|
2012-08-28 21:30:47 +02:00
|
|
|
mov cl, [eax + TCP_SOCKET.request_r_scale]
|
|
|
|
mov ch, TCP_OPT_NOP
|
|
|
|
pushw cx
|
|
|
|
pushw TCP_OPT_WINDOW + 3 shl 8
|
2012-02-25 13:03:22 +01:00
|
|
|
add di, 4
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-28 21:02:56 +02:00
|
|
|
DEBUGF 1,"TCP_send: added scale option\n"
|
|
|
|
|
2012-08-28 21:30:47 +02:00
|
|
|
.no_scale:
|
2011-01-08 15:59:21 +01:00
|
|
|
.no_syn:
|
|
|
|
|
|
|
|
;------------------------------------
|
|
|
|
; Make the timestamp option if needed
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP
|
|
|
|
jz .no_timestamp
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test dl, TH_RST
|
|
|
|
jnz .no_timestamp
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test dl, TH_ACK
|
|
|
|
jz .timestamp
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
test [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
|
|
|
|
jz .no_timestamp
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.timestamp:
|
2012-08-28 21:02:56 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
DEBUGF 1,"TCP_send: added timestamp\n"
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
.no_timestamp:
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
; <Add additional options here>
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
.options_done:
|
|
|
|
|
|
|
|
; eax = socket ptr
|
|
|
|
; edx = flags
|
|
|
|
; edi = header size
|
|
|
|
; esi = data len
|
|
|
|
|
|
|
|
;---------------------------------------------
|
|
|
|
; check if we dont exceed the max segment size (270)
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
add esi, edi ; total TCP segment size
|
|
|
|
cmp esi, [eax + TCP_SOCKET.t_maxseg]
|
|
|
|
jbe .no_overflow
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov esi, [eax + TCP_SOCKET.t_maxseg]
|
2012-08-27 15:55:15 +02:00
|
|
|
or [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
|
2011-01-20 23:29:47 +01:00
|
|
|
.no_overflow:
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
2011-01-08 15:59:21 +01:00
|
|
|
; Start by pushing all TCP header values in reverse order on stack
|
2011-01-20 23:29:47 +01:00
|
|
|
; (essentially, creating the tcp header on the stack!)
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
pushw 0 ; .UrgentPointer dw ?
|
|
|
|
pushw 0 ; .Checksum dw ?
|
2012-07-24 22:29:46 +02:00
|
|
|
pushw 0x00a0 ; .Window dw ? ;;;;;;; FIXME (370)
|
2012-02-25 13:03:22 +01:00
|
|
|
shl edi, 2 ; .DataOffset db ? only 4 left-most bits
|
|
|
|
shl dx, 8
|
|
|
|
or dx, di ; .Flags db ?
|
|
|
|
pushw dx
|
2012-07-24 22:29:46 +02:00
|
|
|
shr edi, 2 ; .DataOffset db ?
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push [eax + TCP_SOCKET.RCV_NXT] ; .AckNumber dd ?
|
|
|
|
ntohd [esp]
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push [eax + TCP_SOCKET.SND_NXT] ; .SequenceNumber dd ?
|
|
|
|
ntohd [esp]
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push [eax + TCP_SOCKET.RemotePort] ; .DestinationPort dw ?
|
|
|
|
ntohw [esp]
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push [eax + TCP_SOCKET.LocalPort] ; .SourcePort dw ?
|
|
|
|
ntohw [esp]
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
push edi ; header size
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;---------------------
|
2011-01-08 15:59:21 +01:00
|
|
|
; Create the IP packet
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov ecx, esi
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-07-17 23:36:34 +02:00
|
|
|
mov ebx, [eax + SOCKET.device]
|
|
|
|
mov edx, [eax + IP_SOCKET.LocalIP] ; source ip
|
2012-02-25 13:03:22 +01:00
|
|
|
mov eax, [eax + IP_SOCKET.RemoteIP] ; dest ip
|
|
|
|
mov di, IP_PROTO_TCP shl 8 + 128
|
|
|
|
call IPv4_output
|
2012-04-03 20:26:53 +02:00
|
|
|
jz .ip_error
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
;-----------------------------------------
|
|
|
|
; Move TCP header from stack to TCP packet
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push ecx
|
2012-07-26 16:54:57 +02:00
|
|
|
mov ecx, [esp + 4]
|
|
|
|
lea esi, [esp + 8]
|
|
|
|
shr ecx, 2 ; count is in bytes, we will work with dwords
|
2012-02-25 13:03:22 +01:00
|
|
|
rep movsd
|
2012-07-26 16:54:57 +02:00
|
|
|
pop ecx ; full TCP packet size
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
pop esi ; headersize
|
|
|
|
add esp, esi ; remove it from stack
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
push edx ; packet size for send proc
|
|
|
|
push eax ; packet ptr for send proc
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov edx, edi ; begin of data
|
|
|
|
sub edx, esi ; begin of packet (edi = begin of data)
|
|
|
|
push ecx
|
|
|
|
sub ecx, esi ; data size
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
;--------------
|
|
|
|
; Copy the data
|
|
|
|
|
|
|
|
; eax = ptr to ring struct
|
|
|
|
; ecx = buffer size
|
|
|
|
; edi = ptr to buffer
|
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
mov eax, [esp + 16] ; get socket ptr
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
push edx
|
2012-08-27 15:55:15 +02:00
|
|
|
push [eax + TCP_SOCKET.SND_NXT] ; we'll need this for timing the transmission
|
2012-07-24 22:29:46 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .nodata
|
2012-07-28 00:14:12 +02:00
|
|
|
mov edx, [eax + TCP_SOCKET.SND_NXT]
|
2012-08-27 15:55:15 +02:00
|
|
|
add [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number <<< CHECKME
|
|
|
|
sub edx, [eax + TCP_SOCKET.SND_UNA] ; offset
|
2012-07-24 22:29:46 +02:00
|
|
|
add eax, STREAM_SOCKET.snd
|
2012-02-25 13:03:22 +01:00
|
|
|
call SOCKET_ring_read
|
2012-07-24 22:29:46 +02:00
|
|
|
.nodata:
|
2012-08-27 15:55:15 +02:00
|
|
|
pop edi
|
2012-02-25 13:03:22 +01:00
|
|
|
pop esi ; begin of data
|
|
|
|
pop ecx ; full packet size
|
2012-08-27 20:49:49 +02:00
|
|
|
mov eax, [esp + 12] ; socket ptr
|
2012-07-26 16:54:57 +02:00
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
;----------------------------------
|
2012-08-27 15:55:15 +02:00
|
|
|
; initialize retransmit timer (400)
|
|
|
|
|
|
|
|
;TODO: check t_force and persist
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
test [esi + TCP_header.Flags], TH_SYN + TH_FIN ; syn and fin take a sequence number
|
2012-02-25 13:03:22 +01:00
|
|
|
jz @f
|
2012-08-27 15:55:15 +02:00
|
|
|
inc [eax + TCP_SOCKET.SND_NXT]
|
2012-02-25 13:03:22 +01:00
|
|
|
test [esi + TCP_header.Flags], TH_FIN
|
|
|
|
jz @f
|
2012-08-27 15:55:15 +02:00
|
|
|
or [eax + TCP_SOCKET.t_flags], TF_SENTFIN ; if we sent a fin, set the sentfin flag
|
2011-01-08 15:59:21 +01:00
|
|
|
@@:
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov edx, [eax + TCP_SOCKET.SND_NXT]
|
2012-08-27 15:55:15 +02:00
|
|
|
cmp edx, [eax + TCP_SOCKET.SND_MAX] ; is this a retransmission?
|
2012-02-25 13:03:22 +01:00
|
|
|
jbe @f
|
2012-08-27 15:55:15 +02:00
|
|
|
mov [eax + TCP_SOCKET.SND_MAX], edx ; [eax + TCP_SOCKET.SND_NXT] from before we updated it
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
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
|
|
|
|
;TODO: update stats
|
2011-01-08 15:59:21 +01:00
|
|
|
@@:
|
|
|
|
|
2011-01-20 23:29:47 +01:00
|
|
|
; set retransmission timer if not already set, and not doing an ACK or keepalive probe
|
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
cmp [eax + TCP_SOCKET.timer_retransmission], 0 ;;;; FIXME
|
|
|
|
ja .retransmit_set
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
cmp edx, [eax + TCP_SOCKET.SND_UNA] ; edx is still [eax + TCP_SOCKET.SND_NXT]
|
2012-02-25 13:03:22 +01:00
|
|
|
je .retransmit_set
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
mov edx, [eax + TCP_SOCKET.t_rxtcur]
|
2012-08-28 22:48:45 +02:00
|
|
|
mov [eax + TCP_SOCKET.timer_retransmission], edx
|
2011-01-20 23:29:47 +01:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
cmp [eax + TCP_SOCKET.timer_persist], 0
|
2012-07-26 16:54:57 +02:00
|
|
|
jne .retransmit_set
|
2012-02-25 13:03:22 +01:00
|
|
|
mov [eax + TCP_SOCKET.timer_persist], 0
|
|
|
|
mov [eax + TCP_SOCKET.t_rxtshift], 0
|
2011-01-20 23:29:47 +01:00
|
|
|
|
|
|
|
.retransmit_set:
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
;--------------------
|
|
|
|
; Create the checksum
|
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP)
|
|
|
|
mov [esi + TCP_header.Checksum], dx
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-08-27 21:19:53 +02:00
|
|
|
; unlock socket
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
|
2011-01-08 15:59:21 +01:00
|
|
|
;----------------
|
|
|
|
; Send the packet
|
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_send: Sending with device %x\n", ebx
|
2012-02-25 13:03:22 +01:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
2012-07-26 16:54:57 +02:00
|
|
|
jnz .send_error
|
2012-08-27 15:55:15 +02:00
|
|
|
|
|
|
|
;---------------
|
|
|
|
; Ok, data sent!
|
|
|
|
|
|
|
|
pop ecx
|
2012-07-26 16:54:57 +02:00
|
|
|
pop eax
|
|
|
|
|
|
|
|
inc [TCP_segments_tx] ; FIXME: correct interface?
|
|
|
|
|
2012-08-27 21:19:53 +02:00
|
|
|
; unlock socket
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
; update advertised receive window
|
|
|
|
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
|
|
|
|
@@:
|
2012-07-26 16:54:57 +02:00
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
; update last ack sent
|
2012-07-26 16:54:57 +02:00
|
|
|
push [eax + TCP_SOCKET.RCV_NXT]
|
|
|
|
pop [eax + TCP_SOCKET.last_ack_sent]
|
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
; and flags
|
2012-07-26 16:54:57 +02:00
|
|
|
and [eax + TCP_SOCKET.t_flags], not (TF_ACKNOW + TF_DELACK)
|
|
|
|
|
2012-08-27 15:55:15 +02:00
|
|
|
test [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
|
|
|
|
jnz TCP_output.again
|
2012-07-26 16:54:57 +02:00
|
|
|
|
|
|
|
; unlock socket
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
2012-08-20 18:04:05 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_send: success!\n"
|
2012-07-26 16:54:57 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
2012-08-28 00:47:10 +02:00
|
|
|
popf
|
2012-02-25 13:03:22 +01:00
|
|
|
ret
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
|
2012-04-03 20:26:53 +02:00
|
|
|
.ip_error:
|
2012-02-25 13:03:22 +01:00
|
|
|
pop ecx
|
|
|
|
add esp, ecx
|
2012-08-27 15:55:15 +02:00
|
|
|
add esp, 4
|
2012-02-25 13:03:22 +01:00
|
|
|
pop eax
|
|
|
|
|
2012-04-18 18:01:38 +02:00
|
|
|
mov [eax + TCP_SOCKET.timer_retransmission], TCP_time_re_min
|
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
; unlock socket
|
2012-02-25 13:03:22 +01:00
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
2012-08-20 18:04:05 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_send: IP error\n"
|
2012-07-26 16:54:57 +02:00
|
|
|
|
|
|
|
or eax, -1
|
2012-08-28 00:47:10 +02:00
|
|
|
popf
|
2012-02-25 13:03:22 +01:00
|
|
|
ret
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-26 16:54:57 +02:00
|
|
|
.send_error:
|
2012-08-27 21:19:53 +02:00
|
|
|
add esp, 8
|
2012-08-20 18:04:05 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"TCP_send: sending failed\n"
|
2012-07-26 16:54:57 +02:00
|
|
|
|
|
|
|
or eax, -2
|
2012-08-28 00:47:10 +02:00
|
|
|
popf
|
2012-07-26 16:54:57 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2011-01-08 15:59:21 +01:00
|
|
|
|
2012-07-24 22:29:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|