Subversion Repositories Kolibri OS

Rev

Rev 3293 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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