Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  Part of the tcp/ip network stack for KolibriOS                 ;;
  7. ;;                                                                 ;;
  8. ;;   Written by hidnplayr@kolibrios.org                            ;;
  9. ;;                                                                 ;;
  10. ;;    Based on the code of 4.4BSD                                  ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 2995 $
  18.  
  19. ;-----------------------------------------------------------------
  20. ;
  21. ; TCP_output
  22. ;
  23. ; IN:  eax = socket pointer
  24. ;
  25. ; OUT: /
  26. ;
  27. ;-----------------------------------------------------------------
  28. align 4
  29. TCP_output:
  30.  
  31.         pushf
  32.         cli
  33.  
  34.         DEBUGF 1,"TCP_output: socket=%x\n", eax
  35.  
  36.         pusha
  37.         lea     ecx, [eax + SOCKET.mutex]
  38.         call    mutex_lock
  39.         popa
  40.  
  41. ; We'll detect the length of the data to be transmitted, and flags to be used
  42. ; If there is some data, or any critical controls to send (SYN / RST), then transmit
  43. ; Otherwise, investigate further
  44.  
  45.         mov     ebx, [eax + TCP_SOCKET.SND_MAX]
  46.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  47.         jbe     .not_idle
  48.  
  49.         mov     ebx, [eax + TCP_SOCKET.t_idle]
  50.         cmp     ebx, [eax + TCP_SOCKET.t_rxtcur]
  51.         jbe     .not_idle
  52.  
  53. ; We have been idle for a while and no ACKS are expected to clock out any data we send..
  54. ; Slow start to get ack "clock" running again.
  55.  
  56.         mov     ebx, [eax + TCP_SOCKET.t_maxseg]
  57.         mov     [eax + TCP_SOCKET.SND_CWND], ebx
  58.  
  59.   .not_idle:
  60.   .again:
  61.         mov     [eax + TCP_SOCKET.temp_bits], 0
  62.  
  63.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]         ; calculate offset (71)
  64.         sub     ebx, [eax + TCP_SOCKET.SND_UNA]         ;
  65.  
  66.         mov     ecx, [eax + TCP_SOCKET.SND_WND]         ; determine window
  67.         cmp     ecx, [eax + TCP_SOCKET.SND_CWND]        ;
  68.         jb      @f                                      ;
  69.         mov     ecx, [eax + TCP_SOCKET.SND_CWND]        ;
  70.        @@:                                              ;
  71.  
  72.         call    TCP_outflags                            ; flags in dl
  73.  
  74. ;------------------------
  75. ; data being forced out ?
  76.  
  77. ; If in persist timeout with window of 0, send 1 byte.
  78. ; Otherwise, if window is small but nonzero, and timer expired,
  79. ; we will send what we can and go to transmit state
  80.  
  81.         cmp     [eax + TCP_SOCKET.t_force], 0
  82.         je      .no_force
  83.  
  84.         test    ecx, ecx
  85.         jnz     .no_zero_window
  86.  
  87.         cmp     ebx, [eax + STREAM_SOCKET.snd.size]
  88.         jae     @f
  89.  
  90.         and     dl, not (TH_FIN)
  91.  
  92.        @@:
  93.         inc     ecx
  94.         jmp     .no_force
  95.  
  96.   .no_zero_window:
  97.         mov     [eax + TCP_SOCKET.timer_persist], 0
  98.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  99.  
  100.   .no_force:
  101.  
  102. ;--------------------------------
  103. ; Calculate how much data to send (106)
  104.  
  105.         mov     esi, [eax + STREAM_SOCKET.snd.size]
  106.         cmp     esi, ecx
  107.         jb      @f
  108.         mov     esi, ecx
  109.        @@:
  110.         sub     esi, ebx
  111.  
  112.  
  113. ;------------------------
  114. ; check for window shrink (107)
  115.  
  116. ; If FIN has been set, but not ACKed, but we havent been called to retransmit, esi will be -1
  117. ; Otherwise, window shrank after we sent into it.
  118.  
  119.         jae     .not_persist
  120.  
  121. ; enter persist state
  122.         xor     esi, esi
  123.  
  124. ; If window shrank to 0
  125.         test    ecx, ecx
  126.         jnz     @f
  127.  
  128. ; cancel pending retransmit
  129.         mov     [eax + TCP_SOCKET.timer_retransmission], 0
  130.  
  131. ; pull SND_NXT back to (closed) window, We will enter persist state below.
  132.         push    [eax + TCP_SOCKET.SND_UNA]
  133.         pop     [eax + TCP_SOCKET.SND_NXT]
  134.        @@:
  135.  
  136. ; If window didn't close completely, just wait for an ACK
  137.  
  138.   .not_persist:
  139.  
  140. ;---------------------------
  141. ; Send one segment at a time (124)
  142.  
  143.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  144.         jbe     @f
  145.  
  146.         mov     esi, [eax + TCP_SOCKET.t_maxseg]
  147.         or      [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
  148.        @@:
  149.  
  150. ;--------------------------------------------
  151. ; Turn of FIN flag if send buffer not emptied (128)
  152.  
  153.         mov     edi, [eax + TCP_SOCKET.SND_NXT]
  154.         add     edi, esi
  155.         sub     edi, [eax + TCP_SOCKET.SND_UNA]
  156.         cmp     edi, [eax + STREAM_SOCKET.snd.size]
  157.         jae     @f
  158.         and     dl, not (TH_FIN)
  159.  
  160.        @@:
  161.  
  162. ;-------------------------------
  163. ; calculate window advertisement (130)
  164.  
  165.         mov     ecx, SOCKET_MAXDATA
  166.         sub     ecx, [eax + STREAM_SOCKET.rcv.size]
  167.  
  168. ;------------------------------
  169. ; Sender silly window avoidance (131)
  170.  
  171.         test    esi, esi
  172.         jz      .len_zero
  173.  
  174.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  175.         je      TCP_send
  176.  
  177.         add     ebx, esi                                ; offset + length
  178.         cmp     ebx, [eax + STREAM_SOCKET.snd.size]
  179.         jb      @f
  180.  
  181.         test    [eax + TCP_SOCKET.t_flags], TF_NODELAY
  182.         jnz     TCP_send
  183.  
  184.         mov     ebx, [eax + TCP_SOCKET.SND_MAX]
  185.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  186.         je      TCP_send
  187.        @@:
  188.  
  189.         test    [eax + TCP_SOCKET.t_force], -1  ;;;
  190.         jnz     TCP_send
  191.  
  192.         mov     ebx, [eax + TCP_SOCKET.max_sndwnd]
  193.         shr     ebx, 1
  194.         cmp     esi, ebx
  195.         jae     TCP_send
  196.  
  197.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]
  198.         cmp     ebx, [eax + TCP_SOCKET.SND_MAX]
  199.         jb      TCP_send
  200.  
  201.   .len_zero:
  202.  
  203. ;----------------------------------------
  204. ; Check if a window update should be sent (154)
  205.  
  206.         DEBUGF  1,"TCP_output: window=%d\n", ecx
  207.  
  208. ; Compare available window to amount of window known to peer (as advertised window less next expected input)
  209. ; If the difference is at least two max size segments, or at least 50% of the maximum possible window,
  210. ; Then we want to send a window update to the peer.
  211.  
  212.         test    ecx, ecx
  213.         jz      .no_window
  214.  
  215.         push    ecx
  216.         mov     cl, [eax + TCP_SOCKET.RCV_SCALE]
  217.         mov     ebx, TCP_max_win
  218.         shl     ebx, cl
  219.         pop     ecx
  220.         cmp     ebx, ecx
  221.         jb      @f
  222.         mov     ebx, ecx
  223.        @@:
  224.         sub     ebx, [eax + TCP_SOCKET.RCV_ADV]
  225.         add     ebx, [eax + TCP_SOCKET.RCV_NXT]
  226.  
  227.         mov     edi, [eax + TCP_SOCKET.t_maxseg]
  228.         shl     edi, 1
  229.  
  230. ;        cmp     ebx, edi
  231. ;        jae     TCP_send
  232.  
  233. ;        cmp     ebx, [eax + TCP_SOCKET.]    ;;; TODO: check with receive buffer high water mark
  234. ;        jae     TCP_send
  235.  
  236.   .no_window:
  237.  
  238. ;--------------------------
  239. ; Should a segment be sent? (174)
  240.  
  241.         DEBUGF  1,"TCP_output: 174\n"
  242.  
  243.         test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW   ; we need to ACK
  244.         jnz     TCP_send
  245.  
  246.         test    dl, TH_SYN + TH_RST                     ; we need to send a SYN or RST
  247.         jnz     TCP_send
  248.  
  249.         mov     ebx, [eax + TCP_SOCKET.SND_UP]          ; when urgent pointer is beyond start of send bufer
  250.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  251.         ja      TCP_send
  252.  
  253.         test    dl, TH_FIN
  254.         jz      .enter_persist  ; no reason to send, enter persist state
  255.  
  256. ; FIN was set, only send if not already sent, or on retransmit
  257.  
  258.         test    [eax + TCP_SOCKET.t_flags], TF_SENTFIN
  259.         jz      TCP_send
  260.  
  261.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]
  262.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  263.         je      TCP_send
  264.  
  265. ;--------------------
  266. ; Enter persist state (191)
  267.  
  268.   .enter_persist:
  269.  
  270.         cmp     [eax + STREAM_SOCKET.snd.size], 0       ; Data ready to send?
  271.         jne     @f
  272.         cmp     [eax + TCP_SOCKET.timer_retransmission], 0
  273.         jne     @f
  274.         cmp     [eax + TCP_SOCKET.timer_persist], 0     ; Persist timer already expired?
  275.         jne     @f
  276.  
  277.         DEBUGF  1,"TCP_output: Entering persist state\n"
  278.  
  279.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  280.         call    TCP_set_persist
  281.        @@:
  282.  
  283. ;----------------------------
  284. ; No reason to send a segment (219)
  285.  
  286.         DEBUGF  1,"TCP_output: No reason to send a segment\n"
  287.  
  288.         pusha
  289.         lea     ecx, [eax + SOCKET.mutex]
  290.         call    mutex_unlock
  291.         popa
  292.  
  293.         popf
  294.         ret
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304. ;-----------------------------------------------
  305. ;
  306. ; Send a segment (222)
  307. ;
  308. ; eax = socket pointer
  309. ; esi = data len
  310. ;  dl = flags
  311. ;
  312. ;-----------------------------------------------
  313. align 4
  314. TCP_send:
  315.  
  316.         DEBUGF  1,"TCP_send: socket=%x length=%u flags=%x\n", eax, esi, dl
  317.  
  318.         push    eax                     ; save socket ptr
  319.         push    esi                     ; and data length too
  320.         mov     edi, sizeof.TCP_header  ; edi will contain headersize
  321.  
  322. ;------------------------------------
  323. ; Send options with first SYN segment
  324.  
  325.         test    dl, TH_SYN
  326.         jz      .options_done
  327.  
  328.         push    [eax + TCP_SOCKET.ISS]
  329.         pop     [eax + TCP_SOCKET.SND_NXT]
  330.  
  331.         test    [eax + TCP_SOCKET.t_flags], TF_NOOPT
  332.         jnz     .options_done
  333.  
  334.         mov     ecx, 1460                              ;;;; FIXME: use routing blablabla to determine MSS
  335.         or      ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16
  336.         bswap   ecx
  337.         push    ecx
  338.         add     di, 4
  339.  
  340.         DEBUGF  1,"TCP_send: added maxseg option\n"
  341.  
  342.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE
  343.         jz      .no_scale
  344.  
  345.         test    dl, TH_ACK
  346.         jz      .scale_opt
  347.  
  348.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  349.         jz      .no_scale
  350.  
  351.   .scale_opt:
  352.         mov     cl, [eax + TCP_SOCKET.request_r_scale]
  353.         mov     ch, TCP_OPT_NOP
  354.         pushw   cx
  355.         pushw   TCP_OPT_WINDOW + 3 shl 8
  356.         add     di, 4
  357.  
  358.         DEBUGF  1,"TCP_send: added scale option\n"
  359.  
  360.   .no_scale:
  361.   .no_syn:
  362.  
  363. ;------------------------------------
  364. ; Make the timestamp option if needed
  365.  
  366.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP
  367.         jz      .no_timestamp
  368.  
  369.         test    dl, TH_RST
  370.         jnz     .no_timestamp
  371.  
  372.         test    dl, TH_ACK
  373.         jz      .timestamp
  374.  
  375.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
  376.         jz      .no_timestamp
  377.  
  378.   .timestamp:
  379.         pushd   0
  380.         pushd   [timer_ticks]
  381.         pushd   TCP_OPT_NOP + TCP_OPT_NOP shl 8 + TCP_OPT_TIMESTAMP shl 16 + 10 shl 24
  382.         add     di, 12
  383.  
  384.         DEBUGF  1,"TCP_send: added timestamp\n"
  385.  
  386.   .no_timestamp:
  387.  
  388.         ; <Add additional options here>
  389.  
  390.   .options_done:
  391.  
  392. ; eax = socket ptr
  393. ; edx = flags
  394. ; edi = header size
  395. ; esi = data len
  396.  
  397. ;---------------------------------------------
  398. ; check if we dont exceed the max segment size (270)
  399.  
  400.         add     esi, edi                        ; total TCP segment size
  401.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  402.         jbe     .no_overflow
  403.  
  404.         mov     esi, [eax + TCP_SOCKET.t_maxseg]
  405.         or      [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
  406.   .no_overflow:
  407.  
  408. ;-----------------------------------------------------------------
  409. ; Start by pushing all TCP header values in reverse order on stack
  410. ; (essentially, creating the tcp header on the stack!)
  411.  
  412.         pushw   0       ;        .UrgentPointer          dw ?
  413.         pushw   0       ;        .Checksum               dw ?
  414.         pushw   0x00a0  ;        .Window                 dw ?    ;;;;;;; FIXME (370)
  415.         shl     edi, 2  ;        .DataOffset             db ?  only 4 left-most bits
  416.         shl     dx, 8
  417.         or      dx, di  ;        .Flags                  db ?
  418.         pushw   dx
  419.         shr     edi, 2  ;        .DataOffset             db ?
  420.  
  421.         push    [eax + TCP_SOCKET.RCV_NXT]      ;        .AckNumber              dd ?
  422.         ntohd   [esp]
  423.  
  424.         push    [eax + TCP_SOCKET.SND_NXT]      ;        .SequenceNumber         dd ?
  425.         ntohd   [esp]
  426.  
  427.         push    [eax + TCP_SOCKET.RemotePort]   ;        .DestinationPort        dw ?
  428.         push    [eax + TCP_SOCKET.LocalPort]    ;        .SourcePort             dw ?
  429.  
  430.         push    edi                     ; header size
  431.  
  432. ;---------------------
  433. ; Create the IP packet
  434.  
  435.         mov     ecx, esi
  436.  
  437.         mov     ebx, [eax + SOCKET.device]
  438.         mov     edx, [eax + IP_SOCKET.LocalIP]  ; source ip
  439.         mov     eax, [eax + IP_SOCKET.RemoteIP] ; dest ip
  440.         mov     di, IP_PROTO_TCP shl 8 + 128
  441.         call    IPv4_output
  442.         jz      .ip_error
  443.  
  444. ;-----------------------------------------
  445. ; Move TCP header from stack to TCP packet
  446.  
  447.         push    ecx
  448.         mov     ecx, [esp + 4]
  449.         lea     esi, [esp + 8]
  450.         shr     ecx, 2                  ; count is in bytes, we will work with dwords
  451.         rep     movsd
  452.         pop     ecx                     ; full TCP packet size
  453.  
  454.         pop     esi                     ; headersize
  455.         add     esp, esi                ; remove it from stack
  456.  
  457.         push    edx                     ; packet size for send proc
  458.         push    eax                     ; packet ptr for send proc
  459.  
  460.         mov     edx, edi                ; begin of data
  461.         sub     edx, esi                ; begin of packet (edi = begin of data)
  462.         push    ecx
  463.         sub     ecx, esi                ; data size
  464.  
  465. ;--------------
  466. ; Copy the data
  467.  
  468. ; eax = ptr to ring struct
  469. ; ecx = buffer size
  470. ; edi = ptr to buffer
  471.  
  472.         mov     eax, [esp + 16]                 ; get socket ptr
  473.  
  474.         push    edx
  475.         push    [eax + TCP_SOCKET.SND_NXT]      ; we'll need this for timing the transmission
  476.         test    ecx, ecx
  477.         jz      .nodata
  478.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  479.         add     [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number <<< CHECKME
  480.         sub     edx, [eax + TCP_SOCKET.SND_UNA] ; offset
  481.         add     eax, STREAM_SOCKET.snd
  482.         call    SOCKET_ring_read
  483.   .nodata:
  484.         pop     edi
  485.         pop     esi                             ; begin of data
  486.         pop     ecx                             ; full packet size
  487.         mov     eax, [esp + 12]                 ; socket ptr
  488.  
  489. ;----------------------------------
  490. ; initialize retransmit timer (400)
  491.  
  492. ;TODO: check t_force and persist
  493.  
  494.         test    [esi + TCP_header.Flags], TH_SYN + TH_FIN       ; syn and fin take a sequence number
  495.         jz      @f
  496.         inc     [eax + TCP_SOCKET.SND_NXT]
  497.         test    [esi + TCP_header.Flags], TH_FIN
  498.         jz      @f
  499.         or      [eax + TCP_SOCKET.t_flags], TF_SENTFIN          ; if we sent a fin, set the sentfin flag
  500.        @@:
  501.  
  502.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  503.         cmp     edx, [eax + TCP_SOCKET.SND_MAX]                 ; is this a retransmission?
  504.         jbe     @f
  505.         mov     [eax + TCP_SOCKET.SND_MAX], edx                 ; [eax + TCP_SOCKET.SND_NXT] from before we updated it
  506.  
  507.         cmp     [eax + TCP_SOCKET.t_rtt], 0                     ; are we currently timing anything?
  508.         je      @f
  509.         mov     [eax + TCP_SOCKET.t_rtt], 1                     ; nope, start transmission timer
  510.         mov     [eax + TCP_SOCKET.t_rtseq], edi
  511. ;TODO: update stats
  512.        @@:
  513.  
  514. ; set retransmission timer if not already set, and not doing an ACK or keepalive probe
  515.  
  516.         cmp     [eax + TCP_SOCKET.timer_retransmission], 0 ;;;; FIXME
  517.         ja      .retransmit_set
  518.  
  519.         cmp     edx, [eax + TCP_SOCKET.SND_UNA]                 ; edx is still [eax + TCP_SOCKET.SND_NXT]
  520.         je      .retransmit_set
  521.  
  522.         mov     edx, [eax + TCP_SOCKET.t_rxtcur]
  523.         mov     [eax + TCP_SOCKET.timer_retransmission], edx
  524.  
  525.         cmp     [eax + TCP_SOCKET.timer_persist], 0
  526.         jne     .retransmit_set
  527.         mov     [eax + TCP_SOCKET.timer_persist], 0
  528.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  529.  
  530.   .retransmit_set:
  531.  
  532. ;--------------------
  533. ; Create the checksum
  534.  
  535.         TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP)
  536.         mov     [esi + TCP_header.Checksum], dx
  537.  
  538. ; unlock socket
  539.         lea     ecx, [eax + SOCKET.mutex]
  540.         call    mutex_unlock
  541.  
  542. ;----------------
  543. ; Send the packet
  544.  
  545.         DEBUGF  1,"TCP_send: Sending with device %x\n", ebx
  546.         call    [ebx + NET_DEVICE.transmit]
  547.         jnz     .send_error
  548.  
  549. ;---------------
  550. ; Ok, data sent!
  551.  
  552.         pop     ecx
  553.         pop     eax
  554.  
  555.         inc     [TCP_segments_tx]       ; FIXME: correct interface?
  556.  
  557. ; unlock socket
  558.         lea     ecx, [eax + SOCKET.mutex]
  559.         call    mutex_lock
  560.  
  561. ; update advertised receive window
  562.         test    ecx, ecx
  563.         jz      @f
  564.         add     ecx, [eax + TCP_SOCKET.RCV_NXT]
  565.         cmp     ecx, [eax + TCP_SOCKET.RCV_ADV]
  566.         jbe     @f
  567.         mov     [eax + TCP_SOCKET.RCV_ADV], ecx
  568.        @@:
  569.  
  570. ; update last ack sent
  571.         push    [eax + TCP_SOCKET.RCV_NXT]
  572.         pop     [eax + TCP_SOCKET.last_ack_sent]
  573.  
  574. ; and flags
  575.         and     [eax + TCP_SOCKET.t_flags], not (TF_ACKNOW + TF_DELACK)
  576.  
  577.         test    [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
  578.         jnz     TCP_output.again
  579.  
  580. ; unlock socket
  581.         lea     ecx, [eax + SOCKET.mutex]
  582.         call    mutex_unlock
  583.  
  584.         DEBUGF 1,"TCP_send: success!\n"
  585.  
  586.         xor     eax, eax
  587.         popf
  588.         ret
  589.  
  590.  
  591.   .ip_error:
  592.         pop     ecx
  593.         add     esp, ecx
  594.         add     esp, 4
  595.         pop     eax
  596.  
  597.         mov     [eax + TCP_SOCKET.timer_retransmission], TCP_time_re_min
  598.  
  599. ; unlock socket
  600.         lea     ecx, [eax + SOCKET.mutex]
  601.         call    mutex_unlock
  602.  
  603.         DEBUGF 1,"TCP_send: IP error\n"
  604.  
  605.         or      eax, -1
  606.         popf
  607.         ret
  608.  
  609.   .send_error:
  610.         add     esp, 8
  611.  
  612.         DEBUGF 1,"TCP_send: sending failed\n"
  613.  
  614.         or      eax, -2
  615.         popf
  616.         ret
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.