Subversion Repositories Kolibri OS

Rev

Rev 2607 | Go to most recent revision | 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: 2614 $
  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       = 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_input
  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
  117.         push    di eax edx
  118.  
  119.         add     ecx, sizeof.ETH_header
  120.         stdcall kernel_alloc, ecx
  121.         test    eax, eax
  122.         jz      .out_of_ram
  123.         mov     edi, eax
  124.  
  125.         pop     esi
  126.         movsd
  127.         movsw
  128.         pop     esi
  129.         movsd
  130.         movsw
  131.         pop     ax
  132.         stosw
  133.  
  134.         lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
  135.         pop     ecx
  136.         lea     edx, [ecx + sizeof.ETH_header]  ; Set edx to complete buffer size
  137.  
  138.         cmp     edx, ETH_FRAME_MINIMUM
  139.         jbe     .adjust_size
  140.   .done:
  141.         DEBUGF  1,"ETH_output: done: %x total size: %u\n", eax, edx
  142.         ret
  143.  
  144.   .adjust_size:
  145.         mov     edx, ETH_FRAME_MINIMUM
  146.         test    edx, edx        ; clear zero flag
  147.         jmp     .done
  148.  
  149.   .out_of_ram:
  150.         DEBUGF  2,"ETH_output: Out of ram space!!\n"
  151.         add     esp, 4+4+2+4
  152.         sub     edi, edi
  153.         ret
  154.  
  155.   .exit:
  156.         DEBUGF  2,"ETH_output: Packet too large!\n"
  157.         sub     edi, edi
  158.         ret
  159.  
  160.  
  161.  
  162. ;-----------------------------------------------------------------
  163. ;
  164. ; ETH_API
  165. ;
  166. ; This function is called by system function 75
  167. ;
  168. ; IN:  subfunction number in bl
  169. ;      device number in bh
  170. ;      ecx, edx, .. depends on subfunction
  171. ;
  172. ; OUT:
  173. ;
  174. ;-----------------------------------------------------------------
  175. align 4
  176. ETH_api:
  177.  
  178.         cmp     bh, MAX_NET_DEVICES
  179.         ja      .error
  180.         movzx   eax, bh
  181.         mov     eax, dword [NET_DRV_LIST + 4*eax]
  182.         cmp     [eax + NET_DEVICE.type], NET_TYPE_ETH
  183.         jne     .error
  184.  
  185.         and     ebx, 0xff
  186.         cmp     ebx, .number
  187.         ja      .error
  188.         jmp     dword [.table + 4*ebx]
  189.  
  190.   .table:
  191.         dd      .packets_tx     ; 0
  192.         dd      .packets_rx     ; 1
  193.         dd      .bytes_tx       ; 2
  194.         dd      .bytes_rx       ; 3
  195.         dd      .read_mac       ; 4
  196.         dd      .write_mac      ; 5
  197.   .number = ($ - .table) / 4 - 1
  198.  
  199.   .error:
  200.         DEBUGF  2,"Device is not ethernet type\n"
  201.         or      eax, -1
  202.         ret
  203.  
  204.   .packets_tx:
  205.         mov     eax, [eax + NET_DEVICE.packets_tx]
  206.  
  207.         ret
  208.  
  209.   .packets_rx:
  210.         mov     eax, [eax + NET_DEVICE.packets_rx]
  211.         ret
  212.  
  213.   .bytes_tx:
  214.         mov     ebx, dword [eax + NET_DEVICE.bytes_tx + 4]
  215.         mov     eax, dword [eax + NET_DEVICE.bytes_tx]
  216.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  217.         ret
  218.  
  219.   .bytes_rx:
  220.         mov     ebx, dword [eax + NET_DEVICE.bytes_rx + 4]
  221.         mov     eax, dword [eax + NET_DEVICE.bytes_rx]
  222.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  223.         ret
  224.  
  225.  
  226.   .read_mac:
  227.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  228.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  229.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  230.         ret
  231.  
  232.   .write_mac:
  233.         push    ecx
  234.         push    dx
  235.         call    [eax + ETH_DEVICE.set_MAC]
  236.         ret
  237.  
  238.