blob: aef5dd1ebc8a5b5f8922a8840fa0d53ea94d06a0 [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
Brian Haleyab32ea52006-09-22 14:15:41 -070059int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
60int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Brian Haleyab32ea52006-09-22 14:15:41 -070062int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64struct ip6frag_skb_cb
65{
66 struct inet6_skb_parm h;
67 int offset;
68};
69
70#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
71
72
73/*
74 * Equivalent of ipv4 struct ipq
75 */
76
77struct frag_queue
78{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070079 struct inet_frag_queue q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned int csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 __u16 nhoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90/* Hash table. */
91
92#define IP6Q_HASHSZ 64
93
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -080094static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static DEFINE_RWLOCK(ip6_frag_lock);
96static u32 ip6_frag_hash_rnd;
97static LIST_HEAD(ip6_frag_lru_list);
98int ip6_frag_nqueues = 0;
99
Herbert Xuf61944e2007-10-15 01:28:47 -0700100static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
101 struct net_device *dev);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static __inline__ void __fq_unlink(struct frag_queue *fq)
104{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700105 hlist_del(&fq->q.list);
106 list_del(&fq->q.lru_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ip6_frag_nqueues--;
108}
109
110static __inline__ void fq_unlink(struct frag_queue *fq)
111{
112 write_lock(&ip6_frag_lock);
113 __fq_unlink(fq);
114 write_unlock(&ip6_frag_lock);
115}
116
Zach Brownf6596f92006-04-10 16:05:34 -0700117/*
118 * callers should be careful not to use the hash value outside the ipfrag_lock
119 * as doing so could race with ipfrag_hash_rnd being recalculated.
120 */
Al Viroe69a4ad2006-11-14 20:56:00 -0800121static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct in6_addr *daddr)
123{
124 u32 a, b, c;
125
Al Viroe69a4ad2006-11-14 20:56:00 -0800126 a = (__force u32)saddr->s6_addr32[0];
127 b = (__force u32)saddr->s6_addr32[1];
128 c = (__force u32)saddr->s6_addr32[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 a += JHASH_GOLDEN_RATIO;
131 b += JHASH_GOLDEN_RATIO;
132 c += ip6_frag_hash_rnd;
133 __jhash_mix(a, b, c);
134
Al Viroe69a4ad2006-11-14 20:56:00 -0800135 a += (__force u32)saddr->s6_addr32[3];
136 b += (__force u32)daddr->s6_addr32[0];
137 c += (__force u32)daddr->s6_addr32[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 __jhash_mix(a, b, c);
139
Al Viroe69a4ad2006-11-14 20:56:00 -0800140 a += (__force u32)daddr->s6_addr32[2];
141 b += (__force u32)daddr->s6_addr32[3];
142 c += (__force u32)id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 __jhash_mix(a, b, c);
144
145 return c & (IP6Q_HASHSZ - 1);
146}
147
148static struct timer_list ip6_frag_secret_timer;
Brian Haleyab32ea52006-09-22 14:15:41 -0700149int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151static void ip6_frag_secret_rebuild(unsigned long dummy)
152{
153 unsigned long now = jiffies;
154 int i;
155
156 write_lock(&ip6_frag_lock);
157 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
158 for (i = 0; i < IP6Q_HASHSZ; i++) {
159 struct frag_queue *q;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800160 struct hlist_node *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700162 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], q.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned int hval = ip6qhashfn(q->id,
164 &q->saddr,
165 &q->daddr);
166
167 if (hval != i) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700168 hlist_del(&q->q.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* Relink to new hash chain. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700171 hlist_add_head(&q->q.list,
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800172 &ip6_frag_hash[hval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 }
177 write_unlock(&ip6_frag_lock);
178
179 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
180}
181
182atomic_t ip6_frag_mem = ATOMIC_INIT(0);
183
184/* Memory Tracking Functions. */
185static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
186{
187 if (work)
188 *work -= skb->truesize;
189 atomic_sub(skb->truesize, &ip6_frag_mem);
190 kfree_skb(skb);
191}
192
193static inline void frag_free_queue(struct frag_queue *fq, int *work)
194{
195 if (work)
196 *work -= sizeof(struct frag_queue);
197 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
198 kfree(fq);
199}
200
201static inline struct frag_queue *frag_alloc_queue(void)
202{
Ingo Oeser78c784c2006-03-20 23:01:17 -0800203 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if(!fq)
206 return NULL;
207 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
208 return fq;
209}
210
211/* Destruction primitives. */
212
213/* Complete destruction of fq. */
214static void ip6_frag_destroy(struct frag_queue *fq, int *work)
215{
216 struct sk_buff *fp;
217
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700218 BUG_TRAP(fq->q.last_in&COMPLETE);
219 BUG_TRAP(del_timer(&fq->q.timer) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Release all fragment data. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700222 fp = fq->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 while (fp) {
224 struct sk_buff *xp = fp->next;
225
226 frag_kfree_skb(fp, work);
227 fp = xp;
228 }
229
230 frag_free_queue(fq, work);
231}
232
233static __inline__ void fq_put(struct frag_queue *fq, int *work)
234{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700235 if (atomic_dec_and_test(&fq->q.refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ip6_frag_destroy(fq, work);
237}
238
239/* Kill fq entry. It is not destroyed immediately,
240 * because caller (and someone more) holds reference count.
241 */
242static __inline__ void fq_kill(struct frag_queue *fq)
243{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700244 if (del_timer(&fq->q.timer))
245 atomic_dec(&fq->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700247 if (!(fq->q.last_in & COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 fq_unlink(fq);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700249 atomic_dec(&fq->q.refcnt);
250 fq->q.last_in |= COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252}
253
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900254static void ip6_evictor(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct frag_queue *fq;
257 struct list_head *tmp;
258 int work;
259
260 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
261 if (work <= 0)
262 return;
263
264 while(work > 0) {
265 read_lock(&ip6_frag_lock);
266 if (list_empty(&ip6_frag_lru_list)) {
267 read_unlock(&ip6_frag_lock);
268 return;
269 }
270 tmp = ip6_frag_lru_list.next;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700271 fq = list_entry(tmp, struct frag_queue, q.lru_list);
272 atomic_inc(&fq->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 read_unlock(&ip6_frag_lock);
274
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700275 spin_lock(&fq->q.lock);
276 if (!(fq->q.last_in&COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 fq_kill(fq);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700278 spin_unlock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 fq_put(fq, &work);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900281 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283}
284
285static void ip6_frag_expire(unsigned long data)
286{
287 struct frag_queue *fq = (struct frag_queue *) data;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900288 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700290 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700292 if (fq->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto out;
294
295 fq_kill(fq);
296
Eric W. Biederman881d9662007-09-17 11:56:21 -0700297 dev = dev_get_by_index(&init_net, fq->iif);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900298 if (!dev)
299 goto out;
300
301 rcu_read_lock();
302 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
303 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
304 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ingo Oeser78c784c2006-03-20 23:01:17 -0800306 /* Don't send error if the first segment did not arrive. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700307 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
Ingo Oeser78c784c2006-03-20 23:01:17 -0800308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Ingo Oeser78c784c2006-03-20 23:01:17 -0800310 /*
311 But use as source device on which LAST ARRIVED
312 segment was received. And do not use fq->dev
313 pointer directly, device might already disappeared.
314 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700315 fq->q.fragments->dev = dev;
316 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317out:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900318 if (dev)
319 dev_put(dev);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700320 spin_unlock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 fq_put(fq, NULL);
322}
323
324/* Creation primitives. */
325
326
Zach Brownf6596f92006-04-10 16:05:34 -0700327static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct frag_queue *fq;
Zach Brownf6596f92006-04-10 16:05:34 -0700330 unsigned int hash;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800331#ifdef CONFIG_SMP
332 struct hlist_node *n;
333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 write_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700336 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#ifdef CONFIG_SMP
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700338 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900339 if (fq->id == fq_in->id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
341 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700342 atomic_inc(&fq->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 write_unlock(&ip6_frag_lock);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700344 fq_in->q.last_in |= COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 fq_put(fq_in, NULL);
346 return fq;
347 }
348 }
349#endif
350 fq = fq_in;
351
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time))
353 atomic_inc(&fq->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700355 atomic_inc(&fq->q.refcnt);
356 hlist_add_head(&fq->q.list, &ip6_frag_hash[hash]);
357 INIT_LIST_HEAD(&fq->q.lru_list);
358 list_add_tail(&fq->q.lru_list, &ip6_frag_lru_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ip6_frag_nqueues++;
360 write_unlock(&ip6_frag_lock);
361 return fq;
362}
363
364
365static struct frag_queue *
Al Viroe69a4ad2006-11-14 20:56:00 -0800366ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900367 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct frag_queue *fq;
370
371 if ((fq = frag_alloc_queue()) == NULL)
372 goto oom;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 fq->id = id;
375 ipv6_addr_copy(&fq->saddr, src);
376 ipv6_addr_copy(&fq->daddr, dst);
377
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700378 init_timer(&fq->q.timer);
379 fq->q.timer.function = ip6_frag_expire;
380 fq->q.timer.data = (long) fq;
381 spin_lock_init(&fq->q.lock);
382 atomic_set(&fq->q.refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Zach Brownf6596f92006-04-10 16:05:34 -0700384 return ip6_frag_intern(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386oom:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900387 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return NULL;
389}
390
391static __inline__ struct frag_queue *
Al Viroe69a4ad2006-11-14 20:56:00 -0800392fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900393 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct frag_queue *fq;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800396 struct hlist_node *n;
Zach Brownf6596f92006-04-10 16:05:34 -0700397 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 read_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700400 hash = ip6qhashfn(id, src, dst);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700401 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900402 if (fq->id == id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ipv6_addr_equal(src, &fq->saddr) &&
404 ipv6_addr_equal(dst, &fq->daddr)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700405 atomic_inc(&fq->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 read_unlock(&ip6_frag_lock);
407 return fq;
408 }
409 }
410 read_unlock(&ip6_frag_lock);
411
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900412 return ip6_frag_create(id, src, dst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415
Herbert Xuf61944e2007-10-15 01:28:47 -0700416static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct frag_hdr *fhdr, int nhoff)
418{
419 struct sk_buff *prev, *next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700420 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int offset, end;
422
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700423 if (fq->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto err;
425
426 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700427 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
428 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if ((unsigned int)end > IPV6_MAXPLEN) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900431 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
432 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700433 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
434 ((u8 *)&fhdr->frag_off -
435 skb_network_header(skb)));
Herbert Xuf61944e2007-10-15 01:28:47 -0700436 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700439 if (skb->ip_summed == CHECKSUM_COMPLETE) {
440 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900441 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700442 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
443 0));
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* Is this the final fragment? */
447 if (!(fhdr->frag_off & htons(IP6_MF))) {
448 /* If we already have some bits beyond end
449 * or have different end, the segment is corrupted.
450 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700451 if (end < fq->q.len ||
452 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700454 fq->q.last_in |= LAST_IN;
455 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else {
457 /* Check if the fragment is rounded to 8 bytes.
458 * Required by the RFC.
459 */
460 if (end & 0x7) {
461 /* RFC2460 says always send parameter problem in
462 * this case. -DaveM
463 */
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900464 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
465 IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900466 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 offsetof(struct ipv6hdr, payload_len));
Herbert Xuf61944e2007-10-15 01:28:47 -0700468 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700470 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Some bits beyond end -> corruption. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700472 if (fq->q.last_in & LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700474 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
477
478 if (end == offset)
479 goto err;
480
481 /* Point into the IP datagram 'data' part. */
482 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
483 goto err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900484
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700485 if (pskb_trim_rcsum(skb, end - offset))
486 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* Find out which fragments are in front and at the back of us
489 * in the chain of fragments so far. We must know where to put
490 * this fragment, right?
491 */
492 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700493 for(next = fq->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (FRAG6_CB(next)->offset >= offset)
495 break; /* bingo! */
496 prev = next;
497 }
498
499 /* We found where to put this one. Check for overlap with
500 * preceding fragment, and, if needed, align things so that
501 * any overlaps are eliminated.
502 */
503 if (prev) {
504 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
505
506 if (i > 0) {
507 offset += i;
508 if (end <= offset)
509 goto err;
510 if (!pskb_pull(skb, i))
511 goto err;
512 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
513 skb->ip_summed = CHECKSUM_NONE;
514 }
515 }
516
517 /* Look for overlap with succeeding segments.
518 * If we can merge fragments, do it.
519 */
520 while (next && FRAG6_CB(next)->offset < end) {
521 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
522
523 if (i < next->len) {
524 /* Eat head of the next overlapped fragment
525 * and leave the loop. The next ones cannot overlap.
526 */
527 if (!pskb_pull(next, i))
528 goto err;
529 FRAG6_CB(next)->offset += i; /* next fragment */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700530 fq->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (next->ip_summed != CHECKSUM_UNNECESSARY)
532 next->ip_summed = CHECKSUM_NONE;
533 break;
534 } else {
535 struct sk_buff *free_it = next;
536
537 /* Old fragment is completely overridden with
538 * new one drop it.
539 */
540 next = next->next;
541
542 if (prev)
543 prev->next = next;
544 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700545 fq->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700547 fq->q.meat -= free_it->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 frag_kfree_skb(free_it, NULL);
549 }
550 }
551
552 FRAG6_CB(skb)->offset = offset;
553
554 /* Insert this fragment in the chain of fragments. */
555 skb->next = next;
556 if (prev)
557 prev->next = skb;
558 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700559 fq->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Herbert Xuf61944e2007-10-15 01:28:47 -0700561 dev = skb->dev;
562 if (dev) {
563 fq->iif = dev->ifindex;
564 skb->dev = NULL;
565 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700566 fq->q.stamp = skb->tstamp;
567 fq->q.meat += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 atomic_add(skb->truesize, &ip6_frag_mem);
569
570 /* The first fragment.
571 * nhoffset is obtained from the first fragment, of course.
572 */
573 if (offset == 0) {
574 fq->nhoffset = nhoff;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700575 fq->q.last_in |= FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700577
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700578 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
Herbert Xuf61944e2007-10-15 01:28:47 -0700579 return ip6_frag_reasm(fq, prev, dev);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 write_lock(&ip6_frag_lock);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700582 list_move_tail(&fq->q.lru_list, &ip6_frag_lru_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 write_unlock(&ip6_frag_lock);
Herbert Xuf61944e2007-10-15 01:28:47 -0700584 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
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);
Herbert Xuf61944e2007-10-15 01:28:47 -0700589 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/*
593 * Check if this packet is complete.
594 * Returns NULL on failure by any reason, and pointer
595 * to current nexthdr field in reassembled frame.
596 *
597 * It is called with locked fq, and caller must check that
598 * queue is eligible for reassembly i.e. it is not COMPLETE,
599 * the last and the first frames arrived and all the bits are here.
600 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700601static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct net_device *dev)
603{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700604 struct sk_buff *fp, *head = fq->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int payload_len;
606 unsigned int nhoff;
607
608 fq_kill(fq);
609
Herbert Xuf61944e2007-10-15 01:28:47 -0700610 /* Make the one we just received the head. */
611 if (prev) {
612 head = prev->next;
613 fp = skb_clone(head, GFP_ATOMIC);
614
615 if (!fp)
616 goto out_oom;
617
618 fp->next = head->next;
619 prev->next = fp;
620
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700621 skb_morph(head, fq->q.fragments);
622 head->next = fq->q.fragments->next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700623
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700624 kfree_skb(fq->q.fragments);
625 fq->q.fragments = head;
Herbert Xuf61944e2007-10-15 01:28:47 -0700626 }
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 BUG_TRAP(head != NULL);
629 BUG_TRAP(FRAG6_CB(head)->offset == 0);
630
631 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700632 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700633 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700634 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (payload_len > IPV6_MAXPLEN)
636 goto out_oversize;
637
638 /* Head of list must not be cloned. */
639 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
640 goto out_oom;
641
642 /* If the first fragment is fragmented itself, we split
643 * it to two chunks: the first with data and paged part
644 * and the second, holding only fragments. */
645 if (skb_shinfo(head)->frag_list) {
646 struct sk_buff *clone;
647 int i, plen = 0;
648
649 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
650 goto out_oom;
651 clone->next = head->next;
652 head->next = clone;
653 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
654 skb_shinfo(head)->frag_list = NULL;
655 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
656 plen += skb_shinfo(head)->frags[i].size;
657 clone->len = clone->data_len = head->data_len - plen;
658 head->data_len -= clone->len;
659 head->len -= clone->len;
660 clone->csum = 0;
661 clone->ip_summed = head->ip_summed;
662 atomic_add(clone->truesize, &ip6_frag_mem);
663 }
664
665 /* We have to remove fragment header from datagram and to relocate
666 * header in order to calculate ICV correctly. */
667 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700668 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900669 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700671 head->mac_header += sizeof(struct frag_hdr);
672 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300675 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700676 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 atomic_sub(head->truesize, &ip6_frag_mem);
678
679 for (fp=head->next; fp; fp = fp->next) {
680 head->data_len += fp->len;
681 head->len += fp->len;
682 if (head->ip_summed != fp->ip_summed)
683 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700684 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 head->csum = csum_add(head->csum, fp->csum);
686 head->truesize += fp->truesize;
687 atomic_sub(fp->truesize, &ip6_frag_mem);
688 }
689
690 head->next = NULL;
691 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700692 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700693 ipv6_hdr(head)->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800694 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700697 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700698 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300699 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700700 head->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900702 rcu_read_lock();
703 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
704 rcu_read_unlock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700705 fq->q.fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 1;
707
708out_oversize:
709 if (net_ratelimit())
710 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
711 goto out_fail;
712out_oom:
713 if (net_ratelimit())
714 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
715out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900716 rcu_read_lock();
717 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
718 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -1;
720}
721
Patrick McHardy951dbc82006-01-06 23:02:34 -0800722static int ipv6_frag_rcv(struct sk_buff **skbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900724 struct sk_buff *skb = *skbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct frag_hdr *fhdr;
726 struct frag_queue *fq;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700727 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900729 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* Jumbo payload inhibits frag. header */
732 if (hdr->payload_len==0) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900733 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300734 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
735 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return -1;
737 }
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700738 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
739 sizeof(struct frag_hdr)))) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900740 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300741 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
742 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -1;
744 }
745
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700746 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700747 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (!(fhdr->frag_off & htons(0xFFF9))) {
750 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700751 skb->transport_header += sizeof(struct frag_hdr);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900752 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700754 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 1;
756 }
757
758 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900759 ip6_evictor(ip6_dst_idev(skb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900761 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
762 ip6_dst_idev(skb->dst))) != NULL) {
Herbert Xuf61944e2007-10-15 01:28:47 -0700763 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700765 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Herbert Xuf61944e2007-10-15 01:28:47 -0700767 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700769 spin_unlock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 fq_put(fq, NULL);
771 return ret;
772 }
773
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900774 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 kfree_skb(skb);
776 return -1;
777}
778
779static struct inet6_protocol frag_protocol =
780{
781 .handler = ipv6_frag_rcv,
782 .flags = INET6_PROTO_NOPOLICY,
783};
784
785void __init ipv6_frag_init(void)
786{
787 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
788 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
789
790 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
791 (jiffies ^ (jiffies >> 6)));
792
793 init_timer(&ip6_frag_secret_timer);
794 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
795 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
796 add_timer(&ip6_frag_secret_timer);
797}