pandora: defconfig: update
[pandora-kernel.git] / net / ipv4 / netfilter / nf_nat_standalone.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/types.h>
9 #include <linux/icmp.h>
10 #include <linux/gfp.h>
11 #include <linux/ip.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter_ipv4.h>
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/proc_fs.h>
17 #include <net/ip.h>
18 #include <net/checksum.h>
19 #include <linux/spinlock.h>
20
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_extend.h>
24 #include <net/netfilter/nf_nat.h>
25 #include <net/netfilter/nf_nat_rule.h>
26 #include <net/netfilter/nf_nat_protocol.h>
27 #include <net/netfilter/nf_nat_core.h>
28 #include <net/netfilter/nf_nat_helper.h>
29 #include <linux/netfilter_ipv4/ip_tables.h>
30
31 #ifdef CONFIG_XFRM
32 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
33 {
34         struct flowi4 *fl4 = &fl->u.ip4;
35         const struct nf_conn *ct;
36         const struct nf_conntrack_tuple *t;
37         enum ip_conntrack_info ctinfo;
38         enum ip_conntrack_dir dir;
39         unsigned long statusbit;
40
41         ct = nf_ct_get(skb, &ctinfo);
42         if (ct == NULL)
43                 return;
44         dir = CTINFO2DIR(ctinfo);
45         t = &ct->tuplehash[dir].tuple;
46
47         if (dir == IP_CT_DIR_ORIGINAL)
48                 statusbit = IPS_DST_NAT;
49         else
50                 statusbit = IPS_SRC_NAT;
51
52         if (ct->status & statusbit) {
53                 fl4->daddr = t->dst.u3.ip;
54                 if (t->dst.protonum == IPPROTO_TCP ||
55                     t->dst.protonum == IPPROTO_UDP ||
56                     t->dst.protonum == IPPROTO_UDPLITE ||
57                     t->dst.protonum == IPPROTO_DCCP ||
58                     t->dst.protonum == IPPROTO_SCTP)
59                         fl4->fl4_dport = t->dst.u.tcp.port;
60         }
61
62         statusbit ^= IPS_NAT_MASK;
63
64         if (ct->status & statusbit) {
65                 fl4->saddr = t->src.u3.ip;
66                 if (t->dst.protonum == IPPROTO_TCP ||
67                     t->dst.protonum == IPPROTO_UDP ||
68                     t->dst.protonum == IPPROTO_UDPLITE ||
69                     t->dst.protonum == IPPROTO_DCCP ||
70                     t->dst.protonum == IPPROTO_SCTP)
71                         fl4->fl4_sport = t->src.u.tcp.port;
72         }
73 }
74 #endif
75
76 static unsigned int
77 nf_nat_fn(unsigned int hooknum,
78           struct sk_buff *skb,
79           const struct net_device *in,
80           const struct net_device *out,
81           int (*okfn)(struct sk_buff *))
82 {
83         struct nf_conn *ct;
84         enum ip_conntrack_info ctinfo;
85         struct nf_conn_nat *nat;
86         /* maniptype == SRC for postrouting. */
87         enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum);
88
89         /* We never see fragments: conntrack defrags on pre-routing
90            and local-out, and nf_nat_out protects post-routing. */
91         NF_CT_ASSERT(!ip_is_fragment(ip_hdr(skb)));
92
93         ct = nf_ct_get(skb, &ctinfo);
94         /* Can't track?  It's not due to stress, or conntrack would
95            have dropped it.  Hence it's the user's responsibilty to
96            packet filter it out, or implement conntrack/NAT for that
97            protocol. 8) --RR */
98         if (!ct)
99                 return NF_ACCEPT;
100
101         /* Don't try to NAT if this packet is not conntracked */
102         if (nf_ct_is_untracked(ct))
103                 return NF_ACCEPT;
104
105         nat = nfct_nat(ct);
106         if (!nat) {
107                 /* NAT module was loaded late. */
108                 if (nf_ct_is_confirmed(ct))
109                         return NF_ACCEPT;
110                 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
111                 if (nat == NULL) {
112                         pr_debug("failed to add NAT extension\n");
113                         return NF_ACCEPT;
114                 }
115         }
116
117         switch (ctinfo) {
118         case IP_CT_RELATED:
119         case IP_CT_RELATED_REPLY:
120                 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
121                         if (!nf_nat_icmp_reply_translation(ct, ctinfo,
122                                                            hooknum, skb))
123                                 return NF_DROP;
124                         else
125                                 return NF_ACCEPT;
126                 }
127                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
128         case IP_CT_NEW:
129
130                 /* Seen it before?  This can happen for loopback, retrans,
131                    or local packets.. */
132                 if (!nf_nat_initialized(ct, maniptype)) {
133                         unsigned int ret;
134
135                         ret = nf_nat_rule_find(skb, hooknum, in, out, ct);
136                         if (ret != NF_ACCEPT)
137                                 return ret;
138                 } else
139                         pr_debug("Already setup manip %s for ct %p\n",
140                                  maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
141                                  ct);
142                 break;
143
144         default:
145                 /* ESTABLISHED */
146                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
147                              ctinfo == IP_CT_ESTABLISHED_REPLY);
148         }
149
150         return nf_nat_packet(ct, ctinfo, hooknum, skb);
151 }
152
153 static unsigned int
154 nf_nat_in(unsigned int hooknum,
155           struct sk_buff *skb,
156           const struct net_device *in,
157           const struct net_device *out,
158           int (*okfn)(struct sk_buff *))
159 {
160         unsigned int ret;
161         __be32 daddr = ip_hdr(skb)->daddr;
162
163         ret = nf_nat_fn(hooknum, skb, in, out, okfn);
164         if (ret != NF_DROP && ret != NF_STOLEN &&
165             daddr != ip_hdr(skb)->daddr)
166                 skb_dst_drop(skb);
167
168         return ret;
169 }
170
171 static unsigned int
172 nf_nat_out(unsigned int hooknum,
173            struct sk_buff *skb,
174            const struct net_device *in,
175            const struct net_device *out,
176            int (*okfn)(struct sk_buff *))
177 {
178 #ifdef CONFIG_XFRM
179         const struct nf_conn *ct;
180         enum ip_conntrack_info ctinfo;
181 #endif
182         unsigned int ret;
183
184         /* root is playing with raw sockets. */
185         if (skb->len < sizeof(struct iphdr) ||
186             ip_hdrlen(skb) < sizeof(struct iphdr))
187                 return NF_ACCEPT;
188
189         ret = nf_nat_fn(hooknum, skb, in, out, okfn);
190 #ifdef CONFIG_XFRM
191         if (ret != NF_DROP && ret != NF_STOLEN &&
192             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
193                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
194
195                 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
196                      ct->tuplehash[!dir].tuple.dst.u3.ip) ||
197                     (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
198                      ct->tuplehash[dir].tuple.src.u.all !=
199                      ct->tuplehash[!dir].tuple.dst.u.all)
200                    )
201                         return ip_xfrm_me_harder(skb) == 0 ? ret : NF_DROP;
202         }
203 #endif
204         return ret;
205 }
206
207 static unsigned int
208 nf_nat_local_fn(unsigned int hooknum,
209                 struct sk_buff *skb,
210                 const struct net_device *in,
211                 const struct net_device *out,
212                 int (*okfn)(struct sk_buff *))
213 {
214         const struct nf_conn *ct;
215         enum ip_conntrack_info ctinfo;
216         unsigned int ret;
217
218         /* root is playing with raw sockets. */
219         if (skb->len < sizeof(struct iphdr) ||
220             ip_hdrlen(skb) < sizeof(struct iphdr))
221                 return NF_ACCEPT;
222
223         ret = nf_nat_fn(hooknum, skb, in, out, okfn);
224         if (ret != NF_DROP && ret != NF_STOLEN &&
225             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
226                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
227
228                 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
229                     ct->tuplehash[!dir].tuple.src.u3.ip) {
230                         if (ip_route_me_harder(skb, RTN_UNSPEC))
231                                 ret = NF_DROP;
232                 }
233 #ifdef CONFIG_XFRM
234                 else if (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
235                          ct->tuplehash[dir].tuple.dst.u.all !=
236                          ct->tuplehash[!dir].tuple.src.u.all)
237                         if (ip_xfrm_me_harder(skb))
238                                 ret = NF_DROP;
239 #endif
240         }
241         return ret;
242 }
243
244 /* We must be after connection tracking and before packet filtering. */
245
246 static struct nf_hook_ops nf_nat_ops[] __read_mostly = {
247         /* Before packet filtering, change destination */
248         {
249                 .hook           = nf_nat_in,
250                 .owner          = THIS_MODULE,
251                 .pf             = NFPROTO_IPV4,
252                 .hooknum        = NF_INET_PRE_ROUTING,
253                 .priority       = NF_IP_PRI_NAT_DST,
254         },
255         /* After packet filtering, change source */
256         {
257                 .hook           = nf_nat_out,
258                 .owner          = THIS_MODULE,
259                 .pf             = NFPROTO_IPV4,
260                 .hooknum        = NF_INET_POST_ROUTING,
261                 .priority       = NF_IP_PRI_NAT_SRC,
262         },
263         /* Before packet filtering, change destination */
264         {
265                 .hook           = nf_nat_local_fn,
266                 .owner          = THIS_MODULE,
267                 .pf             = NFPROTO_IPV4,
268                 .hooknum        = NF_INET_LOCAL_OUT,
269                 .priority       = NF_IP_PRI_NAT_DST,
270         },
271         /* After packet filtering, change source */
272         {
273                 .hook           = nf_nat_fn,
274                 .owner          = THIS_MODULE,
275                 .pf             = NFPROTO_IPV4,
276                 .hooknum        = NF_INET_LOCAL_IN,
277                 .priority       = NF_IP_PRI_NAT_SRC,
278         },
279 };
280
281 static int __init nf_nat_standalone_init(void)
282 {
283         int ret = 0;
284
285         need_ipv4_conntrack();
286
287 #ifdef CONFIG_XFRM
288         BUG_ON(ip_nat_decode_session != NULL);
289         RCU_INIT_POINTER(ip_nat_decode_session, nat_decode_session);
290 #endif
291         ret = nf_nat_rule_init();
292         if (ret < 0) {
293                 pr_err("nf_nat_init: can't setup rules.\n");
294                 goto cleanup_decode_session;
295         }
296         ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
297         if (ret < 0) {
298                 pr_err("nf_nat_init: can't register hooks.\n");
299                 goto cleanup_rule_init;
300         }
301         return ret;
302
303  cleanup_rule_init:
304         nf_nat_rule_cleanup();
305  cleanup_decode_session:
306 #ifdef CONFIG_XFRM
307         RCU_INIT_POINTER(ip_nat_decode_session, NULL);
308         synchronize_net();
309 #endif
310         return ret;
311 }
312
313 static void __exit nf_nat_standalone_fini(void)
314 {
315         nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
316         nf_nat_rule_cleanup();
317 #ifdef CONFIG_XFRM
318         RCU_INIT_POINTER(ip_nat_decode_session, NULL);
319         synchronize_net();
320 #endif
321         /* Conntrack caches are unregistered in nf_conntrack_cleanup */
322 }
323
324 module_init(nf_nat_standalone_init);
325 module_exit(nf_nat_standalone_fini);
326
327 MODULE_LICENSE("GPL");
328 MODULE_ALIAS("ip_nat");