Subversion Repositories Kolibri OS

Rev

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

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