[systemd-commits] 2 commits - Makefile.am src/libsystemd-rtnl src/shared src/systemd src/udev

Tom Gundersen tomegun at kemper.freedesktop.org
Mon Nov 4 23:13:33 CET 2013


 Makefile.am                          |    5 -
 src/libsystemd-rtnl/rtnl-message.c   |   89 ++++++++++++++++--
 src/libsystemd-rtnl/test-rtnl.c      |   47 ++++++++-
 src/shared/net-util.c                |  169 +++++++++++++++++++++++++++++++++++
 src/shared/net-util.h                |   41 ++++++++
 src/systemd/sd-rtnl.h                |   12 ++
 src/udev/net/link-config-gperf.gperf |    1 
 src/udev/net/link-config-parse.c     |  129 --------------------------
 src/udev/net/link-config.c           |   72 +++++---------
 src/udev/net/link-config.h           |    2 
 10 files changed, 374 insertions(+), 193 deletions(-)

New commits:
commit 03d7e632c0435f21373c12cf72a2cc3f12f21112
Author: Tom Gundersen <teg at jklm.no>
Date:   Mon Nov 4 02:42:23 2013 +0100

    rtnl: add support for routes

diff --git a/src/libsystemd-rtnl/rtnl-message.c b/src/libsystemd-rtnl/rtnl-message.c
index eae73a6..9a40a75 100644
--- a/src/libsystemd-rtnl/rtnl-message.c
+++ b/src/libsystemd-rtnl/rtnl-message.c
@@ -67,6 +67,42 @@ static int message_new(sd_rtnl_message **ret, size_t initial_size) {
         return 0;
 }
 
+int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
+                              unsigned char rtm_dst_len, unsigned char rtm_src_len,
+                              unsigned char rtm_tos, unsigned char rtm_table,
+                              unsigned char rtm_scope, unsigned char rtm_protocol,
+                              unsigned char rtm_type, unsigned rtm_flags, sd_rtnl_message **ret) {
+        struct rtmsg *rtm;
+        int r;
+
+        assert_return(nlmsg_type == RTM_NEWROUTE || nlmsg_type == RTM_DELROUTE ||
+                      nlmsg_type == RTM_GETROUTE, -EINVAL);
+        assert_return(ret, -EINVAL);
+
+        r = message_new(ret, NLMSG_SPACE(sizeof(struct rtmsg)));
+        if (r < 0)
+                return r;
+
+        (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
+        (*ret)->hdr->nlmsg_type = nlmsg_type;
+        if (nlmsg_type == RTM_NEWROUTE)
+                (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
+
+        rtm = NLMSG_DATA((*ret)->hdr);
+
+        rtm->rtm_family = rtm_family;
+        rtm->rtm_dst_len = rtm_dst_len;
+        rtm->rtm_src_len = rtm_src_len;
+        rtm->rtm_tos = rtm_tos;
+        rtm->rtm_table = rtm_table;
+        rtm->rtm_protocol = rtm_protocol;
+        rtm->rtm_scope = rtm_scope;
+        rtm->rtm_type = rtm_type;
+        rtm->rtm_flags = rtm_flags;
+
+        return 0;
+}
+
 int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type, unsigned int flags, sd_rtnl_message **ret) {
         struct ifinfomsg *ifi;
         int r;
@@ -189,6 +225,7 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
 int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *data) {
         uint16_t rtm_type;
         struct ifaddrmsg *ifa;
+        struct rtmsg *rtm;
 
         assert_return(m, -EINVAL);
         assert_return(data, -EINVAL);
@@ -204,9 +241,9 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
                                 case IFLA_QDISC:
                                         return add_rtattr(m, type, data, strlen(data) + 1);
                                 case IFLA_MTU:
-                                        return add_rtattr(m, type, data, sizeof(unsigned int));
+                                        return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_LINK:
-                                        return add_rtattr(m, type, data, sizeof(int));
+                                        return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_STATS:
                                         return add_rtattr(m, type, data, sizeof(struct rtnl_link_stats));
                                 case IFLA_ADDRESS:
@@ -237,6 +274,30 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
                                 default:
                                         return -ENOTSUP;
                         }
+                case RTM_NEWROUTE:
+                case RTM_DELROUTE:
+                case RTM_GETROUTE:
+                        switch (type) {
+                                case RTA_DST:
+                                case RTA_SRC:
+                                case RTA_GATEWAY:
+                                        rtm = NLMSG_DATA(m->hdr);
+                                        switch (rtm->rtm_family) {
+                                                case AF_INET:
+                                                        return add_rtattr(m, type, data, sizeof(struct in_addr));
+                                                case AF_INET6:
+                                                        return add_rtattr(m, type, data, sizeof(struct in6_addr));
+                                                default:
+                                                        return -EINVAL;
+                                        }
+                                case RTA_TABLE:
+                                case RTA_PRIORITY:
+                                case RTA_IIF:
+                                case RTA_OIF:
+                                        return add_rtattr(m, type, data, sizeof(uint32_t));
+                                default:
+                                        return -ENOTSUP;
+                        }
                 default:
                         return -ENOTSUP;
         }
@@ -275,7 +336,7 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
                                 m->next_rta = IFLA_RTA(ifi);
                                 m->remaining_size = IFLA_PAYLOAD(m->hdr);
                         }
-                        break;;
+                        break;
                 case RTM_NEWADDR:
                 case RTM_DELADDR:
                 case RTM_GETADDR:
@@ -283,9 +344,19 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
                                 struct ifaddrmsg *ifa = NLMSG_DATA(m->hdr);
 
                                 m->next_rta = IFA_RTA(ifa);
-                                m->remaining_size = IFLA_PAYLOAD(m->hdr);
+                                m->remaining_size = IFA_PAYLOAD(m->hdr);
+                        }
+                        break;
+                case RTM_NEWROUTE:
+                case RTM_DELROUTE:
+                case RTM_GETROUTE:
+                        if (!m->next_rta) {
+                                struct rtmesg *rtm = NLMSG_DATA(m->hdr);
+
+                                m->next_rta = RTM_RTA(rtm);
+                                m->remaining_size = RTM_PAYLOAD(m->hdr);
                         }
-                        break;;
+                        break;
                 default:
                         return -ENOTSUP;
         }
@@ -415,22 +486,22 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
                         case NLMSG_ERROR:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
                                         k = -EIO;
-                                break;;
+                                break;
                         case RTM_NEWLINK:
                         case RTM_DELLINK:
                         case RTM_GETLINK:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
                                         k = -EIO;
-                                break;;
+                                break;
                         case RTM_NEWADDR:
                         case RTM_DELADDR:
                         case RTM_GETADDR:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifaddrmsg)))
                                         k = -EIO;
-                                break;;
+                                break;
                         case NLMSG_NOOP:
                                 k = 0;
-                                break;;
+                                break;
                         default:
                                 k = 0; /* ignoring message of unknown type */
                 }
diff --git a/src/libsystemd-rtnl/test-rtnl.c b/src/libsystemd-rtnl/test-rtnl.c
index 601548b..2d2b237 100644
--- a/src/libsystemd-rtnl/test-rtnl.c
+++ b/src/libsystemd-rtnl/test-rtnl.c
@@ -54,6 +54,43 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
         assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 0, NULL) == 0);
 }
 
+static void test_route(void) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req;
+        uint32_t addr = htonl(INADDR_LOOPBACK);
+        uint32_t index = 2;
+        uint16_t type;
+        void *data;
+        int r;
+
+        r = sd_rtnl_message_route_new(RTM_NEWROUTE, AF_INET, 0, 0, 0,
+                                      RT_TABLE_MAIN, RT_SCOPE_UNIVERSE, RTPROT_BOOT,
+                                      RTN_UNICAST, 0, &req);
+        if (r < 0) {
+                log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
+                return;
+        }
+
+        r = sd_rtnl_message_append(req, RTA_GATEWAY, &addr);
+        if (r < 0) {
+                log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
+                return;
+        }
+
+        r = sd_rtnl_message_append(req, RTA_OIF, &index);
+        if (r < 0) {
+                log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
+                return;
+        }
+
+        assert(sd_rtnl_message_read(req, &type, &data) > 0);
+        assert(type == RTA_GATEWAY);
+        assert(*(uint32_t *) data == addr);
+
+        assert(sd_rtnl_message_read(req, &type, &data) > 0);
+        assert(type == RTA_OIF);
+        assert(*(uint32_t *) data == index);
+}
+
 static void test_multiple(void) {
         sd_rtnl *rtnl1, *rtnl2;
 
@@ -76,6 +113,8 @@ int main(void) {
 
         test_multiple();
 
+        test_route();
+
         assert(sd_rtnl_open(0, &rtnl) >= 0);
         assert(rtnl);
 
@@ -115,17 +154,17 @@ int main(void) {
         assert(sd_rtnl_message_read(m, &type, data) == 0);
 
         assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) >= 0);
-        while (sd_rtnl_message_read(r, &type, &data)) {
+        while (sd_rtnl_message_read(r, &type, &data) > 0) {
                 switch (type) {
 //                        case IFLA_MTU:
 //                                assert(*(unsigned int *) data == 65536);
-//                                break;;
+//                                break;
 //                        case IFLA_QDISC:
 //                                assert(streq((char *) data, "noqueue"));
-//                                break;;
+//                                break;
                         case IFLA_IFNAME:
                                 assert(streq((char *) data, "lo"));
-                                break;;
+                                break;
                 }
         }
 
diff --git a/src/systemd/sd-rtnl.h b/src/systemd/sd-rtnl.h
index ae0dcd8..5c964a3 100644
--- a/src/systemd/sd-rtnl.h
+++ b/src/systemd/sd-rtnl.h
@@ -38,8 +38,16 @@ sd_rtnl *sd_rtnl_unref(sd_rtnl *nl);
 int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl, sd_rtnl_message *message, uint64_t timeout, sd_rtnl_message **reply);
 
 /* messages */
-int sd_rtnl_message_link_new(uint16_t msg_type, int index, unsigned int type, unsigned int flags, sd_rtnl_message **ret);
-int sd_rtnl_message_addr_new(uint16_t msg_type, int index, unsigned char family, unsigned char prefixlen, unsigned char flags, unsigned char scope, sd_rtnl_message **ret);
+int sd_rtnl_message_link_new(uint16_t msg_type, int index, unsigned int type,
+                             unsigned int flags, sd_rtnl_message **ret);
+int sd_rtnl_message_addr_new(uint16_t msg_type, int index, unsigned char family,
+                             unsigned char prefixlen, unsigned char flags,
+                             unsigned char scope, sd_rtnl_message **ret);
+int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
+                              unsigned char rtm_dst_len, unsigned char rtm_src_len,
+                              unsigned char rtm_tos, unsigned char rtm_table,
+                              unsigned char rtm_scope, unsigned char rtm_protocol,
+                              unsigned char rtm_type, unsigned flags, sd_rtnl_message **ret);
 sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m);
 sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m);
 

commit be32eb9b7fbcb22e4b648086d644135e38279633
Author: Tom Gundersen <teg at jklm.no>
Date:   Sat Nov 2 02:13:48 2013 +0100

    net-config: start split out matching and parsing logic
    
    Move this to src/share/net-util.c, so it can be used elsewhere.

diff --git a/Makefile.am b/Makefile.am
index b710236..1d74a5c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -750,7 +750,9 @@ libsystemd_shared_la_SOURCES = \
 	src/shared/ima-util.c \
 	src/shared/ima-util.h \
 	src/shared/ptyfwd.c \
-	src/shared/ptyfwd.h
+	src/shared/ptyfwd.h \
+	src/shared/net-util.c \
+	src/shared/net-util.h
 
 #-------------------------------------------------------------------------------
 noinst_LTLIBRARIES += \
@@ -2329,7 +2331,6 @@ 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/net-util.c b/src/shared/net-util.c
new file mode 100644
index 0000000..c4cb333
--- /dev/null
+++ b/src/shared/net-util.c
@@ -0,0 +1,169 @@
+/*-*- 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 "net-util.h"
+#include "log.h"
+#include "utf8.h"
+#include "util.h"
+#include "conf-parser.h"
+
+bool net_match_config(const struct ether_addr *match_mac,
+                      const char *match_path,
+                      const char *match_driver,
+                      const char *match_type,
+                      const char *match_name,
+                      struct udev_device *device) {
+        const char *property;
+
+        assert(device);
+
+        if (match_mac) {
+                property = udev_device_get_sysattr_value(device, "address");
+                if (!property || memcmp(match_mac, ether_aton(property), ETH_ALEN)) {
+                        log_debug("Interface MAC address (%s) did not match MACAddress=%s",
+                                  property, ether_ntoa(match_mac));
+                        return 0;
+                }
+        }
+
+        if (match_path) {
+                property = udev_device_get_property_value(device, "ID_PATH");
+                if (!streq_ptr(match_path, property)) {
+                        log_debug("Interface persistent path (%s) did not match Path=%s",
+                                  property, match_path);
+                        return 0;
+                }
+        }
+
+        if (match_driver) {
+                property = udev_device_get_driver(device);
+                if (!streq_ptr(match_driver, property)) {
+                        log_debug("Interface device driver (%s) did not match Driver=%s",
+                                  property, match_driver);
+                        return 0;
+                }
+        }
+
+        if (match_type) {
+                property = udev_device_get_devtype(device);
+                if (!streq_ptr(match_type, property)) {
+                        log_debug("Interface type (%s) did not match Type=%s",
+                                  property, match_type);
+                        return 0;
+                }
+        }
+
+        if (match_name) {
+                property = udev_device_get_sysname(device);
+                if (!streq_ptr(match_name, property)) {
+                        log_debug("Interface name (%s) did not match Name=%s",
+                                  property, match_name);
+                        return 0;
+                }
+        }
+
+        return 1;
+}
+
+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 = new0(struct ether_addr, 1);
+        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/shared/net-util.h b/src/shared/net-util.h
new file mode 100644
index 0000000..6b79a9c
--- /dev/null
+++ b/src/shared/net-util.h
@@ -0,0 +1,41 @@
+/*-*- 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/>.
+***/
+
+#pragma once
+
+#include <netinet/ether.h>
+
+#include "udev.h"
+
+bool net_match_config(const struct ether_addr *match_mac,
+                      const char *match_path,
+                      const char *match_driver,
+                      const char *match_type,
+                      const char *match_name,
+                      struct udev_device *device);
+
+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);
diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf
index 130ebc9..1fe0b93 100644
--- a/src/udev/net/link-config-gperf.gperf
+++ b/src/udev/net/link-config-gperf.gperf
@@ -1,6 +1,7 @@
 %{
 #include <stddef.h>
 #include "conf-parser.h"
+#include "net-util.h"
 #include "link-config.h"
 #include "ethtool-util.h"
 %}
diff --git a/src/udev/net/link-config-parse.c b/src/udev/net/link-config-parse.c
deleted file mode 100644
index c339d08..0000000
--- a/src/udev/net/link-config-parse.c
+++ /dev/null
@@ -1,129 +0,0 @@
-/*-*- 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 "util.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 = new0(struct ether_addr, 1);
-        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 883aedc..d930afc 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -38,6 +38,7 @@
 #include "fileio.h"
 #include "hashmap.h"
 #include "rtnl-util.h"
+#include "net-util.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -213,7 +214,6 @@ int link_config_load(link_config_ctx *ctx) {
 
         link_configs_free(ctx);
 
-
         if (!enable_name_policy()) {
                 ctx->enable_name_policy = false;
                 log_info("Network interface NamePolicy= disabled on kernel commandline, ignoring.");
@@ -241,59 +241,23 @@ bool link_config_should_reload(link_config_ctx *ctx) {
         return paths_check_timestamp(ctx->link_dirs, &ctx->link_dirs_ts_usec, false);
 }
 
-static bool match_config(link_config *match, struct udev_device *device) {
-        const char *property;
-
-        if (match->match_mac) {
-                property = udev_device_get_sysattr_value(device, "address");
-                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 (!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 (!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 (!streq_ptr(match->match_type, property)) {
-                        log_debug("Device type (%s) did not match Type=%s",
-                                  property, match->match_type);
-                        return 0;
-                }
-        }
-
-        return 1;
-}
-
 int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_config **ret) {
         link_config *link;
 
         LIST_FOREACH(links, link, ctx->links) {
-                if (match_config(link, device)) {
-                        log_debug("Config file %s applies to device %s", link->filename, udev_device_get_sysname(device));
+                if (net_match_config(link->match_mac, link->match_path,
+                                     link->match_driver, link->match_type,
+                                     NULL, device)) {
+                        log_debug("Config file %s applies to device %s",
+                                  link->filename,
+                                  udev_device_get_sysname(device));
                         *ret = link;
                         return 0;
                 }
         }
 
+        *ret = NULL;
+
         return -ENOENT;
 }
 
@@ -479,3 +443,21 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
 
         return 0;
 }
+
+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");
diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h
index b3ee81f..9747003 100644
--- a/src/udev/net/link-config.h
+++ b/src/udev/net/link-config.h
@@ -85,7 +85,5 @@ 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);



More information about the systemd-commits mailing list