]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
netfilter: ipv6: using csum_ipv6_magic requires net/ip6_checksum.h
[karo-tx-linux.git] / net / ipv6 / netfilter / nf_nat_l3proto_ipv6.c
1 /*
2  * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
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  * Development of IPv6 NAT funded by Astaro.
9  */
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>
20 #include <net/ipv6.h>
21
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>
27
28 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6;
29
30 #ifdef CONFIG_XFRM
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,
35                                        struct flowi *fl)
36 {
37         const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
38         struct flowi6 *fl6 = &fl->u.ip6;
39
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;
48         }
49
50         statusbit ^= IPS_NAT_MASK;
51
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;
60         }
61 }
62 #endif
63
64 static bool nf_nat_ipv6_in_range(const struct nf_conntrack_tuple *t,
65                                  const struct nf_nat_range *range)
66 {
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;
69 }
70
71 static u32 nf_nat_ipv6_secure_port(const struct nf_conntrack_tuple *t,
72                                    __be16 dport)
73 {
74         return secure_ipv6_port_ephemeral(t->src.u3.ip6, t->dst.u3.ip6, dport);
75 }
76
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)
82 {
83         struct ipv6hdr *ipv6h;
84         __be16 frag_off;
85         int hdroff;
86         u8 nexthdr;
87
88         if (!skb_make_writable(skb, iphdroff + sizeof(*ipv6h)))
89                 return false;
90
91         ipv6h = (void *)skb->data + iphdroff;
92         nexthdr = ipv6h->nexthdr;
93         hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h),
94                                   &nexthdr, &frag_off);
95         if (hdroff < 0)
96                 goto manip_addr;
97
98         if ((frag_off & htons(~0x7)) == 0 &&
99             !l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv6, iphdroff, hdroff,
100                                 target, maniptype))
101                 return false;
102 manip_addr:
103         if (maniptype == NF_NAT_MANIP_SRC)
104                 ipv6h->saddr = target->src.u3.in6;
105         else
106                 ipv6h->daddr = target->dst.u3.in6;
107
108         return true;
109 }
110
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)
115 {
116         const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff);
117         const struct in6_addr *oldip, *newip;
118
119         if (maniptype == NF_NAT_MANIP_SRC) {
120                 oldip = &ipv6h->saddr;
121                 newip = &t->src.u3.in6;
122         } else {
123                 oldip = &ipv6h->daddr;
124                 newip = &t->dst.u3.in6;
125         }
126         inet_proto_csum_replace16(check, skb, oldip->s6_addr32,
127                                   newip->s6_addr32, 1);
128 }
129
130 static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
131                                     u8 proto, void *data, __sum16 *check,
132                                     int datalen, int oldlen)
133 {
134         const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
135         struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
136
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,
146                                                   datalen, proto, 0);
147                 } else {
148                         *check = 0;
149                         *check = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
150                                                  datalen, proto,
151                                                  csum_partial(data, datalen,
152                                                               0));
153                         if (proto == IPPROTO_UDP && !*check)
154                                 *check = CSUM_MANGLED_0;
155                 }
156         } else
157                 inet_proto_csum_replace2(check, skb,
158                                          htons(oldlen), htons(datalen), 1);
159 }
160
161 static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
162                                        struct nf_nat_range *range)
163 {
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;
168         }
169
170         if (tb[CTA_NAT_V6_MAXIP])
171                 nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
172                            sizeof(struct in6_addr));
173         else
174                 range->max_addr = range->min_addr;
175
176         return 0;
177 }
178
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,
187 #ifdef CONFIG_XFRM
188         .decode_session = nf_nat_ipv6_decode_session,
189 #endif
190 };
191
192 int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,
193                                     struct nf_conn *ct,
194                                     enum ip_conntrack_info ctinfo,
195                                     unsigned int hooknum,
196                                     unsigned int hdrlen)
197 {
198         struct {
199                 struct icmp6hdr icmp6;
200                 struct ipv6hdr  ip6;
201         } *inside;
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;
207
208         NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
209
210         if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
211                 return 0;
212         if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6))
213                 return 0;
214
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)
218                         return 0;
219                 if (ct->status & IPS_NAT_MASK)
220                         return 0;
221         }
222
223         if (manip == NF_NAT_MANIP_SRC)
224                 statusbit = IPS_SRC_NAT;
225         else
226                 statusbit = IPS_DST_NAT;
227
228         /* Invert if this is reply direction */
229         if (dir == IP_CT_DIR_REPLY)
230                 statusbit ^= IPS_NAT_MASK;
231
232         if (!(ct->status & statusbit))
233                 return 1;
234
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))
238                 return 0;
239
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));
249         }
250
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))
254                 return 0;
255
256         return 1;
257 }
258 EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
259
260 static int __init nf_nat_l3proto_ipv6_init(void)
261 {
262         int err;
263
264         err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
265         if (err < 0)
266                 goto err1;
267         err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv6);
268         if (err < 0)
269                 goto err2;
270         return err;
271
272 err2:
273         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
274 err1:
275         return err;
276 }
277
278 static void __exit nf_nat_l3proto_ipv6_exit(void)
279 {
280         nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6);
281         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
282 }
283
284 MODULE_LICENSE("GPL");
285 MODULE_ALIAS("nf-nat-" __stringify(AF_INET6));
286
287 module_init(nf_nat_l3proto_ipv6_init);
288 module_exit(nf_nat_l3proto_ipv6_exit);