]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network/neighbor: use struct in_addr_data
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Sep 2024 18:54:29 +0000 (03:54 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Sep 2024 18:58:28 +0000 (03:58 +0900)
src/network/networkd-json.c
src/network/networkd-neighbor.c
src/network/networkd-neighbor.h
src/network/networkd-network.c

index 4eafc62c7e481cbead0a97308d5670d2dc69e296..47bc8a663624356f59d45798168807850e3a8d1a 100644 (file)
@@ -97,8 +97,8 @@ static int neighbor_append_json(Neighbor *n, sd_json_variant **array) {
 
         return sd_json_variant_append_arraybo(
                         array,
-                        SD_JSON_BUILD_PAIR_INTEGER("Family", n->family),
-                        JSON_BUILD_PAIR_IN_ADDR("Destination", &n->in_addr, n->family),
+                        SD_JSON_BUILD_PAIR_INTEGER("Family", n->dst_addr.family),
+                        JSON_BUILD_PAIR_IN_ADDR("Destination", &n->dst_addr.address, n->dst_addr.family),
                         JSON_BUILD_PAIR_HW_ADDR("LinkLayerAddress", &n->ll_addr),
                         SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
                         SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
index 3377bb056e5899a93049dfd74d40e390ea6f26c7..aaf823ac01fd23642c65a4f8a99eef4b4e8dc848 100644 (file)
@@ -147,29 +147,29 @@ static int neighbor_dup(const Neighbor *neighbor, Neighbor **ret) {
 static void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state) {
         assert(neighbor);
 
-        siphash24_compress_typesafe(neighbor->family, state);
+        siphash24_compress_typesafe(neighbor->dst_addr.family, state);
 
-        if (!IN_SET(neighbor->family, AF_INET, AF_INET6))
+        if (!IN_SET(neighbor->dst_addr.family, AF_INET, AF_INET6))
                 /* treat any other address family as AF_UNSPEC */
                 return;
 
         /* Equality of neighbors are given by the destination address.
          * See neigh_lookup() in the kernel. */
-        in_addr_hash_func(&neighbor->in_addr, neighbor->family, state);
+        in_addr_hash_func(&neighbor->dst_addr.address, neighbor->dst_addr.family, state);
 }
 
 static int neighbor_compare_func(const Neighbor *a, const Neighbor *b) {
         int r;
 
-        r = CMP(a->family, b->family);
+        r = CMP(a->dst_addr.family, b->dst_addr.family);
         if (r != 0)
                 return r;
 
-        if (!IN_SET(a->family, AF_INET, AF_INET6))
+        if (!IN_SET(a->dst_addr.family, AF_INET, AF_INET6))
                 /* treat any other address family as AF_UNSPEC */
                 return 0;
 
-        return memcmp(&a->in_addr, &b->in_addr, FAMILY_ADDRESS_SIZE(a->family));
+        return memcmp(&a->dst_addr.address, &b->dst_addr.address, FAMILY_ADDRESS_SIZE(a->dst_addr.family));
 }
 
 static int neighbor_get_request(Link *link, const Neighbor *neighbor, Request **ret) {
@@ -244,7 +244,7 @@ static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const
                        "%s %s neighbor (%s): lladdr: %s, dst: %s",
                        str, strna(network_config_source_to_string(neighbor->source)), strna(state),
                        HW_ADDR_TO_STR(&neighbor->ll_addr),
-                       IN_ADDR_TO_STRING(neighbor->family, &neighbor->in_addr));
+                       IN_ADDR_TO_STRING(neighbor->dst_addr.family, &neighbor->dst_addr.address));
 }
 
 static int neighbor_configure(Neighbor *neighbor, Link *link, Request *req) {
@@ -261,7 +261,7 @@ static int neighbor_configure(Neighbor *neighbor, Link *link, Request *req) {
         log_neighbor_debug(neighbor, "Configuring", link);
 
         r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_NEWNEIGH,
-                                      link->ifindex, neighbor->family);
+                                      link->ifindex, neighbor->dst_addr.family);
         if (r < 0)
                 return r;
 
@@ -273,7 +273,7 @@ static int neighbor_configure(Neighbor *neighbor, Link *link, Request *req) {
         if (r < 0)
                 return r;
 
-        r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->family, &neighbor->in_addr);
+        r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->dst_addr.family, &neighbor->dst_addr.address);
         if (r < 0)
                 return r;
 
@@ -338,7 +338,7 @@ static int link_request_neighbor(Link *link, const Neighbor *neighbor) {
                                "The link layer address length (%zu) for neighbor %s does not match with "
                                "the hardware address length (%zu), ignoring the setting.",
                                neighbor->ll_addr.length,
-                               IN_ADDR_TO_STRING(neighbor->family, &neighbor->in_addr),
+                               IN_ADDR_TO_STRING(neighbor->dst_addr.family, &neighbor->dst_addr.address),
                                link->hw_addr.length);
                 return 0;
         }
@@ -451,11 +451,11 @@ int neighbor_remove(Neighbor *neighbor, Link *link) {
         log_neighbor_debug(neighbor, "Removing", link);
 
         r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_DELNEIGH,
-                                      link->ifindex, neighbor->family);
+                                      link->ifindex, neighbor->dst_addr.family);
         if (r < 0)
                 return log_link_error_errno(link, r, "Could not allocate RTM_DELNEIGH message: %m");
 
-        r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->family, &neighbor->in_addr);
+        r = netlink_message_append_in_addr_union(m, NDA_DST, neighbor->dst_addr.family, &neighbor->dst_addr.address);
         if (r < 0)
                 return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m");
 
@@ -593,19 +593,19 @@ int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message,
                 return log_oom();
 
         /* First, retrieve the fundamental information about the neighbor. */
-        r = sd_rtnl_message_neigh_get_family(message, &tmp->family);
+        r = sd_rtnl_message_neigh_get_family(message, &tmp->dst_addr.family);
         if (r < 0) {
                 log_link_warning(link, "rtnl: received neighbor message without family, ignoring.");
                 return 0;
         }
-        if (tmp->family == AF_BRIDGE) /* Currently, we do not support it. */
+        if (tmp->dst_addr.family == AF_BRIDGE) /* Currently, we do not support it. */
                 return 0;
-        if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
-                log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", tmp->family);
+        if (!IN_SET(tmp->dst_addr.family, AF_INET, AF_INET6)) {
+                log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", tmp->dst_addr.family);
                 return 0;
         }
 
-        r = netlink_message_read_in_addr_union(message, NDA_DST, tmp->family, &tmp->in_addr);
+        r = netlink_message_read_in_addr_union(message, NDA_DST, tmp->dst_addr.family, &tmp->dst_addr.address);
         if (r < 0) {
                 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
                 return 0;
@@ -664,13 +664,13 @@ static int neighbor_section_verify(Neighbor *neighbor) {
         if (section_is_invalid(neighbor->section))
                 return -EINVAL;
 
-        if (neighbor->family == AF_UNSPEC)
+        if (neighbor->dst_addr.family == AF_UNSPEC)
                 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
                                          "%s: Neighbor section without Address= configured. "
                                          "Ignoring [Neighbor] section from line %u.",
                                          neighbor->section->filename, neighbor->section->line);
 
-        if (neighbor->family == AF_INET6 && !socket_ipv6_is_supported())
+        if (neighbor->dst_addr.family == AF_INET6 && !socket_ipv6_is_supported())
                 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
                                          "%s: Neighbor section with an IPv6 destination address configured, "
                                          "but the kernel does not support IPv6. "
@@ -709,7 +709,7 @@ int network_drop_invalid_neighbors(Network *network) {
                         log_warning("%s: Duplicated neighbor settings for %s is specified at line %u and %u, "
                                     "dropping the neighbor setting specified at line %u.",
                                     dup->section->filename,
-                                    IN_ADDR_TO_STRING(neighbor->family, &neighbor->in_addr),
+                                    IN_ADDR_TO_STRING(neighbor->dst_addr.family, &neighbor->dst_addr.address),
                                     neighbor->section->line,
                                     dup->section->line, dup->section->line);
                         /* neighbor_detach() will drop the neighbor from neighbors_by_section. */
@@ -740,34 +740,21 @@ int config_parse_neighbor_address(
                 void *data,
                 void *userdata) {
 
-        _cleanup_(neighbor_unref_or_set_invalidp) Neighbor *n = NULL;
+        _cleanup_(neighbor_unref_or_set_invalidp) Neighbor *neighbor = NULL;
         Network *network = ASSERT_PTR(userdata);
         int r;
 
         assert(filename);
-        assert(section);
-        assert(lvalue);
-        assert(rvalue);
 
-        r = neighbor_new_static(network, filename, section_line, &n);
+        r = neighbor_new_static(network, filename, section_line, &neighbor);
         if (r < 0)
                 return log_oom();
 
-        if (isempty(rvalue)) {
-                n->family = AF_UNSPEC;
-                n->in_addr = IN_ADDR_NULL;
-                TAKE_PTR(n);
-                return 0;
-        }
-
-        r = in_addr_from_string_auto(rvalue, &n->family, &n->in_addr);
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Neighbor Address is invalid, ignoring assignment: %s", rvalue);
-                return 0;
-        }
+        r = config_parse_in_addr_data(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &neighbor->dst_addr, NULL);
+        if (r <= 0) /* 0 means non-critical error, but the section will be ignored. */
+                return r;
 
-        TAKE_PTR(n);
+        TAKE_PTR(neighbor);
         return 0;
 }
 
index 7917930bcdb844cc458a5f8ee28d157c83476ef2..0d67879a24f19c5f1ac2a0528ea74dde6ef5868c 100644 (file)
@@ -23,8 +23,7 @@ typedef struct Neighbor {
 
         unsigned n_ref;
 
-        int family;
-        union in_addr_union in_addr;
+        struct in_addr_data dst_addr;
         struct hw_addr_data ll_addr;
 } Neighbor;
 
index 5107da47839cc272d75e3927ffa65b724033dd14..b6d34c4a9cbfb1a329332192d1e6f663f8529e73 100644 (file)
@@ -854,7 +854,7 @@ bool network_has_static_ipv6_configurations(Network *network) {
                         return true;
 
         ORDERED_HASHMAP_FOREACH(neighbor, network->neighbors_by_section)
-                if (neighbor->family == AF_INET6)
+                if (neighbor->dst_addr.family == AF_INET6)
                         return true;
 
         if (!hashmap_isempty(network->address_labels_by_section))