Re: [PATCH v5 3/5] net: of_net: Derive MAC addresses from a unique device ID
From: Alexey Charkov
Date: Fri Dec 04 2026 - 06:25:17 EST
Hi David,
On Fri, Dec 4, 2026 at 1:45 PM David Laight
<david.laight.linux@xxxxxxxxx> wrote:
>
> On Wed, 25 Nov 2026 23:36:40 +0400
> Alexey Charkov <alchark@xxxxxxxxxxx> 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@xxxxxxx>
> > Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
> > ---
> > 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).
I'm following the existing derivation used by Rockchip algorithms
already implemented in bootloaders both upstream [1] and downstream
[2], so I'd rather not deviate from that logic to ensure we arrive at
the same MAC addresses from the same CPUID.
[1] https://elixir.bootlin.com/u-boot/v2026.10/source/arch/arm/mach-rockchip/board.c#L326-L364
[2] https://github.com/rockchip-linux/u-boot/blob/1c535d65b8509f388d09e49fb6961f49fda35a1d/board/rockchip/evb_rk3399/evb-rk3399.c#L77-L108
Best regards,
Alexey