Skip to main content

linux - Advanced NAT - Mix PAT with NAT



I've just configured an Ubuntu Server as a router with NAT/PAT, and I'm trying to do the following thing:



I work for a school that has divided its network into smaller networks connected by routers. I need to create an IP address visible on the internal network of one room that would forward all traffic to an external IP address.




To explain, this is the configuration:



NAT outside: eth1 - 192.168.1.254/24



NAT inside: br0 - 192.168.2.10, masquerading enabled



I need to create an address like 192.168.2.1, also for br0, that would forward all its traffic to the ip 192.168.1.1, and would appear as if that IP is directly connected to the network, but would NOT have masquerading enabled on it.



The basic ideea is that the router's address of br0 to be set to 192.168.2.10, and also br0 to have another address that does not masquerade, and forwards all traffic to 192.168.1.1, that being the address of the main router. The reason for this is that br0 is a bridge between the physical network and a VPN that would be accessed from behind a router with the same address as the destination NAT IP address - 192.168.1.1. As I can't assure that clients can always issue a "route add" command, I need a way to circumvent this.




I found out how to configure secondary IP addresses for br0, and I've configured br0:1 as 192.168.2.1, but I can't seem to make all traffic to it forward to 192.168.1.1.



I thank you in advance for your help.


Answer



What you are trying to do, if I am understanding you correctly, is effectively a form of policy routing with some NAT involved. It sounds like you are just looking to create an alias for some host outside a particular routed subnet which is on that subnet.



You can create DNAT rules without corresponding SNAT (or masquerading). The reason this isn't done is merely that most NAT is designed to hack around a connectivity issue whereby the addresses on the "inside" of the NAT wouldn't otherwise be routeable from the "outside". If in fact the host 192.168.1.1 has a route back into 192.168.2.0/24 or whatever it is, you can indeed just set up a DNAT rule if I recall:



iptables -t nat -A PREROUTING -s 192.168.2.0/24 -d 192.168.2.1 -j DNAT --to 192.168.1.1



Now, stop for a moment and don't do that just yet.



That works when 192.168.2.1 is a destination address (and creates an odd quirk whereby the address replying to traffic is different from the original destination). I don't think that's what you want to do, from your description, though I'm honestly not sure. Doing DNAT won't help at all if the 192.168.2.1 host is supposed to act as a router and not as a destination. If it's supposed to act as a router, NAT (or PAT) won't help at all.



It sounds like you have VPN traffic coming in which ends up bridged onto this subnet 192.168.2.0/24 if I am reading you correctly and filling in the blanks as they are, and it needs to get out to the wide world or some subset of it via this gateway 192.168.1.1. In that case, the thing to do is to simply assign 192.168.2.1/24 (which I suppose you have set as the default gateway on the VPN clients) to your router, make sure forwarding is enabled, and you're good to go - the VPN clients don't ever need to worry about the next hop being 192.168.1.1 just like their original default non-VPN gateway (home gateway?); they are completely unaware of it as the packet transits. ISPs use this kind of scheme with RFC1918 addresses all the time to avoid wasting accidentally-scarce routable addresses; it is not a requirement that all the routers along the path have unique addresses at all (though when they do have, it makes troubleshooting a lot easier). The trick here is that the endpoints of a TCP connection don't need to know anything but the IP address of the next hop.



If you're already doing this and you're having trouble, check the return path. Chances are your gateway at 192.168.1.1 isn't doing NAT for those addresses when sending their traffic outside your NAT (which it will see as 192.168.2.0/24 still), is firewalling them, or doesn't have an appropriate route back to 192.168.2.0/24.



If I've misunderstood your intention on both counts, there is a third thing that might help you. The one and only way you can specify a gateway in iptables is with the TEE target, which clones the packet and routes the clone unchanged to some arbitrary host. You'd need to deal with the not-cloned packet somehow, though. That target can be added in the mangle PREROUTING chain:




iptables -t mangle -A PREROUTING -s 192.168.2.0/24 -j TEE --gateway 192.168.1.1


That is a weird thing to do; this is designed for logging traffic, not routing it. You could dispose of the original packet at some later stage of iptables I suppose, though this is just a mess and I really don't recommend it.



As a random point, you can add additional IP addresses to interfaces using the ip addr add command. Don't use net-tools (ifconfig); it is ancient and missing many features and will only give you headaches in the long run.


Comments

Popular posts from this blog

linux - Awstats - outputting stats for merged Access_logs only producing stats for one server's log

I've been attempting this for two weeks and I've accessed countless number of sites on this issue and it seems there is something I'm not getting here and I'm at a lost. I manged to figure out how to merge logs from two servers together. (Taking care to only merge the matching domains together) The logs from the first server span from 15 Dec 2012 to 8 April 2014 The logs from the second server span from 2 Mar 2014 to 9 April 2014 I was able to successfully merge them using the logresolvemerge.pl script simply enermerating each log and > out_putting_it_to_file Looking at the two logs from each server the format seems exactly the same. The problem I'm having is producing the stats page for the logs. The command I've boiled it down to is /usr/share/awstats/tools/awstats_buildstaticpages.pl -configdir=/home/User/Documents/conf/ -config=example.com awstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl dir=/home/User/Documents/parced -month=all -year=all...

iLO 3 Firmware Update (HP Proliant DL380 G7)

The iLO web interface allows me to upload a .bin file ( Obtain the firmware image (.bin) file from the Online ROM Flash Component for HP Integrated Lights-Out. ) The iLO web interface redirects me to a page in the HP support website ( http://www.hp.com/go/iLO ) where I am supposed to find this .bin firmware, but no luck for me. The support website is a mess and very slow, badly categorized and generally unusable. Where can I find this .bin file? The only related link I am able to find asks me about my server operating system (what does this have to do with the iLO?!) and lets me download an .iso with no .bin file And also a related question: what is the latest iLO 3 version? (for Proliant DL380 G7, not sure if the iLO is tied to the server model)

hp proliant - Smart Array P822 with HBA Mode?

We get an HP DL360 G8 with an Smart Array P822 controller. On that controller will come a HP StorageWorks D2700 . Does anybody know, that it is possible to run the Smart Array P822 in HBA mode? I found only information about the P410i, who can run HBA. If this is not supported, what you think about the LSI 9207-8e controller? Will this fit good in that setup? The Hardware we get is used but all original from HP. The StorageWorks has 25 x 900 GB SAS 10K disks. Because the disks are not new I would like to use only 22 for raid6, and the rest for spare (I need to see if the disk count is optimal or not for zfs). It would be nice if I'm not stick to SAS in future. As OS I would like to install debian stretch with zfs 0.71 as file system and software raid. I have see that hp has an page for debian to. I would like to use hba mode because it is recommend, that zfs know at most as possible about the disk, and I'm independent from the raid controller. For us zfs have many benefits, ...