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 | |
Florian Westphal | 86e93e4 | 2014-07-24 16:50:31 +0200 | [diff] [blame^] | 49 | static int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force); |
| 50 | |
Florian Westphal | fb3cfe6 | 2014-07-24 16:50:30 +0200 | [diff] [blame] | 51 | static unsigned int |
| 52 | inet_frag_hashfn(const struct inet_frags *f, const struct inet_frag_queue *q) |
| 53 | { |
| 54 | return f->hashfn(q) & (INETFRAGS_HASHSZ - 1); |
| 55 | } |
| 56 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 57 | static void inet_frag_secret_rebuild(unsigned long dummy) |
| 58 | { |
| 59 | struct inet_frags *f = (struct inet_frags *)dummy; |
| 60 | unsigned long now = jiffies; |
| 61 | int i; |
| 62 | |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 63 | /* Per bucket lock NOT needed here, due to write lock protection */ |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 64 | write_lock(&f->lock); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 65 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 66 | get_random_bytes(&f->rnd, sizeof(u32)); |
| 67 | for (i = 0; i < INETFRAGS_HASHSZ; i++) { |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 68 | struct inet_frag_bucket *hb; |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 69 | struct inet_frag_queue *q; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 70 | struct hlist_node *n; |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 71 | |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 72 | hb = &f->hash[i]; |
| 73 | hlist_for_each_entry_safe(q, n, &hb->chain, list) { |
Florian Westphal | fb3cfe6 | 2014-07-24 16:50:30 +0200 | [diff] [blame] | 74 | unsigned int hval = inet_frag_hashfn(f, q); |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 75 | |
| 76 | if (hval != i) { |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 77 | struct inet_frag_bucket *hb_dest; |
| 78 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 79 | hlist_del(&q->list); |
| 80 | |
| 81 | /* Relink to new hash chain. */ |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 82 | hb_dest = &f->hash[hval]; |
| 83 | hlist_add_head(&q->list, &hb_dest->chain); |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | write_unlock(&f->lock); |
| 88 | |
Pavel Emelyanov | 3b4bc4a | 2008-01-22 06:11:04 -0800 | [diff] [blame] | 89 | mod_timer(&f->secret_timer, now + f->secret_interval); |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 92 | void inet_frags_init(struct inet_frags *f) |
| 93 | { |
| 94 | int i; |
| 95 | |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 96 | for (i = 0; i < INETFRAGS_HASHSZ; i++) { |
| 97 | struct inet_frag_bucket *hb = &f->hash[i]; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 98 | |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 99 | spin_lock_init(&hb->chain_lock); |
| 100 | INIT_HLIST_HEAD(&hb->chain); |
| 101 | } |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 102 | rwlock_init(&f->lock); |
| 103 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 104 | setup_timer(&f->secret_timer, inet_frag_secret_rebuild, |
| 105 | (unsigned long)f); |
Pavel Emelyanov | 3b4bc4a | 2008-01-22 06:11:04 -0800 | [diff] [blame] | 106 | f->secret_timer.expires = jiffies + f->secret_interval; |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 107 | add_timer(&f->secret_timer); |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL(inet_frags_init); |
| 110 | |
Pavel Emelyanov | e5a2bb8 | 2008-01-22 06:06:23 -0800 | [diff] [blame] | 111 | void inet_frags_init_net(struct netns_frags *nf) |
| 112 | { |
| 113 | nf->nqueues = 0; |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 114 | init_frag_mem_limit(nf); |
Pavel Emelyanov | 3140c25 | 2008-01-22 06:11:48 -0800 | [diff] [blame] | 115 | INIT_LIST_HEAD(&nf->lru_list); |
Jesper Dangaard Brouer | 3ef0eb0 | 2013-01-28 23:45:51 +0000 | [diff] [blame] | 116 | spin_lock_init(&nf->lru_lock); |
Pavel Emelyanov | e5a2bb8 | 2008-01-22 06:06:23 -0800 | [diff] [blame] | 117 | } |
| 118 | EXPORT_SYMBOL(inet_frags_init_net); |
| 119 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 120 | void inet_frags_fini(struct inet_frags *f) |
| 121 | { |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 122 | del_timer(&f->secret_timer); |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 123 | } |
| 124 | EXPORT_SYMBOL(inet_frags_fini); |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 125 | |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 126 | void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f) |
| 127 | { |
| 128 | nf->low_thresh = 0; |
David S. Miller | e8e16b70 | 2008-03-28 17:30:18 -0700 | [diff] [blame] | 129 | |
| 130 | local_bh_disable(); |
Amerigo Wang | 6b10286 | 2012-09-18 16:50:11 +0000 | [diff] [blame] | 131 | inet_frag_evictor(nf, f, true); |
David S. Miller | e8e16b70 | 2008-03-28 17:30:18 -0700 | [diff] [blame] | 132 | local_bh_enable(); |
Jesper Dangaard Brouer | 6d7b857 | 2013-01-28 23:45:33 +0000 | [diff] [blame] | 133 | |
| 134 | percpu_counter_destroy(&nf->mem); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 135 | } |
| 136 | EXPORT_SYMBOL(inet_frags_exit_net); |
| 137 | |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 138 | static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f) |
| 139 | { |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 140 | struct inet_frag_bucket *hb; |
| 141 | unsigned int hash; |
| 142 | |
| 143 | read_lock(&f->lock); |
Florian Westphal | fb3cfe6 | 2014-07-24 16:50:30 +0200 | [diff] [blame] | 144 | hash = inet_frag_hashfn(f, fq); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 145 | hb = &f->hash[hash]; |
| 146 | |
| 147 | spin_lock(&hb->chain_lock); |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 148 | hlist_del(&fq->list); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 149 | spin_unlock(&hb->chain_lock); |
| 150 | |
| 151 | read_unlock(&f->lock); |
Jesper Dangaard Brouer | 3ef0eb0 | 2013-01-28 23:45:51 +0000 | [diff] [blame] | 152 | inet_frag_lru_del(fq); |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f) |
| 156 | { |
| 157 | if (del_timer(&fq->timer)) |
| 158 | atomic_dec(&fq->refcnt); |
| 159 | |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 160 | if (!(fq->last_in & INET_FRAG_COMPLETE)) { |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 161 | fq_unlink(fq, f); |
| 162 | atomic_dec(&fq->refcnt); |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 163 | fq->last_in |= INET_FRAG_COMPLETE; |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 166 | EXPORT_SYMBOL(inet_frag_kill); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 167 | |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 168 | static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f, |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 169 | struct sk_buff *skb) |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 170 | { |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 171 | if (f->skb_free) |
| 172 | f->skb_free(skb); |
| 173 | kfree_skb(skb); |
| 174 | } |
| 175 | |
| 176 | void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f, |
| 177 | int *work) |
| 178 | { |
| 179 | struct sk_buff *fp; |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 180 | struct netns_frags *nf; |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 181 | unsigned int sum, sum_truesize = 0; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 182 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 183 | WARN_ON(!(q->last_in & INET_FRAG_COMPLETE)); |
| 184 | WARN_ON(del_timer(&q->timer) != 0); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 185 | |
| 186 | /* Release all fragment data. */ |
| 187 | fp = q->fragments; |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 188 | nf = q->net; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 189 | while (fp) { |
| 190 | struct sk_buff *xp = fp->next; |
| 191 | |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 192 | sum_truesize += fp->truesize; |
| 193 | frag_kfree_skb(nf, f, fp); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 194 | fp = xp; |
| 195 | } |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 196 | sum = sum_truesize + f->qsize; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 197 | if (work) |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 198 | *work -= sum; |
| 199 | sub_frag_mem_limit(q, sum); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 200 | |
Pavel Emelyanov | c954770 | 2007-10-17 19:48:26 -0700 | [diff] [blame] | 201 | if (f->destructor) |
| 202 | f->destructor(q); |
| 203 | kfree(q); |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 204 | |
| 205 | } |
| 206 | EXPORT_SYMBOL(inet_frag_destroy); |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 207 | |
Florian Westphal | 86e93e4 | 2014-07-24 16:50:31 +0200 | [diff] [blame^] | 208 | static int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force) |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 209 | { |
| 210 | struct inet_frag_queue *q; |
| 211 | int work, evicted = 0; |
| 212 | |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 213 | work = frag_mem_limit(nf) - nf->low_thresh; |
Florian Westphal | e588e2f | 2014-03-06 18:06:41 +0100 | [diff] [blame] | 214 | while (work > 0 || force) { |
Jesper Dangaard Brouer | 3ef0eb0 | 2013-01-28 23:45:51 +0000 | [diff] [blame] | 215 | spin_lock(&nf->lru_lock); |
| 216 | |
Pavel Emelyanov | 3140c25 | 2008-01-22 06:11:48 -0800 | [diff] [blame] | 217 | if (list_empty(&nf->lru_list)) { |
Jesper Dangaard Brouer | 3ef0eb0 | 2013-01-28 23:45:51 +0000 | [diff] [blame] | 218 | spin_unlock(&nf->lru_lock); |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | |
Pavel Emelyanov | 3140c25 | 2008-01-22 06:11:48 -0800 | [diff] [blame] | 222 | q = list_first_entry(&nf->lru_list, |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 223 | struct inet_frag_queue, lru_list); |
| 224 | atomic_inc(&q->refcnt); |
Jesper Dangaard Brouer | 68399ac | 2013-03-27 05:55:25 +0000 | [diff] [blame] | 225 | /* Remove q from list to avoid several CPUs grabbing it */ |
| 226 | list_del_init(&q->lru_list); |
| 227 | |
Jesper Dangaard Brouer | 3ef0eb0 | 2013-01-28 23:45:51 +0000 | [diff] [blame] | 228 | spin_unlock(&nf->lru_lock); |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 229 | |
| 230 | spin_lock(&q->lock); |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 231 | if (!(q->last_in & INET_FRAG_COMPLETE)) |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 232 | inet_frag_kill(q, f); |
| 233 | spin_unlock(&q->lock); |
| 234 | |
| 235 | if (atomic_dec_and_test(&q->refcnt)) |
| 236 | inet_frag_destroy(q, f, &work); |
| 237 | evicted++; |
| 238 | } |
| 239 | |
| 240 | return evicted; |
| 241 | } |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 242 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 243 | static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, |
| 244 | struct inet_frag_queue *qp_in, struct inet_frags *f, |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 245 | void *arg) |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 246 | { |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 247 | struct inet_frag_bucket *hb; |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 248 | struct inet_frag_queue *qp; |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 249 | unsigned int hash; |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 250 | |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 251 | read_lock(&f->lock); /* Protects against hash rebuild */ |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 252 | /* |
| 253 | * While we stayed w/o the lock other CPU could update |
| 254 | * the rnd seed, so we need to re-calculate the hash |
| 255 | * chain. Fortunatelly the qp_in can be used to get one. |
| 256 | */ |
Florian Westphal | fb3cfe6 | 2014-07-24 16:50:30 +0200 | [diff] [blame] | 257 | hash = inet_frag_hashfn(f, qp_in); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 258 | hb = &f->hash[hash]; |
| 259 | spin_lock(&hb->chain_lock); |
| 260 | |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 261 | #ifdef CONFIG_SMP |
| 262 | /* With SMP race we have to recheck hash table, because |
| 263 | * such entry could be created on other cpu, while we |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 264 | * released the hash bucket lock. |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 265 | */ |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 266 | hlist_for_each_entry(qp, &hb->chain, list) { |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 267 | if (qp->net == nf && f->match(qp, arg)) { |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 268 | atomic_inc(&qp->refcnt); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 269 | spin_unlock(&hb->chain_lock); |
| 270 | read_unlock(&f->lock); |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 271 | qp_in->last_in |= INET_FRAG_COMPLETE; |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 272 | inet_frag_put(qp_in, f); |
| 273 | return qp; |
| 274 | } |
| 275 | } |
| 276 | #endif |
| 277 | qp = qp_in; |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 278 | if (!mod_timer(&qp->timer, jiffies + nf->timeout)) |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 279 | atomic_inc(&qp->refcnt); |
| 280 | |
| 281 | atomic_inc(&qp->refcnt); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 282 | hlist_add_head(&qp->list, &hb->chain); |
Nikolay Aleksandrov | 24b9bf4 | 2014-03-03 23:19:18 +0100 | [diff] [blame] | 283 | inet_frag_lru_add(nf, qp); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 284 | spin_unlock(&hb->chain_lock); |
| 285 | read_unlock(&f->lock); |
Nikolay Aleksandrov | 24b9bf4 | 2014-03-03 23:19:18 +0100 | [diff] [blame] | 286 | |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 287 | return qp; |
| 288 | } |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 289 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 290 | static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf, |
| 291 | struct inet_frags *f, void *arg) |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 292 | { |
| 293 | struct inet_frag_queue *q; |
| 294 | |
Florian Westphal | 86e93e4 | 2014-07-24 16:50:31 +0200 | [diff] [blame^] | 295 | if (frag_mem_limit(nf) > nf->high_thresh) |
| 296 | return NULL; |
| 297 | |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 298 | q = kzalloc(f->qsize, GFP_ATOMIC); |
| 299 | if (q == NULL) |
| 300 | return NULL; |
| 301 | |
Gao feng | 54db0cc | 2012-06-08 01:21:40 +0000 | [diff] [blame] | 302 | q->net = nf; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 303 | f->constructor(q, arg); |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 304 | add_frag_mem_limit(q, f->qsize); |
| 305 | |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 306 | setup_timer(&q->timer, f->frag_expire, (unsigned long)q); |
| 307 | spin_lock_init(&q->lock); |
| 308 | atomic_set(&q->refcnt, 1); |
Konstantin Khlebnikov | b56141a | 2013-05-05 04:56:22 +0000 | [diff] [blame] | 309 | INIT_LIST_HEAD(&q->lru_list); |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 310 | |
| 311 | return q; |
| 312 | } |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 313 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 314 | static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf, |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 315 | struct inet_frags *f, void *arg) |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 316 | { |
| 317 | struct inet_frag_queue *q; |
| 318 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 319 | q = inet_frag_alloc(nf, f, arg); |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 320 | if (q == NULL) |
| 321 | return NULL; |
| 322 | |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 323 | return inet_frag_intern(nf, q, f, arg); |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 324 | } |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 325 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 326 | struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, |
| 327 | struct inet_frags *f, void *key, unsigned int hash) |
Hannes Eder | 56bca31 | 2009-02-25 10:32:52 +0000 | [diff] [blame] | 328 | __releases(&f->lock) |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 329 | { |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 330 | struct inet_frag_bucket *hb; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 331 | struct inet_frag_queue *q; |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 332 | int depth = 0; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 333 | |
Florian Westphal | 86e93e4 | 2014-07-24 16:50:31 +0200 | [diff] [blame^] | 334 | if (frag_mem_limit(nf) > nf->high_thresh) |
| 335 | inet_frag_evictor(nf, f, false); |
| 336 | |
Florian Westphal | fb3cfe6 | 2014-07-24 16:50:30 +0200 | [diff] [blame] | 337 | hash &= (INETFRAGS_HASHSZ - 1); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 338 | hb = &f->hash[hash]; |
| 339 | |
| 340 | spin_lock(&hb->chain_lock); |
| 341 | hlist_for_each_entry(q, &hb->chain, list) { |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 342 | if (q->net == nf && f->match(q, key)) { |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 343 | atomic_inc(&q->refcnt); |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 344 | spin_unlock(&hb->chain_lock); |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 345 | read_unlock(&f->lock); |
| 346 | return q; |
| 347 | } |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 348 | depth++; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 349 | } |
Jesper Dangaard Brouer | 19952cc | 2013-04-03 23:38:16 +0000 | [diff] [blame] | 350 | spin_unlock(&hb->chain_lock); |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 351 | read_unlock(&f->lock); |
| 352 | |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 353 | if (depth <= INETFRAGS_MAXDEPTH) |
| 354 | return inet_frag_create(nf, f, key); |
| 355 | else |
| 356 | return ERR_PTR(-ENOBUFS); |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 357 | } |
| 358 | EXPORT_SYMBOL(inet_frag_find); |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 359 | |
| 360 | void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q, |
| 361 | const char *prefix) |
| 362 | { |
| 363 | static const char msg[] = "inet_frag_find: Fragment hash bucket" |
| 364 | " list length grew over limit " __stringify(INETFRAGS_MAXDEPTH) |
| 365 | ". Dropping fragment.\n"; |
| 366 | |
| 367 | if (PTR_ERR(q) == -ENOBUFS) |
| 368 | LIMIT_NETDEBUG(KERN_WARNING "%s%s", prefix, msg); |
| 369 | } |
| 370 | EXPORT_SYMBOL(inet_frag_maybe_warn_overflow); |