Subversion Repositories Kolibri OS

Rev

Rev 1482 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  STACK.INC                                                      ;;
  7. ;;                                                                 ;;
  8. ;;  BASIC TCP/IP stack for KolibriOS                               ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;     based on the work of Mike Hibbett, mikeh@oceanfree.net      ;;
  13. ;;     but also Paolo Franchetti                                   ;;
  14. ;;                                                                 ;;
  15. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  16. ;;             Version 2, June 1991                                ;;
  17. ;;                                                                 ;;
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19.  
  20. $Revision: 1483 $
  21.  
  22. __DEBUG_LEVEL_OLD__  equ __DEBUG_LEVEL__
  23. __DEBUG_LEVEL__ equ 1                   ; this sets the debug level for network part of kernel
  24.  
  25. uglobal
  26.         last_1sTick     db ?
  27.         last_1hsTick    dd ?
  28. endg
  29.  
  30. MAX_NET_DEVICES         equ 16
  31. QUEUE_BEFORE_SENDING    equ 0           ; 1 or 0 (enable or disable) currently only affects ethernet
  32.  
  33. MIN_EPHEMERAL_PORT equ 49152
  34. MAX_EPHEMERAL_PORT equ 61000
  35.  
  36. ETHER           equ 1337                ; TODO: find another value for this (how does it work in posix ?)
  37. ETHER_ARP       equ 0x0608
  38.  
  39. AF_UNSPEC       equ 0
  40. AF_UNIX         equ 1
  41. AF_INET4        equ 2
  42. ;AF_AX25         equ 3
  43. ;AF_IPX          equ 4
  44. ;AF_APPLETALK    equ 5
  45. ;AF_NETROM       equ 6
  46. ;AF_BRIDGE       equ 7
  47. ;AF_AAL5         equ 8
  48. ;AF_X25          equ 9
  49. ;AF_INET6        equ 10
  50. ;AF_MAX          equ 12
  51.  
  52. IP_PROTO_IP     equ 0
  53. IP_PROTO_ICMP   equ 1
  54. IP_PROTO_TCP    equ 6
  55. IP_PROTO_UDP    equ 17
  56.  
  57. ; Socket types
  58. SOCK_STREAM     = 1
  59. SOCK_DGRAM      = 2
  60. SOCK_RAW        = 3
  61.  
  62. TCB_LISTEN              equ 1
  63. TCB_SYN_SENT            equ 2
  64. TCB_SYN_RECEIVED        equ 3
  65. TCB_ESTABLISHED         equ 4
  66. TCB_FIN_WAIT_1          equ 5
  67. TCB_FIN_WAIT_2          equ 6
  68. TCB_CLOSE_WAIT          equ 7
  69. TCB_CLOSING             equ 8
  70. TCB_LAST_ACK            equ 9
  71. TCB_TIMED_WAIT          equ 10
  72. TCB_CLOSED              equ 11
  73.  
  74. TH_FIN                  equ 1 shl 0
  75. TH_SYN                  equ 1 shl 1
  76. TH_RST                  equ 1 shl 2
  77. TH_PUSH                 equ 1 shl 3
  78. TH_ACK                  equ 1 shl 4
  79. TH_URG                  equ 1 shl 5
  80.  
  81.  
  82. macro inc_INET reg {
  83.  
  84.         add     byte [reg + 3], 1
  85.         adc     byte [reg + 2], 0
  86.         adc     byte [reg + 1], 0
  87.         adc     byte [reg], 0
  88.  
  89. }
  90.  
  91.  
  92. macro add_INET reg {
  93.         add     byte [reg + 3], cl
  94.         adc     byte [reg + 2], ch
  95.         adc     byte [reg + 1], 0
  96.         adc     byte [reg], 0
  97.         rol     ecx, 16
  98.         add     byte [reg + 1], cl
  99.         adc     byte [reg], ch
  100.         rol     ecx, 16
  101. }
  102.  
  103.  
  104. macro pseudo_random reg {
  105.  
  106.         add     reg, [esp]
  107.         rol     reg, 5
  108.         xor     reg, [timer_ticks]
  109.         imul    reg, 214013
  110.         xor     reg, 0xdeadbeef
  111.         rol     reg, 9
  112.  
  113.         pushd   reg
  114.         mov     word [esp], 0x8080 ; kernel heap start addr      (os_stack)
  115.         xor     reg, [esp]
  116.         add     esp, 4
  117.  
  118. }
  119.  
  120. include "queue.inc"
  121. include "ARP.inc"
  122. include "IPv4.inc"
  123. include "ethernet.inc"
  124. include "socket.inc"
  125. include "tcp.inc"
  126. include "udp.inc"
  127. include "icmp.inc"
  128.  
  129. ;-----------------------------------------------------------------
  130. ;
  131. ; stack_init
  132. ;
  133. ;  This function calls all network init procedures
  134. ;
  135. ;  IN:  /
  136. ;  OUT: /
  137. ;
  138. ;-----------------------------------------------------------------
  139. align 4
  140. stack_init:
  141.  
  142.         call    ETH_init
  143.         call    IPv4_init
  144.         call    ARP_init
  145.         call    UDP_init
  146.         call    TCP_init
  147.         call    ICMP_init
  148.         call    socket_init
  149.  
  150.         mov     al, 0                 ; set up 1s timer
  151.         out     0x70, al
  152.         in      al, 0x71
  153.         mov     [last_1sTick], al
  154.  
  155.         ret
  156.  
  157.  
  158.  
  159. ;-----------------------------------------------------------------
  160. ;
  161. ; stack_handler
  162. ;
  163. ;  This function calls all network init procedures
  164. ;
  165. ;  IN:  /
  166. ;  OUT: /
  167. ;
  168. ;-----------------------------------------------------------------
  169. align 4
  170. stack_handler:
  171.  
  172.         cmp     [ETH_RUNNING], 0
  173.         je      .exit
  174.  
  175.         ; Test for 10ms tick
  176.         mov     eax, [timer_ticks]
  177.         cmp     eax, [last_1hsTick]
  178.         je      .exit
  179.  
  180.         mov     [last_1hsTick], eax
  181.  
  182.         call    ETH_handler                 ; handle all queued ethernet packets
  183. if QUEUE_BEFORE_SENDING
  184.         call    ETH_send_queued
  185. end if
  186.         call    TCP_send_queued
  187.  
  188.   .sec_tick:
  189.  
  190.         ; Test for 1 second tick
  191.         mov     al, 0
  192.         out     0x70, al
  193.         in      al, 0x71
  194.         cmp     al, [last_1sTick]
  195.         je      .exit
  196.  
  197.         mov     [last_1sTick], al
  198.  
  199.         call    ARP_decrease_entry_ttls
  200.         call    IPv4_decrease_fragment_ttls
  201.         call    TCP_decrease_socket_ttls
  202.  
  203.   .exit:
  204.         ret
  205.  
  206.  
  207. ;-----------------------------------------------------------------
  208. ;
  209. ; checksum_1
  210. ;
  211. ;  This is the first of two functions needed to calculate a checksum.
  212. ;
  213. ;  IN:  edx = start offset for semi-checksum
  214. ;       esi = pointer to data
  215. ;       ecx = data size
  216. ;  OUT: edx = semi-checksum
  217. ;
  218. ;
  219. ; Code was optimized by diamond
  220. ;
  221. ;-----------------------------------------------------------------
  222. align 4
  223. checksum_1:
  224.  
  225.         shr     ecx, 1
  226.         pushf
  227.         jz      .no_2
  228.  
  229.         shr     ecx, 1
  230.         pushf
  231.         jz      .no_4
  232.  
  233.         shr     ecx, 1
  234.         pushf
  235.         jz      .no_8
  236.  
  237.   .loop:
  238.         add     dl, [esi+1]
  239.         adc     dh, [esi+0]
  240.  
  241.         adc     dl, [esi+3]
  242.         adc     dh, [esi+2]
  243.  
  244.         adc     dl, [esi+5]
  245.         adc     dh, [esi+4]
  246.  
  247.         adc     dl, [esi+7]
  248.         adc     dh, [esi+6]
  249.  
  250.         adc     edx, 0
  251.         add     esi, 8
  252.  
  253.         dec     ecx
  254.         jnz     .loop
  255.  
  256.         adc     edx, 0
  257.  
  258.   .no_8:
  259.         popf
  260.         jnc     .no_4
  261.  
  262.         add     dl, [esi+1]
  263.         adc     dh, [esi+0]
  264.  
  265.         adc     dl, [esi+3]
  266.         adc     dh, [esi+2]
  267.  
  268.         adc     edx, 0
  269.         add     esi, 4
  270.  
  271.   .no_4:
  272.         popf
  273.         jnc     .no_2
  274.  
  275.         add     dl, [esi+1]
  276.         adc     dh, [esi+0]
  277.  
  278.         adc     edx, 0
  279.         inc     esi
  280.         inc     esi
  281.  
  282.   .no_2:
  283.         popf
  284.         jnc     .end
  285.  
  286.         add     dh, [esi+0]
  287.         adc     edx, 0
  288.   .end:
  289.         ret
  290.  
  291. ;-----------------------------------------------------------------
  292. ;
  293. ; checksum_2
  294. ;
  295. ;  This function calculates the final ip/tcp/udp checksum for you
  296. ;
  297. ;  IN:  edx = semi-checksum
  298. ;  OUT: dx = checksum (in INET byte order)
  299. ;
  300. ;-----------------------------------------------------------------
  301. align 4
  302. checksum_2:
  303.  
  304.         mov     ecx, edx
  305.         shr     ecx, 16
  306.         and     edx, 0xffff
  307.         add     edx, ecx
  308.  
  309.         mov     ecx, edx
  310.         shr     ecx, 16
  311.         add     edx, ecx
  312.  
  313.         not     dx
  314.         jnz     .not_zero
  315.         dec     dx
  316.   .not_zero:
  317.         xchg    dl, dh
  318.  
  319.         DEBUGF 1,"Checksum: %x\n",dx
  320.  
  321.         ret
  322.  
  323.  
  324.  
  325. ;----------------------------------------------------------------
  326. ;
  327. ;  System function to work with network devices (73)
  328. ;
  329. ;----------------------------------------------------------------
  330. align 4
  331. sys_network:
  332.  
  333.         cmp     ebx, -1
  334.         jne     @f
  335.  
  336.         mov     eax, [ETH_RUNNING]
  337.         jmp     .return
  338.  
  339.    @@:
  340.         cmp     bh, MAX_NET_DEVICES              ; Check if device number exists
  341.         jge     .doesnt_exist
  342.  
  343.         mov     esi, ebx
  344.         and     esi, 0x0000ff00
  345.         shr     esi, 6
  346.  
  347.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  348.         je      .doesnt_exist
  349.  
  350.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  351.         jnz     @f
  352.                                          ; todo
  353.         xor     eax, eax
  354.         jmp     .return
  355.  
  356.  
  357.   @@:
  358.         dec     bl                      ; 1 = Get device name
  359.         jnz     @f
  360.  
  361.         mov     esi, [esi + ETH_DRV_LIST]
  362.         mov     esi, [esi + ETH_DEVICE.name]
  363.         mov     edi, ecx
  364.  
  365.         mov     ecx, 64 ; max length
  366.         repnz   movsb
  367.  
  368.         xor     eax, eax
  369.         jmp     .return
  370.  
  371.   @@:
  372.  
  373.         dec     bl                      ; 2 = Reset the device
  374.         jnz     @f
  375.  
  376.         mov     esi, [esi + ETH_DRV_LIST]
  377.         call    [esi + ETH_DEVICE.reset]
  378.         jmp     .return
  379.  
  380.   @@:
  381.  
  382.         dec     bl                      ; 3 = Stop driver for this device
  383.         jnz     @f
  384.  
  385.         mov     esi, [esi + ETH_DRV_LIST]
  386.         call    [esi + ETH_DEVICE.unload]
  387.         jmp     .return
  388.  
  389.   @@:
  390.         dec     bl                      ; 4 = Get driver pointer
  391.         jnz     @f
  392.  
  393.         ; ..;
  394.  
  395.  
  396.   @@:
  397. ;  ...                                   ; 5 Get driver name
  398.  
  399.   .doesnt_exist:
  400.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  401.         mov     eax, -1
  402.  
  403.   .return:
  404.         mov     [esp+28+4], eax
  405.         ret
  406.  
  407.  
  408. ;----------------------------------------------------------------
  409. ;
  410. ;  System Function To work with Protocols  (75)
  411. ;
  412. ;----------------------------------------------------------------
  413. align 4
  414. sys_protocols:
  415.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  416.         jge     .doesnt_exist
  417.  
  418.         mov     esi, ebx
  419.         and     esi, 0x0000ff00
  420.         shr     esi, 6
  421.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  422.         je      .doesnt_exist
  423.  
  424.         push    .return                         ; return address (we will be using jumps instead of calls)
  425.  
  426.         mov     eax, ebx                        ; set ax to protocol number
  427.         shr     eax, 16                         ;
  428.  
  429.         cmp     ax , IP_PROTO_IP
  430.         je      IPv4_API
  431.  
  432.         cmp     ax , IP_PROTO_ICMP
  433.         je      ICMP_API
  434.  
  435.         cmp     ax , IP_PROTO_UDP
  436.         je      UDP_API
  437.  
  438.         cmp     ax , IP_PROTO_TCP
  439.         je      TCP_API
  440.  
  441.         cmp     ax , ETHER_ARP
  442.         je      ARP_API
  443.  
  444.         cmp     ax , ETHER
  445.         je      ETH_API
  446.  
  447.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  448.  
  449.   .doesnt_exist:
  450.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  451.         mov     eax, -1
  452.  
  453.   .return:
  454.         mov     [esp+28+4], eax
  455.         ret
  456.  
  457.  
  458. __DEBUG_LEVEL__ equ __DEBUG_LEVEL_OLD__