2 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
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.
8 * Based on Rusty Russell's IPv4 NAT code. Development of IPv6 NAT
12 #include <linux/module.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter_ipv6.h>
15 #include <linux/netfilter_ipv6/ip6_tables.h>
16 #include <linux/ipv6.h>
19 #include <net/netfilter/nf_nat.h>
20 #include <net/netfilter/nf_nat_core.h>
21 #include <net/netfilter/nf_nat_l3proto.h>
23 static const struct xt_table nf_nat_ipv6_table = {
25 .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
26 (1 << NF_INET_POST_ROUTING) |
27 (1 << NF_INET_LOCAL_OUT) |
28 (1 << NF_INET_LOCAL_IN),
33 static unsigned int alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
35 /* Force range to this IP; let proto decide mapping for
36 * per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
38 struct nf_nat_range range;
41 pr_debug("Allocating NULL binding for %p (%pI6)\n", ct,
42 HOOK2MANIP(hooknum) == NF_NAT_MANIP_SRC ?
43 &ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip6 :
44 &ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip6);
46 return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
49 static unsigned int nf_nat_rule_find(struct sk_buff *skb, unsigned int hooknum,
50 const struct net_device *in,
51 const struct net_device *out,
54 struct net *net = nf_ct_net(ct);
57 ret = ip6t_do_table(skb, hooknum, in, out, net->ipv6.ip6table_nat);
58 if (ret == NF_ACCEPT) {
59 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
60 ret = alloc_null_binding(ct, hooknum);
66 nf_nat_ipv6_fn(const struct nf_hook_ops *ops,
68 const struct net_device *in,
69 const struct net_device *out,
70 int (*okfn)(struct sk_buff *))
73 enum ip_conntrack_info ctinfo;
74 struct nf_conn_nat *nat;
75 enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
80 ct = nf_ct_get(skb, &ctinfo);
81 /* Can't track? It's not due to stress, or conntrack would
82 * have dropped it. Hence it's the user's responsibilty to
83 * packet filter it out, or implement conntrack/NAT for that
89 /* Don't try to NAT if this packet is not conntracked */
90 if (nf_ct_is_untracked(ct))
95 /* NAT module was loaded late. */
96 if (nf_ct_is_confirmed(ct))
98 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
100 pr_debug("failed to add NAT extension\n");
107 case IP_CT_RELATED_REPLY:
108 nexthdr = ipv6_hdr(skb)->nexthdr;
109 hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
110 &nexthdr, &frag_off);
112 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
113 if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
120 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
122 /* Seen it before? This can happen for loopback, retrans,
125 if (!nf_nat_initialized(ct, maniptype)) {
128 ret = nf_nat_rule_find(skb, ops->hooknum, in, out, ct);
129 if (ret != NF_ACCEPT)
132 pr_debug("Already setup manip %s for ct %p\n",
133 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
135 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
142 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
143 ctinfo == IP_CT_ESTABLISHED_REPLY);
144 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
148 return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
151 nf_ct_kill_acct(ct, ctinfo, skb);
156 nf_nat_ipv6_in(const struct nf_hook_ops *ops,
158 const struct net_device *in,
159 const struct net_device *out,
160 int (*okfn)(struct sk_buff *))
163 struct in6_addr daddr = ipv6_hdr(skb)->daddr;
165 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
166 if (ret != NF_DROP && ret != NF_STOLEN &&
167 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
174 nf_nat_ipv6_out(const struct nf_hook_ops *ops,
176 const struct net_device *in,
177 const struct net_device *out,
178 int (*okfn)(struct sk_buff *))
181 const struct nf_conn *ct;
182 enum ip_conntrack_info ctinfo;
187 /* root is playing with raw sockets. */
188 if (skb->len < sizeof(struct ipv6hdr))
191 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
193 if (ret != NF_DROP && ret != NF_STOLEN &&
194 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
195 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
196 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
198 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
199 &ct->tuplehash[!dir].tuple.dst.u3) ||
200 (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
201 ct->tuplehash[dir].tuple.src.u.all !=
202 ct->tuplehash[!dir].tuple.dst.u.all)) {
203 err = nf_xfrm_me_harder(skb, AF_INET6);
205 ret = NF_DROP_ERR(err);
213 nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops,
215 const struct net_device *in,
216 const struct net_device *out,
217 int (*okfn)(struct sk_buff *))
219 const struct nf_conn *ct;
220 enum ip_conntrack_info ctinfo;
224 /* root is playing with raw sockets. */
225 if (skb->len < sizeof(struct ipv6hdr))
228 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
229 if (ret != NF_DROP && ret != NF_STOLEN &&
230 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
231 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
233 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
234 &ct->tuplehash[!dir].tuple.src.u3)) {
235 err = ip6_route_me_harder(skb);
237 ret = NF_DROP_ERR(err);
240 else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
241 ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
242 ct->tuplehash[dir].tuple.dst.u.all !=
243 ct->tuplehash[!dir].tuple.src.u.all) {
244 err = nf_xfrm_me_harder(skb, AF_INET6);
246 ret = NF_DROP_ERR(err);
253 static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = {
254 /* Before packet filtering, change destination */
256 .hook = nf_nat_ipv6_in,
257 .owner = THIS_MODULE,
259 .hooknum = NF_INET_PRE_ROUTING,
260 .priority = NF_IP6_PRI_NAT_DST,
262 /* After packet filtering, change source */
264 .hook = nf_nat_ipv6_out,
265 .owner = THIS_MODULE,
267 .hooknum = NF_INET_POST_ROUTING,
268 .priority = NF_IP6_PRI_NAT_SRC,
270 /* Before packet filtering, change destination */
272 .hook = nf_nat_ipv6_local_fn,
273 .owner = THIS_MODULE,
275 .hooknum = NF_INET_LOCAL_OUT,
276 .priority = NF_IP6_PRI_NAT_DST,
278 /* After packet filtering, change source */
280 .hook = nf_nat_ipv6_fn,
281 .owner = THIS_MODULE,
283 .hooknum = NF_INET_LOCAL_IN,
284 .priority = NF_IP6_PRI_NAT_SRC,
288 static int __net_init ip6table_nat_net_init(struct net *net)
290 struct ip6t_replace *repl;
292 repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
295 net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl);
297 return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat);
300 static void __net_exit ip6table_nat_net_exit(struct net *net)
302 ip6t_unregister_table(net, net->ipv6.ip6table_nat);
305 static struct pernet_operations ip6table_nat_net_ops = {
306 .init = ip6table_nat_net_init,
307 .exit = ip6table_nat_net_exit,
310 static int __init ip6table_nat_init(void)
314 err = register_pernet_subsys(&ip6table_nat_net_ops);
318 err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
324 unregister_pernet_subsys(&ip6table_nat_net_ops);
329 static void __exit ip6table_nat_exit(void)
331 nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
332 unregister_pernet_subsys(&ip6table_nat_net_ops);
335 module_init(ip6table_nat_init);
336 module_exit(ip6table_nat_exit);
338 MODULE_LICENSE("GPL");