Skip to contentSkip to conversation

netfilter: nf_tables: add NFTA_MASQ_UNSPEC to nft_masq_attributes

PATCH New Pablo Neira Ayuso 3 participants 12 messages Sep 11, 2014 – Sep 12, 2014

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/3 netfilter: nf_tables: add NFTA_MASQ_UNSPEC to nft_masq_attributes

To keep this consistent with other nft_*_attributes.

---
 include/uapi/linux/netfilter/nf_tables.h |    1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/netfilter/nf_tables.h +1 -0
index eeec0ae..66d66dd 100644
@@ -806,6 +806,7 @@ enum nft_nat_attributes {
  * @NFTA_MASQ_FLAGS: NAT flags (see NF_NAT_RANGE_* in linux/netfilter/nf_nat.h) (NLA_U32)
  */
 enum nft_masq_attributes {
+	NFTA_MASQ_UNSPEC,
 	NFTA_MASQ_FLAGS,
 	__NFTA_MASQ_MAX
 };
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH v2 3/3 netfilter: nf_tables: export rule-set generation ID

This patch exposes the ruleset generation ID in three ways:

1) The new command NFT_MSG_GETGEN that exposes the 32-bits ruleset generation ID. This ID is incremented in every commit and it should be large enough to avoid wraparound problems.

2) The less significant 16-bits of the generation ID is exposed through the nfgenmsg->res_id header field. This allows us to quickly catch if the ruleset has change between two consecutive list dumps from different object lists (in this specific case I think the risk of wraparound is unlikely).

3) Userspace subscribers may receive notifications of new rule-set generation after every commit. This also provides an alternative way to monitor the generation ID. If the events are lost, the userspace process hits a overrun error, so it knows that it is working with a stale ruleset anyway.

Patrick spotted that rule-set transformations in userspace may take quite some time. In that case, it annotates the 32-bits generation ID before fetching the rule-set, then:

1) it compares it to what we obtain after the transformation to make sure it is not working with a stale rule-set and no wraparound has ocurred.

2) it subscribes to ruleset notifications, so it can watch for new generation ID.

This is complementary to the NLM_F_DUMP_INTR approach, which allows us to detect an interference in the middle one single list dumping. There is no way to explicitly check that an interference has occurred between two list dumps from the kernel, since it doesn't know how many lists the userspace client is actually going to dump.

---
Change from v1: Expose the 32-bit generation ID through NFT_MSG_GETGEN and add event notification after the commit to notify the new generation ID to subscribers.

 include/uapi/linux/netfilter/nf_tables.h |   16 ++++
 net/netfilter/nf_tables_api.c            |  140 ++++++++++++++++++++++++------
 2 files changed, 130 insertions(+), 26 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +16 -0
index 66d66dd..b72ccfe 100644
@@ -51,6 +51,8 @@ enum nft_verdicts {
  * @NFT_MSG_NEWSETELEM: create a new set element (enum nft_set_elem_attributes)
  * @NFT_MSG_GETSETELEM: get a set element (enum nft_set_elem_attributes)
  * @NFT_MSG_DELSETELEM: delete a set element (enum nft_set_elem_attributes)
+ * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
+ * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -68,6 +70,8 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWSETELEM,
 	NFT_MSG_GETSETELEM,
 	NFT_MSG_DELSETELEM,
+	NFT_MSG_NEWGEN,
+	NFT_MSG_GETGEN,
 	NFT_MSG_MAX,
 };
 
@@ -812,4 +816,16 @@ enum nft_masq_attributes {
 };
 #define NFTA_MASQ_MAX		(__NFTA_MASQ_MAX - 1)
 
+/**
+ * enum nft_gen_attributes - nf_tables ruleset generation attributes
+ *
+ * @NFTA_GEN_ID: Ruleset generation ID (NLA_U32)
+ */
+enum nft_gen_attributes {
+	NFTA_GEN_UNSPEC,
+	NFTA_GEN_ID,
+	__NFTA_GEN_MAX
+};
+#define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
+
 #endif /* _LINUX_NF_TABLES_H */
net/netfilter/nf_tables_api.c +114 -26

266 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 12 lines · Pablo Neira Ayuso

This patch exposes the ruleset generation ID in three ways:

1) The new command NFT_MSG_GETGEN that exposes the 32-bits ruleset generation ID. This ID is incremented in every commit and it should be large enough to avoid wraparound problems.

2) The less significant 16-bits of the generation ID is exposed through the nfgenmsg->res_id header field. This allows us to quickly catch if the ruleset has change between two consecutive list dumps from different object lists (in this specific case I think the risk of wraparound is unlikely).

Quoted 5 lines

3) Userspace subscribers may receive notifications of new rule-set generation after every commit. This also provides an alternative way to monitor the generation ID. If the events are lost, the userspace process hits a overrun error, so it knows that it is working with a stale ruleset anyway.

Correct, there's just one thing to consider here, which is what happens once we add active ruleset state notifications, like counters, limit etc. At that point its not clear anymore whether changes have happened. OTOH it would be just a false positive, so at least things would keep working.

author maintainer
#3 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 19 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso

This patch exposes the ruleset generation ID in three ways:

1) The new command NFT_MSG_GETGEN that exposes the 32-bits ruleset generation ID. This ID is incremented in every commit and it should be large enough to avoid wraparound problems.

2) The less significant 16-bits of the generation ID is exposed through the nfgenmsg->res_id header field. This allows us to quickly catch if the ruleset has change between two consecutive list dumps from different object lists (in this specific case I think the risk of wraparound is unlikely).

Quoted 5 lines

3) Userspace subscribers may receive notifications of new rule-set generation after every commit. This also provides an alternative way to monitor the generation ID. If the events are lost, the userspace process hits a overrun error, so it knows that it is working with a stale ruleset anyway.

Quoted 5 lines

Correct, there's just one thing to consider here, which is what happens once we add active ruleset state notifications, like counters, limit etc. At that point its not clear anymore whether changes have happened. OTOH it would be just a false positive, so at least things would keep working.

Right, I can put the genid notification in a different nfnetlink multicast group (NFNLGRP_NFTABLES_GENID) to avoid false positives if you like the idea, we have plenty of spare groups.

#4 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 26 lines · Pablo Neira Ayuso
Quoted 19 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso

This patch exposes the ruleset generation ID in three ways:

1) The new command NFT_MSG_GETGEN that exposes the 32-bits ruleset generation ID. This ID is incremented in every commit and it should be large enough to avoid wraparound problems.

2) The less significant 16-bits of the generation ID is exposed through the nfgenmsg->res_id header field. This allows us to quickly catch if the ruleset has change between two consecutive list dumps from different object lists (in this specific case I think the risk of wraparound is unlikely).

Quoted 5 lines

3) Userspace subscribers may receive notifications of new rule-set generation after every commit. This also provides an alternative way to monitor the generation ID. If the events are lost, the userspace process hits a overrun error, so it knows that it is working with a stale ruleset anyway.

Quoted 5 lines

Correct, there's just one thing to consider here, which is what happens once we add active ruleset state notifications, like counters, limit etc. At that point its not clear anymore whether changes have happened. OTOH it would be just a false positive, so at least things would keep working.

Quoted 3 lines

Right, I can put the genid notification in a different nfnetlink multicast group (NFNLGRP_NFTABLES_GENID) to avoid false positives if you like the idea, we have plenty of spare groups.

I don't think that's a really good idea since the ordering between the rule notifications and the commit notification wouldn't be reliable. Same thing is probably true for state notifications, not entirely sure yet if they could reasonably be sent to a different group.

author maintainer
#5 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 27 lines · Patrick McHardy
Quoted 26 lines · Pablo Neira Ayuso
Quoted 24 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso

This patch exposes the ruleset generation ID in three ways:

1) The new command NFT_MSG_GETGEN that exposes the 32-bits ruleset generation ID. This ID is incremented in every commit and it should be large enough to avoid wraparound problems.

2) The less significant 16-bits of the generation ID is exposed through the nfgenmsg->res_id header field. This allows us to quickly catch if the ruleset has change between two consecutive list dumps from different object lists (in this specific case I think the risk of wraparound is unlikely).

Quoted 5 lines

3) Userspace subscribers may receive notifications of new rule-set generation after every commit. This also provides an alternative way to monitor the generation ID. If the events are lost, the userspace process hits a overrun error, so it knows that it is working with a stale ruleset anyway.

Correct, there's just one thing to consider here, which is what happens once we add active ruleset state notifications, like counters, limit etc. At that point its not clear anymore whether changes have happened. OTOH it would be just a false positive, so at least things would keep working.

Quoted 8 lines
Quoted 3 lines

Right, I can put the genid notification in a different nfnetlink multicast group (NFNLGRP_NFTABLES_GENID) to avoid false positives if you like the idea, we have plenty of spare groups.

I don't think that's a really good idea since the ordering between the rule notifications and the commit notification wouldn't be reliable. Same thing is probably true for state notifications, not entirely sure yet if they could reasonably be sent to a different group.

Indeed, we have to stick to one single group.

author maintainer
#6 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 32 lines · Pablo Neira Ayuso
Quoted 31 lines · Patrick McHardy
Quoted 29 lines · Pablo Neira Ayuso
Quoted 19 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso

This patch exposes the ruleset generation ID in three ways:

1) The new command NFT_MSG_GETGEN that exposes the 32-bits ruleset generation ID. This ID is incremented in every commit and it should be large enough to avoid wraparound problems.

2) The less significant 16-bits of the generation ID is exposed through the nfgenmsg->res_id header field. This allows us to quickly catch if the ruleset has change between two consecutive list dumps from different object lists (in this specific case I think the risk of wraparound is unlikely).

Quoted 5 lines

3) Userspace subscribers may receive notifications of new rule-set generation after every commit. This also provides an alternative way to monitor the generation ID. If the events are lost, the userspace process hits a overrun error, so it knows that it is working with a stale ruleset anyway.

Quoted 5 lines

Correct, there's just one thing to consider here, which is what happens once we add active ruleset state notifications, like counters, limit etc. At that point its not clear anymore whether changes have happened. OTOH it would be just a false positive, so at least things would keep working.

Right, I can put the genid notification in a different nfnetlink multicast group (NFNLGRP_NFTABLES_GENID) to avoid false positives if you like the idea, we have plenty of spare groups.

Quoted 6 lines
Quoted 4 lines

I don't think that's a really good idea since the ordering between the rule notifications and the commit notification wouldn't be reliable. Same thing is probably true for state notifications, not entirely sure yet if they could reasonably be sent to a different group.

Indeed, we have to stick to one single group.

Oh, you can subscribe to several groups from one single socket. So you get them notifications in order. IIRC, the grouping just provides a way to filter out what you don't want to listen.

#7 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Pablo Neira Ayuso
Quoted 9 lines · Patrick McHardy
Quoted 4 lines

Right, I can put the genid notification in a different nfnetlink multicast group (NFNLGRP_NFTABLES_GENID) to avoid false positives if you like the idea, we have plenty of spare groups.

I don't think that's a really good idea since the ordering between the rule notifications and the commit notification wouldn't be reliable. Same thing is probably true for state notifications, not entirely sure yet if they could reasonably be sent to a different group.

Quoted 6 lines
Quoted 1 line

Indeed, we have to stick to one single group.

Oh, you can subscribe to several groups from one single socket. So you get them notifications in order. IIRC, the grouping just provides a way to filter out what you don't want to listen.

Correct, but at that point we're back to square one because we don't know why the error orginated :)

author maintainer
#8 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 15 lines · Patrick McHardy
Quoted 14 lines · Pablo Neira Ayuso
Quoted 12 lines · Pablo Neira Ayuso
Quoted 9 lines · Patrick McHardy
Quoted 4 lines

Right, I can put the genid notification in a different nfnetlink multicast group (NFNLGRP_NFTABLES_GENID) to avoid false positives if you like the idea, we have plenty of spare groups.

I don't think that's a really good idea since the ordering between the rule notifications and the commit notification wouldn't be reliable. Same thing is probably true for state notifications, not entirely sure yet if they could reasonably be sent to a different group.

Indeed, we have to stick to one single group.

Quoted 7 lines
Quoted 4 lines

Oh, you can subscribe to several groups from one single socket. So you get them notifications in order. IIRC, the grouping just provides a way to filter out what you don't want to listen.

Correct, but at that point we're back to square one because we don't know why the error orginated :)

Right. I'm considering the (likely spamming) stateful notifications that we'll have at some point. Those we can put them in NFNLGRP_NFTABLES_STATES or something similar. So we leave NFNLGRP_NFTABLES for rule-set updates only (including the genid notification, of course).

#9 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

In reply to Pablo Neira Ayuso

net/netfilter/nf_tables_api.c static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb, · line 3333

Quoted 11 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +11 -0
+
+static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
+{
+       struct nlmsghdr *nlh = nlmsg_hdr(skb);
+       struct sk_buff *skb2;
+       int err;
+
+       if (nlmsg_report(nlh) &&
+           !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
+               return 0;
+

The above logic is different from the other functions.

I don't know if that was deliberate.

Quoted 12 lines
net/netfilter/nf_tables_api.c +12 -0
+       err = -ENOBUFS;
+       skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       if (skb2 == NULL)
+               goto err;
+
+       err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
+                                     nlh->nlmsg_seq);
+       if (err < 0) {
+               kfree_skb(skb2);
+               goto err;
+       }
+

net/netfilter/nf_tables_api.c static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb, · line 3355

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+       err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
+                            NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
+err:
+       if (err < 0) {
+               nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
+                                 err);
+       }
+       return err;
+}
+

All the xx_notify() functions looks very similar. Do you think it worth to factorize the code?

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#10 lore raw

Re: [PATCH nf-next 3/3 v2] netfilter: nf_tables: export rule-set generation ID

Quoted 15 lines · Arturo Borrero Gonzalez

net/netfilter/nf_tables_api.c static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb, · line 3333

Quoted 11 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +11 -0
+
+static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
+{
+       struct nlmsghdr *nlh = nlmsg_hdr(skb);
+       struct sk_buff *skb2;
+       int err;
+
+       if (nlmsg_report(nlh) &&
+           !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
+               return 0;
+

The above logic is different from the other functions.

Quoted 1 line

I don't know if that was deliberate.

We don't have nft_ctx at that point. nlmsg_report(nlh) is basically what ctx->report usually constains.

Unless I'm overlooking something obvious, this looks correct to me.

Quoted 17 lines
Quoted 17 lines
net/netfilter/nf_tables_api.c +17 -0
+       err = -ENOBUFS;
+       skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       if (skb2 == NULL)
+               goto err;
+
+       err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
+                                     nlh->nlmsg_seq);
+       if (err < 0) {
+               kfree_skb(skb2);
+               goto err;
+       }
+
+       err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
+                            NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
+err:
+       if (err < 0) {
+               nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
Quoted 8 lines

net/netfilter/nf_tables_api.c static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb, · line 3355

Quoted 5 lines
net/netfilter/nf_tables_api.c +5 -0
+                                 err);
+       }
+       return err;
+}
+

All the xx_notify() functions looks very similar. Do you think it worth to factorize the code?

I prefer if we focus on adding missing features, fixing bugs and so on. We'll have the time to revisit things later on.

author maintainer
#11 lore raw

PATCH 2/3 netfilter: nfnetlink: use original skbuff when committing/aborting

In reply to Pablo Neira Ayuso

This allows us to access the original content of the batch from the commit and the abort paths.

---
 net/netfilter/nfnetlink.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nfnetlink.c +2 -2
index c138b8f..dfc1a61 100644
@@ -357,9 +357,9 @@ ack:
 	}
 done:
 	if (success && done)
-		ss->commit(skb);
+		ss->commit(oskb);
 	else
-		ss->abort(skb);
+		ss->abort(oskb);
 
 	nfnl_unlock(subsys_id);
 	kfree_skb(nskb);
Signature · 1 line
1.7.10.4