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