Skip to contentSkip to conversation

ctnetlink updates for net-next

RFC PATCH New [email protected] 1 participant 7 messages Feb 9, 2012

Subsystems (6)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover ctnetlink updates for net-next

From: Pablo Neira Ayuso <[email protected]>

Hi,

The following are six patches for ctnetlink, four of them to finish the expectation support (this is required by conntrackd, next version will support expectation synchronization).

Two of them allows you to filter dumpings by mark. So we save cycles on building and parsing messages. One for netlink itself, to allow passing the filtering data (or whatever any other data you want to pass to the ->dump and ->done callbacks).

Let me know if you have any comment. I'll push the to my tree if nobody complains in the following days.

Thank you!

Pablo Neira Ayuso (6):
netfilter: ctnetlink: allow to set helper for new expectations
netfilter: ctnetlink: allow to set expectation class
netfilter: ctnetlink: add NAT support for expectations
netfilter: ctnetlink: allow to set expectfn for expectations
netlink: netlink_dump_start may take data pointer for callbacks
netfilter: ctnetlink: support kernel-space dump filterings

 crypto/crypto_user.c                          |    2 +-
 drivers/infiniband/core/netlink.c             |    2 +-
 include/linux/netfilter/nfnetlink_conntrack.h |   12 ++
 include/linux/netlink.h                       |    2 +
 include/net/netfilter/nf_conntrack_helper.h   |   13 ++
 net/core/rtnetlink.c                          |    2 +-
 net/ipv4/inet_diag.c                          |    4 +-
 net/ipv4/netfilter/nf_nat_core.c              |    8 ++
 net/ipv4/netfilter/nf_nat_h323.c              |   14 +++
 net/ipv4/netfilter/nf_nat_sip.c               |    7 +
 net/netfilter/ipset/ip_set_core.c             |    2 +-
 net/netfilter/nf_conntrack_helper.c           |   54 +++++++++
 net/netfilter/nf_conntrack_netlink.c          |  157 +++++++++++++++++++++++-
 net/netfilter/nfnetlink_acct.c                |    2 +-
 net/netlink/af_netlink.c                      |    2 +
 net/netlink/genetlink.c                       |    2 +-
 net/unix/diag.c                               |    2 +-
 net/xfrm/xfrm_user.c                          |    2 +-
 18 files changed, 272 insertions(+), 17 deletions(-)
Signature · 1 line
1.7.7.3
author maintainer
#1 lore raw

PATCH 1/6 netfilter: ctnetlink: allow to set helper for new expectations

From: Pablo Neira Ayuso <[email protected]>

This patch allow you to set the helper for newly created expectations based of the CTA_EXPECT_HELP_NAME attribute. Before this, the helper set was NULL.

---
 net/netfilter/nf_conntrack_netlink.c |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
net/netfilter/nf_conntrack_netlink.c +24 -1
index 9307b03..8338d67 100644
@@ -2017,6 +2017,7 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 	struct nf_conntrack_expect *exp;
 	struct nf_conn *ct;
 	struct nf_conn_help *help;
+	struct nf_conntrack_helper *helper = NULL;
 	int err = 0;
 
 	/* caller guarantees that those three CTA_EXPECT_* exist */
@@ -2035,6 +2036,28 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 	if (!h)
 		return -ENOENT;
 	ct = nf_ct_tuplehash_to_ctrack(h);
+
+	/* Look for helper of this expectation */
+	if (cda[CTA_EXPECT_HELP_NAME]) {
+		const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
+
+		helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
+						    nf_ct_protonum(ct));
+		if (helper == NULL) {
+#ifdef CONFIG_MODULES
+			if (request_module("nfct-helper-%s", helpname) < 0)
+				return -EOPNOTSUPP;
+
+			helper = __nf_conntrack_helper_find(helpname,
+							    nf_ct_l3num(ct),
+							    nf_ct_protonum(ct));
+			if (helper)
+				return -EAGAIN;
+#endif
+			return -EOPNOTSUPP;
+		}
+	}
+
 	exp = nf_ct_expect_alloc(ct);
 	if (!exp) {
 		err = -ENOMEM;
@@ -2065,7 +2088,7 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 	exp->class = 0;
 	exp->expectfn = NULL;
 	exp->master = ct;
-	exp->helper = NULL;
+	exp->helper = helper;
 	memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
 	memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
 	exp->mask.src.u.all = mask.src.u.all;
Signature · 1 line
1.7.7.3
author maintainer
#2 lore raw

PATCH 2/6 netfilter: ctnetlink: allow to set expectation class

In reply to [email protected]

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to set the expectation class.

---
 include/linux/netfilter/nfnetlink_conntrack.h |    1 +
 net/netfilter/nf_conntrack_netlink.c          |   10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletions(-)
include/linux/netfilter/nfnetlink_conntrack.h +1 -0
index debf1ae..40d7c19 100644
@@ -172,6 +172,7 @@ enum ctattr_expect {
 	CTA_EXPECT_HELP_NAME,
 	CTA_EXPECT_ZONE,
 	CTA_EXPECT_FLAGS,
+	CTA_EXPECT_CLASS,
 	__CTA_EXPECT_MAX
 };
 #define CTA_EXPECT_MAX (__CTA_EXPECT_MAX - 1)
net/netfilter/nf_conntrack_netlink.c +9 -1
index 8338d67..f185301 100644
@@ -1668,6 +1668,7 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
 	NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
 	NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
 	NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
+	NLA_PUT_BE32(skb, CTA_EXPECT_CLASS, htonl(exp->class));
 	help = nfct_help(master);
 	if (help) {
 		struct nf_conntrack_helper *helper;
@@ -1833,6 +1834,7 @@ static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
 	[CTA_EXPECT_HELP_NAME]	= { .type = NLA_NUL_STRING },
 	[CTA_EXPECT_ZONE]	= { .type = NLA_U16 },
 	[CTA_EXPECT_FLAGS]	= { .type = NLA_U32 },
+	[CTA_EXPECT_CLASS]	= { .type = NLA_U32 },
 };
 
 static int
@@ -2018,6 +2020,7 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 	struct nf_conn *ct;
 	struct nf_conn_help *help;
 	struct nf_conntrack_helper *helper = NULL;
+	u_int32_t class = 0;
 	int err = 0;
 
 	/* caller guarantees that those three CTA_EXPECT_* exist */
@@ -2058,6 +2061,11 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 		}
 	}
 
+	if (cda[CTA_EXPECT_CLASS] && helper) {
+		class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
+		if (class > helper->expect_class_max)
+			return -EINVAL;
+	}
 	exp = nf_ct_expect_alloc(ct);
 	if (!exp) {
 		err = -ENOMEM;
@@ -2085,7 +2093,7 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 			exp->flags = 0;
 	}
 
-	exp->class = 0;
+	exp->class = class;
 	exp->expectfn = NULL;
 	exp->master = ct;
 	exp->helper = helper;
Signature · 1 line
1.7.7.3
author maintainer
#3 lore raw

PATCH 3/6 netfilter: ctnetlink: add NAT support for expectations

In reply to [email protected]

From: Pablo Neira Ayuso <[email protected]>

This patch adds the missing bits to create expectations that are created in NAT setups.

---
 include/linux/netfilter/nfnetlink_conntrack.h |    9 +++
 net/netfilter/nf_conntrack_netlink.c          |   68 ++++++++++++++++++++++++-
 2 files changed, 75 insertions(+), 2 deletions(-)
include/linux/netfilter/nfnetlink_conntrack.h +9 -0
index 40d7c19..f447779 100644
@@ -173,10 +173,19 @@ enum ctattr_expect {
 	CTA_EXPECT_ZONE,
 	CTA_EXPECT_FLAGS,
 	CTA_EXPECT_CLASS,
+	CTA_EXPECT_NAT,
 	__CTA_EXPECT_MAX
 };
 #define CTA_EXPECT_MAX (__CTA_EXPECT_MAX - 1)
 
+enum ctattr_expect_nat {
+	CTA_EXPECT_NAT_UNSPEC,
+	CTA_EXPECT_NAT_DIR,
+	CTA_EXPECT_NAT_TUPLE,
+	__CTA_EXPECT_NAT_MAX
+};
+#define CTA_EXPECT_NAT_MAX (__CTA_EXPECT_NAT_MAX - 1)
+
 enum ctattr_help {
 	CTA_HELP_UNSPEC,
 	CTA_HELP_NAME,
net/netfilter/nf_conntrack_netlink.c +66 -2

100 lines not on this page · raw message

Signature · 1 line
1.7.7.3
author maintainer
#4 lore raw

PATCH 4/6 netfilter: ctnetlink: allow to set expectfn for expectations

In reply to [email protected]

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to set expectfn which is specifically used by the NAT side of most of the existing conntrack helpers.

I have added a symbol map that uses a string as key to look up for the function that is attached to the expectation object. This is the best solution I came out with to solve this issue.

---
 include/linux/netfilter/nfnetlink_conntrack.h |    1 +
 include/net/netfilter/nf_conntrack_helper.h   |   13 ++++++
 net/ipv4/netfilter/nf_nat_core.c              |    8 ++++
 net/ipv4/netfilter/nf_nat_h323.c              |   14 ++++++
 net/ipv4/netfilter/nf_nat_sip.c               |    7 +++
 net/netfilter/nf_conntrack_helper.c           |   54 +++++++++++++++++++++++++
 net/netfilter/nf_conntrack_netlink.c          |   19 ++++++++-
 7 files changed, 115 insertions(+), 1 deletions(-)
include/linux/netfilter/nfnetlink_conntrack.h +1 -0
index f447779..5ec1abc 100644
@@ -174,6 +174,7 @@ enum ctattr_expect {
 	CTA_EXPECT_FLAGS,
 	CTA_EXPECT_CLASS,
 	CTA_EXPECT_NAT,
+	CTA_EXPECT_FN,
 	__CTA_EXPECT_MAX
 };
 #define CTA_EXPECT_MAX (__CTA_EXPECT_MAX - 1)
include/net/netfilter/nf_conntrack_helper.h +13 -0
index f1c1311..5767dc2 100644
@@ -69,4 +69,17 @@ extern int nf_conntrack_broadcast_help(struct sk_buff *skb,
 				       enum ip_conntrack_info ctinfo,
 				       unsigned int timeout);
 
+struct nf_ct_helper_expectfn {
+	struct list_head head;
+	const char *name;
+	void (*expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
+};
+
+void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n);
+void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n);
+struct nf_ct_helper_expectfn *
+nf_ct_helper_expectfn_find_by_name(const char *name);
+struct nf_ct_helper_expectfn *
+nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
+
 #endif /*_NF_CONNTRACK_HELPER_H*/
net/ipv4/netfilter/nf_nat_core.c +8 -0
index a708933..abb52ad 100644
@@ -686,6 +686,11 @@ static struct pernet_operations nf_nat_net_ops = {
 	.exit = nf_nat_net_exit,
 };
 
+static struct nf_ct_helper_expectfn follow_master_nat = {
+	.name		= "nat-follow-master",
+	.expectfn	= nf_nat_follow_master,
+};
+
 static int __init nf_nat_init(void)
 {
 	size_t i;
@@ -717,6 +722,8 @@ static int __init nf_nat_init(void)
 
 	l3proto = nf_ct_l3proto_find_get((u_int16_t)AF_INET);
 
+	nf_ct_helper_expectfn_register(&follow_master_nat);
+
 	BUG_ON(nf_nat_seq_adjust_hook != NULL);
 	RCU_INIT_POINTER(nf_nat_seq_adjust_hook, nf_nat_seq_adjust);
 	BUG_ON(nfnetlink_parse_nat_setup_hook != NULL);
@@ -736,6 +743,7 @@ static void __exit nf_nat_cleanup(void)
 	unregister_pernet_subsys(&nf_nat_net_ops);
 	nf_ct_l3proto_put(l3proto);
 	nf_ct_extend_unregister(&nat_extend);
+	nf_ct_helper_expectfn_unregister(&follow_master_nat);
 	RCU_INIT_POINTER(nf_nat_seq_adjust_hook, NULL);
 	RCU_INIT_POINTER(nfnetlink_parse_nat_setup_hook, NULL);
 	RCU_INIT_POINTER(nf_ct_nat_offset, NULL);
net/ipv4/netfilter/nf_nat_h323.c +14 -0
index dc1dd91..8253670 100644
@@ -568,6 +568,16 @@ static int nat_callforwarding(struct sk_buff *skb, struct nf_conn *ct,
 	return 0;
 }
 
+static struct nf_ct_helper_expectfn q931_nat = {
+	.name		= "Q.931",
+	.expectfn	= ip_nat_q931_expect,
+};
+
+static struct nf_ct_helper_expectfn callforwarding_nat = {
+	.name		= "callforwarding",
+	.expectfn	= ip_nat_callforwarding_expect,
+};
+
 /****************************************************************************/
 static int __init init(void)
 {
@@ -590,6 +600,8 @@ static int __init init(void)
 	RCU_INIT_POINTER(nat_h245_hook, nat_h245);
 	RCU_INIT_POINTER(nat_callforwarding_hook, nat_callforwarding);
 	RCU_INIT_POINTER(nat_q931_hook, nat_q931);
+	nf_ct_helper_expectfn_register(&q931_nat);
+	nf_ct_helper_expectfn_register(&callforwarding_nat);
 	return 0;
 }
 
@@ -605,6 +617,8 @@ static void __exit fini(void)
 	RCU_INIT_POINTER(nat_h245_hook, NULL);
 	RCU_INIT_POINTER(nat_callforwarding_hook, NULL);
 	RCU_INIT_POINTER(nat_q931_hook, NULL);
+	nf_ct_helper_expectfn_unregister(&q931_nat);
+	nf_ct_helper_expectfn_unregister(&callforwarding_nat);
 	synchronize_rcu();
 }
 
net/ipv4/netfilter/nf_nat_sip.c +7 -0
index d0319f9..57932c4 100644
@@ -526,6 +526,11 @@ err1:
 	return NF_DROP;
 }
 
+static struct nf_ct_helper_expectfn sip_nat = {
+        .name           = "sip",
+        .expectfn       = ip_nat_sip_expected,
+};
+
 static void __exit nf_nat_sip_fini(void)
 {
 	RCU_INIT_POINTER(nf_nat_sip_hook, NULL);
@@ -535,6 +540,7 @@ static void __exit nf_nat_sip_fini(void)
 	RCU_INIT_POINTER(nf_nat_sdp_port_hook, NULL);
 	RCU_INIT_POINTER(nf_nat_sdp_session_hook, NULL);
 	RCU_INIT_POINTER(nf_nat_sdp_media_hook, NULL);
+	nf_ct_helper_expectfn_unregister(&sip_nat);
 	synchronize_rcu();
 }
 
@@ -554,6 +560,7 @@ static int __init nf_nat_sip_init(void)
 	RCU_INIT_POINTER(nf_nat_sdp_port_hook, ip_nat_sdp_port);
 	RCU_INIT_POINTER(nf_nat_sdp_session_hook, ip_nat_sdp_session);
 	RCU_INIT_POINTER(nf_nat_sdp_media_hook, ip_nat_sdp_media);
+	nf_ct_helper_expectfn_register(&sip_nat);
 	return 0;
 }
 
net/netfilter/nf_conntrack_helper.c +54 -0

60 lines not on this page · raw message

net/netfilter/nf_conntrack_netlink.c +18 -1
index ca630b0..e5d5d1c 100644
@@ -1656,6 +1656,8 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
 	struct nlattr *nest_parms;
 	struct nf_conntrack_tuple nat_tuple = {};
 #endif
+	struct nf_ct_helper_expectfn *expfn;
+
 	if (timeout < 0)
 		timeout = 0;
 
@@ -1699,6 +1701,9 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
 		if (helper)
 			NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
 	}
+	expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
+	if (expfn != NULL)
+		NLA_PUT_STRING(skb, CTA_EXPECT_FN, expfn->name);
 
 	return 0;
 
@@ -1858,6 +1863,7 @@ static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
 	[CTA_EXPECT_FLAGS]	= { .type = NLA_U32 },
 	[CTA_EXPECT_CLASS]	= { .type = NLA_U32 },
 	[CTA_EXPECT_NAT]	= { .type = NLA_NESTED },
+	[CTA_EXPECT_FN]		= { .type = NLA_NUL_STRING },
 };
 
 static int
@@ -2150,9 +2156,20 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 		} else
 			exp->flags = 0;
 	}
+	if (cda[CTA_EXPECT_FN]) {
+		const char *name = nla_data(cda[CTA_EXPECT_FN]);
+		struct nf_ct_helper_expectfn *expfn;
+
+		expfn = nf_ct_helper_expectfn_find_by_name(name);
+		if (expfn == NULL) {
+			err = -EINVAL;
+			goto err_out;
+		}
+		exp->expectfn = expfn->expectfn;
+	} else
+		exp->expectfn = NULL;
 
 	exp->class = class;
-	exp->expectfn = NULL;
 	exp->master = ct;
 	exp->helper = helper;
 	memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
Signature · 1 line
1.7.7.3
author maintainer
#5 lore raw

PATCH 5/6 netlink: netlink_dump_start may take data pointer for callbacks

In reply to [email protected]

From: Pablo Neira Ayuso <[email protected]>

This patch modifies the netlink_dump_start function to take one generic pointer to data. This pointer can be used inside the dump() and done() callbacks via cb->data.

Netfilter is going to use this patch to provide filtered dumps to user-space. This is specifically interesting in ctnetlink that may handle lots of conntrack entries. We can save precious cycles by skipping the conversion to TLV format of conntrack entries that are not interesting for user-space.

More specifically, ctnetlink will include one operation to allow to filter the dumping of conntrack entries by ctmark values.

---
 crypto/crypto_user.c                 |    2 +-
 drivers/infiniband/core/netlink.c    |    2 +-
 include/linux/netlink.h              |    2 ++
 net/core/rtnetlink.c                 |    2 +-
 net/ipv4/inet_diag.c                 |    4 ++--
 net/netfilter/ipset/ip_set_core.c    |    2 +-
 net/netfilter/nf_conntrack_netlink.c |    4 ++--
 net/netfilter/nfnetlink_acct.c       |    2 +-
 net/netlink/af_netlink.c             |    2 ++
 net/netlink/genetlink.c              |    2 +-
 net/unix/diag.c                      |    2 +-
 net/xfrm/xfrm_user.c                 |    2 +-
 12 files changed, 16 insertions(+), 12 deletions(-)
crypto/crypto_user.c +1 -1
index 16f8693..231d28a 100644
@@ -391,7 +391,7 @@ static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 			return -EINVAL;
 
 		return netlink_dump_start(crypto_nlsk, skb, nlh,
-					  link->dump, link->done, 0);
+					  link->dump, link->done, NULL, 0);
 	}
 
 	err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
drivers/infiniband/core/netlink.c +1 -1
index d1c8196..13e8098 100644
@@ -149,7 +149,7 @@ static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 				return -EINVAL;
 			return netlink_dump_start(nls, skb, nlh,
 						  client->cb_table[op].dump,
-						  NULL, 0);
+						  NULL, NULL, 0);
 		}
 	}
 
include/linux/netlink.h +2 -0
index 52e4895..ed764e4 100644
@@ -225,6 +225,7 @@ struct netlink_callback {
 	int			(*dump)(struct sk_buff * skb,
 					struct netlink_callback *cb);
 	int			(*done)(struct netlink_callback *cb);
+	void			*data;
 	u16			family;
 	u16			min_dump_alloc;
 	unsigned int		prev_seq, seq;
@@ -266,6 +267,7 @@ extern int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 			      const struct nlmsghdr *nlh,
 			      int (*dump)(struct sk_buff *skb, struct netlink_callback*),
 			      int (*done)(struct netlink_callback*),
+			      void *data,
 			      u16 min_dump_alloc);
 
 
net/core/rtnetlink.c +1 -1
index f16444b..49f49b7 100644
@@ -1979,7 +1979,7 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 		__rtnl_unlock();
 		rtnl = net->rtnl;
 		err = netlink_dump_start(rtnl, skb, nlh, dumpit,
-					 NULL, min_dump_alloc);
+					 NULL, NULL, min_dump_alloc);
 		rtnl_lock();
 		return err;
 	}
net/ipv4/inet_diag.c +2 -2
index fcf2818..c1ff5dd 100644
@@ -962,7 +962,7 @@ static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
 		}
 
 		return netlink_dump_start(sock_diag_nlsk, skb, nlh,
-					  inet_diag_dump_compat, NULL, 0);
+					  inet_diag_dump_compat, NULL, NULL, 0);
 	}
 
 	return inet_diag_get_exact_compat(skb, nlh);
@@ -987,7 +987,7 @@ static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
 		}
 
 		return netlink_dump_start(sock_diag_nlsk, skb, h,
-					  inet_diag_dump, NULL, 0);
+					  inet_diag_dump, NULL, NULL, 0);
 	}
 
 	return inet_diag_get_exact(skb, h, (struct inet_diag_req_v2 *)NLMSG_DATA(h));
net/netfilter/ipset/ip_set_core.c +1 -1
index 32dbf0f..8cfaa9c 100644
@@ -1164,7 +1164,7 @@ ip_set_dump(struct sock *ctnl, struct sk_buff *skb,
 
 	return netlink_dump_start(ctnl, skb, nlh,
 				  ip_set_dump_start,
-				  ip_set_dump_done, 0);
+				  ip_set_dump_done, NULL, 0);
 }
 
 /* Add, del and test */
net/netfilter/nf_conntrack_netlink.c +2 -2
index e5d5d1c..d7301f7 100644
@@ -979,7 +979,7 @@ ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
 
 	if (nlh->nlmsg_flags & NLM_F_DUMP)
 		return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
-					  ctnetlink_done, 0);
+					  ctnetlink_done, NULL, 0);
 
 	err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
 	if (err < 0)
@@ -1883,7 +1883,7 @@ ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
 		return netlink_dump_start(ctnl, skb, nlh,
 					  ctnetlink_exp_dump_table,
-					  ctnetlink_exp_done, 0);
+					  ctnetlink_exp_done, NULL, 0);
 	}
 
 	err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
net/netfilter/nfnetlink_acct.c +1 -1
index 11ba013..b7ea475 100644
@@ -172,7 +172,7 @@ nfnl_acct_get(struct sock *nfnl, struct sk_buff *skb,
 
 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
 		return netlink_dump_start(nfnl, skb, nlh, nfnl_acct_dump,
-					  NULL, 0);
+					  NULL, NULL, 0);
 	}
 
 	if (!tb[NFACCT_NAME])
net/netlink/af_netlink.c +2 -0
index 629b061..19446e8 100644
@@ -1721,6 +1721,7 @@ int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 		       int (*dump)(struct sk_buff *skb,
 				   struct netlink_callback *),
 		       int (*done)(struct netlink_callback *),
+		       void *data,
 		       u16 min_dump_alloc)
 {
 	struct netlink_callback *cb;
@@ -1736,6 +1737,7 @@ int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 	cb->done = done;
 	cb->nlh = nlh;
 	cb->min_dump_alloc = min_dump_alloc;
+	cb->data = data;
 	atomic_inc(&skb->users);
 	cb->skb = skb;
 
net/netlink/genetlink.c +1 -1
index c29d256..1ec141c 100644
@@ -533,7 +533,7 @@ static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 
 		genl_unlock();
 		err = netlink_dump_start(net->genl_sock, skb, nlh,
-					 ops->dumpit, ops->done, 0);
+					 ops->dumpit, ops->done, NULL, 0);
 		genl_lock();
 		return err;
 	}
net/unix/diag.c +1 -1
index 6b7697f..8ad0962 100644
@@ -303,7 +303,7 @@ static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
 
 	if (h->nlmsg_flags & NLM_F_DUMP)
 		return netlink_dump_start(sock_diag_nlsk, skb, h,
-					  unix_diag_dump, NULL, 0);
+					  unix_diag_dump, NULL, NULL, 0);
 	else
 		return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
 }
net/xfrm/xfrm_user.c +1 -1
index 66b84fb..eb86719 100644
@@ -2300,7 +2300,7 @@ static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 			return -EINVAL;
 
 		return netlink_dump_start(net->xfrm.nlsk, skb, nlh,
-					  link->dump, link->done, 0);
+					  link->dump, link->done, NULL, 0);
 	}
 
 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Signature · 1 line
1.7.7.3
author maintainer
#6 lore raw

PATCH 6/6 netfilter: ctnetlink: support kernel-space dump filterings

In reply to [email protected]

From: Pablo Neira Ayuso <[email protected]>

This patch adds CTA_MARK_MASK which, together with CTA_MARK, allows you to selectively send conntrack entries to user-space by returning those that match mark & mask.

With this, we can save cycles in the building and the parsing of the entries that may be later on filtered out in user-space by using the mark & mask.

---
 include/linux/netfilter/nfnetlink_conntrack.h |    1 +
 net/netfilter/nf_conntrack_netlink.c          |   35 +++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)
include/linux/netfilter/nfnetlink_conntrack.h +1 -0
index 5ec1abc..e58e4b9 100644
@@ -43,6 +43,7 @@ enum ctattr_type {
 	CTA_ZONE,
 	CTA_SECCTX,
 	CTA_TIMESTAMP,
+	CTA_MARK_MASK,
 	__CTA_MAX
 };
 #define CTA_MAX (__CTA_MAX - 1)
net/netfilter/nf_conntrack_netlink.c +33 -2
index d7301f7..a823540 100644
@@ -691,9 +691,18 @@ static int ctnetlink_done(struct netlink_callback *cb)
 {
 	if (cb->args[1])
 		nf_ct_put((struct nf_conn *)cb->args[1]);
+	if (cb->data)
+		kfree(cb->data);
 	return 0;
 }
 
+struct ctnetlink_dump_filter {
+	struct {
+		u_int32_t value;
+		u_int32_t mask;
+	} mark;
+};
+
 static int
 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
 {
@@ -703,6 +712,7 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
 	struct hlist_nulls_node *n;
 	struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
 	u_int8_t l3proto = nfmsg->nfgen_family;
+	const struct ctnetlink_dump_filter *filter = cb->data;
 
 	spin_lock_bh(&nf_conntrack_lock);
 	last = (struct nf_conn *)cb->args[1];
@@ -723,6 +733,10 @@ restart:
 					continue;
 				cb->args[1] = 0;
 			}
+			if (filter->mark.mask &&
+			    !(filter->mark.value ==
+					(ct->mark & filter->mark.mask)))
+				continue;
 			if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
 						cb->nlh->nlmsg_seq,
 						NFNL_MSG_TYPE(
@@ -894,6 +908,7 @@ static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
 	[CTA_NAT_DST]		= { .type = NLA_NESTED },
 	[CTA_TUPLE_MASTER]	= { .type = NLA_NESTED },
 	[CTA_ZONE]		= { .type = NLA_U16 },
+	[CTA_MARK_MASK]		= { .type = NLA_U32 },
 };
 
 static int
@@ -977,9 +992,25 @@ ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
 	u16 zone;
 	int err;
 
-	if (nlh->nlmsg_flags & NLM_F_DUMP)
+	if (nlh->nlmsg_flags & NLM_F_DUMP) {
+		struct ctnetlink_dump_filter *filter = NULL;
+
+#if defined(CONFIG_NF_CONNTRACK_MARK)
+		filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
+				 GFP_KERNEL);
+		if (filter == NULL)
+			return -ENOMEM;
+
+		if (cda[CTA_MARK])
+			filter->mark.value = ntohl(nla_get_be32(cda[CTA_MARK]));
+		if (cda[CTA_MARK_MASK]) {
+			filter->mark.mask =
+				ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
+		}
+#endif
 		return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
-					  ctnetlink_done, NULL, 0);
+					  ctnetlink_done, filter, 0);
+	}
 
 	err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
 	if (err < 0)
Signature · 1 line
1.7.7.3