The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Alexey Charkov <alchark@flipper.net>
Cc: "Srinivas Kandagatla" <srini@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Heiko Stuebner" <heiko@sntech.de>,
	"Rafał Miłecki" <rafal@milecki.pl>,
	"Andrew Lunn" <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>, "Andrew Lunn" <andrew@lunn.ch>,
	"Heiner Kallweit" <hkallweit1@gmail.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"Willem de Bruijn" <willemb@google.com>,
	"Simon Horman" <horms@kernel.org>,
	"Ryan Leung" <untilscour@protonmail.com>,
	"Ricardo Pardini" <ricardo@pardini.net>,
	devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-rockchip@lists.infradead.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 3/5] net: of_net: Derive MAC addresses from a unique device ID
Date: Fri, 4 Dec 2026 09:45:52 +0000	[thread overview]
Message-ID: <20261204094552.62ffd53b@pumpkin> (raw)
In-Reply-To: <20261125-rk3576-otp-cpuid-mac-v5-3-b3125c9a5fc8@flipper.net>

On Wed, 25 Nov 2026 23:36:40 +0400
Alexey Charkov <alchark@flipper.net> wrote:

> Bootloaders on several platforms derive a stable MAC address from an
> identifier unique to the chip, such as a SoC serial number in OTP
> memory, so that boards without dedicated storage for an address still
> get a persistent one. They patch the result into the device tree they
> pass on, which leaves kernels started any other way, e.g. straight from
> the SPL in Falcon mode, with a random address changing on every boot.
> 
> Derive the address in of_get_mac_address() instead, as the last resort
> after any stored one, from a "unique-id" NVMEM cell. Doing it here
> rather than in individual drivers lets any controller with a device tree
> node use it, including PCIe ones. Pick the derivation by the compatible
> of the referenced cell, as the algorithms differ between vendors, and
> index the addresses by the device's "ethernetN" alias, which is how the
> bootloaders assign them to interfaces.
> 
> Start with Rockchip, whose bootloaders hash the CPU ID as a lowercase
> hex string with SHA-256 and XOR the index into the last octet. Follow
> U-Boot's rockchip_setup_macaddr() exactly, so that both yield the same
> addresses.
> 
> Suggested-by: Andrew Lunn <andrew@lunn.ch>
> Signed-off-by: Alexey Charkov <alchark@flipper.net>
> ---
>  net/core/of_net.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 103 insertions(+), 2 deletions(-)
> 
> diff --git a/net/core/of_net.c b/net/core/of_net.c
> index 93ea425b9248..1f9858d2d444 100644
> --- a/net/core/of_net.c
> +++ b/net/core/of_net.c
> @@ -4,7 +4,9 @@
>   *
>   * Initially copied out of arch/powerpc/kernel/prom_parse.c
>   */
> +#include <crypto/sha2.h>
>  #include <linux/etherdevice.h>
> +#include <linux/hex.h>
>  #include <linux/kernel.h>
>  #include <linux/of_net.h>
>  #include <linux/of_platform.h>
> @@ -97,6 +99,99 @@ int of_get_mac_address_nvmem(struct device_node *np, u8 *addr)
>  }
>  EXPORT_SYMBOL(of_get_mac_address_nvmem);
>  
> +#define ROCKCHIP_CPUID_LEN	16
> +
> +/*
> + * Rockchip bootloaders hash the CPU ID as a lowercase hex string with SHA-256
> + * and XOR the index into the last octet, see rockchip_setup_macaddr() in
> + * U-Boot's arch/arm/mach-rockchip/board.c.
> + */
> +static int of_mac_from_rockchip_cpuid(const u8 *id, size_t len, int index,
> +				      u8 *addr)
> +{
> +	char hex[ROCKCHIP_CPUID_LEN * 2];
> +	u8 digest[SHA256_DIGEST_SIZE];
> +
> +	if (len != ROCKCHIP_CPUID_LEN || index > U8_MAX)
> +		return -EINVAL;
> +
> +	bin2hex(hex, id, ROCKCHIP_CPUID_LEN);
> +	sha256(hex, sizeof(hex), digest);

If you are going to use sha256() (or pretty much and other hash
function for that matter) you might just as well hash the ascii string.
sha256() does seem a little excessive, jhash() would do even though it
only returns 32 bits.

> +
> +	memcpy(addr, digest, ETH_ALEN);
> +	addr[0] &= 0xfe;	/* clear the multicast bit */
> +	addr[0] |= 0x02;	/* set the locally administered bit */
> +	addr[5] ^= index;

That doesn't look that good to me.
Setting the 'locally administered' bit doesn't really help with uniqueness.
There certainly used to be plenty of physical ethernet cards that had
'locally administered' MAC addresses burnt into their EEPROMs.

It would be better to use the same OUI (first 24 bits) that Linux uses
for 'random' MAC addresses - just with a random number that is fixed
for the specific hardware.

IIRC 'locally administered' addresses mostly exist for DecNet where the
MAC address was set by the local network administrator and was used to
allocate a 32bit value that identified the machine (cf an IP address).

David

> +
> +	return 0;
> +}
> +
> +struct of_mac_unique_id {
> +	const char *compatible;
> +	int (*derive)(const u8 *id, size_t len, int index, u8 *addr);
> +};
> +
> +/* Derivations by the compatible of the unique ID cell */
> +static const struct of_mac_unique_id of_mac_unique_ids[] = {
> +	{ "rockchip,cpuid", of_mac_from_rockchip_cpuid },
> +};
> +
> +/*
> + * Derive a MAC address from a "unique-id" NVMEM cell the way the cell's
> + * binding defines it, indexed by the "ethernetN" alias of the device, as that
> + * is how bootloaders assign the derived addresses to interfaces.
> + */
> +static int of_get_mac_address_unique_id(struct device_node *np, u8 *addr)
> +{
> +	const struct of_mac_unique_id *entry = NULL;
> +	struct of_phandle_args cell_spec;
> +	struct nvmem_cell *cell;
> +	int index, alias, ret, i;
> +	size_t len;
> +	u8 *id;
> +
> +	index = of_property_match_string(np, "nvmem-cell-names", "unique-id");
> +	if (index < 0)
> +		return -ENOENT;
> +
> +	ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
> +						  "#nvmem-cell-cells",
> +						  index, &cell_spec);
> +	if (ret)
> +		return ret;
> +
> +	for (i = 0; i < ARRAY_SIZE(of_mac_unique_ids); i++) {
> +		if (of_device_is_compatible(cell_spec.np,
> +					    of_mac_unique_ids[i].compatible)) {
> +			entry = &of_mac_unique_ids[i];
> +			break;
> +		}
> +	}
> +	of_node_put(cell_spec.np);
> +	if (!entry)
> +		return -EOPNOTSUPP;
> +
> +	alias = of_alias_get_id(np, "ethernet");
> +	if (alias < 0)
> +		return alias;
> +
> +	cell = of_nvmem_cell_get(np, "unique-id");
> +	if (IS_ERR(cell))
> +		return PTR_ERR(cell);
> +
> +	id = nvmem_cell_read(cell, &len);
> +	nvmem_cell_put(cell);
> +	if (IS_ERR(id))
> +		return PTR_ERR(id);
> +
> +	ret = entry->derive(id, len, alias, addr);
> +	kfree(id);
> +	if (ret)
> +		return ret;
> +
> +	return is_valid_ether_addr(addr) ? 0 : -EINVAL;
> +}
> +
>  /**
>   * of_get_mac_address()
>   * @np:		Caller's Device Node
> @@ -108,7 +203,9 @@ EXPORT_SYMBOL(of_get_mac_address_nvmem);
>   * because that is the default address. If that isn't set, then the obsolete
>   * 'address' is checked, just in case we're using an old device tree. If any
>   * of the above isn't set, then try to get MAC address from nvmem cell named
> - * 'mac-address'.
> + * 'mac-address'. As the last resort, derive one from an identifier unique to
> + * the device in an nvmem cell named 'unique-id', if the way to do so is known
> + * for that cell.
>   *
>   * Note that the 'address' property is supposed to contain a virtual address of
>   * the register set, but some DTS files have redefined that property to be the
> @@ -142,7 +239,11 @@ int of_get_mac_address(struct device_node *np, u8 *addr)
>  	if (!ret)
>  		return 0;
>  
> -	return of_get_mac_address_nvmem(np, addr);
> +	ret = of_get_mac_address_nvmem(np, addr);
> +	if (!ret || ret == -EPROBE_DEFER)
> +		return ret;
> +
> +	return of_get_mac_address_unique_id(np, addr);
>  }
>  EXPORT_SYMBOL(of_get_mac_address);
>  
> 


  parent reply	other threads:[~2026-12-04  9:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 19:36 [PATCH v5 0/5] net: Derive stable MAC addresses from a unique device ID Alexey Charkov
2026-11-25 19:36 ` [PATCH v5 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip CPU ID Alexey Charkov
2026-12-03 18:47   ` Rob Herring (Arm)
2026-11-25 19:36 ` [PATCH v5 2/5] dt-bindings: net: ethernet-controller: Allow a unique ID NVMEM cell Alexey Charkov
2026-12-03 18:47   ` Rob Herring (Arm)
2026-11-25 19:36 ` [PATCH v5 3/5] net: of_net: Derive MAC addresses from a unique device ID Alexey Charkov
2026-11-26  4:02   ` Ryan Leung
2026-12-01  1:30   ` Andrew Lunn
2026-12-01  4:17     ` Alexey Charkov
2026-12-01 13:05       ` Andrew Lunn
2026-12-04  9:45   ` David Laight [this message]
2026-11-25 19:36 ` [PATCH v5 4/5] arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout Alexey Charkov
2026-11-25 19:36 ` [PATCH v5 5/5] arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576 Alexey Charkov
2026-12-04  2:18 ` [PATCH v5 0/5] net: Derive stable MAC addresses from a unique device ID Ricardo Pardini
2026-12-04  8:50   ` Alexey Charkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204094552.62ffd53b@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=alchark@flipper.net \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrew@lunn.ch \
    --cc=conor+dt@kernel.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@kernel.org \
    --cc=heiko@sntech.de \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rafal@milecki.pl \
    --cc=ricardo@pardini.net \
    --cc=robh@kernel.org \
    --cc=srini@kernel.org \
    --cc=untilscour@protonmail.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox