Subversion Repositories Kolibri OS

Rev

Rev 2940 | 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: 2942 $
  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.         jne     .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.         test    [eax + TCP_SOCKET.t_force], -1
  82.         jz      .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.         test    ecx, ecx
  207.         jz      .no_window
  208.  
  209.         push    ecx
  210.         mov     cl, [eax + TCP_SOCKET.RCV_SCALE]
  211.         inc     cl                                      ; we want it *2
  212.         mov     ebx, TCP_max_win
  213.         shl     ebx, cl
  214.         pop     ecx
  215.         cmp     ebx, ecx
  216.         cmovb   ebx, ecx
  217.  
  218.         ; now ebx is TWICE the amount we can increase the window
  219.         ; (with TCP_max_win shl rcv_scale as the maximum)
  220.  
  221.         cmp     ebx, [eax + TCP_SOCKET.t_maxseg]
  222.         jae     TCP_send
  223.  
  224.         cmp     ebx, 8192       ;[eax + TCP_SOCKET.]    ;;; FIXME: check with receive buffer high water mark
  225.         jae     TCP_send
  226.  
  227.   .no_window:
  228.  
  229. ;--------------------------
  230. ; Should a segment be sent? (174)
  231.  
  232.         test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW   ; we need to ACK
  233.         jnz     TCP_send
  234.  
  235.         test    dl, TH_SYN + TH_RST                     ; we need to send a SYN or RST
  236.         jnz     TCP_send
  237.  
  238.         mov     ebx, [eax + TCP_SOCKET.SND_UP]          ; when urgent pointer is beyond start of send bufer
  239.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  240.         ja      TCP_send
  241.  
  242.         test    dl, TH_FIN
  243.         jz      .enter_persist  ; no reason to send, enter persist state
  244.  
  245. ; FIN was set, only send if not already sent, or on retransmit
  246.  
  247.         test    [eax + TCP_SOCKET.t_flags], TF_SENTFIN
  248.         jz      TCP_send
  249.  
  250.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]
  251.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  252.         je      TCP_send
  253.  
  254. ;--------------------
  255. ; Enter persist state (191)
  256.  
  257.   .enter_persist:
  258.  
  259.         cmp     [eax + STREAM_SOCKET.snd.size], 0       ; Data ready to send?
  260.         jne     @f
  261.         cmp     [eax + TCP_SOCKET.timer_retransmission], 0
  262.         jne     @f
  263.         cmp     [eax + TCP_SOCKET.timer_persist], 0     ; Persist timer already expired?
  264.         jne     @f
  265.  
  266.         DEBUGF  1,"TCP_output: Entering persist state\n"
  267.  
  268.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  269.         TCP_set_persist eax
  270.        @@:
  271.  
  272. ;----------------------------
  273. ; No reason to send a segment (219)
  274.  
  275.         DEBUGF  1,"TCP_output: No reason to send a segment\n"
  276.  
  277.         pusha
  278.         lea     ecx, [eax + SOCKET.mutex]
  279.         call    mutex_unlock
  280.         popa
  281.  
  282.         popf
  283.         ret
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293. ;-----------------------------------------------
  294. ;
  295. ; Send a segment (222)
  296. ;
  297. ; eax = socket pointer
  298. ; esi = data len
  299. ;  dl = flags
  300. ;
  301. ;-----------------------------------------------
  302. align 4
  303. TCP_send:
  304.  
  305.         DEBUGF  1,"TCP_send: socket=%x length=%u flags=%x\n", eax, esi, dl
  306.  
  307.         push    eax                     ; save socket ptr
  308.         push    esi                     ; and data length too
  309.         mov     edi, sizeof.TCP_header  ; edi will contain headersize
  310.  
  311. ;------------------------------------
  312. ; Send options with first SYN segment
  313.  
  314.         test    dl, TH_SYN
  315.         jz      .options_done
  316.  
  317.         push    [eax + TCP_SOCKET.ISS]
  318.         pop     [eax + TCP_SOCKET.SND_NXT]
  319.  
  320.         test    [eax + TCP_SOCKET.t_flags], TF_NOOPT
  321.         jnz     .options_done
  322.  
  323.         mov     ecx, 1460                              ;;;; FIXME
  324.         or      ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16
  325.         bswap   ecx
  326.         push    ecx
  327.         add     di, 4
  328.  
  329.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE
  330.         jz      .no_syn
  331.  
  332.         test    dl, TH_ACK
  333.         jnz     .scale_opt
  334.  
  335.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  336.         jz      .no_syn
  337.  
  338.   .scale_opt:
  339.         movzx   ecx, byte [eax + TCP_SOCKET.request_r_scale]
  340.         or      ecx, TCP_OPT_WINDOW shl 24 + 4 shl 16 + TCP_OPT_NOP shl 8
  341.         bswap   ecx
  342.         pushd   ecx
  343.         add     di, 4
  344.  
  345.   .no_syn:
  346.  
  347. ;------------------------------------
  348. ; Make the timestamp option if needed
  349.  
  350.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP
  351.         jz      .no_timestamp
  352.  
  353.         test    dl, TH_RST
  354.         jnz     .no_timestamp
  355.  
  356.         test    dl, TH_ACK
  357.         jz      .timestamp
  358.  
  359.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
  360.         jz      .no_timestamp
  361.  
  362.   .timestamp:
  363.         mov     ebx, [timer_ticks]
  364.         bswap   ebx
  365.         push    ebx
  366.         pushw   0
  367.         pushd   TCP_OPT_TIMESTAMP + 10 shl 8 + TCP_OPT_NOP shl 16 + TCP_OPT_NOP shl 24
  368.         add     di, 10
  369.  
  370.   .no_timestamp:
  371.  
  372.         ; <Add additional options here>
  373.  
  374.   .options_done:
  375.  
  376. ; eax = socket ptr
  377. ; edx = flags
  378. ; edi = header size
  379. ; esi = data len
  380.  
  381. ;---------------------------------------------
  382. ; check if we dont exceed the max segment size (270)
  383.  
  384.         add     esi, edi                        ; total TCP segment size
  385.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  386.         jbe     .no_overflow
  387.  
  388.         mov     esi, [eax + TCP_SOCKET.t_maxseg]
  389.         or      [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
  390.   .no_overflow:
  391.  
  392. ;-----------------------------------------------------------------
  393. ; Start by pushing all TCP header values in reverse order on stack
  394. ; (essentially, creating the tcp header on the stack!)
  395.  
  396.         pushw   0       ;        .UrgentPointer          dw ?
  397.         pushw   0       ;        .Checksum               dw ?
  398.         pushw   0x00a0  ;        .Window                 dw ?    ;;;;;;; FIXME (370)
  399.         shl     edi, 2  ;        .DataOffset             db ?  only 4 left-most bits
  400.         shl     dx, 8
  401.         or      dx, di  ;        .Flags                  db ?
  402.         pushw   dx
  403.         shr     edi, 2  ;        .DataOffset             db ?
  404.  
  405.         push    [eax + TCP_SOCKET.RCV_NXT]      ;        .AckNumber              dd ?
  406.         ntohd   [esp]
  407.  
  408.         push    [eax + TCP_SOCKET.SND_NXT]      ;        .SequenceNumber         dd ?
  409.         ntohd   [esp]
  410.  
  411.         push    [eax + TCP_SOCKET.RemotePort]   ;        .DestinationPort        dw ?
  412.         ntohw   [esp]
  413.  
  414.         push    [eax + TCP_SOCKET.LocalPort]    ;        .SourcePort             dw ?
  415.         ntohw   [esp]
  416.  
  417.         push    edi                     ; header size
  418.  
  419. ;---------------------
  420. ; Create the IP packet
  421.  
  422.         mov     ecx, esi
  423.  
  424.         mov     ebx, [eax + SOCKET.device]
  425.         mov     edx, [eax + IP_SOCKET.LocalIP]  ; source ip
  426.         mov     eax, [eax + IP_SOCKET.RemoteIP] ; dest ip
  427.         mov     di, IP_PROTO_TCP shl 8 + 128
  428.         call    IPv4_output
  429.         jz      .ip_error
  430.  
  431. ;-----------------------------------------
  432. ; Move TCP header from stack to TCP packet
  433.  
  434.         push    ecx
  435.         mov     ecx, [esp + 4]
  436.         lea     esi, [esp + 8]
  437.         shr     ecx, 2                  ; count is in bytes, we will work with dwords
  438.         rep     movsd
  439.         pop     ecx                     ; full TCP packet size
  440.  
  441.         pop     esi                     ; headersize
  442.         add     esp, esi                ; remove it from stack
  443.  
  444.         push    edx                     ; packet size for send proc
  445.         push    eax                     ; packet ptr for send proc
  446.  
  447.         mov     edx, edi                ; begin of data
  448.         sub     edx, esi                ; begin of packet (edi = begin of data)
  449.         push    ecx
  450.         sub     ecx, esi                ; data size
  451.  
  452. ;--------------
  453. ; Copy the data
  454.  
  455. ; eax = ptr to ring struct
  456. ; ecx = buffer size
  457. ; edi = ptr to buffer
  458.  
  459.         mov     eax, [esp + 16]                 ; get socket ptr
  460.  
  461.         push    edx
  462.         push    [eax + TCP_SOCKET.SND_NXT]      ; we'll need this for timing the transmission
  463.         test    ecx, ecx
  464.         jz      .nodata
  465.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  466.         add     [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number <<< CHECKME
  467.         sub     edx, [eax + TCP_SOCKET.SND_UNA] ; offset
  468.         add     eax, STREAM_SOCKET.snd
  469.         call    SOCKET_ring_read
  470.   .nodata:
  471.         pop     edi
  472.         pop     esi                             ; begin of data
  473.         pop     ecx                             ; full packet size
  474.         mov     eax, [esp + 12]                 ; socket ptr
  475.  
  476. ;----------------------------------
  477. ; initialize retransmit timer (400)
  478.  
  479. ;TODO: check t_force and persist
  480.  
  481.         test    [esi + TCP_header.Flags], TH_SYN + TH_FIN       ; syn and fin take a sequence number
  482.         jz      @f
  483.         inc     [eax + TCP_SOCKET.SND_NXT]
  484.         test    [esi + TCP_header.Flags], TH_FIN
  485.         jz      @f
  486.         or      [eax + TCP_SOCKET.t_flags], TF_SENTFIN          ; if we sent a fin, set the sentfin flag
  487.        @@:
  488.  
  489.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  490.         cmp     edx, [eax + TCP_SOCKET.SND_MAX]                 ; is this a retransmission?
  491.         jbe     @f
  492.         mov     [eax + TCP_SOCKET.SND_MAX], edx                 ; [eax + TCP_SOCKET.SND_NXT] from before we updated it
  493.  
  494.         cmp     [eax + TCP_SOCKET.t_rtt], 0                     ; are we currently timing anything?
  495.         je      @f
  496.         mov     [eax + TCP_SOCKET.t_rtt], 1                     ; nope, start transmission timer
  497.         mov     [eax + TCP_SOCKET.t_rtseq], edi
  498. ;TODO: update stats
  499.        @@:
  500.  
  501. ; set retransmission timer if not already set, and not doing an ACK or keepalive probe
  502.  
  503.         cmp     [eax + TCP_SOCKET.timer_retransmission], 0 ;;;; FIXME
  504.         ja      .retransmit_set
  505.  
  506.         cmp     edx, [eax + TCP_SOCKET.SND_UNA]                 ; edx is still [eax + TCP_SOCKET.SND_NXT]
  507.         je      .retransmit_set
  508.  
  509.         mov     edx, [eax + TCP_SOCKET.t_rxtcur]
  510.         mov     [eax + TCP_SOCKET.timer_retransmission], dx
  511.  
  512.         cmp     [eax + TCP_SOCKET.timer_persist], 0
  513.         jne     .retransmit_set
  514.         mov     [eax + TCP_SOCKET.timer_persist], 0
  515.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  516.  
  517.   .retransmit_set:
  518.  
  519. ;--------------------
  520. ; Create the checksum
  521.  
  522.         TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP)
  523.         mov     [esi + TCP_header.Checksum], dx
  524.  
  525. ; unlock socket
  526.         lea     ecx, [eax + SOCKET.mutex]
  527.         call    mutex_unlock
  528.  
  529. ;----------------
  530. ; Send the packet
  531.  
  532.         DEBUGF  1,"TCP_send: Sending with device %x\n", ebx
  533.         call    [ebx + NET_DEVICE.transmit]
  534.         jnz     .send_error
  535.  
  536. ;---------------
  537. ; Ok, data sent!
  538.  
  539.         pop     ecx
  540.         pop     eax
  541.  
  542.         inc     [TCP_segments_tx]       ; FIXME: correct interface?
  543.  
  544. ; unlock socket
  545.         lea     ecx, [eax + SOCKET.mutex]
  546.         call    mutex_lock
  547.  
  548. ; update advertised receive window
  549.         test    ecx, ecx
  550.         jz      @f
  551.         add     ecx, [eax + TCP_SOCKET.RCV_NXT]
  552.         cmp     ecx, [eax + TCP_SOCKET.RCV_ADV]
  553.         jbe     @f
  554.         mov     [eax + TCP_SOCKET.RCV_ADV], ecx
  555.        @@:
  556.  
  557. ; update last ack sent
  558.         push    [eax + TCP_SOCKET.RCV_NXT]
  559.         pop     [eax + TCP_SOCKET.last_ack_sent]
  560.  
  561. ; and flags
  562.         and     [eax + TCP_SOCKET.t_flags], not (TF_ACKNOW + TF_DELACK)
  563.  
  564.         test    [eax + TCP_SOCKET.temp_bits], TCP_BIT_SENDALOT
  565.         jnz     TCP_output.again
  566.  
  567. ; unlock socket
  568.         lea     ecx, [eax + SOCKET.mutex]
  569.         call    mutex_unlock
  570.  
  571.         DEBUGF 1,"TCP_send: success!\n"
  572.  
  573.         xor     eax, eax
  574.         popf
  575.         ret
  576.  
  577.  
  578.   .ip_error:
  579.         pop     ecx
  580.         add     esp, ecx
  581.         add     esp, 4
  582.         pop     eax
  583.  
  584.         mov     [eax + TCP_SOCKET.timer_retransmission], TCP_time_re_min
  585.  
  586. ; unlock socket
  587.         lea     ecx, [eax + SOCKET.mutex]
  588.         call    mutex_unlock
  589.  
  590.         DEBUGF 1,"TCP_send: IP error\n"
  591.  
  592.         or      eax, -1
  593.         popf
  594.         ret
  595.  
  596.   .send_error:
  597.         add     esp, 8
  598.  
  599.         DEBUGF 1,"TCP_send: sending failed\n"
  600.  
  601.         or      eax, -2
  602.         popf
  603.         ret
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.