Subversion Repositories Kolibri OS

Rev

Rev 4429 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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