blob: 8f80e245f37053ccf2dfd728378ba60a621055c9 [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>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
31#include <net/sock.h>
32#include <net/snmp.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070033#include <net/inet_frag.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034
35#include <net/ipv6.h>
36#include <net/protocol.h>
37#include <net/transp_v6.h>
38#include <net/rawv6.h>
39#include <net/ndisc.h>
40#include <net/addrconf.h>
Patrick McHardy99fa5f52008-01-31 04:10:40 -080041#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042#include <linux/sysctl.h>
43#include <linux/netfilter.h>
44#include <linux/netfilter_ipv6.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049struct nf_ct_frag6_skb_cb
50{
51 struct inet6_skb_parm h;
52 int offset;
53 struct sk_buff *orig;
54};
55
56#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
57
58struct nf_ct_frag6_queue
59{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070060 struct inet_frag_queue q;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061
Patrick McHardybff9a892006-12-02 22:05:08 -080062 __be32 id; /* fragment id */
Shan Weic92b5442010-01-26 02:40:38 +000063 u32 user;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064 struct in6_addr saddr;
65 struct in6_addr daddr;
66
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067 unsigned int csum;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068 __u16 nhoffset;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069};
70
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070071static struct inet_frags nf_frags;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080072static struct netns_frags nf_init_frags;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080074#ifdef CONFIG_SYSCTL
75struct ctl_table nf_ct_ipv6_sysctl_table[] = {
76 {
77 .procname = "nf_conntrack_frag6_timeout",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080078 .data = &nf_init_frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080079 .maxlen = sizeof(unsigned int),
80 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080081 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080082 },
83 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080084 .procname = "nf_conntrack_frag6_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080085 .data = &nf_init_frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080086 .maxlen = sizeof(unsigned int),
87 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080088 .proc_handler = proc_dointvec,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080089 },
90 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080091 .procname = "nf_conntrack_frag6_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080092 .data = &nf_init_frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080093 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080095 .proc_handler = proc_dointvec,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080096 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -080097 { }
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080098};
99#endif
100
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700101static unsigned int nf_hashfn(struct inet_frag_queue *q)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102{
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200103 const struct nf_ct_frag6_queue *nq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700105 nq = container_of(q, struct nf_ct_frag6_queue, q);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700106 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107}
108
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700109static void nf_skb_free(struct sk_buff *skb)
110{
111 if (NFCT_FRAG6_CB(skb)->orig)
112 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
113}
114
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115/* Memory Tracking Functions. */
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800116static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800117{
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800118 if (work)
119 *work -= skb->truesize;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800120 atomic_sub(skb->truesize, &nf_init_frags.mem);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700121 nf_skb_free(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122 kfree_skb(skb);
123}
124
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800125/* Destruction primitives. */
126
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700127static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800128{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700129 inet_frag_put(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130}
131
132/* Kill fq entry. It is not destroyed immediately,
133 * because caller (and someone more) holds reference count.
134 */
135static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
136{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700137 inet_frag_kill(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800138}
139
140static void nf_ct_frag6_evictor(void)
141{
David S. Millere8e16b702008-03-28 17:30:18 -0700142 local_bh_disable();
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800143 inet_frag_evictor(&nf_init_frags, &nf_frags);
David S. Millere8e16b702008-03-28 17:30:18 -0700144 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800145}
146
147static void nf_ct_frag6_expire(unsigned long data)
148{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700149 struct nf_ct_frag6_queue *fq;
150
151 fq = container_of((struct inet_frag_queue *)data,
152 struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700154 spin_lock(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800155
Joe Perchesbc578a52008-03-28 16:35:27 -0700156 if (fq->q.last_in & INET_FRAG_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157 goto out;
158
159 fq_kill(fq);
160
161out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700162 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700163 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164}
165
166/* Creation primitives. */
167
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700168static __inline__ struct nf_ct_frag6_queue *
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100169fq_find(__be32 id, u32 user, struct in6_addr *src, struct in6_addr *dst)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170{
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700171 struct inet_frag_queue *q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700172 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700173 unsigned int hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800174
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700175 arg.id = id;
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100176 arg.user = user;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700177 arg.src = src;
178 arg.dst = dst;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700179
180 read_lock_bh(&nf_frags.lock);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700181 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800183 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700184 local_bh_enable();
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700185 if (q == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186 goto oom;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800187
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700188 return container_of(q, struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189
190oom:
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700191 pr_debug("Can't alloc new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192 return NULL;
193}
194
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800195
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900196static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200197 const struct frag_hdr *fhdr, int nhoff)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800198{
199 struct sk_buff *prev, *next;
200 int offset, end;
201
Joe Perchesbc578a52008-03-28 16:35:27 -0700202 if (fq->q.last_in & INET_FRAG_COMPLETE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700203 pr_debug("Allready completed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204 goto err;
205 }
206
207 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700208 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
209 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800210
211 if ((unsigned int)end > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700212 pr_debug("offset is too large.\n");
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900213 return -1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214 }
215
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700216 if (skb->ip_summed == CHECKSUM_COMPLETE) {
217 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900218 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700219 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220 0));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700221 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222
223 /* Is this the final fragment? */
224 if (!(fhdr->frag_off & htons(IP6_MF))) {
225 /* If we already have some bits beyond end
226 * or have different end, the segment is corrupted.
227 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700228 if (end < fq->q.len ||
Joe Perchesbc578a52008-03-28 16:35:27 -0700229 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700230 pr_debug("already received last fragment\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800231 goto err;
232 }
Joe Perchesbc578a52008-03-28 16:35:27 -0700233 fq->q.last_in |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700234 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800235 } else {
236 /* Check if the fragment is rounded to 8 bytes.
237 * Required by the RFC.
238 */
239 if (end & 0x7) {
240 /* RFC2460 says always send parameter problem in
241 * this case. -DaveM
242 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700243 pr_debug("end of fragment not rounded to 8 bytes.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244 return -1;
245 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700246 if (end > fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800247 /* Some bits beyond end -> corruption. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700248 if (fq->q.last_in & INET_FRAG_LAST_IN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700249 pr_debug("last packet already reached.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800250 goto err;
251 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700252 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253 }
254 }
255
256 if (end == offset)
257 goto err;
258
259 /* Point into the IP datagram 'data' part. */
260 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700261 pr_debug("queue: message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800262 goto err;
263 }
Herbert Xub38dfee2006-06-09 16:13:01 -0700264 if (pskb_trim_rcsum(skb, end - offset)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700265 pr_debug("Can't trim\n");
Herbert Xub38dfee2006-06-09 16:13:01 -0700266 goto err;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800267 }
268
269 /* Find out which fragments are in front and at the back of us
270 * in the chain of fragments so far. We must know where to put
271 * this fragment, right?
272 */
273 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700274 for (next = fq->q.fragments; next != NULL; next = next->next) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800275 if (NFCT_FRAG6_CB(next)->offset >= offset)
276 break; /* bingo! */
277 prev = next;
278 }
279
280 /* We found where to put this one. Check for overlap with
281 * preceding fragment, and, if needed, align things so that
282 * any overlaps are eliminated.
283 */
284 if (prev) {
285 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
286
287 if (i > 0) {
288 offset += i;
289 if (end <= offset) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700290 pr_debug("overlap\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291 goto err;
292 }
293 if (!pskb_pull(skb, i)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700294 pr_debug("Can't pull\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800295 goto err;
296 }
297 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
298 skb->ip_summed = CHECKSUM_NONE;
299 }
300 }
301
302 /* Look for overlap with succeeding segments.
303 * If we can merge fragments, do it.
304 */
305 while (next && NFCT_FRAG6_CB(next)->offset < end) {
306 /* overlap is 'i' bytes */
307 int i = end - NFCT_FRAG6_CB(next)->offset;
308
309 if (i < next->len) {
310 /* Eat head of the next overlapped fragment
311 * and leave the loop. The next ones cannot overlap.
312 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700313 pr_debug("Eat head of the overlapped parts.: %d", i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800314 if (!pskb_pull(next, i))
315 goto err;
316
317 /* next fragment */
318 NFCT_FRAG6_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700319 fq->q.meat -= i;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320 if (next->ip_summed != CHECKSUM_UNNECESSARY)
321 next->ip_summed = CHECKSUM_NONE;
322 break;
323 } else {
324 struct sk_buff *free_it = next;
325
326 /* Old fragmnet is completely overridden with
327 * new one drop it.
328 */
329 next = next->next;
330
331 if (prev)
332 prev->next = next;
333 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700334 fq->q.fragments = next;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800335
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700336 fq->q.meat -= free_it->len;
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800337 frag_kfree_skb(free_it, NULL);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800338 }
339 }
340
341 NFCT_FRAG6_CB(skb)->offset = offset;
342
343 /* Insert this fragment in the chain of fragments. */
344 skb->next = next;
345 if (prev)
346 prev->next = skb;
347 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700348 fq->q.fragments = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349
350 skb->dev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700351 fq->q.stamp = skb->tstamp;
352 fq->q.meat += skb->len;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800353 atomic_add(skb->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800354
355 /* The first fragment.
356 * nhoffset is obtained from the first fragment, of course.
357 */
358 if (offset == 0) {
359 fq->nhoffset = nhoff;
Joe Perchesbc578a52008-03-28 16:35:27 -0700360 fq->q.last_in |= INET_FRAG_FIRST_IN;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800361 }
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700362 write_lock(&nf_frags.lock);
Pavel Emelyanov3140c252008-01-22 06:11:48 -0800363 list_move_tail(&fq->q.lru_list, &nf_init_frags.lru_list);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700364 write_unlock(&nf_frags.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365 return 0;
366
367err:
368 return -1;
369}
370
371/*
372 * Check if this packet is complete.
373 * Returns NULL on failure by any reason, and pointer
374 * to current nexthdr field in reassembled frame.
375 *
376 * It is called with locked fq, and caller must check that
377 * queue is eligible for reassembly i.e. it is not COMPLETE,
378 * the last and the first frames arrived and all the bits are here.
379 */
380static struct sk_buff *
381nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
382{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700383 struct sk_buff *fp, *op, *head = fq->q.fragments;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800384 int payload_len;
385
386 fq_kill(fq);
387
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700388 WARN_ON(head == NULL);
389 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390
391 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700392 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700393 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700394 sizeof(struct frag_hdr));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395 if (payload_len > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700396 pr_debug("payload len is too large.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397 goto out_oversize;
398 }
399
400 /* Head of list must not be cloned. */
401 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700402 pr_debug("skb is cloned but can't expand head");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800403 goto out_oom;
404 }
405
406 /* If the first fragment is fragmented itself, we split
407 * it to two chunks: the first with data and paged part
408 * and the second, holding only fragments. */
David S. Miller343a9972009-06-09 00:23:58 -0700409 if (skb_has_frags(head)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800410 struct sk_buff *clone;
411 int i, plen = 0;
412
413 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700414 pr_debug("Can't alloc skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800415 goto out_oom;
416 }
417 clone->next = head->next;
418 head->next = clone;
419 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Miller343a9972009-06-09 00:23:58 -0700420 skb_frag_list_init(head);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800421 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
422 plen += skb_shinfo(head)->frags[i].size;
423 clone->len = clone->data_len = head->data_len - plen;
424 head->data_len -= clone->len;
425 head->len -= clone->len;
426 clone->csum = 0;
427 clone->ip_summed = head->ip_summed;
428
429 NFCT_FRAG6_CB(clone)->orig = NULL;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800430 atomic_add(clone->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800431 }
432
433 /* We have to remove fragment header from datagram and to relocate
434 * header in order to calculate ICV correctly. */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300435 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900436 memmove(head->head + sizeof(struct frag_hdr), head->head,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800437 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700438 head->mac_header += sizeof(struct frag_hdr);
439 head->network_header += sizeof(struct frag_hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440
441 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300442 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700443 skb_push(head, head->data - skb_network_header(head));
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800444 atomic_sub(head->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445
446 for (fp=head->next; fp; fp = fp->next) {
447 head->data_len += fp->len;
448 head->len += fp->len;
449 if (head->ip_summed != fp->ip_summed)
450 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700451 else if (head->ip_summed == CHECKSUM_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800452 head->csum = csum_add(head->csum, fp->csum);
453 head->truesize += fp->truesize;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800454 atomic_sub(fp->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800455 }
456
457 head->next = NULL;
458 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700459 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700460 ipv6_hdr(head)->payload_len = htons(payload_len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800461
462 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700463 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700464 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300465 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700466 head->csum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700468 fq->q.fragments = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800469
470 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
471 fp = skb_shinfo(head)->frag_list;
Patrick McHardy9e2dcf72010-02-19 18:18:37 +0100472 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800473 /* at above code, head skb is divided into two skbs. */
474 fp = fp->next;
475
476 op = NFCT_FRAG6_CB(head)->orig;
477 for (; fp; fp = fp->next) {
478 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
479
480 op->next = orig;
481 op = orig;
482 NFCT_FRAG6_CB(fp)->orig = NULL;
483 }
484
485 return head;
486
487out_oversize:
488 if (net_ratelimit())
489 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
490 goto out_fail;
491out_oom:
492 if (net_ratelimit())
493 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
494out_fail:
495 return NULL;
496}
497
498/*
499 * find the header just before Fragment Header.
500 *
501 * if success return 0 and set ...
502 * (*prevhdrp): the value of "Next Header Field" in the header
503 * just before Fragment Header.
504 * (*prevhoff): the offset of "Next Header Field" in the header
505 * just before Fragment Header.
506 * (*fhoff) : the offset of Fragment Header.
507 *
508 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
509 *
510 */
511static int
512find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
513{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700514 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -0300515 const int netoff = skb_network_offset(skb);
516 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
517 int start = netoff + sizeof(struct ipv6hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800518 int len = skb->len - start;
519 u8 prevhdr = NEXTHDR_IPV6;
520
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900521 while (nexthdr != NEXTHDR_FRAGMENT) {
522 struct ipv6_opt_hdr hdr;
523 int hdrlen;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800524
525 if (!ipv6_ext_hdr(nexthdr)) {
526 return -1;
527 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900528 if (nexthdr == NEXTHDR_NONE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700529 pr_debug("next header is none\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800530 return -1;
531 }
Christoph Paaschd1238d52009-03-16 15:52:11 +0100532 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
533 pr_debug("too short\n");
534 return -1;
535 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900536 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
537 BUG();
538 if (nexthdr == NEXTHDR_AUTH)
539 hdrlen = (hdr.hdrlen+2)<<2;
540 else
541 hdrlen = ipv6_optlen(&hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800542
543 prevhdr = nexthdr;
544 prev_nhoff = start;
545
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900546 nexthdr = hdr.nexthdr;
547 len -= hdrlen;
548 start += hdrlen;
549 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800550
551 if (len < 0)
552 return -1;
553
554 *prevhdrp = prevhdr;
555 *prevhoff = prev_nhoff;
556 *fhoff = start;
557
558 return 0;
559}
560
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100561struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800562{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900563 struct sk_buff *clone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800564 struct net_device *dev = skb->dev;
565 struct frag_hdr *fhdr;
566 struct nf_ct_frag6_queue *fq;
567 struct ipv6hdr *hdr;
568 int fhoff, nhoff;
569 u8 prevhdr;
570 struct sk_buff *ret_skb = NULL;
571
572 /* Jumbo payload inhibits frag. header */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700573 if (ipv6_hdr(skb)->payload_len == 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700574 pr_debug("payload len = 0\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800575 return skb;
576 }
577
578 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
579 return skb;
580
581 clone = skb_clone(skb, GFP_ATOMIC);
582 if (clone == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700583 pr_debug("Can't clone skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800584 return skb;
585 }
586
587 NFCT_FRAG6_CB(clone)->orig = skb;
588
589 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700590 pr_debug("message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800591 goto ret_orig;
592 }
593
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300594 skb_set_transport_header(clone, fhoff);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700595 hdr = ipv6_hdr(clone);
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300596 fhdr = (struct frag_hdr *)skb_transport_header(clone);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800597
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800598 if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800599 nf_ct_frag6_evictor();
600
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100601 fq = fq_find(fhdr->identification, user, &hdr->saddr, &hdr->daddr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800602 if (fq == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700603 pr_debug("Can't find and can't create new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800604 goto ret_orig;
605 }
606
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700607 spin_lock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800608
609 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700610 spin_unlock_bh(&fq->q.lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700611 pr_debug("Can't insert skb to queue\n");
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700612 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800613 goto ret_orig;
614 }
615
Joe Perchesbc578a52008-03-28 16:35:27 -0700616 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
617 fq->q.meat == fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800618 ret_skb = nf_ct_frag6_reasm(fq, dev);
619 if (ret_skb == NULL)
Patrick McHardy0d537782007-07-07 22:39:38 -0700620 pr_debug("Can't reassemble fragmented packets\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800621 }
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700622 spin_unlock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800623
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700624 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800625 return ret_skb;
626
627ret_orig:
628 kfree_skb(clone);
629 return skb;
630}
631
632void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
633 struct net_device *in, struct net_device *out,
634 int (*okfn)(struct sk_buff *))
635{
636 struct sk_buff *s, *s2;
637
638 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
639 nf_conntrack_put_reasm(s->nfct_reasm);
640 nf_conntrack_get_reasm(skb);
641 s->nfct_reasm = skb;
642
643 s2 = s->next;
Patrick McHardyf9f02cc2007-01-09 14:32:41 -0800644 s->next = NULL;
645
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100646 NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s, in, out, okfn,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800647 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
648 s = s2;
649 }
650 nf_conntrack_put_reasm(skb);
651}
652
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653int nf_ct_frag6_init(void)
654{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700655 nf_frags.hashfn = nf_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700656 nf_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700657 nf_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700658 nf_frags.skb_free = nf_skb_free;
659 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700660 nf_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700661 nf_frags.frag_expire = nf_ct_frag6_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800662 nf_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800663 nf_init_frags.timeout = IPV6_FRAG_TIMEOUT;
Shan Wei7c070aa2010-01-20 10:42:41 +0100664 nf_init_frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
665 nf_init_frags.low_thresh = IPV6_FRAG_LOW_THRESH;
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800666 inet_frags_init_net(&nf_init_frags);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700667 inet_frags_init(&nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800668
669 return 0;
670}
671
672void nf_ct_frag6_cleanup(void)
673{
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700674 inet_frags_fini(&nf_frags);
675
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800676 nf_init_frags.low_thresh = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800677 nf_ct_frag6_evictor();
678}