blob: dffdc1a389c57da1d16ebeda9b9aff44d6cd961a [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/*
2 * IPv6 fragment reassembly for connection tracking
3 *
4 * Copyright (C)2004 USAGI/WIDE Project
5 *
6 * Author:
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8 *
9 * Based on: net/ipv6/reassembly.c
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000017#define pr_fmt(fmt) "IPv6-nf: " fmt
18
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080019#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/socket.h>
23#include <linux/sockios.h>
24#include <linux/jiffies.h>
25#include <linux/net.h>
26#include <linux/list.h>
27#include <linux/netdevice.h>
28#include <linux/in6.h>
29#include <linux/ipv6.h>
30#include <linux/icmpv6.h>
31#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033
34#include <net/sock.h>
35#include <net/snmp.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070036#include <net/inet_frag.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080037
38#include <net/ipv6.h>
39#include <net/protocol.h>
40#include <net/transp_v6.h>
41#include <net/rawv6.h>
42#include <net/ndisc.h>
43#include <net/addrconf.h>
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +000044#include <net/inet_ecn.h>
Patrick McHardy99fa5f52008-01-31 04:10:40 -080045#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046#include <linux/sysctl.h>
47#include <linux/netfilter.h>
48#include <linux/netfilter_ipv6.h>
49#include <linux/kernel.h>
50#include <linux/module.h>
Eric Dumazetbced94e2011-01-20 21:00:38 +010051#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080053
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054struct nf_ct_frag6_skb_cb
55{
56 struct inet6_skb_parm h;
57 int offset;
58 struct sk_buff *orig;
59};
60
61#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
62
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070063static struct inet_frags nf_frags;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080065#ifdef CONFIG_SYSCTL
Eric Dumazetbe9e9162010-11-15 18:18:29 +010066static struct ctl_table nf_ct_frag6_sysctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080067 {
68 .procname = "nf_conntrack_frag6_timeout",
Amerigo Wangc038a762012-09-18 16:50:08 +000069 .data = &init_net.nf_frag.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080070 .maxlen = sizeof(unsigned int),
71 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080072 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080073 },
74 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080075 .procname = "nf_conntrack_frag6_low_thresh",
Amerigo Wangc038a762012-09-18 16:50:08 +000076 .data = &init_net.nf_frag.frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080077 .maxlen = sizeof(unsigned int),
78 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080079 .proc_handler = proc_dointvec,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080080 },
81 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080082 .procname = "nf_conntrack_frag6_high_thresh",
Amerigo Wangc038a762012-09-18 16:50:08 +000083 .data = &init_net.nf_frag.frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080084 .maxlen = sizeof(unsigned int),
85 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080086 .proc_handler = proc_dointvec,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080087 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -080088 { }
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080089};
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020090
Hein Tiboschf1df1372012-10-26 23:49:26 +000091static int nf_ct_frag6_sysctl_register(struct net *net)
Amerigo Wangc038a762012-09-18 16:50:08 +000092{
93 struct ctl_table *table;
94 struct ctl_table_header *hdr;
95
96 table = nf_ct_frag6_sysctl_table;
97 if (!net_eq(net, &init_net)) {
98 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
99 GFP_KERNEL);
100 if (table == NULL)
101 goto err_alloc;
102
Michal Kubeček894e2ac2013-02-12 23:46:09 +0000103 table[0].data = &net->nf_frag.frags.timeout;
104 table[1].data = &net->nf_frag.frags.low_thresh;
105 table[2].data = &net->nf_frag.frags.high_thresh;
Amerigo Wangc038a762012-09-18 16:50:08 +0000106 }
107
108 hdr = register_net_sysctl(net, "net/netfilter", table);
109 if (hdr == NULL)
110 goto err_reg;
111
Konstantin Khlebnikov4b7cc7f2012-09-25 06:07:50 +0000112 net->nf_frag.sysctl.frags_hdr = hdr;
Amerigo Wangc038a762012-09-18 16:50:08 +0000113 return 0;
114
115err_reg:
116 if (!net_eq(net, &init_net))
117 kfree(table);
118err_alloc:
119 return -ENOMEM;
120}
121
122static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
123{
124 struct ctl_table *table;
125
126 table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg;
127 unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr);
128 if (!net_eq(net, &init_net))
129 kfree(table);
130}
131
132#else
Hein Tiboschf1df1372012-10-26 23:49:26 +0000133static int nf_ct_frag6_sysctl_register(struct net *net)
Amerigo Wangc038a762012-09-18 16:50:08 +0000134{
135 return 0;
136}
137static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
138{
139}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800140#endif
141
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000142static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
143{
144 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
145}
146
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700147static unsigned int nf_hashfn(struct inet_frag_queue *q)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148{
Amerigo Wangb836c992012-09-18 16:50:09 +0000149 const struct frag_queue *nq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150
Amerigo Wangb836c992012-09-18 16:50:09 +0000151 nq = container_of(q, struct frag_queue, q);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700152 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153}
154
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700155static void nf_skb_free(struct sk_buff *skb)
156{
157 if (NFCT_FRAG6_CB(skb)->orig)
158 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
159}
160
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161static void nf_ct_frag6_expire(unsigned long data)
162{
Amerigo Wangb836c992012-09-18 16:50:09 +0000163 struct frag_queue *fq;
164 struct net *net;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700165
Amerigo Wangb836c992012-09-18 16:50:09 +0000166 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
167 net = container_of(fq->q.net, struct net, nf_frag.frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168
Amerigo Wangb836c992012-09-18 16:50:09 +0000169 ip6_expire_frag_queue(net, fq, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170}
171
172/* Creation primitives. */
Amerigo Wangb836c992012-09-18 16:50:09 +0000173static inline struct frag_queue *fq_find(struct net *net, __be32 id,
174 u32 user, struct in6_addr *src,
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000175 struct in6_addr *dst, u8 ecn)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176{
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700177 struct inet_frag_queue *q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700178 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700179 unsigned int hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700181 arg.id = id;
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100182 arg.user = user;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700183 arg.src = src;
184 arg.dst = dst;
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000185 arg.ecn = ecn;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700186
187 read_lock_bh(&nf_frags.lock);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700188 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189
Amerigo Wangc038a762012-09-18 16:50:08 +0000190 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700191 local_bh_enable();
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000192 if (IS_ERR_OR_NULL(q)) {
193 inet_frag_maybe_warn_overflow(q, pr_fmt());
194 return NULL;
195 }
Amerigo Wangb836c992012-09-18 16:50:09 +0000196 return container_of(q, struct frag_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197}
198
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199
Amerigo Wangb836c992012-09-18 16:50:09 +0000200static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200201 const struct frag_hdr *fhdr, int nhoff)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800202{
203 struct sk_buff *prev, *next;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200204 unsigned int payload_len;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 int offset, end;
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000206 u8 ecn;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207
Joe Perchesbc578a52008-03-28 16:35:27 -0700208 if (fq->q.last_in & INET_FRAG_COMPLETE) {
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200209 pr_debug("Already completed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800210 goto err;
211 }
212
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200213 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
214
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215 offset = ntohs(fhdr->frag_off) & ~0x7;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200216 end = offset + (payload_len -
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700217 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800218
219 if ((unsigned int)end > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700220 pr_debug("offset is too large.\n");
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900221 return -1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222 }
223
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000224 ecn = ip6_frag_ecn(ipv6_hdr(skb));
225
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700226 if (skb->ip_summed == CHECKSUM_COMPLETE) {
227 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900228 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700229 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230 0));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700231 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232
233 /* Is this the final fragment? */
234 if (!(fhdr->frag_off & htons(IP6_MF))) {
235 /* If we already have some bits beyond end
236 * or have different end, the segment is corrupted.
237 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700238 if (end < fq->q.len ||
Joe Perchesbc578a52008-03-28 16:35:27 -0700239 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700240 pr_debug("already received last fragment\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800241 goto err;
242 }
Joe Perchesbc578a52008-03-28 16:35:27 -0700243 fq->q.last_in |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700244 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245 } else {
246 /* Check if the fragment is rounded to 8 bytes.
247 * Required by the RFC.
248 */
249 if (end & 0x7) {
250 /* RFC2460 says always send parameter problem in
251 * this case. -DaveM
252 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700253 pr_debug("end of fragment not rounded to 8 bytes.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254 return -1;
255 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700256 if (end > fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257 /* Some bits beyond end -> corruption. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700258 if (fq->q.last_in & INET_FRAG_LAST_IN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700259 pr_debug("last packet already reached.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260 goto err;
261 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700262 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800263 }
264 }
265
266 if (end == offset)
267 goto err;
268
269 /* Point into the IP datagram 'data' part. */
270 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700271 pr_debug("queue: message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800272 goto err;
273 }
Herbert Xub38dfee2006-06-09 16:13:01 -0700274 if (pskb_trim_rcsum(skb, end - offset)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700275 pr_debug("Can't trim\n");
Herbert Xub38dfee2006-06-09 16:13:01 -0700276 goto err;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800277 }
278
279 /* Find out which fragments are in front and at the back of us
280 * in the chain of fragments so far. We must know where to put
281 * this fragment, right?
282 */
Changli Gaoea8fbe82010-07-05 10:38:23 +0200283 prev = fq->q.fragments_tail;
284 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
285 next = NULL;
286 goto found;
287 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800288 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700289 for (next = fq->q.fragments; next != NULL; next = next->next) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290 if (NFCT_FRAG6_CB(next)->offset >= offset)
291 break; /* bingo! */
292 prev = next;
293 }
294
Changli Gaoea8fbe82010-07-05 10:38:23 +0200295found:
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000296 /* RFC5722, Section 4:
297 * When reassembling an IPv6 datagram, if
298 * one or more its constituent fragments is determined to be an
299 * overlapping fragment, the entire datagram (and any constituent
300 * fragments, including those not yet received) MUST be silently
301 * discarded.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000304 /* Check for overlap with preceding fragment. */
305 if (prev &&
Shan Wei22e091e2010-11-12 08:51:55 +0100306 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000307 goto discard_fq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800308
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000309 /* Look for overlap with succeeding segment. */
310 if (next && NFCT_FRAG6_CB(next)->offset < end)
311 goto discard_fq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312
313 NFCT_FRAG6_CB(skb)->offset = offset;
314
315 /* Insert this fragment in the chain of fragments. */
316 skb->next = next;
Changli Gaoea8fbe82010-07-05 10:38:23 +0200317 if (!next)
318 fq->q.fragments_tail = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319 if (prev)
320 prev->next = skb;
321 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700322 fq->q.fragments = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800323
Haibo Xi97cf00e2012-12-06 23:42:17 +0000324 if (skb->dev) {
325 fq->iif = skb->dev->ifindex;
326 skb->dev = NULL;
327 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700328 fq->q.stamp = skb->tstamp;
329 fq->q.meat += skb->len;
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000330 fq->ecn |= ecn;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200331 if (payload_len > fq->q.max_size)
332 fq->q.max_size = payload_len;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000333 add_frag_mem_limit(&fq->q, skb->truesize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800334
335 /* The first fragment.
336 * nhoffset is obtained from the first fragment, of course.
337 */
338 if (offset == 0) {
339 fq->nhoffset = nhoff;
Joe Perchesbc578a52008-03-28 16:35:27 -0700340 fq->q.last_in |= INET_FRAG_FIRST_IN;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800341 }
Jesper Dangaard Brouer3ef0eb02013-01-28 23:45:51 +0000342
343 inet_frag_lru_move(&fq->q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800344 return 0;
345
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000346discard_fq:
Amerigo Wangb836c992012-09-18 16:50:09 +0000347 inet_frag_kill(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800348err:
349 return -1;
350}
351
352/*
353 * Check if this packet is complete.
354 * Returns NULL on failure by any reason, and pointer
355 * to current nexthdr field in reassembled frame.
356 *
357 * It is called with locked fq, and caller must check that
358 * queue is eligible for reassembly i.e. it is not COMPLETE,
359 * the last and the first frames arrived and all the bits are here.
360 */
361static struct sk_buff *
Amerigo Wangb836c992012-09-18 16:50:09 +0000362nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700364 struct sk_buff *fp, *op, *head = fq->q.fragments;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365 int payload_len;
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000366 u8 ecn;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800367
Amerigo Wangb836c992012-09-18 16:50:09 +0000368 inet_frag_kill(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800369
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700370 WARN_ON(head == NULL);
371 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800372
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000373 ecn = ip_frag_ecn_table[fq->ecn];
374 if (unlikely(ecn == 0xff))
375 goto out_fail;
376
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700378 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700379 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700380 sizeof(struct frag_hdr));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800381 if (payload_len > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700382 pr_debug("payload len is too large.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383 goto out_oversize;
384 }
385
386 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000387 if (skb_unclone(head, GFP_ATOMIC)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700388 pr_debug("skb is cloned but can't expand head");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800389 goto out_oom;
390 }
391
392 /* If the first fragment is fragmented itself, we split
393 * it to two chunks: the first with data and paged part
394 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700395 if (skb_has_frag_list(head)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800396 struct sk_buff *clone;
397 int i, plen = 0;
398
Joe Perches0a9ee812011-08-29 14:17:25 -0700399 clone = alloc_skb(0, GFP_ATOMIC);
400 if (clone == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401 goto out_oom;
Joe Perches0a9ee812011-08-29 14:17:25 -0700402
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800403 clone->next = head->next;
404 head->next = clone;
405 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Miller343a9972009-06-09 00:23:58 -0700406 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000407 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
408 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409 clone->len = clone->data_len = head->data_len - plen;
410 head->data_len -= clone->len;
411 head->len -= clone->len;
412 clone->csum = 0;
413 clone->ip_summed = head->ip_summed;
414
415 NFCT_FRAG6_CB(clone)->orig = NULL;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000416 add_frag_mem_limit(&fq->q, clone->truesize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800417 }
418
419 /* We have to remove fragment header from datagram and to relocate
420 * header in order to calculate ICV correctly. */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300421 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900422 memmove(head->head + sizeof(struct frag_hdr), head->head,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700424 head->mac_header += sizeof(struct frag_hdr);
425 head->network_header += sizeof(struct frag_hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426
427 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300428 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700429 skb_push(head, head->data - skb_network_header(head));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800430
431 for (fp=head->next; fp; fp = fp->next) {
432 head->data_len += fp->len;
433 head->len += fp->len;
434 if (head->ip_summed != fp->ip_summed)
435 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700436 else if (head->ip_summed == CHECKSUM_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800437 head->csum = csum_add(head->csum, fp->csum);
438 head->truesize += fp->truesize;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800439 }
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000440 sub_frag_mem_limit(&fq->q, head->truesize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800441
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200442 head->local_df = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800443 head->next = NULL;
444 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700445 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700446 ipv6_hdr(head)->payload_len = htons(payload_len);
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000447 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200448 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449
450 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700451 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700452 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300453 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700454 head->csum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800455
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700456 fq->q.fragments = NULL;
Changli Gaoea8fbe82010-07-05 10:38:23 +0200457 fq->q.fragments_tail = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800458
459 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
460 fp = skb_shinfo(head)->frag_list;
Patrick McHardy9e2dcf72010-02-19 18:18:37 +0100461 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462 /* at above code, head skb is divided into two skbs. */
463 fp = fp->next;
464
465 op = NFCT_FRAG6_CB(head)->orig;
466 for (; fp; fp = fp->next) {
467 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
468
469 op->next = orig;
470 op = orig;
471 NFCT_FRAG6_CB(fp)->orig = NULL;
472 }
473
474 return head;
475
476out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000477 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
478 payload_len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800479 goto out_fail;
480out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000481 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482out_fail:
483 return NULL;
484}
485
486/*
487 * find the header just before Fragment Header.
488 *
489 * if success return 0 and set ...
490 * (*prevhdrp): the value of "Next Header Field" in the header
491 * just before Fragment Header.
492 * (*prevhoff): the offset of "Next Header Field" in the header
493 * just before Fragment Header.
494 * (*fhoff) : the offset of Fragment Header.
495 *
496 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
497 *
498 */
499static int
500find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
501{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700502 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -0300503 const int netoff = skb_network_offset(skb);
504 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
505 int start = netoff + sizeof(struct ipv6hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800506 int len = skb->len - start;
507 u8 prevhdr = NEXTHDR_IPV6;
508
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900509 while (nexthdr != NEXTHDR_FRAGMENT) {
510 struct ipv6_opt_hdr hdr;
511 int hdrlen;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512
513 if (!ipv6_ext_hdr(nexthdr)) {
514 return -1;
515 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900516 if (nexthdr == NEXTHDR_NONE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700517 pr_debug("next header is none\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800518 return -1;
519 }
Christoph Paaschd1238d52009-03-16 15:52:11 +0100520 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
521 pr_debug("too short\n");
522 return -1;
523 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900524 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
525 BUG();
526 if (nexthdr == NEXTHDR_AUTH)
527 hdrlen = (hdr.hdrlen+2)<<2;
528 else
529 hdrlen = ipv6_optlen(&hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800530
531 prevhdr = nexthdr;
532 prev_nhoff = start;
533
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900534 nexthdr = hdr.nexthdr;
535 len -= hdrlen;
536 start += hdrlen;
537 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800538
539 if (len < 0)
540 return -1;
541
542 *prevhdrp = prevhdr;
543 *prevhoff = prev_nhoff;
544 *fhoff = start;
545
546 return 0;
547}
548
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100549struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800550{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900551 struct sk_buff *clone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800552 struct net_device *dev = skb->dev;
Amerigo Wangc038a762012-09-18 16:50:08 +0000553 struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev)
554 : dev_net(skb->dev);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800555 struct frag_hdr *fhdr;
Amerigo Wangb836c992012-09-18 16:50:09 +0000556 struct frag_queue *fq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800557 struct ipv6hdr *hdr;
558 int fhoff, nhoff;
559 u8 prevhdr;
560 struct sk_buff *ret_skb = NULL;
561
562 /* Jumbo payload inhibits frag. header */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700563 if (ipv6_hdr(skb)->payload_len == 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700564 pr_debug("payload len = 0\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800565 return skb;
566 }
567
568 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
569 return skb;
570
571 clone = skb_clone(skb, GFP_ATOMIC);
572 if (clone == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700573 pr_debug("Can't clone skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800574 return skb;
575 }
576
577 NFCT_FRAG6_CB(clone)->orig = skb;
578
579 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700580 pr_debug("message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800581 goto ret_orig;
582 }
583
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300584 skb_set_transport_header(clone, fhoff);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700585 hdr = ipv6_hdr(clone);
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300586 fhdr = (struct frag_hdr *)skb_transport_header(clone);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800587
Amerigo Wang6b102862012-09-18 16:50:11 +0000588 local_bh_disable();
589 inet_frag_evictor(&net->nf_frag.frags, &nf_frags, false);
590 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800591
Hannes Frederic Sowab8dd6a22013-03-24 15:20:51 +0000592 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
593 ip6_frag_ecn(hdr));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800594 if (fq == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700595 pr_debug("Can't find and can't create new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800596 goto ret_orig;
597 }
598
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700599 spin_lock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800600
601 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700602 spin_unlock_bh(&fq->q.lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700603 pr_debug("Can't insert skb to queue\n");
Amerigo Wangb836c992012-09-18 16:50:09 +0000604 inet_frag_put(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800605 goto ret_orig;
606 }
607
Joe Perchesbc578a52008-03-28 16:35:27 -0700608 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
609 fq->q.meat == fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800610 ret_skb = nf_ct_frag6_reasm(fq, dev);
611 if (ret_skb == NULL)
Patrick McHardy0d537782007-07-07 22:39:38 -0700612 pr_debug("Can't reassemble fragmented packets\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800613 }
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700614 spin_unlock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800615
Amerigo Wangb836c992012-09-18 16:50:09 +0000616 inet_frag_put(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800617 return ret_skb;
618
619ret_orig:
620 kfree_skb(clone);
621 return skb;
622}
623
624void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
625 struct net_device *in, struct net_device *out,
626 int (*okfn)(struct sk_buff *))
627{
628 struct sk_buff *s, *s2;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200629 unsigned int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800630
631 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
632 nf_conntrack_put_reasm(s->nfct_reasm);
633 nf_conntrack_get_reasm(skb);
634 s->nfct_reasm = skb;
635
636 s2 = s->next;
Patrick McHardyf9f02cc2007-01-09 14:32:41 -0800637 s->next = NULL;
638
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200639 if (ret != -ECANCELED)
640 ret = NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s,
641 in, out, okfn,
642 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
643 else
644 kfree_skb(s);
645
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800646 s = s2;
647 }
648 nf_conntrack_put_reasm(skb);
649}
650
Amerigo Wangc038a762012-09-18 16:50:08 +0000651static int nf_ct_net_init(struct net *net)
652{
653 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
654 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
655 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
656 inet_frags_init_net(&net->nf_frag.frags);
657
658 return nf_ct_frag6_sysctl_register(net);
659}
660
661static void nf_ct_net_exit(struct net *net)
662{
663 nf_ct_frags6_sysctl_unregister(net);
664 inet_frags_exit_net(&net->nf_frag.frags, &nf_frags);
665}
666
667static struct pernet_operations nf_ct_net_ops = {
668 .init = nf_ct_net_init,
669 .exit = nf_ct_net_exit,
670};
671
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800672int nf_ct_frag6_init(void)
673{
Amerigo Wangc038a762012-09-18 16:50:08 +0000674 int ret = 0;
675
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700676 nf_frags.hashfn = nf_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700677 nf_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700678 nf_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700679 nf_frags.skb_free = nf_skb_free;
Amerigo Wangb836c992012-09-18 16:50:09 +0000680 nf_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700681 nf_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700682 nf_frags.frag_expire = nf_ct_frag6_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800683 nf_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700684 inet_frags_init(&nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800685
Amerigo Wangc038a762012-09-18 16:50:08 +0000686 ret = register_pernet_subsys(&nf_ct_net_ops);
687 if (ret)
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200688 inet_frags_fini(&nf_frags);
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200689
Amerigo Wangc038a762012-09-18 16:50:08 +0000690 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800691}
692
693void nf_ct_frag6_cleanup(void)
694{
Amerigo Wangc038a762012-09-18 16:50:08 +0000695 unregister_pernet_subsys(&nf_ct_net_ops);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700696 inet_frags_fini(&nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800697}