Subversion Repositories Kolibri OS

Rev

Rev 4256 | Rev 4511 | 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: 3346 $
  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.         popf
  102.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH incoming queue is full, discarding packet!\n"
  103.  
  104.         add     esp, sizeof.ETH_queue_entry - 8
  105.         call    NET_packet_free
  106.         add     esp, 4
  107.  
  108.         ret
  109.  
  110.  
  111.  
  112.  
  113. align 4
  114. ETH_process_input:
  115.  
  116.         xor     esi, esi
  117.         mov     ecx, MANUAL_DESTROY
  118.         call    create_event
  119.         mov     [ETH_input_event], eax
  120.  
  121.   .wait:
  122.         mov     eax, [ETH_input_event]
  123.         mov     ebx, [eax + EVENT.id]
  124.         call    wait_event
  125.  
  126.   .loop:
  127.         get_from_queue ETH_queue, ETH_QUEUE_SIZE, sizeof.ETH_queue_entry, .wait
  128.  
  129.         mov     eax, [esi + ETH_queue_entry.packet]
  130.         mov     ecx, [esi + ETH_queue_entry.size]
  131.         mov     ebx, [esi + ETH_queue_entry.device]
  132.  
  133.         pushd   .loop   ; return address
  134.         push    ecx eax
  135.  
  136.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_input: size=%u\n", ecx
  137.         sub     ecx, sizeof.ETH_header
  138.         jb      .dump
  139.  
  140.         lea     edx, [eax + sizeof.ETH_header]
  141.         mov     ax, [eax + ETH_header.Type]
  142.  
  143.         cmp     ax, ETHER_PROTO_IPv4
  144.         je      IPv4_input
  145.  
  146.         cmp     ax, ETHER_PROTO_ARP
  147.         je      ARP_input
  148.  
  149.         cmp     ax, ETHER_PROTO_IPv6
  150.         je      IPv6_input
  151.  
  152.         cmp     ax, ETHER_PROTO_PPP_DISCOVERY
  153.         je      PPPoE_discovery_input
  154.  
  155.         cmp     ax, ETHER_PROTO_PPP_SESSION
  156.         je      PPPoE_session_input
  157.  
  158.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_input: Unknown packet type=%x\n", ax
  159.  
  160.   .dump:
  161.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_input: dumping\n"
  162.         call    NET_packet_free
  163.         add     esp, 4
  164.         ret
  165.  
  166. ;-----------------------------------------------------------------
  167. ;
  168. ; ETH_output
  169. ;
  170. ; IN: eax = pointer to source mac
  171. ;     ebx = device ptr
  172. ;     ecx = packet size
  173. ;     edx = pointer to destination mac
  174. ;      di = protocol
  175. ;
  176. ; OUT: edi = 0 on error, pointer to buffer otherwise
  177. ;      eax = buffer start
  178. ;      ebx = to device structure
  179. ;      ecx = unchanged (packet size of embedded data)
  180. ;      edx = size of complete buffer
  181. ;
  182. ;-----------------------------------------------------------------
  183. align 4
  184. ETH_output:
  185.  
  186.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx
  187.  
  188.         cmp     ecx, [ebx + NET_DEVICE.mtu]
  189.         ja      .exit
  190.  
  191.         push    ecx
  192.         push    di eax edx
  193.  
  194.         add     ecx, sizeof.ETH_header
  195.         stdcall kernel_alloc, ecx
  196.         test    eax, eax
  197.         jz      .out_of_ram
  198.         mov     edi, eax
  199.  
  200.         pop     esi
  201.         movsd
  202.         movsw
  203.         pop     esi
  204.         movsd
  205.         movsw
  206.         pop     ax
  207.         stosw
  208.  
  209.         lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
  210.         pop     ecx
  211.         lea     edx, [ecx + sizeof.ETH_header]  ; Set edx to complete buffer size
  212.  
  213.         cmp     edx, ETH_FRAME_MINIMUM
  214.         jbe     .adjust_size
  215.   .done:
  216.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx
  217.         ret
  218.  
  219.   .adjust_size:
  220.         mov     edx, ETH_FRAME_MINIMUM
  221.         test    edx, edx        ; clear zero flag
  222.         jmp     .done
  223.  
  224.   .out_of_ram:
  225.         DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Out of ram!\n"
  226.         add     esp, 4+4+2+4
  227.         sub     edi, edi
  228.         ret
  229.  
  230.   .exit:
  231.         DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Packet too large!\n"
  232.         sub     edi, edi
  233.         ret
  234.  
  235.  
  236.  
  237. ;-----------------------------------------------------------------
  238. ;
  239. ; ETH_API
  240. ;
  241. ; This function is called by system function 76
  242. ;
  243. ; IN:  subfunction number in bl
  244. ;      device number in bh
  245. ;      ecx, edx, .. depends on subfunction
  246. ;
  247. ; OUT:
  248. ;
  249. ;-----------------------------------------------------------------
  250. align 4
  251. ETH_api:
  252.  
  253.         cmp     bh, NET_DEVICES_MAX
  254.         ja      .error
  255.         movzx   eax, bh
  256.         mov     eax, dword [NET_DRV_LIST + 4*eax]
  257.         cmp     [eax + NET_DEVICE.device_type], NET_DEVICE_ETH
  258.         jne     .error
  259.  
  260.         and     ebx, 0xff
  261.         cmp     ebx, .number
  262.         ja      .error
  263.         jmp     dword [.table + 4*ebx]
  264.  
  265.   .table:
  266.         dd      .read_mac       ; 0
  267.   .number = ($ - .table) / 4 - 1
  268.  
  269.   .error:
  270.         or      eax, -1
  271.         ret
  272.  
  273.  
  274.   .read_mac:
  275.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  276.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  277.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  278.         ret
  279.  
  280.  
  281.