IPv4: Pre-calculate broadcast address once.

+ some stubs for IPv6

git-svn-id: svn://kolibrios.org@2731 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2012-06-03 19:28:01 +00:00
parent ead399f144
commit 3760cb5564
5 changed files with 318 additions and 172 deletions

View File

@ -1,6 +1,6 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
;; Distributed under terms of the GNU General Public License ;;
;; ;;
;; IPv4.INC ;;
@ -18,57 +18,58 @@
$Revision$
MAX_FRAGMENTS = 64
MAX_IP = MAX_NET_DEVICES
IP_MAX_INTERFACES = MAX_IP
MAX_FRAGMENTS = 64
MAX_IP = MAX_NET_DEVICES
IP_MAX_INTERFACES = MAX_IP
struct IPv4_header
VersionAndIHL db ? ; Version[0-3 bits] and IHL(header length)[4-7 bits]
TypeOfService db ? ; precedence [7-5] minimize delay [4], maximize throughput [3], maximize riliability [2] minimize momentary cost [1] and zero [0]
TotalLength dw ?
Identification dw ?
FlagsAndFragmentOffset dw ? ; Flags[0-2] and FragmentOffset[3-15]
TimeToLive db ? ;
Protocol db ?
HeaderChecksum dw ?
SourceAddress dd ?
DestinationAddress dd ?
VersionAndIHL db ? ; Version[0-3 bits] and IHL(header length)[4-7 bits]
TypeOfService db ? ; precedence [7-5] minimize delay [4], maximize throughput [3], maximize riliability [2] minimize momentary cost [1] and zero [0]
TotalLength dw ?
Identification dw ?
FlagsAndFragmentOffset dw ? ; Flags[0-2] and FragmentOffset[3-15]
TimeToLive db ? ;
Protocol db ?
HeaderChecksum dw ?
SourceAddress dd ?
DestinationAddress dd ?
ends
struct FRAGMENT_slot
ttl dw ? ; Time to live for this entry, 0 for empty slot's
id dw ? ; Identification field from IP header
SrcIP dd ? ; .. from IP header
DstIP dd ? ; .. from IP header
ptr dd ? ; Pointer to first packet
ttl dw ? ; Time to live for this entry, 0 for empty slot's
id dw ? ; Identification field from IP header
SrcIP dd ? ; .. from IP header
DstIP dd ? ; .. from IP header
ptr dd ? ; Pointer to first packet
ends
struct FRAGMENT_entry ; This structure will replace the ethernet header in fragmented ip packets
struct FRAGMENT_entry ; This structure will replace the ethernet header in fragmented ip packets
PrevPtr dd ? ; Pointer to previous fragment entry (-1 for first packet)
NextPtr dd ? ; Pointer to next fragment entry (-1 for last packet)
Owner dd ? ; Pointer to structure of driver
rb 2 ; to match ethernet header size ; TODO: fix this hack
; Ip header begins here (we will need the IP header to re-construct the complete packet)
PrevPtr dd ? ; Pointer to previous fragment entry (-1 for first packet)
NextPtr dd ? ; Pointer to next fragment entry (-1 for last packet)
Owner dd ? ; Pointer to structure of driver
rb 2 ; to match ethernet header size ;;; FIXME
; Ip header begins here (we will need the IP header to re-construct the complete packet)
ends
align 4
uglobal
IP_LIST rd MAX_IP
SUBNET_LIST rd MAX_IP
DNS_LIST rd MAX_IP
GATEWAY_LIST rd MAX_IP
IP_LIST rd MAX_IP
SUBNET_LIST rd MAX_IP
DNS_LIST rd MAX_IP
GATEWAY_LIST rd MAX_IP
BROADCAST_LIST rd MAX_IP
IP_PACKETS_TX rd MAX_IP
IP_PACKETS_RX rd MAX_IP
IP_PACKETS_TX rd MAX_IP
IP_PACKETS_RX rd MAX_IP
FRAGMENT_LIST rb MAX_FRAGMENTS * sizeof.FRAGMENT_slot
FRAGMENT_LIST rb MAX_FRAGMENTS * sizeof.FRAGMENT_slot
endg
@ -83,11 +84,7 @@ macro IPv4_init {
xor eax, eax
mov edi, IP_LIST
mov ecx, 4*MAX_IP
rep stosd
mov edi, FRAGMENT_LIST
mov ecx, sizeof.FRAGMENT_slot*MAX_FRAGMENTS/4 + 2*MAX_IP
mov ecx, 7*MAX_IP + (sizeof.FRAGMENT_slot*MAX_FRAGMENTS)/4
rep stosd
}
@ -100,20 +97,27 @@ macro IPv4_init {
;-----------------------------------------------------------------
macro IPv4_decrease_fragment_ttls {
local .loop
local .loop, .next
mov esi, FRAGMENT_LIST
mov ecx, MAX_FRAGMENTS
.loop:
cmp [esi + FRAGMENT_slot.ttl], 0
je .try_next
je .next
dec [esi + FRAGMENT_slot.ttl]
jnz .try_next
jz .died
.next:
add esi, sizeof.FRAGMENT_slot
dec ecx
jnz .loop
jmp .done
.died:
DEBUGF 1,"Fragment slot timed-out!\n"
;;; TODO: clear all entry's of timed-out slot
.try_next:
add esi, 4
loop .loop
jmp .next
.done:
}
@ -186,7 +190,7 @@ macro IPv4_checksum ptr {
;
; IPv4_input:
;
; Will check if IP Packet isnt damaged
; Will check if IPv4 Packet isnt damaged
; and call appropriate handler. (TCP/UDP/ICMP/..)
;
; It will also re-construct fragmented packets
@ -194,27 +198,23 @@ macro IPv4_checksum ptr {
; IN: Pointer to buffer in [esp]
; size of buffer in [esp+4]
; pointer to device struct in ebx
; pointer to IP header in edx
; size of IP packet in ecx
; pointer to IPv4 header in edx
; size of IPv4 packet in ecx
; OUT: /
;
;-----------------------------------------------------------------
align 4
IPv4_input: ; TODO: add code for raw sockets
IPv4_input: ; TODO: add IPv4 raw sockets support
DEBUGF 1,"IPv4_input, packet from: %u.%u.%u.%u ",\
[edx + IPv4_header.SourceAddress]:1,[edx + IPv4_header.SourceAddress + 1]:1,[edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1
[edx + IPv4_header.SourceAddress + 0]:1,[edx + IPv4_header.SourceAddress + 1]:1,\
[edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1
DEBUGF 1,"to: %u.%u.%u.%u\n",\
[edx + IPv4_header.DestinationAddress]:1,[edx + IPv4_header.DestinationAddress + 1]:1,[edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1
;-------------------------------------------
; Check if the packet still has time to live
cmp byte [edx + IPv4_header.TimeToLive], 0
je .dump
[edx + IPv4_header.DestinationAddress + 0]:1,[edx + IPv4_header.DestinationAddress + 1]:1,\
[edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1
;-------------------------------
; Now, re-calculate the checksum
; re-calculate the checksum
IPv4_checksum edx
jnz .dump ; if checksum isn't valid then dump packet
@ -229,38 +229,35 @@ IPv4_input: ; TODO: add code for raw sockets
; check if it matches local ip
mov eax, [IP_LIST+edi]
cmp [edx + IPv4_header.DestinationAddress], eax
je .ip_ok
; check for broadcast
mov eax, [SUBNET_LIST+edi]
not eax
or eax, [IP_LIST+edi]
cmp [edx + IPv4_header.DestinationAddress], eax
je .ip_ok
; or a special broadcast
cmp [edx + IPv4_header.DestinationAddress], -1
je .ip_ok
; maybe it's a multicast then
mov eax, [edx + IPv4_header.DestinationAddress]
and eax, 0xff000000
; cmp eax, 224 shl 24
; je .ip_ok
; or a loopback address
cmp eax, 127 shl 24
cmp eax, [IP_LIST+edi]
je .ip_ok
; or it's not meant for us..
; check for broadcast (IP or (not SUBNET))
DEBUGF 2,"Destination address does not match!\n"
cmp eax, [BROADCAST_LIST+edi]
je .ip_ok
; or a special broadcast (255.255.255.255)
cmp eax, 0xffffffff
je .ip_ok
; maybe it's a multicast (224.0.0.0/4)
and eax, 0x0fffffff
cmp eax, 224
je .ip_ok
; or a loopback address (127.0.0.0/8)
and eax, 0x00ffffff
cmp eax, 127
je .ip_ok
; or it's just not meant for us.. :(
DEBUGF 2,"IPv4_input - Destination address does not match!\n"
jmp .dump
;------------------------
@ -288,11 +285,11 @@ IPv4_input: ; TODO: add code for raw sockets
shl esi, 2 ;
movzx ecx, [edx + IPv4_header.TotalLength] ; Calculate length of encapsulated Packet
xchg cl , ch ;
xchg cl, ch ;
sub ecx, esi ;
lea edi, [edx + IPv4_header.SourceAddress] ; make edi ptr to source and dest IPv4 address
mov al , [edx + IPv4_header.Protocol]
mov al, [edx + IPv4_header.Protocol]
add esi, edx ; make esi ptr to data
cmp al, IP_PROTO_TCP
@ -304,10 +301,10 @@ IPv4_input: ; TODO: add code for raw sockets
cmp al, IP_PROTO_ICMP
je ICMP_input
DEBUGF 2,"unknown Internet protocol: %u\n", al
DEBUGF 2,"IPv4_input - unknown protocol: %u\n", al
.dump:
DEBUGF 2,"IP_Handler - dumping\n"
DEBUGF 2,"IPv4_input - dumping\n"
; inc [dumped_rx_count]
call kernel_free
add esp, 4 ; pop (balance stack)
@ -499,16 +496,10 @@ IPv4_input: ; TODO: add code for raw sockets
mov edx, eax
mov [edx + IPv4_header.TotalLength], cx
add esp, 8
xchg cl, ch
push ecx
xchg cl, ch ;
push ecx ;;;;
push eax ;;;;
; mov esi, edx ; This prints the IP packet to the debug board (usefull when using serial output debug..)
; ;
; packet_to_debug
push eax
jmp .handle_it ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr
.destroy_slot_pop:
@ -527,7 +518,7 @@ IPv4_input: ; TODO: add code for raw sockets
; find fragment slot
;
; IN: pointer to fragmented packet in edx
; OUT: pointer to slot in edi, -1 on error
; OUT: pointer to slot in esi, -1 on error
;
;-----------------------------------------------------------------
align 4
@ -885,7 +876,8 @@ IPv4_dest_to_dev:
je .found_it
.next:
add edi, 4
loop .loop
dec ecx
jnz .loop
.invalid:
xor edi, edi ; if none found, use device 0 as default device
@ -967,6 +959,13 @@ IPv4_api:
.write_ip:
mov [IP_LIST + eax], ecx
; pre-calculate the local broadcast address
mov ebx, [SUBNET_LIST + eax]
not ebx
or ecx, ebx
mov [BROADCAST_LIST + eax], ecx
xor eax, eax
ret
@ -985,6 +984,13 @@ IPv4_api:
.write_subnet:
mov [SUBNET_LIST + eax], ecx
; pre-calculate the local broadcast address
mov ebx, [IP_LIST + eax]
not ecx
or ecx, ebx
mov [BROADCAST_LIST + eax], ecx
xor eax, eax
ret

View File

@ -0,0 +1,109 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Copyright (C) KolibriOS team 2012. All rights reserved. ;;
;; Distributed under terms of the GNU General Public License ;;
;; ;;
;; IPv6.INC ;;
;; ;;
;; Part of the tcp/ip network stack for KolibriOS ;;
;; ;;
;; Written by hidnplayr@kolibrios.org ;;
;; ;;
;; GNU GENERAL PUBLIC LICENSE ;;
;; Version 2, June 1991 ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$Revision$
struct IPv6_header
VersionTrafficFlow dd ? ; Version[0-3], Traffic class[4-11], Flow Label [12-31]
PayloadLength dw ? ; 16 bits, unsigned length of payload (extension headers are part of this)
NextHeader db ? ; Values are same as in IPv4 'Protocol' field
HopLimit db ? ; Decremented by every node, packet is discarded when it reaches 0
SourceAddress rd 4 ; 128-bit addresses
DestinationAddress rd 4 ;
ends
;-----------------------------------------------------------------
;
; IPv6_input:
;
; Will check if IPv6 Packet isnt damaged
; and call appropriate handler. (TCP/UDP/ICMP/..)
;
; It will also re-construct fragmented packets
;
; IN: Pointer to buffer in [esp]
; size of buffer in [esp+4]
; pointer to device struct in ebx
; pointer to IPv6 header in edx
; size of IPv6 packet in ecx
; OUT: /
;
;-----------------------------------------------------------------
align 4
IPv6_input:
DEBUGF 1,"IPv6_input\nfrom: %x:%x:%x:%x:%x:%x:%x:%x\n",\
[edx + IPv6_header.SourceAddress + 0]:4,[edx + IPv6_header.SourceAddress + 2]:4,\
[edx + IPv6_header.SourceAddress + 4]:4,[edx + IPv6_header.SourceAddress + 6]:4,\
[edx + IPv6_header.SourceAddress + 8]:4,[edx + IPv6_header.SourceAddress + 10]:4,\
[edx + IPv6_header.SourceAddress + 12]:4,[edx + IPv6_header.SourceAddress + 14]:4
DEBUGF 1,"to: %x:%x:%x:%x:%x:%x:%x:%x\n",\
[edx + IPv6_header.DestinationAddress + 0]:4,[edx + IPv6_header.DestinationAddress + 2]:4,\
[edx + IPv6_header.DestinationAddress + 4]:4,[edx + IPv6_header.DestinationAddress + 6]:4,\
[edx + IPv6_header.DestinationAddress + 8]:4,[edx + IPv6_header.DestinationAddress + 10]:4,\
[edx + IPv6_header.DestinationAddress + 12]:4,[edx + IPv6_header.DestinationAddress + 14]:4
sub ecx, sizeof.IPv6_header
jb .dump
movzx eax, [edx + IPv6.PayloadLength]
xchg al, ah
cmp eax, ecx
jb .dump
;-------------------------------------------------------------------
; No, it's just a regular IP packet, pass it to the higher protocols
.handle_it:
movzx esi, [edx + IPv6_header.VersionAndIHL] ; Calculate Header length by using IHL field
and esi, 0x0000000f ;
shl esi, 2 ;
movzx ecx, [edx + IPv6_header.TotalLength] ; Calculate length of encapsulated Packet
xchg cl, ch ;
sub ecx, esi ;
lea edi, [edx + IPv6_header.SourceAddress] ; make edi ptr to source and dest IPv6 address
mov al, [edx + IPv6_header.Protocol]
add esi, edx ; make esi ptr to data
; cmp al, IP_PROTO_TCP
; je TCP_input
; cmp al, IP_PROTO_UDP
; je UDP_input
; cmp al, IP_PROTO_ICMP
; je ICMP_input
DEBUGF 2,"IPv6_input - unknown protocol: %u\n", al
.dump:
DEBUGF 2,"IPv6_input - dumping\n"
add esp, 4
call KernelFree
ret

View File

@ -16,6 +16,8 @@
$Revision$
ETH_FRAME_MINIMUM = 60
struct ETH_header
DstMAC dp ? ; destination MAC-address
@ -24,8 +26,6 @@ struct ETH_header
ends
ETH_FRAME_MINIMUM = 60
struct ETH_DEVICE NET_DEVICE
set_mode dd ?
@ -69,7 +69,7 @@ ETH_input:
sub ecx, sizeof.ETH_header
lea edx, [eax + sizeof.ETH_header]
mov ax , [eax + ETH_header.Type]
mov ax, [eax + ETH_header.Type]
cmp ax, ETHER_IPv4
je IPv4_input
@ -77,9 +77,15 @@ ETH_input:
cmp ax, ETHER_ARP
je ARP_input
; cmp ax, ETHER_IPv6
; je IPv6_input
; cmp ax, ETHER_PPP_DISCOVERY
; je PPPoE_discovery_input
; cmp ax, ETHER_PPP_SESSION
; je PPPoE_session_input
DEBUGF 2,"Unknown ethernet packet type %x\n", ax
.dump:

View File

@ -919,10 +919,26 @@ SOCKET_get_opt:
;-----------------------------------------------------------------
;
; SOCKET_set_options
;
; IN: ecx = socket number
; edx = pointer to the options:
; dd level, optname, optval, optlen
; OUT: -1 on error
;
;-----------------------------------------------------------------
align 4
SOCKET_set_opt:
DEBUGF 1,"SOCKET_set_opt\n"
call SOCKET_num_to_ptr
jz s_error
ret

View File

@ -31,79 +31,87 @@ uglobal
net_tmr_count dw ?
endg
MAX_NET_DEVICES = 16
MAX_NET_DEVICES = 16
MIN_EPHEMERAL_PORT = 49152
MAX_EPHEMERAL_PORT = 61000
MIN_EPHEMERAL_PORT = 49152
MAX_EPHEMERAL_PORT = 61000
; Ethernet protocol numbers
ETHER_ARP = 0x0608
ETHER_IPv4 = 0x0008
ETHER_PPP_DISCOVERY = 0x6388
ETHER_PPP_SESSION = 0x6488
ETHER_ARP = 0x0608
ETHER_IPv4 = 0x0008
ETHER_IPv6 = 0xDD86
ETHER_PPP_DISCOVERY = 0x6388
ETHER_PPP_SESSION = 0x6488
;Protocol family
AF_UNSPEC = 0
AF_UNIX = 1
AF_INET4 = 2
AF_INET6 = 10
AF_UNSPEC = 0
AF_UNIX = 1
AF_INET4 = 2
AF_INET6 = 10
; Internet protocol numbers
IP_PROTO_IP = 0
IP_PROTO_ICMP = 1
IP_PROTO_TCP = 6
IP_PROTO_UDP = 17
IP_PROTO_IP = 0
IP_PROTO_ICMP = 1
IP_PROTO_TCP = 6
IP_PROTO_UDP = 17
; Socket types
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
; 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_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
; Socket States
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
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
SS_ASYNC = 0x100 ; async i/o notify
SS_ISCONFIRMING = 0x200 ; deciding to accept connection req
SS_MORETOCOME = 0x400
SS_ASYNC = 0x100 ; async i/o notify
SS_ISCONFIRMING = 0x200 ; deciding to accept connection req
SS_MORETOCOME = 0x400
SOCKET_MAXDATA = 4096*32 ; must be 4096*(power of 2) where 'power of 2' is at least 8
SOCKET_MAXDATA = 4096*32 ; must be 4096*(power of 2) where 'power of 2' is at least 8
; Network driver types
NET_TYPE_ETH = 1
NET_TYPE_SLIP = 2
NET_TYPE_ETH = 1
NET_TYPE_SLIP = 2
MAX_backlog = 20 ; maximum backlog for stream sockets
MAX_backlog = 20 ; maximum backlog for stream sockets
; Error Codes
ENOBUFS = 55
ECONNREFUSED = 61
ECONNRESET = 52
ETIMEDOUT = 60
ECONNABORTED = 53
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
struct NET_DEVICE
@ -159,6 +167,7 @@ include "ethernet.inc"
include "ARP.inc"
include "IPv4.inc"
;include "IPv6.inc"
include "icmp.inc"
include "udp.inc"
@ -197,10 +206,10 @@ stack_init:
mov ecx, (MAX_NET_DEVICES + 2)
rep stosd
; SLIP_init
; PPPOE_init
IPv4_init
; IPv6_init
ICMP_init
ARP_init
@ -661,7 +670,7 @@ sys_network: ; FIXME: make default device easily accessible
;----------------------------------------------------------------
;
; System function to work with protocols (75)
; System function to work with protocols (76)
;
;----------------------------------------------------------------
align 4
@ -680,25 +689,25 @@ sys_protocols:
mov eax, ebx ; set ax to protocol number
shr eax, 16 ;
cmp ax, IP_PROTO_IP
je IPv4_api
cmp ax, IP_PROTO_ICMP
je ICMP_api
cmp ax, IP_PROTO_UDP
je UDP_api
cmp ax, IP_PROTO_TCP
je TCP_api
cmp ax, ETHER_ARP
je ARP_api
cmp ax, 1337 ;;;;;
cmp ax, API_ETH
je ETH_api
; cmp ax, API_PPPoE
cmp ax, API_IPv4
je IPv4_api
cmp ax, API_ICMP
je ICMP_api
cmp ax, API_UDP
je UDP_api
cmp ax, API_TCP
je TCP_api
cmp ax, API_ARP
je ARP_api
; cmp ax, API_PPPOE
; je PPPoE_api
add esp, 4 ; if we reached here, no function was called, so we need to balance stack