LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2022, 07:39 AM   #1
dalacor
Member
 
Registered: Feb 2019
Distribution: Slackware
Posts: 170

Rep: Reputation: Disabled
How do I forward (Route) Packets to Internet. Only works if I NAT


If I use this firewall rule:

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 192.168.1.2

I can ping google, my email works, tracert works etc. That is assuming I have the relevant forwarding rules for each port in place. However, I do not want to NAT the traffic as I need the packets to appear as if they came from the original computer, not the firewall computer!

Problem is, I cannot work out how to do this. I have confirmed that IP Forwarding via kernel is loaded at boot time - packet forwarding is definitely on at boot time. I tried setting forwarding rules with both -i eth1 -o eth0 and other scenarios like that. But if I remove the POSTROUTING rule (i.e. NAT), then I cannot connect to the Internet, ping anything, Imap email doesn't connect etc.

I have the following IP setup:

External IP Address of Firewall - 192.168.1.2
Internet Router IP Address - 192.168.1.1 (This is the gateway on the Firewall Computer)
Internal IP Address of Firewall - 10.10.8.1
My PC IP Address - 10.10.8.2 and my PC gateway is 10.10.8.1 (internal IP address of firewall PC) and my dns server is 192.168.1.1 (my Internet Router).

I have shown my firewall rules below as working when using NAT and POSTROUTING. I have tried changing the forwarding rules to things like -i eth1 -o eth0 for DNS, mail and ICMP etc and removed Postrouting rule. But nothing works unless I use NAT on the firewall. I have tried allowing all traffic through - so removing the Drop Policy Rules again with no effect. I also see no errors in the firewall Logs. So I believe (and speak under correction), that my problem is not a firewall rule per se, but rather that if I am not using NAT (i.e. postrouting), then my computer (and/or the firewall) doesn't know where to send the traffic for the Internet! It seems more like a routing table issue, rather than a firewall access issue. But I would have thought that having the firewall computer as my computer's gateway and having the Internet Router as the firewall's gateway would in theory allow traffic to be routed?

What am I missing for RIP?

This is my firewall Rules for just ICMP, Dns and Email just to keep it simple.

Code:
#!/bin/bash
# New Client Firewall


# Policy Rules

# Ipv4 traffic

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

# Ipv6 traffic

ip6tables -P INPUT DROP
ip6tables -P OUTPUT DROP
ip6tables -P FORWARD DROP


# Established and Related Rules

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT


# NAT Rules (Prerouting, Postrouting and Output Chains)

# Allows Internal Lan Traffic to be Routed to Internet

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 192.168.1.2

# Filter Rules (Input, Output and Forward Chains)

# UDP and TCP Port 53 Dns Lan Resolution

iptables -A FORWARD -p udp --dport 53 -j ACCEPT
iptables -A FORWARD -p tcp --dport 53 -j ACCEPT

# TCP Port 465 Imap Legacy Sending Email
 
iptables -A FORWARD -p tcp --dport 465 -j ACCEPT

# TCP Port 587 Imap Starttls Sending Email
 
iptables -A FORWARD -p tcp --dport 587 -j ACCEPT

# TCP Port 993 SMTP Receiving Email
 
iptables -A FORWARD -p tcp --dport 993 -j ACCEPT

# ICMP Port Ping to Internet

iptables -A FORWARD -p icmp --icmp-type any -j ACCEPT

# Logging

# Log IPv4 Dropped Traffic

iptables -N LOGGING
iptables -A INPUT -j LOGGING
iptables -A OUTPUT -j LOGGING
iptables -A FORWARD -j LOGGING
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
iptables -A LOGGING -j DROP

# Log IPv6 Dropped Traffic

ip6tables -N LOGGING
ip6tables -A INPUT -j LOGGING
ip6tables -A OUTPUT -j LOGGING
ip6tables -A FORWARD -j LOGGING
ip6tables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPv6-IPTables-Dropped: " --log-level 4
ip6tables -A LOGGING -j DROP
 
Old 11-03-2022, 10:31 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
10.10.x.x is a local address. It is not unique and not routable over the internet. You can send packets with that source address, but you will never get a reply.
 
Old 11-03-2022, 10:48 AM   #3
killerhippy
LQ Newbie
 
Registered: Jan 2021
Location: Germany
Distribution: debian
Posts: 22

Rep: Reputation: Disabled
Hello dalacor,

good point from smallpond which is true, I assume you know that already and that there is need to do what you asked.

I think, there is no need to use iptables.

What if you add a route from your 10.x.x.x network to the 192.168.1.x network and a default route to 192.168.1.2?
 
Old 11-04-2022, 09:55 AM   #4
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 116

Rep: Reputation: 13
So you want to use RIP and therefore you need the local IP-address routed via the internet and show up on the other side as the original local addresses?

If that is the case you will have to use a tunnel.

Look up gre tunneling and based on that you might want to consider an ipsec tunnel.
 
Old 11-06-2022, 12:14 PM   #5
dalacor
Member
 
Registered: Feb 2019
Distribution: Slackware
Posts: 170

Original Poster
Rep: Reputation: Disabled
Sorry for the very late reply.

I realise that 10.10.x.x is a private IP Address range. I would not expect that to be routable over the Internet itself. The Draytek Internet Router would obviously use NAT at that point. Perhaps I am asking the wrong question. Should I be using Postrouting to connect computers in a Lan behind a firewall to the Internet even when I have an Internet Router that is already doing NAT?

My impression was that Prerouting and Postrouting was only supposed to be used to make things like Internal Website Servers or Mail Servers appear to be sending from the Public IP Address because one is already prerouting traffic from the Public IP address to that Internal Server.

In my particular case on my test site and one of my clients, I have a Draytek Internet Router which already converts my private IP Address (whether that is the proxy server or the actual original PC) to the Draytek Router Public IP Address and then sent onto the Internet. So I wouldn't have thought that I needed to do NAT for the proxy server as well as the Draytek Router? If my computer was connected directly to the Draytek Router (no proxy server), I obviously don't need Postrouting - so why do I need it with a proxy server if my Draytek Router is the Dns Server and Gateway for my firewall PC?

The source traffic I am interested in is any traffic coming from Site B to Site A over the Lan to Lan VPN that I have setup on the Draytek Routers. However, I presume the proxy servers on both sites would be the source ip address instead of the original servers. I will have to do some testing with src ip of Server A to destination Sever B firewall rules and see what happens. The end aim is that I want to restrict say tcp 135 traffic between domain controllers on both sites and in order to do that, the proxy servers on both sites need to know the actual source IP address of the Servers not the NAT ip address. I was led to understand that Postrouting is not what I need to achieve this.
 
Old 11-06-2022, 01:35 PM   #6
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 415
Blog Entries: 2

Rep: Reputation: Disabled
KatrinALec is right. You want to preserve source but there is no way to do that unless you encapsulate it to pass through the internet to your end point. Any NAT is supposed to NAT, not leak your source ip.
GRE.
You can do this router/router, or host to host. Stunnel, ssh (limited) host to host. Gre/IPSec router to router.
 
Old 11-07-2022, 01:46 AM   #7
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 116

Rep: Reputation: 13
Anything you send out to the internet needs to get converted to a public IP or the answers wouldn't find their way back.

So in your private network A you have the client A 192.168.0.2/24
in your second private network B you have the server B 192.168.1.2/24.

Both networks are only connected via internet.

Your network A has the public address 1.1.1.1
and your network B has the public address 2.2.2.2

So you probably have a gateway to the internet 192.168.0.1 in network A and 192.168.1.1 in network B.
These gateways also have the public IP-addresses.

So client A has to call 2.2.2.2 or it won't get to the network B, and server B has to call 1.1.1.1 or it won't get back to network A.

You probably have more than one client or server in those networks, but all of them know their gateway.

If you call 192.168.1.2 from 192.168.0.2 that will go to the gateway because it's not in the same subnet, but either the gateway will refuse to put it on the internet or at the very least it will never go to 2.2.2.2 because gateway A doesn't know it should send it there.

If you call 2.2.2.2 from 192.168.0.2 it will arrive at the gateway of B but then that won't know to send it to 192.168.1.2 because that information is lacking.

GRE
Tell gateway A to pack anything that wants to go to 192.168.1/24 into a frame with the address 2.2.2.2
and tell gateway B to pack anything that wants to go to 192.168.0/24 in a frame that goes to 1.1.1.1
Both gateways will unpack that kind of frame and reveal the original IP address.
That way the client A can call 192.168.1.2 and that goes via gateway A, gets sent to 2.2.2.2, gets unpacked by gateway B and then there is the original destination.
Answer from B does the same thing backwards.
Private IP addresses and ports are preserved that way.

NAT
Tell gateway A to replace the original source private IP address by it's own (SNAT which is always done post routing).
That doesn't influence the destination IP, so client A has to call 2.2.2.2 already.
So between client A and server A packet is from 192.168.0.2 to 2.2.2.2
on the internet between gateway A and gateway B packet is from 1.1.1.1 to 2.2.2.2
it arrives at gateway B and there it needs another NAT, this time a DNAT, which has to be done pre routing).
That replaces the original destination with 192.168.1.2.
You can either replace anything with destination 2.2.2.2, which might have its disadvantages
or you could only replace things from 1.1.1.1 that go to 2.2.2.2 and want the destination port of 80 or something similar.
So between gateway B and server B you see from 1.1.1.1 to 192.168.1.2.
So the answer packet goes to 1.1.1.1 from 192.168.1.2 and needs the same changes backwards, but hat should be done automatically by the gateways if you use conntrack.

IPSEC
You basically use the GRE method, but you scramble anything between 1.1.1.1 and 2.2.2.2 and back.
 
Old 11-07-2022, 05:12 AM   #8
dalacor
Member
 
Registered: Feb 2019
Distribution: Slackware
Posts: 170

Original Poster
Rep: Reputation: Disabled
I think what I will do first is go through my Iptables firewall rules and see if I can get everything to work using destination IP only and review whether I really need source IP Address anywhere. I didn't realise it was going to be complicated. Most of my traffic (Internet traffic), I want to use NAT and only route traffic between sites. But I have a feeling that just having the destination IP Address for RDP and Ports 135 and 443 should be sufficient.

My network network setup is already quite complicated at the client in question as I have a 10.x internal network on Site A and a slightly different 10.x internal network at site B. Then in addition to that I have the Draytek Routers on a completely different subnet at each site using a 10.14.x range so that all Internal traffic on 10.x has to go through proxy server internal IP address and out external IP address 10.14.x and then onto Router and from then on outwards to either the actual Internet or over the Lan to Lan VPN.

I obviously misunderstood my research on Postrouting and had assumed that this was only meant to be used with Internal Servers that computers outside connect to such as websites, mail servers etc. Obviously this is not the case. My first priority should actually to be to evaluate whether I really need source IP addresses to be retained or not. I would say that there really is only about 3 or 4 ports that really need to be opened between the two networks and that would WSUS 8531, DNS 53, Replication of DC and DFS so ports 135 and 443 as well as RDP 3389 to the servers of course. One or two more. So I just have to think whether I really need source port ip addresses or not.

I will come back to this question, when I have reviewed my Firewall Rules and let you know the outcome. But I am beginning to think that it would be easier to restrict source IP addresses on the Servers Windows Firewalls themselves. I can allow only IP address range xyz to connect to Server via RDP for example. So I could in theory control both source and destination, just using different methods.
 
Old 11-07-2022, 09:32 AM   #9
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
In order to get routing to work through your firewall without NAT, the router needs to know to send packets with a 10.10.8.x dest address to your firewall and to do source NAT on packets received from the firewall with a 10.10.8.x address. By default, the router will only do this for its local 192 subnet.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why static routes set using 'ip route add' and 'route add' commands are not used for routing packets within the same network? kanak@tetcos.com Linux - Networking 7 04-25-2017 05:52 AM
Route eth2 TCP packets to tun0 with IPTABLES & IP RULE/ROUTE Thireus Linux - Networking 4 05-09-2011 12:38 PM
iptables rules to NAT or FORWARD packets between LAN clients templeton Linux - Networking 5 11-28-2010 09:00 AM
IPTables Static NAT, 2 networks, unable to forward packets to Port 80 CommanderKang Linux - Networking 1 08-27-2010 03:35 PM
Route dialup modem packets back through dialup instead of default route cable modem jogress Linux - Networking 1 10-21-2003 03:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 09:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration