2009-09-17 13:55:38 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2010-07-12 01:13:12 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;;
|
2009-09-17 13:55:38 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; UDP.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the tcp/ip network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
struct UDP_header
|
2011-11-09 00:06:26 +01:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
SourcePort dw ?
|
|
|
|
DestinationPort dw ?
|
|
|
|
Length dw ? ; Length of (UDP Header + Data)
|
|
|
|
Checksum dw ?
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
uglobal
|
2011-11-12 16:39:15 +01:00
|
|
|
UDP_PACKETS_TX rd MAX_IP
|
|
|
|
UDP_PACKETS_RX rd MAX_IP
|
2009-09-17 13:55:38 +02:00
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; UDP_init
|
|
|
|
;
|
|
|
|
; This function resets all UDP variables
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2011-11-12 16:39:15 +01:00
|
|
|
macro UDP_init {
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
xor eax, eax
|
|
|
|
mov edi, UDP_PACKETS_TX
|
|
|
|
mov ecx, 2*MAX_IP
|
|
|
|
rep stosd
|
2010-07-27 20:53:38 +02:00
|
|
|
}
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
macro UDP_checksum IP1, IP2 { ; esi = ptr to udp packet, ecx = packet size, destroys: ecx, edx
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
; Pseudoheader
|
2011-11-12 16:39:15 +01:00
|
|
|
mov edx, IP_PROTO_UDP
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
add dl, [IP1+1]
|
|
|
|
adc dh, [IP1+0]
|
|
|
|
adc dl, [IP1+3]
|
|
|
|
adc dh, [IP1+2]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
adc dl, [IP2+1]
|
|
|
|
adc dh, [IP2+0]
|
|
|
|
adc dl, [IP2+3]
|
|
|
|
adc dh, [IP2+2]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
adc dl, cl ; byte[esi+UDP_header.Length+1]
|
|
|
|
adc dh, ch ; byte[esi+UDP_header.Length+0]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
; Done with pseudoheader, now do real header
|
2011-11-12 21:05:07 +01:00
|
|
|
adc dl, byte[esi+UDP_header.SourcePort+1]
|
|
|
|
adc dh, byte[esi+UDP_header.SourcePort+0]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
adc dl, byte[esi+UDP_header.DestinationPort+1]
|
|
|
|
adc dh, byte[esi+UDP_header.DestinationPort+0]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
adc dl, byte[esi+UDP_header.Length+1]
|
|
|
|
adc dh, byte[esi+UDP_header.Length+0]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
adc edx, 0
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
; Done with header, now do data
|
2011-11-12 16:39:15 +01:00
|
|
|
push esi
|
2011-11-12 21:05:07 +01:00
|
|
|
movzx ecx, [esi+UDP_header.Length]
|
2011-11-12 16:39:15 +01:00
|
|
|
rol cx , 8
|
2011-11-12 21:05:07 +01:00
|
|
|
sub cx , sizeof.UDP_header
|
|
|
|
add esi, sizeof.UDP_header
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
call checksum_1
|
|
|
|
call checksum_2
|
|
|
|
pop esi
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
add [esi+UDP_header.Checksum], dx ; this final instruction will set or clear ZF :)
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
}
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; UDP_input:
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; Called by IPv4_input,
|
2009-09-17 13:55:38 +02:00
|
|
|
; this procedure will inject the udp data diagrams in the application sockets.
|
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; IN: [esp] = Pointer to buffer
|
|
|
|
; [esp+4] = size of buffer
|
|
|
|
; ebx = ptr to device struct
|
|
|
|
; ecx = UDP Packet size
|
2011-11-12 21:05:07 +01:00
|
|
|
; esi = ptr to UDP header
|
2011-11-12 16:39:15 +01:00
|
|
|
; edi = ptr to ipv4 source and dest address
|
2010-06-07 16:17:36 +02:00
|
|
|
;
|
2009-09-17 13:55:38 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
UDP_input:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
DEBUGF 1,"UDP_input, size:%u\n", ecx
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2012-02-23 23:21:40 +01:00
|
|
|
; First validate, checksum
|
2011-11-12 21:05:07 +01:00
|
|
|
|
|
|
|
neg [esi + UDP_header.Checksum] ; substract checksum from 0
|
2012-02-23 23:21:40 +01:00
|
|
|
jz .no_checksum ; if checksum is zero, it is considered valid
|
|
|
|
|
|
|
|
; otherwise, we will re-calculate the checksum and add it to this value, thus creating 0 when it is correct
|
2011-11-12 16:39:15 +01:00
|
|
|
|
|
|
|
UDP_checksum (edi), (edi+4)
|
2012-02-23 23:21:40 +01:00
|
|
|
jnz .checksum_mismatch
|
2010-05-28 22:47:32 +02:00
|
|
|
|
|
|
|
.no_checksum:
|
2011-11-12 16:39:15 +01:00
|
|
|
DEBUGF 1,"UDP Checksum is correct\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
; Convert port numbers to intel format
|
|
|
|
|
2012-02-22 11:44:57 +01:00
|
|
|
rol [esi + UDP_header.DestinationPort], 8
|
|
|
|
rol [esi + UDP_header.SourcePort], 8
|
|
|
|
rol [esi + UDP_header.Length], 8
|
2011-11-12 21:05:07 +01:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
; Look for a socket where
|
|
|
|
; IP Packet UDP Destination Port = local Port
|
|
|
|
; IP Packet SA = Remote IP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-22 11:44:57 +01:00
|
|
|
mov cx, [esi + UDP_header.SourcePort]
|
|
|
|
mov dx, [esi + UDP_header.DestinationPort]
|
2011-11-12 21:05:07 +01:00
|
|
|
mov edi, [edi + 4] ; ipv4 source address
|
2011-11-12 16:39:15 +01:00
|
|
|
mov eax, net_sockets
|
2011-11-12 21:05:07 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
.next_socket:
|
2011-11-12 16:39:15 +01:00
|
|
|
mov eax, [eax + SOCKET.NextPtr]
|
|
|
|
or eax, eax
|
|
|
|
jz .dump
|
2011-11-12 21:05:07 +01:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
|
|
|
jne .next_socket
|
2011-11-12 21:05:07 +01:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_UDP
|
|
|
|
jne .next_socket
|
2011-11-12 21:05:07 +01:00
|
|
|
|
2012-02-22 11:44:57 +01:00
|
|
|
cmp [eax + UDP_SOCKET.LocalPort], dx
|
2011-11-12 16:39:15 +01:00
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
DEBUGF 1,"using socket: %x\n", eax
|
|
|
|
|
|
|
|
;;; TODO: when packet is processed, check more sockets!
|
|
|
|
|
2012-02-21 15:09:00 +01:00
|
|
|
; cmp [eax + IP_SOCKET.RemoteIP], 0xffffffff
|
|
|
|
; je @f
|
|
|
|
; cmp [eax + IP_SOCKET.RemoteIP], edi
|
|
|
|
; jne .next_socket
|
|
|
|
; @@:
|
|
|
|
;
|
|
|
|
; FIXME: UDP should check remote IP, but not under all circumstances!
|
2009-10-12 20:14:14 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
cmp [eax + UDP_SOCKET.firstpacket], 0
|
2011-11-12 23:35:01 +01:00
|
|
|
je .updateport
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
cmp [eax + UDP_SOCKET.RemotePort], cx
|
2011-11-12 16:39:15 +01:00
|
|
|
jne .dump
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-02-25 13:03:22 +01:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
2009-12-28 22:02:54 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.updatesock:
|
2011-11-12 16:39:15 +01:00
|
|
|
inc [UDP_PACKETS_RX]
|
|
|
|
DEBUGF 1,"Found valid UDP packet for socket %x\n", eax
|
2012-02-22 11:44:57 +01:00
|
|
|
movzx ecx, [esi + UDP_header.Length]
|
2011-11-12 21:05:07 +01:00
|
|
|
sub ecx, sizeof.UDP_header
|
2012-02-22 11:44:57 +01:00
|
|
|
add esi, sizeof.UDP_header
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
jmp SOCKET_input
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2009-12-27 22:05:25 +01:00
|
|
|
.updateport:
|
2012-02-25 13:03:22 +01:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
2009-12-28 22:02:54 +01:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
DEBUGF 1,"Changing remote port to: %u\n", cx
|
|
|
|
mov [eax + UDP_SOCKET.RemotePort], cx
|
2011-11-12 16:39:15 +01:00
|
|
|
inc [eax + UDP_SOCKET.firstpacket]
|
2009-12-27 22:05:25 +01:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
jmp .updatesock
|
2009-12-27 22:05:25 +01:00
|
|
|
|
2010-06-07 14:57:52 +02:00
|
|
|
|
2010-05-28 22:47:32 +02:00
|
|
|
.checksum_mismatch:
|
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
DEBUGF 2,"UDP_Handler - checksum mismatch\n"
|
2010-05-28 22:47:32 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
.dump:
|
2011-11-12 16:39:15 +01:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4 ; pop (balance stack)
|
|
|
|
DEBUGF 2,"UDP_Handler - dumping\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; UDP_output
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; IN: eax = socket pointer
|
|
|
|
; ecx = number of bytes to send
|
|
|
|
; esi = pointer to data
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
UDP_output:
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
DEBUGF 1,"UDP_output: socket:%x, bytes: %u, data ptr: %x\n", eax, ecx, esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
mov dx, [eax + UDP_SOCKET.RemotePort]
|
|
|
|
DEBUGF 1,"remote port: %u\n", dx
|
|
|
|
rol dx, 8
|
|
|
|
rol edx, 16
|
|
|
|
mov dx, [eax + UDP_SOCKET.LocalPort]
|
|
|
|
DEBUGF 1,"local port: %u\n", dx
|
|
|
|
rol dx, 8
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
mov ebx, [eax + IP_SOCKET.LocalIP]
|
|
|
|
mov eax, [eax + IP_SOCKET.RemoteIP]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
mov di, IP_PROTO_UDP shl 8 + 128
|
|
|
|
sub esp, 8 ; Data ptr and data size will be placed here
|
2011-11-12 21:05:07 +01:00
|
|
|
add ecx, sizeof.UDP_header
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
;;; TODO: fragment id
|
2011-11-12 16:39:15 +01:00
|
|
|
push edx esi
|
|
|
|
call IPv4_output
|
|
|
|
jz .fail
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
mov [esp + 8], eax ; pointer to buffer start
|
|
|
|
mov [esp + 8 + 4], edx ; buffer size
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
mov [edi + UDP_header.Length], cx
|
|
|
|
rol [edi + UDP_header.Length], 8
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
pop esi
|
|
|
|
push edi ecx
|
2011-11-12 21:05:07 +01:00
|
|
|
sub ecx, sizeof.UDP_header
|
|
|
|
add edi, sizeof.UDP_header
|
2011-11-12 16:39:15 +01:00
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
|
|
|
mov ecx, [esp]
|
|
|
|
and ecx, 3
|
|
|
|
rep movsb
|
|
|
|
pop ecx edi
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2011-11-12 21:05:07 +01:00
|
|
|
pop dword [edi + UDP_header.SourcePort]
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-06-07 11:37:13 +02:00
|
|
|
; Checksum
|
2011-11-12 16:39:15 +01:00
|
|
|
mov esi, edi
|
2011-11-12 21:05:07 +01:00
|
|
|
mov [edi + UDP_header.Checksum], 0
|
2011-11-12 16:39:15 +01:00
|
|
|
UDP_checksum (edi-4), (edi-8) ; TODO: fix this, IPv4 packet could have options..
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
inc [UDP_PACKETS_TX]
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
DEBUGF 1,"Sending UDP Packet to device %x\n", ebx
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
.fail:
|
2011-11-12 16:39:15 +01:00
|
|
|
DEBUGF 1,"UDP_output: failed\n"
|
|
|
|
add esp, 4+4+8
|
2012-02-23 23:21:40 +01:00
|
|
|
or eax, -1
|
2011-11-12 16:39:15 +01:00
|
|
|
ret
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; UDP_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-14 23:15:00 +02:00
|
|
|
UDP_api:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 16:39:15 +01:00
|
|
|
test bl, bl
|
|
|
|
jz .packets_tx ; 0
|
|
|
|
dec bl
|
|
|
|
jz .packets_rx ; 1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.error:
|
2011-11-12 16:39:15 +01:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.packets_tx:
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, [UDP_PACKETS_TX + eax]
|
2011-11-12 16:39:15 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.packets_rx:
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, [UDP_PACKETS_RX + eax]
|
2011-11-12 16:39:15 +01:00
|
|
|
ret
|