Updates in NET branch:

- Added RST packet handling code
- Added SOCKET_isconnected
- Added ACK handeling in syn rcvd state (for passive open)


git-svn-id: svn://kolibrios.org@1831 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2011-02-01 17:00:04 +00:00
parent 0d53bac040
commit 36e43113ac
4 changed files with 144 additions and 98 deletions

View File

@ -501,7 +501,7 @@ align 4
@@: @@:
mov [eax + TCP_SOCKET.timer_persist], 0 mov [eax + TCP_SOCKET.timer_persist], 0
mov [eax + TCP_SOCKET.t_state], TCB_SYN_SENT mov [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT
push [TCP_sequence_num] push [TCP_sequence_num]
add [TCP_sequence_num], 6400 add [TCP_sequence_num], 6400
pop [eax + TCP_SOCKET.ISS] pop [eax + TCP_SOCKET.ISS]
@ -585,7 +585,7 @@ SOCKET_listen:
mov [eax + SOCKET.backlog], dx mov [eax + SOCKET.backlog], dx
or [eax + SOCKET.options], SO_ACCEPTCON or [eax + SOCKET.options], SO_ACCEPTCON
mov [eax + TCP_SOCKET.t_state], TCB_LISTEN mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN
push eax push eax
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue
@ -666,7 +666,7 @@ SOCKET_close:
jmp s_error jmp s_error
.tcp: .tcp:
cmp [eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED ; state must be LISTEN, SYN_SENT or CLOSED cmp [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED ; state must be LISTEN, SYN_SENT or CLOSED
jl .free jl .free
call TCP_output call TCP_output
@ -849,7 +849,7 @@ SOCKET_send_icmp:
; ;
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP. ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
; TODO: find best way to notify that send()'ed data were acknowledged ; TODO: find best way to notify that send()'ed data were acknowledged
; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*. ; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
; ;
;----------------------------------------------------------------- ;-----------------------------------------------------------------
align 4 align 4
@ -1738,3 +1738,23 @@ align 4
SOCKET_cant_recv_more: SOCKET_cant_recv_more:
ret ret
;-----------------------------------------------------------------
;
; SOCKET_is_connected
;
; IN: eax = socket ptr
; OUT: /
;
;-----------------------------------------------------------------
align 4
SOCKET_is_connected:
and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
or [eax + SOCKET.options], SS_ISCONNECTED
jmp SOCKET_notify_owner

View File

@ -17,17 +17,17 @@
$Revision$ $Revision$
; Socket states ; Socket states
TCB_CLOSED equ 0 TCPS_CLOSED equ 0
TCB_LISTEN equ 1 TCPS_LISTEN equ 1
TCB_SYN_SENT equ 2 TCPS_SYN_SENT equ 2
TCB_SYN_RECEIVED equ 3 TCPS_SYN_RECEIVED equ 3
TCB_ESTABLISHED equ 4 TCPS_ESTABLISHED equ 4
TCB_CLOSE_WAIT equ 5 TCPS_CLOSE_WAIT equ 5
TCB_FIN_WAIT_1 equ 6 TCPS_FIN_WAIT_1 equ 6
TCB_CLOSING equ 7 TCPS_CLOSING equ 7
TCB_LAST_ACK equ 8 TCPS_LAST_ACK equ 8
TCB_FIN_WAIT_2 equ 9 TCPS_FIN_WAIT_2 equ 9
TCB_TIMED_WAIT equ 10 TCPS_TIMED_WAIT equ 10
; Socket Flags ; Socket Flags
TF_ACKNOW equ 1 shl 0 ; ack peer immediately TF_ACKNOW equ 1 shl 0 ; ack peer immediately

View File

@ -105,7 +105,7 @@ TCP_input:
ntohw [edx + TCP_segment.DestinationPort] ntohw [edx + TCP_segment.DestinationPort]
;------------------------------------------------------------ ;------------------------------------------------------------
; Next thing to do is find the TCB (thus, the socket pointer) ; Next thing to do is find the TCPS (thus, the socket pointer)
; IP Packet TCP Destination Port = local Port ; IP Packet TCP Destination Port = local Port
; (IP Packet SenderAddress = Remote IP) OR (Remote IP = 0) ; (IP Packet SenderAddress = Remote IP) OR (Remote IP = 0)
@ -148,7 +148,7 @@ TCP_input:
;---------------------------- ;----------------------------
; Check if socket isnt closed ; Check if socket isnt closed
cmp [ebx + TCP_SOCKET.t_state], TCB_CLOSED cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
je .drop_not_locked je .drop_not_locked
;---------------- ;----------------
@ -193,7 +193,7 @@ TCP_input:
push [edx - IPv4_Packet.DataOrOptional + IPv4_Packet.SourceAddress] ;;; FIXME push [edx - IPv4_Packet.DataOrOptional + IPv4_Packet.SourceAddress] ;;; FIXME
pop [eax + IP_SOCKET.RemoteIP] pop [eax + IP_SOCKET.RemoteIP]
mov [eax + TCP_SOCKET.t_state], TCB_LISTEN mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN
jmp .not_uni_xfer jmp .not_uni_xfer
@ -213,7 +213,7 @@ TCP_input:
DEBUGF 1,"Segment has options\n" DEBUGF 1,"Segment has options\n"
cmp [ebx + TCP_SOCKET.t_state], TCB_LISTEN ; no options when in listen state cmp [ebx + TCP_SOCKET.t_state], TCPS_LISTEN ; no options when in listen state
jz .not_uni_xfer ; also no header prediction jz .not_uni_xfer ; also no header prediction
lea edi, [edx + TCP_segment.Data] lea edi, [edx + TCP_segment.Data]
@ -310,7 +310,7 @@ TCP_input:
; - If the length is not 0 and the ACK didn't move, we're the receiver side of the transfer. ; - 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 ; 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], TCB_ESTABLISHED cmp [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
jnz .not_uni_xfer jnz .not_uni_xfer
test [edx + TCP_segment.Flags], TH_SYN + TH_FIN + TH_RST + TH_URG test [edx + TCP_segment.Flags], TH_SYN + TH_FIN + TH_RST + TH_URG
@ -438,10 +438,10 @@ TCP_input:
;;;; TODO: 444 ;;;; TODO: 444
cmp [ebx + TCP_SOCKET.t_state], TCB_LISTEN cmp [ebx + TCP_SOCKET.t_state], TCPS_LISTEN
je .LISTEN je .LISTEN
cmp [ebx + TCP_SOCKET.t_state], TCB_SYN_SENT cmp [ebx + TCP_SOCKET.t_state], TCPS_SYN_SENT
je .SYN_SENT je .SYN_SENT
jmp .NOT_LISTEN_OR_SYN_SENT jmp .NOT_LISTEN_OR_SYN_SENT
@ -481,7 +481,7 @@ align 4
TCP_sendseqinit eax TCP_sendseqinit eax
TCP_rcvseqinit eax TCP_rcvseqinit eax
mov [eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED mov [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
mov [eax + TCP_SOCKET.t_flags], TF_ACKNOW mov [eax + TCP_SOCKET.t_flags], TF_ACKNOW
mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval ;;;; macro mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval ;;;; macro
@ -578,7 +578,7 @@ align 4
mov [ebx + SOCKET.state],1 ;;;; FIXME mov [ebx + SOCKET.state],1 ;;;; FIXME
mov [ebx + TCP_SOCKET.t_state], TCB_ESTABLISHED mov [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
;;; TODO: check if we should scale the connection (567-572) ;;; TODO: check if we should scale the connection (567-572)
;;; TODO: update RTT estimators ;;; TODO: update RTT estimators
@ -589,7 +589,7 @@ align 4
DEBUGF 1,"TCP: simultaneous open\n" DEBUGF 1,"TCP: simultaneous open\n"
; We have received a syn but no ACK, so we are having a simultaneous open.. ; We have received a syn but no ACK, so we are having a simultaneous open..
mov [ebx + TCP_SOCKET.t_state], TCB_SYN_RECEIVED mov [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
@ -747,7 +747,7 @@ align 4
cmp [ebx + SOCKET.PID], 0 cmp [ebx + SOCKET.PID], 0
jg @f jg @f
cmp [ebx + TCP_SOCKET.t_state], TCB_CLOSE_WAIT cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
jle @f jle @f
test ecx, ecx test ecx, ecx
@ -811,44 +811,43 @@ align 4
jmp dword [eax + .rst_sw_list] jmp dword [eax + .rst_sw_list]
.rst_sw_list: .rst_sw_list:
dd .rst_skip ;TCB_CLOSED dd .rst_skip ;TCPS_CLOSED
dd .rst_skip ;TCB_LISTEN dd .rst_skip ;TCPS_LISTEN
dd .rst_skip ;TCB_SYN_SENT dd .rst_skip ;TCPS_SYN_SENT
dd .econnrefused ;TCB_SYN_RECEIVED dd .econnrefused ;TCPS_SYN_RECEIVED
dd .econnreset ;TCB_ESTABLISHED dd .econnreset ;TCPS_ESTABLISHED
dd .econnreset ;TCB_CLOSE_WAIT dd .econnreset ;TCPS_CLOSE_WAIT
dd .econnreset ;TCB_FIN_WAIT_1 dd .econnreset ;TCPS_FIN_WAIT_1
dd .rst_close ;TCB_CLOSING dd .rst_close ;TCPS_CLOSING
dd .rst_close ;TCB_LAST_ACK dd .rst_close ;TCPS_LAST_ACK
dd .econnreset ;TCB_FIN_WAIT_2 dd .econnreset ;TCPS_FIN_WAIT_2
dd .rst_close ;TCB_TIMED_WAIT dd .rst_close ;TCPS_TIMED_WAIT
.econnrefused: .econnrefused:
DEBUGF 1,"Connection refused" DEBUGF 1,"Connection refused"
;;; TODO: debug info mov [ebx + SOCKET.errorcode], ECONNREFUSED
jmp .close jmp .close
.econnreset: .econnreset:
DEBUGF 1,"Connection reset" DEBUGF 1,"Connection reset"
;;; TODO: debug info mov [ebx + SOCKET.errorcode], ECONNRESET
.close: .close:
DEBUGF 1,"Closing connection" DEBUGF 1,"Closing connection"
;;; update stats mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
;;; TODO: update stats
mov eax, ebx
call TCP_close
jmp .drop
.rst_close: .rst_close:
DEBUGF 1,"Closing with reset\n" DEBUGF 1,"Closing with reset\n"
;;; Close the socket mov eax, ebx
call TCP_close
jmp .drop jmp .drop
@ -866,9 +865,9 @@ align 4
test [edx + TCP_segment.Flags], TH_SYN test [edx + TCP_segment.Flags], TH_SYN
jz @f jz @f
mov eax, ebx
mov ebx, ECONNRESET mov ebx, ECONNRESET
call TCP_drop call TCP_drop
jmp .drop_with_reset jmp .drop_with_reset
test [edx + TCP_segment.Flags], TH_ACK test [edx + TCP_segment.Flags], TH_ACK
@ -884,12 +883,39 @@ align 4
;--------------- ;---------------
; ACK processing ; ACK processing
cmp [ebx + TCP_SOCKET.t_state], TCB_SYN_RECEIVED cmp [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
jnz .no_syn_rcv jnz .no_syn_rcv
DEBUGF 1,"TCP state = syn received\n" DEBUGF 1,"TCP state = syn received\n"
;;;;; 801-815 mov eax, [edx + TCP_segment.AckNumber]
cmp eax, [ebx + TCP_SOCKET.SND_UNA]
jg .drop_with_reset
cmp [ebx + TCP_SOCKET.SND_MAX], eax
jg .drop_with_reset
;;; update stats
mov eax, ebx
call SOCKET_is_connected
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
;;; 810-811
@@:
;;; 813 ?
mov eax, [edx + TCP_segment.SequenceNumber]
dec eax
mov [ebx + TCP_SOCKET.SND_WL1], eax
jmp .not_dup_ack
.no_syn_rcv: .no_syn_rcv:
@ -1095,7 +1121,7 @@ align 4
mov eax, ebx mov eax, ebx
call SOCKET_notify_owner call SOCKET_notify_owner
; Update TCB ; Update TCPS
mov eax, [edx + TCP_segment.AckNumber] mov eax, [edx + TCP_segment.AckNumber]
mov [ebx + TCP_SOCKET.SND_UNA], eax mov [ebx + TCP_SOCKET.SND_UNA], eax
@ -1118,17 +1144,17 @@ align 4
jmp dword [eax*4 + .ACK_sw_list] jmp dword [eax*4 + .ACK_sw_list]
.ACK_sw_list: .ACK_sw_list:
dd .ack_processed ;TCB_CLOSED dd .ack_processed ;TCPS_CLOSED
dd .ack_processed ;TCB_LISTEN dd .ack_processed ;TCPS_LISTEN
dd .ack_processed ;TCB_SYN_SENT dd .ack_processed ;TCPS_SYN_SENT
dd .ack_processed ;TCB_SYN_RECEIVED dd .ack_processed ;TCPS_SYN_RECEIVED
dd .ack_processed ;TCB_ESTABLISHED dd .ack_processed ;TCPS_ESTABLISHED
dd .ack_processed ;TCB_CLOSE_WAIT dd .ack_processed ;TCPS_CLOSE_WAIT
dd .ack_fw1 ;TCB_FIN_WAIT_1 dd .ack_fw1 ;TCPS_FIN_WAIT_1
dd .ack_c ;TCB_CLOSING dd .ack_c ;TCPS_CLOSING
dd .ack_la ;TCB_LAST_ACK dd .ack_la ;TCPS_LAST_ACK
dd .ack_processed ;TCB_FIN_WAIT_2 dd .ack_processed ;TCPS_FIN_WAIT_2
dd .ack_tw ;TCB_TIMED_WAIT dd .ack_tw ;TCPS_TIMED_WAIT
.ack_fw1: .ack_fw1:
@ -1141,14 +1167,14 @@ align 4
;;; mov [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle ;;; mov [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle
@@: @@:
mov [ebx + TCP_SOCKET.t_state], TCB_FIN_WAIT_2 mov [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
jmp .ack_processed jmp .ack_processed
.ack_c: .ack_c:
jz .ack_processed jz .ack_processed
mov [ebx + TCP_SOCKET.t_state], TCB_TIMED_WAIT mov [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
mov eax, ebx mov eax, ebx
call TCP_cancel_timers call TCP_cancel_timers
mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
@ -1262,7 +1288,7 @@ align 4
cmp [edx + TCP_segment.UrgentPointer], 0 cmp [edx + TCP_segment.UrgentPointer], 0
jz .not_urgent jz .not_urgent
cmp [ebx + TCP_SOCKET.t_state], TCB_TIMED_WAIT cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
je .not_urgent je .not_urgent
; Ignore bogus urgent offsets ; Ignore bogus urgent offsets
@ -1301,7 +1327,7 @@ align 4
test [edx + TCP_segment.Flags], TH_FIN test [edx + TCP_segment.Flags], TH_FIN
jnz .process_fin jnz .process_fin
cmp [ebx + TCP_SOCKET.t_state], TCB_FIN_WAIT_1 cmp [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_1
jge .dont_do_data jge .dont_do_data
test ecx, ecx test ecx, ecx
@ -1344,11 +1370,11 @@ align 4
DEBUGF 1,"Processing FIN\n" DEBUGF 1,"Processing FIN\n"
cmp [ebx + TCP_SOCKET.t_state], TCB_CLOSE_WAIT cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
je .not_first_fin je .not_first_fin
cmp [ebx + TCP_SOCKET.t_state], TCB_CLOSING cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSING
je .not_first_fin je .not_first_fin
cmp [ebx + TCP_SOCKET.t_state], TCB_FIN_WAIT_2 cmp [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
je .not_first_fin je .not_first_fin
DEBUGF 1,"First FIN for this connection\n" DEBUGF 1,"First FIN for this connection\n"
@ -1365,31 +1391,31 @@ align 4
jmp dword [eax + .FIN_sw_list] jmp dword [eax + .FIN_sw_list]
.FIN_sw_list: .FIN_sw_list:
dd .no_fin ;TCB_CLOSED dd .no_fin ;TCPS_CLOSED
dd .no_fin ;TCB_LISTEN dd .no_fin ;TCPS_LISTEN
dd .no_fin ;TCB_SYN_SENT dd .no_fin ;TCPS_SYN_SENT
dd .fin_syn_est ;TCB_SYN_RECEIVED dd .fin_syn_est ;TCPS_SYN_RECEIVED
dd .fin_syn_est ;TCB_ESTABLISHED dd .fin_syn_est ;TCPS_ESTABLISHED
dd .no_fin ;TCB_CLOSE_WAIT dd .no_fin ;TCPS_CLOSE_WAIT
dd .fin_wait1 ;TCB_FIN_WAIT_1 dd .fin_wait1 ;TCPS_FIN_WAIT_1
dd .no_fin ;TCB_CLOSING dd .no_fin ;TCPS_CLOSING
dd .no_fin ;TCB_LAST_ACK dd .no_fin ;TCPS_LAST_ACK
dd .fin_wait2 ;TCB_FIN_WAIT_2 dd .fin_wait2 ;TCPS_FIN_WAIT_2
dd .fin_timed ;TCB_TIMED_WAIT dd .fin_timed ;TCPS_TIMED_WAIT
.fin_syn_est: .fin_syn_est:
mov [ebx + TCP_SOCKET.t_state], TCB_CLOSE_WAIT mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
jmp .no_fin jmp .no_fin
.fin_wait1: .fin_wait1:
mov [ebx + TCP_SOCKET.t_state], TCB_CLOSING mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSING
jmp .no_fin jmp .no_fin
.fin_wait2: .fin_wait2:
mov [ebx + TCP_SOCKET.t_state], TCB_TIMED_WAIT mov [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
mov eax, ebx mov eax, ebx
call TCP_cancel_timers call TCP_cancel_timers
mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL

View File

@ -130,10 +130,10 @@ TCP_drop:
DEBUGF 1,"TCP_drop\n" DEBUGF 1,"TCP_drop\n"
cmp [eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED cmp [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
jl .no_syn_received jl .no_syn_received
mov [eax + TCP_SOCKET.t_state], TCB_CLOSED mov [eax + TCP_SOCKET.t_state], TCPS_CLOSED
call TCP_output call TCP_output
@ -209,17 +209,17 @@ TCP_outflags:
.flaglist: .flaglist:
db TH_RST + TH_ACK ; TCB_CLOSED db TH_RST + TH_ACK ; TCPS_CLOSED
db 0 ; TCB_LISTEN db 0 ; TCPS_LISTEN
db TH_SYN ; TCB_SYN_SENT db TH_SYN ; TCPS_SYN_SENT
db TH_SYN + TH_ACK ; TCB_SYN_RECEIVED db TH_SYN + TH_ACK ; TCPS_SYN_RECEIVED
db TH_ACK ; TCB_ESTABLISHED db TH_ACK ; TCPS_ESTABLISHED
db TH_ACK ; TCB_CLOSE_WAIT db TH_ACK ; TCPS_CLOSE_WAIT
db TH_SYN + TH_ACK ; TCB_FIN_WAIT_1 db TH_SYN + TH_ACK ; TCPS_FIN_WAIT_1
db TH_SYN + TH_ACK ; TCB_CLOSING db TH_SYN + TH_ACK ; TCPS_CLOSING
db TH_SYN + TH_ACK ; TCB_LAST_ACK db TH_SYN + TH_ACK ; TCPS_LAST_ACK
db TH_ACK ; TCB_FIN_WAIT_2 db TH_ACK ; TCPS_FIN_WAIT_2
db TH_ACK ; TCB_TIMED_WAIT db TH_ACK ; TCPS_TIMED_WAIT