Subversion Repositories Kolibri OS

Rev

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

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