1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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/types.h>
13 #include <unistd.h>
14 #include <net/if.h>
15 #include <linux/if_packet.h>
16 
17 #include "fd-util.h"
18 #include "icmp6-util.h"
19 #include "in-addr-util.h"
20 #include "io-util.h"
21 #include "socket-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 
icmp6_bind_router_message(const struct icmp6_filter * filter,const struct ipv6_mreq * mreq)31 static int icmp6_bind_router_message(const struct icmp6_filter *filter,
32                                      const struct ipv6_mreq *mreq) {
33         int ifindex = mreq->ipv6mr_interface;
34         _cleanup_close_ int s = -1;
35         int r;
36 
37         assert(filter);
38         assert(mreq);
39 
40         s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6);
41         if (s < 0)
42                 return -errno;
43 
44         if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, filter, sizeof(*filter)) < 0)
45                 return -errno;
46 
47         if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq, sizeof(*mreq)) < 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, ifindex);
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         r = socket_bind_to_ifindex(s, ifindex);
79         if (r < 0)
80                 return r;
81 
82         return TAKE_FD(s);
83 }
84 
icmp6_bind_router_solicitation(int ifindex)85 int icmp6_bind_router_solicitation(int ifindex) {
86         struct icmp6_filter filter = {};
87         struct ipv6_mreq mreq = {
88                 .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
89                 .ipv6mr_interface = ifindex,
90         };
91 
92         ICMP6_FILTER_SETBLOCKALL(&filter);
93         ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
94 
95         return icmp6_bind_router_message(&filter, &mreq);
96 }
97 
icmp6_bind_router_advertisement(int ifindex)98 int icmp6_bind_router_advertisement(int ifindex) {
99         struct icmp6_filter filter = {};
100         struct ipv6_mreq mreq = {
101                 .ipv6mr_multiaddr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
102                 .ipv6mr_interface = ifindex,
103         };
104 
105         ICMP6_FILTER_SETBLOCKALL(&filter);
106         ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
107 
108         return icmp6_bind_router_message(&filter, &mreq);
109 }
110 
icmp6_send_router_solicitation(int s,const struct ether_addr * ether_addr)111 int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
112         struct sockaddr_in6 dst = {
113                 .sin6_family = AF_INET6,
114                 .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
115         };
116         struct {
117                 struct nd_router_solicit rs;
118                 struct nd_opt_hdr rs_opt;
119                 struct ether_addr rs_opt_mac;
120         } _packed_ rs = {
121                 .rs.nd_rs_type = ND_ROUTER_SOLICIT,
122                 .rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR,
123                 .rs_opt.nd_opt_len = 1,
124         };
125         struct iovec iov = {
126                 .iov_base = &rs,
127                 .iov_len = sizeof(rs),
128         };
129         struct msghdr msg = {
130                 .msg_name = &dst,
131                 .msg_namelen = sizeof(dst),
132                 .msg_iov = &iov,
133                 .msg_iovlen = 1,
134         };
135 
136         assert(s >= 0);
137         assert(ether_addr);
138 
139         rs.rs_opt_mac = *ether_addr;
140 
141         if (sendmsg(s, &msg, 0) < 0)
142                 return -errno;
143 
144         return 0;
145 }
146 
icmp6_receive(int fd,void * buffer,size_t size,struct in6_addr * ret_dst,triple_timestamp * ret_timestamp)147 int icmp6_receive(int fd, void *buffer, size_t size, struct in6_addr *ret_dst,
148                   triple_timestamp *ret_timestamp) {
149 
150         /* This needs to be initialized with zero. See #20741. */
151         CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int)) + /* ttl */
152                          CMSG_SPACE_TIMEVAL) control = {};
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         struct in6_addr addr = {};
165         triple_timestamp t = {};
166         ssize_t len;
167 
168         iov = IOVEC_MAKE(buffer, size);
169 
170         len = recvmsg_safe(fd, &msg, MSG_DONTWAIT);
171         if (len < 0)
172                 return (int) len;
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                 addr = sa.in6.sin6_addr;
181                 if (!in6_addr_is_link_local(&addr))
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_TRUNC));
190 
191         CMSG_FOREACH(cmsg, &msg) {
192                 if (cmsg->cmsg_level == SOL_IPV6 &&
193                     cmsg->cmsg_type == IPV6_HOPLIMIT &&
194                     cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
195                         int hops = *(int*) CMSG_DATA(cmsg);
196 
197                         if (hops != 255)
198                                 return -EMULTIHOP;
199                 }
200 
201                 if (cmsg->cmsg_level == SOL_SOCKET &&
202                     cmsg->cmsg_type == SO_TIMESTAMP &&
203                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
204                         triple_timestamp_from_realtime(&t, timeval_load((struct timeval*) CMSG_DATA(cmsg)));
205         }
206 
207         if (!triple_timestamp_is_set(&t))
208                 triple_timestamp_get(&t);
209 
210         *ret_dst = addr;
211         *ret_timestamp = t;
212         return 0;
213 }
214