chiark / gitweb /
networkctl: use the shared functions for enumerating the local gateways
[elogind.git] / src / network / networkd-link.c
index b46ac5c3c308005aaafea8503df05fcb17af8d13..a4f8c59e09d1e3e49516c406731bf7fb83a0747d 100644 (file)
@@ -515,12 +515,7 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST)
-                log_link_struct(LOG_WARNING, link,
-                                "MESSAGE=%-*s: could not set route: %s",
-                                IFNAMSIZ,
-                                link->ifname, strerror(-r),
-                                "ERRNO=%d", -r,
-                                NULL);
+                log_link_warning_errno(link, -r, "%-*s: could not set route: %m", IFNAMSIZ, link->ifname);
 
         if (link->link_messages == 0) {
                 log_link_debug(link, "routes set");
@@ -576,12 +571,7 @@ int link_route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -ESRCH)
-                log_link_struct(LOG_WARNING, link,
-                                "MESSAGE=%-*s: could not drop route: %s",
-                                IFNAMSIZ,
-                                link->ifname, strerror(-r),
-                                "ERRNO=%d", -r,
-                                NULL);
+                log_link_warning_errno(link, -r, "%-*s: could not drop route: %m", IFNAMSIZ, link->ifname);
 
         return 1;
 }
@@ -631,12 +621,7 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST)
-                log_link_struct(LOG_WARNING, link,
-                                "MESSAGE=%-*s: could not set address: %s",
-                                IFNAMSIZ,
-                                link->ifname, strerror(-r),
-                                "ERRNO=%d", -r,
-                                NULL);
+                log_link_warning_errno(link, -r, "%-*s: could not set address: %m", IFNAMSIZ, link->ifname);
         else if (r >= 0) {
                 /* calling handler directly so take a ref */
                 link_ref(link);
@@ -695,14 +680,30 @@ int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata)
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EADDRNOTAVAIL)
-                log_link_struct(LOG_WARNING, link,
-                                "MESSAGE=%-*s: could not drop address: %s",
+                log_link_warning_errno(link, -r, "%-*s: could not drop address: %m", IFNAMSIZ, link->ifname);
+
+        return 1;
+}
+
+static int link_set_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        _cleanup_link_unref_ Link *link = userdata;
+        int r;
+
+        log_debug_link(link, "set link");
+
+        r = sd_rtnl_message_get_errno(m);
+        if (r < 0 && r != -EEXIST) {
+                log_struct_link(LOG_ERR, link,
+                                "MESSAGE=%-*s: could not join netdev: %s",
                                 IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
+                link_enter_failed(link);
+                return 1;
+        }
 
-        return 1;
+        return 0;
 }
 
 static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata,
@@ -780,11 +781,7 @@ static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0)
-                log_link_struct(LOG_WARNING, link,
-                                "MESSAGE=%-*s: could not set MTU: %s",
-                                IFNAMSIZ, link->ifname, strerror(-r),
-                                "ERRNO=%d", -r,
-                                NULL);
+                log_link_warning_errno(link, -r, "%-*s: could not set MTU: %m", IFNAMSIZ, link->ifname);
 
         return 1;
 }
@@ -826,6 +823,69 @@ int link_set_mtu(Link *link, uint32_t mtu) {
         return 0;
 }
 
+static int link_set_bridge(Link *link) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(link);
+        assert(link->network);
+
+        if(link->network->cost == 0)
+                return 0;
+
+        r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
+                                     RTM_SETLINK, link->ifindex);
+        if (r < 0) {
+                log_error_link(link, "Could not allocate RTM_SETLINK message");
+                return r;
+        }
+
+        r = sd_rtnl_message_link_set_family(req, PF_BRIDGE);
+        if (r < 0) {
+                log_error_link(link,
+                               "Could not set message family %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_open_container(req, IFLA_PROTINFO);
+        if (r < 0) {
+                log_error_link(link,
+                               "Could not append IFLA_PROTINFO attribute: %s",
+                               strerror(-r));
+                return r;
+        }
+
+        if(link->network->cost != 0) {
+                r = sd_rtnl_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost);
+                if (r < 0) {
+                        log_error_link(link,
+                                       "Could not append IFLA_BRPORT_COST attribute: %s",
+                                       strerror(-r));
+                        return r;
+                }
+        }
+
+        r = sd_rtnl_message_close_container(req);
+        if (r < 0) {
+                log_error_link(link,
+                               "Could not append IFLA_LINKINFO attribute: %s",
+                               strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL);
+        if (r < 0) {
+                log_error_link(link,
+                               "Could not send rtnetlink message: %s",
+                               strerror(-r));
+                return r;
+        }
+
+        link_ref(link);
+
+        return r;
+}
+
 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
         Link *link = userdata;
 
@@ -1003,12 +1063,7 @@ static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         if (r < 0) {
                 /* we warn but don't fail the link, as it may
                    be brought up later */
-                log_link_struct(LOG_WARNING, link,
-                                "MESSAGE=%-*s: could not bring up interface: %s",
-                                IFNAMSIZ,
-                                link->ifname, strerror(-r),
-                                "ERRNO=%d", -r,
-                                NULL);
+                log_link_warning_errno(link, -r, "%-*s: could not bring up interface: %m", IFNAMSIZ, link->ifname);
         }
 
         return 1;
@@ -1066,6 +1121,15 @@ static int link_joined(Link *link) {
                 }
         }
 
+        if(link->network->bridge) {
+                r = link_set_bridge(link);
+                if (r < 0) {
+                        log_error_link(link,
+                                       "Could not set bridge message: %s",
+                                       strerror(-r));
+                }
+        }
+
         return link_enter_set_addresses(link);
 }
 
@@ -1084,12 +1148,7 @@ static int netdev_join_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST) {
-                log_link_struct(LOG_ERR, link,
-                                "MESSAGE=%-*s: could not join netdev: %s",
-                                IFNAMSIZ,
-                                link->ifname, strerror(-r),
-                                "ERRNO=%d", -r,
-                                NULL);
+                log_link_error_errno(link, -r, "%-*s: could not join netdev: %m", IFNAMSIZ, link->ifname);
                 link_enter_failed(link);
                 return 1;
         } else
@@ -1120,7 +1179,7 @@ static int link_enter_join_netdev(Link *link) {
                 return link_joined(link);
 
         if (link->network->bond) {
-                log_link_struct(LOG_DEBUG, link,
+                log_link_struct(link, LOG_DEBUG,
                                 "MESSAGE=%-*s: enslaving by '%s'",
                                 IFNAMSIZ,
                                 link->ifname, link->network->bond->ifname,
@@ -1129,7 +1188,7 @@ static int link_enter_join_netdev(Link *link) {
 
                 r = netdev_join(link->network->bond, link, &netdev_join_handler);
                 if (r < 0) {
-                        log_link_struct(LOG_WARNING, link,
+                        log_link_struct(link, LOG_WARNING,
                                         "MESSAGE=%-*s: could not join netdev '%s': %s",
                                         IFNAMSIZ,
                                         link->ifname, link->network->bond->ifname,
@@ -1144,7 +1203,7 @@ static int link_enter_join_netdev(Link *link) {
         }
 
         if (link->network->bridge) {
-                log_link_struct(LOG_DEBUG, link,
+                log_link_struct(link, LOG_DEBUG,
                                 "MESSAGE=%-*s: enslaving by '%s'",
                                 IFNAMSIZ,
                                 link->ifname, link->network->bridge->ifname,
@@ -1154,7 +1213,7 @@ static int link_enter_join_netdev(Link *link) {
                 r = netdev_join(link->network->bridge, link,
                                 &netdev_join_handler);
                 if (r < 0) {
-                        log_link_struct(LOG_WARNING, link,
+                        log_link_struct(link, LOG_WARNING,
                                         "MESSAGE=%-*s: could not join netdev '%s': %s",
                                         IFNAMSIZ,
                                         link->ifname, link->network->bridge->ifname,
@@ -1169,7 +1228,7 @@ static int link_enter_join_netdev(Link *link) {
         }
 
         HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
-                log_link_struct(LOG_DEBUG, link,
+                log_link_struct(link, LOG_DEBUG,
                                 "MESSAGE=%-*s: enslaving by '%s'",
                                 IFNAMSIZ,
                                 link->ifname, netdev->ifname, NETDEVIF(netdev),
@@ -1177,7 +1236,7 @@ static int link_enter_join_netdev(Link *link) {
 
                 r = netdev_join(netdev, link, &netdev_join_handler);
                 if (r < 0) {
-                        log_link_struct(LOG_WARNING, link,
+                        log_link_struct(link, LOG_WARNING,
                                         "MESSAGE=%-*s: could not join netdev '%s': %s",
                                         IFNAMSIZ,
                                         link->ifname, netdev->ifname,