chiark / gitweb /
networkd: netdev - allow setting MACAddress in .netdev files
authorTom Gundersen <teg@jklm.no>
Sat, 14 Jun 2014 13:38:35 +0000 (15:38 +0200)
committerTom Gundersen <teg@jklm.no>
Sat, 14 Jun 2014 13:38:35 +0000 (15:38 +0200)
It may sometimes be necessary to specify the MAC address of a netdev.
Let us set the correct one from the get-go, rather than having the
kernel generate a random one, and then change it after.

src/network/networkd-netdev-gperf.gperf
src/network/networkd-netdev.c
src/network/networkd-tunnel.c
src/network/networkd-veth.c
src/network/networkd.h

index dff0ae6..82cc858 100644 (file)
@@ -23,6 +23,7 @@ NetDev.Description,      config_parse_string,                0,
 NetDev.Name,             config_parse_ifname,                0,                             offsetof(NetDev, ifname)
 NetDev.Kind,             config_parse_netdev_kind,           0,                             offsetof(NetDev, kind)
 NetDev.MTUBytes,         config_parse_iec_size,              0,                             offsetof(NetDev, mtu)
+NetDev.MACAddress,       config_parse_hwaddr,                0,                             offsetof(NetDev, mac)
 VLAN.Id,                 config_parse_uint64,                0,                             offsetof(NetDev, vlanid)
 MACVLAN.Mode,            config_parse_macvlan_mode,          0,                             offsetof(NetDev, macvlan_mode)
 Tunnel.Local,            config_parse_tunnel_address,        0,                             offsetof(NetDev, tunnel_local)
@@ -31,3 +32,4 @@ Tunnel.TOS,              config_parse_unsigned,              0,
 Tunnel.TTL,              config_parse_unsigned,              0,                             offsetof(NetDev, tunnel_ttl)
 Tunnel.DiscoverPathMTU,  config_parse_bool,                  0,                             offsetof(NetDev, tunnel_pmtudisc)
 Peer.Name,               config_parse_ifname,                0,                             offsetof(NetDev, ifname_peer)
+Peer.MACAddress,         config_parse_hwaddr,                0,                             offsetof(NetDev, mac_peer)
index 1519419..0d08e56 100644 (file)
@@ -92,6 +92,8 @@ static void netdev_free(NetDev *netdev) {
 
         free(netdev->description);
         free(netdev->ifname);
+        free(netdev->mac);
+        free(netdev->mac_peer);
 
         condition_free_list(netdev->match_host);
         condition_free_list(netdev->match_virt);
@@ -301,7 +303,7 @@ static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t c
                 return r;
         }
 
-        if(netdev->mtu) {
+        if (netdev->mtu) {
                 r = sd_rtnl_message_append_u32(req, IFLA_MTU, netdev->mtu);
                 if (r < 0) {
                         log_error_netdev(netdev,
@@ -311,6 +313,16 @@ static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t c
                 }
         }
 
+        if (netdev->mac) {
+                r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, netdev->mac);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
         if (r < 0) {
                 log_error_netdev(netdev,
index 60b16ba..885347a 100644 (file)
@@ -60,6 +60,16 @@ static int netdev_fill_ipip_rtnl_message(Link *link, sd_rtnl_message *m) {
                 }
         }
 
+        if (netdev->mac) {
+                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
         if (r < 0) {
                 log_error_netdev(netdev,
@@ -157,6 +167,16 @@ static int netdev_fill_sit_rtnl_message(Link *link, sd_rtnl_message *m) {
                 }
         }
 
+        if (netdev->mac) {
+                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
         if (r < 0) {
                 log_error_netdev(netdev,
@@ -262,6 +282,16 @@ static int netdev_fill_ipgre_rtnl_message(Link *link, sd_rtnl_message *m) {
                 }
         }
 
+        if (netdev->mac) {
+                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
         if (r < 0) {
                 log_error_netdev(netdev,
@@ -367,6 +397,16 @@ static int netdev_fill_vti_rtnl_message(Link *link, sd_rtnl_message *m) {
                 }
         }
 
+        if (netdev->mac) {
+                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
         if (r < 0) {
                 log_error_netdev(netdev,
index 1a5d880..3584981 100644 (file)
@@ -42,6 +42,16 @@ static int netdev_fill_veth_rtnl_message(NetDev *netdev, sd_rtnl_message *m) {
                 return r;
         }
 
+        if (netdev->mac) {
+                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
         if (r < 0) {
                 log_error_netdev(netdev,
@@ -67,7 +77,7 @@ static int netdev_fill_veth_rtnl_message(NetDev *netdev, sd_rtnl_message *m) {
                 return r;
         }
 
-        if(netdev->ifname_peer) {
+        if (netdev->ifname_peer) {
                 r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname_peer);
                 if (r < 0) {
                         log_error("Failed to add netlink interface name: %s", strerror(-r));
@@ -75,6 +85,16 @@ static int netdev_fill_veth_rtnl_message(NetDev *netdev, sd_rtnl_message *m) {
                 }
         }
 
+        if (netdev->mac_peer) {
+                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac_peer);
+                if (r < 0) {
+                        log_error_netdev(netdev,
+                                         "Colud not append IFLA_ADDRESS attribute: %s",
+                                         strerror(-r));
+                    return r;
+                }
+        }
+
         r = sd_rtnl_message_close_container(m);
         if (r < 0) {
                 log_error_netdev(netdev,
index 102744d..f079473 100644 (file)
@@ -103,6 +103,8 @@ struct NetDev {
         char *ifname;
         char *ifname_peer;
         size_t mtu;
+        struct ether_addr *mac;
+        struct ether_addr *mac_peer;
         NetDevKind kind;
 
         uint64_t vlanid;