Merge tag 'ktest-v3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / reassembly.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 fragment reassembly
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4 7 *
1da177e4
LT
8 * Based on: net/ipv4/ip_fragment.c
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
1ab1457c
YH
16/*
17 * Fixes:
1da177e4
LT
18 * Andi Kleen Make it work with multiple hosts.
19 * More RFC compliance.
20 *
21 * Horst von Brand Add missing #include <linux/string.h>
22 * Alexey Kuznetsov SMP races, threading, cleanup.
23 * Patrick McHardy LRU queue of frag heads for evictor.
24 * Mitsuru KANDA @USAGI Register inet6_protocol{}.
25 * David Stevens and
26 * YOSHIFUJI,H. @USAGI Always remove fragment header to
27 * calculate ICV correctly.
28 */
1da177e4
LT
29#include <linux/errno.h>
30#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/socket.h>
33#include <linux/sockios.h>
34#include <linux/jiffies.h>
35#include <linux/net.h>
36#include <linux/list.h>
37#include <linux/netdevice.h>
38#include <linux/in6.h>
39#include <linux/ipv6.h>
40#include <linux/icmpv6.h>
41#include <linux/random.h>
42#include <linux/jhash.h>
f61944ef 43#include <linux/skbuff.h>
5a0e3ad6 44#include <linux/slab.h>
bc3b2d7f 45#include <linux/export.h>
1da177e4
LT
46
47#include <net/sock.h>
48#include <net/snmp.h>
49
50#include <net/ipv6.h>
a11d206d 51#include <net/ip6_route.h>
1da177e4
LT
52#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>
5ab11c98 57#include <net/inet_frag.h>
1da177e4 58
1da177e4
LT
59struct 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
7eb95156 68static struct inet_frags ip6_frags;
1da177e4 69
f61944ef
HX
70static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
71 struct net_device *dev);
72
f6596f9d
ZB
73/*
74 * callers should be careful not to use the hash value outside the ipfrag_lock
75 * as doing so could race with ipfrag_hash_rnd being recalculated.
76 */
93c8b90f
IJ
77unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
78 const struct in6_addr *daddr, u32 rnd)
1da177e4 79{
82a39eb6
JK
80 u32 c;
81
8064b3cf
ED
82 c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
83 (__force u32)id, rnd);
1da177e4 84
7eb95156 85 return c & (INETFRAGS_HASHSZ - 1);
1da177e4 86}
93c8b90f 87EXPORT_SYMBOL_GPL(inet6_hash_frag);
1da177e4 88
321a3a99 89static unsigned int ip6_hashfn(struct inet_frag_queue *q)
1da177e4 90{
321a3a99 91 struct frag_queue *fq;
1da177e4 92
321a3a99 93 fq = container_of(q, struct frag_queue, q);
93c8b90f 94 return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
1da177e4
LT
95}
96
cbc264ca 97bool ip6_frag_match(struct inet_frag_queue *q, void *a)
abd6523d
PE
98{
99 struct frag_queue *fq;
100 struct ip6_create_arg *arg = a;
101
102 fq = container_of(q, struct frag_queue, q);
cbc264ca
ED
103 return fq->id == arg->id &&
104 fq->user == arg->user &&
105 ipv6_addr_equal(&fq->saddr, arg->src) &&
106 ipv6_addr_equal(&fq->daddr, arg->dst);
abd6523d
PE
107}
108EXPORT_SYMBOL(ip6_frag_match);
109
c6fda282 110void ip6_frag_init(struct inet_frag_queue *q, void *a)
1da177e4 111{
c6fda282
PE
112 struct frag_queue *fq = container_of(q, struct frag_queue, q);
113 struct ip6_create_arg *arg = a;
114
115 fq->id = arg->id;
0b5ccb2e 116 fq->user = arg->user;
4e3fd7a0
AD
117 fq->saddr = *arg->src;
118 fq->daddr = *arg->dst;
1da177e4 119}
c6fda282 120EXPORT_SYMBOL(ip6_frag_init);
1da177e4 121
b836c99f
AW
122void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq,
123 struct inet_frags *frags)
1da177e4 124{
a11d206d 125 struct net_device *dev = NULL;
e521db9d 126
5ab11c98 127 spin_lock(&fq->q.lock);
1da177e4 128
bc578a54 129 if (fq->q.last_in & INET_FRAG_COMPLETE)
1da177e4
LT
130 goto out;
131
b836c99f 132 inet_frag_kill(&fq->q, frags);
1da177e4 133
69df9d59
ED
134 rcu_read_lock();
135 dev = dev_get_by_index_rcu(net, fq->iif);
a11d206d 136 if (!dev)
69df9d59 137 goto out_rcu_unlock;
a11d206d 138
483a47d2
DL
139 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
140 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
1da177e4 141
78c784c4 142 /* Don't send error if the first segment did not arrive. */
bc578a54 143 if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
69df9d59 144 goto out_rcu_unlock;
78c784c4 145
78c784c4
IO
146 /*
147 But use as source device on which LAST ARRIVED
148 segment was received. And do not use fq->dev
149 pointer directly, device might already disappeared.
150 */
5ab11c98 151 fq->q.fragments->dev = dev;
3ffe533c 152 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
69df9d59
ED
153out_rcu_unlock:
154 rcu_read_unlock();
1da177e4 155out:
5ab11c98 156 spin_unlock(&fq->q.lock);
b836c99f
AW
157 inet_frag_put(&fq->q, frags);
158}
159EXPORT_SYMBOL(ip6_expire_frag_queue);
160
161static void ip6_frag_expire(unsigned long data)
162{
163 struct frag_queue *fq;
164 struct net *net;
165
166 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
167 net = container_of(fq->q.net, struct net, ipv6.frags);
168
169 ip6_expire_frag_queue(net, fq, &ip6_frags);
1da177e4
LT
170}
171
abd6523d 172static __inline__ struct frag_queue *
b71d1d42 173fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6_addr *dst)
1da177e4 174{
c6fda282
PE
175 struct inet_frag_queue *q;
176 struct ip6_create_arg arg;
abd6523d 177 unsigned int hash;
1da177e4 178
c6fda282 179 arg.id = id;
0b5ccb2e 180 arg.user = IP6_DEFRAG_LOCAL_DELIVER;
c6fda282
PE
181 arg.src = src;
182 arg.dst = dst;
9a375803
PE
183
184 read_lock(&ip6_frags.lock);
93c8b90f 185 hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
1da177e4 186
ac18e750 187 q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
c6fda282 188 if (q == NULL)
9546377c 189 return NULL;
1da177e4 190
c6fda282 191 return container_of(q, struct frag_queue, q);
1da177e4
LT
192}
193
f61944ef 194static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1da177e4
LT
195 struct frag_hdr *fhdr, int nhoff)
196{
197 struct sk_buff *prev, *next;
f61944ef 198 struct net_device *dev;
1da177e4 199 int offset, end;
adf30907 200 struct net *net = dev_net(skb_dst(skb)->dev);
1da177e4 201
bc578a54 202 if (fq->q.last_in & INET_FRAG_COMPLETE)
1da177e4
LT
203 goto err;
204
205 offset = ntohs(fhdr->frag_off) & ~0x7;
0660e03f
ACM
206 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
207 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1da177e4
LT
208
209 if ((unsigned int)end > IPV6_MAXPLEN) {
adf30907 210 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 211 IPSTATS_MIB_INHDRERRORS);
d56f90a7
ACM
212 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
213 ((u8 *)&fhdr->frag_off -
214 skb_network_header(skb)));
f61944ef 215 return -1;
1da177e4
LT
216 }
217
d56f90a7
ACM
218 if (skb->ip_summed == CHECKSUM_COMPLETE) {
219 const unsigned char *nh = skb_network_header(skb);
1ab1457c 220 skb->csum = csum_sub(skb->csum,
d56f90a7
ACM
221 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
222 0));
223 }
1da177e4
LT
224
225 /* Is this the final fragment? */
226 if (!(fhdr->frag_off & htons(IP6_MF))) {
227 /* If we already have some bits beyond end
228 * or have different end, the segment is corrupted.
229 */
5ab11c98 230 if (end < fq->q.len ||
bc578a54 231 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
1da177e4 232 goto err;
bc578a54 233 fq->q.last_in |= INET_FRAG_LAST_IN;
5ab11c98 234 fq->q.len = end;
1da177e4
LT
235 } else {
236 /* Check if the fragment is rounded to 8 bytes.
237 * Required by the RFC.
238 */
239 if (end & 0x7) {
240 /* RFC2460 says always send parameter problem in
241 * this case. -DaveM
242 */
adf30907 243 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 244 IPSTATS_MIB_INHDRERRORS);
1ab1457c 245 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
1da177e4 246 offsetof(struct ipv6hdr, payload_len));
f61944ef 247 return -1;
1da177e4 248 }
5ab11c98 249 if (end > fq->q.len) {
1da177e4 250 /* Some bits beyond end -> corruption. */
bc578a54 251 if (fq->q.last_in & INET_FRAG_LAST_IN)
1da177e4 252 goto err;
5ab11c98 253 fq->q.len = end;
1da177e4
LT
254 }
255 }
256
257 if (end == offset)
258 goto err;
259
260 /* Point into the IP datagram 'data' part. */
261 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
262 goto err;
1ab1457c 263
42ca89c1
SH
264 if (pskb_trim_rcsum(skb, end - offset))
265 goto err;
1da177e4
LT
266
267 /* Find out which fragments are in front and at the back of us
268 * in the chain of fragments so far. We must know where to put
269 * this fragment, right?
270 */
d6bebca9
CG
271 prev = fq->q.fragments_tail;
272 if (!prev || FRAG6_CB(prev)->offset < offset) {
273 next = NULL;
274 goto found;
275 }
1da177e4 276 prev = NULL;
5ab11c98 277 for(next = fq->q.fragments; next != NULL; next = next->next) {
1da177e4
LT
278 if (FRAG6_CB(next)->offset >= offset)
279 break; /* bingo! */
280 prev = next;
281 }
282
d6bebca9 283found:
5de658f8
ED
284 /* RFC5722, Section 4, amended by Errata ID : 3089
285 * When reassembling an IPv6 datagram, if
70789d70
ND
286 * one or more its constituent fragments is determined to be an
287 * overlapping fragment, the entire datagram (and any constituent
5de658f8 288 * fragments) MUST be silently discarded.
1da177e4 289 */
1da177e4 290
70789d70
ND
291 /* Check for overlap with preceding fragment. */
292 if (prev &&
f4642141 293 (FRAG6_CB(prev)->offset + prev->len) > offset)
70789d70 294 goto discard_fq;
1da177e4 295
70789d70
ND
296 /* Look for overlap with succeeding segment. */
297 if (next && FRAG6_CB(next)->offset < end)
298 goto discard_fq;
1da177e4
LT
299
300 FRAG6_CB(skb)->offset = offset;
301
302 /* Insert this fragment in the chain of fragments. */
303 skb->next = next;
d6bebca9
CG
304 if (!next)
305 fq->q.fragments_tail = skb;
1da177e4
LT
306 if (prev)
307 prev->next = skb;
308 else
5ab11c98 309 fq->q.fragments = skb;
1da177e4 310
f61944ef
HX
311 dev = skb->dev;
312 if (dev) {
313 fq->iif = dev->ifindex;
314 skb->dev = NULL;
315 }
5ab11c98
PE
316 fq->q.stamp = skb->tstamp;
317 fq->q.meat += skb->len;
d433673e 318 add_frag_mem_limit(&fq->q, skb->truesize);
1da177e4
LT
319
320 /* The first fragment.
321 * nhoffset is obtained from the first fragment, of course.
322 */
323 if (offset == 0) {
324 fq->nhoffset = nhoff;
bc578a54 325 fq->q.last_in |= INET_FRAG_FIRST_IN;
1da177e4 326 }
f61944ef 327
bc578a54
JP
328 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
329 fq->q.meat == fq->q.len)
f61944ef
HX
330 return ip6_frag_reasm(fq, prev, dev);
331
3ef0eb0d 332 inet_frag_lru_move(&fq->q);
f61944ef 333 return -1;
1da177e4 334
70789d70 335discard_fq:
b836c99f 336 inet_frag_kill(&fq->q, &ip6_frags);
1da177e4 337err:
adf30907 338 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3bd653c8 339 IPSTATS_MIB_REASMFAILS);
1da177e4 340 kfree_skb(skb);
f61944ef 341 return -1;
1da177e4
LT
342}
343
344/*
345 * Check if this packet is complete.
346 * Returns NULL on failure by any reason, and pointer
347 * to current nexthdr field in reassembled frame.
348 *
349 * It is called with locked fq, and caller must check that
350 * queue is eligible for reassembly i.e. it is not COMPLETE,
351 * the last and the first frames arrived and all the bits are here.
352 */
f61944ef 353static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
1da177e4
LT
354 struct net_device *dev)
355{
2bad35b7 356 struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
5ab11c98 357 struct sk_buff *fp, *head = fq->q.fragments;
1da177e4
LT
358 int payload_len;
359 unsigned int nhoff;
ec16439e 360 int sum_truesize;
1da177e4 361
b836c99f 362 inet_frag_kill(&fq->q, &ip6_frags);
1da177e4 363
f61944ef
HX
364 /* Make the one we just received the head. */
365 if (prev) {
366 head = prev->next;
367 fp = skb_clone(head, GFP_ATOMIC);
368
369 if (!fp)
370 goto out_oom;
371
372 fp->next = head->next;
d6bebca9
CG
373 if (!fp->next)
374 fq->q.fragments_tail = fp;
f61944ef
HX
375 prev->next = fp;
376
5ab11c98
PE
377 skb_morph(head, fq->q.fragments);
378 head->next = fq->q.fragments->next;
f61944ef 379
808db80a 380 consume_skb(fq->q.fragments);
5ab11c98 381 fq->q.fragments = head;
f61944ef
HX
382 }
383
547b792c
IJ
384 WARN_ON(head == NULL);
385 WARN_ON(FRAG6_CB(head)->offset != 0);
1da177e4
LT
386
387 /* Unfragmented part is taken from the first segment. */
d56f90a7 388 payload_len = ((head->data - skb_network_header(head)) -
5ab11c98 389 sizeof(struct ipv6hdr) + fq->q.len -
d56f90a7 390 sizeof(struct frag_hdr));
1da177e4
LT
391 if (payload_len > IPV6_MAXPLEN)
392 goto out_oversize;
393
394 /* Head of list must not be cloned. */
14bbd6a5 395 if (skb_unclone(head, GFP_ATOMIC))
1da177e4
LT
396 goto out_oom;
397
398 /* If the first fragment is fragmented itself, we split
399 * it to two chunks: the first with data and paged part
400 * and the second, holding only fragments. */
21dc3301 401 if (skb_has_frag_list(head)) {
1da177e4
LT
402 struct sk_buff *clone;
403 int i, plen = 0;
404
405 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
406 goto out_oom;
407 clone->next = head->next;
408 head->next = clone;
409 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4d9092bb 410 skb_frag_list_init(head);
9e903e08
ED
411 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
412 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
1da177e4
LT
413 clone->len = clone->data_len = head->data_len - plen;
414 head->data_len -= clone->len;
415 head->len -= clone->len;
416 clone->csum = 0;
417 clone->ip_summed = head->ip_summed;
d433673e 418 add_frag_mem_limit(&fq->q, clone->truesize);
1da177e4
LT
419 }
420
421 /* We have to remove fragment header from datagram and to relocate
422 * header in order to calculate ICV correctly. */
423 nhoff = fq->nhoffset;
b0e380b1 424 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
1ab1457c 425 memmove(head->head + sizeof(struct frag_hdr), head->head,
1da177e4 426 (head->data - head->head) - sizeof(struct frag_hdr));
b0e380b1
ACM
427 head->mac_header += sizeof(struct frag_hdr);
428 head->network_header += sizeof(struct frag_hdr);
1da177e4 429
badff6d0 430 skb_reset_transport_header(head);
d56f90a7 431 skb_push(head, head->data - skb_network_header(head));
1da177e4 432
ec16439e
ED
433 sum_truesize = head->truesize;
434 for (fp = head->next; fp;) {
435 bool headstolen;
436 int delta;
437 struct sk_buff *next = fp->next;
438
439 sum_truesize += fp->truesize;
1da177e4
LT
440 if (head->ip_summed != fp->ip_summed)
441 head->ip_summed = CHECKSUM_NONE;
84fa7933 442 else if (head->ip_summed == CHECKSUM_COMPLETE)
1da177e4 443 head->csum = csum_add(head->csum, fp->csum);
ec16439e
ED
444
445 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
446 kfree_skb_partial(fp, headstolen);
447 } else {
448 if (!skb_shinfo(head)->frag_list)
449 skb_shinfo(head)->frag_list = fp;
450 head->data_len += fp->len;
451 head->len += fp->len;
452 head->truesize += fp->truesize;
453 }
454 fp = next;
1da177e4 455 }
d433673e 456 sub_frag_mem_limit(&fq->q, sum_truesize);
1da177e4
LT
457
458 head->next = NULL;
459 head->dev = dev;
5ab11c98 460 head->tstamp = fq->q.stamp;
0660e03f 461 ipv6_hdr(head)->payload_len = htons(payload_len);
951dbc8a 462 IP6CB(head)->nhoff = nhoff;
1da177e4 463
1da177e4 464 /* Yes, and fold redundant checksum back. 8) */
84fa7933 465 if (head->ip_summed == CHECKSUM_COMPLETE)
d56f90a7 466 head->csum = csum_partial(skb_network_header(head),
cfe1fc77 467 skb_network_header_len(head),
d56f90a7 468 head->csum);
1da177e4 469
a11d206d 470 rcu_read_lock();
2bad35b7 471 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
a11d206d 472 rcu_read_unlock();
5ab11c98 473 fq->q.fragments = NULL;
d6bebca9 474 fq->q.fragments_tail = NULL;
1da177e4
LT
475 return 1;
476
477out_oversize:
e87cc472 478 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
1da177e4
LT
479 goto out_fail;
480out_oom:
e87cc472 481 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
1da177e4 482out_fail:
a11d206d 483 rcu_read_lock();
2bad35b7 484 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
a11d206d 485 rcu_read_unlock();
1da177e4
LT
486 return -1;
487}
488
e5bbef20 489static int ipv6_frag_rcv(struct sk_buff *skb)
1da177e4 490{
1da177e4
LT
491 struct frag_hdr *fhdr;
492 struct frag_queue *fq;
b71d1d42 493 const struct ipv6hdr *hdr = ipv6_hdr(skb);
adf30907 494 struct net *net = dev_net(skb_dst(skb)->dev);
6b102865 495 int evicted;
1da177e4 496
adf30907 497 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
1da177e4
LT
498
499 /* Jumbo payload inhibits frag. header */
98b3377c
DL
500 if (hdr->payload_len==0)
501 goto fail_hdr;
502
ea2ae17d 503 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
98b3377c
DL
504 sizeof(struct frag_hdr))))
505 goto fail_hdr;
1da177e4 506
0660e03f 507 hdr = ipv6_hdr(skb);
9c70220b 508 fhdr = (struct frag_hdr *)skb_transport_header(skb);
1da177e4
LT
509
510 if (!(fhdr->frag_off & htons(0xFFF9))) {
511 /* It is not a fragmented frame */
b0e380b1 512 skb->transport_header += sizeof(struct frag_hdr);
483a47d2 513 IP6_INC_STATS_BH(net,
adf30907 514 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
1da177e4 515
d56f90a7 516 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
1da177e4
LT
517 return 1;
518 }
519
6b102865
AW
520 evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags, false);
521 if (evicted)
522 IP6_ADD_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
523 IPSTATS_MIB_REASMFAILS, evicted);
1da177e4 524
9546377c
SW
525 fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
526 if (fq != NULL) {
f61944ef 527 int ret;
1da177e4 528
5ab11c98 529 spin_lock(&fq->q.lock);
1da177e4 530
f61944ef 531 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
1da177e4 532
5ab11c98 533 spin_unlock(&fq->q.lock);
b836c99f 534 inet_frag_put(&fq->q, &ip6_frags);
1da177e4
LT
535 return ret;
536 }
537
adf30907 538 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
1da177e4
LT
539 kfree_skb(skb);
540 return -1;
98b3377c
DL
541
542fail_hdr:
adf30907 543 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
98b3377c
DL
544 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
545 return -1;
1da177e4
LT
546}
547
41135cc8 548static const struct inet6_protocol frag_protocol =
1da177e4
LT
549{
550 .handler = ipv6_frag_rcv,
551 .flags = INET6_PROTO_NOPOLICY,
552};
553
8d8354d2 554#ifdef CONFIG_SYSCTL
0a64b4b8 555static struct ctl_table ip6_frags_ns_ctl_table[] = {
8d8354d2 556 {
8d8354d2 557 .procname = "ip6frag_high_thresh",
e31e0bdc 558 .data = &init_net.ipv6.frags.high_thresh,
8d8354d2
PE
559 .maxlen = sizeof(int),
560 .mode = 0644,
6d9f239a 561 .proc_handler = proc_dointvec
8d8354d2
PE
562 },
563 {
8d8354d2 564 .procname = "ip6frag_low_thresh",
e31e0bdc 565 .data = &init_net.ipv6.frags.low_thresh,
8d8354d2
PE
566 .maxlen = sizeof(int),
567 .mode = 0644,
6d9f239a 568 .proc_handler = proc_dointvec
8d8354d2
PE
569 },
570 {
8d8354d2 571 .procname = "ip6frag_time",
b2fd5321 572 .data = &init_net.ipv6.frags.timeout,
8d8354d2
PE
573 .maxlen = sizeof(int),
574 .mode = 0644,
6d9f239a 575 .proc_handler = proc_dointvec_jiffies,
8d8354d2 576 },
7d291ebb
PE
577 { }
578};
579
580static struct ctl_table ip6_frags_ctl_table[] = {
8d8354d2 581 {
8d8354d2 582 .procname = "ip6frag_secret_interval",
3b4bc4a2 583 .data = &ip6_frags.secret_interval,
8d8354d2
PE
584 .maxlen = sizeof(int),
585 .mode = 0644,
6d9f239a 586 .proc_handler = proc_dointvec_jiffies,
8d8354d2
PE
587 },
588 { }
589};
590
2c8c1e72 591static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
8d8354d2 592{
e4a2d5c2 593 struct ctl_table *table;
8d8354d2
PE
594 struct ctl_table_header *hdr;
595
0a64b4b8 596 table = ip6_frags_ns_ctl_table;
09ad9bc7 597 if (!net_eq(net, &init_net)) {
0a64b4b8 598 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
e4a2d5c2
PE
599 if (table == NULL)
600 goto err_alloc;
601
e31e0bdc
PE
602 table[0].data = &net->ipv6.frags.high_thresh;
603 table[1].data = &net->ipv6.frags.low_thresh;
b2fd5321 604 table[2].data = &net->ipv6.frags.timeout;
464dc801
EB
605
606 /* Don't export sysctls to unprivileged users */
607 if (net->user_ns != &init_user_ns)
608 table[0].procname = NULL;
e4a2d5c2
PE
609 }
610
ec8f23ce 611 hdr = register_net_sysctl(net, "net/ipv6", table);
e4a2d5c2
PE
612 if (hdr == NULL)
613 goto err_reg;
614
615 net->ipv6.sysctl.frags_hdr = hdr;
616 return 0;
617
618err_reg:
09ad9bc7 619 if (!net_eq(net, &init_net))
e4a2d5c2
PE
620 kfree(table);
621err_alloc:
622 return -ENOMEM;
623}
624
2c8c1e72 625static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
e4a2d5c2
PE
626{
627 struct ctl_table *table;
628
629 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
630 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
3705e11a
YH
631 if (!net_eq(net, &init_net))
632 kfree(table);
8d8354d2 633}
7d291ebb
PE
634
635static struct ctl_table_header *ip6_ctl_header;
636
637static int ip6_frags_sysctl_register(void)
638{
43444757 639 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
7d291ebb
PE
640 ip6_frags_ctl_table);
641 return ip6_ctl_header == NULL ? -ENOMEM : 0;
642}
643
644static void ip6_frags_sysctl_unregister(void)
645{
646 unregister_net_sysctl_table(ip6_ctl_header);
647}
8d8354d2 648#else
0a64b4b8 649static inline int ip6_frags_ns_sysctl_register(struct net *net)
e71e0349 650{
8d8354d2
PE
651 return 0;
652}
e4a2d5c2 653
0a64b4b8 654static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
e4a2d5c2
PE
655{
656}
7d291ebb
PE
657
658static inline int ip6_frags_sysctl_register(void)
659{
660 return 0;
661}
662
663static inline void ip6_frags_sysctl_unregister(void)
664{
665}
8d8354d2 666#endif
7d460db9 667
2c8c1e72 668static int __net_init ipv6_frags_init_net(struct net *net)
8d8354d2 669{
7c070aa9
SW
670 net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
671 net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
b2fd5321 672 net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
8d8354d2 673
e5a2bb84
PE
674 inet_frags_init_net(&net->ipv6.frags);
675
0a64b4b8 676 return ip6_frags_ns_sysctl_register(net);
e71e0349
DL
677}
678
2c8c1e72 679static void __net_exit ipv6_frags_exit_net(struct net *net)
81566e83 680{
0a64b4b8 681 ip6_frags_ns_sysctl_unregister(net);
81566e83
PE
682 inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
683}
684
685static struct pernet_operations ip6_frags_ops = {
686 .init = ipv6_frags_init_net,
687 .exit = ipv6_frags_exit_net,
688};
689
853cbbaa 690int __init ipv6_frag_init(void)
1da177e4 691{
853cbbaa 692 int ret;
1da177e4 693
853cbbaa
DL
694 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
695 if (ret)
696 goto out;
e71e0349 697
7d291ebb
PE
698 ret = ip6_frags_sysctl_register();
699 if (ret)
700 goto err_sysctl;
701
0002c630
PE
702 ret = register_pernet_subsys(&ip6_frags_ops);
703 if (ret)
704 goto err_pernet;
8d8354d2 705
321a3a99 706 ip6_frags.hashfn = ip6_hashfn;
c6fda282 707 ip6_frags.constructor = ip6_frag_init;
c9547709 708 ip6_frags.destructor = NULL;
1e4b8287
PE
709 ip6_frags.skb_free = NULL;
710 ip6_frags.qsize = sizeof(struct frag_queue);
abd6523d 711 ip6_frags.match = ip6_frag_match;
e521db9d 712 ip6_frags.frag_expire = ip6_frag_expire;
3b4bc4a2 713 ip6_frags.secret_interval = 10 * 60 * HZ;
7eb95156 714 inet_frags_init(&ip6_frags);
853cbbaa
DL
715out:
716 return ret;
0002c630
PE
717
718err_pernet:
7d291ebb
PE
719 ip6_frags_sysctl_unregister();
720err_sysctl:
0002c630
PE
721 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
722 goto out;
853cbbaa
DL
723}
724
725void ipv6_frag_exit(void)
726{
727 inet_frags_fini(&ip6_frags);
7d291ebb 728 ip6_frags_sysctl_unregister();
81566e83 729 unregister_pernet_subsys(&ip6_frags_ops);
853cbbaa 730 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
1da177e4 731}