blob: c9c78c2e666b86b397d95756695afe49a6a47b5a [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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080017#include <linux/errno.h>
18#include <linux/types.h>
19#include <linux/string.h>
20#include <linux/socket.h>
21#include <linux/sockios.h>
22#include <linux/jiffies.h>
23#include <linux/net.h>
24#include <linux/list.h>
25#include <linux/netdevice.h>
26#include <linux/in6.h>
27#include <linux/ipv6.h>
28#include <linux/icmpv6.h>
29#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031
32#include <net/sock.h>
33#include <net/snmp.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070034#include <net/inet_frag.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035
36#include <net/ipv6.h>
37#include <net/protocol.h>
38#include <net/transp_v6.h>
39#include <net/rawv6.h>
40#include <net/ndisc.h>
41#include <net/addrconf.h>
Patrick McHardy99fa5f52008-01-31 04:10:40 -080042#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043#include <linux/sysctl.h>
44#include <linux/netfilter.h>
45#include <linux/netfilter_ipv6.h>
46#include <linux/kernel.h>
47#include <linux/module.h>
Eric Dumazetbced94e2011-01-20 21:00:38 +010048#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051struct nf_ct_frag6_skb_cb
52{
53 struct inet6_skb_parm h;
54 int offset;
55 struct sk_buff *orig;
56};
57
58#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
59
60struct nf_ct_frag6_queue
61{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070062 struct inet_frag_queue q;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063
Patrick McHardybff9a892006-12-02 22:05:08 -080064 __be32 id; /* fragment id */
Shan Weic92b5442010-01-26 02:40:38 +000065 u32 user;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066 struct in6_addr saddr;
67 struct in6_addr daddr;
68
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069 unsigned int csum;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070 __u16 nhoffset;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071};
72
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070073static struct inet_frags nf_frags;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080074static struct netns_frags nf_init_frags;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080076#ifdef CONFIG_SYSCTL
Eric Dumazetbe9e9162010-11-15 18:18:29 +010077static struct ctl_table nf_ct_frag6_sysctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080078 {
79 .procname = "nf_conntrack_frag6_timeout",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080080 .data = &nf_init_frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080081 .maxlen = sizeof(unsigned int),
82 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080083 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080084 },
85 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080086 .procname = "nf_conntrack_frag6_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080087 .data = &nf_init_frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080088 .maxlen = sizeof(unsigned int),
89 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080090 .proc_handler = proc_dointvec,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080091 },
92 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080093 .procname = "nf_conntrack_frag6_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080094 .data = &nf_init_frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080095 .maxlen = sizeof(unsigned int),
96 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080097 .proc_handler = proc_dointvec,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080098 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -080099 { }
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800100};
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200101
102static struct ctl_table_header *nf_ct_frag6_sysctl_header;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800103#endif
104
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700105static unsigned int nf_hashfn(struct inet_frag_queue *q)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106{
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200107 const struct nf_ct_frag6_queue *nq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800108
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700109 nq = container_of(q, struct nf_ct_frag6_queue, q);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700110 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800111}
112
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700113static void nf_skb_free(struct sk_buff *skb)
114{
115 if (NFCT_FRAG6_CB(skb)->orig)
116 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
117}
118
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119/* Destruction primitives. */
120
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700121static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700123 inet_frag_put(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124}
125
126/* Kill fq entry. It is not destroyed immediately,
127 * because caller (and someone more) holds reference count.
128 */
129static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
130{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700131 inet_frag_kill(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132}
133
134static void nf_ct_frag6_evictor(void)
135{
David S. Millere8e16b702008-03-28 17:30:18 -0700136 local_bh_disable();
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800137 inet_frag_evictor(&nf_init_frags, &nf_frags);
David S. Millere8e16b702008-03-28 17:30:18 -0700138 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139}
140
141static void nf_ct_frag6_expire(unsigned long data)
142{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700143 struct nf_ct_frag6_queue *fq;
144
145 fq = container_of((struct inet_frag_queue *)data,
146 struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700148 spin_lock(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149
Joe Perchesbc578a52008-03-28 16:35:27 -0700150 if (fq->q.last_in & INET_FRAG_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151 goto out;
152
153 fq_kill(fq);
154
155out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700156 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700157 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158}
159
160/* Creation primitives. */
161
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700162static __inline__ struct nf_ct_frag6_queue *
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100163fq_find(__be32 id, u32 user, struct in6_addr *src, struct in6_addr *dst)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164{
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700165 struct inet_frag_queue *q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700166 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700167 unsigned int hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700169 arg.id = id;
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100170 arg.user = user;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700171 arg.src = src;
172 arg.dst = dst;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700173
174 read_lock_bh(&nf_frags.lock);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700175 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800177 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700178 local_bh_enable();
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700179 if (q == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180 goto oom;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800181
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700182 return container_of(q, struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183
184oom:
185 return NULL;
186}
187
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900189static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200190 const struct frag_hdr *fhdr, int nhoff)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191{
192 struct sk_buff *prev, *next;
193 int offset, end;
194
Joe Perchesbc578a52008-03-28 16:35:27 -0700195 if (fq->q.last_in & INET_FRAG_COMPLETE) {
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200196 pr_debug("Already completed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197 goto err;
198 }
199
200 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700201 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
202 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203
204 if ((unsigned int)end > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700205 pr_debug("offset is too large.\n");
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900206 return -1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207 }
208
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700209 if (skb->ip_summed == CHECKSUM_COMPLETE) {
210 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900211 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700212 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213 0));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700214 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215
216 /* Is this the final fragment? */
217 if (!(fhdr->frag_off & htons(IP6_MF))) {
218 /* If we already have some bits beyond end
219 * or have different end, the segment is corrupted.
220 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700221 if (end < fq->q.len ||
Joe Perchesbc578a52008-03-28 16:35:27 -0700222 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700223 pr_debug("already received last fragment\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224 goto err;
225 }
Joe Perchesbc578a52008-03-28 16:35:27 -0700226 fq->q.last_in |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700227 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228 } else {
229 /* Check if the fragment is rounded to 8 bytes.
230 * Required by the RFC.
231 */
232 if (end & 0x7) {
233 /* RFC2460 says always send parameter problem in
234 * this case. -DaveM
235 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700236 pr_debug("end of fragment not rounded to 8 bytes.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800237 return -1;
238 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700239 if (end > fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800240 /* Some bits beyond end -> corruption. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700241 if (fq->q.last_in & INET_FRAG_LAST_IN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700242 pr_debug("last packet already reached.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800243 goto err;
244 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700245 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800246 }
247 }
248
249 if (end == offset)
250 goto err;
251
252 /* Point into the IP datagram 'data' part. */
253 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700254 pr_debug("queue: message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800255 goto err;
256 }
Herbert Xub38dfee2006-06-09 16:13:01 -0700257 if (pskb_trim_rcsum(skb, end - offset)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700258 pr_debug("Can't trim\n");
Herbert Xub38dfee2006-06-09 16:13:01 -0700259 goto err;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260 }
261
262 /* Find out which fragments are in front and at the back of us
263 * in the chain of fragments so far. We must know where to put
264 * this fragment, right?
265 */
Changli Gaoea8fbe82010-07-05 10:38:23 +0200266 prev = fq->q.fragments_tail;
267 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
268 next = NULL;
269 goto found;
270 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700272 for (next = fq->q.fragments; next != NULL; next = next->next) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800273 if (NFCT_FRAG6_CB(next)->offset >= offset)
274 break; /* bingo! */
275 prev = next;
276 }
277
Changli Gaoea8fbe82010-07-05 10:38:23 +0200278found:
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000279 /* RFC5722, Section 4:
280 * When reassembling an IPv6 datagram, if
281 * one or more its constituent fragments is determined to be an
282 * overlapping fragment, the entire datagram (and any constituent
283 * fragments, including those not yet received) MUST be silently
284 * discarded.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800285 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000287 /* Check for overlap with preceding fragment. */
288 if (prev &&
Shan Wei22e091e2010-11-12 08:51:55 +0100289 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000290 goto discard_fq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000292 /* Look for overlap with succeeding segment. */
293 if (next && NFCT_FRAG6_CB(next)->offset < end)
294 goto discard_fq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800295
296 NFCT_FRAG6_CB(skb)->offset = offset;
297
298 /* Insert this fragment in the chain of fragments. */
299 skb->next = next;
Changli Gaoea8fbe82010-07-05 10:38:23 +0200300 if (!next)
301 fq->q.fragments_tail = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302 if (prev)
303 prev->next = skb;
304 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700305 fq->q.fragments = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306
307 skb->dev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700308 fq->q.stamp = skb->tstamp;
309 fq->q.meat += skb->len;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800310 atomic_add(skb->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800311
312 /* The first fragment.
313 * nhoffset is obtained from the first fragment, of course.
314 */
315 if (offset == 0) {
316 fq->nhoffset = nhoff;
Joe Perchesbc578a52008-03-28 16:35:27 -0700317 fq->q.last_in |= INET_FRAG_FIRST_IN;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800318 }
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700319 write_lock(&nf_frags.lock);
Pavel Emelyanov3140c252008-01-22 06:11:48 -0800320 list_move_tail(&fq->q.lru_list, &nf_init_frags.lru_list);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700321 write_unlock(&nf_frags.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322 return 0;
323
Nicolas Dichtel1ee89bd2010-09-03 05:13:07 +0000324discard_fq:
325 fq_kill(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800326err:
327 return -1;
328}
329
330/*
331 * Check if this packet is complete.
332 * Returns NULL on failure by any reason, and pointer
333 * to current nexthdr field in reassembled frame.
334 *
335 * It is called with locked fq, and caller must check that
336 * queue is eligible for reassembly i.e. it is not COMPLETE,
337 * the last and the first frames arrived and all the bits are here.
338 */
339static struct sk_buff *
340nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
341{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700342 struct sk_buff *fp, *op, *head = fq->q.fragments;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800343 int payload_len;
344
345 fq_kill(fq);
346
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700347 WARN_ON(head == NULL);
348 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349
350 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700351 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700353 sizeof(struct frag_hdr));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800354 if (payload_len > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700355 pr_debug("payload len is too large.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356 goto out_oversize;
357 }
358
359 /* Head of list must not be cloned. */
360 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700361 pr_debug("skb is cloned but can't expand head");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800362 goto out_oom;
363 }
364
365 /* If the first fragment is fragmented itself, we split
366 * it to two chunks: the first with data and paged part
367 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700368 if (skb_has_frag_list(head)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800369 struct sk_buff *clone;
370 int i, plen = 0;
371
Joe Perches0a9ee812011-08-29 14:17:25 -0700372 clone = alloc_skb(0, GFP_ATOMIC);
373 if (clone == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800374 goto out_oom;
Joe Perches0a9ee812011-08-29 14:17:25 -0700375
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800376 clone->next = head->next;
377 head->next = clone;
378 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Miller343a9972009-06-09 00:23:58 -0700379 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000380 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
381 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800382 clone->len = clone->data_len = head->data_len - plen;
383 head->data_len -= clone->len;
384 head->len -= clone->len;
385 clone->csum = 0;
386 clone->ip_summed = head->ip_summed;
387
388 NFCT_FRAG6_CB(clone)->orig = NULL;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800389 atomic_add(clone->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390 }
391
392 /* We have to remove fragment header from datagram and to relocate
393 * header in order to calculate ICV correctly. */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300394 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900395 memmove(head->head + sizeof(struct frag_hdr), head->head,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800396 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700397 head->mac_header += sizeof(struct frag_hdr);
398 head->network_header += sizeof(struct frag_hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800399
400 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300401 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700402 skb_push(head, head->data - skb_network_header(head));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800403
404 for (fp=head->next; fp; fp = fp->next) {
405 head->data_len += fp->len;
406 head->len += fp->len;
407 if (head->ip_summed != fp->ip_summed)
408 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700409 else if (head->ip_summed == CHECKSUM_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800410 head->csum = csum_add(head->csum, fp->csum);
411 head->truesize += fp->truesize;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800412 }
Shan Wei841a5942010-06-14 16:28:23 +0200413 atomic_sub(head->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414
415 head->next = NULL;
416 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700417 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700418 ipv6_hdr(head)->payload_len = htons(payload_len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800419
420 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700421 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700422 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300423 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700424 head->csum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700426 fq->q.fragments = NULL;
Changli Gaoea8fbe82010-07-05 10:38:23 +0200427 fq->q.fragments_tail = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800428
429 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
430 fp = skb_shinfo(head)->frag_list;
Patrick McHardy9e2dcf72010-02-19 18:18:37 +0100431 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432 /* at above code, head skb is divided into two skbs. */
433 fp = fp->next;
434
435 op = NFCT_FRAG6_CB(head)->orig;
436 for (; fp; fp = fp->next) {
437 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
438
439 op->next = orig;
440 op = orig;
441 NFCT_FRAG6_CB(fp)->orig = NULL;
442 }
443
444 return head;
445
446out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000447 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
448 payload_len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449 goto out_fail;
450out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000451 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800452out_fail:
453 return NULL;
454}
455
456/*
457 * find the header just before Fragment Header.
458 *
459 * if success return 0 and set ...
460 * (*prevhdrp): the value of "Next Header Field" in the header
461 * just before Fragment Header.
462 * (*prevhoff): the offset of "Next Header Field" in the header
463 * just before Fragment Header.
464 * (*fhoff) : the offset of Fragment Header.
465 *
466 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
467 *
468 */
469static int
470find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
471{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700472 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -0300473 const int netoff = skb_network_offset(skb);
474 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
475 int start = netoff + sizeof(struct ipv6hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 int len = skb->len - start;
477 u8 prevhdr = NEXTHDR_IPV6;
478
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900479 while (nexthdr != NEXTHDR_FRAGMENT) {
480 struct ipv6_opt_hdr hdr;
481 int hdrlen;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482
483 if (!ipv6_ext_hdr(nexthdr)) {
484 return -1;
485 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900486 if (nexthdr == NEXTHDR_NONE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700487 pr_debug("next header is none\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800488 return -1;
489 }
Christoph Paaschd1238d52009-03-16 15:52:11 +0100490 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
491 pr_debug("too short\n");
492 return -1;
493 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900494 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
495 BUG();
496 if (nexthdr == NEXTHDR_AUTH)
497 hdrlen = (hdr.hdrlen+2)<<2;
498 else
499 hdrlen = ipv6_optlen(&hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800500
501 prevhdr = nexthdr;
502 prev_nhoff = start;
503
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900504 nexthdr = hdr.nexthdr;
505 len -= hdrlen;
506 start += hdrlen;
507 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800508
509 if (len < 0)
510 return -1;
511
512 *prevhdrp = prevhdr;
513 *prevhoff = prev_nhoff;
514 *fhoff = start;
515
516 return 0;
517}
518
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100519struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800520{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900521 struct sk_buff *clone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800522 struct net_device *dev = skb->dev;
523 struct frag_hdr *fhdr;
524 struct nf_ct_frag6_queue *fq;
525 struct ipv6hdr *hdr;
526 int fhoff, nhoff;
527 u8 prevhdr;
528 struct sk_buff *ret_skb = NULL;
529
530 /* Jumbo payload inhibits frag. header */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700531 if (ipv6_hdr(skb)->payload_len == 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700532 pr_debug("payload len = 0\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800533 return skb;
534 }
535
536 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
537 return skb;
538
539 clone = skb_clone(skb, GFP_ATOMIC);
540 if (clone == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700541 pr_debug("Can't clone skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800542 return skb;
543 }
544
545 NFCT_FRAG6_CB(clone)->orig = skb;
546
547 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700548 pr_debug("message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800549 goto ret_orig;
550 }
551
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300552 skb_set_transport_header(clone, fhoff);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700553 hdr = ipv6_hdr(clone);
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300554 fhdr = (struct frag_hdr *)skb_transport_header(clone);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800555
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800556 if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800557 nf_ct_frag6_evictor();
558
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100559 fq = fq_find(fhdr->identification, user, &hdr->saddr, &hdr->daddr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800560 if (fq == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700561 pr_debug("Can't find and can't create new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800562 goto ret_orig;
563 }
564
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700565 spin_lock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800566
567 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700568 spin_unlock_bh(&fq->q.lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700569 pr_debug("Can't insert skb to queue\n");
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700570 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800571 goto ret_orig;
572 }
573
Joe Perchesbc578a52008-03-28 16:35:27 -0700574 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
575 fq->q.meat == fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800576 ret_skb = nf_ct_frag6_reasm(fq, dev);
577 if (ret_skb == NULL)
Patrick McHardy0d537782007-07-07 22:39:38 -0700578 pr_debug("Can't reassemble fragmented packets\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800579 }
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700580 spin_unlock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800581
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700582 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800583 return ret_skb;
584
585ret_orig:
586 kfree_skb(clone);
587 return skb;
588}
589
590void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
591 struct net_device *in, struct net_device *out,
592 int (*okfn)(struct sk_buff *))
593{
594 struct sk_buff *s, *s2;
595
596 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
597 nf_conntrack_put_reasm(s->nfct_reasm);
598 nf_conntrack_get_reasm(skb);
599 s->nfct_reasm = skb;
600
601 s2 = s->next;
Patrick McHardyf9f02cc2007-01-09 14:32:41 -0800602 s->next = NULL;
603
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100604 NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s, in, out, okfn,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800605 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
606 s = s2;
607 }
608 nf_conntrack_put_reasm(skb);
609}
610
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800611int nf_ct_frag6_init(void)
612{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700613 nf_frags.hashfn = nf_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700614 nf_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700615 nf_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700616 nf_frags.skb_free = nf_skb_free;
617 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700618 nf_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700619 nf_frags.frag_expire = nf_ct_frag6_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800620 nf_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800621 nf_init_frags.timeout = IPV6_FRAG_TIMEOUT;
Shan Wei7c070aa2010-01-20 10:42:41 +0100622 nf_init_frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
623 nf_init_frags.low_thresh = IPV6_FRAG_LOW_THRESH;
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800624 inet_frags_init_net(&nf_init_frags);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700625 inet_frags_init(&nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800626
David S. Miller7932c2e2010-10-26 09:08:53 -0700627#ifdef CONFIG_SYSCTL
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000628 nf_ct_frag6_sysctl_header = register_net_sysctl(&init_net, "net/netfilter",
629 nf_ct_frag6_sysctl_table);
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200630 if (!nf_ct_frag6_sysctl_header) {
631 inet_frags_fini(&nf_frags);
632 return -ENOMEM;
633 }
David S. Miller7932c2e2010-10-26 09:08:53 -0700634#endif
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200635
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800636 return 0;
637}
638
639void nf_ct_frag6_cleanup(void)
640{
David S. Miller7932c2e2010-10-26 09:08:53 -0700641#ifdef CONFIG_SYSCTL
Eric W. Biederman5dd3df12012-04-19 13:24:33 +0000642 unregister_net_sysctl_table(nf_ct_frag6_sysctl_header);
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200643 nf_ct_frag6_sysctl_header = NULL;
David S. Miller7932c2e2010-10-26 09:08:53 -0700644#endif
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700645 inet_frags_fini(&nf_frags);
646
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800647 nf_init_frags.low_thresh = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800648 nf_ct_frag6_evictor();
649}