Skip to content

Commit bc01bef

Browse files
ummakyneskaber
authored andcommitted
netfilter: ctnetlink: add support for user-space expectation helpers
This patch adds the basic infrastructure to support user-space expectation helpers via ctnetlink and the netfilter queuing infrastructure NFQUEUE. Basically, this patch: * adds NF_CT_EXPECT_USERSPACE flag to identify user-space created expectations. I have also added a sanity check in __nf_ct_expect_check() to avoid that kernel-space helpers may create an expectation if the master conntrack has no helper assigned. * adds some branches to check if the master conntrack helper exists, otherwise we skip the code that refers to kernel-space helper such as the local expectation list and the expectation policy. * allows to set the timeout for user-space expectations with no helper assigned. * a list of expectations created from user-space that depends on ctnetlink (if this module is removed, they are deleted). * includes USERSPACE in the /proc output for expectations that have been created by a user-space helper. This patch also modifies ctnetlink to skip including the helper name in the Netlink messages if no kernel-space helper is set (since no user-space expectation has not kernel-space kernel assigned). You can access an example user-space FTP conntrack helper at: http://people.netfilter.org/pablo/userspace-conntrack-helpers/nf-ftp-helper-userspace-POC.tar.bz Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent 8b008fa commit bc01bef

4 files changed

Lines changed: 79 additions & 31 deletions

File tree

‎include/linux/netfilter/nf_conntrack_common.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ enum ip_conntrack_expect_events {
103103
/* expectation flags */
104104
#define NF_CT_EXPECT_PERMANENT 0x1
105105
#define NF_CT_EXPECT_INACTIVE 0x2
106+
#define NF_CT_EXPECT_USERSPACE 0x4
106107

107108
#ifdef __KERNEL__
108109
struct ip_conntrack_stat {

‎include/net/netfilter/nf_conntrack_expect.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ nf_ct_find_expectation(struct net *net, u16 zone,
8585
void nf_ct_unlink_expect(struct nf_conntrack_expect *exp);
8686
void nf_ct_remove_expectations(struct nf_conn *ct);
8787
void nf_ct_unexpect_related(struct nf_conntrack_expect *exp);
88+
void nf_ct_remove_userspace_expectations(void);
8889

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

‎net/netfilter/nf_conntrack_expect.c‎

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,23 @@ static int nf_ct_expect_hash_rnd_initted __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+
4143
/* nf_conntrack_expect helper functions */
4244
void nf_ct_unlink_expect(struct nf_conntrack_expect *exp)
4345
{
4446
struct nf_conn_help *master_help = nfct_help(exp->master);
4547
struct net *net = nf_ct_exp_net(exp);
4648

47-
NF_CT_ASSERT(master_help);
4849
NF_CT_ASSERT(!timer_pending(&exp->timeout));
4950

5051
hlist_del_rcu(&exp->hnode);
5152
net->ct.expect_count--;
5253

5354
hlist_del(&exp->lnode);
54-
master_help->expecting[exp->class]--;
55+
if (!(exp->flags & NF_CT_EXPECT_USERSPACE))
56+
master_help->expecting[exp->class]--;
57+
5558
nf_ct_expect_put(exp);
5659

5760
NF_CT_STAT_INC(net, expect_delete);
@@ -320,16 +323,21 @@ static void nf_ct_expect_insert(struct nf_conntrack_expect *exp)
320323

321324
atomic_inc(&exp->use);
322325

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

326332
hlist_add_head_rcu(&exp->hnode, &net->ct.expect_hash[h]);
327333
net->ct.expect_count++;
328334

329335
setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
330336
(unsigned long)exp);
331-
p = &master_help->helper->expect_policy[exp->class];
332-
exp->timeout.expires = jiffies + p->timeout * HZ;
337+
if (master_help) {
338+
p = &master_help->helper->expect_policy[exp->class];
339+
exp->timeout.expires = jiffies + p->timeout * HZ;
340+
}
333341
add_timer(&exp->timeout);
334342

335343
atomic_inc(&exp->use);
@@ -380,7 +388,9 @@ static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect)
380388
unsigned int h;
381389
int ret = 1;
382390

383-
if (!master_help->helper) {
391+
/* Don't allow expectations created from kernel-space with no helper */
392+
if (!(expect->flags & NF_CT_EXPECT_USERSPACE) &&
393+
(!master_help || (master_help && !master_help->helper))) {
384394
ret = -ESHUTDOWN;
385395
goto out;
386396
}
@@ -398,13 +408,16 @@ static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect)
398408
}
399409
}
400410
/* Will be over limit? */
401-
p = &master_help->helper->expect_policy[expect->class];
402-
if (p->max_expected &&
403-
master_help->expecting[expect->class] >= p->max_expected) {
404-
evict_oldest_expect(master, expect);
405-
if (master_help->expecting[expect->class] >= p->max_expected) {
406-
ret = -EMFILE;
407-
goto out;
411+
if (master_help) {
412+
p = &master_help->helper->expect_policy[expect->class];
413+
if (p->max_expected &&
414+
master_help->expecting[expect->class] >= p->max_expected) {
415+
evict_oldest_expect(master, expect);
416+
if (master_help->expecting[expect->class]
417+
>= p->max_expected) {
418+
ret = -EMFILE;
419+
goto out;
420+
}
408421
}
409422
}
410423

@@ -439,6 +452,21 @@ int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,
439452
}
440453
EXPORT_SYMBOL_GPL(nf_ct_expect_related_report);
441454

455+
void nf_ct_remove_userspace_expectations(void)
456+
{
457+
struct nf_conntrack_expect *exp;
458+
struct hlist_node *n, *next;
459+
460+
hlist_for_each_entry_safe(exp, n, next,
461+
&nf_ct_userspace_expect_list, lnode) {
462+
if (del_timer(&exp->timeout)) {
463+
nf_ct_unlink_expect(exp);
464+
nf_ct_expect_put(exp);
465+
}
466+
}
467+
}
468+
EXPORT_SYMBOL_GPL(nf_ct_remove_userspace_expectations);
469+
442470
#ifdef CONFIG_PROC_FS
443471
struct ct_expect_iter_state {
444472
struct seq_net_private p;
@@ -529,8 +557,12 @@ static int exp_seq_show(struct seq_file *s, void *v)
529557
seq_printf(s, "PERMANENT");
530558
delim = ",";
531559
}
532-
if (expect->flags & NF_CT_EXPECT_INACTIVE)
560+
if (expect->flags & NF_CT_EXPECT_INACTIVE) {
533561
seq_printf(s, "%sINACTIVE", delim);
562+
delim = ",";
563+
}
564+
if (expect->flags & NF_CT_EXPECT_USERSPACE)
565+
seq_printf(s, "%sUSERSPACE", delim);
534566

535567
helper = rcu_dereference(nfct_help(expect->master)->helper);
536568
if (helper) {

‎net/netfilter/nf_conntrack_netlink.c‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,8 +1560,8 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
15601560
const struct nf_conntrack_expect *exp)
15611561
{
15621562
struct nf_conn *master = exp->master;
1563-
struct nf_conntrack_helper *helper;
15641563
long timeout = (exp->timeout.expires - jiffies) / HZ;
1564+
struct nf_conn_help *help;
15651565

15661566
if (timeout < 0)
15671567
timeout = 0;
@@ -1578,9 +1578,14 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
15781578
NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
15791579
NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
15801580
NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1581-
helper = rcu_dereference(nfct_help(master)->helper);
1582-
if (helper)
1583-
NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1581+
help = nfct_help(master);
1582+
if (help) {
1583+
struct nf_conntrack_helper *helper;
1584+
1585+
helper = rcu_dereference(help->helper);
1586+
if (helper)
1587+
NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1588+
}
15841589

15851590
return 0;
15861591

@@ -1921,24 +1926,32 @@ ctnetlink_create_expect(struct net *net, u16 zone,
19211926
if (!h)
19221927
return -ENOENT;
19231928
ct = nf_ct_tuplehash_to_ctrack(h);
1924-
help = nfct_help(ct);
1925-
1926-
if (!help || !help->helper) {
1927-
/* such conntrack hasn't got any helper, abort */
1928-
err = -EOPNOTSUPP;
1929-
goto out;
1930-
}
1931-
19321929
exp = nf_ct_expect_alloc(ct);
19331930
if (!exp) {
19341931
err = -ENOMEM;
19351932
goto out;
19361933
}
1934+
help = nfct_help(ct);
1935+
if (!help) {
1936+
if (!cda[CTA_EXPECT_TIMEOUT]) {
1937+
err = -EINVAL;
1938+
goto out;
1939+
}
1940+
exp->timeout.expires =
1941+
jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
19371942

1938-
if (cda[CTA_EXPECT_FLAGS])
1939-
exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1940-
else
1941-
exp->flags = 0;
1943+
exp->flags = NF_CT_EXPECT_USERSPACE;
1944+
if (cda[CTA_EXPECT_FLAGS]) {
1945+
exp->flags |=
1946+
ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1947+
}
1948+
} else {
1949+
if (cda[CTA_EXPECT_FLAGS]) {
1950+
exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1951+
exp->flags &= ~NF_CT_EXPECT_USERSPACE;
1952+
} else
1953+
exp->flags = 0;
1954+
}
19421955

19431956
exp->class = 0;
19441957
exp->expectfn = NULL;
@@ -2109,6 +2122,7 @@ static void __exit ctnetlink_exit(void)
21092122
{
21102123
pr_info("ctnetlink: unregistering from nfnetlink.\n");
21112124

2125+
nf_ct_remove_userspace_expectations();
21122126
#ifdef CONFIG_NF_CONNTRACK_EVENTS
21132127
nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
21142128
nf_conntrack_unregister_notifier(&ctnl_notifier);

0 commit comments

Comments
 (0)