Subversion Repositories Kolibri OS

Rev

Rev 6476 | Rev 6907 | 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: 6710 $
  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.         jmp     .drop
  863.  
  864. ;-----------------------------------------------------------------------------------
  865.   .rst_close:
  866.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Closing with reset\n"
  867.         jmp     .unlock_and_close
  868.  
  869. ;-----------------------------------------------------------------------------------
  870.   .no_rst:
  871.  
  872. ;-----------------------------------------------------------------------------------
  873. ;
  874. ; Handle SYN-full and ACK-less segments
  875. ;
  876. ;-----------------------------------------------------------------------------------
  877.  
  878. ; If a SYN is in the window, then this is an error so we send an RST and drop the connection
  879.  
  880.         test    [edx + TCP_header.Flags], TH_SYN
  881.         jz      .not_syn_full
  882.  
  883.         mov     eax, ebx
  884.         mov     ebx, ECONNRESET
  885.         call    tcp_drop
  886.         jmp     .drop_with_reset
  887.   .not_syn_full:
  888.  
  889. ; If ACK bit is off, we drop the segment and return
  890.  
  891.         test    [edx + TCP_header.Flags], TH_ACK
  892.         jz      .drop
  893.  
  894. ;----------------------------------------------------------------------------------
  895. ;
  896. ; ACK processing for SYN_RECEIVED state
  897. ;
  898. ;----------------------------------------------------------------------------------
  899.  
  900.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  901.         jb      .ack_processed                                  ; states: closed, listen, syn_sent
  902.         ja      .no_syn_rcv                                     ; established, fin_wait_1, fin_wait_2, close_wait, closing, last_ack, time_wait
  903.  
  904.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_received\n"
  905.  
  906.         mov     eax, [edx + TCP_header.AckNumber]
  907.         cmp     [ebx + TCP_SOCKET.SND_UNA], eax
  908.         ja      .drop_with_reset
  909.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  910.         ja      .drop_with_reset
  911.  
  912.         inc     [TCPS_connects]
  913.  
  914.         mov     eax, ebx
  915.         call    socket_is_connected
  916.         mov     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  917.  
  918. ; Do window scaling?
  919.  
  920.         test    [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  921.         jz      @f
  922.         test    [ebx + TCP_SOCKET.t_flags], TF_REQ_SCALE
  923.         jz      @f
  924.  
  925.         push    word[ebx + TCP_SOCKET.requested_s_scale]        ; Set send and receive scale factors to the received values
  926.         pop     word[ebx + TCP_SOCKET.SND_SCALE]
  927.        @@:
  928.  
  929.         call    tcp_reassemble
  930.  
  931.         mov     eax, [edx + TCP_header.SequenceNumber]
  932.         dec     eax
  933.         mov     [ebx + TCP_SOCKET.SND_WL1], eax
  934.   .no_syn_rcv:
  935.  
  936. ;-----------------------------------------------------------------------------------
  937. ;
  938. ; ACK processing for SYN_RECEIVED state and higher
  939. ;
  940. ;-----------------------------------------------------------------------------------
  941.  
  942. ;-------------------------
  943. ; Check for duplicate ACKs
  944.  
  945.         mov     eax, [edx + TCP_header.AckNumber]
  946.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  947.         ja      .dup_ack_complete
  948.  
  949.         test    ecx, ecx
  950.         jnz     .reset_dupacks
  951.  
  952.         mov     eax, dword[edx + TCP_header.Window]
  953.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  954.         jne     .reset_dupacks
  955.  
  956.         inc     [TCPS_rcvdupack]
  957.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Processing duplicate ACK\n"
  958.  
  959. ; If we have outstanding data, other than a window probe, this is a completely duplicate ACK
  960. ; (window info didnt change) The ACK is the biggest we've seen and we've seen exactly our rexmt threshold of them,
  961. ; assume a packet has been dropped and retransmit it. Kludge snd_nxt & the congestion window so we send only this one packet.
  962.  
  963.         test    [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission
  964.         jz      .reset_dupacks
  965.  
  966.         mov     eax, [edx + TCP_header.AckNumber]
  967.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  968.         jne     .reset_dupacks
  969.  
  970. ; Increment dupplicat ACK counter
  971. ; If it reaches the threshold, re-transmit the missing segment
  972.  
  973.         inc     [ebx + TCP_SOCKET.t_dupacks]
  974.         cmp     [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
  975.         jb      .dup_ack_complete
  976.         ja      .another_lost
  977.  
  978.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Re-transmitting lost segment\n"
  979.  
  980.         push    [ebx + TCP_SOCKET.SND_NXT]              ; >>>>
  981.  
  982.         mov     eax, [ebx + TCP_SOCKET.SND_WND]
  983.         cmp     eax, [ebx + TCP_SOCKET.SND_CWND]
  984.         jbe     @f
  985.         mov     eax, [ebx + TCP_SOCKET.SND_CWND]
  986.   @@:
  987.         shr     eax, 1
  988.         push    edx
  989.         xor     edx, edx
  990.         div     [ebx + TCP_SOCKET.t_maxseg]
  991.         cmp     eax, 2
  992.         ja      @f
  993.         xor     eax, eax
  994.         mov     al, 2
  995.        @@:
  996.         mul     [ebx + TCP_SOCKET.t_maxseg]
  997.         pop     edx
  998.         mov     [ebx + TCP_SOCKET.SND_SSTHRESH], eax
  999.  
  1000.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission   ; turn off retransmission timer
  1001.         mov     [ebx + TCP_SOCKET.t_rtt], 0
  1002.         mov     eax, [edx + TCP_header.AckNumber]
  1003.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1004.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1005.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1006.  
  1007. ; Unlock the socket
  1008.  
  1009.         push    ebx
  1010.         lea     ecx, [ebx + SOCKET.mutex]
  1011.         call    mutex_unlock
  1012.  
  1013. ; retransmit missing segment
  1014.  
  1015.         mov     eax, [esp]
  1016.         call    tcp_output
  1017.  
  1018. ; Lock the socket again
  1019.  
  1020.         mov     ecx, [esp]
  1021.         add     ecx, SOCKET.mutex
  1022.         call    mutex_lock
  1023.         pop     ebx
  1024.  
  1025. ; Continue processing
  1026.  
  1027.         xor     edx, edx
  1028.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1029.         mul     [ebx + TCP_SOCKET.t_dupacks]
  1030.         add     eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1031.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1032.  
  1033.         pop     eax                                     ; <<<<
  1034.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1035.         jb      @f
  1036.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1037.        @@:
  1038.         jmp     .drop
  1039.  
  1040.   .another_lost:
  1041.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Increasing congestion window\n"
  1042.  
  1043.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1044.         add     [ebx + TCP_SOCKET.SND_CWND], eax
  1045.  
  1046. ; Unlock the socket
  1047.  
  1048.         push    ebx
  1049.         lea     ecx, [ebx + SOCKET.mutex]
  1050.         call    mutex_unlock
  1051.  
  1052. ; retransmit missing segment, again
  1053.  
  1054.         mov     eax, [esp]
  1055.         call    tcp_output
  1056.  
  1057. ; Lock the socket again
  1058.  
  1059.         mov     ecx, [esp]
  1060.         add     ecx, SOCKET.mutex
  1061.         call    mutex_lock
  1062.         pop     ebx
  1063.  
  1064. ; And drop the incoming segment
  1065.  
  1066.         jmp     .drop
  1067.  
  1068.   .reset_dupacks:               ; We got a new ACK, reset duplicate ACK counter
  1069.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  1070.         jmp     .ack_processed
  1071.  
  1072.   .dup_ack_complete:
  1073.  
  1074. ;-------------------------------------------------
  1075. ; If the congestion window was inflated to account
  1076. ; for the other side's cached packets, retract it
  1077.  
  1078.         mov     eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1079.         cmp     eax, [ebx + TCP_SOCKET.SND_CWND]
  1080.         ja      @f
  1081.         cmp     [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
  1082.         jbe     @f
  1083.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1084.        @@:
  1085.  
  1086.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  1087.  
  1088.         mov     eax, [edx + TCP_header.AckNumber]
  1089.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  1090.         jbe     @f
  1091.         inc     [TCPS_rcvacktoomuch]
  1092.         jmp     .drop_after_ack
  1093.        @@:
  1094.  
  1095.         mov     edi, [edx + TCP_header.AckNumber]
  1096.         sub     edi, [ebx + TCP_SOCKET.SND_UNA]         ; now we got the number of acked bytes in edi
  1097.         inc     [TCPS_rcvackpack]
  1098.         add     [TCPS_rcvackbyte], edi
  1099.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: acceptable ACK for %u bytes\n", edi
  1100.  
  1101. ;-----------------------------------------------------------------------------------
  1102. ;
  1103. ; RTT measurements and retransmission timer
  1104. ;
  1105. ;-----------------------------------------------------------------------------------
  1106.  
  1107. ; If we have a timestamp, update smoothed RTT
  1108.  
  1109.         test    [temp_bits], TCP_BIT_TIMESTAMP
  1110.         jz      .timestamp_not_present
  1111.         mov     eax, [timestamp]
  1112.         sub     eax, [ebx + TCP_SOCKET.ts_ecr]
  1113.         inc     eax
  1114.         call    tcp_xmit_timer
  1115.         jmp     .rtt_done_
  1116.  
  1117. ; If no timestamp but transmit timer is running and timed sequence number was acked,
  1118. ; update smoothed RTT. Since we now have an RTT measurement, cancel the timer backoff
  1119. ; (Phil Karn's retransmit algo)
  1120. ; Recompute the initial retransmit timer
  1121.  
  1122.   .timestamp_not_present:
  1123.         mov     eax, [edx + TCP_header.AckNumber]
  1124.         cmp     eax, [ebx + TCP_SOCKET.t_rtseq]
  1125.         jbe     .rtt_done_
  1126.         mov     eax, [ebx + TCP_SOCKET.t_rtt]
  1127.         test    eax, eax
  1128.         jz      .rtt_done_
  1129.         call    tcp_xmit_timer
  1130.   .rtt_done_:
  1131.  
  1132. ; If all outstanding data is acked, stop retransmit timer and remember to restart (more output or persist)
  1133. ; If there is more data to be acked, restart retransmit timer, using current (possible backed-off) value.
  1134.  
  1135.         mov     eax, [ebx + TCP_SOCKET.SND_MAX]
  1136.         cmp     eax, [edx + TCP_header.AckNumber]
  1137.         jne     .more_data
  1138.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission
  1139.         or      [temp_bits], TCP_BIT_NEEDOUTPUT
  1140.         jmp     .no_restart
  1141.   .more_data:
  1142.         test    [ebx + TCP_SOCKET.timer_flags], timer_flag_persist
  1143.         jnz     .no_restart
  1144.  
  1145.         mov     eax, [ebx + TCP_SOCKET.t_rxtcur]
  1146.         mov     [ebx + TCP_SOCKET.timer_retransmission], eax
  1147.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission
  1148.   .no_restart:
  1149.  
  1150. ;-----------------------------------------------------------------------------------
  1151. ;
  1152. ; Open congestion window in response to ACKs
  1153. ;
  1154. ;-----------------------------------------------------------------------------------
  1155.  
  1156. ; If the window gives us less then sstresh packets in flight, open exponentially.
  1157. ; Otherwise, open lineary
  1158.  
  1159.         mov     esi, [ebx + TCP_SOCKET.SND_CWND]
  1160.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1161.         cmp     esi, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1162.         jbe     @f
  1163.         push    edx
  1164.         push    eax
  1165.         mul     eax             ; t_maxseg*t_maxseg
  1166.         div     esi             ; t_maxseg*t_maxseg/snd_cwnd
  1167.         pop     edx             ; t_maxseg
  1168.         shr     edx, 3          ; t_maxseg/8
  1169.         add     eax, edx        ; t_maxseg*t_maxseg/snd_cwnd + t_maxseg/8
  1170.         pop     edx
  1171.        @@:
  1172.         add     esi, eax
  1173.  
  1174.         push    ecx
  1175.         mov     cl, [ebx + TCP_SOCKET.SND_SCALE]
  1176.         mov     eax, TCP_max_win
  1177.         shl     eax, cl
  1178.         pop     ecx
  1179.  
  1180.         cmp     esi, eax
  1181.         jbe     @f
  1182.         mov     esi, eax
  1183.   @@:
  1184.         mov     [ebx + TCP_SOCKET.SND_CWND], esi
  1185.  
  1186. ;-----------------------------------------------------------------------------------
  1187. ;
  1188. ; Remove acknowledged data from send buffer
  1189. ;
  1190. ;-----------------------------------------------------------------------------------
  1191.  
  1192. ; If the number of bytes acknowledged exceeds the number of bytes on the send buffer,
  1193. ; snd_wnd is decremented by the number of bytes in the send buffer and TCP knows
  1194. ; that its FIN has been ACKed. (FIN occupies 1 byte in the sequence number space)
  1195.  
  1196.         cmp     edi, [ebx + STREAM_SOCKET.snd.size]
  1197.         jbe     .no_fin_ack
  1198.  
  1199. ; Drop all data in output buffer
  1200.  
  1201.         push    ecx edx ebx
  1202.         mov     ecx, [ebx + STREAM_SOCKET.snd.size]
  1203.         sub     [ebx + TCP_SOCKET.SND_WND], ecx
  1204.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1205.         call    socket_ring_free
  1206.         pop     ebx edx ecx
  1207.  
  1208.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is acked\n"
  1209.         or      [temp_bits], TCP_BIT_FIN_IS_ACKED
  1210.         jmp     .ack_complete
  1211.   .no_fin_ack:
  1212.  
  1213. ; Drop acknowledged data
  1214.  
  1215.         push    ecx edx ebx
  1216.         mov     ecx, edi
  1217.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1218.         call    socket_ring_free
  1219.         pop     ebx
  1220.         sub     [ebx + TCP_SOCKET.SND_WND], ecx
  1221.         pop     edx ecx
  1222.   .ack_complete:
  1223.  
  1224. ;-----------------------------------------------------------------------------------
  1225. ;
  1226. ; Wake up process waiting on send buffer
  1227. ;
  1228. ;-----------------------------------------------------------------------------------
  1229.  
  1230.         mov     eax, ebx
  1231.         call    socket_notify
  1232.  
  1233. ; Update TCPS
  1234.  
  1235.         mov     eax, [edx + TCP_header.AckNumber]
  1236.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  1237.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1238.         jb      @f
  1239.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1240.        @@:
  1241.  
  1242. ;-----------------------------------------------------------------------------------
  1243. ;
  1244. ; State specific ACK handeling
  1245. ;
  1246. ;-----------------------------------------------------------------------------------
  1247.  
  1248.         mov     eax, [ebx + TCP_SOCKET.t_state]
  1249.         jmp     dword[.ack_sw_list+eax*4]
  1250.  
  1251.   .ack_sw_list:
  1252.         dd      .ack_processed  ; TCPS_CLOSED
  1253.         dd      .ack_processed  ; TCPS_LISTEN
  1254.         dd      .ack_processed  ; TCPS_SYN_SENT
  1255.         dd      .ack_processed  ; TCPS_SYN_RECEIVED
  1256.         dd      .ack_processed  ; TCPS_ESTABLISHED
  1257.         dd      .ack_processed  ; TCPS_CLOSE_WAIT
  1258.         dd      .ack_fw1        ; TCPS_FIN_WAIT_1
  1259.         dd      .ack_c          ; TCPS_CLOSING
  1260.         dd      .ack_la         ; TCPS_LAST_ACK
  1261.         dd      .ack_processed  ; TCPS_FIN_WAIT_2
  1262.         dd      .ack_tw         ; TCPS_TIMED_WAIT
  1263.  
  1264. ;-----------------------------------------------------------------------------------
  1265.   .ack_fw1:
  1266. ; If our FIN is now acked, enter FIN_WAIT_2
  1267.  
  1268.         test    [temp_bits], TCP_BIT_FIN_IS_ACKED
  1269.         jz      .ack_processed
  1270.  
  1271. ; If we can't receive any more data, then closing user can proceed.
  1272. ; Starting the timer is contrary to the specification, but if we dont get a FIN,
  1273. ; we'll hang forever.
  1274.  
  1275.         test    [ebx + SOCKET.state], SS_CANTRCVMORE
  1276.         jnz     @f
  1277.         mov     eax, ebx
  1278.         call    socket_is_disconnected
  1279.         mov     [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle
  1280.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
  1281.        @@:
  1282.         mov     [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
  1283.         jmp     .ack_processed
  1284.  
  1285. ;-----------------------------------------------------------------------------------
  1286.   .ack_c:
  1287. ; Enter the TIME_WAIT state if our FIN is acked in CLOSED state.
  1288.  
  1289.         test    [temp_bits], TCP_BIT_FIN_IS_ACKED
  1290.         jz      .ack_processed
  1291.  
  1292.         mov     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1293.         mov     eax, ebx
  1294.         call    tcp_cancel_timers
  1295.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1296.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
  1297.         mov     eax, ebx
  1298.         call    socket_is_disconnected
  1299.         jmp     .ack_processed
  1300.  
  1301. ;-----------------------------------------------------------------------------------
  1302.   .ack_la:
  1303. ; In LAST_ACK state, we may still be waiting for data to drain and/or to be acked.
  1304. ; If our FIN is acked however, enter CLOSED state and return.
  1305.  
  1306.         test    [temp_bits], TCP_BIT_FIN_IS_ACKED
  1307.         jz      .ack_processed
  1308.  
  1309.   .unlock_and_close:
  1310.         push    ebx
  1311.         lea     ecx, [ebx + SOCKET.mutex]
  1312.         call    mutex_unlock
  1313.         pop     eax
  1314.  
  1315.         call    tcp_close
  1316.         jmp     .drop_no_socket
  1317.  
  1318. ;-----------------------------------------------------------------------------------
  1319.   .ack_tw:
  1320. ; In TIME_WAIT state the only thing that should arrive is a retransmission of the remote FIN.
  1321. ; Acknowledge it and restart the FINACK timer
  1322.  
  1323.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2*TCP_time_MSL
  1324.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_2msl
  1325.         jmp     .drop_after_ack
  1326.  
  1327. ;-----------------------------------------------------------------------------------
  1328. ;
  1329. ; Initiation of Passive Open?
  1330. ;
  1331. ;-----------------------------------------------------------------------------------
  1332.  
  1333.   .state_listen:
  1334.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: state=listen\n"
  1335.  
  1336.         test    [edx + TCP_header.Flags], TH_RST
  1337.         jnz     .drop
  1338.  
  1339.         test    [edx + TCP_header.Flags], TH_ACK
  1340.         jnz     .drop_with_reset
  1341.  
  1342.         test    [edx + TCP_header.Flags], TH_SYN
  1343.         jz      .drop
  1344.  
  1345.         inc     [TCPS_accepts]
  1346.  
  1347. ;;; TODO: check if it's a broadcast or multicast, and drop if so
  1348.  
  1349. ;-------------------------------------------
  1350. ; Processing of SYN received in LISTEN state
  1351.  
  1352.         push    [edi + IPv4_header.SourceAddress]
  1353.         pop     [ebx + IP_SOCKET.RemoteIP]
  1354.  
  1355.         push    [edx + TCP_header.SourcePort]
  1356.         pop     [ebx + TCP_SOCKET.RemotePort]
  1357.  
  1358.         push    [edx + TCP_header.SequenceNumber]
  1359.         pop     [ebx + TCP_SOCKET.IRS]
  1360.  
  1361.         mov     eax, [TCP_sequence_num]
  1362.         add     [TCP_sequence_num], TCP_ISSINCR / 2
  1363.         mov     [ebx + TCP_SOCKET.ISS], eax
  1364.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1365.  
  1366.         tcp_sendseqinit ebx
  1367.         tcp_rcvseqinit ebx
  1368.  
  1369.         mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  1370.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1371.         mov     [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval  ;;;; macro
  1372.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
  1373.  
  1374.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1375.         call    socket_ring_create
  1376.         test    eax, eax
  1377.         jz      .drop
  1378.  
  1379.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  1380.         call    socket_ring_create
  1381.         test    eax, eax
  1382.         jz      .drop
  1383.  
  1384.         and     [temp_bits], not TCP_BIT_DROPSOCKET
  1385.  
  1386.         pusha
  1387.         mov     eax, ebx
  1388.         call    socket_notify
  1389.         popa
  1390.  
  1391.         jmp     .trim
  1392.  
  1393. ;-----------------------------------------------------------------------------------
  1394. ;
  1395. ; Completion of active open?
  1396. ;
  1397. ;-----------------------------------------------------------------------------------
  1398.  
  1399.   .state_syn_sent:
  1400.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_sent\n"
  1401.  
  1402.         test    [edx + TCP_header.Flags], TH_ACK
  1403.         jz      @f
  1404.  
  1405.         mov     eax, [edx + TCP_header.AckNumber]
  1406.         cmp     eax, [ebx + TCP_SOCKET.ISS]
  1407.         jbe     .drop_with_reset
  1408.  
  1409.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  1410.         ja      .drop_with_reset
  1411.        @@:
  1412.  
  1413.         test    [edx + TCP_header.Flags], TH_RST
  1414.         jz      @f
  1415.  
  1416.         test    [edx + TCP_header.Flags], TH_ACK
  1417.         jz      .drop
  1418.  
  1419.         mov     eax, ebx
  1420.         mov     ebx, ECONNREFUSED
  1421.         call    tcp_drop
  1422.         jmp     .drop
  1423.        @@:
  1424.  
  1425. ;-----------------------------------------------------------------------------------
  1426. ;
  1427. ; Process received SYN in response to an active open
  1428. ;
  1429. ;-----------------------------------------------------------------------------------
  1430.  
  1431.         test    [edx + TCP_header.Flags], TH_SYN
  1432.         jz      .drop
  1433.  
  1434.         test    [edx + TCP_header.Flags], TH_ACK
  1435.         jz      @f
  1436.  
  1437.         mov     eax, [edx + TCP_header.AckNumber]
  1438.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  1439.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1440.         jbe     @f
  1441.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1442.  
  1443.         and     [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission   ; disable retransmission timer
  1444.        @@:
  1445.  
  1446.         push    [edx + TCP_header.SequenceNumber]
  1447.         pop     [ebx + TCP_SOCKET.IRS]
  1448.  
  1449.         tcp_rcvseqinit ebx
  1450.  
  1451.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1452.  
  1453.         mov     eax, [ebx + TCP_SOCKET.SND_UNA]
  1454.         cmp     eax, [ebx + TCP_SOCKET.ISS]
  1455.         jbe     .simultaneous_open
  1456.  
  1457.         test    [edx + TCP_header.Flags], TH_ACK
  1458.         jz      .simultaneous_open
  1459.  
  1460.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: active open\n"
  1461.  
  1462.         inc     [TCPS_connects]
  1463.  
  1464. ; set socket state to connected
  1465.  
  1466.         push    eax
  1467.         mov     eax, ebx
  1468.         call    socket_is_connected
  1469.         pop     eax
  1470.         mov     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  1471.  
  1472. ; Do window scaling on this connection ?
  1473.  
  1474.         mov     eax, [ebx + TCP_SOCKET.t_flags]
  1475.         and     eax, TF_REQ_SCALE or TF_RCVD_SCALE
  1476.         cmp     eax, TF_REQ_SCALE or TF_RCVD_SCALE
  1477.         jne     .no_scaling
  1478.  
  1479.         mov     ax, word[ebx + TCP_SOCKET.requested_s_scale]
  1480.         mov     word[ebx + TCP_SOCKET.SND_SCALE], ax
  1481.   .no_scaling:
  1482.  
  1483. ;;; TODO: reassemble packets queue
  1484.  
  1485. ; If we didnt have time to re-transmit the SYN,
  1486. ; Use its rtt as our initial srtt & rtt var.
  1487.  
  1488.         mov     eax, [ebx + TCP_SOCKET.t_rtt]
  1489.         test    eax, eax
  1490.         je      .trim
  1491.         call    tcp_xmit_timer
  1492.         jmp     .trim
  1493.  
  1494. ;-----------------------------------------------------------------------------------
  1495. ;
  1496. ; Simultaneous open (We have received a SYN but no ACK)
  1497. ;
  1498. ;-----------------------------------------------------------------------------------
  1499.  
  1500.   .simultaneous_open:
  1501.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: simultaneous open\n"
  1502.         mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  1503.  
  1504. ;-----------------------------------------------------------------------------------
  1505. ;
  1506. ; Common processing for receipt of SYN
  1507. ;
  1508. ;-----------------------------------------------------------------------------------
  1509.  
  1510.   .trim:
  1511. ; Advance sequence number to correspond to first data byte.
  1512. ; If data, trim to stay within window, dropping FIN if necessary
  1513.  
  1514.         inc     [edx + TCP_header.SequenceNumber]
  1515.  
  1516. ; Drop any received data that doesnt fit in the receive window.
  1517.  
  1518.         cmp     ecx, [ebx + TCP_SOCKET.RCV_WND]
  1519.         jbe     .dont_trim
  1520.  
  1521.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: received data does not fit in window, trimming %u bytes\n", eax
  1522.         inc     [TCPS_rcvpackafterwin]
  1523.         sub     ecx, [ebx + TCP_SOCKET.RCV_WND]
  1524.         add     [TCPS_rcvbyteafterwin], ecx
  1525.  
  1526.         and     [edx + TCP_header.Flags], not (TH_FIN)
  1527.         mov     ecx, [ebx + TCP_SOCKET.RCV_WND]
  1528.  
  1529.   .dont_trim:
  1530.         mov     eax, [edx + TCP_header.SequenceNumber]
  1531.         mov     [ebx + TCP_SOCKET.RCV_UP], eax
  1532.         dec     eax
  1533.         mov     [ebx + TCP_SOCKET.SND_WL1], eax
  1534.  
  1535. ;-----------------------------------------------------------------------------------
  1536. ;
  1537. ; Update window information (step 6 in RFC793)
  1538. ;
  1539. ;-----------------------------------------------------------------------------------
  1540.  
  1541.   .ack_processed:
  1542.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK processed\n"
  1543.  
  1544. ; dont look at window if no ACK
  1545.  
  1546.         test    [edx + TCP_header.Flags], TH_ACK
  1547.         jz      .no_window_update
  1548.  
  1549. ; Does the segment contain new data?
  1550.  
  1551.         mov     eax, [ebx + TCP_SOCKET.SND_WL1]
  1552.         cmp     eax, [edx + TCP_header.SequenceNumber]
  1553.         jb      .update_window
  1554.         ja      @f
  1555.  
  1556. ; No new data but a new ACK ?
  1557.  
  1558.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1559.         cmp     eax, [edx + TCP_header.AckNumber]
  1560.         jb      .update_window
  1561.        @@:
  1562.  
  1563. ; No new data or ACK but advertised window is larger then current window?
  1564.  
  1565.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1566.         cmp     eax, [edx + TCP_header.AckNumber]
  1567.         jne     .no_window_update
  1568.  
  1569.         mov     eax, dword[edx + TCP_header.Window]
  1570.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  1571.         jbe     .no_window_update
  1572.  
  1573.  
  1574. ; Keep track of pure window updates
  1575.   .update_window:
  1576.         test    ecx, ecx
  1577.         jnz     @f
  1578.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1579.         cmp     eax, [edx + TCP_header.AckNumber]
  1580.         jne     @f
  1581.         mov     eax, dword[edx + TCP_header.Window]
  1582.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  1583.         jbe     @f
  1584.         inc     [TCPS_rcvwinupd]
  1585.        @@:
  1586.  
  1587.         mov     eax, dword[edx + TCP_header.Window]
  1588.         mov     [ebx + TCP_SOCKET.SND_WND], eax
  1589.         cmp     eax, [ebx + TCP_SOCKET.max_sndwnd]
  1590.         jbe     @f
  1591.         mov     [ebx + TCP_SOCKET.max_sndwnd], eax
  1592.        @@:
  1593.  
  1594.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Updating window to %u\n", eax
  1595.  
  1596.         push    [edx + TCP_header.SequenceNumber]
  1597.         pop     [ebx + TCP_SOCKET.SND_WL1]
  1598.  
  1599.         push    [edx + TCP_header.AckNumber]
  1600.         pop     [ebx + TCP_SOCKET.SND_WL2]
  1601.  
  1602.         or      [temp_bits], TCP_BIT_NEEDOUTPUT
  1603.   .no_window_update:
  1604.  
  1605. ;-----------------------------------------------------------------------------------
  1606. ;
  1607. ; Process URG flag
  1608. ;
  1609. ;-----------------------------------------------------------------------------------
  1610.  
  1611.         test    [edx + TCP_header.Flags], TH_URG
  1612.         jz      .not_urgent
  1613.  
  1614.         cmp     [edx + TCP_header.UrgentPointer], 0
  1615.         jz      .not_urgent
  1616.  
  1617.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1618.         je      .not_urgent
  1619.  
  1620. ; Ignore bogus urgent offsets
  1621.  
  1622.         movzx   eax, [edx + TCP_header.UrgentPointer]
  1623.         add     eax, [ebx + STREAM_SOCKET.rcv.size]
  1624.         cmp     eax, SOCKET_BUFFER_SIZE
  1625.         jbe     .not_urgent
  1626.  
  1627.         mov     [edx + TCP_header.UrgentPointer], 0
  1628.         and     [edx + TCP_header.Flags], not (TH_URG)
  1629.         jmp     .do_data
  1630.  
  1631.   .not_urgent:
  1632.  
  1633. ; processing of received urgent pointer
  1634.  
  1635.         ;;; TODO (1051-1093)
  1636.  
  1637. ;-----------------------------------------------------------------------------------
  1638. ;
  1639. ; Process the data
  1640. ;
  1641. ;-----------------------------------------------------------------------------------
  1642.  
  1643.   .do_data:
  1644.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1645.         jae     .final_processing
  1646.  
  1647.         test    [edx + TCP_header.Flags], TH_FIN
  1648.         jnz     @f
  1649.  
  1650.         test    ecx, ecx
  1651.         jz      .final_processing
  1652.        @@:
  1653.  
  1654. ; The segment is in order?
  1655.  
  1656.         mov     eax, [edx + TCP_header.SequenceNumber]
  1657.         cmp     eax, [ebx + TCP_SOCKET.RCV_NXT]
  1658.         jne     .out_of_order
  1659.  
  1660. ; The reassembly queue is empty?
  1661.  
  1662.         cmp     [ebx + TCP_SOCKET.seg_next], 0
  1663.         jne     .out_of_order
  1664.  
  1665. ; The connection is established?
  1666.  
  1667.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  1668.         jne     .out_of_order
  1669.  
  1670. ; Ok, lets do this..  Set delayed ACK flag and copy data into socket buffer
  1671.  
  1672.         or      [ebx + TCP_SOCKET.t_flags], TF_DELACK
  1673.  
  1674.         pusha
  1675.         mov     esi, [dataoffset]
  1676.         add     esi, edx
  1677.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  1678.         call    socket_ring_write                       ; Add the data to the socket buffer
  1679.         add     [ebx + TCP_SOCKET.RCV_NXT], ecx         ; Update sequence number with number of bytes we have copied
  1680.         popa
  1681.  
  1682. ; Wake up the sleeping process
  1683.  
  1684.         mov     eax, ebx
  1685.         call    socket_notify
  1686.  
  1687.         jmp     .data_done
  1688.  
  1689.   .out_of_order:
  1690.         DEBUGF  DEBUG_NETWORK_VERBOSE,  "TCP data is out of order!\nSequencenumber is %u, we expected %u.\n", \
  1691.         [edx + TCP_header.SequenceNumber], [ebx + TCP_SOCKET.RCV_NXT]
  1692.  
  1693. ; Uh-oh, some data is out of order, lets call TCP reassemble for help
  1694.  
  1695.         call    tcp_reassemble
  1696.  
  1697. ; Generate ACK immediately, to let the other end know that a segment was received out of order,
  1698. ; and to tell it what sequence number is expected. This aids the fast-retransmit algorithm.
  1699.  
  1700.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1701.   .data_done:
  1702.  
  1703. ;-----------------------------------------------------------------------------------
  1704. ;
  1705. ; Process FIN
  1706. ;
  1707. ;-----------------------------------------------------------------------------------
  1708.  
  1709.         test    [edx + TCP_header.Flags], TH_FIN
  1710.         jz      .final_processing
  1711.  
  1712.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Processing FIN\n"
  1713.  
  1714.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1715.         jae     .not_first_fin
  1716.  
  1717.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: First FIN for this connection\n"
  1718.  
  1719.         mov     eax, ebx
  1720.         call    socket_cant_recv_more
  1721.  
  1722.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1723.         inc     [ebx + TCP_SOCKET.RCV_NXT]
  1724.  
  1725.   .not_first_fin:
  1726.         mov     eax, [ebx + TCP_SOCKET.t_state]
  1727.         jmp     dword[.fin_sw_list+eax*4]
  1728.  
  1729.   .fin_sw_list:
  1730.         dd      .final_processing       ; TCPS_CLOSED
  1731.         dd      .final_processing       ; TCPS_LISTEN
  1732.         dd      .final_processing       ; TCPS_SYN_SENT
  1733.         dd      .fin_syn_est            ; TCPS_SYN_RECEIVED
  1734.         dd      .fin_syn_est            ; TCPS_ESTABLISHED
  1735.         dd      .final_processing       ; TCPS_CLOSE_WAIT
  1736.         dd      .fin_wait1              ; TCPS_FIN_WAIT_1
  1737.         dd      .final_processing       ; TCPS_CLOSING
  1738.         dd      .final_processing       ; TCPS_LAST_ACK
  1739.         dd      .fin_wait2              ; TCPS_FIN_WAIT_2
  1740.         dd      .fin_timed              ; TCPS_TIMED_WAIT
  1741.  
  1742. ;-----------------------------------------------------------------------------------
  1743.   .fin_syn_est:
  1744. ; In SYN_RECEIVED and ESTABLISHED state, enter the CLOSE_WAIT state
  1745.  
  1746.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
  1747.         jmp     .final_processing
  1748.  
  1749. ;-----------------------------------------------------------------------------------
  1750.   .fin_wait1:
  1751. ; From FIN_WAIT_1 state, enter CLOSING state (our FIN has not been ACKed)
  1752.  
  1753.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSING
  1754.         jmp     .final_processing
  1755.  
  1756. ;-----------------------------------------------------------------------------------
  1757.   .fin_wait2:
  1758. ; From FIN_WAIT_2 state, enter TIME_WAIT state and start the timer
  1759.  
  1760.         mov     [ebx + TCP_SOCKET.t_state], TCPS_TIME_WAIT
  1761.         mov     eax, ebx
  1762.         call    tcp_cancel_timers
  1763.         call    socket_is_disconnected
  1764.  
  1765. ;-----------------------------------------------------------------------------------
  1766.   .fin_timed:
  1767. ; (re)start the 2 MSL timer
  1768.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1769.         or      [ebx + TCP_SOCKET.timer_flags], timer_flag_wait
  1770.  
  1771. ;-----------------------------------------------------------------------------------
  1772. ;
  1773. ; Finally, drop the segment
  1774. ;
  1775. ;-----------------------------------------------------------------------------------
  1776.  
  1777.   .final_processing:
  1778.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Final processing\n"
  1779.  
  1780.         push    ebx
  1781.         lea     ecx, [ebx + SOCKET.mutex]
  1782.         call    mutex_unlock
  1783.         pop     eax
  1784.  
  1785.         test    [temp_bits], TCP_BIT_NEEDOUTPUT
  1786.         jnz     .need_output
  1787.  
  1788.         test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW
  1789.         jz      .done
  1790.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK now!\n"
  1791.  
  1792.   .need_output:
  1793.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: need output\n"
  1794.         call    tcp_output
  1795.  
  1796.   .done:
  1797.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: dumping\n"
  1798.  
  1799.         call    net_buff_free
  1800.         jmp     .loop
  1801.  
  1802. ;-----------------------------------------------------------------------------------
  1803. ;
  1804. ; Drop segment, reply with an RST segment when needed
  1805. ;
  1806. ;-----------------------------------------------------------------------------------
  1807.  
  1808. ;-----------------------------------------------------------------------------------
  1809.   .drop_after_ack:
  1810.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Drop after ACK\n"
  1811.  
  1812.         push    edx ebx
  1813.         lea     ecx, [ebx + SOCKET.mutex]
  1814.         call    mutex_unlock
  1815.         pop     eax edx
  1816.  
  1817.         test    [edx + TCP_header.Flags], TH_RST
  1818.         jnz     .done
  1819.  
  1820.         or      [eax + TCP_SOCKET.t_flags], TF_ACKNOW
  1821.         jmp     .need_output
  1822.  
  1823. ;-----------------------------------------------------------------------------------
  1824.   .drop_with_reset:
  1825.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Drop with reset\n"
  1826.  
  1827.         push    ebx edx
  1828.         lea     ecx, [ebx + SOCKET.mutex]
  1829.         call    mutex_unlock
  1830.         pop     edx ebx
  1831.  
  1832.         test    [edx + TCP_header.Flags], TH_RST
  1833.         jnz     .done
  1834.  
  1835. ; TODO: if its a multicast/broadcast, also drop
  1836.  
  1837.         test    [edx + TCP_header.Flags], TH_ACK
  1838.         jnz     .respond_ack
  1839.  
  1840.         test    [edx + TCP_header.Flags], TH_SYN
  1841.         jnz     .respond_syn
  1842.         jmp     .done
  1843.  
  1844.   .respond_ack:
  1845.         push    ebx
  1846.         mov     cl, TH_RST
  1847.         call    tcp_respond
  1848.         pop     ebx
  1849.         jmp     .destroy_new_socket
  1850.  
  1851.   .respond_syn:
  1852.         push    ebx
  1853.         mov     cl, TH_RST + TH_ACK
  1854.         call    tcp_respond
  1855.         pop     ebx
  1856.         jmp     .destroy_new_socket
  1857.  
  1858. ;-----------------------------------------
  1859. ; The connection has no associated socket
  1860.  
  1861.   .no_socket:
  1862.         pusha
  1863.         mov     ecx, socket_mutex
  1864.         call    mutex_unlock
  1865.         popa
  1866.  
  1867.   .respond_seg_reset:
  1868.         test    [edx + TCP_header.Flags], TH_RST
  1869.         jnz     .drop_no_socket
  1870.  
  1871. ; TODO: if its a multicast/broadcast, also drop
  1872.  
  1873.         test    [edx + TCP_header.Flags], TH_ACK
  1874.         jnz     .respond_seg_ack
  1875.  
  1876.         test    [edx + TCP_header.Flags], TH_SYN
  1877.         jnz     .respond_seg_syn
  1878.  
  1879.         jmp     .drop_no_socket
  1880.  
  1881.   .respond_seg_ack:
  1882.         mov     cl, TH_RST
  1883.         xor     ebx, ebx                ; FIXME: find a way to get the receiving device ptr
  1884.         call    tcp_respond_segment
  1885.         jmp     .drop_no_socket
  1886.  
  1887.   .respond_seg_syn:
  1888.         mov     cl, TH_RST + TH_ACK
  1889.         xor     ebx, ebx                ; FIXME: find a way to get the receiving device ptr
  1890.         call    tcp_respond_segment
  1891.         jmp     .drop_no_socket
  1892.  
  1893. ;------------------------------------------------
  1894. ; Unlock socket mutex and prepare to drop segment
  1895.  
  1896.   .drop:
  1897.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Dropping segment\n"
  1898.  
  1899.         pusha
  1900.         lea     ecx, [ebx + SOCKET.mutex]
  1901.         call    mutex_unlock
  1902.         popa
  1903.  
  1904. ;--------------------------------------------
  1905. ; Destroy the newly created socket if needed
  1906.  
  1907.   .destroy_new_socket:
  1908.         test    [temp_bits], TCP_BIT_DROPSOCKET
  1909.         jz      .drop_no_socket
  1910.  
  1911.         mov     eax, ebx
  1912.         call    socket_free
  1913.  
  1914. ;------------------
  1915. ; Drop the segment
  1916.  
  1917.   .drop_no_socket:
  1918.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Drop (no socket)\n"
  1919.  
  1920.         call    net_buff_free
  1921.         jmp     .loop
  1922.  
  1923. endp
  1924.