Skip to contentSkip to conversation

multiple NAT on one box

DISCUSSION Kristof Cauwel 2 participants 2 messages Nov 16, 2003 netfilter

lore
Share & tools
lore
#0 lore raw

Hi boys and girls,

Just found this mailing list and I sure hope you are going to be able to help me... I'll get started right away by describing the situation:
I have (let's say) 2 home networks and 1 linux gateway (kernel 2.4.22). Actually the 2 networks are part of the same physical network, but they are seperated by means of VLAN tagging. So it looks a bit like this:

NETWORK 1 NETWORK 2
------------------ ------------------

         |                                     |
      vlan1                             vlan2
------------------------------------------------
|             LINUX NAT BOX               |
------------------------------------------------
                   eth0:1/eth0:2
                            |
                    ---------------
                    INTERNET

--> vlan1 and vlan2 are virtual interfaces on eth1 (VLAN tagging enabled) --> eth0:1 and eth0:2 are aliases of eth0

So I have 2 IP adresses towards the internet (one to serve network 1 and one to serve network 2) and 2 different networks on my LAN side. What I want to do is NAT all trafic comming from network 1 on the first IP and NAT all trafic comming from network 2 on the second IP. To make my life a bit easier, I've made sure that each VLAN corresponds with a whole subnet. I know that you can do this stuff with the NAT module that excists but this is a simplification of what I have to do. The idea is to create a virtual home gateway that serves thousands of families at a time (I don't have commercial plans with this, it's my thesis subject at university, just bo be clear on this). So you would have way more than 2 networks on the LAN side and I'm a bit afraid that the single nat table is gonna become too big (and thus to slow to search in). It will also be almost impossible to remove a user's nat rules if he disconnects from the service...
My idea was to have some sort of intermediate NAT table. If I check where a packet came from or where it's going to (depends if it's going to or comming from the internet), I can identify it with a user. Each user would have it's own user-nat table (bit like user tables in the firewalling part of iptables). In the intermediate table there would be some sort of pointer to the user table. I hope you are getting the idea....

I've been browsing through the code and the howto's all weekend, but without huge succes I'm afraid :-( First off all, where is the nat table searched? I found the place where it's registered and I found a lot of code concerning some hash table (or is this the code where you try to find a hit in the nat table?). Am I correct in assuming that I would have to change code in not only the nat part but also the conntrack part (if so, where is the table updated in the conntrack code)?

Hope you don't think I'm lazy or anything but I'm sure you can understand that it's not easy to be thrown in all that code... I'm just a newbie in this sort of stuff, trying to survive in the code jungle :-)

Thanks for any comments, answers, ...
kristof

#1 lore raw
Quoted 4 lines · Kristof Cauwel

So I have 2 IP adresses towards the internet (one to serve network 1 and one to serve network 2) and 2 different networks on my LAN side. What I want to do is NAT all trafic comming from network 1 on the first IP and NAT all trafic comming from network 2 on the second IP.

I assume the two VLAN are using different IP networks? If so then just make suitable SNAT rules in POSTROUTING selecting the IP address to NAT as based on the source IP address.

iptables -t nat -A POSTROUTING -s net.of.vlan.1/mask -j SNAT --to-source ip.for.vlan.1

or similar rules depending on how you want the clients to be NAT:ed.

If you have overlapping IP addresses on the VLANs then things get a little more complex.

To have the traffic routed correctly you need to use policy routing based on the original source addresses and the "external" addresses. This part is actually mostly independent of the NAT question. See the Linux advanced routing howto or the iproute2 documentation for details.

I don't see anything in what you discuss where it is apparent that iptables needs extensions to handle what you are doing in a clean manner. The 'nat' table supports custom chains just as the 'filter' table does.

You do not need to worry about the sizes of the conntrack table (includes NAT of existing sessions). Your only concern there is to make sure the conntrack hash size is at least sufficiently large for the amount of concurrent sessions you need to support.

For administrative reasons I would recommend making your NAT rules as general as possible, deferring access controls to the filter table only.

If I have overlooked anything of what you are doing then please don't be afraid to try to get it thru to my thick brain..

Regards
Henrik