dt-bindings: net: mediatek: Support custom ifname
Subsystems (5)
PATCH 1/2 dt-bindings: net: mediatek: Support custom ifname
From: Reto Schneider <[email protected]>
The (optional) label property allows to specify customized interfaces names.
The motivation behind this change is to allow embedded devices to keep their first switch port be named "eth0", even when switching to the DSA architecture. In order to do so, it must be possible to name the MAC interface differently from eth0.
---
Documentation/devicetree/bindings/net/mediatek-net.txt | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/net/mediatek-net.txt +4 -0
@@ -51,6 +51,9 @@ Required properties: is equal to 0 and the MAC uses fixed-link to connect with internal switch such as MT7530. +Optional properties: +- label: Name of interface, defaults to ethX if missing + Example: eth: ethernet@1b100000 {
@@ -76,6 +79,7 @@ eth: ethernet@1b100000 { compatible = "mediatek,eth-mac"; reg = <0>; phy-handle = <&phy0>; + label = "mac1"; }; gmac2: mac@1 {
Signature · 1 line
2.30.2
PATCH 2/2 net: ethernet: mtk_eth_soc: Support custom ifname
From: Reto Schneider <[email protected]>
Name the MAC interface name according to the label property. If the property is missing, the default name (ethX) gets used.
---
drivers/net/ethernet/mediatek/mtk_eth_soc.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +4 -0
@@ -2948,6 +2948,7 @@ static const struct net_device_ops mtk_netdev_ops = {static int mtk_add_mac (struct mtk_eth *eth,struct device_node *np) {const __be32 *_id =of_get_property (np,"reg" ,NULL ); +const char *const name =of_get_property (np,"label" ,NULL );phy_interface_t phy_mode;struct phylink *phylink;struct mtk_mac *mac;
@@ -3020,6 +3021,9 @@ static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np) mac->phylink = phylink; +if (name) +strncpy (eth->netdev[id]->name, name,IFNAMSIZ ); +SET_NETDEV_DEV (eth->netdev[id], eth->dev); eth->netdev[id]->watchdog_timeo =5 *HZ ; eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
Signature · 1 line
2.30.2
Re: [PATCH v1 2/2] net: ethernet: mtk_eth_soc: Support custom ifname
Quoted 13 lines · Reto Schneider
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1
-0@@ -2948,6 +2948,7 @@ static const struct net_device_ops mtk_netdev_ops = {static int mtk_add_mac (struct mtk_eth *eth,struct device_node *np) {const __be32 *_id =of_get_property (np,"reg" ,NULL ); +const char *const name =of_get_property (np,"label" ,NULL );phy_interface_t phy_mode;struct phylink *phylink;struct mtk_mac *mac;
drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np) · line 3025
Quoted 5 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +2
-0@@ -3020,6 +3021,9 @@ static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np) mac->phylink = phylink; +if (name) +strncpy (eth->netdev[id]->name, name,IFNAMSIZ );
Please don't use strncpy() - this is a good example why strncpy() is bad news.
* strncpy - Copy a length-limited, C-string
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @count: The maximum number of bytes to copy
*
* The result is not %NUL-terminated if the source exceeds
* @count bytes.
Consequently, if "name" is IFNAMSIZ bytes or longer, eth->netdev[id]->name will not be NUL terminated, and subsequent use will run off the end of the string. strscpy() is safer to use here.
Thanks.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH v1 2/2] net: ethernet: mtk_eth_soc: Support custom ifname
Hi Russell,
Quoted 11 lines · Russell King (Oracle)
Please don't use
strncpy()- this is a good example whystrncpy()is bad news.* strncpy - Copy a length-limited, C-string
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @count: The maximum number of bytes to copy
*
* The result is not %NUL-terminated if the source exceeds
* @count bytes.
Quoted 3 lines
Consequently, if "name" is IFNAMSIZ bytes or longer, eth->netdev[id]->name will not be NUL terminated, and subsequent use will run off the end of the string.
strscpy()is safer to use here.
Thanks a lot for finding this (embarrassing mistake) and pointing me in the right direction (did dot know about strscpy).
Will send v2 soon.
Kind regards,
Reto
David Miller
maintainerRe: [PATCH v1 2/2] net: ethernet: mtk_eth_soc: Support custom ifname
In reply to Reto SchneiderFrom: Reto Schneider <[email protected]>
Date: Sun, 13 Jun 2021 13:58:19 +0200
Quoted 6 lines
From: Reto Schneider <[email protected]>
Name the MAC interface name according to the label property. If the property is missing, the default name (ethX) gets used.
Signed-off-by Reto Schneider <[email protected]>
Please solve naming issues in userspace via udev, thank you.
PATCH v2 1/2 dt-bindings: net: mediatek: Support custom ifname
In reply to Reto SchneiderFrom: Reto Schneider <[email protected]>
The (optional) label property allows to specify customized interfaces names.
The motivation behind this change is to allow embedded devices to keep their first switch port be named "eth0", even when switching to the DSA architecture. In order to do so, it must be possible to name the MAC interface differently from eth0.
---
(no changes since v1)
Documentation/devicetree/bindings/net/mediatek-net.txt | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/net/mediatek-net.txt +4 -0
@@ -51,6 +51,9 @@ Required properties: is equal to 0 and the MAC uses fixed-link to connect with internal switch such as MT7530. +Optional properties: +- label: Name of interface, defaults to ethX if missing + Example: eth: ethernet@1b100000 {
@@ -76,6 +79,7 @@ eth: ethernet@1b100000 { compatible = "mediatek,eth-mac"; reg = <0>; phy-handle = <&phy0>; + label = "mac1"; }; gmac2: mac@1 {
Signature · 1 line
2.30.2
PATCH v2 2/2 net: ethernet: mtk_eth_soc: Support custom ifname
In reply to Reto SchneiderFrom: Reto Schneider <[email protected]>
Name the MAC interface name according to the label property. If the property is missing, the default name (ethX) gets used.
Labels with more than IFNAMSIZ -1 characters will be truncated silently, which seems to be what most of the code base does when using strscpy.
---
Changes in v2:
- Avoid dangerous usage of strncpy
drivers/net/ethernet/mediatek/mtk_eth_soc.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +4 -0
@@ -2948,6 +2948,7 @@ static const struct net_device_ops mtk_netdev_ops = {static int mtk_add_mac (struct mtk_eth *eth,struct device_node *np) {const __be32 *_id =of_get_property (np,"reg" ,NULL ); +const char *const name =of_get_property (np,"label" ,NULL );phy_interface_t phy_mode;struct phylink *phylink;struct mtk_mac *mac;
@@ -3020,6 +3021,9 @@ static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np) mac->phylink = phylink; +if (name) +strscpy (eth->netdev[id]->name, name,IFNAMSIZ ); +SET_NETDEV_DEV (eth->netdev[id], eth->dev); eth->netdev[id]->watchdog_timeo =5 *HZ ; eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
Signature · 1 line
2.30.2