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