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