Skip to content

Commit 3d058d7

Browse files
committed
netfilter: rework user-space expectation helper support
This partially reworks bc01bef which added userspace expectation support. This patch removes the nf_ct_userspace_expect_list since now we force to use the new iptables CT target feature to add the helper extension for conntracks that have attached expectations from userspace. A new version of the proof-of-concept code to implement userspace helpers from userspace is available at: http://people.netfilter.org/pablo/userspace-conntrack-helpers/nf-ftp-helper-POC.tar.bz2 This patch also modifies the CT target to allow to set the conntrack's userspace helper status flags. This flag is used to tell the conntrack system to explicitly allocate the helper extension. This helper extension is useful to link the userspace expectations with the master conntrack that is being tracked from one userspace helper. This feature fixes a problem in the current approach of the userspace helper support. Basically, if the master conntrack that has got a userspace expectation vanishes, the expectations point to one invalid memory address. Thus, triggering an oops in the expectation deletion event path. I decided not to add a new revision of the CT target because I only needed to add a new flag for it. I'll document in this issue in the iptables manpage. I have also changed the return value from EINVAL to EOPNOTSUPP if one flag not supported is specified. Thus, in the future adding new features that only require a new flag can be added without a new revision. There is no official code using this in userspace (apart from the proof-of-concept) that uses this infrastructure but there will be some by beginning 2012. Reported-by: Sam Roberts <vieuxtech@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent c4042a3 commit 3d058d7

7 files changed

Lines changed: 48 additions & 48 deletions

File tree

‎include/linux/netfilter/nf_conntrack_common.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ enum ip_conntrack_status {
8383
/* Conntrack is a fake untracked entry */
8484
IPS_UNTRACKED_BIT = 12,
8585
IPS_UNTRACKED = (1 << IPS_UNTRACKED_BIT),
86+
87+
/* Conntrack has a userspace helper. */
88+
IPS_USERSPACE_HELPER_BIT = 13,
89+
IPS_USERSPACE_HELPER = (1 << IPS_USERSPACE_HELPER_BIT),
8690
};
8791

8892
/* Connection tracking event types */

‎include/linux/netfilter/xt_CT.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
#include <linux/types.h>
55

6-
#define XT_CT_NOTRACK 0x1
6+
#define XT_CT_NOTRACK 0x1
7+
#define XT_CT_USERSPACE_HELPER 0x2
78

89
struct xt_ct_target_info {
910
__u16 flags;

‎include/net/netfilter/nf_conntrack_expect.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ static inline void nf_ct_unlink_expect(struct nf_conntrack_expect *exp)
9191

9292
void nf_ct_remove_expectations(struct nf_conn *ct);
9393
void nf_ct_unexpect_related(struct nf_conntrack_expect *exp);
94-
void nf_ct_remove_userspace_expectations(void);
9594

9695
/* Allocate space for an expectation: this is mandatory before calling
9796
nf_ct_expect_related. You will have to call put afterwards. */

‎net/netfilter/nf_conntrack_expect.c‎

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@ unsigned int nf_ct_expect_max __read_mostly;
3838

3939
static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
4040

41-
static HLIST_HEAD(nf_ct_userspace_expect_list);
42-
4341
/* nf_conntrack_expect helper functions */
4442
void nf_ct_unlink_expect_report(struct nf_conntrack_expect *exp,
4543
u32 pid, int report)
4644
{
4745
struct nf_conn_help *master_help = nfct_help(exp->master);
4846
struct net *net = nf_ct_exp_net(exp);
4947

48+
NF_CT_ASSERT(master_help);
5049
NF_CT_ASSERT(!timer_pending(&exp->timeout));
5150

5251
hlist_del_rcu(&exp->hnode);
5352
net->ct.expect_count--;
5453

5554
hlist_del(&exp->lnode);
56-
if (!(exp->flags & NF_CT_EXPECT_USERSPACE))
57-
master_help->expecting[exp->class]--;
55+
master_help->expecting[exp->class]--;
5856

5957
nf_ct_expect_event_report(IPEXP_DESTROY, exp, pid, report);
6058
nf_ct_expect_put(exp);
@@ -314,37 +312,34 @@ void nf_ct_expect_put(struct nf_conntrack_expect *exp)
314312
}
315313
EXPORT_SYMBOL_GPL(nf_ct_expect_put);
316314

317-
static void nf_ct_expect_insert(struct nf_conntrack_expect *exp)
315+
static int nf_ct_expect_insert(struct nf_conntrack_expect *exp)
318316
{
319317
struct nf_conn_help *master_help = nfct_help(exp->master);
318+
struct nf_conntrack_helper *helper;
320319
struct net *net = nf_ct_exp_net(exp);
321-
const struct nf_conntrack_expect_policy *p;
322320
unsigned int h = nf_ct_expect_dst_hash(&exp->tuple);
323321

324322
/* two references : one for hash insert, one for the timer */
325323
atomic_add(2, &exp->use);
326324

327-
if (master_help) {
328-
hlist_add_head(&exp->lnode, &master_help->expectations);
329-
master_help->expecting[exp->class]++;
330-
} else if (exp->flags & NF_CT_EXPECT_USERSPACE)
331-
hlist_add_head(&exp->lnode, &nf_ct_userspace_expect_list);
325+
hlist_add_head(&exp->lnode, &master_help->expectations);
326+
master_help->expecting[exp->class]++;
332327

333328
hlist_add_head_rcu(&exp->hnode, &net->ct.expect_hash[h]);
334329
net->ct.expect_count++;
335330

336331
setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
337332
(unsigned long)exp);
338-
if (master_help) {
339-
p = &rcu_dereference_protected(
340-
master_help->helper,
341-
lockdep_is_held(&nf_conntrack_lock)
342-
)->expect_policy[exp->class];
343-
exp->timeout.expires = jiffies + p->timeout * HZ;
333+
helper = rcu_dereference_protected(master_help->helper,
334+
lockdep_is_held(&nf_conntrack_lock));
335+
if (helper) {
336+
exp->timeout.expires = jiffies +
337+
helper->expect_policy[exp->class].timeout * HZ;
344338
}
345339
add_timer(&exp->timeout);
346340

347341
NF_CT_STAT_INC(net, expect_create);
342+
return 0;
348343
}
349344

350345
/* Race with expectations being used means we could have none to find; OK. */
@@ -389,14 +384,13 @@ static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect)
389384
struct nf_conntrack_expect *i;
390385
struct nf_conn *master = expect->master;
391386
struct nf_conn_help *master_help = nfct_help(master);
387+
struct nf_conntrack_helper *helper;
392388
struct net *net = nf_ct_exp_net(expect);
393389
struct hlist_node *n;
394390
unsigned int h;
395391
int ret = 1;
396392

397-
/* Don't allow expectations created from kernel-space with no helper */
398-
if (!(expect->flags & NF_CT_EXPECT_USERSPACE) &&
399-
(!master_help || (master_help && !master_help->helper))) {
393+
if (!master_help) {
400394
ret = -ESHUTDOWN;
401395
goto out;
402396
}
@@ -414,11 +408,10 @@ static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect)
414408
}
415409
}
416410
/* Will be over limit? */
417-
if (master_help) {
418-
p = &rcu_dereference_protected(
419-
master_help->helper,
420-
lockdep_is_held(&nf_conntrack_lock)
421-
)->expect_policy[expect->class];
411+
helper = rcu_dereference_protected(master_help->helper,
412+
lockdep_is_held(&nf_conntrack_lock));
413+
if (helper) {
414+
p = &helper->expect_policy[expect->class];
422415
if (p->max_expected &&
423416
master_help->expecting[expect->class] >= p->max_expected) {
424417
evict_oldest_expect(master, expect);
@@ -450,8 +443,9 @@ int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,
450443
if (ret <= 0)
451444
goto out;
452445

453-
ret = 0;
454-
nf_ct_expect_insert(expect);
446+
ret = nf_ct_expect_insert(expect);
447+
if (ret < 0)
448+
goto out;
455449
spin_unlock_bh(&nf_conntrack_lock);
456450
nf_ct_expect_event_report(IPEXP_NEW, expect, pid, report);
457451
return ret;
@@ -461,21 +455,6 @@ int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,
461455
}
462456
EXPORT_SYMBOL_GPL(nf_ct_expect_related_report);
463457

464-
void nf_ct_remove_userspace_expectations(void)
465-
{
466-
struct nf_conntrack_expect *exp;
467-
struct hlist_node *n, *next;
468-
469-
hlist_for_each_entry_safe(exp, n, next,
470-
&nf_ct_userspace_expect_list, lnode) {
471-
if (del_timer(&exp->timeout)) {
472-
nf_ct_unlink_expect(exp);
473-
nf_ct_expect_put(exp);
474-
}
475-
}
476-
}
477-
EXPORT_SYMBOL_GPL(nf_ct_remove_userspace_expectations);
478-
479458
#ifdef CONFIG_PROC_FS
480459
struct ct_expect_iter_state {
481460
struct seq_net_private p;

‎net/netfilter/nf_conntrack_helper.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
121121
int ret = 0;
122122

123123
if (tmpl != NULL) {
124+
/* we've got a userspace helper. */
125+
if (tmpl->status & IPS_USERSPACE_HELPER) {
126+
help = nf_ct_helper_ext_add(ct, flags);
127+
if (help == NULL) {
128+
ret = -ENOMEM;
129+
goto out;
130+
}
131+
rcu_assign_pointer(help->helper, NULL);
132+
__set_bit(IPS_USERSPACE_HELPER_BIT, &ct->status);
133+
ret = 0;
134+
goto out;
135+
}
124136
help = nfct_help(tmpl);
125137
if (help != NULL)
126138
helper = help->helper;

‎net/netfilter/nf_conntrack_netlink.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,10 @@ ctnetlink_create_expect(struct net *net, u16 zone,
20402040
}
20412041
help = nfct_help(ct);
20422042
if (!help) {
2043+
err = -EOPNOTSUPP;
2044+
goto out;
2045+
}
2046+
if (test_bit(IPS_USERSPACE_HELPER_BIT, &ct->status)) {
20432047
if (!cda[CTA_EXPECT_TIMEOUT]) {
20442048
err = -EINVAL;
20452049
goto out;
@@ -2264,7 +2268,6 @@ static void __exit ctnetlink_exit(void)
22642268
{
22652269
pr_info("ctnetlink: unregistering from nfnetlink.\n");
22662270

2267-
nf_ct_remove_userspace_expectations();
22682271
unregister_pernet_subsys(&ctnetlink_net_ops);
22692272
nfnetlink_subsys_unregister(&ctnl_exp_subsys);
22702273
nfnetlink_subsys_unregister(&ctnl_subsys);

‎net/netfilter/xt_CT.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par)
6262
int ret = 0;
6363
u8 proto;
6464

65-
if (info->flags & ~XT_CT_NOTRACK)
66-
return -EINVAL;
65+
if (info->flags & ~(XT_CT_NOTRACK | XT_CT_USERSPACE_HELPER))
66+
return -EOPNOTSUPP;
6767

6868
if (info->flags & XT_CT_NOTRACK) {
6969
ct = nf_ct_untracked_get();
@@ -92,7 +92,9 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par)
9292
GFP_KERNEL))
9393
goto err3;
9494

95-
if (info->helper[0]) {
95+
if (info->flags & XT_CT_USERSPACE_HELPER) {
96+
__set_bit(IPS_USERSPACE_HELPER_BIT, &ct->status);
97+
} else if (info->helper[0]) {
9698
ret = -ENOENT;
9799
proto = xt_ct_find_proto(par);
98100
if (!proto) {

0 commit comments

Comments
 (0)