Skip to contentSkip to conversation

Add nf_flow_encap_push() for xmit direct

PATCH In review Eric Woudstra 3 participants 6 messages Apr 8, 2025 – Apr 9, 2025

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

v11 cover Add nf_flow_encap_push() for xmit direct

Patch to add nf_flow_encap_push(), see patch message.

Added patch to eliminate array of flexible structures warning.

Changed in v11:
- Only push when tuple.out.ifidx == tuple.out.hw_ifidx
- No changes in nft_dev_path_info()

v10 split from patch-set: bridge-fastpath and related improvements v9

Eric Woudstra (2):
net: pppoe: avoid zero-length arrays in struct pppoe_hdr
netfilter: nf_flow_table_offload: Add nf_flow_encap_push() for xmit
direct

 drivers/net/ppp/pppoe.c          |  2 +-
 include/uapi/linux/if_pppox.h    |  4 ++
 net/netfilter/nf_flow_table_ip.c | 97 +++++++++++++++++++++++++++++++-
 3 files changed, 100 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v11 1/2 net: pppoe: avoid zero-length arrays in struct pppoe_hdr

Jakub Kicinski suggested following patch:

W=1 C=1 GCC build gives us:

net/bridge/netfilter/nf_conntrack_bridge.c: note: in included file (through ../include/linux/if_pppox.h, ../include/uapi/linux/netfilter_bridge.h, ../include/linux/netfilter_bridge.h): include/uapi/linux/if_pppox.h:
153:29: warning: array of flexible structures

It doesn't like that hdr has a zero-length array which overlaps proto. The kernel code doesn't currently need those arrays.

PPPoE connection is functional after applying this patch.

---
 drivers/net/ppp/pppoe.c       | 2 +-
 include/uapi/linux/if_pppox.h | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/net/ppp/pppoe.c +1 -1
index 68e631718ab0..17946af6a8cf 100644
@@ -882,7 +882,7 @@ static int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
 
 	ph = skb_put(skb, total_len + sizeof(struct pppoe_hdr));
-	start = (char *)&ph->tag[0];
+	start = (char *)ph + sizeof(*ph);
 
 	error = memcpy_from_msg(start, m, total_len);
 	if (error < 0) {
include/uapi/linux/if_pppox.h +4 -0
index 9abd80dcc46f..29b804aa7474 100644
@@ -122,7 +122,9 @@ struct sockaddr_pppol2tpv3in6 {
 struct pppoe_tag {
 	__be16 tag_type;
 	__be16 tag_len;
+#ifndef __KERNEL__
 	char tag_data[];
+#endif
 } __attribute__ ((packed));
 
 /* Tag identifiers */
@@ -150,7 +152,9 @@ struct pppoe_hdr {
 	__u8 code;
 	__be16 sid;
 	__be16 length;
+#ifndef __KERNEL__
 	struct pppoe_tag tag[];
+#endif
 } __packed;
 
 /* Length of entire PPPoE + PPP header */
Signature · 1 line
2.47.1
Reviewed-by on v11 1/2
#2 lore raw

Re: [PATCH v11 nf-next 1/2] net: pppoe: avoid zero-length arrays in struct pppoe_hdr

Quoted 9 lines · Eric Woudstra

Jakub Kicinski suggested following patch:

W=1 C=1 GCC build gives us:

net/bridge/netfilter/nf_conntrack_bridge.c: note: in included file (through ../include/linux/if_pppox.h, ../include/uapi/linux/netfilter_bridge.h, ../include/linux/netfilter_bridge.h): include/uapi/linux/if_pppox.h:
153:29: warning: array of flexible structures

Quoted 7 lines

It doesn't like that hdr has a zero-length array which overlaps proto. The kernel code doesn't currently need those arrays.

PPPoE connection is functional after applying this patch.

Signature · 1 line
Kees Cook
#3 lore raw

PATCH v11 2/2 netfilter: nf_flow_table_offload: Add nf_flow_encap_push() for xmit direct

In reply to Eric Woudstra

Loosely based on wenxu's patches:

"nf_flow_table_offload: offload the vlan/PPPoE encap in the flowtable".

Fixed double vlan and pppoe packets, almost entirely rewriting the patch.

When there is no extra vlan-device or pppoe-device added to the fastpath, it may still be possible that the other tuple has encaps.

This is the case when there is only a bridge in the forward-fastpath, without a vlan-device. When the bridge is tagging at ingress and keeping at egress, the other tuple will have an encap.

It will be also be the case in the future bridge-fastpath.

In these cases it is necessary to push these encaps.

This patch adds nf_flow_encap_push() and alters nf_flow_queue_xmit() to call it, only when (tuple.out.ifidx == tuple.out.hw_ifidx).

---
 net/netfilter/nf_flow_table_ip.c | 97 +++++++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 2 deletions(-)
net/netfilter/nf_flow_table_ip.c +95 -2
index 8cd4cf7ae211..64a12b9668e7 100644
@@ -306,6 +306,92 @@ static bool nf_flow_skb_encap_protocol(struct sk_buff *skb, __be16 proto,
 	return false;
 }
 
+static int nf_flow_vlan_inner_push(struct sk_buff *skb, __be16 proto, u16 id)
+{
+	struct vlan_hdr *vhdr;
+
+	if (skb_cow_head(skb, VLAN_HLEN))
+		return -1;
+
+	__skb_push(skb, VLAN_HLEN);
+	skb_reset_network_header(skb);
+
+	vhdr = (struct vlan_hdr *)(skb->data);
+	vhdr->h_vlan_TCI = htons(id);
+	vhdr->h_vlan_encapsulated_proto = skb->protocol;
+	skb->protocol = proto;
+
+	return 0;
+}
+
+static int nf_flow_ppoe_push(struct sk_buff *skb, u16 id)
+{
+	struct ppp_hdr {
+		struct pppoe_hdr hdr;
+		__be16 proto;
+	} *ph;
+	int data_len = skb->len + 2;
+	__be16 proto;
+
+	if (skb_cow_head(skb, PPPOE_SES_HLEN))
+		return -1;
+
+	if (skb->protocol == htons(ETH_P_IP))
+		proto = htons(PPP_IP);
+	else if (skb->protocol == htons(ETH_P_IPV6))
+		proto = htons(PPP_IPV6);
+	else
+		return -1;
+
+	__skb_push(skb, PPPOE_SES_HLEN);
+	skb_reset_network_header(skb);
+
+	ph = (struct ppp_hdr *)(skb->data);
+	ph->hdr.ver  = 1;
+	ph->hdr.type = 1;
+	ph->hdr.code = 0;
+	ph->hdr.sid  = htons(id);
+	ph->hdr.length = htons(data_len);
+	ph->proto = proto;
+	skb->protocol = htons(ETH_P_PPP_SES);
+
+	return 0;
+}
+
+static int nf_flow_encap_push(struct sk_buff *skb,
+			      struct flow_offload_tuple_rhash *tuplehash,
+			      unsigned short *type)
+{
+	int i = 0, ret = 0;
+
+	if (!tuplehash->tuple.encap_num)
+		return 0;
+
+	if (tuplehash->tuple.encap[i].proto == htons(ETH_P_8021Q) ||
+	    tuplehash->tuple.encap[i].proto == htons(ETH_P_8021AD)) {
+		__vlan_hwaccel_put_tag(skb, tuplehash->tuple.encap[i].proto,
+				       tuplehash->tuple.encap[i].id);
+		i++;
+		if (i >= tuplehash->tuple.encap_num)
+			return 0;
+	}
+
+	switch (tuplehash->tuple.encap[i].proto) {
+	case htons(ETH_P_8021Q):
+		*type = ETH_P_8021Q;
+		ret = nf_flow_vlan_inner_push(skb,
+					      tuplehash->tuple.encap[i].proto,
+					      tuplehash->tuple.encap[i].id);
+		break;
+	case htons(ETH_P_PPP_SES):
+		*type = ETH_P_PPP_SES;
+		ret = nf_flow_ppoe_push(skb,
+					tuplehash->tuple.encap[i].id);
+		break;
+	}
+	return ret;
+}
+
 static void nf_flow_encap_pop(struct sk_buff *skb,
 			      struct flow_offload_tuple_rhash *tuplehash)
 {
@@ -335,6 +421,7 @@ static void nf_flow_encap_pop(struct sk_buff *skb,
 
 static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
 				       const struct flow_offload_tuple_rhash *tuplehash,
+				       struct flow_offload_tuple_rhash *other_tuplehash,
 				       unsigned short type)
 {
 	struct net_device *outdev;
@@ -343,6 +430,10 @@ static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
 	if (!outdev)
 		return NF_DROP;
 
+	if (tuplehash->tuple.out.ifidx == tuplehash->tuple.out.hw_ifidx &&
+	    (nf_flow_encap_push(skb, other_tuplehash, &type) < 0))
+		return NF_DROP;
+
 	skb->dev = outdev;
 	dev_hard_header(skb, skb->dev, type, tuplehash->tuple.out.h_dest,
 			tuplehash->tuple.out.h_source, skb->len);
@@ -462,7 +553,8 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
 		ret = NF_STOLEN;
 		break;
 	case FLOW_OFFLOAD_XMIT_DIRECT:
-		ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IP);
+		ret = nf_flow_queue_xmit(state->net, skb, tuplehash,
+					 &flow->tuplehash[!dir], ETH_P_IP);
 		if (ret == NF_DROP)
 			flow_offload_teardown(flow);
 		break;
@@ -757,7 +849,8 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
 		ret = NF_STOLEN;
 		break;
 	case FLOW_OFFLOAD_XMIT_DIRECT:
-		ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IPV6);
+		ret = nf_flow_queue_xmit(state->net, skb, tuplehash,
+					 &flow->tuplehash[!dir], ETH_P_IPV6);
 		if (ret == NF_DROP)
 			flow_offload_teardown(flow);
 		break;
Signature · 1 line
2.47.1
#4 lore raw
In reply to Eric Woudstra

Hi,

Please, one series at a time. You pick a good target to start with.

I already provided a few suggestions on where to start from.

Thanks.

Quoted 23 lines · Eric Woudstra

Patch to add nf_flow_encap_push(), see patch message.

Added patch to eliminate array of flexible structures warning.

Changed in v11:
- Only push when tuple.out.ifidx == tuple.out.hw_ifidx
- No changes in nft_dev_path_info()

v10 split from patch-set: bridge-fastpath and related improvements v9

Eric Woudstra (2):
net: pppoe: avoid zero-length arrays in struct pppoe_hdr
netfilter: nf_flow_table_offload: Add nf_flow_encap_push() for xmit
direct

 drivers/net/ppp/pppoe.c          |  2 +-
 include/uapi/linux/if_pppox.h    |  4 ++
 net/netfilter/nf_flow_table_ip.c | 97 +++++++++++++++++++++++++++++++-
 3 files changed, 100 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.1
#5 lore raw
Quoted 6 lines · Pablo Neira Ayuso

Hi,

Please, one series at a time. You pick a good target to start with.

I already provided a few suggestions on where to start from.

Ok, I view the patch-set 'conntrack: bridge: add double vlan, pppoe and pppoe-in-q' more as an added bonus to the bridge-fastpath. It is quite separate from the rest. I would consider this patch-set as low priority.

May I suggest we focus on patch-sets:

'Add nf_flow_encap_push() for xmit direct'

and

'netfilter: fastpath fixes'

After these issues are addressed, only then it makes sense to add
patch-set: 'netfilter: Add bridge-fastpath'.

Thanks