Subversion Repositories Kolibri OS

Rev

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