]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
940367a0 | 2 | /*** |
810adae9 | 3 | Copyright © 2014 Intel Corporation. All rights reserved. |
940367a0 TG |
4 | ***/ |
5 | ||
940367a0 TG |
6 | #include <netinet/icmp6.h> |
7 | #include <netinet/in.h> | |
3ffd4af2 LP |
8 | #include <stdio.h> |
9 | #include <string.h> | |
940367a0 | 10 | |
3ffd4af2 | 11 | #include "fd-util.h" |
940367a0 | 12 | #include "icmp6-util.h" |
88d5a3db | 13 | #include "in-addr-util.h" |
dd59d5e5 | 14 | #include "network-common.h" |
5cfa2c3d | 15 | #include "socket-util.h" |
940367a0 | 16 | |
37c011e7 YW |
17 | int icmp6_bind(int ifindex, bool is_router) { |
18 | struct icmp6_filter filter = {}; | |
19 | struct ipv6_mreq mreq; | |
5bb1d7fb | 20 | _cleanup_close_ int s = -EBADF; |
1e7a0e21 | 21 | int r; |
940367a0 | 22 | |
37c011e7 YW |
23 | assert(ifindex > 0); |
24 | ||
25 | ICMP6_FILTER_SETBLOCKALL(&filter); | |
26 | if (is_router) { | |
27 | mreq = (struct ipv6_mreq) { | |
2c28eb02 | 28 | .ipv6mr_multiaddr = IN6_ADDR_ALL_ROUTERS_MULTICAST, |
d559f463 | 29 | .ipv6mr_ifindex = ifindex, |
37c011e7 YW |
30 | }; |
31 | ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); | |
32 | } else { | |
33 | mreq = (struct ipv6_mreq) { | |
2c28eb02 | 34 | .ipv6mr_multiaddr = IN6_ADDR_ALL_NODES_MULTICAST, |
d559f463 | 35 | .ipv6mr_ifindex = ifindex, |
37c011e7 YW |
36 | }; |
37 | ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter); | |
696eb2b8 | 38 | ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter); |
44e8cf30 | 39 | ICMP6_FILTER_SETPASS(ND_REDIRECT, &filter); |
37c011e7 | 40 | } |
113e124f | 41 | |
cddf4d81 | 42 | s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6); |
940367a0 TG |
43 | if (s < 0) |
44 | return -errno; | |
45 | ||
37c011e7 | 46 | if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) |
6142bb37 PF |
47 | return -errno; |
48 | ||
37c011e7 | 49 | if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) |
940367a0 TG |
50 | return -errno; |
51 | ||
37c011e7 YW |
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. */ | |
953a02d1 | 55 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex); |
940367a0 | 56 | if (r < 0) |
9e5b6496 | 57 | return r; |
940367a0 | 58 | |
2ff48e98 | 59 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, false); |
940367a0 | 60 | if (r < 0) |
2ff48e98 | 61 | return r; |
940367a0 | 62 | |
9e5b6496 | 63 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); |
940367a0 | 64 | if (r < 0) |
9e5b6496 | 65 | return r; |
940367a0 | 66 | |
9e5b6496 | 67 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); |
cddf4d81 | 68 | if (r < 0) |
9e5b6496 | 69 | return r; |
cddf4d81 | 70 | |
d559f463 | 71 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); |
940367a0 | 72 | if (r < 0) |
2ff48e98 | 73 | return r; |
940367a0 | 74 | |
2ff48e98 | 75 | r = setsockopt_int(s, SOL_SOCKET, SO_TIMESTAMP, true); |
1e7a0e21 | 76 | if (r < 0) |
2ff48e98 | 77 | return r; |
1e7a0e21 | 78 | |
953a02d1 | 79 | r = socket_bind_to_ifindex(s, ifindex); |
15fec93b | 80 | if (r < 0) |
953a02d1 | 81 | return r; |
15fec93b | 82 | |
c10d6bdb | 83 | return TAKE_FD(s); |
940367a0 TG |
84 | } |
85 | ||
ac336e75 YW |
86 | int icmp6_send(int fd, const struct in6_addr *dst, const struct iovec *iov, size_t n_iov) { |
87 | struct sockaddr_in6 sa = { | |
88 | .sin6_family = AF_INET6, | |
89 | .sin6_addr = *ASSERT_PTR(dst), | |
90 | }; | |
ca114462 | 91 | struct msghdr msg = { |
ac336e75 | 92 | .msg_name = &sa, |
ca114462 YW |
93 | .msg_namelen = sizeof(struct sockaddr_in6), |
94 | .msg_iov = (struct iovec*) iov, | |
95 | .msg_iovlen = n_iov, | |
96 | }; | |
97 | ||
ac336e75 YW |
98 | assert(fd >= 0); |
99 | ||
ca114462 YW |
100 | if (sendmsg(fd, &msg, 0) < 0) |
101 | return -errno; | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
51211638 YW |
106 | int icmp6_receive( |
107 | int fd, | |
108 | void *buffer, | |
109 | size_t size, | |
110 | struct in6_addr *ret_sender, | |
111 | triple_timestamp *ret_timestamp) { | |
fb29cdbe | 112 | |
c7464843 YW |
113 | /* This needs to be initialized with zero. See #20741. |
114 | * The issue is fixed on glibc-2.35 (8fba672472ae0055387e9315fc2eddfa6775ca79). */ | |
fb29cdbe | 115 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int)) + /* ttl */ |
f782eee6 | 116 | CMSG_SPACE_TIMEVAL) control = {}; |
f7a6418d | 117 | struct iovec iov = { buffer, size }; |
88d5a3db PF |
118 | union sockaddr_union sa = {}; |
119 | struct msghdr msg = { | |
120 | .msg_name = &sa.sa, | |
121 | .msg_namelen = sizeof(sa), | |
122 | .msg_iov = &iov, | |
123 | .msg_iovlen = 1, | |
124 | .msg_control = &control, | |
125 | .msg_controllen = sizeof(control), | |
126 | }; | |
88d5a3db PF |
127 | ssize_t len; |
128 | ||
2adfd1bd | 129 | len = recvmsg_safe(fd, &msg, MSG_DONTWAIT); |
14f37112 | 130 | if (len < 0) |
2adfd1bd | 131 | return (int) len; |
88d5a3db PF |
132 | |
133 | if ((size_t) len != size) | |
134 | return -EINVAL; | |
135 | ||
9f0430b7 | 136 | if (msg.msg_namelen != sizeof(struct sockaddr_in6) || sa.in6.sin6_family != AF_INET6) |
88d5a3db PF |
137 | return -EPFNOSUPPORT; |
138 | ||
9f0430b7 YW |
139 | if (!in6_addr_is_link_local(&sa.in6.sin6_addr) && !in6_addr_is_null(&sa.in6.sin6_addr)) |
140 | return -EADDRNOTAVAIL; | |
88d5a3db | 141 | |
88d5a3db PF |
142 | assert(!(msg.msg_flags & MSG_TRUNC)); |
143 | ||
d559f463 | 144 | int *hops = CMSG_FIND_DATA(&msg, IPPROTO_IPV6, IPV6_HOPLIMIT, int); |
dd59d5e5 YW |
145 | if (hops && *hops != 255) |
146 | return -EMULTIHOP; | |
88d5a3db | 147 | |
dd59d5e5 YW |
148 | if (ret_timestamp) |
149 | triple_timestamp_from_cmsg(ret_timestamp, &msg); | |
51211638 | 150 | if (ret_sender) |
9f0430b7 | 151 | *ret_sender = sa.in6.sin6_addr; |
88d5a3db PF |
152 | return 0; |
153 | } |