VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / ipv4 / netfilter / ipt_MASQUERADE.c
1 /* Masquerade.  Simple mapping which alters range to a local IP address
2    (depending on route). */
3
4 /* (C) 1999-2001 Paul `Rusty' Russell
5  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/ip.h>
15 #include <linux/timer.h>
16 #include <linux/module.h>
17 #include <linux/netfilter.h>
18 #include <net/protocol.h>
19 #include <net/ip.h>
20 #include <net/checksum.h>
21 #include <linux/netfilter_ipv4.h>
22 #include <linux/netfilter_ipv4/ip_nat_rule.h>
23 #include <linux/netfilter_ipv4/ip_tables.h>
24
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
27 MODULE_DESCRIPTION("iptables MASQUERADE target module");
28
29 #if 0
30 #define DEBUGP printk
31 #else
32 #define DEBUGP(format, args...)
33 #endif
34
35 /* Lock protects masq region inside conntrack */
36 static DECLARE_RWLOCK(masq_lock);
37
38 /* FIXME: Multiple targets. --RR */
39 static int
40 masquerade_check(const char *tablename,
41                  const struct ipt_entry *e,
42                  void *targinfo,
43                  unsigned int targinfosize,
44                  unsigned int hook_mask)
45 {
46         const struct ip_nat_multi_range *mr = targinfo;
47
48         if (strcmp(tablename, "nat") != 0) {
49                 DEBUGP("masquerade_check: bad table `%s'.\n", tablename);
50                 return 0;
51         }
52         if (targinfosize != IPT_ALIGN(sizeof(*mr))) {
53                 DEBUGP("masquerade_check: size %u != %u.\n",
54                        targinfosize, sizeof(*mr));
55                 return 0;
56         }
57         if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
58                 DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask);
59                 return 0;
60         }
61         if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
62                 DEBUGP("masquerade_check: bad MAP_IPS.\n");
63                 return 0;
64         }
65         if (mr->rangesize != 1) {
66                 DEBUGP("masquerade_check: bad rangesize %u.\n", mr->rangesize);
67                 return 0;
68         }
69         return 1;
70 }
71
72 static unsigned int
73 masquerade_target(struct sk_buff **pskb,
74                   const struct net_device *in,
75                   const struct net_device *out,
76                   unsigned int hooknum,
77                   const void *targinfo,
78                   void *userinfo)
79 {
80         struct ip_conntrack *ct;
81         enum ip_conntrack_info ctinfo;
82         const struct ip_nat_multi_range *mr;
83         struct ip_nat_multi_range newrange;
84         u_int32_t newsrc;
85         struct rtable *rt;
86
87         IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
88
89         /* FIXME: For the moment, don't do local packets, breaks
90            testsuite for 2.3.49 --RR */
91         if ((*pskb)->sk)
92                 return NF_ACCEPT;
93
94         ct = ip_conntrack_get(*pskb, &ctinfo);
95         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW
96                                   || ctinfo == IP_CT_RELATED));
97
98         mr = targinfo;
99
100         {
101                 struct flowi fl = { .nl_u = { .ip4_u =
102                                               { .daddr = (*pskb)->nh.iph->daddr,
103                                                 .tos = (RT_TOS((*pskb)->nh.iph->tos) |
104                                                         RTO_CONN),
105 #ifdef CONFIG_IP_ROUTE_FWMARK
106                                                 .fwmark = (*pskb)->nfmark
107 #endif
108                                               } } };
109                 if (ip_route_output_key(&rt, &fl) != 0) {
110                         /* Funky routing can do this. */
111                         if (net_ratelimit())
112                                 printk("MASQUERADE:"
113                                        " No route: Rusty's brain broke!\n");
114                         return NF_DROP;
115                 }
116                 if (rt->u.dst.dev != out) {
117                         if (net_ratelimit())
118                                 printk("MASQUERADE:"
119                                        " Route sent us somewhere else.\n");
120                         ip_rt_put(rt);
121                         return NF_DROP;
122                 }
123         }
124
125         newsrc = rt->rt_src;
126         DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
127         ip_rt_put(rt);
128
129         WRITE_LOCK(&masq_lock);
130         ct->nat.masq_index = out->ifindex;
131         WRITE_UNLOCK(&masq_lock);
132
133         /* Transfer from original range. */
134         newrange = ((struct ip_nat_multi_range)
135                 { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
136                          newsrc, newsrc,
137                          mr->range[0].min, mr->range[0].max } } });
138
139         /* Hand modified range to generic setup. */
140         return ip_nat_setup_info(ct, &newrange, hooknum);
141 }
142
143 static inline int
144 device_cmp(const struct ip_conntrack *i, void *_ina)
145 {
146         int ret = 0;
147         struct in_ifaddr *ina = _ina;
148
149         READ_LOCK(&masq_lock);
150         /* If it's masquerading out this interface with a different address,
151            or we don't know the new address of this interface. */
152         if (i->nat.masq_index == ina->ifa_dev->dev->ifindex
153             && i->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip != ina->ifa_address)
154                 ret = 1;
155         READ_UNLOCK(&masq_lock);
156
157         return ret;
158 }
159
160 static int masq_inet_event(struct notifier_block *this,
161                            unsigned long event,
162                            void *ptr)
163 {
164         /* For some configurations, interfaces often come back with
165          * the same address.  If not, clean up old conntrack
166          * entries. */
167         if (event == NETDEV_UP)
168                 ip_ct_selective_cleanup(device_cmp, ptr);
169
170         return NOTIFY_DONE;
171 }
172
173 static struct notifier_block masq_inet_notifier = {
174         .notifier_call  = masq_inet_event,
175 };
176
177 static struct ipt_target masquerade = {
178         .name           = "MASQUERADE",
179         .target         = masquerade_target,
180         .checkentry     = masquerade_check,
181         .me             = THIS_MODULE,
182 };
183
184 static int __init init(void)
185 {
186         int ret;
187
188         ret = ipt_register_target(&masquerade);
189
190         if (ret == 0)
191                 /* Register IP address change reports */
192                 register_inetaddr_notifier(&masq_inet_notifier);
193
194         return ret;
195 }
196
197 static void __exit fini(void)
198 {
199         ipt_unregister_target(&masquerade);
200         unregister_inetaddr_notifier(&masq_inet_notifier);      
201 }
202
203 module_init(init);
204 module_exit(fini);