Subversion Repositories Kolibri OS

Rev

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