2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2021-06-16 12:04:10 +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 ;;
|
|
|
|
;; ;;
|
|
|
|
;; STACK.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; TCP/IP stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; Some parts of code are based on the work of: ;;
|
|
|
|
;; Mike Hibbett (menuetos network stack) ;;
|
|
|
|
;; Eugen Brasoveanu (solar os network stack and drivers) ;;
|
|
|
|
;; mike.dld (kolibrios socket code) ;;
|
|
|
|
;; ;;
|
|
|
|
;; TCP part is based on 4.4BSD ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-04-17 23:19:45 +02:00
|
|
|
$Revision$
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
uglobal
|
2013-05-28 19:34:26 +02:00
|
|
|
net_10ms dd ?
|
|
|
|
net_tmr_count dw ?
|
2011-10-14 23:38:50 +02:00
|
|
|
endg
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUG_NETWORK_ERROR = 1
|
2013-12-19 22:39:14 +01:00
|
|
|
DEBUG_NETWORK_VERBOSE = 0
|
2021-07-07 21:26:17 +02:00
|
|
|
NETWORK_SANITY_CHECKS = 1
|
2013-05-28 21:19:23 +02:00
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
NET_DEVICES_MAX = 16
|
2015-03-18 14:40:27 +01:00
|
|
|
NET_BUFFERS = 512
|
|
|
|
NET_BUFFER_SIZE = 2048
|
2013-05-28 19:34:26 +02:00
|
|
|
ARP_BLOCK = 1 ; true or false
|
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
EPHEMERAL_PORT_MIN = 49152
|
|
|
|
EPHEMERAL_PORT_MAX = 61000
|
2013-05-28 19:34:26 +02:00
|
|
|
MIN_EPHEMERAL_PORT_N = 0x00C0 ; same in Network byte order (FIXME)
|
|
|
|
MAX_EPHEMERAL_PORT_N = 0x48EE ; same in Network byte order (FIXME)
|
|
|
|
|
|
|
|
; Ethernet protocol numbers
|
2013-06-04 16:12:37 +02:00
|
|
|
ETHER_PROTO_ARP = 0x0608
|
|
|
|
ETHER_PROTO_IPv4 = 0x0008
|
|
|
|
ETHER_PROTO_IPv6 = 0xDD86
|
|
|
|
ETHER_PROTO_PPP_DISCOVERY = 0x6388
|
|
|
|
ETHER_PROTO_PPP_SESSION = 0x6488
|
|
|
|
|
|
|
|
; Internet protocol numbers
|
|
|
|
IP_PROTO_IP = 0
|
|
|
|
IP_PROTO_ICMP = 1
|
|
|
|
IP_PROTO_TCP = 6
|
|
|
|
IP_PROTO_UDP = 17
|
2015-10-11 20:23:40 +02:00
|
|
|
IP_PROTO_RAW = 255
|
|
|
|
|
|
|
|
; IP options
|
|
|
|
IP_TOS = 1
|
|
|
|
IP_TTL = 2
|
|
|
|
IP_HDRINCL = 3
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; PPP protocol numbers
|
2013-06-04 16:12:37 +02:00
|
|
|
PPP_PROTO_IPv4 = 0x2100
|
|
|
|
PPP_PROTO_IPV6 = 0x5780
|
|
|
|
PPP_PROTO_ETHERNET = 666 ; FIXME
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;Protocol family
|
|
|
|
AF_UNSPEC = 0
|
|
|
|
AF_LOCAL = 1
|
|
|
|
AF_INET4 = 2
|
|
|
|
AF_INET6 = 10
|
2013-06-04 16:12:37 +02:00
|
|
|
AF_PPP = 777 ; FIXME
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Socket types
|
|
|
|
SOCK_STREAM = 1
|
|
|
|
SOCK_DGRAM = 2
|
|
|
|
SOCK_RAW = 3
|
|
|
|
|
2015-10-11 20:23:40 +02:00
|
|
|
; Socket level
|
|
|
|
SOL_SOCKET = 0xffff
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; Socket options
|
|
|
|
SO_ACCEPTCON = 1 shl 0
|
|
|
|
SO_BROADCAST = 1 shl 1
|
|
|
|
SO_DEBUG = 1 shl 2
|
|
|
|
SO_DONTROUTE = 1 shl 3
|
|
|
|
SO_KEEPALIVE = 1 shl 4
|
|
|
|
SO_OOBINLINE = 1 shl 5
|
|
|
|
SO_REUSEADDR = 1 shl 6
|
|
|
|
SO_REUSEPORT = 1 shl 7
|
|
|
|
SO_USELOOPBACK = 1 shl 8
|
|
|
|
SO_BINDTODEVICE = 1 shl 9
|
2016-08-15 20:06:19 +02:00
|
|
|
SO_LINGER = 1 shl 10
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
SO_NONBLOCK = 1 shl 31
|
|
|
|
|
|
|
|
; Socket flags for user calls
|
|
|
|
MSG_PEEK = 0x02
|
|
|
|
MSG_DONTWAIT = 0x40
|
|
|
|
|
|
|
|
; Socket States
|
2013-06-04 16:12:37 +02:00
|
|
|
SS_NOFDREF = 0x0001 ; no file table ref any more
|
|
|
|
SS_ISCONNECTED = 0x0002 ; socket connected to a peer
|
|
|
|
SS_ISCONNECTING = 0x0004 ; in process of connecting to peer
|
|
|
|
SS_ISDISCONNECTING = 0x0008 ; in process of disconnecting
|
|
|
|
SS_CANTSENDMORE = 0x0010 ; can't send more data to peer
|
|
|
|
SS_CANTRCVMORE = 0x0020 ; can't receive more data from peer
|
|
|
|
SS_RCVATMARK = 0x0040 ; at mark on input
|
|
|
|
SS_ISABORTING = 0x0080 ; aborting fd references - close()
|
|
|
|
SS_RESTARTSYS = 0x0100 ; restart blocked system calls
|
|
|
|
SS_ISDISCONNECTED = 0x0800 ; socket disconnected from peer
|
|
|
|
|
2013-06-17 20:17:17 +02:00
|
|
|
SS_ASYNC = 0x1000 ; async i/o notify
|
|
|
|
SS_ISCONFIRMING = 0x2000 ; deciding to accept connection req
|
|
|
|
SS_MORETOCOME = 0x4000
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
SS_BLOCKED = 0x8000
|
|
|
|
|
|
|
|
|
2016-04-30 23:15:55 +02:00
|
|
|
SOCKET_BUFFER_SIZE = 4096*8 ; must be 4096*(power of 2) where 'power of 2' is at least 8
|
2013-06-04 16:12:37 +02:00
|
|
|
MAX_backlog = 20 ; maximum backlog for stream sockets
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Error Codes
|
2013-06-17 20:17:17 +02:00
|
|
|
ENOBUFS = 1
|
2013-10-14 09:08:51 +02:00
|
|
|
EINPROGRESS = 2
|
2013-06-17 20:17:17 +02:00
|
|
|
EOPNOTSUPP = 4
|
|
|
|
EWOULDBLOCK = 6
|
|
|
|
ENOTCONN = 9
|
|
|
|
EALREADY = 10
|
|
|
|
EINVAL = 11
|
|
|
|
EMSGSIZE = 12
|
|
|
|
ENOMEM = 18
|
|
|
|
EADDRINUSE = 20
|
2016-08-15 20:06:19 +02:00
|
|
|
EADDRNOTAVAIL = 21
|
2013-05-28 19:34:26 +02:00
|
|
|
ECONNRESET = 52
|
2016-08-15 20:06:19 +02:00
|
|
|
ECONNABORTED = 53
|
2013-10-14 09:08:51 +02:00
|
|
|
EISCONN = 56
|
2013-05-28 19:34:26 +02:00
|
|
|
ETIMEDOUT = 60
|
2016-08-15 20:06:19 +02:00
|
|
|
ECONNREFUSED = 61
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Api protocol numbers
|
|
|
|
API_ETH = 0
|
|
|
|
API_IPv4 = 1
|
|
|
|
API_ICMP = 2
|
|
|
|
API_UDP = 3
|
|
|
|
API_TCP = 4
|
|
|
|
API_ARP = 5
|
|
|
|
API_PPPOE = 6
|
|
|
|
API_IPv6 = 7
|
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
; Network device types
|
|
|
|
NET_DEVICE_LOOPBACK = 0
|
|
|
|
NET_DEVICE_ETH = 1
|
|
|
|
NET_DEVICE_SLIP = 2
|
|
|
|
|
|
|
|
; Network link types (link protocols)
|
2015-03-18 14:40:27 +01:00
|
|
|
NET_LINK_LOOPBACK = 0
|
2013-06-04 16:12:37 +02:00
|
|
|
NET_LINK_MAC = 1 ; Media access control (ethernet, isdn, ...)
|
|
|
|
NET_LINK_PPP = 2 ; Point to Point Protocol (PPPoE, ...)
|
|
|
|
NET_LINK_IEEE802.11 = 3 ; IEEE 802.11 (WiFi)
|
|
|
|
|
|
|
|
; Hardware acceleration bits
|
2013-12-19 22:31:51 +01:00
|
|
|
NET_HWACC_TCP_IPv4_IN = 1 shl 0
|
|
|
|
NET_HWACC_TCP_IPv4_OUT = 1 shl 1
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
; Network frame types
|
2015-12-27 16:37:31 +01:00
|
|
|
NET_BUFF_LOOPBACK = 0
|
|
|
|
NET_BUFF_ETH = 1
|
2015-03-17 22:50:29 +01:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
struct NET_DEVICE
|
|
|
|
|
2013-06-04 16:12:37 +02:00
|
|
|
device_type dd ? ; Type field
|
2013-05-28 19:34:26 +02:00
|
|
|
mtu dd ? ; Maximal Transmission Unit
|
|
|
|
name dd ? ; Ptr to 0 terminated string
|
|
|
|
|
|
|
|
unload dd ? ; Ptrs to driver functions
|
|
|
|
reset dd ? ;
|
|
|
|
transmit dd ? ;
|
|
|
|
|
2021-06-20 22:22:57 +02:00
|
|
|
link_state dd ? ; link state (0 = no link)
|
|
|
|
hwacc dd ? ; bitmask stating enabled HW accelerations (offload engines)
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
bytes_tx dq ? ; Statistics, updated by the driver
|
|
|
|
bytes_rx dq ? ;
|
2021-06-20 22:22:57 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
packets_tx dd ? ;
|
2021-06-20 22:22:57 +02:00
|
|
|
packets_tx_err dd ? ; CRC errors, too long or too short frames
|
|
|
|
packets_tx_drop dd ? ;
|
|
|
|
packets_tx_ovr dd ? ; FIFO overrun
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2021-06-20 22:22:57 +02:00
|
|
|
packets_rx dd ? ;
|
|
|
|
packets_rx_err dd ? ; CRC errors, too long or too short frames
|
|
|
|
packets_rx_drop dd ? ;
|
|
|
|
packets_rx_ovr dd ? ; FIFO overrun
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
struct NET_BUFF
|
|
|
|
|
|
|
|
NextPtr dd ? ; pointer to next frame in list
|
|
|
|
PrevPtr dd ? ; pointer to previous frame in list
|
|
|
|
device dd ? ; ptr to NET_DEVICE structure
|
|
|
|
type dd ? ; encapsulation type: e.g. Ethernet
|
|
|
|
length dd ? ; size of encapsulated data
|
|
|
|
offset dd ? ; offset to actual data (24 bytes for default frame)
|
|
|
|
data rb 0
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Exactly as it says..
|
|
|
|
macro pseudo_random reg {
|
|
|
|
add reg, [esp]
|
|
|
|
rol reg, 5
|
|
|
|
xor reg, [timer_ticks]
|
|
|
|
; add reg, [CPU_FREQ]
|
|
|
|
imul reg, 214013
|
|
|
|
xor reg, 0xdeadbeef
|
|
|
|
rol reg, 9
|
|
|
|
}
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; Network to Hardware byte order (dword)
|
|
|
|
macro ntohd reg {
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
rol word reg, 8
|
|
|
|
rol dword reg, 16
|
|
|
|
rol word reg , 8
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
}
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; Network to Hardware byte order (word)
|
|
|
|
macro ntohw reg {
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
rol word reg, 8
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
}
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
include "queue.inc"
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
include "loopback.inc"
|
|
|
|
include "ethernet.inc"
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
include "PPPoE.inc"
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
include "ARP.inc"
|
|
|
|
include "IPv4.inc"
|
|
|
|
include "IPv6.inc"
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
include "icmp.inc"
|
|
|
|
include "udp.inc"
|
|
|
|
include "tcp.inc"
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
include "socket.inc"
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
uglobal
|
2013-06-24 13:39:05 +02:00
|
|
|
align 4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
net_device_count dd ?
|
|
|
|
net_device_list rd NET_DEVICES_MAX
|
2015-03-18 14:40:27 +01:00
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
net_buffs_free rd NET_BUFFERS ; list of pointers to actual net buffs
|
|
|
|
.current dd ? ; pointer to current element in net_buffs_free list
|
|
|
|
|
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
net_buffs_low dd ? ; actual net buff mem region start
|
|
|
|
net_buffs_high dd ? ; actual net buff mem region stop
|
|
|
|
end if
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; stack_init: Initialize all network variables ;
|
|
|
|
; ;
|
|
|
|
; IN: / ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2011-10-14 23:38:50 +02:00
|
|
|
stack_init:
|
|
|
|
|
2015-03-18 14:40:27 +01:00
|
|
|
; allocate network buffers
|
|
|
|
stdcall kernel_alloc, NET_BUFFER_SIZE*NET_BUFFERS
|
|
|
|
test eax, eax
|
|
|
|
jz .fail
|
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
mov [net_buffs_low], eax
|
|
|
|
end if
|
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov edi, net_buffs_free
|
2015-03-18 14:40:27 +01:00
|
|
|
mov ecx, NET_BUFFERS
|
|
|
|
cld
|
|
|
|
.loop:
|
|
|
|
stosd
|
|
|
|
add eax, NET_BUFFER_SIZE
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
sub eax, NET_BUFFER_SIZE
|
|
|
|
mov [net_buffs_high], eax
|
|
|
|
end if
|
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, net_buffs_free
|
2015-03-18 14:40:27 +01:00
|
|
|
stosd
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; Init the network drivers list
|
|
|
|
xor eax, eax
|
2019-08-25 20:46:47 +02:00
|
|
|
mov edi, net_device_count
|
2015-03-17 23:32:21 +01:00
|
|
|
mov ecx, (NET_DEVICES_MAX + 1)
|
2013-06-27 01:35:43 +02:00
|
|
|
rep stosd
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
eth_init
|
2013-10-07 22:53:02 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
pppoe_init
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
ipv4_init
|
|
|
|
; ipv6_init
|
|
|
|
icmp_init
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_init
|
|
|
|
udp_init
|
|
|
|
tcp_init
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
socket_init
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
loop_init
|
2013-06-05 02:21:20 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [net_tmr_count], 0
|
2015-03-18 14:40:27 +01:00
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-03-18 14:40:27 +01:00
|
|
|
.fail:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "Stack init failed!\n"
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
2013-05-27 11:02:35 +02:00
|
|
|
; Wakeup every tick.
|
|
|
|
proc stack_handler_has_work?
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-27 01:35:43 +02:00
|
|
|
mov eax, [timer_ticks]
|
|
|
|
cmp eax, [net_10ms]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-27 11:02:35 +02:00
|
|
|
ret
|
|
|
|
endp
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; stack_handler: Network handlers called from os_loop. ;
|
|
|
|
; ;
|
|
|
|
; IN: / ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2011-10-14 23:38:50 +02:00
|
|
|
align 4
|
|
|
|
stack_handler:
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; Test for 10ms tick
|
|
|
|
mov eax, [timer_ticks]
|
|
|
|
cmp eax, [net_10ms]
|
|
|
|
je .exit
|
|
|
|
mov [net_10ms], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
cmp [net_device_count], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
je .exit
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
test [net_10ms], 0x0f ; 160ms
|
|
|
|
jnz .exit
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
tcp_timer_160ms
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
test [net_10ms], 0x3f ; 640ms
|
|
|
|
jnz .exit
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_decrease_entry_ttls
|
|
|
|
ipv4_decrease_fragment_ttls
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2014-07-30 11:44:18 +02:00
|
|
|
xor edx, edx
|
|
|
|
mov eax, [TCP_timer1_event]
|
|
|
|
mov ebx, [eax + EVENT.id]
|
|
|
|
xor esi, esi
|
|
|
|
call raise_event
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.exit:
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
2013-08-07 15:13:29 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
proc net_buff_alloc stdcall, buffersize
|
|
|
|
|
2015-03-18 14:40:27 +01:00
|
|
|
cmp [buffersize], NET_BUFFER_SIZE
|
|
|
|
ja .too_large
|
|
|
|
|
|
|
|
spin_lock_irqsave
|
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [net_buffs_free.current]
|
|
|
|
cmp eax, net_buffs_free+NET_BUFFERS*4
|
2015-03-18 14:40:27 +01:00
|
|
|
jae .out_of_mem
|
|
|
|
mov eax, [eax]
|
2019-08-25 20:21:44 +02:00
|
|
|
add [net_buffs_free.current], 4
|
2015-03-18 14:40:27 +01:00
|
|
|
|
|
|
|
spin_unlock_irqrestore
|
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
cmp eax, [net_buffs_low]
|
|
|
|
cmp eax, [net_buffs_low]
|
|
|
|
jb .assert_mbuff
|
|
|
|
cmp eax, [net_buffs_high]
|
|
|
|
ja .assert_mbuff
|
|
|
|
test eax, 0x7ff
|
|
|
|
jnz .assert_mbuff
|
|
|
|
end if
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "net_buff_alloc: 0x%x\n", eax
|
2015-03-18 14:40:27 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
.out_of_mem:
|
|
|
|
spin_unlock_irqrestore
|
|
|
|
|
|
|
|
xor eax, eax
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: out of mem!\n"
|
2015-03-18 14:40:27 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
.too_large:
|
|
|
|
xor eax, eax
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: too large!\n"
|
2015-03-18 14:40:27 +01:00
|
|
|
ret
|
2021-07-07 21:26:17 +02:00
|
|
|
|
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
.assert_mbuff:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: invalid buffer 0x%x\n", eax
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: caller=0x%x\n", [esp+4]
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
end if
|
|
|
|
|
2015-03-18 14:40:27 +01:00
|
|
|
endp
|
2015-03-17 22:50:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
proc net_buff_free stdcall, buffer
|
2015-03-18 14:40:27 +01:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "net_buff_free: 0x%x\n", [buffer]
|
2015-03-18 14:40:27 +01:00
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
mov eax, [buffer]
|
|
|
|
cmp eax, [net_buffs_low]
|
|
|
|
jb .assert_mbuff
|
|
|
|
cmp eax, [net_buffs_high]
|
|
|
|
ja .assert_mbuff
|
|
|
|
test eax, 0x7ff
|
|
|
|
jnz .assert_mbuff
|
|
|
|
end if
|
|
|
|
|
2015-03-18 14:40:27 +01:00
|
|
|
spin_lock_irqsave
|
|
|
|
|
2021-06-20 22:22:57 +02:00
|
|
|
sub [net_buffs_free.current], 4 ; move pointer backwards
|
|
|
|
mov eax, [net_buffs_free.current] ; place free'd buffer pointer on the list
|
2015-03-18 14:40:27 +01:00
|
|
|
push [buffer]
|
|
|
|
pop dword[eax]
|
|
|
|
|
|
|
|
spin_unlock_irqrestore
|
|
|
|
|
|
|
|
ret
|
2021-07-07 21:26:17 +02:00
|
|
|
|
|
|
|
if defined NETWORK_SANITY_CHECKS
|
|
|
|
.assert_mbuff:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "net_buff_free: invalid buffer 0x%x\n", eax
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "net_buff_free: caller=0x%x\n", [esp+4]
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
end if
|
|
|
|
|
2015-03-18 14:40:27 +01:00
|
|
|
endp
|
2013-08-07 15:13:29 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
net_link_changed:
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "net_link_changed device=0x%x status=0x%x\n", ebx, [ebx + NET_DEVICE.link_state]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
net_send_event:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "net_send_event\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Send event to all applications
|
|
|
|
push edi ecx
|
|
|
|
mov edi, SLOT_BASE
|
2021-06-16 12:04:10 +02:00
|
|
|
mov ecx, [thread_count]
|
2011-10-14 23:38:50 +02:00
|
|
|
.loop:
|
2021-06-16 15:57:17 +02:00
|
|
|
add edi, sizeof.APPDATA
|
|
|
|
or [edi + APPDATA.occurred_events], EVENT_NETWORK2
|
2011-10-14 23:38:50 +02:00
|
|
|
loop .loop
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx edi
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; net_add_device: Called by network driver to register interface. ;
|
|
|
|
; ;
|
|
|
|
; IN: ebx = ptr to device structure ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = device num on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
net_add_device:
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "net_add_device: %x\n", ebx ;;; TODO: use mutex to lock net device list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
cmp [net_device_count], NET_DEVICES_MAX
|
2013-05-28 19:34:26 +02:00
|
|
|
jae .error
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;----------------------------------
|
|
|
|
; Check if device is already listed
|
|
|
|
mov eax, ebx
|
2013-06-04 16:12:37 +02:00
|
|
|
mov ecx, NET_DEVICES_MAX ; We need to check whole list because a device may be removed without re-organizing list
|
2019-08-25 20:46:47 +02:00
|
|
|
mov edi, net_device_list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-27 01:35:43 +02:00
|
|
|
repne scasd ; See if device is already in the list
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .error
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;----------------------------
|
|
|
|
; Find empty slot in the list
|
|
|
|
xor eax, eax
|
2013-06-04 16:12:37 +02:00
|
|
|
mov ecx, NET_DEVICES_MAX
|
2019-08-25 20:46:47 +02:00
|
|
|
mov edi, net_device_list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-27 01:35:43 +02:00
|
|
|
repne scasd
|
2013-05-28 19:34:26 +02:00
|
|
|
jnz .error
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
sub edi, 4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;-----------------------------
|
|
|
|
; Add device to the found slot
|
|
|
|
mov [edi], ebx ; add device to list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, edi ; Calculate device number in eax
|
2019-08-25 20:46:47 +02:00
|
|
|
sub eax, net_device_list
|
2013-05-28 19:34:26 +02:00
|
|
|
shr eax, 2
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
inc [net_device_count] ; Indicate that one more network device is up and running
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_send_event
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Device number: %u\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.error:
|
|
|
|
or eax, -1
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "Adding network device failed\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; net_remove_device: Called by network driver to unregister dev. ;
|
|
|
|
; ;
|
|
|
|
; IN: ebx = ptr to device ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax: -1 on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2011-10-14 23:38:50 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
net_remove_device:
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
cmp [net_device_count], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
je .error
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;----------------------------
|
|
|
|
; Find the driver in the list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, ebx
|
2013-06-04 16:12:37 +02:00
|
|
|
mov ecx, NET_DEVICES_MAX
|
2019-08-25 20:46:47 +02:00
|
|
|
mov edi, net_device_list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-27 01:35:43 +02:00
|
|
|
repne scasd
|
2013-05-28 19:34:26 +02:00
|
|
|
jnz .error
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
;------------------------
|
|
|
|
; Remove it from the list
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov dword [edi-4], eax
|
2019-08-25 20:46:47 +02:00
|
|
|
dec [net_device_count]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_send_event
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
xor eax, eax
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.error:
|
|
|
|
or eax, -1
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; net_ptr_to_num ;
|
|
|
|
; ;
|
|
|
|
; IN: ebx = ptr to device struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: edi = device number ;
|
|
|
|
; edi = -1 on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
net_ptr_to_num:
|
2013-06-11 12:46:29 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_ptr_to_num4
|
2013-06-11 12:46:29 +02:00
|
|
|
ror edi, 2 ; If -1, stay -1
|
|
|
|
; valid device numbers have last two bits 0, so do just shr
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
net_ptr_to_num4: ; Todo, place number in device structure so we only need to verify?
|
2013-06-11 12:46:29 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
test ebx, ebx
|
|
|
|
jz .fail
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
push ecx
|
2013-06-04 16:12:37 +02:00
|
|
|
mov ecx, NET_DEVICES_MAX
|
2019-08-25 20:46:47 +02:00
|
|
|
mov edi, net_device_list
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
|
|
|
cmp ebx, [edi]
|
2013-06-11 12:46:29 +02:00
|
|
|
je .found
|
2013-05-28 19:34:26 +02:00
|
|
|
add edi, 4
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx
|
2015-03-17 22:50:29 +01:00
|
|
|
.fail:
|
|
|
|
or edi, -1
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.found:
|
2019-08-25 20:46:47 +02:00
|
|
|
sub edi, net_device_list
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; checksum_1: Calculate semi-checksum for network packets. ;
|
|
|
|
; ;
|
|
|
|
; IN: edx = start offset for semi-checksum ;
|
|
|
|
; esi = pointer to data ;
|
|
|
|
; ecx = data size ;
|
|
|
|
; ;
|
|
|
|
; OUT: edx = semi-checksum ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2011-10-14 23:38:50 +02:00
|
|
|
align 4
|
2013-05-28 19:34:26 +02:00
|
|
|
checksum_1:
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
shr ecx, 1
|
|
|
|
pushf
|
|
|
|
jz .no_2
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
shr ecx, 1
|
|
|
|
pushf
|
|
|
|
jz .no_4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
shr ecx, 1
|
|
|
|
pushf
|
|
|
|
jz .no_8
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
|
|
|
add dl, [esi+1]
|
|
|
|
adc dh, [esi+0]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc dl, [esi+3]
|
|
|
|
adc dh, [esi+2]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc dl, [esi+5]
|
|
|
|
adc dh, [esi+4]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc dl, [esi+7]
|
|
|
|
adc dh, [esi+6]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc edx, 0
|
|
|
|
add esi, 8
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc edx, 0
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_8:
|
|
|
|
popf
|
|
|
|
jnc .no_4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
add dl, [esi+1]
|
|
|
|
adc dh, [esi+0]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc dl, [esi+3]
|
|
|
|
adc dh, [esi+2]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc edx, 0
|
|
|
|
add esi, 4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_4:
|
|
|
|
popf
|
|
|
|
jnc .no_2
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
add dl, [esi+1]
|
|
|
|
adc dh, [esi+0]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
adc edx, 0
|
|
|
|
inc esi
|
|
|
|
inc esi
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_2:
|
|
|
|
popf
|
|
|
|
jnc .end
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
add dh, [esi+0]
|
|
|
|
adc edx, 0
|
|
|
|
.end:
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; checksum_2: Calculate the final ip/tcp/udp checksum. ;
|
|
|
|
; ;
|
|
|
|
; IN: edx = semi-checksum ;
|
|
|
|
; ;
|
|
|
|
; OUT: dx = checksum (in INET byte order) ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
|
|
|
checksum_2:
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ecx, edx
|
|
|
|
shr ecx, 16
|
|
|
|
and edx, 0xffff
|
|
|
|
add edx, ecx
|
|
|
|
|
|
|
|
mov ecx, edx
|
|
|
|
shr ecx, 16
|
|
|
|
add dx, cx
|
|
|
|
test dx, dx ; it seems that ZF is not set when CF is set :(
|
|
|
|
not dx
|
|
|
|
jnz .not_zero
|
|
|
|
dec dx
|
|
|
|
.not_zero:
|
|
|
|
xchg dl, dh
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "Checksum: %x\n", dx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; System function 74: Low level access to network devices. ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2013-06-05 02:21:20 +02:00
|
|
|
sys_network:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-09-01 10:02:59 +02:00
|
|
|
cmp bl, 255
|
2013-05-28 19:34:26 +02:00
|
|
|
jne @f
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
mov eax, [net_device_count]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
2019-08-25 20:46:47 +02:00
|
|
|
cmp bh, NET_DEVICES_MAX ; Check if device number exists
|
2013-05-28 19:34:26 +02:00
|
|
|
jae .doesnt_exist
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov esi, ebx
|
|
|
|
and esi, 0x0000ff00
|
|
|
|
shr esi, 6
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
cmp dword[esi + net_device_list], 0 ; check if device is running
|
2013-05-28 19:34:26 +02:00
|
|
|
je .doesnt_exist
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2019-08-25 20:46:47 +02:00
|
|
|
mov eax, [esi + net_device_list]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
and ebx, 0x000000ff
|
|
|
|
cmp ebx, .number
|
|
|
|
ja .doesnt_exist
|
|
|
|
jmp dword [.table + 4*ebx]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.table:
|
2021-07-07 21:26:17 +02:00
|
|
|
dd .get_type ; 0
|
|
|
|
dd .get_dev_name ; 1
|
|
|
|
dd .reset ; 2
|
|
|
|
dd .stop ; 3
|
|
|
|
dd .get_ptr ; 4
|
|
|
|
dd .get_drv_name ; 5
|
|
|
|
|
|
|
|
dd .packets_tx ; 6
|
|
|
|
dd .packets_rx ; 7
|
|
|
|
dd .bytes_tx ; 8
|
|
|
|
dd .bytes_rx ; 9
|
2013-06-05 02:21:20 +02:00
|
|
|
dd .state ; 10
|
2021-07-07 21:26:17 +02:00
|
|
|
dd .packets_tx_err ; 11
|
|
|
|
dd .packets_tx_drop ; 12
|
|
|
|
dd .packets_tx_ovr ; 13
|
|
|
|
dd .packets_rx_err ; 14
|
|
|
|
dd .packets_rx_drop ; 15
|
|
|
|
dd .packets_rx_ovr ; 16
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.number = ($ - .table) / 4 - 1
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.get_type:
|
2013-06-04 16:12:37 +02:00
|
|
|
mov eax, [eax + NET_DEVICE.device_type]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.get_dev_name:
|
2021-07-07 21:26:17 +02:00
|
|
|
mov ebx, eax
|
2021-04-28 08:56:45 +02:00
|
|
|
stdcall is_region_userspace, ecx, 64
|
|
|
|
jz .bad_buffer
|
2021-07-07 21:26:17 +02:00
|
|
|
mov esi, [ebx + NET_DEVICE.name]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov edi, ecx
|
|
|
|
|
|
|
|
mov ecx, 64/4 ; max length
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsd
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.reset:
|
2013-05-28 19:34:26 +02:00
|
|
|
call [eax + NET_DEVICE.reset]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.stop:
|
2013-05-28 19:34:26 +02:00
|
|
|
call [eax + NET_DEVICE.unload]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.get_ptr:
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.get_drv_name:
|
|
|
|
xor eax, eax
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.packets_tx:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_tx]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.packets_rx:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_rx]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.bytes_tx:
|
2015-12-27 16:37:31 +01:00
|
|
|
mov ebx, dword[eax + NET_DEVICE.bytes_tx + 4]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+20], ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
mov eax, dword[eax + NET_DEVICE.bytes_tx]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.bytes_rx:
|
2015-12-27 16:37:31 +01:00
|
|
|
mov ebx, dword[eax + NET_DEVICE.bytes_rx + 4]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+20], ebx
|
2015-12-27 16:37:31 +01:00
|
|
|
mov eax, dword[eax + NET_DEVICE.bytes_rx]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2021-07-07 21:26:17 +02:00
|
|
|
.packets_tx_err:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_tx_err]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx_drop:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_tx_drop]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx_ovr:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_tx_ovr]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx_err:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_rx_err]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx_drop:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_rx_drop]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx_ovr:
|
|
|
|
mov eax, [eax + NET_DEVICE.packets_rx_ovr]
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.state:
|
|
|
|
mov eax, [eax + NET_DEVICE.link_state]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
.doesnt_exist:
|
2021-04-28 08:56:45 +02:00
|
|
|
.bad_buffer: ; Sanity check failed, exit
|
2013-06-05 02:21:20 +02:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; System function 76: Low level access to protocol handlers. ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
|
|
|
sys_protocols:
|
2013-06-04 16:12:37 +02:00
|
|
|
cmp bh, NET_DEVICES_MAX ; Check if device number exists
|
2013-05-28 19:34:26 +02:00
|
|
|
jae .doesnt_exist
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:35:44 +02:00
|
|
|
mov eax, ebx
|
|
|
|
and eax, 0x0000ff00
|
|
|
|
shr eax, 6 ; now we have the device num * 4 in eax
|
|
|
|
cmp [eax + net_device_list], 0 ; check if device is running
|
2013-05-28 19:34:26 +02:00
|
|
|
je .doesnt_exist
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
push .return ; return address (we will be using jumps instead of calls)
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, ebx ; set ax to protocol number
|
|
|
|
shr eax, 16 ;
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_ETH
|
2015-12-27 16:37:31 +01:00
|
|
|
je eth_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_IPv4
|
2015-12-27 16:37:31 +01:00
|
|
|
je ipv4_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_ICMP
|
2015-12-27 16:37:31 +01:00
|
|
|
je icmp_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_UDP
|
2015-12-27 16:37:31 +01:00
|
|
|
je udp_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_TCP
|
2015-12-27 16:37:31 +01:00
|
|
|
je tcp_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_ARP
|
2015-12-27 16:37:31 +01:00
|
|
|
je arp_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_PPPOE
|
2015-12-27 16:37:31 +01:00
|
|
|
je pppoe_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ax, API_IPv6
|
2015-12-27 16:37:31 +01:00
|
|
|
je ipv6_api
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
add esp, 4 ; if we reached here, no function was called, so we need to balance stack
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.doesnt_exist:
|
|
|
|
mov eax, -1
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.return:
|
|
|
|
mov [esp+28+4], eax ; return eax value to the program
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|