netfilter: nf_nat: add protoff argument to packet mangling functions
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_standalone.c
CommitLineData
5b1158e9
JK
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>
5a0e3ad6 10#include <linux/gfp.h>
5b1158e9
JK
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>
2d59e5ca 23#include <net/netfilter/nf_conntrack_extend.h>
5b1158e9
JK
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
5b1158e9
JK
31#ifdef CONFIG_XFRM
32static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
33{
3073e5ab 34 struct flowi4 *fl4 = &fl->u.ip4;
72b72949
JE
35 const struct nf_conn *ct;
36 const struct nf_conntrack_tuple *t;
5b1158e9
JK
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) {
3073e5ab 53 fl4->daddr = t->dst.u3.ip;
5b1158e9 54 if (t->dst.protonum == IPPROTO_TCP ||
6185f870 55 t->dst.protonum == IPPROTO_UDP ||
4910a087 56 t->dst.protonum == IPPROTO_UDPLITE ||
9d908a69
PM
57 t->dst.protonum == IPPROTO_DCCP ||
58 t->dst.protonum == IPPROTO_SCTP)
9cce96df 59 fl4->fl4_dport = t->dst.u.tcp.port;
5b1158e9
JK
60 }
61
62 statusbit ^= IPS_NAT_MASK;
63
64 if (ct->status & statusbit) {
3073e5ab 65 fl4->saddr = t->src.u3.ip;
5b1158e9 66 if (t->dst.protonum == IPPROTO_TCP ||
6185f870 67 t->dst.protonum == IPPROTO_UDP ||
4910a087 68 t->dst.protonum == IPPROTO_UDPLITE ||
9d908a69
PM
69 t->dst.protonum == IPPROTO_DCCP ||
70 t->dst.protonum == IPPROTO_SCTP)
9cce96df 71 fl4->fl4_sport = t->src.u.tcp.port;
5b1158e9
JK
72 }
73}
74#endif
75
76static unsigned int
77nf_nat_fn(unsigned int hooknum,
3db05fea 78 struct sk_buff *skb,
5b1158e9
JK
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;
5b1158e9
JK
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. */
56f8a75c 91 NF_CT_ASSERT(!ip_is_fragment(ip_hdr(skb)));
5b1158e9 92
3db05fea 93 ct = nf_ct_get(skb, &ctinfo);
5b1158e9
JK
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 */
42cf800c 98 if (!ct)
5b1158e9 99 return NF_ACCEPT;
5b1158e9
JK
100
101 /* Don't try to NAT if this packet is not conntracked */
5bfddbd4 102 if (nf_ct_is_untracked(ct))
5b1158e9
JK
103 return NF_ACCEPT;
104
105 nat = nfct_nat(ct);
2d59e5ca 106 if (!nat) {
8c87238b
PM
107 /* NAT module was loaded late. */
108 if (nf_ct_is_confirmed(ct))
109 return NF_ACCEPT;
2d59e5ca
YK
110 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
111 if (nat == NULL) {
0d53778e 112 pr_debug("failed to add NAT extension\n");
2d59e5ca
YK
113 return NF_ACCEPT;
114 }
115 }
5b1158e9
JK
116
117 switch (ctinfo) {
118 case IP_CT_RELATED:
fb048833 119 case IP_CT_RELATED_REPLY:
3db05fea 120 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
5b1158e9 121 if (!nf_nat_icmp_reply_translation(ct, ctinfo,
3db05fea 122 hooknum, skb))
5b1158e9
JK
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:
5b1158e9
JK
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
c68cd6cc 135 ret = nf_nat_rule_find(skb, hooknum, in, out, ct);
22068311 136 if (ret != NF_ACCEPT)
5b1158e9 137 return ret;
5b1158e9 138 } else
0d53778e 139 pr_debug("Already setup manip %s for ct %p\n",
cbc9f2f4 140 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
0d53778e 141 ct);
5b1158e9
JK
142 break;
143
144 default:
145 /* ESTABLISHED */
146 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
fb048833 147 ctinfo == IP_CT_ESTABLISHED_REPLY);
5b1158e9
JK
148 }
149
3db05fea 150 return nf_nat_packet(ct, ctinfo, hooknum, skb);
5b1158e9
JK
151}
152
153static unsigned int
154nf_nat_in(unsigned int hooknum,
3db05fea 155 struct sk_buff *skb,
e905a9ed
YH
156 const struct net_device *in,
157 const struct net_device *out,
158 int (*okfn)(struct sk_buff *))
5b1158e9
JK
159{
160 unsigned int ret;
3db05fea 161 __be32 daddr = ip_hdr(skb)->daddr;
5b1158e9 162
3db05fea 163 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
5b1158e9 164 if (ret != NF_DROP && ret != NF_STOLEN &&
adf30907
ED
165 daddr != ip_hdr(skb)->daddr)
166 skb_dst_drop(skb);
167
5b1158e9
JK
168 return ret;
169}
170
171static unsigned int
172nf_nat_out(unsigned int hooknum,
3db05fea 173 struct sk_buff *skb,
5b1158e9
JK
174 const struct net_device *in,
175 const struct net_device *out,
176 int (*okfn)(struct sk_buff *))
177{
178#ifdef CONFIG_XFRM
72b72949 179 const struct nf_conn *ct;
5b1158e9
JK
180 enum ip_conntrack_info ctinfo;
181#endif
182 unsigned int ret;
183
184 /* root is playing with raw sockets. */
3db05fea
HX
185 if (skb->len < sizeof(struct iphdr) ||
186 ip_hdrlen(skb) < sizeof(struct iphdr))
5b1158e9
JK
187 return NF_ACCEPT;
188
3db05fea 189 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
5b1158e9
JK
190#ifdef CONFIG_XFRM
191 if (ret != NF_DROP && ret != NF_STOLEN &&
3db05fea 192 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
5b1158e9
JK
193 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
194
3666ed1c
JP
195 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
196 ct->tuplehash[!dir].tuple.dst.u3.ip) ||
197 (ct->tuplehash[dir].tuple.src.u.all !=
198 ct->tuplehash[!dir].tuple.dst.u.all)
199 )
3db05fea 200 return ip_xfrm_me_harder(skb) == 0 ? ret : NF_DROP;
5b1158e9
JK
201 }
202#endif
203 return ret;
204}
205
206static unsigned int
207nf_nat_local_fn(unsigned int hooknum,
3db05fea 208 struct sk_buff *skb,
5b1158e9
JK
209 const struct net_device *in,
210 const struct net_device *out,
211 int (*okfn)(struct sk_buff *))
212{
72b72949 213 const struct nf_conn *ct;
5b1158e9
JK
214 enum ip_conntrack_info ctinfo;
215 unsigned int ret;
216
217 /* root is playing with raw sockets. */
3db05fea
HX
218 if (skb->len < sizeof(struct iphdr) ||
219 ip_hdrlen(skb) < sizeof(struct iphdr))
5b1158e9
JK
220 return NF_ACCEPT;
221
3db05fea 222 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
5b1158e9 223 if (ret != NF_DROP && ret != NF_STOLEN &&
3db05fea 224 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
5b1158e9
JK
225 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
226
227 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
848c29fd 228 ct->tuplehash[!dir].tuple.src.u3.ip) {
3db05fea 229 if (ip_route_me_harder(skb, RTN_UNSPEC))
5b1158e9 230 ret = NF_DROP;
848c29fd
PM
231 }
232#ifdef CONFIG_XFRM
233 else if (ct->tuplehash[dir].tuple.dst.u.all !=
234 ct->tuplehash[!dir].tuple.src.u.all)
3db05fea 235 if (ip_xfrm_me_harder(skb))
848c29fd
PM
236 ret = NF_DROP;
237#endif
5b1158e9
JK
238 }
239 return ret;
240}
241
5b1158e9
JK
242/* We must be after connection tracking and before packet filtering. */
243
1999414a 244static struct nf_hook_ops nf_nat_ops[] __read_mostly = {
5b1158e9
JK
245 /* Before packet filtering, change destination */
246 {
247 .hook = nf_nat_in,
248 .owner = THIS_MODULE,
24c232d8 249 .pf = NFPROTO_IPV4,
6e23ae2a 250 .hooknum = NF_INET_PRE_ROUTING,
5b1158e9
JK
251 .priority = NF_IP_PRI_NAT_DST,
252 },
253 /* After packet filtering, change source */
254 {
255 .hook = nf_nat_out,
256 .owner = THIS_MODULE,
24c232d8 257 .pf = NFPROTO_IPV4,
6e23ae2a 258 .hooknum = NF_INET_POST_ROUTING,
5b1158e9
JK
259 .priority = NF_IP_PRI_NAT_SRC,
260 },
5b1158e9
JK
261 /* Before packet filtering, change destination */
262 {
263 .hook = nf_nat_local_fn,
264 .owner = THIS_MODULE,
24c232d8 265 .pf = NFPROTO_IPV4,
6e23ae2a 266 .hooknum = NF_INET_LOCAL_OUT,
5b1158e9
JK
267 .priority = NF_IP_PRI_NAT_DST,
268 },
269 /* After packet filtering, change source */
270 {
271 .hook = nf_nat_fn,
272 .owner = THIS_MODULE,
24c232d8 273 .pf = NFPROTO_IPV4,
6e23ae2a 274 .hooknum = NF_INET_LOCAL_IN,
5b1158e9
JK
275 .priority = NF_IP_PRI_NAT_SRC,
276 },
5b1158e9
JK
277};
278
279static int __init nf_nat_standalone_init(void)
280{
2d59e5ca 281 int ret = 0;
5b1158e9 282
591e6206 283 need_ipv4_conntrack();
5b1158e9 284
5b1158e9
JK
285#ifdef CONFIG_XFRM
286 BUG_ON(ip_nat_decode_session != NULL);
a9b3cd7f 287 RCU_INIT_POINTER(ip_nat_decode_session, nat_decode_session);
5b1158e9
JK
288#endif
289 ret = nf_nat_rule_init();
290 if (ret < 0) {
654d0fbd 291 pr_err("nf_nat_init: can't setup rules.\n");
5b1158e9
JK
292 goto cleanup_decode_session;
293 }
294 ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
295 if (ret < 0) {
654d0fbd 296 pr_err("nf_nat_init: can't register hooks.\n");
5b1158e9
JK
297 goto cleanup_rule_init;
298 }
5b1158e9
JK
299 return ret;
300
301 cleanup_rule_init:
302 nf_nat_rule_cleanup();
303 cleanup_decode_session:
304#ifdef CONFIG_XFRM
a9b3cd7f 305 RCU_INIT_POINTER(ip_nat_decode_session, NULL);
5b1158e9
JK
306 synchronize_net();
307#endif
5b1158e9
JK
308 return ret;
309}
310
311static void __exit nf_nat_standalone_fini(void)
312{
313 nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
314 nf_nat_rule_cleanup();
5b1158e9 315#ifdef CONFIG_XFRM
a9b3cd7f 316 RCU_INIT_POINTER(ip_nat_decode_session, NULL);
5b1158e9
JK
317 synchronize_net();
318#endif
319 /* Conntrack caches are unregistered in nf_conntrack_cleanup */
320}
321
322module_init(nf_nat_standalone_init);
323module_exit(nf_nat_standalone_fini);
324
325MODULE_LICENSE("GPL");
326MODULE_ALIAS("ip_nat");
This page took 0.607147 seconds and 5 git commands to generate.