Subversion Repositories Kolibri OS

Rev

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