Subversion Repositories Kolibri OS

Rev

Rev 6413 | Rev 6710 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  Part of the TCP/IP network stack for KolibriOS                 ;;
  7. ;;                                                                 ;;
  8. ;;   Written by hidnplayr@kolibrios.org                            ;;
  9. ;;                                                                 ;;
  10. ;;    Based on the algorithms used in 4.4BSD                       ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 6476 $
  18.  
  19. TCP_BIT_NEEDOUTPUT      = 1 shl 0
  20. TCP_BIT_TIMESTAMP       = 1 shl 1
  21. TCP_BIT_DROPSOCKET      = 1 shl 2
  22. TCP_BIT_FIN_IS_ACKED    = 1 shl 3
  23.  
  24. ;-----------------------------------------------------------------;
  25. ;                                                                 ;
  26. ; TCP_input: Add a segment to the incoming TCP queue.             ;
  27. ;                                                                 ;
  28. ;  IN:  [esp] = ptr to buffer                                     ;
  29. ;       ebx = ptr to device struct                                ;
  30. ;       ecx = TCP segment size                                    ;
  31. ;       edx = ptr to IPv4 header                                  ;
  32. ;       esi = ptr to TCP segment                                  ;
  33. ;       edi = interface number*4                                  ;
  34. ;                                                                 ;
  35. ;  OUT: /                                                         ;
  36. ;                                                                 ;
  37. ;-----------------------------------------------------------------;
  38. align 4
  39. tcp_input:
  40.  
  41. ; record the current time
  42.         push    [timer_ticks]           ; in 1/100 seconds
  43.         push    ebx ecx esi edx         ; mind the order (see TCP_queue_entry struct)
  44.         mov     esi, esp
  45.  
  46.         push    edi
  47.         add_to_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .fail
  48.         pop     edi
  49.         add     esp, sizeof.TCP_queue_entry
  50.  
  51.         inc     [TCP_segments_rx + edi]
  52.  
  53.         xor     edx, edx
  54.         mov     eax, [TCP_input_event]
  55.         mov     ebx, [eax + EVENT.id]
  56.         xor     esi, esi
  57.         call    raise_event
  58.  
  59.         ret
  60.  
  61.   .fail:
  62.         pop     edi
  63.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP incoming queue is full, discarding packet!\n"
  64.  
  65.         call    net_ptr_to_num4
  66.         inc     [TCP_segments_missed + edi]
  67.  
  68.         add     esp, sizeof.TCP_queue_entry - 4
  69.         call    net_buff_free
  70.         ret
  71.  
  72.  
  73. ;-----------------------------------------------------------------;
  74. ;                                                                 ;
  75. ; TCP_process_input: Process segments from the incoming TCP queue.;
  76. ;                                                                 ;
  77. ;  IN:  /                                                         ;
  78. ;  OUT: /                                                         ;
  79. ;                                                                 ;
  80. ;-----------------------------------------------------------------;
  81. align 4
  82. proc tcp_process_input
  83.  
  84. locals
  85.         dataoffset      dd ?
  86.         timestamp       dd ?
  87.         temp_bits       db ?
  88. endl
  89.  
  90.         xor     esi, esi
  91.         mov     ecx, MANUAL_DESTROY
  92.         call    create_event
  93.         mov     [TCP_input_event], eax
  94.  
  95.   .wait:
  96.         mov     eax, [TCP_input_event]
  97.         mov     ebx, [eax + EVENT.id]
  98.         call    wait_event
  99.  
  100.   .loop:
  101.         get_from_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .wait
  102.  
  103.         push    [esi + TCP_queue_entry.timestamp]
  104.         pop     [timestamp]
  105.         push    [esi + TCP_queue_entry.buffer_ptr]
  106.  
  107.         mov     ebx, [esi + TCP_queue_entry.device_ptr]
  108.         mov     ecx, [esi + TCP_queue_entry.segment_size]
  109.         mov     edi, [esi + TCP_queue_entry.ip_ptr]                     ; ptr to ipv4 header
  110.         mov     esi, [esi + TCP_queue_entry.segment_ptr]                ; change esi last
  111.  
  112.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: size=%u time=%d\n", ecx, [timer_ticks]
  113.  
  114.         mov     edx, esi
  115.  
  116. ; Verify the checksum (if not already done by hw)
  117.  
  118.         test    [ebx + NET_DEVICE.hwacc], NET_HWACC_TCP_IPv4_IN
  119.         jnz     .checksum_ok
  120.  
  121.         push    ecx esi
  122.         pushw   [esi + TCP_header.Checksum]
  123.         mov     [esi + TCP_header.Checksum], 0
  124.         tcp_checksum (edi+IPv4_header.SourceAddress), (edi+IPv4_header.DestinationAddress)
  125.         pop     cx                              ; previous checksum
  126.         cmp     cx, dx
  127.         pop     edx ecx
  128.         jne     .drop_no_socket
  129.   .checksum_ok:
  130.  
  131. ; Verify the data offset
  132.  
  133.         movzx   eax, [edx + TCP_header.DataOffset]
  134.         and     al, 0xf0                        ; Calculate TCP segment header size (throwing away unused reserved bits in TCP header)
  135.         shr     al, 2
  136.         cmp     al, sizeof.TCP_header           ; Now see if it's at least the size of a standard TCP header
  137.         jb      .drop_no_socket                 ; If not, drop the packet
  138.         mov     [dataoffset], eax
  139.  
  140.         sub     ecx, eax                                                ; substract TCP header size from total segment size
  141.         jb      .drop_no_socket                                         ; If total segment size is less then the advertised header size, drop packet
  142.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: %u bytes of data\n", ecx
  143.  
  144. ;-------------------------------------------
  145. ; Convert Big-endian values to little endian
  146.  
  147.         ntohd   [edx + TCP_header.SequenceNumber]
  148.         ntohd   [edx + TCP_header.AckNumber]
  149.  
  150.         ntohw   [edx + TCP_header.Window]
  151.         ntohw   [edx + TCP_header.UrgentPointer]
  152.  
  153. ;-----------------------------------------------------------------------------------
  154. ;
  155. ; Find the socket pointer
  156. ;
  157. ;-----------------------------------------------------------------------------------
  158.  
  159. ; IP Packet TCP Destination Port = local Port
  160. ; (IP Packet SenderAddress = Remote IP)  OR  (Remote IP = 0)
  161. ; (IP Packet TCP Source Port = remote Port) OR (remote Port = 0)
  162.  
  163.   .findpcb:
  164.         pusha
  165.         mov     ecx, socket_mutex
  166.         call    mutex_lock
  167.         popa
  168.  
  169.         mov     ebx, net_sockets
  170.         mov     si, [edx + TCP_header.DestinationPort]
  171.  
  172.   .socket_loop:
  173.         mov     ebx, [ebx + SOCKET.NextPtr]
  174.         or      ebx, ebx
  175.         jz      .no_socket ;respond_seg_reset
  176.  
  177.         cmp     [ebx + SOCKET.Domain], AF_INET4
  178.         jne     .socket_loop
  179.  
  180.         cmp     [ebx + SOCKET.Protocol], IP_PROTO_TCP
  181.         jne     .socket_loop
  182.  
  183.         cmp     [ebx + TCP_SOCKET.LocalPort], si
  184.         jne     .socket_loop
  185.  
  186.         mov     eax, [ebx + IP_SOCKET.RemoteIP]
  187.         cmp     eax, [edi + IPv4_header.SourceAddress]
  188.         je      @f
  189.         test    eax, eax
  190.         jnz     .socket_loop
  191.        @@:
  192.  
  193.         mov     ax, [ebx + TCP_SOCKET.RemotePort]
  194.         cmp     [edx + TCP_header.SourcePort], ax
  195.         je      .found_socket
  196.         test    ax, ax
  197.         jnz     .socket_loop
  198.   .found_socket:                                        ; ebx now contains the socketpointer
  199.         pusha
  200.         mov     ecx, socket_mutex
  201.         call    mutex_unlock
  202.         popa
  203.  
  204.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: socket ptr=%x state=%u flags=%x\n", ebx, [ebx + TCP_SOCKET.t_state], [edx + TCP_header.Flags]:2
  205.  
  206. ;----------------------------
  207. ; Check if socket isnt closed
  208.  
  209.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
  210.         je      .drop_no_socket
  211.  
  212. ;----------------
  213. ; Lock the socket
  214.  
  215.         pusha
  216.         lea     ecx, [ebx + SOCKET.mutex]
  217.         call    mutex_lock
  218.         popa
  219.  
  220.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: socket locked\n"
  221.  
  222. ;---------------------------
  223. ; disable all temporary bits
  224.  
  225.         mov     [temp_bits], 0
  226.  
  227. ;---------------------------------------
  228. ; unscale the window into a 32 bit value
  229.  
  230.         movzx   eax, [edx + TCP_header.Window]
  231.         push    ecx
  232.         mov     cl, [ebx + TCP_SOCKET.SND_SCALE]
  233.         shl     eax, cl
  234.         mov     dword[edx + TCP_header.Window], eax     ; word after window is checksum, we dont need checksum anymore
  235.         pop     ecx
  236.  
  237. ;-----------------------------------------------------------------------------------
  238. ;
  239. ; Accept incoming connections
  240. ;
  241. ;-----------------------------------------------------------------------------------
  242.  
  243.         test    [ebx + SOCKET.options], SO_ACCEPTCON
  244.         jz      .no_accept
  245.  
  246.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Accepting new connection\n"
  247.  
  248. ; Unlock current socket
  249.  
  250.         pusha
  251.         lea     ecx, [ebx + SOCKET.mutex]
  252.         call    mutex_unlock
  253.         popa
  254.  
  255. ; Fork it
  256.  
  257.         push    ecx edx esi edi
  258.         call    socket_fork
  259.         pop     edi esi edx ecx
  260.  
  261.         test    eax, eax
  262.         jz      .drop_no_socket
  263.  
  264. ; Success! Use the new socket from now on (it is already locked)
  265.  
  266.         mov     ebx, eax
  267.  
  268.         mov     [temp_bits], TCP_BIT_DROPSOCKET
  269.  
  270.         push    [edi + IPv4_header.DestinationAddress]
  271.         pop     [ebx + IP_SOCKET.LocalIP]
  272.  
  273.         push    [edx + TCP_header.DestinationPort]
  274.         pop     [ebx + TCP_SOCKET.LocalPort]
  275.  
  276.         mov     [ebx + TCP_SOCKET.t_state], TCPS_LISTEN
  277.   .no_accept:
  278.  
  279.  
  280. ;-------------------------------------
  281. ; Reset idle timer and keepalive timer
  282.  
  283.         mov     [ebx + TCP_SOCKET.t_idle], 0
  284.         mov     [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_idle
  285.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
  286.  
  287. ;-----------------------------------------------------------------------------------
  288. ;
  289. ; Process TCP options
  290. ;
  291. ;-----------------------------------------------------------------------------------
  292.  
  293. ;;; FIXME: for LISTEN, options should be called after we determined route, we need it for MSS
  294. ;;;        cmp     [ebx + TCP_SOCKET.t_state], TCPS_LISTEN ; no options when in listen state
  295. ;;;        jz      .not_uni_xfer                           ; also no header prediction
  296.  
  297.         push    ecx
  298.  
  299.         mov     ecx, [dataoffset]
  300.         cmp     ecx, sizeof.TCP_header          ; Does header contain any options?
  301.         je      .no_options
  302.  
  303.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Segment has options\n"
  304.  
  305.         add     ecx, edx
  306.         lea     esi, [edx + sizeof.TCP_header]
  307.  
  308.   .opt_loop:
  309.         cmp     esi, ecx                        ; are we scanning outside of header?
  310.         jae     .no_options
  311.         lodsb
  312.         cmp     al, TCP_OPT_EOL                 ; end of option list?
  313.         je      .no_options
  314.         cmp     al, TCP_OPT_NOP
  315.         je      .opt_loop
  316.         cmp     al, TCP_OPT_MAXSEG
  317.         je      .opt_maxseg
  318.         cmp     al, TCP_OPT_WINDOW
  319.         je      .opt_window
  320.         cmp     al, TCP_OPT_SACK_PERMIT
  321.         je      .opt_sack_permit
  322. ;        cmp     al, TCP_OPT_SACK
  323. ;        je      .opt_sack
  324.         cmp     al, TCP_OPT_TIMESTAMP
  325.         je      .opt_timestamp
  326.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: unknown option:%u\n", al
  327.         jmp     .no_options                     ; If we reach here, some unknown options were received, skip them all!
  328.  
  329.   .opt_maxseg:
  330.         lodsb
  331.         cmp     al, 4
  332.         jne     .no_options                     ; error occured, ignore all options!
  333.  
  334.         test    [edx + TCP_header.Flags], TH_SYN
  335.         jz      @f
  336.  
  337.         xor     eax, eax
  338.         lodsw
  339.         rol     ax, 8
  340.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Maxseg=%u\n", eax
  341.         call    tcp_mss
  342.        @@:
  343.         jmp     .opt_loop
  344.  
  345.  
  346.   .opt_window:
  347.         lodsb
  348.         cmp     al, 3
  349.         jne     .no_options
  350.  
  351.         test    [edx + TCP_header.Flags], TH_SYN
  352.         jz      @f
  353.  
  354.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Got window scale option\n"
  355.         or      [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  356.  
  357.         lodsb
  358.         mov     [ebx + TCP_SOCKET.SND_SCALE], al
  359.         ;;;;; TODO
  360.  
  361.        @@:
  362.         jmp     .opt_loop
  363.  
  364.  
  365.   .opt_sack_permit:
  366.         lodsb
  367.         cmp     al, 2
  368.         jne     .no_options
  369.  
  370.         test    [edx + TCP_header.Flags], TH_SYN
  371.         jz      @f
  372.  
  373.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Selective Acknowledgement permitted\n"
  374.         or      [ebx + TCP_SOCKET.t_flags], TF_SACK_PERMIT
  375.  
  376.        @@:
  377.         jmp     .opt_loop
  378.  
  379.  
  380.   .opt_timestamp:
  381.         lodsb
  382.         cmp     al, 10                          ; length must be 10
  383.         jne     .no_options
  384.  
  385.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Got timestamp option\n"
  386.  
  387.         test    [edx + TCP_header.Flags], TH_SYN
  388.         jz      @f
  389.         or      [ebx + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
  390.        @@:
  391.  
  392.         lodsd
  393.         bswap   eax
  394.         mov     [ebx + TCP_SOCKET.ts_val], eax
  395.         lodsd                                   ; timestamp echo reply
  396.         mov     [ebx + TCP_SOCKET.ts_ecr], eax
  397.         or      [temp_bits], TCP_BIT_TIMESTAMP
  398.  
  399.         ; Since we have a timestamp, lets do the paws test right away!
  400.  
  401.         test    [edx + TCP_header.Flags], TH_RST
  402.         jnz     .no_paws
  403.  
  404.         mov     eax, [ebx + TCP_SOCKET.ts_recent]
  405.         test    eax, eax
  406.         jz      .no_paws
  407.         cmp     eax, [ebx + TCP_SOCKET.ts_val]
  408.         jbe     .no_paws
  409.  
  410.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: PAWS: detected an old segment\n"
  411.  
  412.         mov     eax, [timestamp]
  413.         sub     eax, [ebx + TCP_SOCKET.ts_recent_age]
  414.  
  415.         pop     ecx
  416.         cmp     eax, TCP_PAWS_IDLE
  417.         jle     .paws_drop
  418.         push    ecx
  419.         mov     [ebx + TCP_SOCKET.ts_recent], 0         ; timestamp was invalid, fix it.
  420.   .no_paws:
  421.         jmp     .opt_loop
  422.  
  423.   .paws_drop:
  424.         inc     [TCPS_rcvduppack]
  425.         add     [TCPS_rcvdupbyte], ecx
  426.         inc     [TCPS_pawsdrop]
  427.         jmp     .drop_after_ack
  428.  
  429.   .no_options:
  430.  
  431.         pop     ecx
  432.  
  433. ;-----------------------------------------------------------------------------------
  434. ;
  435. ; Header prediction
  436. ;
  437. ;-----------------------------------------------------------------------------------
  438.  
  439. ; According to Van Jacobson, there are two common cases for an uni-directional data transfer.
  440. ;
  441. ; General rule: the packets has no control flags, is in-sequence,
  442. ;   window width didnt change and we're not retransmitting.
  443. ;
  444. ; Second rules:
  445. ;  -  If the length is 0 and the ACK moved forward, we're the sender side of the transfer.
  446. ;      In this case we'll free the ACK'ed data and notify higher levels that we have free space in buffer
  447. ;
  448. ;  -  If the length is not 0 and the ACK didn't move, we're the receiver side of the transfer.
  449. ;      If the packets are in order (data queue is empty), add the data to the socket buffer and request a delayed ACK
  450.  
  451.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  452.         jnz     .not_uni_xfer
  453.  
  454.         test    [edx + TCP_header.Flags], TH_SYN + TH_FIN + TH_RST + TH_URG
  455.         jnz     .not_uni_xfer
  456.  
  457.         test    [edx + TCP_header.Flags], TH_ACK
  458.         jz      .not_uni_xfer
  459.  
  460.         mov     eax, [edx + TCP_header.SequenceNumber]
  461.         cmp     eax, [ebx + TCP_SOCKET.RCV_NXT]
  462.         jne     .not_uni_xfer
  463.  
  464.         mov     eax, dword[edx + TCP_header.Window]
  465.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  466.         jne     .not_uni_xfer
  467.  
  468.         mov     eax, [ebx + TCP_SOCKET.SND_NXT]
  469.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  470.         jne     .not_uni_xfer
  471.  
  472. ;---------------------------------------
  473. ; check if we are sender in the uni-xfer
  474.  
  475. ; If the following 4 conditions are all true, this segment is a pure ACK.
  476. ;
  477. ; - The segment contains no data.
  478.  
  479.         test    ecx, ecx
  480.         jnz     .not_sender
  481.  
  482. ; - The congestion window is greater than or equal to the current send window.
  483. ;     This test is true only if the window is fully open, that is, the connection is not in the middle of slow start or congestion avoidance.
  484.  
  485.         mov     eax, [ebx + TCP_SOCKET.SND_CWND]
  486.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  487.         jb      .not_uni_xfer
  488.  
  489. ; - The acknowledgment field in the segment is less than or equal to the maximum sequence number sent.
  490.  
  491.         mov     eax, [edx + TCP_header.AckNumber]
  492.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  493.         ja      .not_uni_xfer
  494.  
  495. ; - The acknowledgment field in the segment is greater than the largest unacknowledged sequence number.
  496.  
  497.         sub     eax, [ebx + TCP_SOCKET.SND_UNA]
  498.         jbe     .not_uni_xfer
  499.  
  500.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction: we are sender\n"
  501.  
  502. ;---------------------------------
  503. ; Packet is a pure ACK, process it
  504.  
  505.         inc     [TCPS_predack]
  506.  
  507.         inc     [TCPS_rcvackpack]
  508.         add     [TCPS_rcvackbyte], eax
  509.  
  510. ; Delete acknowledged bytes from send buffer
  511.  
  512.         pusha
  513.         mov     ecx, eax
  514.         lea     eax, [ebx + STREAM_SOCKET.snd]
  515.         call    socket_ring_free
  516.         popa
  517.  
  518. ; Update RTT estimators
  519.  
  520.         test    [temp_bits], TCP_BIT_TIMESTAMP
  521.         jz      .no_timestamp_rtt
  522.         mov     eax, [timestamp]
  523.         sub     eax, [ebx + TCP_SOCKET.ts_ecr]
  524.         inc     eax
  525.         call    tcp_xmit_timer
  526.         jmp     .rtt_done
  527.   .no_timestamp_rtt:
  528.  
  529.         cmp     [ebx + TCP_SOCKET.t_rtt], 0
  530.         je      .rtt_done
  531.         mov     eax, [edx + TCP_header.AckNumber]
  532.         cmp     eax, [ebx + TCP_SOCKET.t_rtseq]
  533.         jbe     .rtt_done
  534.         mov     eax, [ebx + TCP_SOCKET.t_rtt]
  535.         call    tcp_xmit_timer
  536.   .rtt_done:
  537.  
  538. ; update window pointers
  539.  
  540.         mov     eax, [edx + TCP_header.AckNumber]
  541.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  542.  
  543. ; Stop retransmit timer
  544.  
  545.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission
  546.  
  547. ; Unlock the socket
  548.  
  549.         pusha
  550.         lea     ecx, [ebx + SOCKET.mutex]
  551.         call    mutex_unlock
  552.         popa
  553.  
  554. ; Awaken waiting processes
  555.  
  556.         mov     eax, ebx
  557.         call    socket_notify
  558.  
  559. ; Generate more output
  560.  
  561.         call    tcp_output
  562.  
  563.         jmp     .drop_no_socket
  564.  
  565. ;-------------------------------------------------
  566. ; maybe we are the receiver in the uni-xfer then..
  567.  
  568.   .not_sender:
  569.  
  570. ; - The amount of data in the segment is greater than 0 (data count is in ecx)
  571. ; - The acknowledgment field equals the largest unacknowledged sequence number. This means no data is acknowledged by this segment.
  572.  
  573.         mov     eax, [edx + TCP_header.AckNumber]
  574.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  575.         jne     .not_uni_xfer
  576.  
  577. ; - The reassembly list of out-of-order segments for the connection is empty.
  578.  
  579.         cmp     [ebx + TCP_SOCKET.seg_next], 0
  580.         jne     .not_uni_xfer
  581.  
  582. ; Complete processing of received data
  583.  
  584.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction: we are receiving %u bytes\n", ecx
  585.  
  586.         mov     esi, [dataoffset]
  587.         add     esi, edx
  588.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  589.         call    socket_ring_write                       ; Add the data to the socket buffer
  590.         add     [ebx + TCP_SOCKET.RCV_NXT], ecx         ; Update sequence number with number of bytes we have copied
  591.  
  592.         mov     eax, ebx
  593.         call    socket_notify
  594.  
  595.         or      [ebx + TCP_SOCKET.t_flags], TF_DELACK   ; Set delayed ack flag
  596.  
  597.         jmp     .drop
  598.  
  599.  
  600. ;-----------------------------------------------------------------------------------
  601. ;
  602. ; TCP segment processing, the slow way
  603. ;
  604. ;-----------------------------------------------------------------------------------
  605.  
  606.   .not_uni_xfer:
  607.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction failed\n"
  608.  
  609. ; Calculate receive window size
  610.  
  611.         push    edx
  612.         mov     eax, SOCKET_BUFFER_SIZE
  613.         sub     eax, [ebx + STREAM_SOCKET.rcv.size]
  614.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Space in receive buffer=%d\n", eax
  615.         mov     edx, [ebx + TCP_SOCKET.RCV_ADV]
  616.         sub     edx, [ebx + TCP_SOCKET.RCV_NXT]
  617.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Current advertised window=%d\n", edx
  618.         cmp     eax, edx
  619.         jg      @f
  620.         mov     eax, edx
  621.        @@:
  622.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Receive window size=%d\n", eax
  623.         mov     [ebx + TCP_SOCKET.RCV_WND], eax
  624.         pop     edx
  625.  
  626. ; If we are in listen or syn_sent state, go to that specific code right away
  627.  
  628.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_LISTEN
  629.         je      .state_listen
  630.  
  631.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_SYN_SENT
  632.         je      .state_syn_sent
  633.  
  634. ;-----------------------------------------------------------------------------------
  635. ;
  636. ; Trim any data not in window
  637. ;
  638. ;-----------------------------------------------------------------------------------
  639.  
  640. ;-------------------------------------------------
  641. ; Check for duplicate data at beginning of segment
  642.  
  643. ; Calculate number of bytes we need to drop
  644.  
  645.         mov     eax, [ebx + TCP_SOCKET.RCV_NXT]
  646.         sub     eax, [edx + TCP_header.SequenceNumber]
  647.         jle     .no_duplicate
  648.  
  649.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: %u bytes duplicate data!\n", eax
  650.  
  651. ; Check for duplicate SYN
  652.  
  653.         test    [edx + TCP_header.Flags], TH_SYN
  654.         jz      .no_dup_syn
  655.  
  656.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: got duplicate syn\n"
  657.  
  658.         and     [edx + TCP_header.Flags], not (TH_SYN)
  659.         inc     [edx + TCP_header.SequenceNumber]
  660.  
  661.         cmp     [edx + TCP_header.UrgentPointer], 1
  662.         jbe     @f
  663.         dec     [edx + TCP_header.UrgentPointer]
  664.         jmp     .dup_syn
  665.        @@:
  666.         and     [edx + TCP_header.Flags], not (TH_URG)
  667.   .dup_syn:
  668.         dec     eax
  669.   .no_dup_syn:
  670.  
  671. ;-----------------------------------
  672. ; Check for entire duplicate segment
  673.  
  674.         cmp     eax, ecx                ; eax holds number of bytes to drop, ecx is data size
  675.         jb      .no_complete_dup
  676.         jnz     @f
  677.         test    [edx + TCP_header.Flags], TH_FIN
  678.         jnz     .no_complete_dup
  679.        @@:
  680.  
  681. ; Any valid FIN must be to the left of the window.
  682. ; At this point the FIN must be out of sequence or a duplicate, drop it
  683.  
  684.         and     [edx + TCP_header.Flags], not TH_FIN
  685.  
  686. ; send an ACK to resynchronize and drop any data.
  687. ; But keep on processing for RST or ACK
  688.  
  689.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  690.         mov     eax, ecx
  691.  
  692.         inc     [TCPS_rcvduppack]
  693.         add     [TCPS_rcvdupbyte], eax
  694.         jmp     .dup_processed
  695.   .no_complete_dup:
  696.         inc     [TCPS_rcvpartduppack]
  697.         add     [TCPS_rcvpartdupbyte], eax
  698.   .dup_processed:
  699.  
  700. ;-----------------------------------------------
  701. ; Remove duplicate data and update urgent offset
  702.  
  703.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: trimming duplicate data\n"
  704.  
  705. ; Trim data from left side of window
  706.  
  707.         add     [dataoffset], eax
  708.         add     [edx + TCP_header.SequenceNumber], eax
  709.         sub     ecx, eax
  710.  
  711.         sub     [edx + TCP_header.UrgentPointer], ax
  712.         jg      @f
  713.         and     [edx + TCP_header.Flags], not (TH_URG)
  714.         mov     [edx + TCP_header.UrgentPointer], 0
  715.        @@:
  716.   .no_duplicate:
  717.  
  718. ;--------------------------------------------------
  719. ; Handle data that arrives after process terminates
  720.  
  721.         cmp     [ebx + SOCKET.PID], 0                   ;;; TODO: use socket flags instead??
  722.         jne     .not_terminated
  723.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
  724.         jbe     .not_terminated
  725.         test    ecx, ecx
  726.         jz      .not_terminated
  727.  
  728.         mov     eax, ebx
  729.         call    tcp_close
  730.         inc     [TCPS_rcvafterclose]
  731.         jmp     .respond_seg_reset
  732.   .not_terminated:
  733.  
  734. ;----------------------------------------
  735. ; Remove data beyond right edge of window
  736.  
  737.         mov     eax, [edx + TCP_header.SequenceNumber]
  738.         add     eax, ecx
  739.         sub     eax, [ebx + TCP_SOCKET.RCV_NXT]
  740.         sub     eax, [ebx + TCP_SOCKET.RCV_WND]         ; eax now holds the number of bytes to drop
  741.         jle     .no_excess_data
  742.  
  743.         DEBUGF  DEBUG_NETWORK_VERBOSE, "%d bytes beyond right edge of window\n", eax
  744.  
  745.         inc     [TCPS_rcvpackafterwin]
  746.  
  747.         cmp     eax, ecx
  748.         jl      .dont_drop_all
  749.  
  750.         add     [TCPS_rcvbyteafterwin], ecx
  751.  
  752. ;----------------------------------------------------------------------------------------------------
  753. ; If a new connection request is received while in TIME_WAIT, drop the old connection and start over,
  754. ; if the sequence numbers are above the previous ones
  755.  
  756.         test    [edx + TCP_header.Flags], TH_SYN
  757.         jz      .no_new_request
  758.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  759.         jne     .no_new_request
  760. ;        mov     edx, [ebx + TCP_SOCKET.RCV_NXT]
  761. ;        cmp     edx, [edx + TCP_header.SequenceNumber]
  762. ;        add     edx, 64000      ; TCP_ISSINCR   FIXME
  763.         mov     eax, ebx
  764.         call    tcp_close
  765.         jmp     .findpcb        ; FIXME: skip code for unscaling window, ...
  766.   .no_new_request:
  767.  
  768. ; If window is closed, we can only take segments at window edge, and have to drop data and PUSH from
  769. ; incoming segments. Continue processing, but remember to ACK. Otherwise drop segment and ACK
  770.  
  771.         cmp     [ebx + TCP_SOCKET.RCV_WND], 0
  772.         jne     .drop_after_ack
  773.         mov     esi, [edx + TCP_header.SequenceNumber]
  774.         cmp     esi, [ebx + TCP_SOCKET.RCV_NXT]
  775.         jne     .drop_after_ack
  776.  
  777.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  778.         inc     [TCPS_rcvwinprobe]
  779.   .dont_drop_all:
  780.         add     [TCPS_rcvbyteafterwin], eax
  781.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Trimming %u bytes from the right of the window\n"
  782.  
  783. ; remove data from the right side of window (decrease data length)
  784.  
  785.         sub     ecx, eax
  786.         and     [edx + TCP_header.Flags], not (TH_PUSH or TH_FIN)
  787.   .no_excess_data:
  788.  
  789. ;-----------------------------------------------------------------------------------
  790. ;
  791. ; Record timestamp
  792. ;
  793. ;-----------------------------------------------------------------------------------
  794.  
  795. ; If last ACK falls within this segments sequence numbers, record its timestamp
  796.  
  797.         test    [temp_bits], TCP_BIT_TIMESTAMP
  798.         jz      .no_timestamp
  799.         mov     eax, [ebx + TCP_SOCKET.last_ack_sent]
  800.         sub     eax, [edx + TCP_header.SequenceNumber]
  801.         jb      .no_timestamp
  802.         test    [edx + TCP_header.Flags], TH_SYN or TH_FIN      ; SYN and FIN occupy one byte
  803.         jz      @f
  804.         dec     eax
  805.        @@:
  806.         sub     eax, ecx
  807.         jae     .no_timestamp
  808.  
  809.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Recording timestamp\n"
  810.  
  811.         mov     eax, [timestamp]
  812.         mov     [ebx + TCP_SOCKET.ts_recent_age], eax
  813.         mov     eax, [ebx + TCP_SOCKET.ts_val]
  814.         mov     [ebx + TCP_SOCKET.ts_recent], eax
  815.   .no_timestamp:
  816.  
  817. ;-----------------------------------------------------------------------------------
  818. ;
  819. ; Process RST flag
  820. ;
  821. ;-----------------------------------------------------------------------------------
  822.  
  823.         test    [edx + TCP_header.Flags], TH_RST
  824.         jz      .no_rst
  825.  
  826.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Got an RST flag\n"
  827.  
  828.         mov     eax, [ebx + TCP_SOCKET.t_state]
  829.         shl     eax, 2
  830.         jmp     dword [eax + .rst_sw_list]
  831.  
  832. ;-----------------------------------------------------------------------------------
  833.   .rst_sw_list:
  834.         dd      .no_rst         ; TCPS_CLOSED
  835.         dd      .no_rst         ; TCPS_LISTEN
  836.         dd      .no_rst         ; TCPS_SYN_SENT
  837.         dd      .econnrefused   ; TCPS_SYN_RECEIVED
  838.         dd      .econnreset     ; TCPS_ESTABLISHED
  839.         dd      .econnreset     ; TCPS_CLOSE_WAIT
  840.         dd      .econnreset     ; TCPS_FIN_WAIT_1
  841.         dd      .rst_close      ; TCPS_CLOSING
  842.         dd      .rst_close      ; TCPS_LAST_ACK
  843.         dd      .econnreset     ; TCPS_FIN_WAIT_2
  844.         dd      .rst_close      ; TCPS_TIME_WAIT
  845.  
  846. ;-----------------------------------------------------------------------------------
  847.   .econnrefused:
  848.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Connection refused\n"
  849.         mov     [ebx + SOCKET.errorcode], ECONNREFUSED
  850.         jmp     .close
  851.  
  852. ;-----------------------------------------------------------------------------------
  853.   .econnreset:
  854.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Connection reset\n"
  855.         mov     [ebx + SOCKET.errorcode], ECONNRESET
  856.   .close:
  857.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Closing connection\n"
  858.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
  859.         inc     [TCPS_drops]
  860.  
  861.  
  862.         mov     eax, ebx
  863.         call    tcp_close
  864.         jmp     .drop_no_socket
  865.  
  866. ;-----------------------------------------------------------------------------------
  867.   .rst_close:
  868.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Closing with reset\n"
  869.  
  870.         mov     eax, ebx
  871.         call    tcp_close
  872.         jmp     .drop_no_socket
  873.  
  874. ;-----------------------------------------------------------------------------------
  875.   .no_rst:
  876.  
  877. ;-----------------------------------------------------------------------------------
  878. ;
  879. ; Handle SYN-full and ACK-less segments
  880. ;
  881. ;-----------------------------------------------------------------------------------
  882.  
  883. ; If a SYN is in the window, then this is an error so we send an RST and drop the connection
  884.  
  885.         test    [edx + TCP_header.Flags], TH_SYN
  886.         jz      .not_syn_full
  887.  
  888.         mov     eax, ebx
  889.         mov     ebx, ECONNRESET
  890.         call    tcp_drop
  891.         jmp     .drop_with_reset
  892.   .not_syn_full:
  893.  
  894. ; If ACK bit is off, we drop the segment and return
  895.  
  896.         test    [edx + TCP_header.Flags], TH_ACK
  897.         jz      .drop
  898.  
  899. ;----------------------------------------------------------------------------------
  900. ;
  901. ; ACK processing for SYN_RECEIVED state
  902. ;
  903. ;----------------------------------------------------------------------------------
  904.  
  905.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  906.         jb      .ack_processed                                  ; states: closed, listen, syn_sent
  907.         ja      .no_syn_rcv                                     ; established, fin_wait_1, fin_wait_2, close_wait, closing, last_ack, time_wait
  908.  
  909.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_received\n"
  910.  
  911.         mov     eax, [edx + TCP_header.AckNumber]
  912.         cmp     [ebx + TCP_SOCKET.SND_UNA], eax
  913.         ja      .drop_with_reset
  914.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  915.         ja      .drop_with_reset
  916.  
  917.         inc     [TCPS_connects]
  918.  
  919.         mov     eax, ebx
  920.         call    socket_is_connected
  921.         mov     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  922.  
  923. ; Do window scaling?
  924.  
  925.         test    [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  926.         jz      @f
  927.         test    [ebx + TCP_SOCKET.t_flags], TF_REQ_SCALE
  928.         jz      @f
  929.  
  930.         push    word[ebx + TCP_SOCKET.requested_s_scale]        ; Set send and receive scale factors to the received values
  931.         pop     word[ebx + TCP_SOCKET.SND_SCALE]
  932.        @@:
  933.  
  934.         call    tcp_reassemble
  935.  
  936.         mov     eax, [edx + TCP_header.SequenceNumber]
  937.         dec     eax
  938.         mov     [ebx + TCP_SOCKET.SND_WL1], eax
  939.   .no_syn_rcv:
  940.  
  941. ;-----------------------------------------------------------------------------------
  942. ;
  943. ; ACK processing for SYN_RECEIVED state and higher
  944. ;
  945. ;-----------------------------------------------------------------------------------
  946.  
  947. ;-------------------------
  948. ; Check for duplicate ACKs
  949.  
  950.         mov     eax, [edx + TCP_header.AckNumber]
  951.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  952.         ja      .dup_ack_complete
  953.  
  954.         test    ecx, ecx
  955.         jnz     .reset_dupacks
  956.  
  957.         mov     eax, dword[edx + TCP_header.Window]
  958.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  959.         jne     .reset_dupacks
  960.  
  961.         inc     [TCPS_rcvdupack]
  962.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Processing duplicate ACK\n"
  963.  
  964. ; If we have outstanding data, other than a window probe, this is a completely duplicate ACK
  965. ; (window info didnt change) The ACK is the biggest we've seen and we've seen exactly our rexmt threshold of them,
  966. ; assume a packet has been dropped and retransmit it. Kludge snd_nxt & the congestion window so we send only this one packet.
  967.  
  968.         test    [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission
  969.         jz      .reset_dupacks
  970.  
  971.         mov     eax, [edx + TCP_header.AckNumber]
  972.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  973.         jne     .reset_dupacks
  974.  
  975. ; Increment dupplicat ACK counter
  976. ; If it reaches the threshold, re-transmit the missing segment
  977.  
  978.         inc     [ebx + TCP_SOCKET.t_dupacks]
  979.         cmp     [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
  980.         jb      .dup_ack_complete
  981.         ja      .another_lost
  982.  
  983.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Re-transmitting lost segment\n"
  984.  
  985.         push    [ebx + TCP_SOCKET.SND_NXT]              ; >>>>
  986.  
  987.         mov     eax, [ebx + TCP_SOCKET.SND_WND]
  988.         cmp     eax, [ebx + TCP_SOCKET.SND_CWND]
  989.         jbe     @f
  990.         mov     eax, [ebx + TCP_SOCKET.SND_CWND]
  991.   @@:
  992.         shr     eax, 1
  993.         push    edx
  994.         xor     edx, edx
  995.         div     [ebx + TCP_SOCKET.t_maxseg]
  996.         cmp     eax, 2
  997.         ja      @f
  998.         xor     eax, eax
  999.         mov     al, 2
  1000.        @@:
  1001.         mul     [ebx + TCP_SOCKET.t_maxseg]
  1002.         pop     edx
  1003.         mov     [ebx + TCP_SOCKET.SND_SSTHRESH], eax
  1004.  
  1005.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission   ; turn off retransmission timer
  1006.         mov     [ebx + TCP_SOCKET.t_rtt], 0
  1007.         mov     eax, [edx + TCP_header.AckNumber]
  1008.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1009.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1010.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1011.  
  1012. ; Unlock the socket
  1013.  
  1014.         push    ebx
  1015.         lea     ecx, [ebx + SOCKET.mutex]
  1016.         call    mutex_unlock
  1017.  
  1018. ; retransmit missing segment
  1019.  
  1020.         mov     eax, [esp]
  1021.         call    tcp_output
  1022.  
  1023. ; Lock the socket again
  1024.  
  1025.         mov     ecx, [esp]
  1026.         add     ecx, SOCKET.mutex
  1027.         call    mutex_lock
  1028.         pop     ebx
  1029.  
  1030. ; Continue processing
  1031.  
  1032.         xor     edx, edx
  1033.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1034.         mul     [ebx + TCP_SOCKET.t_dupacks]
  1035.         add     eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1036.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1037.  
  1038.         pop     eax                                     ; <<<<
  1039.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1040.         jb      @f
  1041.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1042.        @@:
  1043.         jmp     .drop
  1044.  
  1045.   .another_lost:
  1046.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Increasing congestion window\n"
  1047.  
  1048.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1049.         add     [ebx + TCP_SOCKET.SND_CWND], eax
  1050.  
  1051. ; Unlock the socket
  1052.  
  1053.         push    ebx
  1054.         lea     ecx, [ebx + SOCKET.mutex]
  1055.         call    mutex_unlock
  1056.  
  1057. ; retransmit missing segment, again
  1058.  
  1059.         mov     eax, [esp]
  1060.         call    tcp_output
  1061.  
  1062. ; Lock the socket again
  1063.  
  1064.         mov     ecx, [esp]
  1065.         add     ecx, SOCKET.mutex
  1066.         call    mutex_lock
  1067.         pop     ebx
  1068.  
  1069. ; And drop the incoming segment
  1070.  
  1071.         jmp     .drop
  1072.  
  1073.   .reset_dupacks:               ; We got a new ACK, reset duplicate ACK counter
  1074.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  1075.         jmp     .ack_processed
  1076.  
  1077.   .dup_ack_complete:
  1078.  
  1079. ;-------------------------------------------------
  1080. ; If the congestion window was inflated to account
  1081. ; for the other side's cached packets, retract it
  1082.  
  1083.         mov     eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1084.         cmp     eax, [ebx + TCP_SOCKET.SND_CWND]
  1085.         ja      @f
  1086.         cmp     [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
  1087.         jbe     @f
  1088.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1089.        @@:
  1090.  
  1091.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  1092.  
  1093.         mov     eax, [edx + TCP_header.AckNumber]
  1094.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  1095.         jbe     @f
  1096.         inc     [TCPS_rcvacktoomuch]
  1097.         jmp     .drop_after_ack
  1098.        @@:
  1099.  
  1100.         mov     edi, [edx + TCP_header.AckNumber]
  1101.         sub     edi, [ebx + TCP_SOCKET.SND_UNA]         ; now we got the number of acked bytes in edi
  1102.         inc     [TCPS_rcvackpack]
  1103.         add     [TCPS_rcvackbyte], edi
  1104.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: acceptable ACK for %u bytes\n", edi
  1105.  
  1106. ;-----------------------------------------------------------------------------------
  1107. ;
  1108. ; RTT measurements and retransmission timer
  1109. ;
  1110. ;-----------------------------------------------------------------------------------
  1111.  
  1112. ; If we have a timestamp, update smoothed RTT
  1113.  
  1114.         test    [temp_bits], TCP_BIT_TIMESTAMP
  1115.         jz      .timestamp_not_present
  1116.         mov     eax, [timestamp]
  1117.         sub     eax, [ebx + TCP_SOCKET.ts_ecr]
  1118.         inc     eax
  1119.         call    tcp_xmit_timer
  1120.         jmp     .rtt_done_
  1121.  
  1122. ; If no timestamp but transmit timer is running and timed sequence number was acked,
  1123. ; update smoothed RTT. Since we now have an RTT measurement, cancel the timer backoff
  1124. ; (Phil Karn's retransmit algo)
  1125. ; Recompute the initial retransmit timer
  1126.  
  1127.   .timestamp_not_present:
  1128.         mov     eax, [edx + TCP_header.AckNumber]
  1129.         cmp     eax, [ebx + TCP_SOCKET.t_rtseq]
  1130.         jbe     .rtt_done_
  1131.         mov     eax, [ebx + TCP_SOCKET.t_rtt]
  1132.         test    eax, eax
  1133.         jz      .rtt_done_
  1134.         call    tcp_xmit_timer
  1135.   .rtt_done_:
  1136.  
  1137. ; If all outstanding data is acked, stop retransmit timer and remember to restart (more output or persist)
  1138. ; If there is more data to be acked, restart retransmit timer, using current (possible backed-off) value.
  1139.  
  1140.         mov     eax, [ebx + TCP_SOCKET.SND_MAX]
  1141.         cmp     eax, [edx + TCP_header.AckNumber]
  1142.         jne     .more_data
  1143.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission
  1144.         or      [temp_bits], TCP_BIT_NEEDOUTPUT
  1145.         jmp     .no_restart
  1146.   .more_data:
  1147.         test    [ebx + TCP_SOCKET.timer_flags], timer_flag_persist
  1148.         jnz     .no_restart
  1149.  
  1150.         mov     eax, [ebx + TCP_SOCKET.t_rxtcur]
  1151.         mov     [ebx + TCP_SOCKET.timer_retransmission], eax
  1152.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission
  1153.   .no_restart:
  1154.  
  1155. ;-----------------------------------------------------------------------------------
  1156. ;
  1157. ; Open congestion window in response to ACKs
  1158. ;
  1159. ;-----------------------------------------------------------------------------------
  1160.  
  1161. ; If the window gives us less then sstresh packets in flight, open exponentially.
  1162. ; Otherwise, open lineary
  1163.  
  1164.         mov     esi, [ebx + TCP_SOCKET.SND_CWND]
  1165.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1166.         cmp     esi, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1167.         jbe     @f
  1168.         push    edx
  1169.         push    eax
  1170.         mul     eax             ; t_maxseg*t_maxseg
  1171.         div     esi             ; t_maxseg*t_maxseg/snd_cwnd
  1172.         pop     edx             ; t_maxseg
  1173.         shr     edx, 3          ; t_maxseg/8
  1174.         add     eax, edx        ; t_maxseg*t_maxseg/snd_cwnd + t_maxseg/8
  1175.         pop     edx
  1176.        @@:
  1177.         add     esi, eax
  1178.  
  1179.         push    ecx
  1180.         mov     cl, [ebx + TCP_SOCKET.SND_SCALE]
  1181.         mov     eax, TCP_max_win
  1182.         shl     eax, cl
  1183.         pop     ecx
  1184.  
  1185.         cmp     esi, eax
  1186.         jbe     @f
  1187.         mov     esi, eax
  1188.   @@:
  1189.         mov     [ebx + TCP_SOCKET.SND_CWND], esi
  1190.  
  1191. ;-----------------------------------------------------------------------------------
  1192. ;
  1193. ; Remove acknowledged data from send buffer
  1194. ;
  1195. ;-----------------------------------------------------------------------------------
  1196.  
  1197. ; If the number of bytes acknowledged exceeds the number of bytes on the send buffer,
  1198. ; snd_wnd is decremented by the number of bytes in the send buffer and TCP knows
  1199. ; that its FIN has been ACKed. (FIN occupies 1 byte in the sequence number space)
  1200.  
  1201.         cmp     edi, [ebx + STREAM_SOCKET.snd.size]
  1202.         jbe     .no_fin_ack
  1203.  
  1204. ; Drop all data in output buffer
  1205.  
  1206.         push    ecx edx ebx
  1207.         mov     ecx, [ebx + STREAM_SOCKET.snd.size]
  1208.         sub     [ebx + TCP_SOCKET.SND_WND], ecx
  1209.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1210.         call    socket_ring_free
  1211.         pop     ebx edx ecx
  1212.  
  1213.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is acked\n"
  1214.         or      [temp_bits], TCP_BIT_FIN_IS_ACKED
  1215.         jmp     .ack_complete
  1216.   .no_fin_ack:
  1217.  
  1218. ; Drop acknowledged data
  1219.  
  1220.         push    ecx edx ebx
  1221.         mov     ecx, edi
  1222.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1223.         call    socket_ring_free
  1224.         pop     ebx
  1225.         sub     [ebx + TCP_SOCKET.SND_WND], ecx
  1226.         pop     edx ecx
  1227.   .ack_complete:
  1228.  
  1229. ;-----------------------------------------------------------------------------------
  1230. ;
  1231. ; Wake up process waiting on send buffer
  1232. ;
  1233. ;-----------------------------------------------------------------------------------
  1234.  
  1235.         mov     eax, ebx
  1236.         call    socket_notify
  1237.  
  1238. ; Update TCPS
  1239.  
  1240.         mov     eax, [edx + TCP_header.AckNumber]
  1241.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  1242.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1243.         jb      @f
  1244.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1245.        @@:
  1246.  
  1247. ;-----------------------------------------------------------------------------------
  1248. ;
  1249. ; State specific ACK handeling
  1250. ;
  1251. ;-----------------------------------------------------------------------------------
  1252.  
  1253.         mov     eax, [ebx + TCP_SOCKET.t_state]
  1254.         jmp     dword[.ack_sw_list+eax*4]
  1255.  
  1256.   .ack_sw_list:
  1257.         dd      .ack_processed  ; TCPS_CLOSED
  1258.         dd      .ack_processed  ; TCPS_LISTEN
  1259.         dd      .ack_processed  ; TCPS_SYN_SENT
  1260.         dd      .ack_processed  ; TCPS_SYN_RECEIVED
  1261.         dd      .ack_processed  ; TCPS_ESTABLISHED
  1262.         dd      .ack_processed  ; TCPS_CLOSE_WAIT
  1263.         dd      .ack_fw1        ; TCPS_FIN_WAIT_1
  1264.         dd      .ack_c          ; TCPS_CLOSING
  1265.         dd      .ack_la         ; TCPS_LAST_ACK
  1266.         dd      .ack_processed  ; TCPS_FIN_WAIT_2
  1267.         dd      .ack_tw         ; TCPS_TIMED_WAIT
  1268.  
  1269. ;-----------------------------------------------------------------------------------
  1270.   .ack_fw1:
  1271. ; If our FIN is now acked, enter FIN_WAIT_2
  1272.  
  1273.         test    [temp_bits], TCP_BIT_FIN_IS_ACKED
  1274.         jz      .ack_processed
  1275.  
  1276. ; If we can't receive any more data, then closing user can proceed.
  1277. ; Starting the timer is contrary to the specification, but if we dont get a FIN,
  1278. ; we'll hang forever.
  1279.  
  1280.         test    [ebx + SOCKET.state], SS_CANTRCVMORE
  1281.         jnz     @f
  1282.         mov     eax, ebx
  1283.         call    socket_is_disconnected
  1284.         mov     [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle
  1285.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
  1286.        @@:
  1287.         mov     [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
  1288.         jmp     .ack_processed
  1289.  
  1290. ;-----------------------------------------------------------------------------------
  1291.   .ack_c:
  1292. ; Enter the TIME_WAIT state if our FIN is acked in CLOSED state.
  1293.  
  1294.         test    [temp_bits], TCP_BIT_FIN_IS_ACKED
  1295.         jz      .ack_processed
  1296.  
  1297.         mov     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1298.         mov     eax, ebx
  1299.         call    tcp_cancel_timers
  1300.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1301.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
  1302.         mov     eax, ebx
  1303.         call    socket_is_disconnected
  1304.         jmp     .ack_processed
  1305.  
  1306. ;-----------------------------------------------------------------------------------
  1307.   .ack_la:
  1308. ; In LAST_ACK state, we may still be waiting for data to drain and/or to be acked.
  1309. ; If our FIN is acked however, enter CLOSED state and return.
  1310.  
  1311.         test    [temp_bits], TCP_BIT_FIN_IS_ACKED
  1312.         jz      .ack_processed
  1313.  
  1314.         push    ebx
  1315.         lea     ecx, [ebx + SOCKET.mutex]
  1316.         call    mutex_unlock
  1317.         pop     eax
  1318.  
  1319.         call    tcp_close
  1320.         jmp     .drop_no_socket
  1321.  
  1322. ;-----------------------------------------------------------------------------------
  1323.   .ack_tw:
  1324. ; In TIME_WAIT state the only thing that should arrive is a retransmission of the remote FIN.
  1325. ; Acknowledge it and restart the FINACK timer
  1326.  
  1327.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2*TCP_time_MSL
  1328.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_2msl
  1329.         jmp     .drop_after_ack
  1330.  
  1331. ;-----------------------------------------------------------------------------------
  1332. ;
  1333. ; Initiation of Passive Open?
  1334. ;
  1335. ;-----------------------------------------------------------------------------------
  1336.  
  1337.   .state_listen:
  1338.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: state=listen\n"
  1339.  
  1340.         test    [edx + TCP_header.Flags], TH_RST
  1341.         jnz     .drop
  1342.  
  1343.         test    [edx + TCP_header.Flags], TH_ACK
  1344.         jnz     .drop_with_reset
  1345.  
  1346.         test    [edx + TCP_header.Flags], TH_SYN
  1347.         jz      .drop
  1348.  
  1349.         inc     [TCPS_accepts]
  1350.  
  1351. ;;; TODO: check if it's a broadcast or multicast, and drop if so
  1352.  
  1353. ;-------------------------------------------
  1354. ; Processing of SYN received in LISTEN state
  1355.  
  1356.         push    [edi + IPv4_header.SourceAddress]
  1357.         pop     [ebx + IP_SOCKET.RemoteIP]
  1358.  
  1359.         push    [edx + TCP_header.SourcePort]
  1360.         pop     [ebx + TCP_SOCKET.RemotePort]
  1361.  
  1362.         push    [edx + TCP_header.SequenceNumber]
  1363.         pop     [ebx + TCP_SOCKET.IRS]
  1364.  
  1365.         mov     eax, [TCP_sequence_num]
  1366.         add     [TCP_sequence_num], TCP_ISSINCR / 2
  1367.         mov     [ebx + TCP_SOCKET.ISS], eax
  1368.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1369.  
  1370.         tcp_sendseqinit ebx
  1371.         tcp_rcvseqinit ebx
  1372.  
  1373.         mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  1374.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1375.         mov     [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval  ;;;; macro
  1376.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
  1377.  
  1378.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1379.         call    socket_ring_create
  1380.         test    eax, eax
  1381.         jz      .drop
  1382.  
  1383.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  1384.         call    socket_ring_create
  1385.         test    eax, eax
  1386.         jz      .drop
  1387.  
  1388.         and     [temp_bits], not TCP_BIT_DROPSOCKET
  1389.  
  1390.         pusha
  1391.         mov     eax, ebx
  1392.         call    socket_notify
  1393.         popa
  1394.  
  1395.         jmp     .trim
  1396.  
  1397. ;-----------------------------------------------------------------------------------
  1398. ;
  1399. ; Completion of active open?
  1400. ;
  1401. ;-----------------------------------------------------------------------------------
  1402.  
  1403.   .state_syn_sent:
  1404.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_sent\n"
  1405.  
  1406.         test    [edx + TCP_header.Flags], TH_ACK
  1407.         jz      @f
  1408.  
  1409.         mov     eax, [edx + TCP_header.AckNumber]
  1410.         cmp     eax, [ebx + TCP_SOCKET.ISS]
  1411.         jbe     .drop_with_reset
  1412.  
  1413.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  1414.         ja      .drop_with_reset
  1415.        @@:
  1416.  
  1417.         test    [edx + TCP_header.Flags], TH_RST
  1418.         jz      @f
  1419.  
  1420.         test    [edx + TCP_header.Flags], TH_ACK
  1421.         jz      .drop
  1422.  
  1423.         mov     eax, ebx
  1424.         mov     ebx, ECONNREFUSED
  1425.         call    tcp_drop
  1426.         jmp     .drop
  1427.        @@:
  1428.  
  1429. ;-----------------------------------------------------------------------------------
  1430. ;
  1431. ; Process received SYN in response to an active open
  1432. ;
  1433. ;-----------------------------------------------------------------------------------
  1434.  
  1435.         test    [edx + TCP_header.Flags], TH_SYN
  1436.         jz      .drop
  1437.  
  1438.         test    [edx + TCP_header.Flags], TH_ACK
  1439.         jz      @f
  1440.  
  1441.         mov     eax, [edx + TCP_header.AckNumber]
  1442.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  1443.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1444.         jbe     @f
  1445.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1446.  
  1447.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission   ; disable retransmission timer
  1448.        @@:
  1449.  
  1450.         push    [edx + TCP_header.SequenceNumber]
  1451.         pop     [ebx + TCP_SOCKET.IRS]
  1452.  
  1453.         tcp_rcvseqinit ebx
  1454.  
  1455.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1456.  
  1457.         mov     eax, [ebx + TCP_SOCKET.SND_UNA]
  1458.         cmp     eax, [ebx + TCP_SOCKET.ISS]
  1459.         jbe     .simultaneous_open
  1460.  
  1461.         test    [edx + TCP_header.Flags], TH_ACK
  1462.         jz      .simultaneous_open
  1463.  
  1464.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: active open\n"
  1465.  
  1466.         inc     [TCPS_connects]
  1467.  
  1468. ; set socket state to connected
  1469.  
  1470.         push    eax
  1471.         mov     eax, ebx
  1472.         call    socket_is_connected
  1473.         pop     eax
  1474.         mov     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  1475.  
  1476. ; Do window scaling on this connection ?
  1477.  
  1478.         mov     eax, [ebx + TCP_SOCKET.t_flags]
  1479.         and     eax, TF_REQ_SCALE or TF_RCVD_SCALE
  1480.         cmp     eax, TF_REQ_SCALE or TF_RCVD_SCALE
  1481.         jne     .no_scaling
  1482.  
  1483.         mov     ax, word[ebx + TCP_SOCKET.requested_s_scale]
  1484.         mov     word[ebx + TCP_SOCKET.SND_SCALE], ax
  1485.   .no_scaling:
  1486.  
  1487. ;;; TODO: reassemble packets queue
  1488.  
  1489. ; If we didnt have time to re-transmit the SYN,
  1490. ; Use its rtt as our initial srtt & rtt var.
  1491.  
  1492.         mov     eax, [ebx + TCP_SOCKET.t_rtt]
  1493.         test    eax, eax
  1494.         je      .trim
  1495.         call    tcp_xmit_timer
  1496.         jmp     .trim
  1497.  
  1498. ;-----------------------------------------------------------------------------------
  1499. ;
  1500. ; Simultaneous open (We have received a SYN but no ACK)
  1501. ;
  1502. ;-----------------------------------------------------------------------------------
  1503.  
  1504.   .simultaneous_open:
  1505.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: simultaneous open\n"
  1506.         mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  1507.  
  1508. ;-----------------------------------------------------------------------------------
  1509. ;
  1510. ; Common processing for receipt of SYN
  1511. ;
  1512. ;-----------------------------------------------------------------------------------
  1513.  
  1514.   .trim:
  1515. ; Advance sequence number to correspond to first data byte.
  1516. ; If data, trim to stay within window, dropping FIN if necessary
  1517.  
  1518.         inc     [edx + TCP_header.SequenceNumber]
  1519.  
  1520. ; Drop any received data that doesnt fit in the receive window.
  1521.  
  1522.         cmp     ecx, [ebx + TCP_SOCKET.RCV_WND]
  1523.         jbe     .dont_trim
  1524.  
  1525.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: received data does not fit in window, trimming %u bytes\n", eax
  1526.         inc     [TCPS_rcvpackafterwin]
  1527.         sub     ecx, [ebx + TCP_SOCKET.RCV_WND]
  1528.         add     [TCPS_rcvbyteafterwin], ecx
  1529.  
  1530.         and     [edx + TCP_header.Flags], not (TH_FIN)
  1531.         mov     ecx, [ebx + TCP_SOCKET.RCV_WND]
  1532.  
  1533.   .dont_trim:
  1534.         mov     eax, [edx + TCP_header.SequenceNumber]
  1535.         mov     [ebx + TCP_SOCKET.RCV_UP], eax
  1536.         dec     eax
  1537.         mov     [ebx + TCP_SOCKET.SND_WL1], eax
  1538.  
  1539. ;-----------------------------------------------------------------------------------
  1540. ;
  1541. ; Update window information (step 6 in RFC793)
  1542. ;
  1543. ;-----------------------------------------------------------------------------------
  1544.  
  1545.   .ack_processed:
  1546.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK processed\n"
  1547.  
  1548. ; dont look at window if no ACK
  1549.  
  1550.         test    [edx + TCP_header.Flags], TH_ACK
  1551.         jz      .no_window_update
  1552.  
  1553. ; Does the segment contain new data?
  1554.  
  1555.         mov     eax, [ebx + TCP_SOCKET.SND_WL1]
  1556.         cmp     eax, [edx + TCP_header.SequenceNumber]
  1557.         jb      .update_window
  1558.         ja      @f
  1559.  
  1560. ; No new data but a new ACK ?
  1561.  
  1562.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1563.         cmp     eax, [edx + TCP_header.AckNumber]
  1564.         jb      .update_window
  1565.        @@:
  1566.  
  1567. ; No new data or ACK but advertised window is larger then current window?
  1568.  
  1569.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1570.         cmp     eax, [edx + TCP_header.AckNumber]
  1571.         jne     .no_window_update
  1572.  
  1573.         mov     eax, dword[edx + TCP_header.Window]
  1574.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  1575.         jbe     .no_window_update
  1576.  
  1577.  
  1578. ; Keep track of pure window updates
  1579.   .update_window:
  1580.         test    ecx, ecx
  1581.         jnz     @f
  1582.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1583.         cmp     eax, [edx + TCP_header.AckNumber]
  1584.         jne     @f
  1585.         mov     eax, dword[edx + TCP_header.Window]
  1586.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  1587.         jbe     @f
  1588.         inc     [TCPS_rcvwinupd]
  1589.        @@:
  1590.  
  1591.         mov     eax, dword[edx + TCP_header.Window]
  1592.         mov     [ebx + TCP_SOCKET.SND_WND], eax
  1593.         cmp     eax, [ebx + TCP_SOCKET.max_sndwnd]
  1594.         jbe     @f
  1595.         mov     [ebx + TCP_SOCKET.max_sndwnd], eax
  1596.        @@:
  1597.  
  1598.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Updating window to %u\n", eax
  1599.  
  1600.         push    [edx + TCP_header.SequenceNumber]
  1601.         pop     [ebx + TCP_SOCKET.SND_WL1]
  1602.  
  1603.         push    [edx + TCP_header.AckNumber]
  1604.         pop     [ebx + TCP_SOCKET.SND_WL2]
  1605.  
  1606.         or      [temp_bits], TCP_BIT_NEEDOUTPUT
  1607.   .no_window_update:
  1608.  
  1609. ;-----------------------------------------------------------------------------------
  1610. ;
  1611. ; Process URG flag
  1612. ;
  1613. ;-----------------------------------------------------------------------------------
  1614.  
  1615.         test    [edx + TCP_header.Flags], TH_URG
  1616.         jz      .not_urgent
  1617.  
  1618.         cmp     [edx + TCP_header.UrgentPointer], 0
  1619.         jz      .not_urgent
  1620.  
  1621.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1622.         je      .not_urgent
  1623.  
  1624. ; Ignore bogus urgent offsets
  1625.  
  1626.         movzx   eax, [edx + TCP_header.UrgentPointer]
  1627.         add     eax, [ebx + STREAM_SOCKET.rcv.size]
  1628.         cmp     eax, SOCKET_BUFFER_SIZE
  1629.         jbe     .not_urgent
  1630.  
  1631.         mov     [edx + TCP_header.UrgentPointer], 0
  1632.         and     [edx + TCP_header.Flags], not (TH_URG)
  1633.         jmp     .do_data
  1634.  
  1635.   .not_urgent:
  1636.  
  1637. ; processing of received urgent pointer
  1638.  
  1639.         ;;; TODO (1051-1093)
  1640.  
  1641. ;-----------------------------------------------------------------------------------
  1642. ;
  1643. ; Process the data
  1644. ;
  1645. ;-----------------------------------------------------------------------------------
  1646.  
  1647.   .do_data:
  1648.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1649.         jae     .final_processing
  1650.  
  1651.         test    [edx + TCP_header.Flags], TH_FIN
  1652.         jnz     @f
  1653.  
  1654.         test    ecx, ecx
  1655.         jz      .final_processing
  1656.        @@:
  1657.  
  1658. ; The segment is in order?
  1659.  
  1660.         mov     eax, [edx + TCP_header.SequenceNumber]
  1661.         cmp     eax, [ebx + TCP_SOCKET.RCV_NXT]
  1662.         jne     .out_of_order
  1663.  
  1664. ; The reassembly queue is empty?
  1665.  
  1666.         cmp     [ebx + TCP_SOCKET.seg_next], 0
  1667.         jne     .out_of_order
  1668.  
  1669. ; The connection is established?
  1670.  
  1671.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  1672.         jne     .out_of_order
  1673.  
  1674. ; Ok, lets do this..  Set delayed ACK flag and copy data into socket buffer
  1675.  
  1676.         or      [ebx + TCP_SOCKET.t_flags], TF_DELACK
  1677.  
  1678.         pusha
  1679.         mov     esi, [dataoffset]
  1680.         add     esi, edx
  1681.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  1682.         call    socket_ring_write                       ; Add the data to the socket buffer
  1683.         add     [ebx + TCP_SOCKET.RCV_NXT], ecx         ; Update sequence number with number of bytes we have copied
  1684.         popa
  1685.  
  1686. ; Wake up the sleeping process
  1687.  
  1688.         mov     eax, ebx
  1689.         call    socket_notify
  1690.  
  1691.         jmp     .data_done
  1692.  
  1693.   .out_of_order:
  1694.         DEBUGF  DEBUG_NETWORK_VERBOSE,  "TCP data is out of order!\nSequencenumber is %u, we expected %u.\n", \
  1695.         [edx + TCP_header.SequenceNumber], [ebx + TCP_SOCKET.RCV_NXT]
  1696.  
  1697. ; Uh-oh, some data is out of order, lets call TCP reassemble for help
  1698.  
  1699.         call    tcp_reassemble
  1700.  
  1701. ; Generate ACK immediately, to let the other end know that a segment was received out of order,
  1702. ; and to tell it what sequence number is expected. This aids the fast-retransmit algorithm.
  1703.  
  1704.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1705.   .data_done:
  1706.  
  1707. ;-----------------------------------------------------------------------------------
  1708. ;
  1709. ; Process FIN
  1710. ;
  1711. ;-----------------------------------------------------------------------------------
  1712.  
  1713.         test    [edx + TCP_header.Flags], TH_FIN
  1714.         jz      .final_processing
  1715.  
  1716.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Processing FIN\n"
  1717.  
  1718.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1719.         jae     .not_first_fin
  1720.  
  1721.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: First FIN for this connection\n"
  1722.  
  1723.         mov     eax, ebx
  1724.         call    socket_cant_recv_more
  1725.  
  1726.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1727.         inc     [ebx + TCP_SOCKET.RCV_NXT]
  1728.  
  1729.   .not_first_fin:
  1730.         mov     eax, [ebx + TCP_SOCKET.t_state]
  1731.         jmp     dword[.fin_sw_list+eax*4]
  1732.  
  1733.   .fin_sw_list:
  1734.         dd      .final_processing       ; TCPS_CLOSED
  1735.         dd      .final_processing       ; TCPS_LISTEN
  1736.         dd      .final_processing       ; TCPS_SYN_SENT
  1737.         dd      .fin_syn_est            ; TCPS_SYN_RECEIVED
  1738.         dd      .fin_syn_est            ; TCPS_ESTABLISHED
  1739.         dd      .final_processing       ; TCPS_CLOSE_WAIT
  1740.         dd      .fin_wait1              ; TCPS_FIN_WAIT_1
  1741.         dd      .final_processing       ; TCPS_CLOSING
  1742.         dd      .final_processing       ; TCPS_LAST_ACK
  1743.         dd      .fin_wait2              ; TCPS_FIN_WAIT_2
  1744.         dd      .fin_timed              ; TCPS_TIMED_WAIT
  1745.  
  1746. ;-----------------------------------------------------------------------------------
  1747.   .fin_syn_est:
  1748. ; In SYN_RECEIVED and ESTABLISHED state, enter the CLOSE_WAIT state
  1749.  
  1750.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
  1751.         jmp     .final_processing
  1752.  
  1753. ;-----------------------------------------------------------------------------------
  1754.   .fin_wait1:
  1755. ; From FIN_WAIT_1 state, enter CLOSING state (our FIN has not been ACKed)
  1756.  
  1757.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSING
  1758.         jmp     .final_processing
  1759.  
  1760. ;-----------------------------------------------------------------------------------
  1761.   .fin_wait2:
  1762. ; From FIN_WAIT_2 state, enter TIME_WAIT state and start the timer
  1763.  
  1764.         mov     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1765.         mov     eax, ebx
  1766.         call    tcp_cancel_timers
  1767.         call    socket_is_disconnected
  1768.  
  1769. ;-----------------------------------------------------------------------------------
  1770.   .fin_timed:
  1771. ; (re)start the 2 MSL timer
  1772.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1773.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
  1774.  
  1775. ;-----------------------------------------------------------------------------------
  1776. ;
  1777. ; Finally, drop the segment
  1778. ;
  1779. ;-----------------------------------------------------------------------------------
  1780.  
  1781.   .final_processing:
  1782.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Final processing\n"
  1783.  
  1784.         push    ebx
  1785.         lea     ecx, [ebx + SOCKET.mutex]
  1786.         call    mutex_unlock
  1787.         pop     eax
  1788.  
  1789.         test    [temp_bits], TCP_BIT_NEEDOUTPUT
  1790.         jnz     .need_output
  1791.  
  1792.         test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW
  1793.         jz      .done
  1794.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK now!\n"
  1795.  
  1796.   .need_output:
  1797.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: need output\n"
  1798.         call    tcp_output
  1799.  
  1800.   .done:
  1801.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: dumping\n"
  1802.  
  1803.         call    net_buff_free
  1804.         jmp     .loop
  1805.  
  1806. ;-----------------------------------------------------------------------------------
  1807. ;
  1808. ; Drop segment, reply with an RST segment when needed
  1809. ;
  1810. ;-----------------------------------------------------------------------------------
  1811.  
  1812. ;-----------------------------------------------------------------------------------
  1813.   .drop_after_ack:
  1814.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Drop after ACK\n"
  1815.  
  1816.         push    edx ebx
  1817.         lea     ecx, [ebx + SOCKET.mutex]
  1818.         call    mutex_unlock
  1819.         pop     eax edx
  1820.  
  1821.         test    [edx + TCP_header.Flags], TH_RST
  1822.         jnz     .done
  1823.  
  1824.         or      [eax + TCP_SOCKET.t_flags], TF_ACKNOW
  1825.         jmp     .need_output
  1826.  
  1827. ;-----------------------------------------------------------------------------------
  1828.   .drop_with_reset:
  1829.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Drop with reset\n"
  1830.  
  1831.         push    ebx edx
  1832.         lea     ecx, [ebx + SOCKET.mutex]
  1833.         call    mutex_unlock
  1834.         pop     edx ebx
  1835.  
  1836.         test    [edx + TCP_header.Flags], TH_RST
  1837.         jnz     .done
  1838.  
  1839. ; TODO: if its a multicast/broadcast, also drop
  1840.  
  1841.         test    [edx + TCP_header.Flags], TH_ACK
  1842.         jnz     .respond_ack
  1843.  
  1844.         test    [edx + TCP_header.Flags], TH_SYN
  1845.         jnz     .respond_syn
  1846.         jmp     .done
  1847.  
  1848.   .respond_ack:
  1849.         push    ebx
  1850.         mov     cl, TH_RST
  1851.         call    tcp_respond
  1852.         pop     ebx
  1853.         jmp     .destroy_new_socket
  1854.  
  1855.   .respond_syn:
  1856.         push    ebx
  1857.         mov     cl, TH_RST + TH_ACK
  1858.         call    tcp_respond
  1859.         pop     ebx
  1860.         jmp     .destroy_new_socket
  1861.  
  1862. ;-----------------------------------------
  1863. ; The connection has no associated socket
  1864.  
  1865.   .no_socket:
  1866.         pusha
  1867.         mov     ecx, socket_mutex
  1868.         call    mutex_unlock
  1869.         popa
  1870.  
  1871.   .respond_seg_reset:
  1872.         test    [edx + TCP_header.Flags], TH_RST
  1873.         jnz     .drop_no_socket
  1874.  
  1875. ; TODO: if its a multicast/broadcast, also drop
  1876.  
  1877.         test    [edx + TCP_header.Flags], TH_ACK
  1878.         jnz     .respond_seg_ack
  1879.  
  1880.         test    [edx + TCP_header.Flags], TH_SYN
  1881.         jnz     .respond_seg_syn
  1882.  
  1883.         jmp     .drop_no_socket
  1884.  
  1885.   .respond_seg_ack:
  1886.         mov     cl, TH_RST
  1887.         xor     ebx, ebx                ; FIXME: find a way to get the receiving device ptr
  1888.         call    tcp_respond_segment
  1889.         jmp     .drop_no_socket
  1890.  
  1891.   .respond_seg_syn:
  1892.         mov     cl, TH_RST + TH_ACK
  1893.         xor     ebx, ebx                ; FIXME: find a way to get the receiving device ptr
  1894.         call    tcp_respond_segment
  1895.         jmp     .drop_no_socket
  1896.  
  1897. ;------------------------------------------------
  1898. ; Unlock socket mutex and prepare to drop segment
  1899.  
  1900.   .drop:
  1901.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Dropping segment\n"
  1902.  
  1903.         pusha
  1904.         lea     ecx, [ebx + SOCKET.mutex]
  1905.         call    mutex_unlock
  1906.         popa
  1907.  
  1908. ;--------------------------------------------
  1909. ; Destroy the newly created socket if needed
  1910.  
  1911.   .destroy_new_socket:
  1912.         test    [temp_bits], TCP_BIT_DROPSOCKET
  1913.         jz      .drop_no_socket
  1914.  
  1915.         mov     eax, ebx
  1916.         call    socket_free
  1917.  
  1918. ;------------------
  1919. ; Drop the segment
  1920.  
  1921.   .drop_no_socket:
  1922.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Drop (no socket)\n"
  1923.  
  1924.         call    net_buff_free
  1925.         jmp     .loop
  1926.  
  1927. endp
  1928.