Skip to contentSkip to conversation

netfilter: nfnetlink: add commit operation to nfnl_subsystems

RFC In review Pablo Neira Ayuso 1 participant 3 messages May 20, 2013

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

RFC 1/2 netfilter: nfnetlink: add commit operation to nfnl_subsystems

This patch adds the commit operations, that is called after processing a batch composed of several netlink messages. This only allows batches composed of netlink messages going to the same subsystem.

---
 net/netfilter/nfnetlink.c |   73 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 55 insertions(+), 18 deletions(-)
net/netfilter/nfnetlink.c +55 -18
index ffb92c0..c470123 100644
@@ -87,11 +87,21 @@ EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
 static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
 {
 	u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
+	const struct nfnetlink_subsystem *ss;
 
 	if (subsys_id >= NFNL_SUBSYS_COUNT)
 		return NULL;
 
-	return rcu_dereference(subsys_table[subsys_id]);
+	ss = rcu_dereference(subsys_table[subsys_id]);
+	if (!ss) {
+#ifdef CONFIG_MODULES
+		rcu_read_unlock();
+		request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
+		rcu_read_lock();
+		ss = nfnetlink_get_subsys(type);
+#endif
+	}
+	return ss;
 }
 
 static inline const struct nfnl_callback *
@@ -150,17 +160,8 @@ replay:
 	rcu_read_lock();
 	ss = nfnetlink_get_subsys(type);
 	if (!ss) {
-#ifdef CONFIG_MODULES
 		rcu_read_unlock();
-		request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
-		rcu_read_lock();
-		ss = nfnetlink_get_subsys(type);
-		if (!ss)
-#endif
-		{
-			rcu_read_unlock();
-			return -EINVAL;
-		}
+		return -EINVAL;
 	}
 
 	nc = nfnetlink_find_client(type, ss);
@@ -210,22 +211,58 @@ replay:
 
 static void nfnetlink_rcv(struct sk_buff *skb)
 {
+	unsigned int last_type = 0;
+
+	if (skb->len < nlmsg_total_size(0))
+		return;
+
+	/* This is a batch composed of multiple messages */
+	if (skb->len != nlmsg_hdr(skb)->nlmsg_len) {
+		struct nlmsghdr *nlh = nlmsg_hdr(skb);
+		unsigned int len = 0;
+
+		while (len < skb->len) {
+			/* We don't allow multiple messages in a batch
+			 * going to different subsystems.
+			 */
+			if (last_type &&
+			    last_type != NFNL_SUBSYS_ID(nlh->nlmsg_type))
+				return netlink_ack(skb, nlh, -EINVAL);
+
+			len += nlh->nlmsg_len;
+			last_type = NFNL_SUBSYS_ID(nlh->nlmsg_type);
+			nlh = (struct nlmsghdr *)(skb->data + len);
+		}
+	}
+
 	netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
+
+	/* Post-processing of this batch, if any. */
+	if (last_type) {
+		struct net *net = sock_net(skb->sk);
+		const struct nfnetlink_subsystem *ss;
+
+		rcu_read_lock();
+		ss = nfnetlink_get_subsys(last_type);
+		if (!ss) {
+			rcu_read_unlock();
+			return netlink_ack(skb, nlmsg_hdr(skb), -EINVAL);
+		}
+
+		if (ss->commit)
+			ss->commit(skb, net);
+
+		rcu_read_unlock();
+	}
 }
 
 #ifdef CONFIG_MODULES
 static void nfnetlink_bind(int group)
 {
-	const struct nfnetlink_subsystem *ss;
 	int type = nfnl_group2type[group];
 
 	rcu_read_lock();
-	ss = nfnetlink_get_subsys(type);
-	if (!ss) {
-		rcu_read_unlock();
-		request_module("nfnetlink-subsys-%d", type);
-		return;
-	}
+	nfnetlink_get_subsys(type);
 	rcu_read_unlock();
 }
 #endif
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

RFC 2/2 netfilter: nf_tables: improvements for the existing transaction approach

* One transaction comes in one batch packet composed of several netlink messages from user-space.

* All rule updates are handled as transactions.

* No need for explicit begin, commit and abort commands.

* Get rid of the extra struct list_head per rule: a temporary object is allocated to store the rule update information.

* There is a single list per net namespace that contains pending rule updates.

* The transaction list is now owned by the netlink socket portid that adds the first rule that waits to be committed. If another process wants to perform some rule-set update, it hits -EBUSY. Chances to hit this are low, as it is limited to the time that is required to process a batch.

Thanks to Patrick McHardy and Tomasz Burstyka for feedback and brainstorming on this patch.

---
 include/linux/netfilter/nfnetlink.h      |    1 +
 include/net/netfilter/nf_tables.h        |   21 ++-
 include/net/netns/nftables.h             |    2 +
 include/uapi/linux/netfilter/nf_tables.h |    8 --
 net/netfilter/nf_tables_api.c            |  223 ++++++++++++------------------
 5 files changed, 111 insertions(+), 144 deletions(-)
include/linux/netfilter/nfnetlink.h +1 -0
index 4966dde..b719fa8 100644
@@ -22,6 +22,7 @@ struct nfnetlink_subsystem {
 	const char *name;
 	__u8 subsys_id;			/* nfnetlink subsystem ID */
 	__u8 cb_count;			/* number of callbacks */
+	int (*commit)(struct sk_buff *, struct net *);
 	const struct nfnl_callback *cb;	/* callback for individual types */
 };
 
include/net/netfilter/nf_tables.h +17 -4
index 3782777..4baa84f 100644
@@ -321,7 +321,6 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)
  *	struct nft_rule - nf_tables rule
  *
  *	@list: used internally
- *	@dirty_list: this rule needs an update after new generation
  *	@rcu_head: used internally for rcu
  *	@handle: rule handle
  *	@genmask: generation mask
@@ -330,7 +329,6 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)
  */
 struct nft_rule {
 	struct list_head		list;
-	struct list_head		dirty_list;
 	struct rcu_head			rcu_head;
 	u64				handle:46,
 					genmask:2,
@@ -339,6 +337,23 @@ struct nft_rule {
 		__attribute__((aligned(__alignof__(struct nft_expr))));
 };
 
+/**
+ *	struct nft_rule_trans - nf_tables rule update in transaction
+ *
+ *	@list: used internally
+ *	@rule: rule that needs to be updated
+ *	@chain: chain that this rule belongs to
+ *	@table: table for which this chain applies
+ *	@net: the net namespace that this rule updates belongs to
+ */
+struct nft_rule_trans {
+	struct list_head		list;
+	struct nft_rule			*rule;
+	const struct nft_chain		*chain;
+	const struct nft_table		*table;
+	struct net			*net;
+};
+
 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
 {
 	return (struct nft_expr *)&rule->data[0];
@@ -372,7 +387,6 @@ enum nft_chain_flags {
  *	struct nft_chain - nf_tables chain
  *
  *	@rules: list of rules in the chain
- *	@dirty_rules: rules that need an update after next generation
  *	@list: used internally
  *	@rcu_head: used internally
  *	@net: net namespace that this chain belongs to
@@ -385,7 +399,6 @@ enum nft_chain_flags {
  */
 struct nft_chain {
 	struct list_head		rules;
-	struct list_head		dirty_rules;
 	struct list_head		list;
 	struct rcu_head			rcu_head;
 	struct net			*net;
include/net/netns/nftables.h +2 -0
index cc79f82..14d9f14 100644
@@ -10,6 +10,8 @@ struct netns_nftables {
 	struct nft_af_info	*ipv4;
 	struct nft_af_info	*ipv6;
 	struct nft_af_info	*bridge;
+	u32			transaction_owner;
+	struct list_head	transaction_rules;
 	u8			gencursor;
 	u8			genctr;
 };
include/uapi/linux/netfilter/nf_tables.h +0 -8
index 76b215f..43e8be1 100644
@@ -37,8 +37,6 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWSETELEM,
 	NFT_MSG_GETSETELEM,
 	NFT_MSG_DELSETELEM,
-	NFT_MSG_COMMIT,
-	NFT_MSG_ABORT,
 	NFT_MSG_MAX,
 };
 
@@ -88,18 +86,12 @@ enum nft_chain_attributes {
 };
 #define NFTA_CHAIN_MAX		(__NFTA_CHAIN_MAX - 1)
 
-enum {
-	NFT_RULE_F_COMMIT	= (1 << 0),
-	NFT_RULE_F_MASK		= NFT_RULE_F_COMMIT,
-};
-
 enum nft_rule_attributes {
 	NFTA_RULE_UNSPEC,
 	NFTA_RULE_TABLE,
 	NFTA_RULE_CHAIN,
 	NFTA_RULE_HANDLE,
 	NFTA_RULE_EXPRESSIONS,
-	NFTA_RULE_FLAGS,
 	NFTA_RULE_COMPAT,
 	__NFTA_RULE_MAX
 };
net/netfilter/nf_tables_api.c +91 -132

336 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author maintainer
#2 lore raw

Re: [RFC 2/2] netfilter: nf_tables: improvements for the existing transaction approach

Quoted 6 lines · Pablo Neira Ayuso

* One transaction comes in one batch packet composed of several netlink messages from user-space.

* All rule updates are handled as transactions.

* No need for explicit begin, commit and abort commands.

We seem to need an explicit commit operation, we still have to support iptables-restore -t, in that case the rule-set update is not applied. This is supported with the current approach but this RFC does not cover that case. I'm going to give it another try to re-spin to this.