Skip to contentSkip to conversation

net/packet: Fix a comment about hard_header_len and add a warning for it

PATCH In review Xie He 2 participants 5 messages Sep 11, 2020 – Sep 13, 2020

Subsystems (3)
lore
Share & tools
lore

Xie He

author
#0 lore raw

PATCH 1/1 net/packet: Fix a comment about hard_header_len and add a warning for it

This patch tries to clarify the difference between hard_header_len and needed_headroom by fixing an outdated comment and adding a WARN_ON_ONCE warning for hard_header_len.

The difference between hard_header_len and needed_headroom as understood by this patch is based on the following reasons:

1.

In af_packet.c, the function packet_snd first reserves a headroom of length (dev->hard_header_len + dev->needed_headroom).
Then if the socket is a SOCK_DGRAM socket, it calls dev_hard_header, which calls dev->header_ops->create, to create the link layer header. If the socket is a SOCK_RAW socket, it "un-reserves" a headroom of length (dev->hard_header_len), and checks if the user has provided a header sized between (dev->min_header_len) and (dev->hard_header_len) (in dev_validate_header).
This shows the developers of af_packet.c expect hard_header_len to be consistent with header_ops.

2.

In af_packet.c, the function packet_sendmsg_spkt has a FIXME comment. That comment states that prepending an LL header internally in a driver is considered a bug. I believe this bug can be fixed by setting hard_header_len to 0, making the internal header completely invisible to af_packet.c (and requesting the headroom in needed_headroom instead).

3.

There is a commit for a WiFi driver:
commit 9454f7a895b8 ("mwifiex: set needed_headroom, not hard_header_len") According to the discussion about it at:
https://patchwork.kernel.org/patch/11407493/
The author tried to set the WiFi driver's hard_header_len to the Ethernet header length, and request additional header space internally needed by setting needed_headroom.
This means this usage is already adopted by driver developers.

Cc Willem de Bruijn <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Brian Norris <[email protected]>
Cc Cong Wang <[email protected]>
---
 net/packet/af_packet.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/packet/af_packet.c +11 -6
index af6c93ed9fa0..93c89d3b2511 100644
@@ -93,12 +93,15 @@
 
 /*
    Assumptions:
-   - if device has no dev->hard_header routine, it adds and removes ll header
-     inside itself. In this case ll header is invisible outside of device,
-     but higher levels still should reserve dev->hard_header_len.
-     Some devices are enough clever to reallocate skb, when header
-     will not fit to reserved space (tunnel), another ones are silly
-     (PPP).
+   - If the device has no dev->header_ops, there is no LL header visible
+     above the device. In this case, its hard_header_len should be 0.
+     The device may prepend its own header internally. In this case, its
+     needed_headroom should be set to the space needed for it to add its
+     internal header.
+     For example, a WiFi driver pretending to be an Ethernet driver should
+     set its hard_header_len to be the Ethernet header length, and set its
+     needed_headroom to be (the real WiFi header length - the fake Ethernet
+     header length).
    - packet socket receives packets with pulled ll header,
      so that SOCK_RAW should push it back.
 
@@ -2936,6 +2939,8 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 	skb_reset_network_header(skb);
 
 	err = -EINVAL;
+	if (!dev->header_ops)
+		WARN_ON_ONCE(dev->hard_header_len != 0);
 	if (sock->type == SOCK_DGRAM) {
 		offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
 		if (unlikely(offset < 0))
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 76 lines · Xie He

This patch tries to clarify the difference between hard_header_len and needed_headroom by fixing an outdated comment and adding a WARN_ON_ONCE warning for hard_header_len.

The difference between hard_header_len and needed_headroom as understood by this patch is based on the following reasons:

1.

In af_packet.c, the function packet_snd first reserves a headroom of length (dev->hard_header_len + dev->needed_headroom).
Then if the socket is a SOCK_DGRAM socket, it calls dev_hard_header, which calls dev->header_ops->create, to create the link layer header. If the socket is a SOCK_RAW socket, it "un-reserves" a headroom of length (dev->hard_header_len), and checks if the user has provided a header sized between (dev->min_header_len) and (dev->hard_header_len) (in dev_validate_header).
This shows the developers of af_packet.c expect hard_header_len to be consistent with header_ops.

2.

In af_packet.c, the function packet_sendmsg_spkt has a FIXME comment. That comment states that prepending an LL header internally in a driver is considered a bug. I believe this bug can be fixed by setting hard_header_len to 0, making the internal header completely invisible to af_packet.c (and requesting the headroom in needed_headroom instead).

3.

There is a commit for a WiFi driver:
commit 9454f7a895b8 ("mwifiex: set needed_headroom, not hard_header_len") According to the discussion about it at:
https://patchwork.kernel.org/patch/11407493/
The author tried to set the WiFi driver's hard_header_len to the Ethernet header length, and request additional header space internally needed by setting needed_headroom.
This means this usage is already adopted by driver developers.

Cc Willem de Bruijn <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Brian Norris <[email protected]>
Cc Cong Wang <[email protected]>
---
 net/packet/af_packet.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/packet/af_packet.c +9 -6
index af6c93ed9fa0..93c89d3b2511 100644
@@ -93,12 +93,15 @@
 
 /*
    Assumptions:
-   - if device has no dev->hard_header routine, it adds and removes ll header
-     inside itself. In this case ll header is invisible outside of device,
-     but higher levels still should reserve dev->hard_header_len.
-     Some devices are enough clever to reallocate skb, when header
-     will not fit to reserved space (tunnel), another ones are silly
-     (PPP).
+   - If the device has no dev->header_ops, there is no LL header visible
+     above the device. In this case, its hard_header_len should be 0.
+     The device may prepend its own header internally. In this case, its
+     needed_headroom should be set to the space needed for it to add its
+     internal header.
+     For example, a WiFi driver pretending to be an Ethernet driver should
+     set its hard_header_len to be the Ethernet header length, and set its
+     needed_headroom to be (the real WiFi header length - the fake Ethernet
+     header length).
    - packet socket receives packets with pulled ll header,
      so that SOCK_RAW should push it back.
 

net/packet/af_packet.c static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) · line 2946

Quoted 8 lines
net/packet/af_packet.c +2 -0
@@ -2936,6 +2939,8 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
        skb_reset_network_header(skb);
 
        err = -EINVAL;
+       if (!dev->header_ops)
+               WARN_ON_ONCE(dev->hard_header_len != 0);
        if (sock->type == SOCK_DGRAM) {
                offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
                if (unlikely(offset < 0))

From a quick scan, a few device types that might trigger this

net/atm/clip.c
drivers/net/wan/hdlc_fr.c
drivers/net/appletalk/ipddp.c
drivers/net/ppp/ppp_generic.c
drivers/net/net_failover.c

author
#2 lore raw
Quoted 8 lines · Willem de Bruijn

From a quick scan, a few device types that might trigger this

net/atm/clip.c
drivers/net/wan/hdlc_fr.c
drivers/net/appletalk/ipddp.c
drivers/net/ppp/ppp_generic.c
drivers/net/net_failover.c

I have recently fixed this problem in the "net" tree in hdlc_fr.c.

Glad to see the number of drivers that have this problem is not very big.

maintainer
#3 lore raw
Quoted 12 lines · Xie He
Quoted 8 lines · Willem de Bruijn

From a quick scan, a few device types that might trigger this

net/atm/clip.c
drivers/net/wan/hdlc_fr.c
drivers/net/appletalk/ipddp.c
drivers/net/ppp/ppp_generic.c
drivers/net/net_failover.c

Quoted 3 lines

I have recently fixed this problem in the "net" tree in hdlc_fr.c.

Glad to see the number of drivers that have this problem is not very big.

I am concerned about adding a WARN_ON_ONCE that we already expect to fire on some platforms.

Probably better to add the documentation without the warning.

I know I suggested the check before, sorry for the churn, but I hadn't checked the existing state yet.

author
#4 lore raw
Quoted 8 lines · Willem de Bruijn

I am concerned about adding a WARN_ON_ONCE that we already expect to fire on some platforms.

Probably better to add the documentation without the warning.

I know I suggested the check before, sorry for the churn, but I hadn't checked the existing state yet.

OK. No problem. It's always good to discuss and find out the best way before we do the changes.

I'll drop the WARN_ON_ONCE part and resubmit the patch.