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