blob: 36a3d8dc61f56ce5abe0239da51d0a48550af5d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 fragment reassembly
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090016/*
17 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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 */
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000029
30#define pr_fmt(fmt) "IPv6: " fmt
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
Herbert Xuf61944e2007-10-15 01:28:47 -070046#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040048#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/sock.h>
51#include <net/snmp.h>
52
53#include <net/ipv6.h>
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +090054#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#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>
Florian Westphal70b095c2018-07-14 01:14:01 +020060#include <net/ipv6_frag.h>
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +000061#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020063static const char ip6_frag_cache_name[] = "ip6-frags";
64
Fabian Frederickfc08c252014-10-29 11:38:17 +010065static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +000066{
67 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070070static struct inet_frags ip6_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Herbert Xuf61944e2007-10-15 01:28:47 -070072static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
73 struct net_device *dev);
74
Kees Cook78802012017-10-16 17:29:20 -070075static void ip6_frag_expire(struct timer_list *t)
Amerigo Wangb836c992012-09-18 16:50:09 +000076{
Kees Cook78802012017-10-16 17:29:20 -070077 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Amerigo Wangb836c992012-09-18 16:50:09 +000078 struct frag_queue *fq;
79 struct net *net;
80
Kees Cook78802012017-10-16 17:29:20 -070081 fq = container_of(frag, struct frag_queue, q);
Amerigo Wangb836c992012-09-18 16:50:09 +000082 net = container_of(fq->q.net, struct net, ipv6.frags);
83
Florian Westphal70b095c2018-07-14 01:14:01 +020084 ip6frag_expire_frag_queue(net, fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Fabian Frederickfc08c252014-10-29 11:38:17 +010087static struct frag_queue *
Eric Dumazet648700f2018-03-31 12:58:49 -070088fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Eric Dumazet648700f2018-03-31 12:58:49 -070090 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 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070097 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Eric Dumazet648700f2018-03-31 12:58:49 -070099 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
100 IPV6_ADDR_LINKLOCAL)))
101 key.iif = 0;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700102
Eric Dumazet648700f2018-03-31 12:58:49 -0700103 q = inet_frag_find(&net->ipv6.frags, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700104 if (!q)
Shan Wei95463772010-02-11 00:12:45 +0000105 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700106
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700107 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Herbert Xuf61944e2007-10-15 01:28:47 -0700110static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Eric Dumazet415787d2018-04-17 18:11:44 -0700111 struct frag_hdr *fhdr, int nhoff,
112 u32 *prob_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct sk_buff *prev, *next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700115 struct net_device *dev;
Willem de Bruijndbd17592016-11-02 11:02:18 -0400116 int offset, end, fragsize;
Eric Dumazetadf30902009-06-02 05:19:30 +0000117 struct net *net = dev_net(skb_dst(skb)->dev);
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000118 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200120 if (fq->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto err;
122
123 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700124 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
125 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if ((unsigned int)end > IPV6_MAXPLEN) {
Eric Dumazet415787d2018-04-17 18:11:44 -0700128 *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700129 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000132 ecn = ip6_frag_ecn(ipv6_hdr(skb));
133
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700134 if (skb->ip_summed == CHECKSUM_COMPLETE) {
135 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900136 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700137 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
138 0));
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
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 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700146 if (end < fq->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200147 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700148 goto discard_fq;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200149 fq->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700150 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } 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 */
Eric Dumazet415787d2018-04-17 18:11:44 -0700159 *prob_offset = offsetof(struct ipv6hdr, payload_len);
Herbert Xuf61944e2007-10-15 01:28:47 -0700160 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700162 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200164 if (fq->q.flags & INET_FRAG_LAST_IN)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700165 goto discard_fq;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700166 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 }
169
170 if (end == offset)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700171 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* Point into the IP datagram 'data' part. */
174 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700175 goto discard_fq;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900176
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700177 if (pskb_trim_rcsum(skb, end - offset))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700178 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
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 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000184 prev = fq->q.fragments_tail;
Eric Dumazet219badf2018-03-31 12:58:59 -0700185 if (!prev || prev->ip_defrag_offset < offset) {
Changli Gaod6bebca2010-06-29 04:39:37 +0000186 next = NULL;
187 goto found;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100190 for (next = fq->q.fragments; next != NULL; next = next->next) {
Eric Dumazet219badf2018-03-31 12:58:59 -0700191 if (next->ip_defrag_offset >= offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break; /* bingo! */
193 prev = next;
194 }
195
Changli Gaod6bebca2010-06-29 04:39:37 +0000196found:
Eric Dumazet5de658f2012-01-30 04:29:24 +0000197 /* RFC5722, Section 4, amended by Errata ID : 3089
198 * When reassembling an IPv6 datagram, if
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000199 * one or more its constituent fragments is determined to be an
200 * overlapping fragment, the entire datagram (and any constituent
Eric Dumazet5de658f2012-01-30 04:29:24 +0000201 * fragments) MUST be silently discarded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000204 /* Check for overlap with preceding fragment. */
205 if (prev &&
Eric Dumazet219badf2018-03-31 12:58:59 -0700206 (prev->ip_defrag_offset + prev->len) > offset)
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000207 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000209 /* Look for overlap with succeeding segment. */
Eric Dumazet219badf2018-03-31 12:58:59 -0700210 if (next && next->ip_defrag_offset < end)
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000211 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Su Yanjun7f334a72018-12-29 14:07:55 -0500213 /* Note : skb->ip_defrag_offset and skb->sk share the same location */
Eric Dumazet219badf2018-03-31 12:58:59 -0700214 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Insert this fragment in the chain of fragments. */
222 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000223 if (!next)
224 fq->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (prev)
226 prev->next = skb;
227 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700228 fq->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700230 fq->q.stamp = skb->tstamp;
231 fq->q.meat += skb->len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000232 fq->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200233 add_frag_mem_limit(fq->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Willem de Bruijndbd17592016-11-02 11:02:18 -0400235 fragsize = -skb_network_offset(skb) + skb->len;
236 if (fragsize > fq->q.max_size)
237 fq->q.max_size = fragsize;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* The first fragment.
240 * nhoffset is obtained from the first fragment, of course.
241 */
242 if (offset == 0) {
243 fq->nhoffset = nhoff;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200244 fq->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700246
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200247 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000248 fq->q.meat == fq->q.len) {
249 int res;
250 unsigned long orefdst = skb->_skb_refdst;
Herbert Xuf61944e2007-10-15 01:28:47 -0700251
Eric Dumazet97599dc2013-04-16 12:55:41 +0000252 skb->_skb_refdst = 0UL;
253 res = ip6_frag_reasm(fq, prev, dev);
254 skb->_skb_refdst = orefdst;
255 return res;
256 }
257
258 skb_dst_drop(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000261discard_fq:
Eric Dumazet093ba722018-03-31 12:58:44 -0700262 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263err:
Eric Dumazet1d015502016-04-27 16:44:40 -0700264 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
265 IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 kfree_skb(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700267 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
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 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700279static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct net_device *dev)
281{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700282 struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700283 struct sk_buff *fp, *head = fq->q.fragments;
Jiri Wiesnerebaf39e2018-12-05 16:55:29 +0100284 int payload_len, delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned int nhoff;
Eric Dumazetec164392012-05-19 03:02:35 +0000286 int sum_truesize;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000287 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Eric Dumazet093ba722018-03-31 12:58:44 -0700289 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000291 ecn = ip_frag_ecn_table[fq->ecn];
292 if (unlikely(ecn == 0xff))
293 goto out_fail;
294
Herbert Xuf61944e2007-10-15 01:28:47 -0700295 /* 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;
Changli Gaod6bebca2010-06-29 04:39:37 +0000304 if (!fp->next)
305 fq->q.fragments_tail = fp;
Herbert Xuf61944e2007-10-15 01:28:47 -0700306 prev->next = fp;
307
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700308 skb_morph(head, fq->q.fragments);
309 head->next = fq->q.fragments->next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700310
Eric Dumazet808db802012-04-24 10:17:59 +0000311 consume_skb(fq->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700312 fq->q.fragments = head;
Herbert Xuf61944e2007-10-15 01:28:47 -0700313 }
314
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700315 WARN_ON(head == NULL);
Eric Dumazet219badf2018-03-31 12:58:59 -0700316 WARN_ON(head->ip_defrag_offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700319 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700320 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700321 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (payload_len > IPV6_MAXPLEN)
323 goto out_oversize;
324
Jiri Wiesnerebaf39e2018-12-05 16:55:29 +0100325 delta = - head->truesize;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000328 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto out_oom;
330
Jiri Wiesnerebaf39e2018-12-05 16:55:29 +0100331 delta += head->truesize;
332 if (delta)
333 add_frag_mem_limit(fq->q.net, delta);
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* If the first fragment is fragmented itself, we split
336 * it to two chunks: the first with data and paged part
337 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700338 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct sk_buff *clone;
340 int i, plen = 0;
341
Ian Morrise5d08d72014-11-23 21:28:43 +0000342 clone = alloc_skb(0, GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100343 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto out_oom;
345 clone->next = head->next;
346 head->next = clone;
347 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Miller4d9092b2009-06-09 00:20:05 -0700348 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000349 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
350 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 clone->len = clone->data_len = head->data_len - plen;
352 head->data_len -= clone->len;
353 head->len -= clone->len;
354 clone->csum = 0;
355 clone->ip_summed = head->ip_summed;
Florian Westphal0e60d242015-07-23 12:05:38 +0200356 add_frag_mem_limit(fq->q.net, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 /* We have to remove fragment header from datagram and to relocate
360 * header in order to calculate ICV correctly. */
361 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700362 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900363 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 (head->data - head->head) - sizeof(struct frag_hdr));
Jason A. Donenfeldb678aa52016-10-21 18:28:25 +0900365 if (skb_mac_header_was_set(head))
366 head->mac_header += sizeof(struct frag_hdr);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700367 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300369 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700370 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Eric Dumazetec164392012-05-19 03:02:35 +0000372 sum_truesize = head->truesize;
373 for (fp = head->next; fp;) {
374 bool headstolen;
375 int delta;
376 struct sk_buff *next = fp->next;
377
378 sum_truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (head->ip_summed != fp->ip_summed)
380 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700381 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 head->csum = csum_add(head->csum, fp->csum);
Eric Dumazetec164392012-05-19 03:02:35 +0000383
384 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
385 kfree_skb_partial(fp, headstolen);
386 } else {
Herbert Xud15f5ac2018-12-20 21:20:10 +0800387 fp->sk = NULL;
Eric Dumazetec164392012-05-19 03:02:35 +0000388 if (!skb_shinfo(head)->frag_list)
389 skb_shinfo(head)->frag_list = fp;
390 head->data_len += fp->len;
391 head->len += fp->len;
392 head->truesize += fp->truesize;
393 }
394 fp = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Florian Westphal0e60d242015-07-23 12:05:38 +0200396 sub_frag_mem_limit(fq->q.net, sum_truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David S. Millera8305bf2018-07-29 20:42:53 -0700398 skb_mark_not_on_list(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700400 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700401 ipv6_hdr(head)->payload_len = htons(payload_len);
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000402 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800403 IP6CB(head)->nhoff = nhoff;
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200404 IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
Willem de Bruijndbd17592016-11-02 11:02:18 -0400405 IP6CB(head)->frag_max_size = fq->q.max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Yes, and fold redundant checksum back. 8) */
Daniel Borkmann6b83d282016-02-20 00:29:30 +0100408 skb_postpush_rcsum(head, skb_network_header(head),
409 skb_network_header_len(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900411 rcu_read_lock();
Eric Dumazet1d015502016-04-27 16:44:40 -0700412 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900413 rcu_read_unlock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700414 fq->q.fragments = NULL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000415 fq->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000416 fq->q.fragments_tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 1;
418
419out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000420 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 goto out_fail;
422out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000423 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900425 rcu_read_lock();
Eric Dumazet1d015502016-04-27 16:44:40 -0700426 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900427 rcu_read_unlock();
Peter Oskolkov2475f592018-09-21 11:17:15 -0700428 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -1;
430}
431
Herbert Xue5bbef22007-10-15 12:50:28 -0700432static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct frag_hdr *fhdr;
435 struct frag_queue *fq;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000436 const struct ipv6hdr *hdr = ipv6_hdr(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000437 struct net *net = dev_net(skb_dst(skb)->dev);
Eric Dumazet648700f2018-03-31 12:58:49 -0700438 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200440 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
441 goto fail_hdr;
442
Eric Dumazet1d015502016-04-27 16:44:40 -0700443 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* Jumbo payload inhibits frag. header */
Ian Morris67ba4152014-08-24 21:53:10 +0100446 if (hdr->payload_len == 0)
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700447 goto fail_hdr;
448
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700449 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700450 sizeof(struct frag_hdr))))
451 goto fail_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700453 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700454 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (!(fhdr->frag_off & htons(0xFFF9))) {
457 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700458 skb->transport_header += sizeof(struct frag_hdr);
Eric Dumazet1d015502016-04-27 16:44:40 -0700459 __IP6_INC_STATS(net,
460 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700462 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200463 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 1;
465 }
466
Florian Westphal0ed42292018-08-03 02:22:20 +0200467 if (skb->len - skb_network_offset(skb) < IPV6_MIN_MTU &&
468 fhdr->frag_off & htons(IP6_MF))
469 goto fail_hdr;
470
Eric Dumazet648700f2018-03-31 12:58:49 -0700471 iif = skb->dev ? skb->dev->ifindex : 0;
472 fq = fq_find(net, fhdr->identification, hdr, iif);
Ian Morris53b24b82015-03-29 14:00:05 +0100473 if (fq) {
Eric Dumazet415787d2018-04-17 18:11:44 -0700474 u32 prob_offset = 0;
Herbert Xuf61944e2007-10-15 01:28:47 -0700475 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700477 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Eric Dumazet648700f2018-03-31 12:58:49 -0700479 fq->iif = iif;
Eric Dumazet415787d2018-04-17 18:11:44 -0700480 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
481 &prob_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700483 spin_unlock(&fq->q.lock);
Eric Dumazet093ba722018-03-31 12:58:44 -0700484 inet_frag_put(&fq->q);
Eric Dumazet415787d2018-04-17 18:11:44 -0700485 if (prob_offset) {
486 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
487 IPSTATS_MIB_INHDRERRORS);
488 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return ret;
491 }
492
Eric Dumazet1d015502016-04-27 16:44:40 -0700493 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 kfree_skb(skb);
495 return -1;
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700496
497fail_hdr:
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400498 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
Eric Dumazet1d015502016-04-27 16:44:40 -0700499 IPSTATS_MIB_INHDRERRORS);
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700500 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
501 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Ian Morriscc24bec2014-08-24 21:53:11 +0100504static const struct inet6_protocol frag_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .handler = ipv6_frag_rcv,
506 .flags = INET6_PROTO_NOPOLICY,
507};
508
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800509#ifdef CONFIG_SYSCTL
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200510
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700511static struct ctl_table ip6_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800512 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800513 .procname = "ip6frag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800514 .data = &init_net.ipv6.frags.high_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700515 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800516 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700517 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200518 .extra1 = &init_net.ipv6.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800519 },
520 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800521 .procname = "ip6frag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800522 .data = &init_net.ipv6.frags.low_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700523 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800524 .mode = 0644,
Eric Dumazet6e00f7d2018-04-01 21:57:59 -0700525 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200526 .extra2 = &init_net.ipv6.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800527 },
528 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800529 .procname = "ip6frag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800530 .data = &init_net.ipv6.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800531 .maxlen = sizeof(int),
532 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800533 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800534 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700535 { }
536};
537
Florian Westphale3a57d12014-07-24 16:50:35 +0200538/* secret interval has been deprecated */
539static int ip6_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700540static struct ctl_table ip6_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800541 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800542 .procname = "ip6frag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200543 .data = &ip6_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800544 .maxlen = sizeof(int),
545 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800546 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800547 },
548 { }
549};
Daniel Lezcano7d460db2008-01-18 23:52:35 -0800550
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000551static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800552{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800553 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800554 struct ctl_table_header *hdr;
555
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700556 table = ip6_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800557 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700558 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100559 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800560 goto err_alloc;
561
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800562 table[0].data = &net->ipv6.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200563 table[0].extra1 = &net->ipv6.frags.low_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800564 table[1].data = &net->ipv6.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200565 table[1].extra2 = &net->ipv6.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800566 table[2].data = &net->ipv6.frags.timeout;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800567 }
568
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000569 hdr = register_net_sysctl(net, "net/ipv6", table);
Ian Morris63159f22015-03-29 14:00:04 +0100570 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800571 goto err_reg;
572
573 net->ipv6.sysctl.frags_hdr = hdr;
574 return 0;
575
576err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800577 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800578 kfree(table);
579err_alloc:
580 return -ENOMEM;
581}
582
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000583static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800584{
585 struct ctl_table *table;
586
587 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
588 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
Yang Hongyang3705e112009-12-18 20:25:13 -0800589 if (!net_eq(net, &init_net))
590 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800591}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700592
593static struct ctl_table_header *ip6_ctl_header;
594
595static int ip6_frags_sysctl_register(void)
596{
Eric W. Biederman43444752012-04-19 13:22:55 +0000597 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700598 ip6_frags_ctl_table);
599 return ip6_ctl_header == NULL ? -ENOMEM : 0;
600}
601
602static void ip6_frags_sysctl_unregister(void)
603{
604 unregister_net_sysctl_table(ip6_ctl_header);
605}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800606#else
Fabian Frederickfc08c252014-10-29 11:38:17 +0100607static int ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800608{
609 return 0;
610}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800611
Fabian Frederickfc08c252014-10-29 11:38:17 +0100612static void ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800613{
614}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700615
Fabian Frederickfc08c252014-10-29 11:38:17 +0100616static int ip6_frags_sysctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700617{
618 return 0;
619}
620
Fabian Frederickfc08c252014-10-29 11:38:17 +0100621static void ip6_frags_sysctl_unregister(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700622{
623}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800624#endif
625
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000626static int __net_init ipv6_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800627{
Eric Dumazet787bea72018-03-31 12:58:43 -0700628 int res;
629
Shan Wei7c070aa2010-01-20 10:42:41 +0100630 net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
631 net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800632 net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
Eric Dumazet093ba722018-03-31 12:58:44 -0700633 net->ipv6.frags.f = &ip6_frags;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800634
Eric Dumazet787bea72018-03-31 12:58:43 -0700635 res = inet_frags_init_net(&net->ipv6.frags);
636 if (res < 0)
637 return res;
Jesper Dangaard Brouer5a636432017-09-01 11:26:13 +0200638
Eric Dumazet787bea72018-03-31 12:58:43 -0700639 res = ip6_frags_ns_sysctl_register(net);
640 if (res < 0)
Eric Dumazet093ba722018-03-31 12:58:44 -0700641 inet_frags_exit_net(&net->ipv6.frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700642 return res;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800643}
644
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000645static void __net_exit ipv6_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800646{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700647 ip6_frags_ns_sysctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700648 inet_frags_exit_net(&net->ipv6.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800649}
650
651static struct pernet_operations ip6_frags_ops = {
652 .init = ipv6_frags_init_net,
653 .exit = ipv6_frags_exit_net,
654};
655
Florian Westphal70b095c2018-07-14 01:14:01 +0200656static const struct rhashtable_params ip6_rhash_params = {
Eric Dumazet648700f2018-03-31 12:58:49 -0700657 .head_offset = offsetof(struct inet_frag_queue, node),
Florian Westphal70b095c2018-07-14 01:14:01 +0200658 .hashfn = ip6frag_key_hashfn,
659 .obj_hashfn = ip6frag_obj_hashfn,
660 .obj_cmpfn = ip6frag_obj_cmpfn,
Eric Dumazet648700f2018-03-31 12:58:49 -0700661 .automatic_shrinking = true,
662};
Eric Dumazet648700f2018-03-31 12:58:49 -0700663
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800664int __init ipv6_frag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800666 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Florian Westphal70b095c2018-07-14 01:14:01 +0200668 ip6_frags.constructor = ip6frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700669 ip6_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700670 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700671 ip6_frags.frag_expire = ip6_frag_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200672 ip6_frags.frags_cache_name = ip6_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700673 ip6_frags.rhash_params = ip6_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200674 ret = inet_frags_init(&ip6_frags);
675 if (ret)
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700676 goto out;
677
678 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
679 if (ret)
680 goto err_protocol;
681
682 ret = ip6_frags_sysctl_register();
683 if (ret)
684 goto err_sysctl;
685
686 ret = register_pernet_subsys(&ip6_frags_ops);
687 if (ret)
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200688 goto err_pernet;
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700689
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800690out:
691 return ret;
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700692
693err_pernet:
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700694 ip6_frags_sysctl_unregister();
695err_sysctl:
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700696 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700697err_protocol:
698 inet_frags_fini(&ip6_frags);
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700699 goto out;
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800700}
701
702void ipv6_frag_exit(void)
703{
704 inet_frags_fini(&ip6_frags);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700705 ip6_frags_sysctl_unregister();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800706 unregister_pernet_subsys(&ip6_frags_ops);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800707 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}