2009-09-17 15:55:38 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2012-06-03 23:28:01 +04:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
|
2009-09-17 15:55:38 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
2009-11-06 23:45:08 +03:00
|
|
|
;; IPv4.INC ;;
|
2009-09-17 15:55:38 +04:00
|
|
|
;; ;;
|
2012-12-28 23:56:52 +04:00
|
|
|
;; Part of the TCP/IP network stack for KolibriOS ;;
|
2009-09-17 15:55:38 +04:00
|
|
|
;; ;;
|
|
|
|
;; Based on the work of [Johnny_B] and [smb] ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 18:39:59 +04:00
|
|
|
$Revision$
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
MAX_FRAGMENTS = 64
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
struct IPv4_header
|
2011-11-09 03:06:26 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
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 ?
|
2011-11-09 03:06:26 +04:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
ends
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
struct FRAGMENT_slot
|
2011-11-09 03:06:26 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
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
|
2011-11-09 03:06:26 +04:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
ends
|
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
struct FRAGMENT_entry ; This structure will replace the ethernet header in fragmented ip packets
|
2011-11-09 03:06:26 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
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)
|
2009-09-17 15:55:38 +04:00
|
|
|
ends
|
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2010-07-28 02:08:21 +04:00
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
|
2012-08-15 20:15:57 +04:00
|
|
|
IP_LIST rd MAX_NET_DEVICES
|
|
|
|
SUBNET_LIST rd MAX_NET_DEVICES
|
|
|
|
DNS_LIST rd MAX_NET_DEVICES
|
|
|
|
GATEWAY_LIST rd MAX_NET_DEVICES
|
|
|
|
BROADCAST_LIST rd MAX_NET_DEVICES
|
2010-07-28 02:08:21 +04:00
|
|
|
|
2013-02-27 00:17:02 +04:00
|
|
|
IP_packets_tx rd MAX_NET_DEVICES
|
|
|
|
IP_packets_rx rd MAX_NET_DEVICES
|
|
|
|
IP_packets_dumped rd MAX_NET_DEVICES
|
2010-07-28 02:08:21 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
FRAGMENT_LIST rb MAX_FRAGMENTS * sizeof.FRAGMENT_slot
|
2010-07-28 02:08:21 +04:00
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; IPv4_init
|
|
|
|
;
|
|
|
|
; This function resets all IP variables
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2011-11-12 19:39:15 +04:00
|
|
|
macro IPv4_init {
|
2010-07-28 02:08:21 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
xor eax, eax
|
|
|
|
mov edi, IP_LIST
|
2012-08-15 20:15:57 +04:00
|
|
|
mov ecx, 7*MAX_NET_DEVICES + (sizeof.FRAGMENT_slot*MAX_FRAGMENTS)/4
|
2011-11-12 19:39:15 +04:00
|
|
|
rep stosd
|
2010-07-28 02:08:21 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Decrease TimeToLive of all fragment slots
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
macro IPv4_decrease_fragment_ttls {
|
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
local .loop, .next
|
2010-07-28 02:08:21 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov esi, FRAGMENT_LIST
|
|
|
|
mov ecx, MAX_FRAGMENTS
|
2010-07-28 02:08:21 +04:00
|
|
|
.loop:
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp [esi + FRAGMENT_slot.ttl], 0
|
2012-06-03 23:28:01 +04:00
|
|
|
je .next
|
2011-11-12 19:39:15 +04:00
|
|
|
dec [esi + FRAGMENT_slot.ttl]
|
2012-06-03 23:28:01 +04:00
|
|
|
jz .died
|
|
|
|
.next:
|
|
|
|
add esi, sizeof.FRAGMENT_slot
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
jmp .done
|
|
|
|
|
|
|
|
.died:
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4 Fragment slot timed-out!\n"
|
2010-07-28 02:08:21 +04:00
|
|
|
;;; TODO: clear all entry's of timed-out slot
|
2012-06-03 23:28:01 +04:00
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.done:
|
2010-07-28 02:08:21 +04:00
|
|
|
}
|
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
macro IPv4_checksum ptr {
|
|
|
|
|
2011-11-12 23:20:33 +04:00
|
|
|
; This is the fast procedure to create or check an IP header without options
|
2010-07-27 22:53:38 +04:00
|
|
|
; To create a new checksum, the checksum field must be set to 0 before computation
|
|
|
|
; To check an existing checksum, leave the checksum as is, and it will be 0 after this procedure, if it was correct
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push ebx
|
|
|
|
xor ebx, ebx
|
|
|
|
add bl, [ptr+1]
|
|
|
|
adc bh, [ptr+0]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+3]
|
|
|
|
adc bh, [ptr+2]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+5]
|
|
|
|
adc bh, [ptr+4]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+7]
|
|
|
|
adc bh, [ptr+6]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+9]
|
|
|
|
adc bh, [ptr+8]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
|
|
|
; we skip 11th and 12th byte, they are the checksum bytes and should be 0 for re-calculation
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+13]
|
|
|
|
adc bh, [ptr+12]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+15]
|
|
|
|
adc bh, [ptr+14]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+17]
|
|
|
|
adc bh, [ptr+16]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc bl, [ptr+19]
|
|
|
|
adc bh, [ptr+18]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
adc ebx, 0
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push ecx
|
|
|
|
mov ecx, ebx
|
|
|
|
shr ecx, 16
|
|
|
|
and ebx, 0xffff
|
|
|
|
add ebx, ecx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, ebx
|
|
|
|
shr ecx, 16
|
|
|
|
add ebx, ecx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
not bx
|
|
|
|
jnz .not_zero
|
|
|
|
dec bx
|
2010-07-27 22:53:38 +04:00
|
|
|
.not_zero:
|
2011-11-12 19:39:15 +04:00
|
|
|
xchg bl, bh
|
|
|
|
pop ecx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
neg word [ptr+10] ; zero will stay zero so we just get the checksum
|
|
|
|
add word [ptr+10], bx ; , else we will get (new checksum - old checksum) in the end, wich should be 0 :)
|
|
|
|
pop ebx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2010-07-27 22:53:38 +04:00
|
|
|
; IPv4_input:
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2012-06-03 23:28:01 +04:00
|
|
|
; Will check if IPv4 Packet isnt damaged
|
2009-09-17 15:55:38 +04:00
|
|
|
; 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
|
2012-06-03 23:28:01 +04:00
|
|
|
; pointer to IPv4 header in edx
|
|
|
|
; size of IPv4 packet in ecx
|
2009-09-17 15:55:38 +04:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
2012-06-03 23:28:01 +04:00
|
|
|
IPv4_input: ; TODO: add IPv4 raw sockets support
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_input, packet from: %u.%u.%u.%u ",\
|
2012-06-03 23:28:01 +04:00
|
|
|
[edx + IPv4_header.SourceAddress + 0]:1,[edx + IPv4_header.SourceAddress + 1]:1,\
|
|
|
|
[edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"to: %u.%u.%u.%u\n",\
|
2012-06-03 23:28:01 +04:00
|
|
|
[edx + IPv4_header.DestinationAddress + 0]:1,[edx + IPv4_header.DestinationAddress + 1]:1,\
|
|
|
|
[edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1
|
2010-05-29 00:47:32 +04:00
|
|
|
|
|
|
|
;-------------------------------
|
2012-06-03 23:28:01 +04:00
|
|
|
; re-calculate the checksum
|
2009-11-06 23:45:08 +03:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
IPv4_checksum edx
|
|
|
|
jnz .dump ; if checksum isn't valid then dump packet
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Checksum ok\n"
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
;-----------------------------------
|
|
|
|
; Check if destination IP is correct
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
call NET_ptr_to_num
|
|
|
|
shl edi, 2
|
2009-11-08 21:00:01 +03:00
|
|
|
|
2013-02-19 20:56:54 +04:00
|
|
|
; check if it matches local ip (Using RFC1122 strong end system model)
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
mov eax, [edx + IPv4_header.DestinationAddress]
|
2013-02-27 00:17:02 +04:00
|
|
|
cmp eax, [IP_LIST + edi]
|
2011-11-12 19:39:15 +04:00
|
|
|
je .ip_ok
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
; check for broadcast (IP or (not SUBNET))
|
2010-07-12 03:13:12 +04:00
|
|
|
|
2013-02-27 00:17:02 +04:00
|
|
|
cmp eax, [BROADCAST_LIST + edi]
|
2011-11-12 19:39:15 +04:00
|
|
|
je .ip_ok
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
; or a special broadcast (255.255.255.255)
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
cmp eax, 0xffffffff
|
2011-11-12 19:39:15 +04:00
|
|
|
je .ip_ok
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
; maybe it's a multicast (224.0.0.0/4)
|
2010-07-15 22:54:19 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
and eax, 0x0fffffff
|
|
|
|
cmp eax, 224
|
|
|
|
je .ip_ok
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
; or a loopback address (127.0.0.0/8)
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
and eax, 0x00ffffff
|
|
|
|
cmp eax, 127
|
2011-11-12 19:39:15 +04:00
|
|
|
je .ip_ok
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
; or it's just not meant for us.. :(
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 2,"IPv4_input: Destination address does not match!\n"
|
2011-11-12 19:39:15 +04:00
|
|
|
jmp .dump
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
;------------------------
|
|
|
|
; Now we can update stats
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
.ip_ok:
|
2013-02-27 00:17:02 +04:00
|
|
|
inc [IP_packets_rx + edi]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
;----------------------------------
|
|
|
|
; Check if the packet is fragmented
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
test [edx + IPv4_header.FlagsAndFragmentOffset], 1 shl 5 ; Is 'more fragments' flag set ?
|
|
|
|
jnz .has_fragments ; If so, we definately have a fragmented packet
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
test [edx + IPv4_header.FlagsAndFragmentOffset], 0xff1f ; If flag is not set, but there is a fragment offset, the packet is last in series of fragmented packets
|
|
|
|
jnz .is_last_fragment
|
2010-05-29 00:47:32 +04:00
|
|
|
|
|
|
|
;-------------------------------------------------------------------
|
|
|
|
; No, it's just a regular IP packet, pass it to the higher protocols
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
.handle_it: ; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
movzx esi, [edx + IPv4_header.VersionAndIHL] ; Calculate Header length by using IHL field
|
|
|
|
and esi, 0x0000000f ;
|
|
|
|
shl esi, 2 ;
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
movzx ecx, [edx + IPv4_header.TotalLength] ; Calculate length of encapsulated Packet
|
2012-06-03 23:28:01 +04:00
|
|
|
xchg cl, ch ;
|
2011-11-12 19:39:15 +04:00
|
|
|
sub ecx, esi ;
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
lea edi, [edx + IPv4_header.SourceAddress] ; make edi ptr to source and dest IPv4 address
|
2012-06-03 23:28:01 +04:00
|
|
|
mov al, [edx + IPv4_header.Protocol]
|
2011-11-13 00:05:07 +04:00
|
|
|
add esi, edx ; make esi ptr to data
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-04-17 22:41:58 +04:00
|
|
|
cmp al, IP_PROTO_TCP
|
2011-11-12 19:39:15 +04:00
|
|
|
je TCP_input
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-04-17 22:41:58 +04:00
|
|
|
cmp al, IP_PROTO_UDP
|
2011-11-12 19:39:15 +04:00
|
|
|
je UDP_input
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-04-17 22:41:58 +04:00
|
|
|
cmp al, IP_PROTO_ICMP
|
2011-11-12 19:39:15 +04:00
|
|
|
je ICMP_input
|
|
|
|
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 2,"IPv4_input: unknown protocol %u\n", al
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.dump:
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 1,"IPv4_input: dumping\n"
|
2013-02-27 00:17:02 +04:00
|
|
|
inc [IP_packets_dumped] ; FIXME: use correct interface
|
2011-11-12 19:39:15 +04:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4 ; pop (balance stack)
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
;---------------------------
|
|
|
|
; Fragmented packet handler
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.has_fragments:
|
2011-11-12 19:39:15 +04:00
|
|
|
movzx eax, [edx + IPv4_header.FlagsAndFragmentOffset]
|
2012-08-15 20:15:57 +04:00
|
|
|
xchg al, ah
|
|
|
|
shl ax, 3
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: fragmented packet offset=%u id=%x\n", ax, [edx + IPv4_header.Identification]:4
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-08-15 20:15:57 +04:00
|
|
|
test ax, ax ; Is this the first packet of the fragment?
|
2011-11-12 19:39:15 +04:00
|
|
|
jz .is_first_fragment
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
;-------------------------------------------------------
|
|
|
|
; We have a fragmented IP packet, but it's not the first
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Middle fragment packet received!\n"
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
call IPv4_find_fragment_slot
|
|
|
|
cmp esi, -1
|
|
|
|
je .dump
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [esi + FRAGMENT_slot.ttl], 15 ; Reset the ttl
|
|
|
|
mov esi, [esi + FRAGMENT_slot.ptr]
|
|
|
|
or edi, -1
|
|
|
|
.find_last_entry: ; The following routine will try to find the last entry
|
|
|
|
cmp edi, [esi + FRAGMENT_entry.PrevPtr]
|
|
|
|
jne .destroy_slot ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
|
|
|
|
mov edi, esi
|
|
|
|
mov esi, [esi + FRAGMENT_entry.NextPtr]
|
|
|
|
cmp esi, -1
|
|
|
|
jne .find_last_entry
|
|
|
|
; We found the last entry (pointer is now in edi)
|
|
|
|
; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
pop eax ; pointer to packet
|
|
|
|
mov [edi + FRAGMENT_entry.NextPtr], eax ; update pointer of previous entry to the new entry
|
|
|
|
mov [eax + FRAGMENT_entry.NextPtr], -1
|
|
|
|
mov [eax + FRAGMENT_entry.PrevPtr], edi
|
|
|
|
mov [eax + FRAGMENT_entry.Owner], ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
add esp, 4
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
;------------------------------------
|
|
|
|
; We have received the first fragment
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.is_first_fragment:
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: First fragment packet received!\n"
|
2011-11-12 19:39:15 +04:00
|
|
|
; try to locate a free slot..
|
|
|
|
mov ecx, MAX_FRAGMENTS
|
|
|
|
mov esi, FRAGMENT_LIST
|
2010-05-29 00:47:32 +04:00
|
|
|
.find_free_slot:
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp word [esi + FRAGMENT_slot.ttl], 0
|
|
|
|
je .found_free_slot
|
|
|
|
add esi, sizeof.FRAGMENT_slot
|
|
|
|
loop .find_free_slot
|
|
|
|
jmp .dump ; If no free slot was found, dump the packet
|
|
|
|
|
|
|
|
.found_free_slot: ; We found a free slot, let's fill in the FRAGMENT_slot structure
|
|
|
|
mov [esi + FRAGMENT_slot.ttl], 15 ; RFC recommends 15 secs as ttl
|
2012-08-15 20:15:57 +04:00
|
|
|
mov ax, [edx + IPv4_header.Identification]
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [esi + FRAGMENT_slot.id], ax
|
2012-11-01 16:33:15 +04:00
|
|
|
mov eax, [edx + IPv4_header.SourceAddress]
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [esi + FRAGMENT_slot.SrcIP], eax
|
|
|
|
mov eax, [edx + IPv4_header.DestinationAddress]
|
|
|
|
mov [esi + FRAGMENT_slot.DstIP], eax
|
|
|
|
pop eax
|
|
|
|
mov [esi + FRAGMENT_slot.ptr], eax
|
|
|
|
; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
|
|
|
|
mov [eax + FRAGMENT_entry.NextPtr], -1
|
|
|
|
mov [eax + FRAGMENT_entry.PrevPtr], -1
|
|
|
|
mov [eax + FRAGMENT_entry.Owner], ebx
|
|
|
|
|
|
|
|
add esp, 4 ; balance stack and exit
|
|
|
|
ret
|
2010-05-29 00:47:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------
|
|
|
|
; We have received the last fragment
|
|
|
|
|
|
|
|
.is_last_fragment:
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Last fragment packet received!\n"
|
2010-05-29 00:47:32 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
call IPv4_find_fragment_slot
|
|
|
|
cmp esi, -1
|
|
|
|
je .dump
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov esi, [esi + FRAGMENT_slot.ptr] ; We found the first entry, let's calculate total size of the packet in eax, so we can allocate a buffer
|
|
|
|
push esi
|
|
|
|
xor eax, eax
|
|
|
|
or edi, -1
|
2010-05-29 00:47:32 +04:00
|
|
|
|
|
|
|
.count_bytes:
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp [esi + FRAGMENT_entry.PrevPtr], edi
|
|
|
|
jne .destroy_slot_pop ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
|
2013-02-17 21:58:53 +04:00
|
|
|
mov cx, [esi + sizeof.FRAGMENT_entry + IPv4_header.TotalLength] ; Add total length
|
2011-11-12 19:39:15 +04:00
|
|
|
xchg cl, ch
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Packet size=%u\n", cx
|
2011-11-12 19:39:15 +04:00
|
|
|
add ax, cx
|
2013-02-17 21:58:53 +04:00
|
|
|
movzx cx, [esi + sizeof.FRAGMENT_entry + IPv4_header.VersionAndIHL] ; Sub Header length
|
2011-11-12 19:39:15 +04:00
|
|
|
and cx, 0x000F
|
|
|
|
shl cx, 2
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Header size=%u\n", cx
|
2011-11-12 19:39:15 +04:00
|
|
|
sub ax, cx
|
|
|
|
mov edi, esi
|
|
|
|
mov esi, [esi + FRAGMENT_entry.NextPtr]
|
|
|
|
cmp esi, -1
|
|
|
|
jne .count_bytes
|
|
|
|
|
|
|
|
mov esi, [esp+4]
|
2013-02-17 21:58:53 +04:00
|
|
|
mov [edi + FRAGMENT_entry.NextPtr], esi ; Add this packet to the chain, this simplifies the following code
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [esi + FRAGMENT_entry.NextPtr], -1
|
|
|
|
mov [esi + FRAGMENT_entry.PrevPtr], edi
|
|
|
|
mov [esi + FRAGMENT_entry.Owner], ebx
|
|
|
|
|
2013-02-17 21:58:53 +04:00
|
|
|
mov cx, [edx + IPv4_header.TotalLength] ; Note: This time we dont substract Header length
|
2012-08-15 20:15:57 +04:00
|
|
|
xchg cl, ch
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Packet size=%u\n", cx
|
2012-08-15 20:15:57 +04:00
|
|
|
add ax, cx
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Total Received data size=%u\n", eax
|
2011-11-12 19:39:15 +04:00
|
|
|
|
|
|
|
push eax
|
2012-08-15 20:15:57 +04:00
|
|
|
mov ax, [edx + IPv4_header.FlagsAndFragmentOffset]
|
|
|
|
xchg al, ah
|
|
|
|
shl ax, 3
|
|
|
|
add cx, ax
|
2011-11-12 19:39:15 +04:00
|
|
|
pop eax
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Total Fragment size=%u\n", ecx
|
2011-11-12 19:39:15 +04:00
|
|
|
|
|
|
|
cmp ax, cx
|
|
|
|
jne .destroy_slot_pop
|
|
|
|
|
|
|
|
push eax
|
|
|
|
push eax
|
|
|
|
call kernel_alloc
|
|
|
|
test eax, eax
|
|
|
|
je .destroy_slot_pop ; If we dont have enough space to allocate the buffer, discard all packets in slot
|
|
|
|
mov edx, [esp+4] ; Get pointer to first fragment entry back in edx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.rebuild_packet_loop:
|
2012-08-15 20:15:57 +04:00
|
|
|
movzx ecx, [edx + sizeof.FRAGMENT_entry + IPv4_header.FlagsAndFragmentOffset] ; Calculate the fragment offset
|
|
|
|
xchg cl, ch ; intel byte order
|
|
|
|
shl cx, 3 ; multiply by 8 and clear first 3 bits
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Fragment offset=%u\n", cx
|
2011-11-12 19:39:15 +04:00
|
|
|
|
|
|
|
lea edi, [eax + ecx] ; Notice that edi will be equal to eax for first fragment
|
2012-08-15 20:15:57 +04:00
|
|
|
movzx ebx, [edx + sizeof.FRAGMENT_entry + IPv4_header.VersionAndIHL] ; Find header size (in ebx) of fragment
|
|
|
|
and bx, 0x000F ;
|
|
|
|
shl bx, 2 ;
|
2011-11-12 19:39:15 +04:00
|
|
|
|
2012-08-15 20:15:57 +04:00
|
|
|
lea esi, [edx + sizeof.FRAGMENT_entry] ; Set esi to the correct begin of fragment
|
|
|
|
movzx ecx, [edx + sizeof.FRAGMENT_entry + IPv4_header.TotalLength] ; Calculate total length of fragment
|
2011-11-12 19:39:15 +04:00
|
|
|
xchg cl, ch ; intel byte order
|
|
|
|
|
|
|
|
cmp edi, eax ; Is this packet the first fragment ?
|
|
|
|
je .first_fragment
|
|
|
|
sub cx, bx ; If not, dont copy the header
|
|
|
|
add esi, ebx ;
|
2010-05-29 00:47:32 +04:00
|
|
|
.first_fragment:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push cx ; First copy dword-wise, then byte-wise
|
|
|
|
shr cx, 2 ;
|
|
|
|
rep movsd ;
|
|
|
|
pop cx ;
|
|
|
|
and cx, 3 ;
|
|
|
|
rep movsb ;
|
|
|
|
|
|
|
|
push eax
|
|
|
|
push edx ; Push pointer to fragment onto stack
|
|
|
|
mov ebx, [edx + FRAGMENT_entry.Owner] ; we need to remeber the owner, in case this is the last packet
|
|
|
|
mov edx, [edx + FRAGMENT_entry.NextPtr] ; Set edx to the next pointer
|
|
|
|
call kernel_free ; free the previous fragment buffer (this uses the value from stack)
|
|
|
|
pop eax
|
|
|
|
cmp edx, -1 ; Check if it is last fragment in chain
|
|
|
|
jne .rebuild_packet_loop
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
xchg cl, ch
|
|
|
|
mov edx, eax
|
|
|
|
mov [edx + IPv4_header.TotalLength], cx
|
|
|
|
add esp, 8
|
2012-06-03 23:28:01 +04:00
|
|
|
xchg cl, ch
|
|
|
|
push ecx
|
2011-11-12 19:39:15 +04:00
|
|
|
|
2012-06-03 23:28:01 +04:00
|
|
|
push eax
|
2011-11-12 19:39:15 +04:00
|
|
|
jmp .handle_it ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr
|
2009-11-06 23:45:08 +03:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.destroy_slot_pop:
|
2011-11-12 19:39:15 +04:00
|
|
|
add esp, 4
|
2010-05-29 00:47:32 +04:00
|
|
|
.destroy_slot:
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_input: Destroy fragment slot!\n"
|
2011-11-12 19:39:15 +04:00
|
|
|
; TODO!
|
|
|
|
jmp .dump
|
2009-11-06 23:45:08 +03:00
|
|
|
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2009-09-18 18:31:34 +04:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; find fragment slot
|
|
|
|
;
|
2010-07-12 03:13:12 +04:00
|
|
|
; IN: pointer to fragmented packet in edx
|
2012-06-03 23:28:01 +04:00
|
|
|
; OUT: pointer to slot in esi, -1 on error
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2010-05-29 00:47:32 +04:00
|
|
|
align 4
|
|
|
|
IPv4_find_fragment_slot:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
;;; TODO: the RFC says we should check protocol number too
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push eax ebx ecx edx
|
2012-08-15 20:15:57 +04:00
|
|
|
mov ax, [edx + IPv4_header.Identification]
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, MAX_FRAGMENTS
|
|
|
|
mov esi, FRAGMENT_LIST
|
|
|
|
mov ebx, [edx + IPv4_header.SourceAddress]
|
|
|
|
mov edx, [edx + IPv4_header.DestinationAddress]
|
2009-09-17 15:55:38 +04:00
|
|
|
.find_slot:
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp [esi + FRAGMENT_slot.id], ax
|
|
|
|
jne .try_next
|
|
|
|
cmp [esi + FRAGMENT_slot.SrcIP], ebx
|
|
|
|
jne .try_next
|
|
|
|
cmp [esi + FRAGMENT_slot.DstIP], edx
|
|
|
|
je .found_slot
|
2009-09-17 15:55:38 +04:00
|
|
|
.try_next:
|
2011-11-12 19:39:15 +04:00
|
|
|
add esi, sizeof.FRAGMENT_slot
|
|
|
|
loop .find_slot
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-08-15 20:15:57 +04:00
|
|
|
or esi, -1
|
2009-09-17 15:55:38 +04:00
|
|
|
.found_slot:
|
2011-11-12 19:39:15 +04:00
|
|
|
pop edx ecx ebx eax
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
;------------------------------------------------------------------
|
|
|
|
;
|
2010-07-27 22:53:38 +04:00
|
|
|
; IPv4_output
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
|
|
|
; IN: eax = dest ip
|
2012-07-18 01:36:34 +04:00
|
|
|
; ebx = output device ptr/0 for automatic choice
|
2009-09-17 15:55:38 +04:00
|
|
|
; ecx = data length
|
2012-07-18 01:36:34 +04:00
|
|
|
; edx = source ip
|
2010-07-27 22:53:38 +04:00
|
|
|
; di = TTL shl 8 + protocol
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-06 23:45:08 +03:00
|
|
|
; OUT: eax = pointer to buffer start
|
|
|
|
; ebx = pointer to device struct (needed for sending procedure)
|
2009-09-17 15:55:38 +04:00
|
|
|
; ecx = unchanged (packet size of embedded data)
|
2009-11-06 23:45:08 +03:00
|
|
|
; edx = size of complete buffer
|
2010-07-12 03:13:12 +04:00
|
|
|
; edi = pointer to start of data (0 on error)
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2010-07-12 03:13:12 +04:00
|
|
|
;------------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
align 4
|
2010-07-27 22:53:38 +04:00
|
|
|
IPv4_output:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-04-03 20:34:37 +04:00
|
|
|
DEBUGF 1,"IPv4_output: size=%u\n", ecx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp ecx, 65500 ; Max IPv4 packet size
|
|
|
|
ja .too_large
|
2009-10-12 22:14:14 +04:00
|
|
|
|
2012-07-18 01:36:34 +04:00
|
|
|
push ecx eax edx di
|
2009-10-12 18:39:59 +04:00
|
|
|
|
2012-08-08 17:14:32 +04:00
|
|
|
cmp eax, 1 shl 24 + 127
|
|
|
|
je .loopback
|
|
|
|
|
2012-07-18 01:36:34 +04:00
|
|
|
call IPv4_dest_to_dev ; outputs device number in edi, dest ip in eax
|
2011-11-12 19:39:15 +04:00
|
|
|
call ARP_IP_to_MAC
|
|
|
|
test eax, 0xffff0000 ; error bits
|
|
|
|
jnz .arp_error
|
2012-07-18 01:36:34 +04:00
|
|
|
push ebx ; push the mac onto the stack
|
2011-11-12 19:39:15 +04:00
|
|
|
push ax
|
|
|
|
|
2013-02-27 00:17:02 +04:00
|
|
|
inc [IP_packets_tx + edi] ; update stats
|
2012-07-18 01:36:34 +04:00
|
|
|
|
|
|
|
mov ebx, [NET_DRV_LIST + edi]
|
2011-11-12 19:39:15 +04:00
|
|
|
lea eax, [ebx + ETH_DEVICE.mac]
|
|
|
|
mov edx, esp
|
2012-07-18 01:36:34 +04:00
|
|
|
mov ecx, [esp + 10 + 6]
|
2011-11-12 19:39:15 +04:00
|
|
|
add ecx, sizeof.IPv4_header
|
2012-07-18 01:36:34 +04:00
|
|
|
mov di, ETHER_IPv4
|
2011-11-12 19:39:15 +04:00
|
|
|
call ETH_output
|
2012-04-03 22:26:53 +04:00
|
|
|
jz .eth_error
|
2012-07-18 01:36:34 +04:00
|
|
|
add esp, 6 ; pop the mac out of the stack
|
2011-11-12 19:39:15 +04:00
|
|
|
|
2012-08-08 17:14:32 +04:00
|
|
|
.continue:
|
2012-07-18 01:36:34 +04:00
|
|
|
xchg cl, ch ; internet byte order
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [edi + IPv4_header.VersionAndIHL], 0x45 ; IPv4, normal length (no Optional header)
|
|
|
|
mov [edi + IPv4_header.TypeOfService], 0 ; nothing special, just plain ip packet
|
|
|
|
mov [edi + IPv4_header.TotalLength], cx
|
2012-07-18 01:36:34 +04:00
|
|
|
mov [edi + IPv4_header.Identification], 0 ; fragment id: FIXME
|
|
|
|
mov [edi + IPv4_header.FlagsAndFragmentOffset], 0
|
2011-11-12 19:39:15 +04:00
|
|
|
pop word [edi + IPv4_header.TimeToLive] ; ttl shl 8 + protocol
|
2011-11-09 03:06:26 +04:00
|
|
|
; [edi + IPv4_header.Protocol]
|
2012-07-18 01:36:34 +04:00
|
|
|
mov [edi + IPv4_header.HeaderChecksum], 0
|
2011-11-12 19:39:15 +04:00
|
|
|
popd [edi + IPv4_header.SourceAddress]
|
|
|
|
popd [edi + IPv4_header.DestinationAddress]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
pop ecx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
IPv4_checksum edi
|
|
|
|
add edi, sizeof.IPv4_header
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_output: success!\n"
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-04-03 22:26:53 +04:00
|
|
|
.eth_error:
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_output: ethernet error\n"
|
2012-07-18 01:36:34 +04:00
|
|
|
add esp, 3*4+2+6
|
|
|
|
xor edi, edi
|
2012-04-03 22:26:53 +04:00
|
|
|
ret
|
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
.arp_error:
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_output: ARP error=%x\n", eax
|
2012-07-18 01:36:34 +04:00
|
|
|
add esp, 3*4+2
|
|
|
|
xor edi, edi
|
2012-04-03 22:26:53 +04:00
|
|
|
ret
|
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
.too_large:
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_output: Packet too large!\n"
|
2012-07-18 01:36:34 +04:00
|
|
|
xor edi, edi
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-08-08 17:14:32 +04:00
|
|
|
.loopback:
|
|
|
|
mov dword [esp + 2], eax
|
|
|
|
add ecx, sizeof.IPv4_header
|
|
|
|
mov di, ETHER_IPv4
|
|
|
|
call LOOP_output
|
|
|
|
jmp .continue
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-07-31 01:54:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; IPv4_output_raw
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; ecx = data length
|
|
|
|
; esi = data ptr
|
|
|
|
;
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
IPv4_output_raw:
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
DEBUGF 1,"IPv4_output_raw: size=%u ptr=%x socket=%x\n", ecx, esi, eax
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp ecx, 1480 ;;;;; FIXME
|
|
|
|
ja .too_large
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
sub esp, 8
|
|
|
|
push esi eax
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2012-07-18 01:36:34 +04:00
|
|
|
call IPv4_dest_to_dev
|
2011-11-12 19:39:15 +04:00
|
|
|
call ARP_IP_to_MAC
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
test eax, 0xffff0000 ; error bits
|
|
|
|
jnz .arp_error
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2012-07-18 01:36:34 +04:00
|
|
|
push ebx ; push the mac
|
2011-11-12 19:39:15 +04:00
|
|
|
push ax
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2013-02-27 00:17:02 +04:00
|
|
|
inc [IP_packets_tx + edi]
|
2012-07-18 01:36:34 +04:00
|
|
|
mov ebx, [NET_DRV_LIST + edi]
|
2011-11-12 19:39:15 +04:00
|
|
|
lea eax, [ebx + ETH_DEVICE.mac]
|
|
|
|
mov edx, esp
|
2012-07-18 01:36:34 +04:00
|
|
|
mov ecx, [esp + 6 + 4]
|
2011-11-12 19:39:15 +04:00
|
|
|
add ecx, sizeof.IPv4_header
|
|
|
|
mov di, ETHER_IPv4
|
|
|
|
call ETH_output
|
|
|
|
jz .error
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
add esp, 6 ; pop the mac
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov dword[esp+4+4], edx
|
|
|
|
mov dword[esp+4+4+4], eax
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
pop eax esi
|
2010-07-31 01:54:27 +04:00
|
|
|
;; todo: check socket options if we should add header, or just compute checksum
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push edi ecx
|
|
|
|
rep movsb
|
|
|
|
pop ecx edi
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-09 03:06:26 +04:00
|
|
|
; [edi + IPv4_header.VersionAndIHL] ; IPv4, normal length (no Optional header)
|
|
|
|
; [edi + IPv4_header.TypeOfService] ; nothing special, just plain ip packet
|
|
|
|
; [edi + IPv4_header.TotalLength]
|
|
|
|
; [edi + IPv4_header.TotalLength] ; internet byte order
|
|
|
|
; [edi + IPv4_header.FlagsAndFragmentOffset]
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [edi + IPv4_header.HeaderChecksum], 0
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-09 03:06:26 +04:00
|
|
|
; [edi + IPv4_header.TimeToLive] ; ttl shl 8 + protocol
|
|
|
|
; [edi + IPv4_header.Protocol]
|
|
|
|
; [edi + IPv4_header.Identification] ; fragment id
|
|
|
|
; [edi + IPv4_header.SourceAddress]
|
|
|
|
; [edi + IPv4_header.DestinationAddress]
|
2010-07-31 01:54:27 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
IPv4_checksum edi ;;;; todo: checksum for IP packet with options!
|
|
|
|
add edi, sizeof.IPv4_header
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_output_raw: device=%x\n", ebx
|
2011-11-12 19:39:15 +04:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
2010-07-31 01:54:27 +04:00
|
|
|
|
|
|
|
.error:
|
2011-11-12 19:39:15 +04:00
|
|
|
add esp, 6
|
2010-07-31 01:54:27 +04:00
|
|
|
.arp_error:
|
2011-11-12 19:39:15 +04:00
|
|
|
add esp, 8+4+4
|
2010-07-31 01:54:27 +04:00
|
|
|
.too_large:
|
2013-02-17 21:58:53 +04:00
|
|
|
DEBUGF 2,"IPv4_output_raw: Failed\n"
|
2011-11-12 19:39:15 +04:00
|
|
|
sub edi, edi
|
|
|
|
ret
|
2010-07-31 01:54:27 +04:00
|
|
|
|
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
;--------------------------------------------------------
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: dword [esp] = pointer to buffer containing ipv4 packet to be fragmented
|
|
|
|
; dword [esp+4] = buffer size
|
|
|
|
; esi = pointer to ip header in that buffer
|
|
|
|
; ecx = max size of fragments
|
|
|
|
;
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;--------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-06-07 13:37:13 +04:00
|
|
|
align 4
|
2010-07-27 22:53:38 +04:00
|
|
|
IPv4_fragment:
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
DEBUGF 1,"IPv4_fragment\n"
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
and ecx, not 111b ; align 4
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp ecx, sizeof.IPv4_header + 8 ; must be able to put at least 8 bytes
|
|
|
|
jb .err2
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push esi ecx
|
|
|
|
mov eax, [esi + IPv4_header.DestinationAddress]
|
|
|
|
call ARP_IP_to_MAC
|
|
|
|
pop ecx esi
|
|
|
|
cmp eax, -1
|
|
|
|
jz .err2
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push ebx
|
|
|
|
push ax
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ebx, [NET_DRV_LIST]
|
|
|
|
lea eax, [ebx + ETH_DEVICE.mac]
|
|
|
|
push eax
|
2010-06-07 13:37:13 +04:00
|
|
|
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push esi ; ptr to ip header
|
|
|
|
sub ecx, sizeof.IPv4_header ; substract header size
|
|
|
|
push ecx ; max data size
|
|
|
|
push dword 0 ; offset
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
.new_fragment:
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"Ipv4_fragment: new fragment"
|
2010-06-07 13:37:13 +04:00
|
|
|
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov eax, [esp + 3*4]
|
|
|
|
lea ebx, [esp + 4*4]
|
|
|
|
mov di , ETHER_IPv4
|
|
|
|
call ETH_output
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp edi, -1
|
|
|
|
jz .err
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
; copy header
|
2011-11-12 19:39:15 +04:00
|
|
|
mov esi, [esp + 2*4]
|
|
|
|
mov ecx, 5 ; 5 dwords: TODO: use IHL field of the header!
|
|
|
|
rep movsd
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
; copy data
|
2011-11-12 19:39:15 +04:00
|
|
|
mov esi, [esp + 2*4]
|
|
|
|
add esi, sizeof.IPv4_header
|
|
|
|
add esi, [esp] ; offset
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, [esp + 1*4]
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"IPv4_fragment: copying %u bytes\n", ecx
|
2011-11-12 19:39:15 +04:00
|
|
|
rep movsb
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
; now, correct header
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, [esp + 1*4]
|
|
|
|
add ecx, sizeof.IPv4_header
|
|
|
|
xchg cl, ch
|
|
|
|
mov [edi + IPv4_header.TotalLength], cx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, [esp] ; offset
|
|
|
|
xchg cl, ch
|
2010-07-27 22:53:38 +04:00
|
|
|
|
|
|
|
; cmp dword[esp + 4*4], 0 ; last fragment?;<<<<<<
|
|
|
|
; je .last_fragment
|
2011-11-12 19:39:15 +04:00
|
|
|
or cx, 1 shl 2 ; more fragments
|
2010-07-27 22:53:38 +04:00
|
|
|
; .last_fragment:
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [edi + IPv4_header.FlagsAndFragmentOffset], cx
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [edi + IPv4_header.HeaderChecksum], 0
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<< send the packet
|
|
|
|
mov ecx, [esp + 1*4]
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
push edx eax
|
|
|
|
IPv4_checksum edi
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, [esp+4]
|
|
|
|
add [esp], ecx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ecx, [esp+3*4+6+4] ; ptr to begin of buff
|
|
|
|
add ecx, [esp+3*4+6+4+4] ; buff size
|
|
|
|
sub ecx, [esp+2*4] ; ptr to ip header
|
|
|
|
add ecx, [esp] ; offset
|
2010-06-07 13:37:13 +04:00
|
|
|
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"Ipv4_fragment: %u bytes remaining\n", ecx
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp ecx, [esp+1*4]
|
|
|
|
jae .new_fragment
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
mov [esp+4], ecx ; set fragment size to remaining packet size
|
|
|
|
jmp .new_fragment
|
2010-07-27 22:53:38 +04:00
|
|
|
|
|
|
|
.err:
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"Ipv4_fragment: failed\n"
|
2010-07-27 22:53:38 +04:00
|
|
|
.done:
|
2011-11-12 19:39:15 +04:00
|
|
|
add esp, 12 + 4 + 6
|
2010-07-27 22:53:38 +04:00
|
|
|
.err2:
|
2012-07-27 03:21:35 +04:00
|
|
|
DEBUGF 1,"Ipv4_fragment: dumping\n"
|
2011-11-12 19:39:15 +04:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4
|
2010-07-27 22:53:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2010-06-07 13:37:13 +04:00
|
|
|
|
|
|
|
|
2009-10-12 18:39:59 +04:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; IPv4_dest_to_dev
|
|
|
|
;
|
2012-07-18 01:36:34 +04:00
|
|
|
; IN: eax = Destination IP
|
|
|
|
; OUT: edi = device id * 4
|
|
|
|
; eax = ip of gateway if nescessary, unchanged otherwise
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
IPv4_dest_to_dev:
|
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp eax, 0xffffffff
|
2012-07-18 01:36:34 +04:00
|
|
|
je .broadcast
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
xor edi, edi
|
2012-08-15 20:15:57 +04:00
|
|
|
mov ecx, MAX_NET_DEVICES
|
2009-09-17 15:55:38 +04:00
|
|
|
.loop:
|
2011-11-12 19:39:15 +04:00
|
|
|
mov ebx, [IP_LIST+edi]
|
|
|
|
and ebx, [SUBNET_LIST+edi]
|
|
|
|
jz .next
|
|
|
|
mov edx, eax
|
|
|
|
and edx, [SUBNET_LIST+edi]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
cmp ebx, edx
|
|
|
|
je .found_it
|
2010-07-27 22:53:38 +04:00
|
|
|
.next:
|
2011-11-12 19:39:15 +04:00
|
|
|
add edi, 4
|
2012-06-03 23:28:01 +04:00
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-07-27 22:53:38 +04:00
|
|
|
.invalid:
|
2012-07-18 01:36:34 +04:00
|
|
|
xor edi, edi ; if none found, use device 0 as default
|
|
|
|
mov eax, [GATEWAY_LIST]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.found_it:
|
2011-11-12 19:39:15 +04:00
|
|
|
DEBUGF 1,"IPv4_dest_to_dev: %u\n", edi
|
2012-07-18 01:36:34 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2012-07-18 01:36:34 +04:00
|
|
|
.broadcast:
|
|
|
|
xor edi, edi
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; IPv4_get_frgmnt_num
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: fragment number in ax
|
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
IPv4_get_frgmnt_num:
|
2011-11-12 19:39:15 +04:00
|
|
|
xor ax, ax ;;; TODO: replace this with real code
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; IPv4_API
|
|
|
|
;
|
|
|
|
; This function is called by system function 75
|
|
|
|
;
|
|
|
|
; IN: subfunction number in bl
|
|
|
|
; device number in bh
|
|
|
|
; ecx, edx, .. depends on subfunction
|
|
|
|
;
|
|
|
|
; OUT:
|
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
2012-04-15 01:15:00 +04:00
|
|
|
IPv4_api:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2011-11-12 19:39:15 +04:00
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
2012-04-17 22:41:58 +04:00
|
|
|
and ebx, 0x000000ff
|
2012-04-15 01:15:00 +04:00
|
|
|
cmp ebx, .number
|
|
|
|
ja .error
|
|
|
|
jmp dword [.table + 4*ebx]
|
|
|
|
|
|
|
|
.table:
|
|
|
|
dd .packets_tx ; 0
|
|
|
|
dd .packets_rx ; 1
|
2012-04-17 22:41:58 +04:00
|
|
|
dd .read_ip ; 2
|
2012-04-15 01:15:00 +04:00
|
|
|
dd .write_ip ; 3
|
|
|
|
dd .read_dns ; 4
|
|
|
|
dd .write_dns ; 5
|
|
|
|
dd .read_subnet ; 6
|
|
|
|
dd .write_subnet ; 7
|
|
|
|
dd .read_gateway ; 8
|
|
|
|
dd .write_gateway ; 9
|
|
|
|
.number = ($ - .table) / 4 - 1
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.error:
|
2011-11-12 19:39:15 +04:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.packets_tx:
|
2013-02-27 00:17:02 +04:00
|
|
|
mov eax, [IP_packets_tx + eax]
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.packets_rx:
|
2013-02-27 00:17:02 +04:00
|
|
|
mov eax, [IP_packets_rx + eax]
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.read_ip:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov eax, [IP_LIST + eax]
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.write_ip:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov [IP_LIST + eax], ecx
|
2013-01-11 14:52:01 +04:00
|
|
|
mov edi, eax ; device number, we'll need it for ARP
|
2012-06-03 23:28:01 +04:00
|
|
|
|
|
|
|
; pre-calculate the local broadcast address
|
|
|
|
mov ebx, [SUBNET_LIST + eax]
|
|
|
|
not ebx
|
2013-01-11 14:52:01 +04:00
|
|
|
or ebx, ecx
|
|
|
|
mov [BROADCAST_LIST + eax], ebx
|
|
|
|
|
|
|
|
mov eax, ecx
|
|
|
|
call ARP_output_request ; now send a gratuitous ARP
|
2012-06-03 23:28:01 +04:00
|
|
|
|
2013-03-07 14:47:59 +04:00
|
|
|
call NET_send_event
|
2011-11-12 19:39:15 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.read_dns:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov eax, [DNS_LIST + eax]
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.write_dns:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov [DNS_LIST + eax], ecx
|
2013-03-07 14:47:59 +04:00
|
|
|
call NET_send_event
|
2011-11-12 19:39:15 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.read_subnet:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov eax, [SUBNET_LIST + eax]
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.write_subnet:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov [SUBNET_LIST + eax], ecx
|
2012-06-03 23:28:01 +04:00
|
|
|
|
|
|
|
; pre-calculate the local broadcast address
|
|
|
|
mov ebx, [IP_LIST + eax]
|
|
|
|
not ecx
|
|
|
|
or ecx, ebx
|
|
|
|
mov [BROADCAST_LIST + eax], ecx
|
|
|
|
|
2013-03-07 14:47:59 +04:00
|
|
|
call NET_send_event
|
2011-11-12 19:39:15 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.read_gateway:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov eax, [GATEWAY_LIST + eax]
|
2011-11-12 19:39:15 +04:00
|
|
|
ret
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
.write_gateway:
|
2012-04-15 01:15:00 +04:00
|
|
|
mov [GATEWAY_LIST + eax], ecx
|
2013-03-07 14:47:59 +04:00
|
|
|
|
|
|
|
call NET_send_event
|
2011-11-12 19:39:15 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|