Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * The IP fragmentation functionality. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 10 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * Fixes: |
| 13 | * Alan Cox : Split from ip.c , see ip_input.c for history. |
| 14 | * David S. Miller : Begin massive cleanup... |
| 15 | * Andi Kleen : Add sysctls. |
| 16 | * xxxx : Overlapfrag bug. |
| 17 | * Ultima : ip_expire() kernel panic. |
| 18 | * Bill Hawes : Frag accounting and evictor fixes. |
| 19 | * John McDonald : 0 length frag bug. |
| 20 | * Alexey Kuznetsov: SMP races, threading, cleanup. |
| 21 | * Patrick McHardy : LRU queue of frag heads for evictor. |
| 22 | */ |
| 23 | |
Joe Perches | afd46503 | 2012-03-12 07:03:32 +0000 | [diff] [blame] | 24 | #define pr_fmt(fmt) "IPv4: " fmt |
| 25 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 26 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/ip.h> |
| 34 | #include <linux/icmp.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/jhash.h> |
| 37 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 39 | #include <net/route.h> |
| 40 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/sock.h> |
| 42 | #include <net/ip.h> |
| 43 | #include <net/icmp.h> |
| 44 | #include <net/checksum.h> |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 45 | #include <net/inetpeer.h> |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 46 | #include <net/inet_frag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/tcp.h> |
| 48 | #include <linux/udp.h> |
| 49 | #include <linux/inet.h> |
| 50 | #include <linux/netfilter_ipv4.h> |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 51 | #include <net/inet_ecn.h> |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 52 | #include <net/l3mdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6 |
| 55 | * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c |
| 56 | * as well. Or notify me, at least. --ANK |
| 57 | */ |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 58 | static const char ip_frag_cache_name[] = "ip4-frags"; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* Describe an entry in the "incomplete datagrams" queue. */ |
| 61 | struct ipq { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 62 | struct inet_frag_queue q; |
| 63 | |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 64 | u8 ecn; /* RFC3168 support */ |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 65 | u16 max_df_size; /* largest frag with DF set seen */ |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 66 | int iif; |
| 67 | unsigned int rid; |
| 68 | struct inet_peer *peer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 71 | static u8 ip4_frag_ecn(u8 tos) |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 72 | { |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 73 | return 1 << (tos & INET_ECN_MASK); |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 76 | static struct inet_frags ip4_frags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 78 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, |
| 79 | struct sk_buff *prev_tail, struct net_device *dev); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 80 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 81 | |
Florian Westphal | 36c7778 | 2014-07-24 16:50:29 +0200 | [diff] [blame] | 82 | static void ip4_frag_init(struct inet_frag_queue *q, const void *a) |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 83 | { |
| 84 | struct ipq *qp = container_of(q, struct ipq, q); |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 85 | struct net *net = q->fqdir->net; |
Gao feng | 54db0cc | 2012-06-08 01:21:40 +0000 | [diff] [blame] | 86 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 87 | const struct frag_v4_compare_key *key = a; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 88 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 89 | q->key.v4 = *key; |
| 90 | qp->ecn = 0; |
Eric Dumazet | 6ce3b4d | 2019-05-24 09:03:30 -0700 | [diff] [blame] | 91 | qp->peer = q->fqdir->max_dist ? |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 92 | inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) : |
David Ahern | 192132b | 2015-08-27 16:07:03 -0700 | [diff] [blame] | 93 | NULL; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 96 | static void ip4_frag_free(struct inet_frag_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 98 | struct ipq *qp; |
| 99 | |
| 100 | qp = container_of(q, struct ipq, q); |
| 101 | if (qp->peer) |
| 102 | inet_putpeer(qp->peer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* Destruction primitives. */ |
| 107 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 108 | static void ipq_put(struct ipq *ipq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 110 | inet_frag_put(&ipq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Kill ipq entry. It is not destroyed immediately, |
| 114 | * because caller (and someone more) holds reference count. |
| 115 | */ |
| 116 | static void ipq_kill(struct ipq *ipq) |
| 117 | { |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 118 | inet_frag_kill(&ipq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Andy Zhou | 5cf4228 | 2015-05-15 14:15:35 -0700 | [diff] [blame] | 121 | static bool frag_expire_skip_icmp(u32 user) |
| 122 | { |
| 123 | return user == IP_DEFRAG_AF_PACKET || |
| 124 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN, |
Andy Zhou | 8bc0486 | 2015-05-15 14:15:36 -0700 | [diff] [blame] | 125 | __IP_DEFRAG_CONNTRACK_IN_END) || |
| 126 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN, |
| 127 | __IP_DEFRAG_CONNTRACK_BRIDGE_IN); |
Andy Zhou | 5cf4228 | 2015-05-15 14:15:35 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* |
| 131 | * Oops, a fragment queue timed out. Kill it and send an ICMP reply. |
| 132 | */ |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 133 | static void ip_expire(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 135 | struct inet_frag_queue *frag = from_timer(frag, t, timer); |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 136 | const struct iphdr *iph; |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 137 | struct sk_buff *head = NULL; |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 138 | struct net *net; |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 139 | struct ipq *qp; |
| 140 | int err; |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 141 | |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 142 | qp = container_of(frag, struct ipq, q); |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 143 | net = qp->q.fqdir->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 145 | rcu_read_lock(); |
Eric Dumazet | d5dd887 | 2019-06-18 11:09:00 -0700 | [diff] [blame] | 146 | |
| 147 | if (qp->q.fqdir->dead) |
| 148 | goto out_rcu_unlock; |
| 149 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 150 | spin_lock(&qp->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 152 | if (qp->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | goto out; |
| 154 | |
| 155 | ipq_kill(qp); |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 156 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 157 | __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT); |
Nikolay Aleksandrov | 2e404f6 | 2014-08-01 12:29:47 +0200 | [diff] [blame] | 158 | |
Dan Carpenter | 70837ff | 2018-08-06 22:17:35 +0300 | [diff] [blame] | 159 | if (!(qp->q.flags & INET_FRAG_FIRST_IN)) |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 160 | goto out; |
Nikolay Aleksandrov | 2e404f6 | 2014-08-01 12:29:47 +0200 | [diff] [blame] | 161 | |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 162 | /* sk_buff::dev and sk_buff::rbnode are unionized. So we |
| 163 | * pull the head out of the tree in order to be able to |
| 164 | * deal with head->dev. |
| 165 | */ |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 166 | head = inet_frag_pull_head(&qp->q); |
| 167 | if (!head) |
| 168 | goto out; |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 169 | head->dev = dev_get_by_index_rcu(net, qp->iif); |
| 170 | if (!head->dev) |
| 171 | goto out; |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 172 | |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 173 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 174 | /* skb has no dst, perform route lookup again */ |
| 175 | iph = ip_hdr(head); |
| 176 | err = ip_route_input_noref(head, iph->daddr, iph->saddr, |
David S. Miller | c6cffba | 2012-07-26 11:14:38 +0000 | [diff] [blame] | 177 | iph->tos, head->dev); |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 178 | if (err) |
| 179 | goto out; |
Eric Dumazet | 64f3b9e | 2011-05-04 10:02:26 +0000 | [diff] [blame] | 180 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 181 | /* Only an end host needs to send an ICMP |
| 182 | * "Fragment Reassembly Timeout" message, per RFC792. |
| 183 | */ |
| 184 | if (frag_expire_skip_icmp(qp->q.key.v4.user) && |
| 185 | (skb_rtable(head)->rt_type != RTN_LOCAL)) |
| 186 | goto out; |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 187 | |
Eric Dumazet | 1eec5d5 | 2018-03-31 12:58:54 -0700 | [diff] [blame] | 188 | spin_unlock(&qp->q.lock); |
| 189 | icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); |
Eric Dumazet | 1eec5d5 | 2018-03-31 12:58:54 -0700 | [diff] [blame] | 190 | goto out_rcu_unlock; |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | out: |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 193 | spin_unlock(&qp->q.lock); |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 194 | out_rcu_unlock: |
| 195 | rcu_read_unlock(); |
zhong jiang | 1d08962 | 2018-09-20 17:37:43 +0800 | [diff] [blame] | 196 | kfree_skb(head); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 197 | ipq_put(qp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 200 | /* Find the correct entry in the "incomplete datagrams" queue for |
| 201 | * this IP datagram, and create new one, if nothing is found. |
| 202 | */ |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 203 | static struct ipq *ip_find(struct net *net, struct iphdr *iph, |
| 204 | u32 user, int vif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 206 | struct frag_v4_compare_key key = { |
| 207 | .saddr = iph->saddr, |
| 208 | .daddr = iph->daddr, |
| 209 | .user = user, |
| 210 | .vif = vif, |
| 211 | .id = iph->id, |
| 212 | .protocol = iph->protocol, |
| 213 | }; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 214 | struct inet_frag_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 216 | q = inet_frag_find(net->ipv4.fqdir, &key); |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 217 | if (!q) |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 218 | return NULL; |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 219 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 220 | return container_of(q, struct ipq, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 223 | /* Is the fragment too far ahead to be part of ipq? */ |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 224 | static int ip_frag_too_far(struct ipq *qp) |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 225 | { |
| 226 | struct inet_peer *peer = qp->peer; |
Eric Dumazet | 6ce3b4d | 2019-05-24 09:03:30 -0700 | [diff] [blame] | 227 | unsigned int max = qp->q.fqdir->max_dist; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 228 | unsigned int start, end; |
| 229 | |
| 230 | int rc; |
| 231 | |
| 232 | if (!peer || !max) |
| 233 | return 0; |
| 234 | |
| 235 | start = qp->rid; |
| 236 | end = atomic_inc_return(&peer->rid); |
| 237 | qp->rid = end; |
| 238 | |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 239 | rc = qp->q.fragments_tail && (end - start) > max; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 240 | |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 241 | if (rc) |
| 242 | __IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 243 | |
| 244 | return rc; |
| 245 | } |
| 246 | |
| 247 | static int ip_frag_reinit(struct ipq *qp) |
| 248 | { |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 249 | unsigned int sum_truesize = 0; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 250 | |
Eric Dumazet | 6ce3b4d | 2019-05-24 09:03:30 -0700 | [diff] [blame] | 251 | if (!mod_timer(&qp->q.timer, jiffies + qp->q.fqdir->timeout)) { |
Reshetova, Elena | edcb691 | 2017-06-30 13:08:07 +0300 | [diff] [blame] | 252 | refcount_inc(&qp->q.refcnt); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 253 | return -ETIMEDOUT; |
| 254 | } |
| 255 | |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 256 | sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments); |
Eric Dumazet | 6ce3b4d | 2019-05-24 09:03:30 -0700 | [diff] [blame] | 257 | sub_frag_mem_limit(qp->q.fqdir, sum_truesize); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 258 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 259 | qp->q.flags = 0; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 260 | qp->q.len = 0; |
| 261 | qp->q.meat = 0; |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 262 | qp->q.rb_fragments = RB_ROOT; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 263 | qp->q.fragments_tail = NULL; |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 264 | qp->q.last_run_head = NULL; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 265 | qp->iif = 0; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 266 | qp->ecn = 0; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | /* Add new segment to existing queue. */ |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 272 | static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 274 | struct net *net = qp->q.fqdir->net; |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 275 | int ihl, end, flags, offset; |
| 276 | struct sk_buff *prev_tail; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 277 | struct net_device *dev; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 278 | unsigned int fragsize; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 279 | int err = -ENOENT; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 280 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 282 | if (qp->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | goto err; |
| 284 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 285 | if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 286 | unlikely(ip_frag_too_far(qp)) && |
| 287 | unlikely(err = ip_frag_reinit(qp))) { |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 288 | ipq_kill(qp); |
| 289 | goto err; |
| 290 | } |
| 291 | |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 292 | ecn = ip4_frag_ecn(ip_hdr(skb)->tos); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 293 | offset = ntohs(ip_hdr(skb)->frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | flags = offset & ~IP_OFFSET; |
| 295 | offset &= IP_OFFSET; |
| 296 | offset <<= 3; /* offset is in 8-byte chunks */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 297 | ihl = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | /* Determine the position of this fragment. */ |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 300 | end = offset + skb->len - skb_network_offset(skb) - ihl; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 301 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | /* Is this the final fragment? */ |
| 304 | if ((flags & IP_MF) == 0) { |
| 305 | /* If we already have some bits beyond end |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 306 | * or have different end, the segment is corrupted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 308 | if (end < qp->q.len || |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 309 | ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 310 | goto discard_qp; |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 311 | qp->q.flags |= INET_FRAG_LAST_IN; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 312 | qp->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } else { |
| 314 | if (end&7) { |
| 315 | end &= ~7; |
| 316 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
| 317 | skb->ip_summed = CHECKSUM_NONE; |
| 318 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 319 | if (end > qp->q.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* Some bits beyond end -> corruption. */ |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 321 | if (qp->q.flags & INET_FRAG_LAST_IN) |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 322 | goto discard_qp; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 323 | qp->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | if (end == offset) |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 327 | goto discard_qp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 329 | err = -ENOMEM; |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 330 | if (!pskb_pull(skb, skb_network_offset(skb) + ihl)) |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 331 | goto discard_qp; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 332 | |
| 333 | err = pskb_trim_rcsum(skb, end - offset); |
| 334 | if (err) |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 335 | goto discard_qp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 337 | /* Note : skb->rbnode and skb->dev share the same location. */ |
| 338 | dev = skb->dev; |
| 339 | /* Makes sure compiler wont do silly aliasing games */ |
| 340 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 342 | prev_tail = qp->q.fragments_tail; |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 343 | err = inet_frag_queue_insert(&qp->q, skb, offset, end); |
| 344 | if (err) |
| 345 | goto insert_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 347 | if (dev) |
| 348 | qp->iif = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 350 | qp->q.stamp = skb->tstamp; |
| 351 | qp->q.meat += skb->len; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 352 | qp->ecn |= ecn; |
Eric Dumazet | 6ce3b4d | 2019-05-24 09:03:30 -0700 | [diff] [blame] | 353 | add_frag_mem_limit(qp->q.fqdir, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | if (offset == 0) |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 355 | qp->q.flags |= INET_FRAG_FIRST_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 357 | fragsize = skb->len + ihl; |
| 358 | |
| 359 | if (fragsize > qp->q.max_size) |
| 360 | qp->q.max_size = fragsize; |
| 361 | |
Patrick McHardy | 5f2d04f | 2012-08-26 19:13:55 +0200 | [diff] [blame] | 362 | if (ip_hdr(skb)->frag_off & htons(IP_DF) && |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 363 | fragsize > qp->max_df_size) |
| 364 | qp->max_df_size = fragsize; |
Patrick McHardy | 5f2d04f | 2012-08-26 19:13:55 +0200 | [diff] [blame] | 365 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 366 | if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 367 | qp->q.meat == qp->q.len) { |
| 368 | unsigned long orefdst = skb->_skb_refdst; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 369 | |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 370 | skb->_skb_refdst = 0UL; |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 371 | err = ip_frag_reasm(qp, skb, prev_tail, dev); |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 372 | skb->_skb_refdst = orefdst; |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 373 | if (err) |
| 374 | inet_frag_kill(&qp->q); |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 375 | return err; |
| 376 | } |
| 377 | |
| 378 | skb_dst_drop(skb); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 379 | return -EINPROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 381 | insert_error: |
| 382 | if (err == IPFRAG_DUP) { |
| 383 | kfree_skb(skb); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | err = -EINVAL; |
Peter Oskolkov | 0ff89ef | 2018-08-28 11:36:19 -0700 | [diff] [blame] | 387 | __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS); |
Peter Oskolkov | 7969e5c | 2018-08-02 23:34:37 +0000 | [diff] [blame] | 388 | discard_qp: |
| 389 | inet_frag_kill(&qp->q); |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 390 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | err: |
| 392 | kfree_skb(skb); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 393 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Guillaume Nault | 891584f | 2019-08-02 17:15:03 +0200 | [diff] [blame] | 396 | static bool ip_frag_coalesce_ok(const struct ipq *qp) |
| 397 | { |
| 398 | return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER; |
| 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* Build a new IP datagram from all its fragments. */ |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 402 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 403 | struct sk_buff *prev_tail, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
Eric Dumazet | a39aca6 | 2019-05-24 09:03:38 -0700 | [diff] [blame] | 405 | struct net *net = qp->q.fqdir->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | struct iphdr *iph; |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 407 | void *reasm_data; |
| 408 | int len, err; |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 409 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | ipq_kill(qp); |
| 412 | |
Hannes Frederic Sowa | be99197 | 2013-03-22 08:24:37 +0000 | [diff] [blame] | 413 | ecn = ip_frag_ecn_table[qp->ecn]; |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 414 | if (unlikely(ecn == 0xff)) { |
| 415 | err = -EINVAL; |
| 416 | goto out_fail; |
| 417 | } |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 418 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 419 | /* Make the one we just received the head. */ |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 420 | reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail); |
| 421 | if (!reasm_data) |
| 422 | goto out_nomem; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 423 | |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 424 | len = ip_hdrlen(skb) + qp->q.len; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 425 | err = -E2BIG; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 426 | if (len > 65535) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | goto out_oversize; |
| 428 | |
Guillaume Nault | 891584f | 2019-08-02 17:15:03 +0200 | [diff] [blame] | 429 | inet_frag_reasm_finish(&qp->q, skb, reasm_data, |
| 430 | ip_frag_coalesce_ok(qp)); |
Jiri Wiesner | ebaf39e | 2018-12-05 16:55:29 +0100 | [diff] [blame] | 431 | |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 432 | skb->dev = dev; |
| 433 | IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 435 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | iph->tot_len = htons(len); |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 437 | iph->tos |= ecn; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 438 | |
| 439 | /* When we set IP_DF on a refragmented skb we must also force a |
| 440 | * call to ip_fragment to avoid forwarding a DF-skb of size s while |
| 441 | * original sender only sent fragments of size f (where f < s). |
| 442 | * |
| 443 | * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest |
| 444 | * frag seen to avoid sending tiny DF-fragments in case skb was built |
| 445 | * from one very small df-fragment and one large non-df frag. |
| 446 | */ |
| 447 | if (qp->max_df_size == qp->q.max_size) { |
Peter Oskolkov | c23f35d | 2019-01-22 10:02:50 -0800 | [diff] [blame] | 448 | IPCB(skb)->flags |= IPSKB_FRAG_PMTU; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 449 | iph->frag_off = htons(IP_DF); |
| 450 | } else { |
| 451 | iph->frag_off = 0; |
| 452 | } |
| 453 | |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 454 | ip_send_check(iph); |
| 455 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 456 | __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); |
Peter Oskolkov | fa0f527 | 2018-08-02 23:34:39 +0000 | [diff] [blame] | 457 | qp->q.rb_fragments = RB_ROOT; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 458 | qp->q.fragments_tail = NULL; |
Peter Oskolkov | a4fd284 | 2018-08-11 20:27:25 +0000 | [diff] [blame] | 459 | qp->q.last_run_head = NULL; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 460 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | out_nomem: |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 463 | net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp); |
David Howells | 4554247 | 2007-10-17 21:37:22 -0700 | [diff] [blame] | 464 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | goto out_fail; |
| 466 | out_oversize: |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 467 | net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | out_fail: |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 469 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 470 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* Process an incoming IP datagram fragment. */ |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 474 | int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 476 | struct net_device *dev = skb->dev ? : skb_dst(skb)->dev; |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 477 | int vif = l3mdev_master_ifindex_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | struct ipq *qp; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 479 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 480 | __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); |
Joe Stringer | 8282f27 | 2016-01-22 15:49:12 -0800 | [diff] [blame] | 481 | skb_orphan(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | /* Lookup (or create) queue header */ |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 484 | qp = ip_find(net, ip_hdr(skb), user, vif); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 485 | if (qp) { |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 486 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 488 | spin_lock(&qp->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 490 | ret = ip_frag_queue(qp, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 492 | spin_unlock(&qp->q.lock); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 493 | ipq_put(qp); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 494 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 497 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | kfree_skb(skb); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 499 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 501 | EXPORT_SYMBOL(ip_defrag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 503 | struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 504 | { |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 505 | struct iphdr iph; |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 506 | int netoff; |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 507 | u32 len; |
| 508 | |
| 509 | if (skb->protocol != htons(ETH_P_IP)) |
| 510 | return skb; |
| 511 | |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 512 | netoff = skb_network_offset(skb); |
| 513 | |
| 514 | if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 515 | return skb; |
| 516 | |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 517 | if (iph.ihl < 5 || iph.version != 4) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 518 | return skb; |
| 519 | |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 520 | len = ntohs(iph.tot_len); |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 521 | if (skb->len < netoff + len || len < (iph.ihl * 4)) |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 522 | return skb; |
| 523 | |
| 524 | if (ip_is_fragment(&iph)) { |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 525 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 526 | if (skb) { |
Cong Wang | 7de414a | 2018-11-01 12:02:37 -0700 | [diff] [blame] | 527 | if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) { |
| 528 | kfree_skb(skb); |
| 529 | return NULL; |
| 530 | } |
| 531 | if (pskb_trim_rcsum(skb, netoff + len)) { |
| 532 | kfree_skb(skb); |
| 533 | return NULL; |
| 534 | } |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 535 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 536 | if (ip_defrag(net, skb, user)) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 537 | return NULL; |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 538 | skb_clear_hash(skb); |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | return skb; |
| 542 | } |
| 543 | EXPORT_SYMBOL(ip_check_defrag); |
| 544 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 545 | #ifdef CONFIG_SYSCTL |
Eric Dumazet | 3d23401 | 2018-04-04 08:35:10 -0700 | [diff] [blame] | 546 | static int dist_min; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 547 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 548 | static struct ctl_table ip4_frags_ns_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 549 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 550 | .procname = "ipfrag_high_thresh", |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 551 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 552 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 553 | .proc_handler = proc_doulongvec_minmax, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 554 | }, |
| 555 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 556 | .procname = "ipfrag_low_thresh", |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 557 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 558 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 559 | .proc_handler = proc_doulongvec_minmax, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 560 | }, |
| 561 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 562 | .procname = "ipfrag_time", |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 563 | .maxlen = sizeof(int), |
| 564 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 565 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 566 | }, |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 567 | { |
| 568 | .procname = "ipfrag_max_dist", |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 569 | .maxlen = sizeof(int), |
| 570 | .mode = 0644, |
| 571 | .proc_handler = proc_dointvec_minmax, |
Eric Dumazet | 3d23401 | 2018-04-04 08:35:10 -0700 | [diff] [blame] | 572 | .extra1 = &dist_min, |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 573 | }, |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 574 | { } |
| 575 | }; |
| 576 | |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 577 | /* secret interval has been deprecated */ |
| 578 | static int ip4_frags_secret_interval_unused; |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 579 | static struct ctl_table ip4_frags_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 580 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 581 | .procname = "ipfrag_secret_interval", |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 582 | .data = &ip4_frags_secret_interval_unused, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 583 | .maxlen = sizeof(int), |
| 584 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 585 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 586 | }, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 587 | { } |
| 588 | }; |
| 589 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 590 | static int __net_init ip4_frags_ns_ctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 591 | { |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 592 | struct ctl_table *table; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 593 | struct ctl_table_header *hdr; |
| 594 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 595 | table = ip4_frags_ns_ctl_table; |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 596 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 597 | table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 598 | if (!table) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 599 | goto err_alloc; |
| 600 | |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 601 | } |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 602 | table[0].data = &net->ipv4.fqdir->high_thresh; |
| 603 | table[0].extra1 = &net->ipv4.fqdir->low_thresh; |
| 604 | table[1].data = &net->ipv4.fqdir->low_thresh; |
| 605 | table[1].extra2 = &net->ipv4.fqdir->high_thresh; |
| 606 | table[2].data = &net->ipv4.fqdir->timeout; |
| 607 | table[3].data = &net->ipv4.fqdir->max_dist; |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 608 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 609 | hdr = register_net_sysctl(net, "net/ipv4", table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 610 | if (!hdr) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 611 | goto err_reg; |
| 612 | |
| 613 | net->ipv4.frags_hdr = hdr; |
| 614 | return 0; |
| 615 | |
| 616 | err_reg: |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 617 | if (!net_eq(net, &init_net)) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 618 | kfree(table); |
| 619 | err_alloc: |
| 620 | return -ENOMEM; |
| 621 | } |
| 622 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 623 | static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 624 | { |
| 625 | struct ctl_table *table; |
| 626 | |
| 627 | table = net->ipv4.frags_hdr->ctl_table_arg; |
| 628 | unregister_net_sysctl_table(net->ipv4.frags_hdr); |
| 629 | kfree(table); |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 630 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 631 | |
Fabian Frederick | 57a02c3 | 2014-10-01 19:18:57 +0200 | [diff] [blame] | 632 | static void __init ip4_frags_ctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 633 | { |
Eric W. Biederman | 4344475 | 2012-04-19 13:22:55 +0000 | [diff] [blame] | 634 | register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 635 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 636 | #else |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 637 | static int ip4_frags_ns_ctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 638 | { |
| 639 | return 0; |
| 640 | } |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 641 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 642 | static void ip4_frags_ns_ctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 643 | { |
| 644 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 645 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 646 | static void __init ip4_frags_ctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 647 | { |
| 648 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 649 | #endif |
| 650 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 651 | static int __net_init ipv4_frags_init_net(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 652 | { |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 653 | int res; |
| 654 | |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 655 | res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net); |
| 656 | if (res < 0) |
| 657 | return res; |
Jesper Dangaard Brouer | c2a9366 | 2013-01-15 07:16:35 +0000 | [diff] [blame] | 658 | /* Fragment cache limits. |
| 659 | * |
| 660 | * The fragment memory accounting code, (tries to) account for |
| 661 | * the real memory usage, by measuring both the size of frag |
| 662 | * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) |
| 663 | * and the SKB's truesize. |
| 664 | * |
| 665 | * A 64K fragment consumes 129736 bytes (44*2944)+200 |
| 666 | * (1500 truesize == 2944, sizeof(struct ipq) == 200) |
| 667 | * |
| 668 | * We will commit 4MB at one time. Should we cross that limit |
| 669 | * we will prune down to 3MB, making room for approx 8 big 64K |
| 670 | * fragments 8x128k. |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 671 | */ |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 672 | net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024; |
| 673 | net->ipv4.fqdir->low_thresh = 3 * 1024 * 1024; |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 674 | /* |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 675 | * Important NOTE! Fragment queue must be destroyed before MSL expires. |
| 676 | * RFC791 is wrong proposing to prolongate timer each fragment arrival |
| 677 | * by TTL. |
| 678 | */ |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 679 | net->ipv4.fqdir->timeout = IP_FRAG_TIME; |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 680 | |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 681 | net->ipv4.fqdir->max_dist = 64; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 682 | |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 683 | res = ip4_frags_ns_ctl_register(net); |
| 684 | if (res < 0) |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 685 | fqdir_exit(net->ipv4.fqdir); |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 686 | return res; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Eric Dumazet | d5dd887 | 2019-06-18 11:09:00 -0700 | [diff] [blame] | 689 | static void __net_exit ipv4_frags_pre_exit_net(struct net *net) |
| 690 | { |
| 691 | fqdir_pre_exit(net->ipv4.fqdir); |
| 692 | } |
| 693 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 694 | static void __net_exit ipv4_frags_exit_net(struct net *net) |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 695 | { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 696 | ip4_frags_ns_ctl_unregister(net); |
Eric Dumazet | 4907abc | 2019-05-24 09:03:39 -0700 | [diff] [blame] | 697 | fqdir_exit(net->ipv4.fqdir); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static struct pernet_operations ip4_frags_ops = { |
Eric Dumazet | d5dd887 | 2019-06-18 11:09:00 -0700 | [diff] [blame] | 701 | .init = ipv4_frags_init_net, |
| 702 | .pre_exit = ipv4_frags_pre_exit_net, |
| 703 | .exit = ipv4_frags_exit_net, |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 704 | }; |
| 705 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 706 | |
| 707 | static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed) |
| 708 | { |
| 709 | return jhash2(data, |
| 710 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); |
| 711 | } |
| 712 | |
| 713 | static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed) |
| 714 | { |
| 715 | const struct inet_frag_queue *fq = data; |
| 716 | |
| 717 | return jhash2((const u32 *)&fq->key.v4, |
| 718 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); |
| 719 | } |
| 720 | |
| 721 | static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) |
| 722 | { |
| 723 | const struct frag_v4_compare_key *key = arg->key; |
| 724 | const struct inet_frag_queue *fq = ptr; |
| 725 | |
| 726 | return !!memcmp(&fq->key, key, sizeof(*key)); |
| 727 | } |
| 728 | |
| 729 | static const struct rhashtable_params ip4_rhash_params = { |
| 730 | .head_offset = offsetof(struct inet_frag_queue, node), |
| 731 | .key_offset = offsetof(struct inet_frag_queue, key), |
| 732 | .key_len = sizeof(struct frag_v4_compare_key), |
| 733 | .hashfn = ip4_key_hashfn, |
| 734 | .obj_hashfn = ip4_obj_hashfn, |
| 735 | .obj_cmpfn = ip4_obj_cmpfn, |
| 736 | .automatic_shrinking = true, |
| 737 | }; |
| 738 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 739 | void __init ipfrag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | { |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 741 | ip4_frags.constructor = ip4_frag_init; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 742 | ip4_frags.destructor = ip4_frag_free; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 743 | ip4_frags.qsize = sizeof(struct ipq); |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 744 | ip4_frags.frag_expire = ip_expire; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 745 | ip4_frags.frags_cache_name = ip_frag_cache_name; |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 746 | ip4_frags.rhash_params = ip4_rhash_params; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 747 | if (inet_frags_init(&ip4_frags)) |
| 748 | panic("IP: failed to allocate ip4_frags cache\n"); |
Eric Dumazet | 483a6e4 | 2018-03-31 12:58:47 -0700 | [diff] [blame] | 749 | ip4_frags_ctl_register(); |
| 750 | register_pernet_subsys(&ip4_frags_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |