blob: 28e44782c94d1e0485c9d7c685179716ab252c22 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IPv6 fragment reassembly
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Based on: net/ipv4/ip_fragment.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090012/*
13 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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 */
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000025
26#define pr_fmt(fmt) "IPv6: " fmt
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Herbert Xuf61944e2007-10-15 01:28:47 -070042#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040044#include <linux/export.h>
Hangbin Liu2efdaaa2020-10-27 20:33:13 +080045#include <linux/tcp.h>
46#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/sock.h>
49#include <net/snmp.h>
50
51#include <net/ipv6.h>
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +090052#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <net/protocol.h>
54#include <net/transp_v6.h>
55#include <net/rawv6.h>
56#include <net/ndisc.h>
57#include <net/addrconf.h>
Florian Westphal70b095c2018-07-14 01:14:01 +020058#include <net/ipv6_frag.h>
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +000059#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020061static const char ip6_frag_cache_name[] = "ip6-frags";
62
Fabian Frederickfc08c252014-10-29 11:38:17 +010063static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +000064{
65 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070068static struct inet_frags ip6_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Peter Oskolkovd4289fc2019-01-22 10:02:51 -080070static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
71 struct sk_buff *prev_tail, struct net_device *dev);
Herbert Xuf61944e2007-10-15 01:28:47 -070072
Kees Cook78802012017-10-16 17:29:20 -070073static void ip6_frag_expire(struct timer_list *t)
Amerigo Wangb836c992012-09-18 16:50:09 +000074{
Kees Cook78802012017-10-16 17:29:20 -070075 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Amerigo Wangb836c992012-09-18 16:50:09 +000076 struct frag_queue *fq;
Amerigo Wangb836c992012-09-18 16:50:09 +000077
Kees Cook78802012017-10-16 17:29:20 -070078 fq = container_of(frag, struct frag_queue, q);
Amerigo Wangb836c992012-09-18 16:50:09 +000079
Eric Dumazeta39aca62019-05-24 09:03:38 -070080 ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Fabian Frederickfc08c252014-10-29 11:38:17 +010083static struct frag_queue *
Eric Dumazet648700f2018-03-31 12:58:49 -070084fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Eric Dumazet648700f2018-03-31 12:58:49 -070086 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 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070093 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Eric Dumazet648700f2018-03-31 12:58:49 -070095 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
96 IPV6_ADDR_LINKLOCAL)))
97 key.iif = 0;
Pavel Emelyanov9a375802008-06-27 20:06:08 -070098
Eric Dumazet4907abc2019-05-24 09:03:39 -070099 q = inet_frag_find(net->ipv6.fqdir, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700100 if (!q)
Shan Wei95463772010-02-11 00:12:45 +0000101 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700102
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700103 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Herbert Xuf61944e2007-10-15 01:28:47 -0700106static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Eric Dumazet415787d2018-04-17 18:11:44 -0700107 struct frag_hdr *fhdr, int nhoff,
108 u32 *prob_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Eric Dumazetadf30902009-06-02 05:19:30 +0000110 struct net *net = dev_net(skb_dst(skb)->dev);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800111 int offset, end, fragsize;
112 struct sk_buff *prev_tail;
113 struct net_device *dev;
114 int err = -ENOENT;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000115 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200117 if (fq->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 goto err;
119
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800120 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700122 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
123 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 if ((unsigned int)end > IPV6_MAXPLEN) {
Eric Dumazet415787d2018-04-17 18:11:44 -0700126 *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800127 /* note that if prob_offset is set, the skb is freed elsewhere,
128 * we do not free it here.
129 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700130 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000133 ecn = ip6_frag_ecn(ipv6_hdr(skb));
134
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700135 if (skb->ip_summed == CHECKSUM_COMPLETE) {
136 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900137 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700138 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
139 0));
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
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 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700147 if (end < fq->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200148 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700149 goto discard_fq;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200150 fq->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700151 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 } 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 */
Eric Dumazet415787d2018-04-17 18:11:44 -0700160 *prob_offset = offsetof(struct ipv6hdr, payload_len);
Herbert Xuf61944e2007-10-15 01:28:47 -0700161 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700163 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200165 if (fq->q.flags & INET_FRAG_LAST_IN)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700166 goto discard_fq;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700167 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 }
170
171 if (end == offset)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700172 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800174 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Point into the IP datagram 'data' part. */
176 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700177 goto discard_fq;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900178
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800179 err = pskb_trim_rcsum(skb, end - offset);
180 if (err)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700181 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800183 /* Note : skb->rbnode and skb->dev share the same location. */
Eric Dumazet219badf2018-03-31 12:58:59 -0700184 dev = skb->dev;
Eric Dumazet219badf2018-03-31 12:58:59 -0700185 /* Makes sure compiler wont do silly aliasing games */
186 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800188 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700196 fq->q.stamp = skb->tstamp;
197 fq->q.meat += skb->len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000198 fq->ecn |= ecn;
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700199 add_frag_mem_limit(fq->q.fqdir, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Willem de Bruijndbd17592016-11-02 11:02:18 -0400201 fragsize = -skb_network_offset(skb) + skb->len;
202 if (fragsize > fq->q.max_size)
203 fq->q.max_size = fragsize;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* The first fragment.
206 * nhoffset is obtained from the first fragment, of course.
207 */
208 if (offset == 0) {
209 fq->nhoffset = nhoff;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200210 fq->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700212
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200213 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000214 fq->q.meat == fq->q.len) {
Eric Dumazet97599dc2013-04-16 12:55:41 +0000215 unsigned long orefdst = skb->_skb_refdst;
Herbert Xuf61944e2007-10-15 01:28:47 -0700216
Eric Dumazet97599dc2013-04-16 12:55:41 +0000217 skb->_skb_refdst = 0UL;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800218 err = ip6_frag_reasm(fq, skb, prev_tail, dev);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000219 skb->_skb_refdst = orefdst;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800220 return err;
Eric Dumazet97599dc2013-04-16 12:55:41 +0000221 }
222
223 skb_dst_drop(skb);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800224 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800226insert_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);
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000234discard_fq:
Eric Dumazet093ba722018-03-31 12:58:44 -0700235 inet_frag_kill(&fq->q);
Eric Dumazet1d015502016-04-27 16:44:40 -0700236 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
237 IPSTATS_MIB_REASMFAILS);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800238err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kfree_skb(skb);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/*
244 * Check if this packet is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
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 */
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800250static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
251 struct sk_buff *prev_tail, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Eric Dumazeta39aca62019-05-24 09:03:38 -0700253 struct net *net = fq->q.fqdir->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int nhoff;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800255 void *reasm_data;
256 int payload_len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000257 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Eric Dumazet093ba722018-03-31 12:58:44 -0700259 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000261 ecn = ip_frag_ecn_table[fq->ecn];
262 if (unlikely(ecn == 0xff))
263 goto out_fail;
264
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800265 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
266 if (!reasm_data)
267 goto out_oom;
Herbert Xuf61944e2007-10-15 01:28:47 -0700268
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800269 payload_len = ((skb->data - skb_network_header(skb)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700270 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700271 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (payload_len > IPV6_MAXPLEN)
273 goto out_oversize;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* We have to remove fragment header from datagram and to relocate
276 * header in order to calculate ICV correctly. */
277 nhoff = fq->nhoffset;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800278 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800285 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Guillaume Nault891584f2019-08-02 17:15:03 +0200287 inet_frag_reasm_finish(&fq->q, skb, reasm_data, true);
Eric Dumazetec164392012-05-19 03:02:35 +0000288
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800289 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Yes, and fold redundant checksum back. 8) */
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800297 skb_postpush_rcsum(skb, skb_network_header(skb),
298 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900300 rcu_read_lock();
Stephen Suryaputrae1ae5c22019-06-10 10:32:50 -0400301 __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMOKS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900302 rcu_read_unlock();
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000303 fq->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000304 fq->q.fragments_tail = NULL;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800305 fq->q.last_run_head = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 1;
307
308out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000309 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto out_fail;
311out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000312 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900314 rcu_read_lock();
Stephen Suryaputrae1ae5c22019-06-10 10:32:50 -0400315 __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMFAILS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900316 rcu_read_unlock();
Peter Oskolkov2475f592018-09-21 11:17:15 -0700317 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -1;
319}
320
Herbert Xue5bbef22007-10-15 12:50:28 -0700321static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct frag_hdr *fhdr;
324 struct frag_queue *fq;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000325 const struct ipv6hdr *hdr = ipv6_hdr(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000326 struct net *net = dev_net(skb_dst(skb)->dev);
Hangbin Liu2efdaaa2020-10-27 20:33:13 +0800327 u8 nexthdr;
Georg Kohmann9d9e9372020-11-11 12:50:25 +0100328 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200330 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
331 goto fail_hdr;
332
Eric Dumazet1d015502016-04-27 16:44:40 -0700333 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Jumbo payload inhibits frag. header */
Ian Morris67ba4152014-08-24 21:53:10 +0100336 if (hdr->payload_len == 0)
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700337 goto fail_hdr;
338
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700339 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700340 sizeof(struct frag_hdr))))
341 goto fail_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700343 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700344 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Francesco Ruggerie29f0112021-05-21 13:21:14 -0700346 if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700348 skb->transport_header += sizeof(struct frag_hdr);
Eric Dumazet1d015502016-04-27 16:44:40 -0700349 __IP6_INC_STATS(net,
350 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700352 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200353 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
Francesco Ruggerie29f0112021-05-21 13:21:14 -0700354 IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) +
355 sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 1;
357 }
358
Hangbin Liu2efdaaa2020-10-27 20:33:13 +0800359 /* RFC 8200, Section 4.5 Fragment Header:
360 * If the first fragment does not include all headers through an
361 * Upper-Layer header, then that fragment should be discarded and
362 * an ICMP Parameter Problem, Code 3, message should be sent to
363 * the source of the fragment, with the Pointer field set to zero.
364 */
365 nexthdr = hdr->nexthdr;
Georg Kohmann2d8f6482020-11-19 10:58:33 +0100366 if (ipv6frag_thdr_truncated(skb, skb_transport_offset(skb), &nexthdr)) {
Georg Kohmann9d9e9372020-11-11 12:50:25 +0100367 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
368 IPSTATS_MIB_INHDRERRORS);
369 icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0);
370 return -1;
Hangbin Liu2efdaaa2020-10-27 20:33:13 +0800371 }
372
Eric Dumazet648700f2018-03-31 12:58:49 -0700373 iif = skb->dev ? skb->dev->ifindex : 0;
374 fq = fq_find(net, fhdr->identification, hdr, iif);
Ian Morris53b24b82015-03-29 14:00:05 +0100375 if (fq) {
Eric Dumazet415787d2018-04-17 18:11:44 -0700376 u32 prob_offset = 0;
Herbert Xuf61944e2007-10-15 01:28:47 -0700377 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700379 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Eric Dumazet648700f2018-03-31 12:58:49 -0700381 fq->iif = iif;
Eric Dumazet415787d2018-04-17 18:11:44 -0700382 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
383 &prob_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700385 spin_unlock(&fq->q.lock);
Eric Dumazet093ba722018-03-31 12:58:44 -0700386 inet_frag_put(&fq->q);
Eric Dumazet415787d2018-04-17 18:11:44 -0700387 if (prob_offset) {
388 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
389 IPSTATS_MIB_INHDRERRORS);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800390 /* icmpv6_param_prob() calls kfree_skb(skb) */
Eric Dumazet415787d2018-04-17 18:11:44 -0700391 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return ret;
394 }
395
Eric Dumazet1d015502016-04-27 16:44:40 -0700396 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 kfree_skb(skb);
398 return -1;
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700399
400fail_hdr:
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400401 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
Eric Dumazet1d015502016-04-27 16:44:40 -0700402 IPSTATS_MIB_INHDRERRORS);
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700403 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
404 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Ian Morriscc24bec2014-08-24 21:53:11 +0100407static const struct inet6_protocol frag_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 .handler = ipv6_frag_rcv,
409 .flags = INET6_PROTO_NOPOLICY,
410};
411
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800412#ifdef CONFIG_SYSCTL
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200413
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700414static struct ctl_table ip6_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800415 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800416 .procname = "ip6frag_high_thresh",
Eric Dumazet3e67f102018-03-31 12:58:53 -0700417 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800418 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700419 .proc_handler = proc_doulongvec_minmax,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800420 },
421 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800422 .procname = "ip6frag_low_thresh",
Eric Dumazet3e67f102018-03-31 12:58:53 -0700423 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800424 .mode = 0644,
Eric Dumazet6e00f7d2018-04-01 21:57:59 -0700425 .proc_handler = proc_doulongvec_minmax,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800426 },
427 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800428 .procname = "ip6frag_time",
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800429 .maxlen = sizeof(int),
430 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800431 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800432 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700433 { }
434};
435
Florian Westphale3a57d12014-07-24 16:50:35 +0200436/* secret interval has been deprecated */
437static int ip6_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700438static struct ctl_table ip6_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800439 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800440 .procname = "ip6frag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200441 .data = &ip6_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800442 .maxlen = sizeof(int),
443 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800444 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800445 },
446 { }
447};
Daniel Lezcano7d460db2008-01-18 23:52:35 -0800448
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000449static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800450{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800451 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800452 struct ctl_table_header *hdr;
453
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700454 table = ip6_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800455 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700456 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100457 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800458 goto err_alloc;
459
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800460 }
Eric Dumazet4907abc2019-05-24 09:03:39 -0700461 table[0].data = &net->ipv6.fqdir->high_thresh;
462 table[0].extra1 = &net->ipv6.fqdir->low_thresh;
463 table[1].data = &net->ipv6.fqdir->low_thresh;
464 table[1].extra2 = &net->ipv6.fqdir->high_thresh;
465 table[2].data = &net->ipv6.fqdir->timeout;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800466
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000467 hdr = register_net_sysctl(net, "net/ipv6", table);
Ian Morris63159f22015-03-29 14:00:04 +0100468 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800469 goto err_reg;
470
471 net->ipv6.sysctl.frags_hdr = hdr;
472 return 0;
473
474err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800475 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800476 kfree(table);
477err_alloc:
478 return -ENOMEM;
479}
480
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000481static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800482{
483 struct ctl_table *table;
484
485 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
486 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
Yang Hongyang3705e112009-12-18 20:25:13 -0800487 if (!net_eq(net, &init_net))
488 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800489}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700490
491static struct ctl_table_header *ip6_ctl_header;
492
493static int ip6_frags_sysctl_register(void)
494{
Eric W. Biederman43444752012-04-19 13:22:55 +0000495 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700496 ip6_frags_ctl_table);
497 return ip6_ctl_header == NULL ? -ENOMEM : 0;
498}
499
500static void ip6_frags_sysctl_unregister(void)
501{
502 unregister_net_sysctl_table(ip6_ctl_header);
503}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800504#else
Fabian Frederickfc08c252014-10-29 11:38:17 +0100505static int ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800506{
507 return 0;
508}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800509
Fabian Frederickfc08c252014-10-29 11:38:17 +0100510static void ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800511{
512}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700513
Fabian Frederickfc08c252014-10-29 11:38:17 +0100514static int ip6_frags_sysctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700515{
516 return 0;
517}
518
Fabian Frederickfc08c252014-10-29 11:38:17 +0100519static void ip6_frags_sysctl_unregister(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700520{
521}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800522#endif
523
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000524static int __net_init ipv6_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800525{
Eric Dumazet787bea72018-03-31 12:58:43 -0700526 int res;
527
Eric Dumazeta39aca62019-05-24 09:03:38 -0700528 res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
Eric Dumazet787bea72018-03-31 12:58:43 -0700529 if (res < 0)
530 return res;
Jesper Dangaard Brouer5a636432017-09-01 11:26:13 +0200531
Eric Dumazet4907abc2019-05-24 09:03:39 -0700532 net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
533 net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
534 net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
535
Eric Dumazet787bea72018-03-31 12:58:43 -0700536 res = ip6_frags_ns_sysctl_register(net);
537 if (res < 0)
Eric Dumazet4907abc2019-05-24 09:03:39 -0700538 fqdir_exit(net->ipv6.fqdir);
Eric Dumazet787bea72018-03-31 12:58:43 -0700539 return res;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800540}
541
Eric Dumazetd5dd8872019-06-18 11:09:00 -0700542static void __net_exit ipv6_frags_pre_exit_net(struct net *net)
543{
544 fqdir_pre_exit(net->ipv6.fqdir);
545}
546
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000547static void __net_exit ipv6_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800548{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700549 ip6_frags_ns_sysctl_unregister(net);
Eric Dumazet4907abc2019-05-24 09:03:39 -0700550 fqdir_exit(net->ipv6.fqdir);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800551}
552
553static struct pernet_operations ip6_frags_ops = {
Eric Dumazetd5dd8872019-06-18 11:09:00 -0700554 .init = ipv6_frags_init_net,
555 .pre_exit = ipv6_frags_pre_exit_net,
556 .exit = ipv6_frags_exit_net,
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800557};
558
Florian Westphal70b095c2018-07-14 01:14:01 +0200559static const struct rhashtable_params ip6_rhash_params = {
Eric Dumazet648700f2018-03-31 12:58:49 -0700560 .head_offset = offsetof(struct inet_frag_queue, node),
Florian Westphal70b095c2018-07-14 01:14:01 +0200561 .hashfn = ip6frag_key_hashfn,
562 .obj_hashfn = ip6frag_obj_hashfn,
563 .obj_cmpfn = ip6frag_obj_cmpfn,
Eric Dumazet648700f2018-03-31 12:58:49 -0700564 .automatic_shrinking = true,
565};
Eric Dumazet648700f2018-03-31 12:58:49 -0700566
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800567int __init ipv6_frag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800569 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Florian Westphal70b095c2018-07-14 01:14:01 +0200571 ip6_frags.constructor = ip6frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700572 ip6_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700573 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700574 ip6_frags.frag_expire = ip6_frag_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200575 ip6_frags.frags_cache_name = ip6_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700576 ip6_frags.rhash_params = ip6_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200577 ret = inet_frags_init(&ip6_frags);
578 if (ret)
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700579 goto out;
580
581 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
582 if (ret)
583 goto err_protocol;
584
585 ret = ip6_frags_sysctl_register();
586 if (ret)
587 goto err_sysctl;
588
589 ret = register_pernet_subsys(&ip6_frags_ops);
590 if (ret)
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200591 goto err_pernet;
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700592
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800593out:
594 return ret;
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700595
596err_pernet:
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700597 ip6_frags_sysctl_unregister();
598err_sysctl:
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700599 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700600err_protocol:
601 inet_frags_fini(&ip6_frags);
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700602 goto out;
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800603}
604
605void ipv6_frag_exit(void)
606{
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700607 ip6_frags_sysctl_unregister();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800608 unregister_pernet_subsys(&ip6_frags_ops);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800609 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Eric Dumazetae7352d2019-05-27 16:56:48 -0700610 inet_frags_fini(&ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}