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 * Development of IPv6 NAT funded by Astaro.
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/ipv6.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <net/secure_seq.h>
17 #include <net/checksum.h>
18 #include <net/ip6_checksum.h>
19 #include <net/ip6_route.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_nat_core.h>
25 #include <net/netfilter/nf_nat_l3proto.h>
26 #include <net/netfilter/nf_nat_l4proto.h>
28 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6
;
31 static void nf_nat_ipv6_decode_session(struct sk_buff
*skb
,
32 const struct nf_conn
*ct
,
33 enum ip_conntrack_dir dir
,
34 unsigned long statusbit
,
37 const struct nf_conntrack_tuple
*t
= &ct
->tuplehash
[dir
].tuple
;
38 struct flowi6
*fl6
= &fl
->u
.ip6
;
40 if (ct
->status
& statusbit
) {
41 fl6
->daddr
= t
->dst
.u3
.in6
;
42 if (t
->dst
.protonum
== IPPROTO_TCP
||
43 t
->dst
.protonum
== IPPROTO_UDP
||
44 t
->dst
.protonum
== IPPROTO_UDPLITE
||
45 t
->dst
.protonum
== IPPROTO_DCCP
||
46 t
->dst
.protonum
== IPPROTO_SCTP
)
47 fl6
->fl6_dport
= t
->dst
.u
.all
;
50 statusbit
^= IPS_NAT_MASK
;
52 if (ct
->status
& statusbit
) {
53 fl6
->saddr
= t
->src
.u3
.in6
;
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 fl6
->fl6_sport
= t
->src
.u
.all
;
64 static bool nf_nat_ipv6_in_range(const struct nf_conntrack_tuple
*t
,
65 const struct nf_nat_range
*range
)
67 return ipv6_addr_cmp(&t
->src
.u3
.in6
, &range
->min_addr
.in6
) >= 0 &&
68 ipv6_addr_cmp(&t
->src
.u3
.in6
, &range
->max_addr
.in6
) <= 0;
71 static u32
nf_nat_ipv6_secure_port(const struct nf_conntrack_tuple
*t
,
74 return secure_ipv6_port_ephemeral(t
->src
.u3
.ip6
, t
->dst
.u3
.ip6
, dport
);
77 static bool nf_nat_ipv6_manip_pkt(struct sk_buff
*skb
,
78 unsigned int iphdroff
,
79 const struct nf_nat_l4proto
*l4proto
,
80 const struct nf_conntrack_tuple
*target
,
81 enum nf_nat_manip_type maniptype
)
83 struct ipv6hdr
*ipv6h
;
88 if (!skb_make_writable(skb
, iphdroff
+ sizeof(*ipv6h
)))
91 ipv6h
= (void *)skb
->data
+ iphdroff
;
92 nexthdr
= ipv6h
->nexthdr
;
93 hdroff
= ipv6_skip_exthdr(skb
, iphdroff
+ sizeof(*ipv6h
),
98 if ((frag_off
& htons(~0x7)) == 0 &&
99 !l4proto
->manip_pkt(skb
, &nf_nat_l3proto_ipv6
, iphdroff
, hdroff
,
103 if (maniptype
== NF_NAT_MANIP_SRC
)
104 ipv6h
->saddr
= target
->src
.u3
.in6
;
106 ipv6h
->daddr
= target
->dst
.u3
.in6
;
111 static void nf_nat_ipv6_csum_update(struct sk_buff
*skb
,
112 unsigned int iphdroff
, __sum16
*check
,
113 const struct nf_conntrack_tuple
*t
,
114 enum nf_nat_manip_type maniptype
)
116 const struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*)(skb
->data
+ iphdroff
);
117 const struct in6_addr
*oldip
, *newip
;
119 if (maniptype
== NF_NAT_MANIP_SRC
) {
120 oldip
= &ipv6h
->saddr
;
121 newip
= &t
->src
.u3
.in6
;
123 oldip
= &ipv6h
->daddr
;
124 newip
= &t
->dst
.u3
.in6
;
126 inet_proto_csum_replace16(check
, skb
, oldip
->s6_addr32
,
127 newip
->s6_addr32
, 1);
130 static void nf_nat_ipv6_csum_recalc(struct sk_buff
*skb
,
131 u8 proto
, void *data
, __sum16
*check
,
132 int datalen
, int oldlen
)
134 const struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
135 struct rt6_info
*rt
= (struct rt6_info
*)skb_dst(skb
);
137 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
138 if (!(rt
->rt6i_flags
& RTF_LOCAL
) &&
139 (!skb
->dev
|| skb
->dev
->features
& NETIF_F_V6_CSUM
)) {
140 skb
->ip_summed
= CHECKSUM_PARTIAL
;
141 skb
->csum_start
= skb_headroom(skb
) +
142 skb_network_offset(skb
) +
143 (data
- (void *)skb
->data
);
144 skb
->csum_offset
= (void *)check
- data
;
145 *check
= ~csum_ipv6_magic(&ipv6h
->saddr
, &ipv6h
->daddr
,
149 *check
= csum_ipv6_magic(&ipv6h
->saddr
, &ipv6h
->daddr
,
151 csum_partial(data
, datalen
,
153 if (proto
== IPPROTO_UDP
&& !*check
)
154 *check
= CSUM_MANGLED_0
;
157 inet_proto_csum_replace2(check
, skb
,
158 htons(oldlen
), htons(datalen
), 1);
161 static int nf_nat_ipv6_nlattr_to_range(struct nlattr
*tb
[],
162 struct nf_nat_range
*range
)
164 if (tb
[CTA_NAT_V6_MINIP
]) {
165 nla_memcpy(&range
->min_addr
.ip6
, tb
[CTA_NAT_V6_MINIP
],
166 sizeof(struct in6_addr
));
167 range
->flags
|= NF_NAT_RANGE_MAP_IPS
;
170 if (tb
[CTA_NAT_V6_MAXIP
])
171 nla_memcpy(&range
->max_addr
.ip6
, tb
[CTA_NAT_V6_MAXIP
],
172 sizeof(struct in6_addr
));
174 range
->max_addr
= range
->min_addr
;
179 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6
= {
180 .l3proto
= NFPROTO_IPV6
,
181 .secure_port
= nf_nat_ipv6_secure_port
,
182 .in_range
= nf_nat_ipv6_in_range
,
183 .manip_pkt
= nf_nat_ipv6_manip_pkt
,
184 .csum_update
= nf_nat_ipv6_csum_update
,
185 .csum_recalc
= nf_nat_ipv6_csum_recalc
,
186 .nlattr_to_range
= nf_nat_ipv6_nlattr_to_range
,
188 .decode_session
= nf_nat_ipv6_decode_session
,
192 int nf_nat_icmpv6_reply_translation(struct sk_buff
*skb
,
194 enum ip_conntrack_info ctinfo
,
195 unsigned int hooknum
,
199 struct icmp6hdr icmp6
;
202 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
203 enum nf_nat_manip_type manip
= HOOK2MANIP(hooknum
);
204 const struct nf_nat_l4proto
*l4proto
;
205 struct nf_conntrack_tuple target
;
206 unsigned long statusbit
;
208 NF_CT_ASSERT(ctinfo
== IP_CT_RELATED
|| ctinfo
== IP_CT_RELATED_REPLY
);
210 if (!skb_make_writable(skb
, hdrlen
+ sizeof(*inside
)))
212 if (nf_ip6_checksum(skb
, hooknum
, hdrlen
, IPPROTO_ICMPV6
))
215 inside
= (void *)skb
->data
+ hdrlen
;
216 if (inside
->icmp6
.icmp6_type
== NDISC_REDIRECT
) {
217 if ((ct
->status
& IPS_NAT_DONE_MASK
) != IPS_NAT_DONE_MASK
)
219 if (ct
->status
& IPS_NAT_MASK
)
223 if (manip
== NF_NAT_MANIP_SRC
)
224 statusbit
= IPS_SRC_NAT
;
226 statusbit
= IPS_DST_NAT
;
228 /* Invert if this is reply direction */
229 if (dir
== IP_CT_DIR_REPLY
)
230 statusbit
^= IPS_NAT_MASK
;
232 if (!(ct
->status
& statusbit
))
235 l4proto
= __nf_nat_l4proto_find(NFPROTO_IPV6
, inside
->ip6
.nexthdr
);
236 if (!nf_nat_ipv6_manip_pkt(skb
, hdrlen
+ sizeof(inside
->icmp6
),
237 l4proto
, &ct
->tuplehash
[!dir
].tuple
, !manip
))
240 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
241 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
242 inside
= (void *)skb
->data
+ hdrlen
;
243 inside
->icmp6
.icmp6_cksum
= 0;
244 inside
->icmp6
.icmp6_cksum
=
245 csum_ipv6_magic(&ipv6h
->saddr
, &ipv6h
->daddr
,
246 skb
->len
- hdrlen
, IPPROTO_ICMPV6
,
247 csum_partial(&inside
->icmp6
,
248 skb
->len
- hdrlen
, 0));
251 nf_ct_invert_tuplepr(&target
, &ct
->tuplehash
[!dir
].tuple
);
252 l4proto
= __nf_nat_l4proto_find(NFPROTO_IPV6
, IPPROTO_ICMPV6
);
253 if (!nf_nat_ipv6_manip_pkt(skb
, 0, l4proto
, &target
, manip
))
258 EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation
);
260 static int __init
nf_nat_l3proto_ipv6_init(void)
264 err
= nf_nat_l4proto_register(NFPROTO_IPV6
, &nf_nat_l4proto_icmpv6
);
267 err
= nf_nat_l3proto_register(&nf_nat_l3proto_ipv6
);
273 nf_nat_l4proto_unregister(NFPROTO_IPV6
, &nf_nat_l4proto_icmpv6
);
278 static void __exit
nf_nat_l3proto_ipv6_exit(void)
280 nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6
);
281 nf_nat_l4proto_unregister(NFPROTO_IPV6
, &nf_nat_l4proto_icmpv6
);
284 MODULE_LICENSE("GPL");
285 MODULE_ALIAS("nf-nat-" __stringify(AF_INET6
));
287 module_init(nf_nat_l3proto_ipv6_init
);
288 module_exit(nf_nat_l3proto_ipv6_exit
);