From 47d756fe2f2c395d3b38d0662ff93a5c4a07b384 Mon Sep 17 00:00:00 2001 From: hidnplayr Date: Mon, 20 Jul 2015 11:13:42 +0000 Subject: [PATCH] Bugfixes to bind socket to device. git-svn-id: svn://kolibrios.org@5584 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/network/IPv4.inc | 26 ++++++++++++++++++++++++++ kernel/trunk/network/icmp.inc | 1 + kernel/trunk/network/socket.inc | 4 ++-- kernel/trunk/network/tcp_output.inc | 1 + kernel/trunk/network/tcp_subr.inc | 2 ++ kernel/trunk/network/udp.inc | 1 + 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/kernel/trunk/network/IPv4.inc b/kernel/trunk/network/IPv4.inc index cc67432505..7580c5ef52 100644 --- a/kernel/trunk/network/IPv4.inc +++ b/kernel/trunk/network/IPv4.inc @@ -569,6 +569,7 @@ IPv4_find_fragment_slot: ; IPv4_output ; ; IN: eax = Destination IP +; ebx = device ptr (or 0 to let IP layer decide) ; ecx = data length ; edx = Source IP ; di = TTL shl 8 + protocol @@ -863,6 +864,7 @@ IPv4_fragment: ; IPv4_route ; ; IN: eax = Destination IP +; ebx = outgoing device / 0 ; edx = Source IP ; OUT: eax = Destination IP (or gateway IP) ; edx = Source IP @@ -874,6 +876,9 @@ IPv4_fragment: align 4 IPv4_route: ; TODO: return error if no valid route found + test ebx, ebx + jnz .got_device + cmp eax, 0xffffffff je .broadcast @@ -903,6 +908,27 @@ IPv4_route: ; TODO: return error if no valid route found ret + .got_device: +; Validate device ptr and convert to device number + call NET_ptr_to_num4 + cmp edi, -1 + je .fail + + mov edx, [IP_LIST + edi] ; Source IP + +; Check if we should route to gateway or not + mov ebx, [IP_LIST + edi] + and ebx, [SUBNET_LIST + edi] + mov ecx, eax + and ecx, [SUBNET_LIST + edi] + je @f + mov eax, [GATEWAY_LIST + edi] + @@: + ret + + .fail: + ret + ;--------------------------------------------------------------------------- diff --git a/kernel/trunk/network/icmp.inc b/kernel/trunk/network/icmp.inc index 151c59917a..a34dcd6d46 100644 --- a/kernel/trunk/network/icmp.inc +++ b/kernel/trunk/network/icmp.inc @@ -388,6 +388,7 @@ ICMP_output_raw: mov di, IP_PROTO_ICMP SHL 8 + 128 ; TTL mov edx, [eax + IP_SOCKET.LocalIP] + mov ebx, [eax + IP_SOCKET.device] mov eax, [eax + IP_SOCKET.RemoteIP] call IPv4_output jz .exit diff --git a/kernel/trunk/network/socket.inc b/kernel/trunk/network/socket.inc index 7a17d217f1..b5432ed145 100644 --- a/kernel/trunk/network/socket.inc +++ b/kernel/trunk/network/socket.inc @@ -1160,7 +1160,7 @@ SOCKET_set_opt: cmp dword[edx+8], 0 je .unbind - movzx edx, byte[edx + 9] + movzx edx, byte[edx + 12] cmp edx, NET_DEVICES_MAX ja .invalid @@ -1169,7 +1169,7 @@ SOCKET_set_opt: jz .already mov [eax + SOCKET.device], edx - DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n",eax, edx + DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx mov dword[esp+32], 0 ; success! ret diff --git a/kernel/trunk/network/tcp_output.inc b/kernel/trunk/network/tcp_output.inc index 0acbb4dcf8..a7e8ad2041 100644 --- a/kernel/trunk/network/tcp_output.inc +++ b/kernel/trunk/network/tcp_output.inc @@ -504,6 +504,7 @@ endl mov ecx, esi mov edx, [eax + IP_SOCKET.LocalIP] ; source ip + mov ebx, [eax + IP_SOCKET.device] mov eax, [eax + IP_SOCKET.RemoteIP] ; dest ip mov di, IP_PROTO_TCP shl 8 + 128 call IPv4_output diff --git a/kernel/trunk/network/tcp_subr.inc b/kernel/trunk/network/tcp_subr.inc index 96e5b0b43d..618b56bdc7 100644 --- a/kernel/trunk/network/tcp_subr.inc +++ b/kernel/trunk/network/tcp_subr.inc @@ -287,6 +287,7 @@ TCP_respond: push cx ebx mov eax, [ebx + IP_SOCKET.RemoteIP] mov edx, [ebx + IP_SOCKET.LocalIP] + mov ebx, [ebx + IP_SOCKET.device] mov ecx, sizeof.TCP_header mov di, IP_PROTO_TCP shl 8 + 128 call IPv4_output @@ -372,6 +373,7 @@ TCP_respond_segment: mov eax, [edi] mov ecx, sizeof.TCP_header mov di, IP_PROTO_TCP shl 8 + 128 + xor ebx, ebx ;;; fixme call IPv4_output jz .error pop esi cx diff --git a/kernel/trunk/network/udp.inc b/kernel/trunk/network/udp.inc index 3df4d8d9b8..c8ae773507 100644 --- a/kernel/trunk/network/udp.inc +++ b/kernel/trunk/network/udp.inc @@ -258,6 +258,7 @@ UDP_output: sub esp, 4 ; Data ptr will be placed here push edx esi mov edx, [eax + IP_SOCKET.LocalIP] + mov ebx, [eax + IP_SOCKET.device] mov eax, [eax + IP_SOCKET.RemoteIP] mov di, IP_PROTO_UDP shl 8 + 128 add ecx, sizeof.UDP_header