Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * IPv6 fragment reassembly |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Based on: net/ipv4/ip_fragment.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 12 | /* |
| 13 | * Fixes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Andi Kleen Make it work with multiple hosts. |
| 15 | * More RFC compliance. |
| 16 | * |
| 17 | * Horst von Brand Add missing #include <linux/string.h> |
| 18 | * Alexey Kuznetsov SMP races, threading, cleanup. |
| 19 | * Patrick McHardy LRU queue of frag heads for evictor. |
| 20 | * Mitsuru KANDA @USAGI Register inet6_protocol{}. |
| 21 | * David Stevens and |
| 22 | * YOSHIFUJI,H. @USAGI Always remove fragment header to |
| 23 | * calculate ICV correctly. |
| 24 | */ |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 25 | |
| 26 | #define pr_fmt(fmt) "IPv6: " fmt |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/socket.h> |
| 32 | #include <linux/sockios.h> |
| 33 | #include <linux/jiffies.h> |
| 34 | #include <linux/net.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/netdevice.h> |
| 37 | #include <linux/in6.h> |
| 38 | #include <linux/ipv6.h> |
| 39 | #include <linux/icmpv6.h> |
| 40 | #include <linux/random.h> |
| 41 | #include <linux/jhash.h> |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 42 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 44 | #include <linux/export.h> |
Hangbin Liu | 2efdaaa | 2020-10-27 20:33:13 +0800 | [diff] [blame] | 45 | #include <linux/tcp.h> |
| 46 | #include <linux/udp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #include <net/sock.h> |
| 49 | #include <net/snmp.h> |
| 50 | |
| 51 | #include <net/ipv6.h> |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 52 | #include <net/ip6_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #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 Westphal | 70b095c | 2018-07-14 01:14:01 +0200 | [diff] [blame] | 58 | #include <net/ipv6_frag.h> |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 59 | #include <net/inet_ecn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 61 | static const char ip6_frag_cache_name[] = "ip6-frags"; |
| 62 | |
Fabian Frederick | fc08c25 | 2014-10-29 11:38:17 +0100 | [diff] [blame] | 63 | static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 64 | { |
| 65 | return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); |
| 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 68 | static struct inet_frags ip6_frags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 70 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, |
| 71 | struct sk_buff *prev_tail, struct net_device *dev); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 72 | |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 73 | static void ip6_frag_expire(struct timer_list *t) |
Amerigo Wang | b836c99 | 2012-09-18 16:50:09 +0000 | [diff] [blame] | 74 | { |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 75 | struct inet_frag_queue *frag = from_timer(frag, t, timer); |
Amerigo Wang | b836c99 | 2012-09-18 16:50:09 +0000 | [diff] [blame] | 76 | struct frag_queue *fq; |
Amerigo Wang | b836c99 | 2012-09-18 16:50:09 +0000 | [diff] [blame] | 77 | |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 78 | fq = container_of(frag, struct frag_queue, q); |
Amerigo Wang | b836c99 | 2012-09-18 16:50:09 +0000 | [diff] [blame] | 79 | |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 80 | ip6frag_expire_frag_queue(fq->q.fqdir->net, fq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Fabian Frederick | fc08c25 | 2014-10-29 11:38:17 +0100 | [diff] [blame] | 83 | static struct frag_queue * |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 84 | fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 86 | struct frag_v6_compare_key key = { |
| 87 | .id = id, |
| 88 | .saddr = hdr->saddr, |
| 89 | .daddr = hdr->daddr, |
| 90 | .user = IP6_DEFRAG_LOCAL_DELIVER, |
| 91 | .iif = iif, |
| 92 | }; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 93 | struct inet_frag_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 95 | if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST | |
| 96 | IPV6_ADDR_LINKLOCAL))) |
| 97 | key.iif = 0; |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 98 | |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 99 | q = inet_frag_find(net->ipv6.fqdir, &key); |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 100 | if (!q) |
Shan Wei | 9546377 | 2010-02-11 00:12:45 +0000 | [diff] [blame] | 101 | return NULL; |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 102 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 103 | return container_of(q, struct frag_queue, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 106 | static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 107 | struct frag_hdr *fhdr, int nhoff, |
| 108 | u32 *prob_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 110 | struct net *net = dev_net(skb_dst(skb)->dev); |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 111 | int offset, end, fragsize; |
| 112 | struct sk_buff *prev_tail; |
| 113 | struct net_device *dev; |
| 114 | int err = -ENOENT; |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 115 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 117 | if (fq->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | goto err; |
| 119 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 120 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | offset = ntohs(fhdr->frag_off) & ~0x7; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 122 | end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - |
| 123 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | if ((unsigned int)end > IPV6_MAXPLEN) { |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 126 | *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb); |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 127 | /* note that if prob_offset is set, the skb is freed elsewhere, |
| 128 | * we do not free it here. |
| 129 | */ |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 130 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 133 | ecn = ip6_frag_ecn(ipv6_hdr(skb)); |
| 134 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 135 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 136 | const unsigned char *nh = skb_network_header(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 137 | skb->csum = csum_sub(skb->csum, |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 138 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
| 139 | 0)); |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* Is this the final fragment? */ |
| 143 | if (!(fhdr->frag_off & htons(IP6_MF))) { |
| 144 | /* If we already have some bits beyond end |
| 145 | * or have different end, the segment is corrupted. |
| 146 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 147 | if (end < fq->q.len || |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 148 | ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) |
Peter Oskolkov | 2475f59 | 2018-09-21 11:17:15 -0700 | [diff] [blame] | 149 | goto discard_fq; |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 150 | fq->q.flags |= INET_FRAG_LAST_IN; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 151 | fq->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } else { |
| 153 | /* Check if the fragment is rounded to 8 bytes. |
| 154 | * Required by the RFC. |
| 155 | */ |
| 156 | if (end & 0x7) { |
| 157 | /* RFC2460 says always send parameter problem in |
| 158 | * this case. -DaveM |
| 159 | */ |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 160 | *prob_offset = offsetof(struct ipv6hdr, payload_len); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 161 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 163 | if (end > fq->q.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* Some bits beyond end -> corruption. */ |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 165 | if (fq->q.flags & INET_FRAG_LAST_IN) |
Peter Oskolkov | 2475f59 | 2018-09-21 11:17:15 -0700 | [diff] [blame] | 166 | goto discard_fq; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 167 | fq->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | if (end == offset) |
Peter Oskolkov | 2475f59 | 2018-09-21 11:17:15 -0700 | [diff] [blame] | 172 | goto discard_fq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 174 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* Point into the IP datagram 'data' part. */ |
| 176 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) |
Peter Oskolkov | 2475f59 | 2018-09-21 11:17:15 -0700 | [diff] [blame] | 177 | goto discard_fq; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 178 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 179 | err = pskb_trim_rcsum(skb, end - offset); |
| 180 | if (err) |
Peter Oskolkov | 2475f59 | 2018-09-21 11:17:15 -0700 | [diff] [blame] | 181 | goto discard_fq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 183 | /* Note : skb->rbnode and skb->dev share the same location. */ |
Eric Dumazet | 219badf | 2018-03-31 12:58:59 -0700 | [diff] [blame] | 184 | dev = skb->dev; |
Eric Dumazet | 219badf | 2018-03-31 12:58:59 -0700 | [diff] [blame] | 185 | /* Makes sure compiler wont do silly aliasing games */ |
| 186 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 188 | prev_tail = fq->q.fragments_tail; |
| 189 | err = inet_frag_queue_insert(&fq->q, skb, offset, end); |
| 190 | if (err) |
| 191 | goto insert_error; |
| 192 | |
| 193 | if (dev) |
| 194 | fq->iif = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 196 | fq->q.stamp = skb->tstamp; |
| 197 | fq->q.meat += skb->len; |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 198 | fq->ecn |= ecn; |
Eric Dumazet | 6ce3b4d | 2019-05-24 09:03:30 -0700 | [diff] [blame] | 199 | add_frag_mem_limit(fq->q.fqdir, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Willem de Bruijn | dbd1759 | 2016-11-02 11:02:18 -0400 | [diff] [blame] | 201 | fragsize = -skb_network_offset(skb) + skb->len; |
| 202 | if (fragsize > fq->q.max_size) |
| 203 | fq->q.max_size = fragsize; |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* The first fragment. |
| 206 | * nhoffset is obtained from the first fragment, of course. |
| 207 | */ |
| 208 | if (offset == 0) { |
| 209 | fq->nhoffset = nhoff; |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 210 | fq->q.flags |= INET_FRAG_FIRST_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 212 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 213 | if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 214 | fq->q.meat == fq->q.len) { |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 215 | unsigned long orefdst = skb->_skb_refdst; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 216 | |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 217 | skb->_skb_refdst = 0UL; |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 218 | err = ip6_frag_reasm(fq, skb, prev_tail, dev); |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 219 | skb->_skb_refdst = orefdst; |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 220 | return err; |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | skb_dst_drop(skb); |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 224 | return -EINPROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 226 | insert_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 Dichtel | 70789d7 | 2010-09-03 05:13:05 +0000 | [diff] [blame] | 234 | discard_fq: |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 235 | inet_frag_kill(&fq->q); |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 236 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 237 | IPSTATS_MIB_REASMFAILS); |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 238 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | kfree_skb(skb); |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 240 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Check if this packet is complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | * |
| 246 | * It is called with locked fq, and caller must check that |
| 247 | * queue is eligible for reassembly i.e. it is not COMPLETE, |
| 248 | * the last and the first frames arrived and all the bits are here. |
| 249 | */ |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 250 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, |
| 251 | struct sk_buff *prev_tail, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 253 | struct net *net = fq->q.fqdir->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | unsigned int nhoff; |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 255 | void *reasm_data; |
| 256 | int payload_len; |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 257 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 259 | inet_frag_kill(&fq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Hannes Frederic Sowa | eec2e61 | 2013-03-22 08:24:44 +0000 | [diff] [blame] | 261 | ecn = ip_frag_ecn_table[fq->ecn]; |
| 262 | if (unlikely(ecn == 0xff)) |
| 263 | goto out_fail; |
| 264 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 265 | reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail); |
| 266 | if (!reasm_data) |
| 267 | goto out_oom; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 268 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 269 | payload_len = ((skb->data - skb_network_header(skb)) - |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 270 | sizeof(struct ipv6hdr) + fq->q.len - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 271 | sizeof(struct frag_hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (payload_len > IPV6_MAXPLEN) |
| 273 | goto out_oversize; |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | /* We have to remove fragment header from datagram and to relocate |
| 276 | * header in order to calculate ICV correctly. */ |
| 277 | nhoff = fq->nhoffset; |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 278 | skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0]; |
| 279 | memmove(skb->head + sizeof(struct frag_hdr), skb->head, |
| 280 | (skb->data - skb->head) - sizeof(struct frag_hdr)); |
| 281 | if (skb_mac_header_was_set(skb)) |
| 282 | skb->mac_header += sizeof(struct frag_hdr); |
| 283 | skb->network_header += sizeof(struct frag_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 285 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Guillaume Nault | 891584f | 2019-08-02 17:15:03 +0200 | [diff] [blame] | 287 | inet_frag_reasm_finish(&fq->q, skb, reasm_data, true); |
Eric Dumazet | ec16439 | 2012-05-19 03:02:35 +0000 | [diff] [blame] | 288 | |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 289 | skb->dev = dev; |
| 290 | ipv6_hdr(skb)->payload_len = htons(payload_len); |
| 291 | ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn); |
| 292 | IP6CB(skb)->nhoff = nhoff; |
| 293 | IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; |
| 294 | IP6CB(skb)->frag_max_size = fq->q.max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | /* Yes, and fold redundant checksum back. 8) */ |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 297 | skb_postpush_rcsum(skb, skb_network_header(skb), |
| 298 | skb_network_header_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 300 | rcu_read_lock(); |
Stephen Suryaputra | e1ae5c2 | 2019-06-10 10:32:50 -0400 | [diff] [blame] | 301 | __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMOKS); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 302 | rcu_read_unlock(); |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 303 | fq->q.rb_fragments = RB_ROOT; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 304 | fq->q.fragments_tail = NULL; |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 305 | fq->q.last_run_head = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return 1; |
| 307 | |
| 308 | out_oversize: |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 309 | net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | goto out_fail; |
| 311 | out_oom: |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 312 | net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | out_fail: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 314 | rcu_read_lock(); |
Stephen Suryaputra | e1ae5c2 | 2019-06-10 10:32:50 -0400 | [diff] [blame] | 315 | __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMFAILS); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 316 | rcu_read_unlock(); |
Peter Oskolkov | 2475f59 | 2018-09-21 11:17:15 -0700 | [diff] [blame] | 317 | inet_frag_kill(&fq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 321 | static int ipv6_frag_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | struct frag_hdr *fhdr; |
| 324 | struct frag_queue *fq; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 325 | const struct ipv6hdr *hdr = ipv6_hdr(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 326 | struct net *net = dev_net(skb_dst(skb)->dev); |
Hangbin Liu | 2efdaaa | 2020-10-27 20:33:13 +0800 | [diff] [blame] | 327 | u8 nexthdr; |
Georg Kohmann | 9d9e937 | 2020-11-11 12:50:25 +0100 | [diff] [blame] | 328 | int iif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Hannes Frederic Sowa | f46078c | 2013-08-16 13:30:07 +0200 | [diff] [blame] | 330 | if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) |
| 331 | goto fail_hdr; |
| 332 | |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 333 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | /* Jumbo payload inhibits frag. header */ |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 336 | if (hdr->payload_len == 0) |
Denis V. Lunev | 98b3377c | 2008-10-08 10:31:44 -0700 | [diff] [blame] | 337 | goto fail_hdr; |
| 338 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 339 | if (!pskb_may_pull(skb, (skb_transport_offset(skb) + |
Denis V. Lunev | 98b3377c | 2008-10-08 10:31:44 -0700 | [diff] [blame] | 340 | sizeof(struct frag_hdr)))) |
| 341 | goto fail_hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 343 | hdr = ipv6_hdr(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 344 | fhdr = (struct frag_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Francesco Ruggeri | e29f011 | 2021-05-21 13:21:14 -0700 | [diff] [blame] | 346 | if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | /* It is not a fragmented frame */ |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 348 | skb->transport_header += sizeof(struct frag_hdr); |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 349 | __IP6_INC_STATS(net, |
| 350 | ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 352 | IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); |
Hannes Frederic Sowa | f46078c | 2013-08-16 13:30:07 +0200 | [diff] [blame] | 353 | IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; |
Francesco Ruggeri | e29f011 | 2021-05-21 13:21:14 -0700 | [diff] [blame] | 354 | IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) + |
| 355 | sizeof(struct ipv6hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return 1; |
| 357 | } |
| 358 | |
Hangbin Liu | 2efdaaa | 2020-10-27 20:33:13 +0800 | [diff] [blame] | 359 | /* 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 Kohmann | 2d8f648 | 2020-11-19 10:58:33 +0100 | [diff] [blame] | 366 | if (ipv6frag_thdr_truncated(skb, skb_transport_offset(skb), &nexthdr)) { |
Georg Kohmann | 9d9e937 | 2020-11-11 12:50:25 +0100 | [diff] [blame] | 367 | __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 Liu | 2efdaaa | 2020-10-27 20:33:13 +0800 | [diff] [blame] | 371 | } |
| 372 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 373 | iif = skb->dev ? skb->dev->ifindex : 0; |
| 374 | fq = fq_find(net, fhdr->identification, hdr, iif); |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 375 | if (fq) { |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 376 | u32 prob_offset = 0; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 377 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 379 | spin_lock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 381 | fq->iif = iif; |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 382 | ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, |
| 383 | &prob_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 385 | spin_unlock(&fq->q.lock); |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 386 | inet_frag_put(&fq->q); |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 387 | if (prob_offset) { |
| 388 | __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), |
| 389 | IPSTATS_MIB_INHDRERRORS); |
Peter Oskolkov | d4289fc | 2019-01-22 10:02:51 -0800 | [diff] [blame] | 390 | /* icmpv6_param_prob() calls kfree_skb(skb) */ |
Eric Dumazet | 415787d | 2018-04-17 18:11:44 -0700 | [diff] [blame] | 391 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); |
| 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return ret; |
| 394 | } |
| 395 | |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 396 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | kfree_skb(skb); |
| 398 | return -1; |
Denis V. Lunev | 98b3377c | 2008-10-08 10:31:44 -0700 | [diff] [blame] | 399 | |
| 400 | fail_hdr: |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 401 | __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 402 | IPSTATS_MIB_INHDRERRORS); |
Denis V. Lunev | 98b3377c | 2008-10-08 10:31:44 -0700 | [diff] [blame] | 403 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); |
| 404 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Ian Morris | cc24bec | 2014-08-24 21:53:11 +0100 | [diff] [blame] | 407 | static const struct inet6_protocol frag_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | .handler = ipv6_frag_rcv, |
| 409 | .flags = INET6_PROTO_NOPOLICY, |
| 410 | }; |
| 411 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 412 | #ifdef CONFIG_SYSCTL |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 413 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 414 | static struct ctl_table ip6_frags_ns_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 415 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 416 | .procname = "ip6frag_high_thresh", |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 417 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 418 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 419 | .proc_handler = proc_doulongvec_minmax, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 420 | }, |
| 421 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 422 | .procname = "ip6frag_low_thresh", |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 423 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 424 | .mode = 0644, |
Eric Dumazet | 6e00f7d | 2018-04-01 21:57:59 -0700 | [diff] [blame] | 425 | .proc_handler = proc_doulongvec_minmax, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 426 | }, |
| 427 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 428 | .procname = "ip6frag_time", |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 429 | .maxlen = sizeof(int), |
| 430 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 431 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 432 | }, |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 433 | { } |
| 434 | }; |
| 435 | |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 436 | /* secret interval has been deprecated */ |
| 437 | static int ip6_frags_secret_interval_unused; |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 438 | static struct ctl_table ip6_frags_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 439 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 440 | .procname = "ip6frag_secret_interval", |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 441 | .data = &ip6_frags_secret_interval_unused, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 442 | .maxlen = sizeof(int), |
| 443 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 444 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 445 | }, |
| 446 | { } |
| 447 | }; |
Daniel Lezcano | 7d460db | 2008-01-18 23:52:35 -0800 | [diff] [blame] | 448 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 449 | static int __net_init ip6_frags_ns_sysctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 450 | { |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 451 | struct ctl_table *table; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 452 | struct ctl_table_header *hdr; |
| 453 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 454 | table = ip6_frags_ns_ctl_table; |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 455 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 456 | table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 457 | if (!table) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 458 | goto err_alloc; |
| 459 | |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 460 | } |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 461 | 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 Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 466 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 467 | hdr = register_net_sysctl(net, "net/ipv6", table); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 468 | if (!hdr) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 469 | goto err_reg; |
| 470 | |
| 471 | net->ipv6.sysctl.frags_hdr = hdr; |
| 472 | return 0; |
| 473 | |
| 474 | err_reg: |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 475 | if (!net_eq(net, &init_net)) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 476 | kfree(table); |
| 477 | err_alloc: |
| 478 | return -ENOMEM; |
| 479 | } |
| 480 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 481 | static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 482 | { |
| 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 Hongyang | 3705e11 | 2009-12-18 20:25:13 -0800 | [diff] [blame] | 487 | if (!net_eq(net, &init_net)) |
| 488 | kfree(table); |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 489 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 490 | |
| 491 | static struct ctl_table_header *ip6_ctl_header; |
| 492 | |
| 493 | static int ip6_frags_sysctl_register(void) |
| 494 | { |
Eric W. Biederman | 4344475 | 2012-04-19 13:22:55 +0000 | [diff] [blame] | 495 | ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 496 | ip6_frags_ctl_table); |
| 497 | return ip6_ctl_header == NULL ? -ENOMEM : 0; |
| 498 | } |
| 499 | |
| 500 | static void ip6_frags_sysctl_unregister(void) |
| 501 | { |
| 502 | unregister_net_sysctl_table(ip6_ctl_header); |
| 503 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 504 | #else |
Fabian Frederick | fc08c25 | 2014-10-29 11:38:17 +0100 | [diff] [blame] | 505 | static int ip6_frags_ns_sysctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 506 | { |
| 507 | return 0; |
| 508 | } |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 509 | |
Fabian Frederick | fc08c25 | 2014-10-29 11:38:17 +0100 | [diff] [blame] | 510 | static void ip6_frags_ns_sysctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 511 | { |
| 512 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 513 | |
Fabian Frederick | fc08c25 | 2014-10-29 11:38:17 +0100 | [diff] [blame] | 514 | static int ip6_frags_sysctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 515 | { |
| 516 | return 0; |
| 517 | } |
| 518 | |
Fabian Frederick | fc08c25 | 2014-10-29 11:38:17 +0100 | [diff] [blame] | 519 | static void ip6_frags_sysctl_unregister(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 520 | { |
| 521 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 522 | #endif |
| 523 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 524 | static int __net_init ipv6_frags_init_net(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 525 | { |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 526 | int res; |
| 527 | |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 528 | res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net); |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 529 | if (res < 0) |
| 530 | return res; |
Jesper Dangaard Brouer | 5a63643 | 2017-09-01 11:26:13 +0200 | [diff] [blame] | 531 | |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 532 | 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 Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 536 | res = ip6_frags_ns_sysctl_register(net); |
| 537 | if (res < 0) |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 538 | fqdir_exit(net->ipv6.fqdir); |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 539 | return res; |
Daniel Lezcano | e71e034 | 2008-01-10 02:56:03 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Eric Dumazet | d5dd887 | 2019-06-18 11:09:00 -0700 | [diff] [blame] | 542 | static void __net_exit ipv6_frags_pre_exit_net(struct net *net) |
| 543 | { |
| 544 | fqdir_pre_exit(net->ipv6.fqdir); |
| 545 | } |
| 546 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 547 | static void __net_exit ipv6_frags_exit_net(struct net *net) |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 548 | { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 549 | ip6_frags_ns_sysctl_unregister(net); |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 550 | fqdir_exit(net->ipv6.fqdir); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static struct pernet_operations ip6_frags_ops = { |
Eric Dumazet | d5dd887 | 2019-06-18 11:09:00 -0700 | [diff] [blame] | 554 | .init = ipv6_frags_init_net, |
| 555 | .pre_exit = ipv6_frags_pre_exit_net, |
| 556 | .exit = ipv6_frags_exit_net, |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 557 | }; |
| 558 | |
Florian Westphal | 70b095c | 2018-07-14 01:14:01 +0200 | [diff] [blame] | 559 | static const struct rhashtable_params ip6_rhash_params = { |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 560 | .head_offset = offsetof(struct inet_frag_queue, node), |
Florian Westphal | 70b095c | 2018-07-14 01:14:01 +0200 | [diff] [blame] | 561 | .hashfn = ip6frag_key_hashfn, |
| 562 | .obj_hashfn = ip6frag_obj_hashfn, |
| 563 | .obj_cmpfn = ip6frag_obj_cmpfn, |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 564 | .automatic_shrinking = true, |
| 565 | }; |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 566 | |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 567 | int __init ipv6_frag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 569 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Florian Westphal | 70b095c | 2018-07-14 01:14:01 +0200 | [diff] [blame] | 571 | ip6_frags.constructor = ip6frag_init; |
Pavel Emelyanov | c954770 | 2007-10-17 19:48:26 -0700 | [diff] [blame] | 572 | ip6_frags.destructor = NULL; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 573 | ip6_frags.qsize = sizeof(struct frag_queue); |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 574 | ip6_frags.frag_expire = ip6_frag_expire; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 575 | ip6_frags.frags_cache_name = ip6_frag_cache_name; |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 576 | ip6_frags.rhash_params = ip6_rhash_params; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 577 | ret = inet_frags_init(&ip6_frags); |
| 578 | if (ret) |
Eric Dumazet | 5b975ba | 2018-03-31 12:58:45 -0700 | [diff] [blame] | 579 | 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 Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 591 | goto err_pernet; |
Eric Dumazet | 5b975ba | 2018-03-31 12:58:45 -0700 | [diff] [blame] | 592 | |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 593 | out: |
| 594 | return ret; |
Pavel Emelyanov | 0002c63 | 2008-05-19 13:52:28 -0700 | [diff] [blame] | 595 | |
| 596 | err_pernet: |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 597 | ip6_frags_sysctl_unregister(); |
| 598 | err_sysctl: |
Pavel Emelyanov | 0002c63 | 2008-05-19 13:52:28 -0700 | [diff] [blame] | 599 | inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
Eric Dumazet | 5b975ba | 2018-03-31 12:58:45 -0700 | [diff] [blame] | 600 | err_protocol: |
| 601 | inet_frags_fini(&ip6_frags); |
Pavel Emelyanov | 0002c63 | 2008-05-19 13:52:28 -0700 | [diff] [blame] | 602 | goto out; |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | void ipv6_frag_exit(void) |
| 606 | { |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 607 | ip6_frags_sysctl_unregister(); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 608 | unregister_pernet_subsys(&ip6_frags_ops); |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 609 | inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
Eric Dumazet | ae7352d | 2019-05-27 16:56:48 -0700 | [diff] [blame] | 610 | inet_frags_fini(&ip6_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |