2009-09-22 18:42:54 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2012-07-17 23:36:34 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
|
2009-09-22 18:42:54 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; STACK.INC ;;
|
|
|
|
;; ;;
|
2011-01-24 23:01:54 +01:00
|
|
|
;; TCP/IP stack for KolibriOS ;;
|
2009-09-22 18:42:54 +02:00
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
2011-01-24 23:01:54 +01:00
|
|
|
;; 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 ;;
|
2009-09-22 18:42:54 +02:00
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
__DEBUG_LEVEL_OLD__ equ __DEBUG_LEVEL__ ; use seperate debug level for network part of kernel
|
|
|
|
__DEBUG_LEVEL__ equ 1
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
uglobal
|
2012-02-21 15:09:00 +01:00
|
|
|
net_10ms dd ?
|
|
|
|
net_tmr_count dw ?
|
2009-09-17 13:55:38 +02:00
|
|
|
endg
|
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
MAX_NET_DEVICES = 16
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
MIN_EPHEMERAL_PORT = 49152
|
2012-11-01 13:33:15 +01:00
|
|
|
MIN_EPHEMERAL_PORT_N = 0x00C0 ; same in Network byte order (FIXME)
|
2012-06-03 21:28:01 +02:00
|
|
|
MAX_EPHEMERAL_PORT = 61000
|
2012-11-01 13:33:15 +01:00
|
|
|
MAX_EPHEMERAL_PORT_N = 0x48EE ; same in Network byte order (FIXME)
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; Ethernet protocol numbers
|
2012-06-03 21:28:01 +02:00
|
|
|
ETHER_ARP = 0x0608
|
|
|
|
ETHER_IPv4 = 0x0008
|
|
|
|
ETHER_IPv6 = 0xDD86
|
|
|
|
ETHER_PPP_DISCOVERY = 0x6388
|
|
|
|
ETHER_PPP_SESSION = 0x6488
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-08-19 21:09:09 +02:00
|
|
|
; PPP protocol numbers
|
|
|
|
PPP_IPv4 = 0x2100
|
2012-09-01 22:44:13 +02:00
|
|
|
PPP_IPV6 = 0x5780
|
2012-08-19 21:09:09 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
;Protocol family
|
2012-06-03 21:28:01 +02:00
|
|
|
AF_UNSPEC = 0
|
|
|
|
AF_UNIX = 1
|
|
|
|
AF_INET4 = 2
|
|
|
|
AF_INET6 = 10
|
2012-08-19 21:09:09 +02:00
|
|
|
AF_PPP = 777
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; Internet protocol numbers
|
2012-06-03 21:28:01 +02:00
|
|
|
IP_PROTO_IP = 0
|
|
|
|
IP_PROTO_ICMP = 1
|
|
|
|
IP_PROTO_TCP = 6
|
|
|
|
IP_PROTO_UDP = 17
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-08-19 21:09:09 +02:00
|
|
|
; PPP protocol number
|
|
|
|
PPP_PROTO_ETHERNET = 666
|
|
|
|
|
2009-10-07 22:36:58 +02:00
|
|
|
; Socket types
|
2012-06-03 21:28:01 +02:00
|
|
|
SOCK_STREAM = 1
|
|
|
|
SOCK_DGRAM = 2
|
|
|
|
SOCK_RAW = 3
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; Socket options
|
2012-06-03 21:28:01 +02:00
|
|
|
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
|
2012-07-17 23:36:34 +02:00
|
|
|
SO_BINDTODEVICE = 1 shl 9
|
|
|
|
|
2012-10-30 20:46:04 +01:00
|
|
|
SO_BLOCK = 1 shl 10
|
|
|
|
|
2012-07-17 23:36:34 +02:00
|
|
|
; Socket level
|
|
|
|
SOL_SOCKET = 0
|
2011-02-24 15:16:02 +01:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2011-01-28 17:49:08 +01:00
|
|
|
; Socket States
|
2012-06-03 21:28:01 +02:00
|
|
|
SS_NOFDREF = 0x001 ; no file table ref any more
|
|
|
|
SS_ISCONNECTED = 0x002 ; socket connected to a peer
|
|
|
|
SS_ISCONNECTING = 0x004 ; in process of connecting to peer
|
|
|
|
SS_ISDISCONNECTING = 0x008 ; in process of disconnecting
|
|
|
|
SS_CANTSENDMORE = 0x010 ; can't send more data to peer
|
|
|
|
SS_CANTRCVMORE = 0x020 ; can't receive more data from peer
|
|
|
|
SS_RCVATMARK = 0x040 ; at mark on input
|
|
|
|
SS_ISABORTING = 0x080 ; aborting fd references - close()
|
|
|
|
SS_RESTARTSYS = 0x100 ; restart blocked system calls
|
|
|
|
SS_ISDISCONNECTED = 0x800 ; socket disconnected from peer
|
2011-01-28 17:49:08 +01:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
SS_ASYNC = 0x100 ; async i/o notify
|
|
|
|
SS_ISCONFIRMING = 0x200 ; deciding to accept connection req
|
|
|
|
SS_MORETOCOME = 0x400
|
2011-01-28 17:49:08 +01:00
|
|
|
|
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
SOCKET_MAXDATA = 4096*32 ; must be 4096*(power of 2) where 'power of 2' is at least 8
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; Network driver types
|
2012-08-08 15:14:32 +02:00
|
|
|
NET_TYPE_LOOPBACK = 0
|
2012-06-03 21:28:01 +02:00
|
|
|
NET_TYPE_ETH = 1
|
|
|
|
NET_TYPE_SLIP = 2
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
MAX_backlog = 20 ; maximum backlog for stream sockets
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-12-10 19:37:02 +01:00
|
|
|
; Error Codes
|
2012-06-03 21:28:01 +02:00
|
|
|
ENOBUFS = 55
|
|
|
|
ECONNREFUSED = 61
|
|
|
|
ECONNRESET = 52
|
|
|
|
ETIMEDOUT = 60
|
|
|
|
ECONNABORTED = 53
|
|
|
|
|
|
|
|
; Api protocol numbers
|
|
|
|
API_ETH = 0
|
|
|
|
API_IPv4 = 1
|
|
|
|
API_ICMP = 2
|
|
|
|
API_UDP = 3
|
|
|
|
API_TCP = 4
|
|
|
|
API_ARP = 5
|
|
|
|
API_PPPOE = 6
|
2009-12-13 15:55:42 +01:00
|
|
|
|
2012-08-15 18:15:57 +02:00
|
|
|
HWACC_TCP_IPv4 = 1 shl 0
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
struct NET_DEVICE
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
type dd ? ; Type field
|
|
|
|
mtu dd ? ; Maximal Transmission Unit
|
|
|
|
name dd ? ; Ptr to 0 terminated string
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
unload dd ? ; Ptrs to driver functions
|
|
|
|
reset dd ? ;
|
|
|
|
transmit dd ? ;
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
bytes_tx dq ? ; Statistics, updated by the driver
|
|
|
|
bytes_rx dq ? ;
|
|
|
|
packets_tx dd ? ;
|
|
|
|
packets_rx dd ? ;
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2011-11-09 00:06:26 +01:00
|
|
|
; hwacc dd ? ; bitmask stating available hardware accelerations (offload engines)
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2011-11-09 00:06:26 +01:00
|
|
|
ends
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
; Exactly as it says..
|
|
|
|
macro pseudo_random reg {
|
2012-02-21 15:09:00 +01:00
|
|
|
add reg, [esp]
|
|
|
|
rol reg, 5
|
|
|
|
xor reg, [timer_ticks]
|
|
|
|
add reg, [CPU_FREQ]
|
|
|
|
imul reg, 214013
|
|
|
|
xor reg, 0xdeadbeef
|
|
|
|
rol reg, 9
|
2010-07-12 01:13:12 +02:00
|
|
|
}
|
2009-12-13 15:55:42 +01:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
macro ntohd reg {
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
rol word reg, 8
|
|
|
|
rol dword reg, 16
|
|
|
|
rol word reg , 8
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
macro ntohw reg {
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
rol word reg, 8
|
2009-12-13 15:55:42 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-22 17:06:05 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
include "queue.inc"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-08-08 15:14:32 +02:00
|
|
|
include "loopback.inc"
|
2010-07-12 01:13:12 +02:00
|
|
|
include "ethernet.inc"
|
2010-12-18 00:04:18 +01:00
|
|
|
|
2012-08-19 21:09:09 +02:00
|
|
|
include "PPPoE.inc"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-09-28 22:16:23 +02:00
|
|
|
include "ARP.inc"
|
|
|
|
include "IPv4.inc"
|
2012-06-03 21:28:01 +02:00
|
|
|
;include "IPv6.inc"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
include "icmp.inc"
|
2010-07-12 01:13:12 +02:00
|
|
|
include "udp.inc"
|
|
|
|
include "tcp.inc"
|
|
|
|
|
|
|
|
include "socket.inc"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
NET_RUNNING dd ?
|
|
|
|
NET_DEFAULT dd ?
|
|
|
|
NET_DRV_LIST rd MAX_NET_DEVICES
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
endg
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; stack_init
|
|
|
|
;
|
|
|
|
; This function calls all network init procedures
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
stack_init:
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; Init the network drivers list
|
2012-02-21 15:09:00 +01:00
|
|
|
xor eax, eax
|
|
|
|
mov edi, NET_RUNNING
|
|
|
|
mov ecx, (MAX_NET_DEVICES + 2)
|
|
|
|
rep stosd
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-08-30 22:01:42 +02:00
|
|
|
PPPoE_init
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
IPv4_init
|
2012-06-03 21:28:01 +02:00
|
|
|
; IPv6_init
|
2012-02-21 15:09:00 +01:00
|
|
|
ICMP_init
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
ARP_init
|
|
|
|
UDP_init
|
|
|
|
TCP_init
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
SOCKET_init
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov [net_tmr_count], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; stack_handler
|
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; This function is called in kernel loop
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
stack_handler:
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp [NET_RUNNING], 0
|
|
|
|
je .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
; Test for 10ms tick
|
|
|
|
mov eax, [timer_ticks]
|
|
|
|
cmp eax, [net_10ms]
|
|
|
|
je .exit
|
|
|
|
mov [net_10ms], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
test [net_10ms], 0x0f ; 160ms
|
|
|
|
jnz .exit
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
TCP_timer_160ms
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
test [net_10ms], 0x3f ; 640ms
|
|
|
|
jnz .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
TCP_timer_640ms
|
|
|
|
ARP_decrease_entry_ttls
|
|
|
|
IPv4_decrease_fragment_ttls
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2011-09-22 06:08:56 +02:00
|
|
|
; NET_add_device:
|
2010-07-12 01:13:12 +02:00
|
|
|
;
|
|
|
|
; This function is called by the network drivers,
|
|
|
|
; to register each running NIC to the kernel
|
|
|
|
;
|
|
|
|
; IN: Pointer to device structure in ebx
|
|
|
|
; OUT: Device num in eax, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
NET_add_device:
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
DEBUGF 1,"NET_Add_Device: %x\n", ebx ;;; TODO: use mutex to lock net device list
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, [NET_RUNNING]
|
|
|
|
cmp eax, MAX_NET_DEVICES
|
|
|
|
jae .error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
;----------------------------------
|
|
|
|
; Check if device is already listed
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, ebx
|
|
|
|
mov ecx, MAX_NET_DEVICES ; We need to check whole list because a device may be removed without re-organizing list
|
|
|
|
mov edi, NET_DRV_LIST
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
repne scasd ; See if device is already in the list
|
|
|
|
jz .error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
;----------------------------
|
|
|
|
; Find empty slot in the list
|
2012-02-21 15:09:00 +01:00
|
|
|
xor eax, eax
|
|
|
|
mov ecx, MAX_NET_DEVICES
|
|
|
|
mov edi, NET_DRV_LIST
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
repne scasd
|
|
|
|
jnz .error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
sub edi, 4
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2011-09-22 06:08:56 +02:00
|
|
|
;-----------------------------
|
|
|
|
; Add device to the found slot
|
2012-02-21 15:09:00 +01:00
|
|
|
mov [edi], ebx ; add device to list
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, edi ; Calculate device number in eax
|
|
|
|
sub eax, NET_DRV_LIST
|
|
|
|
shr eax, 2
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
inc [NET_RUNNING] ; Indicate that one more network device is up and running
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp eax, 1 ; If it's the first network device, try to set it as default
|
|
|
|
jne @f
|
|
|
|
push eax
|
|
|
|
call NET_set_default
|
|
|
|
pop eax
|
2011-09-22 06:08:56 +02:00
|
|
|
@@:
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
DEBUGF 1,"Device number: %u\n", eax
|
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2011-09-22 06:08:56 +02:00
|
|
|
.error:
|
2012-02-21 15:09:00 +01:00
|
|
|
or eax, -1
|
|
|
|
DEBUGF 2,"Adding network device failed\n"
|
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-22 06:08:56 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; NET_set_default
|
|
|
|
;
|
|
|
|
; API to set the default interface
|
|
|
|
;
|
|
|
|
; IN: Device num in eax
|
|
|
|
; OUT: Device num in eax, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
NET_set_default:
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"NET_set_default: device=%x\n", eax
|
2011-09-22 06:08:56 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp eax, MAX_NET_DEVICES
|
|
|
|
jae .error
|
2011-09-22 06:08:56 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp [NET_DRV_LIST+eax*4], 0
|
|
|
|
je .error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov [NET_DEFAULT], eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"NET_set_default: succes\n"
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
.error:
|
2012-02-21 15:09:00 +01:00
|
|
|
or eax, -1
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"NET_set_default: failed\n"
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; NET_Remove_Device:
|
|
|
|
;
|
|
|
|
; This function is called by etwork drivers,
|
|
|
|
; to unregister network devices from the kernel
|
2012-07-27 01:21:35 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: Pointer to device structure in ebx
|
|
|
|
; OUT: eax: -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
NET_remove_device:
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp [NET_RUNNING], 0
|
|
|
|
je .error
|
|
|
|
|
|
|
|
cmp [NET_DRV_LIST], ebx
|
|
|
|
jne @f
|
|
|
|
mov [NET_DRV_LIST], 0
|
|
|
|
cmp [NET_RUNNING], 1
|
|
|
|
je @f
|
|
|
|
; there are still active devices, find one and make it default
|
|
|
|
xor eax, eax
|
|
|
|
mov ecx, MAX_NET_DEVICES
|
|
|
|
mov edi, NET_DRV_LIST
|
|
|
|
repe scasd
|
|
|
|
je @f
|
|
|
|
shr edi, 2
|
|
|
|
dec edi
|
|
|
|
mov [NET_DEFAULT], edi
|
2011-09-22 06:08:56 +02:00
|
|
|
@@:
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
;----------------------------
|
|
|
|
; Find the driver in the list
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, ebx
|
|
|
|
mov ecx, MAX_NET_DEVICES
|
|
|
|
mov edi, NET_DRV_LIST+4
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
repne scasd
|
|
|
|
jnz .error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
;------------------------
|
|
|
|
; Remove it from the list
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
xor eax, eax
|
|
|
|
mov dword [edi-4], eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
dec [NET_RUNNING]
|
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
.error:
|
2012-02-21 15:09:00 +01:00
|
|
|
or eax, -1
|
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; NET_ptr_to_num
|
|
|
|
;
|
|
|
|
; IN: ebx = ptr to device struct
|
|
|
|
; OUT: edi = -1 on error, device number otherwise
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
NET_ptr_to_num:
|
2012-02-21 15:09:00 +01:00
|
|
|
push ecx
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov ecx, MAX_NET_DEVICES
|
|
|
|
mov edi, NET_DRV_LIST
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
.loop:
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp ebx, [edi]
|
|
|
|
jz .found
|
|
|
|
add edi, 4
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
; repnz scasd could work too if eax is used instead of ebx!
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
or edi, -1
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
pop ecx
|
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
.found:
|
2012-02-21 15:09:00 +01:00
|
|
|
sub edi, NET_DRV_LIST
|
|
|
|
shr edi, 2
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
pop ecx
|
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; checksum_1
|
|
|
|
;
|
2010-06-07 11:37:13 +02:00
|
|
|
; This is the first of two functions needed to calculate a checksum.
|
2009-11-06 21:45:08 +01:00
|
|
|
;
|
2010-05-28 22:47:32 +02:00
|
|
|
; IN: edx = start offset for semi-checksum
|
2009-11-06 21:45:08 +01:00
|
|
|
; esi = pointer to data
|
|
|
|
; ecx = data size
|
|
|
|
; OUT: edx = semi-checksum
|
|
|
|
;
|
2010-05-28 22:47:32 +02:00
|
|
|
;
|
|
|
|
; Code was optimized by diamond
|
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
checksum_1:
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
shr ecx, 1
|
|
|
|
pushf
|
|
|
|
jz .no_2
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
shr ecx, 1
|
|
|
|
pushf
|
|
|
|
jz .no_4
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
shr ecx, 1
|
|
|
|
pushf
|
|
|
|
jz .no_8
|
2010-05-28 22:47:32 +02:00
|
|
|
|
|
|
|
.loop:
|
2012-02-21 15:09:00 +01:00
|
|
|
add dl, [esi+1]
|
|
|
|
adc dh, [esi+0]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc dl, [esi+3]
|
|
|
|
adc dh, [esi+2]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc dl, [esi+5]
|
|
|
|
adc dh, [esi+4]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc dl, [esi+7]
|
|
|
|
adc dh, [esi+6]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc edx, 0
|
|
|
|
add esi, 8
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc edx, 0
|
2010-05-28 22:47:32 +02:00
|
|
|
|
|
|
|
.no_8:
|
2012-02-21 15:09:00 +01:00
|
|
|
popf
|
|
|
|
jnc .no_4
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
add dl, [esi+1]
|
|
|
|
adc dh, [esi+0]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc dl, [esi+3]
|
|
|
|
adc dh, [esi+2]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc edx, 0
|
|
|
|
add esi, 4
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-05-28 22:47:32 +02:00
|
|
|
.no_4:
|
2012-02-21 15:09:00 +01:00
|
|
|
popf
|
|
|
|
jnc .no_2
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
add dl, [esi+1]
|
|
|
|
adc dh, [esi+0]
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
adc edx, 0
|
|
|
|
inc esi
|
|
|
|
inc esi
|
2010-05-28 22:47:32 +02:00
|
|
|
|
|
|
|
.no_2:
|
2012-02-21 15:09:00 +01:00
|
|
|
popf
|
|
|
|
jnc .end
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
add dh, [esi+0]
|
|
|
|
adc edx, 0
|
2010-05-28 22:47:32 +02:00
|
|
|
.end:
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; checksum_2
|
|
|
|
;
|
|
|
|
; This function calculates the final ip/tcp/udp checksum for you
|
|
|
|
;
|
|
|
|
; IN: edx = semi-checksum
|
|
|
|
; OUT: dx = checksum (in INET byte order)
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
checksum_2:
|
|
|
|
|
2012-02-21 15:09:00 +01: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
|
2009-11-06 21:45:08 +01:00
|
|
|
.not_zero:
|
2012-02-21 15:09:00 +01:00
|
|
|
xchg dl, dh
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
DEBUGF 1,"Checksum: %x\n", dx
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;----------------------------------------------------------------
|
|
|
|
;
|
2012-12-30 21:31:11 +01:00
|
|
|
; System function to work with network devices (75)
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
;----------------------------------------------------------------
|
|
|
|
align 4
|
2012-02-21 15:09:00 +01:00
|
|
|
sys_network: ; FIXME: make default device easily accessible
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp ebx, -1
|
|
|
|
jne @f
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, [NET_RUNNING]
|
|
|
|
jmp .return
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
@@:
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp bh, MAX_NET_DEVICES ; Check if device number exists
|
|
|
|
jae .doesnt_exist
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov esi, ebx
|
|
|
|
and esi, 0x0000ff00
|
|
|
|
shr esi, 6
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp dword [esi + NET_DRV_LIST], 0 ; check if driver is running
|
|
|
|
je .doesnt_exist
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
mov eax, [esi + NET_DRV_LIST]
|
|
|
|
|
|
|
|
and ebx, 0x000000ff
|
|
|
|
cmp ebx, .number
|
|
|
|
ja .doesnt_exist
|
|
|
|
jmp dword [.table + 4*ebx]
|
|
|
|
|
|
|
|
.table:
|
|
|
|
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 .set_default ; 6
|
|
|
|
.number = ($ - .table) / 4 - 1
|
|
|
|
|
|
|
|
.get_type: ; 0 = Get device type (ethernet/token ring/...)
|
|
|
|
|
|
|
|
mov eax, [eax + NET_DEVICE.type]
|
2012-02-21 15:09:00 +01:00
|
|
|
jmp .return
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
.get_dev_name: ; 1 = Get device name
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
mov esi, [eax + NET_DEVICE.name]
|
2012-02-21 15:09:00 +01:00
|
|
|
mov edi, ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
mov ecx, 64/4 ; max length
|
|
|
|
rep movsd
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
xor eax, eax
|
|
|
|
jmp .return
|
2009-10-02 21:49:42 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
.reset: ; 2 = Reset the device
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
call [eax + NET_DEVICE.reset]
|
2012-02-21 15:09:00 +01:00
|
|
|
jmp .return
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
.stop: ; 3 = Stop driver for this device
|
2009-10-02 21:49:42 +02:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
call [eax + NET_DEVICE.unload]
|
2012-02-21 15:09:00 +01:00
|
|
|
jmp .return
|
2009-10-02 21:49:42 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
.get_ptr: ; 4 = Get driver pointer
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
jmp .return
|
2011-09-22 06:08:56 +02:00
|
|
|
|
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
.get_drv_name: ; 5 = Get driver name
|
2011-09-22 06:08:56 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
xor eax, eax
|
|
|
|
jmp .return
|
2011-09-22 06:08:56 +02:00
|
|
|
|
|
|
|
|
2012-04-17 20:41:58 +02:00
|
|
|
.set_default: ; 6 = Set default device
|
2011-09-22 06:08:56 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
call NET_set_default
|
|
|
|
jmp .return
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
.doesnt_exist:
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, -1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-02 21:49:42 +02:00
|
|
|
.return:
|
2012-04-17 20:41:58 +02:00
|
|
|
mov [esp+32], eax
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
;----------------------------------------------------------------
|
|
|
|
;
|
2012-06-03 21:28:01 +02:00
|
|
|
; System function to work with protocols (76)
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
;----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
sys_protocols:
|
2012-02-21 15:09:00 +01:00
|
|
|
cmp bh, MAX_NET_DEVICES ; Check if device number exists
|
|
|
|
jae .doesnt_exist
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov esi, ebx
|
|
|
|
and esi, 0x0000ff00
|
|
|
|
shr esi, 6 ; now we have the device num * 4 in esi
|
|
|
|
cmp [esi + NET_DRV_LIST], 0 ; check if driver is running
|
|
|
|
je .doesnt_exist
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
push .return ; return address (we will be using jumps instead of calls)
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, ebx ; set ax to protocol number
|
|
|
|
shr eax, 16 ;
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
cmp ax, API_ETH
|
|
|
|
je ETH_api
|
|
|
|
|
|
|
|
cmp ax, API_IPv4
|
2012-04-14 23:15:00 +02:00
|
|
|
je IPv4_api
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
cmp ax, API_ICMP
|
2012-04-14 23:15:00 +02:00
|
|
|
je ICMP_api
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
cmp ax, API_UDP
|
2012-04-14 23:15:00 +02:00
|
|
|
je UDP_api
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
cmp ax, API_TCP
|
2012-04-14 23:15:00 +02:00
|
|
|
je TCP_api
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-06-03 21:28:01 +02:00
|
|
|
cmp ax, API_ARP
|
2012-04-14 23:15:00 +02:00
|
|
|
je ARP_api
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-08-19 21:09:09 +02:00
|
|
|
cmp ax, API_PPPOE
|
|
|
|
je PPPoE_api
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
add esp, 4 ; if we reached here, no function was called, so we need to balance stack
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.doesnt_exist:
|
2012-02-21 15:09:00 +01:00
|
|
|
mov eax, -1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.return:
|
2012-04-14 23:15:00 +02:00
|
|
|
mov [esp+28+4], eax ; return eax value to the program
|
2012-02-21 15:09:00 +01:00
|
|
|
ret
|
2010-05-28 22:47:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
__DEBUG_LEVEL__ equ __DEBUG_LEVEL_OLD__
|