]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | /*** | |
3 | Copyright © 2014 Intel Corporation. All rights reserved. | |
4 | ***/ | |
5 | ||
6 | #include <netinet/icmp6.h> | |
7 | #include <netinet/in.h> | |
8 | #include <stdio.h> | |
9 | #include <string.h> | |
10 | ||
11 | #include "fd-util.h" | |
12 | #include "icmp6-util.h" | |
13 | #include "in-addr-util.h" | |
14 | #include "network-common.h" | |
15 | #include "socket-util.h" | |
16 | ||
17 | int icmp6_bind(int ifindex, bool is_router) { | |
18 | struct icmp6_filter filter = {}; | |
19 | struct ipv6_mreq mreq; | |
20 | _cleanup_close_ int s = -EBADF; | |
21 | int r; | |
22 | ||
23 | assert(ifindex > 0); | |
24 | ||
25 | ICMP6_FILTER_SETBLOCKALL(&filter); | |
26 | if (is_router) { | |
27 | mreq = (struct ipv6_mreq) { | |
28 | .ipv6mr_multiaddr = IN6_ADDR_ALL_ROUTERS_MULTICAST, | |
29 | .ipv6mr_ifindex = ifindex, | |
30 | }; | |
31 | ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); | |
32 | } else { | |
33 | mreq = (struct ipv6_mreq) { | |
34 | .ipv6mr_multiaddr = IN6_ADDR_ALL_NODES_MULTICAST, | |
35 | .ipv6mr_ifindex = ifindex, | |
36 | }; | |
37 | ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter); | |
38 | ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter); | |
39 | ICMP6_FILTER_SETPASS(ND_REDIRECT, &filter); | |
40 | } | |
41 | ||
42 | s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6); | |
43 | if (s < 0) | |
44 | return -errno; | |
45 | ||
46 | if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) | |
47 | return -errno; | |
48 | ||
49 | if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) | |
50 | return -errno; | |
51 | ||
52 | /* RFC 3315, section 6.7, bullet point 2 may indicate that an IPV6_PKTINFO socket option also applies | |
53 | * for ICMPv6 multicast. Empirical experiments indicates otherwise and therefore an IPV6_MULTICAST_IF | |
54 | * socket option is used here instead. */ | |
55 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex); | |
56 | if (r < 0) | |
57 | return r; | |
58 | ||
59 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, false); | |
60 | if (r < 0) | |
61 | return r; | |
62 | ||
63 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); | |
64 | if (r < 0) | |
65 | return r; | |
66 | ||
67 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); | |
68 | if (r < 0) | |
69 | return r; | |
70 | ||
71 | r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); | |
72 | if (r < 0) | |
73 | return r; | |
74 | ||
75 | r = setsockopt_int(s, SOL_SOCKET, SO_TIMESTAMP, true); | |
76 | if (r < 0) | |
77 | return r; | |
78 | ||
79 | r = socket_bind_to_ifindex(s, ifindex); | |
80 | if (r < 0) | |
81 | return r; | |
82 | ||
83 | return TAKE_FD(s); | |
84 | } | |
85 | ||
86 | int icmp6_send(int fd, const struct in6_addr *dst, const struct iovec *iov, size_t n_iov) { | |
87 | struct sockaddr_in6 sa = { | |
88 | .sin6_family = AF_INET6, | |
89 | .sin6_addr = *ASSERT_PTR(dst), | |
90 | }; | |
91 | struct msghdr msg = { | |
92 | .msg_name = &sa, | |
93 | .msg_namelen = sizeof(struct sockaddr_in6), | |
94 | .msg_iov = (struct iovec*) iov, | |
95 | .msg_iovlen = n_iov, | |
96 | }; | |
97 | ||
98 | assert(fd >= 0); | |
99 | ||
100 | if (sendmsg(fd, &msg, 0) < 0) | |
101 | return -errno; | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | int icmp6_receive( | |
107 | int fd, | |
108 | void *buffer, | |
109 | size_t size, | |
110 | struct in6_addr *ret_sender, | |
111 | triple_timestamp *ret_timestamp) { | |
112 | ||
113 | /* This needs to be initialized with zero. See #20741. | |
114 | * The issue is fixed on glibc-2.35 (8fba672472ae0055387e9315fc2eddfa6775ca79). */ | |
115 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int)) + /* ttl */ | |
116 | CMSG_SPACE_TIMEVAL) control = {}; | |
117 | struct iovec iov = { buffer, size }; | |
118 | union sockaddr_union sa = {}; | |
119 | struct msghdr msg = { | |
120 | .msg_name = &sa.sa, | |
121 | .msg_namelen = sizeof(sa), | |
122 | .msg_iov = &iov, | |
123 | .msg_iovlen = 1, | |
124 | .msg_control = &control, | |
125 | .msg_controllen = sizeof(control), | |
126 | }; | |
127 | ssize_t len; | |
128 | ||
129 | len = recvmsg_safe(fd, &msg, MSG_DONTWAIT); | |
130 | if (len < 0) | |
131 | return (int) len; | |
132 | ||
133 | if ((size_t) len != size) | |
134 | return -EINVAL; | |
135 | ||
136 | if (msg.msg_namelen != sizeof(struct sockaddr_in6) || sa.in6.sin6_family != AF_INET6) | |
137 | return -EPFNOSUPPORT; | |
138 | ||
139 | if (!in6_addr_is_link_local(&sa.in6.sin6_addr) && !in6_addr_is_null(&sa.in6.sin6_addr)) | |
140 | return -EADDRNOTAVAIL; | |
141 | ||
142 | assert(!(msg.msg_flags & MSG_TRUNC)); | |
143 | ||
144 | int *hops = CMSG_FIND_DATA(&msg, IPPROTO_IPV6, IPV6_HOPLIMIT, int); | |
145 | if (hops && *hops != 255) | |
146 | return -EMULTIHOP; | |
147 | ||
148 | if (ret_timestamp) | |
149 | triple_timestamp_from_cmsg(ret_timestamp, &msg); | |
150 | if (ret_sender) | |
151 | *ret_sender = sa.in6.sin6_addr; | |
152 | return 0; | |
153 | } |