blob: 0d6c72d6b9ba04227f541b47b7c2df91d98c7c12 [file] [log] [blame]
Martin Josefsson77ab9cf2006-11-29 02:34:58 +01001/* Expectation handling for nf_conntrack. */
2
3/* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02006 * (c) 2005-2012 Patrick McHardy <kaber@trash.net>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/types.h>
14#include <linux/netfilter.h>
15#include <linux/skbuff.h>
16#include <linux/proc_fs.h>
17#include <linux/seq_file.h>
18#include <linux/stddef.h>
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/percpu.h>
22#include <linux/kernel.h>
Patrick McHardya71c0852007-07-07 22:33:47 -070023#include <linux/jhash.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040024#include <linux/moduleparam.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040025#include <linux/export.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020026#include <net/net_namespace.h>
Florian Westphala9a083c2016-05-06 00:51:48 +020027#include <net/netns/hash.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010028
29#include <net/netfilter/nf_conntrack.h>
30#include <net/netfilter/nf_conntrack_core.h>
31#include <net/netfilter/nf_conntrack_expect.h>
32#include <net/netfilter/nf_conntrack_helper.h>
33#include <net/netfilter/nf_conntrack_tuple.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010034#include <net/netfilter/nf_conntrack_zones.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010035
Patrick McHardya71c0852007-07-07 22:33:47 -070036unsigned int nf_ct_expect_hsize __read_mostly;
37EXPORT_SYMBOL_GPL(nf_ct_expect_hsize);
38
Florian Westphal0a93aae2016-05-06 00:51:49 +020039struct hlist_head *nf_ct_expect_hash __read_mostly;
40EXPORT_SYMBOL_GPL(nf_ct_expect_hash);
41
Patrick McHardyf264a7d2007-07-07 22:36:24 -070042unsigned int nf_ct_expect_max __read_mostly;
Patrick McHardya71c0852007-07-07 22:33:47 -070043
Patrick McHardye9c1b082007-07-07 22:32:53 -070044static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
Florian Westphal7001c6d2016-04-18 16:17:00 +020045static unsigned int nf_ct_expect_hashrnd __read_mostly;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010046
47/* nf_conntrack_expect helper functions */
Pablo Neira Ayusoebbf41d2010-10-19 10:19:06 +020048void nf_ct_unlink_expect_report(struct nf_conntrack_expect *exp,
Patrick McHardyec464e52013-04-17 06:47:08 +000049 u32 portid, int report)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010050{
51 struct nf_conn_help *master_help = nfct_help(exp->master);
Alexey Dobriyan9b03f382008-10-08 11:35:03 +020052 struct net *net = nf_ct_exp_net(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010053
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +010054 NF_CT_ASSERT(master_help);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010055 NF_CT_ASSERT(!timer_pending(&exp->timeout));
56
Patrick McHardy7d0742d2008-01-31 04:38:19 -080057 hlist_del_rcu(&exp->hnode);
Alexey Dobriyan9b03f382008-10-08 11:35:03 +020058 net->ct.expect_count--;
Patrick McHardya71c0852007-07-07 22:33:47 -070059
Patrick McHardyb5605802007-07-07 22:35:56 -070060 hlist_del(&exp->lnode);
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +010061 master_help->expecting[exp->class]--;
Pablo Neira Ayusobc01befd2010-09-28 21:06:34 +020062
Patrick McHardyec464e52013-04-17 06:47:08 +000063 nf_ct_expect_event_report(IPEXP_DESTROY, exp, portid, report);
Patrick McHardy68236452007-07-07 22:30:49 -070064 nf_ct_expect_put(exp);
Patrick McHardyb5605802007-07-07 22:35:56 -070065
Alexey Dobriyan0d55af82008-10-08 11:35:07 +020066 NF_CT_STAT_INC(net, expect_delete);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010067}
Pablo Neira Ayusoebbf41d2010-10-19 10:19:06 +020068EXPORT_SYMBOL_GPL(nf_ct_unlink_expect_report);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010069
Patrick McHardy68236452007-07-07 22:30:49 -070070static void nf_ct_expectation_timed_out(unsigned long ul_expect)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010071{
72 struct nf_conntrack_expect *exp = (void *)ul_expect;
73
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +010074 spin_lock_bh(&nf_conntrack_expect_lock);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010075 nf_ct_unlink_expect(exp);
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +010076 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy68236452007-07-07 22:30:49 -070077 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010078}
79
Florian Westphala9a083c2016-05-06 00:51:48 +020080static unsigned int nf_ct_expect_dst_hash(const struct net *n, const struct nf_conntrack_tuple *tuple)
Patrick McHardya71c0852007-07-07 22:33:47 -070081{
Florian Westphala9a083c2016-05-06 00:51:48 +020082 unsigned int hash, seed;
Patrick McHardy34498822007-12-17 22:45:52 -080083
Florian Westphal7001c6d2016-04-18 16:17:00 +020084 get_random_once(&nf_ct_expect_hashrnd, sizeof(nf_ct_expect_hashrnd));
Patrick McHardya71c0852007-07-07 22:33:47 -070085
Florian Westphala9a083c2016-05-06 00:51:48 +020086 seed = nf_ct_expect_hashrnd ^ net_hash_mix(n);
87
Patrick McHardy34498822007-12-17 22:45:52 -080088 hash = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
Patrick McHardya71c0852007-07-07 22:33:47 -070089 (((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
Florian Westphala9a083c2016-05-06 00:51:48 +020090 (__force __u16)tuple->dst.u.all) ^ seed);
Daniel Borkmann8fc54f62014-08-23 20:58:54 +020091
92 return reciprocal_scale(hash, nf_ct_expect_hsize);
Patrick McHardya71c0852007-07-07 22:33:47 -070093}
94
Florian Westphal03d7dc52016-05-06 00:51:47 +020095static bool
96nf_ct_exp_equal(const struct nf_conntrack_tuple *tuple,
97 const struct nf_conntrack_expect *i,
98 const struct nf_conntrack_zone *zone,
99 const struct net *net)
100{
101 return nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask) &&
102 net_eq(net, nf_ct_net(i->master)) &&
103 nf_ct_zone_equal_any(i->master, zone);
104}
105
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100106struct nf_conntrack_expect *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200107__nf_ct_expect_find(struct net *net,
108 const struct nf_conntrack_zone *zone,
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100109 const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100110{
111 struct nf_conntrack_expect *i;
Patrick McHardya71c0852007-07-07 22:33:47 -0700112 unsigned int h;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100113
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200114 if (!net->ct.expect_count)
Patrick McHardya71c0852007-07-07 22:33:47 -0700115 return NULL;
116
Florian Westphala9a083c2016-05-06 00:51:48 +0200117 h = nf_ct_expect_dst_hash(net, tuple);
Florian Westphal0a93aae2016-05-06 00:51:49 +0200118 hlist_for_each_entry_rcu(i, &nf_ct_expect_hash[h], hnode) {
Florian Westphal03d7dc52016-05-06 00:51:47 +0200119 if (nf_ct_exp_equal(tuple, i, zone, net))
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100120 return i;
121 }
122 return NULL;
123}
Patrick McHardy68236452007-07-07 22:30:49 -0700124EXPORT_SYMBOL_GPL(__nf_ct_expect_find);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100125
126/* Just find a expectation corresponding to a tuple. */
127struct nf_conntrack_expect *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200128nf_ct_expect_find_get(struct net *net,
129 const struct nf_conntrack_zone *zone,
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100130 const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100131{
132 struct nf_conntrack_expect *i;
133
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800134 rcu_read_lock();
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100135 i = __nf_ct_expect_find(net, zone, tuple);
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800136 if (i && !atomic_inc_not_zero(&i->use))
137 i = NULL;
138 rcu_read_unlock();
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100139
140 return i;
141}
Patrick McHardy68236452007-07-07 22:30:49 -0700142EXPORT_SYMBOL_GPL(nf_ct_expect_find_get);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100143
144/* If an expectation for this connection is found, it gets delete from
145 * global list then returned. */
146struct nf_conntrack_expect *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200147nf_ct_find_expectation(struct net *net,
148 const struct nf_conntrack_zone *zone,
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100149 const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100150{
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700151 struct nf_conntrack_expect *i, *exp = NULL;
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700152 unsigned int h;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100153
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200154 if (!net->ct.expect_count)
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700155 return NULL;
156
Florian Westphala9a083c2016-05-06 00:51:48 +0200157 h = nf_ct_expect_dst_hash(net, tuple);
Florian Westphal0a93aae2016-05-06 00:51:49 +0200158 hlist_for_each_entry(i, &nf_ct_expect_hash[h], hnode) {
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700159 if (!(i->flags & NF_CT_EXPECT_INACTIVE) &&
Florian Westphal03d7dc52016-05-06 00:51:47 +0200160 nf_ct_exp_equal(tuple, i, zone, net)) {
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700161 exp = i;
162 break;
163 }
164 }
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800165 if (!exp)
166 return NULL;
167
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100168 /* If master is not in hash table yet (ie. packet hasn't left
169 this machine yet), how can other end know about expected?
170 Hence these are not the droids you are looking for (if
171 master ct never got confirmed, we'd hold a reference to it
172 and weird things would happen to future packets). */
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800173 if (!nf_ct_is_confirmed(exp->master))
174 return NULL;
175
Jesper Dangaard Brouere1b207d2014-03-03 14:45:39 +0100176 /* Avoid race with other CPUs, that for exp->master ct, is
177 * about to invoke ->destroy(), or nf_ct_delete() via timeout
178 * or early_drop().
179 *
180 * The atomic_inc_not_zero() check tells: If that fails, we
181 * know that the ct is being destroyed. If it succeeds, we
182 * can be sure the ct cannot disappear underneath.
183 */
184 if (unlikely(nf_ct_is_dying(exp->master) ||
185 !atomic_inc_not_zero(&exp->master->ct_general.use)))
186 return NULL;
187
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800188 if (exp->flags & NF_CT_EXPECT_PERMANENT) {
189 atomic_inc(&exp->use);
190 return exp;
191 } else if (del_timer(&exp->timeout)) {
192 nf_ct_unlink_expect(exp);
193 return exp;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100194 }
Jesper Dangaard Brouere1b207d2014-03-03 14:45:39 +0100195 /* Undo exp->master refcnt increase, if del_timer() failed */
196 nf_ct_put(exp->master);
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800197
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100198 return NULL;
199}
200
201/* delete all expectations for this conntrack */
202void nf_ct_remove_expectations(struct nf_conn *ct)
203{
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100204 struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyb5605802007-07-07 22:35:56 -0700205 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800206 struct hlist_node *next;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100207
208 /* Optimization: most connection never expect any others. */
Patrick McHardy6002f2662008-03-25 20:09:15 -0700209 if (!help)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100210 return;
211
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100212 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800213 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardyb5605802007-07-07 22:35:56 -0700214 if (del_timer(&exp->timeout)) {
215 nf_ct_unlink_expect(exp);
216 nf_ct_expect_put(exp);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800217 }
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100218 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100219 spin_unlock_bh(&nf_conntrack_expect_lock);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100220}
Patrick McHardy13b18332006-12-02 22:11:25 -0800221EXPORT_SYMBOL_GPL(nf_ct_remove_expectations);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100222
223/* Would two expected things clash? */
224static inline int expect_clash(const struct nf_conntrack_expect *a,
225 const struct nf_conntrack_expect *b)
226{
227 /* Part covered by intersection of masks must be unequal,
228 otherwise they clash */
Patrick McHardyd4156e82007-07-07 22:31:32 -0700229 struct nf_conntrack_tuple_mask intersect_mask;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100230 int count;
231
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100232 intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100233
234 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
235 intersect_mask.src.u3.all[count] =
236 a->mask.src.u3.all[count] & b->mask.src.u3.all[count];
237 }
238
Joe Stringer4b318142015-07-21 21:37:31 -0700239 return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask) &&
Florian Westphal03d7dc52016-05-06 00:51:47 +0200240 net_eq(nf_ct_net(a->master), nf_ct_net(b->master)) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200241 nf_ct_zone_equal_any(a->master, nf_ct_zone(b->master));
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100242}
243
244static inline int expect_matches(const struct nf_conntrack_expect *a,
245 const struct nf_conntrack_expect *b)
246{
Joe Perchesf64f9e72009-11-29 16:55:45 -0800247 return a->master == b->master && a->class == b->class &&
Daniel Borkmann308ac912015-08-08 21:40:01 +0200248 nf_ct_tuple_equal(&a->tuple, &b->tuple) &&
249 nf_ct_tuple_mask_equal(&a->mask, &b->mask) &&
Florian Westphal03d7dc52016-05-06 00:51:47 +0200250 net_eq(nf_ct_net(a->master), nf_ct_net(b->master)) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200251 nf_ct_zone_equal_any(a->master, nf_ct_zone(b->master));
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100252}
253
254/* Generally a bad idea to call this: could have matched already. */
Patrick McHardy68236452007-07-07 22:30:49 -0700255void nf_ct_unexpect_related(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100256{
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100257 spin_lock_bh(&nf_conntrack_expect_lock);
Patrick McHardy4e1d4e62007-07-07 22:32:03 -0700258 if (del_timer(&exp->timeout)) {
259 nf_ct_unlink_expect(exp);
260 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100261 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100262 spin_unlock_bh(&nf_conntrack_expect_lock);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100263}
Patrick McHardy68236452007-07-07 22:30:49 -0700264EXPORT_SYMBOL_GPL(nf_ct_unexpect_related);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100265
266/* We don't increase the master conntrack refcount for non-fulfilled
267 * conntracks. During the conntrack destruction, the expectations are
268 * always killed before the conntrack itself */
Patrick McHardy68236452007-07-07 22:30:49 -0700269struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100270{
271 struct nf_conntrack_expect *new;
272
Patrick McHardy68236452007-07-07 22:30:49 -0700273 new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100274 if (!new)
275 return NULL;
276
277 new->master = me;
278 atomic_set(&new->use, 1);
279 return new;
280}
Patrick McHardy68236452007-07-07 22:30:49 -0700281EXPORT_SYMBOL_GPL(nf_ct_expect_alloc);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100282
Patrick McHardy6002f2662008-03-25 20:09:15 -0700283void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200284 u_int8_t family,
Patrick McHardy1d9d7522008-03-25 20:07:58 -0700285 const union nf_inet_addr *saddr,
286 const union nf_inet_addr *daddr,
287 u_int8_t proto, const __be16 *src, const __be16 *dst)
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800288{
289 int len;
290
291 if (family == AF_INET)
292 len = 4;
293 else
294 len = 16;
295
296 exp->flags = 0;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700297 exp->class = class;
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800298 exp->expectfn = NULL;
299 exp->helper = NULL;
300 exp->tuple.src.l3num = family;
301 exp->tuple.dst.protonum = proto;
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800302
303 if (saddr) {
304 memcpy(&exp->tuple.src.u3, saddr, len);
305 if (sizeof(exp->tuple.src.u3) > len)
306 /* address needs to be cleared for nf_ct_tuple_equal */
307 memset((void *)&exp->tuple.src.u3 + len, 0x00,
308 sizeof(exp->tuple.src.u3) - len);
309 memset(&exp->mask.src.u3, 0xFF, len);
310 if (sizeof(exp->mask.src.u3) > len)
311 memset((void *)&exp->mask.src.u3 + len, 0x00,
312 sizeof(exp->mask.src.u3) - len);
313 } else {
314 memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3));
315 memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3));
316 }
317
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800318 if (src) {
Al Viroa34c4582007-07-26 17:33:19 +0100319 exp->tuple.src.u.all = *src;
320 exp->mask.src.u.all = htons(0xFFFF);
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800321 } else {
322 exp->tuple.src.u.all = 0;
323 exp->mask.src.u.all = 0;
324 }
325
Patrick McHardyd4156e82007-07-07 22:31:32 -0700326 memcpy(&exp->tuple.dst.u3, daddr, len);
327 if (sizeof(exp->tuple.dst.u3) > len)
328 /* address needs to be cleared for nf_ct_tuple_equal */
329 memset((void *)&exp->tuple.dst.u3 + len, 0x00,
330 sizeof(exp->tuple.dst.u3) - len);
331
Al Viroa34c4582007-07-26 17:33:19 +0100332 exp->tuple.dst.u.all = *dst;
Pablo Neira Ayusof09eca82013-07-09 20:16:39 +0200333
334#ifdef CONFIG_NF_NAT_NEEDED
335 memset(&exp->saved_addr, 0, sizeof(exp->saved_addr));
336 memset(&exp->saved_proto, 0, sizeof(exp->saved_proto));
337#endif
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800338}
Patrick McHardy68236452007-07-07 22:30:49 -0700339EXPORT_SYMBOL_GPL(nf_ct_expect_init);
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800340
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800341static void nf_ct_expect_free_rcu(struct rcu_head *head)
342{
343 struct nf_conntrack_expect *exp;
344
345 exp = container_of(head, struct nf_conntrack_expect, rcu);
346 kmem_cache_free(nf_ct_expect_cachep, exp);
347}
348
Patrick McHardy68236452007-07-07 22:30:49 -0700349void nf_ct_expect_put(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100350{
351 if (atomic_dec_and_test(&exp->use))
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800352 call_rcu(&exp->rcu, nf_ct_expect_free_rcu);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100353}
Patrick McHardy68236452007-07-07 22:30:49 -0700354EXPORT_SYMBOL_GPL(nf_ct_expect_put);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100355
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100356static int nf_ct_expect_insert(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100357{
358 struct nf_conn_help *master_help = nfct_help(exp->master);
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100359 struct nf_conntrack_helper *helper;
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200360 struct net *net = nf_ct_exp_net(exp);
Florian Westphala9a083c2016-05-06 00:51:48 +0200361 unsigned int h = nf_ct_expect_dst_hash(net, &exp->tuple);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100362
Eric Dumazet3bfd45f2010-11-16 10:19:18 +0100363 /* two references : one for hash insert, one for the timer */
364 atomic_add(2, &exp->use);
Patrick McHardyb5605802007-07-07 22:35:56 -0700365
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100366 hlist_add_head(&exp->lnode, &master_help->expectations);
367 master_help->expecting[exp->class]++;
Patrick McHardya71c0852007-07-07 22:33:47 -0700368
Florian Westphal0a93aae2016-05-06 00:51:49 +0200369 hlist_add_head_rcu(&exp->hnode, &nf_ct_expect_hash[h]);
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200370 net->ct.expect_count++;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100371
Patrick McHardy68236452007-07-07 22:30:49 -0700372 setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
373 (unsigned long)exp);
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100374 helper = rcu_dereference_protected(master_help->helper,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100375 lockdep_is_held(&nf_conntrack_expect_lock));
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100376 if (helper) {
377 exp->timeout.expires = jiffies +
378 helper->expect_policy[exp->class].timeout * HZ;
Pablo Neira Ayusobc01befd2010-09-28 21:06:34 +0200379 }
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100380 add_timer(&exp->timeout);
381
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200382 NF_CT_STAT_INC(net, expect_create);
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100383 return 0;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100384}
385
386/* Race with expectations being used means we could have none to find; OK. */
Patrick McHardy6002f2662008-03-25 20:09:15 -0700387static void evict_oldest_expect(struct nf_conn *master,
388 struct nf_conntrack_expect *new)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100389{
Patrick McHardyb5605802007-07-07 22:35:56 -0700390 struct nf_conn_help *master_help = nfct_help(master);
Patrick McHardy6002f2662008-03-25 20:09:15 -0700391 struct nf_conntrack_expect *exp, *last = NULL;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100392
Sasha Levinb67bfe02013-02-27 17:06:00 -0800393 hlist_for_each_entry(exp, &master_help->expectations, lnode) {
Patrick McHardy6002f2662008-03-25 20:09:15 -0700394 if (exp->class == new->class)
395 last = exp;
396 }
Patrick McHardyb5605802007-07-07 22:35:56 -0700397
Patrick McHardy6002f2662008-03-25 20:09:15 -0700398 if (last && del_timer(&last->timeout)) {
399 nf_ct_unlink_expect(last);
400 nf_ct_expect_put(last);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100401 }
402}
403
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100404static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100405{
Patrick McHardy6002f2662008-03-25 20:09:15 -0700406 const struct nf_conntrack_expect_policy *p;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100407 struct nf_conntrack_expect *i;
408 struct nf_conn *master = expect->master;
409 struct nf_conn_help *master_help = nfct_help(master);
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100410 struct nf_conntrack_helper *helper;
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200411 struct net *net = nf_ct_exp_net(expect);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800412 struct hlist_node *next;
Patrick McHardya71c0852007-07-07 22:33:47 -0700413 unsigned int h;
Jarno Rajahalmef107c6d2017-02-23 17:08:54 -0800414 int ret = 0;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100415
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100416 if (!master_help) {
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700417 ret = -ESHUTDOWN;
418 goto out;
419 }
Florian Westphala9a083c2016-05-06 00:51:48 +0200420 h = nf_ct_expect_dst_hash(net, &expect->tuple);
Florian Westphal0a93aae2016-05-06 00:51:49 +0200421 hlist_for_each_entry_safe(i, next, &nf_ct_expect_hash[h], hnode) {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100422 if (expect_matches(i, expect)) {
Pablo Neira Ayuso2614f862012-08-16 02:25:24 +0200423 if (del_timer(&i->timeout)) {
424 nf_ct_unlink_expect(i);
425 nf_ct_expect_put(i);
426 break;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100427 }
428 } else if (expect_clash(i, expect)) {
429 ret = -EBUSY;
430 goto out;
431 }
432 }
433 /* Will be over limit? */
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100434 helper = rcu_dereference_protected(master_help->helper,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100435 lockdep_is_held(&nf_conntrack_expect_lock));
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100436 if (helper) {
437 p = &helper->expect_policy[expect->class];
Pablo Neira Ayusobc01befd2010-09-28 21:06:34 +0200438 if (p->max_expected &&
439 master_help->expecting[expect->class] >= p->max_expected) {
440 evict_oldest_expect(master, expect);
441 if (master_help->expecting[expect->class]
442 >= p->max_expected) {
443 ret = -EMFILE;
444 goto out;
445 }
Patrick McHardy6002f2662008-03-25 20:09:15 -0700446 }
447 }
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100448
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200449 if (net->ct.expect_count >= nf_ct_expect_max) {
Joe Perchese87cc472012-05-13 21:56:26 +0000450 net_warn_ratelimited("nf_conntrack: expectation table full\n");
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700451 ret = -EMFILE;
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700452 }
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100453out:
454 return ret;
455}
456
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100457int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,
Patrick McHardyec464e52013-04-17 06:47:08 +0000458 u32 portid, int report)
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100459{
460 int ret;
461
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100462 spin_lock_bh(&nf_conntrack_expect_lock);
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100463 ret = __nf_ct_expect_check(expect);
Jarno Rajahalmef107c6d2017-02-23 17:08:54 -0800464 if (ret < 0)
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100465 goto out;
Pablo Neira Ayuso83731672009-04-06 17:47:20 +0200466
Pablo Neira Ayuso3d058d72011-12-18 01:55:54 +0100467 ret = nf_ct_expect_insert(expect);
468 if (ret < 0)
469 goto out;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100470 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardyec464e52013-04-17 06:47:08 +0000471 nf_ct_expect_event_report(IPEXP_NEW, expect, portid, report);
Pablo Neira Ayuso83731672009-04-06 17:47:20 +0200472 return ret;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100473out:
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100474 spin_unlock_bh(&nf_conntrack_expect_lock);
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100475 return ret;
476}
477EXPORT_SYMBOL_GPL(nf_ct_expect_related_report);
478
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200479#ifdef CONFIG_NF_CONNTRACK_PROCFS
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700480struct ct_expect_iter_state {
Alexey Dobriyandc5129f2008-10-08 11:35:06 +0200481 struct seq_net_private p;
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700482 unsigned int bucket;
483};
484
485static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100486{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700487 struct ct_expect_iter_state *st = seq->private;
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800488 struct hlist_node *n;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100489
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700490 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
Florian Westphal0a93aae2016-05-06 00:51:49 +0200491 n = rcu_dereference(hlist_first_rcu(&nf_ct_expect_hash[st->bucket]));
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800492 if (n)
493 return n;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100494 }
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700495 return NULL;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100496}
497
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700498static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
499 struct hlist_node *head)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100500{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700501 struct ct_expect_iter_state *st = seq->private;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100502
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100503 head = rcu_dereference(hlist_next_rcu(head));
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700504 while (head == NULL) {
505 if (++st->bucket >= nf_ct_expect_hsize)
506 return NULL;
Florian Westphal0a93aae2016-05-06 00:51:49 +0200507 head = rcu_dereference(hlist_first_rcu(&nf_ct_expect_hash[st->bucket]));
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700508 }
509 return head;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100510}
511
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700512static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
513{
514 struct hlist_node *head = ct_expect_get_first(seq);
515
516 if (head)
517 while (pos && (head = ct_expect_get_next(seq, head)))
518 pos--;
519 return pos ? NULL : head;
520}
521
522static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800523 __acquires(RCU)
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700524{
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800525 rcu_read_lock();
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700526 return ct_expect_get_idx(seq, *pos);
527}
528
529static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
530{
531 (*pos)++;
532 return ct_expect_get_next(seq, v);
533}
534
535static void exp_seq_stop(struct seq_file *seq, void *v)
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800536 __releases(RCU)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100537{
Patrick McHardy7d0742d2008-01-31 04:38:19 -0800538 rcu_read_unlock();
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100539}
540
541static int exp_seq_show(struct seq_file *s, void *v)
542{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700543 struct nf_conntrack_expect *expect;
Patrick McHardyb87921b2010-02-11 12:22:48 +0100544 struct nf_conntrack_helper *helper;
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700545 struct hlist_node *n = v;
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700546 char *delim = "";
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700547
548 expect = hlist_entry(n, struct nf_conntrack_expect, hnode);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100549
550 if (expect->timeout.function)
551 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
552 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
553 else
554 seq_printf(s, "- ");
555 seq_printf(s, "l3proto = %u proto=%u ",
556 expect->tuple.src.l3num,
557 expect->tuple.dst.protonum);
558 print_tuple(s, &expect->tuple,
559 __nf_ct_l3proto_find(expect->tuple.src.l3num),
Martin Josefsson605dcad2006-11-29 02:35:06 +0100560 __nf_ct_l4proto_find(expect->tuple.src.l3num,
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100561 expect->tuple.dst.protonum));
Patrick McHardy4bb119e2008-03-25 20:08:17 -0700562
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700563 if (expect->flags & NF_CT_EXPECT_PERMANENT) {
564 seq_printf(s, "PERMANENT");
565 delim = ",";
566 }
Pablo Neira Ayusobc01befd2010-09-28 21:06:34 +0200567 if (expect->flags & NF_CT_EXPECT_INACTIVE) {
Patrick McHardy359b9ab2008-03-25 20:08:37 -0700568 seq_printf(s, "%sINACTIVE", delim);
Pablo Neira Ayusobc01befd2010-09-28 21:06:34 +0200569 delim = ",";
570 }
571 if (expect->flags & NF_CT_EXPECT_USERSPACE)
572 seq_printf(s, "%sUSERSPACE", delim);
Patrick McHardy4bb119e2008-03-25 20:08:17 -0700573
Patrick McHardyb87921b2010-02-11 12:22:48 +0100574 helper = rcu_dereference(nfct_help(expect->master)->helper);
575 if (helper) {
576 seq_printf(s, "%s%s", expect->flags ? " " : "", helper->name);
Liping Zhangb173a282016-08-08 21:57:58 +0800577 if (helper->expect_policy[expect->class].name[0])
Patrick McHardyb87921b2010-02-11 12:22:48 +0100578 seq_printf(s, "/%s",
579 helper->expect_policy[expect->class].name);
580 }
581
Joe Perches1ca9e412015-03-16 11:25:17 -0700582 seq_putc(s, '\n');
583
584 return 0;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100585}
586
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700587static const struct seq_operations exp_seq_ops = {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100588 .start = exp_seq_start,
589 .next = exp_seq_next,
590 .stop = exp_seq_stop,
591 .show = exp_seq_show
592};
593
594static int exp_open(struct inode *inode, struct file *file)
595{
Alexey Dobriyandc5129f2008-10-08 11:35:06 +0200596 return seq_open_net(inode, file, &exp_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700597 sizeof(struct ct_expect_iter_state));
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100598}
599
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700600static const struct file_operations exp_file_ops = {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100601 .owner = THIS_MODULE,
602 .open = exp_open,
603 .read = seq_read,
604 .llseek = seq_lseek,
Alexey Dobriyandc5129f2008-10-08 11:35:06 +0200605 .release = seq_release_net,
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100606};
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200607#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Patrick McHardye9c1b082007-07-07 22:32:53 -0700608
Alexey Dobriyandc5129f2008-10-08 11:35:06 +0200609static int exp_proc_init(struct net *net)
Patrick McHardye9c1b082007-07-07 22:32:53 -0700610{
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200611#ifdef CONFIG_NF_CONNTRACK_PROCFS
Patrick McHardye9c1b082007-07-07 22:32:53 -0700612 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000613 kuid_t root_uid;
614 kgid_t root_gid;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700615
Gao fengd4beaa62013-02-18 01:34:54 +0000616 proc = proc_create("nf_conntrack_expect", 0440, net->proc_net,
617 &exp_file_ops);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700618 if (!proc)
619 return -ENOMEM;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000620
621 root_uid = make_kuid(net->user_ns, 0);
622 root_gid = make_kgid(net->user_ns, 0);
623 if (uid_valid(root_uid) && gid_valid(root_gid))
624 proc_set_user(proc, root_uid, root_gid);
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200625#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Patrick McHardye9c1b082007-07-07 22:32:53 -0700626 return 0;
627}
628
Alexey Dobriyandc5129f2008-10-08 11:35:06 +0200629static void exp_proc_remove(struct net *net)
Patrick McHardye9c1b082007-07-07 22:32:53 -0700630{
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200631#ifdef CONFIG_NF_CONNTRACK_PROCFS
Gao fengece31ff2013-02-18 01:34:56 +0000632 remove_proc_entry("nf_conntrack_expect", net->proc_net);
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200633#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Patrick McHardye9c1b082007-07-07 22:32:53 -0700634}
635
Alexey Dobriyan13ccdfc2010-02-08 11:17:22 -0800636module_param_named(expect_hashsize, nf_ct_expect_hsize, uint, 0400);
Patrick McHardya71c0852007-07-07 22:33:47 -0700637
Gao feng83b4dbe2013-01-21 22:10:25 +0000638int nf_conntrack_expect_pernet_init(struct net *net)
Patrick McHardye9c1b082007-07-07 22:32:53 -0700639{
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200640 net->ct.expect_count = 0;
Florian Westphal0a93aae2016-05-06 00:51:49 +0200641 return exp_proc_init(net);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700642}
643
Gao feng83b4dbe2013-01-21 22:10:25 +0000644void nf_conntrack_expect_pernet_fini(struct net *net)
Patrick McHardye9c1b082007-07-07 22:32:53 -0700645{
Alexey Dobriyandc5129f2008-10-08 11:35:06 +0200646 exp_proc_remove(net);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700647}
Gao feng83b4dbe2013-01-21 22:10:25 +0000648
649int nf_conntrack_expect_init(void)
650{
651 if (!nf_ct_expect_hsize) {
652 nf_ct_expect_hsize = nf_conntrack_htable_size / 256;
653 if (!nf_ct_expect_hsize)
654 nf_ct_expect_hsize = 1;
655 }
656 nf_ct_expect_max = nf_ct_expect_hsize * 4;
657 nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
658 sizeof(struct nf_conntrack_expect),
659 0, 0, NULL);
660 if (!nf_ct_expect_cachep)
661 return -ENOMEM;
Florian Westphal0a93aae2016-05-06 00:51:49 +0200662
663 nf_ct_expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize, 0);
664 if (!nf_ct_expect_hash) {
665 kmem_cache_destroy(nf_ct_expect_cachep);
666 return -ENOMEM;
667 }
668
Gao feng83b4dbe2013-01-21 22:10:25 +0000669 return 0;
670}
671
672void nf_conntrack_expect_fini(void)
673{
674 rcu_barrier(); /* Wait for call_rcu() before destroy */
675 kmem_cache_destroy(nf_ct_expect_cachep);
Florian Westphal0a93aae2016-05-06 00:51:49 +0200676 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_hsize);
Gao feng83b4dbe2013-01-21 22:10:25 +0000677}