2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2021-07-07 21:26:17 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;;
|
2013-05-28 19:34:26 +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 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-04-17 23:19:45 +02:00
|
|
|
$Revision$
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ETH_FRAME_MINIMUM = 60
|
2013-10-07 22:53:02 +02:00
|
|
|
ETH_QUEUE_SIZE = 255
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
struct ETH_header
|
|
|
|
|
|
|
|
DstMAC dp ? ; destination MAC-address
|
|
|
|
SrcMAC dp ? ; source MAC-address
|
|
|
|
Type dw ? ; type of the upper-layer protocol
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct ETH_DEVICE NET_DEVICE
|
|
|
|
|
|
|
|
mac dp ?
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
iglobal
|
|
|
|
align 4
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
ETH_BROADCAST dp 0xffffffffffff
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
ETH_frame_queued dd 0 ; Number of queued frames
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
ETH_frame_head dd ETH_frame_head ; Pointer to next frame in the linked list
|
|
|
|
ETH_frame_tail dd ETH_frame_head ; Pointer to last frame in the linked list
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
endg
|
|
|
|
|
2013-10-07 22:53:02 +02:00
|
|
|
uglobal
|
|
|
|
align 4
|
|
|
|
ETH_input_event dd ?
|
|
|
|
endg
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
macro eth_init {
|
2013-10-07 22:53:02 +02:00
|
|
|
|
|
|
|
movi ebx, 1
|
2015-12-27 16:37:31 +01:00
|
|
|
mov ecx, eth_process_input
|
2013-10-07 22:53:02 +02:00
|
|
|
call new_sys_threads
|
|
|
|
test eax, eax
|
|
|
|
jns @f
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR,'K : cannot create kernel thread for ethernet, error %d\n', eax
|
|
|
|
@@:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2021-12-04 17:01:10 +01:00
|
|
|
; This function is called by ethernet drivers.
|
|
|
|
; Push the received ethernet packet onto the ethernet input queue.
|
|
|
|
;
|
|
|
|
; Input:
|
|
|
|
; [esp + 4] = Pointer to buffer
|
|
|
|
; [esp + 8] = Return address (yes, really)
|
|
|
|
;
|
|
|
|
; Example:
|
|
|
|
; push .retaddr
|
|
|
|
; push buf_addr
|
|
|
|
; jmp eth_input
|
2015-12-27 16:37:31 +01:00
|
|
|
eth_input:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
pop eax
|
2021-07-07 21:26:17 +02:00
|
|
|
|
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
cmp eax, [net_buffs_low]
|
|
|
|
jb .assert_mbuff
|
|
|
|
cmp eax, [net_buffs_high]
|
|
|
|
ja .assert_mbuff
|
|
|
|
test eax, 0x7ff
|
|
|
|
jnz .assert_mbuff
|
|
|
|
end if
|
|
|
|
|
|
|
|
spin_lock_irqsave
|
2015-03-17 22:50:29 +01:00
|
|
|
|
|
|
|
cmp [ETH_frame_queued], ETH_QUEUE_SIZE
|
|
|
|
jae .full
|
|
|
|
inc [ETH_frame_queued]
|
|
|
|
|
|
|
|
; Add frame to the end of the linked list
|
|
|
|
mov [eax + NET_BUFF.NextPtr], ETH_frame_head
|
|
|
|
|
|
|
|
mov ebx, [ETH_frame_tail]
|
|
|
|
mov [eax + NET_BUFF.PrevPtr], ebx
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
mov [ETH_frame_tail], eax
|
|
|
|
mov [ebx + NET_BUFF.NextPtr], eax
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
spin_unlock_irqrestore
|
2015-03-17 22:50:29 +01:00
|
|
|
|
2018-11-06 21:12:47 +01:00
|
|
|
; Mark it as being an Ethernet Frame
|
|
|
|
mov [eax + NET_BUFF.type], NET_BUFF_ETH
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
; Now queue an event to process it
|
2013-10-07 22:53:02 +02:00
|
|
|
xor edx, edx
|
|
|
|
mov eax, [ETH_input_event]
|
|
|
|
mov ebx, [eax + EVENT.id]
|
|
|
|
xor esi, esi
|
|
|
|
call raise_event
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
.full:
|
2021-07-07 21:26:17 +02:00
|
|
|
mov ebx, [eax + NET_BUFF.device]
|
|
|
|
inc [ebx + NET_DEVICE.packets_rx_ovr]
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH incoming queue is full, discarding packet!\n"
|
|
|
|
spin_unlock_irqrestore
|
|
|
|
stdcall net_buff_free, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
.assert_mbuff:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "eth_input: invalid buffer 0x%x\n", eax
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "eth_input: caller=0x%x\n", [esp+4]
|
|
|
|
xor eax, eax
|
2013-10-07 22:53:02 +02:00
|
|
|
ret
|
2021-07-07 21:26:17 +02:00
|
|
|
end if
|
2013-10-07 22:53:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; eth_process_input: Process packets from ethernet input queue. ;
|
|
|
|
; ;
|
|
|
|
; IN: / ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-10-07 22:53:02 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
eth_process_input:
|
2013-10-07 22:53:02 +02:00
|
|
|
|
|
|
|
xor esi, esi
|
|
|
|
mov ecx, MANUAL_DESTROY
|
|
|
|
call create_event
|
|
|
|
mov [ETH_input_event], eax
|
2015-03-17 22:50:29 +01:00
|
|
|
pushf
|
2013-10-07 22:53:02 +02:00
|
|
|
.wait:
|
2015-03-17 22:50:29 +01:00
|
|
|
popf
|
2013-10-07 22:53:02 +02:00
|
|
|
mov eax, [ETH_input_event]
|
|
|
|
mov ebx, [eax + EVENT.id]
|
|
|
|
call wait_event
|
|
|
|
|
|
|
|
.loop:
|
2015-03-17 22:50:29 +01:00
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
cmp [ETH_frame_queued], 0
|
|
|
|
je .wait
|
|
|
|
|
|
|
|
dec [ETH_frame_queued]
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
mov esi, [ETH_frame_head]
|
|
|
|
mov ebx, [esi + NET_BUFF.NextPtr]
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
mov [ETH_frame_head], ebx
|
|
|
|
mov [ebx + NET_BUFF.PrevPtr], ETH_frame_head
|
|
|
|
|
|
|
|
popf
|
|
|
|
|
|
|
|
mov eax, [esi + NET_BUFF.offset]
|
|
|
|
add eax, esi
|
|
|
|
mov ecx, [esi + NET_BUFF.length]
|
|
|
|
mov ebx, [esi + NET_BUFF.device]
|
|
|
|
|
|
|
|
pushd .loop ; return address for protocol handler
|
|
|
|
push esi ; keep pointer to NET_BUFF on stack
|
2013-10-07 22:53:02 +02:00
|
|
|
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: size=%u\n", ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
sub ecx, sizeof.ETH_header
|
2021-07-07 21:26:17 +02:00
|
|
|
jb .err
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
; Set registers for protocol handlers
|
2013-05-28 19:34:26 +02:00
|
|
|
lea edx, [eax + sizeof.ETH_header]
|
|
|
|
mov ax, [eax + ETH_header.Type]
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
; Place protocol handlers here
|
2013-06-04 16:12:37 +02:00
|
|
|
cmp ax, ETHER_PROTO_IPv4
|
2015-12-27 16:37:31 +01:00
|
|
|
je ipv4_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
cmp ax, ETHER_PROTO_ARP
|
2015-12-27 16:37:31 +01:00
|
|
|
je arp_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-07-17 17:52:31 +02:00
|
|
|
; cmp ax, ETHER_PROTO_IPv6
|
2015-12-27 16:37:31 +01:00
|
|
|
; je ipv6_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-07-17 17:52:31 +02:00
|
|
|
; cmp ax, ETHER_PROTO_PPP_DISCOVERY
|
2015-12-27 16:37:31 +01:00
|
|
|
; je pppoe_discovery_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-07-17 17:52:31 +02:00
|
|
|
; cmp ax, ETHER_PROTO_PPP_SESSION
|
2015-12-27 16:37:31 +01:00
|
|
|
; je pppoe_session_input
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-11-19 19:58:46 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: Unknown packet type=%x\n", ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
.drop:
|
|
|
|
mov eax, [esp]
|
|
|
|
mov eax, [eax + NET_BUFF.device]
|
|
|
|
inc [eax + NET_DEVICE.packets_rx_drop]
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: dropping\n"
|
|
|
|
call net_buff_free
|
|
|
|
ret
|
|
|
|
|
|
|
|
.err:
|
|
|
|
mov eax, [esp]
|
|
|
|
mov eax, [eax + NET_BUFF.device]
|
|
|
|
inc [eax + NET_DEVICE.packets_rx_err]
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: invalid frame received\n"
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
|
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; eth_output ;
|
|
|
|
; ;
|
|
|
|
; IN: ax = protocol ;
|
|
|
|
; ebx = device ptr ;
|
|
|
|
; ecx = payload size ;
|
|
|
|
; edx = pointer to destination mac ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = start of net frame / 0 on error ;
|
|
|
|
; ebx = device ptr ;
|
|
|
|
; ecx = payload size ;
|
|
|
|
; edi = start of payload ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
eth_output:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-02 14:48:32 +02:00
|
|
|
cmp ecx, [ebx + ETH_DEVICE.mtu]
|
2021-07-07 21:26:17 +02:00
|
|
|
ja .too_large
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push ecx
|
2014-08-02 14:48:32 +02:00
|
|
|
push ax edx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
add ecx, sizeof.ETH_header + NET_BUFF.data
|
2015-12-27 16:37:31 +01:00
|
|
|
stdcall net_buff_alloc, ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .out_of_ram
|
2021-07-07 21:26:17 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
mov [eax + NET_BUFF.type], NET_BUFF_ETH
|
|
|
|
mov [eax + NET_BUFF.device], ebx
|
|
|
|
mov [eax + NET_BUFF.offset], NET_BUFF.data
|
|
|
|
lea edi, [eax + NET_BUFF.data]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
pop esi
|
|
|
|
movsd
|
|
|
|
movsw
|
2014-08-02 14:48:32 +02:00
|
|
|
lea esi, [ebx + ETH_DEVICE.mac]
|
2013-05-28 19:34:26 +02:00
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
pop ax
|
|
|
|
stosw
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
lea eax, [edi - sizeof.ETH_header - NET_BUFF.data] ; Set eax to buffer start
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
lea edx, [ecx + sizeof.ETH_header] ; Set edx to complete buffer size
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp edx, ETH_FRAME_MINIMUM
|
|
|
|
jbe .adjust_size
|
|
|
|
.done:
|
2015-03-17 22:50:29 +01:00
|
|
|
mov [eax + NET_BUFF.length], edx
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.adjust_size:
|
|
|
|
mov edx, ETH_FRAME_MINIMUM
|
2014-08-02 14:48:32 +02:00
|
|
|
test edx, edx ; clear zero flag
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .done
|
|
|
|
|
|
|
|
.out_of_ram:
|
2021-07-07 21:26:17 +02:00
|
|
|
inc [ebx + NET_DEVICE.packets_tx_drop]
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: Out of ram!\n"
|
|
|
|
add esp, 4+2
|
|
|
|
pop ecx
|
2014-08-02 14:48:32 +02:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
.too_large:
|
|
|
|
inc [eax + NET_DEVICE.packets_tx_err]
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: Packet too large!\n"
|
2014-08-02 14:48:32 +02:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; eth_api: Part of system function 76. ;
|
|
|
|
; ;
|
|
|
|
; IN: bl = subfunction number ;
|
|
|
|
; bh = device number ;
|
|
|
|
; ecx, edx, .. depends on subfunction ;
|
|
|
|
; ;
|
|
|
|
; OUT: depends on subfunction ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
eth_api:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
cmp bh, NET_DEVICES_MAX
|
2013-05-28 19:34:26 +02:00
|
|
|
ja .error
|
|
|
|
movzx eax, bh
|
2019-08-25 20:46:47 +02:00
|
|
|
mov eax, dword [net_device_list + 4*eax]
|
2013-06-04 16:12:37 +02:00
|
|
|
cmp [eax + NET_DEVICE.device_type], NET_DEVICE_ETH
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .error
|
|
|
|
|
|
|
|
and ebx, 0xff
|
|
|
|
cmp ebx, .number
|
|
|
|
ja .error
|
|
|
|
jmp dword [.table + 4*ebx]
|
|
|
|
|
|
|
|
.table:
|
2013-06-05 02:21:20 +02:00
|
|
|
dd .read_mac ; 0
|
2013-05-28 19:34:26 +02:00
|
|
|
.number = ($ - .table) / 4 - 1
|
|
|
|
|
|
|
|
.error:
|
|
|
|
or eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
.read_mac:
|
|
|
|
movzx ebx, word [eax + ETH_DEVICE.mac]
|
|
|
|
mov eax, dword [eax + ETH_DEVICE.mac + 2]
|
2015-03-17 22:50:29 +01:00
|
|
|
mov [esp+20+4], ebx ; FIXME
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|