Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 19 | #include <linux/random.h> |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/rtnetlink.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 23 | |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 24 | #include <net/sock.h> |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 25 | #include <net/inet_frag.h> |
Hannes Frederic Sowa | be99197 | 2013-03-22 08:24:37 +0000 | [diff] [blame] | 26 | #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 | */ |
| 32 | const 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 | }; |
| 47 | EXPORT_SYMBOL(ip_frag_ecn_table); |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 48 | |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 49 | int inet_frags_init(struct inet_frags *f) |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 50 | { |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 51 | 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 Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 57 | } |
| 58 | EXPORT_SYMBOL(inet_frags_init); |
| 59 | |
| 60 | void inet_frags_fini(struct inet_frags *f) |
| 61 | { |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 62 | /* We must wait that all inet_frag_destroy_rcu() have completed. */ |
| 63 | rcu_barrier(); |
| 64 | |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 65 | kmem_cache_destroy(f->frags_cachep); |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 66 | f->frags_cachep = NULL; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 67 | } |
| 68 | EXPORT_SYMBOL(inet_frags_fini); |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 69 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 70 | static 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 Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 91 | void inet_frags_exit_net(struct netns_frags *nf) |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 92 | { |
Paolo Abeni | 8c84965 | 2018-07-06 12:30:20 +0200 | [diff] [blame^] | 93 | nf->high_thresh = 0; /* prevent creation of new frags */ |
Florian Westphal | b13d3cb | 2014-07-24 16:50:32 +0200 | [diff] [blame] | 94 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 95 | rhashtable_free_and_destroy(&nf->rhashtable, inet_frags_free_cb, NULL); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(inet_frags_exit_net); |
| 98 | |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 99 | void inet_frag_kill(struct inet_frag_queue *fq) |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 100 | { |
| 101 | if (del_timer(&fq->timer)) |
| 102 | atomic_dec(&fq->refcnt); |
| 103 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 104 | if (!(fq->flags & INET_FRAG_COMPLETE)) { |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 105 | 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 Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 109 | atomic_dec(&fq->refcnt); |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 112 | EXPORT_SYMBOL(inet_frag_kill); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 113 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 114 | static 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 Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 125 | void inet_frag_destroy(struct inet_frag_queue *q) |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 126 | { |
| 127 | struct sk_buff *fp; |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 128 | struct netns_frags *nf; |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 129 | unsigned int sum, sum_truesize = 0; |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 130 | struct inet_frags *f; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 131 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 132 | WARN_ON(!(q->flags & INET_FRAG_COMPLETE)); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 133 | WARN_ON(del_timer(&q->timer) != 0); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 134 | |
| 135 | /* Release all fragment data. */ |
| 136 | fp = q->fragments; |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 137 | nf = q->net; |
Eric Dumazet | 2ffb1c3 | 2018-10-10 12:29:50 -0700 | [diff] [blame] | 138 | f = nf->f; |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 139 | if (fp) { |
| 140 | do { |
| 141 | struct sk_buff *xp = fp->next; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 142 | |
Peter Oskolkov | 1004395 | 2018-10-10 12:30:13 -0700 | [diff] [blame] | 143 | sum_truesize += fp->truesize; |
| 144 | kfree_skb(fp); |
| 145 | fp = xp; |
| 146 | } while (fp); |
| 147 | } else { |
Peter Oskolkov | 4077ddb | 2018-10-10 12:30:15 -0700 | [diff] [blame] | 148 | sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 149 | } |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 150 | sum = sum_truesize + f->qsize; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 151 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 152 | call_rcu(&q->rcu, inet_frag_destroy_rcu); |
Florian Westphal | 5719b29 | 2015-07-23 12:05:39 +0200 | [diff] [blame] | 153 | |
| 154 | sub_frag_mem_limit(nf, sum); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 155 | } |
| 156 | EXPORT_SYMBOL(inet_frag_destroy); |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 157 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 158 | static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf, |
Nikolay Aleksandrov | f926e23 | 2014-08-01 12:29:46 +0200 | [diff] [blame] | 159 | struct inet_frags *f, |
| 160 | void *arg) |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 161 | { |
| 162 | struct inet_frag_queue *q; |
| 163 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 164 | if (!nf->high_thresh || frag_mem_limit(nf) > nf->high_thresh) |
| 165 | return NULL; |
| 166 | |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 167 | q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 168 | if (!q) |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 169 | return NULL; |
| 170 | |
Gao feng | 54db0cc | 2012-06-08 01:21:40 +0000 | [diff] [blame] | 171 | q->net = nf; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 172 | f->constructor(q, arg); |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 173 | add_frag_mem_limit(nf, f->qsize); |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 174 | |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 175 | setup_timer(&q->timer, f->frag_expire, (unsigned long)q); |
| 176 | spin_lock_init(&q->lock); |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 177 | atomic_set(&q->refcnt, 3); |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 178 | |
| 179 | return q; |
| 180 | } |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 181 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 182 | static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf, |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 183 | void *arg, |
| 184 | struct inet_frag_queue **prev) |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 185 | { |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 186 | struct inet_frags *f = nf->f; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 187 | struct inet_frag_queue *q; |
| 188 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 189 | q = inet_frag_alloc(nf, f, arg); |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 190 | if (!q) { |
| 191 | *prev = ERR_PTR(-ENOMEM); |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 192 | return NULL; |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 193 | } |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 194 | mod_timer(&q->timer, jiffies + nf->timeout); |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 195 | |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 196 | *prev = rhashtable_lookup_get_insert_key(&nf->rhashtable, &q->key, |
| 197 | &q->node, f->rhash_params); |
| 198 | if (*prev) { |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 199 | q->flags |= INET_FRAG_COMPLETE; |
| 200 | inet_frag_kill(q); |
| 201 | inet_frag_destroy(q); |
Eric Dumazet | c5282a0 | 2018-07-30 20:09:11 -0700 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 204 | return q; |
| 205 | } |
| 206 | EXPORT_SYMBOL(inet_frag_create); |
Eric Dumazet | c5282a0 | 2018-07-30 20:09:11 -0700 | [diff] [blame] | 207 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 208 | /* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */ |
| 209 | struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key) |
| 210 | { |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 211 | struct inet_frag_queue *fq = NULL, *prev; |
Florian Westphal | 86e93e4 | 2014-07-24 16:50:31 +0200 | [diff] [blame] | 212 | |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 213 | rcu_read_lock(); |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 214 | 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 Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 219 | if (!atomic_inc_not_zero(&fq->refcnt)) |
| 220 | fq = NULL; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 221 | } |
Eric Dumazet | 23ce9c5 | 2018-10-10 12:29:56 -0700 | [diff] [blame] | 222 | rcu_read_unlock(); |
Eric Dumazet | eb18330 | 2018-11-08 17:34:27 -0800 | [diff] [blame] | 223 | return fq; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 224 | } |
| 225 | EXPORT_SYMBOL(inet_frag_find); |