blob: fa1055b669d128557056dcdff017304722a4e27e [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>
57
Brian Haleyab32ea52006-09-22 14:15:41 -070058int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
59int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Brian Haleyab32ea52006-09-22 14:15:41 -070061int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63struct ip6frag_skb_cb
64{
65 struct inet6_skb_parm h;
66 int offset;
67};
68
69#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
70
71
72/*
73 * Equivalent of ipv4 struct ipq
74 */
75
76struct frag_queue
77{
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -080078 struct hlist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct list_head lru_list; /* lru list member */
80
Al Viroe69a4ad2006-11-14 20:56:00 -080081 __be32 id; /* fragment id */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct in6_addr saddr;
83 struct in6_addr daddr;
84
85 spinlock_t lock;
86 atomic_t refcnt;
87 struct timer_list timer; /* expire timer */
88 struct sk_buff *fragments;
89 int len;
90 int meat;
91 int iif;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -070092 ktime_t stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int csum;
94 __u8 last_in; /* has first/last segment arrived? */
95#define COMPLETE 4
96#define FIRST_IN 2
97#define LAST_IN 1
98 __u16 nhoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101/* Hash table. */
102
103#define IP6Q_HASHSZ 64
104
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800105static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static DEFINE_RWLOCK(ip6_frag_lock);
107static u32 ip6_frag_hash_rnd;
108static LIST_HEAD(ip6_frag_lru_list);
109int ip6_frag_nqueues = 0;
110
Herbert Xuf61944e2007-10-15 01:28:47 -0700111static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
112 struct net_device *dev);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static __inline__ void __fq_unlink(struct frag_queue *fq)
115{
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800116 hlist_del(&fq->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 list_del(&fq->lru_list);
118 ip6_frag_nqueues--;
119}
120
121static __inline__ void fq_unlink(struct frag_queue *fq)
122{
123 write_lock(&ip6_frag_lock);
124 __fq_unlink(fq);
125 write_unlock(&ip6_frag_lock);
126}
127
Zach Brownf6596f92006-04-10 16:05:34 -0700128/*
129 * callers should be careful not to use the hash value outside the ipfrag_lock
130 * as doing so could race with ipfrag_hash_rnd being recalculated.
131 */
Al Viroe69a4ad2006-11-14 20:56:00 -0800132static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct in6_addr *daddr)
134{
135 u32 a, b, c;
136
Al Viroe69a4ad2006-11-14 20:56:00 -0800137 a = (__force u32)saddr->s6_addr32[0];
138 b = (__force u32)saddr->s6_addr32[1];
139 c = (__force u32)saddr->s6_addr32[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 a += JHASH_GOLDEN_RATIO;
142 b += JHASH_GOLDEN_RATIO;
143 c += ip6_frag_hash_rnd;
144 __jhash_mix(a, b, c);
145
Al Viroe69a4ad2006-11-14 20:56:00 -0800146 a += (__force u32)saddr->s6_addr32[3];
147 b += (__force u32)daddr->s6_addr32[0];
148 c += (__force u32)daddr->s6_addr32[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 __jhash_mix(a, b, c);
150
Al Viroe69a4ad2006-11-14 20:56:00 -0800151 a += (__force u32)daddr->s6_addr32[2];
152 b += (__force u32)daddr->s6_addr32[3];
153 c += (__force u32)id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 __jhash_mix(a, b, c);
155
156 return c & (IP6Q_HASHSZ - 1);
157}
158
159static struct timer_list ip6_frag_secret_timer;
Brian Haleyab32ea52006-09-22 14:15:41 -0700160int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162static void ip6_frag_secret_rebuild(unsigned long dummy)
163{
164 unsigned long now = jiffies;
165 int i;
166
167 write_lock(&ip6_frag_lock);
168 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
169 for (i = 0; i < IP6Q_HASHSZ; i++) {
170 struct frag_queue *q;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800171 struct hlist_node *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800173 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int hval = ip6qhashfn(q->id,
175 &q->saddr,
176 &q->daddr);
177
178 if (hval != i) {
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800179 hlist_del(&q->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* Relink to new hash chain. */
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800182 hlist_add_head(&q->list,
183 &ip6_frag_hash[hval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 }
188 write_unlock(&ip6_frag_lock);
189
190 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
191}
192
193atomic_t ip6_frag_mem = ATOMIC_INIT(0);
194
195/* Memory Tracking Functions. */
196static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
197{
198 if (work)
199 *work -= skb->truesize;
200 atomic_sub(skb->truesize, &ip6_frag_mem);
201 kfree_skb(skb);
202}
203
204static inline void frag_free_queue(struct frag_queue *fq, int *work)
205{
206 if (work)
207 *work -= sizeof(struct frag_queue);
208 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
209 kfree(fq);
210}
211
212static inline struct frag_queue *frag_alloc_queue(void)
213{
Ingo Oeser78c784c2006-03-20 23:01:17 -0800214 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if(!fq)
217 return NULL;
218 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
219 return fq;
220}
221
222/* Destruction primitives. */
223
224/* Complete destruction of fq. */
225static void ip6_frag_destroy(struct frag_queue *fq, int *work)
226{
227 struct sk_buff *fp;
228
229 BUG_TRAP(fq->last_in&COMPLETE);
230 BUG_TRAP(del_timer(&fq->timer) == 0);
231
232 /* Release all fragment data. */
233 fp = fq->fragments;
234 while (fp) {
235 struct sk_buff *xp = fp->next;
236
237 frag_kfree_skb(fp, work);
238 fp = xp;
239 }
240
241 frag_free_queue(fq, work);
242}
243
244static __inline__ void fq_put(struct frag_queue *fq, int *work)
245{
246 if (atomic_dec_and_test(&fq->refcnt))
247 ip6_frag_destroy(fq, work);
248}
249
250/* Kill fq entry. It is not destroyed immediately,
251 * because caller (and someone more) holds reference count.
252 */
253static __inline__ void fq_kill(struct frag_queue *fq)
254{
255 if (del_timer(&fq->timer))
256 atomic_dec(&fq->refcnt);
257
258 if (!(fq->last_in & COMPLETE)) {
259 fq_unlink(fq);
260 atomic_dec(&fq->refcnt);
261 fq->last_in |= COMPLETE;
262 }
263}
264
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900265static void ip6_evictor(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct frag_queue *fq;
268 struct list_head *tmp;
269 int work;
270
271 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
272 if (work <= 0)
273 return;
274
275 while(work > 0) {
276 read_lock(&ip6_frag_lock);
277 if (list_empty(&ip6_frag_lru_list)) {
278 read_unlock(&ip6_frag_lock);
279 return;
280 }
281 tmp = ip6_frag_lru_list.next;
282 fq = list_entry(tmp, struct frag_queue, lru_list);
283 atomic_inc(&fq->refcnt);
284 read_unlock(&ip6_frag_lock);
285
286 spin_lock(&fq->lock);
287 if (!(fq->last_in&COMPLETE))
288 fq_kill(fq);
289 spin_unlock(&fq->lock);
290
291 fq_put(fq, &work);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900292 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294}
295
296static void ip6_frag_expire(unsigned long data)
297{
298 struct frag_queue *fq = (struct frag_queue *) data;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900299 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 spin_lock(&fq->lock);
302
303 if (fq->last_in & COMPLETE)
304 goto out;
305
306 fq_kill(fq);
307
Eric W. Biederman881d9662007-09-17 11:56:21 -0700308 dev = dev_get_by_index(&init_net, fq->iif);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900309 if (!dev)
310 goto out;
311
312 rcu_read_lock();
313 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
314 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
315 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Ingo Oeser78c784c2006-03-20 23:01:17 -0800317 /* Don't send error if the first segment did not arrive. */
318 if (!(fq->last_in&FIRST_IN) || !fq->fragments)
319 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Ingo Oeser78c784c2006-03-20 23:01:17 -0800321 /*
322 But use as source device on which LAST ARRIVED
323 segment was received. And do not use fq->dev
324 pointer directly, device might already disappeared.
325 */
326 fq->fragments->dev = dev;
327 icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328out:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900329 if (dev)
330 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_unlock(&fq->lock);
332 fq_put(fq, NULL);
333}
334
335/* Creation primitives. */
336
337
Zach Brownf6596f92006-04-10 16:05:34 -0700338static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct frag_queue *fq;
Zach Brownf6596f92006-04-10 16:05:34 -0700341 unsigned int hash;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800342#ifdef CONFIG_SMP
343 struct hlist_node *n;
344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 write_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700347 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#ifdef CONFIG_SMP
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800349 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900350 if (fq->id == fq_in->id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
352 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
353 atomic_inc(&fq->refcnt);
354 write_unlock(&ip6_frag_lock);
355 fq_in->last_in |= COMPLETE;
356 fq_put(fq_in, NULL);
357 return fq;
358 }
359 }
360#endif
361 fq = fq_in;
362
363 if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
364 atomic_inc(&fq->refcnt);
365
366 atomic_inc(&fq->refcnt);
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800367 hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 INIT_LIST_HEAD(&fq->lru_list);
369 list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
370 ip6_frag_nqueues++;
371 write_unlock(&ip6_frag_lock);
372 return fq;
373}
374
375
376static struct frag_queue *
Al Viroe69a4ad2006-11-14 20:56:00 -0800377ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900378 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct frag_queue *fq;
381
382 if ((fq = frag_alloc_queue()) == NULL)
383 goto oom;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 fq->id = id;
386 ipv6_addr_copy(&fq->saddr, src);
387 ipv6_addr_copy(&fq->daddr, dst);
388
389 init_timer(&fq->timer);
390 fq->timer.function = ip6_frag_expire;
391 fq->timer.data = (long) fq;
392 spin_lock_init(&fq->lock);
393 atomic_set(&fq->refcnt, 1);
394
Zach Brownf6596f92006-04-10 16:05:34 -0700395 return ip6_frag_intern(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397oom:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900398 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return NULL;
400}
401
402static __inline__ struct frag_queue *
Al Viroe69a4ad2006-11-14 20:56:00 -0800403fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900404 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct frag_queue *fq;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800407 struct hlist_node *n;
Zach Brownf6596f92006-04-10 16:05:34 -0700408 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 read_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700411 hash = ip6qhashfn(id, src, dst);
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800412 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900413 if (fq->id == id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 ipv6_addr_equal(src, &fq->saddr) &&
415 ipv6_addr_equal(dst, &fq->daddr)) {
416 atomic_inc(&fq->refcnt);
417 read_unlock(&ip6_frag_lock);
418 return fq;
419 }
420 }
421 read_unlock(&ip6_frag_lock);
422
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900423 return ip6_frag_create(id, src, dst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426
Herbert Xuf61944e2007-10-15 01:28:47 -0700427static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct frag_hdr *fhdr, int nhoff)
429{
430 struct sk_buff *prev, *next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700431 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int offset, end;
433
434 if (fq->last_in & COMPLETE)
435 goto err;
436
437 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700438 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
439 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if ((unsigned int)end > IPV6_MAXPLEN) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900442 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
443 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700444 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
445 ((u8 *)&fhdr->frag_off -
446 skb_network_header(skb)));
Herbert Xuf61944e2007-10-15 01:28:47 -0700447 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700450 if (skb->ip_summed == CHECKSUM_COMPLETE) {
451 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900452 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700453 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
454 0));
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* Is this the final fragment? */
458 if (!(fhdr->frag_off & htons(IP6_MF))) {
459 /* If we already have some bits beyond end
460 * or have different end, the segment is corrupted.
461 */
462 if (end < fq->len ||
463 ((fq->last_in & LAST_IN) && end != fq->len))
464 goto err;
465 fq->last_in |= LAST_IN;
466 fq->len = end;
467 } else {
468 /* Check if the fragment is rounded to 8 bytes.
469 * Required by the RFC.
470 */
471 if (end & 0x7) {
472 /* RFC2460 says always send parameter problem in
473 * this case. -DaveM
474 */
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900475 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
476 IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900477 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 offsetof(struct ipv6hdr, payload_len));
Herbert Xuf61944e2007-10-15 01:28:47 -0700479 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 if (end > fq->len) {
482 /* Some bits beyond end -> corruption. */
483 if (fq->last_in & LAST_IN)
484 goto err;
485 fq->len = end;
486 }
487 }
488
489 if (end == offset)
490 goto err;
491
492 /* Point into the IP datagram 'data' part. */
493 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
494 goto err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900495
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700496 if (pskb_trim_rcsum(skb, end - offset))
497 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Find out which fragments are in front and at the back of us
500 * in the chain of fragments so far. We must know where to put
501 * this fragment, right?
502 */
503 prev = NULL;
504 for(next = fq->fragments; next != NULL; next = next->next) {
505 if (FRAG6_CB(next)->offset >= offset)
506 break; /* bingo! */
507 prev = next;
508 }
509
510 /* We found where to put this one. Check for overlap with
511 * preceding fragment, and, if needed, align things so that
512 * any overlaps are eliminated.
513 */
514 if (prev) {
515 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
516
517 if (i > 0) {
518 offset += i;
519 if (end <= offset)
520 goto err;
521 if (!pskb_pull(skb, i))
522 goto err;
523 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
524 skb->ip_summed = CHECKSUM_NONE;
525 }
526 }
527
528 /* Look for overlap with succeeding segments.
529 * If we can merge fragments, do it.
530 */
531 while (next && FRAG6_CB(next)->offset < end) {
532 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
533
534 if (i < next->len) {
535 /* Eat head of the next overlapped fragment
536 * and leave the loop. The next ones cannot overlap.
537 */
538 if (!pskb_pull(next, i))
539 goto err;
540 FRAG6_CB(next)->offset += i; /* next fragment */
541 fq->meat -= i;
542 if (next->ip_summed != CHECKSUM_UNNECESSARY)
543 next->ip_summed = CHECKSUM_NONE;
544 break;
545 } else {
546 struct sk_buff *free_it = next;
547
548 /* Old fragment is completely overridden with
549 * new one drop it.
550 */
551 next = next->next;
552
553 if (prev)
554 prev->next = next;
555 else
556 fq->fragments = next;
557
558 fq->meat -= free_it->len;
559 frag_kfree_skb(free_it, NULL);
560 }
561 }
562
563 FRAG6_CB(skb)->offset = offset;
564
565 /* Insert this fragment in the chain of fragments. */
566 skb->next = next;
567 if (prev)
568 prev->next = skb;
569 else
570 fq->fragments = skb;
571
Herbert Xuf61944e2007-10-15 01:28:47 -0700572 dev = skb->dev;
573 if (dev) {
574 fq->iif = dev->ifindex;
575 skb->dev = NULL;
576 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700577 fq->stamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 fq->meat += skb->len;
579 atomic_add(skb->truesize, &ip6_frag_mem);
580
581 /* The first fragment.
582 * nhoffset is obtained from the first fragment, of course.
583 */
584 if (offset == 0) {
585 fq->nhoffset = nhoff;
586 fq->last_in |= FIRST_IN;
587 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700588
589 if (fq->last_in == (FIRST_IN | LAST_IN) && fq->meat == fq->len)
590 return ip6_frag_reasm(fq, prev, dev);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 write_lock(&ip6_frag_lock);
593 list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
594 write_unlock(&ip6_frag_lock);
Herbert Xuf61944e2007-10-15 01:28:47 -0700595 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597err:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900598 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree_skb(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700600 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
604 * Check if this packet is complete.
605 * Returns NULL on failure by any reason, and pointer
606 * to current nexthdr field in reassembled frame.
607 *
608 * It is called with locked fq, and caller must check that
609 * queue is eligible for reassembly i.e. it is not COMPLETE,
610 * the last and the first frames arrived and all the bits are here.
611 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700612static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct net_device *dev)
614{
615 struct sk_buff *fp, *head = fq->fragments;
616 int payload_len;
617 unsigned int nhoff;
618
619 fq_kill(fq);
620
Herbert Xuf61944e2007-10-15 01:28:47 -0700621 /* Make the one we just received the head. */
622 if (prev) {
623 head = prev->next;
624 fp = skb_clone(head, GFP_ATOMIC);
625
626 if (!fp)
627 goto out_oom;
628
629 fp->next = head->next;
630 prev->next = fp;
631
632 skb_morph(head, fq->fragments);
633 head->next = fq->fragments->next;
634
635 kfree_skb(fq->fragments);
636 fq->fragments = head;
637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 BUG_TRAP(head != NULL);
640 BUG_TRAP(FRAG6_CB(head)->offset == 0);
641
642 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700643 payload_len = ((head->data - skb_network_header(head)) -
644 sizeof(struct ipv6hdr) + fq->len -
645 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (payload_len > IPV6_MAXPLEN)
647 goto out_oversize;
648
649 /* Head of list must not be cloned. */
650 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
651 goto out_oom;
652
653 /* If the first fragment is fragmented itself, we split
654 * it to two chunks: the first with data and paged part
655 * and the second, holding only fragments. */
656 if (skb_shinfo(head)->frag_list) {
657 struct sk_buff *clone;
658 int i, plen = 0;
659
660 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
661 goto out_oom;
662 clone->next = head->next;
663 head->next = clone;
664 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
665 skb_shinfo(head)->frag_list = NULL;
666 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
667 plen += skb_shinfo(head)->frags[i].size;
668 clone->len = clone->data_len = head->data_len - plen;
669 head->data_len -= clone->len;
670 head->len -= clone->len;
671 clone->csum = 0;
672 clone->ip_summed = head->ip_summed;
673 atomic_add(clone->truesize, &ip6_frag_mem);
674 }
675
676 /* We have to remove fragment header from datagram and to relocate
677 * header in order to calculate ICV correctly. */
678 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700679 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900680 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700682 head->mac_header += sizeof(struct frag_hdr);
683 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300686 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700687 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 atomic_sub(head->truesize, &ip6_frag_mem);
689
690 for (fp=head->next; fp; fp = fp->next) {
691 head->data_len += fp->len;
692 head->len += fp->len;
693 if (head->ip_summed != fp->ip_summed)
694 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700695 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 head->csum = csum_add(head->csum, fp->csum);
697 head->truesize += fp->truesize;
698 atomic_sub(fp->truesize, &ip6_frag_mem);
699 }
700
701 head->next = NULL;
702 head->dev = dev;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700703 head->tstamp = fq->stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700704 ipv6_hdr(head)->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800705 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700708 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700709 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300710 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700711 head->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900713 rcu_read_lock();
714 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
715 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 fq->fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 1;
718
719out_oversize:
720 if (net_ratelimit())
721 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
722 goto out_fail;
723out_oom:
724 if (net_ratelimit())
725 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
726out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900727 rcu_read_lock();
728 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
729 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -1;
731}
732
Patrick McHardy951dbc82006-01-06 23:02:34 -0800733static int ipv6_frag_rcv(struct sk_buff **skbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900735 struct sk_buff *skb = *skbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct frag_hdr *fhdr;
737 struct frag_queue *fq;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700738 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900740 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /* Jumbo payload inhibits frag. header */
743 if (hdr->payload_len==0) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900744 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300745 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
746 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return -1;
748 }
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700749 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
750 sizeof(struct frag_hdr)))) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900751 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300752 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
753 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -1;
755 }
756
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700757 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700758 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (!(fhdr->frag_off & htons(0xFFF9))) {
761 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700762 skb->transport_header += sizeof(struct frag_hdr);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900763 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700765 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 1;
767 }
768
769 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900770 ip6_evictor(ip6_dst_idev(skb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900772 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
773 ip6_dst_idev(skb->dst))) != NULL) {
Herbert Xuf61944e2007-10-15 01:28:47 -0700774 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 spin_lock(&fq->lock);
777
Herbert Xuf61944e2007-10-15 01:28:47 -0700778 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 spin_unlock(&fq->lock);
781 fq_put(fq, NULL);
782 return ret;
783 }
784
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900785 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 kfree_skb(skb);
787 return -1;
788}
789
790static struct inet6_protocol frag_protocol =
791{
792 .handler = ipv6_frag_rcv,
793 .flags = INET6_PROTO_NOPOLICY,
794};
795
796void __init ipv6_frag_init(void)
797{
798 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
799 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
800
801 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
802 (jiffies ^ (jiffies >> 6)));
803
804 init_timer(&ip6_frag_secret_timer);
805 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
806 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
807 add_timer(&ip6_frag_secret_timer);
808}