]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/icmp6-util.c
Merge pull request #16336 from yuwata/ifindex-cleanups
[thirdparty/systemd.git] / src / libsystemd-network / icmp6-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
940367a0 2/***
810adae9 3 Copyright © 2014 Intel Corporation. All rights reserved.
940367a0
TG
4***/
5
6#include <errno.h>
940367a0
TG
7#include <netinet/icmp6.h>
8#include <netinet/in.h>
3ffd4af2
LP
9#include <netinet/ip6.h>
10#include <stdio.h>
11#include <string.h>
3ffd4af2
LP
12#include <sys/types.h>
13#include <unistd.h>
15fec93b 14#include <net/if.h>
3ffd4af2 15#include <linux/if_packet.h>
940367a0 16
3ffd4af2 17#include "fd-util.h"
940367a0 18#include "icmp6-util.h"
88d5a3db 19#include "in-addr-util.h"
5cfa2c3d
LP
20#include "io-util.h"
21#include "socket-util.h"
940367a0
TG
22
23#define IN6ADDR_ALL_ROUTERS_MULTICAST_INIT \
24 { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } } }
26
27#define IN6ADDR_ALL_NODES_MULTICAST_INIT \
28 { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } }
30
6142bb37
PF
31static int icmp6_bind_router_message(const struct icmp6_filter *filter,
32 const struct ipv6_mreq *mreq) {
953a02d1 33 int ifindex = mreq->ipv6mr_interface;
940367a0 34 _cleanup_close_ int s = -1;
1e7a0e21 35 int r;
940367a0 36
cddf4d81 37 s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6);
940367a0
TG
38 if (s < 0)
39 return -errno;
40
6142bb37
PF
41 r = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, filter, sizeof(*filter));
42 if (r < 0)
43 return -errno;
44
45 r = setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq, sizeof(*mreq));
940367a0
TG
46 if (r < 0)
47 return -errno;
48
49 /* RFC 3315, section 6.7, bullet point 2 may indicate that an
50 IPV6_PKTINFO socket option also applies for ICMPv6 multicast.
51 Empirical experiments indicates otherwise and therefore an
52 IPV6_MULTICAST_IF socket option is used here instead */
953a02d1 53 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex);
940367a0 54 if (r < 0)
9e5b6496 55 return r;
940367a0 56
2ff48e98 57 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, false);
940367a0 58 if (r < 0)
2ff48e98 59 return r;
940367a0 60
9e5b6496 61 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255);
940367a0 62 if (r < 0)
9e5b6496 63 return r;
940367a0 64
9e5b6496 65 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255);
cddf4d81 66 if (r < 0)
9e5b6496 67 return r;
cddf4d81 68
2ff48e98 69 r = setsockopt_int(s, SOL_IPV6, IPV6_RECVHOPLIMIT, true);
940367a0 70 if (r < 0)
2ff48e98 71 return r;
940367a0 72
2ff48e98 73 r = setsockopt_int(s, SOL_SOCKET, SO_TIMESTAMP, true);
1e7a0e21 74 if (r < 0)
2ff48e98 75 return r;
1e7a0e21 76
953a02d1 77 r = socket_bind_to_ifindex(s, ifindex);
15fec93b 78 if (r < 0)
953a02d1 79 return r;
15fec93b 80
c10d6bdb 81 return TAKE_FD(s);
940367a0
TG
82}
83
1a6c9136 84int icmp6_bind_router_solicitation(int ifindex) {
6142bb37
PF
85 struct icmp6_filter filter = {};
86 struct ipv6_mreq mreq = {
87 .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
1a6c9136 88 .ipv6mr_interface = ifindex,
6142bb37
PF
89 };
90
91 ICMP6_FILTER_SETBLOCKALL(&filter);
92 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
93
94 return icmp6_bind_router_message(&filter, &mreq);
95}
96
1a6c9136 97int icmp6_bind_router_advertisement(int ifindex) {
6142bb37
PF
98 struct icmp6_filter filter = {};
99 struct ipv6_mreq mreq = {
100 .ipv6mr_multiaddr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
1a6c9136 101 .ipv6mr_interface = ifindex,
6142bb37
PF
102 };
103
104 ICMP6_FILTER_SETBLOCKALL(&filter);
105 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
106
107 return icmp6_bind_router_message(&filter, &mreq);
108}
109
940367a0
TG
110int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
111 struct sockaddr_in6 dst = {
112 .sin6_family = AF_INET6,
113 .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
114 };
115 struct {
116 struct nd_router_solicit rs;
117 struct nd_opt_hdr rs_opt;
118 struct ether_addr rs_opt_mac;
119 } _packed_ rs = {
120 .rs.nd_rs_type = ND_ROUTER_SOLICIT,
6d06ac1f
TG
121 .rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR,
122 .rs_opt.nd_opt_len = 1,
940367a0 123 };
6d06ac1f
TG
124 struct iovec iov = {
125 .iov_base = &rs,
126 .iov_len = sizeof(rs),
940367a0
TG
127 };
128 struct msghdr msg = {
129 .msg_name = &dst,
130 .msg_namelen = sizeof(dst),
6d06ac1f 131 .msg_iov = &iov,
940367a0
TG
132 .msg_iovlen = 1,
133 };
134 int r;
135
6d06ac1f
TG
136 assert(s >= 0);
137 assert(ether_addr);
138
139 rs.rs_opt_mac = *ether_addr;
940367a0
TG
140
141 r = sendmsg(s, &msg, 0);
142 if (r < 0)
143 return -errno;
144
145 return 0;
146}
88d5a3db
PF
147
148int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *dst,
149 triple_timestamp *timestamp) {
fb29cdbe
LP
150
151 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int)) + /* ttl */
152 CMSG_SPACE(sizeof(struct timeval))) control;
88d5a3db
PF
153 struct iovec iov = {};
154 union sockaddr_union sa = {};
155 struct msghdr msg = {
156 .msg_name = &sa.sa,
157 .msg_namelen = sizeof(sa),
158 .msg_iov = &iov,
159 .msg_iovlen = 1,
160 .msg_control = &control,
161 .msg_controllen = sizeof(control),
162 };
163 struct cmsghdr *cmsg;
164 ssize_t len;
165
5cfa2c3d 166 iov = IOVEC_MAKE(buffer, size);
88d5a3db 167
2adfd1bd 168 len = recvmsg_safe(fd, &msg, MSG_DONTWAIT);
14f37112 169 if (len < 0)
2adfd1bd 170 return (int) len;
88d5a3db
PF
171
172 if ((size_t) len != size)
173 return -EINVAL;
174
175 if (msg.msg_namelen == sizeof(struct sockaddr_in6) &&
176 sa.in6.sin6_family == AF_INET6) {
177
178 *dst = sa.in6.sin6_addr;
179 if (in_addr_is_link_local(AF_INET6, (union in_addr_union*) dst) <= 0)
180 return -EADDRNOTAVAIL;
181
182 } else if (msg.msg_namelen > 0)
183 return -EPFNOSUPPORT;
184
185 /* namelen == 0 only happens when running the test-suite over a socketpair */
186
187 assert(!(msg.msg_flags & MSG_CTRUNC));
188 assert(!(msg.msg_flags & MSG_TRUNC));
189
190 CMSG_FOREACH(cmsg, &msg) {
191 if (cmsg->cmsg_level == SOL_IPV6 &&
192 cmsg->cmsg_type == IPV6_HOPLIMIT &&
193 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
194 int hops = *(int*) CMSG_DATA(cmsg);
195
196 if (hops != 255)
197 return -EMULTIHOP;
198 }
199
200 if (cmsg->cmsg_level == SOL_SOCKET &&
201 cmsg->cmsg_type == SO_TIMESTAMP &&
202 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
203 triple_timestamp_from_realtime(timestamp, timeval_load((struct timeval*) CMSG_DATA(cmsg)));
204 }
205
206 if (!triple_timestamp_is_set(timestamp))
207 triple_timestamp_get(timestamp);
208
209 return 0;
210}