Subversion Repositories Kolibri OS

Rev

Rev 2301 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  ETHERNET.INC                                                   ;;
  7. ;;                                                                 ;;
  8. ;;  Ethernet network layer for KolibriOS                           ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 2305 $
  18.  
  19. struct  ETH_header
  20.  
  21.         DstMAC          dp  ?  ; destination MAC-address
  22.         SrcMAC          dp  ?  ; source MAC-address
  23.         Type            dw  ?  ; type of the upper-layer protocol
  24.  
  25. ends
  26.  
  27. ETH_FRAME_MINIMUM       equ 60
  28.  
  29. struct  ETH_DEVICE      NET_DEVICE
  30.  
  31.         set_mode        dd ?
  32.         get_mode        dd ?
  33.  
  34.         set_MAC         dd ?
  35.         get_MAC         dd ?
  36.  
  37.         mode            dd ?
  38.         mac             dp ?
  39.  
  40. ends
  41.  
  42. align 4
  43. iglobal
  44.  
  45.         ETH_BROADCAST   dp  0xffffffffffff
  46. endg
  47.  
  48. ;-----------------------------------------------------------------
  49. ;
  50. ; ETH_input
  51. ;
  52. ;  This function is called by ethernet drivers,
  53. ;  It pushes the received ethernet packets onto the eth_in_queue
  54. ;
  55. ;  IN:   [esp]  = Pointer to buffer
  56. ;       [esp+4] = size of buffer
  57. ;         ebx   = pointer to eth_device
  58. ;  OUT: /
  59. ;
  60. ;-----------------------------------------------------------------
  61. align 4
  62. ETH_input:
  63.         mov     eax, [esp]
  64.         mov     ecx, [esp+4]
  65.  
  66.         DEBUGF  1,"ETH_input - size: %u\n", ecx
  67.         cmp     ecx, ETH_FRAME_MINIMUM
  68.         jb      .dump
  69.         sub     ecx, sizeof.ETH_header
  70.  
  71.         lea     edx, [eax + sizeof.ETH_header]
  72.         mov     ax , [eax + ETH_header.Type]
  73.  
  74.         cmp     ax, ETHER_IPv4
  75.         je      IPv4_input
  76.  
  77.         cmp     ax, ETHER_ARP
  78.         je      ARP_input
  79.  
  80. ;        cmp     ax, ETHER_PPP_DISCOVERY
  81. ;        je      PPPOE_discovery
  82.  
  83.         DEBUGF  2,"Unknown ethernet packet type %x\n", ax
  84.  
  85.   .dump:
  86.         DEBUGF  2,"ETH_input - dumping\n"
  87.         call    kernel_free
  88.         add     esp, 4
  89.         ret
  90.  
  91. ;-----------------------------------------------------------------
  92. ;
  93. ; ETH_output
  94. ;
  95. ; IN: eax = pointer to source mac
  96. ;     ebx = device ptr
  97. ;     ecx = packet size
  98. ;     edx = pointer to destination mac
  99. ;      di = protocol
  100. ;
  101. ; OUT: edi = 0 on error, pointer to buffer otherwise
  102. ;      eax = buffer start
  103. ;      ebx = to device structure
  104. ;      ecx = unchanged (packet size of embedded data)
  105. ;      edx = size of complete buffer
  106. ;
  107. ;-----------------------------------------------------------------
  108. align 4
  109. ETH_output:
  110.  
  111.         DEBUGF  1,"ETH_output: size=%u device:%x\n", ecx, ebx
  112.  
  113.         cmp     ecx, [ebx + NET_DEVICE.mtu]
  114.         ja      .exit
  115.  
  116.         push    ecx                     ; << 1
  117.         push    di eax edx              ; << 2
  118.         add     ecx, sizeof.ETH_header
  119.  
  120.         push    ecx                     ; << 3
  121.  
  122.         push    ecx                     ; << 4
  123.         call    kernel_alloc            ; >> 4
  124.         test    eax, eax
  125.         jz      .out_of_ram
  126.         mov     edi, eax
  127.  
  128.         pop     ecx                     ; >> 3
  129.  
  130.         pop     esi                     ; >> 2
  131.         movsd
  132.         movsw
  133.         pop     esi                     ; >> 2
  134.         movsd
  135.         movsw
  136.         pop     ax                      ; >> 2
  137.         stosw
  138.  
  139.         lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
  140.         mov     edx, ecx                        ; Set edx to complete buffer size
  141.  
  142.         pop     ecx                     ; >> 1
  143.  
  144.         cmp     edx, ETH_FRAME_MINIMUM
  145.         jb      .adjust_size
  146.         DEBUGF  1,"ETH_output: done: %x total size: %u\n", eax, edx
  147.         ret
  148.  
  149.   .adjust_size:
  150.         mov     edx, ETH_FRAME_MINIMUM
  151.         test    edx, edx        ; clear zero flag
  152.         ret
  153.  
  154.   .out_of_ram:
  155.         DEBUGF  2,"ETH_output: Out of ram space!!\n"
  156.         add     esp, 3*4+2+4
  157.         sub     edi, edi
  158.         ret
  159.  
  160.   .exit:
  161.         DEBUGF  2,"ETH_output: Packet too large!\n"
  162.         sub     edi, edi
  163. ;;;        dec     edi
  164.         ret
  165.  
  166.  
  167.  
  168. ;-----------------------------------------------------------------
  169. ;
  170. ; ETH_API
  171. ;
  172. ; This function is called by system function 75
  173. ;
  174. ; IN:  subfunction number in bl
  175. ;      device number in bh
  176. ;      ecx, edx, .. depends on subfunction
  177. ;
  178. ; OUT:
  179. ;
  180. ;-----------------------------------------------------------------
  181. align 4
  182. ETH_API:
  183.  
  184.         cmp     bh, MAX_NET_DEVICES
  185.         ja      .error
  186.         movzx   eax, bh
  187.         shl     eax, 2
  188.  
  189.         mov     eax, dword [NET_DRV_LIST + eax]
  190.         cmp     [eax + NET_DEVICE.type], NET_TYPE_ETH
  191.         jne     .error
  192.  
  193.         test    bl, bl
  194.         jz      .packets_tx     ; 0
  195.         dec     bl
  196.         jz      .packets_rx     ; 1
  197.         dec     bl
  198.         jz      .bytes_tx       ; 2
  199.         dec     bl
  200.         jz      .bytes_rx       ; 3
  201.         dec     bl
  202.         jz      .read_mac       ; 4
  203.         dec     bl
  204.         jz      .write_mac      ; 5
  205.  
  206.   .error:
  207.         DEBUGF  2,"Device is not ethernet type\n"
  208.         or      eax, -1
  209.         ret
  210.  
  211. .packets_tx:
  212.         mov     eax, [eax + NET_DEVICE.packets_tx]
  213.  
  214.         ret
  215.  
  216. .packets_rx:
  217.         mov     eax, [eax + NET_DEVICE.packets_rx]
  218.         ret
  219.  
  220. .bytes_tx:
  221.         mov     ebx, dword [eax + NET_DEVICE.bytes_tx + 4]
  222.         mov     eax, dword [eax + NET_DEVICE.bytes_tx]
  223.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  224.         ret
  225.  
  226. .bytes_rx:
  227.         mov     ebx, dword [eax + NET_DEVICE.bytes_rx + 4]
  228.         mov     eax, dword [eax + NET_DEVICE.bytes_rx]
  229.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  230.         ret
  231.  
  232.  
  233. .read_mac:
  234.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  235.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  236.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  237.         ret
  238.  
  239. .write_mac:
  240.         push    ecx
  241.         push    dx
  242.         call    [eax + ETH_DEVICE.set_MAC]
  243.         ret
  244.  
  245.