Added option to use SNAT nat target instead of MASQUERADE to speed up routing.
[tomato.git] / release / src / linux / linux / net / ipv4 / netfilter / ipt_MASQUERADE.c
blobbf2f2c1699a7d0667d2c2db0c6963cecd25fe6a5
1 /* Masquerade. Simple mapping which alters range to a local IP address
2 (depending on route). */
3 #include <linux/config.h>
4 #include <linux/types.h>
5 #include <linux/ip.h>
6 #include <linux/timer.h>
7 #include <linux/module.h>
8 #include <linux/netfilter.h>
9 #include <net/protocol.h>
10 #include <net/checksum.h>
11 #include <linux/netfilter_ipv4.h>
12 #include <linux/netfilter_ipv4/ip_nat_rule.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
15 #define DEBUGP(format, args...)
17 /* Lock protects masq region inside conntrack */
18 DECLARE_RWLOCK(masq_lock);
20 static int
21 masquerade_check(const char *tablename,
22 const struct ipt_entry *e,
23 void *targinfo,
24 unsigned int targinfosize,
25 unsigned int hook_mask)
27 const struct ip_nat_multi_range *mr = targinfo;
29 if (strcmp(tablename, "nat") != 0) {
30 DEBUGP("masquerade_check: bad table `%s'.\n", table);
31 return 0;
33 if (targinfosize != IPT_ALIGN(sizeof(*mr))) {
34 DEBUGP("masquerade_check: size %u != %u.\n",
35 targinfosize, sizeof(*mr));
36 return 0;
38 if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
39 DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask);
40 return 0;
42 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
43 DEBUGP("masquerade_check: bad MAP_IPS.\n");
44 return 0;
46 if (mr->rangesize != 1) {
47 DEBUGP("masquerade_check: bad rangesize %u.\n", mr->rangesize);
48 return 0;
50 return 1;
53 static unsigned int
54 masquerade_target(struct sk_buff **pskb,
55 unsigned int hooknum,
56 const struct net_device *in,
57 const struct net_device *out,
58 const void *targinfo,
59 void *userinfo)
61 struct ip_conntrack *ct;
62 enum ip_conntrack_info ctinfo;
63 const struct ip_nat_multi_range *mr;
64 struct ip_nat_multi_range newrange;
65 u_int32_t newsrc;
66 struct rtable *rt;
67 struct rt_key key;
69 IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
71 if ((*pskb)->sk)
72 return NF_ACCEPT;
74 ct = ip_conntrack_get(*pskb, &ctinfo);
75 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW
76 || ctinfo == IP_CT_RELATED));
78 mr = targinfo;
80 key.dst = (*pskb)->nh.iph->daddr;
81 key.src = 0; /* Unknown: that's what we're trying to establish */
82 key.tos = RT_TOS((*pskb)->nh.iph->tos)|RTO_CONN;
83 key.oif = out->ifindex;
84 #ifdef CONFIG_IP_ROUTE_FWMARK
85 key.fwmark = (*pskb)->nfmark;
86 #endif
87 if (ip_route_output_key(&rt, &key) != 0) {
88 /* Shouldn't happen */
89 printk("MASQUERADE: No route: Rusty's brain broke!\n");
90 return NF_DROP;
93 newsrc = rt->rt_src;
94 DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
95 ip_rt_put(rt);
97 WRITE_LOCK(&masq_lock);
98 ct->nat.masq_index = out->ifindex;
99 WRITE_UNLOCK(&masq_lock);
101 /* Transfer from original range. */
102 newrange = ((struct ip_nat_multi_range)
103 { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
104 newsrc, newsrc,
105 mr->range[0].min, mr->range[0].max } } });
107 /* Hand modified range to generic setup. */
108 return ip_nat_setup_info(ct, &newrange, hooknum);
111 static inline int
112 device_cmp(const struct ip_conntrack *i, void *ifindex)
114 int ret;
116 READ_LOCK(&masq_lock);
117 ret = (i->nat.masq_index == (int)(long)ifindex);
118 READ_UNLOCK(&masq_lock);
120 return ret;
123 static int masq_device_event(struct notifier_block *this,
124 unsigned long event,
125 void *ptr)
127 struct net_device *dev = ptr;
129 if (event == NETDEV_DOWN) {
130 /* Device was downed. Search entire table for
131 conntracks which were associated with that device,
132 and forget them. */
133 IP_NF_ASSERT(dev->ifindex != 0);
135 ip_ct_selective_cleanup(device_cmp, (void *)(long)dev->ifindex);
138 return NOTIFY_DONE;
141 static int masq_inet_event(struct notifier_block *this,
142 unsigned long event,
143 void *ptr)
145 struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
147 if (event == NETDEV_DOWN) {
148 /* IP address was deleted. Search entire table for
149 conntracks which were associated with that device,
150 and forget them. */
151 IP_NF_ASSERT(dev->ifindex != 0);
153 ip_ct_selective_cleanup(device_cmp, (void *)(long)dev->ifindex);
156 return NOTIFY_DONE;
159 static struct notifier_block masq_dev_notifier = {
160 masq_device_event,
161 NULL,
165 static struct notifier_block masq_inet_notifier = {
166 masq_inet_event,
167 NULL,
171 static struct ipt_target masquerade
172 = { { NULL, NULL }, "MASQUERADE", masquerade_target, masquerade_check, NULL,
173 THIS_MODULE };
175 static int __init init(void)
177 int ret;
179 ret = ipt_register_target(&masquerade);
181 if (ret == 0) {
182 /* Register for device down reports */
183 register_netdevice_notifier(&masq_dev_notifier);
184 /* Register IP address change reports */
185 register_inetaddr_notifier(&masq_inet_notifier);
188 return ret;
191 static void __exit fini(void)
193 ipt_unregister_target(&masquerade);
194 unregister_netdevice_notifier(&masq_dev_notifier);
195 unregister_inetaddr_notifier(&masq_inet_notifier);
198 module_init(init);
199 module_exit(fini);
200 MODULE_LICENSE("GPL");
202 EXPORT_SYMBOL(masq_lock);