Subversion Repositories Kolibri OS

Rev

Rev 2891 | Go to most recent revision | 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: 2893 $
  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.         DEBUGF 1,"TCP_output: socket=%x\n", eax
  32.  
  33.         pushf
  34.         cli
  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.sendalot], 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.         inc     [eax + TCP_SOCKET.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.         popf
  282.  
  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.         mov     edi, sizeof.TCP_header  ; edi will contain headersize
  309.  
  310. ;------------------------------------
  311. ; Send options with first SYN segment
  312.  
  313.         test    dl, TH_SYN
  314.         jz      .options_done
  315.  
  316.         push    [eax + TCP_SOCKET.ISS]
  317.         pop     [eax + TCP_SOCKET.SND_NXT]
  318.  
  319.         test    [eax + TCP_SOCKET.t_flags], TF_NOOPT
  320.         jnz     .options_done
  321.  
  322.         mov     ecx, 1460                              ;;;; FIXME
  323.         or      ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16
  324.         bswap   ecx
  325.         push    ecx
  326.         add     di, 4
  327.  
  328.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE
  329.         jz      .no_syn
  330.  
  331.         test    dl, TH_ACK
  332.         jnz     .scale_opt
  333.  
  334.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  335.         jz      .no_syn
  336.  
  337.   .scale_opt:
  338.         movzx   ecx, byte [eax + TCP_SOCKET.request_r_scale]
  339.         or      ecx, TCP_OPT_WINDOW shl 24 + 4 shl 16 + TCP_OPT_NOP shl 8
  340.         bswap   ecx
  341.         pushd   ecx
  342.         add     di, 4
  343.  
  344.   .no_syn:
  345.  
  346. ;------------------------------------
  347. ; Make the timestamp option if needed
  348.  
  349.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP
  350.         jz      .no_timestamp
  351.  
  352.         test    dl, TH_RST
  353.         jnz     .no_timestamp
  354.  
  355.         test    dl, TH_ACK
  356.         jz      .timestamp
  357.  
  358.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
  359.         jz      .no_timestamp
  360.  
  361.   .timestamp:
  362.         mov     ebx, [timer_ticks]
  363.         bswap   ebx
  364.         push    ebx
  365.         pushw   0
  366.         pushd   TCP_OPT_TIMESTAMP + 10 shl 8 + TCP_OPT_NOP shl 16 + TCP_OPT_NOP shl 24
  367.         add     di, 10
  368.  
  369.   .no_timestamp:
  370.  
  371.         ; <Add additional options here>
  372.  
  373.   .options_done:
  374.  
  375. ; eax = socket ptr
  376. ; edx = flags
  377. ; edi = header size
  378. ; esi = data len
  379.  
  380. ;---------------------------------------------
  381. ; check if we dont exceed the max segment size (270)
  382.  
  383.         add     esi, edi                        ; total TCP segment size
  384.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  385.         jbe     .no_overflow
  386.  
  387.         mov     esi, [eax + TCP_SOCKET.t_maxseg]
  388.         inc     [eax + TCP_SOCKET.sendalot]
  389.   .no_overflow:
  390.  
  391. ;-----------------------------------------------------------------
  392. ; Start by pushing all TCP header values in reverse order on stack
  393. ; (essentially, creating the tcp header on the stack!)
  394.  
  395.         pushw   0       ;        .UrgentPointer          dw ?
  396.         pushw   0       ;        .Checksum               dw ?
  397.         pushw   0x00a0  ;        .Window                 dw ?    ;;;;;;; FIXME (370)
  398.         shl     edi, 2  ;        .DataOffset             db ?  only 4 left-most bits
  399.         shl     dx, 8
  400.         or      dx, di  ;        .Flags                  db ?
  401.         pushw   dx
  402.         shr     edi, 2  ;        .DataOffset             db ?
  403.  
  404.         push    [eax + TCP_SOCKET.RCV_NXT]      ;        .AckNumber              dd ?
  405.         ntohd   [esp]
  406.  
  407.         push    [eax + TCP_SOCKET.SND_NXT]      ;        .SequenceNumber         dd ?
  408.         ntohd   [esp]
  409.  
  410.         push    [eax + TCP_SOCKET.RemotePort]   ;        .DestinationPort        dw ?
  411.         ntohw   [esp]
  412.  
  413.         push    [eax + TCP_SOCKET.LocalPort]    ;        .SourcePort             dw ?
  414.         ntohw   [esp]
  415.  
  416.         push    edi                     ; header size
  417.  
  418. ;---------------------
  419. ; Create the IP packet
  420.  
  421.         mov     ecx, esi
  422.  
  423.         mov     ebx, [eax + SOCKET.device]
  424.         mov     edx, [eax + IP_SOCKET.LocalIP]  ; source ip
  425.         mov     eax, [eax + IP_SOCKET.RemoteIP] ; dest ip
  426.         mov     di, IP_PROTO_TCP shl 8 + 128
  427.         call    IPv4_output
  428.         jz      .ip_error
  429.  
  430. ;-----------------------------------------
  431. ; Move TCP header from stack to TCP packet
  432.  
  433.         push    ecx
  434.         mov     ecx, [esp + 4]
  435.         lea     esi, [esp + 8]
  436.         shr     ecx, 2                  ; count is in bytes, we will work with dwords
  437.         rep     movsd
  438.         pop     ecx                     ; full TCP packet size
  439.  
  440.         pop     esi                     ; headersize
  441.         add     esp, esi                ; remove it from stack
  442.  
  443.         push    edx                     ; packet size for send proc
  444.         push    eax                     ; packet ptr for send proc
  445.  
  446.         mov     edx, edi                ; begin of data
  447.         sub     edx, esi                ; begin of packet (edi = begin of data)
  448.         push    ecx
  449.         sub     ecx, esi                ; data size
  450.  
  451. ;--------------
  452. ; Copy the data
  453.  
  454. ; eax = ptr to ring struct
  455. ; ecx = buffer size
  456. ; edi = ptr to buffer
  457.  
  458.         mov     eax, [esp + 12]                 ; get socket ptr
  459.  
  460.         push    edx
  461.         test    ecx, ecx
  462.         jz      .nodata
  463.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  464.         add     [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number
  465.         sub     edx, [eax + TCP_SOCKET.SND_UNA]
  466.         add     eax, STREAM_SOCKET.snd
  467.         call    SOCKET_ring_read
  468.   .nodata:
  469.         pop     esi                             ; begin of data
  470.         pop     ecx                             ; full packet size
  471.         mov     eax, [esp + 8]
  472.  
  473.  
  474. ;----------------------------------
  475. ; update sequence number and timers  (400)
  476.  
  477.         test    [esi + TCP_header.Flags], TH_SYN + TH_FIN
  478.         jz      @f
  479.         inc     [eax + TCP_SOCKET.SND_NXT]      ; syn and fin take a sequence number
  480.         test    [esi + TCP_header.Flags], TH_FIN
  481.         jz      @f
  482.         or      [eax + TCP_SOCKET.t_flags], TF_SENTFIN  ; if we sent a fin, set the sentfin flag
  483.        @@:
  484.  
  485.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  486.         cmp     edx, [eax + TCP_SOCKET.SND_MAX]
  487.         jbe     @f
  488.         mov     [eax + TCP_SOCKET.SND_MAX], edx
  489.  
  490.         ;;;; TODO: time transmission (420)
  491.  
  492.        @@:
  493.  
  494. ; set retransmission timer if not already set, and not doing an ACK or keepalive probe
  495.  
  496.         cmp     [eax + TCP_SOCKET.timer_retransmission], 1000 ;;;; FIXME
  497.         jb      .retransmit_set
  498.  
  499.         cmp     edx, [eax + TCP_SOCKET.SND_UNA]         ; edx = [eax + TCP_SOCKET.SND_NXT]
  500.         je      .retransmit_set
  501.  
  502.         mov     edx, [eax + TCP_SOCKET.t_rxtcur]
  503.         mov     [eax + TCP_SOCKET.timer_retransmission], dx
  504.  
  505.         cmp     [eax + TCP_SOCKET.timer_persist], 0
  506.         jne     .retransmit_set
  507.         mov     [eax + TCP_SOCKET.timer_persist], 0
  508.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  509.  
  510.   .retransmit_set:
  511.  
  512. ;--------------------
  513. ; Create the checksum
  514.  
  515.         TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP)
  516.         mov     [esi + TCP_header.Checksum], dx
  517.  
  518. ;----------------
  519. ; Send the packet
  520.  
  521.         DEBUGF  1,"TCP_send: Sending with device %x\n", ebx
  522.         call    [ebx + NET_DEVICE.transmit]
  523.         jnz     .send_error
  524.         pop     eax
  525.  
  526.         inc     [TCP_segments_tx]       ; FIXME: correct interface?
  527.  
  528. ;;; TODO: (485)
  529.  
  530.         push    [eax + TCP_SOCKET.RCV_NXT]
  531.         pop     [eax + TCP_SOCKET.last_ack_sent]
  532.  
  533.         and     [eax + TCP_SOCKET.t_flags], not (TF_ACKNOW + TF_DELACK)
  534.  
  535.         cmp     [eax + TCP_SOCKET.sendalot], 0
  536.         jne     TCP_output.again
  537.  
  538. ; unlock socket
  539.         lea     ecx, [eax + SOCKET.mutex]
  540.         call    mutex_unlock
  541.         popf
  542.         DEBUGF 1,"TCP_send: success!\n"
  543.  
  544.         xor     eax, eax
  545.         ret
  546.  
  547.  
  548.   .ip_error:
  549.         pop     ecx
  550.         add     esp, ecx
  551.         pop     eax
  552.  
  553.         mov     [eax + TCP_SOCKET.timer_retransmission], TCP_time_re_min
  554.  
  555. ; unlock socket
  556.         lea     ecx, [eax + SOCKET.mutex]
  557.         call    mutex_unlock
  558.         popf
  559.         DEBUGF 1,"TCP_send: IP error\n"
  560.  
  561.         or      eax, -1
  562.         ret
  563.  
  564.   .send_error:
  565.         pop     eax
  566. ; unlock socket
  567.         lea     ecx, [eax + SOCKET.mutex]
  568.         call    mutex_unlock
  569.         popf
  570.         DEBUGF 1,"TCP_send: sending failed\n"
  571.  
  572.         or      eax, -2
  573.         ret
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.