[systemd-commits] 4 commits - links/99-default.link Makefile.am network/99-default.link rules/85-net-configure-link.rules shell-completion/bash shell-completion/zsh src/shared src/udev

Tom Gundersen tomegun at kemper.freedesktop.org
Tue Oct 29 14:34:54 CET 2013


 Makefile.am                            |   13 +-
 links/99-default.link                  |    3 
 network/99-default.link                |    3 
 rules/85-net-configure-link.rules      |    8 -
 shell-completion/bash/udevadm          |    2 
 shell-completion/zsh/_udevadm          |    2 
 src/shared/conf-parser.c               |    1 
 src/shared/conf-parser.h               |   61 +++++++++++++
 src/udev/net/ethtool-util.c            |   75 +++++++++++-----
 src/udev/net/ethtool-util.h            |   34 ++++++-
 src/udev/net/link-config-gperf.gperf   |   15 +--
 src/udev/net/link-config-parse.c       |  128 +++++++++++++++++++++++++++
 src/udev/net/link-config.c             |  152 ++++++++++++---------------------
 src/udev/net/link-config.h             |   42 +++++++--
 src/udev/udev-builtin-net_link.c       |   96 --------------------
 src/udev/udev-builtin-net_setup_link.c |   96 ++++++++++++++++++++
 src/udev/udev-builtin.c                |    2 
 src/udev/udev.h                        |    2 
 18 files changed, 488 insertions(+), 247 deletions(-)

New commits:
commit 5fde13d748749f0e06e2e6cdd15f0980a79ea82c
Author: Tom Gundersen <teg at jklm.no>
Date:   Mon Oct 28 20:59:56 2013 +0100

    udev: link-config - add proper parsing

diff --git a/Makefile.am b/Makefile.am
index 2f6ba21..a06a79b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2317,6 +2317,7 @@ libudev_core_la_SOURCES = \
 	src/udev/udev-builtin-usb_id.c \
 	src/udev/net/link-config.h \
 	src/udev/net/link-config.c \
+	src/udev/net/link-config-parse.c \
 	src/udev/net/ethtool-util.h \
 	src/udev/net/ethtool-util.c
 
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index 6085d33..16c3c71 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -24,6 +24,7 @@
 #include <errno.h>
 #include <assert.h>
 #include <stdlib.h>
+#include <netinet/ether.h>
 
 #include "conf-parser.h"
 #include "util.h"
diff --git a/src/udev/net/ethtool-util.c b/src/udev/net/ethtool-util.c
index 0a4118c..4fad52b 100644
--- a/src/udev/net/ethtool-util.c
+++ b/src/udev/net/ethtool-util.c
@@ -29,6 +29,24 @@
 #include "strxcpyx.h"
 #include "util.h"
 #include "log.h"
+#include "conf-parser.h"
+
+static const char* const duplex_table[] = {
+        [DUP_FULL] = "full",
+        [DUP_HALF] = "half"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(duplex, Duplex);
+DEFINE_CONFIG_PARSE_ENUM(config_parse_duplex, duplex, Duplex, "Failed to parse duplex setting");
+
+static const char* const wol_table[] = {
+        [WOL_PHY] = "phy",
+        [WOL_MAGIC] = "magic",
+        [WOL_OFF] = "off"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(wol, WakeOnLan);
+DEFINE_CONFIG_PARSE_ENUM(config_parse_wol, wol, WakeOnLan, "Failed to parse WakeOnLan setting");
 
 int ethtool_connect(int *ret) {
         int fd;
@@ -45,14 +63,14 @@ int ethtool_connect(int *ret) {
         return 0;
 }
 
-int ethtool_set_speed(int fd, const char *ifname, const unsigned int speed, const char *duplex)
+int ethtool_set_speed(int fd, const char *ifname, unsigned int speed, Duplex duplex)
 {
         struct ifreq ifr;
         struct ethtool_cmd ecmd;
         bool need_update;
         int r;
 
-        if (speed == 0 && !duplex)
+        if (speed == 0 && duplex == _DUP_INVALID)
                 return 0;
 
         memset(&ecmd, 0x00, sizeof(struct ethtool_cmd));
@@ -70,17 +88,21 @@ int ethtool_set_speed(int fd, const char *ifname, const unsigned int speed, cons
                 need_update = true;
         }
 
-        if (duplex) {
-                if (streq(duplex, "half")) {
+        switch (duplex) {
+                case DUP_HALF:
                         if (ecmd.duplex != DUPLEX_HALF) {
                                 ecmd.duplex = DUPLEX_HALF;
                                 need_update = true;
                         }
-                } else if (streq(duplex, "full"))
+                        break;
+                case DUP_FULL:
                         if (ecmd.duplex != DUPLEX_FULL) {
                                 ecmd.duplex = DUPLEX_FULL;
                                 need_update = true;
                         }
+                        break;
+                default:
+                        break;
         }
 
         if (need_update) {
@@ -94,13 +116,13 @@ int ethtool_set_speed(int fd, const char *ifname, const unsigned int speed, cons
         return 0;
 }
 
-int ethtool_set_wol(int fd, const char *ifname, const char *wol) {
+int ethtool_set_wol(int fd, const char *ifname, WakeOnLan wol) {
         struct ifreq ifr;
         struct ethtool_wolinfo ecmd;
         bool need_update;
         int r;
 
-        if (!wol)
+        if (wol == _WOL_INVALID)
                 return 0;
 
         memset(&ecmd, 0x00, sizeof(struct ethtool_wolinfo));
@@ -113,23 +135,28 @@ int ethtool_set_wol(int fd, const char *ifname, const char *wol) {
         if (r < 0)
                 return -errno;
 
-        if (streq(wol, "phy")) {
-                if (ecmd.wolopts != WAKE_PHY) {
-                        ecmd.wolopts = WAKE_PHY;
-                        need_update = true;
-                }
-        } else if (streq(wol, "magic")) {
-                if (ecmd.wolopts != WAKE_MAGIC) {
-                        ecmd.wolopts = WAKE_MAGIC;
-                        need_update = true;
-                }
-        } else if (streq(wol, "off")) {
-                if (ecmd.wolopts != 0) {
-                        ecmd.wolopts = 0;
-                        need_update = true;
-                }
-        } else
-                return -EINVAL;
+        switch (wol) {
+                case WOL_PHY:
+                        if (ecmd.wolopts != WAKE_PHY) {
+                                ecmd.wolopts = WAKE_PHY;
+                                need_update = true;
+                        }
+                        break;
+                case WOL_MAGIC:
+                        if (ecmd.wolopts != WAKE_MAGIC) {
+                                ecmd.wolopts = WAKE_MAGIC;
+                                need_update = true;
+                        }
+                        break;
+                case WOL_OFF:
+                        if (ecmd.wolopts != 0) {
+                                ecmd.wolopts = 0;
+                                need_update = true;
+                        }
+                        break;
+                default:
+                        break;
+        }
 
         if (need_update) {
                 ecmd.cmd = ETHTOOL_SWOL;
diff --git a/src/udev/net/ethtool-util.h b/src/udev/net/ethtool-util.h
index 74bbada..a02088f 100644
--- a/src/udev/net/ethtool-util.h
+++ b/src/udev/net/ethtool-util.h
@@ -19,7 +19,37 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#pragma once
+
+#include <macro.h>
+
+/* we can't use DUPLEX_ prefix, as it
+ * clashes with <linux/ethtool.h> */
+typedef enum Duplex {
+        DUP_FULL,
+        DUP_HALF,
+        _DUP_MAX,
+        _DUP_INVALID = -1
+} Duplex;
+
+typedef enum WakeOnLan {
+        WOL_PHY,
+        WOL_MAGIC,
+        WOL_OFF,
+        _WOL_MAX,
+        _WOL_INVALID = -1
+} WakeOnLan;
+
 int ethtool_connect(int *ret);
 
-int ethtool_set_speed(int fd, const char *ifname, const unsigned int speed, const char *duplex);
-int ethtool_set_wol(int fd, const char *ifname, const char *wol);
+int ethtool_set_speed(int fd, const char *ifname, unsigned int speed, Duplex duplex);
+int ethtool_set_wol(int fd, const char *ifname, WakeOnLan wol);
+
+const char *duplex_to_string(Duplex d) _const_;
+Duplex duplex_from_string(const char *d) _pure_;
+
+const char *wol_to_string(WakeOnLan wol) _const_;
+WakeOnLan wol_from_string(const char *wol) _pure_;
+
+int config_parse_duplex(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_wol(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf
index 1d90531..130ebc9 100644
--- a/src/udev/net/link-config-gperf.gperf
+++ b/src/udev/net/link-config-gperf.gperf
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include "conf-parser.h"
 #include "link-config.h"
+#include "ethtool-util.h"
 %}
 struct ConfigPerfItem;
 %null_strings
@@ -14,16 +15,16 @@ struct ConfigPerfItem;
 %struct-type
 %includes
 %%
-Match.MACAddress,                   config_parse_string,        0, offsetof(link_config, match_mac)
+Match.MACAddress,                   config_parse_hwaddr,        0, offsetof(link_config, match_mac)
 Match.Path,                         config_parse_string,        0, offsetof(link_config, match_path)
 Match.Driver,                       config_parse_string,        0, offsetof(link_config, match_driver)
 Match.Type,                         config_parse_string,        0, offsetof(link_config, match_type)
 Link.Description,                   config_parse_string,        0, offsetof(link_config, description)
-Link.MACAddressPolicy,              config_parse_string,        0, offsetof(link_config, mac_policy)
-Link.MACAddress,                    config_parse_string,        0, offsetof(link_config, mac)
-Link.NamePolicy,                    config_parse_strv,          0, offsetof(link_config, name_policy)
-Link.Name,                          config_parse_string,        0, offsetof(link_config, name)
+Link.MACAddressPolicy,              config_parse_mac_policy,    0, offsetof(link_config, mac_policy)
+Link.MACAddress,                    config_parse_hwaddr,        0, offsetof(link_config, mac)
+Link.NamePolicy,                    config_parse_name_policy,   0, offsetof(link_config, name_policy)
+Link.Name,                          config_parse_ifname,        0, offsetof(link_config, name)
 Link.MTU,                           config_parse_unsigned,      0, offsetof(link_config, mtu)
 Link.SpeedMBytes,                   config_parse_unsigned,      0, offsetof(link_config, speed)
-Link.Duplex,                        config_parse_string,        0, offsetof(link_config, duplex)
-Link.WakeOnLan,                     config_parse_string,        0, offsetof(link_config, wol)
+Link.Duplex,                        config_parse_duplex,        0, offsetof(link_config, duplex)
+Link.WakeOnLan,                     config_parse_wol,           0, offsetof(link_config, wol)
diff --git a/src/udev/net/link-config-parse.c b/src/udev/net/link-config-parse.c
new file mode 100644
index 0000000..54dd57b
--- /dev/null
+++ b/src/udev/net/link-config-parse.c
@@ -0,0 +1,128 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright (C) 2013 Tom Gundersen <teg at jklm.no>
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <netinet/ether.h>
+#include <net/if.h>
+
+#include "link-config.h"
+
+#include "utf8.h"
+#include "conf-parser.h"
+
+static const char* const mac_policy_table[] = {
+        [MACPOLICY_PERSISTENT] = "persistent",
+        [MACPOLICY_RANDOM] = "random"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
+DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy, "Failed to parse MAC address policy");
+
+static const char* const name_policy_table[] = {
+        [NAMEPOLICY_ONBOARD] = "onboard",
+        [NAMEPOLICY_SLOT] = "slot",
+        [NAMEPOLICY_PATH] = "path",
+        [NAMEPOLICY_MAC] = "mac"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
+DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy");
+
+int config_parse_ifname(const char *unit,
+                        const char *filename,
+                        unsigned line,
+                        const char *section,
+                        const char *lvalue,
+                        int ltype,
+                        const char *rvalue,
+                        void *data,
+                        void *userdata) {
+
+        char **s = data;
+        char *n;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        n = strdup(rvalue);
+        if (!n)
+                return log_oom();
+
+        if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
+                free(n);
+                return 0;
+        }
+
+        free(*s);
+        if (*n)
+                *s = n;
+        else {
+                free(n);
+                *s = NULL;
+        }
+
+        return 0;
+}
+
+int config_parse_hwaddr(const char *unit,
+                        const char *filename,
+                        unsigned line,
+                        const char *section,
+                        const char *lvalue,
+                        int ltype,
+                        const char *rvalue,
+                        void *data,
+                        void *userdata) {
+        struct ether_addr **hwaddr = data;
+        struct ether_addr *n;
+        int r;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        n = calloc(1, sizeof(struct ether_addr));
+        if (!n)
+                return log_oom();
+
+        r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+                   &n->ether_addr_octet[0],
+                   &n->ether_addr_octet[1],
+                   &n->ether_addr_octet[2],
+                   &n->ether_addr_octet[3],
+                   &n->ether_addr_octet[4],
+                   &n->ether_addr_octet[5]);
+        if (r != 6) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Not a valid MAC address, ignoring assignment: %s", rvalue);
+                free(n);
+                return 0;
+        }
+
+        free(*hwaddr);
+        *hwaddr = n;
+
+        return 0;
+}
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index 0a86d0e..0b5916b 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -153,6 +153,11 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
                 goto failure;
         }
 
+        link->mac_policy = _MACPOLICY_INVALID;
+        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) {
@@ -205,32 +210,36 @@ static bool match_config(link_config *match, struct udev_device *device) {
 
         if (match->match_mac) {
                 property = udev_device_get_sysattr_value(device, "address");
-                if (!property || !streq(match->match_mac, property)) {
-                        log_debug("Device MAC address (%s) did not match MACAddress=%s", property, match->match_mac);
+                if (!property || memcmp(match->match_mac, ether_aton(property), ETH_ALEN)) {
+                        log_debug("Device MAC address (%s) did not match MACAddress=%s",
+                                  property, ether_ntoa(match->match_mac));
                         return 0;
                 }
         }
 
         if (match->match_path) {
                 property = udev_device_get_property_value(device, "ID_PATH");
-                if (!property || !streq(match->match_path, property)) {
-                        log_debug("Device's persistent path (%s) did not match Path=%s", property, match->match_path);
+                if (!streq_ptr(match->match_path, property)) {
+                        log_debug("Device's persistent path (%s) did not match Path=%s",
+                                  property, match->match_path);
                         return 0;
                 }
         }
 
         if (match->match_driver) {
                 property = udev_device_get_driver(device);
-                if (!property || !streq(match->match_driver, property)) {
-                        log_debug("Device driver (%s) did not match Driver=%s", property, match->match_driver);
+                if (!streq_ptr(match->match_driver, property)) {
+                        log_debug("Device driver (%s) did not match Driver=%s",
+                                  property, match->match_driver);
                         return 0;
                 }
         }
 
         if (match->match_type) {
                 property = udev_device_get_devtype(device);
-                if (!property || !streq(match->match_type, property)) {
-                        log_debug("Device type (%s) did not match Type=%s", property, match->match_type);
+                if (!streq_ptr(match->match_type, property)) {
+                        log_debug("Device type (%s) did not match Type=%s",
+                                  property, match->match_type);
                         return 0;
                 }
         }
@@ -344,15 +353,10 @@ static bool mac_is_permanent(struct udev_device *device) {
         return type == 0;
 }
 
-static int get_mac(struct udev_device *device, bool want_random, struct ether_addr **ret) {
-        struct ether_addr *mac;
+static int get_mac(struct udev_device *device, bool want_random, struct ether_addr *mac) {
         unsigned int seed;
         int r, i;
 
-        mac = calloc(1, sizeof(struct ether_addr));
-        if (!mac)
-                return -ENOMEM;
-
         if (want_random)
                 seed = random_u();
         else {
@@ -393,14 +397,13 @@ static int get_mac(struct udev_device *device, bool want_random, struct ether_ad
         mac->ether_addr_octet[0] &= 0xfe;        /* clear multicast bit */
         mac->ether_addr_octet[0] |= 0x02;        /* set local assignment bit (IEEE802) */
 
-        *ret = mac;
-
         return 0;
 }
 
 int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
         const char *name;
-        char *new_name = NULL;
+        const char *new_name = NULL;
+        struct ether_addr generated_mac;
         struct ether_addr *mac = NULL;
         int r, ifindex;
 
@@ -416,30 +419,17 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                 if (r < 0)
                         log_warning("Could not set description of %s to '%s': %s",
                                     name, config->description, strerror(-r));
-                else
-                        log_info("Set link description of %s to '%s'", name,
-                                 config->description);
         }
 
-        if (config->speed || config->duplex) {
-                r = ethtool_set_speed(ctx->ethtool_fd, name,
-                                      config->speed, config->duplex);
-                if (r < 0)
-                        log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
-                                    name, config->speed, config->duplex, strerror(-r));
-                else
-                        log_info("Set speed or duplex of %s to %u Mbytes (%s)", name,
-                                 config->speed, config->duplex);
-        }
+        r = ethtool_set_speed(ctx->ethtool_fd, name, config->speed, config->duplex);
+        if (r < 0)
+                log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
+                             name, config->speed, duplex_to_string(config->duplex), strerror(-r));
 
-        if (config->wol) {
-                r = ethtool_set_wol(ctx->ethtool_fd, name, config->wol);
-                if (r < 0)
-                        log_warning("Could not set WakeOnLan of %s to %s: %s",
-                                    name, config->wol, strerror(-r));
-                else
-                        log_info("Set WakeOnLan of %s to %s", name, config->wol);
-        }
+        r = ethtool_set_wol(ctx->ethtool_fd, name, config->wol);
+        if (r < 0)
+                log_warning("Could not set WakeOnLan of %s to %s: %s",
+                            name, wol_to_string(config->wol), strerror(-r));
 
         ifindex = udev_device_get_ifindex(device);
         if (ifindex <= 0) {
@@ -448,84 +438,58 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
         }
 
         if (config->name_policy && enable_name_policy()) {
-                char **policy;
+                NamePolicy *policy;
 
-                STRV_FOREACH(policy, config->name_policy) {
-                        if (streq(*policy, "onboard")) {
-                                r = strdup_or_null(udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD"), &new_name);
-                                if (r < 0)
-                                        return r;
-                                if (new_name)
+                for (policy = config->name_policy; !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
+                        switch (*policy) {
+                                case NAMEPOLICY_ONBOARD:
+                                        new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
                                         break;
-                        } else if (streq(*policy, "slot")) {
-                                r = strdup_or_null(udev_device_get_property_value(device, "ID_NET_NAME_SLOT"), &new_name);
-                                if (r < 0)
-                                        return r;
-                                if (new_name)
+                                case NAMEPOLICY_SLOT:
+                                        new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
                                         break;
-                        } else if (streq(*policy, "path")) {
-                                r = strdup_or_null(udev_device_get_property_value(device, "ID_NET_NAME_PATH"), &new_name);
-                                if (r < 0)
-                                        return r;
-                                if (new_name)
+                                case NAMEPOLICY_PATH:
+                                        new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
                                         break;
-                        } else if (streq(*policy, "mac")) {
-                                r = strdup_or_null(udev_device_get_property_value(device, "ID_NET_NAME_MAC"), &new_name);
-                                if (r < 0)
-                                        return r;
-                                if (new_name)
+                                case NAMEPOLICY_MAC:
+                                        new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
                                         break;
-                        } else
-                                log_warning("Invalid link naming policy '%s', ignoring.", *policy);
+                                default:
+                                        break;
+                        }
                 }
         }
 
         if (!new_name && config->name) {
-                new_name = calloc(1, IFNAMSIZ);
-                strscpy(new_name, IFNAMSIZ, config->name);
+                new_name = config->name;
         }
 
-        if (config->mac_policy) {
-                if (streq(config->mac_policy, "persistent")) {
+        switch (config->mac_policy) {
+                case MACPOLICY_PERSISTENT:
                         if (!mac_is_permanent(device)) {
-                                r = get_mac(device, false, &mac);
+                                r = get_mac(device, false, &generated_mac);
                                 if (r < 0)
                                         return r;
+                                mac = &generated_mac;
                         }
-                } else if (streq(config->mac_policy, "random")) {
+                        break;
+                case MACPOLICY_RANDOM:
                         if (!mac_is_random(device)) {
-                                r = get_mac(device, true, &mac);
+                                r = get_mac(device, true, &generated_mac);
                                 if (r < 0)
                                         return r;
+                                mac = &generated_mac;
                         }
-                } else
-                        log_warning("Invalid MACAddress policy '%s', ignoring.", config->mac_policy);
-        }
-
-        if (!mac && config->mac) {
-                mac = calloc(1, sizeof(struct ether_addr));
-                r = sscanf(config->mac, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
-                           &mac->ether_addr_octet[0],
-                           &mac->ether_addr_octet[1],
-                           &mac->ether_addr_octet[2],
-                           &mac->ether_addr_octet[3],
-                           &mac->ether_addr_octet[4],
-                           &mac->ether_addr_octet[5]);
-                if (r != 6) {
-                        r = -EINVAL;
-                        goto out;
-                }
+                        break;
+                default:
+                        mac = config->mac;
         }
 
         r = rtnl_set_properties(ctx->rtnl, ifindex, new_name, mac, config->mtu);
         if (r < 0) {
                 log_warning("Could not set Name, MACAddress or MTU on %s: %s", name, strerror(-r));
-                goto out;
+                return r;
         }
 
         return 0;
-out:
-        free(new_name);
-        free(mac);
-        return r;
 }
diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h
index d0a81f8..f5dee5a 100644
--- a/src/udev/net/link-config.h
+++ b/src/udev/net/link-config.h
@@ -21,31 +21,48 @@
 
 #pragma once
 
+#include "ethtool-util.h"
+
 #include "libudev.h"
 #include "util.h"
 #include "list.h"
 
-
 typedef struct link_config_ctx link_config_ctx;
 typedef struct link_config link_config;
 
+typedef enum MACPolicy {
+        MACPOLICY_PERSISTENT,
+        MACPOLICY_RANDOM,
+        _MACPOLICY_MAX,
+        _MACPOLICY_INVALID = -1
+} MACPolicy;
+
+typedef enum NamePolicy {
+        NAMEPOLICY_ONBOARD,
+        NAMEPOLICY_SLOT,
+        NAMEPOLICY_PATH,
+        NAMEPOLICY_MAC,
+        _NAMEPOLICY_MAX,
+        _NAMEPOLICY_INVALID = -1
+} NamePolicy;
+
 struct link_config {
         char *filename;
 
-        char *match_mac;
+        struct ether_addr *match_mac;
         char *match_path;
         char *match_driver;
         char *match_type;
 
         char *description;
-        char *mac;
-        char *mac_policy;
-        char **name_policy;
+        struct ether_addr *mac;
+        MACPolicy mac_policy;
+        NamePolicy *name_policy;
         char *name;
         unsigned int mtu;
         unsigned int speed;
-        char *duplex;
-        char *wol;
+        Duplex duplex;
+        WakeOnLan wol;
 
         LIST_FIELDS(link_config, links);
 };
@@ -59,5 +76,16 @@ bool link_config_should_reload(link_config_ctx *ctx);
 int link_config_get(link_config_ctx *ctx, struct udev_device *device, struct link_config **ret);
 int link_config_apply(link_config_ctx *ctx, struct link_config *config, struct udev_device *device);
 
+const char *name_policy_to_string(NamePolicy p) _const_;
+NamePolicy name_policy_from_string(const char *p) _pure_;
+
+const char *mac_policy_to_string(MACPolicy p) _const_;
+MACPolicy mac_policy_from_string(const char *p) _pure_;
+
 /* gperf lookup function */
 const struct ConfigPerfItem* link_config_gperf_lookup(const char *key, unsigned length);
+
+int config_parse_hwaddr(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_ifname(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_mac_policy(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_name_policy(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);

commit 916484f54d084e11a11458716b2e0bbdf4822d40
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Oct 29 13:03:13 2013 +0100

    conf-parser: add macro for ENUMV
    
    Parses a whitespace separated list of strings into a vector of enums.

diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
index 9435d54..f988e1c 100644
--- a/src/shared/conf-parser.h
+++ b/src/shared/conf-parser.h
@@ -149,3 +149,64 @@ int log_syntax_internal(const char *unit, int level,
                 *i = x;                                                 \
                 return 0;                                               \
         }
+
+#define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg)              \
+        int function(const char *unit,                                         \
+                     const char *filename,                                     \
+                     unsigned line,                                            \
+                     const char *section,                                      \
+                     const char *lvalue,                                       \
+                     int ltype,                                                \
+                     const char *rvalue,                                       \
+                     void *data,                                               \
+                     void *userdata) {                                         \
+                                                                               \
+                type **enums = data, *xs, x, *ys;                              \
+                char *w, *state;                                               \
+                size_t l, i = 0;                                               \
+                                                                               \
+                assert(filename);                                              \
+                assert(lvalue);                                                \
+                assert(rvalue);                                                \
+                assert(data);                                                  \
+                                                                               \
+                xs = new0(type, 1);                                            \
+                *xs = invalid;                                                 \
+                                                                               \
+                FOREACH_WORD(w, l, rvalue, state) {                            \
+                        _cleanup_free_ char *en = NULL;                        \
+                                                                               \
+                        en = strndup(w, l);                                    \
+                        if (!en)                                               \
+                                return -ENOMEM;                                \
+                                                                               \
+                        if ((x = name##_from_string(en)) < 0) {                \
+                                log_syntax(unit, LOG_ERR, filename, line,      \
+                                       -x, msg ", ignoring: %s", en);          \
+                                continue;                                      \
+                        }                                                      \
+                                                                               \
+                        for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
+                                if (*ys == x) {                                \
+                                        log_syntax(unit, LOG_ERR, filename,    \
+                                              line, -x,                        \
+                                              "Duplicate entry, ignoring: %s", \
+                                              en);                             \
+                                        x = invalid;                           \
+                                }                                              \
+                        }                                                      \
+                                                                               \
+                        if (x == invalid)                                      \
+                                continue;                                      \
+                                                                               \
+                        *(xs + i) = x;                                         \
+                        xs = realloc(xs, ++i + 1);                             \
+                        if (!xs)                                               \
+                                return -ENOMEM;                                \
+                        *(xs + i) = invalid;                                   \
+                }                                                              \
+                                                                               \
+                free(*enums);                                                  \
+                *enums = xs;                                                   \
+                return 0;                                                      \
+        }

commit 0b99c9f8f0cbfe9ab3de443cb6f94ecd7d21eae3
Author: Tom Gundersen <teg at jklm.no>
Date:   Mon Oct 28 20:20:59 2013 +0100

    udev: builtin - rename net_link to net_setup_link
    
    Also add shell completions.

diff --git a/Makefile.am b/Makefile.am
index bf5459a..2f6ba21 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2312,7 +2312,7 @@ libudev_core_la_SOURCES = \
 	src/udev/udev-builtin-input_id.c \
 	src/udev/udev-builtin-keyboard.c \
 	src/udev/udev-builtin-net_id.c \
-	src/udev/udev-builtin-net_link.c \
+	src/udev/udev-builtin-net_setup_link.c \
 	src/udev/udev-builtin-path_id.c \
 	src/udev/udev-builtin-usb_id.c \
 	src/udev/net/link-config.h \
diff --git a/rules/85-net-configure-link.rules b/rules/85-net-configure-link.rules
index 29d6893..d0ecb93 100644
--- a/rules/85-net-configure-link.rules
+++ b/rules/85-net-configure-link.rules
@@ -1,11 +1,11 @@
 # do not edit this file, it will be overwritten on update
 
-SUBSYSTEM!="net", GOTO="net_link_end"
+SUBSYSTEM!="net", GOTO="net_setup_link_end"
 
 IMPORT{builtin}="path_id"
 
-ACTION!="add", GOTO="net_link_end"
+ACTION!="add", GOTO="net_setup_link_end"
 
-RUN{builtin}="net_link"
+RUN{builtin}="net_setup_link"
 
-LABEL="net_link_end"
+LABEL="net_setup_link_end"
diff --git a/shell-completion/bash/udevadm b/shell-completion/bash/udevadm
index 8ad8550..d58cdf5 100644
--- a/shell-completion/bash/udevadm
+++ b/shell-completion/bash/udevadm
@@ -83,7 +83,7 @@ _udevadm() {
                         fi
                         ;;
                 'test-builtin')
-                        comps='blkid btrfs hwdb input_id keyboard kmod net_id path_id usb_id uaccess'
+                        comps='blkid btrfs hwdb input_id keyboard kmod net_id net_setup_link path_id usb_id uaccess'
                         ;;
                 *)
                         comps=${VERBS[*]}
diff --git a/shell-completion/zsh/_udevadm b/shell-completion/zsh/_udevadm
index 04e9f8d..e5d252c 100644
--- a/shell-completion/zsh/_udevadm
+++ b/shell-completion/zsh/_udevadm
@@ -75,7 +75,7 @@ _udevadm_test-builtin(){
     if (( CURRENT == 2 )); then
     _arguments \
         '--help[Print help text]' \
-        '*::builtins:(blkid btrfs hwdb input_id kmod path_id usb_id uaccess)'
+        '*::builtins:(blkid btrfs hwdb input_id net_id net_setup_link kmod path_id usb_id uaccess)'
     elif  (( CURRENT == 3 )); then
         _arguments \
             '--help[Print help text]' \
diff --git a/src/udev/udev-builtin-net_link.c b/src/udev/udev-builtin-net_link.c
deleted file mode 100644
index f4fb63e..0000000
--- a/src/udev/udev-builtin-net_link.c
+++ /dev/null
@@ -1,96 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2013 Tom Gundersen
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include "link-config.h"
-#include "udev.h"
-#include "log.h"
-
-link_config_ctx *ctx;
-
-static int builtin_net_link(struct udev_device *dev, int argc, char **argv, bool test) {
-        link_config *link;
-        int r;
-
-        if (argc > 1) {
-                log_error("This program takes no arguments.");
-                return EXIT_FAILURE;
-        }
-
-        r = link_config_get(ctx, dev, &link);
-        if (r < 0) {
-                if (r == -ENOENT) {
-                        log_debug("No matching link configuration found");
-                        return EXIT_SUCCESS;
-                } else {
-                        log_error("Could not get link config");
-                        return EXIT_FAILURE;
-                }
-        }
-
-        r = link_config_apply(ctx, link, dev);
-        if (r < 0) {
-                log_error("Could not apply link config to %s", udev_device_get_sysname(dev));
-                return EXIT_FAILURE;
-        }
-
-        return EXIT_SUCCESS;
-}
-
-static int builtin_net_link_init(struct udev *udev) {
-        int r;
-
-        if (ctx)
-                return 0;
-
-        r = link_config_ctx_new(&ctx);
-        if (r < 0)
-                return r;
-
-        r = link_config_load(ctx);
-        if (r < 0)
-                return r;
-
-        log_debug("Created link configuration context");
-        return 0;
-}
-
-static void builtin_net_link_exit(struct udev *udev) {
-        link_config_ctx_free(ctx);
-        log_debug("Unloaded link configuration context");
-}
-
-static bool builtin_net_link_validate(struct udev *udev) {
-        log_debug("Check if link configuration needs reloading");
-        if (!ctx)
-                return false;
-
-        return link_config_should_reload(ctx);
-}
-
-const struct udev_builtin udev_builtin_net_link = {
-        .name = "net_link",
-        .cmd = builtin_net_link,
-        .init = builtin_net_link_init,
-        .exit = builtin_net_link_exit,
-        .validate = builtin_net_link_validate,
-        .help = "configure network link",
-        .run_once = false,
-};
diff --git a/src/udev/udev-builtin-net_setup_link.c b/src/udev/udev-builtin-net_setup_link.c
new file mode 100644
index 0000000..739221b
--- /dev/null
+++ b/src/udev/udev-builtin-net_setup_link.c
@@ -0,0 +1,96 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Tom Gundersen
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "link-config.h"
+#include "udev.h"
+#include "log.h"
+
+link_config_ctx *ctx;
+
+static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
+        link_config *link;
+        int r;
+
+        if (argc > 1) {
+                log_error("This program takes no arguments.");
+                return EXIT_FAILURE;
+        }
+
+        r = link_config_get(ctx, dev, &link);
+        if (r < 0) {
+                if (r == -ENOENT) {
+                        log_debug("No matching link configuration found");
+                        return EXIT_SUCCESS;
+                } else {
+                        log_error("Could not get link config");
+                        return EXIT_FAILURE;
+                }
+        }
+
+        r = link_config_apply(ctx, link, dev);
+        if (r < 0) {
+                log_error("Could not apply link config to %s", udev_device_get_sysname(dev));
+                return EXIT_FAILURE;
+        }
+
+        return EXIT_SUCCESS;
+}
+
+static int builtin_net_setup_link_init(struct udev *udev) {
+        int r;
+
+        if (ctx)
+                return 0;
+
+        r = link_config_ctx_new(&ctx);
+        if (r < 0)
+                return r;
+
+        r = link_config_load(ctx);
+        if (r < 0)
+                return r;
+
+        log_debug("Created link configuration context");
+        return 0;
+}
+
+static void builtin_net_setup_link_exit(struct udev *udev) {
+        link_config_ctx_free(ctx);
+        log_debug("Unloaded link configuration context");
+}
+
+static bool builtin_net_setup_link_validate(struct udev *udev) {
+        log_debug("Check if link configuration needs reloading");
+        if (!ctx)
+                return false;
+
+        return link_config_should_reload(ctx);
+}
+
+const struct udev_builtin udev_builtin_net_setup_link = {
+        .name = "net_setup_link",
+        .cmd = builtin_net_setup_link,
+        .init = builtin_net_setup_link_init,
+        .exit = builtin_net_setup_link_exit,
+        .validate = builtin_net_setup_link_validate,
+        .help = "configure network link",
+        .run_once = false,
+};
diff --git a/src/udev/udev-builtin.c b/src/udev/udev-builtin.c
index 85901e8..fd373d0 100644
--- a/src/udev/udev-builtin.c
+++ b/src/udev/udev-builtin.c
@@ -44,7 +44,7 @@ static const struct udev_builtin *builtins[] = {
         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
 #endif
         [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id,
-        [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_link,
+        [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link,
         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
 #ifdef HAVE_ACL
diff --git a/src/udev/udev.h b/src/udev/udev.h
index 7cca8b8..46235b1 100644
--- a/src/udev/udev.h
+++ b/src/udev/udev.h
@@ -184,7 +184,7 @@ extern const struct udev_builtin udev_builtin_keyboard;
 extern const struct udev_builtin udev_builtin_kmod;
 #endif
 extern const struct udev_builtin udev_builtin_net_id;
-extern const struct udev_builtin udev_builtin_net_link;
+extern const struct udev_builtin udev_builtin_net_setup_link;
 extern const struct udev_builtin udev_builtin_path_id;
 extern const struct udev_builtin udev_builtin_usb_id;
 extern const struct udev_builtin udev_builtin_uaccess;

commit 9dc670ea766c711741f462b29572f2e5f8f3f6bc
Author: Tom Gundersen <teg at jklm.no>
Date:   Mon Oct 28 20:11:10 2013 +0100

    network: move configuration to /etc/systemd/network
    
    This is private configuraiton, so let's not pollute the namespace (and hence make Debian happy :) ).

diff --git a/Makefile.am b/Makefile.am
index bb18217..bf5459a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -83,7 +83,7 @@ userunitdir=$(prefix)/lib/systemd/user
 userpresetdir=$(prefix)/lib/systemd/user-preset
 tmpfilesdir=$(prefix)/lib/tmpfiles.d
 sysctldir=$(prefix)/lib/sysctl.d
-linksdir=$(prefix)/lib/net/links
+networkdir=$(prefix)/lib/systemd/network
 pkgincludedir=$(includedir)/systemd
 systemgeneratordir=$(rootlibexecdir)/system-generators
 usergeneratordir=$(prefix)/lib/systemd/user-generators
@@ -2214,8 +2214,8 @@ INSTALL_DIRS += \
 	$(sysconfdir)/udev/rules.d \
 	$(sysconfdir)/udev/hwdb.d
 
-dist_links_DATA = \
-	links/99-default.link
+dist_network_DATA = \
+	network/99-default.link
 
 dist_udevrules_DATA += \
 	rules/99-systemd.rules \
@@ -4492,8 +4492,8 @@ endif
 INSTALL_DIRS += \
 	$(prefix)/lib/modules-load.d \
 	$(sysconfdir)/modules-load.d \
-	$(prefix)/lib/net/links \
-	$(sysconfdir)/net/links \
+	$(prefix)/lib/systemd/network \
+	$(sysconfdir)/systemd/network \
 	$(prefix)/lib/sysctl.d \
 	$(sysconfdir)/sysctl.d \
 	$(prefix)/lib/kernel/install.d \
diff --git a/links/99-default.link b/links/99-default.link
deleted file mode 100644
index 05be03b..0000000
--- a/links/99-default.link
+++ /dev/null
@@ -1,3 +0,0 @@
-[Link]
-NamePolicy=onboard slot path
-MACAddressPolicy=persistent
diff --git a/network/99-default.link b/network/99-default.link
new file mode 100644
index 0000000..05be03b
--- /dev/null
+++ b/network/99-default.link
@@ -0,0 +1,3 @@
+[Link]
+NamePolicy=onboard slot path
+MACAddressPolicy=persistent
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index 089bd76..0a86d0e 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -74,9 +74,9 @@ int link_config_ctx_new(link_config_ctx **ret) {
 
         LIST_HEAD_INIT(ctx->links);
 
-        ctx->link_dirs = strv_new("/etc/net/links",
-                                  "/run/net/links",
-                                  "/usr/lib/net/links",
+        ctx->link_dirs = strv_new("/etc/systemd/network",
+                                  "/run/systemd/network",
+                                  "/usr/lib/systemd/network",
                                   NULL);
         if (!ctx->link_dirs) {
                 log_error("failed to build link config directory array");



More information about the systemd-commits mailing list