From 37c011e74c9a3d18c4e75a897452f1fedb098975 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 14 Feb 2024 16:45:55 +0900 Subject: [PATCH 1/4] icmp6-util: merge icmp6_bind_router_{solicitation,advertisement}() into icmp6_bind() No functional change, just refactoring. --- src/libsystemd-network/icmp6-util-unix.c | 10 ++-- src/libsystemd-network/icmp6-util.c | 69 +++++++++--------------- src/libsystemd-network/icmp6-util.h | 4 +- src/libsystemd-network/sd-ndisc.c | 2 +- src/libsystemd-network/sd-radv.c | 2 +- 5 files changed, 31 insertions(+), 56 deletions(-) diff --git a/src/libsystemd-network/icmp6-util-unix.c b/src/libsystemd-network/icmp6-util-unix.c index 01edb854df..5eac4a0e53 100644 --- a/src/libsystemd-network/icmp6-util-unix.c +++ b/src/libsystemd-network/icmp6-util-unix.c @@ -16,15 +16,11 @@ static struct in6_addr dummy_link_local = { }, }; -int icmp6_bind_router_solicitation(int ifindex) { - if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) +int icmp6_bind(int ifindex, bool is_router) { + if (!is_router && socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) return -errno; - return test_fd[0]; -} - -int icmp6_bind_router_advertisement(int ifindex) { - return test_fd[1]; + return test_fd[is_router]; } int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { diff --git a/src/libsystemd-network/icmp6-util.c b/src/libsystemd-network/icmp6-util.c index 72c20baadc..2224d17cba 100644 --- a/src/libsystemd-network/icmp6-util.c +++ b/src/libsystemd-network/icmp6-util.c @@ -21,37 +21,42 @@ #include "network-common.h" #include "socket-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 IN6ADDR_ALL_NODES_MULTICAST_INIT \ - { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } } - -static int icmp6_bind_router_message(const struct icmp6_filter *filter, - const struct ipv6_mreq *mreq) { - int ifindex = mreq->ipv6mr_interface; +int icmp6_bind(int ifindex, bool is_router) { + struct icmp6_filter filter = {}; + struct ipv6_mreq mreq; _cleanup_close_ int s = -EBADF; int r; - assert(filter); - assert(mreq); + assert(ifindex > 0); + + ICMP6_FILTER_SETBLOCKALL(&filter); + if (is_router) { + mreq = (struct ipv6_mreq) { + .ipv6mr_multiaddr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT, + .ipv6mr_interface = ifindex, + }; + ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); + } else { + mreq = (struct ipv6_mreq) { + .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT, + .ipv6mr_interface = ifindex, + }; + ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter); + } s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6); if (s < 0) return -errno; - if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, filter, sizeof(*filter)) < 0) + if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) return -errno; - if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq, sizeof(*mreq)) < 0) + if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) return -errno; - /* RFC 3315, section 6.7, bullet point 2 may indicate that an - IPV6_PKTINFO socket option also applies for ICMPv6 multicast. - Empirical experiments indicates otherwise and therefore an - IPV6_MULTICAST_IF socket option is used here instead */ + /* RFC 3315, section 6.7, bullet point 2 may indicate that an IPV6_PKTINFO socket option also applies + * for ICMPv6 multicast. Empirical experiments indicates otherwise and therefore an IPV6_MULTICAST_IF + * socket option is used here instead. */ r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex); if (r < 0) return r; @@ -83,32 +88,6 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter, return TAKE_FD(s); } -int icmp6_bind_router_solicitation(int ifindex) { - struct icmp6_filter filter = {}; - struct ipv6_mreq mreq = { - .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT, - .ipv6mr_interface = ifindex, - }; - - ICMP6_FILTER_SETBLOCKALL(&filter); - ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter); - - return icmp6_bind_router_message(&filter, &mreq); -} - -int icmp6_bind_router_advertisement(int ifindex) { - struct icmp6_filter filter = {}; - struct ipv6_mreq mreq = { - .ipv6mr_multiaddr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT, - .ipv6mr_interface = ifindex, - }; - - ICMP6_FILTER_SETBLOCKALL(&filter); - ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); - - return icmp6_bind_router_message(&filter, &mreq); -} - int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { struct sockaddr_in6 dst = { .sin6_family = AF_INET6, diff --git a/src/libsystemd-network/icmp6-util.h b/src/libsystemd-network/icmp6-util.h index 0a9ecb4c44..72db688b0b 100644 --- a/src/libsystemd-network/icmp6-util.h +++ b/src/libsystemd-network/icmp6-util.h @@ -6,6 +6,7 @@ ***/ #include +#include #include "time-util.h" @@ -17,8 +18,7 @@ { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } } -int icmp6_bind_router_solicitation(int ifindex); -int icmp6_bind_router_advertisement(int ifindex); +int icmp6_bind(int ifindex, bool is_router); int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr); int icmp6_receive( int fd, diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index ac31d212e3..ccccec3081 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -352,7 +352,7 @@ int sd_ndisc_start(sd_ndisc *nd) { if (r < 0) goto fail; - nd->fd = icmp6_bind_router_solicitation(nd->ifindex); + nd->fd = icmp6_bind(nd->ifindex, /* is_router = */ false); if (nd->fd < 0) return nd->fd; diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 97d306c49b..b770563a8c 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -402,7 +402,7 @@ int sd_radv_start(sd_radv *ra) { if (r < 0) goto fail; - r = icmp6_bind_router_advertisement(ra->ifindex); + r = icmp6_bind(ra->ifindex, /* is_router = */ true); if (r < 0) goto fail; From 6a27ca08c6f5be0ecc2868c6c455c27223b7d803 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 14 Feb 2024 15:59:38 +0900 Subject: [PATCH 2/4] sd-ndisc: several trivial cleanups - update several log messages, - use event_reset_time_relative(), - split out ndisc_setup_recv_event() and ndisc_setup_timer(). No functional change, just refactoring and preparation for later commits. --- src/libsystemd-network/sd-ndisc.c | 94 +++++++++++++++++++------------ 1 file changed, 59 insertions(+), 35 deletions(-) diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index ccccec3081..15ee2c0929 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -234,19 +234,19 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda if (r < 0) switch (r) { case -EADDRNOTAVAIL: - log_ndisc(nd, "Received RA from neither link-local nor null address. Ignoring."); + log_ndisc(nd, "Received an ICMPv6 packet from neither link-local nor null address, ignoring."); return 0; case -EMULTIHOP: - log_ndisc(nd, "Received RA with invalid hop limit. Ignoring."); + log_ndisc(nd, "Received an ICMPv6 packet with an invalid hop limit, ignoring."); return 0; case -EPFNOSUPPORT: - log_ndisc(nd, "Received invalid source address from ICMPv6 socket. Ignoring."); + log_ndisc(nd, "Received an ICMPv6 packet with an invalid source address, ignoring."); return 0; default: - log_ndisc_errno(nd, r, "Unexpected error while reading from ICMPv6, ignoring: %m"); + log_ndisc_errno(nd, r, "Unexpected error while receiving an ICMPv6 packet, ignoring: %m"); return 0; } @@ -335,9 +335,61 @@ int sd_ndisc_stop(sd_ndisc *nd) { return 1; } +static int ndisc_setup_recv_event(sd_ndisc *nd) { + int r; + + assert(nd); + assert(nd->event); + assert(nd->ifindex > 0); + + _cleanup_close_ int fd = -EBADF; + fd = icmp6_bind(nd->ifindex, /* is_router = */ false); + if (fd < 0) + return fd; + + _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; + r = sd_event_add_io(nd->event, &s, fd, EPOLLIN, ndisc_recv, nd); + if (r < 0) + return r; + + r = sd_event_source_set_priority(s, nd->event_priority); + if (r < 0) + return r; + + (void) sd_event_source_set_description(s, "ndisc-receive-router-message"); + + nd->fd = TAKE_FD(fd); + nd->recv_event_source = TAKE_PTR(s); + return 1; +} + +static int ndisc_setup_timer(sd_ndisc *nd) { + int r; + + assert(nd); + assert(nd->event); + + r = event_reset_time_relative(nd->event, &nd->timeout_event_source, + CLOCK_BOOTTIME, + USEC_PER_SEC / 2, 1 * USEC_PER_SEC, /* See RFC 8415 sec. 18.2.1 */ + ndisc_timeout, nd, + nd->event_priority, "ndisc-timeout", true); + if (r < 0) + return r; + + r = event_reset_time_relative(nd->event, &nd->timeout_no_ra, + CLOCK_BOOTTIME, + NDISC_TIMEOUT_NO_RA_USEC, 10 * USEC_PER_MSEC, + ndisc_timeout_no_ra, nd, + nd->event_priority, "ndisc-timeout-no-ra", true); + if (r < 0) + return r; + + return 0; +} + int sd_ndisc_start(sd_ndisc *nd) { int r; - usec_t time_now; assert_return(nd, -EINVAL); assert_return(nd->event, -EINVAL); @@ -346,39 +398,11 @@ int sd_ndisc_start(sd_ndisc *nd) { if (sd_ndisc_is_running(nd)) return 0; - assert(!nd->recv_event_source); - - r = sd_event_now(nd->event, CLOCK_BOOTTIME, &time_now); + r = ndisc_setup_recv_event(nd); if (r < 0) goto fail; - nd->fd = icmp6_bind(nd->ifindex, /* is_router = */ false); - if (nd->fd < 0) - return nd->fd; - - r = sd_event_add_io(nd->event, &nd->recv_event_source, nd->fd, EPOLLIN, ndisc_recv, nd); - if (r < 0) - goto fail; - - r = sd_event_source_set_priority(nd->recv_event_source, nd->event_priority); - if (r < 0) - goto fail; - - (void) sd_event_source_set_description(nd->recv_event_source, "ndisc-receive-message"); - - r = event_reset_time(nd->event, &nd->timeout_event_source, - CLOCK_BOOTTIME, - time_now + USEC_PER_SEC / 2, 1 * USEC_PER_SEC, /* See RFC 8415 sec. 18.2.1 */ - ndisc_timeout, nd, - nd->event_priority, "ndisc-timeout", true); - if (r < 0) - goto fail; - - r = event_reset_time(nd->event, &nd->timeout_no_ra, - CLOCK_BOOTTIME, - time_now + NDISC_TIMEOUT_NO_RA_USEC, 10 * USEC_PER_MSEC, - ndisc_timeout_no_ra, nd, - nd->event_priority, "ndisc-timeout-no-ra", true); + r = ndisc_setup_timer(nd); if (r < 0) goto fail; From 36653443f71ed52ec377197d37a7787584a29296 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 18 Feb 2024 12:45:48 +0900 Subject: [PATCH 3/4] sd-radv: several cleanups - split out radv_setup_recv_event(), - slightly update log messages, - use DIV_ROUND_UP(), - use structured initializer more. No functional change, just preparation for later commits. --- src/libsystemd-network/sd-radv.c | 112 ++++++++++++++++++------------- 1 file changed, 64 insertions(+), 48 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index b770563a8c..71e7a71897 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -121,30 +121,39 @@ static sd_radv *radv_free(sd_radv *ra) { DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free); static bool router_lifetime_is_valid(usec_t lifetime_usec) { + assert_cc(RADV_MAX_ROUTER_LIFETIME_USEC <= UINT16_MAX * USEC_PER_SEC); return lifetime_usec == 0 || (lifetime_usec >= RADV_MIN_ROUTER_LIFETIME_USEC && lifetime_usec <= RADV_MAX_ROUTER_LIFETIME_USEC); } -static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_usec) { +static int radv_send_router(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_usec) { + assert(ra); + assert(router_lifetime_is_valid(lifetime_usec)); + struct sockaddr_in6 dst_addr = { .sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ALL_NODES_MULTICAST_INIT, }; - struct nd_router_advert adv = {}; + struct nd_router_advert adv = { + .nd_ra_type = ND_ROUTER_ADVERT, + .nd_ra_router_lifetime = usec_to_be16_sec(lifetime_usec), + .nd_ra_retransmit = usec_to_be32_msec(ra->retransmit_usec), + }; struct { struct nd_opt_hdr opthdr; struct ether_addr slladdr; } _packed_ opt_mac = { .opthdr = { .nd_opt_type = ND_OPT_SOURCE_LINKADDR, - .nd_opt_len = (sizeof(struct nd_opt_hdr) + - sizeof(struct ether_addr) - 1) /8 + 1, + .nd_opt_len = DIV_ROUND_UP(sizeof(struct nd_opt_hdr) + sizeof(struct ether_addr), 8), }, + .slladdr = ra->mac_addr, }; struct nd_opt_mtu opt_mtu = { .nd_opt_mtu_type = ND_OPT_MTU, .nd_opt_mtu_len = 1, + .nd_opt_mtu_mtu = htobe32(ra->mtu), }; /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, N routes, N pref64 prefixes, RDNSS, * DNSSL, and home agent. */ @@ -157,9 +166,6 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_us usec_t time_now; int r; - assert(ra); - assert(router_lifetime_is_valid(lifetime_usec)); - r = sd_event_now(ra->event, CLOCK_BOOTTIME, &time_now); if (r < 0) return r; @@ -167,25 +173,19 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_us if (dst && in6_addr_is_set(dst)) dst_addr.sin6_addr = *dst; - adv.nd_ra_type = ND_ROUTER_ADVERT; + /* The nd_ra_curhoplimit and nd_ra_flags_reserved fields cannot specified with nd_ra_router_lifetime + * simultaneously in the structured initializer in the above. */ adv.nd_ra_curhoplimit = ra->hop_limit; - adv.nd_ra_retransmit = usec_to_be32_msec(ra->retransmit_usec); adv.nd_ra_flags_reserved = ra->flags; - assert_cc(RADV_MAX_ROUTER_LIFETIME_USEC <= UINT16_MAX * USEC_PER_SEC); - adv.nd_ra_router_lifetime = usec_to_be16_sec(lifetime_usec); iov[msg.msg_iovlen++] = IOVEC_MAKE(&adv, sizeof(adv)); - /* MAC address is optional, either because the link does not use L2 - addresses or load sharing is desired. See RFC 4861, Section 4.2 */ - if (!ether_addr_is_null(&ra->mac_addr)) { - opt_mac.slladdr = ra->mac_addr; + /* MAC address is optional, either because the link does not use L2 addresses or load sharing is + * desired. See RFC 4861, Section 4.2. */ + if (!ether_addr_is_null(&ra->mac_addr)) iov[msg.msg_iovlen++] = IOVEC_MAKE(&opt_mac, sizeof(opt_mac)); - } - if (ra->mtu > 0) { - opt_mtu.nd_opt_mtu_mtu = htobe32(ra->mtu); + if (ra->mtu > 0) iov[msg.msg_iovlen++] = IOVEC_MAKE(&opt_mtu, sizeof(opt_mtu)); - } LIST_FOREACH(prefix, p, ra->prefixes) { usec_t lifetime_valid_usec, lifetime_preferred_usec; @@ -263,15 +263,15 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat if (r < 0) switch (r) { case -EADDRNOTAVAIL: - log_radv(ra, "Received RS from neither link-local nor null address. Ignoring"); + log_radv(ra, "Received RS from neither link-local nor null address, ignoring."); return 0; case -EMULTIHOP: - log_radv(ra, "Received RS with invalid hop limit. Ignoring."); + log_radv(ra, "Received RS with invalid hop limit, ignoring."); return 0; case -EPFNOSUPPORT: - log_radv(ra, "Received invalid source address from ICMPv6 socket. Ignoring."); + log_radv(ra, "Received invalid source address from ICMPv6 socket, ignoring."); return 0; default: @@ -288,12 +288,11 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat * address option. See RFC 4861 Section 6.1.1. */ const char *addr = IN6_ADDR_TO_STRING(&src); - - r = radv_send(ra, &src, ra->lifetime_usec); + r = radv_send_router(ra, &src, ra->lifetime_usec); if (r < 0) log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", addr); else - log_radv(ra, "Sent solicited Router Advertisement to %s", addr); + log_radv(ra, "Sent solicited Router Advertisement to %s.", addr); return 0; } @@ -311,7 +310,7 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) { if (r < 0) goto fail; - r = radv_send(ra, NULL, ra->lifetime_usec); + r = radv_send_router(ra, NULL, ra->lifetime_usec); if (r < 0) log_radv_errno(ra, r, "Unable to send Router Advertisement, ignoring: %m"); @@ -371,9 +370,10 @@ int sd_radv_stop(sd_radv *ra) { log_radv(ra, "Stopping IPv6 Router Advertisement daemon"); - /* RFC 4861, Section 6.2.5, send at least one Router Advertisement - with zero lifetime */ - r = radv_send(ra, NULL, 0); + /* RFC 4861, Section 6.2.5: + * the router SHOULD transmit one or more (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final + * multicast Router Advertisements on the interface with a Router Lifetime field of zero. */ + r = radv_send_router(ra, NULL, 0); if (r < 0) log_radv_errno(ra, r, "Unable to send last Router Advertisement with router lifetime set to zero, ignoring: %m"); @@ -384,6 +384,34 @@ int sd_radv_stop(sd_radv *ra) { return 0; } +static int radv_setup_recv_event(sd_radv *ra) { + int r; + + assert(ra); + assert(ra->event); + assert(ra->ifindex > 0); + + _cleanup_close_ int fd = -EBADF; + fd = icmp6_bind(ra->ifindex, /* is_router = */ true); + if (fd < 0) + return fd; + + _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; + r = sd_event_add_io(ra->event, &s, fd, EPOLLIN, radv_recv, ra); + if (r < 0) + return r; + + r = sd_event_source_set_priority(s, ra->event_priority); + if (r < 0) + return r; + + (void) sd_event_source_set_description(s, "radv-receive-message"); + + ra->fd = TAKE_FD(fd); + ra->recv_event_source = TAKE_PTR(s); + return 0; +} + int sd_radv_start(sd_radv *ra) { int r; @@ -394,6 +422,10 @@ int sd_radv_start(sd_radv *ra) { if (ra->state != RADV_STATE_IDLE) return 0; + r = radv_setup_recv_event(ra); + if (r < 0) + goto fail; + r = event_reset_time(ra->event, &ra->timeout_event_source, CLOCK_BOOTTIME, 0, 0, @@ -402,22 +434,6 @@ int sd_radv_start(sd_radv *ra) { if (r < 0) goto fail; - r = icmp6_bind(ra->ifindex, /* is_router = */ true); - if (r < 0) - goto fail; - - ra->fd = r; - - r = sd_event_add_io(ra->event, &ra->recv_event_source, ra->fd, EPOLLIN, radv_recv, ra); - if (r < 0) - goto fail; - - r = sd_event_source_set_priority(ra->recv_event_source, ra->event_priority); - if (r < 0) - goto fail; - - (void) sd_event_source_set_description(ra->recv_event_source, "radv-receive-message"); - ra->state = RADV_STATE_ADVERTISING; log_radv(ra, "Started IPv6 Router Advertisement daemon"); @@ -674,7 +690,7 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) { return 0; /* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */ - r = radv_send(ra, NULL, ra->lifetime_usec); + r = radv_send_router(ra, NULL, ra->lifetime_usec); if (r < 0) log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix %s, ignoring: %m", addr_p); else @@ -770,7 +786,7 @@ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) { return 0; /* If RAs have already been sent, send an RA immediately to announce the newly-added route prefix */ - r = radv_send(ra, NULL, ra->lifetime_usec); + r = radv_send_router(ra, NULL, ra->lifetime_usec); if (r < 0) log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix %s, ignoring: %m", strna(addr_p)); @@ -842,7 +858,7 @@ int sd_radv_add_pref64_prefix(sd_radv *ra, sd_radv_pref64_prefix *p) { return 0; /* If RAs have already been sent, send an RA immediately to announce the newly-added route prefix */ - r = radv_send(ra, NULL, ra->lifetime_usec); + r = radv_send_router(ra, NULL, ra->lifetime_usec); if (r < 0) log_radv_errno(ra, r, "Unable to send Router Advertisement for added PREF64 prefix %s, ignoring: %m", strna(addr_p)); From a860df82b485e426c9fc5dfcdb9a7b7815701fe9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 19 Feb 2024 02:21:31 +0900 Subject: [PATCH 4/4] test: split out {dump,verify}_ra_message() Then, let's not modify the global object. --- src/libsystemd-network/test-ndisc-ra.c | 121 ++++++++++++++----------- 1 file changed, 66 insertions(+), 55 deletions(-) diff --git a/src/libsystemd-network/test-ndisc-ra.c b/src/libsystemd-network/test-ndisc-ra.c index 442b139f9b..c5f10cc795 100644 --- a/src/libsystemd-network/test-ndisc-ra.c +++ b/src/libsystemd-network/test-ndisc-ra.c @@ -20,37 +20,6 @@ static struct ether_addr mac_addr = { .ether_addr_octet = { 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53 } }; -static uint8_t advertisement[] = { - /* ICMPv6 Router Advertisement, no checksum */ - 0x86, 0x00, 0x00, 0x00, 0x40, 0xc0, 0x00, 0xb4, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Source Link Layer Address Option */ - 0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53, - /* Prefix Information Option */ - 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4, - 0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00, - 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Prefix Information Option */ - 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x0e, 0x10, - 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, - 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Prefix Information Option */ - 0x03, 0x04, 0x30, 0xc0, 0x00, 0x00, 0x0e, 0x10, - 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, - 0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Recursive DNS Server Option */ - 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, - 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, - /* DNS Search List Option */ - 0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, - 0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74, - 0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -}; - static bool test_stopped; static struct { struct in6_addr address; @@ -271,49 +240,91 @@ TEST(radv) { assert_se(!ra); } -static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) { - sd_radv *ra = userdata; - unsigned char buf[168]; - size_t i; +static void dump_message(const uint8_t *buf, size_t len) { + assert(len >= sizeof(struct nd_router_advert)); - assert_se(read(test_fd[0], &buf, sizeof(buf)) == sizeof(buf)); + printf("Received Router Advertisement with lifetime %i sec\n", + (buf[6] << 8) + buf[7]); - /* router lifetime must be zero when test is stopped */ - if (test_stopped) { - advertisement[6] = 0x00; - advertisement[7] = 0x00; - } - - printf ("Received Router Advertisement with lifetime %i\n", - (advertisement[6] << 8) + advertisement[7]); - - /* test only up to buf size, rest is not yet implemented */ - for (i = 0; i < sizeof(buf); i++) { + for (size_t i = 0; i < len; i++) { if (!(i % 8)) printf("%3zu: ", i); printf("0x%02x", buf[i]); - assert_se(buf[i] == advertisement[i]); - if ((i + 1) % 8) printf(", "); else printf("\n"); } +} + +static void verify_message(const uint8_t *buf, size_t len) { + static const uint8_t advertisement[] = { + /* ICMPv6 Router Advertisement, no checksum */ + 0x86, 0x00, 0x00, 0x00, 0x40, 0xc0, 0x00, 0xb4, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /* Source Link Layer Address Option */ + 0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53, + /* Prefix Information Option */ + 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4, + 0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00, + 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /* Prefix Information Option */ + 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x0e, 0x10, + 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, + 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /* Prefix Information Option */ + 0x03, 0x04, 0x30, 0xc0, 0x00, 0x00, 0x0e, 0x10, + 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, + 0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /* Recursive DNS Server Option */ + 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, + 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + /* DNS Search List Option */ + 0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, + 0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74, + 0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + + /* verify only up to known options, rest is not yet implemented */ + for (size_t i = 0, m = MIN(len, sizeof(advertisement)); i < m; i++) { + if (test_stopped) + switch (i) { + case 6 ... 7: /* router lifetime must be zero on stop. */ + assert_se(buf[i] == 0); + continue; + } + + assert_se(buf[i] == advertisement[i]); + } +} + +static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) { + sd_radv *ra = ASSERT_PTR(userdata); + _cleanup_free_ uint8_t *buf = NULL; + ssize_t buflen; + + buflen = next_datagram_size_fd(fd); + assert_se(buflen >= 0); + assert_se(buf = new0(uint8_t, buflen)); + + assert_se(read(fd, buf, buflen) == buflen); + + dump_message(buf, buflen); + verify_message(buf, buflen); if (test_stopped) { - sd_event *e; - - e = sd_radv_get_event(ra); - sd_event_exit(e, 0); - + assert_se(sd_event_exit(sd_radv_get_event(ra), 0) >= 0); return 0; } assert_se(sd_radv_stop(ra) >= 0); test_stopped = true; - return 0; }