Subversion Repositories Kolibri OS

Rev

Rev 6915 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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