blob: 73ea204eaa6f22c3bf9aa6e9089b262f24b4b490 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 fragment reassembly
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
9 *
10 * Based on: net/ipv4/ip_fragment.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090018/*
19 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Andi Kleen Make it work with multiple hosts.
21 * More RFC compliance.
22 *
23 * Horst von Brand Add missing #include <linux/string.h>
24 * Alexey Kuznetsov SMP races, threading, cleanup.
25 * Patrick McHardy LRU queue of frag heads for evictor.
26 * Mitsuru KANDA @USAGI Register inet6_protocol{}.
27 * David Stevens and
28 * YOSHIFUJI,H. @USAGI Always remove fragment header to
29 * calculate ICV correctly.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/types.h>
33#include <linux/string.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/jiffies.h>
37#include <linux/net.h>
38#include <linux/list.h>
39#include <linux/netdevice.h>
40#include <linux/in6.h>
41#include <linux/ipv6.h>
42#include <linux/icmpv6.h>
43#include <linux/random.h>
44#include <linux/jhash.h>
Herbert Xuf61944e2007-10-15 01:28:47 -070045#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/sock.h>
48#include <net/snmp.h>
49
50#include <net/ipv6.h>
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +090051#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/protocol.h>
53#include <net/transp_v6.h>
54#include <net/rawv6.h>
55#include <net/ndisc.h>
56#include <net/addrconf.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070057#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct ip6frag_skb_cb
60{
61 struct inet6_skb_parm h;
62 int offset;
63};
64
65#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
66
67
68/*
69 * Equivalent of ipv4 struct ipq
70 */
71
72struct frag_queue
73{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070074 struct inet_frag_queue q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Al Viroe69a4ad2006-11-14 20:56:00 -080076 __be32 id; /* fragment id */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct in6_addr saddr;
78 struct in6_addr daddr;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned int csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 __u16 nhoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Pavel Emelyanov04128f22007-10-15 02:33:45 -070085struct inet_frags_ctl ip6_frags_ctl __read_mostly = {
86 .high_thresh = 256 * 1024,
87 .low_thresh = 192 * 1024,
88 .timeout = IPV6_FRAG_TIMEOUT,
89 .secret_interval = 10 * 60 * HZ,
90};
91
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070092static struct inet_frags ip6_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070094int ip6_frag_nqueues(void)
95{
96 return ip6_frags.nqueues;
97}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070099int ip6_frag_mem(void)
100{
101 return atomic_read(&ip6_frags.mem);
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Herbert Xuf61944e2007-10-15 01:28:47 -0700104static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
105 struct net_device *dev);
106
Zach Brownf6596f92006-04-10 16:05:34 -0700107/*
108 * callers should be careful not to use the hash value outside the ipfrag_lock
109 * as doing so could race with ipfrag_hash_rnd being recalculated.
110 */
Al Viroe69a4ad2006-11-14 20:56:00 -0800111static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct in6_addr *daddr)
113{
114 u32 a, b, c;
115
Al Viroe69a4ad2006-11-14 20:56:00 -0800116 a = (__force u32)saddr->s6_addr32[0];
117 b = (__force u32)saddr->s6_addr32[1];
118 c = (__force u32)saddr->s6_addr32[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 a += JHASH_GOLDEN_RATIO;
121 b += JHASH_GOLDEN_RATIO;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700122 c += ip6_frags.rnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 __jhash_mix(a, b, c);
124
Al Viroe69a4ad2006-11-14 20:56:00 -0800125 a += (__force u32)saddr->s6_addr32[3];
126 b += (__force u32)daddr->s6_addr32[0];
127 c += (__force u32)daddr->s6_addr32[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 __jhash_mix(a, b, c);
129
Al Viroe69a4ad2006-11-14 20:56:00 -0800130 a += (__force u32)daddr->s6_addr32[2];
131 b += (__force u32)daddr->s6_addr32[3];
132 c += (__force u32)id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 __jhash_mix(a, b, c);
134
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700135 return c & (INETFRAGS_HASHSZ - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700138static unsigned int ip6_hashfn(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700140 struct frag_queue *fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700142 fq = container_of(q, struct frag_queue, q);
143 return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700146int ip6_frag_equal(struct inet_frag_queue *q1, struct inet_frag_queue *q2)
147{
148 struct frag_queue *fq1, *fq2;
149
150 fq1 = container_of(q1, struct frag_queue, q);
151 fq2 = container_of(q2, struct frag_queue, q);
152 return (fq1->id == fq2->id &&
153 ipv6_addr_equal(&fq2->saddr, &fq1->saddr) &&
154 ipv6_addr_equal(&fq2->daddr, &fq1->daddr));
155}
156EXPORT_SYMBOL(ip6_frag_equal);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Memory Tracking Functions. */
159static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
160{
161 if (work)
162 *work -= skb->truesize;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700163 atomic_sub(skb->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 kfree_skb(skb);
165}
166
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700167static void ip6_frag_free(struct inet_frag_queue *fq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700169 kfree(container_of(fq, struct frag_queue, q));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static inline struct frag_queue *frag_alloc_queue(void)
173{
Ingo Oeser78c784c2006-03-20 23:01:17 -0800174 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if(!fq)
177 return NULL;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700178 atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return fq;
180}
181
182/* Destruction primitives. */
183
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700184static __inline__ void fq_put(struct frag_queue *fq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700186 inet_frag_put(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/* Kill fq entry. It is not destroyed immediately,
190 * because caller (and someone more) holds reference count.
191 */
192static __inline__ void fq_kill(struct frag_queue *fq)
193{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700194 inet_frag_kill(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900197static void ip6_evictor(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700199 int evicted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700201 evicted = inet_frag_evictor(&ip6_frags);
202 if (evicted)
203 IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static void ip6_frag_expire(unsigned long data)
207{
208 struct frag_queue *fq = (struct frag_queue *) data;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900209 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700211 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700213 if (fq->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 goto out;
215
216 fq_kill(fq);
217
Eric W. Biederman881d9662007-09-17 11:56:21 -0700218 dev = dev_get_by_index(&init_net, fq->iif);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900219 if (!dev)
220 goto out;
221
222 rcu_read_lock();
223 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
224 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Ingo Oeser78c784c2006-03-20 23:01:17 -0800227 /* Don't send error if the first segment did not arrive. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700228 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
Ingo Oeser78c784c2006-03-20 23:01:17 -0800229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Ingo Oeser78c784c2006-03-20 23:01:17 -0800231 /*
232 But use as source device on which LAST ARRIVED
233 segment was received. And do not use fq->dev
234 pointer directly, device might already disappeared.
235 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700236 fq->q.fragments->dev = dev;
237 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238out:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900239 if (dev)
240 dev_put(dev);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700241 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700242 fq_put(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/* Creation primitives. */
246
247
Pavel Emelyanovfd9e6352007-10-17 19:43:37 -0700248static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in,
249 unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700251 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700253 q = inet_frag_intern(&fq_in->q, &ip6_frags, hash);
254 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257
258static struct frag_queue *
Al Viroe69a4ad2006-11-14 20:56:00 -0800259ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
Pavel Emelyanovfd9e6352007-10-17 19:43:37 -0700260 struct inet6_dev *idev, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct frag_queue *fq;
263
264 if ((fq = frag_alloc_queue()) == NULL)
265 goto oom;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 fq->id = id;
268 ipv6_addr_copy(&fq->saddr, src);
269 ipv6_addr_copy(&fq->daddr, dst);
270
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700271 init_timer(&fq->q.timer);
272 fq->q.timer.function = ip6_frag_expire;
273 fq->q.timer.data = (long) fq;
274 spin_lock_init(&fq->q.lock);
275 atomic_set(&fq->q.refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Pavel Emelyanovfd9e6352007-10-17 19:43:37 -0700277 return ip6_frag_intern(fq, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279oom:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900280 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return NULL;
282}
283
284static __inline__ struct frag_queue *
Al Viroe69a4ad2006-11-14 20:56:00 -0800285fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900286 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct frag_queue *fq;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800289 struct hlist_node *n;
Zach Brownf6596f92006-04-10 16:05:34 -0700290 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700292 read_lock(&ip6_frags.lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700293 hash = ip6qhashfn(id, src, dst);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700294 hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900295 if (fq->id == id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ipv6_addr_equal(src, &fq->saddr) &&
297 ipv6_addr_equal(dst, &fq->daddr)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700298 atomic_inc(&fq->q.refcnt);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700299 read_unlock(&ip6_frags.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return fq;
301 }
302 }
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700303 read_unlock(&ip6_frags.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Pavel Emelyanovfd9e6352007-10-17 19:43:37 -0700305 return ip6_frag_create(id, src, dst, idev, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308
Herbert Xuf61944e2007-10-15 01:28:47 -0700309static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct frag_hdr *fhdr, int nhoff)
311{
312 struct sk_buff *prev, *next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700313 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int offset, end;
315
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700316 if (fq->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto err;
318
319 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700320 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
321 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if ((unsigned int)end > IPV6_MAXPLEN) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900324 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
325 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700326 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
327 ((u8 *)&fhdr->frag_off -
328 skb_network_header(skb)));
Herbert Xuf61944e2007-10-15 01:28:47 -0700329 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700332 if (skb->ip_summed == CHECKSUM_COMPLETE) {
333 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900334 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700335 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
336 0));
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Is this the final fragment? */
340 if (!(fhdr->frag_off & htons(IP6_MF))) {
341 /* If we already have some bits beyond end
342 * or have different end, the segment is corrupted.
343 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700344 if (end < fq->q.len ||
345 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700347 fq->q.last_in |= LAST_IN;
348 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
350 /* Check if the fragment is rounded to 8 bytes.
351 * Required by the RFC.
352 */
353 if (end & 0x7) {
354 /* RFC2460 says always send parameter problem in
355 * this case. -DaveM
356 */
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900357 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
358 IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900359 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 offsetof(struct ipv6hdr, payload_len));
Herbert Xuf61944e2007-10-15 01:28:47 -0700361 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700363 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Some bits beyond end -> corruption. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700365 if (fq->q.last_in & LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700367 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
370
371 if (end == offset)
372 goto err;
373
374 /* Point into the IP datagram 'data' part. */
375 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
376 goto err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900377
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700378 if (pskb_trim_rcsum(skb, end - offset))
379 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* Find out which fragments are in front and at the back of us
382 * in the chain of fragments so far. We must know where to put
383 * this fragment, right?
384 */
385 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700386 for(next = fq->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (FRAG6_CB(next)->offset >= offset)
388 break; /* bingo! */
389 prev = next;
390 }
391
392 /* We found where to put this one. Check for overlap with
393 * preceding fragment, and, if needed, align things so that
394 * any overlaps are eliminated.
395 */
396 if (prev) {
397 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
398
399 if (i > 0) {
400 offset += i;
401 if (end <= offset)
402 goto err;
403 if (!pskb_pull(skb, i))
404 goto err;
405 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
406 skb->ip_summed = CHECKSUM_NONE;
407 }
408 }
409
410 /* Look for overlap with succeeding segments.
411 * If we can merge fragments, do it.
412 */
413 while (next && FRAG6_CB(next)->offset < end) {
414 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
415
416 if (i < next->len) {
417 /* Eat head of the next overlapped fragment
418 * and leave the loop. The next ones cannot overlap.
419 */
420 if (!pskb_pull(next, i))
421 goto err;
422 FRAG6_CB(next)->offset += i; /* next fragment */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700423 fq->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (next->ip_summed != CHECKSUM_UNNECESSARY)
425 next->ip_summed = CHECKSUM_NONE;
426 break;
427 } else {
428 struct sk_buff *free_it = next;
429
430 /* Old fragment is completely overridden with
431 * new one drop it.
432 */
433 next = next->next;
434
435 if (prev)
436 prev->next = next;
437 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700438 fq->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700440 fq->q.meat -= free_it->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 frag_kfree_skb(free_it, NULL);
442 }
443 }
444
445 FRAG6_CB(skb)->offset = offset;
446
447 /* Insert this fragment in the chain of fragments. */
448 skb->next = next;
449 if (prev)
450 prev->next = skb;
451 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700452 fq->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Herbert Xuf61944e2007-10-15 01:28:47 -0700454 dev = skb->dev;
455 if (dev) {
456 fq->iif = dev->ifindex;
457 skb->dev = NULL;
458 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700459 fq->q.stamp = skb->tstamp;
460 fq->q.meat += skb->len;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700461 atomic_add(skb->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* The first fragment.
464 * nhoffset is obtained from the first fragment, of course.
465 */
466 if (offset == 0) {
467 fq->nhoffset = nhoff;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700468 fq->q.last_in |= FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700470
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700471 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
Herbert Xuf61944e2007-10-15 01:28:47 -0700472 return ip6_frag_reasm(fq, prev, dev);
473
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700474 write_lock(&ip6_frags.lock);
475 list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
476 write_unlock(&ip6_frags.lock);
Herbert Xuf61944e2007-10-15 01:28:47 -0700477 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479err:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900480 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 kfree_skb(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700482 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/*
486 * Check if this packet is complete.
487 * Returns NULL on failure by any reason, and pointer
488 * to current nexthdr field in reassembled frame.
489 *
490 * It is called with locked fq, and caller must check that
491 * queue is eligible for reassembly i.e. it is not COMPLETE,
492 * the last and the first frames arrived and all the bits are here.
493 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700494static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct net_device *dev)
496{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700497 struct sk_buff *fp, *head = fq->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int payload_len;
499 unsigned int nhoff;
500
501 fq_kill(fq);
502
Herbert Xuf61944e2007-10-15 01:28:47 -0700503 /* Make the one we just received the head. */
504 if (prev) {
505 head = prev->next;
506 fp = skb_clone(head, GFP_ATOMIC);
507
508 if (!fp)
509 goto out_oom;
510
511 fp->next = head->next;
512 prev->next = fp;
513
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700514 skb_morph(head, fq->q.fragments);
515 head->next = fq->q.fragments->next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700516
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700517 kfree_skb(fq->q.fragments);
518 fq->q.fragments = head;
Herbert Xuf61944e2007-10-15 01:28:47 -0700519 }
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 BUG_TRAP(head != NULL);
522 BUG_TRAP(FRAG6_CB(head)->offset == 0);
523
524 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700525 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700526 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700527 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (payload_len > IPV6_MAXPLEN)
529 goto out_oversize;
530
531 /* Head of list must not be cloned. */
532 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
533 goto out_oom;
534
535 /* If the first fragment is fragmented itself, we split
536 * it to two chunks: the first with data and paged part
537 * and the second, holding only fragments. */
538 if (skb_shinfo(head)->frag_list) {
539 struct sk_buff *clone;
540 int i, plen = 0;
541
542 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
543 goto out_oom;
544 clone->next = head->next;
545 head->next = clone;
546 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
547 skb_shinfo(head)->frag_list = NULL;
548 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
549 plen += skb_shinfo(head)->frags[i].size;
550 clone->len = clone->data_len = head->data_len - plen;
551 head->data_len -= clone->len;
552 head->len -= clone->len;
553 clone->csum = 0;
554 clone->ip_summed = head->ip_summed;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700555 atomic_add(clone->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 /* We have to remove fragment header from datagram and to relocate
559 * header in order to calculate ICV correctly. */
560 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700561 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900562 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700564 head->mac_header += sizeof(struct frag_hdr);
565 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300568 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700569 skb_push(head, head->data - skb_network_header(head));
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700570 atomic_sub(head->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 for (fp=head->next; fp; fp = fp->next) {
573 head->data_len += fp->len;
574 head->len += fp->len;
575 if (head->ip_summed != fp->ip_summed)
576 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700577 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 head->csum = csum_add(head->csum, fp->csum);
579 head->truesize += fp->truesize;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700580 atomic_sub(fp->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 head->next = NULL;
584 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700585 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700586 ipv6_hdr(head)->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800587 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700590 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700591 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300592 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700593 head->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900595 rcu_read_lock();
596 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
597 rcu_read_unlock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700598 fq->q.fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 1;
600
601out_oversize:
602 if (net_ratelimit())
603 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
604 goto out_fail;
605out_oom:
606 if (net_ratelimit())
607 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
608out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900609 rcu_read_lock();
610 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
611 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -1;
613}
614
Herbert Xue5bbef22007-10-15 12:50:28 -0700615static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct frag_hdr *fhdr;
618 struct frag_queue *fq;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700619 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900621 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Jumbo payload inhibits frag. header */
624 if (hdr->payload_len==0) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900625 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300626 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
627 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -1;
629 }
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700630 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
631 sizeof(struct frag_hdr)))) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900632 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300633 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
634 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -1;
636 }
637
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700638 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700639 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (!(fhdr->frag_off & htons(0xFFF9))) {
642 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700643 skb->transport_header += sizeof(struct frag_hdr);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900644 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700646 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 1;
648 }
649
Pavel Emelyanov04128f22007-10-15 02:33:45 -0700650 if (atomic_read(&ip6_frags.mem) > ip6_frags_ctl.high_thresh)
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900651 ip6_evictor(ip6_dst_idev(skb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900653 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
654 ip6_dst_idev(skb->dst))) != NULL) {
Herbert Xuf61944e2007-10-15 01:28:47 -0700655 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700657 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Herbert Xuf61944e2007-10-15 01:28:47 -0700659 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700661 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700662 fq_put(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return ret;
664 }
665
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900666 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 kfree_skb(skb);
668 return -1;
669}
670
671static struct inet6_protocol frag_protocol =
672{
673 .handler = ipv6_frag_rcv,
674 .flags = INET6_PROTO_NOPOLICY,
675};
676
677void __init ipv6_frag_init(void)
678{
679 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
680 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
681
Pavel Emelyanov04128f22007-10-15 02:33:45 -0700682 ip6_frags.ctl = &ip6_frags_ctl;
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700683 ip6_frags.hashfn = ip6_hashfn;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700684 ip6_frags.destructor = ip6_frag_free;
685 ip6_frags.skb_free = NULL;
686 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanov2588fe12007-10-17 19:44:34 -0700687 ip6_frags.equal = ip6_frag_equal;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700688 inet_frags_init(&ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}