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 ;;
@ -52,7 +52,7 @@ struct FRAGMENT_entry ; This structure will replace the etherne
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
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
@ -64,6 +64,7 @@ uglobal
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
@ -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
;------------------------
@ -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 ?
@ -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

@ -39,6 +39,7 @@ MAX_EPHEMERAL_PORT = 61000
; Ethernet protocol numbers
ETHER_ARP = 0x0608
ETHER_IPv4 = 0x0008
ETHER_IPv6 = 0xDD86
ETHER_PPP_DISCOVERY = 0x6388
ETHER_PPP_SESSION = 0x6488
@ -103,7 +104,14 @@ 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