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