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