chiark / gitweb /
udev: link-config: add rtnl support
authorTom Gundersen <teg@jklm.no>
Sat, 26 Oct 2013 16:54:16 +0000 (18:54 +0200)
committerTom Gundersen <teg@jklm.no>
Sun, 27 Oct 2013 21:23:58 +0000 (22:23 +0100)
This adds support for setting the mac address, name and mtu.

Example:

[Link]
MTU=1450
MACAddress=98:76:54:32:10:ab
Name=wireless0

Makefile.am
src/udev/net/link-config-gperf.gperf
src/udev/net/link-config.c
src/udev/net/link-config.h
src/udev/udev-builtin-net_link.c

index 6ce4b7fffa4e52dfb35a4a47891a4b998e247ee0..e5ed3020b1932afd8323eef4c20bdd981c0d6dd3 100644 (file)
@@ -2344,6 +2344,7 @@ libudev_core_la_LIBADD = \
        libsystemd-label.la \
        libsystemd-daemon-internal.la \
        libsystemd-shared.la \
+       libsystemd-rtnl.la \
        $(BLKID_LIBS) \
        $(KMOD_LIBS)
 
index 9e2f4d4f5213a0ced1bbcd5a8d147fce9bb7e3d4..182a6641929005951feefe92843909d9317b6980 100644 (file)
@@ -19,6 +19,9 @@ Match.Path,                         config_parse_string,        0, offsetof(link
 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.MACAddress,                    config_parse_string,        0, offsetof(link_config, mac)
+Link.Name,                          config_parse_string,        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)
index 3beb28acba24be6d671dbb0410e2d16a75ed851f..902ed0955d23bbd2c17549caeac4be181849e2e5 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <netinet/ether.h>
+
 #include "link-config.h"
 
 #include "ethtool-util.h"
 
+#include "libudev-private.h"
+#include "sd-rtnl.h"
 #include "util.h"
 #include "log.h"
 #include "strv.h"
@@ -35,6 +39,8 @@ struct link_config_ctx {
 
         int ethtool_fd;
 
+        sd_rtnl *rtnl;
+
         char **link_dirs;
         usec_t *link_dirs_ts_usec;
 };
@@ -56,6 +62,12 @@ int link_config_ctx_new(link_config_ctx **ret) {
                 return r;
         }
 
+        r = sd_rtnl_open(0, &ctx->rtnl);
+        if (r < 0) {
+                link_config_ctx_free(ctx);
+                return r;
+        }
+
         LIST_HEAD_INIT(ctx->links);
 
         ctx->link_dirs = strv_new("/etc/net/links",
@@ -104,7 +116,11 @@ void link_config_ctx_free(link_config_ctx *ctx) {
         if (!ctx)
                 return;
 
-        close_nointr_nofail(ctx->ethtool_fd);
+        if (ctx->ethtool_fd >= 0)
+                close_nointr_nofail(ctx->ethtool_fd);
+
+        sd_rtnl_unref(ctx->rtnl);
+
         strv_free(ctx->link_dirs);
         free(ctx->link_dirs_ts_usec);
         link_configs_free(ctx);
@@ -234,9 +250,54 @@ int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_confi
         return -ENOENT;
 }
 
+static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const char *mac, unsigned int mtu) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
+        bool need_update;
+        int r;
+
+        assert(rtnl);
+        assert(ifindex > 0);
+
+        r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
+        if (r < 0)
+                return r;
+
+        if (name) {
+                r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
+                if (r < 0)
+                        return r;
+
+                need_update = true;
+        }
+
+        if (mac) {
+                r = sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac));
+                if (r < 0)
+                        return r;
+
+                need_update = true;
+        }
+
+        if (mtu > 0) {
+                r = sd_rtnl_message_append(message, IFLA_MTU, &mtu);
+                if (r < 0)
+                        return r;
+
+                need_update = true;
+        }
+
+        if  (need_update) {
+                r = sd_rtnl_send_with_reply_and_block(rtnl, message, 250 * USEC_PER_MSEC, NULL);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
         const char *name;
-        int r;
+        int r, ifindex;
 
         name = udev_device_get_sysname(device);
         if (!name)
@@ -275,5 +336,17 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                         log_info("Set WakeOnLan of %s to %s", name, config->wol);
         }
 
+        ifindex = udev_device_get_ifindex(device);
+        if (ifindex <= 0) {
+                log_warning("Could not find ifindex");
+                return -ENODEV;
+        }
+
+        r = rtnl_set_properties(ctx->rtnl, ifindex, config->name, config->mac, config->mtu);
+        if (r < 0) {
+                log_warning("Could not set Name, MACAddress or MTU on %s", name);
+                return r;
+        }
+
         return 0;
 }
index 57d2c9c3cd635fe3e5a5d11112a6b046254b50ef..849e481520e3aa068204296e69f7714cd9e34c87 100644 (file)
@@ -38,6 +38,9 @@ struct link_config {
         char *match_type;
 
         char *description;
+        char *mac;
+        char *name;
+        unsigned int mtu;
         unsigned int speed;
         char *duplex;
         char *wol;
index d7cbe6a0162c35a054060bc45bba12fc60b041cf..f4fb63ec51bef47c4862d48141b9e297ed942b7b 100644 (file)
@@ -37,7 +37,7 @@ static int builtin_net_link(struct udev_device *dev, int argc, char **argv, bool
         r = link_config_get(ctx, dev, &link);
         if (r < 0) {
                 if (r == -ENOENT) {
-                        log_info("No matching link configuration found");
+                        log_debug("No matching link configuration found");
                         return EXIT_SUCCESS;
                 } else {
                         log_error("Could not get link config");
@@ -47,7 +47,7 @@ static int builtin_net_link(struct udev_device *dev, int argc, char **argv, bool
 
         r = link_config_apply(ctx, link, dev);
         if (r < 0) {
-                log_error("Could not apply link config");
+                log_error("Could not apply link config to %s", udev_device_get_sysname(dev));
                 return EXIT_FAILURE;
         }