2009-09-17 13:55:38 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2010-07-12 01:13:12 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;;
|
2009-09-17 13:55:38 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; ETHERNET.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Ethernet network layer for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
struct ETH_FRAME
|
2010-07-12 01:13:12 +02:00
|
|
|
.DstMAC dp ? ; destination MAC-address
|
|
|
|
.SrcMAC dp ? ; source MAC-address
|
|
|
|
.Type dw ? ; type of the upper-layer protocol
|
|
|
|
.Data: ; data (46-1500 bytes for a normal packet)
|
2009-09-17 13:55:38 +02:00
|
|
|
ends
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
virtual at NET_DEVICE.end
|
|
|
|
|
|
|
|
ETH_DEVICE:
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
.set_mode dd ?
|
|
|
|
.get_mode dd ?
|
|
|
|
|
2010-07-15 20:54:19 +02:00
|
|
|
.set_MAC dd ?
|
|
|
|
.get_MAC dd ?
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.mode dd ?
|
2009-09-17 13:55:38 +02:00
|
|
|
.mac dp ?
|
2009-11-15 22:44:17 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
end virtual
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
align 4
|
|
|
|
iglobal
|
|
|
|
|
|
|
|
ETH_BROADCAST dp 0xffffffffffff
|
|
|
|
endg
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
ETH_RUNNING dd ?
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; ETH_init
|
|
|
|
;
|
|
|
|
; This function resets all ethernet variables
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
ETH_init:
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [ETH_RUNNING], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; ETH_Receiver:
|
|
|
|
;
|
|
|
|
; This function is called by ethernet drivers,
|
|
|
|
; It pushes the received ethernet packets onto the eth_in_queue
|
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: [esp] = Pointer to buffer
|
2009-11-09 14:59:46 +01:00
|
|
|
; [esp-4] = size of buffer
|
2010-07-12 01:13:12 +02:00
|
|
|
; ebx = pointer to eth_device
|
2009-09-17 13:55:38 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
2009-11-06 21:45:08 +01:00
|
|
|
ETH_receiver:
|
2010-05-28 22:47:32 +02:00
|
|
|
mov eax, [esp]
|
|
|
|
mov ecx, [esp+4]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"ETH_Handler - size: %u\n", ecx
|
|
|
|
cmp ecx, 60 ; check packet length
|
|
|
|
jl .dump
|
|
|
|
sub ecx, ETH_FRAME.Data
|
|
|
|
|
|
|
|
lea edx, [eax + ETH_FRAME.Data]
|
|
|
|
mov ax , [eax + ETH_FRAME.Type]
|
|
|
|
|
|
|
|
cmp ax, ETHER_IPv4
|
2009-10-05 22:47:27 +02:00
|
|
|
je IPv4_handler
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp ax, ETHER_ARP
|
2009-10-05 22:47:27 +02:00
|
|
|
je ARP_handler
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-15 20:54:19 +02:00
|
|
|
; cmp ax, ETHER_PPP_DISCOVERY
|
|
|
|
; je PPPOE_discovery
|
|
|
|
|
2010-05-28 22:47:32 +02:00
|
|
|
DEBUGF 2,"Unknown ethernet packet type %x\n", ax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.dump:
|
2010-05-28 22:47:32 +02:00
|
|
|
DEBUGF 2,"ETH_Handler - dumping\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4
|
|
|
|
ret
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
; ETH_create_packet
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: eax = pointer to source mac
|
|
|
|
; ebx = pointer to destination mac
|
|
|
|
; ecx = packet size
|
|
|
|
; edx = device number
|
|
|
|
; di = protocol
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; OUT: edi = 0 on error, pointer to buffer otherwise
|
|
|
|
; eax = buffer start
|
|
|
|
; ebx = to device structure
|
|
|
|
; ecx = unchanged (packet size of embedded data)
|
|
|
|
; edx = size of complete buffer
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
2009-11-06 21:45:08 +01:00
|
|
|
ETH_create_packet:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-05-28 22:47:32 +02:00
|
|
|
DEBUGF 1,"Creating Ethernet Packet (size=%u): \n", ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-15 20:54:19 +02:00
|
|
|
push esi
|
|
|
|
mov esi, [NET_DRV_LIST] ;;; TODO: FIXME
|
|
|
|
cmp ecx, [esi + NET_DEVICE.mtu]
|
|
|
|
pop esi
|
2009-09-17 13:55:38 +02:00
|
|
|
jg .exit
|
|
|
|
|
|
|
|
push ecx di eax ebx edx
|
|
|
|
|
|
|
|
add ecx, ETH_FRAME.Data
|
|
|
|
push ecx
|
|
|
|
push ecx
|
|
|
|
call kernel_alloc
|
2010-07-12 01:13:12 +02:00
|
|
|
mov edi, eax
|
|
|
|
test edi, edi
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .pop_exit
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
pop edx
|
|
|
|
|
|
|
|
pop esi
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
pop esi
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
pop ax
|
|
|
|
stosw
|
|
|
|
|
|
|
|
lea eax, [edi - ETH_FRAME.Data] ; Set eax to buffer start
|
2009-10-12 16:39:59 +02:00
|
|
|
mov edx, ecx ; Set ebx to complete buffer size
|
2009-09-17 13:55:38 +02:00
|
|
|
pop ecx
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
xor ebx, ebx ;;;; TODO: Fixme
|
2010-07-12 01:13:12 +02:00
|
|
|
mov ebx, [NET_DRV_LIST + ebx]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
cmp edx, 46 + ETH_FRAME.Data ; If data size is less then 46, add padding bytes
|
2010-07-15 20:54:19 +02:00
|
|
|
jge .continue
|
2009-10-12 16:39:59 +02:00
|
|
|
mov edx, 46 + ETH_FRAME.Data
|
|
|
|
.continue:
|
|
|
|
|
2010-05-28 22:47:32 +02:00
|
|
|
DEBUGF 1,"done: %x size:%u device:%x\n", eax, edx, ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.pop_exit:
|
2010-05-28 22:47:32 +02:00
|
|
|
DEBUGF 2,"Out of ram space!!\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
add esp, 18
|
2010-07-12 01:13:12 +02:00
|
|
|
and edi, 0
|
2009-10-12 16:39:59 +02:00
|
|
|
ret
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
.exit:
|
2010-05-28 22:47:32 +02:00
|
|
|
DEBUGF 2,"Packet too large!\n"
|
2010-07-12 01:13:12 +02:00
|
|
|
and edi, 0
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; ETH_API
|
|
|
|
;
|
|
|
|
; This function is called by system function 75
|
|
|
|
;
|
|
|
|
; IN: subfunction number in bl
|
|
|
|
; device number in bh
|
|
|
|
; ecx, edx, .. depends on subfunction
|
|
|
|
;
|
|
|
|
; OUT:
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
ETH_API:
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp bh, MAX_NET_DEVICES
|
|
|
|
jg .error
|
2009-09-17 13:55:38 +02:00
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp bl, 7
|
|
|
|
jz .out_queue
|
|
|
|
cmp bl, 6
|
|
|
|
jz .in_queue
|
|
|
|
|
|
|
|
mov eax, dword [NET_DRV_LIST + eax]
|
|
|
|
cmp [eax + NET_DEVICE.type], NET_TYPE_ETH
|
|
|
|
jne .error
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
test bl, bl
|
|
|
|
jz .packets_tx ; 0
|
|
|
|
dec bl
|
|
|
|
jz .packets_rx ; 1
|
|
|
|
dec bl
|
|
|
|
jz .bytes_tx ; 2
|
|
|
|
dec bl
|
|
|
|
jz .bytes_rx ; 3
|
|
|
|
dec bl
|
|
|
|
jz .read_mac ; 4
|
|
|
|
dec bl
|
|
|
|
jz .write_mac ; 5
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.error:
|
|
|
|
DEBUGF 2,"Device is not ethernet type\n"
|
|
|
|
or eax, -1
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx:
|
2010-07-15 20:54:19 +02:00
|
|
|
mov eax, dword [eax + NET_DEVICE.packets_tx]
|
2009-09-22 18:42:54 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx:
|
2010-07-15 20:54:19 +02:00
|
|
|
mov eax, dword [eax + NET_DEVICE.packets_rx]
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.bytes_tx:
|
2010-07-15 20:54:19 +02:00
|
|
|
mov ebx, dword [eax + NET_DEVICE.bytes_tx + 4]
|
|
|
|
mov eax, dword [eax + NET_DEVICE.bytes_tx]
|
2009-09-24 20:32:03 +02:00
|
|
|
mov [esp+20+4], ebx ; TODO: fix this ugly code
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.bytes_rx:
|
2010-07-15 20:54:19 +02:00
|
|
|
mov ebx, dword [eax + NET_DEVICE.bytes_rx + 4]
|
|
|
|
mov eax, dword [eax + NET_DEVICE.bytes_rx]
|
2009-09-24 20:32:03 +02:00
|
|
|
mov [esp+20+4], ebx ; TODO: fix this ugly code
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
2009-09-24 20:32:03 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
.read_mac:
|
|
|
|
movzx ebx, word [eax + ETH_DEVICE.mac]
|
|
|
|
mov eax, dword [eax + ETH_DEVICE.mac + 2]
|
2009-09-22 18:42:54 +02:00
|
|
|
mov [esp+20+4], ebx ; TODO: fix this ugly code
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.write_mac:
|
|
|
|
push ecx
|
|
|
|
push dx
|
2010-07-15 20:54:19 +02:00
|
|
|
call [eax + ETH_DEVICE.set_MAC]
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.in_queue:
|
2010-07-12 01:13:12 +02:00
|
|
|
if ETH_QUEUE
|
2009-09-17 13:55:38 +02:00
|
|
|
add eax, ETH_IN_QUEUE
|
|
|
|
mov eax, [eax + queue.size]
|
2010-07-12 01:13:12 +02:00
|
|
|
else
|
|
|
|
or eax, -1
|
|
|
|
end if
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.out_queue:
|
2010-07-12 01:13:12 +02:00
|
|
|
if ETH_QUEUE
|
2009-09-17 13:55:38 +02:00
|
|
|
add eax, ETH_OUT_QUEUE
|
|
|
|
mov eax, [eax + queue.size]
|
2010-07-12 01:13:12 +02:00
|
|
|
else
|
|
|
|
or eax, -1
|
|
|
|
end if
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|