blob: 6eed991a4a3f5c6056da7480a55904aa807c215b [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>
30#include <linux/jhash.h>
31
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>
42#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#define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */
49#define NF_CT_FRAG6_LOW_THRESH 196608 /* == 192*1024 */
50#define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUT
51
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052struct nf_ct_frag6_skb_cb
53{
54 struct inet6_skb_parm h;
55 int offset;
56 struct sk_buff *orig;
57};
58
59#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
60
61struct nf_ct_frag6_queue
62{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070063 struct inet_frag_queue q;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064
Patrick McHardybff9a892006-12-02 22:05:08 -080065 __be32 id; /* fragment id */
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
77struct ctl_table nf_ct_ipv6_sysctl_table[] = {
78 {
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,
83 .proc_handler = &proc_dointvec_jiffies,
84 },
85 {
86 .ctl_name = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
87 .procname = "nf_conntrack_frag6_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080088 .data = &nf_init_frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080089 .maxlen = sizeof(unsigned int),
90 .mode = 0644,
91 .proc_handler = &proc_dointvec,
92 },
93 {
94 .ctl_name = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
95 .procname = "nf_conntrack_frag6_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080096 .data = &nf_init_frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080097 .maxlen = sizeof(unsigned int),
98 .mode = 0644,
99 .proc_handler = &proc_dointvec,
100 },
101 { .ctl_name = 0 }
102};
103#endif
104
Patrick McHardybff9a892006-12-02 22:05:08 -0800105static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106 struct in6_addr *daddr)
107{
108 u32 a, b, c;
109
Patrick McHardybff9a892006-12-02 22:05:08 -0800110 a = (__force u32)saddr->s6_addr32[0];
111 b = (__force u32)saddr->s6_addr32[1];
112 c = (__force u32)saddr->s6_addr32[2];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800113
114 a += JHASH_GOLDEN_RATIO;
115 b += JHASH_GOLDEN_RATIO;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700116 c += nf_frags.rnd;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800117 __jhash_mix(a, b, c);
118
Patrick McHardybff9a892006-12-02 22:05:08 -0800119 a += (__force u32)saddr->s6_addr32[3];
120 b += (__force u32)daddr->s6_addr32[0];
121 c += (__force u32)daddr->s6_addr32[1];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122 __jhash_mix(a, b, c);
123
Patrick McHardybff9a892006-12-02 22:05:08 -0800124 a += (__force u32)daddr->s6_addr32[2];
125 b += (__force u32)daddr->s6_addr32[3];
126 c += (__force u32)id;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800127 __jhash_mix(a, b, c);
128
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700129 return c & (INETFRAGS_HASHSZ - 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130}
131
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700132static unsigned int nf_hashfn(struct inet_frag_queue *q)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800133{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700134 struct nf_ct_frag6_queue *nq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800135
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700136 nq = container_of(q, struct nf_ct_frag6_queue, q);
137 return ip6qhashfn(nq->id, &nq->saddr, &nq->daddr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800138}
139
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700140static void nf_skb_free(struct sk_buff *skb)
141{
142 if (NFCT_FRAG6_CB(skb)->orig)
143 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
144}
145
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146/* Memory Tracking Functions. */
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800147static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148{
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800149 if (work)
150 *work -= skb->truesize;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800151 atomic_sub(skb->truesize, &nf_init_frags.mem);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700152 nf_skb_free(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153 kfree_skb(skb);
154}
155
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800156/* Destruction primitives. */
157
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700158static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700160 inet_frag_put(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161}
162
163/* Kill fq entry. It is not destroyed immediately,
164 * because caller (and someone more) holds reference count.
165 */
166static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
167{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700168 inet_frag_kill(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169}
170
171static void nf_ct_frag6_evictor(void)
172{
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800173 inet_frag_evictor(&nf_init_frags, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800174}
175
176static void nf_ct_frag6_expire(unsigned long data)
177{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700178 struct nf_ct_frag6_queue *fq;
179
180 fq = container_of((struct inet_frag_queue *)data,
181 struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700183 spin_lock(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700185 if (fq->q.last_in & COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186 goto out;
187
188 fq_kill(fq);
189
190out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700191 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700192 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193}
194
195/* Creation primitives. */
196
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700197static __inline__ struct nf_ct_frag6_queue *
198fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199{
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700200 struct inet_frag_queue *q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700201 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700202 unsigned int hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700204 arg.id = id;
205 arg.src = src;
206 arg.dst = dst;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700207 hash = ip6qhashfn(id, src, dst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800208
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800209 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700210 if (q == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211 goto oom;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700213 return container_of(q, struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214
215oom:
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700216 pr_debug("Can't alloc new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800217 return NULL;
218}
219
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900221static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222 struct frag_hdr *fhdr, int nhoff)
223{
224 struct sk_buff *prev, *next;
225 int offset, end;
226
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700227 if (fq->q.last_in & COMPLETE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700228 pr_debug("Allready completed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800229 goto err;
230 }
231
232 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700233 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
234 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800235
236 if ((unsigned int)end > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700237 pr_debug("offset is too large.\n");
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900238 return -1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239 }
240
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700241 if (skb->ip_summed == CHECKSUM_COMPLETE) {
242 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900243 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700244 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245 0));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700246 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800247
248 /* Is this the final fragment? */
249 if (!(fhdr->frag_off & htons(IP6_MF))) {
250 /* If we already have some bits beyond end
251 * or have different end, the segment is corrupted.
252 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700253 if (end < fq->q.len ||
254 ((fq->q.last_in & LAST_IN) && end != fq->q.len)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700255 pr_debug("already received last fragment\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800256 goto err;
257 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700258 fq->q.last_in |= LAST_IN;
259 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260 } else {
261 /* Check if the fragment is rounded to 8 bytes.
262 * Required by the RFC.
263 */
264 if (end & 0x7) {
265 /* RFC2460 says always send parameter problem in
266 * this case. -DaveM
267 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700268 pr_debug("end of fragment not rounded to 8 bytes.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800269 return -1;
270 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700271 if (end > fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800272 /* Some bits beyond end -> corruption. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700273 if (fq->q.last_in & LAST_IN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700274 pr_debug("last packet already reached.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800275 goto err;
276 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700277 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800278 }
279 }
280
281 if (end == offset)
282 goto err;
283
284 /* Point into the IP datagram 'data' part. */
285 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700286 pr_debug("queue: message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800287 goto err;
288 }
Herbert Xub38dfee2006-06-09 16:13:01 -0700289 if (pskb_trim_rcsum(skb, end - offset)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700290 pr_debug("Can't trim\n");
Herbert Xub38dfee2006-06-09 16:13:01 -0700291 goto err;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292 }
293
294 /* Find out which fragments are in front and at the back of us
295 * in the chain of fragments so far. We must know where to put
296 * this fragment, right?
297 */
298 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700299 for (next = fq->q.fragments; next != NULL; next = next->next) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300 if (NFCT_FRAG6_CB(next)->offset >= offset)
301 break; /* bingo! */
302 prev = next;
303 }
304
305 /* We found where to put this one. Check for overlap with
306 * preceding fragment, and, if needed, align things so that
307 * any overlaps are eliminated.
308 */
309 if (prev) {
310 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
311
312 if (i > 0) {
313 offset += i;
314 if (end <= offset) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700315 pr_debug("overlap\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800316 goto err;
317 }
318 if (!pskb_pull(skb, i)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700319 pr_debug("Can't pull\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320 goto err;
321 }
322 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
323 skb->ip_summed = CHECKSUM_NONE;
324 }
325 }
326
327 /* Look for overlap with succeeding segments.
328 * If we can merge fragments, do it.
329 */
330 while (next && NFCT_FRAG6_CB(next)->offset < end) {
331 /* overlap is 'i' bytes */
332 int i = end - NFCT_FRAG6_CB(next)->offset;
333
334 if (i < next->len) {
335 /* Eat head of the next overlapped fragment
336 * and leave the loop. The next ones cannot overlap.
337 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700338 pr_debug("Eat head of the overlapped parts.: %d", i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800339 if (!pskb_pull(next, i))
340 goto err;
341
342 /* next fragment */
343 NFCT_FRAG6_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700344 fq->q.meat -= i;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800345 if (next->ip_summed != CHECKSUM_UNNECESSARY)
346 next->ip_summed = CHECKSUM_NONE;
347 break;
348 } else {
349 struct sk_buff *free_it = next;
350
351 /* Old fragmnet is completely overridden with
352 * new one drop it.
353 */
354 next = next->next;
355
356 if (prev)
357 prev->next = next;
358 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700359 fq->q.fragments = next;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800360
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700361 fq->q.meat -= free_it->len;
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800362 frag_kfree_skb(free_it, NULL);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363 }
364 }
365
366 NFCT_FRAG6_CB(skb)->offset = offset;
367
368 /* Insert this fragment in the chain of fragments. */
369 skb->next = next;
370 if (prev)
371 prev->next = skb;
372 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700373 fq->q.fragments = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800374
375 skb->dev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700376 fq->q.stamp = skb->tstamp;
377 fq->q.meat += skb->len;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800378 atomic_add(skb->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379
380 /* The first fragment.
381 * nhoffset is obtained from the first fragment, of course.
382 */
383 if (offset == 0) {
384 fq->nhoffset = nhoff;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700385 fq->q.last_in |= FIRST_IN;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386 }
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700387 write_lock(&nf_frags.lock);
388 list_move_tail(&fq->q.lru_list, &nf_frags.lru_list);
389 write_unlock(&nf_frags.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390 return 0;
391
392err:
393 return -1;
394}
395
396/*
397 * Check if this packet is complete.
398 * Returns NULL on failure by any reason, and pointer
399 * to current nexthdr field in reassembled frame.
400 *
401 * It is called with locked fq, and caller must check that
402 * queue is eligible for reassembly i.e. it is not COMPLETE,
403 * the last and the first frames arrived and all the bits are here.
404 */
405static struct sk_buff *
406nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
407{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700408 struct sk_buff *fp, *op, *head = fq->q.fragments;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409 int payload_len;
410
411 fq_kill(fq);
412
413 BUG_TRAP(head != NULL);
414 BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0);
415
416 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700417 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700418 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700419 sizeof(struct frag_hdr));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420 if (payload_len > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700421 pr_debug("payload len is too large.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800422 goto out_oversize;
423 }
424
425 /* Head of list must not be cloned. */
426 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700427 pr_debug("skb is cloned but can't expand head");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800428 goto out_oom;
429 }
430
431 /* If the first fragment is fragmented itself, we split
432 * it to two chunks: the first with data and paged part
433 * and the second, holding only fragments. */
434 if (skb_shinfo(head)->frag_list) {
435 struct sk_buff *clone;
436 int i, plen = 0;
437
438 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700439 pr_debug("Can't alloc skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440 goto out_oom;
441 }
442 clone->next = head->next;
443 head->next = clone;
444 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
445 skb_shinfo(head)->frag_list = NULL;
446 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
447 plen += skb_shinfo(head)->frags[i].size;
448 clone->len = clone->data_len = head->data_len - plen;
449 head->data_len -= clone->len;
450 head->len -= clone->len;
451 clone->csum = 0;
452 clone->ip_summed = head->ip_summed;
453
454 NFCT_FRAG6_CB(clone)->orig = NULL;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800455 atomic_add(clone->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456 }
457
458 /* We have to remove fragment header from datagram and to relocate
459 * header in order to calculate ICV correctly. */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300460 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900461 memmove(head->head + sizeof(struct frag_hdr), head->head,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700463 head->mac_header += sizeof(struct frag_hdr);
464 head->network_header += sizeof(struct frag_hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800465
466 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300467 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700468 skb_push(head, head->data - skb_network_header(head));
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800469 atomic_sub(head->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470
471 for (fp=head->next; fp; fp = fp->next) {
472 head->data_len += fp->len;
473 head->len += fp->len;
474 if (head->ip_summed != fp->ip_summed)
475 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700476 else if (head->ip_summed == CHECKSUM_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 head->csum = csum_add(head->csum, fp->csum);
478 head->truesize += fp->truesize;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800479 atomic_sub(fp->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480 }
481
482 head->next = NULL;
483 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700484 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700485 ipv6_hdr(head)->payload_len = htons(payload_len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800486
487 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700488 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700489 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300490 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700491 head->csum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800492
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700493 fq->q.fragments = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800494
495 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
496 fp = skb_shinfo(head)->frag_list;
497 if (NFCT_FRAG6_CB(fp)->orig == NULL)
498 /* at above code, head skb is divided into two skbs. */
499 fp = fp->next;
500
501 op = NFCT_FRAG6_CB(head)->orig;
502 for (; fp; fp = fp->next) {
503 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
504
505 op->next = orig;
506 op = orig;
507 NFCT_FRAG6_CB(fp)->orig = NULL;
508 }
509
510 return head;
511
512out_oversize:
513 if (net_ratelimit())
514 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
515 goto out_fail;
516out_oom:
517 if (net_ratelimit())
518 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
519out_fail:
520 return NULL;
521}
522
523/*
524 * find the header just before Fragment Header.
525 *
526 * if success return 0 and set ...
527 * (*prevhdrp): the value of "Next Header Field" in the header
528 * just before Fragment Header.
529 * (*prevhoff): the offset of "Next Header Field" in the header
530 * just before Fragment Header.
531 * (*fhoff) : the offset of Fragment Header.
532 *
533 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
534 *
535 */
536static int
537find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
538{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700539 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -0300540 const int netoff = skb_network_offset(skb);
541 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
542 int start = netoff + sizeof(struct ipv6hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800543 int len = skb->len - start;
544 u8 prevhdr = NEXTHDR_IPV6;
545
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900546 while (nexthdr != NEXTHDR_FRAGMENT) {
547 struct ipv6_opt_hdr hdr;
548 int hdrlen;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800549
550 if (!ipv6_ext_hdr(nexthdr)) {
551 return -1;
552 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900553 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700554 pr_debug("too short\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800555 return -1;
556 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900557 if (nexthdr == NEXTHDR_NONE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700558 pr_debug("next header is none\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800559 return -1;
560 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900561 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
562 BUG();
563 if (nexthdr == NEXTHDR_AUTH)
564 hdrlen = (hdr.hdrlen+2)<<2;
565 else
566 hdrlen = ipv6_optlen(&hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800567
568 prevhdr = nexthdr;
569 prev_nhoff = start;
570
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900571 nexthdr = hdr.nexthdr;
572 len -= hdrlen;
573 start += hdrlen;
574 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800575
576 if (len < 0)
577 return -1;
578
579 *prevhdrp = prevhdr;
580 *prevhoff = prev_nhoff;
581 *fhoff = start;
582
583 return 0;
584}
585
586struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
587{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900588 struct sk_buff *clone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800589 struct net_device *dev = skb->dev;
590 struct frag_hdr *fhdr;
591 struct nf_ct_frag6_queue *fq;
592 struct ipv6hdr *hdr;
593 int fhoff, nhoff;
594 u8 prevhdr;
595 struct sk_buff *ret_skb = NULL;
596
597 /* Jumbo payload inhibits frag. header */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700598 if (ipv6_hdr(skb)->payload_len == 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700599 pr_debug("payload len = 0\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800600 return skb;
601 }
602
603 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
604 return skb;
605
606 clone = skb_clone(skb, GFP_ATOMIC);
607 if (clone == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700608 pr_debug("Can't clone skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800609 return skb;
610 }
611
612 NFCT_FRAG6_CB(clone)->orig = skb;
613
614 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700615 pr_debug("message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800616 goto ret_orig;
617 }
618
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300619 skb_set_transport_header(clone, fhoff);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700620 hdr = ipv6_hdr(clone);
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300621 fhdr = (struct frag_hdr *)skb_transport_header(clone);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800622
623 if (!(fhdr->frag_off & htons(0xFFF9))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700624 pr_debug("Invalid fragment offset\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800625 /* It is not a fragmented frame */
626 goto ret_orig;
627 }
628
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800629 if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800630 nf_ct_frag6_evictor();
631
632 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
633 if (fq == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700634 pr_debug("Can't find and can't create new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800635 goto ret_orig;
636 }
637
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700638 spin_lock(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800639
640 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700641 spin_unlock(&fq->q.lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700642 pr_debug("Can't insert skb to queue\n");
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700643 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800644 goto ret_orig;
645 }
646
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700647 if (fq->q.last_in == (FIRST_IN|LAST_IN) && fq->q.meat == fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800648 ret_skb = nf_ct_frag6_reasm(fq, dev);
649 if (ret_skb == NULL)
Patrick McHardy0d537782007-07-07 22:39:38 -0700650 pr_debug("Can't reassemble fragmented packets\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800651 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700652 spin_unlock(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700654 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800655 return ret_skb;
656
657ret_orig:
658 kfree_skb(clone);
659 return skb;
660}
661
662void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
663 struct net_device *in, struct net_device *out,
664 int (*okfn)(struct sk_buff *))
665{
666 struct sk_buff *s, *s2;
667
668 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
669 nf_conntrack_put_reasm(s->nfct_reasm);
670 nf_conntrack_get_reasm(skb);
671 s->nfct_reasm = skb;
672
673 s2 = s->next;
Patrick McHardyf9f02cc2007-01-09 14:32:41 -0800674 s->next = NULL;
675
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800676 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
677 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
678 s = s2;
679 }
680 nf_conntrack_put_reasm(skb);
681}
682
683int nf_ct_frag6_kfree_frags(struct sk_buff *skb)
684{
685 struct sk_buff *s, *s2;
686
687 for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) {
688
689 s2 = s->next;
690 kfree_skb(s);
691 }
692
693 kfree_skb(skb);
694
695 return 0;
696}
697
698int nf_ct_frag6_init(void)
699{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700700 nf_frags.hashfn = nf_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700701 nf_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700702 nf_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700703 nf_frags.skb_free = nf_skb_free;
704 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700705 nf_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700706 nf_frags.frag_expire = nf_ct_frag6_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800707 nf_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800708 nf_init_frags.timeout = IPV6_FRAG_TIMEOUT;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800709 nf_init_frags.high_thresh = 256 * 1024;
710 nf_init_frags.low_thresh = 192 * 1024;
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800711 inet_frags_init_net(&nf_init_frags);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700712 inet_frags_init(&nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800713
714 return 0;
715}
716
717void nf_ct_frag6_cleanup(void)
718{
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700719 inet_frags_fini(&nf_frags);
720
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800721 nf_init_frags.low_thresh = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800722 nf_ct_frag6_evictor();
723}