]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - net/ipv6/reassembly.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-kernels.git] / net / ipv6 / reassembly.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * IPv6 fragment reassembly
1ab1457c 4 * Linux INET6 implementation
1da177e4
LT
5 *
6 * Authors:
1ab1457c 7 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4 8 *
1da177e4 9 * Based on: net/ipv4/ip_fragment.c
1da177e4
LT
10 */
11
1ab1457c
YH
12/*
13 * Fixes:
1da177e4
LT
14 * Andi Kleen Make it work with multiple hosts.
15 * More RFC compliance.
16 *
17 * Horst von Brand Add missing #include <linux/string.h>
18 * Alexey Kuznetsov SMP races, threading, cleanup.
19 * Patrick McHardy LRU queue of frag heads for evictor.
20 * Mitsuru KANDA @USAGI Register inet6_protocol{}.
21 * David Stevens and
22 * YOSHIFUJI,H. @USAGI Always remove fragment header to
23 * calculate ICV correctly.
24 */
5a3da1fe
HFS
25
26#define pr_fmt(fmt) "IPv6: " fmt
27
1da177e4
LT
28#include <linux/errno.h>
29#include <linux/types.h>
30#include <linux/string.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
33#include <linux/jiffies.h>
34#include <linux/net.h>
35#include <linux/list.h>
36#include <linux/netdevice.h>
37#include <linux/in6.h>
38#include <linux/ipv6.h>
39#include <linux/icmpv6.h>
40#include <linux/random.h>
41#include <linux/jhash.h>
f61944ef 42#include <linux/skbuff.h>
5a0e3ad6 43#include <linux/slab.h>
bc3b2d7f 44#include <linux/export.h>
1da177e4
LT
45
46#include <net/sock.h>
47#include <net/snmp.h>
48
49#include <net/ipv6.h>
a11d206d 50#include <net/ip6_route.h>
1da177e4
LT
51#include <net/protocol.h>
52#include <net/transp_v6.h>
53#include <net/rawv6.h>
54#include <net/ndisc.h>
55#include <net/addrconf.h>
70b095c8 56#include <net/ipv6_frag.h>
eec2e618 57#include <net/inet_ecn.h>
1da177e4 58
d4ad4d22
NA
59static const char ip6_frag_cache_name[] = "ip6-frags";
60
fc08c258 61static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
eec2e618
HFS
62{
63 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
64}
1da177e4 65
7eb95156 66static struct inet_frags ip6_frags;
1da177e4 67
d4289fcc
PO
68static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
69 struct sk_buff *prev_tail, struct net_device *dev);
f61944ef 70
78802011 71static void ip6_frag_expire(struct timer_list *t)
b836c99f 72{
78802011 73 struct inet_frag_queue *frag = from_timer(frag, t, timer);
b836c99f
AW
74 struct frag_queue *fq;
75 struct net *net;
76
78802011 77 fq = container_of(frag, struct frag_queue, q);
b836c99f
AW
78 net = container_of(fq->q.net, struct net, ipv6.frags);
79
70b095c8 80 ip6frag_expire_frag_queue(net, fq);
1da177e4
LT
81}
82
fc08c258 83static struct frag_queue *
648700f7 84fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
1da177e4 85{
648700f7
ED
86 struct frag_v6_compare_key key = {
87 .id = id,
88 .saddr = hdr->saddr,
89 .daddr = hdr->daddr,
90 .user = IP6_DEFRAG_LOCAL_DELIVER,
91 .iif = iif,
92 };
c6fda282 93 struct inet_frag_queue *q;
9a375803 94
648700f7
ED
95 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
96 IPV6_ADDR_LINKLOCAL)))
97 key.iif = 0;
1da177e4 98
648700f7 99 q = inet_frag_find(&net->ipv6.frags, &key);
2d44ed22 100 if (!q)
9546377c 101 return NULL;
2d44ed22 102
c6fda282 103 return container_of(q, struct frag_queue, q);
1da177e4
LT
104}
105
f61944ef 106static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
415787d7
ED
107 struct frag_hdr *fhdr, int nhoff,
108 u32 *prob_offset)
1da177e4 109{
adf30907 110 struct net *net = dev_net(skb_dst(skb)->dev);
d4289fcc
PO
111 int offset, end, fragsize;
112 struct sk_buff *prev_tail;
113 struct net_device *dev;
114 int err = -ENOENT;
eec2e618 115 u8 ecn;
1da177e4 116
06aa8b8a 117 if (fq->q.flags & INET_FRAG_COMPLETE)
1da177e4
LT
118 goto err;
119
d4289fcc 120 err = -EINVAL;
1da177e4 121 offset = ntohs(fhdr->frag_off) & ~0x7;
0660e03f
ACM
122 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
123 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1da177e4
LT
124
125 if ((unsigned int)end > IPV6_MAXPLEN) {
415787d7 126 *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
d4289fcc
PO
127 /* note that if prob_offset is set, the skb is freed elsewhere,
128 * we do not free it here.
129 */
f61944ef 130 return -1;
1da177e4
LT
131 }
132
eec2e618
HFS
133 ecn = ip6_frag_ecn(ipv6_hdr(skb));
134
d56f90a7
ACM
135 if (skb->ip_summed == CHECKSUM_COMPLETE) {
136 const unsigned char *nh = skb_network_header(skb);
1ab1457c 137 skb->csum = csum_sub(skb->csum,
d56f90a7
ACM
138 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
139 0));
140 }
1da177e4
LT
141
142 /* Is this the final fragment? */
143 if (!(fhdr->frag_off & htons(IP6_MF))) {
144 /* If we already have some bits beyond end
145 * or have different end, the segment is corrupted.
146 */
5ab11c98 147 if (end < fq->q.len ||
06aa8b8a 148 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
2475f59c 149 goto discard_fq;
06aa8b8a 150 fq->q.flags |= INET_FRAG_LAST_IN;
5ab11c98 151 fq->q.len = end;
1da177e4
LT
152 } else {
153 /* Check if the fragment is rounded to 8 bytes.
154 * Required by the RFC.
155 */
156 if (end & 0x7) {
157 /* RFC2460 says always send parameter problem in
158 * this case. -DaveM
159 */
415787d7 160 *prob_offset = offsetof(struct ipv6hdr, payload_len);
f61944ef 161 return -1;
1da177e4 162 }
5ab11c98 163 if (end > fq->q.len) {
1da177e4 164 /* Some bits beyond end -> corruption. */
06aa8b8a 165 if (fq->q.flags & INET_FRAG_LAST_IN)
2475f59c 166 goto discard_fq;
5ab11c98 167 fq->q.len = end;
1da177e4
LT
168 }
169 }
170
171 if (end == offset)
2475f59c 172 goto discard_fq;
1da177e4 173
d4289fcc 174 err = -ENOMEM;
1da177e4
LT
175 /* Point into the IP datagram 'data' part. */
176 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
2475f59c 177 goto discard_fq;
1ab1457c 178
d4289fcc
PO
179 err = pskb_trim_rcsum(skb, end - offset);
180 if (err)
70789d70 181 goto discard_fq;
1da177e4 182
d4289fcc 183 /* Note : skb->rbnode and skb->dev share the same location. */
219badfa 184 dev = skb->dev;
219badfa
ED
185 /* Makes sure compiler wont do silly aliasing games */
186 barrier();
1da177e4 187
d4289fcc
PO
188 prev_tail = fq->q.fragments_tail;
189 err = inet_frag_queue_insert(&fq->q, skb, offset, end);
190 if (err)
191 goto insert_error;
192
193 if (dev)
194 fq->iif = dev->ifindex;
1da177e4 195
5ab11c98
PE
196 fq->q.stamp = skb->tstamp;
197 fq->q.meat += skb->len;
eec2e618 198 fq->ecn |= ecn;
0e60d245 199 add_frag_mem_limit(fq->q.net, skb->truesize);
1da177e4 200
dbd1759e
WB
201 fragsize = -skb_network_offset(skb) + skb->len;
202 if (fragsize > fq->q.max_size)
203 fq->q.max_size = fragsize;
204
1da177e4
LT
205 /* The first fragment.
206 * nhoffset is obtained from the first fragment, of course.
207 */
208 if (offset == 0) {
209 fq->nhoffset = nhoff;
06aa8b8a 210 fq->q.flags |= INET_FRAG_FIRST_IN;
1da177e4 211 }
f61944ef 212
06aa8b8a 213 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
97599dc7 214 fq->q.meat == fq->q.len) {
97599dc7
ED
215 unsigned long orefdst = skb->_skb_refdst;
216
217 skb->_skb_refdst = 0UL;
d4289fcc 218 err = ip6_frag_reasm(fq, skb, prev_tail, dev);
97599dc7 219 skb->_skb_refdst = orefdst;
d4289fcc 220 return err;
97599dc7 221 }
f61944ef 222
97599dc7 223 skb_dst_drop(skb);
d4289fcc 224 return -EINPROGRESS;
1da177e4 225
d4289fcc
PO
226insert_error:
227 if (err == IPFRAG_DUP) {
228 kfree_skb(skb);
229 return -EINVAL;
230 }
231 err = -EINVAL;
232 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
233 IPSTATS_MIB_REASM_OVERLAPS);
70789d70 234discard_fq:
093ba729 235 inet_frag_kill(&fq->q);
1d015503
ED
236 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
237 IPSTATS_MIB_REASMFAILS);
d4289fcc 238err:
1da177e4 239 kfree_skb(skb);
d4289fcc 240 return err;
1da177e4
LT
241}
242
243/*
244 * Check if this packet is complete.
1da177e4
LT
245 *
246 * It is called with locked fq, and caller must check that
247 * queue is eligible for reassembly i.e. it is not COMPLETE,
248 * the last and the first frames arrived and all the bits are here.
249 */
d4289fcc
PO
250static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
251 struct sk_buff *prev_tail, struct net_device *dev)
1da177e4 252{
2bad35b7 253 struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
1da177e4 254 unsigned int nhoff;
d4289fcc
PO
255 void *reasm_data;
256 int payload_len;
eec2e618 257 u8 ecn;
1da177e4 258
093ba729 259 inet_frag_kill(&fq->q);
1da177e4 260
eec2e618
HFS
261 ecn = ip_frag_ecn_table[fq->ecn];
262 if (unlikely(ecn == 0xff))
263 goto out_fail;
264
d4289fcc
PO
265 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
266 if (!reasm_data)
267 goto out_oom;
1da177e4 268
d4289fcc 269 payload_len = ((skb->data - skb_network_header(skb)) -
5ab11c98 270 sizeof(struct ipv6hdr) + fq->q.len -
d56f90a7 271 sizeof(struct frag_hdr));
1da177e4
LT
272 if (payload_len > IPV6_MAXPLEN)
273 goto out_oversize;
274
1da177e4
LT
275 /* We have to remove fragment header from datagram and to relocate
276 * header in order to calculate ICV correctly. */
277 nhoff = fq->nhoffset;
d4289fcc
PO
278 skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
279 memmove(skb->head + sizeof(struct frag_hdr), skb->head,
280 (skb->data - skb->head) - sizeof(struct frag_hdr));
281 if (skb_mac_header_was_set(skb))
282 skb->mac_header += sizeof(struct frag_hdr);
283 skb->network_header += sizeof(struct frag_hdr);
284
285 skb_reset_transport_header(skb);
286
287 inet_frag_reasm_finish(&fq->q, skb, reasm_data);
1da177e4 288
d4289fcc
PO
289 skb->dev = dev;
290 ipv6_hdr(skb)->payload_len = htons(payload_len);
291 ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
292 IP6CB(skb)->nhoff = nhoff;
293 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
294 IP6CB(skb)->frag_max_size = fq->q.max_size;
1da177e4 295
1da177e4 296 /* Yes, and fold redundant checksum back. 8) */
d4289fcc
PO
297 skb_postpush_rcsum(skb, skb_network_header(skb),
298 skb_network_header_len(skb));
1da177e4 299
a11d206d 300 rcu_read_lock();
1d015503 301 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
a11d206d 302 rcu_read_unlock();
fa0f5273 303 fq->q.rb_fragments = RB_ROOT;
d6bebca9 304 fq->q.fragments_tail = NULL;
d4289fcc 305 fq->q.last_run_head = NULL;
1da177e4
LT
306 return 1;
307
308out_oversize:
e87cc472 309 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
1da177e4
LT
310 goto out_fail;
311out_oom:
e87cc472 312 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
1da177e4 313out_fail:
a11d206d 314 rcu_read_lock();
1d015503 315 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
a11d206d 316 rcu_read_unlock();
2475f59c 317 inet_frag_kill(&fq->q);
1da177e4
LT
318 return -1;
319}
320
e5bbef20 321static int ipv6_frag_rcv(struct sk_buff *skb)
1da177e4 322{
1da177e4
LT
323 struct frag_hdr *fhdr;
324 struct frag_queue *fq;
b71d1d42 325 const struct ipv6hdr *hdr = ipv6_hdr(skb);
adf30907 326 struct net *net = dev_net(skb_dst(skb)->dev);
648700f7 327 int iif;
1da177e4 328
f46078cf
HFS
329 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
330 goto fail_hdr;
331
1d015503 332 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
1da177e4
LT
333
334 /* Jumbo payload inhibits frag. header */
67ba4152 335 if (hdr->payload_len == 0)
98b3377c
DL
336 goto fail_hdr;
337
ea2ae17d 338 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
98b3377c
DL
339 sizeof(struct frag_hdr))))
340 goto fail_hdr;
1da177e4 341
0660e03f 342 hdr = ipv6_hdr(skb);
9c70220b 343 fhdr = (struct frag_hdr *)skb_transport_header(skb);
1da177e4
LT
344
345 if (!(fhdr->frag_off & htons(0xFFF9))) {
346 /* It is not a fragmented frame */
b0e380b1 347 skb->transport_header += sizeof(struct frag_hdr);
1d015503
ED
348 __IP6_INC_STATS(net,
349 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
1da177e4 350
d56f90a7 351 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
f46078cf 352 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
1da177e4
LT
353 return 1;
354 }
355
648700f7
ED
356 iif = skb->dev ? skb->dev->ifindex : 0;
357 fq = fq_find(net, fhdr->identification, hdr, iif);
53b24b8f 358 if (fq) {
415787d7 359 u32 prob_offset = 0;
f61944ef 360 int ret;
1da177e4 361
5ab11c98 362 spin_lock(&fq->q.lock);
1da177e4 363
648700f7 364 fq->iif = iif;
415787d7
ED
365 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
366 &prob_offset);
1da177e4 367
5ab11c98 368 spin_unlock(&fq->q.lock);
093ba729 369 inet_frag_put(&fq->q);
415787d7
ED
370 if (prob_offset) {
371 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
372 IPSTATS_MIB_INHDRERRORS);
d4289fcc 373 /* icmpv6_param_prob() calls kfree_skb(skb) */
415787d7
ED
374 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
375 }
1da177e4
LT
376 return ret;
377 }
378
1d015503 379 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
1da177e4
LT
380 kfree_skb(skb);
381 return -1;
98b3377c
DL
382
383fail_hdr:
bdb7cc64 384 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
1d015503 385 IPSTATS_MIB_INHDRERRORS);
98b3377c
DL
386 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
387 return -1;
1da177e4
LT
388}
389
cc24beca 390static const struct inet6_protocol frag_protocol = {
1da177e4
LT
391 .handler = ipv6_frag_rcv,
392 .flags = INET6_PROTO_NOPOLICY,
393};
394
8d8354d2 395#ifdef CONFIG_SYSCTL
1bab4c75 396
0a64b4b8 397static struct ctl_table ip6_frags_ns_ctl_table[] = {
8d8354d2 398 {
8d8354d2 399 .procname = "ip6frag_high_thresh",
e31e0bdc 400 .data = &init_net.ipv6.frags.high_thresh,
3e67f106 401 .maxlen = sizeof(unsigned long),
8d8354d2 402 .mode = 0644,
3e67f106 403 .proc_handler = proc_doulongvec_minmax,
1bab4c75 404 .extra1 = &init_net.ipv6.frags.low_thresh
8d8354d2
PE
405 },
406 {
8d8354d2 407 .procname = "ip6frag_low_thresh",
e31e0bdc 408 .data = &init_net.ipv6.frags.low_thresh,
3e67f106 409 .maxlen = sizeof(unsigned long),
8d8354d2 410 .mode = 0644,
6e00f7dd 411 .proc_handler = proc_doulongvec_minmax,
1bab4c75 412 .extra2 = &init_net.ipv6.frags.high_thresh
8d8354d2
PE
413 },
414 {
8d8354d2 415 .procname = "ip6frag_time",
b2fd5321 416 .data = &init_net.ipv6.frags.timeout,
8d8354d2
PE
417 .maxlen = sizeof(int),
418 .mode = 0644,
6d9f239a 419 .proc_handler = proc_dointvec_jiffies,
8d8354d2 420 },
7d291ebb
PE
421 { }
422};
423
e3a57d18
FW
424/* secret interval has been deprecated */
425static int ip6_frags_secret_interval_unused;
7d291ebb 426static struct ctl_table ip6_frags_ctl_table[] = {
8d8354d2 427 {
8d8354d2 428 .procname = "ip6frag_secret_interval",
e3a57d18 429 .data = &ip6_frags_secret_interval_unused,
8d8354d2
PE
430 .maxlen = sizeof(int),
431 .mode = 0644,
6d9f239a 432 .proc_handler = proc_dointvec_jiffies,
8d8354d2
PE
433 },
434 { }
435};
436
2c8c1e72 437static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
8d8354d2 438{
e4a2d5c2 439 struct ctl_table *table;
8d8354d2
PE
440 struct ctl_table_header *hdr;
441
0a64b4b8 442 table = ip6_frags_ns_ctl_table;
09ad9bc7 443 if (!net_eq(net, &init_net)) {
0a64b4b8 444 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
63159f29 445 if (!table)
e4a2d5c2
PE
446 goto err_alloc;
447
e31e0bdc 448 table[0].data = &net->ipv6.frags.high_thresh;
1bab4c75 449 table[0].extra1 = &net->ipv6.frags.low_thresh;
e31e0bdc 450 table[1].data = &net->ipv6.frags.low_thresh;
1bab4c75 451 table[1].extra2 = &net->ipv6.frags.high_thresh;
b2fd5321 452 table[2].data = &net->ipv6.frags.timeout;
e4a2d5c2
PE
453 }
454
ec8f23ce 455 hdr = register_net_sysctl(net, "net/ipv6", table);
63159f29 456 if (!hdr)
e4a2d5c2
PE
457 goto err_reg;
458
459 net->ipv6.sysctl.frags_hdr = hdr;
460 return 0;
461
462err_reg:
09ad9bc7 463 if (!net_eq(net, &init_net))
e4a2d5c2
PE
464 kfree(table);
465err_alloc:
466 return -ENOMEM;
467}
468
2c8c1e72 469static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
e4a2d5c2
PE
470{
471 struct ctl_table *table;
472
473 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
474 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
3705e11a
YH
475 if (!net_eq(net, &init_net))
476 kfree(table);
8d8354d2 477}
7d291ebb
PE
478
479static struct ctl_table_header *ip6_ctl_header;
480
481static int ip6_frags_sysctl_register(void)
482{
43444757 483 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
7d291ebb
PE
484 ip6_frags_ctl_table);
485 return ip6_ctl_header == NULL ? -ENOMEM : 0;
486}
487
488static void ip6_frags_sysctl_unregister(void)
489{
490 unregister_net_sysctl_table(ip6_ctl_header);
491}
8d8354d2 492#else
fc08c258 493static int ip6_frags_ns_sysctl_register(struct net *net)
e71e0349 494{
8d8354d2
PE
495 return 0;
496}
e4a2d5c2 497
fc08c258 498static void ip6_frags_ns_sysctl_unregister(struct net *net)
e4a2d5c2
PE
499{
500}
7d291ebb 501
fc08c258 502static int ip6_frags_sysctl_register(void)
7d291ebb
PE
503{
504 return 0;
505}
506
fc08c258 507static void ip6_frags_sysctl_unregister(void)
7d291ebb
PE
508{
509}
8d8354d2 510#endif
7d460db9 511
2c8c1e72 512static int __net_init ipv6_frags_init_net(struct net *net)
8d8354d2 513{
787bea77
ED
514 int res;
515
7c070aa9
SW
516 net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
517 net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
b2fd5321 518 net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
093ba729 519 net->ipv6.frags.f = &ip6_frags;
8d8354d2 520
787bea77
ED
521 res = inet_frags_init_net(&net->ipv6.frags);
522 if (res < 0)
523 return res;
5a63643e 524
787bea77
ED
525 res = ip6_frags_ns_sysctl_register(net);
526 if (res < 0)
093ba729 527 inet_frags_exit_net(&net->ipv6.frags);
787bea77 528 return res;
e71e0349
DL
529}
530
2c8c1e72 531static void __net_exit ipv6_frags_exit_net(struct net *net)
81566e83 532{
0a64b4b8 533 ip6_frags_ns_sysctl_unregister(net);
093ba729 534 inet_frags_exit_net(&net->ipv6.frags);
81566e83
PE
535}
536
537static struct pernet_operations ip6_frags_ops = {
538 .init = ipv6_frags_init_net,
539 .exit = ipv6_frags_exit_net,
540};
541
70b095c8 542static const struct rhashtable_params ip6_rhash_params = {
648700f7 543 .head_offset = offsetof(struct inet_frag_queue, node),
70b095c8
FW
544 .hashfn = ip6frag_key_hashfn,
545 .obj_hashfn = ip6frag_obj_hashfn,
546 .obj_cmpfn = ip6frag_obj_cmpfn,
648700f7
ED
547 .automatic_shrinking = true,
548};
648700f7 549
853cbbaa 550int __init ipv6_frag_init(void)
1da177e4 551{
853cbbaa 552 int ret;
1da177e4 553
70b095c8 554 ip6_frags.constructor = ip6frag_init;
5b975bab
ED
555 ip6_frags.destructor = NULL;
556 ip6_frags.qsize = sizeof(struct frag_queue);
5b975bab
ED
557 ip6_frags.frag_expire = ip6_frag_expire;
558 ip6_frags.frags_cache_name = ip6_frag_cache_name;
648700f7 559 ip6_frags.rhash_params = ip6_rhash_params;
5b975bab 560 ret = inet_frags_init(&ip6_frags);
853cbbaa
DL
561 if (ret)
562 goto out;
e71e0349 563
5b975bab
ED
564 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
565 if (ret)
566 goto err_protocol;
567
7d291ebb
PE
568 ret = ip6_frags_sysctl_register();
569 if (ret)
570 goto err_sysctl;
571
0002c630
PE
572 ret = register_pernet_subsys(&ip6_frags_ops);
573 if (ret)
574 goto err_pernet;
8d8354d2 575
853cbbaa
DL
576out:
577 return ret;
0002c630
PE
578
579err_pernet:
7d291ebb
PE
580 ip6_frags_sysctl_unregister();
581err_sysctl:
0002c630 582 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
5b975bab
ED
583err_protocol:
584 inet_frags_fini(&ip6_frags);
0002c630 585 goto out;
853cbbaa
DL
586}
587
588void ipv6_frag_exit(void)
589{
590 inet_frags_fini(&ip6_frags);
7d291ebb 591 ip6_frags_sysctl_unregister();
81566e83 592 unregister_pernet_subsys(&ip6_frags_ops);
853cbbaa 593 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
1da177e4 594}