linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] drivers/net/wan/hdlc_x25: Added needed_headroom and a skb->len check
@ 2020-08-13 18:17 Xie He
  2020-08-15  3:41 ` David Miller
  0 siblings, 1 reply; 3+ messages in thread
From: Xie He @ 2020-08-13 18:17 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski, netdev, linux-kernel, linux-x25
  Cc: Xie He, Willem de Bruijn, Martin Schiller, Andrew Hendry

1. Added a skb->len check

This driver expects upper layers to include a pseudo header of 1 byte
when passing down a skb for transmission. This driver will read this
1-byte header. This patch added a skb->len check before reading the
header to make sure the header exists.

2. Added needed_headroom and set hard_header_len to 0

When this driver transmits data,
  first this driver will remove a pseudo header of 1 byte,
  then the lapb module will prepend the LAPB header of 2 or 3 bytes.
So the value of needed_headroom in this driver should be 3 - 1.

Because this driver has no header_ops, according to the logic of
af_packet.c, the value of hard_header_len should be 0.

Reason of setting needed_headroom and hard_header_len at this place:

This driver is written using the API of the hdlc module, the hdlc
module enables this driver (the protocol driver) to run on any hardware
that has a driver (the hardware driver) written using the API of the
hdlc module.

Two other hdlc protocol drivers - hdlc_ppp and hdlc_raw_eth, also set
things like hard_header_len at this place. In hdlc_ppp, it sets
hard_header_len after attach_hdlc_protocol and before setting dev->type.
In hdlc_raw_eth, it sets hard_header_len by calling ether_setup after
attach_hdlc_protocol and after memcpy the settings.

3. Reset needed_headroom when detaching protocols (in hdlc.c)

When detaching a protocol from a hardware device, the hdlc module will
reset various parameters of the device (including hard_header_len) to
the default values. We add needed_headroom here so that needed_headroom
will also be reset.

Cc: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Cc: Martin Schiller <ms@dev.tdt.de>
Cc: Andrew Hendry <andrew.hendry@gmail.com>
Signed-off-by: Xie He <xie.he.0141@gmail.com>
---
 drivers/net/wan/hdlc.c     |  1 +
 drivers/net/wan/hdlc_x25.c | 17 ++++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wan/hdlc.c b/drivers/net/wan/hdlc.c
index dfc16770458d..386ed2aa31fd 100644
--- a/drivers/net/wan/hdlc.c
+++ b/drivers/net/wan/hdlc.c
@@ -230,6 +230,7 @@ static void hdlc_setup_dev(struct net_device *dev)
 	dev->max_mtu		 = HDLC_MAX_MTU;
 	dev->type		 = ARPHRD_RAWHDLC;
 	dev->hard_header_len	 = 16;
+	dev->needed_headroom	 = 0;
 	dev->addr_len		 = 0;
 	dev->header_ops		 = &hdlc_null_ops;
 }
diff --git a/drivers/net/wan/hdlc_x25.c b/drivers/net/wan/hdlc_x25.c
index f70336bb6f52..f52b9fed0593 100644
--- a/drivers/net/wan/hdlc_x25.c
+++ b/drivers/net/wan/hdlc_x25.c
@@ -107,8 +107,14 @@ static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
 {
 	int result;
 
+	/* There should be a pseudo header of 1 byte added by upper layers.
+	 * Check to make sure it is there before reading it.
+	 */
+	if (skb->len < 1) {
+		kfree_skb(skb);
+		return NETDEV_TX_OK;
+	}
 
-	/* X.25 to LAPB */
 	switch (skb->data[0]) {
 	case X25_IFACE_DATA:	/* Data to be transmitted */
 		skb_pull(skb, 1);
@@ -294,6 +300,15 @@ static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
 			return result;
 
 		memcpy(&state(hdlc)->settings, &new_settings, size);
+
+		/* There's no header_ops so hard_header_len should be 0. */
+		dev->hard_header_len = 0;
+		/* When transmitting data:
+		 * first we'll remove a pseudo header of 1 byte,
+		 * then we'll prepend an LAPB header of at most 3 bytes.
+		 */
+		dev->needed_headroom = 3 - 1;
+
 		dev->type = ARPHRD_X25;
 		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
 		netif_dormant_off(dev);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] drivers/net/wan/hdlc_x25: Added needed_headroom and a skb->len check
  2020-08-13 18:17 [PATCH net] drivers/net/wan/hdlc_x25: Added needed_headroom and a skb->len check Xie He
@ 2020-08-15  3:41 ` David Miller
  2020-08-15  5:21   ` Xie He
  0 siblings, 1 reply; 3+ messages in thread
From: David Miller @ 2020-08-15  3:41 UTC (permalink / raw)
  To: xie.he.0141
  Cc: kuba, netdev, linux-kernel, linux-x25, willemdebruijn.kernel, ms,
	andrew.hendry

From: Xie He <xie.he.0141@gmail.com>
Date: Thu, 13 Aug 2020 11:17:04 -0700

> 1. Added a skb->len check
> 
> This driver expects upper layers to include a pseudo header of 1 byte
> when passing down a skb for transmission. This driver will read this
> 1-byte header. This patch added a skb->len check before reading the
> header to make sure the header exists.
> 
> 2. Added needed_headroom and set hard_header_len to 0
> 
> When this driver transmits data,
>   first this driver will remove a pseudo header of 1 byte,
>   then the lapb module will prepend the LAPB header of 2 or 3 bytes.
> So the value of needed_headroom in this driver should be 3 - 1.
> 
> Because this driver has no header_ops, according to the logic of
> af_packet.c, the value of hard_header_len should be 0.
> 
> Reason of setting needed_headroom and hard_header_len at this place:
> 
> This driver is written using the API of the hdlc module, the hdlc
> module enables this driver (the protocol driver) to run on any hardware
> that has a driver (the hardware driver) written using the API of the
> hdlc module.
> 
> Two other hdlc protocol drivers - hdlc_ppp and hdlc_raw_eth, also set
> things like hard_header_len at this place. In hdlc_ppp, it sets
> hard_header_len after attach_hdlc_protocol and before setting dev->type.
> In hdlc_raw_eth, it sets hard_header_len by calling ether_setup after
> attach_hdlc_protocol and after memcpy the settings.
> 
> 3. Reset needed_headroom when detaching protocols (in hdlc.c)
> 
> When detaching a protocol from a hardware device, the hdlc module will
> reset various parameters of the device (including hard_header_len) to
> the default values. We add needed_headroom here so that needed_headroom
> will also be reset.
> 
> Cc: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
> Cc: Martin Schiller <ms@dev.tdt.de>
> Cc: Andrew Hendry <andrew.hendry@gmail.com>
> Signed-off-by: Xie He <xie.he.0141@gmail.com>

Applied, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] drivers/net/wan/hdlc_x25: Added needed_headroom and a skb->len check
  2020-08-15  3:41 ` David Miller
@ 2020-08-15  5:21   ` Xie He
  0 siblings, 0 replies; 3+ messages in thread
From: Xie He @ 2020-08-15  5:21 UTC (permalink / raw)
  To: David Miller
  Cc: Jakub Kicinski, Linux Kernel Network Developers, LKML, Linux X25,
	Willem de Bruijn, Martin Schiller, Andrew Hendry

On Fri, Aug 14, 2020 at 8:41 PM David Miller <davem@davemloft.net> wrote:
>
> Applied, thanks.

Thank you, David!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-15 22:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-13 18:17 [PATCH net] drivers/net/wan/hdlc_x25: Added needed_headroom and a skb->len check Xie He
2020-08-15  3:41 ` David Miller
2020-08-15  5:21   ` Xie He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).