Subversion Repositories Kolibri OS

Rev

Rev 2300 | 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: 2301 $
  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. ETH_FRAME_MINIMUM       equ 60
  27.  
  28. virtual at NET_DEVICE.end
  29.  
  30.         ETH_DEVICE:
  31.  
  32.         .set_mode       dd ?
  33.         .get_mode       dd ?
  34.  
  35.         .set_MAC        dd ?
  36.         .get_MAC        dd ?
  37.  
  38.         .mode           dd ?
  39.         .mac            dp ?
  40.  
  41. end virtual
  42.  
  43. align 4
  44. iglobal
  45.  
  46.         ETH_BROADCAST   dp  0xffffffffffff
  47. endg
  48.  
  49. ;-----------------------------------------------------------------
  50. ;
  51. ; ETH_input
  52. ;
  53. ;  This function is called by ethernet drivers,
  54. ;  It pushes the received ethernet packets onto the eth_in_queue
  55. ;
  56. ;  IN:   [esp]  = Pointer to buffer
  57. ;       [esp+4] = size of buffer
  58. ;         ebx   = pointer to eth_device
  59. ;  OUT: /
  60. ;
  61. ;-----------------------------------------------------------------
  62. align 4
  63. ETH_input:
  64.         mov     eax, [esp]
  65.         mov     ecx, [esp+4]
  66.  
  67.         DEBUGF  1,"ETH_input - size: %u\n", ecx
  68.         cmp     ecx, ETH_FRAME_MINIMUM
  69.         jb      .dump
  70.         sub     ecx, ETH_FRAME.Data
  71.  
  72.         lea     edx, [eax + ETH_FRAME.Data]
  73.         mov     ax , [eax + ETH_FRAME.Type]
  74.  
  75.         cmp     ax, ETHER_IPv4
  76.         je      IPv4_input
  77.  
  78.         cmp     ax, ETHER_ARP
  79.         je      ARP_input
  80.  
  81. ;        cmp     ax, ETHER_PPP_DISCOVERY
  82. ;        je      PPPOE_discovery
  83.  
  84.         DEBUGF  2,"Unknown ethernet packet type %x\n", ax
  85.  
  86.   .dump:
  87.         DEBUGF  2,"ETH_input - dumping\n"
  88.         call    kernel_free
  89.         add     esp, 4
  90.         ret
  91.  
  92. ;-----------------------------------------------------------------
  93. ;
  94. ; ETH_output
  95. ;
  96. ; IN: eax = pointer to source mac
  97. ;     ebx = device ptr
  98. ;     ecx = packet size
  99. ;     edx = pointer to destination mac
  100. ;      di = protocol
  101. ;
  102. ; OUT: edi = 0 on error, pointer to buffer otherwise
  103. ;      eax = buffer start
  104. ;      ebx = to device structure
  105. ;      ecx = unchanged (packet size of embedded data)
  106. ;      edx = size of complete buffer
  107. ;
  108. ;-----------------------------------------------------------------
  109. align 4
  110. ETH_output:
  111.  
  112.         DEBUGF  1,"ETH_output: size=%u device:%x\n", ecx, ebx
  113.  
  114.         cmp     ecx, [ebx + NET_DEVICE.mtu]
  115.         ja      .exit
  116.  
  117.         push    ecx                     ; << 1
  118.         push    di eax edx              ; << 2
  119.         add     ecx, ETH_FRAME.Data
  120.  
  121.         push    ecx                     ; << 3
  122.  
  123.         push    ecx                     ; << 4
  124.         call    kernel_alloc            ; >> 4
  125.         test    eax, eax
  126.         jz      .out_of_ram
  127.         mov     edi, eax
  128.  
  129.         pop     ecx                     ; >> 3
  130.  
  131.         pop     esi                     ; >> 2
  132.         movsd
  133.         movsw
  134.         pop     esi                     ; >> 2
  135.         movsd
  136.         movsw
  137.         pop     ax                      ; >> 2
  138.         stosw
  139.  
  140.         lea     eax, [edi - ETH_FRAME.Data]  ; Set eax to buffer start
  141.         mov     edx, ecx                     ; Set edx to complete buffer size
  142.  
  143.         pop     ecx                     ; >> 1
  144.  
  145.         cmp     edx, ETH_FRAME_MINIMUM
  146.         jb      .adjust_size
  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.         ret
  154.  
  155.   .out_of_ram:
  156.         DEBUGF  2,"ETH_output: Out of ram space!!\n"
  157.         add     esp, 3*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. ;;;        dec     edi
  165.         ret
  166.  
  167.  
  168.  
  169. ;-----------------------------------------------------------------
  170. ;
  171. ; ETH_API
  172. ;
  173. ; This function is called by system function 75
  174. ;
  175. ; IN:  subfunction number in bl
  176. ;      device number in bh
  177. ;      ecx, edx, .. depends on subfunction
  178. ;
  179. ; OUT:
  180. ;
  181. ;-----------------------------------------------------------------
  182. align 4
  183. ETH_API:
  184.  
  185.         cmp     bh, MAX_NET_DEVICES
  186.         ja      .error
  187.         movzx   eax, bh
  188.         shl     eax, 2
  189.  
  190.         mov     eax, dword [NET_DRV_LIST + eax]
  191.         cmp     [eax + NET_DEVICE.type], NET_TYPE_ETH
  192.         jne     .error
  193.  
  194.         test    bl, bl
  195.         jz      .packets_tx     ; 0
  196.         dec     bl
  197.         jz      .packets_rx     ; 1
  198.         dec     bl
  199.         jz      .bytes_tx       ; 2
  200.         dec     bl
  201.         jz      .bytes_rx       ; 3
  202.         dec     bl
  203.         jz      .read_mac       ; 4
  204.         dec     bl
  205.         jz      .write_mac      ; 5
  206.  
  207.   .error:
  208.         DEBUGF  2,"Device is not ethernet type\n"
  209.         or      eax, -1
  210.         ret
  211.  
  212. .packets_tx:
  213.         mov     eax, dword [eax + NET_DEVICE.packets_tx]
  214.  
  215.         ret
  216.  
  217. .packets_rx:
  218.         mov     eax, dword [eax + NET_DEVICE.packets_rx]
  219.         ret
  220.  
  221. .bytes_tx:
  222.         mov     ebx, dword [eax + NET_DEVICE.bytes_tx + 4]
  223.         mov     eax, dword [eax + NET_DEVICE.bytes_tx]
  224.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  225.         ret
  226.  
  227. .bytes_rx:
  228.         mov     ebx, dword [eax + NET_DEVICE.bytes_rx + 4]
  229.         mov     eax, dword [eax + NET_DEVICE.bytes_rx]
  230.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  231.         ret
  232.  
  233.  
  234. .read_mac:
  235.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  236.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  237.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  238.         ret
  239.  
  240. .write_mac:
  241.         push    ecx
  242.         push    dx
  243.         call    [eax + ETH_DEVICE.set_MAC]
  244.         ret
  245.  
  246.