Subversion Repositories Kolibri OS

Rev

Rev 5155 | Rev 5442 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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