Subversion Repositories Kolibri OS

Rev

Rev 1251 | 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: 1254 $
  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
  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.         inc     byte [reg + 3]
  81.         adc     byte [reg + 2], 0
  82.         adc     byte [reg + 1], 0
  83.         adc     byte [reg + 0], 0
  84.  
  85. }
  86.  
  87.  
  88. macro add_INET reg {
  89.  
  90.         rol     ecx, 16
  91.         adc     byte [reg + 3], ch
  92.         adc     byte [reg + 2], cl
  93.         rol     ecx, 16
  94.         adc     byte [reg + 1], ch
  95.         adc     byte [reg + 0], cl
  96.  
  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.  
  119. align 4
  120. stack_init:
  121.  
  122.         call    ETH_init
  123.         call    IPv4_init
  124.         call    ARP_init
  125.         call    UDP_init
  126.         call    TCP_init
  127.         call    ICMP_init
  128.         call    socket_init
  129.  
  130.         mov     al, 0                 ; set up 1s timer
  131.         out     0x70, al
  132.         in      al, 0x71
  133.         mov     [last_1sTick], al
  134.  
  135.         ret
  136.  
  137.  
  138.  
  139. ;-----------------------------------------------
  140. ;
  141. ; stack_handler
  142. ;
  143. ;  This function calls all network init procedures
  144. ;
  145. ;  IN:  /
  146. ;  OUT: /
  147. ;
  148. ;-----------------------------------------------
  149.  
  150. align 4
  151. stack_handler:
  152.  
  153.     cmp     [ETH_RUNNING], 0
  154.     je      .exit
  155.  
  156.     ; Test for 1/100 s (10ms) tick
  157.     mov     eax, [timer_ticks]
  158.     cmp     eax, [last_1hsTick]
  159.     je      .exit
  160.  
  161.     mov     [last_1hsTick], eax
  162.  
  163.     call    ETH_handler                 ; handle all queued ethernet packets
  164.     call    ETH_send_queued
  165.     call    TCP_send_queued
  166.  
  167.   .sec_tick:
  168.  
  169.     ; Test for 1 second tick
  170.     mov     al, 0
  171.     out     0x70, al
  172.     in      al, 0x71
  173.     cmp     al, [last_1sTick]
  174.     je      .exit
  175.  
  176.     mov     [last_1sTick], al
  177.  
  178.     call    ARP_decrease_entry_ttls
  179.     call    IPv4_decrease_fragment_ttls
  180.     call    TCP_decrease_socket_ttls
  181.  
  182.   .exit:
  183.     ret
  184.  
  185.  
  186.  
  187.  
  188.  
  189. ;-----------------------------------------------------------------
  190. ;
  191. ; checksum_1
  192. ;
  193. ;  This is the first of two functions needed to calculate the TCP checksum.
  194. ;
  195. ;  IN:  edx = start offeset for semi-checksum
  196. ;       esi = pointer to data
  197. ;       ecx = data size
  198. ;  OUT: edx = semi-checksum
  199. ;
  200. ;-----------------------------------------------------------------
  201.  
  202. align 4
  203. checksum_1:
  204.  
  205.         xor     eax, eax
  206.         shr     ecx, 1
  207.         pushf
  208. .loop:
  209.         lodsw
  210.         xchg    al, ah
  211.         add     edx, eax
  212.         loop    .loop
  213.  
  214.         popf
  215.         jnc     .end
  216.  
  217.         add     dh, [esi]
  218.         adc     edx, 0
  219.  
  220. .end:
  221.  
  222.         ret
  223.  
  224.  
  225.  
  226. ;-----------------------------------------------------------------
  227. ;
  228. ; checksum_2
  229. ;
  230. ;  This function calculates the final ip/tcp/udp checksum for you
  231. ;
  232. ;  IN:  edx = semi-checksum
  233. ;  OUT: dx = checksum (in INET byte order)
  234. ;
  235. ;-----------------------------------------------------------------
  236.  
  237. align 4
  238. checksum_2:
  239.  
  240.         mov     ecx, edx
  241.         shr     ecx, 16
  242.         and     edx, 0xffff
  243.         add     edx, ecx
  244.         mov     eax, edx
  245.         shr     eax, 16
  246.         add     edx, eax
  247.  
  248.         not     dx
  249.         jnz     .not_zero
  250.         dec     dx
  251.   .not_zero:
  252.         xchg    dl, dh
  253.  
  254.         DEBUGF 1,"Checksum: %x\n",dx
  255.  
  256.         ret
  257.  
  258.  
  259.  
  260. ;----------------------------------------------------------------
  261. ;
  262. ;  System function to work with network devices (73)
  263. ;
  264. ;----------------------------------------------------------------
  265.  
  266. align 4
  267. sys_network:
  268.  
  269.         cmp     ebx, -1
  270.         jne     @f
  271.  
  272.         mov     eax, [ETH_RUNNING]
  273.         jmp     .return
  274.  
  275.    @@:
  276.         cmp     bh, MAX_NET_DEVICES              ; Check if device number exists
  277.         jge     .doesnt_exist
  278.  
  279.         mov     esi, ebx
  280.         and     esi, 0x0000ff00
  281.         shr     esi, 6
  282.  
  283.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  284.         je      .doesnt_exist
  285.  
  286.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  287.         jnz     @f
  288.                                          ; todo
  289.         xor     eax, eax
  290.         jmp     .return
  291.  
  292.  
  293.   @@:
  294.         dec     bl                      ; 1 = Get device name
  295.         jnz     @f
  296.  
  297.         mov     esi, [esi + ETH_DRV_LIST]
  298.         mov     esi, [esi + ETH_DEVICE.name]
  299.         mov     edi, ecx
  300.  
  301.         mov     ecx, 64 ; max length
  302.         repnz   movsb
  303.  
  304.         xor     eax, eax
  305.         jmp     .return
  306.  
  307.   @@:
  308.  
  309.         dec     bl                      ; 2 = Reset the device
  310.         jnz     @f
  311.  
  312.         mov     esi, [esi + ETH_DRV_LIST]
  313.         call    [esi + ETH_DEVICE.reset]
  314.         jmp     .return
  315.  
  316.   @@:
  317.  
  318.         dec     bl                      ; 3 = Stop driver for this device
  319.         jnz     @f
  320.  
  321.         mov     esi, [esi + ETH_DRV_LIST]
  322.         call    [esi + ETH_DEVICE.unload]
  323.         jmp     .return
  324.  
  325.   @@:
  326.         dec     bl                      ; 4 = Get driver pointer
  327.         jnz     @f
  328.  
  329.         ; ..;
  330.  
  331.  
  332.   @@:
  333. ;  ...                                   ; 5 Get driver name
  334.  
  335.   .doesnt_exist:
  336.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  337.         mov     eax, -1
  338.  
  339.   .return:
  340.         mov     [esp+28+4], eax
  341.         ret
  342.  
  343.  
  344. ;----------------------------------------------------------------
  345. ;
  346. ;  System Function To work with Protocols  (75)
  347. ;
  348. ;----------------------------------------------------------------
  349.  
  350. align 4
  351. sys_protocols:
  352.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  353.         jge     .doesnt_exist
  354.  
  355.         mov     esi, ebx
  356.         and     esi, 0x0000ff00
  357.         shr     esi, 6
  358.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  359.         je      .doesnt_exist
  360.  
  361.         push    .return                         ; return address (we will be using jumps instead of calls)
  362.  
  363.         mov     eax, ebx                        ; set ax to protocol number
  364.         shr     eax, 16                         ;
  365.  
  366.         cmp     ax , IP_PROTO_IP
  367.         je      IPv4_API
  368.  
  369.         cmp     ax , IP_PROTO_ICMP
  370.         je      ICMP_API
  371.  
  372.         cmp     ax , IP_PROTO_UDP
  373.         je      UDP_API
  374.  
  375.         cmp     ax , IP_PROTO_TCP
  376.         je      TCP_API
  377.  
  378.         cmp     ax , ETHER_ARP
  379.         je      ARP_API
  380.  
  381.         cmp     ax , ETHER
  382.         je      ETH_API
  383.  
  384.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  385.  
  386.   .doesnt_exist:
  387.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  388.         mov     eax, -1
  389.  
  390.   .return:
  391.         mov     [esp+28+4], eax
  392.         ret