๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Sebastian Andrzej Siewior Wed, 25 Nov 2026 14:31
[PATCH net-next v12 3/8] hsr: Add a magic header for sending PTP packets
#22557
Author: Sebastian Andrzej Siewior
Date: Wed, 25 Nov 2026 14:31
247 lines
17535 bytes
Sending PTP packets (ETH_P_1588) via the HSR stack is pointless in its
current shape because the requested PTP timestamp is not routed to the
sender. It also needs to be distinguished on which port the message
should be sent and whether or not a HSR header should be attached.

To pass this information, a custom header (struct hsr_inline_header) is
introduced. This header is expected and mandatory if the ether type is
ETH_P_1588. To avoid any wrong usage, there is a magic field to ensure
it really is the header.

The inline header is just prepended containing the port and header
parameter. After the header, a regular packet follows. It is ensured that
the inline header and the following ethernet or HSR header is linear
and can be accessed. Given that this information is important for how
PTP is used, PTP packets missing this information will be dropped.

The two retrieved parameters are passed to hsr_forward_skb() and will be
used later.

Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 MAINTAINERS                  |  1 +
 include/uapi/linux/hsr_ptp.h | 20 +++++++++++
 net/hsr/hsr_device.c         | 83 +++++++++++++++++++++++++++++++++++++-------
 net/hsr/hsr_forward.c        |  3 +-
 net/hsr/hsr_forward.h        |  3 +-
 net/hsr/hsr_slave.c          |  4 +--
 6 files changed, 97 insertions(+), 17 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b80..f41ce40c9e306 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12307,6 +12307,7 @@ F:	drivers/net/usb/hso.c
 HSR NETWORK PROTOCOL
 L:	netdev@vger.kernel.org
 S:	Orphan
+F:	include/uapi/linux/hsr_ptp.h
 F:	net/hsr/
 F:	tools/testing/selftests/net/hsr/
 
diff --git a/include/uapi/linux/hsr_ptp.h b/include/uapi/linux/hsr_ptp.h
new file mode 100644
index 0000000000000..95e905441ebdc
--- /dev/null
+++ b/include/uapi/linux/hsr_ptp.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
+#ifndef __UAPI_HSR_PTP_H
+#define __UAPI_HSR_PTP_H
+
+#include <linux/types.h>
+
+#define HSR_INLINE_HDR  0xaf485352
+#define HSR_INLINE_HDR_PORT_A	1
+#define HSR_INLINE_HDR_PORT_B	2
+
+struct hsr_inline_header {
+	__u8 req_tx_port;
+	__u8 has_hsr_header;
+	__be32 __pad0;
+	__be32 magic;
+	__be16 __pad1;
+	__be16 eth_type;
+} __attribute__ ((packed));
+
+#endif
diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c
index 59df647d2fd45..7e79d5b34e8bb 100644
--- a/net/hsr/hsr_device.c
+++ b/net/hsr/hsr_device.c
@@ -12,6 +12,8 @@
 #include <linux/etherdevice.h>
 #include <linux/rtnetlink.h>
 #include <linux/pkt_sched.h>
+#include <uapi/linux/hsr_ptp.h>
+
 #include "hsr_device.h"
 #include "hsr_slave.h"
 #include "hsr_framereg.h"
@@ -221,26 +223,81 @@ static netdev_features_t hsr_fix_features(struct net_device *dev,
 	return hsr_features_recompute(hsr, features);
 }
 
+static int hsr_ptp_parse_header(struct sk_buff *skb, bool *has_hsr_header,
+				enum hsr_port_type *req_tx_port)
+{
+	struct hsr_inline_header *hsr_inline_hdr;
+	struct ethhdr *eth_hdr;
+	unsigned int hdr_len;
+
+	BUILD_BUG_ON(sizeof(struct hsr_inline_header) != sizeof(struct ethhdr));
+
+	skb_reset_mac_header(skb);
+	if (!skb->sk)
+		return -EINVAL;
+
+	/* need to access the magic header */
+	if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header)))
+		return -EINVAL;
+
+	hsr_inline_hdr = (struct hsr_inline_header *)skb_mac_header(skb);
+	if (hsr_inline_hdr->magic != htonl(HSR_INLINE_HDR))
+		return -EINVAL;
+
+	*has_hsr_header = hsr_inline_hdr->has_hsr_header;
+	*req_tx_port = hsr_inline_hdr->req_tx_port;
+	if (*req_tx_port != HSR_PT_SLAVE_A && *req_tx_port != HSR_PT_SLAVE_B)
+		return -EINVAL;
+
+	if (hsr_inline_hdr->__pad0 != 0 || hsr_inline_hdr->__pad1 != 0)
+		return -EINVAL;
+
+	hdr_len = ETH_HLEN;
+	if (*has_hsr_header)
+		hdr_len += HSR_HLEN;
+
+	/* Ensure the real header after the stripped one can be
+	 * accessed
+	 */
+	if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header) +
+			   hdr_len))
+		return -EINVAL;
+
+	eth_hdr = skb_pull(skb, sizeof(struct hsr_inline_header));
+	skb_set_network_header(skb, hdr_len);
+	skb->protocol = eth_hdr->h_proto;
+	return 0;
+}
+
 static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
 {
+	enum hsr_port_type req_tx_port = HSR_PT_NONE;
 	struct hsr_priv *hsr = netdev_priv(dev);
+	bool has_hsr_header = false;
 	struct hsr_port *master;
 
 	rcu_read_lock();
 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
-	if (master) {
-		skb->dev = master->dev;
-		skb_reset_mac_header(skb);
-		skb_reset_mac_len(skb);
-		spin_lock_bh(&hsr->seqnr_lock);
-		hsr_forward_skb(skb, master);
-		spin_unlock_bh(&hsr->seqnr_lock);
-	} else {
-		dev_core_stats_tx_dropped_inc(dev);
-		dev_kfree_skb_any(skb);
-	}
+	if (!master)
+		goto drop;
+
+	skb->dev = master->dev;
+	if (skb->protocol == htons(ETH_P_1588) &&
+	    hsr_ptp_parse_header(skb, &has_hsr_header, &req_tx_port))
+		goto drop;
+
+	skb_reset_mac_header(skb);
+	skb_reset_mac_len(skb);
+	spin_lock_bh(&hsr->seqnr_lock);
+	hsr_forward_skb(skb, master, req_tx_port, has_hsr_header);
+	spin_unlock_bh(&hsr->seqnr_lock);
 	rcu_read_unlock();
 
+	return NETDEV_TX_OK;
+drop:
+	rcu_read_unlock();
+	dev_core_stats_tx_dropped_inc(dev);
+	dev_kfree_skb_any(skb);
 	return NETDEV_TX_OK;
 }
 
@@ -362,7 +419,7 @@ static void send_hsr_supervision_frame(struct hsr_port *port,
 		return;
 	}
 
-	hsr_forward_skb(skb, port);
+	hsr_forward_skb(skb, port, HSR_PT_NONE, false);
 	spin_unlock_bh(&hsr->seqnr_lock);
 	return;
 }
@@ -430,7 +487,7 @@ static void send_prp_supervision_frame(struct hsr_port *master,
 		return;
 	}
 
-	hsr_forward_skb(skb, master);
+	hsr_forward_skb(skb, master, HSR_PT_NONE, false);
 	spin_unlock_bh(&hsr->seqnr_lock);
 }
 
diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
index b183358207506..f49d962337f8b 100644
--- a/net/hsr/hsr_forward.c
+++ b/net/hsr/hsr_forward.c
@@ -771,7 +771,8 @@ static int fill_frame_info(struct hsr_frame_info *frame,
 }
 
 /* Must be called holding rcu read lock (because of the port parameter) */
-void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port)
+void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port,
+		     enum hsr_port_type req_tx_port, bool has_hsr_header)
 {
 	struct hsr_frame_info frame;
 
diff --git a/net/hsr/hsr_forward.h b/net/hsr/hsr_forward.h
index 206636750b300..662dd369c7df2 100644
--- a/net/hsr/hsr_forward.h
+++ b/net/hsr/hsr_forward.h
@@ -13,7 +13,8 @@
 #include <linux/netdevice.h>
 #include "hsr_main.h"
 
-void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port);
+void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port,
+		     enum hsr_port_type req_tx_port, bool has_hsr_header);
 struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,
 					struct hsr_port *port);
 struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
diff --git a/net/hsr/hsr_slave.c b/net/hsr/hsr_slave.c
index a546f70f9cc8b..5274ba6dd36e6 100644
--- a/net/hsr/hsr_slave.c
+++ b/net/hsr/hsr_slave.c
@@ -78,10 +78,10 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
 	 */
 	if (port->type == HSR_PT_INTERLINK) {
 		spin_lock_bh(&hsr->seqnr_lock);
-		hsr_forward_skb(skb, port);
+		hsr_forward_skb(skb, port, HSR_PT_NONE, false);
 		spin_unlock_bh(&hsr->seqnr_lock);
 	} else {
-		hsr_forward_skb(skb, port);
+		hsr_forward_skb(skb, port, HSR_PT_NONE, false);
 	}
 
 finish_consume:

-- 
2.55.0
Re: [PATCH net-next v12 3/8] hsr: Add a magic header for sending PTP packets
#23106
Author: netdev-bot+sashiko
Date: Thu, 26 Nov 2026 14:40
301 lines
18805 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] In hsr_ptp_parse_header() (net/hsr/hsr_device.c), the req_tx_port
  byte from userspace is stored straight into an enum hsr_port_type.
- [Low] hsr_ptp_parse_header() does not strictly validate the new uAPI
  header, even though it does require __pad0 and __pad1 to be zero.
- [Low] Not confirmed by baseline review; kept because this concern needs
  separate validation.
- [Low] Not confirmed by baseline review; kept because this concern needs
  separate validation.
- [Low] Not confirmed by baseline review; kept because this concern needs
  separate validation.
- [Low] Not confirmed by baseline review; kept because this concern needs
  separate validation.

--

hsr: Add a magic header for sending PTP packets

This patch adds a uAPI struct hsr_inline_header. PTP (ETH_P_1588) frames
sent through an HSR/PRP master device must now start with it. The header
carries the requested transmit port and whether the following frame
already has an HSR tag. hsr_dev_xmit() parses and strips the header,
drops PTP frames that lack it, and passes the two values on to
hsr_forward_skb() for later patches in the series to use.

> diff --git a/include/uapi/linux/hsr_ptp.h b/include/uapi/linux/hsr_ptp.h
> new file mode 100644
> index 0000000000000..95e905441ebdc
> --- /dev/null
> +++ b/include/uapi/linux/hsr_ptp.h
> @@ -0,0 +1,20 @@

[ ... ]

> +#define HSR_INLINE_HDR  0xaf485352
> +#define HSR_INLINE_HDR_PORT_A	1
> +#define HSR_INLINE_HDR_PORT_B	2
> +
> +struct hsr_inline_header {
> +	__u8 req_tx_port;
> +	__u8 has_hsr_header;
> +	__be32 __pad0;
> +	__be32 magic;
> +	__be16 __pad1;
> +	__be16 eth_type;
> +} __attribute__ ((packed));
> +
> +#endif
> diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c
> index 59df647d2fd45..7e79d5b34e8bb 100644
> --- a/net/hsr/hsr_device.c
> +++ b/net/hsr/hsr_device.c

[ ... ]

> @@ -221,26 +223,81 @@ static netdev_features_t hsr_fix_features(struct net_device *dev,
>  	return hsr_features_recompute(hsr, features);
>  }
>  
> +static int hsr_ptp_parse_header(struct sk_buff *skb, bool *has_hsr_header,
> +				enum hsr_port_type *req_tx_port)
> +{
> +	struct hsr_inline_header *hsr_inline_hdr;
> +	struct ethhdr *eth_hdr;
> +	unsigned int hdr_len;
> +
> +	BUILD_BUG_ON(sizeof(struct hsr_inline_header) != sizeof(struct ethhdr));
> +
> +	skb_reset_mac_header(skb);
> +	if (!skb->sk)
> +		return -EINVAL;

[Severity: Low]
Is this socket check meant as an extra policy on top of the magic check?
PTP frames that reach hsr_dev_xmit() without a socket are dropped here.
That happens, for example, when hsr0 is a bridge port or a mirred target.

Those frames would not carry the HSR_INLINE_HDR magic either, so the
magic check below would drop them anyway. The commit message already
says "PTP packets missing this information will be dropped".

The socket requirement also seems to match "hsr: Assign a socket for
cloned skbs" later in the series, which needs skb->sk to route the
timestamp back.

Could a short comment here say why skb->sk is required?

> +
> +	/* need to access the magic header */
> +	if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header)))
> +		return -EINVAL;
> +
> +	hsr_inline_hdr = (struct hsr_inline_header *)skb_mac_header(skb);
> +	if (hsr_inline_hdr->magic != htonl(HSR_INLINE_HDR))
> +		return -EINVAL;
> +
> +	*has_hsr_header = hsr_inline_hdr->has_hsr_header;
> +	*req_tx_port = hsr_inline_hdr->req_tx_port;
> +	if (*req_tx_port != HSR_PT_SLAVE_A && *req_tx_port != HSR_PT_SLAVE_B)
> +		return -EINVAL;

[Severity: Low]
Should this check use the uAPI constants rather than the internal enum?

The byte from userspace goes straight into an enum hsr_port_type. It is
then compared against HSR_PT_SLAVE_A and HSR_PT_SLAVE_B from
include/linux/if_hsr.h.

HSR_INLINE_HDR_PORT_A and HSR_INLINE_HDR_PORT_B, exported in
include/uapi/linux/hsr_ptp.h, are not used by any kernel code. Only the
selftest and the tools/ copy of the header refer to them.

The two only agree because of the current enum layout:

include/linux/if_hsr.h:
enum hsr_port_type {
	HSR_PT_NONE = 0,	/* Must be 0, used by framereg */
	HSR_PT_SLAVE_A,
	HSR_PT_SLAVE_B,
	...

Later in the series, hsr_forward_do() also compares frame->req_tx_port
directly against port->type. If enum hsr_port_type is ever reordered or
extended, the meaning of the exported values would change silently.

There are two ways to make the coupling visible. One is an explicit
translation from HSR_INLINE_HDR_PORT_A/B. The other is
BUILD_BUG_ON(HSR_INLINE_HDR_PORT_A != HSR_PT_SLAVE_A) and the same for B.
Would either of these work here?

> +
> +	if (hsr_inline_hdr->__pad0 != 0 || hsr_inline_hdr->__pad1 != 0)
> +		return -EINVAL;
> +
> +	hdr_len = ETH_HLEN;
> +	if (*has_hsr_header)
> +		hdr_len += HSR_HLEN;

[Severity: Low]
How strictly should the new uAPI header be validated here? __pad0 and
__pad1 must be zero, but the other fields are looser.

This assignment turns any value from 1 to 255 into true:

	*has_hsr_header = hsr_inline_hdr->has_hsr_header;

Once this ships, can those values ever get another meaning, such as
future flags? Would it be better to reject anything other than 0 or 1?

eth_type is never checked. It only matters indirectly, when AF_PACKET
sends with protocol 0 and the hsr parse_protocol callback reads offset
12. If sll_protocol is set to ETH_P_1588 explicitly, any eth_type is
accepted. Is eth_type meant to be validated?

has_hsr_header is also accepted on PRP devices, which have no front HSR
tag (PRP uses a trailer). HSR_HLEN is still added to hdr_len, so
skb->network_header and skb->mac_len (20) do not match the real frame
layout.

I followed this to the end of the series and it does not appear to
crash. prp_register_frame_out() returns 0 when port_rcv is the master,
and the skb_std path is injected directly. Even so, should
has_hsr_header be rejected for PRP?

None of these semantics are documented in include/uapi/linux/hsr_ptp.h.

> +
> +	/* Ensure the real header after the stripped one can be
> +	 * accessed
> +	 */
> +	if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header) +
> +			   hdr_len))
> +		return -EINVAL;
> +
> +	eth_hdr = skb_pull(skb, sizeof(struct hsr_inline_header));
> +	skb_set_network_header(skb, hdr_len);
> +	skb->protocol = eth_hdr->h_proto;
> +	return 0;
> +}
> +
>  static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
>  {

[ ... ]

> +	skb->dev = master->dev;
> +	if (skb->protocol == htons(ETH_P_1588) &&
> +	    hsr_ptp_parse_header(skb, &has_hsr_header, &req_tx_port))
> +		goto drop;

[Severity: Low]
Is the mandatory inline header meant to depend on skb->protocol rather
than on the ethertype in the frame?

Here skb->protocol comes from the AF_PACKET sll_protocol (or po->num). A
PTP frame sent with a different sll_protocol therefore skips
hsr_ptp_parse_header() and is forwarded to both ports, as it was before
this patch.

The receive side in hsr_handle_frame() classifies by the frame's
ethertype instead:

		if (hsr_ethhdr->hsr_tag.encap_proto == htons(ETH_P_1588))
			goto finish_free_consume;

This looks deliberate, given "hsr: Add header_ops::parse_protocol"
earlier in the series. The sender also needs CAP_NET_RAW, and the result
is only the behaviour from before this patch.

Could the commit message wording "if the ether type is ETH_P_1588" say
that it refers to skb->protocol?

> +
> +	skb_reset_mac_header(skb);
> +	skb_reset_mac_len(skb);

[Severity: Low]
At this commit, can a sender with has_hsr_header set put a sequence
number of its choosing into the duplicate-discard state?

has_hsr_header is trusted without checking that a real HSR tag follows.
skb_reset_mac_len() here turns hdr_len into skb->mac_len == 20.

A user-crafted ETH_P_HSR frame from the master port then passes this
check in hsr_fill_frame_info():

		if (skb->mac_len < sizeof(struct hsr_ethhdr))
			return -EINVAL;
		...
		frame->skb_hsr = skb;
		frame->sequence_nr = hsr_get_skb_sequence_nr(skb);

The frame then goes through hsr_get_node(), hsr_register_frame_in() and
hsr_register_frame_out()/hsr_check_duplicate(), possibly for a spoofed
node.

This seems limited to this intermediate commit. After "hsr: Use the port
and header information in hsr_forward_skb()", a frame with
has_hsr_header set is handled differently:

- fill_frame_info() skips hsr_get_node().
- hsr_fill_frame_info() returns early with skb_std = skb, before it
  reads the sequence number.
- hsr_forward_skb() skips hsr_register_frame_in().
- hsr_forward_do() sends the frame as a plain clone through
  inject_into_stack.

What remains at that point is a CAP_NET_RAW sender choosing the bytes
that go out of a chosen slave port. It could already do that on the
slave device directly.

Is it worth reordering the series so a bisect does not land on this
state?

> +	spin_lock_bh(&hsr->seqnr_lock);
> +	hsr_forward_skb(skb, master, req_tx_port, has_hsr_header);
> +	spin_unlock_bh(&hsr->seqnr_lock);

[ ... ]

> diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
> index b183358207506..f49d962337f8b 100644
> --- a/net/hsr/hsr_forward.c
> +++ b/net/hsr/hsr_forward.c
> @@ -771,7 +771,8 @@ static int fill_frame_info(struct hsr_frame_info *frame,
>  }
>  
>  /* Must be called holding rcu read lock (because of the port parameter) */
> -void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port)
> +void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port,
> +		     enum hsr_port_type req_tx_port, bool has_hsr_header)
>  {
>  	struct hsr_frame_info frame;
>  

[Severity: Low]
At this commit, req_tx_port and has_hsr_header are accepted here but not
used. The body still calls:

	if (fill_frame_info(&frame, skb, port) < 0)
		goto out_drop;

So hsr_dev_xmit() parses and validates the new uAPI fields, but they
have no effect until "hsr: Use the port and header information in
hsr_forward_skb()" later in the series. In that patch, fill_frame_info()
stores them, and hsr_forward_do() skips ports that do not match
req_tx_port. Frames with has_hsr_header set also skip
hsr_register_frame_in() and re-tagging there.

The commit message does say the values "will be used later". Would it be
easier for bisection to add the uAPI and the forwarding changes in the
same patch?

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125-hsr_ptp-v12-0-9ddd2a73cb90%40linutronix.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads