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