blob: 35e9784fab4e1098ddfd52fffc630e31b46e1502 [file] [log] [blame]
Pavel Emelyanov7eb95152007-10-15 02:31:52 -07001/*
2 * inet fragments management
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Pavel Emelyanov <xemul@openvz.org>
10 * Started as consolidation of ipv4/ip_fragment.c,
11 * ipv6/reassembly. and ipv6 nf conntrack reassembly
12 */
13
14#include <linux/list.h>
15#include <linux/spinlock.h>
16#include <linux/module.h>
17#include <linux/timer.h>
18#include <linux/mm.h>
Pavel Emelyanov321a3a92007-10-15 02:38:08 -070019#include <linux/random.h>
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070020#include <linux/skbuff.h>
21#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100023#include <linux/rhashtable.h>
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070024
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000025#include <net/sock.h>
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070026#include <net/inet_frag.h>
Hannes Frederic Sowabe991972013-03-22 08:24:37 +000027#include <net/inet_ecn.h>
Peter Oskolkovc23f35d2019-01-22 10:02:50 -080028#include <net/ip.h>
29#include <net/ipv6.h>
30
31/* Use skb->cb to track consecutive/adjacent fragments coming at
32 * the end of the queue. Nodes in the rb-tree queue will
33 * contain "runs" of one or more adjacent fragments.
34 *
35 * Invariants:
36 * - next_frag is NULL at the tail of a "run";
37 * - the head of a "run" has the sum of all fragment lengths in frag_run_len.
38 */
39struct ipfrag_skb_cb {
40 union {
41 struct inet_skb_parm h4;
42 struct inet6_skb_parm h6;
43 };
44 struct sk_buff *next_frag;
45 int frag_run_len;
46};
47
48#define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
49
50static void fragcb_clear(struct sk_buff *skb)
51{
52 RB_CLEAR_NODE(&skb->rbnode);
53 FRAG_CB(skb)->next_frag = NULL;
54 FRAG_CB(skb)->frag_run_len = skb->len;
55}
56
57/* Append skb to the last "run". */
58static void fragrun_append_to_last(struct inet_frag_queue *q,
59 struct sk_buff *skb)
60{
61 fragcb_clear(skb);
62
63 FRAG_CB(q->last_run_head)->frag_run_len += skb->len;
64 FRAG_CB(q->fragments_tail)->next_frag = skb;
65 q->fragments_tail = skb;
66}
67
68/* Create a new "run" with the skb. */
69static void fragrun_create(struct inet_frag_queue *q, struct sk_buff *skb)
70{
71 BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb));
72 fragcb_clear(skb);
73
74 if (q->last_run_head)
75 rb_link_node(&skb->rbnode, &q->last_run_head->rbnode,
76 &q->last_run_head->rbnode.rb_right);
77 else
78 rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node);
79 rb_insert_color(&skb->rbnode, &q->rb_fragments);
80
81 q->fragments_tail = skb;
82 q->last_run_head = skb;
83}
Hannes Frederic Sowabe991972013-03-22 08:24:37 +000084
85/* Given the OR values of all fragments, apply RFC 3168 5.3 requirements
86 * Value : 0xff if frame should be dropped.
87 * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field
88 */
89const u8 ip_frag_ecn_table[16] = {
90 /* at least one fragment had CE, and others ECT_0 or ECT_1 */
91 [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE,
92 [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
93 [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
94
95 /* invalid combinations : drop frame */
96 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff,
97 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff,
98 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff,
99 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
100 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff,
101 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff,
102 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
103};
104EXPORT_SYMBOL(ip_frag_ecn_table);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700105
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200106int inet_frags_init(struct inet_frags *f)
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700107{
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200108 f->frags_cachep = kmem_cache_create(f->frags_cache_name, f->qsize, 0, 0,
109 NULL);
110 if (!f->frags_cachep)
111 return -ENOMEM;
112
Eric Dumazetdc93f462019-05-27 16:56:49 -0700113 refcount_set(&f->refcnt, 1);
114 init_completion(&f->completion);
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200115 return 0;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700116}
117EXPORT_SYMBOL(inet_frags_init);
118
119void inet_frags_fini(struct inet_frags *f)
120{
Eric Dumazetdc93f462019-05-27 16:56:49 -0700121 if (refcount_dec_and_test(&f->refcnt))
122 complete(&f->completion);
123
124 wait_for_completion(&f->completion);
Eric Dumazet648700f2018-03-31 12:58:49 -0700125
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200126 kmem_cache_destroy(f->frags_cachep);
Eric Dumazet648700f2018-03-31 12:58:49 -0700127 f->frags_cachep = NULL;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700128}
129EXPORT_SYMBOL(inet_frags_fini);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700130
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700131/* called from rhashtable_free_and_destroy() at netns_frags dismantle */
Eric Dumazet648700f2018-03-31 12:58:49 -0700132static void inet_frags_free_cb(void *ptr, void *arg)
133{
134 struct inet_frag_queue *fq = ptr;
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700135 int count;
Eric Dumazet648700f2018-03-31 12:58:49 -0700136
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700137 count = del_timer_sync(&fq->timer) ? 1 : 0;
Eric Dumazet648700f2018-03-31 12:58:49 -0700138
139 spin_lock_bh(&fq->lock);
140 if (!(fq->flags & INET_FRAG_COMPLETE)) {
141 fq->flags |= INET_FRAG_COMPLETE;
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700142 count++;
143 } else if (fq->flags & INET_FRAG_HASH_DEAD) {
144 count++;
Eric Dumazet648700f2018-03-31 12:58:49 -0700145 }
146 spin_unlock_bh(&fq->lock);
147
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700148 if (refcount_sub_and_test(count, &fq->refcnt))
149 inet_frag_destroy(fq);
150}
151
152static void fqdir_rwork_fn(struct work_struct *work)
153{
154 struct fqdir *fqdir = container_of(to_rcu_work(work),
155 struct fqdir, destroy_rwork);
Eric Dumazetdc93f462019-05-27 16:56:49 -0700156 struct inet_frags *f = fqdir->f;
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700157
158 rhashtable_free_and_destroy(&fqdir->rhashtable, inet_frags_free_cb, NULL);
Eric Dumazetdc93f462019-05-27 16:56:49 -0700159
160 /* We need to make sure all ongoing call_rcu(..., inet_frag_destroy_rcu)
161 * have completed, since they need to dereference fqdir.
162 * Would it not be nice to have kfree_rcu_barrier() ? :)
163 */
164 rcu_barrier();
165
166 if (refcount_dec_and_test(&f->refcnt))
167 complete(&f->completion);
168
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700169 kfree(fqdir);
Eric Dumazet648700f2018-03-31 12:58:49 -0700170}
171
Eric Dumazet6b73d192019-05-27 16:56:47 -0700172int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net)
173{
174 struct fqdir *fqdir = kzalloc(sizeof(*fqdir), GFP_KERNEL);
175 int res;
176
177 if (!fqdir)
178 return -ENOMEM;
179 fqdir->f = f;
180 fqdir->net = net;
181 res = rhashtable_init(&fqdir->rhashtable, &fqdir->f->rhash_params);
182 if (res < 0) {
183 kfree(fqdir);
184 return res;
185 }
Eric Dumazetdc93f462019-05-27 16:56:49 -0700186 refcount_inc(&f->refcnt);
Eric Dumazet6b73d192019-05-27 16:56:47 -0700187 *fqdirp = fqdir;
188 return 0;
189}
190EXPORT_SYMBOL(fqdir_init);
191
Eric Dumazet89fb9002019-05-24 09:03:31 -0700192void fqdir_exit(struct fqdir *fqdir)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800193{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700194 fqdir->high_thresh = 0; /* prevent creation of new frags */
Florian Westphalb13d3cb2014-07-24 16:50:32 +0200195
Herbert Xu32707c42019-05-29 13:40:26 +0800196 fqdir->dead = true;
197
198 /* call_rcu is supposed to provide memory barrier semantics,
199 * separating the setting of fqdir->dead with the destruction
200 * work. This implicit barrier is paired with inet_frag_kill().
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700201 */
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700202
203 INIT_RCU_WORK(&fqdir->destroy_rwork, fqdir_rwork_fn);
204 queue_rcu_work(system_wq, &fqdir->destroy_rwork);
205
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800206}
Eric Dumazet89fb9002019-05-24 09:03:31 -0700207EXPORT_SYMBOL(fqdir_exit);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800208
Eric Dumazet093ba722018-03-31 12:58:44 -0700209void inet_frag_kill(struct inet_frag_queue *fq)
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700210{
211 if (del_timer(&fq->timer))
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300212 refcount_dec(&fq->refcnt);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700213
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200214 if (!(fq->flags & INET_FRAG_COMPLETE)) {
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700215 struct fqdir *fqdir = fq->fqdir;
Eric Dumazet648700f2018-03-31 12:58:49 -0700216
217 fq->flags |= INET_FRAG_COMPLETE;
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700218 rcu_read_lock();
Herbert Xu32707c42019-05-29 13:40:26 +0800219 /* The RCU read lock provides a memory barrier
220 * guaranteeing that if fqdir->dead is false then
221 * the hash table destruction will not start until
222 * after we unlock. Paired with inet_frags_exit_net().
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700223 */
Herbert Xu32707c42019-05-29 13:40:26 +0800224 if (!fqdir->dead) {
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700225 rhashtable_remove_fast(&fqdir->rhashtable, &fq->node,
226 fqdir->f->rhash_params);
227 refcount_dec(&fq->refcnt);
228 } else {
229 fq->flags |= INET_FRAG_HASH_DEAD;
230 }
231 rcu_read_unlock();
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700232 }
233}
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700234EXPORT_SYMBOL(inet_frag_kill);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700235
Eric Dumazet648700f2018-03-31 12:58:49 -0700236static void inet_frag_destroy_rcu(struct rcu_head *head)
237{
238 struct inet_frag_queue *q = container_of(head, struct inet_frag_queue,
239 rcu);
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700240 struct inet_frags *f = q->fqdir->f;
Eric Dumazet648700f2018-03-31 12:58:49 -0700241
242 if (f->destructor)
243 f->destructor(q);
244 kmem_cache_free(f->frags_cachep, q);
245}
246
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800247unsigned int inet_frag_rbtree_purge(struct rb_root *root)
248{
249 struct rb_node *p = rb_first(root);
250 unsigned int sum = 0;
251
252 while (p) {
253 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
254
255 p = rb_next(p);
256 rb_erase(&skb->rbnode, root);
257 while (skb) {
258 struct sk_buff *next = FRAG_CB(skb)->next_frag;
259
260 sum += skb->truesize;
261 kfree_skb(skb);
262 skb = next;
263 }
264 }
265 return sum;
266}
267EXPORT_SYMBOL(inet_frag_rbtree_purge);
268
Eric Dumazet093ba722018-03-31 12:58:44 -0700269void inet_frag_destroy(struct inet_frag_queue *q)
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700270{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700271 struct fqdir *fqdir;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000272 unsigned int sum, sum_truesize = 0;
Eric Dumazet093ba722018-03-31 12:58:44 -0700273 struct inet_frags *f;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700274
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200275 WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700276 WARN_ON(del_timer(&q->timer) != 0);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700277
278 /* Release all fragment data. */
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700279 fqdir = q->fqdir;
280 f = fqdir->f;
Peter Oskolkovd8cf7572019-02-25 17:43:46 -0800281 sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000282 sum = sum_truesize + f->qsize;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700283
Eric Dumazet648700f2018-03-31 12:58:49 -0700284 call_rcu(&q->rcu, inet_frag_destroy_rcu);
Florian Westphal5719b292015-07-23 12:05:39 +0200285
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700286 sub_frag_mem_limit(fqdir, sum);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700287}
288EXPORT_SYMBOL(inet_frag_destroy);
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700289
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700290static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir,
Nikolay Aleksandrovf926e232014-08-01 12:29:46 +0200291 struct inet_frags *f,
292 void *arg)
Pavel Emelyanove521db92007-10-17 19:45:23 -0700293{
294 struct inet_frag_queue *q;
295
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200296 q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100297 if (!q)
Pavel Emelyanove521db92007-10-17 19:45:23 -0700298 return NULL;
299
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700300 q->fqdir = fqdir;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700301 f->constructor(q, arg);
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700302 add_frag_mem_limit(fqdir, f->qsize);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000303
Kees Cook78802012017-10-16 17:29:20 -0700304 timer_setup(&q->timer, f->frag_expire, 0);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700305 spin_lock_init(&q->lock);
Eric Dumazet648700f2018-03-31 12:58:49 -0700306 refcount_set(&q->refcnt, 3);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700307
308 return q;
309}
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700310
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700311static struct inet_frag_queue *inet_frag_create(struct fqdir *fqdir,
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800312 void *arg,
313 struct inet_frag_queue **prev)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700314{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700315 struct inet_frags *f = fqdir->f;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700316 struct inet_frag_queue *q;
317
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700318 q = inet_frag_alloc(fqdir, f, arg);
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800319 if (!q) {
320 *prev = ERR_PTR(-ENOMEM);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700321 return NULL;
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800322 }
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700323 mod_timer(&q->timer, jiffies + fqdir->timeout);
Eric Dumazet648700f2018-03-31 12:58:49 -0700324
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700325 *prev = rhashtable_lookup_get_insert_key(&fqdir->rhashtable, &q->key,
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800326 &q->node, f->rhash_params);
327 if (*prev) {
Eric Dumazet648700f2018-03-31 12:58:49 -0700328 q->flags |= INET_FRAG_COMPLETE;
329 inet_frag_kill(q);
330 inet_frag_destroy(q);
331 return NULL;
332 }
333 return q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700334}
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700335
Eric Dumazet648700f2018-03-31 12:58:49 -0700336/* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700337struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key)
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700338{
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800339 struct inet_frag_queue *fq = NULL, *prev;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700340
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700341 if (!fqdir->high_thresh || frag_mem_limit(fqdir) > fqdir->high_thresh)
Eric Dumazet56e2c942018-07-30 20:09:11 -0700342 return NULL;
343
Eric Dumazet648700f2018-03-31 12:58:49 -0700344 rcu_read_lock();
Florian Westphal86e93e42014-07-24 16:50:31 +0200345
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700346 prev = rhashtable_lookup(&fqdir->rhashtable, key, fqdir->f->rhash_params);
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800347 if (!prev)
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700348 fq = inet_frag_create(fqdir, key, &prev);
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800349 if (prev && !IS_ERR(prev)) {
350 fq = prev;
Eric Dumazet648700f2018-03-31 12:58:49 -0700351 if (!refcount_inc_not_zero(&fq->refcnt))
352 fq = NULL;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700353 }
Eric Dumazet648700f2018-03-31 12:58:49 -0700354 rcu_read_unlock();
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800355 return fq;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700356}
357EXPORT_SYMBOL(inet_frag_find);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800358
359int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
360 int offset, int end)
361{
362 struct sk_buff *last = q->fragments_tail;
363
364 /* RFC5722, Section 4, amended by Errata ID : 3089
365 * When reassembling an IPv6 datagram, if
366 * one or more its constituent fragments is determined to be an
367 * overlapping fragment, the entire datagram (and any constituent
368 * fragments) MUST be silently discarded.
369 *
370 * Duplicates, however, should be ignored (i.e. skb dropped, but the
371 * queue/fragments kept for later reassembly).
372 */
373 if (!last)
374 fragrun_create(q, skb); /* First fragment. */
375 else if (last->ip_defrag_offset + last->len < end) {
376 /* This is the common case: skb goes to the end. */
377 /* Detect and discard overlaps. */
378 if (offset < last->ip_defrag_offset + last->len)
379 return IPFRAG_OVERLAP;
380 if (offset == last->ip_defrag_offset + last->len)
381 fragrun_append_to_last(q, skb);
382 else
383 fragrun_create(q, skb);
384 } else {
385 /* Binary search. Note that skb can become the first fragment,
386 * but not the last (covered above).
387 */
388 struct rb_node **rbn, *parent;
389
390 rbn = &q->rb_fragments.rb_node;
391 do {
392 struct sk_buff *curr;
393 int curr_run_end;
394
395 parent = *rbn;
396 curr = rb_to_skb(parent);
397 curr_run_end = curr->ip_defrag_offset +
398 FRAG_CB(curr)->frag_run_len;
399 if (end <= curr->ip_defrag_offset)
400 rbn = &parent->rb_left;
401 else if (offset >= curr_run_end)
402 rbn = &parent->rb_right;
403 else if (offset >= curr->ip_defrag_offset &&
404 end <= curr_run_end)
405 return IPFRAG_DUP;
406 else
407 return IPFRAG_OVERLAP;
408 } while (*rbn);
409 /* Here we have parent properly set, and rbn pointing to
410 * one of its NULL left/right children. Insert skb.
411 */
412 fragcb_clear(skb);
413 rb_link_node(&skb->rbnode, parent, rbn);
414 rb_insert_color(&skb->rbnode, &q->rb_fragments);
415 }
416
417 skb->ip_defrag_offset = offset;
418
419 return IPFRAG_OK;
420}
421EXPORT_SYMBOL(inet_frag_queue_insert);
422
423void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
424 struct sk_buff *parent)
425{
426 struct sk_buff *fp, *head = skb_rb_first(&q->rb_fragments);
427 struct sk_buff **nextp;
428 int delta;
429
430 if (head != skb) {
431 fp = skb_clone(skb, GFP_ATOMIC);
432 if (!fp)
433 return NULL;
434 FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
435 if (RB_EMPTY_NODE(&skb->rbnode))
436 FRAG_CB(parent)->next_frag = fp;
437 else
438 rb_replace_node(&skb->rbnode, &fp->rbnode,
439 &q->rb_fragments);
440 if (q->fragments_tail == skb)
441 q->fragments_tail = fp;
442 skb_morph(skb, head);
443 FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
444 rb_replace_node(&head->rbnode, &skb->rbnode,
445 &q->rb_fragments);
446 consume_skb(head);
447 head = skb;
448 }
449 WARN_ON(head->ip_defrag_offset != 0);
450
451 delta = -head->truesize;
452
453 /* Head of list must not be cloned. */
454 if (skb_unclone(head, GFP_ATOMIC))
455 return NULL;
456
457 delta += head->truesize;
458 if (delta)
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700459 add_frag_mem_limit(q->fqdir, delta);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800460
461 /* If the first fragment is fragmented itself, we split
462 * it to two chunks: the first with data and paged part
463 * and the second, holding only fragments.
464 */
465 if (skb_has_frag_list(head)) {
466 struct sk_buff *clone;
467 int i, plen = 0;
468
469 clone = alloc_skb(0, GFP_ATOMIC);
470 if (!clone)
471 return NULL;
472 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
473 skb_frag_list_init(head);
474 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
475 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
476 clone->data_len = head->data_len - plen;
477 clone->len = clone->data_len;
478 head->truesize += clone->truesize;
479 clone->csum = 0;
480 clone->ip_summed = head->ip_summed;
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700481 add_frag_mem_limit(q->fqdir, clone->truesize);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800482 skb_shinfo(head)->frag_list = clone;
483 nextp = &clone->next;
484 } else {
485 nextp = &skb_shinfo(head)->frag_list;
486 }
487
488 return nextp;
489}
490EXPORT_SYMBOL(inet_frag_reasm_prepare);
491
492void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
493 void *reasm_data)
494{
495 struct sk_buff **nextp = (struct sk_buff **)reasm_data;
496 struct rb_node *rbn;
497 struct sk_buff *fp;
498
499 skb_push(head, head->data - skb_network_header(head));
500
501 /* Traverse the tree in order, to build frag_list. */
502 fp = FRAG_CB(head)->next_frag;
503 rbn = rb_next(&head->rbnode);
504 rb_erase(&head->rbnode, &q->rb_fragments);
505 while (rbn || fp) {
506 /* fp points to the next sk_buff in the current run;
507 * rbn points to the next run.
508 */
509 /* Go through the current run. */
510 while (fp) {
511 *nextp = fp;
512 nextp = &fp->next;
513 fp->prev = NULL;
514 memset(&fp->rbnode, 0, sizeof(fp->rbnode));
515 fp->sk = NULL;
516 head->data_len += fp->len;
517 head->len += fp->len;
518 if (head->ip_summed != fp->ip_summed)
519 head->ip_summed = CHECKSUM_NONE;
520 else if (head->ip_summed == CHECKSUM_COMPLETE)
521 head->csum = csum_add(head->csum, fp->csum);
522 head->truesize += fp->truesize;
523 fp = FRAG_CB(fp)->next_frag;
524 }
525 /* Move to the next run. */
526 if (rbn) {
527 struct rb_node *rbnext = rb_next(rbn);
528
529 fp = rb_to_skb(rbn);
530 rb_erase(rbn, &q->rb_fragments);
531 rbn = rbnext;
532 }
533 }
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700534 sub_frag_mem_limit(q->fqdir, head->truesize);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800535
536 *nextp = NULL;
537 skb_mark_not_on_list(head);
538 head->prev = NULL;
539 head->tstamp = q->stamp;
540}
541EXPORT_SYMBOL(inet_frag_reasm_finish);
542
543struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q)
544{
Peter Oskolkovd8cf7572019-02-25 17:43:46 -0800545 struct sk_buff *head, *skb;
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800546
Peter Oskolkovd8cf7572019-02-25 17:43:46 -0800547 head = skb_rb_first(&q->rb_fragments);
548 if (!head)
549 return NULL;
550 skb = FRAG_CB(head)->next_frag;
551 if (skb)
552 rb_replace_node(&head->rbnode, &skb->rbnode,
553 &q->rb_fragments);
554 else
555 rb_erase(&head->rbnode, &q->rb_fragments);
556 memset(&head->rbnode, 0, sizeof(head->rbnode));
557 barrier();
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800558
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800559 if (head == q->fragments_tail)
560 q->fragments_tail = NULL;
561
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700562 sub_frag_mem_limit(q->fqdir, head->truesize);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800563
564 return head;
565}
566EXPORT_SYMBOL(inet_frag_pull_head);