network: use netlink_message_append_{in_addr,sockaddr}_union()
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "netlink-util.h"
|
||||
#include "networkd-link.h"
|
||||
#include "networkd-manager.h"
|
||||
#include "networkd-util.h"
|
||||
@@ -62,10 +63,7 @@ static int wireguard_set_ipmask_one(NetDev *netdev, sd_netlink_message *message,
|
||||
if (r < 0)
|
||||
goto cancel;
|
||||
|
||||
if (mask->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(message, WGALLOWEDIP_A_IPADDR, &mask->ip.in);
|
||||
else if (mask->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(message, WGALLOWEDIP_A_IPADDR, &mask->ip.in6);
|
||||
r = netlink_message_append_in_addr_union(message, WGALLOWEDIP_A_IPADDR, mask->family, &mask->ip);
|
||||
if (r < 0)
|
||||
goto cancel;
|
||||
|
||||
@@ -122,12 +120,11 @@ static int wireguard_set_peer_one(NetDev *netdev, sd_netlink_message *message, c
|
||||
if (r < 0)
|
||||
goto cancel;
|
||||
|
||||
if (peer->endpoint.sa.sa_family == AF_INET)
|
||||
r = sd_netlink_message_append_sockaddr_in(message, WGPEER_A_ENDPOINT, &peer->endpoint.in);
|
||||
else if (peer->endpoint.sa.sa_family == AF_INET6)
|
||||
r = sd_netlink_message_append_sockaddr_in6(message, WGPEER_A_ENDPOINT, &peer->endpoint.in6);
|
||||
if (r < 0)
|
||||
goto cancel;
|
||||
if (IN_SET(peer->endpoint.sa.sa_family, AF_INET, AF_INET6)) {
|
||||
r = netlink_message_append_sockaddr_union(message, WGPEER_A_ENDPOINT, &peer->endpoint);
|
||||
if (r < 0)
|
||||
goto cancel;
|
||||
}
|
||||
}
|
||||
|
||||
r = sd_netlink_message_open_container(message, WGPEER_A_ALLOWEDIPS);
|
||||
|
||||
@@ -469,10 +469,7 @@ int address_remove(
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not set prefixlen: %m");
|
||||
|
||||
if (address->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
|
||||
else if (address->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
|
||||
r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
|
||||
|
||||
@@ -618,18 +615,12 @@ int address_configure(
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not set scope: %m");
|
||||
|
||||
if (address->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
|
||||
else if (address->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
|
||||
r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
|
||||
|
||||
if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) {
|
||||
if (address->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in);
|
||||
else if (address->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6);
|
||||
r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m");
|
||||
} else if (address->family == AF_INET && address->prefixlen <= 30) {
|
||||
|
||||
@@ -137,20 +137,9 @@ int neighbor_configure(Neighbor *neighbor, Link *link, link_netlink_message_hand
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append NDA_LLADDR attribute: %m");
|
||||
|
||||
switch (neighbor->family) {
|
||||
case AF_INET6:
|
||||
r = sd_netlink_message_append_in6_addr(req, NDA_DST, &neighbor->in_addr.in6);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append NDA_DST attribute: %m");
|
||||
break;
|
||||
case AF_INET:
|
||||
r = sd_netlink_message_append_in_addr(req, NDA_DST, &neighbor->in_addr.in);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append NDA_DST attribute: %m");
|
||||
break;
|
||||
default:
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Neighbor with invalid address family");
|
||||
}
|
||||
r = netlink_message_append_in_addr_union(req, NDA_DST, neighbor->family, &neighbor->in_addr);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append NDA_DST attribute: %m");
|
||||
|
||||
r = netlink_call_async(link->manager->rtnl, NULL, req, callback ?: neighbor_handler,
|
||||
link_netlink_destroy_callback, link);
|
||||
|
||||
@@ -409,19 +409,13 @@ int route_remove(Route *route, Link *link,
|
||||
return log_error_errno(r, "Could not create RTM_DELROUTE message: %m");
|
||||
|
||||
if (in_addr_is_null(route->family, &route->gw) == 0) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
|
||||
}
|
||||
|
||||
if (route->dst_prefixlen) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_DST attribute: %m");
|
||||
|
||||
@@ -431,10 +425,7 @@ int route_remove(Route *route, Link *link,
|
||||
}
|
||||
|
||||
if (route->src_prefixlen) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
|
||||
|
||||
@@ -444,10 +435,7 @@ int route_remove(Route *route, Link *link,
|
||||
}
|
||||
|
||||
if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
|
||||
}
|
||||
@@ -520,10 +508,7 @@ int route_configure(
|
||||
return log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
|
||||
|
||||
if (in_addr_is_null(route->family, &route->gw) == 0) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &route->gw.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_GATEWAY, &route->gw.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
|
||||
|
||||
@@ -533,10 +518,7 @@ int route_configure(
|
||||
}
|
||||
|
||||
if (route->dst_prefixlen) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_DST, &route->dst.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_DST, &route->dst.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_DST attribute: %m");
|
||||
|
||||
@@ -546,10 +528,7 @@ int route_configure(
|
||||
}
|
||||
|
||||
if (route->src_prefixlen) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_SRC, &route->src.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_SRC, &route->src.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
|
||||
|
||||
@@ -559,10 +538,7 @@ int route_configure(
|
||||
}
|
||||
|
||||
if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
|
||||
if (route->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc.in);
|
||||
else if (route->family == AF_INET6)
|
||||
r = sd_netlink_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc.in6);
|
||||
r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
|
||||
}
|
||||
|
||||
@@ -370,10 +370,7 @@ int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *lin
|
||||
return log_error_errno(r, "Could not allocate RTM_DELRULE message: %m");
|
||||
|
||||
if (in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->from) == 0) {
|
||||
if (routing_policy_rule->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(m, FRA_SRC, &routing_policy_rule->from.in);
|
||||
else
|
||||
r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &routing_policy_rule->from.in6);
|
||||
r = netlink_message_append_in_addr_union(m, FRA_SRC, routing_policy_rule->family, &routing_policy_rule->from);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append FRA_SRC attribute: %m");
|
||||
|
||||
@@ -383,10 +380,7 @@ int routing_policy_rule_remove(RoutingPolicyRule *routing_policy_rule, Link *lin
|
||||
}
|
||||
|
||||
if (in_addr_is_null(routing_policy_rule->family, &routing_policy_rule->to) == 0) {
|
||||
if (routing_policy_rule->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(m, FRA_DST, &routing_policy_rule->to.in);
|
||||
else
|
||||
r = sd_netlink_message_append_in6_addr(m, FRA_DST, &routing_policy_rule->to.in6);
|
||||
r = netlink_message_append_in_addr_union(m, FRA_DST, routing_policy_rule->family, &routing_policy_rule->to);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append FRA_DST attribute: %m");
|
||||
|
||||
@@ -495,10 +489,7 @@ int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, link_netl
|
||||
return log_error_errno(r, "Could not allocate RTM_NEWRULE message: %m");
|
||||
|
||||
if (in_addr_is_null(rule->family, &rule->from) == 0) {
|
||||
if (rule->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(m, FRA_SRC, &rule->from.in);
|
||||
else
|
||||
r = sd_netlink_message_append_in6_addr(m, FRA_SRC, &rule->from.in6);
|
||||
r = netlink_message_append_in_addr_union(m, FRA_SRC, rule->family, &rule->from);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append FRA_SRC attribute: %m");
|
||||
|
||||
@@ -508,10 +499,7 @@ int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link, link_netl
|
||||
}
|
||||
|
||||
if (in_addr_is_null(rule->family, &rule->to) == 0) {
|
||||
if (rule->family == AF_INET)
|
||||
r = sd_netlink_message_append_in_addr(m, FRA_DST, &rule->to.in);
|
||||
else
|
||||
r = sd_netlink_message_append_in6_addr(m, FRA_DST, &rule->to.in6);
|
||||
r = netlink_message_append_in_addr_union(m, FRA_DST, rule->family, &rule->to);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Could not append FRA_DST attribute: %m");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user