Subversion Repositories Kolibri OS

Rev

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