Subversion Repositories Kolibri OS

Rev

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