From d818ce495794c0a56d564dc0318ba7825bd91e66 Mon Sep 17 00:00:00 2001 From: Red Bear OS Date: Thu, 9 Jul 2026 12:24:19 +0300 Subject: [PATCH] virtio-netd: replace unimplemented!() with random MAC fallback When the VIRTIO device doesn't report a MAC in config space, generate a random locally-administered unicast MAC instead of panicking with unimplemented!(). This matches Linux 7.1 drivers/net/virtio_net.c virtnet_probe() behavior. Reads /scheme/rand for random bytes; falls back to a fixed MAC if the random source is unavailable. MAC is forced to unicast + locally administered (bit 0=0, bit 1=1). --- drivers/net/virtio-netd/src/main.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/net/virtio-netd/src/main.rs b/drivers/net/virtio-netd/src/main.rs index 17d168efdf..ffa5bac8b4 100644 --- a/drivers/net/virtio-netd/src/main.rs +++ b/drivers/net/virtio-netd/src/main.rs @@ -84,7 +84,21 @@ fn deamon( device.transport.ack_driver_feature(VIRTIO_NET_F_MAC); mac } else { - unimplemented!() + // VIRTIO device didn't report a MAC in config space. + // Generate a random locally-administered unicast MAC as fallback. + // Linux 7.1 drivers/net/virtio_net.c: virtnet_probe() also + // generates a random MAC when the device doesn't provide one. + let mut mac = [0u8; 6]; + if let Ok(rand_bytes) = std::fs::read("/scheme/rand") { + let n = rand_bytes.len().min(6); + mac[..n].copy_from_slice(&rand_bytes[..n]); + } else { + // Fallback: use a fixed locally-administered MAC + mac = [0x02, 0x00, 0x00, 0x00, 0x00, 0x01]; + } + mac[0] = (mac[0] & 0xFE) | 0x02; // unicast + locally administered + log::warn!("virtio-netd: no MAC in config, using random {:02x?}", mac); + mac }; device.transport.finalize_features();