Subversion Repositories Kolibri OS

Rev

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