| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2014 Intel Corporation. All rights reserved. |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <netinet/icmp6.h> |
| 7 |
#include <netinet/in.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "fd-util.h" |
| 12 |
#include "icmp6-util.h" |
| 13 |
#include "in-addr-util.h" |
| 14 |
#include "network-common.h" |
| 15 |
#include "socket-util.h" |
| 16 |
|
| 17 |
int icmp6_bind(int ifindex, bool is_router) { |
| 18 |
struct icmp6_filter filter = {}; |
| 19 |
struct ipv6_mreq mreq; |
| 20 |
_cleanup_close_ int s = -EBADF; |
| 21 |
int r; |
| 22 |
|
| 23 |
assert(ifindex > 0); |
| 24 |
|
| 25 |
ICMP6_FILTER_SETBLOCKALL(&filter); |
| 26 |
if (is_router) { |
| 27 |
mreq = (struct ipv6_mreq) { |
| 28 |
.ipv6mr_multiaddr = IN6_ADDR_ALL_ROUTERS_MULTICAST, |
| 29 |
.ipv6mr_ifindex = ifindex, |
| 30 |
}; |
| 31 |
ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); |
| 32 |
} else { |
| 33 |
mreq = (struct ipv6_mreq) { |
| 34 |
.ipv6mr_multiaddr = IN6_ADDR_ALL_NODES_MULTICAST, |
| 35 |
.ipv6mr_ifindex = ifindex, |
| 36 |
}; |
| 37 |
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter); |
| 38 |
ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter); |
| 39 |
ICMP6_FILTER_SETPASS(ND_REDIRECT, &filter); |
| 40 |
} |
| 41 |
|
| 42 |
s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6); |
| 43 |
if (s < 0) |
| 44 |
return -errno; |
| 45 |
|
| 46 |
if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) |
| 47 |
return -errno; |
| 48 |
|
| 49 |
if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) |
| 50 |
return -errno; |
| 51 |
|
| 52 |
/* RFC 3315, section 6.7, bullet point 2 may indicate that an IPV6_PKTINFO socket option also applies |
| 53 |
* for ICMPv6 multicast. Empirical experiments indicates otherwise and therefore an IPV6_MULTICAST_IF |
| 54 |
* socket option is used here instead. */ |
| 55 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
|
| 59 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, false); |
| 60 |
if (r < 0) |
| 61 |
return r; |
| 62 |
|
| 63 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); |
| 64 |
if (r < 0) |
| 65 |
return r; |
| 66 |
|
| 67 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); |
| 68 |
if (r < 0) |
| 69 |
return r; |
| 70 |
|
| 71 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
r = socket_set_recvpktinfo(s, AF_INET6, true); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
|
| 79 |
r = setsockopt_int(s, SOL_SOCKET, SO_TIMESTAMP, true); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
|
| 83 |
r = socket_bind_to_ifindex(s, ifindex); |
| 84 |
if (r < 0) |
| 85 |
return r; |
| 86 |
|
| 87 |
return TAKE_FD(s); |
| 88 |
} |
| 89 |
|
| 90 |
int icmp6_send(int fd, const struct in6_addr *dst, const struct iovec *iov, size_t n_iov) { |
| 91 |
struct sockaddr_in6 sa = { |
| 92 |
.sin6_family = AF_INET6, |
| 93 |
.sin6_addr = *ASSERT_PTR(dst), |
| 94 |
}; |
| 95 |
struct msghdr msg = { |
| 96 |
.msg_name = &sa, |
| 97 |
.msg_namelen = sizeof(struct sockaddr_in6), |
| 98 |
.msg_iov = (struct iovec*) iov, |
| 99 |
.msg_iovlen = n_iov, |
| 100 |
}; |
| 101 |
|
| 102 |
assert(fd >= 0); |
| 103 |
|
| 104 |
if (sendmsg(fd, &msg, 0) < 0) |
| 105 |
return -errno; |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
int icmp6_receive( |
| 111 |
int fd, |
| 112 |
void *buffer, |
| 113 |
size_t size, |
| 114 |
struct in6_addr *ret_sender, |
| 115 |
int *ret_ifindex, |
| 116 |
triple_timestamp *ret_timestamp) { |
| 117 |
|
| 118 |
/* This needs to be initialized with zero. See #20741. |
| 119 |
* The issue is fixed on glibc-2.35 (8fba672472ae0055387e9315fc2eddfa6775ca79). */ |
| 120 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int)) + /* ttl */ |
| 121 |
CMSG_SPACE(sizeof(struct in6_pktinfo)) + |
| 122 |
CMSG_SPACE_TIMEVAL) control = {}; |
| 123 |
struct iovec iov = { buffer, size }; |
| 124 |
union sockaddr_union sa = {}; |
| 125 |
struct msghdr msg = { |
| 126 |
.msg_name = &sa.sa, |
| 127 |
.msg_namelen = sizeof(sa), |
| 128 |
.msg_iov = &iov, |
| 129 |
.msg_iovlen = 1, |
| 130 |
.msg_control = &control, |
| 131 |
.msg_controllen = sizeof(control), |
| 132 |
}; |
| 133 |
ssize_t len; |
| 134 |
|
| 135 |
len = recvmsg_safe(fd, &msg, MSG_DONTWAIT); |
| 136 |
if (len < 0) |
| 137 |
return (int) len; |
| 138 |
|
| 139 |
if ((size_t) len != size) |
| 140 |
return -EINVAL; |
| 141 |
|
| 142 |
if (msg.msg_namelen != sizeof(struct sockaddr_in6) || sa.in6.sin6_family != AF_INET6) |
| 143 |
return -EPFNOSUPPORT; |
| 144 |
|
| 145 |
if (!in6_addr_is_link_local(&sa.in6.sin6_addr) && !in6_addr_is_null(&sa.in6.sin6_addr)) |
| 146 |
return -EADDRNOTAVAIL; |
| 147 |
|
| 148 |
assert(!(msg.msg_flags & MSG_TRUNC)); |
| 149 |
|
| 150 |
int *hops = CMSG_FIND_DATA(&msg, IPPROTO_IPV6, IPV6_HOPLIMIT, int); |
| 151 |
if (hops && *hops != 255) |
| 152 |
return -EMULTIHOP; |
| 153 |
|
| 154 |
struct in6_pktinfo *pktinfo = CMSG_FIND_DATA(&msg, IPPROTO_IPV6, IPV6_PKTINFO, struct in6_pktinfo); |
| 155 |
if (ret_ifindex) |
| 156 |
*ret_ifindex = pktinfo ? pktinfo->ipi6_ifindex : 0; |
| 157 |
|
| 158 |
if (ret_timestamp) |
| 159 |
triple_timestamp_from_cmsg(ret_timestamp, &msg); |
| 160 |
if (ret_sender) |
| 161 |
*ret_sender = sa.in6.sin6_addr; |
| 162 |
return 0; |
| 163 |
} |