2009-10-06 00:47:27 +04: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 15:55:38 +04:00
|
|
|
|
2009-10-12 18:39:59 +04:00
|
|
|
$Revision$
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-10-06 00:47:27 +04:00
|
|
|
TCP_RETRIES equ 5 ; Number of times to resend a Packet
|
2009-11-08 21:00:01 +03:00
|
|
|
TCP_PACKET_TTL equ 50 ; resend if not replied to in 1/100 s
|
|
|
|
TCP_SOCKET_TTL equ 10 ; # of secs to wait before closing socket
|
2009-10-06 00:47:27 +04:00
|
|
|
TCP_QUEUE_SIZE equ 16
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
struct TCP_Packet
|
|
|
|
.SourcePort dw ?
|
|
|
|
.DestinationPort dw ?
|
|
|
|
.SequenceNumber dd ?
|
|
|
|
.AckNumber dd ?
|
2009-10-06 00:47:27 +04: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 15:55:38 +04:00
|
|
|
.Window dw ?
|
|
|
|
.Checksum dw ?
|
|
|
|
.UrgentPointer dw ?
|
2009-11-16 00:44:17 +03:00
|
|
|
; .Options rb 3
|
|
|
|
; .Padding db ?
|
2009-09-17 15:55:38 +04:00
|
|
|
.Data:
|
|
|
|
ends
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
struct tcp_in_queue_entry
|
|
|
|
.data_ptr dd ?
|
|
|
|
.data_size dd ?
|
|
|
|
.offset dd ?
|
|
|
|
.size:
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct tcp_out_queue_entry
|
|
|
|
.data_ptr dd ?
|
|
|
|
.data_size dd ?
|
|
|
|
.ttl dd ?
|
|
|
|
.retries dd ?
|
|
|
|
.owner dd ?
|
|
|
|
.sendproc dd ?
|
|
|
|
.seq_num dd ?
|
|
|
|
.socket dd ?
|
|
|
|
.size:
|
|
|
|
ends
|
|
|
|
|
2009-10-06 00:47:27 +04:00
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
TCP_PACKETS_TX rd MAX_IP
|
|
|
|
TCP_PACKETS_RX rd MAX_IP
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
TCP_IN_QUEUE rd (tcp_in_queue_entry.size*TCP_QUEUE_SIZE+queue.data)/4
|
2009-11-08 21:00:01 +03:00
|
|
|
TCP_OUT_QUEUE dd ?
|
2009-11-06 23:45:08 +03:00
|
|
|
rd (tcp_out_queue_entry.size*TCP_QUEUE_SIZE)/4
|
2009-10-06 00:47:27 +04:00
|
|
|
endg
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
iglobal
|
2009-11-08 21:00:01 +03:00
|
|
|
stateHandler:
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2009-10-06 00:47:27 +04: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 23:45:08 +03:00
|
|
|
init_queue TCP_IN_QUEUE
|
2009-11-08 21:00:01 +03:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; tcp_out_queue is a special type of queue:
|
|
|
|
; The first dword is a counter of total packets queued.
|
|
|
|
; The remaining bytes are socket 'slots' wich use tcp_out_queue_entry data structure.
|
|
|
|
; An empty slot is know by the fact that tcp_out_queue_entry.data_ptr (first dword of the slot) is set to 0
|
|
|
|
; There are TCP_OUT_QUEUE_SIZE number of slots
|
2009-11-09 16:59:46 +03:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
xor eax, eax
|
|
|
|
mov esi, TCP_OUT_QUEUE
|
|
|
|
mov ecx, TCP_QUEUE_SIZE*tcp_out_queue_entry/4+1
|
|
|
|
rep stosd
|
2009-10-06 00:47:27 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
; TCP_decrease_socket_ttls
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-10-06 00:47:27 +04:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
2009-11-06 23:45:08 +03:00
|
|
|
TCP_decrease_socket_ttls:
|
2009-11-16 00:44:17 +03:00
|
|
|
; scan through all the sockets, decrementing active timers
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
mov ebx, net_sockets
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
cmp [ebx + SOCKET_head.NextPtr], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
je .exit
|
|
|
|
|
|
|
|
.next_socket:
|
2009-11-06 23:45:08 +03:00
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 15:55:38 +04:00
|
|
|
or ebx, ebx
|
|
|
|
jz .exit
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
cmp [ebx + SOCKET_head.Type], IP_PROTO_TCP
|
|
|
|
jne .next_socket
|
|
|
|
|
2009-11-08 21:00:01 +03: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.state]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.timer], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
jne .decrement_tcb
|
2009-11-06 23:45:08 +03:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
jne .decrement_wnd
|
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.decrement_tcb:
|
2009-11-16 00:44:17 +03:00
|
|
|
; decrement it, delete socket if TCB timer = 0 & socket in timewait state
|
2009-11-08 21:00:01 +03:00
|
|
|
dec [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.timer]
|
2009-09-17 15:55:38 +04:00
|
|
|
jnz .next_socket
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_TIMED_WAIT
|
2009-09-17 15:55:38 +04:00
|
|
|
jne .next_socket
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
push [ebx + SOCKET_head.PrevPtr]
|
2009-09-17 15:55:38 +04:00
|
|
|
stdcall net_socket_free, ebx
|
|
|
|
pop ebx
|
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.decrement_wnd:
|
2009-11-06 23:45:08 +03:00
|
|
|
dec [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer]
|
2009-09-17 15:55:38 +04:00
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-10-06 00:47:27 +04:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
; TCP_send_queued:
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03: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-06 00:47:27 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
; IN: /
|
2009-10-06 00:47:27 +04:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
TCP_send_queued:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
cmp [TCP_OUT_QUEUE], 0
|
|
|
|
je .exit
|
2009-10-06 00:47:27 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
mov eax, TCP_QUEUE_SIZE
|
|
|
|
mov ecx, [TCP_OUT_QUEUE]
|
|
|
|
mov esi, TCP_OUT_QUEUE+4
|
2009-10-06 00:47:27 +04:00
|
|
|
|
2009-11-06 23:45:08 +03: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 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
.found_one:
|
|
|
|
dec [esi + tcp_out_queue_entry.ttl]
|
|
|
|
jz .send_it
|
|
|
|
.find_next:
|
2009-11-08 21:00:01 +03:00
|
|
|
add esi, tcp_out_queue_entry.size
|
2009-11-06 23:45:08 +03:00
|
|
|
dec eax
|
|
|
|
jz .exit
|
2009-11-08 21:00:01 +03:00
|
|
|
test ecx, ecx
|
|
|
|
jnz .loop
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
.send_it:
|
|
|
|
push eax ecx esi
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov ebx, [esi + tcp_out_queue_entry.owner]
|
2009-11-06 23:45:08 +03:00
|
|
|
push [esi + tcp_out_queue_entry.data_size]
|
|
|
|
push [esi + tcp_out_queue_entry.data_ptr]
|
2009-11-08 21:00:01 +03:00
|
|
|
DEBUGF 1,"Now sending TCP packet %x, size: %u, owner: %x, sendproc %x\n", [esp], [esp+4], ebx, [esi + tcp_out_queue_entry.sendproc]
|
2009-11-09 16:59:46 +03:00
|
|
|
inc [TCP_PACKETS_TX]
|
2009-11-06 23:45:08 +03:00
|
|
|
call [esi + tcp_out_queue_entry.sendproc]
|
2009-11-08 21:00:01 +03:00
|
|
|
add esp, 8
|
2009-11-06 23:45:08 +03:00
|
|
|
pop esi ecx eax
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
dec [esi + tcp_out_queue_entry.retries]
|
|
|
|
jz .remove_it
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
mov [esi + tcp_out_queue_entry.ttl], TCP_PACKET_TTL
|
2009-11-06 23:45:08 +03:00
|
|
|
jmp .find_next
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
.remove_it:
|
|
|
|
push [esi + tcp_out_queue_entry.data_ptr]
|
|
|
|
mov [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
call kernel_free
|
|
|
|
jmp .find_next
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03: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
|
2009-11-16 00:44:17 +03:00
|
|
|
; pointer to TCP Packet in edx
|
|
|
|
; SourceAddres (IPv4) in esi
|
2009-11-06 23:45:08 +03:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
TCP_handler :
|
|
|
|
|
|
|
|
DEBUGF 1,"TCP_Handler\n"
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; TODO: validate checksum
|
2009-11-08 21:00:01 +03:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; IP Packet TCP Destination Port = local Port
|
|
|
|
; IP Packet SA = Remote IP OR = 0
|
|
|
|
; IP Packet TCP Source Port = remote Port OR = 0
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
mov ebx, net_sockets
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
.socket_loop:
|
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 15:55:38 +04:00
|
|
|
or ebx, ebx
|
2009-11-06 23:45:08 +03:00
|
|
|
jz .dump
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03: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 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
mov eax, [ebx + SOCKET_head.end + IPv4_SOCKET.RemoteIP]
|
|
|
|
cmp eax, esi
|
|
|
|
je @f
|
|
|
|
test eax, eax
|
|
|
|
jne .socket_loop
|
|
|
|
@@:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
mov ax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort]
|
|
|
|
cmp [edx + TCP_Packet.SourcePort] , ax
|
2009-11-16 00:44:17 +03:00
|
|
|
je .found_socket
|
2009-11-06 23:45:08 +03:00
|
|
|
test ax, ax
|
2009-11-08 21:00:01 +03:00
|
|
|
jnz .socket_loop
|
2009-11-16 00:44:17 +03:00
|
|
|
.found_socket:
|
2009-11-08 21:00:01 +03:00
|
|
|
DEBUGF 1,"Found valid socket for packet\n"
|
|
|
|
|
2009-11-09 16:59:46 +03:00
|
|
|
inc [TCP_PACKETS_RX]
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
add ebx, SOCKET_head.lock
|
2009-11-06 23:45:08 +03:00
|
|
|
call wait_mutex
|
2009-11-16 00:44:17 +03:00
|
|
|
sub ebx, SOCKET_head.lock
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
;-------------------------------
|
2009-11-06 23:45:08 +03:00
|
|
|
; ebx is pointer to socket
|
|
|
|
; ecx is size of tcp packet
|
|
|
|
; edx is pointer to tcp packet
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; calculate header length
|
|
|
|
movzx eax, [edx + TCP_Packet.DataOffset]
|
|
|
|
and eax, 11110000b
|
|
|
|
shr eax, 2
|
|
|
|
DEBUGF 1,"TCP header size: %u\n", eax
|
|
|
|
sub ecx, eax
|
|
|
|
|
|
|
|
;-------------------------------
|
|
|
|
; ecx is size of tcp data
|
|
|
|
|
|
|
|
; as a Packet has been received, update the TCB timer
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.timer], TCP_SOCKET_TTL
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; If the received Packet has an ACK bit set, remove any Packets in the resend queue that this received Packet acknowledges
|
2009-11-06 23:45:08 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-11-16 00:44:17 +03:00
|
|
|
jz .no_ack ; No ACK, so no data yet
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Calculate ACK number
|
|
|
|
mov edi, [edx + TCP_Packet.AckNumber]
|
|
|
|
bswap edi
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.last_ack_number], edi
|
|
|
|
DEBUGF 1,"Setting last_ack_number to %u\n", edi
|
|
|
|
bswap edi
|
2009-11-08 21:00:01 +03:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Dequeue all acknowledged packets
|
|
|
|
cmp [TCP_OUT_QUEUE], 0 ; first, check if any packets are queued at all
|
|
|
|
je .no_ack
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
push ecx
|
2009-11-08 21:00:01 +03:00
|
|
|
DEBUGF 1,"Removing all queued packets with smaller ACK\n"
|
2009-11-06 23:45:08 +03:00
|
|
|
mov ecx, TCP_QUEUE_SIZE
|
|
|
|
mov esi, TCP_OUT_QUEUE+4
|
|
|
|
.loop:
|
|
|
|
cmp [esi + tcp_out_queue_entry.data_ptr], 0
|
2009-11-08 21:00:01 +03:00
|
|
|
je .maybe_next
|
2009-11-16 00:44:17 +03:00
|
|
|
|
|
|
|
cmp [esi + tcp_out_queue_entry.socket], ebx
|
|
|
|
jne .maybe_next
|
|
|
|
|
|
|
|
cmp [esi + tcp_out_queue_entry.seq_num], edi
|
2009-11-06 23:45:08 +03:00
|
|
|
jg .maybe_next
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"Removing a queued packet\n"
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
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 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
.maybe_next:
|
|
|
|
add esi, tcp_out_queue_entry.size
|
|
|
|
loop .loop
|
2009-11-08 21:00:01 +03:00
|
|
|
pop ecx
|
2009-11-16 00:44:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
; Now call the correct handler, depending on the socket state
|
|
|
|
.no_ack:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov eax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state]
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
cmp eax, TCB_LISTEN
|
2009-11-08 21:00:01 +03:00
|
|
|
jb .dump
|
2009-11-06 23:45:08 +03:00
|
|
|
cmp eax, TCB_CLOSED
|
2009-11-08 21:00:01 +03:00
|
|
|
ja .dump
|
2009-11-06 23:45:08 +03:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
dec eax
|
2009-11-06 23:45:08 +03:00
|
|
|
shl eax, 2
|
2009-11-08 21:00:01 +03:00
|
|
|
add eax, stateHandler
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
call dword[eax]
|
2009-10-06 00:47:27 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
.dump:
|
|
|
|
DEBUGF 1,"Dumping TCP packet\n"
|
2009-10-06 00:47:27 +04:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4 ; pop (balance stack)
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2009-10-06 00:47:27 +04:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2009-11-16 00:44:17 +03:00
|
|
|
; TCP_send (Assumes socket mutex set)
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
; IN: eax = socket pointer
|
2009-11-16 00:44:17 +03:00
|
|
|
; bl = flags
|
|
|
|
; ecx = number of bytes to send, may be set to 0
|
|
|
|
; esi = pointer to data
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
2009-11-16 00:44:17 +03:00
|
|
|
TCP_send:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
DEBUGF 1,"Creating TCP packet, socket: %x, flags: %x\n",eax, bl
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
mov di , IP_PROTO_TCP
|
2009-11-16 00:44:17 +03:00
|
|
|
add ecx, TCP_Packet.Data
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
push bx eax esi
|
2009-11-06 23:45:08 +03: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 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
call IPv4_create_packet
|
|
|
|
cmp edi, -1
|
|
|
|
je .fail
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; If there is any data, copy it first
|
2009-11-06 23:45:08 +03:00
|
|
|
pop esi
|
2009-11-16 00:44:17 +03:00
|
|
|
push edi
|
|
|
|
add edi, TCP_Packet.Data
|
|
|
|
sub ecx, TCP_Packet.Data
|
2009-10-06 00:47:27 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb: shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
2009-11-16 00:44:17 +03:00
|
|
|
.nw: test ecx, ecx
|
|
|
|
jz .nd
|
|
|
|
rep movsd
|
|
|
|
.nd:
|
|
|
|
pop edi
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
; Fill in the TCP header
|
|
|
|
pop esi
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; fill in tcp sequence number
|
2009-11-06 23:45:08 +03:00
|
|
|
push [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
pop [edi + TCP_Packet.SequenceNumber]
|
2009-11-16 00:44:17 +03:00
|
|
|
inc_INET (ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT) ;;;;;;;;
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Fill in local and remote ports
|
|
|
|
push dword [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort]
|
2009-11-06 23:45:08 +03:00
|
|
|
pop dword [edi + TCP_Packet.SourcePort]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Acknumber
|
2009-11-08 21:00:01 +03:00
|
|
|
push [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
2009-11-06 23:45:08 +03:00
|
|
|
pop [edi + TCP_Packet.AckNumber]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Fill in other tcp options
|
2009-11-06 23:45:08 +03: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-11-08 21:00:01 +03:00
|
|
|
mov [edi + TCP_Packet.Checksum], 0
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Push pointer to and size of total packet (needed for send procedure)
|
2009-11-08 21:00:01 +03:00
|
|
|
push edx eax
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; push socket number (for TCP_add_to_queue)
|
|
|
|
push esi
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Now, calculate the checksum ; TODO: calculate correct checksum for packets with data
|
|
|
|
pushw TCP_Packet.Data shl 8
|
2009-11-08 21:00:01 +03:00
|
|
|
pushw IP_PROTO_TCP shl 8
|
2009-11-09 16:59:46 +03:00
|
|
|
pushd [edi-4] ; destination address ; TODO: fix this, IPv4 packet could have options..
|
2009-11-08 21:00:01 +03:00
|
|
|
pushd [edi-8] ; source address
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
xor edx, edx
|
2009-11-16 00:44:17 +03:00
|
|
|
mov ecx, TCP_Packet.Data
|
2009-11-08 21:00:01 +03:00
|
|
|
mov esi, edi
|
|
|
|
call checksum_1
|
2009-11-06 23:45:08 +03:00
|
|
|
mov ecx, 12
|
|
|
|
mov esi, esp
|
|
|
|
call checksum_1
|
|
|
|
add esp, 12 ; remove the pseudoheader from stack
|
2009-11-08 21:00:01 +03:00
|
|
|
; and store it in TCP header
|
2009-11-06 23:45:08 +03:00
|
|
|
call checksum_2
|
|
|
|
mov [edi + TCP_Packet.Checksum], dx
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; At last send the packet!
|
2009-11-06 23:45:08 +03:00
|
|
|
DEBUGF 1,"Sending TCP Packet to device %x\n", ebx
|
2009-11-08 21:00:01 +03:00
|
|
|
mov edx, [edi + TCP_Packet.SequenceNumber]
|
2009-11-16 00:44:17 +03:00
|
|
|
bswap edx
|
|
|
|
mov esi, [ebx + ETH_DEVICE.transmit]
|
|
|
|
pop edi
|
|
|
|
jmp TCP_queue
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
.fail:
|
2009-11-08 21:00:01 +03:00
|
|
|
add esp, 2+4
|
2009-11-16 00:44:17 +03:00
|
|
|
or eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Queue a TCP packet for sending
|
|
|
|
;
|
|
|
|
; IN: [esp] pointer to buffer
|
|
|
|
; [esp + 4] size of buffer
|
|
|
|
; ebx = driver struct
|
|
|
|
; esi = sender proc
|
|
|
|
; edx = sequence number of this packet in normal byte order
|
|
|
|
; edi = socket number
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
TCP_queue:
|
|
|
|
|
|
|
|
bswap edx
|
|
|
|
DEBUGF 1,"Adding packet to TCP queue, buffer: %x, size: %u, driver: %x, acknum: %u\n", [esp], [esp+4], ebx, edx
|
|
|
|
bswap edx
|
|
|
|
|
|
|
|
cmp [TCP_OUT_QUEUE], TCP_QUEUE_SIZE
|
|
|
|
jge .full
|
|
|
|
|
|
|
|
mov ecx, TCP_QUEUE_SIZE
|
|
|
|
mov eax, TCP_OUT_QUEUE+4
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
cmp [eax + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
je .found_it
|
|
|
|
add eax, tcp_out_queue_entry.size
|
|
|
|
loop .loop
|
|
|
|
|
|
|
|
.full: ; silently discard the packet
|
|
|
|
DEBUGF 1,"TCP queue is full!\n"
|
|
|
|
|
|
|
|
call kernel_free
|
|
|
|
add esp, 4
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.found_it: ; eax points to empty queue entry
|
|
|
|
|
|
|
|
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.seq_num], edx
|
|
|
|
mov [eax + tcp_out_queue_entry.socket], edi
|
|
|
|
|
|
|
|
inc [TCP_OUT_QUEUE]
|
|
|
|
|
|
|
|
sub eax, TCP_OUT_QUEUE+4
|
|
|
|
DEBUGF 1,"Added to queue in pos %u\n", eax
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 00:44:04 +03:00
|
|
|
;---------- TCB state handlers start here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
stateTCB_LISTEN:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: Listen\n"
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_SYN ; SYN packet? => send syn+ack, open new socket and set connection to established
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .exit
|
2009-11-16 00:44:17 +03:00
|
|
|
; Exit if backlog queue is full
|
2009-11-09 14:34:51 +03:00
|
|
|
mov ax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog_cur]
|
|
|
|
cmp ax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog]
|
|
|
|
jae .exit
|
2009-11-16 00:44:17 +03:00
|
|
|
; Allocate new socket
|
|
|
|
push esi edi
|
2009-11-09 14:34:51 +03:00
|
|
|
call net_socket_alloc
|
|
|
|
test eax, eax
|
2009-11-16 00:44:17 +03:00
|
|
|
jz .fail
|
|
|
|
; Copy structure from current socket to new, including lock
|
|
|
|
lea esi, [ebx + SOCKET_head.PID] ; yes, PID must also be copied
|
2009-11-09 14:34:51 +03:00
|
|
|
lea edi, [eax + SOCKET_head.PID]
|
|
|
|
mov ecx, ((SOCKET_head.end - SOCKET_head.PID) + IPv4_SOCKET.end + TCP_SOCKET.end + 3)/4
|
|
|
|
rep movsd
|
|
|
|
pop edi esi
|
2009-11-16 00:44:17 +03:00
|
|
|
; Push pointer to new socket to queue
|
2009-11-09 14:34:51 +03:00
|
|
|
movzx ecx, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog_cur]
|
|
|
|
inc [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog_cur]
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.end + ecx*4], eax
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-09 14:34:51 +03:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], esi ; IP source address
|
|
|
|
mov cx, [edx + TCP_Packet.SourcePort]
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort], cx
|
|
|
|
mov ecx, [edx + TCP_Packet.SequenceNumber]
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.IRS], ecx
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT], ecx
|
|
|
|
lea esi, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
2009-11-06 23:45:08 +03:00
|
|
|
inc_INET esi ; RCV.NXT
|
2009-11-09 14:34:51 +03:00
|
|
|
mov ecx, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.ISS]
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT], ecx
|
2009-11-06 23:45:08 +03:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
|
2009-11-09 14:34:51 +03:00
|
|
|
push eax
|
2009-11-16 00:44:17 +03:00
|
|
|
; Now construct the response
|
2009-09-17 15:55:38 +04:00
|
|
|
mov bl, TH_SYN + TH_ACK
|
2009-11-16 00:44:17 +03:00
|
|
|
xor ecx, ecx
|
|
|
|
call TCP_send
|
2009-11-09 14:34:51 +03:00
|
|
|
pop eax
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
mov [eax + SOCKET_head.lock], 0
|
2009-11-09 14:34:51 +03:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_RECEIVED
|
|
|
|
call notify_network_event
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
.fail:
|
|
|
|
add esp, 8
|
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
ret
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
stateTCB_SYN_SENT:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: Syn_Sent\n"
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; We are awaiting an ACK to our SYN, with a SYM
|
|
|
|
; Look at control flags - expecting an ACK
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
mov al, [edx + TCP_Packet.Flags]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
test al, TH_RST
|
|
|
|
jnz .reset ; jump if RST bit set
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
push [edx + TCP_Packet.SequenceNumber] ;;
|
|
|
|
pop [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT] ;;
|
|
|
|
inc_INET (ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT) ;;
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
push [edx + TCP_Packet.AckNumber] ;;;;;;
|
|
|
|
pop [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT] ;;;;;;
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
and al, TH_SYN + TH_ACK
|
|
|
|
jz .exit ; jump if none of the following is set: RST, SYN, ACK
|
2009-11-09 00:44:04 +03:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
test al, TH_ACK
|
|
|
|
jz .onlysyn ; jump if only SYN bit is set
|
|
|
|
|
|
|
|
; If we arrived here, SYN and ACK are set
|
|
|
|
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_ESTABLISHED
|
|
|
|
pushw TH_ACK
|
|
|
|
|
|
|
|
.send: ; Send an ACK
|
2009-11-08 21:00:01 +03:00
|
|
|
mov eax, ebx
|
2009-11-16 00:44:17 +03:00
|
|
|
pop bx
|
|
|
|
push eax
|
|
|
|
xor ecx, ecx
|
|
|
|
call TCP_send
|
2009-09-17 15:55:38 +04:00
|
|
|
pop ebx
|
|
|
|
|
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
.reset:
|
|
|
|
; TODO: ....
|
|
|
|
|
|
|
|
; remove all queued TCP packets for this connection !
|
|
|
|
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSED
|
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
ret
|
|
|
|
|
|
|
|
.onlysyn:
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_RECEIVED
|
|
|
|
pushw TH_SYN + TH_ACK
|
|
|
|
jmp .send
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_SYN_RECEIVED:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: Syn_received\n"
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_RST ; reset connection? => LISTEN
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .check_ack
|
|
|
|
|
2009-11-16 00:44:17 +03: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 15:55:38 +04:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LISTEN
|
2009-09-17 15:55:38 +04:00
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.check_ack:
|
2009-11-16 00:44:17 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK ; ACK? => connection established!
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .exit
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_ESTABLISHED
|
2009-11-09 14:34:51 +03:00
|
|
|
mov eax, ebx
|
|
|
|
call notify_network_event
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_ESTABLISHED:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: Established\n"
|
|
|
|
|
2009-11-09 00:44:04 +03:00
|
|
|
mov eax, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
2009-11-16 00:44:17 +03:00
|
|
|
bswap eax
|
|
|
|
DEBUGF 1,"RCV_NXT is set to:%u\n", eax
|
|
|
|
bswap eax
|
2009-11-09 00:44:04 +03:00
|
|
|
cmp eax, [edx + TCP_Packet.SequenceNumber]
|
|
|
|
jne .exit
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Calculate next sequencenumber
|
|
|
|
test ecx, ecx
|
|
|
|
jnz @f
|
|
|
|
inc ecx
|
|
|
|
@@:
|
|
|
|
add_INET (ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT)
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_FIN
|
2009-11-16 00:44:17 +03:00
|
|
|
jnz .fin
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.check_ack:
|
2009-11-06 23:45:08 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .exit
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
DEBUGF 1,"Received ACK\n"
|
2009-11-16 00:44:17 +03:00
|
|
|
; 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-09 00:44:04 +03:00
|
|
|
push ecx
|
2009-11-06 23:45:08 +03:00
|
|
|
mov cx, [edx + TCP_Packet.Window]
|
2009-09-17 15:55:38 +04:00
|
|
|
xchg cl, ch
|
|
|
|
cmp cx, 1024
|
|
|
|
ja @f
|
2009-11-06 23:45:08 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 1
|
2009-11-09 00:44:04 +03:00
|
|
|
@@:
|
|
|
|
pop ecx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
; Now, see if we received any data
|
2009-11-06 23:45:08 +03:00
|
|
|
test ecx, ecx
|
2009-11-16 00:44:17 +03:00
|
|
|
jz .ack
|
|
|
|
|
|
|
|
DEBUGF 1,"Got %u bytes data!\n", ecx
|
|
|
|
; calculate header length
|
|
|
|
movzx eax, [edx + TCP_Packet.DataOffset]
|
|
|
|
and eax, 11110000b
|
|
|
|
shr eax, 2
|
|
|
|
DEBUGF 1,"TCP header size: %u\n", eax
|
|
|
|
add edx, eax
|
|
|
|
add esp, 4
|
|
|
|
pop esi
|
|
|
|
add esp, 4
|
2009-11-06 23:45:08 +03:00
|
|
|
sub edx, esi
|
|
|
|
mov edi, edx
|
2009-11-09 00:44:04 +03:00
|
|
|
mov eax, ebx
|
2009-11-16 00:44:17 +03:00
|
|
|
jmp socket_internal_receiver ; Place the data from packet into socket
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.ack:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov eax, ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
mov bl, TH_ACK
|
2009-11-16 00:44:17 +03:00
|
|
|
push eax
|
|
|
|
xor ecx, ecx
|
|
|
|
call TCP_send ; send the ack
|
|
|
|
pop ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
.fin:
|
|
|
|
; Remove all resend entries from the queue
|
|
|
|
mov ecx, TCP_QUEUE_SIZE
|
|
|
|
mov esi, TCP_OUT_QUEUE+4
|
|
|
|
|
|
|
|
.removeloop:
|
|
|
|
cmp [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
je .maybe_next
|
|
|
|
|
|
|
|
; TODO: check if the packets belong to the same tcp connection !
|
|
|
|
|
|
|
|
DEBUGF 1,"Removing a queued packet\n"
|
|
|
|
|
|
|
|
push [esi + tcp_out_queue_entry.data_ptr]
|
|
|
|
mov [esi + tcp_out_queue_entry.data_ptr], 0
|
|
|
|
dec [TCP_OUT_QUEUE]
|
|
|
|
call kernel_free
|
|
|
|
|
|
|
|
.maybe_next:
|
|
|
|
add esi, tcp_out_queue_entry.size
|
|
|
|
loop .removeloop
|
|
|
|
|
|
|
|
; Send an ACK to that fin, and enter closewait state
|
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSE_WAIT
|
|
|
|
jmp .check_ack
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_FIN_WAIT_1:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: Fin_wait_1\n"
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; We can either receive an ACK of a fin, or a fin
|
2009-11-06 23:45:08 +03:00
|
|
|
mov al, [edx + TCP_Packet.Flags]
|
2009-09-17 15:55:38 +04:00
|
|
|
and al, TH_FIN + TH_ACK
|
|
|
|
|
|
|
|
cmp al, TH_ACK
|
|
|
|
jne @f
|
|
|
|
|
|
|
|
; It was an ACK
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_FIN_WAIT_2
|
2009-09-17 15:55:38 +04:00
|
|
|
jmp .exit
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
@@: mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSING
|
2009-09-17 15:55:38 +04:00
|
|
|
cmp al, TH_FIN
|
|
|
|
je @f
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_TIMED_WAIT
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-16 00:44:17 +03:00
|
|
|
@@:
|
|
|
|
|
|
|
|
; lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
; inc_INET esi
|
2009-11-08 21:00:01 +03:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; Send an ACK
|
2009-11-08 21:00:01 +03:00
|
|
|
mov eax, ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
mov bl, TH_ACK
|
2009-11-16 00:44:17 +03:00
|
|
|
push eax
|
|
|
|
xor ecx, ecx
|
|
|
|
call TCP_send
|
|
|
|
pop ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_FIN_WAIT_2:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: Fin_wait_2\n"
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_FIN
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; Change state, as we have a fin
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_TIMED_WAIT
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
lea esi, [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RCV_NXT]
|
|
|
|
inc_INET esi
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; Send an ACK
|
2009-11-08 21:00:01 +03:00
|
|
|
mov eax, ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
mov bl, TH_ACK
|
2009-11-16 00:44:17 +03:00
|
|
|
push eax
|
|
|
|
xor ecx, ecx
|
|
|
|
call TCP_send
|
|
|
|
pop ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_CLOSE_WAIT:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: close_wait\n"
|
2009-09-17 15:55:38 +04:00
|
|
|
; Intentionally left empty
|
|
|
|
; socket_close_tcp handles this
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
align 4
|
|
|
|
stateTCB_CLOSING:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: closingn\n"
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; We can either receive an ACK of a fin, or a fin
|
2009-11-06 23:45:08 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .exit
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_TIMED_WAIT
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.exit:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
stateTCB_LAST_ACK:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: last_ackn\n"
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; Look at control flags - expecting an ACK
|
2009-11-06 23:45:08 +03:00
|
|
|
test [edx + TCP_Packet.Flags], TH_ACK
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .exit
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
; delete the socket
|
|
|
|
stdcall net_socket_free, ebx
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
stateTCB_TIME_WAIT:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: time_wait\n"
|
|
|
|
|
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 23:45:08 +03:00
|
|
|
align 4
|
|
|
|
stateTCB_CLOSED:
|
2009-11-08 21:00:01 +03:00
|
|
|
|
|
|
|
DEBUGF 1,"TCBStateHandler: closed\n"
|
|
|
|
|
|
|
|
mov [ebx + SOCKET_head.lock], 0
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; TCP_API
|
|
|
|
;
|
|
|
|
; This function is called by system function 75
|
|
|
|
;
|
|
|
|
; IN: subfunction number in bl
|
|
|
|
; device number in bh
|
|
|
|
; ecx, edx, .. depends on subfunction
|
|
|
|
;
|
|
|
|
; OUT:
|
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
TCP_API:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-11-08 21:00:01 +03:00
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
|
|
|
test bl, bl
|
|
|
|
jz .packets_tx ; 0
|
|
|
|
dec bl
|
|
|
|
jz .packets_rx ; 1
|
|
|
|
|
|
|
|
.error:
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx:
|
|
|
|
add eax, TCP_PACKETS_TX
|
|
|
|
mov eax, [eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx:
|
|
|
|
add eax, TCP_PACKETS_RX
|
|
|
|
mov eax, [eax]
|
|
|
|
ret
|