blob: de795c04e34c1862723eb8100a8f376a5b375ebb [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>
45
46#include <net/sock.h>
47#include <net/snmp.h>
48
49#include <net/ipv6.h>
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +090050#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/protocol.h>
52#include <net/transp_v6.h>
53#include <net/rawv6.h>
54#include <net/ndisc.h>
55#include <net/addrconf.h>
56
Brian Haleyab32ea52006-09-22 14:15:41 -070057int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
58int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Brian Haleyab32ea52006-09-22 14:15:41 -070060int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62struct ip6frag_skb_cb
63{
64 struct inet6_skb_parm h;
65 int offset;
66};
67
68#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
69
70
71/*
72 * Equivalent of ipv4 struct ipq
73 */
74
75struct frag_queue
76{
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -080077 struct hlist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct list_head lru_list; /* lru list member */
79
Al Viroe69a4adc2006-11-14 20:56:00 -080080 __be32 id; /* fragment id */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct in6_addr saddr;
82 struct in6_addr daddr;
83
84 spinlock_t lock;
85 atomic_t refcnt;
86 struct timer_list timer; /* expire timer */
87 struct sk_buff *fragments;
88 int len;
89 int meat;
90 int iif;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -070091 ktime_t stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned int csum;
93 __u8 last_in; /* has first/last segment arrived? */
94#define COMPLETE 4
95#define FIRST_IN 2
96#define LAST_IN 1
97 __u16 nhoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100/* Hash table. */
101
102#define IP6Q_HASHSZ 64
103
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800104static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static DEFINE_RWLOCK(ip6_frag_lock);
106static u32 ip6_frag_hash_rnd;
107static LIST_HEAD(ip6_frag_lru_list);
108int ip6_frag_nqueues = 0;
109
110static __inline__ void __fq_unlink(struct frag_queue *fq)
111{
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800112 hlist_del(&fq->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 list_del(&fq->lru_list);
114 ip6_frag_nqueues--;
115}
116
117static __inline__ void fq_unlink(struct frag_queue *fq)
118{
119 write_lock(&ip6_frag_lock);
120 __fq_unlink(fq);
121 write_unlock(&ip6_frag_lock);
122}
123
Zach Brownf6596f92006-04-10 16:05:34 -0700124/*
125 * callers should be careful not to use the hash value outside the ipfrag_lock
126 * as doing so could race with ipfrag_hash_rnd being recalculated.
127 */
Al Viroe69a4adc2006-11-14 20:56:00 -0800128static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct in6_addr *daddr)
130{
131 u32 a, b, c;
132
Al Viroe69a4adc2006-11-14 20:56:00 -0800133 a = (__force u32)saddr->s6_addr32[0];
134 b = (__force u32)saddr->s6_addr32[1];
135 c = (__force u32)saddr->s6_addr32[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 a += JHASH_GOLDEN_RATIO;
138 b += JHASH_GOLDEN_RATIO;
139 c += ip6_frag_hash_rnd;
140 __jhash_mix(a, b, c);
141
Al Viroe69a4adc2006-11-14 20:56:00 -0800142 a += (__force u32)saddr->s6_addr32[3];
143 b += (__force u32)daddr->s6_addr32[0];
144 c += (__force u32)daddr->s6_addr32[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 __jhash_mix(a, b, c);
146
Al Viroe69a4adc2006-11-14 20:56:00 -0800147 a += (__force u32)daddr->s6_addr32[2];
148 b += (__force u32)daddr->s6_addr32[3];
149 c += (__force u32)id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 __jhash_mix(a, b, c);
151
152 return c & (IP6Q_HASHSZ - 1);
153}
154
155static struct timer_list ip6_frag_secret_timer;
Brian Haleyab32ea52006-09-22 14:15:41 -0700156int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158static void ip6_frag_secret_rebuild(unsigned long dummy)
159{
160 unsigned long now = jiffies;
161 int i;
162
163 write_lock(&ip6_frag_lock);
164 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
165 for (i = 0; i < IP6Q_HASHSZ; i++) {
166 struct frag_queue *q;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800167 struct hlist_node *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800169 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int hval = ip6qhashfn(q->id,
171 &q->saddr,
172 &q->daddr);
173
174 if (hval != i) {
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800175 hlist_del(&q->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Relink to new hash chain. */
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800178 hlist_add_head(&q->list,
179 &ip6_frag_hash[hval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 }
184 write_unlock(&ip6_frag_lock);
185
186 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
187}
188
189atomic_t ip6_frag_mem = ATOMIC_INIT(0);
190
191/* Memory Tracking Functions. */
192static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
193{
194 if (work)
195 *work -= skb->truesize;
196 atomic_sub(skb->truesize, &ip6_frag_mem);
197 kfree_skb(skb);
198}
199
200static inline void frag_free_queue(struct frag_queue *fq, int *work)
201{
202 if (work)
203 *work -= sizeof(struct frag_queue);
204 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
205 kfree(fq);
206}
207
208static inline struct frag_queue *frag_alloc_queue(void)
209{
Ingo Oeser78c784c2006-03-20 23:01:17 -0800210 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if(!fq)
213 return NULL;
214 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
215 return fq;
216}
217
218/* Destruction primitives. */
219
220/* Complete destruction of fq. */
221static void ip6_frag_destroy(struct frag_queue *fq, int *work)
222{
223 struct sk_buff *fp;
224
225 BUG_TRAP(fq->last_in&COMPLETE);
226 BUG_TRAP(del_timer(&fq->timer) == 0);
227
228 /* Release all fragment data. */
229 fp = fq->fragments;
230 while (fp) {
231 struct sk_buff *xp = fp->next;
232
233 frag_kfree_skb(fp, work);
234 fp = xp;
235 }
236
237 frag_free_queue(fq, work);
238}
239
240static __inline__ void fq_put(struct frag_queue *fq, int *work)
241{
242 if (atomic_dec_and_test(&fq->refcnt))
243 ip6_frag_destroy(fq, work);
244}
245
246/* Kill fq entry. It is not destroyed immediately,
247 * because caller (and someone more) holds reference count.
248 */
249static __inline__ void fq_kill(struct frag_queue *fq)
250{
251 if (del_timer(&fq->timer))
252 atomic_dec(&fq->refcnt);
253
254 if (!(fq->last_in & COMPLETE)) {
255 fq_unlink(fq);
256 atomic_dec(&fq->refcnt);
257 fq->last_in |= COMPLETE;
258 }
259}
260
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900261static void ip6_evictor(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct frag_queue *fq;
264 struct list_head *tmp;
265 int work;
266
267 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
268 if (work <= 0)
269 return;
270
271 while(work > 0) {
272 read_lock(&ip6_frag_lock);
273 if (list_empty(&ip6_frag_lru_list)) {
274 read_unlock(&ip6_frag_lock);
275 return;
276 }
277 tmp = ip6_frag_lru_list.next;
278 fq = list_entry(tmp, struct frag_queue, lru_list);
279 atomic_inc(&fq->refcnt);
280 read_unlock(&ip6_frag_lock);
281
282 spin_lock(&fq->lock);
283 if (!(fq->last_in&COMPLETE))
284 fq_kill(fq);
285 spin_unlock(&fq->lock);
286
287 fq_put(fq, &work);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900288 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
291
292static void ip6_frag_expire(unsigned long data)
293{
294 struct frag_queue *fq = (struct frag_queue *) data;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900295 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 spin_lock(&fq->lock);
298
299 if (fq->last_in & COMPLETE)
300 goto out;
301
302 fq_kill(fq);
303
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900304 dev = dev_get_by_index(fq->iif);
305 if (!dev)
306 goto out;
307
308 rcu_read_lock();
309 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
310 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
311 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Ingo Oeser78c784c2006-03-20 23:01:17 -0800313 /* Don't send error if the first segment did not arrive. */
314 if (!(fq->last_in&FIRST_IN) || !fq->fragments)
315 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Ingo Oeser78c784c2006-03-20 23:01:17 -0800317 /*
318 But use as source device on which LAST ARRIVED
319 segment was received. And do not use fq->dev
320 pointer directly, device might already disappeared.
321 */
322 fq->fragments->dev = dev;
323 icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324out:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900325 if (dev)
326 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock(&fq->lock);
328 fq_put(fq, NULL);
329}
330
331/* Creation primitives. */
332
333
Zach Brownf6596f92006-04-10 16:05:34 -0700334static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct frag_queue *fq;
Zach Brownf6596f92006-04-10 16:05:34 -0700337 unsigned int hash;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800338#ifdef CONFIG_SMP
339 struct hlist_node *n;
340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 write_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700343 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#ifdef CONFIG_SMP
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800345 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900346 if (fq->id == fq_in->id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
348 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
349 atomic_inc(&fq->refcnt);
350 write_unlock(&ip6_frag_lock);
351 fq_in->last_in |= COMPLETE;
352 fq_put(fq_in, NULL);
353 return fq;
354 }
355 }
356#endif
357 fq = fq_in;
358
359 if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
360 atomic_inc(&fq->refcnt);
361
362 atomic_inc(&fq->refcnt);
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800363 hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 INIT_LIST_HEAD(&fq->lru_list);
365 list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
366 ip6_frag_nqueues++;
367 write_unlock(&ip6_frag_lock);
368 return fq;
369}
370
371
372static struct frag_queue *
Al Viroe69a4adc2006-11-14 20:56:00 -0800373ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900374 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct frag_queue *fq;
377
378 if ((fq = frag_alloc_queue()) == NULL)
379 goto oom;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 fq->id = id;
382 ipv6_addr_copy(&fq->saddr, src);
383 ipv6_addr_copy(&fq->daddr, dst);
384
385 init_timer(&fq->timer);
386 fq->timer.function = ip6_frag_expire;
387 fq->timer.data = (long) fq;
388 spin_lock_init(&fq->lock);
389 atomic_set(&fq->refcnt, 1);
390
Zach Brownf6596f92006-04-10 16:05:34 -0700391 return ip6_frag_intern(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393oom:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900394 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return NULL;
396}
397
398static __inline__ struct frag_queue *
Al Viroe69a4adc2006-11-14 20:56:00 -0800399fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900400 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct frag_queue *fq;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800403 struct hlist_node *n;
Zach Brownf6596f92006-04-10 16:05:34 -0700404 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 read_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700407 hash = ip6qhashfn(id, src, dst);
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800408 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900409 if (fq->id == id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ipv6_addr_equal(src, &fq->saddr) &&
411 ipv6_addr_equal(dst, &fq->daddr)) {
412 atomic_inc(&fq->refcnt);
413 read_unlock(&ip6_frag_lock);
414 return fq;
415 }
416 }
417 read_unlock(&ip6_frag_lock);
418
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900419 return ip6_frag_create(id, src, dst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900423static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct frag_hdr *fhdr, int nhoff)
425{
426 struct sk_buff *prev, *next;
427 int offset, end;
428
429 if (fq->last_in & COMPLETE)
430 goto err;
431
432 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700433 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
434 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if ((unsigned int)end > IPV6_MAXPLEN) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900437 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
438 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700439 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
440 ((u8 *)&fhdr->frag_off -
441 skb_network_header(skb)));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900442 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700445 if (skb->ip_summed == CHECKSUM_COMPLETE) {
446 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900447 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700448 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
449 0));
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* Is this the final fragment? */
453 if (!(fhdr->frag_off & htons(IP6_MF))) {
454 /* If we already have some bits beyond end
455 * or have different end, the segment is corrupted.
456 */
457 if (end < fq->len ||
458 ((fq->last_in & LAST_IN) && end != fq->len))
459 goto err;
460 fq->last_in |= LAST_IN;
461 fq->len = end;
462 } else {
463 /* Check if the fragment is rounded to 8 bytes.
464 * Required by the RFC.
465 */
466 if (end & 0x7) {
467 /* RFC2460 says always send parameter problem in
468 * this case. -DaveM
469 */
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900470 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
471 IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900472 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 offsetof(struct ipv6hdr, payload_len));
474 return;
475 }
476 if (end > fq->len) {
477 /* Some bits beyond end -> corruption. */
478 if (fq->last_in & LAST_IN)
479 goto err;
480 fq->len = end;
481 }
482 }
483
484 if (end == offset)
485 goto err;
486
487 /* Point into the IP datagram 'data' part. */
488 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
489 goto err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900490
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700491 if (pskb_trim_rcsum(skb, end - offset))
492 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Find out which fragments are in front and at the back of us
495 * in the chain of fragments so far. We must know where to put
496 * this fragment, right?
497 */
498 prev = NULL;
499 for(next = fq->fragments; next != NULL; next = next->next) {
500 if (FRAG6_CB(next)->offset >= offset)
501 break; /* bingo! */
502 prev = next;
503 }
504
505 /* We found where to put this one. Check for overlap with
506 * preceding fragment, and, if needed, align things so that
507 * any overlaps are eliminated.
508 */
509 if (prev) {
510 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
511
512 if (i > 0) {
513 offset += i;
514 if (end <= offset)
515 goto err;
516 if (!pskb_pull(skb, i))
517 goto err;
518 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
519 skb->ip_summed = CHECKSUM_NONE;
520 }
521 }
522
523 /* Look for overlap with succeeding segments.
524 * If we can merge fragments, do it.
525 */
526 while (next && FRAG6_CB(next)->offset < end) {
527 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
528
529 if (i < next->len) {
530 /* Eat head of the next overlapped fragment
531 * and leave the loop. The next ones cannot overlap.
532 */
533 if (!pskb_pull(next, i))
534 goto err;
535 FRAG6_CB(next)->offset += i; /* next fragment */
536 fq->meat -= i;
537 if (next->ip_summed != CHECKSUM_UNNECESSARY)
538 next->ip_summed = CHECKSUM_NONE;
539 break;
540 } else {
541 struct sk_buff *free_it = next;
542
543 /* Old fragment is completely overridden with
544 * new one drop it.
545 */
546 next = next->next;
547
548 if (prev)
549 prev->next = next;
550 else
551 fq->fragments = next;
552
553 fq->meat -= free_it->len;
554 frag_kfree_skb(free_it, NULL);
555 }
556 }
557
558 FRAG6_CB(skb)->offset = offset;
559
560 /* Insert this fragment in the chain of fragments. */
561 skb->next = next;
562 if (prev)
563 prev->next = skb;
564 else
565 fq->fragments = skb;
566
567 if (skb->dev)
568 fq->iif = skb->dev->ifindex;
569 skb->dev = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700570 fq->stamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 fq->meat += skb->len;
572 atomic_add(skb->truesize, &ip6_frag_mem);
573
574 /* The first fragment.
575 * nhoffset is obtained from the first fragment, of course.
576 */
577 if (offset == 0) {
578 fq->nhoffset = nhoff;
579 fq->last_in |= FIRST_IN;
580 }
581 write_lock(&ip6_frag_lock);
582 list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
583 write_unlock(&ip6_frag_lock);
584 return;
585
586err:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900587 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 kfree_skb(skb);
589}
590
591/*
592 * Check if this packet is complete.
593 * Returns NULL on failure by any reason, and pointer
594 * to current nexthdr field in reassembled frame.
595 *
596 * It is called with locked fq, and caller must check that
597 * queue is eligible for reassembly i.e. it is not COMPLETE,
598 * the last and the first frames arrived and all the bits are here.
599 */
600static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct net_device *dev)
602{
603 struct sk_buff *fp, *head = fq->fragments;
604 int payload_len;
605 unsigned int nhoff;
606
607 fq_kill(fq);
608
609 BUG_TRAP(head != NULL);
610 BUG_TRAP(FRAG6_CB(head)->offset == 0);
611
612 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700613 payload_len = ((head->data - skb_network_header(head)) -
614 sizeof(struct ipv6hdr) + fq->len -
615 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (payload_len > IPV6_MAXPLEN)
617 goto out_oversize;
618
619 /* Head of list must not be cloned. */
620 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
621 goto out_oom;
622
623 /* If the first fragment is fragmented itself, we split
624 * it to two chunks: the first with data and paged part
625 * and the second, holding only fragments. */
626 if (skb_shinfo(head)->frag_list) {
627 struct sk_buff *clone;
628 int i, plen = 0;
629
630 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
631 goto out_oom;
632 clone->next = head->next;
633 head->next = clone;
634 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
635 skb_shinfo(head)->frag_list = NULL;
636 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
637 plen += skb_shinfo(head)->frags[i].size;
638 clone->len = clone->data_len = head->data_len - plen;
639 head->data_len -= clone->len;
640 head->len -= clone->len;
641 clone->csum = 0;
642 clone->ip_summed = head->ip_summed;
643 atomic_add(clone->truesize, &ip6_frag_mem);
644 }
645
646 /* We have to remove fragment header from datagram and to relocate
647 * header in order to calculate ICV correctly. */
648 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700649 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900650 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700652 head->mac_header += sizeof(struct frag_hdr);
653 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300656 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700657 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 atomic_sub(head->truesize, &ip6_frag_mem);
659
660 for (fp=head->next; fp; fp = fp->next) {
661 head->data_len += fp->len;
662 head->len += fp->len;
663 if (head->ip_summed != fp->ip_summed)
664 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700665 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 head->csum = csum_add(head->csum, fp->csum);
667 head->truesize += fp->truesize;
668 atomic_sub(fp->truesize, &ip6_frag_mem);
669 }
670
671 head->next = NULL;
672 head->dev = dev;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700673 head->tstamp = fq->stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700674 ipv6_hdr(head)->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800675 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 *skb_in = head;
678
679 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700680 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700681 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300682 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700683 head->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900685 rcu_read_lock();
686 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
687 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 fq->fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 1;
690
691out_oversize:
692 if (net_ratelimit())
693 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
694 goto out_fail;
695out_oom:
696 if (net_ratelimit())
697 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
698out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900699 rcu_read_lock();
700 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
701 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -1;
703}
704
Patrick McHardy951dbc82006-01-06 23:02:34 -0800705static int ipv6_frag_rcv(struct sk_buff **skbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900707 struct sk_buff *skb = *skbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct net_device *dev = skb->dev;
709 struct frag_hdr *fhdr;
710 struct frag_queue *fq;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700711 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900713 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* Jumbo payload inhibits frag. header */
716 if (hdr->payload_len==0) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900717 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300718 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
719 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -1;
721 }
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700722 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
723 sizeof(struct frag_hdr)))) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900724 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300725 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
726 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -1;
728 }
729
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700730 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700731 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (!(fhdr->frag_off & htons(0xFFF9))) {
734 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700735 skb->transport_header += sizeof(struct frag_hdr);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900736 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700738 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 1;
740 }
741
742 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900743 ip6_evictor(ip6_dst_idev(skb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900745 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
746 ip6_dst_idev(skb->dst))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int ret = -1;
748
749 spin_lock(&fq->lock);
750
Patrick McHardy951dbc82006-01-06 23:02:34 -0800751 ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (fq->last_in == (FIRST_IN|LAST_IN) &&
754 fq->meat == fq->len)
Patrick McHardy951dbc82006-01-06 23:02:34 -0800755 ret = ip6_frag_reasm(fq, skbp, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 spin_unlock(&fq->lock);
758 fq_put(fq, NULL);
759 return ret;
760 }
761
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900762 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kfree_skb(skb);
764 return -1;
765}
766
767static struct inet6_protocol frag_protocol =
768{
769 .handler = ipv6_frag_rcv,
770 .flags = INET6_PROTO_NOPOLICY,
771};
772
773void __init ipv6_frag_init(void)
774{
775 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
776 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
777
778 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
779 (jiffies ^ (jiffies >> 6)));
780
781 init_timer(&ip6_frag_secret_timer);
782 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
783 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
784 add_timer(&ip6_frag_secret_timer);
785}