chiark / gitweb /
udev: net_setup_link - don't use Description as Alias
authorTom Gundersen <teg@jklm.no>
Mon, 25 Nov 2013 00:33:04 +0000 (01:33 +0100)
committerTom Gundersen <teg@jklm.no>
Mon, 25 Nov 2013 13:53:56 +0000 (14:53 +0100)
Use Description only internally, and allow Alias to be set
as a separate option. For instance SNMP uses ifalias for
a specific purpose, so let's not write to it by default.

man/udev.xml
src/libsystemd-rtnl/rtnl-message.c
src/libsystemd-rtnl/rtnl-util.c
src/libsystemd-rtnl/rtnl-util.h
src/shared/net-util.c
src/shared/net-util.h
src/udev/net/link-config-gperf.gperf
src/udev/net/link-config.c
src/udev/net/link-config.h

index d157b8c433e657037fed6c29cfa3749045452284..bd071e17709414db21e51e51ea657f269b6acb10 100644 (file)
         <varlistentry>
           <term><varname>Description</varname></term>
           <listitem>
-            <para>A description of the device. The <literal>ifalias</literal> is set to this value.</para>
+            <para>A description of the device.</para>
+          </listitem>
+        </varlistentry>
+        <varlistentry>
+          <term><varname>Alias</varname></term>
+          <listitem>
+            <para>The <literal>ifalias</literal> is set to this value.</para>
           </listitem>
         </varlistentry>
         <varlistentry>
index 1ce68626688d7ae7bc3e231bf1fd102bcf54dee3..26da7f2db88c3b3db5cce254dce4afe2224b276d 100644 (file)
@@ -129,7 +129,8 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type,
         struct ifinfomsg *ifi;
         int r;
 
-        assert_return(nlmsg_type == RTM_NEWLINK || nlmsg_type == RTM_DELLINK || nlmsg_type == RTM_GETLINK, -EINVAL);
+        assert_return(nlmsg_type == RTM_NEWLINK || nlmsg_type == RTM_DELLINK ||
+                      nlmsg_type == RTM_SETLINK || nlmsg_type == RTM_GETLINK, -EINVAL);
         assert_return(index > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
@@ -258,14 +259,15 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
 
         switch (rtm_type) {
                 case RTM_NEWLINK:
+                case RTM_SETLINK:
                 case RTM_DELLINK:
                 case RTM_GETLINK:
                         switch (type) {
                                 case IFLA_IFNAME:
+                                case IFLA_IFALIAS:
                                 case IFLA_QDISC:
                                         return add_rtattr(m, type, data, strlen(data) + 1);
                                 case IFLA_MTU:
-                                        return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_LINK:
                                         return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_STATS:
@@ -352,6 +354,7 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
 
         switch (rtm_type) {
                 case RTM_NEWLINK:
+                case RTM_SETLINK:
                 case RTM_DELLINK:
                 case RTM_GETLINK:
                         if (!m->next_rta) {
@@ -512,6 +515,7 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
                                         k = -EIO;
                                 break;
                         case RTM_NEWLINK:
+                        case RTM_SETLINK:
                         case RTM_DELLINK:
                         case RTM_GETLINK:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
index d40858a63b24fbc5a8d2962bdbca2cb9d52dc227..4e7661bb7775a1185d8c7eb31e108f4793bc83d4 100644 (file)
@@ -34,7 +34,7 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         assert(ifindex > 0);
         assert(name);
 
-        r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
+        r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, 0, 0, &message);
         if (r < 0)
                 return r;
 
@@ -49,7 +49,8 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         return 0;
 }
 
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const struct ether_addr *mac, unsigned mtu) {
+int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
+                             const struct ether_addr *mac, unsigned mtu) {
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
         bool need_update = false;
         int r;
@@ -57,13 +58,22 @@ int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const struct ether_addr
         assert(rtnl);
         assert(ifindex > 0);
 
-        if (!mac && mtu == 0)
+        if (!alias && !mac && mtu == 0)
                 return 0;
 
-        r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
+        r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, 0, 0, &message);
         if (r < 0)
                 return r;
 
+        if (alias) {
+                r = sd_rtnl_message_append(message, IFLA_IFALIAS, alias);
+                if (r < 0)
+                        return r;
+
+                need_update = true;
+
+        }
+
         if (mac) {
                 r = sd_rtnl_message_append(message, IFLA_ADDRESS, mac);
                 if (r < 0)
index ba0f71f944c2d39b10aee568814a956059523d2e..013002dd6007288f7cc731b2256a9c95b9574e98 100644 (file)
@@ -27,7 +27,7 @@
 #include "sd-rtnl.h"
 
 int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name);
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const struct ether_addr *mac, unsigned mtu);
+int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias, const struct ether_addr *mac, unsigned mtu);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_rtnl*, sd_rtnl_unref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_rtnl_message*, sd_rtnl_message_unref);
index 2734d119ce89c6fde6d862ba47993f980c7ddb30..f2fd0819d375dc5170058b6859251d541dbd1f0f 100644 (file)
@@ -20,7 +20,7 @@
 ***/
 
 #include <netinet/ether.h>
-#include <net/if.h>
+#include <linux/if.h>
 #include <arpa/inet.h>
 
 #include "net-util.h"
@@ -123,6 +123,46 @@ int config_parse_ifname(const char *unit,
         return 0;
 }
 
+int config_parse_ifalias(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) >= IFALIASZ) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Interface alias 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,
index eac394bffa24fc4a1ca854d4fc4a02939b28127d..d6ca737e7fb4e92970940d5a804b452dfc0a1a3c 100644 (file)
@@ -43,4 +43,8 @@ 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_ifalias(const char *unit, const char *filename, unsigned line,
+                         const char *section, const char *lvalue, int ltype,
+                         const char *rvalue, void *data, void *userdata);
+
 int net_parse_inaddr(const char *address, unsigned char *family, void *dst);
index 1fe0b93e5365f4c0b19942c224f193effe643f2a..950fb15286faf024f13b7bc39d001d4b0dc42695 100644 (file)
@@ -25,6 +25,7 @@ Link.MACAddressPolicy,              config_parse_mac_policy,    0, offsetof(link
 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.Alias,                         config_parse_ifalias,       0, offsetof(link_config, alias)
 Link.MTU,                           config_parse_unsigned,      0, offsetof(link_config, mtu)
 Link.SpeedMBytes,                   config_parse_unsigned,      0, offsetof(link_config, speed)
 Link.Duplex,                        config_parse_duplex,        0, offsetof(link_config, duplex)
index 5156ba6f8ad781c9fde6fae47a4ecad1d297c57b..37918d35e3e07cda73ad539a82363b76444ec0ba 100644 (file)
@@ -124,6 +124,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);
         }
@@ -364,14 +365,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",
@@ -439,9 +432,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;
         }
 
index 974700363531e47c7cd7dc4d780c0aab1c32abb8..f74a790d558958295e5bd6df56d0da6f68338066 100644 (file)
@@ -59,6 +59,7 @@ struct link_config {
         MACPolicy mac_policy;
         NamePolicy *name_policy;
         char *name;
+        char *alias;
         unsigned int mtu;
         unsigned int speed;
         Duplex duplex;