defconfig: exynos9610: Re-add dropped Wi-Fi AP options lost
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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 */
5a3da1fe
HFS
29
30#define pr_fmt(fmt) "IPv6: " fmt
31
1da177e4
LT
32#include <linux/errno.h>
33#include <linux/types.h>
34#include <linux/string.h>
35#include <linux/socket.h>
36#include <linux/sockios.h>
37#include <linux/jiffies.h>
38#include <linux/net.h>
39#include <linux/list.h>
40#include <linux/netdevice.h>
41#include <linux/in6.h>
42#include <linux/ipv6.h>
43#include <linux/icmpv6.h>
44#include <linux/random.h>
45#include <linux/jhash.h>
f61944ef 46#include <linux/skbuff.h>
5a0e3ad6 47#include <linux/slab.h>
bc3b2d7f 48#include <linux/export.h>
1da177e4
LT
49
50#include <net/sock.h>
51#include <net/snmp.h>
52
53#include <net/ipv6.h>
a11d206d 54#include <net/ip6_route.h>
1da177e4
LT
55#include <net/protocol.h>
56#include <net/transp_v6.h>
57#include <net/rawv6.h>
58#include <net/ndisc.h>
59#include <net/addrconf.h>
5ab11c98 60#include <net/inet_frag.h>
eec2e618 61#include <net/inet_ecn.h>
1da177e4 62
d4ad4d22
NA
63static const char ip6_frag_cache_name[] = "ip6-frags";
64
cc24beca 65struct ip6frag_skb_cb {
1da177e4
LT
66 struct inet6_skb_parm h;
67 int offset;
68};
69
67ba4152 70#define FRAG6_CB(skb) ((struct ip6frag_skb_cb *)((skb)->cb))
1da177e4 71
fc08c258 72static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
eec2e618
HFS
73{
74 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
75}
1da177e4 76
7eb95156 77static struct inet_frags ip6_frags;
1da177e4 78
f61944ef
HX
79static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
80 struct net_device *dev);
81
36c77782 82void ip6_frag_init(struct inet_frag_queue *q, const void *a)
1da177e4 83{
c6fda282 84 struct frag_queue *fq = container_of(q, struct frag_queue, q);
9aee41ef 85 const struct frag_v6_compare_key *key = a;
c6fda282 86
9aee41ef
ED
87 q->key.v6 = *key;
88 fq->ecn = 0;
1da177e4 89}
c6fda282 90EXPORT_SYMBOL(ip6_frag_init);
1da177e4 91
673220d6 92void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq)
1da177e4 93{
a11d206d 94 struct net_device *dev = NULL;
3226bdcb 95 struct sk_buff *head;
e521db9d 96
3226bdcb 97 rcu_read_lock();
5ab11c98 98 spin_lock(&fq->q.lock);
1da177e4 99
06aa8b8a 100 if (fq->q.flags & INET_FRAG_COMPLETE)
1da177e4
LT
101 goto out;
102
673220d6 103 inet_frag_kill(&fq->q);
1da177e4 104
69df9d59 105 dev = dev_get_by_index_rcu(net, fq->iif);
a11d206d 106 if (!dev)
3226bdcb 107 goto out;
a11d206d 108
1d015503 109 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
1d015503 110 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
2e404f63 111
78c784c4 112 /* Don't send error if the first segment did not arrive. */
3226bdcb
ED
113 head = fq->q.fragments;
114 if (!(fq->q.flags & INET_FRAG_FIRST_IN) || !head)
115 goto out;
78c784c4 116
2e404f63
NA
117 /* But use as source device on which LAST ARRIVED
118 * segment was received. And do not use fq->dev
119 * pointer directly, device might already disappeared.
78c784c4 120 */
3226bdcb
ED
121 head->dev = dev;
122 skb_get(head);
123 spin_unlock(&fq->q.lock);
124
125 icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
126 kfree_skb(head);
127 goto out_rcu_unlock;
128
1da177e4 129out:
5ab11c98 130 spin_unlock(&fq->q.lock);
3226bdcb
ED
131out_rcu_unlock:
132 rcu_read_unlock();
673220d6 133 inet_frag_put(&fq->q);
b836c99f
AW
134}
135EXPORT_SYMBOL(ip6_expire_frag_queue);
136
79bdf76c 137#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
138static void ip6_frag_expire(unsigned long t)
139#else
0512f7e9 140static void ip6_frag_expire(struct timer_list *t)
79bdf76c 141#endif
b836c99f 142{
79bdf76c 143 struct inet_frag_queue *frag = from_timer(frag, (struct timer_list *)t, timer);
b836c99f
AW
144 struct frag_queue *fq;
145 struct net *net;
146
0512f7e9 147 fq = container_of(frag, struct frag_queue, q);
b836c99f
AW
148 net = container_of(fq->q.net, struct net, ipv6.frags);
149
673220d6 150 ip6_expire_frag_queue(net, fq);
1da177e4
LT
151}
152
fc08c258 153static struct frag_queue *
9aee41ef 154fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
1da177e4 155{
9aee41ef
ED
156 struct frag_v6_compare_key key = {
157 .id = id,
158 .saddr = hdr->saddr,
159 .daddr = hdr->daddr,
160 .user = IP6_DEFRAG_LOCAL_DELIVER,
161 .iif = iif,
162 };
c6fda282 163 struct inet_frag_queue *q;
9a375803 164
9aee41ef
ED
165 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
166 IPV6_ADDR_LINKLOCAL)))
167 key.iif = 0;
1da177e4 168
9aee41ef 169 q = inet_frag_find(&net->ipv6.frags, &key);
caa4249e 170 if (!q)
9546377c 171 return NULL;
caa4249e 172
c6fda282 173 return container_of(q, struct frag_queue, q);
1da177e4
LT
174}
175
f61944ef 176static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1da177e4
LT
177 struct frag_hdr *fhdr, int nhoff)
178{
179 struct sk_buff *prev, *next;
f61944ef 180 struct net_device *dev;
dbd1759e 181 int offset, end, fragsize;
adf30907 182 struct net *net = dev_net(skb_dst(skb)->dev);
eec2e618 183 u8 ecn;
1da177e4 184
06aa8b8a 185 if (fq->q.flags & INET_FRAG_COMPLETE)
1da177e4
LT
186 goto err;
187
188 offset = ntohs(fhdr->frag_off) & ~0x7;
0660e03f
ACM
189 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
190 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1da177e4
LT
191
192 if ((unsigned int)end > IPV6_MAXPLEN) {
1d015503
ED
193 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
194 IPSTATS_MIB_INHDRERRORS);
d56f90a7
ACM
195 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
196 ((u8 *)&fhdr->frag_off -
197 skb_network_header(skb)));
f61944ef 198 return -1;
1da177e4
LT
199 }
200
eec2e618
HFS
201 ecn = ip6_frag_ecn(ipv6_hdr(skb));
202
d56f90a7
ACM
203 if (skb->ip_summed == CHECKSUM_COMPLETE) {
204 const unsigned char *nh = skb_network_header(skb);
1ab1457c 205 skb->csum = csum_sub(skb->csum,
d56f90a7
ACM
206 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
207 0));
208 }
1da177e4
LT
209
210 /* Is this the final fragment? */
211 if (!(fhdr->frag_off & htons(IP6_MF))) {
212 /* If we already have some bits beyond end
213 * or have different end, the segment is corrupted.
214 */
5ab11c98 215 if (end < fq->q.len ||
06aa8b8a 216 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
1da177e4 217 goto err;
06aa8b8a 218 fq->q.flags |= INET_FRAG_LAST_IN;
5ab11c98 219 fq->q.len = end;
1da177e4
LT
220 } else {
221 /* Check if the fragment is rounded to 8 bytes.
222 * Required by the RFC.
223 */
224 if (end & 0x7) {
225 /* RFC2460 says always send parameter problem in
226 * this case. -DaveM
227 */
1d015503
ED
228 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
229 IPSTATS_MIB_INHDRERRORS);
1ab1457c 230 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
1da177e4 231 offsetof(struct ipv6hdr, payload_len));
f61944ef 232 return -1;
1da177e4 233 }
5ab11c98 234 if (end > fq->q.len) {
1da177e4 235 /* Some bits beyond end -> corruption. */
06aa8b8a 236 if (fq->q.flags & INET_FRAG_LAST_IN)
1da177e4 237 goto err;
5ab11c98 238 fq->q.len = end;
1da177e4
LT
239 }
240 }
241
242 if (end == offset)
243 goto err;
244
245 /* Point into the IP datagram 'data' part. */
246 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
247 goto err;
1ab1457c 248
42ca89c1
SH
249 if (pskb_trim_rcsum(skb, end - offset))
250 goto err;
1da177e4
LT
251
252 /* Find out which fragments are in front and at the back of us
253 * in the chain of fragments so far. We must know where to put
254 * this fragment, right?
255 */
d6bebca9
CG
256 prev = fq->q.fragments_tail;
257 if (!prev || FRAG6_CB(prev)->offset < offset) {
258 next = NULL;
259 goto found;
260 }
1da177e4 261 prev = NULL;
67ba4152 262 for (next = fq->q.fragments; next != NULL; next = next->next) {
1da177e4
LT
263 if (FRAG6_CB(next)->offset >= offset)
264 break; /* bingo! */
265 prev = next;
266 }
267
d6bebca9 268found:
5de658f8
ED
269 /* RFC5722, Section 4, amended by Errata ID : 3089
270 * When reassembling an IPv6 datagram, if
70789d70
ND
271 * one or more its constituent fragments is determined to be an
272 * overlapping fragment, the entire datagram (and any constituent
5de658f8 273 * fragments) MUST be silently discarded.
1da177e4 274 */
1da177e4 275
70789d70
ND
276 /* Check for overlap with preceding fragment. */
277 if (prev &&
f4642141 278 (FRAG6_CB(prev)->offset + prev->len) > offset)
70789d70 279 goto discard_fq;
1da177e4 280
70789d70
ND
281 /* Look for overlap with succeeding segment. */
282 if (next && FRAG6_CB(next)->offset < end)
283 goto discard_fq;
1da177e4
LT
284
285 FRAG6_CB(skb)->offset = offset;
286
287 /* Insert this fragment in the chain of fragments. */
288 skb->next = next;
d6bebca9
CG
289 if (!next)
290 fq->q.fragments_tail = skb;
1da177e4
LT
291 if (prev)
292 prev->next = skb;
293 else
5ab11c98 294 fq->q.fragments = skb;
1da177e4 295
f61944ef
HX
296 dev = skb->dev;
297 if (dev) {
298 fq->iif = dev->ifindex;
299 skb->dev = NULL;
300 }
5ab11c98
PE
301 fq->q.stamp = skb->tstamp;
302 fq->q.meat += skb->len;
eec2e618 303 fq->ecn |= ecn;
0e60d245 304 add_frag_mem_limit(fq->q.net, skb->truesize);
1da177e4 305
dbd1759e
WB
306 fragsize = -skb_network_offset(skb) + skb->len;
307 if (fragsize > fq->q.max_size)
308 fq->q.max_size = fragsize;
309
1da177e4
LT
310 /* The first fragment.
311 * nhoffset is obtained from the first fragment, of course.
312 */
313 if (offset == 0) {
314 fq->nhoffset = nhoff;
06aa8b8a 315 fq->q.flags |= INET_FRAG_FIRST_IN;
1da177e4 316 }
f61944ef 317
06aa8b8a 318 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
97599dc7
ED
319 fq->q.meat == fq->q.len) {
320 int res;
321 unsigned long orefdst = skb->_skb_refdst;
322
323 skb->_skb_refdst = 0UL;
324 res = ip6_frag_reasm(fq, prev, dev);
325 skb->_skb_refdst = orefdst;
326 return res;
327 }
f61944ef 328
97599dc7 329 skb_dst_drop(skb);
f61944ef 330 return -1;
1da177e4 331
70789d70 332discard_fq:
673220d6 333 inet_frag_kill(&fq->q);
1da177e4 334err:
1d015503
ED
335 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
336 IPSTATS_MIB_REASMFAILS);
1da177e4 337 kfree_skb(skb);
f61944ef 338 return -1;
1da177e4
LT
339}
340
341/*
342 * Check if this packet is complete.
343 * Returns NULL on failure by any reason, and pointer
344 * to current nexthdr field in reassembled frame.
345 *
346 * It is called with locked fq, and caller must check that
347 * queue is eligible for reassembly i.e. it is not COMPLETE,
348 * the last and the first frames arrived and all the bits are here.
349 */
f61944ef 350static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
1da177e4
LT
351 struct net_device *dev)
352{
2bad35b7 353 struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
5ab11c98 354 struct sk_buff *fp, *head = fq->q.fragments;
da4b6929 355 int payload_len, delta;
1da177e4 356 unsigned int nhoff;
ec16439e 357 int sum_truesize;
eec2e618 358 u8 ecn;
1da177e4 359
673220d6 360 inet_frag_kill(&fq->q);
1da177e4 361
eec2e618
HFS
362 ecn = ip_frag_ecn_table[fq->ecn];
363 if (unlikely(ecn == 0xff))
364 goto out_fail;
365
f61944ef
HX
366 /* Make the one we just received the head. */
367 if (prev) {
368 head = prev->next;
369 fp = skb_clone(head, GFP_ATOMIC);
370
371 if (!fp)
372 goto out_oom;
373
374 fp->next = head->next;
d6bebca9
CG
375 if (!fp->next)
376 fq->q.fragments_tail = fp;
f61944ef
HX
377 prev->next = fp;
378
5ab11c98
PE
379 skb_morph(head, fq->q.fragments);
380 head->next = fq->q.fragments->next;
f61944ef 381
808db80a 382 consume_skb(fq->q.fragments);
5ab11c98 383 fq->q.fragments = head;
f61944ef
HX
384 }
385
547b792c
IJ
386 WARN_ON(head == NULL);
387 WARN_ON(FRAG6_CB(head)->offset != 0);
1da177e4
LT
388
389 /* Unfragmented part is taken from the first segment. */
d56f90a7 390 payload_len = ((head->data - skb_network_header(head)) -
5ab11c98 391 sizeof(struct ipv6hdr) + fq->q.len -
d56f90a7 392 sizeof(struct frag_hdr));
1da177e4
LT
393 if (payload_len > IPV6_MAXPLEN)
394 goto out_oversize;
395
da4b6929
JW
396 delta = - head->truesize;
397
1da177e4 398 /* Head of list must not be cloned. */
14bbd6a5 399 if (skb_unclone(head, GFP_ATOMIC))
1da177e4
LT
400 goto out_oom;
401
da4b6929
JW
402 delta += head->truesize;
403 if (delta)
404 add_frag_mem_limit(fq->q.net, delta);
405
1da177e4
LT
406 /* If the first fragment is fragmented itself, we split
407 * it to two chunks: the first with data and paged part
408 * and the second, holding only fragments. */
21dc3301 409 if (skb_has_frag_list(head)) {
1da177e4
LT
410 struct sk_buff *clone;
411 int i, plen = 0;
412
e5d08d71 413 clone = alloc_skb(0, GFP_ATOMIC);
63159f29 414 if (!clone)
1da177e4
LT
415 goto out_oom;
416 clone->next = head->next;
417 head->next = clone;
418 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4d9092bb 419 skb_frag_list_init(head);
9e903e08
ED
420 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
421 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
1da177e4
LT
422 clone->len = clone->data_len = head->data_len - plen;
423 head->data_len -= clone->len;
424 head->len -= clone->len;
425 clone->csum = 0;
426 clone->ip_summed = head->ip_summed;
0e60d245 427 add_frag_mem_limit(fq->q.net, clone->truesize);
1da177e4
LT
428 }
429
430 /* We have to remove fragment header from datagram and to relocate
431 * header in order to calculate ICV correctly. */
432 nhoff = fq->nhoffset;
b0e380b1 433 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
1ab1457c 434 memmove(head->head + sizeof(struct frag_hdr), head->head,
1da177e4 435 (head->data - head->head) - sizeof(struct frag_hdr));
b678aa57
JD
436 if (skb_mac_header_was_set(head))
437 head->mac_header += sizeof(struct frag_hdr);
b0e380b1 438 head->network_header += sizeof(struct frag_hdr);
1da177e4 439
badff6d0 440 skb_reset_transport_header(head);
d56f90a7 441 skb_push(head, head->data - skb_network_header(head));
1da177e4 442
ec16439e
ED
443 sum_truesize = head->truesize;
444 for (fp = head->next; fp;) {
445 bool headstolen;
446 int delta;
447 struct sk_buff *next = fp->next;
448
449 sum_truesize += fp->truesize;
1da177e4
LT
450 if (head->ip_summed != fp->ip_summed)
451 head->ip_summed = CHECKSUM_NONE;
84fa7933 452 else if (head->ip_summed == CHECKSUM_COMPLETE)
1da177e4 453 head->csum = csum_add(head->csum, fp->csum);
ec16439e
ED
454
455 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
456 kfree_skb_partial(fp, headstolen);
457 } else {
458 if (!skb_shinfo(head)->frag_list)
459 skb_shinfo(head)->frag_list = fp;
460 head->data_len += fp->len;
461 head->len += fp->len;
462 head->truesize += fp->truesize;
463 }
464 fp = next;
1da177e4 465 }
0e60d245 466 sub_frag_mem_limit(fq->q.net, sum_truesize);
1da177e4
LT
467
468 head->next = NULL;
469 head->dev = dev;
5ab11c98 470 head->tstamp = fq->q.stamp;
0660e03f 471 ipv6_hdr(head)->payload_len = htons(payload_len);
eec2e618 472 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
951dbc8a 473 IP6CB(head)->nhoff = nhoff;
f46078cf 474 IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
dbd1759e 475 IP6CB(head)->frag_max_size = fq->q.max_size;
1da177e4 476
1da177e4 477 /* Yes, and fold redundant checksum back. 8) */
6b83d28a
DB
478 skb_postpush_rcsum(head, skb_network_header(head),
479 skb_network_header_len(head));
1da177e4 480
a11d206d 481 rcu_read_lock();
1d015503 482 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
a11d206d 483 rcu_read_unlock();
5ab11c98 484 fq->q.fragments = NULL;
6b921536 485 fq->q.rb_fragments = RB_ROOT;
d6bebca9 486 fq->q.fragments_tail = NULL;
1da177e4
LT
487 return 1;
488
489out_oversize:
e87cc472 490 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
1da177e4
LT
491 goto out_fail;
492out_oom:
e87cc472 493 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
1da177e4 494out_fail:
a11d206d 495 rcu_read_lock();
1d015503 496 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
a11d206d 497 rcu_read_unlock();
1da177e4
LT
498 return -1;
499}
500
e5bbef20 501static int ipv6_frag_rcv(struct sk_buff *skb)
1da177e4 502{
1da177e4
LT
503 struct frag_hdr *fhdr;
504 struct frag_queue *fq;
b71d1d42 505 const struct ipv6hdr *hdr = ipv6_hdr(skb);
adf30907 506 struct net *net = dev_net(skb_dst(skb)->dev);
9aee41ef 507 int iif;
1da177e4 508
f46078cf
HFS
509 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
510 goto fail_hdr;
511
1d015503 512 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
1da177e4
LT
513
514 /* Jumbo payload inhibits frag. header */
67ba4152 515 if (hdr->payload_len == 0)
98b3377c
DL
516 goto fail_hdr;
517
ea2ae17d 518 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
98b3377c
DL
519 sizeof(struct frag_hdr))))
520 goto fail_hdr;
1da177e4 521
0660e03f 522 hdr = ipv6_hdr(skb);
9c70220b 523 fhdr = (struct frag_hdr *)skb_transport_header(skb);
1da177e4
LT
524
525 if (!(fhdr->frag_off & htons(0xFFF9))) {
526 /* It is not a fragmented frame */
b0e380b1 527 skb->transport_header += sizeof(struct frag_hdr);
1d015503
ED
528 __IP6_INC_STATS(net,
529 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
1da177e4 530
d56f90a7 531 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
f46078cf 532 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
1da177e4
LT
533 return 1;
534 }
535
5123ffda
FW
536 if (skb->len - skb_network_offset(skb) < IPV6_MIN_MTU &&
537 fhdr->frag_off & htons(IP6_MF))
538 goto fail_hdr;
539
9aee41ef
ED
540 iif = skb->dev ? skb->dev->ifindex : 0;
541 fq = fq_find(net, fhdr->identification, hdr, iif);
53b24b8f 542 if (fq) {
f61944ef 543 int ret;
1da177e4 544
5ab11c98 545 spin_lock(&fq->q.lock);
1da177e4 546
9aee41ef 547 fq->iif = iif;
f61944ef 548 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
1da177e4 549
5ab11c98 550 spin_unlock(&fq->q.lock);
673220d6 551 inet_frag_put(&fq->q);
1da177e4
LT
552 return ret;
553 }
554
1d015503 555 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
1da177e4
LT
556 kfree_skb(skb);
557 return -1;
98b3377c
DL
558
559fail_hdr:
1d015503
ED
560 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
561 IPSTATS_MIB_INHDRERRORS);
98b3377c
DL
562 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
563 return -1;
1da177e4
LT
564}
565
cc24beca 566static const struct inet6_protocol frag_protocol = {
1da177e4
LT
567 .handler = ipv6_frag_rcv,
568 .flags = INET6_PROTO_NOPOLICY,
569};
570
8d8354d2 571#ifdef CONFIG_SYSCTL
1bab4c75 572
0a64b4b8 573static struct ctl_table ip6_frags_ns_ctl_table[] = {
8d8354d2 574 {
8d8354d2 575 .procname = "ip6frag_high_thresh",
e31e0bdc 576 .data = &init_net.ipv6.frags.high_thresh,
990204dd 577 .maxlen = sizeof(unsigned long),
8d8354d2 578 .mode = 0644,
990204dd 579 .proc_handler = proc_doulongvec_minmax,
1bab4c75 580 .extra1 = &init_net.ipv6.frags.low_thresh
8d8354d2
PE
581 },
582 {
8d8354d2 583 .procname = "ip6frag_low_thresh",
e31e0bdc 584 .data = &init_net.ipv6.frags.low_thresh,
990204dd 585 .maxlen = sizeof(unsigned long),
8d8354d2 586 .mode = 0644,
5fff99e8 587 .proc_handler = proc_doulongvec_minmax,
1bab4c75 588 .extra2 = &init_net.ipv6.frags.high_thresh
8d8354d2
PE
589 },
590 {
8d8354d2 591 .procname = "ip6frag_time",
b2fd5321 592 .data = &init_net.ipv6.frags.timeout,
8d8354d2
PE
593 .maxlen = sizeof(int),
594 .mode = 0644,
6d9f239a 595 .proc_handler = proc_dointvec_jiffies,
8d8354d2 596 },
7d291ebb
PE
597 { }
598};
599
e3a57d18
FW
600/* secret interval has been deprecated */
601static int ip6_frags_secret_interval_unused;
7d291ebb 602static struct ctl_table ip6_frags_ctl_table[] = {
8d8354d2 603 {
8d8354d2 604 .procname = "ip6frag_secret_interval",
e3a57d18 605 .data = &ip6_frags_secret_interval_unused,
8d8354d2
PE
606 .maxlen = sizeof(int),
607 .mode = 0644,
6d9f239a 608 .proc_handler = proc_dointvec_jiffies,
8d8354d2
PE
609 },
610 { }
611};
612
2c8c1e72 613static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
8d8354d2 614{
e4a2d5c2 615 struct ctl_table *table;
8d8354d2
PE
616 struct ctl_table_header *hdr;
617
0a64b4b8 618 table = ip6_frags_ns_ctl_table;
09ad9bc7 619 if (!net_eq(net, &init_net)) {
0a64b4b8 620 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
63159f29 621 if (!table)
e4a2d5c2
PE
622 goto err_alloc;
623
e31e0bdc 624 table[0].data = &net->ipv6.frags.high_thresh;
1bab4c75
NA
625 table[0].extra1 = &net->ipv6.frags.low_thresh;
626 table[0].extra2 = &init_net.ipv6.frags.high_thresh;
e31e0bdc 627 table[1].data = &net->ipv6.frags.low_thresh;
1bab4c75 628 table[1].extra2 = &net->ipv6.frags.high_thresh;
b2fd5321 629 table[2].data = &net->ipv6.frags.timeout;
e4a2d5c2
PE
630 }
631
ec8f23ce 632 hdr = register_net_sysctl(net, "net/ipv6", table);
63159f29 633 if (!hdr)
e4a2d5c2
PE
634 goto err_reg;
635
636 net->ipv6.sysctl.frags_hdr = hdr;
637 return 0;
638
639err_reg:
09ad9bc7 640 if (!net_eq(net, &init_net))
e4a2d5c2
PE
641 kfree(table);
642err_alloc:
643 return -ENOMEM;
644}
645
2c8c1e72 646static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
e4a2d5c2
PE
647{
648 struct ctl_table *table;
649
650 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
651 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
3705e11a
YH
652 if (!net_eq(net, &init_net))
653 kfree(table);
8d8354d2 654}
7d291ebb
PE
655
656static struct ctl_table_header *ip6_ctl_header;
657
658static int ip6_frags_sysctl_register(void)
659{
43444757 660 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
7d291ebb
PE
661 ip6_frags_ctl_table);
662 return ip6_ctl_header == NULL ? -ENOMEM : 0;
663}
664
665static void ip6_frags_sysctl_unregister(void)
666{
667 unregister_net_sysctl_table(ip6_ctl_header);
668}
8d8354d2 669#else
fc08c258 670static int ip6_frags_ns_sysctl_register(struct net *net)
e71e0349 671{
8d8354d2
PE
672 return 0;
673}
e4a2d5c2 674
fc08c258 675static void ip6_frags_ns_sysctl_unregister(struct net *net)
e4a2d5c2
PE
676{
677}
7d291ebb 678
fc08c258 679static int ip6_frags_sysctl_register(void)
7d291ebb
PE
680{
681 return 0;
682}
683
fc08c258 684static void ip6_frags_sysctl_unregister(void)
7d291ebb
PE
685{
686}
8d8354d2 687#endif
7d460db9 688
2c8c1e72 689static int __net_init ipv6_frags_init_net(struct net *net)
8d8354d2 690{
6093d5ab
ED
691 int res;
692
7c070aa9
SW
693 net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
694 net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
b2fd5321 695 net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
673220d6 696 net->ipv6.frags.f = &ip6_frags;
8d8354d2 697
6093d5ab
ED
698 res = inet_frags_init_net(&net->ipv6.frags);
699 if (res < 0)
700 return res;
5a63643e 701
6093d5ab
ED
702 res = ip6_frags_ns_sysctl_register(net);
703 if (res < 0)
673220d6 704 inet_frags_exit_net(&net->ipv6.frags);
6093d5ab 705 return res;
e71e0349
DL
706}
707
2c8c1e72 708static void __net_exit ipv6_frags_exit_net(struct net *net)
81566e83 709{
0a64b4b8 710 ip6_frags_ns_sysctl_unregister(net);
673220d6 711 inet_frags_exit_net(&net->ipv6.frags);
81566e83
PE
712}
713
714static struct pernet_operations ip6_frags_ops = {
715 .init = ipv6_frags_init_net,
716 .exit = ipv6_frags_exit_net,
717};
718
9aee41ef
ED
719static u32 ip6_key_hashfn(const void *data, u32 len, u32 seed)
720{
721 return jhash2(data,
722 sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
723}
724
725static u32 ip6_obj_hashfn(const void *data, u32 len, u32 seed)
726{
727 const struct inet_frag_queue *fq = data;
728
729 return jhash2((const u32 *)&fq->key.v6,
730 sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
731}
732
733static int ip6_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
734{
735 const struct frag_v6_compare_key *key = arg->key;
736 const struct inet_frag_queue *fq = ptr;
737
738 return !!memcmp(&fq->key, key, sizeof(*key));
739}
740
741const struct rhashtable_params ip6_rhash_params = {
742 .head_offset = offsetof(struct inet_frag_queue, node),
743 .hashfn = ip6_key_hashfn,
744 .obj_hashfn = ip6_obj_hashfn,
745 .obj_cmpfn = ip6_obj_cmpfn,
746 .automatic_shrinking = true,
747};
748EXPORT_SYMBOL(ip6_rhash_params);
749
853cbbaa 750int __init ipv6_frag_init(void)
1da177e4 751{
853cbbaa 752 int ret;
1da177e4 753
eb1686ae
ED
754 ip6_frags.constructor = ip6_frag_init;
755 ip6_frags.destructor = NULL;
756 ip6_frags.qsize = sizeof(struct frag_queue);
eb1686ae
ED
757 ip6_frags.frag_expire = ip6_frag_expire;
758 ip6_frags.frags_cache_name = ip6_frag_cache_name;
9aee41ef 759 ip6_frags.rhash_params = ip6_rhash_params;
eb1686ae 760 ret = inet_frags_init(&ip6_frags);
853cbbaa
DL
761 if (ret)
762 goto out;
e71e0349 763
eb1686ae
ED
764 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
765 if (ret)
766 goto err_protocol;
767
7d291ebb
PE
768 ret = ip6_frags_sysctl_register();
769 if (ret)
770 goto err_sysctl;
771
0002c630
PE
772 ret = register_pernet_subsys(&ip6_frags_ops);
773 if (ret)
774 goto err_pernet;
8d8354d2 775
853cbbaa
DL
776out:
777 return ret;
0002c630
PE
778
779err_pernet:
7d291ebb
PE
780 ip6_frags_sysctl_unregister();
781err_sysctl:
0002c630 782 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
eb1686ae
ED
783err_protocol:
784 inet_frags_fini(&ip6_frags);
0002c630 785 goto out;
853cbbaa
DL
786}
787
788void ipv6_frag_exit(void)
789{
790 inet_frags_fini(&ip6_frags);
7d291ebb 791 ip6_frags_sysctl_unregister();
81566e83 792 unregister_pernet_subsys(&ip6_frags_ops);
853cbbaa 793 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
1da177e4 794}