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