blob: 2b816f1ebbb4ad6fc65aaeff7fadcea8a78e5f9f [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
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700196 /* paired with READ_ONCE() in inet_frag_kill() :
197 * We want to prevent rhashtable_remove_fast() calls
198 */
199 smp_store_release(&fqdir->dead, true);
200
201 INIT_RCU_WORK(&fqdir->destroy_rwork, fqdir_rwork_fn);
202 queue_rcu_work(system_wq, &fqdir->destroy_rwork);
203
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800204}
Eric Dumazet89fb9002019-05-24 09:03:31 -0700205EXPORT_SYMBOL(fqdir_exit);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800206
Eric Dumazet093ba722018-03-31 12:58:44 -0700207void inet_frag_kill(struct inet_frag_queue *fq)
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700208{
209 if (del_timer(&fq->timer))
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300210 refcount_dec(&fq->refcnt);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700211
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200212 if (!(fq->flags & INET_FRAG_COMPLETE)) {
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700213 struct fqdir *fqdir = fq->fqdir;
Eric Dumazet648700f2018-03-31 12:58:49 -0700214
215 fq->flags |= INET_FRAG_COMPLETE;
Eric Dumazet3c8fc872019-05-24 09:03:40 -0700216 rcu_read_lock();
217 /* This READ_ONCE() is paired with smp_store_release()
218 * in inet_frags_exit_net().
219 */
220 if (!READ_ONCE(fqdir->dead)) {
221 rhashtable_remove_fast(&fqdir->rhashtable, &fq->node,
222 fqdir->f->rhash_params);
223 refcount_dec(&fq->refcnt);
224 } else {
225 fq->flags |= INET_FRAG_HASH_DEAD;
226 }
227 rcu_read_unlock();
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700228 }
229}
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700230EXPORT_SYMBOL(inet_frag_kill);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700231
Eric Dumazet648700f2018-03-31 12:58:49 -0700232static void inet_frag_destroy_rcu(struct rcu_head *head)
233{
234 struct inet_frag_queue *q = container_of(head, struct inet_frag_queue,
235 rcu);
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700236 struct inet_frags *f = q->fqdir->f;
Eric Dumazet648700f2018-03-31 12:58:49 -0700237
238 if (f->destructor)
239 f->destructor(q);
240 kmem_cache_free(f->frags_cachep, q);
241}
242
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800243unsigned int inet_frag_rbtree_purge(struct rb_root *root)
244{
245 struct rb_node *p = rb_first(root);
246 unsigned int sum = 0;
247
248 while (p) {
249 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
250
251 p = rb_next(p);
252 rb_erase(&skb->rbnode, root);
253 while (skb) {
254 struct sk_buff *next = FRAG_CB(skb)->next_frag;
255
256 sum += skb->truesize;
257 kfree_skb(skb);
258 skb = next;
259 }
260 }
261 return sum;
262}
263EXPORT_SYMBOL(inet_frag_rbtree_purge);
264
Eric Dumazet093ba722018-03-31 12:58:44 -0700265void inet_frag_destroy(struct inet_frag_queue *q)
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700266{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700267 struct fqdir *fqdir;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000268 unsigned int sum, sum_truesize = 0;
Eric Dumazet093ba722018-03-31 12:58:44 -0700269 struct inet_frags *f;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700270
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200271 WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700272 WARN_ON(del_timer(&q->timer) != 0);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700273
274 /* Release all fragment data. */
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700275 fqdir = q->fqdir;
276 f = fqdir->f;
Peter Oskolkovd8cf7572019-02-25 17:43:46 -0800277 sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000278 sum = sum_truesize + f->qsize;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700279
Eric Dumazet648700f2018-03-31 12:58:49 -0700280 call_rcu(&q->rcu, inet_frag_destroy_rcu);
Florian Westphal5719b292015-07-23 12:05:39 +0200281
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700282 sub_frag_mem_limit(fqdir, sum);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700283}
284EXPORT_SYMBOL(inet_frag_destroy);
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700285
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700286static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir,
Nikolay Aleksandrovf926e232014-08-01 12:29:46 +0200287 struct inet_frags *f,
288 void *arg)
Pavel Emelyanove521db92007-10-17 19:45:23 -0700289{
290 struct inet_frag_queue *q;
291
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200292 q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100293 if (!q)
Pavel Emelyanove521db92007-10-17 19:45:23 -0700294 return NULL;
295
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700296 q->fqdir = fqdir;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700297 f->constructor(q, arg);
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700298 add_frag_mem_limit(fqdir, f->qsize);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000299
Kees Cook78802012017-10-16 17:29:20 -0700300 timer_setup(&q->timer, f->frag_expire, 0);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700301 spin_lock_init(&q->lock);
Eric Dumazet648700f2018-03-31 12:58:49 -0700302 refcount_set(&q->refcnt, 3);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700303
304 return q;
305}
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700306
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700307static struct inet_frag_queue *inet_frag_create(struct fqdir *fqdir,
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800308 void *arg,
309 struct inet_frag_queue **prev)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700310{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700311 struct inet_frags *f = fqdir->f;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700312 struct inet_frag_queue *q;
313
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700314 q = inet_frag_alloc(fqdir, f, arg);
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800315 if (!q) {
316 *prev = ERR_PTR(-ENOMEM);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700317 return NULL;
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800318 }
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700319 mod_timer(&q->timer, jiffies + fqdir->timeout);
Eric Dumazet648700f2018-03-31 12:58:49 -0700320
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700321 *prev = rhashtable_lookup_get_insert_key(&fqdir->rhashtable, &q->key,
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800322 &q->node, f->rhash_params);
323 if (*prev) {
Eric Dumazet648700f2018-03-31 12:58:49 -0700324 q->flags |= INET_FRAG_COMPLETE;
325 inet_frag_kill(q);
326 inet_frag_destroy(q);
327 return NULL;
328 }
329 return q;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700330}
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700331
Eric Dumazet648700f2018-03-31 12:58:49 -0700332/* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700333struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key)
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700334{
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800335 struct inet_frag_queue *fq = NULL, *prev;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700336
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700337 if (!fqdir->high_thresh || frag_mem_limit(fqdir) > fqdir->high_thresh)
Eric Dumazet56e2c942018-07-30 20:09:11 -0700338 return NULL;
339
Eric Dumazet648700f2018-03-31 12:58:49 -0700340 rcu_read_lock();
Florian Westphal86e93e42014-07-24 16:50:31 +0200341
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700342 prev = rhashtable_lookup(&fqdir->rhashtable, key, fqdir->f->rhash_params);
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800343 if (!prev)
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700344 fq = inet_frag_create(fqdir, key, &prev);
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800345 if (prev && !IS_ERR(prev)) {
346 fq = prev;
Eric Dumazet648700f2018-03-31 12:58:49 -0700347 if (!refcount_inc_not_zero(&fq->refcnt))
348 fq = NULL;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700349 }
Eric Dumazet648700f2018-03-31 12:58:49 -0700350 rcu_read_unlock();
Eric Dumazet0d5b9312018-11-08 17:34:27 -0800351 return fq;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700352}
353EXPORT_SYMBOL(inet_frag_find);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800354
355int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
356 int offset, int end)
357{
358 struct sk_buff *last = q->fragments_tail;
359
360 /* RFC5722, Section 4, amended by Errata ID : 3089
361 * When reassembling an IPv6 datagram, if
362 * one or more its constituent fragments is determined to be an
363 * overlapping fragment, the entire datagram (and any constituent
364 * fragments) MUST be silently discarded.
365 *
366 * Duplicates, however, should be ignored (i.e. skb dropped, but the
367 * queue/fragments kept for later reassembly).
368 */
369 if (!last)
370 fragrun_create(q, skb); /* First fragment. */
371 else if (last->ip_defrag_offset + last->len < end) {
372 /* This is the common case: skb goes to the end. */
373 /* Detect and discard overlaps. */
374 if (offset < last->ip_defrag_offset + last->len)
375 return IPFRAG_OVERLAP;
376 if (offset == last->ip_defrag_offset + last->len)
377 fragrun_append_to_last(q, skb);
378 else
379 fragrun_create(q, skb);
380 } else {
381 /* Binary search. Note that skb can become the first fragment,
382 * but not the last (covered above).
383 */
384 struct rb_node **rbn, *parent;
385
386 rbn = &q->rb_fragments.rb_node;
387 do {
388 struct sk_buff *curr;
389 int curr_run_end;
390
391 parent = *rbn;
392 curr = rb_to_skb(parent);
393 curr_run_end = curr->ip_defrag_offset +
394 FRAG_CB(curr)->frag_run_len;
395 if (end <= curr->ip_defrag_offset)
396 rbn = &parent->rb_left;
397 else if (offset >= curr_run_end)
398 rbn = &parent->rb_right;
399 else if (offset >= curr->ip_defrag_offset &&
400 end <= curr_run_end)
401 return IPFRAG_DUP;
402 else
403 return IPFRAG_OVERLAP;
404 } while (*rbn);
405 /* Here we have parent properly set, and rbn pointing to
406 * one of its NULL left/right children. Insert skb.
407 */
408 fragcb_clear(skb);
409 rb_link_node(&skb->rbnode, parent, rbn);
410 rb_insert_color(&skb->rbnode, &q->rb_fragments);
411 }
412
413 skb->ip_defrag_offset = offset;
414
415 return IPFRAG_OK;
416}
417EXPORT_SYMBOL(inet_frag_queue_insert);
418
419void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
420 struct sk_buff *parent)
421{
422 struct sk_buff *fp, *head = skb_rb_first(&q->rb_fragments);
423 struct sk_buff **nextp;
424 int delta;
425
426 if (head != skb) {
427 fp = skb_clone(skb, GFP_ATOMIC);
428 if (!fp)
429 return NULL;
430 FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
431 if (RB_EMPTY_NODE(&skb->rbnode))
432 FRAG_CB(parent)->next_frag = fp;
433 else
434 rb_replace_node(&skb->rbnode, &fp->rbnode,
435 &q->rb_fragments);
436 if (q->fragments_tail == skb)
437 q->fragments_tail = fp;
438 skb_morph(skb, head);
439 FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
440 rb_replace_node(&head->rbnode, &skb->rbnode,
441 &q->rb_fragments);
442 consume_skb(head);
443 head = skb;
444 }
445 WARN_ON(head->ip_defrag_offset != 0);
446
447 delta = -head->truesize;
448
449 /* Head of list must not be cloned. */
450 if (skb_unclone(head, GFP_ATOMIC))
451 return NULL;
452
453 delta += head->truesize;
454 if (delta)
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700455 add_frag_mem_limit(q->fqdir, delta);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800456
457 /* If the first fragment is fragmented itself, we split
458 * it to two chunks: the first with data and paged part
459 * and the second, holding only fragments.
460 */
461 if (skb_has_frag_list(head)) {
462 struct sk_buff *clone;
463 int i, plen = 0;
464
465 clone = alloc_skb(0, GFP_ATOMIC);
466 if (!clone)
467 return NULL;
468 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
469 skb_frag_list_init(head);
470 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
471 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
472 clone->data_len = head->data_len - plen;
473 clone->len = clone->data_len;
474 head->truesize += clone->truesize;
475 clone->csum = 0;
476 clone->ip_summed = head->ip_summed;
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700477 add_frag_mem_limit(q->fqdir, clone->truesize);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800478 skb_shinfo(head)->frag_list = clone;
479 nextp = &clone->next;
480 } else {
481 nextp = &skb_shinfo(head)->frag_list;
482 }
483
484 return nextp;
485}
486EXPORT_SYMBOL(inet_frag_reasm_prepare);
487
488void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
489 void *reasm_data)
490{
491 struct sk_buff **nextp = (struct sk_buff **)reasm_data;
492 struct rb_node *rbn;
493 struct sk_buff *fp;
494
495 skb_push(head, head->data - skb_network_header(head));
496
497 /* Traverse the tree in order, to build frag_list. */
498 fp = FRAG_CB(head)->next_frag;
499 rbn = rb_next(&head->rbnode);
500 rb_erase(&head->rbnode, &q->rb_fragments);
501 while (rbn || fp) {
502 /* fp points to the next sk_buff in the current run;
503 * rbn points to the next run.
504 */
505 /* Go through the current run. */
506 while (fp) {
507 *nextp = fp;
508 nextp = &fp->next;
509 fp->prev = NULL;
510 memset(&fp->rbnode, 0, sizeof(fp->rbnode));
511 fp->sk = NULL;
512 head->data_len += fp->len;
513 head->len += fp->len;
514 if (head->ip_summed != fp->ip_summed)
515 head->ip_summed = CHECKSUM_NONE;
516 else if (head->ip_summed == CHECKSUM_COMPLETE)
517 head->csum = csum_add(head->csum, fp->csum);
518 head->truesize += fp->truesize;
519 fp = FRAG_CB(fp)->next_frag;
520 }
521 /* Move to the next run. */
522 if (rbn) {
523 struct rb_node *rbnext = rb_next(rbn);
524
525 fp = rb_to_skb(rbn);
526 rb_erase(rbn, &q->rb_fragments);
527 rbn = rbnext;
528 }
529 }
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700530 sub_frag_mem_limit(q->fqdir, head->truesize);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800531
532 *nextp = NULL;
533 skb_mark_not_on_list(head);
534 head->prev = NULL;
535 head->tstamp = q->stamp;
536}
537EXPORT_SYMBOL(inet_frag_reasm_finish);
538
539struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q)
540{
Peter Oskolkovd8cf7572019-02-25 17:43:46 -0800541 struct sk_buff *head, *skb;
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800542
Peter Oskolkovd8cf7572019-02-25 17:43:46 -0800543 head = skb_rb_first(&q->rb_fragments);
544 if (!head)
545 return NULL;
546 skb = FRAG_CB(head)->next_frag;
547 if (skb)
548 rb_replace_node(&head->rbnode, &skb->rbnode,
549 &q->rb_fragments);
550 else
551 rb_erase(&head->rbnode, &q->rb_fragments);
552 memset(&head->rbnode, 0, sizeof(head->rbnode));
553 barrier();
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800554
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800555 if (head == q->fragments_tail)
556 q->fragments_tail = NULL;
557
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700558 sub_frag_mem_limit(q->fqdir, head->truesize);
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800559
560 return head;
561}
562EXPORT_SYMBOL(inet_frag_pull_head);