]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
icmp6-util: merge icmp6_bind_router_{solicitation,advertisement}() into icmp6_bind()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 Feb 2024 07:45:55 +0000 (16:45 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 24 Feb 2024 03:37:50 +0000 (12:37 +0900)
No functional change, just refactoring.

src/libsystemd-network/icmp6-util-unix.c
src/libsystemd-network/icmp6-util.c
src/libsystemd-network/icmp6-util.h
src/libsystemd-network/sd-ndisc.c
src/libsystemd-network/sd-radv.c

index 01edb854df3fb1a6d3aa4526c84882433adb0f77..5eac4a0e5357ef27a88b030067998ac613acb6b0 100644 (file)
@@ -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) {
index 72c20baadc8968061f5a43b03e9d7cc8b0ae0b5d..2224d17cba1a8101c05a859e2d1e91e082b1af8a 100644 (file)
 #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,
index 0a9ecb4c44d72cdc7c1481a5646a7a4a00e58bef..72db688b0b9e8c6ab21f905a4ecb973d9d615048 100644 (file)
@@ -6,6 +6,7 @@
 ***/
 
 #include <net/ethernet.h>
+#include <stdbool.h>
 
 #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,
index ac31d212e39ad0dcbe909cae04deeb7b4ca0cea5..ccccec30813e185b82503f8b02b9f790788ccd9c 100644 (file)
@@ -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;
 
index 97d306c49b2b067039bf77996acf6e8edca57261..b770563a8c01916a7c8db99de47c6cd36e25fd27 100644 (file)
@@ -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;