Subversion Repositories Kolibri OS

Rev

Rev 2614 | 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: 2731 $
  18.  
  19. ETH_FRAME_MINIMUM       = 60
  20.  
  21. struct  ETH_header
  22.  
  23.         DstMAC          dp  ?  ; destination MAC-address
  24.         SrcMAC          dp  ?  ; source MAC-address
  25.         Type            dw  ?  ; type of the upper-layer protocol
  26.  
  27. ends
  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_IPv6
  81. ;        je      IPv6_input
  82.  
  83. ;        cmp     ax, ETHER_PPP_DISCOVERY
  84. ;        je      PPPoE_discovery_input
  85.  
  86. ;        cmp     ax, ETHER_PPP_SESSION
  87. ;        je      PPPoE_session_input
  88.  
  89.         DEBUGF  2,"Unknown ethernet packet type %x\n", ax
  90.  
  91.   .dump:
  92.         DEBUGF  2,"ETH_input - dumping\n"
  93.         call    kernel_free
  94.         add     esp, 4
  95.         ret
  96.  
  97. ;-----------------------------------------------------------------
  98. ;
  99. ; ETH_output
  100. ;
  101. ; IN: eax = pointer to source mac
  102. ;     ebx = device ptr
  103. ;     ecx = packet size
  104. ;     edx = pointer to destination mac
  105. ;      di = protocol
  106. ;
  107. ; OUT: edi = 0 on error, pointer to buffer otherwise
  108. ;      eax = buffer start
  109. ;      ebx = to device structure
  110. ;      ecx = unchanged (packet size of embedded data)
  111. ;      edx = size of complete buffer
  112. ;
  113. ;-----------------------------------------------------------------
  114. align 4
  115. ETH_output:
  116.  
  117.         DEBUGF  1,"ETH_output: size=%u device:%x\n", ecx, ebx
  118.  
  119.         cmp     ecx, [ebx + NET_DEVICE.mtu]
  120.         ja      .exit
  121.  
  122.         push    ecx
  123.         push    di eax edx
  124.  
  125.         add     ecx, sizeof.ETH_header
  126.         stdcall kernel_alloc, ecx
  127.         test    eax, eax
  128.         jz      .out_of_ram
  129.         mov     edi, eax
  130.  
  131.         pop     esi
  132.         movsd
  133.         movsw
  134.         pop     esi
  135.         movsd
  136.         movsw
  137.         pop     ax
  138.         stosw
  139.  
  140.         lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
  141.         pop     ecx
  142.         lea     edx, [ecx + sizeof.ETH_header]  ; Set edx to complete buffer size
  143.  
  144.         cmp     edx, ETH_FRAME_MINIMUM
  145.         jbe     .adjust_size
  146.   .done:
  147.         DEBUGF  1,"ETH_output: done: %x total size: %u\n", eax, edx
  148.         ret
  149.  
  150.   .adjust_size:
  151.         mov     edx, ETH_FRAME_MINIMUM
  152.         test    edx, edx        ; clear zero flag
  153.         jmp     .done
  154.  
  155.   .out_of_ram:
  156.         DEBUGF  2,"ETH_output: Out of ram space!!\n"
  157.         add     esp, 4+4+2+4
  158.         sub     edi, edi
  159.         ret
  160.  
  161.   .exit:
  162.         DEBUGF  2,"ETH_output: Packet too large!\n"
  163.         sub     edi, 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.         mov     eax, dword [NET_DRV_LIST + 4*eax]
  188.         cmp     [eax + NET_DEVICE.type], NET_TYPE_ETH
  189.         jne     .error
  190.  
  191.         and     ebx, 0xff
  192.         cmp     ebx, .number
  193.         ja      .error
  194.         jmp     dword [.table + 4*ebx]
  195.  
  196.   .table:
  197.         dd      .packets_tx     ; 0
  198.         dd      .packets_rx     ; 1
  199.         dd      .bytes_tx       ; 2
  200.         dd      .bytes_rx       ; 3
  201.         dd      .read_mac       ; 4
  202.         dd      .write_mac      ; 5
  203.   .number = ($ - .table) / 4 - 1
  204.  
  205.   .error:
  206.         DEBUGF  2,"Device is not ethernet type\n"
  207.         or      eax, -1
  208.         ret
  209.  
  210.   .packets_tx:
  211.         mov     eax, [eax + NET_DEVICE.packets_tx]
  212.  
  213.         ret
  214.  
  215.   .packets_rx:
  216.         mov     eax, [eax + NET_DEVICE.packets_rx]
  217.         ret
  218.  
  219.   .bytes_tx:
  220.         mov     ebx, dword [eax + NET_DEVICE.bytes_tx + 4]
  221.         mov     eax, dword [eax + NET_DEVICE.bytes_tx]
  222.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  223.         ret
  224.  
  225.   .bytes_rx:
  226.         mov     ebx, dword [eax + NET_DEVICE.bytes_rx + 4]
  227.         mov     eax, dword [eax + NET_DEVICE.bytes_rx]
  228.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  229.         ret
  230.  
  231.  
  232.   .read_mac:
  233.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  234.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  235.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  236.         ret
  237.  
  238.   .write_mac:
  239.         push    ecx
  240.         push    dx
  241.         call    [eax + ETH_DEVICE.set_MAC]
  242.         ret
  243.  
  244.