]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/netfilter/nf_nat_rule.c
net: replace NIPQUAD() in net/ipv4/netfilter/
[linux-2.6-omap-h63xx.git] / net / ipv4 / netfilter / nf_nat_rule.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
9 /* Everything about the rules for NAT. */
10 #include <linux/types.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/kmod.h>
16 #include <linux/skbuff.h>
17 #include <linux/proc_fs.h>
18 #include <net/checksum.h>
19 #include <net/route.h>
20 #include <linux/bitops.h>
21
22 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_core.h>
25 #include <net/netfilter/nf_nat_rule.h>
26
27 #define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
28                          (1 << NF_INET_POST_ROUTING) | \
29                          (1 << NF_INET_LOCAL_OUT))
30
31 static struct
32 {
33         struct ipt_replace repl;
34         struct ipt_standard entries[3];
35         struct ipt_error term;
36 } nat_initial_table __net_initdata = {
37         .repl = {
38                 .name = "nat",
39                 .valid_hooks = NAT_VALID_HOOKS,
40                 .num_entries = 4,
41                 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
42                 .hook_entry = {
43                         [NF_INET_PRE_ROUTING] = 0,
44                         [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard),
45                         [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
46                 },
47                 .underflow = {
48                         [NF_INET_PRE_ROUTING] = 0,
49                         [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard),
50                         [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
51                 },
52         },
53         .entries = {
54                 IPT_STANDARD_INIT(NF_ACCEPT),   /* PRE_ROUTING */
55                 IPT_STANDARD_INIT(NF_ACCEPT),   /* POST_ROUTING */
56                 IPT_STANDARD_INIT(NF_ACCEPT),   /* LOCAL_OUT */
57         },
58         .term = IPT_ERROR_INIT,                 /* ERROR */
59 };
60
61 static struct xt_table nat_table = {
62         .name           = "nat",
63         .valid_hooks    = NAT_VALID_HOOKS,
64         .lock           = __RW_LOCK_UNLOCKED(__nat_table.lock),
65         .me             = THIS_MODULE,
66         .af             = AF_INET,
67 };
68
69 /* Source NAT */
70 static unsigned int
71 ipt_snat_target(struct sk_buff *skb, const struct xt_target_param *par)
72 {
73         struct nf_conn *ct;
74         enum ip_conntrack_info ctinfo;
75         const struct nf_nat_multi_range_compat *mr = par->targinfo;
76
77         NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
78
79         ct = nf_ct_get(skb, &ctinfo);
80
81         /* Connection must be valid and new. */
82         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
83                             ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
84         NF_CT_ASSERT(par->out != NULL);
85
86         return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
87 }
88
89 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
90 static void warn_if_extra_mangle(struct net *net, __be32 dstip, __be32 srcip)
91 {
92         static int warned = 0;
93         struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
94         struct rtable *rt;
95
96         if (ip_route_output_key(net, &rt, &fl) != 0)
97                 return;
98
99         if (rt->rt_src != srcip && !warned) {
100                 printk("NAT: no longer support implicit source local NAT\n");
101                 printk("NAT: packet src %pI4 -> dst %pI4\n", &srcip, &dstip);
102                 warned = 1;
103         }
104         ip_rt_put(rt);
105 }
106
107 static unsigned int
108 ipt_dnat_target(struct sk_buff *skb, const struct xt_target_param *par)
109 {
110         struct nf_conn *ct;
111         enum ip_conntrack_info ctinfo;
112         const struct nf_nat_multi_range_compat *mr = par->targinfo;
113
114         NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
115                      par->hooknum == NF_INET_LOCAL_OUT);
116
117         ct = nf_ct_get(skb, &ctinfo);
118
119         /* Connection must be valid and new. */
120         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
121
122         if (par->hooknum == NF_INET_LOCAL_OUT &&
123             mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
124                 warn_if_extra_mangle(dev_net(par->out), ip_hdr(skb)->daddr,
125                                      mr->range[0].min_ip);
126
127         return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST);
128 }
129
130 static bool ipt_snat_checkentry(const struct xt_tgchk_param *par)
131 {
132         const struct nf_nat_multi_range_compat *mr = par->targinfo;
133
134         /* Must be a valid range */
135         if (mr->rangesize != 1) {
136                 printk("SNAT: multiple ranges no longer supported\n");
137                 return false;
138         }
139         return true;
140 }
141
142 static bool ipt_dnat_checkentry(const struct xt_tgchk_param *par)
143 {
144         const struct nf_nat_multi_range_compat *mr = par->targinfo;
145
146         /* Must be a valid range */
147         if (mr->rangesize != 1) {
148                 printk("DNAT: multiple ranges no longer supported\n");
149                 return false;
150         }
151         return true;
152 }
153
154 unsigned int
155 alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
156 {
157         /* Force range to this IP; let proto decide mapping for
158            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
159            Use reply in case it's already been mangled (eg local packet).
160         */
161         __be32 ip
162                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
163                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
164                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
165         struct nf_nat_range range
166                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
167
168         pr_debug("Allocating NULL binding for %p (%pI4)\n", ct, &ip);
169         return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
170 }
171
172 int nf_nat_rule_find(struct sk_buff *skb,
173                      unsigned int hooknum,
174                      const struct net_device *in,
175                      const struct net_device *out,
176                      struct nf_conn *ct)
177 {
178         struct net *net = nf_ct_net(ct);
179         int ret;
180
181         ret = ipt_do_table(skb, hooknum, in, out, net->ipv4.nat_table);
182
183         if (ret == NF_ACCEPT) {
184                 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
185                         /* NUL mapping */
186                         ret = alloc_null_binding(ct, hooknum);
187         }
188         return ret;
189 }
190
191 static struct xt_target ipt_snat_reg __read_mostly = {
192         .name           = "SNAT",
193         .target         = ipt_snat_target,
194         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
195         .table          = "nat",
196         .hooks          = 1 << NF_INET_POST_ROUTING,
197         .checkentry     = ipt_snat_checkentry,
198         .family         = AF_INET,
199 };
200
201 static struct xt_target ipt_dnat_reg __read_mostly = {
202         .name           = "DNAT",
203         .target         = ipt_dnat_target,
204         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
205         .table          = "nat",
206         .hooks          = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
207         .checkentry     = ipt_dnat_checkentry,
208         .family         = AF_INET,
209 };
210
211 static int __net_init nf_nat_rule_net_init(struct net *net)
212 {
213         net->ipv4.nat_table = ipt_register_table(net, &nat_table,
214                                                  &nat_initial_table.repl);
215         if (IS_ERR(net->ipv4.nat_table))
216                 return PTR_ERR(net->ipv4.nat_table);
217         return 0;
218 }
219
220 static void __net_exit nf_nat_rule_net_exit(struct net *net)
221 {
222         ipt_unregister_table(net->ipv4.nat_table);
223 }
224
225 static struct pernet_operations nf_nat_rule_net_ops = {
226         .init = nf_nat_rule_net_init,
227         .exit = nf_nat_rule_net_exit,
228 };
229
230 int __init nf_nat_rule_init(void)
231 {
232         int ret;
233
234         ret = register_pernet_subsys(&nf_nat_rule_net_ops);
235         if (ret != 0)
236                 goto out;
237         ret = xt_register_target(&ipt_snat_reg);
238         if (ret != 0)
239                 goto unregister_table;
240
241         ret = xt_register_target(&ipt_dnat_reg);
242         if (ret != 0)
243                 goto unregister_snat;
244
245         return ret;
246
247  unregister_snat:
248         xt_unregister_target(&ipt_snat_reg);
249  unregister_table:
250         unregister_pernet_subsys(&nf_nat_rule_net_ops);
251  out:
252         return ret;
253 }
254
255 void nf_nat_rule_cleanup(void)
256 {
257         xt_unregister_target(&ipt_dnat_reg);
258         xt_unregister_target(&ipt_snat_reg);
259         unregister_pernet_subsys(&nf_nat_rule_net_ops);
260 }