Subversion Repositories Kolibri OS

Rev

Rev 4511 | Rev 5001 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. 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: 4850 $
  18.  
  19. ETH_FRAME_MINIMUM       = 60
  20. ETH_QUEUE_SIZE          = 255
  21.  
  22. struct  ETH_header
  23.  
  24.         DstMAC          dp  ?  ; destination MAC-address
  25.         SrcMAC          dp  ?  ; source MAC-address
  26.         Type            dw  ?  ; type of the upper-layer protocol
  27.  
  28. ends
  29.  
  30. struct  ETH_DEVICE      NET_DEVICE
  31.  
  32.         mac             dp ?
  33.  
  34. ends
  35.  
  36. struct  ETH_queue_entry
  37.  
  38.         device          dd ?
  39.         packet          dd ?
  40.         size            dd ?
  41.  
  42. ends
  43.  
  44. iglobal
  45. align 4
  46.  
  47.         ETH_BROADCAST   dp  0xffffffffffff
  48. endg
  49.  
  50. uglobal
  51. align 4
  52.         ETH_input_event dd ?
  53.         ETH_queue       rd (ETH_QUEUE_SIZE*sizeof.ETH_queue_entry + sizeof.queue)/4
  54. endg
  55.  
  56. macro   ETH_init {
  57.  
  58.         init_queue ETH_queue
  59.  
  60.         movi    ebx, 1
  61.         mov     ecx, ETH_process_input
  62.         call    new_sys_threads
  63.         test    eax, eax
  64.         jns     @f
  65.         DEBUGF  DEBUG_NETWORK_ERROR,'K : cannot create kernel thread for ethernet, error %d\n', eax
  66.   @@:
  67.  
  68. }
  69.  
  70. ;-----------------------------------------------------------------
  71. ;
  72. ; ETH_input
  73. ;
  74. ;  This function is called by ethernet drivers,
  75. ;  It pushes the received ethernet packets onto the eth_in_queue
  76. ;
  77. ;  IN:   [esp]  = Pointer to buffer
  78. ;       [esp+4] = size of buffer
  79. ;         ebx   = pointer to eth_device
  80. ;  OUT: /
  81. ;
  82. ;-----------------------------------------------------------------
  83. align 4
  84. ETH_input:
  85.  
  86.         push    ebx
  87.         mov     esi, esp
  88.  
  89.         add_to_queue ETH_queue, ETH_QUEUE_SIZE, sizeof.ETH_queue_entry, .fail
  90.         add     esp, sizeof.ETH_queue_entry
  91.  
  92.         xor     edx, edx
  93.         mov     eax, [ETH_input_event]
  94.         mov     ebx, [eax + EVENT.id]
  95.         xor     esi, esi
  96.         call    raise_event
  97.  
  98.         ret
  99.  
  100.   .fail:
  101.         DEBUGF  DEBUG_NETWORK_ERROR, "ETH incoming queue is full, discarding packet!\n"
  102.  
  103.         pop     ebx
  104.         call    NET_packet_free
  105.         add     esp, 4
  106.  
  107.         ret
  108.  
  109.  
  110.  
  111.  
  112. align 4
  113. ETH_process_input:
  114.  
  115.         xor     esi, esi
  116.         mov     ecx, MANUAL_DESTROY
  117.         call    create_event
  118.         mov     [ETH_input_event], eax
  119.  
  120.   .wait:
  121.         mov     eax, [ETH_input_event]
  122.         mov     ebx, [eax + EVENT.id]
  123.         call    wait_event
  124.  
  125.   .loop:
  126.         get_from_queue ETH_queue, ETH_QUEUE_SIZE, sizeof.ETH_queue_entry, .wait
  127.  
  128.         mov     eax, [esi + ETH_queue_entry.packet]
  129.         mov     ecx, [esi + ETH_queue_entry.size]
  130.         mov     ebx, [esi + ETH_queue_entry.device]
  131.  
  132.         pushd   .loop   ; return address
  133.         push    ecx eax
  134.  
  135.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_input: size=%u\n", ecx
  136.         sub     ecx, sizeof.ETH_header
  137.         jb      .dump
  138.  
  139.         lea     edx, [eax + sizeof.ETH_header]
  140.         mov     ax, [eax + ETH_header.Type]
  141.  
  142.         cmp     ax, ETHER_PROTO_IPv4
  143.         je      IPv4_input
  144.  
  145.         cmp     ax, ETHER_PROTO_ARP
  146.         je      ARP_input
  147.  
  148.         cmp     ax, ETHER_PROTO_IPv6
  149.         je      IPv6_input
  150.  
  151.         cmp     ax, ETHER_PROTO_PPP_DISCOVERY
  152.         je      PPPoE_discovery_input
  153.  
  154.         cmp     ax, ETHER_PROTO_PPP_SESSION
  155.         je      PPPoE_session_input
  156.  
  157.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_input: Unknown packet type=%x\n", ax
  158.  
  159.   .dump:
  160.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_input: dumping\n"
  161.         call    NET_packet_free
  162.         add     esp, 4
  163.         ret
  164.  
  165. ;-----------------------------------------------------------------
  166. ;
  167. ; ETH_output
  168. ;
  169. ; IN: eax = pointer to source mac
  170. ;     ebx = device ptr
  171. ;     ecx = packet size
  172. ;     edx = pointer to destination mac
  173. ;      di = protocol
  174. ;
  175. ; OUT: edi = 0 on error, pointer to buffer otherwise
  176. ;      eax = buffer start
  177. ;      ebx = to device structure
  178. ;      ecx = unchanged (packet size of embedded data)
  179. ;      edx = size of complete buffer
  180. ;
  181. ;-----------------------------------------------------------------
  182. align 4
  183. ETH_output:
  184.  
  185.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx
  186.  
  187.         cmp     ecx, [ebx + NET_DEVICE.mtu]
  188.         ja      .exit
  189.  
  190.         push    ecx
  191.         push    di eax edx
  192.  
  193.         add     ecx, sizeof.ETH_header
  194.         stdcall kernel_alloc, ecx
  195.         test    eax, eax
  196.         jz      .out_of_ram
  197.         mov     edi, eax
  198.  
  199.         pop     esi
  200.         movsd
  201.         movsw
  202.         pop     esi
  203.         movsd
  204.         movsw
  205.         pop     ax
  206.         stosw
  207.  
  208.         lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
  209.         pop     ecx
  210.         lea     edx, [ecx + sizeof.ETH_header]  ; Set edx to complete buffer size
  211.  
  212.         cmp     edx, ETH_FRAME_MINIMUM
  213.         jbe     .adjust_size
  214.   .done:
  215.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx
  216.         ret
  217.  
  218.   .adjust_size:
  219.         mov     edx, ETH_FRAME_MINIMUM
  220.         test    edx, edx        ; clear zero flag
  221.         jmp     .done
  222.  
  223.   .out_of_ram:
  224.         DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Out of ram!\n"
  225.         add     esp, 4+4+2+4
  226.         sub     edi, edi
  227.         ret
  228.  
  229.   .exit:
  230.         DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Packet too large!\n"
  231.         sub     edi, edi
  232.         ret
  233.  
  234.  
  235.  
  236. ;-----------------------------------------------------------------
  237. ;
  238. ; ETH_API
  239. ;
  240. ; This function is called by system function 76
  241. ;
  242. ; IN:  subfunction number in bl
  243. ;      device number in bh
  244. ;      ecx, edx, .. depends on subfunction
  245. ;
  246. ; OUT:
  247. ;
  248. ;-----------------------------------------------------------------
  249. align 4
  250. ETH_api:
  251.  
  252.         cmp     bh, NET_DEVICES_MAX
  253.         ja      .error
  254.         movzx   eax, bh
  255.         mov     eax, dword [NET_DRV_LIST + 4*eax]
  256.         cmp     [eax + NET_DEVICE.device_type], NET_DEVICE_ETH
  257.         jne     .error
  258.  
  259.         and     ebx, 0xff
  260.         cmp     ebx, .number
  261.         ja      .error
  262.         jmp     dword [.table + 4*ebx]
  263.  
  264.   .table:
  265.         dd      .read_mac       ; 0
  266.   .number = ($ - .table) / 4 - 1
  267.  
  268.   .error:
  269.         or      eax, -1
  270.         ret
  271.  
  272.  
  273.   .read_mac:
  274.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  275.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  276.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  277.         ret
  278.  
  279.  
  280.