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