summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-04-18 00:50:40 +0200
committerTom Gundersen <teg@jklm.no>2014-04-19 18:41:58 +0200
commitcdc85c875b842b9309f72caefc51c262f521cf92 (patch)
tree541972100ab56dc244259f197833479333c791cb
parent505f8da7325591defe5f751f328bd26915267602 (diff)
networkd: netdev - set predictable mac address when creating netdev
-rw-r--r--src/network/networkd-netdev.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/network/networkd-netdev.c b/src/network/networkd-netdev.c
index b7fc48db94..075a8790a0 100644
--- a/src/network/networkd-netdev.c
+++ b/src/network/networkd-netdev.c
@@ -25,6 +25,7 @@
#include "conf-files.h"
#include "conf-parser.h"
#include "list.h"
+#include "siphash24.h"
#define VLANID_MAX 4094
@@ -180,8 +181,48 @@ static int netdev_create_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userda
return 1;
}
+#define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
+
+static int netdev_get_mac(NetDev *netdev, struct ether_addr *mac) {
+ uint8_t result[8];
+ size_t l, sz;
+ uint8_t *v;
+ int r;
+
+ assert(netdev);
+ assert(netdev->name);
+ assert(mac);
+
+ l = strlen(netdev->name);
+ sz = sizeof(sd_id128_t) + l;
+ v = alloca(sz);
+
+ /* fetch some persistent data unique to the machine */
+ r = sd_id128_get_machine((sd_id128_t*) v);
+ if (r < 0)
+ return r;
+
+ /* combine with some data unique (on this machine) to this
+ * netdev */
+ memcpy(v + sizeof(sd_id128_t), netdev->name, l);
+
+ /* Let's hash the host machine ID plus the container name. We
+ * use a fixed, but originally randomly created hash key here. */
+ siphash24(result, v, sz, HASH_KEY.bytes);
+
+ assert_cc(ETH_ALEN <= sizeof(result));
+ memcpy(mac->ether_addr_octet, result, ETH_ALEN);
+
+ /* see eth_random_addr in the kernel */
+ mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
+ mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
+
+ return 0;
+}
+
static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+ struct ether_addr mac;
const char *kind;
int r;
@@ -192,6 +233,12 @@ static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t c
assert(netdev->manager);
assert(netdev->manager->rtnl);
+ r = netdev_get_mac(netdev, &mac);
+ if (r < 0) {
+ log_error("Failed to generate predictable MAC address for %s", netdev->name);
+ return r;
+ }
+
r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, 0);
if (r < 0) {
log_error_netdev(netdev,
@@ -218,6 +265,14 @@ static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t c
return r;
}
+ r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, &mac);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Colud not append IFLA_ADDRESS attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
if (r < 0) {
log_error_netdev(netdev,