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