blob: e0b9424fa1b2b3457cc99051b49c22105f5f3aff [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#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,
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};
101#endif
102
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700103static unsigned int nf_hashfn(struct inet_frag_queue *q)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104{
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200105 const struct nf_ct_frag6_queue *nq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700107 nq = container_of(q, struct nf_ct_frag6_queue, q);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700108 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109}
110
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700111static void nf_skb_free(struct sk_buff *skb)
112{
113 if (NFCT_FRAG6_CB(skb)->orig)
114 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
115}
116
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800117/* Memory Tracking Functions. */
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800118static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119{
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800120 if (work)
121 *work -= skb->truesize;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800122 atomic_sub(skb->truesize, &nf_init_frags.mem);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700123 nf_skb_free(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124 kfree_skb(skb);
125}
126
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800127/* Destruction primitives. */
128
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700129static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700131 inet_frag_put(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132}
133
134/* Kill fq entry. It is not destroyed immediately,
135 * because caller (and someone more) holds reference count.
136 */
137static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
138{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700139 inet_frag_kill(&fq->q, &nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800140}
141
142static void nf_ct_frag6_evictor(void)
143{
David S. Millere8e16b72008-03-28 17:30:18 -0700144 local_bh_disable();
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800145 inet_frag_evictor(&nf_init_frags, &nf_frags);
David S. Millere8e16b72008-03-28 17:30:18 -0700146 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147}
148
149static void nf_ct_frag6_expire(unsigned long data)
150{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700151 struct nf_ct_frag6_queue *fq;
152
153 fq = container_of((struct inet_frag_queue *)data,
154 struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800155
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700156 spin_lock(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157
Joe Perchesbc578a52008-03-28 16:35:27 -0700158 if (fq->q.last_in & INET_FRAG_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159 goto out;
160
161 fq_kill(fq);
162
163out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700164 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700165 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166}
167
168/* Creation primitives. */
169
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700170static __inline__ struct nf_ct_frag6_queue *
171fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800172{
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700173 struct inet_frag_queue *q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700174 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700175 unsigned int hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700177 arg.id = id;
178 arg.src = src;
179 arg.dst = dst;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700180
181 read_lock_bh(&nf_frags.lock);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700182 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800184 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700185 local_bh_enable();
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700186 if (q == NULL)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800187 goto oom;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700189 return container_of(q, struct nf_ct_frag6_queue, q);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800190
191oom:
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700192 pr_debug("Can't alloc new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193 return NULL;
194}
195
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800196
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900197static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200198 const struct frag_hdr *fhdr, int nhoff)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199{
200 struct sk_buff *prev, *next;
201 int offset, end;
202
Joe Perchesbc578a52008-03-28 16:35:27 -0700203 if (fq->q.last_in & INET_FRAG_COMPLETE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700204 pr_debug("Allready completed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 goto err;
206 }
207
208 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700209 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
210 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211
212 if ((unsigned int)end > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700213 pr_debug("offset is too large.\n");
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900214 return -1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215 }
216
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700217 if (skb->ip_summed == CHECKSUM_COMPLETE) {
218 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900219 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700220 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221 0));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700222 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223
224 /* Is this the final fragment? */
225 if (!(fhdr->frag_off & htons(IP6_MF))) {
226 /* If we already have some bits beyond end
227 * or have different end, the segment is corrupted.
228 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700229 if (end < fq->q.len ||
Joe Perchesbc578a52008-03-28 16:35:27 -0700230 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700231 pr_debug("already received last fragment\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232 goto err;
233 }
Joe Perchesbc578a52008-03-28 16:35:27 -0700234 fq->q.last_in |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700235 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800236 } else {
237 /* Check if the fragment is rounded to 8 bytes.
238 * Required by the RFC.
239 */
240 if (end & 0x7) {
241 /* RFC2460 says always send parameter problem in
242 * this case. -DaveM
243 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700244 pr_debug("end of fragment not rounded to 8 bytes.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245 return -1;
246 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700247 if (end > fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800248 /* Some bits beyond end -> corruption. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700249 if (fq->q.last_in & INET_FRAG_LAST_IN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700250 pr_debug("last packet already reached.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800251 goto err;
252 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700253 fq->q.len = end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254 }
255 }
256
257 if (end == offset)
258 goto err;
259
260 /* Point into the IP datagram 'data' part. */
261 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700262 pr_debug("queue: message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800263 goto err;
264 }
Herbert Xub38dfee2006-06-09 16:13:01 -0700265 if (pskb_trim_rcsum(skb, end - offset)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700266 pr_debug("Can't trim\n");
Herbert Xub38dfee2006-06-09 16:13:01 -0700267 goto err;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800268 }
269
270 /* Find out which fragments are in front and at the back of us
271 * in the chain of fragments so far. We must know where to put
272 * this fragment, right?
273 */
274 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700275 for (next = fq->q.fragments; next != NULL; next = next->next) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800276 if (NFCT_FRAG6_CB(next)->offset >= offset)
277 break; /* bingo! */
278 prev = next;
279 }
280
281 /* We found where to put this one. Check for overlap with
282 * preceding fragment, and, if needed, align things so that
283 * any overlaps are eliminated.
284 */
285 if (prev) {
286 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
287
288 if (i > 0) {
289 offset += i;
290 if (end <= offset) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700291 pr_debug("overlap\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292 goto err;
293 }
294 if (!pskb_pull(skb, i)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700295 pr_debug("Can't pull\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800296 goto err;
297 }
298 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
299 skb->ip_summed = CHECKSUM_NONE;
300 }
301 }
302
303 /* Look for overlap with succeeding segments.
304 * If we can merge fragments, do it.
305 */
306 while (next && NFCT_FRAG6_CB(next)->offset < end) {
307 /* overlap is 'i' bytes */
308 int i = end - NFCT_FRAG6_CB(next)->offset;
309
310 if (i < next->len) {
311 /* Eat head of the next overlapped fragment
312 * and leave the loop. The next ones cannot overlap.
313 */
Patrick McHardy0d537782007-07-07 22:39:38 -0700314 pr_debug("Eat head of the overlapped parts.: %d", i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800315 if (!pskb_pull(next, i))
316 goto err;
317
318 /* next fragment */
319 NFCT_FRAG6_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700320 fq->q.meat -= i;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800321 if (next->ip_summed != CHECKSUM_UNNECESSARY)
322 next->ip_summed = CHECKSUM_NONE;
323 break;
324 } else {
325 struct sk_buff *free_it = next;
326
327 /* Old fragmnet is completely overridden with
328 * new one drop it.
329 */
330 next = next->next;
331
332 if (prev)
333 prev->next = next;
334 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700335 fq->q.fragments = next;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800336
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700337 fq->q.meat -= free_it->len;
Yasuyuki Kozakai1ba430b2005-11-14 15:28:18 -0800338 frag_kfree_skb(free_it, NULL);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800339 }
340 }
341
342 NFCT_FRAG6_CB(skb)->offset = offset;
343
344 /* Insert this fragment in the chain of fragments. */
345 skb->next = next;
346 if (prev)
347 prev->next = skb;
348 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700349 fq->q.fragments = skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800350
351 skb->dev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 fq->q.stamp = skb->tstamp;
353 fq->q.meat += skb->len;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800354 atomic_add(skb->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355
356 /* The first fragment.
357 * nhoffset is obtained from the first fragment, of course.
358 */
359 if (offset == 0) {
360 fq->nhoffset = nhoff;
Joe Perchesbc578a52008-03-28 16:35:27 -0700361 fq->q.last_in |= INET_FRAG_FIRST_IN;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800362 }
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700363 write_lock(&nf_frags.lock);
Pavel Emelyanov3140c252008-01-22 06:11:48 -0800364 list_move_tail(&fq->q.lru_list, &nf_init_frags.lru_list);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700365 write_unlock(&nf_frags.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800366 return 0;
367
368err:
369 return -1;
370}
371
372/*
373 * Check if this packet is complete.
374 * Returns NULL on failure by any reason, and pointer
375 * to current nexthdr field in reassembled frame.
376 *
377 * It is called with locked fq, and caller must check that
378 * queue is eligible for reassembly i.e. it is not COMPLETE,
379 * the last and the first frames arrived and all the bits are here.
380 */
381static struct sk_buff *
382nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
383{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700384 struct sk_buff *fp, *op, *head = fq->q.fragments;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 int payload_len;
386
387 fq_kill(fq);
388
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700389 WARN_ON(head == NULL);
390 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800391
392 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700393 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700394 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700395 sizeof(struct frag_hdr));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800396 if (payload_len > IPV6_MAXPLEN) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700397 pr_debug("payload len is too large.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398 goto out_oversize;
399 }
400
401 /* Head of list must not be cloned. */
402 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700403 pr_debug("skb is cloned but can't expand head");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 goto out_oom;
405 }
406
407 /* If the first fragment is fragmented itself, we split
408 * it to two chunks: the first with data and paged part
409 * and the second, holding only fragments. */
David S. Miller343a9972009-06-09 00:23:58 -0700410 if (skb_has_frags(head)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411 struct sk_buff *clone;
412 int i, plen = 0;
413
414 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700415 pr_debug("Can't alloc skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416 goto out_oom;
417 }
418 clone->next = head->next;
419 head->next = clone;
420 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Miller343a9972009-06-09 00:23:58 -0700421 skb_frag_list_init(head);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800422 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
423 plen += skb_shinfo(head)->frags[i].size;
424 clone->len = clone->data_len = head->data_len - plen;
425 head->data_len -= clone->len;
426 head->len -= clone->len;
427 clone->csum = 0;
428 clone->ip_summed = head->ip_summed;
429
430 NFCT_FRAG6_CB(clone)->orig = NULL;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800431 atomic_add(clone->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432 }
433
434 /* We have to remove fragment header from datagram and to relocate
435 * header in order to calculate ICV correctly. */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300436 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900437 memmove(head->head + sizeof(struct frag_hdr), head->head,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800438 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700439 head->mac_header += sizeof(struct frag_hdr);
440 head->network_header += sizeof(struct frag_hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800441
442 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300443 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700444 skb_push(head, head->data - skb_network_header(head));
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800445 atomic_sub(head->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800446
447 for (fp=head->next; fp; fp = fp->next) {
448 head->data_len += fp->len;
449 head->len += fp->len;
450 if (head->ip_summed != fp->ip_summed)
451 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700452 else if (head->ip_summed == CHECKSUM_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800453 head->csum = csum_add(head->csum, fp->csum);
454 head->truesize += fp->truesize;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800455 atomic_sub(fp->truesize, &nf_init_frags.mem);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456 }
457
458 head->next = NULL;
459 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700460 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700461 ipv6_hdr(head)->payload_len = htons(payload_len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462
463 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700464 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700465 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300466 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700467 head->csum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700469 fq->q.fragments = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470
471 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
472 fp = skb_shinfo(head)->frag_list;
473 if (NFCT_FRAG6_CB(fp)->orig == NULL)
474 /* at above code, head skb is divided into two skbs. */
475 fp = fp->next;
476
477 op = NFCT_FRAG6_CB(head)->orig;
478 for (; fp; fp = fp->next) {
479 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
480
481 op->next = orig;
482 op = orig;
483 NFCT_FRAG6_CB(fp)->orig = NULL;
484 }
485
486 return head;
487
488out_oversize:
489 if (net_ratelimit())
490 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
491 goto out_fail;
492out_oom:
493 if (net_ratelimit())
494 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
495out_fail:
496 return NULL;
497}
498
499/*
500 * find the header just before Fragment Header.
501 *
502 * if success return 0 and set ...
503 * (*prevhdrp): the value of "Next Header Field" in the header
504 * just before Fragment Header.
505 * (*prevhoff): the offset of "Next Header Field" in the header
506 * just before Fragment Header.
507 * (*fhoff) : the offset of Fragment Header.
508 *
509 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
510 *
511 */
512static int
513find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
514{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700515 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -0300516 const int netoff = skb_network_offset(skb);
517 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
518 int start = netoff + sizeof(struct ipv6hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519 int len = skb->len - start;
520 u8 prevhdr = NEXTHDR_IPV6;
521
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900522 while (nexthdr != NEXTHDR_FRAGMENT) {
523 struct ipv6_opt_hdr hdr;
524 int hdrlen;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800525
526 if (!ipv6_ext_hdr(nexthdr)) {
527 return -1;
528 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900529 if (nexthdr == NEXTHDR_NONE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700530 pr_debug("next header is none\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800531 return -1;
532 }
Christoph Paaschd1238d52009-03-16 15:52:11 +0100533 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
534 pr_debug("too short\n");
535 return -1;
536 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900537 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
538 BUG();
539 if (nexthdr == NEXTHDR_AUTH)
540 hdrlen = (hdr.hdrlen+2)<<2;
541 else
542 hdrlen = ipv6_optlen(&hdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800543
544 prevhdr = nexthdr;
545 prev_nhoff = start;
546
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900547 nexthdr = hdr.nexthdr;
548 len -= hdrlen;
549 start += hdrlen;
550 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800551
552 if (len < 0)
553 return -1;
554
555 *prevhdrp = prevhdr;
556 *prevhoff = prev_nhoff;
557 *fhoff = start;
558
559 return 0;
560}
561
562struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
563{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900564 struct sk_buff *clone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800565 struct net_device *dev = skb->dev;
566 struct frag_hdr *fhdr;
567 struct nf_ct_frag6_queue *fq;
568 struct ipv6hdr *hdr;
569 int fhoff, nhoff;
570 u8 prevhdr;
571 struct sk_buff *ret_skb = NULL;
572
573 /* Jumbo payload inhibits frag. header */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700574 if (ipv6_hdr(skb)->payload_len == 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700575 pr_debug("payload len = 0\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800576 return skb;
577 }
578
579 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
580 return skb;
581
582 clone = skb_clone(skb, GFP_ATOMIC);
583 if (clone == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700584 pr_debug("Can't clone skb\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800585 return skb;
586 }
587
588 NFCT_FRAG6_CB(clone)->orig = skb;
589
590 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700591 pr_debug("message is too short.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800592 goto ret_orig;
593 }
594
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300595 skb_set_transport_header(clone, fhoff);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700596 hdr = ipv6_hdr(clone);
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300597 fhdr = (struct frag_hdr *)skb_transport_header(clone);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800598
599 if (!(fhdr->frag_off & htons(0xFFF9))) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700600 pr_debug("Invalid fragment offset\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800601 /* It is not a fragmented frame */
602 goto ret_orig;
603 }
604
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800605 if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800606 nf_ct_frag6_evictor();
607
608 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
609 if (fq == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700610 pr_debug("Can't find and can't create new queue\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800611 goto ret_orig;
612 }
613
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700614 spin_lock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800615
616 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700617 spin_unlock_bh(&fq->q.lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700618 pr_debug("Can't insert skb to queue\n");
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700619 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800620 goto ret_orig;
621 }
622
Joe Perchesbc578a52008-03-28 16:35:27 -0700623 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
624 fq->q.meat == fq->q.len) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800625 ret_skb = nf_ct_frag6_reasm(fq, dev);
626 if (ret_skb == NULL)
Patrick McHardy0d537782007-07-07 22:39:38 -0700627 pr_debug("Can't reassemble fragmented packets\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800628 }
Jarek Poplawskib9c69892008-06-04 09:58:27 -0700629 spin_unlock_bh(&fq->q.lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800630
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700631 fq_put(fq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800632 return ret_skb;
633
634ret_orig:
635 kfree_skb(clone);
636 return skb;
637}
638
639void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
640 struct net_device *in, struct net_device *out,
641 int (*okfn)(struct sk_buff *))
642{
643 struct sk_buff *s, *s2;
644
645 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
646 nf_conntrack_put_reasm(s->nfct_reasm);
647 nf_conntrack_get_reasm(skb);
648 s->nfct_reasm = skb;
649
650 s2 = s->next;
Patrick McHardyf9f02cc2007-01-09 14:32:41 -0800651 s->next = NULL;
652
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
654 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
655 s = s2;
656 }
657 nf_conntrack_put_reasm(skb);
658}
659
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800660int nf_ct_frag6_init(void)
661{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700662 nf_frags.hashfn = nf_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700663 nf_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700664 nf_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700665 nf_frags.skb_free = nf_skb_free;
666 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700667 nf_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700668 nf_frags.frag_expire = nf_ct_frag6_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800669 nf_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800670 nf_init_frags.timeout = IPV6_FRAG_TIMEOUT;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800671 nf_init_frags.high_thresh = 256 * 1024;
672 nf_init_frags.low_thresh = 192 * 1024;
Pavel Emelyanove5a2bb8422008-01-22 06:06:23 -0800673 inet_frags_init_net(&nf_init_frags);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700674 inet_frags_init(&nf_frags);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800675
676 return 0;
677}
678
679void nf_ct_frag6_cleanup(void)
680{
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700681 inet_frags_fini(&nf_frags);
682
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800683 nf_init_frags.low_thresh = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800684 nf_ct_frag6_evictor();
685}