chiark / gitweb /
bus: fix hello ioctl buffer size calculation
[elogind.git] / src / udev / net / link-config.c
index d930afcc0753295e0b4650f841be0ac4cb86ec0b..e6618b8cbfc99d742c463c9739eaca8074e41b35 100644 (file)
@@ -39,6 +39,7 @@
 #include "hashmap.h"
 #include "rtnl-util.h"
 #include "net-util.h"
+#include "siphash24.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -124,6 +125,7 @@ static void link_configs_free(link_config_ctx *ctx) {
                 free(link->match_driver);
                 free(link->match_type);
                 free(link->description);
+                free(link->alias);
 
                 free(link);
         }
@@ -148,9 +150,12 @@ void link_config_ctx_free(link_config_ctx *ctx) {
 
 static int load_link(link_config_ctx *ctx, const char *filename) {
         link_config *link;
-        FILE *file;
+        _cleanup_fclose_ FILE *file;
         int r;
 
+        assert(ctx);
+        assert(filename);
+
         file = fopen(filename, "re");
         if (!file) {
                 if (errno == ENOENT)
@@ -169,11 +174,10 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
         link->wol = _WOL_INVALID;
         link->duplex = _DUP_INVALID;
 
-
         r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
                          (void*) link_config_gperf_lookup, false, false, link);
         if (r < 0) {
-                log_warning("Colud not parse config file %s: %s", filename, strerror(-r));
+                log_warning("Could not parse config file %s: %s", filename, strerror(-r));
                 goto failure;
         } else
                 log_debug("Parsed configuration file %s", filename);
@@ -195,11 +199,11 @@ static bool enable_name_policy(void) {
         int r;
         size_t l;
 
-        r = read_one_line_file("/proc/cmdline", &line);
-        if (r < 0) {
+        r = proc_cmdline(&line);
+        if (r < 0)
                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
-                return true; /* something is very wrong, let's not make it worse */
-        }
+        if (r <= 0)
+                return true;
 
         FOREACH_WORD_QUOTED(w, l, line, state)
                 if (strneq(w, "net.ifnames=0", l))
@@ -245,9 +249,14 @@ int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_confi
         link_config *link;
 
         LIST_FOREACH(links, link, ctx->links) {
+
                 if (net_match_config(link->match_mac, link->match_path,
-                                     link->match_driver, link->match_type,
-                                     NULL, device)) {
+                                     link->match_driver, link->match_type, NULL,
+                                     udev_device_get_sysattr_value(device, "address"),
+                                     udev_device_get_property_value(device, "ID_PATH"),
+                                     udev_device_get_driver(device),
+                                     udev_device_get_devtype(device),
+                                     NULL)) {
                         log_debug("Config file %s applies to device %s",
                                   link->filename,
                                   udev_device_get_sysname(device));
@@ -293,17 +302,18 @@ static bool mac_is_permanent(struct udev_device *device) {
         return type == 0;
 }
 
+#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
+
 static int get_mac(struct udev_device *device, bool want_random, struct ether_addr *mac) {
-        unsigned int seed;
-        int r, i;
+        int r;
 
         if (want_random)
-                seed = random_u();
+                random_bytes(mac->ether_addr_octet, ETH_ALEN);
         else {
                 const char *name;
-                sd_id128_t machine;
-                char machineid_buf[33];
-                const char *seed_str;
+                uint8_t result[8];
+                size_t l, sz;
+                uint8_t *v;
 
                 /* fetch some persistent data unique (on this machine) to this device */
                 name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
@@ -315,22 +325,24 @@ static int get_mac(struct udev_device *device, bool want_random, struct ether_ad
                                         return -ENOENT;
                         }
                 }
+
+                l = strlen(name);
+                sz = sizeof(sd_id128_t) + l;
+                v = alloca(sz);
+
                 /* fetch some persistent data unique to this machine */
-                r = sd_id128_get_machine(&machine);
+                r = sd_id128_get_machine((sd_id128_t*) v);
                 if (r < 0)
                         return r;
+                memcpy(v + sizeof(sd_id128_t), name, l);
 
-                /* combine the data */
-                seed_str = strappenda(name, sd_id128_to_string(machine, machineid_buf));
+                /* Let's hash the machine ID plus the device name. We
+                 * use a fixed, but originally randomly created hash
+                 * key here. */
+                siphash24(result, v, sz, HASH_KEY.bytes);
 
-                /* hash to get seed */
-                seed = string_hash_func(seed_str);
-        }
-
-        srandom(seed);
-
-        for(i = 0; i < ETH_ALEN; i++) {
-                mac->ether_addr_octet[i] = random();
+                assert_cc(ETH_ALEN <= sizeof(result));
+                memcpy(mac->ether_addr_octet, result, ETH_ALEN);
         }
 
         /* see eth_random_addr in the kernel */
@@ -360,14 +372,6 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
         if (!old_name)
                 return -EINVAL;
 
-        if (config->description) {
-                r = udev_device_set_sysattr_value(device, "ifalias",
-                                                  config->description);
-                if (r < 0)
-                        log_warning("Could not set description of %s to '%s': %s",
-                                    old_name, config->description, strerror(-r));
-        }
-
         r = ethtool_set_speed(ctx->ethtool_fd, old_name, config->speed, config->duplex);
         if (r < 0)
                 log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
@@ -435,9 +439,9 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                         mac = config->mac;
         }
 
-        r = rtnl_set_link_properties(ctx->rtnl, ifindex, mac, config->mtu);
+        r = rtnl_set_link_properties(ctx->rtnl, ifindex, config->alias, mac, config->mtu);
         if (r < 0) {
-                log_warning("Could not set MACAddress or MTU on %s: %s", old_name, strerror(-r));
+                log_warning("Could not set Alias, MACAddress or MTU on %s: %s", old_name, strerror(-r));
                 return r;
         }