Hi,

Wouldn't it be user friendly to tell why the MAC is random? Something like this.

if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) {
     printf("No /etc/hostuuid found. Fall back to a random mac address.");
     goto rando;
}

I know it would save me a lot of time if I would encounter this case.
Plus the message is only printed once on boot.

Regards,
Ronald.


Van: Kyle Evans <[email protected]>
Datum: woensdag, 2 juni 2021 05:59
Aan: [email protected], [email protected], 
[email protected]
Onderwerp: git: 2d741f33bd07 - main - kern: ether_gen_addr: randomize on 
default hostuuid, too

The branch main has been updated by kevans:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=2d741f33bd07bf94a59635db3c7b9e070a8a6e55

commit 2d741f33bd07bf94a59635db3c7b9e070a8a6e55
Author:     Kyle Evans <[email protected]>
AuthorDate: 2021-04-16 01:11:35 +0000
Commit:     Kyle Evans <[email protected]>
CommitDate: 2021-06-02 03:59:21 +0000

    kern: ether_gen_addr: randomize on default hostuuid, too
Currently, this will still hash the default (all zero) hostuuid and
    potentially arrive at a MAC address that has a high chance of collision
    if another interface of the same name appears in the same broadcast
    domain on another host without a hostuuid, e.g., some virtual machine
    setups.
Instead of using the default hostuuid, just treat it as a failure and
    generate a random LA unicast MAC address.
Reviewed by: bz, gbe, imp, kbowling, kp
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D29788
---
 share/man/man9/ether_gen_addr.9 |  5 +++--
 sys/kern/kern_jail.c            |  1 -
 sys/net/if_ethersubr.c          | 17 ++++++++++++++---
 sys/sys/jail.h                  |  1 +
 4 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/share/man/man9/ether_gen_addr.9 b/share/man/man9/ether_gen_addr.9
index 1b98a841736d..f69cb199e2c3 100644
--- a/share/man/man9/ether_gen_addr.9
+++ b/share/man/man9/ether_gen_addr.9
@@ -61,8 +61,9 @@ or on machines that do not use
 .Xr loader 8 .
 .Pp
 .Nm
-can fail to derive a MAC address due to memory allocation failure.
-In this case, a locally-administered unicast MAC address will be randomly
+can fail to derive a MAC address due to memory allocation failure, or because
+the hostid has not been populated.
+In these cases, a locally-administered unicast MAC address will be randomly
 generated and returned via the
 .Ar hwaddr
 parameter.
diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c
index 303e31490eb1..9784f3bfc23b 100644
--- a/sys/kern/kern_jail.c
+++ b/sys/kern/kern_jail.c
@@ -76,7 +76,6 @@ __FBSDID("$FreeBSD$");
#include <security/mac/mac_framework.h> -#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
 #define    PRISON0_HOSTUUID_MODULE "hostuuid"
MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c
index 01c2d2f7b3e8..7eb46df8281a 100644
--- a/sys/net/if_ethersubr.c
+++ b/sys/net/if_ethersubr.c
@@ -1443,6 +1443,11 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr 
*hwaddr)
    char jailname[MAXHOSTNAMELEN];
getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid));
+   if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) {
+       /* Fall back to a random mac address. */
+       goto rando;
+   }
+
    /* If each (vnet) jail would also have a unique hostuuid this would not
     * be necessary. */
    getjailname(curthread->td_ucred, jailname, sizeof(jailname));
@@ -1450,9 +1455,7 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr 
*hwaddr)
        jailname);
    if (sz < 0) {
        /* Fall back to a random mac address. */
-       arc4rand(hwaddr, sizeof(*hwaddr), 0);
-       hwaddr->octet[0] = 0x02;
-       return;
+       goto rando;
    }
SHA1Init(&ctx);
@@ -1467,6 +1470,14 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr 
*hwaddr)
        hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) &
            0xFF;
    }
+
+   return;
+rando:
+   arc4rand(hwaddr, sizeof(*hwaddr), 0);
+   /* Unicast */
+   hwaddr->octet[0] &= 0xFE;
+   /* Locally administered. */
+   hwaddr->octet[0] |= 0x02;
 }
DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
diff --git a/sys/sys/jail.h b/sys/sys/jail.h
index c9d4c65e4d9e..76ed377e3f06 100644
--- a/sys/sys/jail.h
+++ b/sys/sys/jail.h
@@ -140,6 +140,7 @@ MALLOC_DECLARE(M_PRISON);
 #include <sys/osd.h>
#define HOSTUUIDLEN 64
+#define    DEFAULT_HOSTUUID    "00000000-0000-0000-0000-000000000000"
 #define    OSRELEASELEN    32
struct racct;
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/dev-commits-src-all
To unsubscribe, send any mail to "[email protected]"



_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/dev-commits-src-all
To unsubscribe, send any mail to "[email protected]"

Reply via email to