Subversion Repositories Kolibri OS

Rev

Rev 2932 | Blame | Last modification | View Log | Download | RSS feed

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