blob: 0fb49dedc9fb29317b4247bc4ff9714ef1158c85 [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>
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070023
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000024#include <net/sock.h>
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070025#include <net/inet_frag.h>
Hannes Frederic Sowabe991972013-03-22 08:24:37 +000026#include <net/inet_ecn.h>
27
28/* Given the OR values of all fragments, apply RFC 3168 5.3 requirements
29 * Value : 0xff if frame should be dropped.
30 * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field
31 */
32const u8 ip_frag_ecn_table[16] = {
33 /* at least one fragment had CE, and others ECT_0 or ECT_1 */
34 [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE,
35 [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
36 [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
37
38 /* invalid combinations : drop frame */
39 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff,
40 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff,
41 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff,
42 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
43 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff,
44 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff,
45 [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
46};
47EXPORT_SYMBOL(ip_frag_ecn_table);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070048
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020049int inet_frags_init(struct inet_frags *f)
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070050{
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020051 f->frags_cachep = kmem_cache_create(f->frags_cache_name, f->qsize, 0, 0,
52 NULL);
53 if (!f->frags_cachep)
54 return -ENOMEM;
55
56 return 0;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070057}
58EXPORT_SYMBOL(inet_frags_init);
59
60void inet_frags_fini(struct inet_frags *f)
61{
Eric Dumazet23ce9c52018-10-10 12:29:56 -070062 /* We must wait that all inet_frag_destroy_rcu() have completed. */
63 rcu_barrier();
64
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020065 kmem_cache_destroy(f->frags_cachep);
Eric Dumazet23ce9c52018-10-10 12:29:56 -070066 f->frags_cachep = NULL;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070067}
68EXPORT_SYMBOL(inet_frags_fini);
Pavel Emelyanov277e6502007-10-15 02:37:18 -070069
Eric Dumazet23ce9c52018-10-10 12:29:56 -070070static void inet_frags_free_cb(void *ptr, void *arg)
71{
72 struct inet_frag_queue *fq = ptr;
73
74 /* If we can not cancel the timer, it means this frag_queue
75 * is already disappearing, we have nothing to do.
76 * Otherwise, we own a refcount until the end of this function.
77 */
78 if (!del_timer(&fq->timer))
79 return;
80
81 spin_lock_bh(&fq->lock);
82 if (!(fq->flags & INET_FRAG_COMPLETE)) {
83 fq->flags |= INET_FRAG_COMPLETE;
84 atomic_dec(&fq->refcnt);
85 }
86 spin_unlock_bh(&fq->lock);
87
88 inet_frag_put(fq);
89}
90
Eric Dumazet2ffb1c32018-10-10 12:29:50 -070091void inet_frags_exit_net(struct netns_frags *nf)
Pavel Emelyanov81566e82008-01-22 06:12:39 -080092{
Paolo Abeni8c849652018-07-06 12:30:20 +020093 nf->high_thresh = 0; /* prevent creation of new frags */
Florian Westphalb13d3cb2014-07-24 16:50:32 +020094
Eric Dumazet23ce9c52018-10-10 12:29:56 -070095 rhashtable_free_and_destroy(&nf->rhashtable, inet_frags_free_cb, NULL);
Pavel Emelyanov81566e82008-01-22 06:12:39 -080096}
97EXPORT_SYMBOL(inet_frags_exit_net);
98
Eric Dumazet2ffb1c32018-10-10 12:29:50 -070099void inet_frag_kill(struct inet_frag_queue *fq)
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700100{
101 if (del_timer(&fq->timer))
102 atomic_dec(&fq->refcnt);
103
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200104 if (!(fq->flags & INET_FRAG_COMPLETE)) {
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700105 struct netns_frags *nf = fq->net;
106
107 fq->flags |= INET_FRAG_COMPLETE;
108 rhashtable_remove_fast(&nf->rhashtable, &fq->node, nf->f->rhash_params);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700109 atomic_dec(&fq->refcnt);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700110 }
111}
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700112EXPORT_SYMBOL(inet_frag_kill);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700113
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700114static void inet_frag_destroy_rcu(struct rcu_head *head)
115{
116 struct inet_frag_queue *q = container_of(head, struct inet_frag_queue,
117 rcu);
118 struct inet_frags *f = q->net->f;
119
120 if (f->destructor)
121 f->destructor(q);
122 kmem_cache_free(f->frags_cachep, q);
123}
124
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700125void inet_frag_destroy(struct inet_frag_queue *q)
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700126{
127 struct sk_buff *fp;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800128 struct netns_frags *nf;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000129 unsigned int sum, sum_truesize = 0;
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700130 struct inet_frags *f;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700131
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200132 WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700133 WARN_ON(del_timer(&q->timer) != 0);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700134
135 /* Release all fragment data. */
136 fp = q->fragments;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800137 nf = q->net;
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700138 f = nf->f;
Peter Oskolkov10043952018-10-10 12:30:13 -0700139 if (fp) {
140 do {
141 struct sk_buff *xp = fp->next;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700142
Peter Oskolkov10043952018-10-10 12:30:13 -0700143 sum_truesize += fp->truesize;
144 kfree_skb(fp);
145 fp = xp;
146 } while (fp);
147 } else {
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700148 sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700149 }
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000150 sum = sum_truesize + f->qsize;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700151
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700152 call_rcu(&q->rcu, inet_frag_destroy_rcu);
Florian Westphal5719b292015-07-23 12:05:39 +0200153
154 sub_frag_mem_limit(nf, sum);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700155}
156EXPORT_SYMBOL(inet_frag_destroy);
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700157
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800158static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
Nikolay Aleksandrovf926e232014-08-01 12:29:46 +0200159 struct inet_frags *f,
160 void *arg)
Pavel Emelyanove521db92007-10-17 19:45:23 -0700161{
162 struct inet_frag_queue *q;
163
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700164 if (!nf->high_thresh || frag_mem_limit(nf) > nf->high_thresh)
165 return NULL;
166
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200167 q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100168 if (!q)
Pavel Emelyanove521db92007-10-17 19:45:23 -0700169 return NULL;
170
Gao feng54db0cc2012-06-08 01:21:40 +0000171 q->net = nf;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700172 f->constructor(q, arg);
Florian Westphal0e60d242015-07-23 12:05:38 +0200173 add_frag_mem_limit(nf, f->qsize);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000174
Pavel Emelyanove521db92007-10-17 19:45:23 -0700175 setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
176 spin_lock_init(&q->lock);
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700177 atomic_set(&q->refcnt, 3);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700178
179 return q;
180}
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700181
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800182static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
Eric Dumazeteb183302018-11-08 17:34:27 -0800183 void *arg,
184 struct inet_frag_queue **prev)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700185{
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700186 struct inet_frags *f = nf->f;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700187 struct inet_frag_queue *q;
188
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800189 q = inet_frag_alloc(nf, f, arg);
Eric Dumazeteb183302018-11-08 17:34:27 -0800190 if (!q) {
191 *prev = ERR_PTR(-ENOMEM);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700192 return NULL;
Eric Dumazeteb183302018-11-08 17:34:27 -0800193 }
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700194 mod_timer(&q->timer, jiffies + nf->timeout);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700195
Eric Dumazeteb183302018-11-08 17:34:27 -0800196 *prev = rhashtable_lookup_get_insert_key(&nf->rhashtable, &q->key,
197 &q->node, f->rhash_params);
198 if (*prev) {
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700199 q->flags |= INET_FRAG_COMPLETE;
200 inet_frag_kill(q);
201 inet_frag_destroy(q);
Eric Dumazetc5282a02018-07-30 20:09:11 -0700202 return NULL;
203 }
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700204 return q;
205}
206EXPORT_SYMBOL(inet_frag_create);
Eric Dumazetc5282a02018-07-30 20:09:11 -0700207
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700208/* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */
209struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key)
210{
Eric Dumazeteb183302018-11-08 17:34:27 -0800211 struct inet_frag_queue *fq = NULL, *prev;
Florian Westphal86e93e42014-07-24 16:50:31 +0200212
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700213 rcu_read_lock();
Eric Dumazeteb183302018-11-08 17:34:27 -0800214 prev = rhashtable_lookup(&nf->rhashtable, key, nf->f->rhash_params);
215 if (!prev)
216 fq = inet_frag_create(nf, key, &prev);
217 if (prev && !IS_ERR(prev)) {
218 fq = prev;
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700219 if (!atomic_inc_not_zero(&fq->refcnt))
220 fq = NULL;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700221 }
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700222 rcu_read_unlock();
Eric Dumazeteb183302018-11-08 17:34:27 -0800223 return fq;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700224}
225EXPORT_SYMBOL(inet_frag_find);