blob: 1815ff0cf6289296c25ad5a224e7c6300ae436a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 fragment reassembly
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
9 *
10 * Based on: net/ipv4/ip_fragment.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090018/*
19 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Andi Kleen Make it work with multiple hosts.
21 * More RFC compliance.
22 *
23 * Horst von Brand Add missing #include <linux/string.h>
24 * Alexey Kuznetsov SMP races, threading, cleanup.
25 * Patrick McHardy LRU queue of frag heads for evictor.
26 * Mitsuru KANDA @USAGI Register inet6_protocol{}.
27 * David Stevens and
28 * YOSHIFUJI,H. @USAGI Always remove fragment header to
29 * calculate ICV correctly.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/types.h>
33#include <linux/string.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/jiffies.h>
37#include <linux/net.h>
38#include <linux/list.h>
39#include <linux/netdevice.h>
40#include <linux/in6.h>
41#include <linux/ipv6.h>
42#include <linux/icmpv6.h>
43#include <linux/random.h>
44#include <linux/jhash.h>
Herbert Xuf61944e2007-10-15 01:28:47 -070045#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/sock.h>
48#include <net/snmp.h>
49
50#include <net/ipv6.h>
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +090051#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/protocol.h>
53#include <net/transp_v6.h>
54#include <net/rawv6.h>
55#include <net/ndisc.h>
56#include <net/addrconf.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070057#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct ip6frag_skb_cb
60{
61 struct inet6_skb_parm h;
62 int offset;
63};
64
65#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
66
67
68/*
69 * Equivalent of ipv4 struct ipq
70 */
71
72struct frag_queue
73{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070074 struct inet_frag_queue q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Al Viroe69a4ad2006-11-14 20:56:00 -080076 __be32 id; /* fragment id */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct in6_addr saddr;
78 struct in6_addr daddr;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned int csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 __u16 nhoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070085static struct inet_frags ip6_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070087int ip6_frag_nqueues(void)
88{
89 return ip6_frags.nqueues;
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070092int ip6_frag_mem(void)
93{
94 return atomic_read(&ip6_frags.mem);
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Herbert Xuf61944e2007-10-15 01:28:47 -070097static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
98 struct net_device *dev);
99
Zach Brownf6596f92006-04-10 16:05:34 -0700100/*
101 * callers should be careful not to use the hash value outside the ipfrag_lock
102 * as doing so could race with ipfrag_hash_rnd being recalculated.
103 */
Al Viroe69a4ad2006-11-14 20:56:00 -0800104static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct in6_addr *daddr)
106{
107 u32 a, b, c;
108
Al Viroe69a4ad2006-11-14 20:56:00 -0800109 a = (__force u32)saddr->s6_addr32[0];
110 b = (__force u32)saddr->s6_addr32[1];
111 c = (__force u32)saddr->s6_addr32[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 a += JHASH_GOLDEN_RATIO;
114 b += JHASH_GOLDEN_RATIO;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700115 c += ip6_frags.rnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 __jhash_mix(a, b, c);
117
Al Viroe69a4ad2006-11-14 20:56:00 -0800118 a += (__force u32)saddr->s6_addr32[3];
119 b += (__force u32)daddr->s6_addr32[0];
120 c += (__force u32)daddr->s6_addr32[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 __jhash_mix(a, b, c);
122
Al Viroe69a4ad2006-11-14 20:56:00 -0800123 a += (__force u32)daddr->s6_addr32[2];
124 b += (__force u32)daddr->s6_addr32[3];
125 c += (__force u32)id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 __jhash_mix(a, b, c);
127
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700128 return c & (INETFRAGS_HASHSZ - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700131static unsigned int ip6_hashfn(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700133 struct frag_queue *fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700135 fq = container_of(q, struct frag_queue, q);
136 return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700139int ip6_frag_match(struct inet_frag_queue *q, void *a)
140{
141 struct frag_queue *fq;
142 struct ip6_create_arg *arg = a;
143
144 fq = container_of(q, struct frag_queue, q);
145 return (fq->id == arg->id &&
146 ipv6_addr_equal(&fq->saddr, arg->src) &&
147 ipv6_addr_equal(&fq->daddr, arg->dst));
148}
149EXPORT_SYMBOL(ip6_frag_match);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Memory Tracking Functions. */
152static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
153{
154 if (work)
155 *work -= skb->truesize;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700156 atomic_sub(skb->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 kfree_skb(skb);
158}
159
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700160void ip6_frag_init(struct inet_frag_queue *q, void *a)
161{
162 struct frag_queue *fq = container_of(q, struct frag_queue, q);
163 struct ip6_create_arg *arg = a;
164
165 fq->id = arg->id;
166 ipv6_addr_copy(&fq->saddr, arg->src);
167 ipv6_addr_copy(&fq->daddr, arg->dst);
168}
169EXPORT_SYMBOL(ip6_frag_init);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/* Destruction primitives. */
172
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700173static __inline__ void fq_put(struct frag_queue *fq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700175 inet_frag_put(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/* Kill fq entry. It is not destroyed immediately,
179 * because caller (and someone more) holds reference count.
180 */
181static __inline__ void fq_kill(struct frag_queue *fq)
182{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700183 inet_frag_kill(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900186static void ip6_evictor(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700188 int evicted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700190 evicted = inet_frag_evictor(&ip6_frags);
191 if (evicted)
192 IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static void ip6_frag_expire(unsigned long data)
196{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700197 struct frag_queue *fq;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900198 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Pavel Emelyanove521db92007-10-17 19:45:23 -0700200 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
201
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700202 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700204 if (fq->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out;
206
207 fq_kill(fq);
208
Eric W. Biederman881d9662007-09-17 11:56:21 -0700209 dev = dev_get_by_index(&init_net, fq->iif);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900210 if (!dev)
211 goto out;
212
213 rcu_read_lock();
214 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
215 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
216 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Ingo Oeser78c784c2006-03-20 23:01:17 -0800218 /* Don't send error if the first segment did not arrive. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700219 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
Ingo Oeser78c784c2006-03-20 23:01:17 -0800220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Ingo Oeser78c784c2006-03-20 23:01:17 -0800222 /*
223 But use as source device on which LAST ARRIVED
224 segment was received. And do not use fq->dev
225 pointer directly, device might already disappeared.
226 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700227 fq->q.fragments->dev = dev;
228 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900230 if (dev)
231 dev_put(dev);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700232 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700233 fq_put(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700236static __inline__ struct frag_queue *
237fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
238 struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700240 struct inet_frag_queue *q;
241 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700242 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700244 arg.id = id;
245 arg.src = src;
246 arg.dst = dst;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700247 hash = ip6qhashfn(id, src, dst);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700248
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700249 q = inet_frag_find(&ip6_frags, &arg, hash);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700250 if (q == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto oom;
252
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700253 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255oom:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900256 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return NULL;
258}
259
Herbert Xuf61944e2007-10-15 01:28:47 -0700260static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct frag_hdr *fhdr, int nhoff)
262{
263 struct sk_buff *prev, *next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700264 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int offset, end;
266
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700267 if (fq->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto err;
269
270 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700271 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
272 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if ((unsigned int)end > IPV6_MAXPLEN) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900275 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
276 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700277 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
278 ((u8 *)&fhdr->frag_off -
279 skb_network_header(skb)));
Herbert Xuf61944e2007-10-15 01:28:47 -0700280 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700283 if (skb->ip_summed == CHECKSUM_COMPLETE) {
284 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900285 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700286 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
287 0));
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Is this the final fragment? */
291 if (!(fhdr->frag_off & htons(IP6_MF))) {
292 /* If we already have some bits beyond end
293 * or have different end, the segment is corrupted.
294 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700295 if (end < fq->q.len ||
296 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700298 fq->q.last_in |= LAST_IN;
299 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } else {
301 /* Check if the fragment is rounded to 8 bytes.
302 * Required by the RFC.
303 */
304 if (end & 0x7) {
305 /* RFC2460 says always send parameter problem in
306 * this case. -DaveM
307 */
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900308 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
309 IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900310 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 offsetof(struct ipv6hdr, payload_len));
Herbert Xuf61944e2007-10-15 01:28:47 -0700312 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700314 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Some bits beyond end -> corruption. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700316 if (fq->q.last_in & LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700318 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 }
321
322 if (end == offset)
323 goto err;
324
325 /* Point into the IP datagram 'data' part. */
326 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
327 goto err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900328
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700329 if (pskb_trim_rcsum(skb, end - offset))
330 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Find out which fragments are in front and at the back of us
333 * in the chain of fragments so far. We must know where to put
334 * this fragment, right?
335 */
336 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700337 for(next = fq->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (FRAG6_CB(next)->offset >= offset)
339 break; /* bingo! */
340 prev = next;
341 }
342
343 /* We found where to put this one. Check for overlap with
344 * preceding fragment, and, if needed, align things so that
345 * any overlaps are eliminated.
346 */
347 if (prev) {
348 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
349
350 if (i > 0) {
351 offset += i;
352 if (end <= offset)
353 goto err;
354 if (!pskb_pull(skb, i))
355 goto err;
356 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
357 skb->ip_summed = CHECKSUM_NONE;
358 }
359 }
360
361 /* Look for overlap with succeeding segments.
362 * If we can merge fragments, do it.
363 */
364 while (next && FRAG6_CB(next)->offset < end) {
365 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
366
367 if (i < next->len) {
368 /* Eat head of the next overlapped fragment
369 * and leave the loop. The next ones cannot overlap.
370 */
371 if (!pskb_pull(next, i))
372 goto err;
373 FRAG6_CB(next)->offset += i; /* next fragment */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700374 fq->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (next->ip_summed != CHECKSUM_UNNECESSARY)
376 next->ip_summed = CHECKSUM_NONE;
377 break;
378 } else {
379 struct sk_buff *free_it = next;
380
381 /* Old fragment is completely overridden with
382 * new one drop it.
383 */
384 next = next->next;
385
386 if (prev)
387 prev->next = next;
388 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700389 fq->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700391 fq->q.meat -= free_it->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 frag_kfree_skb(free_it, NULL);
393 }
394 }
395
396 FRAG6_CB(skb)->offset = offset;
397
398 /* Insert this fragment in the chain of fragments. */
399 skb->next = next;
400 if (prev)
401 prev->next = skb;
402 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700403 fq->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Herbert Xuf61944e2007-10-15 01:28:47 -0700405 dev = skb->dev;
406 if (dev) {
407 fq->iif = dev->ifindex;
408 skb->dev = NULL;
409 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700410 fq->q.stamp = skb->tstamp;
411 fq->q.meat += skb->len;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700412 atomic_add(skb->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* The first fragment.
415 * nhoffset is obtained from the first fragment, of course.
416 */
417 if (offset == 0) {
418 fq->nhoffset = nhoff;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700419 fq->q.last_in |= FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700421
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700422 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
Herbert Xuf61944e2007-10-15 01:28:47 -0700423 return ip6_frag_reasm(fq, prev, dev);
424
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700425 write_lock(&ip6_frags.lock);
426 list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
427 write_unlock(&ip6_frags.lock);
Herbert Xuf61944e2007-10-15 01:28:47 -0700428 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430err:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900431 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 kfree_skb(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700433 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * Check if this packet is complete.
438 * Returns NULL on failure by any reason, and pointer
439 * to current nexthdr field in reassembled frame.
440 *
441 * It is called with locked fq, and caller must check that
442 * queue is eligible for reassembly i.e. it is not COMPLETE,
443 * the last and the first frames arrived and all the bits are here.
444 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700445static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct net_device *dev)
447{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700448 struct sk_buff *fp, *head = fq->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int payload_len;
450 unsigned int nhoff;
451
452 fq_kill(fq);
453
Herbert Xuf61944e2007-10-15 01:28:47 -0700454 /* Make the one we just received the head. */
455 if (prev) {
456 head = prev->next;
457 fp = skb_clone(head, GFP_ATOMIC);
458
459 if (!fp)
460 goto out_oom;
461
462 fp->next = head->next;
463 prev->next = fp;
464
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700465 skb_morph(head, fq->q.fragments);
466 head->next = fq->q.fragments->next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700467
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700468 kfree_skb(fq->q.fragments);
469 fq->q.fragments = head;
Herbert Xuf61944e2007-10-15 01:28:47 -0700470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 BUG_TRAP(head != NULL);
473 BUG_TRAP(FRAG6_CB(head)->offset == 0);
474
475 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700476 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700477 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700478 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (payload_len > IPV6_MAXPLEN)
480 goto out_oversize;
481
482 /* Head of list must not be cloned. */
483 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
484 goto out_oom;
485
486 /* If the first fragment is fragmented itself, we split
487 * it to two chunks: the first with data and paged part
488 * and the second, holding only fragments. */
489 if (skb_shinfo(head)->frag_list) {
490 struct sk_buff *clone;
491 int i, plen = 0;
492
493 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
494 goto out_oom;
495 clone->next = head->next;
496 head->next = clone;
497 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
498 skb_shinfo(head)->frag_list = NULL;
499 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
500 plen += skb_shinfo(head)->frags[i].size;
501 clone->len = clone->data_len = head->data_len - plen;
502 head->data_len -= clone->len;
503 head->len -= clone->len;
504 clone->csum = 0;
505 clone->ip_summed = head->ip_summed;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700506 atomic_add(clone->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 /* We have to remove fragment header from datagram and to relocate
510 * header in order to calculate ICV correctly. */
511 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700512 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900513 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700515 head->mac_header += sizeof(struct frag_hdr);
516 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300519 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700520 skb_push(head, head->data - skb_network_header(head));
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700521 atomic_sub(head->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 for (fp=head->next; fp; fp = fp->next) {
524 head->data_len += fp->len;
525 head->len += fp->len;
526 if (head->ip_summed != fp->ip_summed)
527 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700528 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 head->csum = csum_add(head->csum, fp->csum);
530 head->truesize += fp->truesize;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700531 atomic_sub(fp->truesize, &ip6_frags.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
534 head->next = NULL;
535 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700536 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700537 ipv6_hdr(head)->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800538 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700541 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700542 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300543 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700544 head->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900546 rcu_read_lock();
547 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
548 rcu_read_unlock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700549 fq->q.fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 1;
551
552out_oversize:
553 if (net_ratelimit())
554 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
555 goto out_fail;
556out_oom:
557 if (net_ratelimit())
558 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
559out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900560 rcu_read_lock();
561 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
562 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -1;
564}
565
Herbert Xue5bbef22007-10-15 12:50:28 -0700566static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct frag_hdr *fhdr;
569 struct frag_queue *fq;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700570 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900572 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* Jumbo payload inhibits frag. header */
575 if (hdr->payload_len==0) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900576 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300577 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
578 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -1;
580 }
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700581 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
582 sizeof(struct frag_hdr)))) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900583 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300584 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
585 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -1;
587 }
588
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700589 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700590 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (!(fhdr->frag_off & htons(0xFFF9))) {
593 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700594 skb->transport_header += sizeof(struct frag_hdr);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900595 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700597 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 1;
599 }
600
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800601 if (atomic_read(&ip6_frags.mem) > init_net.ipv6.sysctl.frags.high_thresh)
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900602 ip6_evictor(ip6_dst_idev(skb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900604 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
605 ip6_dst_idev(skb->dst))) != NULL) {
Herbert Xuf61944e2007-10-15 01:28:47 -0700606 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700608 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Herbert Xuf61944e2007-10-15 01:28:47 -0700610 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700612 spin_unlock(&fq->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700613 fq_put(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return ret;
615 }
616
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900617 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 kfree_skb(skb);
619 return -1;
620}
621
622static struct inet6_protocol frag_protocol =
623{
624 .handler = ipv6_frag_rcv,
625 .flags = INET6_PROTO_NOPOLICY,
626};
627
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800628#ifdef CONFIG_SYSCTL
629static struct ctl_table ip6_frags_ctl_table[] = {
630 {
631 .ctl_name = NET_IPV6_IP6FRAG_HIGH_THRESH,
632 .procname = "ip6frag_high_thresh",
633 .data = &init_net.ipv6.sysctl.frags.high_thresh,
634 .maxlen = sizeof(int),
635 .mode = 0644,
636 .proc_handler = &proc_dointvec
637 },
638 {
639 .ctl_name = NET_IPV6_IP6FRAG_LOW_THRESH,
640 .procname = "ip6frag_low_thresh",
641 .data = &init_net.ipv6.sysctl.frags.low_thresh,
642 .maxlen = sizeof(int),
643 .mode = 0644,
644 .proc_handler = &proc_dointvec
645 },
646 {
647 .ctl_name = NET_IPV6_IP6FRAG_TIME,
648 .procname = "ip6frag_time",
649 .data = &init_net.ipv6.sysctl.frags.timeout,
650 .maxlen = sizeof(int),
651 .mode = 0644,
652 .proc_handler = &proc_dointvec_jiffies,
653 .strategy = &sysctl_jiffies,
654 },
655 {
656 .ctl_name = NET_IPV6_IP6FRAG_SECRET_INTERVAL,
657 .procname = "ip6frag_secret_interval",
658 .data = &init_net.ipv6.sysctl.frags.secret_interval,
659 .maxlen = sizeof(int),
660 .mode = 0644,
661 .proc_handler = &proc_dointvec_jiffies,
662 .strategy = &sysctl_jiffies
663 },
664 { }
665};
Daniel Lezcano7d460db2008-01-18 23:52:35 -0800666
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800667static int ip6_frags_sysctl_register(struct net *net)
668{
669 struct ctl_table_header *hdr;
670
671 hdr = register_net_sysctl_table(net, net_ipv6_ctl_path,
672 ip6_frags_ctl_table);
673 return hdr == NULL ? -ENOMEM : 0;
674}
675#else
676static inline int ip6_frags_sysctl_register(struct net *net)
677{
678 return 0;
679}
680#endif
681
682static int ipv6_frags_init_net(struct net *net)
683{
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800684 ip6_frags.ctl = &net->ipv6.sysctl.frags;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800685
686 net->ipv6.sysctl.frags.high_thresh = 256 * 1024;
687 net->ipv6.sysctl.frags.low_thresh = 192 * 1024;
688 net->ipv6.sysctl.frags.timeout = IPV6_FRAG_TIMEOUT;
689 net->ipv6.sysctl.frags.secret_interval = 10 * 60 * HZ;
690
691 return ip6_frags_sysctl_register(net);
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800692}
693
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800694int __init ipv6_frag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800698 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
699 if (ret)
700 goto out;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800701
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800702 ipv6_frags_init_net(&init_net);
703
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700704 ip6_frags.hashfn = ip6_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700705 ip6_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700706 ip6_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700707 ip6_frags.skb_free = NULL;
708 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700709 ip6_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700710 ip6_frags.frag_expire = ip6_frag_expire;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700711 inet_frags_init(&ip6_frags);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800712out:
713 return ret;
714}
715
716void ipv6_frag_exit(void)
717{
718 inet_frags_fini(&ip6_frags);
719 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}