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 - iDRAC6 Virtual Media native library cannot be loaded

When attempting to mount Virtual Media on a iDRAC6 IP KVM session I get the following error: I'm using Ubuntu 9.04 and: $ javaws -version Java(TM) Web Start 1.6.0_16 $ uname -a Linux aud22419-linux 2.6.28-15-generic #51-Ubuntu SMP Mon Aug 31 13:39:06 UTC 2009 x86_64 GNU/Linux $ firefox -version Mozilla Firefox 3.0.14, Copyright (c) 1998 - 2009 mozilla.org On Windows + IE it (unsurprisingly) works. I've just gotten off the phone with the Dell tech support and I was told it is known to work on Linux + Firefox, albeit Ubuntu is not supported (by Dell, that is). Has anyone out there managed to mount virtual media in the same scenario?

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,

apache 2.2 - Server Potentially Compromised -- c99madshell

So, low and behold, a legacy site we've been hosting for a client had a version of FCKEditor that allowed someone to upload the dreaded c99madshell exploit onto our web host. I'm not a big security buff -- frankly I'm just a dev currently responsible for S/A duties due to a loss of personnel. Accordingly, I'd love any help you server-faulters could provide in assessing the damage from the exploit. To give you a bit of information: The file was uploaded into a directory within the webroot, "/_img/fck_uploads/File/". The Apache user and group are restricted such that they can't log in and don't have permissions outside of the directory from which we serve sites. All the files had 770 permissions (user rwx, group rwx, other none) -- something I wanted to fix but was told to hold off on as it wasn't "high priority" (hopefully this changes that). So it seems the hackers could've easily executed the script. Now I wasn't able