Subversion Repositories Kolibri OS

Rev

Rev 2891 | 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: 2914 $
  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.         mov     eax, [TCP_sequence_num]
  493.         add     [TCP_sequence_num], 64000 / 2
  494.         mov     [ebx + TCP_SOCKET.ISS], eax
  495.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  496.  
  497.         TCP_sendseqinit ebx
  498.         TCP_rcvseqinit ebx
  499.  
  500.         mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  501.         mov     [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  502.         mov     [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval  ;;;; macro
  503.  
  504.         lea     eax, [ebx + STREAM_SOCKET.snd]
  505.         call    SOCKET_ring_create
  506.  
  507.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  508.         call    SOCKET_ring_create
  509.  
  510. ;;;        call    SOCKET_notify_owner
  511.  
  512.         jmp     .trim_then_step6
  513.                                    
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523. ;------------
  524. ; Active Open
  525.  
  526. align 4
  527. .SYN_SENT:
  528.  
  529.         DEBUGF  1,"TCP_input: state=syn_sent\n"
  530.  
  531.         test    [edx + TCP_header.Flags], TH_ACK
  532.         jz      @f
  533.  
  534.         mov     eax, [edx + TCP_header.AckNumber]
  535.         cmp     eax, [ebx + TCP_SOCKET.ISS]
  536.         jbe     .drop_with_reset
  537.  
  538.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  539.         ja      .drop_with_reset
  540.        @@:
  541.  
  542.         test    [edx + TCP_header.Flags], TH_RST
  543.         jz      @f
  544.  
  545.         test    [edx + TCP_header.Flags], TH_ACK
  546.         jz      .drop
  547.  
  548.         mov     eax, ebx
  549.         mov     ebx, ECONNREFUSED
  550.         call    TCP_drop
  551.  
  552.         jmp     .drop
  553.        @@:
  554.  
  555.         test    [edx + TCP_header.Flags], TH_SYN
  556.         jz      .drop
  557.  
  558. ; at this point, segment seems to be valid
  559.  
  560.         test    [edx + TCP_header.Flags], TH_ACK
  561.         jz      .no_syn_ack
  562.  
  563. ; now, process received SYN in response to an active open
  564.  
  565.         mov     eax, [edx + TCP_header.AckNumber]
  566.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  567.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  568.         jbe     @f
  569.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  570.        @@:
  571.  
  572.   .no_syn_ack:
  573.  
  574.         mov     [ebx + TCP_SOCKET.timer_retransmission], 0      ; disable retransmission
  575.  
  576.         push    [edx + TCP_header.SequenceNumber]
  577.         pop     [ebx + TCP_SOCKET.IRS]
  578.  
  579.         TCP_rcvseqinit ebx
  580.  
  581.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  582.  
  583.         mov     eax, [ebx + TCP_SOCKET.SND_UNA]
  584.         cmp     eax, [ebx + TCP_SOCKET.ISS]
  585.         jbe     .simultaneous_open
  586.  
  587.         test    [edx + TCP_header.Flags], TH_ACK
  588.         jz      .simultaneous_open
  589.  
  590.         DEBUGF  1,"TCP_input: active open\n"
  591.  
  592. ;;; TODO: update stats
  593.  
  594. ; set socket state to connected
  595.         mov     [ebx + SOCKET.state], SS_ISCONNECTED
  596.         mov     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  597.  
  598. ;;; TODO: check if we should scale the connection (567-572)
  599.         mov     [ebx + TCP_SOCKET.SND_SCALE], 0
  600.  
  601. ;;; TODO: update RTT estimators
  602.  
  603.         jmp     .trim_then_step6
  604.  
  605.   .simultaneous_open:
  606.  
  607.         DEBUGF  1,"TCP_input: simultaneous open\n"
  608. ; We have received a syn but no ACK, so we are having a simultaneous open..
  609.         mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617. ;-------------------------------------
  618. ; Common processing for receipt of SYN
  619.  
  620.   .trim_then_step6:
  621.  
  622.         inc     [edx + TCP_header.SequenceNumber]
  623.  
  624. ;;; TODO: Drop any received data that follows receive window (590)
  625.  
  626.         mov     eax, [edx + TCP_header.SequenceNumber]
  627.         mov     [ebx + TCP_SOCKET.RCV_UP], eax
  628.         dec     eax
  629.         mov     [ebx + TCP_SOCKET.SND_WL1], eax
  630.  
  631.         jmp     .ack_processed
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.   .NOT_LISTEN_OR_SYN_SENT:
  641.  
  642.         DEBUGF  1,"TCP_input: state is not listen or syn_sent\n"
  643.  
  644. ;--------------------------------------------
  645. ; Protection Against Wrapped Sequence Numbers
  646.  
  647. ; First, check if timestamp is present
  648.  
  649. ;;;; TODO 602
  650.  
  651. ; Then, check if at least some bytes of data are within window
  652.  
  653. ;;;; TODO
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662. ;----------------------------
  663. ; trim any data not in window
  664.  
  665. ; check for duplicate data at beginning of segment
  666.  
  667.         mov     eax, [ebx + TCP_SOCKET.RCV_NXT]
  668.         sub     eax, [edx + TCP_header.SequenceNumber]
  669.         jbe     .no_duplicate
  670.  
  671.         DEBUGF  1,"TCP_input: %u bytes duplicate data!\n", eax
  672.  
  673.         test    [edx + TCP_header.Flags], TH_SYN
  674.         jz      .no_dup_syn
  675.  
  676. ; remove duplicate syn
  677.  
  678.         and     [edx + TCP_header.Flags], not (TH_SYN)
  679.         inc     [edx + TCP_header.SequenceNumber]
  680.  
  681.         cmp     [edx + TCP_header.UrgentPointer], 1
  682.         jbe     @f
  683.         dec     [edx + TCP_header.UrgentPointer]
  684.         jmp     .dup_syn
  685.        @@:
  686.         and     [edx + TCP_header.Flags], not (TH_URG)
  687.   .dup_syn:
  688.         dec     eax
  689.   .no_dup_syn:
  690.  
  691. ; eax holds number of bytes to drop
  692.  
  693. ; Check for entire duplicate packet
  694.  
  695.         cmp     eax, ecx
  696.         jae     .duplicate
  697.  
  698.         DEBUGF  1,"TCP_input: Going to drop %u out of %u bytes\n", eax, ecx
  699.  
  700. ;;; TODO: apply figure 28.30
  701.  
  702. ; Check for duplicate FIN
  703.  
  704.         test    [edx + TCP_header.Flags], TH_FIN
  705.         jz      .no_fin2
  706.         inc     ecx
  707.         cmp     eax, ecx
  708.         jne     @f
  709.  
  710.         mov     eax, ecx
  711.         and     [edx + TCP_header.Flags], not TH_FIN
  712.         or      [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  713.         dec     ecx
  714.         jmp     .no_duplicate
  715.       @@:
  716.         dec     ecx
  717.   .no_fin2:
  718.  
  719. ; Handle the case when a bound socket connects to itself
  720. ; Allow packets with a SYN and an ACK to continue with the processing
  721.  
  722. ;-------------------------------------
  723. ; Generate duplicate ACK if nescessary
  724.  
  725. ; This code also handles simultaneous half-open or self-connects
  726.  
  727.         test    eax, eax
  728.         jnz     .drop_after_ack
  729.  
  730.         cmp     [edx + TCP_header.Flags], TH_ACK
  731.         jz      .drop_after_ack
  732.  
  733.   .duplicate:
  734.  
  735.         DEBUGF  1,"TCP_input: Duplicate received\n"
  736.  
  737. ;----------------------------------------
  738. ; Update statistics for duplicate packets
  739.  
  740. ;;; TODO
  741.  
  742.         jmp     .drop_after_ack
  743.  
  744.   .no_duplicate:
  745.  
  746. ;-----------------------------------------------
  747. ; Remove duplicate data and update urgent offset
  748.  
  749.         add     [edx + TCP_header.SequenceNumber], eax
  750.         sub     ecx, eax ;;;;;;;;  Checkme
  751.  
  752.         sub     [edx + TCP_header.UrgentPointer], ax
  753.         ja      @f
  754.  
  755.         and     [edx + TCP_header.Flags], not (TH_URG)
  756.         mov     [edx + TCP_header.UrgentPointer], 0
  757.        @@:
  758.  
  759. ;--------------------------------------------------
  760. ; Handle data that arrives after process terminates
  761.  
  762.         cmp     [ebx + SOCKET.PID], 0
  763.         ja      @f
  764.  
  765.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
  766.         jbe     @f
  767.  
  768.         test    ecx, ecx
  769.         jz      @f
  770.  
  771. ;;; Close the socket
  772. ;;; update stats
  773.  
  774.         jmp     .drop_with_reset
  775.        @@:
  776.  
  777. ;----------------------------------------
  778. ; Remove data beyond right edge of window
  779.  
  780.         mov     eax, [edx + TCP_header.SequenceNumber]
  781.         add     eax, ecx
  782.         sub     eax, [ebx + TCP_SOCKET.RCV_NXT]
  783.         sub     ax, [ebx + TCP_SOCKET.RCV_WND]
  784.  
  785. ; eax now holds the number of bytes to drop
  786.  
  787.         jbe     .no_excess_data
  788.  
  789. ;;; TODO: update stats
  790.  
  791.         cmp     eax, ecx
  792.         jb      .dont_drop_all
  793.  
  794. ;;; TODO 700-736
  795.  
  796.   .dont_drop_all:
  797.  
  798.   .no_excess_data:
  799.  
  800. ;-----------------
  801. ; Record timestamp
  802.  
  803. ;;; TODO 737-746
  804.  
  805.  
  806. ;------------------
  807. ; Process RST flags
  808.  
  809.         test    [edx + TCP_header.Flags], TH_RST
  810.         jz      .no_rst
  811.  
  812.         DEBUGF  1,"TCP_input: Got an RST flag\n"
  813.  
  814.         mov     eax, [ebx + TCP_SOCKET.t_state]
  815.         shl     eax, 2
  816.         jmp     dword [eax + .rst_sw_list]
  817.  
  818.   .rst_sw_list:
  819.         dd      .no_rst         ; TCPS_CLOSED
  820.         dd      .no_rst         ; TCPS_LISTEN
  821.         dd      .no_rst         ; TCPS_SYN_SENT
  822.         dd      .econnrefused   ; TCPS_SYN_RECEIVED
  823.         dd      .econnreset     ; TCPS_ESTABLISHED
  824.         dd      .econnreset     ; TCPS_CLOSE_WAIT
  825.         dd      .econnreset     ; TCPS_FIN_WAIT_1
  826.         dd      .rst_close      ; TCPS_CLOSING
  827.         dd      .rst_close      ; TCPS_LAST_ACK
  828.         dd      .econnreset     ; TCPS_FIN_WAIT_2
  829.         dd      .rst_close      ; TCPS_TIMED_WAIT
  830.  
  831.   .econnrefused:
  832.         DEBUGF  1,"TCP_input: Connection refused\n"
  833.  
  834.         mov     [ebx + SOCKET.errorcode], ECONNREFUSED
  835.         jmp     .close
  836.  
  837.   .econnreset:
  838.         DEBUGF  1,"TCP_input: Connection reset\n"
  839.  
  840.         mov     [ebx + SOCKET.errorcode], ECONNRESET
  841.  
  842.   .close:
  843.         DEBUGF  1,"TCP_input: Closing connection\n"
  844.  
  845.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSED
  846.         ;;; TODO: update stats (tcp drops)
  847.         mov     eax, ebx
  848.         call    TCP_close
  849.         jmp     .drop
  850.  
  851.   .rst_close:
  852.         DEBUGF  1,"TCP_input: Closing with reset\n"
  853.  
  854.         mov     eax, ebx
  855.         call    TCP_close
  856.         jmp     .drop
  857.  
  858.   .no_rst:
  859.  
  860.  
  861. ;--------------------------------------
  862. ; handle SYN-full and ACK-less segments
  863.  
  864.         test    [edx + TCP_header.Flags], TH_SYN
  865.         jz      @f
  866.  
  867.         mov     eax, ebx
  868.         mov     ebx, ECONNRESET
  869.         call    TCP_drop
  870.         jmp     .drop_with_reset
  871.       @@:
  872.  
  873. ;---------------
  874. ; ACK processing
  875.  
  876.         test    [edx + TCP_header.Flags], TH_ACK
  877.         jz      .drop
  878.  
  879.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  880.         jnz     .no_syn_rcv
  881.  
  882.         DEBUGF  1,"TCP_input: state=syn_received\n"
  883.  
  884.         mov     eax, [edx + TCP_header.AckNumber]
  885.         cmp     [ebx + TCP_SOCKET.SND_UNA], eax
  886.         ja      .drop_with_reset
  887.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  888.         ja      .drop_with_reset
  889.  
  890. ;;; TODO: update stats
  891.  
  892.         mov     eax, ebx
  893.         call    SOCKET_is_connected
  894.         mov     [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  895.  
  896. ; Do window scaling?
  897.  
  898.         test    [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  899.         jz      @f
  900.         test    [ebx + TCP_SOCKET.t_flags], TF_REQ_SCALE
  901.         jz      @f
  902.  
  903.         push    word [ebx + TCP_SOCKET.requested_s_scale]       ; Set send and receive scale factors to the received values
  904.         pop     word [ebx + TCP_SOCKET.SND_SCALE]
  905.        @@:
  906.  
  907. ;;; TODO: call TCP_reassemble
  908.  
  909.         mov     eax, [edx + TCP_header.SequenceNumber]
  910.         dec     eax
  911.         mov     [ebx + TCP_SOCKET.SND_WL1], eax
  912.  
  913.   .no_syn_rcv:
  914.  
  915. ; check for duplicate ACK
  916.  
  917.         mov     eax, [edx + TCP_header.AckNumber]
  918.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  919.         ja      .not_dup_ack
  920.  
  921.         test    ecx, ecx
  922.         jnz     .reset_dupacks
  923.  
  924.         mov     eax, dword [edx + TCP_header.Window]
  925.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  926.         jne     .reset_dupacks
  927.  
  928.         DEBUGF  1,"TCP_input: Processing duplicate ACK\n"
  929.  
  930.         cmp     [ebx + TCP_SOCKET.timer_retransmission], 10000 ;;;;  FIXME
  931.         ja      @f
  932.  
  933.         mov     eax, [edx + TCP_header.AckNumber]
  934.         cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
  935.         je      .dup_ack
  936.  
  937.        @@:
  938.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  939.         jmp     .not_dup_ack
  940.  
  941.   .dup_ack:
  942.         inc     [ebx + TCP_SOCKET.t_dupacks]
  943.         cmp     [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
  944.         jne     .no_re_xmit
  945.  
  946.         push    [ebx + TCP_SOCKET.SND_NXT]              ; >>>>
  947.  
  948.         mov     eax, [ebx + TCP_SOCKET.SND_WND]
  949.         cmp     eax, [ebx + TCP_SOCKET.SND_CWND]
  950.         cmova   eax, [ebx + TCP_SOCKET.SND_CWND]
  951.         shr     eax, 1
  952.         push    edx
  953.         xor     edx, edx
  954.         div     [ebx + TCP_SOCKET.t_maxseg]
  955.         cmp     eax, 2
  956.         jae     @f
  957.         mov     ax, 2
  958.        @@:
  959.         mul     [ebx + TCP_SOCKET.t_maxseg]
  960.         pop     edx
  961.         mov     [ebx + TCP_SOCKET.SND_SSTHRESH], eax
  962.  
  963.         mov     [ebx + TCP_SOCKET.timer_retransmission], 0      ; turn off retransmission timer
  964.         mov     [ebx + TCP_SOCKET.t_rtt], 0
  965.         mov     eax, [edx + TCP_header.AckNumber]
  966.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  967.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  968.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  969.  
  970.         mov     eax, ebx
  971.         call    TCP_output                                      ; retransmit missing segment
  972.  
  973.         push    edx
  974.         xor     edx, edx
  975.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  976.         mul     [ebx + TCP_SOCKET.t_dupacks]
  977.         pop     edx
  978.         add     eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
  979.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  980.  
  981.         pop     eax                                     ; <<<<
  982.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  983.         jb      @f
  984.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  985.        @@:
  986.  
  987.         jmp     .drop
  988.  
  989.  
  990.   .no_re_xmit:
  991.         jbe     .not_dup_ack
  992.  
  993.         DEBUGF  1,"TCP_input: Increasing congestion window\n"
  994.  
  995.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  996.         add     [ebx + TCP_SOCKET.SND_CWND], eax
  997.  
  998.         mov     eax, ebx
  999.         call    TCP_output
  1000.  
  1001.         jmp     .drop
  1002.  
  1003.  
  1004.   .not_dup_ack:
  1005.  
  1006. ;-------------------------------------------------
  1007. ; If the congestion window was inflated to account
  1008. ; for the other side's cached packets, retract it
  1009.  
  1010.         mov     eax, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1011.         cmp     eax, [ebx + TCP_SOCKET.SND_CWND]
  1012.         ja      @f
  1013.         cmp     [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh
  1014.         jbe     @f
  1015.         mov     [ebx + TCP_SOCKET.SND_CWND], eax
  1016.        @@:
  1017.  
  1018.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  1019.  
  1020.         mov     eax, [edx + TCP_header.AckNumber]
  1021.         cmp     eax, [ebx + TCP_SOCKET.SND_MAX]
  1022.         jbe     @f
  1023.  
  1024.         ;;; TODO: update stats
  1025.         jmp     .drop_after_ack
  1026.  
  1027.        @@:
  1028.  
  1029.         mov     edi, [edx + TCP_header.AckNumber]
  1030.         sub     edi, [ebx + TCP_SOCKET.SND_UNA]         ; now we got the number of acked bytes in edi
  1031.  
  1032.         ;;; TODO: update stats
  1033.  
  1034.  
  1035.         DEBUGF  1,"TCP_input: acceptable ACK for %u bytes\n", edi
  1036.  
  1037. ;------------------------------------------
  1038. ; RTT measurements and retransmission timer
  1039.  
  1040.         ;;;;; 912 - 926
  1041.  
  1042.         mov     [ebx + TCP_SOCKET.timer_retransmission], 0
  1043.  
  1044.         mov     eax, [ebx + TCP_SOCKET.SND_MAX]
  1045.         cmp     eax, [edx + TCP_header.AckNumber]
  1046.         je      .all_outstanding
  1047.         mov     [ebx + TCP_SOCKET.timer_retransmission], 120 ;;;; TODO: correct this value (use a macro for it)
  1048.  
  1049.   .all_outstanding:
  1050.         inc     [ebx + TCP_SOCKET.sendalot]     ; need output
  1051.  
  1052. ;-------------------------------------------
  1053. ; Open congestion window in response to ACKs
  1054.  
  1055.         mov     esi, [ebx + TCP_SOCKET.SND_CWND]
  1056.         mov     eax, [ebx + TCP_SOCKET.t_maxseg]
  1057.  
  1058.         cmp     esi, [ebx + TCP_SOCKET.SND_SSTHRESH]
  1059.         jbe     @f
  1060.         push    edx
  1061.         push    eax
  1062.         mul     eax
  1063.         div     esi
  1064.         pop     edx
  1065.         shr     edx, 3
  1066.         add     eax, edx
  1067.         pop     edx
  1068.        @@:
  1069.  
  1070.         add     esi, eax
  1071.  
  1072.         push    ecx
  1073.         mov     cl, [ebx + TCP_SOCKET.SND_SCALE]
  1074.         mov     eax, TCP_max_win
  1075.         shl     eax, cl
  1076.         pop     ecx
  1077.  
  1078.         cmp     esi, eax
  1079.         cmova   esi, eax
  1080.         mov     [ebx + TCP_SOCKET.SND_CWND], esi
  1081.  
  1082. ;------------------------------------------
  1083. ; Remove acknowledged data from send buffer
  1084.  
  1085.         cmp     edi, [ebx + STREAM_SOCKET.snd.size]
  1086.         jbe     .finiacked
  1087.  
  1088.         push    ecx edx ebx
  1089.         mov     ecx, [ebx + STREAM_SOCKET.snd.size]
  1090.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1091.         sub     [ebx + TCP_SOCKET.SND_WND], ecx
  1092.         call    SOCKET_ring_free
  1093.         pop     ebx edx ecx
  1094.  
  1095.         DEBUGF  1,"TCP_input: our FIN is acked\n"
  1096.         stc
  1097.  
  1098.         jmp     .wakeup
  1099.  
  1100.   .finiacked:
  1101.  
  1102.         push    ecx edx ebx
  1103.         mov     ecx, edi
  1104.         lea     eax, [ebx + STREAM_SOCKET.snd]
  1105.         call    SOCKET_ring_free
  1106.         pop     ebx
  1107.         sub     [ebx + TCP_SOCKET.SND_WND], ecx
  1108.         pop     edx ecx
  1109.  
  1110.         DEBUGF  1,"TCP_input: our FIN is not acked\n"
  1111.         clc
  1112.  
  1113. ;----------------------------------------
  1114. ; Wake up process waiting on send buffer
  1115.  
  1116.   .wakeup:
  1117.  
  1118.         pushf
  1119.         mov     eax, ebx
  1120.         call    SOCKET_notify_owner
  1121.  
  1122. ; Update TCPS
  1123.  
  1124.         mov     eax, [edx + TCP_header.AckNumber]
  1125.         mov     [ebx + TCP_SOCKET.SND_UNA], eax
  1126.         cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
  1127.         jb      @f
  1128.         mov     [ebx + TCP_SOCKET.SND_NXT], eax
  1129.        @@:
  1130.  
  1131.         popf
  1132.  
  1133. ; General ACK handling complete
  1134. ; Now do the state-specific ones
  1135.  
  1136.         mov     eax, [ebx + TCP_SOCKET.t_state]
  1137.         jmp     dword [eax*4 + .ACK_sw_list]
  1138.  
  1139.   .ACK_sw_list:
  1140.         dd      .ack_processed  ; TCPS_CLOSED
  1141.         dd      .ack_processed  ; TCPS_LISTEN
  1142.         dd      .ack_processed  ; TCPS_SYN_SENT
  1143.         dd      .ack_processed  ; TCPS_SYN_RECEIVED
  1144.         dd      .ack_processed  ; TCPS_ESTABLISHED
  1145.         dd      .ack_processed  ; TCPS_CLOSE_WAIT
  1146.         dd      .ack_fw1        ; TCPS_FIN_WAIT_1
  1147.         dd      .ack_c          ; TCPS_CLOSING
  1148.         dd      .ack_la         ; TCPS_LAST_ACK
  1149.         dd      .ack_processed  ; TCPS_FIN_WAIT_2
  1150.         dd      .ack_tw         ; TCPS_TIMED_WAIT
  1151.  
  1152.  
  1153.   .ack_fw1:
  1154.         jnc     .ack_processed
  1155.  
  1156.         test    [ebx + SOCKET.state], SS_CANTRCVMORE
  1157.         jnz     @f
  1158.         mov     eax, ebx
  1159.         call    SOCKET_is_disconnected
  1160. ;;;        mov     [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle       ; FIXME
  1161.        @@:
  1162.  
  1163.         mov     [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
  1164.         jmp     .ack_processed
  1165.  
  1166.  
  1167.   .ack_c:
  1168.         jnc     .ack_processed
  1169.  
  1170.         mov     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
  1171.         mov     eax, ebx
  1172.         call    TCP_cancel_timers
  1173.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1174.         mov     eax, ebx
  1175.         call    SOCKET_is_disconnected
  1176.         jmp     .ack_processed
  1177.  
  1178.  
  1179.   .ack_la:
  1180.         jnc     .ack_processed
  1181.  
  1182.  
  1183.         mov     eax, ebx
  1184.         call    TCP_disconnect
  1185.         jmp     .drop
  1186.  
  1187.  
  1188.   .ack_tw:
  1189.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1190.         jmp     .drop_after_ack
  1191.  
  1192.  
  1193.  
  1194.   .reset_dupacks:               ; We got a new ACK, reset duplicate ACK counter
  1195.  
  1196.         mov     [ebx + TCP_SOCKET.t_dupacks], 0
  1197.  
  1198.   .ack_processed:       ; (step 6)
  1199.  
  1200.         DEBUGF  1,"TCP_input: ACK processed\n"
  1201.  
  1202. ;----------------------------------------------
  1203. ; check if we need to update window information
  1204.  
  1205.         test    [edx + TCP_header.Flags], TH_ACK
  1206.         jz      .no_window_update
  1207.  
  1208.         mov     eax, [ebx + TCP_SOCKET.SND_WL1]
  1209.         cmp     eax, [edx + TCP_header.SequenceNumber]
  1210.         jb      .update_window
  1211.         ja      @f
  1212.  
  1213.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1214.         cmp     eax, [edx + TCP_header.AckNumber]
  1215.         jb      .update_window
  1216.         ja      .no_window_update
  1217.        @@:
  1218.  
  1219.         mov     eax, [ebx + TCP_SOCKET.SND_WL2]
  1220.         cmp     eax, [edx + TCP_header.AckNumber]
  1221.         jne     .no_window_update
  1222.  
  1223.         mov     eax, dword [edx + TCP_header.Window]
  1224.         cmp     eax, [ebx + TCP_SOCKET.SND_WND]
  1225.         jbe     .no_window_update
  1226.  
  1227.   .update_window:
  1228.  
  1229. ;;; TODO: Keep track of pure window updates
  1230.  
  1231.         mov     eax, dword [edx + TCP_header.Window]
  1232.         cmp     eax, [ebx + TCP_SOCKET.max_sndwnd]
  1233.         jbe     @f
  1234.         mov     [ebx + TCP_SOCKET.max_sndwnd], eax
  1235.        @@:
  1236.         mov     [ebx + TCP_SOCKET.SND_WND], eax
  1237.  
  1238.         DEBUGF  1,"TCP_input: Updating window to %u\n", eax
  1239.  
  1240.         push    [edx + TCP_header.SequenceNumber]
  1241.         pop     [ebx + TCP_SOCKET.SND_WL1]
  1242.  
  1243.         push    [edx + TCP_header.AckNumber]
  1244.         pop     [ebx + TCP_SOCKET.SND_WL2]
  1245.  
  1246.         inc     [ebx + TCP_SOCKET.sendalot]
  1247.  
  1248.   .no_window_update:
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256. ;-----------------
  1257. ; process URG flag
  1258.  
  1259.         test    [edx + TCP_header.Flags], TH_URG
  1260.         jz      .not_urgent
  1261.  
  1262.         cmp     [edx + TCP_header.UrgentPointer], 0
  1263.         jz      .not_urgent
  1264.  
  1265.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
  1266.         je      .not_urgent
  1267.  
  1268. ; Ignore bogus urgent offsets
  1269.  
  1270.         ;;; 1040-1050
  1271.  
  1272.         movzx   eax, [edx + TCP_header.UrgentPointer]
  1273.         add     eax, [ebx + STREAM_SOCKET.rcv.size]
  1274.         cmp     eax, SOCKET_MAXDATA
  1275.         jbe     .not_urgent
  1276.  
  1277.         mov     [edx + TCP_header.UrgentPointer], 0
  1278.         and     [edx + TCP_header.Flags], not (TH_URG)
  1279.         jmp     .do_data
  1280.  
  1281.   .not_urgent:
  1282.  
  1283. ; processing of received urgent pointer
  1284.  
  1285.         ;;; TODO (1051-1093)
  1286.  
  1287.  
  1288. ;---------------------------------------
  1289. ; process the data in the segment (1094)
  1290.  
  1291.   .do_data:
  1292.  
  1293.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
  1294.         jae     .final_processing
  1295.  
  1296.         test    [edx + TCP_header.Flags], TH_FIN
  1297.         jnz     @f
  1298.  
  1299.         test    ecx, ecx
  1300.         jnz     .final_processing
  1301.        @@:
  1302.  
  1303. ;        call    TCP_reassemble        ;;; TODO
  1304.  
  1305. ;---------------
  1306. ; FIN processing
  1307.  
  1308.         test    [edx + TCP_header.Flags], TH_FIN
  1309.         jz      .no_fin
  1310.  
  1311.         DEBUGF  1,"TCP_input: Processing FIN\n"
  1312.  
  1313.         cmp     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
  1314.         jae     .not_first_fin
  1315.  
  1316.         DEBUGF  1,"TCP_input: First FIN for this connection\n"
  1317.  
  1318.         mov     eax, ebx
  1319.         call    SOCKET_cant_recv_more
  1320.  
  1321.         mov     [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1322.         inc     [ebx + TCP_SOCKET.RCV_NXT]
  1323.  
  1324.   .not_first_fin:
  1325.         mov     eax, [ebx + TCP_SOCKET.t_state]
  1326.         shl     eax, 2
  1327.         jmp     dword [eax + .FIN_sw_list]
  1328.  
  1329.   .FIN_sw_list:
  1330.         dd      .no_fin         ; TCPS_CLOSED
  1331.         dd      .no_fin         ; TCPS_LISTEN
  1332.         dd      .no_fin         ; TCPS_SYN_SENT
  1333.         dd      .fin_syn_est    ; TCPS_SYN_RECEIVED
  1334.         dd      .fin_syn_est    ; TCPS_ESTABLISHED
  1335.         dd      .no_fin         ; TCPS_CLOSE_WAIT
  1336.         dd      .fin_wait1      ; TCPS_FIN_WAIT_1
  1337.         dd      .no_fin         ; TCPS_CLOSING
  1338.         dd      .no_fin         ; TCPS_LAST_ACK
  1339.         dd      .fin_wait2      ; TCPS_FIN_WAIT_2
  1340.         dd      .fin_timed      ; TCPS_TIMED_WAIT
  1341.  
  1342.   .fin_syn_est:
  1343.  
  1344.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
  1345.         jmp     .no_fin
  1346.  
  1347.   .fin_wait1:
  1348.  
  1349.         mov     [ebx + TCP_SOCKET.t_state], TCPS_CLOSING
  1350.         jmp     .no_fin
  1351.  
  1352.   .fin_wait2:
  1353.  
  1354.         mov     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
  1355.         mov     eax, ebx
  1356.         call    TCP_cancel_timers
  1357.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1358.         call    SOCKET_is_disconnected
  1359.         jmp     .no_fin
  1360.  
  1361.   .fin_timed:
  1362.         mov     [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL
  1363.         jmp     .no_fin
  1364.  
  1365.   .no_fin:
  1366.  
  1367.  
  1368.  
  1369.  
  1370. ;-----------------
  1371. ; Final processing
  1372.  
  1373.   .final_processing:
  1374.  
  1375.         DEBUGF  1,"TCP_input: Final processing\n"
  1376.  
  1377.         cmp     [ebx + TCP_SOCKET.sendalot], 0
  1378.         jne     .need_output
  1379.  
  1380.         test    [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1381.         jz      .dumpit
  1382.  
  1383.         DEBUGF  1,"TCP_input: ACK now!\n"
  1384.  
  1385.   .need_output:
  1386.  
  1387.         pusha
  1388.         lea     ecx, [ebx + SOCKET.mutex]
  1389.         call    mutex_unlock
  1390.         popa
  1391.  
  1392.         push    ebx
  1393.         mov     eax, ebx
  1394.         call    TCP_output
  1395.         pop     ebx
  1396.  
  1397.         call    kernel_free
  1398.         add     esp, 4
  1399.         ret
  1400.  
  1401.   .dumpit:
  1402.  
  1403.         DEBUGF  1,"TCP_input: dumping\n"
  1404.  
  1405.         pusha
  1406.         lea     ecx, [ebx + SOCKET.mutex]
  1407.         call    mutex_unlock
  1408.         popa
  1409.  
  1410.         call    kernel_free
  1411.         add     esp, 4
  1412.         ret
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420. ;------------------------------------------
  1421. ; Generate an ACK, droping incoming segment
  1422.  
  1423. align 4
  1424. .drop_after_ack:
  1425.  
  1426.         DEBUGF  1,"TCP_input: Drop after ACK\n"
  1427.  
  1428.         test    [edx + TCP_header.Flags], TH_RST
  1429.         jnz     .drop
  1430.  
  1431.         and     [ebx + TCP_SOCKET.t_flags], TF_ACKNOW
  1432.  
  1433.         pusha
  1434.         lea     ecx, [ebx + SOCKET.mutex]
  1435.         call    mutex_unlock
  1436.         popa
  1437.  
  1438.         push    ebx
  1439. ;        mov     cl, TH_ACK
  1440. ;        call    TCP_respond_socket
  1441.         mov     eax, ebx
  1442.         call    TCP_output
  1443.         pop     ebx
  1444.  
  1445.         call    kernel_free
  1446.         add     esp, 4
  1447.         ret
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. ;-------------------------------------------
  1457. ; Generate an RST, dropping incoming segment
  1458.  
  1459. align 4
  1460. .drop_with_reset:
  1461.  
  1462.         DEBUGF  1,"TCP_input: Drop with reset\n"
  1463.  
  1464.         pusha
  1465.         lea     ecx, [ebx + SOCKET.mutex]
  1466.         call    mutex_unlock
  1467.         popa
  1468.  
  1469.         test    [edx + TCP_header.Flags], TH_RST
  1470.         jnz     .drop
  1471.  
  1472.         ;;; if its a multicast/broadcast, also drop
  1473.  
  1474.         test    [edx + TCP_header.Flags], TH_ACK
  1475.         jnz     .respond_ack
  1476.  
  1477.         test    [edx + TCP_header.Flags], TH_SYN
  1478.         jnz     .respond_syn
  1479.  
  1480.         call    kernel_free
  1481.         add     esp, 4
  1482.         ret
  1483.  
  1484.   .respond_ack:
  1485.  
  1486.         push    ebx
  1487.         mov     cl, TH_RST
  1488.         call    TCP_respond_socket
  1489.         pop     ebx
  1490.         jmp     .destroy_new_socket
  1491.  
  1492.  
  1493.   .respond_syn:
  1494.  
  1495.         push    ebx
  1496.         mov     cl, TH_RST + TH_ACK
  1497.         call    TCP_respond_socket
  1498.         pop     ebx
  1499.         jmp     .destroy_new_socket
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505. ;-----
  1506. ; Drop
  1507.  
  1508. align 4
  1509. .drop:
  1510.  
  1511.         pusha
  1512.         lea     ecx, [ebx + SOCKET.mutex]
  1513.         call    mutex_unlock
  1514.         popa
  1515.  
  1516. .drop_not_locked:
  1517.  
  1518.         DEBUGF  1,"TCP_input: Dropping packet\n"
  1519.  
  1520.         ;;;; If debugging options are enabled, output the packet somwhere
  1521.  
  1522.   .destroy_new_socket:
  1523.  
  1524.         ;;;; kill the newly created socket
  1525.  
  1526.         call    kernel_free
  1527.         add     esp, 4
  1528.         ret
  1529.  
  1530.  
  1531.  
  1532.  
  1533. .drop_with_reset_no_socket:
  1534.  
  1535.         DEBUGF  1,"TCP_input: Drop with reset (no socket)\n"
  1536.  
  1537.         test    [edx + TCP_header.Flags], TH_RST
  1538.         jnz     .drop_no_socket
  1539.  
  1540.         ;;; if its a multicast/broadcast, also drop
  1541.  
  1542.         test    [edx + TCP_header.Flags], TH_ACK
  1543.         jnz     .respond_seg_ack
  1544.  
  1545.         test    [edx + TCP_header.Flags], TH_SYN
  1546.         jnz     .respond_seg_syn
  1547.  
  1548. .drop_no_socket:
  1549.  
  1550.         DEBUGF  1,"TCP_input: Drop (no socket)\n"
  1551.  
  1552.         call    kernel_free
  1553.         add     esp, 4
  1554.         ret
  1555.  
  1556.   .respond_seg_ack:
  1557.  
  1558.         mov     cl, TH_RST
  1559.         call    TCP_respond_segment
  1560.         jmp     .drop_no_socket
  1561.  
  1562.   .respond_seg_syn:
  1563.  
  1564.         mov     cl, TH_RST + TH_ACK
  1565.         call    TCP_respond_segment
  1566.         jmp     .drop_no_socket