base-files: add function for generating random MAC
authorDavid Bauer <mail@david-bauer.net>
Sat, 11 Jul 2020 11:48:40 +0000 (13:48 +0200)
committerDavid Bauer <mail@david-bauer.net>
Tue, 28 Jul 2020 13:52:44 +0000 (15:52 +0200)
This adds a function for generating a valid random MAC address (unset MC
bit / set locally administered bit).

It is necessary for devices which do not have a MAC address programmed
by the manufacturer.

Signed-off-by: David Bauer <mail@david-bauer.net>
package/base-files/Makefile
package/base-files/files/lib/functions/system.sh

index 284899bd69efbef7d54c0ac2158ab77eb74712a1..d32bae8635e2722aeb594e36233e3b631d9cdd6a 100644 (file)
@@ -12,7 +12,7 @@ include $(INCLUDE_DIR)/version.mk
 include $(INCLUDE_DIR)/feeds.mk
 
 PKG_NAME:=base-files
-PKG_RELEASE:=223
+PKG_RELEASE:=224
 PKG_FLAGS:=nonshared
 
 PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/
index f3c901ff7982f32c1033ecca7fb3cc38a13895ef..80e417182a269326e560524b4136def819703257 100644 (file)
@@ -174,6 +174,18 @@ macaddr_setbit_la() {
        macaddr_setbit $1 7
 }
 
+macaddr_unsetbit_mc() {
+       local mac=$1
+
+       printf "%02x:%s" $((0x${mac%%:*} & ~0x01)) ${mac#*:}
+}
+
+macaddr_random() {
+       local randsrc=$(get_mac_binary /dev/urandom 0)
+       
+       echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
+}
+
 macaddr_2bin() {
        local mac=$1