summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-04-15 07:43:12 +0200
committerGitHub <noreply@github.com>2024-04-15 07:43:12 +0200
commit9506269d12ac9459dbab9cbb1068f97eaab259be (patch)
tree5eae2a32547d1bf29037f9f7334588736e3b73c3 /src
parentndisc-option: add missing case (diff)
parentsd-ndisc: make ndisc_send() and icmp6_send() take struct in6_addr (diff)
downloadsystemd-9506269d12ac9459dbab9cbb1068f97eaab259be.tar.xz
systemd-9506269d12ac9459dbab9cbb1068f97eaab259be.zip
Merge pull request #32266 from yuwata/libsystemd-network-trivial-cleanups
libsystemd-network: trivial cleanups
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd-network/dhcp6-internal.h5
-rw-r--r--src/libsystemd-network/dhcp6-network.c18
-rw-r--r--src/libsystemd-network/dhcp6-protocol.h8
-rw-r--r--src/libsystemd-network/fuzz-dhcp6-client.c4
-rw-r--r--src/libsystemd-network/fuzz-ndisc-rs.c8
-rw-r--r--src/libsystemd-network/icmp6-util-unix.c2
-rw-r--r--src/libsystemd-network/icmp6-util.c14
-rw-r--r--src/libsystemd-network/icmp6-util.h18
-rw-r--r--src/libsystemd-network/ndisc-option.c2
-rw-r--r--src/libsystemd-network/ndisc-option.h2
-rw-r--r--src/libsystemd-network/sd-dhcp6-client.c4
-rw-r--r--src/libsystemd-network/sd-ndisc.c6
-rw-r--r--src/libsystemd-network/sd-radv.c2
-rw-r--r--src/libsystemd-network/test-dhcp6-client.c7
-rw-r--r--src/libsystemd-network/test-ndisc-send.c11
15 files changed, 49 insertions, 62 deletions
diff --git a/src/libsystemd-network/dhcp6-internal.h b/src/libsystemd-network/dhcp6-internal.h
index 3fbfc028e9..ecd62ea802 100644
--- a/src/libsystemd-network/dhcp6-internal.h
+++ b/src/libsystemd-network/dhcp6-internal.h
@@ -84,9 +84,8 @@ struct sd_dhcp6_client {
bool send_release;
};
-int dhcp6_network_bind_udp_socket(int ifindex, struct in6_addr *address);
-int dhcp6_network_send_udp_socket(int s, struct in6_addr *address,
- const void *packet, size_t len);
+int dhcp6_network_bind_udp_socket(int ifindex, const struct in6_addr *address);
+int dhcp6_network_send_udp_socket(int s, const struct in6_addr *address, const void *packet, size_t len);
int dhcp6_client_send_message(sd_dhcp6_client *client);
int dhcp6_client_set_transaction_id(sd_dhcp6_client *client, uint32_t transaction_id);
diff --git a/src/libsystemd-network/dhcp6-network.c b/src/libsystemd-network/dhcp6-network.c
index a3e4e19e8e..0aa8469cc3 100644
--- a/src/libsystemd-network/dhcp6-network.c
+++ b/src/libsystemd-network/dhcp6-network.c
@@ -17,9 +17,10 @@
#include "fd-util.h"
#include "socket-util.h"
-int dhcp6_network_bind_udp_socket(int ifindex, struct in6_addr *local_address) {
+int dhcp6_network_bind_udp_socket(int ifindex, const struct in6_addr *local_address) {
union sockaddr_union src = {
.in6.sin6_family = AF_INET6,
+ .in6.sin6_addr = *ASSERT_PTR(local_address),
.in6.sin6_port = htobe16(DHCP6_PORT_CLIENT),
.in6.sin6_scope_id = ifindex,
};
@@ -27,9 +28,6 @@ int dhcp6_network_bind_udp_socket(int ifindex, struct in6_addr *local_address) {
int r;
assert(ifindex > 0);
- assert(local_address);
-
- src.in6.sin6_addr = *local_address;
s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_UDP);
if (s < 0)
@@ -58,20 +56,14 @@ int dhcp6_network_bind_udp_socket(int ifindex, struct in6_addr *local_address) {
return TAKE_FD(s);
}
-int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
- const void *packet, size_t len) {
+int dhcp6_network_send_udp_socket(int s, const struct in6_addr *server_address, const void *packet, size_t len) {
union sockaddr_union dest = {
.in6.sin6_family = AF_INET6,
+ .in6.sin6_addr = *ASSERT_PTR(server_address),
.in6.sin6_port = htobe16(DHCP6_PORT_SERVER),
};
- int r;
-
- assert(server_address);
- memcpy(&dest.in6.sin6_addr, server_address, sizeof(dest.in6.sin6_addr));
-
- r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in6));
- if (r < 0)
+ if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in6)) < 0)
return -errno;
return 0;
diff --git a/src/libsystemd-network/dhcp6-protocol.h b/src/libsystemd-network/dhcp6-protocol.h
index c70f93203d..ab75bad434 100644
--- a/src/libsystemd-network/dhcp6-protocol.h
+++ b/src/libsystemd-network/dhcp6-protocol.h
@@ -28,9 +28,11 @@ typedef struct DHCP6Message DHCP6Message;
#define DHCP6_MIN_OPTIONS_SIZE \
1280 - sizeof(struct ip6_hdr) - sizeof(struct udphdr)
-#define IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT \
- { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02 } } }
+#define IN6_ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS \
+ ((const struct in6_addr) { { { \
+ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, \
+ } } } )
enum {
DHCP6_PORT_SERVER = 547,
diff --git a/src/libsystemd-network/fuzz-dhcp6-client.c b/src/libsystemd-network/fuzz-dhcp6-client.c
index 2d4284476c..2b6e3357e9 100644
--- a/src/libsystemd-network/fuzz-dhcp6-client.c
+++ b/src/libsystemd-network/fuzz-dhcp6-client.c
@@ -12,11 +12,11 @@
static int test_dhcp_fd[2] = EBADF_PAIR;
-int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address, const void *packet, size_t len) {
+int dhcp6_network_send_udp_socket(int s, const struct in6_addr *server_address, const void *packet, size_t len) {
return len;
}
-int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
+int dhcp6_network_bind_udp_socket(int index, const struct in6_addr *local_address) {
assert_se(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_dhcp_fd) >= 0);
return TAKE_FD(test_dhcp_fd[0]);
}
diff --git a/src/libsystemd-network/fuzz-ndisc-rs.c b/src/libsystemd-network/fuzz-ndisc-rs.c
index c52043a13b..54737e4a06 100644
--- a/src/libsystemd-network/fuzz-ndisc-rs.c
+++ b/src/libsystemd-network/fuzz-ndisc-rs.c
@@ -35,11 +35,6 @@ static void test_with_sd_ndisc(const uint8_t *data, size_t size) {
}
static void test_with_icmp6_packet(const uint8_t *data, size_t size) {
- static const struct sockaddr_in6 dst = {
- .sin6_family = AF_INET6,
- .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
- };
-
_cleanup_close_pair_ int fd_pair[2] = EBADF_PAIR;
_cleanup_(icmp6_packet_unrefp) ICMP6Packet *packet = NULL;
_cleanup_set_free_ Set *options = NULL;
@@ -53,7 +48,8 @@ static void test_with_icmp6_packet(const uint8_t *data, size_t size) {
if (ndisc_parse_options(packet, &options) < 0)
return;
- if (ndisc_send(fd_pair[1], &dst, icmp6_packet_get_header(packet), options, /* timestamp = */ 0) < 0)
+ if (ndisc_send(fd_pair[1], &IN6_ADDR_ALL_ROUTERS_MULTICAST,
+ icmp6_packet_get_header(packet), options, /* timestamp = */ 0) < 0)
return;
packet = icmp6_packet_unref(packet);
diff --git a/src/libsystemd-network/icmp6-util-unix.c b/src/libsystemd-network/icmp6-util-unix.c
index c400e4205e..d6d505717d 100644
--- a/src/libsystemd-network/icmp6-util-unix.c
+++ b/src/libsystemd-network/icmp6-util-unix.c
@@ -23,7 +23,7 @@ int icmp6_bind(int ifindex, bool is_router) {
return test_fd[is_router];
}
-int icmp6_send(int fd, const struct sockaddr_in6 *dst, const struct iovec *iov, size_t n_iov) {
+int icmp6_send(int fd, const struct in6_addr *dst, const struct iovec *iov, size_t n_iov) {
return writev(fd, iov, n_iov);
}
diff --git a/src/libsystemd-network/icmp6-util.c b/src/libsystemd-network/icmp6-util.c
index d4b7ae5855..a28f175b5e 100644
--- a/src/libsystemd-network/icmp6-util.c
+++ b/src/libsystemd-network/icmp6-util.c
@@ -33,13 +33,13 @@ int icmp6_bind(int ifindex, bool is_router) {
ICMP6_FILTER_SETBLOCKALL(&filter);
if (is_router) {
mreq = (struct ipv6_mreq) {
- .ipv6mr_multiaddr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
+ .ipv6mr_multiaddr = IN6_ADDR_ALL_ROUTERS_MULTICAST,
.ipv6mr_interface = ifindex,
};
ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
} else {
mreq = (struct ipv6_mreq) {
- .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
+ .ipv6mr_multiaddr = IN6_ADDR_ALL_NODES_MULTICAST,
.ipv6mr_interface = ifindex,
};
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
@@ -91,14 +91,20 @@ int icmp6_bind(int ifindex, bool is_router) {
return TAKE_FD(s);
}
-int icmp6_send(int fd, const struct sockaddr_in6 *dst, const struct iovec *iov, size_t n_iov) {
+int icmp6_send(int fd, const struct in6_addr *dst, const struct iovec *iov, size_t n_iov) {
+ struct sockaddr_in6 sa = {
+ .sin6_family = AF_INET6,
+ .sin6_addr = *ASSERT_PTR(dst),
+ };
struct msghdr msg = {
- .msg_name = (struct sockaddr_in6*) dst,
+ .msg_name = &sa,
.msg_namelen = sizeof(struct sockaddr_in6),
.msg_iov = (struct iovec*) iov,
.msg_iovlen = n_iov,
};
+ assert(fd >= 0);
+
if (sendmsg(fd, &msg, 0) < 0)
return -errno;
diff --git a/src/libsystemd-network/icmp6-util.h b/src/libsystemd-network/icmp6-util.h
index 49cdcba3ff..9e5063f51b 100644
--- a/src/libsystemd-network/icmp6-util.h
+++ b/src/libsystemd-network/icmp6-util.h
@@ -12,16 +12,20 @@
#include "time-util.h"
-#define IN6ADDR_ALL_ROUTERS_MULTICAST_INIT \
- { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } } }
+#define IN6_ADDR_ALL_ROUTERS_MULTICAST \
+ ((const struct in6_addr) { { { \
+ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, \
+ } } } )
-#define IN6ADDR_ALL_NODES_MULTICAST_INIT \
- { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } }
+#define IN6_ADDR_ALL_NODES_MULTICAST \
+ ((const struct in6_addr) { { { \
+ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, \
+ } } } )
int icmp6_bind(int ifindex, bool is_router);
-int icmp6_send(int fd, const struct sockaddr_in6 *dst, const struct iovec *iov, size_t n_iov);
+int icmp6_send(int fd, const struct in6_addr *dst, const struct iovec *iov, size_t n_iov);
int icmp6_receive(
int fd,
void *buffer,
diff --git a/src/libsystemd-network/ndisc-option.c b/src/libsystemd-network/ndisc-option.c
index 11499a3921..901a3b399b 100644
--- a/src/libsystemd-network/ndisc-option.c
+++ b/src/libsystemd-network/ndisc-option.c
@@ -1400,7 +1400,7 @@ int ndisc_option_get_mac(Set *options, uint8_t type, struct ether_addr *ret) {
return 0;
}
-int ndisc_send(int fd, const struct sockaddr_in6 *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp) {
+int ndisc_send(int fd, const struct in6_addr *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp) {
int r;
assert(fd >= 0);
diff --git a/src/libsystemd-network/ndisc-option.h b/src/libsystemd-network/ndisc-option.h
index da52858caa..d7bd86147b 100644
--- a/src/libsystemd-network/ndisc-option.h
+++ b/src/libsystemd-network/ndisc-option.h
@@ -327,4 +327,4 @@ static inline int ndisc_option_set_prefix64(
return ndisc_option_add_prefix64_internal(options, 0, prefixlen, prefix, lifetime, valid_until);
}
-int ndisc_send(int fd, const struct sockaddr_in6 *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp);
+int ndisc_send(int fd, const struct in6_addr *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp);
diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c
index 229ceef783..9f14a32896 100644
--- a/src/libsystemd-network/sd-dhcp6-client.c
+++ b/src/libsystemd-network/sd-dhcp6-client.c
@@ -743,8 +743,6 @@ static int client_append_mudurl(sd_dhcp6_client *client, uint8_t **buf, size_t *
int dhcp6_client_send_message(sd_dhcp6_client *client) {
_cleanup_free_ uint8_t *buf = NULL;
- struct in6_addr all_servers =
- IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
struct sd_dhcp6_option *j;
usec_t elapsed_usec, time_now;
be16_t elapsed_time;
@@ -839,7 +837,7 @@ int dhcp6_client_send_message(sd_dhcp6_client *client) {
if (r < 0)
return r;
- r = dhcp6_network_send_udp_socket(client->fd, &all_servers, buf, offset);
+ r = dhcp6_network_send_udp_socket(client->fd, &IN6_ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS, buf, offset);
if (r < 0)
return r;
diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c
index cba9470ce0..b4136563e1 100644
--- a/src/libsystemd-network/sd-ndisc.c
+++ b/src/libsystemd-network/sd-ndisc.c
@@ -341,10 +341,6 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda
}
static int ndisc_send_router_solicitation(sd_ndisc *nd) {
- static const struct sockaddr_in6 dst = {
- .sin6_family = AF_INET6,
- .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
- };
static const struct nd_router_solicit header = {
.nd_rs_type = ND_ROUTER_SOLICIT,
};
@@ -360,7 +356,7 @@ static int ndisc_send_router_solicitation(sd_ndisc *nd) {
return r;
}
- return ndisc_send(nd->fd, &dst, &header.nd_rs_hdr, options, USEC_INFINITY);
+ return ndisc_send(nd->fd, &IN6_ADDR_ALL_ROUTERS_MULTICAST, &header.nd_rs_hdr, options, USEC_INFINITY);
}
static usec_t ndisc_timeout_compute_random(usec_t val) {
diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c
index cde45388b7..316fcf1d2b 100644
--- a/src/libsystemd-network/sd-radv.c
+++ b/src/libsystemd-network/sd-radv.c
@@ -134,7 +134,7 @@ static int radv_send_router(sd_radv *ra, const struct in6_addr *dst, usec_t life
struct sockaddr_in6 dst_addr = {
.sin6_family = AF_INET6,
- .sin6_addr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
+ .sin6_addr = IN6_ADDR_ALL_NODES_MULTICAST,
};
struct nd_router_advert adv = {
.nd_ra_type = ND_ROUTER_ADVERT,
diff --git a/src/libsystemd-network/test-dhcp6-client.c b/src/libsystemd-network/test-dhcp6-client.c
index ecf3f095c3..37d5a0cf6c 100644
--- a/src/libsystemd-network/test-dhcp6-client.c
+++ b/src/libsystemd-network/test-dhcp6-client.c
@@ -59,8 +59,7 @@
static const struct in6_addr local_address =
{ { { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, } } };
-static const struct in6_addr mcast_address =
- IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
+static const struct in6_addr mcast_address = IN6_ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS;
static const struct in6_addr ia_na_address1 = { { { IA_NA_ADDRESS1_BYTES } } };
static const struct in6_addr ia_na_address2 = { { { IA_NA_ADDRESS2_BYTES } } };
static const struct in6_addr ia_pd_prefix1 = { { { IA_PD_PREFIX1_BYTES } } };
@@ -1028,7 +1027,7 @@ static void test_client_callback(sd_dhcp6_client *client, int event, void *userd
}
}
-int dhcp6_network_send_udp_socket(int s, struct in6_addr *a, const void *packet, size_t len) {
+int dhcp6_network_send_udp_socket(int s, const struct in6_addr *a, const void *packet, size_t len) {
log_debug("/* %s(count=%u) */", __func__, test_client_sent_message_count);
assert_se(a);
@@ -1072,7 +1071,7 @@ int dhcp6_network_send_udp_socket(int s, struct in6_addr *a, const void *packet,
return len;
}
-int dhcp6_network_bind_udp_socket(int ifindex, struct in6_addr *a) {
+int dhcp6_network_bind_udp_socket(int ifindex, const struct in6_addr *a) {
assert_se(ifindex == test_ifindex);
assert_se(a);
assert_se(in6_addr_equal(a, &local_address));
diff --git a/src/libsystemd-network/test-ndisc-send.c b/src/libsystemd-network/test-ndisc-send.c
index 71e445c7e7..1b1b27d634 100644
--- a/src/libsystemd-network/test-ndisc-send.c
+++ b/src/libsystemd-network/test-ndisc-send.c
@@ -289,9 +289,9 @@ static int parse_argv(int argc, char *argv[]) {
if (in6_addr_is_null(&arg_dest.in6)) {
if (IN_SET(arg_icmp6_type, ND_ROUTER_ADVERT, ND_NEIGHBOR_ADVERT, ND_REDIRECT))
- arg_dest.in6 = (struct in6_addr) IN6ADDR_ALL_NODES_MULTICAST_INIT;
+ arg_dest.in6 = IN6_ADDR_ALL_NODES_MULTICAST;
else
- arg_dest.in6 = (struct in6_addr) IN6ADDR_ALL_ROUTERS_MULTICAST_INIT;
+ arg_dest.in6 = IN6_ADDR_ALL_ROUTERS_MULTICAST;
}
if (arg_set_source_mac) {
@@ -348,12 +348,7 @@ static int send_icmp6(int fd, const struct icmp6_hdr *hdr) {
return r;
}
- struct sockaddr_in6 dst_sockaddr = {
- .sin6_family = AF_INET6,
- .sin6_addr = arg_dest.in6,
- };
-
- return ndisc_send(fd, &dst_sockaddr, hdr, options, now(CLOCK_BOOTTIME));
+ return ndisc_send(fd, &arg_dest.in6, hdr, options, now(CLOCK_BOOTTIME));
}
static int send_router_solicit(int fd) {