]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/icmp6-util.c
tree-wide: remove redundant assignments
[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
6142bb37
PF
84int icmp6_bind_router_solicitation(int index) {
85 struct icmp6_filter filter = {};
86 struct ipv6_mreq mreq = {
87 .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
88 .ipv6mr_interface = index,
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
97int icmp6_bind_router_advertisement(int index) {
98 struct icmp6_filter filter = {};
99 struct ipv6_mreq mreq = {
100 .ipv6mr_multiaddr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
101 .ipv6mr_interface = index,
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) {
150 union {
151 struct cmsghdr cmsghdr;
152 uint8_t buf[CMSG_SPACE(sizeof(int)) + /* ttl */
153 CMSG_SPACE(sizeof(struct timeval))];
154 } control = {};
155 struct iovec iov = {};
156 union sockaddr_union sa = {};
157 struct msghdr msg = {
158 .msg_name = &sa.sa,
159 .msg_namelen = sizeof(sa),
160 .msg_iov = &iov,
161 .msg_iovlen = 1,
162 .msg_control = &control,
163 .msg_controllen = sizeof(control),
164 };
165 struct cmsghdr *cmsg;
166 ssize_t len;
167
5cfa2c3d 168 iov = IOVEC_MAKE(buffer, size);
88d5a3db 169
2adfd1bd 170 len = recvmsg_safe(fd, &msg, MSG_DONTWAIT);
14f37112 171 if (len < 0)
2adfd1bd 172 return (int) len;
88d5a3db
PF
173
174 if ((size_t) len != size)
175 return -EINVAL;
176
177 if (msg.msg_namelen == sizeof(struct sockaddr_in6) &&
178 sa.in6.sin6_family == AF_INET6) {
179
180 *dst = sa.in6.sin6_addr;
181 if (in_addr_is_link_local(AF_INET6, (union in_addr_union*) dst) <= 0)
182 return -EADDRNOTAVAIL;
183
184 } else if (msg.msg_namelen > 0)
185 return -EPFNOSUPPORT;
186
187 /* namelen == 0 only happens when running the test-suite over a socketpair */
188
189 assert(!(msg.msg_flags & MSG_CTRUNC));
190 assert(!(msg.msg_flags & MSG_TRUNC));
191
192 CMSG_FOREACH(cmsg, &msg) {
193 if (cmsg->cmsg_level == SOL_IPV6 &&
194 cmsg->cmsg_type == IPV6_HOPLIMIT &&
195 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
196 int hops = *(int*) CMSG_DATA(cmsg);
197
198 if (hops != 255)
199 return -EMULTIHOP;
200 }
201
202 if (cmsg->cmsg_level == SOL_SOCKET &&
203 cmsg->cmsg_type == SO_TIMESTAMP &&
204 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
205 triple_timestamp_from_realtime(timestamp, timeval_load((struct timeval*) CMSG_DATA(cmsg)));
206 }
207
208 if (!triple_timestamp_is_set(timestamp))
209 triple_timestamp_get(timestamp);
210
211 return 0;
212}