]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: introduce neighbor_drop_{,foreign_}neighbors()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Sep 2020 15:37:39 +0000 (00:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:44:42 +0000 (02:44 +0900)
src/network/networkd-link.c
src/network/networkd-neighbor.c
src/network/networkd-neighbor.h

index 9b56cc1f844762bae64c1f613322982fb34abc93..9aebe745654d21b41bb363ebfa8c0138253ac008 100644 (file)
@@ -2593,22 +2593,6 @@ static bool link_is_static_address_configured(Link *link, Address *address) {
         return false;
 }
 
-static bool link_is_neighbor_configured(Link *link, Neighbor *neighbor) {
-        Neighbor *net_neighbor;
-
-        assert(link);
-        assert(neighbor);
-
-        if (!link->network)
-                return false;
-
-        HASHMAP_FOREACH(net_neighbor, link->network->neighbors_by_section)
-                if (neighbor_equal(net_neighbor, neighbor))
-                        return true;
-
-        return false;
-}
-
 static bool link_is_static_route_configured(Link *link, Route *route) {
         Route *net_route;
 
@@ -2695,7 +2679,6 @@ static int link_enumerate_ipv6_tentative_addresses(Link *link) {
 
 static int link_drop_foreign_config(Link *link) {
         Address *address;
-        Neighbor *neighbor;
         Route *route;
         int r;
 
@@ -2729,17 +2712,9 @@ static int link_drop_foreign_config(Link *link) {
                 }
         }
 
-        SET_FOREACH(neighbor, link->neighbors_foreign) {
-                if (link_is_neighbor_configured(link, neighbor)) {
-                        r = neighbor_add(link, neighbor, NULL);
-                        if (r < 0)
-                                return r;
-                } else {
-                        r = neighbor_remove(neighbor, link);
-                        if (r < 0)
-                                return r;
-                }
-        }
+        r = link_drop_foreign_neighbors(link);
+        if (r < 0)
+                return r;
 
         SET_FOREACH(route, link->routes_foreign) {
                 /* do not touch routes managed by the kernel */
@@ -2808,7 +2783,6 @@ static int remove_static_address_handler(sd_netlink *rtnl, sd_netlink_message *m
 
 static int link_drop_config(Link *link) {
         Address *address, *pool_address;
-        Neighbor *neighbor;
         Route *route;
         int r;
 
@@ -2832,11 +2806,9 @@ static int link_drop_config(Link *link) {
                         }
         }
 
-        SET_FOREACH(neighbor, link->neighbors) {
-                r = neighbor_remove(neighbor, link);
-                if (r < 0)
-                        return r;
-        }
+        r = link_drop_neighbors(link);
+        if (r < 0)
+                return r;
 
         SET_FOREACH(route, link->routes) {
                 /* do not touch routes managed by the kernel */
index f04a0b762cf0154742b711f5836e56c334d11a4a..5135ed64226afd8b8fdc1918d66af6fb715dee1c 100644 (file)
@@ -173,7 +173,7 @@ static int neighbor_add_internal(Link *link, Set **neighbors, const Neighbor *in
         return 0;
 }
 
-int neighbor_add(Link *link, const Neighbor *in, Neighbor **ret) {
+static int neighbor_add(Link *link, const Neighbor *in, Neighbor **ret) {
         Neighbor *neighbor;
         int r;
 
@@ -204,7 +204,7 @@ static int neighbor_add_foreign(Link *link, const Neighbor *in, Neighbor **ret)
         return neighbor_add_internal(link, &link->neighbors_foreign, in, ret);
 }
 
-bool neighbor_equal(const Neighbor *n1, const Neighbor *n2) {
+static bool neighbor_equal(const Neighbor *n1, const Neighbor *n2) {
         if (n1 == n2)
                 return true;
 
@@ -330,7 +330,7 @@ static int neighbor_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link
         return 1;
 }
 
-int neighbor_remove(Neighbor *neighbor, Link *link) {
+static int neighbor_remove(Neighbor *neighbor, Link *link) {
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
         int r;
 
@@ -359,6 +359,57 @@ int neighbor_remove(Neighbor *neighbor, Link *link) {
         return 0;
 }
 
+static bool link_is_neighbor_configured(Link *link, Neighbor *neighbor) {
+        Neighbor *net_neighbor;
+
+        assert(link);
+        assert(neighbor);
+
+        if (!link->network)
+                return false;
+
+        HASHMAP_FOREACH(net_neighbor, link->network->neighbors_by_section)
+                if (neighbor_equal(net_neighbor, neighbor))
+                        return true;
+
+        return false;
+}
+
+int link_drop_foreign_neighbors(Link *link) {
+        Neighbor *neighbor;
+        int r;
+
+        assert(link);
+
+        SET_FOREACH(neighbor, link->neighbors_foreign)
+                if (link_is_neighbor_configured(link, neighbor)) {
+                        r = neighbor_add(link, neighbor, NULL);
+                        if (r < 0)
+                                return r;
+                } else {
+                        r = neighbor_remove(neighbor, link);
+                        if (r < 0)
+                                return r;
+                }
+
+        return 0;
+}
+
+int link_drop_neighbors(Link *link) {
+        Neighbor *neighbor;
+        int k, r = 0;
+
+        assert(link);
+
+        SET_FOREACH(neighbor, link->neighbors) {
+                k = neighbor_remove(neighbor, link);
+                if (k < 0 && r >= 0)
+                        r = k;
+        }
+
+        return r;
+}
+
 static int manager_rtnl_process_neighbor_lladdr(sd_netlink_message *message, union lladdr_union *lladdr, size_t *size, char **str) {
         int r;
 
index 56d1f66bf737421f85f77b4328b43c7b48f06f44..a0e3fbd47a938befac2145aa132111cd30faac50 100644 (file)
@@ -33,12 +33,9 @@ typedef struct Neighbor {
 void neighbor_free(Neighbor *neighbor);
 int neighbor_section_verify(Neighbor *neighbor);
 
-int neighbor_remove(Neighbor *neighbor, Link *link);
-
-int neighbor_add(Link *link, const Neighbor *in, Neighbor **ret);
-bool neighbor_equal(const Neighbor *n1, const Neighbor *n2);
-
 int link_set_neighbors(Link *link);
+int link_drop_neighbors(Link *link);
+int link_drop_foreign_neighbors(Link *link);
 
 int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, Manager *m);