Skip to contentSkip to conversation

netfilter: nf_tables: extend payload to support writing data

RFC PATCH In review Nikolay Aleksandrov 2 participants 7 messages Feb 17, 2014 – Feb 19, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 netfilter: nf_tables: extend payload to support writing data

This patch extends the payload expression to support packet writing. The new payload attribute - SREG specifies the source register to use when changing packet data, the rest of the attributes are the same:
base - where to start from
offset - offset in the packet
len - length to write

The DREG attribute should not be set if writing is intended, if both attributes are set the SREG (packet writing) will take precedence. When the expression is dumped both registers will get dumped and the user can differentiate the type of the payload (reading/writing) by checking if the sreg attribute is different from zero.

---
I'm sending this patch early thus the RFC (I'm still testing), just to see if you have any comments on the structure and changes. Now to justify a few of the changes:
I added the sreg as a separate variable to struct nft_payload in order for the user to be able to recognize which payload type is the expression when dumping since both SREG and DREG get dumped.
I also factored the offset code in nft_payload_make_offset since it's used by both evaluation functions, returns -1 on error.
The two init functions check only the registers as that's what is different the rest of the attributes are still checked by the select_ops. I've also allowed to have both SREG and DREG set but in such case SREG takes precedence.
I left the old payload names as they were, but they can get _get or something else if you'd like.
One question though, I saw that you've applied my previous power of 2 patch but I didn't see it in net/net-next, is there another tree that I should be writing the patches against ?
This patch applies to Dave's net-next tree.

 include/net/netfilter/nf_tables_core.h   |   1 +
 include/uapi/linux/netfilter/nf_tables.h |   2 +
 net/netfilter/nft_payload.c              | 101 +++++++++++++++++++++++++------
 3 files changed, 84 insertions(+), 20 deletions(-)
include/net/netfilter/nf_tables_core.h +1 -0
index cf2b7ae..0a0bd9a 100644
@@ -32,6 +32,7 @@ struct nft_payload {
 	u8			offset;
 	u8			len;
 	enum nft_registers	dreg:8;
+	enum nft_registers	sreg:8;
 };
 
 extern const struct nft_expr_ops nft_payload_fast_ops;
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 83c985a..7730a36 100644
@@ -480,6 +480,7 @@ enum nft_payload_bases {
 /**
  * enum nft_payload_attributes - nf_tables payload expression netlink attributes
  *
+ * @NFTA_PAYLOAD_SREG: source register to load data from (NLA_U32: nft_registers)
  * @NFTA_PAYLOAD_DREG: destination register to load data into (NLA_U32: nft_registers)
  * @NFTA_PAYLOAD_BASE: payload base (NLA_U32: nft_payload_bases)
  * @NFTA_PAYLOAD_OFFSET: payload offset relative to base (NLA_U32)
@@ -491,6 +492,7 @@ enum nft_payload_attributes {
 	NFTA_PAYLOAD_BASE,
 	NFTA_PAYLOAD_OFFSET,
 	NFTA_PAYLOAD_LEN,
+	NFTA_PAYLOAD_SREG,
 	__NFTA_PAYLOAD_MAX
 };
 #define NFTA_PAYLOAD_MAX	(__NFTA_PAYLOAD_MAX - 1)
net/netfilter/nft_payload.c +81 -20

154 lines not on this page · raw message

Signature · 1 line
1.8.4.2
#1 lore raw
Quoted 7 lines · Nikolay Aleksandrov

This patch extends the payload expression to support packet writing. The new payload attribute - SREG specifies the source register to use when changing packet data, the rest of the attributes are the same:
base - where to start from
offset - offset in the packet
len - length to write

Quoted 5 lines

The DREG attribute should not be set if writing is intended, if both attributes are set the SREG (packet writing) will take precedence. When the expression is dumped both registers will get dumped and the user can differentiate the type of the payload (reading/writing) by checking if the sreg attribute is different from zero.

I'd suggest to return an error if both are set. We should only accept clearly defined input and reject everything else.

Quoted 6 lines

I'm sending this patch early thus the RFC (I'm still testing), just to see if you have any comments on the structure and changes. Now to justify a few of the changes:
I added the sreg as a separate variable to struct nft_payload in order for the user to be able to recognize which payload type is the expression when dumping since both SREG and DREG get dumped.

Adding another register seems fine. I'd suggest to only dump the one actually used though.

Quoted 6 lines

I also factored the offset code in nft_payload_make_offset since it's used by both evaluation functions, returns -1 on error.
The two init functions check only the registers as that's what is different the rest of the attributes are still checked by the select_ops. I've also allowed to have both SREG and DREG set but in such case SREG takes precedence.

See above.

Quoted 2 lines

I left the old payload names as they were, but they can get _get or something else if you'd like.

Actually I dislike the _get and _set suffixes, so I'm happy to use them as little as possible :)

net/netfilter/nft_payload.c line 21

Quoted 13 lines
net/netfilter/nft_payload.c +2 -3
index a2aeb31..1b42668 100644
@@ -17,23 +17,19 @@
 #include <net/netfilter/nf_tables_core.h>
 #include <net/netfilter/nf_tables.h>
 
-static void nft_payload_eval(const struct nft_expr *expr,
-			     struct nft_data data[NFT_REG_MAX + 1],
-			     const struct nft_pktinfo *pkt)
+static int nft_payload_make_offset(const struct nft_pktinfo *pkt,
+				   const struct nft_payload *payload)

We're using priv everywhere. Please keep this consistent. Also please make sure (in the entire patch) that arguments are aligned with the opening (.

Quoted 4 lines
net/netfilter/nft_payload.c +0 -3
 {
-	const struct nft_payload *priv = nft_expr_priv(expr);
-	const struct sk_buff *skb = pkt->skb;
-	struct nft_data *dest = &data[priv->dreg];

net/netfilter/nft_payload.c line 27

Quoted 10 lines
net/netfilter/nft_payload.c +3 -5
-	int offset;
+	int offset = -1;
 
-	switch (priv->base) {
+	switch (payload->base) {
 	case NFT_PAYLOAD_LL_HEADER:
-		if (!skb_mac_header_was_set(skb))
-			goto err;
-		offset = skb_mac_header(skb) - skb->data;
+		if (!skb_mac_header_was_set(pkt->skb))

Why don't you keep the local skb pointer?

Quoted 13 lines
net/netfilter/nft_payload.c +6 -1
+			return offset;
+		offset = skb_mac_header(pkt->skb) - pkt->skb->data;
 		break;
 	case NFT_PAYLOAD_NETWORK_HEADER:
-		offset = skb_network_offset(skb);
+		offset = skb_network_offset(pkt->skb);
 		break;
 	case NFT_PAYLOAD_TRANSPORT_HEADER:
 		offset = pkt->xt.thoff;
 
+static void nft_payload_set_eval(const struct nft_expr *expr,
+				 struct nft_data data[NFT_REG_MAX + 1],
+				 const struct nft_pktinfo *pkt)

net/netfilter/nft_payload.c

Quoted 10 lines
net/netfilter/nft_payload.c +10 -0
+{
+	const struct nft_payload *priv = nft_expr_priv(expr);
+	struct nft_data *source = &data[priv->sreg];
+	int offset = nft_payload_make_offset(pkt, priv);
+
+	if (offset == -1)
+		goto err;
+	if (!skb_make_writable(pkt->skb, offset + priv->len))
+		goto err;
+	memcpy(pkt->skb->data + offset, source, priv->len);

We need to take care of checksumming. Shouldn't be too hard to get *most* cases right using the existing checksum helpers.

Quoted 6 lines
net/netfilter/nft_payload.c +6 -0
+
+	return;
+err:
+	data[NFT_REG_VERDICT].verdict = NFT_BREAK;
+}
+
#2 lore raw
Quoted 8 lines · Patrick McHardy
Quoted 7 lines · Nikolay Aleksandrov

This patch extends the payload expression to support packet writing. The new payload attribute - SREG specifies the source register to use when changing packet data, the rest of the attributes are the same:
base - where to start from
offset - offset in the packet
len - length to write

Quoted 9 lines
Quoted 5 lines

The DREG attribute should not be set if writing is intended, if both attributes are set the SREG (packet writing) will take precedence. When the expression is dumped both registers will get dumped and the user can differentiate the type of the payload (reading/writing) by checking if the sreg attribute is different from zero.

I'd suggest to return an error if both are set. We should only accept clearly defined input and reject everything else.

Okay, will do.

Quoted 10 lines
Quoted 6 lines

I'm sending this patch early thus the RFC (I'm still testing), just to see if you have any comments on the structure and changes. Now to justify a few of the changes:
I added the sreg as a separate variable to struct nft_payload in order for the user to be able to recognize which payload type is the expression when dumping since both SREG and DREG get dumped.

Adding another register seems fine. I'd suggest to only dump the one actually used though.

Given the previous comment, yep :)

Quoted 23 lines
Quoted 6 lines

I also factored the offset code in nft_payload_make_offset since it's used by both evaluation functions, returns -1 on error.
The two init functions check only the registers as that's what is different the rest of the attributes are still checked by the select_ops. I've also allowed to have both SREG and DREG set but in such case SREG takes precedence.

See above.

Quoted 2 lines

I left the old payload names as they were, but they can get _get or something else if you'd like.

Actually I dislike the _get and _set suffixes, so I'm happy to use them as little as possible :)

Quoted 8 lines
net/netfilter/nft_payload.c +0 -0
index a2aeb31..1b42668 100644
@@ -17,23 +17,19 @@
 #include <net/netfilter/nf_tables_core.h>
 #include <net/netfilter/nf_tables.h>
 
Quoted 10 lines

net/netfilter/nft_payload.c line 21

Quoted 5 lines
net/netfilter/nft_payload.c +2 -3
-static void nft_payload_eval(const struct nft_expr *expr,
-			     struct nft_data data[NFT_REG_MAX + 1],
-			     const struct nft_pktinfo *pkt)
+static int nft_payload_make_offset(const struct nft_pktinfo *pkt,
+				   const struct nft_payload *payload)

We're using priv everywhere. Please keep this consistent. Also please make sure (in the entire patch) that arguments are aligned with the opening (.

Okay, I'll use the priv.
They're adjusted, apply the patch and see for yourself, or do you mean some other adjustment ?
Both arguments are aligned from what I can see.

Quoted 7 lines
Quoted 7 lines
net/netfilter/nft_payload.c +1 -4
 {
-	const struct nft_payload *priv = nft_expr_priv(expr);
-	const struct sk_buff *skb = pkt->skb;
-	struct nft_data *dest = &data[priv->dreg];
-	int offset;
+	int offset = -1;
 
Quoted 10 lines

net/netfilter/nft_payload.c line 27

Quoted 7 lines
net/netfilter/nft_payload.c +2 -4
-	switch (priv->base) {
+	switch (payload->base) {
 	case NFT_PAYLOAD_LL_HEADER:
-		if (!skb_mac_header_was_set(skb))
-			goto err;
-		offset = skb_mac_header(skb) - skb->data;
+		if (!skb_mac_header_was_set(pkt->skb))

Why don't you keep the local skb pointer?

Anyway is fine by me, I'll leave it.

Quoted 10 lines
Quoted 9 lines
net/netfilter/nft_payload.c +3 -1
+			return offset;
+		offset = skb_mac_header(pkt->skb) - pkt->skb->data;
 		break;
 	case NFT_PAYLOAD_NETWORK_HEADER:
-		offset = skb_network_offset(skb);
+		offset = skb_network_offset(pkt->skb);
 		break;
 	case NFT_PAYLOAD_TRANSPORT_HEADER:
 		offset = pkt->xt.thoff;
Quoted 17 lines

net/netfilter/nft_payload.c static void nft_payload_eval(const struct nft_expr *expr, · line 74

Quoted 13 lines
net/netfilter/nft_payload.c +13 -0
+static void nft_payload_set_eval(const struct nft_expr *expr,
+				 struct nft_data data[NFT_REG_MAX + 1],
+				 const struct nft_pktinfo *pkt)
+{
+	const struct nft_payload *priv = nft_expr_priv(expr);
+	struct nft_data *source = &data[priv->sreg];
+	int offset = nft_payload_make_offset(pkt, priv);
+
+	if (offset == -1)
+		goto err;
+	if (!skb_make_writable(pkt->skb, offset + priv->len))
+		goto err;
+	memcpy(pkt->skb->data + offset, source, priv->len);

We need to take care of checksumming. Shouldn't be too hard to get *most* cases right using the existing checksum helpers.

Yes, but would you like to do that in here or have a separate op which is configurable i.e. you can set what to calculate and where to put it, or would you prefer to make it automatic based on what we're changing here ?

Quoted 11 lines

net/netfilter/nft_payload.c static void nft_payload_eval(const struct nft_expr *expr, · line 80

Quoted 6 lines
net/netfilter/nft_payload.c +6 -0
+
+	return;
+err:
+	data[NFT_REG_VERDICT].verdict = NFT_BREAK;
+}
+

--
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

#3 lore raw
Quoted 12 lines · Nikolay Aleksandrov
Quoted 11 lines · Patrick McHardy

net/netfilter/nft_payload.c line 21

Quoted 6 lines
net/netfilter/nft_payload.c +2 -3
-static void nft_payload_eval(const struct nft_expr *expr,
-			     struct nft_data data[NFT_REG_MAX + 1],
-			     const struct nft_pktinfo *pkt)
+static int nft_payload_make_offset(const struct nft_pktinfo *pkt,
+				   const struct nft_payload *payload)

We're using priv everywhere. Please keep this consistent. Also please make sure (in the entire patch) that arguments are aligned with the opening (.

Quoted 4 lines

Okay, I'll use the priv.
They're adjusted, apply the patch and see for yourself, or do you mean some other adjustment ?
Both arguments are aligned from what I can see.

I see. Probably misrepresented by mutt.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
net/netfilter/nft_payload.c +8 -0
+static void nft_payload_set_eval(const struct nft_expr *expr,
+				 struct nft_data data[NFT_REG_MAX + 1],
+				 const struct nft_pktinfo *pkt)
+{
+	const struct nft_payload *priv = nft_expr_priv(expr);
+	struct nft_data *source = &data[priv->sreg];
+	int offset = nft_payload_make_offset(pkt, priv);
+
Quoted 9 lines

net/netfilter/nft_payload.c static void nft_payload_eval(const struct nft_expr *expr, · line 74

Quoted 5 lines
net/netfilter/nft_payload.c +5 -0
+	if (offset == -1)
+		goto err;
+	if (!skb_make_writable(pkt->skb, offset + priv->len))
+		goto err;
+	memcpy(pkt->skb->data + offset, source, priv->len);

We need to take care of checksumming. Shouldn't be too hard to get *most* cases right using the existing checksum helpers.

Quoted 3 lines

Yes, but would you like to do that in here or have a separate op which is configurable i.e. you can set what to calculate and where to put it, or would you prefer to make it automatic based on what we're changing here ?

Something here is a lot cheaper since we can use incremental checksumming. That won't be possible somewhere else, additionally we'd have to check the checksum before recalculating it to make sure we don't fix up bad packets. So yes, I think it should be done here.

#4 lore raw
Quoted 37 lines · Patrick McHardy
Quoted 12 lines · Nikolay Aleksandrov
Quoted 11 lines · Patrick McHardy

net/netfilter/nft_payload.c line 21

Quoted 6 lines
net/netfilter/nft_payload.c +2 -3
-static void nft_payload_eval(const struct nft_expr *expr,
-			     struct nft_data data[NFT_REG_MAX + 1],
-			     const struct nft_pktinfo *pkt)
+static int nft_payload_make_offset(const struct nft_pktinfo *pkt,
+				   const struct nft_payload *payload)

We're using priv everywhere. Please keep this consistent. Also please make sure (in the entire patch) that arguments are aligned with the opening (.

Quoted 4 lines

Okay, I'll use the priv.
They're adjusted, apply the patch and see for yourself, or do you mean some other adjustment ?
Both arguments are aligned from what I can see.

I see. Probably misrepresented by mutt.

Quoted 17 lines
Quoted 17 lines

net/netfilter/nft_payload.c static void nft_payload_eval(const struct nft_expr *expr, · line 74

Quoted 13 lines
net/netfilter/nft_payload.c +13 -0
+static void nft_payload_set_eval(const struct nft_expr *expr,
+				 struct nft_data data[NFT_REG_MAX + 1],
+				 const struct nft_pktinfo *pkt)
+{
+	const struct nft_payload *priv = nft_expr_priv(expr);
+	struct nft_data *source = &data[priv->sreg];
+	int offset = nft_payload_make_offset(pkt, priv);
+
+	if (offset == -1)
+		goto err;
+	if (!skb_make_writable(pkt->skb, offset + priv->len))
+		goto err;
+	memcpy(pkt->skb->data + offset, source, priv->len);

We need to take care of checksumming. Shouldn't be too hard to get *most* cases right using the existing checksum helpers.

Quoted 9 lines
Quoted 3 lines

Yes, but would you like to do that in here or have a separate op which is configurable i.e. you can set what to calculate and where to put it, or would you prefer to make it automatic based on what we're changing here ?

Something here is a lot cheaper since we can use incremental checksumming. That won't be possible somewhere else, additionally we'd have to check the checksum before recalculating it to make sure we don't fix up bad packets. So yes, I think it should be done here.

Okay, I've been working on this today and have gotten to a point where it works :) But I have a few questions, currently I've made it so the header that you modify is the only one that gets its checksum updated (with the exception of pseudo header if the address gets written to), I recompute the changed bytes one at a time because of the freedom of offset and length. Now this works fine and I can mangle the ip/ip6 headers or the transport headers (tcp/udp, I'll look into adding sctp as well), but there's a problem with the cross-header writing i.e. if a write spills from the network header to the transport header, currently I only update the network header part (correctly, only up to the bytes that were changed inside) and leave the transport header broken.
This also applies to the LL header, if a write spills into the network header then we'll have a broken packet.

Would you like me to add additional logic to support the cross-header writes or leave it as it is ?

Nik

#5 lore raw
Quoted 27 lines · Nikolay Aleksandrov
Quoted 4 lines · Patrick McHardy
Quoted 4 lines
Quoted 4 lines

We need to take care of checksumming. Shouldn't be too hard to get *most* cases right using the existing checksum helpers.

Quoted 9 lines
Quoted 3 lines

Yes, but would you like to do that in here or have a separate op which is configurable i.e. you can set what to calculate and where to put it, or would you prefer to make it automatic based on what we're changing here ?

Something here is a lot cheaper since we can use incremental checksumming. That won't be possible somewhere else, additionally we'd have to check the checksum before recalculating it to make sure we don't fix up bad packets. So yes, I think it should be done here.

Okay, I've been working on this today and have gotten to a point where it works :) But I have a few questions, currently I've made it so the header that you modify is the only one that gets its checksum updated (with the exception of pseudo header if the address gets written to), I recompute the changed bytes one at a time because of the freedom of offset and length. Now this works fine and I can mangle the ip/ip6 headers or the transport headers (tcp/udp, I'll look into adding sctp as well), but there's a problem with the cross-header writing i.e. if a write spills from the network header to the transport header, currently I only update the network header part (correctly, only up to the bytes that were changed inside) and leave the transport header broken.
This also applies to the LL header, if a write spills into the network header then we'll have a broken packet.

Quoted 2 lines

Would you like me to add additional logic to support the cross-header writes or leave it as it is ?

I think that should be fine.

Regarding the one byte at a time - update, how about rounding down to the next multiple of four, adjusting the replacement data accordingly and using csum_replace4() instead?

#6 lore raw
Quoted 28 lines · Patrick McHardy
Quoted 27 lines · Nikolay Aleksandrov
Quoted 4 lines · Patrick McHardy
Quoted 4 lines
Quoted 4 lines

We need to take care of checksumming. Shouldn't be too hard to get *most* cases right using the existing checksum helpers.

Quoted 9 lines
Quoted 3 lines

Yes, but would you like to do that in here or have a separate op which is configurable i.e. you can set what to calculate and where to put it, or would you prefer to make it automatic based on what we're changing here ?

Something here is a lot cheaper since we can use incremental checksumming. That won't be possible somewhere else, additionally we'd have to check the checksum before recalculating it to make sure we don't fix up bad packets. So yes, I think it should be done here.

Okay, I've been working on this today and have gotten to a point where it works :) But I have a few questions, currently I've made it so the header that you modify is the only one that gets its checksum updated (with the exception of pseudo header if the address gets written to), I recompute the changed bytes one at a time because of the freedom of offset and length. Now this works fine and I can mangle the ip/ip6 headers or the transport headers (tcp/udp, I'll look into adding sctp as well), but there's a problem with the cross-header writing i.e. if a write spills from the network header to the transport header, currently I only update the network header part (correctly, only up to the bytes that were changed inside) and leave the transport header broken.
This also applies to the LL header, if a write spills into the network header then we'll have a broken packet.

Quoted 9 lines
Quoted 2 lines

Would you like me to add additional logic to support the cross-header writes or leave it as it is ?

I think that should be fine.

Regarding the one byte at a time - update, how about rounding down to the next multiple of four, adjusting the replacement data accordingly and using csum_replace4() instead?

I was hoping you wouldn't say that :-) (joking)
Yep, I know, I'll do it.

Thanks again