From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
stable@dpdk.org, Gagandeep Singh <g.singh@nxp.com>,
Sachin Saxena <sachin.saxena@nxp.com>,
Vanshika Shukla <vanshika.shukla@nxp.com>,
Apeksha Gupta <apeksha.gupta@nxp.com>
Subject: [PATCH 14/18] drivers/net: use common random MAC address helper
Date: Tue, 15 Sep 2026 12:21:12 -0700 [thread overview]
Message-ID: <20260915192207.3926217-15-stephen@networkplumber.org> (raw)
In-Reply-To: <20260915192207.3926217-1-stephen@networkplumber.org>
Both drivers open coded a random MAC address: take 32 bits from
rte_rand(), clear the multicast bit and set the local assignment bit.
That is what rte_eth_random_addr() does, and it now takes its
randomness from the operating system rather than rte_rand().
The enetc version also wrote past the end of the six byte address.
After incrementing a uint32_t pointer to the last two bytes it stored
a full uint32_t there, overrunning hw->mac.addr by two bytes. The
address bytes are now read explicitly instead of through a wider
pointer, which also makes the value written to PMAR1 independent of
the host byte order.
Note that the read path at the top of the same function has the same
overrun. It is left alone here, this patch only changes the code that
generates the address.
Fixes: a7fc52fcadb1 ("net/enetc: introduce ENETC4 PMD")
Cc: stable@dpdk.org
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
drivers/net/enetc/enetc4_vf.c | 22 +++++++++++-----------
drivers/net/enetfec/enet_ethdev.c | 16 ++--------------
2 files changed, 13 insertions(+), 25 deletions(-)
diff --git a/drivers/net/enetc/enetc4_vf.c b/drivers/net/enetc/enetc4_vf.c
index ef5f1e6d66..df3b4bd89b 100644
--- a/drivers/net/enetc/enetc4_vf.c
+++ b/drivers/net/enetc/enetc4_vf.c
@@ -4,7 +4,6 @@
#include <stdbool.h>
#include <rte_kvargs.h>
-#include <rte_random.h>
#include <dpaax_iova_table.h>
#include "enetc_logs.h"
#include "enetc.h"
@@ -1252,18 +1251,19 @@ enetc4_vf_mac_init(struct enetc_eth_hw *hw, struct rte_eth_dev *eth_dev)
low_mac = (uint16_t)*mac;
if ((high_mac | low_mac) == 0) {
- char *first_byte;
+ const uint8_t *addr = hw->mac.addr;
+
ENETC_PMD_NOTICE("MAC is not available for this SI, "
"set random MAC");
- mac = (uint32_t *)hw->mac.addr;
- *mac = (uint32_t)rte_rand();
- first_byte = (char *)mac;
- *first_byte &= 0xfe; /* clear multicast bit */
- *first_byte |= 0x02; /* set local assignment bit (IEEE802) */
- enetc4_port_wr(enetc_hw, ENETC4_PMAR0, *mac);
- mac++;
- *mac = (uint16_t)rte_rand();
- enetc4_port_wr(enetc_hw, ENETC4_PMAR1, *mac);
+ rte_eth_random_addr(hw->mac.addr);
+ /* The address is six bytes, read the last two explicitly
+ * rather than through a wider pointer.
+ */
+ enetc4_port_wr(enetc_hw, ENETC4_PMAR0,
+ addr[0] | addr[1] << 8 |
+ addr[2] << 16 | (uint32_t)addr[3] << 24);
+ enetc4_port_wr(enetc_hw, ENETC4_PMAR1,
+ addr[4] | addr[5] << 8);
enetc_print_ethaddr("New address: ",
(const struct rte_ether_addr *)hw->mac.addr);
}
diff --git a/drivers/net/enetfec/enet_ethdev.c b/drivers/net/enetfec/enet_ethdev.c
index c7ef097875..691f634d80 100644
--- a/drivers/net/enetfec/enet_ethdev.c
+++ b/drivers/net/enetfec/enet_ethdev.c
@@ -637,20 +637,8 @@ pmd_enetfec_probe(struct rte_vdev_device *vdev)
*mac = (uint16_t)(tmac >> ENETFEC_MAC_SHIFT);
high_mac = (uint16_t)(*mac);
- if ((high_mac | low_mac) == 0 || (high_mac | low_mac) == ENETFEC_MAC_RESET) {
- uint8_t *first_byte;
-
- mac = (uint16_t *)addr.addr_bytes;
- tmac = (uint32_t)rte_rand();
- first_byte = (uint8_t *)&tmac;
- *first_byte &= (uint8_t)~RTE_ETHER_GROUP_ADDR; /* clear multicast bit */
- *first_byte |= RTE_ETHER_LOCAL_ADMIN_ADDR; /* set local assignment bit (IEEE802) */
- *mac = (uint16_t)tmac;
- mac++;
- *mac = (uint16_t)(tmac >> ENETFEC_MAC_SHIFT);
- mac++;
- *mac = (uint16_t)rte_rand();
- }
+ if ((high_mac | low_mac) == 0 || (high_mac | low_mac) == ENETFEC_MAC_RESET)
+ rte_eth_random_addr(addr.addr_bytes);
enetfec_set_mac_address(dev, &addr);
--
2.53.0
next prev parent reply other threads:[~2026-09-15 19:23 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 19:20 [PATCH 00/18] random number generator improvements Stephen Hemminger
2026-09-15 19:20 ` [PATCH 01/18] eal/windows: add getentropy shim Stephen Hemminger
2026-09-15 19:21 ` [PATCH 02/18] random: always use getentropy Stephen Hemminger
2026-09-15 19:21 ` [PATCH 03/18] eal: add 32 bit random number function Stephen Hemminger
2026-09-15 19:21 ` [PATCH 04/18] sched: fix data race in RED random number generation Stephen Hemminger
2026-09-15 19:21 ` [PATCH 05/18] eal/windows: add getrandom shim Stephen Hemminger
2026-09-15 19:21 ` [PATCH 06/18] eal: add function to get random bytes Stephen Hemminger
2026-09-15 19:21 ` [PATCH 07/18] hash: use secure random for Toeplitz hash key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 08/18] pipeline: use secure random for IPsec salt Stephen Hemminger
2026-09-15 19:21 ` [PATCH 09/18] net: use secure random for random MAC address Stephen Hemminger
2026-09-15 19:21 ` [PATCH 10/18] ipsec: use secure random for SAD hash seed Stephen Hemminger
2026-09-15 19:21 ` [PATCH 11/18] net/intel: use secure random for default RSS key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 12/18] random: use all 64 bits of the seed Stephen Hemminger
2026-09-15 19:21 ` [PATCH 13/18] drivers/net: use secure random for RSS key Stephen Hemminger
2026-09-15 19:21 ` Stephen Hemminger [this message]
2026-09-15 19:21 ` [PATCH 15/18] random: remove RDSEED fallback for initial seed Stephen Hemminger
2026-09-16 7:44 ` Bruce Richardson
2026-09-15 19:21 ` [PATCH 16/18] test: allow setting random seed for tests Stephen Hemminger
2026-09-15 19:21 ` [PATCH 17/18] test: add random number generator tests Stephen Hemminger
2026-09-15 19:21 ` [PATCH 18/18] doc: recommend secure random for unpredictable values Stephen Hemminger
2026-09-22 12:32 ` Aaron Conole
2026-10-10 2:27 ` [PATCH v2 0/5] random: fix initial seed entropy Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 1/5] eal/windows: add getentropy shim Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 2/5] random: always use getentropy Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 3/5] random: use all 64 bits of the seed Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 4/5] random: remove RDSEED fallback for initial seed Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 5/5] test: allow setting random seed for tests Stephen Hemminger
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=20260915192207.3926217-15-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=apeksha.gupta@nxp.com \
--cc=dev@dpdk.org \
--cc=g.singh@nxp.com \
--cc=sachin.saxena@nxp.com \
--cc=stable@dpdk.org \
--cc=vanshika.shukla@nxp.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