icmp6-util: merge icmp6_bind_router_{solicitation,advertisement}() into icmp6_bind()

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2024-02-24 12:37:50 +09:00
parent afdf63fcf2
commit 37c011e74c
5 changed files with 31 additions and 56 deletions
+3 -7
View 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) {
+24 -45
View File
@@ -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,
+2 -2
View 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,
+1 -1
View 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;
+1 -1
View 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;