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 | |
Peter Oskolkov | e9e4ac4 | 2018-10-10 12:30:14 -0700 | [diff] [blame] | 59 | /* Use skb->cb to track consecutive/adjacent fragments coming at |
| 60 | * the end of the queue. Nodes in the rb-tree queue will |
| 61 | * contain "runs" of one or more adjacent fragments. |
| 62 | * |
| 63 | * Invariants: |
| 64 | * - next_frag is NULL at the tail of a "run"; |
| 65 | * - the head of a "run" has the sum of all fragment lengths in frag_run_len. |
| 66 | */ |
| 67 | struct ipfrag_skb_cb { |
| 68 | struct inet_skb_parm h; |
| 69 | struct sk_buff *next_frag; |
| 70 | int frag_run_len; |
| 71 | }; |
| 72 | |
| 73 | #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb)) |
| 74 | |
| 75 | static void ip4_frag_init_run(struct sk_buff *skb) |
| 76 | { |
| 77 | BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb)); |
| 78 | |
| 79 | FRAG_CB(skb)->next_frag = NULL; |
| 80 | FRAG_CB(skb)->frag_run_len = skb->len; |
| 81 | } |
| 82 | |
| 83 | /* Append skb to the last "run". */ |
| 84 | static void ip4_frag_append_to_last_run(struct inet_frag_queue *q, |
| 85 | struct sk_buff *skb) |
| 86 | { |
| 87 | RB_CLEAR_NODE(&skb->rbnode); |
| 88 | FRAG_CB(skb)->next_frag = NULL; |
| 89 | |
| 90 | FRAG_CB(q->last_run_head)->frag_run_len += skb->len; |
| 91 | FRAG_CB(q->fragments_tail)->next_frag = skb; |
| 92 | q->fragments_tail = skb; |
| 93 | } |
| 94 | |
| 95 | /* Create a new "run" with the skb. */ |
| 96 | static void ip4_frag_create_run(struct inet_frag_queue *q, struct sk_buff *skb) |
| 97 | { |
| 98 | if (q->last_run_head) |
| 99 | rb_link_node(&skb->rbnode, &q->last_run_head->rbnode, |
| 100 | &q->last_run_head->rbnode.rb_right); |
| 101 | else |
| 102 | rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node); |
| 103 | rb_insert_color(&skb->rbnode, &q->rb_fragments); |
| 104 | |
| 105 | ip4_frag_init_run(skb); |
| 106 | q->fragments_tail = skb; |
| 107 | q->last_run_head = skb; |
| 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* Describe an entry in the "incomplete datagrams" queue. */ |
| 111 | struct ipq { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 112 | struct inet_frag_queue q; |
| 113 | |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 114 | u8 ecn; /* RFC3168 support */ |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 115 | u16 max_df_size; /* largest frag with DF set seen */ |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 116 | int iif; |
| 117 | unsigned int rid; |
| 118 | struct inet_peer *peer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 121 | static u8 ip4_frag_ecn(u8 tos) |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 122 | { |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 123 | return 1 << (tos & INET_ECN_MASK); |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 126 | static struct inet_frags ip4_frags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 128 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, |
| 129 | struct sk_buff *prev_tail, struct net_device *dev); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 130 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 131 | |
Florian Westphal | 36c7778 | 2014-07-24 16:50:29 +0200 | [diff] [blame] | 132 | 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] | 133 | { |
| 134 | struct ipq *qp = container_of(q, struct ipq, q); |
Gao feng | 54db0cc | 2012-06-08 01:21:40 +0000 | [diff] [blame] | 135 | struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4, |
| 136 | frags); |
| 137 | struct net *net = container_of(ipv4, struct net, ipv4); |
| 138 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 139 | const struct frag_v4_compare_key *key = a; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 140 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 141 | q->key.v4 = *key; |
| 142 | qp->ecn = 0; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 143 | qp->peer = q->net->max_dist ? |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 144 | inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) : |
David Ahern | 192132b | 2015-08-27 16:07:03 -0700 | [diff] [blame] | 145 | NULL; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 148 | static void ip4_frag_free(struct inet_frag_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 150 | struct ipq *qp; |
| 151 | |
| 152 | qp = container_of(q, struct ipq, q); |
| 153 | if (qp->peer) |
| 154 | inet_putpeer(qp->peer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | /* Destruction primitives. */ |
| 159 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 160 | static void ipq_put(struct ipq *ipq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 162 | inet_frag_put(&ipq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Kill ipq entry. It is not destroyed immediately, |
| 166 | * because caller (and someone more) holds reference count. |
| 167 | */ |
| 168 | static void ipq_kill(struct ipq *ipq) |
| 169 | { |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 170 | inet_frag_kill(&ipq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Andy Zhou | 5cf4228 | 2015-05-15 14:15:35 -0700 | [diff] [blame] | 173 | static bool frag_expire_skip_icmp(u32 user) |
| 174 | { |
| 175 | return user == IP_DEFRAG_AF_PACKET || |
| 176 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN, |
Andy Zhou | 8bc0486 | 2015-05-15 14:15:36 -0700 | [diff] [blame] | 177 | __IP_DEFRAG_CONNTRACK_IN_END) || |
| 178 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN, |
| 179 | __IP_DEFRAG_CONNTRACK_BRIDGE_IN); |
Andy Zhou | 5cf4228 | 2015-05-15 14:15:35 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
| 183 | * Oops, a fragment queue timed out. Kill it and send an ICMP reply. |
| 184 | */ |
| 185 | static void ip_expire(unsigned long arg) |
| 186 | { |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 187 | const struct iphdr *iph; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 188 | struct sk_buff *head = NULL; |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 189 | struct net *net; |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 190 | struct ipq *qp; |
| 191 | int err; |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 192 | |
| 193 | qp = container_of((struct inet_frag_queue *) arg, struct ipq, q); |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 194 | net = container_of(qp->q.net, struct net, ipv4.frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Eric Dumazet | 7656871 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 196 | rcu_read_lock(); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 197 | spin_lock(&qp->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 199 | if (qp->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | goto out; |
| 201 | |
| 202 | ipq_kill(qp); |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 203 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 204 | __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT); |
Nikolay Aleksandrov | 2e404f6 | 2014-08-01 12:29:47 +0200 | [diff] [blame] | 205 | |
Dan Carpenter | 5a0f340 | 2018-10-10 12:30:17 -0700 | [diff] [blame^] | 206 | if (!(qp->q.flags & INET_FRAG_FIRST_IN)) |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 207 | goto out; |
Nikolay Aleksandrov | 2e404f6 | 2014-08-01 12:29:47 +0200 | [diff] [blame] | 208 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 209 | /* sk_buff::dev and sk_buff::rbnode are unionized. So we |
| 210 | * pull the head out of the tree in order to be able to |
| 211 | * deal with head->dev. |
| 212 | */ |
| 213 | if (qp->q.fragments) { |
| 214 | head = qp->q.fragments; |
| 215 | qp->q.fragments = head->next; |
| 216 | } else { |
| 217 | head = skb_rb_first(&qp->q.rb_fragments); |
| 218 | if (!head) |
| 219 | goto out; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 220 | if (FRAG_CB(head)->next_frag) |
| 221 | rb_replace_node(&head->rbnode, |
| 222 | &FRAG_CB(head)->next_frag->rbnode, |
| 223 | &qp->q.rb_fragments); |
| 224 | else |
| 225 | rb_erase(&head->rbnode, &qp->q.rb_fragments); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 226 | memset(&head->rbnode, 0, sizeof(head->rbnode)); |
| 227 | barrier(); |
| 228 | } |
| 229 | if (head == qp->q.fragments_tail) |
| 230 | qp->q.fragments_tail = NULL; |
| 231 | |
| 232 | sub_frag_mem_limit(qp->q.net, head->truesize); |
| 233 | |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 234 | head->dev = dev_get_by_index_rcu(net, qp->iif); |
| 235 | if (!head->dev) |
| 236 | goto out; |
Eric Dumazet | 7656871 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 237 | |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 238 | |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 239 | /* skb has no dst, perform route lookup again */ |
| 240 | iph = ip_hdr(head); |
| 241 | err = ip_route_input_noref(head, iph->daddr, iph->saddr, |
David S. Miller | c6cffba | 2012-07-26 11:14:38 +0000 | [diff] [blame] | 242 | iph->tos, head->dev); |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 243 | if (err) |
| 244 | goto out; |
Eric Dumazet | 64f3b9e | 2011-05-04 10:02:26 +0000 | [diff] [blame] | 245 | |
Eric Dumazet | 49106f3 | 2018-10-10 12:29:58 -0700 | [diff] [blame] | 246 | /* Only an end host needs to send an ICMP |
| 247 | * "Fragment Reassembly Timeout" message, per RFC792. |
| 248 | */ |
| 249 | if (frag_expire_skip_icmp(qp->q.key.v4.user) && |
| 250 | (skb_rtable(head)->rt_type != RTN_LOCAL)) |
| 251 | goto out; |
Eric Dumazet | 7656871 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 252 | |
Eric Dumazet | 7a87ec9 | 2018-10-10 12:30:01 -0700 | [diff] [blame] | 253 | spin_unlock(&qp->q.lock); |
| 254 | icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); |
Eric Dumazet | 7a87ec9 | 2018-10-10 12:30:01 -0700 | [diff] [blame] | 255 | goto out_rcu_unlock; |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | out: |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 258 | spin_unlock(&qp->q.lock); |
Eric Dumazet | 7656871 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 259 | out_rcu_unlock: |
| 260 | rcu_read_unlock(); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 261 | if (head) |
| 262 | kfree_skb(head); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 263 | ipq_put(qp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 266 | /* Find the correct entry in the "incomplete datagrams" queue for |
| 267 | * this IP datagram, and create new one, if nothing is found. |
| 268 | */ |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 269 | static struct ipq *ip_find(struct net *net, struct iphdr *iph, |
| 270 | u32 user, int vif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 272 | struct frag_v4_compare_key key = { |
| 273 | .saddr = iph->saddr, |
| 274 | .daddr = iph->daddr, |
| 275 | .user = user, |
| 276 | .vif = vif, |
| 277 | .id = iph->id, |
| 278 | .protocol = iph->protocol, |
| 279 | }; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 280 | struct inet_frag_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 282 | q = inet_frag_find(&net->ipv4.frags, &key); |
Eric Dumazet | 965e2ad | 2018-10-10 12:29:59 -0700 | [diff] [blame] | 283 | if (!q) |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 284 | return NULL; |
Eric Dumazet | 965e2ad | 2018-10-10 12:29:59 -0700 | [diff] [blame] | 285 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 286 | return container_of(q, struct ipq, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 289 | /* Is the fragment too far ahead to be part of ipq? */ |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 290 | static int ip_frag_too_far(struct ipq *qp) |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 291 | { |
| 292 | struct inet_peer *peer = qp->peer; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 293 | unsigned int max = qp->q.net->max_dist; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 294 | unsigned int start, end; |
| 295 | |
| 296 | int rc; |
| 297 | |
| 298 | if (!peer || !max) |
| 299 | return 0; |
| 300 | |
| 301 | start = qp->rid; |
| 302 | end = atomic_inc_return(&peer->rid); |
| 303 | qp->rid = end; |
| 304 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 305 | rc = qp->q.fragments_tail && (end - start) > max; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 306 | |
| 307 | if (rc) { |
Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 308 | struct net *net; |
| 309 | |
| 310 | net = container_of(qp->q.net, struct net, ipv4.frags); |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 311 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return rc; |
| 315 | } |
| 316 | |
| 317 | static int ip_frag_reinit(struct ipq *qp) |
| 318 | { |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 319 | unsigned int sum_truesize = 0; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 320 | |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 321 | if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 322 | atomic_inc(&qp->q.refcnt); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 323 | return -ETIMEDOUT; |
| 324 | } |
| 325 | |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 326 | sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments); |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 327 | sub_frag_mem_limit(qp->q.net, sum_truesize); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 328 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 329 | qp->q.flags = 0; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 330 | qp->q.len = 0; |
| 331 | qp->q.meat = 0; |
| 332 | qp->q.fragments = NULL; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 333 | qp->q.rb_fragments = RB_ROOT; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 334 | qp->q.fragments_tail = NULL; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 335 | qp->q.last_run_head = NULL; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 336 | qp->iif = 0; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 337 | qp->ecn = 0; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* Add new segment to existing queue. */ |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 343 | static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Peter Oskolkov | 82f36cb | 2018-10-10 12:30:07 -0700 | [diff] [blame] | 345 | struct net *net = container_of(qp->q.net, struct net, ipv4.frags); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 346 | struct rb_node **rbn, *parent; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 347 | struct sk_buff *skb1, *prev_tail; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 348 | struct net_device *dev; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 349 | unsigned int fragsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int flags, offset; |
| 351 | int ihl, end; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 352 | int err = -ENOENT; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 353 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 355 | if (qp->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | goto err; |
| 357 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 358 | if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 359 | unlikely(ip_frag_too_far(qp)) && |
| 360 | unlikely(err = ip_frag_reinit(qp))) { |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 361 | ipq_kill(qp); |
| 362 | goto err; |
| 363 | } |
| 364 | |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 365 | ecn = ip4_frag_ecn(ip_hdr(skb)->tos); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 366 | offset = ntohs(ip_hdr(skb)->frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | flags = offset & ~IP_OFFSET; |
| 368 | offset &= IP_OFFSET; |
| 369 | offset <<= 3; /* offset is in 8-byte chunks */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 370 | ihl = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | /* Determine the position of this fragment. */ |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 373 | end = offset + skb->len - skb_network_offset(skb) - ihl; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 374 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | /* Is this the final fragment? */ |
| 377 | if ((flags & IP_MF) == 0) { |
| 378 | /* If we already have some bits beyond end |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 379 | * or have different end, the segment is corrupted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 381 | if (end < qp->q.len || |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 382 | ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | goto err; |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 384 | qp->q.flags |= INET_FRAG_LAST_IN; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 385 | qp->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } else { |
| 387 | if (end&7) { |
| 388 | end &= ~7; |
| 389 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
| 390 | skb->ip_summed = CHECKSUM_NONE; |
| 391 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 392 | if (end > qp->q.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* Some bits beyond end -> corruption. */ |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 394 | if (qp->q.flags & INET_FRAG_LAST_IN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | goto err; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 396 | qp->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | if (end == offset) |
| 400 | goto err; |
| 401 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 402 | err = -ENOMEM; |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 403 | if (!pskb_pull(skb, skb_network_offset(skb) + ihl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | goto err; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 405 | |
| 406 | err = pskb_trim_rcsum(skb, end - offset); |
| 407 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | goto err; |
| 409 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 410 | /* Note : skb->rbnode and skb->dev share the same location. */ |
| 411 | dev = skb->dev; |
| 412 | /* Makes sure compiler wont do silly aliasing games */ |
| 413 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Peter Oskolkov | 82f36cb | 2018-10-10 12:30:07 -0700 | [diff] [blame] | 415 | /* RFC5722, Section 4, amended by Errata ID : 3089 |
| 416 | * When reassembling an IPv6 datagram, if |
| 417 | * one or more its constituent fragments is determined to be an |
| 418 | * overlapping fragment, the entire datagram (and any constituent |
| 419 | * fragments) MUST be silently discarded. |
| 420 | * |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 421 | * We do the same here for IPv4 (and increment an snmp counter). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 424 | /* Find out where to put this fragment. */ |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 425 | prev_tail = qp->q.fragments_tail; |
| 426 | if (!prev_tail) |
| 427 | ip4_frag_create_run(&qp->q, skb); /* First fragment. */ |
| 428 | else if (prev_tail->ip_defrag_offset + prev_tail->len < end) { |
| 429 | /* This is the common case: skb goes to the end. */ |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 430 | /* Detect and discard overlaps. */ |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 431 | if (offset < prev_tail->ip_defrag_offset + prev_tail->len) |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 432 | goto discard_qp; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 433 | if (offset == prev_tail->ip_defrag_offset + prev_tail->len) |
| 434 | ip4_frag_append_to_last_run(&qp->q, skb); |
| 435 | else |
| 436 | ip4_frag_create_run(&qp->q, skb); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 437 | } else { |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 438 | /* Binary search. Note that skb can become the first fragment, |
| 439 | * but not the last (covered above). |
| 440 | */ |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 441 | rbn = &qp->q.rb_fragments.rb_node; |
| 442 | do { |
| 443 | parent = *rbn; |
| 444 | skb1 = rb_to_skb(parent); |
| 445 | if (end <= skb1->ip_defrag_offset) |
| 446 | rbn = &parent->rb_left; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 447 | else if (offset >= skb1->ip_defrag_offset + |
| 448 | FRAG_CB(skb1)->frag_run_len) |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 449 | rbn = &parent->rb_right; |
| 450 | else /* Found an overlap with skb1. */ |
| 451 | goto discard_qp; |
| 452 | } while (*rbn); |
| 453 | /* Here we have parent properly set, and rbn pointing to |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 454 | * one of its NULL left/right children. Insert skb. |
| 455 | */ |
| 456 | ip4_frag_init_run(skb); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 457 | rb_link_node(&skb->rbnode, parent, rbn); |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 458 | rb_insert_color(&skb->rbnode, &qp->q.rb_fragments); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Eric Dumazet | d838486 | 2018-10-10 12:30:06 -0700 | [diff] [blame] | 461 | if (dev) |
| 462 | qp->iif = dev->ifindex; |
Eric Dumazet | d838486 | 2018-10-10 12:30:06 -0700 | [diff] [blame] | 463 | skb->ip_defrag_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 465 | qp->q.stamp = skb->tstamp; |
| 466 | qp->q.meat += skb->len; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 467 | qp->ecn |= ecn; |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 468 | add_frag_mem_limit(qp->q.net, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (offset == 0) |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 470 | qp->q.flags |= INET_FRAG_FIRST_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 472 | fragsize = skb->len + ihl; |
| 473 | |
| 474 | if (fragsize > qp->q.max_size) |
| 475 | qp->q.max_size = fragsize; |
| 476 | |
Patrick McHardy | 5f2d04f | 2012-08-26 19:13:55 +0200 | [diff] [blame] | 477 | if (ip_hdr(skb)->frag_off & htons(IP_DF) && |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 478 | fragsize > qp->max_df_size) |
| 479 | qp->max_df_size = fragsize; |
Patrick McHardy | 5f2d04f | 2012-08-26 19:13:55 +0200 | [diff] [blame] | 480 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 481 | if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 482 | qp->q.meat == qp->q.len) { |
| 483 | unsigned long orefdst = skb->_skb_refdst; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 484 | |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 485 | skb->_skb_refdst = 0UL; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 486 | err = ip_frag_reasm(qp, skb, prev_tail, dev); |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 487 | skb->_skb_refdst = orefdst; |
| 488 | return err; |
| 489 | } |
| 490 | |
| 491 | skb_dst_drop(skb); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 492 | return -EINPROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Peter Oskolkov | 82f36cb | 2018-10-10 12:30:07 -0700 | [diff] [blame] | 494 | discard_qp: |
| 495 | inet_frag_kill(&qp->q); |
| 496 | err = -EINVAL; |
| 497 | __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | err: |
| 499 | kfree_skb(skb); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 500 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* Build a new IP datagram from all its fragments. */ |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 504 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 505 | struct sk_buff *prev_tail, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Jorge Boncompte [DTI2] | 2bad35b | 2009-03-18 23:26:11 -0700 | [diff] [blame] | 507 | struct net *net = container_of(qp->q.net, struct net, ipv4.frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | struct iphdr *iph; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 509 | struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments); |
| 510 | struct sk_buff **nextp; /* To build frag_list. */ |
| 511 | struct rb_node *rbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | int len; |
| 513 | int ihlen; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 514 | int err; |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 515 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | ipq_kill(qp); |
| 518 | |
Hannes Frederic Sowa | be99197 | 2013-03-22 08:24:37 +0000 | [diff] [blame] | 519 | ecn = ip_frag_ecn_table[qp->ecn]; |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 520 | if (unlikely(ecn == 0xff)) { |
| 521 | err = -EINVAL; |
| 522 | goto out_fail; |
| 523 | } |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 524 | /* Make the one we just received the head. */ |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 525 | if (head != skb) { |
| 526 | fp = skb_clone(skb, GFP_ATOMIC); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 527 | if (!fp) |
| 528 | goto out_nomem; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 529 | FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag; |
| 530 | if (RB_EMPTY_NODE(&skb->rbnode)) |
| 531 | FRAG_CB(prev_tail)->next_frag = fp; |
| 532 | else |
| 533 | rb_replace_node(&skb->rbnode, &fp->rbnode, |
| 534 | &qp->q.rb_fragments); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 535 | if (qp->q.fragments_tail == skb) |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 536 | qp->q.fragments_tail = fp; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 537 | skb_morph(skb, head); |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 538 | FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 539 | rb_replace_node(&head->rbnode, &skb->rbnode, |
| 540 | &qp->q.rb_fragments); |
| 541 | consume_skb(head); |
| 542 | head = skb; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Eric Dumazet | d838486 | 2018-10-10 12:30:06 -0700 | [diff] [blame] | 545 | WARN_ON(head->ip_defrag_offset != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | /* Allocate a new buffer for the datagram. */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 548 | ihlen = ip_hdrlen(head); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 549 | len = ihlen + qp->q.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 551 | err = -E2BIG; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 552 | if (len > 65535) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | goto out_oversize; |
| 554 | |
| 555 | /* Head of list must not be cloned. */ |
Pravin B Shelar | 14bbd6a | 2013-02-14 09:44:49 +0000 | [diff] [blame] | 556 | if (skb_unclone(head, GFP_ATOMIC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto out_nomem; |
| 558 | |
| 559 | /* If the first fragment is fragmented itself, we split |
| 560 | * it to two chunks: the first with data and paged part |
| 561 | * and the second, holding only fragments. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 562 | if (skb_has_frag_list(head)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | struct sk_buff *clone; |
| 564 | int i, plen = 0; |
| 565 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 566 | clone = alloc_skb(0, GFP_ATOMIC); |
| 567 | if (!clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; |
David S. Miller | d7fcf1a | 2009-06-09 00:19:37 -0700 | [diff] [blame] | 570 | skb_frag_list_init(head); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 571 | for (i = 0; i < skb_shinfo(head)->nr_frags; i++) |
| 572 | plen += skb_frag_size(&skb_shinfo(head)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | clone->len = clone->data_len = head->data_len - plen; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 574 | head->truesize += clone->truesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | clone->csum = 0; |
| 576 | clone->ip_summed = head->ip_summed; |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 577 | add_frag_mem_limit(qp->q.net, clone->truesize); |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 578 | skb_shinfo(head)->frag_list = clone; |
| 579 | nextp = &clone->next; |
| 580 | } else { |
| 581 | nextp = &skb_shinfo(head)->frag_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 584 | skb_push(head, head->data - skb_network_header(head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 586 | /* Traverse the tree in order, to build frag_list. */ |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 587 | fp = FRAG_CB(head)->next_frag; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 588 | rbn = rb_next(&head->rbnode); |
| 589 | rb_erase(&head->rbnode, &qp->q.rb_fragments); |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 590 | while (rbn || fp) { |
| 591 | /* fp points to the next sk_buff in the current run; |
| 592 | * rbn points to the next run. |
| 593 | */ |
| 594 | /* Go through the current run. */ |
| 595 | while (fp) { |
| 596 | *nextp = fp; |
| 597 | nextp = &fp->next; |
| 598 | fp->prev = NULL; |
| 599 | memset(&fp->rbnode, 0, sizeof(fp->rbnode)); |
Taehee Yoo | 85e59af9 | 2018-10-10 12:30:16 -0700 | [diff] [blame] | 600 | fp->sk = NULL; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 601 | head->data_len += fp->len; |
| 602 | head->len += fp->len; |
| 603 | if (head->ip_summed != fp->ip_summed) |
| 604 | head->ip_summed = CHECKSUM_NONE; |
| 605 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
| 606 | head->csum = csum_add(head->csum, fp->csum); |
| 607 | head->truesize += fp->truesize; |
| 608 | fp = FRAG_CB(fp)->next_frag; |
| 609 | } |
| 610 | /* Move to the next run. */ |
| 611 | if (rbn) { |
| 612 | struct rb_node *rbnext = rb_next(rbn); |
| 613 | |
| 614 | fp = rb_to_skb(rbn); |
| 615 | rb_erase(rbn, &qp->q.rb_fragments); |
| 616 | rbn = rbnext; |
| 617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
David S. Miller | 5510b3c | 2015-07-31 23:52:20 -0700 | [diff] [blame] | 619 | sub_frag_mem_limit(qp->q.net, head->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 621 | *nextp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | head->next = NULL; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 623 | head->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | head->dev = dev; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 625 | head->tstamp = qp->q.stamp; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 626 | IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 628 | iph = ip_hdr(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | iph->tot_len = htons(len); |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 630 | iph->tos |= ecn; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 631 | |
| 632 | /* When we set IP_DF on a refragmented skb we must also force a |
| 633 | * call to ip_fragment to avoid forwarding a DF-skb of size s while |
| 634 | * original sender only sent fragments of size f (where f < s). |
| 635 | * |
| 636 | * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest |
| 637 | * frag seen to avoid sending tiny DF-fragments in case skb was built |
| 638 | * from one very small df-fragment and one large non-df frag. |
| 639 | */ |
| 640 | if (qp->max_df_size == qp->q.max_size) { |
| 641 | IPCB(head)->flags |= IPSKB_FRAG_PMTU; |
| 642 | iph->frag_off = htons(IP_DF); |
| 643 | } else { |
| 644 | iph->frag_off = 0; |
| 645 | } |
| 646 | |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 647 | ip_send_check(iph); |
| 648 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 649 | __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 650 | qp->q.fragments = NULL; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 651 | qp->q.rb_fragments = RB_ROOT; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 652 | qp->q.fragments_tail = NULL; |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 653 | qp->q.last_run_head = NULL; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 654 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | out_nomem: |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 657 | 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] | 658 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | goto out_fail; |
| 660 | out_oversize: |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 661 | 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] | 662 | out_fail: |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 663 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 664 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* Process an incoming IP datagram fragment. */ |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 668 | int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 670 | struct net_device *dev = skb->dev ? : skb_dst(skb)->dev; |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 671 | int vif = l3mdev_master_ifindex_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | struct ipq *qp; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 673 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 674 | __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); |
Joe Stringer | 8282f27 | 2016-01-22 15:49:12 -0800 | [diff] [blame] | 675 | skb_orphan(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /* Lookup (or create) queue header */ |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 678 | qp = ip_find(net, ip_hdr(skb), user, vif); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 679 | if (qp) { |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 680 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 682 | spin_lock(&qp->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 684 | ret = ip_frag_queue(qp, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 686 | spin_unlock(&qp->q.lock); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 687 | ipq_put(qp); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 688 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 691 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | kfree_skb(skb); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 693 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 695 | EXPORT_SYMBOL(ip_defrag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 697 | 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] | 698 | { |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 699 | struct iphdr iph; |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 700 | int netoff; |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 701 | u32 len; |
| 702 | |
| 703 | if (skb->protocol != htons(ETH_P_IP)) |
| 704 | return skb; |
| 705 | |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 706 | netoff = skb_network_offset(skb); |
| 707 | |
| 708 | if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 709 | return skb; |
| 710 | |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 711 | if (iph.ihl < 5 || iph.version != 4) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 712 | return skb; |
| 713 | |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 714 | len = ntohs(iph.tot_len); |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 715 | if (skb->len < netoff + len || len < (iph.ihl * 4)) |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 716 | return skb; |
| 717 | |
| 718 | if (ip_is_fragment(&iph)) { |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 719 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 720 | if (skb) { |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 721 | if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 722 | return skb; |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 723 | if (pskb_trim_rcsum(skb, netoff + len)) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 724 | return skb; |
| 725 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 726 | if (ip_defrag(net, skb, user)) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 727 | return NULL; |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 728 | skb_clear_hash(skb); |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | return skb; |
| 732 | } |
| 733 | EXPORT_SYMBOL(ip_check_defrag); |
| 734 | |
Peter Oskolkov | e9e4ac4 | 2018-10-10 12:30:14 -0700 | [diff] [blame] | 735 | unsigned int inet_frag_rbtree_purge(struct rb_root *root) |
| 736 | { |
| 737 | struct rb_node *p = rb_first(root); |
| 738 | unsigned int sum = 0; |
| 739 | |
| 740 | while (p) { |
| 741 | struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); |
| 742 | |
| 743 | p = rb_next(p); |
| 744 | rb_erase(&skb->rbnode, root); |
| 745 | while (skb) { |
| 746 | struct sk_buff *next = FRAG_CB(skb)->next_frag; |
| 747 | |
| 748 | sum += skb->truesize; |
| 749 | kfree_skb(skb); |
| 750 | skb = next; |
| 751 | } |
| 752 | } |
| 753 | return sum; |
| 754 | } |
| 755 | EXPORT_SYMBOL(inet_frag_rbtree_purge); |
| 756 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 757 | #ifdef CONFIG_SYSCTL |
Eric Dumazet | d838486 | 2018-10-10 12:30:06 -0700 | [diff] [blame] | 758 | static int dist_min; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 759 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 760 | static struct ctl_table ip4_frags_ns_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 761 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 762 | .procname = "ipfrag_high_thresh", |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 763 | .data = &init_net.ipv4.frags.high_thresh, |
Eric Dumazet | 7f61706 | 2018-10-10 12:30:00 -0700 | [diff] [blame] | 764 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 765 | .mode = 0644, |
Eric Dumazet | 7f61706 | 2018-10-10 12:30:00 -0700 | [diff] [blame] | 766 | .proc_handler = proc_doulongvec_minmax, |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 767 | .extra1 = &init_net.ipv4.frags.low_thresh |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 768 | }, |
| 769 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 770 | .procname = "ipfrag_low_thresh", |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 771 | .data = &init_net.ipv4.frags.low_thresh, |
Eric Dumazet | 7f61706 | 2018-10-10 12:30:00 -0700 | [diff] [blame] | 772 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 773 | .mode = 0644, |
Eric Dumazet | 7f61706 | 2018-10-10 12:30:00 -0700 | [diff] [blame] | 774 | .proc_handler = proc_doulongvec_minmax, |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 775 | .extra2 = &init_net.ipv4.frags.high_thresh |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 776 | }, |
| 777 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 778 | .procname = "ipfrag_time", |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 779 | .data = &init_net.ipv4.frags.timeout, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 780 | .maxlen = sizeof(int), |
| 781 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 782 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 783 | }, |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 784 | { |
| 785 | .procname = "ipfrag_max_dist", |
| 786 | .data = &init_net.ipv4.frags.max_dist, |
| 787 | .maxlen = sizeof(int), |
| 788 | .mode = 0644, |
| 789 | .proc_handler = proc_dointvec_minmax, |
Eric Dumazet | d838486 | 2018-10-10 12:30:06 -0700 | [diff] [blame] | 790 | .extra1 = &dist_min, |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 791 | }, |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 792 | { } |
| 793 | }; |
| 794 | |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 795 | /* secret interval has been deprecated */ |
| 796 | static int ip4_frags_secret_interval_unused; |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 797 | static struct ctl_table ip4_frags_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 798 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 799 | .procname = "ipfrag_secret_interval", |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 800 | .data = &ip4_frags_secret_interval_unused, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 801 | .maxlen = sizeof(int), |
| 802 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 803 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 804 | }, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 805 | { } |
| 806 | }; |
| 807 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 808 | static int __net_init ip4_frags_ns_ctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 809 | { |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 810 | struct ctl_table *table; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 811 | struct ctl_table_header *hdr; |
| 812 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 813 | table = ip4_frags_ns_ctl_table; |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 814 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 815 | table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 816 | if (!table) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 817 | goto err_alloc; |
| 818 | |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 819 | table[0].data = &net->ipv4.frags.high_thresh; |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 820 | table[0].extra1 = &net->ipv4.frags.low_thresh; |
| 821 | table[0].extra2 = &init_net.ipv4.frags.high_thresh; |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 822 | table[1].data = &net->ipv4.frags.low_thresh; |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 823 | table[1].extra2 = &net->ipv4.frags.high_thresh; |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 824 | table[2].data = &net->ipv4.frags.timeout; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 825 | table[3].data = &net->ipv4.frags.max_dist; |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 828 | hdr = register_net_sysctl(net, "net/ipv4", table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 829 | if (!hdr) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 830 | goto err_reg; |
| 831 | |
| 832 | net->ipv4.frags_hdr = hdr; |
| 833 | return 0; |
| 834 | |
| 835 | err_reg: |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 836 | if (!net_eq(net, &init_net)) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 837 | kfree(table); |
| 838 | err_alloc: |
| 839 | return -ENOMEM; |
| 840 | } |
| 841 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 842 | static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 843 | { |
| 844 | struct ctl_table *table; |
| 845 | |
| 846 | table = net->ipv4.frags_hdr->ctl_table_arg; |
| 847 | unregister_net_sysctl_table(net->ipv4.frags_hdr); |
| 848 | kfree(table); |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 849 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 850 | |
Fabian Frederick | 57a02c3 | 2014-10-01 19:18:57 +0200 | [diff] [blame] | 851 | static void __init ip4_frags_ctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 852 | { |
Eric W. Biederman | 4344475 | 2012-04-19 13:22:55 +0000 | [diff] [blame] | 853 | register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 854 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 855 | #else |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 856 | static int ip4_frags_ns_ctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 857 | { |
| 858 | return 0; |
| 859 | } |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 860 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 861 | static void ip4_frags_ns_ctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 862 | { |
| 863 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 864 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 865 | static void __init ip4_frags_ctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 866 | { |
| 867 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 868 | #endif |
| 869 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 870 | static int __net_init ipv4_frags_init_net(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 871 | { |
Eric Dumazet | 7fca771 | 2018-10-10 12:29:49 -0700 | [diff] [blame] | 872 | int res; |
| 873 | |
Jesper Dangaard Brouer | c2a9366 | 2013-01-15 07:16:35 +0000 | [diff] [blame] | 874 | /* Fragment cache limits. |
| 875 | * |
| 876 | * The fragment memory accounting code, (tries to) account for |
| 877 | * the real memory usage, by measuring both the size of frag |
| 878 | * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) |
| 879 | * and the SKB's truesize. |
| 880 | * |
| 881 | * A 64K fragment consumes 129736 bytes (44*2944)+200 |
| 882 | * (1500 truesize == 2944, sizeof(struct ipq) == 200) |
| 883 | * |
| 884 | * We will commit 4MB at one time. Should we cross that limit |
| 885 | * we will prune down to 3MB, making room for approx 8 big 64K |
| 886 | * fragments 8x128k. |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 887 | */ |
Jesper Dangaard Brouer | c2a9366 | 2013-01-15 07:16:35 +0000 | [diff] [blame] | 888 | net->ipv4.frags.high_thresh = 4 * 1024 * 1024; |
| 889 | net->ipv4.frags.low_thresh = 3 * 1024 * 1024; |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 890 | /* |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 891 | * Important NOTE! Fragment queue must be destroyed before MSL expires. |
| 892 | * RFC791 is wrong proposing to prolongate timer each fragment arrival |
| 893 | * by TTL. |
| 894 | */ |
| 895 | net->ipv4.frags.timeout = IP_FRAG_TIME; |
| 896 | |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 897 | net->ipv4.frags.max_dist = 64; |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 898 | net->ipv4.frags.f = &ip4_frags; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 899 | |
Eric Dumazet | 7fca771 | 2018-10-10 12:29:49 -0700 | [diff] [blame] | 900 | res = inet_frags_init_net(&net->ipv4.frags); |
| 901 | if (res < 0) |
| 902 | return res; |
| 903 | res = ip4_frags_ns_ctl_register(net); |
| 904 | if (res < 0) |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 905 | inet_frags_exit_net(&net->ipv4.frags); |
Eric Dumazet | 7fca771 | 2018-10-10 12:29:49 -0700 | [diff] [blame] | 906 | return res; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 909 | static void __net_exit ipv4_frags_exit_net(struct net *net) |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 910 | { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 911 | ip4_frags_ns_ctl_unregister(net); |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 912 | inet_frags_exit_net(&net->ipv4.frags); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static struct pernet_operations ip4_frags_ops = { |
| 916 | .init = ipv4_frags_init_net, |
| 917 | .exit = ipv4_frags_exit_net, |
| 918 | }; |
| 919 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 920 | |
| 921 | static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed) |
| 922 | { |
| 923 | return jhash2(data, |
| 924 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); |
| 925 | } |
| 926 | |
| 927 | static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed) |
| 928 | { |
| 929 | const struct inet_frag_queue *fq = data; |
| 930 | |
| 931 | return jhash2((const u32 *)&fq->key.v4, |
| 932 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); |
| 933 | } |
| 934 | |
| 935 | static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) |
| 936 | { |
| 937 | const struct frag_v4_compare_key *key = arg->key; |
| 938 | const struct inet_frag_queue *fq = ptr; |
| 939 | |
| 940 | return !!memcmp(&fq->key, key, sizeof(*key)); |
| 941 | } |
| 942 | |
| 943 | static const struct rhashtable_params ip4_rhash_params = { |
| 944 | .head_offset = offsetof(struct inet_frag_queue, node), |
| 945 | .key_offset = offsetof(struct inet_frag_queue, key), |
| 946 | .key_len = sizeof(struct frag_v4_compare_key), |
| 947 | .hashfn = ip4_key_hashfn, |
| 948 | .obj_hashfn = ip4_obj_hashfn, |
| 949 | .obj_cmpfn = ip4_obj_cmpfn, |
| 950 | .automatic_shrinking = true, |
| 951 | }; |
| 952 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 953 | void __init ipfrag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 955 | ip4_frags.constructor = ip4_frag_init; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 956 | ip4_frags.destructor = ip4_frag_free; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 957 | ip4_frags.qsize = sizeof(struct ipq); |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 958 | ip4_frags.frag_expire = ip_expire; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 959 | ip4_frags.frags_cache_name = ip_frag_cache_name; |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 960 | ip4_frags.rhash_params = ip4_rhash_params; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 961 | if (inet_frags_init(&ip4_frags)) |
| 962 | panic("IP: failed to allocate ip4_frags cache\n"); |
Eric Dumazet | bbf6d86 | 2018-10-10 12:29:51 -0700 | [diff] [blame] | 963 | ip4_frags_ctl_register(); |
| 964 | register_pernet_subsys(&ip4_frags_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |