blob: 3f488555999e3018cb81edadeb2d60ae4699c4fa [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 Westphal33336cd2019-04-26 08:41:06 -070060#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
Peter Oskolkoveccf76e2019-04-26 08:41:07 -070072static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
73 struct sk_buff *prev_tail, struct net_device *dev);
Herbert Xuf61944e2007-10-15 01:28:47 -070074
Amerigo Wangb836c992012-09-18 16:50:09 +000075static void ip6_frag_expire(unsigned long data)
76{
77 struct frag_queue *fq;
78 struct net *net;
79
80 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
81 net = container_of(fq->q.net, struct net, ipv6.frags);
82
Florian Westphal33336cd2019-04-26 08:41:06 -070083 ip6frag_expire_frag_queue(net, fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Fabian Frederickfc08c252014-10-29 11:38:17 +010086static struct frag_queue *
Eric Dumazet23ce9c52018-10-10 12:29:56 -070087fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Eric Dumazet23ce9c52018-10-10 12:29:56 -070089 struct frag_v6_compare_key key = {
90 .id = id,
91 .saddr = hdr->saddr,
92 .daddr = hdr->daddr,
93 .user = IP6_DEFRAG_LOCAL_DELIVER,
94 .iif = iif,
95 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070096 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Eric Dumazet23ce9c52018-10-10 12:29:56 -070098 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
99 IPV6_ADDR_LINKLOCAL)))
100 key.iif = 0;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700101
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700102 q = inet_frag_find(&net->ipv6.frags, &key);
Eric Dumazet965e2ad2018-10-10 12:29:59 -0700103 if (!q)
Shan Wei95463772010-02-11 00:12:45 +0000104 return NULL;
Eric Dumazet965e2ad2018-10-10 12:29:59 -0700105
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700106 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Herbert Xuf61944e2007-10-15 01:28:47 -0700109static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Eric Dumazet6a063692019-04-26 08:41:04 -0700110 struct frag_hdr *fhdr, int nhoff,
111 u32 *prob_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Eric Dumazetadf30902009-06-02 05:19:30 +0000113 struct net *net = dev_net(skb_dst(skb)->dev);
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700114 int offset, end, fragsize;
115 struct sk_buff *prev_tail;
116 struct net_device *dev;
117 int err = -ENOENT;
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
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700123 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700125 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
126 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if ((unsigned int)end > IPV6_MAXPLEN) {
Eric Dumazet6a063692019-04-26 08:41:04 -0700129 *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700130 /* note that if prob_offset is set, the skb is freed elsewhere,
131 * we do not free it here.
132 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700133 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000136 ecn = ip6_frag_ecn(ipv6_hdr(skb));
137
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700138 if (skb->ip_summed == CHECKSUM_COMPLETE) {
139 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900140 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700141 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
142 0));
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* Is this the final fragment? */
146 if (!(fhdr->frag_off & htons(IP6_MF))) {
147 /* If we already have some bits beyond end
148 * or have different end, the segment is corrupted.
149 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700150 if (end < fq->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200151 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700152 goto discard_fq;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200153 fq->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700154 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
156 /* Check if the fragment is rounded to 8 bytes.
157 * Required by the RFC.
158 */
159 if (end & 0x7) {
160 /* RFC2460 says always send parameter problem in
161 * this case. -DaveM
162 */
Eric Dumazet6a063692019-04-26 08:41:04 -0700163 *prob_offset = offsetof(struct ipv6hdr, payload_len);
Herbert Xuf61944e2007-10-15 01:28:47 -0700164 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700166 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200168 if (fq->q.flags & INET_FRAG_LAST_IN)
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700169 goto discard_fq;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700170 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 }
173
174 if (end == offset)
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700175 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700177 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Point into the IP datagram 'data' part. */
179 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000180 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700182 err = pskb_trim_rcsum(skb, end - offset);
183 if (err)
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000184 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700186 /* Note : skb->rbnode and skb->dev share the same location. */
Herbert Xuf61944e2007-10-15 01:28:47 -0700187 dev = skb->dev;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700188 /* Makes sure compiler wont do silly aliasing games */
189 barrier();
190
191 prev_tail = fq->q.fragments_tail;
192 err = inet_frag_queue_insert(&fq->q, skb, offset, end);
193 if (err)
194 goto insert_error;
195
196 if (dev)
Herbert Xuf61944e2007-10-15 01:28:47 -0700197 fq->iif = dev->ifindex;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700198
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700199 fq->q.stamp = skb->tstamp;
200 fq->q.meat += skb->len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000201 fq->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200202 add_frag_mem_limit(fq->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700204 fragsize = -skb_network_offset(skb) + skb->len;
205 if (fragsize > fq->q.max_size)
206 fq->q.max_size = fragsize;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* The first fragment.
209 * nhoffset is obtained from the first fragment, of course.
210 */
211 if (offset == 0) {
212 fq->nhoffset = nhoff;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200213 fq->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700215
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200216 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000217 fq->q.meat == fq->q.len) {
Eric Dumazet97599dc2013-04-16 12:55:41 +0000218 unsigned long orefdst = skb->_skb_refdst;
Herbert Xuf61944e2007-10-15 01:28:47 -0700219
Eric Dumazet97599dc2013-04-16 12:55:41 +0000220 skb->_skb_refdst = 0UL;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700221 err = ip6_frag_reasm(fq, skb, prev_tail, dev);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000222 skb->_skb_refdst = orefdst;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700223 return err;
Eric Dumazet97599dc2013-04-16 12:55:41 +0000224 }
225
226 skb_dst_drop(skb);
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700227 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700229insert_error:
230 if (err == IPFRAG_DUP) {
231 kfree_skb(skb);
232 return -EINVAL;
233 }
234 err = -EINVAL;
235 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
236 IPSTATS_MIB_REASM_OVERLAPS);
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000237discard_fq:
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700238 inet_frag_kill(&fq->q);
Eric Dumazet1d015502016-04-27 16:44:40 -0700239 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
240 IPSTATS_MIB_REASMFAILS);
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700241err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 kfree_skb(skb);
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
247 * Check if this packet is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *
249 * It is called with locked fq, and caller must check that
250 * queue is eligible for reassembly i.e. it is not COMPLETE,
251 * the last and the first frames arrived and all the bits are here.
252 */
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700253static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
254 struct sk_buff *prev_tail, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700256 struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned int nhoff;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700258 void *reasm_data;
259 int payload_len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000260 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700262 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000264 ecn = ip_frag_ecn_table[fq->ecn];
265 if (unlikely(ecn == 0xff))
266 goto out_fail;
267
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700268 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
269 if (!reasm_data)
270 goto out_oom;
Herbert Xuf61944e2007-10-15 01:28:47 -0700271
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700272 payload_len = ((skb->data - skb_network_header(skb)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700273 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700274 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (payload_len > IPV6_MAXPLEN)
276 goto out_oversize;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* We have to remove fragment header from datagram and to relocate
279 * header in order to calculate ICV correctly. */
280 nhoff = fq->nhoffset;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700281 skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
282 memmove(skb->head + sizeof(struct frag_hdr), skb->head,
283 (skb->data - skb->head) - sizeof(struct frag_hdr));
284 if (skb_mac_header_was_set(skb))
285 skb->mac_header += sizeof(struct frag_hdr);
286 skb->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700288 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700290 inet_frag_reasm_finish(&fq->q, skb, reasm_data);
Eric Dumazetec164392012-05-19 03:02:35 +0000291
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700292 skb->dev = dev;
293 ipv6_hdr(skb)->payload_len = htons(payload_len);
294 ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
295 IP6CB(skb)->nhoff = nhoff;
296 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
297 IP6CB(skb)->frag_max_size = fq->q.max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* Yes, and fold redundant checksum back. 8) */
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700300 skb_postpush_rcsum(skb, skb_network_header(skb),
301 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900303 rcu_read_lock();
Eric Dumazet1d015502016-04-27 16:44:40 -0700304 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900305 rcu_read_unlock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700306 fq->q.fragments = NULL;
Peter Oskolkov10043952018-10-10 12:30:13 -0700307 fq->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000308 fq->q.fragments_tail = NULL;
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700309 fq->q.last_run_head = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 1;
311
312out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000313 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto out_fail;
315out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000316 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900318 rcu_read_lock();
Eric Dumazet1d015502016-04-27 16:44:40 -0700319 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900320 rcu_read_unlock();
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700321 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -1;
323}
324
Herbert Xue5bbef22007-10-15 12:50:28 -0700325static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct frag_hdr *fhdr;
328 struct frag_queue *fq;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000329 const struct ipv6hdr *hdr = ipv6_hdr(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000330 struct net *net = dev_net(skb_dst(skb)->dev);
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700331 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200333 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
334 goto fail_hdr;
335
Eric Dumazet1d015502016-04-27 16:44:40 -0700336 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Jumbo payload inhibits frag. header */
Ian Morris67ba4152014-08-24 21:53:10 +0100339 if (hdr->payload_len == 0)
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700340 goto fail_hdr;
341
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700342 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700343 sizeof(struct frag_hdr))))
344 goto fail_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700346 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700347 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!(fhdr->frag_off & htons(0xFFF9))) {
350 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700351 skb->transport_header += sizeof(struct frag_hdr);
Eric Dumazet1d015502016-04-27 16:44:40 -0700352 __IP6_INC_STATS(net,
353 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700355 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200356 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 1;
358 }
359
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700360 iif = skb->dev ? skb->dev->ifindex : 0;
361 fq = fq_find(net, fhdr->identification, hdr, iif);
Ian Morris53b24b82015-03-29 14:00:05 +0100362 if (fq) {
Eric Dumazet6a063692019-04-26 08:41:04 -0700363 u32 prob_offset = 0;
Herbert Xuf61944e2007-10-15 01:28:47 -0700364 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700366 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700368 fq->iif = iif;
Eric Dumazet6a063692019-04-26 08:41:04 -0700369 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
370 &prob_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700372 spin_unlock(&fq->q.lock);
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700373 inet_frag_put(&fq->q);
Eric Dumazet6a063692019-04-26 08:41:04 -0700374 if (prob_offset) {
375 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
376 IPSTATS_MIB_INHDRERRORS);
Peter Oskolkoveccf76e2019-04-26 08:41:07 -0700377 /* icmpv6_param_prob() calls kfree_skb(skb) */
Eric Dumazet6a063692019-04-26 08:41:04 -0700378 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return ret;
381 }
382
Eric Dumazet1d015502016-04-27 16:44:40 -0700383 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kfree_skb(skb);
385 return -1;
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700386
387fail_hdr:
Eric Dumazet1d015502016-04-27 16:44:40 -0700388 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
389 IPSTATS_MIB_INHDRERRORS);
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700390 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
391 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Ian Morriscc24bec2014-08-24 21:53:11 +0100394static const struct inet6_protocol frag_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .handler = ipv6_frag_rcv,
396 .flags = INET6_PROTO_NOPOLICY,
397};
398
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800399#ifdef CONFIG_SYSCTL
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200400
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700401static struct ctl_table ip6_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800402 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800403 .procname = "ip6frag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800404 .data = &init_net.ipv6.frags.high_thresh,
Eric Dumazet7f617062018-10-10 12:30:00 -0700405 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800406 .mode = 0644,
Eric Dumazet7f617062018-10-10 12:30:00 -0700407 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200408 .extra1 = &init_net.ipv6.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800409 },
410 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800411 .procname = "ip6frag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800412 .data = &init_net.ipv6.frags.low_thresh,
Eric Dumazet7f617062018-10-10 12:30:00 -0700413 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800414 .mode = 0644,
Eric Dumazetd8384862018-10-10 12:30:06 -0700415 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200416 .extra2 = &init_net.ipv6.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800417 },
418 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800419 .procname = "ip6frag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800420 .data = &init_net.ipv6.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800421 .maxlen = sizeof(int),
422 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800423 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800424 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700425 { }
426};
427
Florian Westphale3a57d12014-07-24 16:50:35 +0200428/* secret interval has been deprecated */
429static int ip6_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700430static struct ctl_table ip6_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800431 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800432 .procname = "ip6frag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200433 .data = &ip6_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800434 .maxlen = sizeof(int),
435 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800436 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800437 },
438 { }
439};
Daniel Lezcano7d460db2008-01-18 23:52:35 -0800440
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000441static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800442{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800443 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800444 struct ctl_table_header *hdr;
445
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700446 table = ip6_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800447 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700448 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100449 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800450 goto err_alloc;
451
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800452 table[0].data = &net->ipv6.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200453 table[0].extra1 = &net->ipv6.frags.low_thresh;
454 table[0].extra2 = &init_net.ipv6.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800455 table[1].data = &net->ipv6.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200456 table[1].extra2 = &net->ipv6.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800457 table[2].data = &net->ipv6.frags.timeout;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800458 }
459
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000460 hdr = register_net_sysctl(net, "net/ipv6", table);
Ian Morris63159f22015-03-29 14:00:04 +0100461 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800462 goto err_reg;
463
464 net->ipv6.sysctl.frags_hdr = hdr;
465 return 0;
466
467err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800468 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800469 kfree(table);
470err_alloc:
471 return -ENOMEM;
472}
473
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000474static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800475{
476 struct ctl_table *table;
477
478 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
479 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
Yang Hongyang3705e112009-12-18 20:25:13 -0800480 if (!net_eq(net, &init_net))
481 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800482}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700483
484static struct ctl_table_header *ip6_ctl_header;
485
486static int ip6_frags_sysctl_register(void)
487{
Eric W. Biederman43444752012-04-19 13:22:55 +0000488 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700489 ip6_frags_ctl_table);
490 return ip6_ctl_header == NULL ? -ENOMEM : 0;
491}
492
493static void ip6_frags_sysctl_unregister(void)
494{
495 unregister_net_sysctl_table(ip6_ctl_header);
496}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800497#else
Fabian Frederickfc08c252014-10-29 11:38:17 +0100498static int ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800499{
500 return 0;
501}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800502
Fabian Frederickfc08c252014-10-29 11:38:17 +0100503static void ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800504{
505}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700506
Fabian Frederickfc08c252014-10-29 11:38:17 +0100507static int ip6_frags_sysctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700508{
509 return 0;
510}
511
Fabian Frederickfc08c252014-10-29 11:38:17 +0100512static void ip6_frags_sysctl_unregister(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700513{
514}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800515#endif
516
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000517static int __net_init ipv6_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800518{
Eric Dumazet7fca7712018-10-10 12:29:49 -0700519 int res;
520
Shan Wei7c070aa2010-01-20 10:42:41 +0100521 net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
522 net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800523 net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700524 net->ipv6.frags.f = &ip6_frags;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800525
Eric Dumazet7fca7712018-10-10 12:29:49 -0700526 res = inet_frags_init_net(&net->ipv6.frags);
527 if (res < 0)
528 return res;
Jesper Dangaard Brouer1bcf1872017-09-01 11:26:13 +0200529
Eric Dumazet7fca7712018-10-10 12:29:49 -0700530 res = ip6_frags_ns_sysctl_register(net);
531 if (res < 0)
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700532 inet_frags_exit_net(&net->ipv6.frags);
Eric Dumazet7fca7712018-10-10 12:29:49 -0700533 return res;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800534}
535
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000536static void __net_exit ipv6_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800537{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700538 ip6_frags_ns_sysctl_unregister(net);
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700539 inet_frags_exit_net(&net->ipv6.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800540}
541
542static struct pernet_operations ip6_frags_ops = {
543 .init = ipv6_frags_init_net,
544 .exit = ipv6_frags_exit_net,
545};
546
Florian Westphal33336cd2019-04-26 08:41:06 -0700547static const struct rhashtable_params ip6_rhash_params = {
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700548 .head_offset = offsetof(struct inet_frag_queue, node),
Florian Westphal33336cd2019-04-26 08:41:06 -0700549 .hashfn = ip6frag_key_hashfn,
550 .obj_hashfn = ip6frag_obj_hashfn,
551 .obj_cmpfn = ip6frag_obj_cmpfn,
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700552 .automatic_shrinking = true,
553};
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700554
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800555int __init ipv6_frag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800557 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Florian Westphal33336cd2019-04-26 08:41:06 -0700559 ip6_frags.constructor = ip6frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700560 ip6_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700561 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700562 ip6_frags.frag_expire = ip6_frag_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200563 ip6_frags.frags_cache_name = ip6_frag_cache_name;
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700564 ip6_frags.rhash_params = ip6_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200565 ret = inet_frags_init(&ip6_frags);
566 if (ret)
Eric Dumazetdae73e72018-10-10 12:29:52 -0700567 goto out;
568
569 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
570 if (ret)
571 goto err_protocol;
572
573 ret = ip6_frags_sysctl_register();
574 if (ret)
575 goto err_sysctl;
576
577 ret = register_pernet_subsys(&ip6_frags_ops);
578 if (ret)
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200579 goto err_pernet;
Eric Dumazetdae73e72018-10-10 12:29:52 -0700580
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800581out:
582 return ret;
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700583
584err_pernet:
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700585 ip6_frags_sysctl_unregister();
586err_sysctl:
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700587 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Eric Dumazetdae73e72018-10-10 12:29:52 -0700588err_protocol:
589 inet_frags_fini(&ip6_frags);
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700590 goto out;
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800591}
592
593void ipv6_frag_exit(void)
594{
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700595 ip6_frags_sysctl_unregister();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800596 unregister_pernet_subsys(&ip6_frags_ops);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800597 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Eric Dumazet8ae8a9f2019-05-27 16:56:48 -0700598 inet_frags_fini(&ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}