2009-10-05 22:47:27 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;;
|
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; TCP.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the tcp/ip network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
TCB_LISTEN equ 1
|
|
|
|
TCB_SYN_SENT equ 2
|
|
|
|
TCB_SYN_RECEIVED equ 3
|
|
|
|
TCB_ESTABLISHED equ 4
|
|
|
|
TCB_FIN_WAIT_1 equ 5
|
|
|
|
TCB_FIN_WAIT_2 equ 6
|
|
|
|
TCB_CLOSE_WAIT equ 7
|
|
|
|
TCB_CLOSING equ 8
|
|
|
|
TCB_LAST_ACK equ 9
|
|
|
|
TCB_TIMED_WAIT equ 10
|
|
|
|
TCB_CLOSED equ 11
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
TH_FIN equ 1 shl 0
|
|
|
|
TH_SYN equ 1 shl 1
|
|
|
|
TH_RST equ 1 shl 2
|
|
|
|
TH_PUSH equ 1 shl 3
|
|
|
|
TH_ACK equ 1 shl 4
|
|
|
|
TH_URG equ 1 shl 5
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
TWOMSL equ 10 ; # of secs to wait before closing socket
|
|
|
|
|
|
|
|
TCP_RETRIES equ 5 ; Number of times to resend a Packet
|
2009-11-06 21:45:08 +01:00
|
|
|
TCP_TIMEOUT equ 10 ; resend if not replied to in 1/100 s
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
TCP_QUEUE_SIZE equ 16
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
struct TCP_Packet
|
|
|
|
.SourcePort dw ?
|
|
|
|
.DestinationPort dw ?
|
|
|
|
.SequenceNumber dd ?
|
|
|
|
.AckNumber dd ?
|
2009-10-05 22:47:27 +02:00
|
|
|
.DataOffset db ? ; DataOffset[0-3 bits] and Reserved[4-7]
|
|
|
|
.Flags db ? ; Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN
|
2009-09-17 13:55:38 +02:00
|
|
|
.Window dw ?
|
|
|
|
.Checksum dw ?
|
|
|
|
.UrgentPointer dw ?
|
|
|
|
.Options rb 3
|
|
|
|
.Padding db ?
|
|
|
|
.Data:
|
|
|
|
ends
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
TCP_PACKETS_TX rd MAX_IP
|
|
|
|
TCP_PACKETS_RX rd MAX_IP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
TCP_IN_QUEUE rd (tcp_in_queue_entry.size*TCP_QUEUE_SIZE+queue.data)/4
|
|
|
|
TCP_OUT_QUEUE dd ?
|
|
|
|
rd (tcp_out_queue_entry.size*TCP_QUEUE_SIZE)/4
|
2009-10-05 22:47:27 +02:00
|
|
|
endg
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
iglobal
|
|
|
|
|
|
|
|
TCBStateHandler:
|
|
|
|
|
|
|
|
dd stateTCB_LISTEN
|
|
|
|
dd stateTCB_SYN_SENT
|
|
|
|
dd stateTCB_SYN_RECEIVED
|
|
|
|
dd stateTCB_ESTABLISHED
|
|
|
|
dd stateTCB_FIN_WAIT_1
|
|
|
|
dd stateTCB_FIN_WAIT_2
|
|
|
|
dd stateTCB_CLOSE_WAIT
|
|
|
|
dd stateTCB_CLOSING
|
|
|
|
dd stateTCB_LAST_ACK
|
|
|
|
dd stateTCB_TIME_WAIT
|
|
|
|
dd stateTCB_CLOSED
|
|
|
|
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
macro inc_INET reg {
|
|
|
|
|
|
|
|
inc byte [reg + 0]
|
|
|
|
adc byte [reg + 1], 0
|
|
|
|
adc byte [reg + 2], 0
|
|
|
|
adc byte [reg + 3], 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
macro add_INET reg {
|
|
|
|
|
|
|
|
rol ecx, 16
|
|
|
|
adc byte [reg + 0], ch
|
|
|
|
adc byte [reg + 1], cl
|
|
|
|
rol ecx, 16
|
|
|
|
adc byte [reg + 2], ch
|
|
|
|
adc byte [reg + 3], cl
|
|
|
|
|
|
|
|
}
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; TCP_init
|
|
|
|
;
|
|
|
|
; This function resets all TCP variables
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
TCP_init:
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
mov edi, TCP_PACKETS_TX
|
|
|
|
mov ecx, 2*MAX_IP
|
|
|
|
rep stosd
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
init_queue TCP_IN_QUEUE
|
|
|
|
init_queue TCP_OUT_QUEUE
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; TCP_decrease_socket_ttls
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-10-05 22:47:27 +02:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
align 4
|
2009-11-06 21:45:08 +01:00
|
|
|
TCP_decrease_socket_ttls:
|
2009-09-17 13:55:38 +02:00
|
|
|
; scan through all the sockets, decrementing active timers
|
|
|
|
|
|
|
|
mov ebx, net_sockets
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.NextPtr], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
je .exit
|
|
|
|
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz .exit
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.Type], IP_PROTO_TCP
|
|
|
|
jne .next_socket
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
; DEBUGF 1, "K : %x-%x: %x-%x-%x-%u\n", [ebx + SOCKET.PID]:2, [ebx + SOCKET.Number]:2, [ebx + SOCKET.LocalPort]:4, [ebx + SOCKET.RemoteIP], [ebx + SOCKET.RemotePort]:4, [ebx + SOCKET.TCBState]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBTimer], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .decrement_tcb
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .decrement_wnd
|
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.decrement_tcb:
|
|
|
|
; decrement it, delete socket if TCB timer = 0 & socket in timewait state
|
2009-11-06 21:45:08 +01:00
|
|
|
dec [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBTimer]
|
2009-09-17 13:55:38 +02:00
|
|
|
jnz .next_socket
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_TIMED_WAIT
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [ebx + SOCKET_head.PrevPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
stdcall net_socket_free, ebx
|
|
|
|
pop ebx
|
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.decrement_wnd:
|
|
|
|
; TODO - prove it works!
|
2009-11-06 21:45:08 +01:00
|
|
|
dec [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer]
|
2009-09-17 13:55:38 +02:00
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; TCP_send_queued:
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; Decreases 'ttl' of tcp packets queued.
|
|
|
|
; if 'ttl' reaches 0, resend the packet and decrease 'retries'
|
|
|
|
; if 'retries' reaches zero, remove the queued packet
|
2009-10-05 22:47:27 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; IN: /
|
2009-10-05 22:47:27 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
TCP_send_queued:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [TCP_OUT_QUEUE], 0
|
|
|
|
je .exit
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov eax, TCP_QUEUE_SIZE
|
|
|
|
mov ecx, [TCP_OUT_QUEUE]
|
|
|
|
mov esi, TCP_OUT_QUEUE+4
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.loop:
|
|
|
|
cmp [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
jnz .found_one
|
|
|
|
add esi, tcp_out_queue_entry.size
|
|
|
|
loop .loop
|
|
|
|
.exit:
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.found_one:
|
|
|
|
dec [esi + tcp_out_queue_entry.ttl]
|
|
|
|
jz .send_it
|
|
|
|
.find_next:
|
|
|
|
dec eax
|
|
|
|
jz .exit
|
|
|
|
jmp .loop
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.send_it:
|
|
|
|
push eax ecx esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [esi + tcp_out_queue_entry.data_size]
|
|
|
|
push [esi + tcp_out_queue_entry.data_ptr]
|
|
|
|
mov ebx, [esi + tcp_out_queue_entry.owner]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
call [esi + tcp_out_queue_entry.sendproc]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
pop esi ecx eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
dec [esi + tcp_out_queue_entry.retries]
|
|
|
|
jz .remove_it
|
|
|
|
mov [esi + tcp_out_queue_entry.ttl], TCP_TIMEOUT
|
|
|
|
jmp .find_next
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.remove_it:
|
|
|
|
push [esi + tcp_out_queue_entry.data_ptr]
|
|
|
|
mov [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
dec [TCP_OUT_QUEUE]
|
|
|
|
call kernel_free
|
|
|
|
jmp .find_next
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; TCP_add_to_queue:
|
|
|
|
;
|
|
|
|
; Queue a TCP packet for sending
|
|
|
|
;
|
|
|
|
; IN: [esp] pointer to buffer
|
|
|
|
; [esp + 4] size of buffer
|
|
|
|
; ebx = driver struct
|
|
|
|
; esi = sender proc
|
|
|
|
; edx = acknum
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
TCP_add_to_queue:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [TCP_OUT_QUEUE], TCP_QUEUE_SIZE
|
|
|
|
jge .full
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ecx, TCP_QUEUE_SIZE
|
|
|
|
mov eax, TCP_OUT_QUEUE+4
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.loop:
|
|
|
|
cmp [eax + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
je .found_it
|
|
|
|
add eax, tcp_out_queue_entry.size
|
|
|
|
loop .loop
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.full: ; silently discard the packet
|
|
|
|
call kernel_free
|
|
|
|
add esp, 4
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.found_it: ; eax point to empty queue entry
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
pop [eax + tcp_out_queue_entry.data_ptr]
|
|
|
|
pop [eax + tcp_out_queue_entry.data_size]
|
|
|
|
mov [eax + tcp_out_queue_entry.ttl], 1 ; send immediately
|
|
|
|
mov [eax + tcp_out_queue_entry.retries], TCP_RETRIES
|
|
|
|
mov [eax + tcp_out_queue_entry.owner], ebx
|
|
|
|
mov [eax + tcp_out_queue_entry.sendproc], esi
|
|
|
|
mov [eax + tcp_out_queue_entry.ack_num], edx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; TCP_handler:
|
|
|
|
;
|
|
|
|
; Called by IPv4_handler,
|
|
|
|
; this procedure will inject the tcp data diagrams in the application sockets.
|
|
|
|
;
|
|
|
|
; IN: Pointer to buffer in [esp]
|
|
|
|
; size of buffer in [esp+4]
|
|
|
|
; pointer to device struct in ebx
|
|
|
|
; TCP Packet size in ecx
|
|
|
|
; pointer to TCP Packet data in edx
|
|
|
|
; SourceAddres in esi
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
TCP_handler :
|
|
|
|
|
|
|
|
DEBUGF 1,"TCP_Handler\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; IP Packet TCP Destination Port = local Port
|
2009-11-06 21:45:08 +01:00
|
|
|
; IP Packet SA = Remote IP OR = 0
|
|
|
|
; IP Packet TCP Source Port = remote Port OR = 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov ebx, net_sockets
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.socket_loop:
|
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
2009-11-06 21:45:08 +01:00
|
|
|
jz .dump
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ax, [edx + TCP_Packet.DestinationPort]
|
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], ax
|
|
|
|
jne .socket_loop
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov eax, [ebx + SOCKET_head.end + IPv4_SOCKET.RemoteIP]
|
|
|
|
cmp eax, esi
|
|
|
|
je @f
|
|
|
|
test eax, eax
|
|
|
|
jne .socket_loop
|
|
|
|
@@:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort]
|
|
|
|
cmp [edx + TCP_Packet.SourcePort] , ax
|
|
|
|
je .change_state
|
|
|
|
test ax, ax
|
|
|
|
jne .socket_loop
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.change_state:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push ebx
|
|
|
|
lea ebx, [ebx + SOCKET_head.lock]
|
|
|
|
call wait_mutex
|
|
|
|
pop ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
;----------------------------------
|
|
|
|
; ebx is pointer to socket
|
|
|
|
; ecx is size of tcp packet
|
|
|
|
; edx is pointer to tcp packet
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; as a Packet has been received, update the TCB timer
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBTimer], TWOMSL
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; If the received Packet has an ACK bit set, remove any Packets in the resend queue that this received Packet acknowledges
|
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
|
|
|
jz .call_handler ; No ACK, so no data yet
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov eax, [edx + TCP_Packet.SequenceNumber] ; Calculate sequencenumber in eax
|
|
|
|
bswap eax ;
|
|
|
|
add eax, ecx ;
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [TCP_OUT_QUEUE], 0
|
|
|
|
je .call_handler
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push ecx
|
|
|
|
mov ecx, TCP_QUEUE_SIZE
|
|
|
|
mov esi, TCP_OUT_QUEUE+4
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
cmp [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
jne .maybe_next
|
|
|
|
cmp [esi + tcp_out_queue_entry.ack_num], eax
|
|
|
|
jg .maybe_next
|
|
|
|
|
|
|
|
push [esi + tcp_out_queue_entry.data_ptr]
|
|
|
|
mov [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
dec [TCP_OUT_QUEUE]
|
|
|
|
call kernel_free
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.maybe_next:
|
|
|
|
add esi, tcp_out_queue_entry.size
|
|
|
|
loop .loop
|
|
|
|
pop ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.call_handler:
|
|
|
|
; Call handler for given TCB state
|
|
|
|
mov eax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState]
|
|
|
|
cmp eax, TCB_LISTEN
|
|
|
|
jb .exit
|
|
|
|
cmp eax, TCB_CLOSED
|
|
|
|
ja .exit
|
|
|
|
|
|
|
|
shl eax, 2
|
|
|
|
add eax, TCBStateHandler - 4
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push .exit
|
|
|
|
jmp eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.dump:
|
|
|
|
DEBUGF 1,"Dumping TCP packet\n"
|
2009-10-05 22:47:27 +02:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4 ; pop (balance stack)
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; TCP_socket_send
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; IN: eax = socket pointer
|
|
|
|
; ecx = number of bytes to send
|
|
|
|
; esi = pointer to data
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
TCP_socket_send:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Creating TCP Packet\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov di , IP_PROTO_TCP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; Create an IPv4 Packet of the correct size
|
2009-09-17 13:55:38 +02:00
|
|
|
push eax
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [eax + SOCKET_head.end + IPv4_SOCKET.LocalIP]
|
|
|
|
mov eax, [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; meanwhile, create the pseudoheader in stack,
|
|
|
|
; (now that we still have all the variables that are needed.)
|
|
|
|
push cx
|
|
|
|
push di
|
|
|
|
push eax
|
|
|
|
push ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push ecx esi eax ; save some variables for later
|
|
|
|
add ecx, TCP_Packet.Data
|
|
|
|
call IPv4_create_packet
|
|
|
|
cmp edi, -1
|
|
|
|
je .fail
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
pop esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; Now add the TCP header to the IPv4 packet
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
pop [edi + TCP_Packet.SequenceNumber]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push dword [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort]
|
|
|
|
pop dword [edi + TCP_Packet.SourcePort]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
pop [edi + TCP_Packet.AckNumber]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov al, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.flags]
|
|
|
|
mov [edi + TCP_Packet.Flags], al
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [edi + TCP_Packet.Window], 0x0005 ; 1280 bytes ;;; TODO: read RFC !
|
|
|
|
mov [edi + TCP_Packet.UrgentPointer], 0
|
|
|
|
mov [edi + TCP_Packet.DataOffset], 0x50
|
|
|
|
mov [edi + TCP_Packet.Checksum], 0
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; Copy the data
|
|
|
|
mov esi, [esp]
|
|
|
|
mov ecx, [esp+4]
|
|
|
|
add edi, TCP_Packet.Data
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb: shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
|
|
|
.nw: rep movsd
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; Now, calculate the checksum for pseudoheader
|
|
|
|
xor edx, edx
|
|
|
|
mov ecx, 12
|
|
|
|
mov esi, esp
|
|
|
|
call checksum_1
|
|
|
|
add esp, 12 ; remove the pseudoheader from stack
|
|
|
|
; And that of the data
|
|
|
|
pop esi
|
|
|
|
pop ecx
|
|
|
|
call checksum_1
|
|
|
|
; Now create the final checksum and store it in TCP header
|
|
|
|
call checksum_2
|
|
|
|
mov [edi + TCP_Packet.Checksum], dx
|
|
|
|
|
|
|
|
; And now, send it!
|
|
|
|
DEBUGF 1,"Sending TCP Packet to device %x\n", ebx
|
|
|
|
mov esi, ETH_sender
|
|
|
|
mov edx, [edi + TCP_Packet.AckNumber]
|
|
|
|
jmp TCP_add_to_queue
|
|
|
|
|
|
|
|
.fail:
|
|
|
|
add esp, 12+4
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; IN: eax = socket pointer
|
|
|
|
; bl = flags
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
TCP_send_ack:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Creating TCP ACK\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov di , IP_PROTO_TCP
|
|
|
|
mov cx , TCP_Packet.Data
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push bx eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; Create an IPv4 Packet of the correct size
|
|
|
|
mov ebx, [eax + SOCKET_head.end + IPv4_SOCKET.LocalIP]
|
|
|
|
mov eax, [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
call IPv4_create_packet
|
|
|
|
cmp edi, -1
|
|
|
|
je .fail
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
; Fill in the TCP header
|
|
|
|
pop esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
pop [edi + TCP_Packet.SequenceNumber]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push dword [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort]
|
|
|
|
pop dword [edi + TCP_Packet.SourcePort]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
pop [edi + TCP_Packet.AckNumber]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
pop cx
|
|
|
|
mov [edi + TCP_Packet.Flags], cl
|
|
|
|
mov [edi + TCP_Packet.Window], 0x0005 ; 1280 bytes
|
|
|
|
mov [edi + TCP_Packet.UrgentPointer], 0
|
|
|
|
mov [edi + TCP_Packet.DataOffset], 0x50
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push eax edx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push word TCP_Packet.Data shl 8
|
|
|
|
push IP_PROTO_TCP
|
|
|
|
push [esi + SOCKET_head.end + SOCKET_head.end + IPv4_SOCKET.RemoteIP]
|
|
|
|
push [esi + SOCKET_head.end + SOCKET_head.end + IPv4_SOCKET.LocalIP]
|
|
|
|
|
|
|
|
; Now, calculate the checksum for pseudoheader
|
|
|
|
xor edx, edx
|
|
|
|
mov ecx, 12
|
|
|
|
mov esi, esp
|
|
|
|
call checksum_1
|
|
|
|
add esp, 12 ; remove the pseudoheader from stack
|
|
|
|
; Now create the final checksum and store it in TCP header
|
|
|
|
call checksum_2
|
|
|
|
mov [edi + TCP_Packet.Checksum], dx
|
|
|
|
|
|
|
|
; And now, send it!
|
|
|
|
DEBUGF 1,"Sending TCP Packet to device %x\n", ebx
|
|
|
|
mov esi, ETH_sender
|
|
|
|
mov edx, [edi + TCP_Packet.AckNumber]
|
|
|
|
jmp TCP_add_to_queue
|
|
|
|
|
|
|
|
.fail:
|
|
|
|
add esp, 12+4
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
stateTCB_LISTEN:
|
2009-09-17 13:55:38 +02:00
|
|
|
; In this case, we are expecting a SYN Packet
|
|
|
|
; For now, if the Packet is a SYN, process it, and send a response
|
|
|
|
; If not, ignore it
|
|
|
|
|
|
|
|
; Look at control flags
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_SYN
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; We have a SYN. update the socket with this IP Packets details,
|
|
|
|
; And send a response
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.RemoteIP], esi ; IP source address
|
|
|
|
mov ax, [edx + TCP_Packet.SourcePort]
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort], ax
|
|
|
|
mov eax, [edx + TCP_Packet.SequenceNumber]
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.IRS], eax
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT], eax
|
|
|
|
lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
inc_INET esi ; RCV.NXT
|
|
|
|
mov eax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.ISS]
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT], eax
|
|
|
|
|
|
|
|
; Now construct the response
|
2009-09-17 13:55:38 +02:00
|
|
|
mov bl, TH_SYN + TH_ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
call TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_SYN_RECEIVED
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; increment SND.NXT in socket
|
2009-11-06 21:45:08 +01:00
|
|
|
lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
inc_INET esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
stateTCB_SYN_SENT:
|
2009-09-17 13:55:38 +02:00
|
|
|
; We are awaiting an ACK to our SYN, with a SYM
|
|
|
|
; Look at control flags - expecting an ACK
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov al, [edx + TCP_Packet.Flags]
|
2009-09-17 13:55:38 +02:00
|
|
|
and al, TH_SYN + TH_ACK
|
|
|
|
cmp al, TH_SYN + TH_ACK
|
|
|
|
je .syn_ack
|
|
|
|
|
|
|
|
test al, TH_SYN
|
|
|
|
jz .exit
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_SYN_RECEIVED
|
2009-09-17 13:55:38 +02:00
|
|
|
push TH_SYN + TH_ACK
|
|
|
|
jmp .send
|
|
|
|
|
|
|
|
.syn_ack:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_ESTABLISHED
|
2009-09-17 13:55:38 +02:00
|
|
|
push TH_ACK
|
|
|
|
|
|
|
|
.send:
|
|
|
|
; Store the recv.nxt field
|
2009-11-06 21:45:08 +01:00
|
|
|
mov eax, [edx + TCP_Packet.SequenceNumber]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; Update our recv.nxt field
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT], eax
|
|
|
|
lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
inc_INET esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; Send an ACK
|
|
|
|
pop ebx
|
2009-11-06 21:45:08 +01:00
|
|
|
call TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_SYN_RECEIVED:
|
2009-09-17 13:55:38 +02:00
|
|
|
; In this case, we are expecting an ACK Packet
|
|
|
|
; For now, if the Packet is an ACK, process it,
|
|
|
|
; If not, ignore it
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_RST
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .check_ack
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.OrigRemotePort]
|
|
|
|
pop [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort]
|
|
|
|
push [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.OrigRemoteIP]
|
|
|
|
pop [ebx + SOCKET_head.end + IPv4_SOCKET.RemoteIP]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_LISTEN
|
2009-09-17 13:55:38 +02:00
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.check_ack:
|
|
|
|
; Look at control flags - expecting an ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .exit
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_ESTABLISHED
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_ESTABLISHED:
|
2009-09-17 13:55:38 +02:00
|
|
|
; Here we are expecting data, or a request to close
|
|
|
|
; OR both...
|
|
|
|
|
|
|
|
; Did we receive a FIN or RST?
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_FIN
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .check_ack
|
|
|
|
|
|
|
|
; It was a fin or reset.
|
|
|
|
|
|
|
|
; Remove resend entries from the queue - I dont want to send any more data
|
2009-11-06 21:45:08 +01:00
|
|
|
; Send an ACK to that fin, and enter closewait state
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.check_ack:
|
|
|
|
; Check that we received an ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; First, look at the incoming window. If this is less than or equal to 1024,
|
|
|
|
; Set the socket window timer to 1. This will stop an additional Packets being queued.
|
|
|
|
; ** I may need to tweak this value, since I do not know how many Packets are already queued
|
2009-11-06 21:45:08 +01:00
|
|
|
mov cx, [edx + TCP_Packet.Window]
|
2009-09-17 13:55:38 +02:00
|
|
|
xchg cl, ch
|
|
|
|
cmp cx, 1024
|
|
|
|
ja @f
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
@@: ; OK, here is the deal
|
|
|
|
; My recv.nct field holds the seq of the expected next rec byte
|
|
|
|
; if the recevied sequence number is not equal to this, do not
|
|
|
|
; increment the recv.nxt field, do not copy data - just send a
|
|
|
|
; repeat ack.
|
|
|
|
|
|
|
|
; recv.nxt is in dword [edx+24], in inet format
|
|
|
|
; recv seq is in [sktAddr]+56, in inet format
|
|
|
|
; just do a comparision
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ecx, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_CLOSE_WAIT
|
2009-09-17 13:55:38 +02:00
|
|
|
jne @f
|
|
|
|
mov ecx, eax
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
@@: cmp ecx, [edx + TCP_Packet.SequenceNumber]
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .ack
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
test ecx, ecx
|
|
|
|
jnz .data
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; If we had received a fin, we need to ACK it.
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_CLOSE_WAIT
|
2009-09-17 13:55:38 +02:00
|
|
|
je .ack
|
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.data:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov esi, [esp + 4]
|
|
|
|
sub edx, esi
|
|
|
|
mov edi, edx
|
|
|
|
call socket_internal_receiver
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.ack:
|
|
|
|
; Send an ACK
|
|
|
|
mov bl, TH_ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
call TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_FIN_WAIT_1:
|
2009-09-17 13:55:38 +02:00
|
|
|
; We can either receive an ACK of a fin, or a fin
|
2009-11-06 21:45:08 +01:00
|
|
|
mov al, [edx + TCP_Packet.Flags]
|
2009-09-17 13:55:38 +02:00
|
|
|
and al, TH_FIN + TH_ACK
|
|
|
|
|
|
|
|
cmp al, TH_ACK
|
|
|
|
jne @f
|
|
|
|
|
|
|
|
; It was an ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_FIN_WAIT_2
|
2009-09-17 13:55:38 +02:00
|
|
|
jmp .exit
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
@@: mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_CLOSING
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp al, TH_FIN
|
|
|
|
je @f
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_TIMED_WAIT
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
@@: lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
inc_INET esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; Send an ACK
|
|
|
|
mov bl, TH_ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
call TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_FIN_WAIT_2:
|
|
|
|
test [edx + TCP_Packet.Flags], TH_FIN
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; Change state, as we have a fin
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_TIMED_WAIT
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
inc_INET esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; Send an ACK
|
|
|
|
mov bl, TH_ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
call TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_CLOSE_WAIT:
|
2009-09-17 13:55:38 +02:00
|
|
|
; Intentionally left empty
|
|
|
|
; socket_close_tcp handles this
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_CLOSING:
|
2009-09-17 13:55:38 +02:00
|
|
|
; We can either receive an ACK of a fin, or a fin
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .exit
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_TIMED_WAIT
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
stateTCB_LAST_ACK:
|
2009-09-17 13:55:38 +02:00
|
|
|
; Look at control flags - expecting an ACK
|
2009-11-06 21:45:08 +01:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; delete the socket
|
|
|
|
stdcall net_socket_free, ebx
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
stateTCB_TIME_WAIT:
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
|
|
|
stateTCB_CLOSED:
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|