Subversion Repositories Kolibri OS

Rev

Rev 1256 | 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: 1257 $
  21.  
  22. uglobal
  23.         last_1sTick     db ?
  24.         last_1hsTick    dd ?
  25. endg
  26.  
  27. MAX_NET_DEVICES equ 16
  28.  
  29. MIN_EPHEMERAL_PORT equ 49152
  30. MAX_EPHEMERAL_PORT equ 61000
  31.  
  32. ETHER           equ 1337     ; TODO: find another value for this (how does it work in posix ?)
  33. ETHER_ARP       equ 0x0608
  34.  
  35. AF_UNSPEC       equ 0
  36. AF_UNIX         equ 1
  37. AF_INET4        equ 2
  38. ;AF_AX25         equ 3
  39. ;AF_IPX          equ 4
  40. ;AF_APPLETALK    equ 5
  41. ;AF_NETROM       equ 6
  42. ;AF_BRIDGE       equ 7
  43. ;AF_AAL5         equ 8
  44. ;AF_X25          equ 9
  45. ;AF_INET6        equ 10
  46. ;AF_MAX          equ 12
  47.  
  48. IP_PROTO_IP     equ 0
  49. IP_PROTO_ICMP   equ 1
  50. IP_PROTO_TCP    equ 6
  51. IP_PROTO_UDP    equ 17
  52.  
  53. ; Socket types
  54. SOCK_STREAM     = 1
  55. SOCK_DGRAM      = 2
  56. SOCK_RAW        = 3
  57.  
  58. TCB_LISTEN              equ 1
  59. TCB_SYN_SENT            equ 2
  60. TCB_SYN_RECEIVED        equ 3
  61. TCB_ESTABLISHED         equ 4
  62. TCB_FIN_WAIT_1          equ 5
  63. TCB_FIN_WAIT_2          equ 6
  64. TCB_CLOSE_WAIT          equ 7
  65. TCB_CLOSING             equ 8
  66. TCB_LAST_ACK            equ 9
  67. TCB_TIMED_WAIT          equ 10
  68. TCB_CLOSED              equ 11
  69.  
  70. TH_FIN                  equ 1 shl 0
  71. TH_SYN                  equ 1 shl 1
  72. TH_RST                  equ 1 shl 2
  73. TH_PUSH                 equ 1 shl 3
  74. TH_ACK                  equ 1 shl 4
  75. TH_URG                  equ 1 shl 5
  76.  
  77.  
  78. macro inc_INET reg {
  79.  
  80.         add     byte [reg + 3], 1
  81.         adc     byte [reg + 2], 0
  82.         adc     byte [reg + 1], 0
  83.         adc     byte [reg], 0
  84.  
  85. }
  86.  
  87.  
  88. macro add_INET reg {
  89.         add     byte [reg + 3], cl
  90.         adc     byte [reg + 2], ch
  91.         adc     byte [reg + 1], 0
  92.         adc     byte [reg], 0
  93.         rol     ecx, 16
  94.         add     byte [reg + 1], cl
  95.         adc     byte [reg], ch
  96.         rol     ecx, 16
  97. }
  98.  
  99. include "queue.inc"
  100. include "ARP.inc"
  101. include "IPv4.inc"
  102. include "ethernet.inc"
  103. include "socket.inc"
  104. include "tcp.inc"
  105. include "udp.inc"
  106. include "icmp.inc"
  107.  
  108. ;-----------------------------------------------------------------
  109. ;
  110. ; stack_init
  111. ;
  112. ;  This function calls all network init procedures
  113. ;
  114. ;  IN:  /
  115. ;  OUT: /
  116. ;
  117. ;-----------------------------------------------------------------
  118. align 4
  119. stack_init:
  120.  
  121.         call    ETH_init
  122.         call    IPv4_init
  123.         call    ARP_init
  124.         call    UDP_init
  125.         call    TCP_init
  126.         call    ICMP_init
  127.         call    socket_init
  128.  
  129.         mov     al, 0                 ; set up 1s timer
  130.         out     0x70, al
  131.         in      al, 0x71
  132.         mov     [last_1sTick], al
  133.  
  134.         ret
  135.  
  136.  
  137.  
  138. ;-----------------------------------------------------------------
  139. ;
  140. ; stack_handler
  141. ;
  142. ;  This function calls all network init procedures
  143. ;
  144. ;  IN:  /
  145. ;  OUT: /
  146. ;
  147. ;-----------------------------------------------------------------
  148. align 4
  149. stack_handler:
  150.  
  151.         cmp     [ETH_RUNNING], 0
  152.         je      .exit
  153.  
  154.         ; Test for 1/100 s (10ms) tick
  155.         mov     eax, [timer_ticks]
  156.         cmp     eax, [last_1hsTick]
  157.         je      .exit
  158.  
  159.         mov     [last_1hsTick], eax
  160.  
  161.         call    ETH_handler                 ; handle all queued ethernet packets
  162.         call    ETH_send_queued
  163.         call    TCP_send_queued
  164.  
  165.   .sec_tick:
  166.  
  167.         ; Test for 1 second tick
  168.         mov     al, 0
  169.         out     0x70, al
  170.         in      al, 0x71
  171.         cmp     al, [last_1sTick]
  172.         je      .exit
  173.  
  174.         mov     [last_1sTick], al
  175.  
  176.         call    ARP_decrease_entry_ttls
  177.         call    IPv4_decrease_fragment_ttls
  178.         call    TCP_decrease_socket_ttls
  179.  
  180.   .exit:
  181.         ret
  182.  
  183.  
  184. ;-----------------------------------------------------------------
  185. ;
  186. ; checksum_1
  187. ;
  188. ;  This is the first of two functions needed to calculate the TCP checksum.
  189. ;
  190. ;  IN:  edx = start offeset for semi-checksum
  191. ;       esi = pointer to data
  192. ;       ecx = data size
  193. ;  OUT: edx = semi-checksum
  194. ;
  195. ;-----------------------------------------------------------------
  196. align 4
  197. checksum_1:
  198.  
  199.         xor     eax, eax
  200.         shr     ecx, 1
  201.         pushf
  202. .loop:
  203.         lodsw
  204.         xchg    al, ah
  205.         add     edx, eax
  206.         loop    .loop
  207.  
  208.         popf
  209.         jnc     .end
  210.  
  211.         add     dh, [esi]
  212.         adc     edx, 0
  213.  
  214. .end:
  215.  
  216.         ret
  217.  
  218.  
  219. ;-----------------------------------------------------------------
  220. ;
  221. ; checksum_2
  222. ;
  223. ;  This function calculates the final ip/tcp/udp checksum for you
  224. ;
  225. ;  IN:  edx = semi-checksum
  226. ;  OUT: dx = checksum (in INET byte order)
  227. ;
  228. ;-----------------------------------------------------------------
  229. align 4
  230. checksum_2:
  231.  
  232.         mov     ecx, edx
  233.         shr     ecx, 16
  234.         and     edx, 0xffff
  235.         add     edx, ecx
  236.         mov     eax, edx
  237.         shr     eax, 16
  238.         add     edx, eax
  239.  
  240.         not     dx
  241.         jnz     .not_zero
  242.         dec     dx
  243.   .not_zero:
  244.         xchg    dl, dh
  245.  
  246.         DEBUGF 1,"Checksum: %x\n",dx
  247.  
  248.         ret
  249.  
  250.  
  251.  
  252. ;----------------------------------------------------------------
  253. ;
  254. ;  System function to work with network devices (73)
  255. ;
  256. ;----------------------------------------------------------------
  257. align 4
  258. sys_network:
  259.  
  260.         cmp     ebx, -1
  261.         jne     @f
  262.  
  263.         mov     eax, [ETH_RUNNING]
  264.         jmp     .return
  265.  
  266.    @@:
  267.         cmp     bh, MAX_NET_DEVICES              ; Check if device number exists
  268.         jge     .doesnt_exist
  269.  
  270.         mov     esi, ebx
  271.         and     esi, 0x0000ff00
  272.         shr     esi, 6
  273.  
  274.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  275.         je      .doesnt_exist
  276.  
  277.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  278.         jnz     @f
  279.                                          ; todo
  280.         xor     eax, eax
  281.         jmp     .return
  282.  
  283.  
  284.   @@:
  285.         dec     bl                      ; 1 = Get device name
  286.         jnz     @f
  287.  
  288.         mov     esi, [esi + ETH_DRV_LIST]
  289.         mov     esi, [esi + ETH_DEVICE.name]
  290.         mov     edi, ecx
  291.  
  292.         mov     ecx, 64 ; max length
  293.         repnz   movsb
  294.  
  295.         xor     eax, eax
  296.         jmp     .return
  297.  
  298.   @@:
  299.  
  300.         dec     bl                      ; 2 = Reset the device
  301.         jnz     @f
  302.  
  303.         mov     esi, [esi + ETH_DRV_LIST]
  304.         call    [esi + ETH_DEVICE.reset]
  305.         jmp     .return
  306.  
  307.   @@:
  308.  
  309.         dec     bl                      ; 3 = Stop driver for this device
  310.         jnz     @f
  311.  
  312.         mov     esi, [esi + ETH_DRV_LIST]
  313.         call    [esi + ETH_DEVICE.unload]
  314.         jmp     .return
  315.  
  316.   @@:
  317.         dec     bl                      ; 4 = Get driver pointer
  318.         jnz     @f
  319.  
  320.         ; ..;
  321.  
  322.  
  323.   @@:
  324. ;  ...                                   ; 5 Get driver name
  325.  
  326.   .doesnt_exist:
  327.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  328.         mov     eax, -1
  329.  
  330.   .return:
  331.         mov     [esp+28+4], eax
  332.         ret
  333.  
  334.  
  335. ;----------------------------------------------------------------
  336. ;
  337. ;  System Function To work with Protocols  (75)
  338. ;
  339. ;----------------------------------------------------------------
  340. align 4
  341. sys_protocols:
  342.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  343.         jge     .doesnt_exist
  344.  
  345.         mov     esi, ebx
  346.         and     esi, 0x0000ff00
  347.         shr     esi, 6
  348.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  349.         je      .doesnt_exist
  350.  
  351.         push    .return                         ; return address (we will be using jumps instead of calls)
  352.  
  353.         mov     eax, ebx                        ; set ax to protocol number
  354.         shr     eax, 16                         ;
  355.  
  356.         cmp     ax , IP_PROTO_IP
  357.         je      IPv4_API
  358.  
  359.         cmp     ax , IP_PROTO_ICMP
  360.         je      ICMP_API
  361.  
  362.         cmp     ax , IP_PROTO_UDP
  363.         je      UDP_API
  364.  
  365.         cmp     ax , IP_PROTO_TCP
  366.         je      TCP_API
  367.  
  368.         cmp     ax , ETHER_ARP
  369.         je      ARP_API
  370.  
  371.         cmp     ax , ETHER
  372.         je      ETH_API
  373.  
  374.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  375.  
  376.   .doesnt_exist:
  377.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  378.         mov     eax, -1
  379.  
  380.   .return:
  381.         mov     [esp+28+4], eax
  382.         ret
  383.