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