blob: 836ea964cf140d8b0134894455f18addc069e13e [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
Peter Oskolkovd4289fc2019-01-22 10:02:51 -080072static 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
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;
Amerigo Wangb836c992012-09-18 16:50:09 +000079
Kees Cook78802012017-10-16 17:29:20 -070080 fq = container_of(frag, struct frag_queue, q);
Amerigo Wangb836c992012-09-18 16:50:09 +000081
Eric Dumazeta39aca62019-05-24 09:03:38 -070082 ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Fabian Frederickfc08c252014-10-29 11:38:17 +010085static struct frag_queue *
Eric Dumazet648700f2018-03-31 12:58:49 -070086fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Dumazet648700f2018-03-31 12:58:49 -070088 struct frag_v6_compare_key key = {
89 .id = id,
90 .saddr = hdr->saddr,
91 .daddr = hdr->daddr,
92 .user = IP6_DEFRAG_LOCAL_DELIVER,
93 .iif = iif,
94 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070095 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric Dumazet648700f2018-03-31 12:58:49 -070097 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
98 IPV6_ADDR_LINKLOCAL)))
99 key.iif = 0;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700100
Eric Dumazet4907abc2019-05-24 09:03:39 -0700101 q = inet_frag_find(net->ipv6.fqdir, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700102 if (!q)
Shan Wei95463772010-02-11 00:12:45 +0000103 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700104
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700105 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Herbert Xuf61944e2007-10-15 01:28:47 -0700108static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Eric Dumazet415787d2018-04-17 18:11:44 -0700109 struct frag_hdr *fhdr, int nhoff,
110 u32 *prob_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Eric Dumazetadf30902009-06-02 05:19:30 +0000112 struct net *net = dev_net(skb_dst(skb)->dev);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800113 int offset, end, fragsize;
114 struct sk_buff *prev_tail;
115 struct net_device *dev;
116 int err = -ENOENT;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000117 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200119 if (fq->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 goto err;
121
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800122 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800129 /* note that if prob_offset is set, the skb is freed elsewhere,
130 * we do not free it here.
131 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700132 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000135 ecn = ip6_frag_ecn(ipv6_hdr(skb));
136
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700137 if (skb->ip_summed == CHECKSUM_COMPLETE) {
138 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900139 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700140 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
141 0));
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* Is this the final fragment? */
145 if (!(fhdr->frag_off & htons(IP6_MF))) {
146 /* If we already have some bits beyond end
147 * or have different end, the segment is corrupted.
148 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700149 if (end < fq->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200150 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700151 goto discard_fq;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200152 fq->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700153 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else {
155 /* Check if the fragment is rounded to 8 bytes.
156 * Required by the RFC.
157 */
158 if (end & 0x7) {
159 /* RFC2460 says always send parameter problem in
160 * this case. -DaveM
161 */
Eric Dumazet415787d2018-04-17 18:11:44 -0700162 *prob_offset = offsetof(struct ipv6hdr, payload_len);
Herbert Xuf61944e2007-10-15 01:28:47 -0700163 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700165 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200167 if (fq->q.flags & INET_FRAG_LAST_IN)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700168 goto discard_fq;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700169 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
172
173 if (end == offset)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700174 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800176 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* Point into the IP datagram 'data' part. */
178 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
Peter Oskolkov2475f592018-09-21 11:17:15 -0700179 goto discard_fq;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900180
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800181 err = pskb_trim_rcsum(skb, end - offset);
182 if (err)
Peter Oskolkov2475f592018-09-21 11:17:15 -0700183 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800185 /* Note : skb->rbnode and skb->dev share the same location. */
Eric Dumazet219badf2018-03-31 12:58:59 -0700186 dev = skb->dev;
Eric Dumazet219badf2018-03-31 12:58:59 -0700187 /* Makes sure compiler wont do silly aliasing games */
188 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800190 prev_tail = fq->q.fragments_tail;
191 err = inet_frag_queue_insert(&fq->q, skb, offset, end);
192 if (err)
193 goto insert_error;
194
195 if (dev)
196 fq->iif = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700198 fq->q.stamp = skb->tstamp;
199 fq->q.meat += skb->len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000200 fq->ecn |= ecn;
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700201 add_frag_mem_limit(fq->q.fqdir, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Willem de Bruijndbd17592016-11-02 11:02:18 -0400203 fragsize = -skb_network_offset(skb) + skb->len;
204 if (fragsize > fq->q.max_size)
205 fq->q.max_size = fragsize;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* The first fragment.
208 * nhoffset is obtained from the first fragment, of course.
209 */
210 if (offset == 0) {
211 fq->nhoffset = nhoff;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200212 fq->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700214
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200215 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000216 fq->q.meat == fq->q.len) {
Eric Dumazet97599dc2013-04-16 12:55:41 +0000217 unsigned long orefdst = skb->_skb_refdst;
Herbert Xuf61944e2007-10-15 01:28:47 -0700218
Eric Dumazet97599dc2013-04-16 12:55:41 +0000219 skb->_skb_refdst = 0UL;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800220 err = ip6_frag_reasm(fq, skb, prev_tail, dev);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000221 skb->_skb_refdst = orefdst;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800222 return err;
Eric Dumazet97599dc2013-04-16 12:55:41 +0000223 }
224
225 skb_dst_drop(skb);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800226 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800228insert_error:
229 if (err == IPFRAG_DUP) {
230 kfree_skb(skb);
231 return -EINVAL;
232 }
233 err = -EINVAL;
234 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
235 IPSTATS_MIB_REASM_OVERLAPS);
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000236discard_fq:
Eric Dumazet093ba722018-03-31 12:58:44 -0700237 inet_frag_kill(&fq->q);
Eric Dumazet1d015502016-04-27 16:44:40 -0700238 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
239 IPSTATS_MIB_REASMFAILS);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800240err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 kfree_skb(skb);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800242 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * Check if this packet is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
248 * It is called with locked fq, and caller must check that
249 * queue is eligible for reassembly i.e. it is not COMPLETE,
250 * the last and the first frames arrived and all the bits are here.
251 */
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800252static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
253 struct sk_buff *prev_tail, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Eric Dumazeta39aca62019-05-24 09:03:38 -0700255 struct net *net = fq->q.fqdir->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned int nhoff;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800257 void *reasm_data;
258 int payload_len;
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000259 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Eric Dumazet093ba722018-03-31 12:58:44 -0700261 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Hannes Frederic Sowaeec2e612013-03-22 08:24:44 +0000263 ecn = ip_frag_ecn_table[fq->ecn];
264 if (unlikely(ecn == 0xff))
265 goto out_fail;
266
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800267 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
268 if (!reasm_data)
269 goto out_oom;
Herbert Xuf61944e2007-10-15 01:28:47 -0700270
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800271 payload_len = ((skb->data - skb_network_header(skb)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700272 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700273 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (payload_len > IPV6_MAXPLEN)
275 goto out_oversize;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* We have to remove fragment header from datagram and to relocate
278 * header in order to calculate ICV correctly. */
279 nhoff = fq->nhoffset;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800280 skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
281 memmove(skb->head + sizeof(struct frag_hdr), skb->head,
282 (skb->data - skb->head) - sizeof(struct frag_hdr));
283 if (skb_mac_header_was_set(skb))
284 skb->mac_header += sizeof(struct frag_hdr);
285 skb->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800287 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800289 inet_frag_reasm_finish(&fq->q, skb, reasm_data);
Eric Dumazetec164392012-05-19 03:02:35 +0000290
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800291 skb->dev = dev;
292 ipv6_hdr(skb)->payload_len = htons(payload_len);
293 ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
294 IP6CB(skb)->nhoff = nhoff;
295 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
296 IP6CB(skb)->frag_max_size = fq->q.max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* Yes, and fold redundant checksum back. 8) */
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800299 skb_postpush_rcsum(skb, skb_network_header(skb),
300 skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900302 rcu_read_lock();
Eric Dumazet1d015502016-04-27 16:44:40 -0700303 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900304 rcu_read_unlock();
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000305 fq->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000306 fq->q.fragments_tail = NULL;
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800307 fq->q.last_run_head = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 1;
309
310out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000311 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto out_fail;
313out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000314 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900316 rcu_read_lock();
Eric Dumazet1d015502016-04-27 16:44:40 -0700317 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900318 rcu_read_unlock();
Peter Oskolkov2475f592018-09-21 11:17:15 -0700319 inet_frag_kill(&fq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -1;
321}
322
Herbert Xue5bbef22007-10-15 12:50:28 -0700323static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct frag_hdr *fhdr;
326 struct frag_queue *fq;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000327 const struct ipv6hdr *hdr = ipv6_hdr(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000328 struct net *net = dev_net(skb_dst(skb)->dev);
Eric Dumazet648700f2018-03-31 12:58:49 -0700329 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200331 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
332 goto fail_hdr;
333
Eric Dumazet1d015502016-04-27 16:44:40 -0700334 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Jumbo payload inhibits frag. header */
Ian Morris67ba4152014-08-24 21:53:10 +0100337 if (hdr->payload_len == 0)
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700338 goto fail_hdr;
339
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700340 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700341 sizeof(struct frag_hdr))))
342 goto fail_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700344 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700345 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (!(fhdr->frag_off & htons(0xFFF9))) {
348 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700349 skb->transport_header += sizeof(struct frag_hdr);
Eric Dumazet1d015502016-04-27 16:44:40 -0700350 __IP6_INC_STATS(net,
351 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700353 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Hannes Frederic Sowaf46078c2013-08-16 13:30:07 +0200354 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 1;
356 }
357
Eric Dumazet648700f2018-03-31 12:58:49 -0700358 iif = skb->dev ? skb->dev->ifindex : 0;
359 fq = fq_find(net, fhdr->identification, hdr, iif);
Ian Morris53b24b82015-03-29 14:00:05 +0100360 if (fq) {
Eric Dumazet415787d2018-04-17 18:11:44 -0700361 u32 prob_offset = 0;
Herbert Xuf61944e2007-10-15 01:28:47 -0700362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700364 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Eric Dumazet648700f2018-03-31 12:58:49 -0700366 fq->iif = iif;
Eric Dumazet415787d2018-04-17 18:11:44 -0700367 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
368 &prob_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700370 spin_unlock(&fq->q.lock);
Eric Dumazet093ba722018-03-31 12:58:44 -0700371 inet_frag_put(&fq->q);
Eric Dumazet415787d2018-04-17 18:11:44 -0700372 if (prob_offset) {
373 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
374 IPSTATS_MIB_INHDRERRORS);
Peter Oskolkovd4289fc2019-01-22 10:02:51 -0800375 /* icmpv6_param_prob() calls kfree_skb(skb) */
Eric Dumazet415787d2018-04-17 18:11:44 -0700376 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return ret;
379 }
380
Eric Dumazet1d015502016-04-27 16:44:40 -0700381 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree_skb(skb);
383 return -1;
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700384
385fail_hdr:
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400386 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
Eric Dumazet1d015502016-04-27 16:44:40 -0700387 IPSTATS_MIB_INHDRERRORS);
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700388 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
389 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Ian Morriscc24bec2014-08-24 21:53:11 +0100392static const struct inet6_protocol frag_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .handler = ipv6_frag_rcv,
394 .flags = INET6_PROTO_NOPOLICY,
395};
396
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800397#ifdef CONFIG_SYSCTL
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200398
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700399static struct ctl_table ip6_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800400 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800401 .procname = "ip6frag_high_thresh",
Eric Dumazet3e67f102018-03-31 12:58:53 -0700402 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800403 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700404 .proc_handler = proc_doulongvec_minmax,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800405 },
406 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800407 .procname = "ip6frag_low_thresh",
Eric Dumazet3e67f102018-03-31 12:58:53 -0700408 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800409 .mode = 0644,
Eric Dumazet6e00f7d2018-04-01 21:57:59 -0700410 .proc_handler = proc_doulongvec_minmax,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800411 },
412 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800413 .procname = "ip6frag_time",
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800414 .maxlen = sizeof(int),
415 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800416 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800417 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700418 { }
419};
420
Florian Westphale3a57d12014-07-24 16:50:35 +0200421/* secret interval has been deprecated */
422static int ip6_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700423static struct ctl_table ip6_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800424 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800425 .procname = "ip6frag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200426 .data = &ip6_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800427 .maxlen = sizeof(int),
428 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800429 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800430 },
431 { }
432};
Daniel Lezcano7d460db2008-01-18 23:52:35 -0800433
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000434static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800435{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800436 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800437 struct ctl_table_header *hdr;
438
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700439 table = ip6_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800440 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700441 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100442 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800443 goto err_alloc;
444
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800445 }
Eric Dumazet4907abc2019-05-24 09:03:39 -0700446 table[0].data = &net->ipv6.fqdir->high_thresh;
447 table[0].extra1 = &net->ipv6.fqdir->low_thresh;
448 table[1].data = &net->ipv6.fqdir->low_thresh;
449 table[1].extra2 = &net->ipv6.fqdir->high_thresh;
450 table[2].data = &net->ipv6.fqdir->timeout;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800451
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000452 hdr = register_net_sysctl(net, "net/ipv6", table);
Ian Morris63159f22015-03-29 14:00:04 +0100453 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800454 goto err_reg;
455
456 net->ipv6.sysctl.frags_hdr = hdr;
457 return 0;
458
459err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800460 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800461 kfree(table);
462err_alloc:
463 return -ENOMEM;
464}
465
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000466static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800467{
468 struct ctl_table *table;
469
470 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
471 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
Yang Hongyang3705e112009-12-18 20:25:13 -0800472 if (!net_eq(net, &init_net))
473 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800474}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700475
476static struct ctl_table_header *ip6_ctl_header;
477
478static int ip6_frags_sysctl_register(void)
479{
Eric W. Biederman43444752012-04-19 13:22:55 +0000480 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700481 ip6_frags_ctl_table);
482 return ip6_ctl_header == NULL ? -ENOMEM : 0;
483}
484
485static void ip6_frags_sysctl_unregister(void)
486{
487 unregister_net_sysctl_table(ip6_ctl_header);
488}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800489#else
Fabian Frederickfc08c252014-10-29 11:38:17 +0100490static int ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800491{
492 return 0;
493}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800494
Fabian Frederickfc08c252014-10-29 11:38:17 +0100495static void ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800496{
497}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700498
Fabian Frederickfc08c252014-10-29 11:38:17 +0100499static int ip6_frags_sysctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700500{
501 return 0;
502}
503
Fabian Frederickfc08c252014-10-29 11:38:17 +0100504static void ip6_frags_sysctl_unregister(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700505{
506}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800507#endif
508
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000509static int __net_init ipv6_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800510{
Eric Dumazet787bea72018-03-31 12:58:43 -0700511 int res;
512
Eric Dumazeta39aca62019-05-24 09:03:38 -0700513 res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
Eric Dumazet787bea72018-03-31 12:58:43 -0700514 if (res < 0)
515 return res;
Jesper Dangaard Brouer5a636432017-09-01 11:26:13 +0200516
Eric Dumazet4907abc2019-05-24 09:03:39 -0700517 net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
518 net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
519 net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
520
Eric Dumazet787bea72018-03-31 12:58:43 -0700521 res = ip6_frags_ns_sysctl_register(net);
522 if (res < 0)
Eric Dumazet4907abc2019-05-24 09:03:39 -0700523 fqdir_exit(net->ipv6.fqdir);
Eric Dumazet787bea72018-03-31 12:58:43 -0700524 return res;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800525}
526
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000527static void __net_exit ipv6_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800528{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700529 ip6_frags_ns_sysctl_unregister(net);
Eric Dumazet4907abc2019-05-24 09:03:39 -0700530 fqdir_exit(net->ipv6.fqdir);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800531}
532
533static struct pernet_operations ip6_frags_ops = {
534 .init = ipv6_frags_init_net,
535 .exit = ipv6_frags_exit_net,
536};
537
Florian Westphal70b095c2018-07-14 01:14:01 +0200538static const struct rhashtable_params ip6_rhash_params = {
Eric Dumazet648700f2018-03-31 12:58:49 -0700539 .head_offset = offsetof(struct inet_frag_queue, node),
Florian Westphal70b095c2018-07-14 01:14:01 +0200540 .hashfn = ip6frag_key_hashfn,
541 .obj_hashfn = ip6frag_obj_hashfn,
542 .obj_cmpfn = ip6frag_obj_cmpfn,
Eric Dumazet648700f2018-03-31 12:58:49 -0700543 .automatic_shrinking = true,
544};
Eric Dumazet648700f2018-03-31 12:58:49 -0700545
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800546int __init ipv6_frag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800548 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Florian Westphal70b095c2018-07-14 01:14:01 +0200550 ip6_frags.constructor = ip6frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700551 ip6_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700552 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700553 ip6_frags.frag_expire = ip6_frag_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200554 ip6_frags.frags_cache_name = ip6_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700555 ip6_frags.rhash_params = ip6_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200556 ret = inet_frags_init(&ip6_frags);
557 if (ret)
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700558 goto out;
559
560 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
561 if (ret)
562 goto err_protocol;
563
564 ret = ip6_frags_sysctl_register();
565 if (ret)
566 goto err_sysctl;
567
568 ret = register_pernet_subsys(&ip6_frags_ops);
569 if (ret)
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200570 goto err_pernet;
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700571
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800572out:
573 return ret;
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700574
575err_pernet:
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700576 ip6_frags_sysctl_unregister();
577err_sysctl:
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700578 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Eric Dumazet5b975ba2018-03-31 12:58:45 -0700579err_protocol:
580 inet_frags_fini(&ip6_frags);
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700581 goto out;
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800582}
583
584void ipv6_frag_exit(void)
585{
586 inet_frags_fini(&ip6_frags);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700587 ip6_frags_sysctl_unregister();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800588 unregister_pernet_subsys(&ip6_frags_ops);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800589 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}