Subversion Repositories Kolibri OS

Rev

Rev 1733 | 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: 2220 $
  18.  
  19. struct  ETH_FRAME
  20.         .DstMAC         dp  ?  ; destination MAC-address
  21.         .SrcMAC         dp  ?  ; source MAC-address
  22.         .Type           dw  ?  ; type of the upper-layer protocol
  23.         .Data:                 ; data (46-1500 bytes for a normal packet)
  24. ends
  25.  
  26. virtual at NET_DEVICE.end
  27.  
  28.         ETH_DEVICE:
  29.  
  30.         .set_mode       dd ?
  31.         .get_mode       dd ?
  32.  
  33.         .set_MAC        dd ?
  34.         .get_MAC        dd ?
  35.  
  36.         .mode           dd ?
  37.         .mac            dp ?
  38.  
  39. end virtual
  40.  
  41. align 4
  42. iglobal
  43.  
  44.         ETH_BROADCAST   dp  0xffffffffffff
  45. endg
  46.  
  47. ;-----------------------------------------------------------------
  48. ;
  49. ; ETH_input
  50. ;
  51. ;  This function is called by ethernet drivers,
  52. ;  It pushes the received ethernet packets onto the eth_in_queue
  53. ;
  54. ;  IN:   [esp]  = Pointer to buffer
  55. ;       [esp+4] = size of buffer
  56. ;         ebx   = pointer to eth_device
  57. ;  OUT: /
  58. ;
  59. ;-----------------------------------------------------------------
  60. align 4
  61. ETH_input:
  62.         mov     eax, [esp]
  63.         mov     ecx, [esp+4]
  64.  
  65.         DEBUGF  1,"ETH_input - size: %u\n", ecx
  66.         cmp     ecx, 60    ; check packet length
  67.         jl      .dump
  68.         sub     ecx, ETH_FRAME.Data
  69.  
  70.         lea     edx, [eax + ETH_FRAME.Data]
  71.         mov     ax , [eax + ETH_FRAME.Type]
  72.  
  73.         cmp     ax, ETHER_IPv4
  74.         je      IPv4_input
  75.  
  76.         cmp     ax, ETHER_ARP
  77.         je      ARP_input
  78.  
  79. ;        cmp     ax, ETHER_PPP_DISCOVERY
  80. ;        je      PPPOE_discovery
  81.  
  82.         DEBUGF  2,"Unknown ethernet packet type %x\n", ax
  83.  
  84.   .dump:
  85.         DEBUGF  2,"ETH_input - dumping\n"
  86.         call    kernel_free
  87.         add     esp, 4
  88.         ret
  89.  
  90. ;-----------------------------------------------------------------
  91. ;
  92. ; ETH_output
  93. ;
  94. ; IN: eax = pointer to source mac
  95. ;     ebx = device ptr
  96. ;     ecx = packet size
  97. ;     edx = pointer to destination mac
  98. ;      di = protocol
  99. ;
  100. ; OUT: edi = 0 on error, pointer to buffer otherwise
  101. ;      eax = buffer start
  102. ;      ebx = to device structure
  103. ;      ecx = unchanged (packet size of embedded data)
  104. ;      edx = size of complete buffer
  105. ;
  106. ;-----------------------------------------------------------------
  107. align 4
  108. ETH_output:
  109.  
  110.         DEBUGF  1,"ETH_output: size=%u device:%x\n", ecx, ebx
  111.  
  112.         cmp     ecx, [ebx + NET_DEVICE.mtu]
  113.         jg      .exit
  114.  
  115.         push    ecx                     ; << 1
  116.         push    di eax edx              ; << 2
  117.         add     ecx, ETH_FRAME.Data
  118.  
  119.         push    ecx                     ; << 3
  120.  
  121.         push    ecx                     ; << 4
  122.         call    kernel_alloc            ; >> 4
  123.         test    eax, eax
  124.         jz      .out_of_ram
  125.         mov     edi, eax
  126.  
  127.         pop     ecx                     ; >> 3
  128.  
  129.         pop     esi                     ; >> 2
  130.         movsd
  131.         movsw
  132.         pop     esi                     ; >> 2
  133.         movsd
  134.         movsw
  135.         pop     ax                      ; >> 2
  136.         stosw
  137.  
  138.         lea     eax, [edi - ETH_FRAME.Data]  ; Set eax to buffer start
  139.         mov     edx, ecx                     ; Set edx to complete buffer size
  140.  
  141.         pop     ecx                     ; >> 1
  142.  
  143.         cmp     edx, 60-1               ; minimum ethernet packet size
  144.         jle     .adjust_size
  145.         DEBUGF  1,"ETH_output: done: %x total size: %u\n", eax, edx
  146.         ret
  147.  
  148.   .adjust_size:
  149.         mov     edx, 60
  150.         test    edx, edx        ; clear zero flag
  151.         ret
  152.  
  153.   .out_of_ram:
  154.         DEBUGF  2,"ETH_output: Out of ram space!!\n"
  155.         add     esp, 3*4+2+4
  156.         sub     edi, edi
  157.         ret
  158.  
  159.   .exit:
  160.         DEBUGF  2,"ETH_output: Packet too large!\n"
  161.         sub     edi, edi
  162. ;;;        dec     edi
  163.         ret
  164.  
  165.  
  166.  
  167. ;-----------------------------------------------------------------
  168. ;
  169. ; ETH_API
  170. ;
  171. ; This function is called by system function 75
  172. ;
  173. ; IN:  subfunction number in bl
  174. ;      device number in bh
  175. ;      ecx, edx, .. depends on subfunction
  176. ;
  177. ; OUT:
  178. ;
  179. ;-----------------------------------------------------------------
  180. align 4
  181. ETH_API:
  182.  
  183.         cmp     bh, MAX_NET_DEVICES
  184.         jg      .error
  185.         movzx   eax, bh
  186.         shl     eax, 2
  187.  
  188.         mov     eax, dword [NET_DRV_LIST + eax]
  189.         cmp     [eax + NET_DEVICE.type], NET_TYPE_ETH
  190.         jne     .error
  191.  
  192.         test    bl, bl
  193.         jz      .packets_tx     ; 0
  194.         dec     bl
  195.         jz      .packets_rx     ; 1
  196.         dec     bl
  197.         jz      .bytes_tx       ; 2
  198.         dec     bl
  199.         jz      .bytes_rx       ; 3
  200.         dec     bl
  201.         jz      .read_mac       ; 4
  202.         dec     bl
  203.         jz      .write_mac      ; 5
  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, dword [eax + NET_DEVICE.packets_tx]
  212.  
  213.         ret
  214.  
  215. .packets_rx:
  216.         mov     eax, dword [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.