Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #include "patchpanel/ndproxy.h" |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 6 | |
| 7 | #include <errno.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
Taoyu Li | f0370c9 | 2019-09-18 15:04:37 +0900 | [diff] [blame] | 11 | #include <sysexits.h> |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <arpa/inet.h> |
Taoyu Li | fbcda45 | 2019-09-11 16:57:25 +0900 | [diff] [blame] | 15 | #include <linux/filter.h> |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 16 | #include <linux/if_packet.h> |
| 17 | #include <linux/in6.h> |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 18 | #include <linux/netlink.h> |
| 19 | #include <linux/rtnetlink.h> |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 20 | #include <net/ethernet.h> |
| 21 | #include <net/if.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <sys/socket.h> |
| 24 | |
| 25 | #include <string> |
Taoyu Li | f0370c9 | 2019-09-18 15:04:37 +0900 | [diff] [blame] | 26 | #include <utility> |
| 27 | |
| 28 | #include <base/bind.h> |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 29 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 30 | #include "patchpanel/minijailed_process_runner.h" |
| 31 | #include "patchpanel/net_util.h" |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 32 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 33 | namespace patchpanel { |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 34 | namespace { |
Taoyu Li | fbcda45 | 2019-09-11 16:57:25 +0900 | [diff] [blame] | 35 | const unsigned char kBroadcastMacAddress[] = {0xff, 0xff, 0xff, |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 36 | 0xff, 0xff, 0xff}; |
Taoyu Li | fbcda45 | 2019-09-11 16:57:25 +0900 | [diff] [blame] | 37 | |
| 38 | sock_filter kNDFrameBpfInstructions[] = { |
| 39 | // Load ethernet type. |
| 40 | BPF_STMT(BPF_LD | BPF_H | BPF_ABS, offsetof(ether_header, ether_type)), |
| 41 | // Check if it equals IPv6, if not, then goto return 0. |
| 42 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_IPV6, 0, 9), |
| 43 | // Move index to start of IPv6 header. |
| 44 | BPF_STMT(BPF_LDX | BPF_IMM, sizeof(ether_header)), |
| 45 | // Load IPv6 next header. |
| 46 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, offsetof(ip6_hdr, ip6_nxt)), |
| 47 | // Check if equals ICMPv6, if not, then goto return 0. |
| 48 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 6), |
| 49 | // Move index to start of ICMPv6 header. |
| 50 | BPF_STMT(BPF_LDX | BPF_IMM, sizeof(ether_header) + sizeof(ip6_hdr)), |
| 51 | // Load ICMPv6 type. |
| 52 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, offsetof(icmp6_hdr, icmp6_type)), |
| 53 | // Check if is ND ICMPv6 message. |
| 54 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_ROUTER_SOLICIT, 4, 0), |
| 55 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_ROUTER_ADVERT, 3, 0), |
| 56 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 2, 0), |
| 57 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_ADVERT, 1, 0), |
| 58 | // Return 0. |
| 59 | BPF_STMT(BPF_RET | BPF_K, 0), |
| 60 | // Return MAX. |
| 61 | BPF_STMT(BPF_RET | BPF_K, IP_MAXPACKET), |
| 62 | }; |
| 63 | const sock_fprog kNDFrameBpfProgram = { |
| 64 | .len = sizeof(kNDFrameBpfInstructions) / sizeof(sock_filter), |
| 65 | .filter = kNDFrameBpfInstructions}; |
| 66 | |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 67 | } // namespace |
| 68 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 69 | constexpr ssize_t NDProxy::kTranslateErrorNotICMPv6Frame; |
| 70 | constexpr ssize_t NDProxy::kTranslateErrorNotNDFrame; |
| 71 | constexpr ssize_t NDProxy::kTranslateErrorInsufficientLength; |
| 72 | constexpr ssize_t NDProxy::kTranslateErrorBufferMisaligned; |
| 73 | |
| 74 | NDProxy::NDProxy() |
| 75 | : in_frame_buffer_(AlignFrameBuffer(in_frame_buffer_extended_)), |
| 76 | out_frame_buffer_(AlignFrameBuffer(out_frame_buffer_extended_)) {} |
| 77 | |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 78 | base::ScopedFD NDProxy::PreparePacketSocket() { |
| 79 | base::ScopedFD fd( |
| 80 | socket(AF_PACKET, SOCK_RAW | SOCK_CLOEXEC, htons(ETH_P_IPV6))); |
| 81 | if (!fd.is_valid()) { |
| 82 | PLOG(ERROR) << "socket() failed"; |
| 83 | return base::ScopedFD(); |
| 84 | } |
| 85 | if (setsockopt(fd.get(), SOL_SOCKET, SO_ATTACH_FILTER, &kNDFrameBpfProgram, |
| 86 | sizeof(kNDFrameBpfProgram))) { |
| 87 | PLOG(ERROR) << "setsockopt(SO_ATTACH_FILTER) failed"; |
| 88 | return base::ScopedFD(); |
| 89 | } |
| 90 | return fd; |
| 91 | } |
| 92 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 93 | bool NDProxy::Init() { |
| 94 | rtnl_fd_ = base::ScopedFD( |
| 95 | socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)); |
| 96 | if (!rtnl_fd_.is_valid()) { |
| 97 | PLOG(ERROR) << "socket() failed for rtnetlink socket"; |
| 98 | return false; |
| 99 | } |
| 100 | sockaddr_nl local = { |
| 101 | .nl_family = AF_NETLINK, |
| 102 | .nl_groups = 0, |
| 103 | }; |
| 104 | if (bind(rtnl_fd_.get(), reinterpret_cast<sockaddr*>(&local), sizeof(local)) < |
| 105 | 0) { |
| 106 | PLOG(ERROR) << "bind() failed on rtnetlink socket"; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | dummy_fd_ = base::ScopedFD(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 111 | if (!dummy_fd_.is_valid()) { |
| 112 | PLOG(ERROR) << "socket() failed for dummy socket"; |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 117 | |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 118 | // In an ICMPv6 Ethernet Frame *frame with length frame_len, replace the mac |
| 119 | // address in option opt_type into *target_mac. nd_hdr_len indicates the length |
| 120 | // of ICMPv6 ND message headers (so the first option starts after nd_hdr_len.) |
| 121 | void NDProxy::ReplaceMacInIcmpOption(uint8_t* frame, |
| 122 | ssize_t frame_len, |
| 123 | size_t nd_hdr_len, |
| 124 | uint8_t opt_type, |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 125 | const MacAddress& target_mac) { |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 126 | nd_opt_hdr* opt; |
| 127 | nd_opt_hdr* end = reinterpret_cast<nd_opt_hdr*>(&frame[frame_len]); |
| 128 | for (opt = reinterpret_cast<nd_opt_hdr*>(frame + ETHER_HDR_LEN + |
| 129 | sizeof(ip6_hdr) + nd_hdr_len); |
| 130 | opt < end && opt->nd_opt_len > 0; |
| 131 | opt = reinterpret_cast<nd_opt_hdr*>(reinterpret_cast<uint64_t*>(opt) + |
| 132 | opt->nd_opt_len)) { |
| 133 | if (opt->nd_opt_type == opt_type) { |
| 134 | uint8_t* mac_in_opt = |
| 135 | reinterpret_cast<uint8_t*>(opt) + sizeof(nd_opt_hdr); |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 136 | memcpy(mac_in_opt, target_mac.data(), ETHER_ADDR_LEN); |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // RFC 4389 |
| 142 | // Read the input ICMPv6 frame and determine whether it should be proxied. If |
| 143 | // so, fill out_frame buffer with proxied frame and return the length of proxied |
| 144 | // frame (usually same with input frame length). Return a negative value if |
| 145 | // proxy is not needed or error occured. |
Taoyu Li | 18041fe | 2019-11-13 15:49:31 +0900 | [diff] [blame] | 146 | // in_frame: buffer containing input ethernet frame; needs special alignment |
| 147 | // so that IP header is 4-bytes aligned; |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 148 | // frame_len: the length of input frame; |
| 149 | // local_mac_addr: MAC address of interface that will be used to send frame; |
Taoyu Li | 18041fe | 2019-11-13 15:49:31 +0900 | [diff] [blame] | 150 | // out_frame: buffer for output frame; should have at least space of frame_len; |
| 151 | // needs special alignment so that IP header is 4-bytes aligned. |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 152 | ssize_t NDProxy::TranslateNDFrame(const uint8_t* in_frame, |
| 153 | ssize_t frame_len, |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 154 | const MacAddress& local_mac_addr, |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 155 | uint8_t* out_frame) { |
Taoyu Li | 18041fe | 2019-11-13 15:49:31 +0900 | [diff] [blame] | 156 | if ((reinterpret_cast<uintptr_t>(in_frame + ETHER_HDR_LEN) & 0x3) != 0 || |
| 157 | (reinterpret_cast<uintptr_t>(out_frame + ETHER_HDR_LEN) & 0x3) != 0) { |
| 158 | return kTranslateErrorBufferMisaligned; |
| 159 | } |
Taoyu Li | cc621b2 | 2019-10-21 15:03:12 +0900 | [diff] [blame] | 160 | if (frame_len < ETHER_HDR_LEN + sizeof(ip6_hdr) + sizeof(icmp6_hdr)) { |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 161 | return kTranslateErrorInsufficientLength; |
| 162 | } |
| 163 | if (reinterpret_cast<const ethhdr*>(in_frame)->h_proto != htons(ETH_P_IPV6) || |
| 164 | reinterpret_cast<const ip6_hdr*>(in_frame + ETHER_HDR_LEN)->ip6_nxt != |
| 165 | IPPROTO_ICMPV6) { |
| 166 | return kTranslateErrorNotICMPv6Frame; |
| 167 | } |
| 168 | |
| 169 | memcpy(out_frame, in_frame, frame_len); |
| 170 | ethhdr* eth = reinterpret_cast<ethhdr*>(out_frame); |
| 171 | ip6_hdr* ip6 = reinterpret_cast<ip6_hdr*>(out_frame + ETHER_HDR_LEN); |
| 172 | icmp6_hdr* icmp6 = |
| 173 | reinterpret_cast<icmp6_hdr*>(out_frame + ETHER_HDR_LEN + sizeof(ip6_hdr)); |
| 174 | |
| 175 | // If destination MAC is unicast (Individual/Group bit in MAC address == 0), |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 176 | // it needs to be modified so guest OS L3 stack can see it. |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 177 | // For proxy cascading case, we also need to recheck if destination MAC is |
| 178 | // ff:ff:ff:ff:ff:ff (which must have been filled by an upstream proxy). |
| 179 | if (!(eth->h_dest[0] & 0x1) || |
| 180 | memcmp(eth->h_dest, kBroadcastMacAddress, ETHER_ADDR_LEN) == 0) { |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 181 | MacAddress neighbor_mac; |
| 182 | if (GetNeighborMac(ip6->ip6_dst, &neighbor_mac)) { |
| 183 | memcpy(eth->h_dest, neighbor_mac.data(), ETHER_ADDR_LEN); |
| 184 | } else { |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 185 | // If we can't resolve the destination IP into MAC from kernel neighbor |
| 186 | // table, fill destination MAC with broadcast MAC instead. |
| 187 | memcpy(eth->h_dest, kBroadcastMacAddress, ETHER_ADDR_LEN); |
| 188 | } |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | switch (icmp6->icmp6_type) { |
| 192 | case ND_ROUTER_SOLICIT: |
| 193 | ReplaceMacInIcmpOption(out_frame, frame_len, sizeof(nd_router_solicit), |
| 194 | ND_OPT_SOURCE_LINKADDR, local_mac_addr); |
| 195 | break; |
| 196 | case ND_ROUTER_ADVERT: { |
| 197 | // RFC 4389 Section 4.1.3.3 - Set Proxy bit |
| 198 | nd_router_advert* ra = reinterpret_cast<nd_router_advert*>(icmp6); |
| 199 | if (ra->nd_ra_flags_reserved & 0x04) { |
| 200 | // According to RFC 4389, an RA packet with 'Proxy' bit set already |
| 201 | // should not be proxied again, in order to avoid loop. However, we'll |
| 202 | // need this form of proxy cascading in Crostini (Host->VM->Container) |
| 203 | // so we are ignoring the check here. Note that we know we are doing RA |
| 204 | // proxy in only one direction so there should be no loop. |
| 205 | } |
| 206 | ra->nd_ra_flags_reserved |= 0x04; |
| 207 | |
| 208 | ReplaceMacInIcmpOption(out_frame, frame_len, sizeof(nd_router_advert), |
| 209 | ND_OPT_SOURCE_LINKADDR, local_mac_addr); |
| 210 | break; |
| 211 | } |
| 212 | case ND_NEIGHBOR_SOLICIT: |
| 213 | ReplaceMacInIcmpOption(out_frame, frame_len, sizeof(nd_neighbor_solicit), |
| 214 | ND_OPT_SOURCE_LINKADDR, local_mac_addr); |
| 215 | break; |
| 216 | case ND_NEIGHBOR_ADVERT: |
| 217 | ReplaceMacInIcmpOption(out_frame, frame_len, sizeof(nd_neighbor_advert), |
| 218 | ND_OPT_TARGET_LINKADDR, local_mac_addr); |
| 219 | break; |
| 220 | default: |
| 221 | return kTranslateErrorNotNDFrame; |
| 222 | } |
| 223 | |
| 224 | // We need to clear the old checksum first so checksum calculation does not |
| 225 | // wrongly take old checksum into account. |
| 226 | icmp6->icmp6_cksum = 0; |
| 227 | icmp6->icmp6_cksum = Icmpv6Checksum(ip6, icmp6); |
| 228 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 229 | memcpy(eth->h_source, local_mac_addr.data(), ETHER_ADDR_LEN); |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 230 | return frame_len; |
| 231 | } |
| 232 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 233 | void NDProxy::ReadAndProcessOneFrame(int fd) { |
| 234 | sockaddr_ll dst_addr; |
| 235 | struct iovec iov = { |
| 236 | .iov_base = in_frame_buffer_, |
| 237 | .iov_len = IP_MAXPACKET, |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 238 | }; |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 239 | msghdr hdr = { |
| 240 | .msg_name = &dst_addr, |
| 241 | .msg_namelen = sizeof(dst_addr), |
| 242 | .msg_iov = &iov, |
| 243 | .msg_iovlen = 1, |
| 244 | .msg_control = nullptr, |
| 245 | .msg_controllen = 0, |
| 246 | .msg_flags = 0, |
| 247 | }; |
| 248 | |
| 249 | ssize_t len; |
| 250 | if ((len = recvmsg(fd, &hdr, 0)) < 0) { |
| 251 | PLOG(ERROR) << "recvmsg() failed"; |
| 252 | return; |
| 253 | } |
| 254 | ip6_hdr* ip6 = reinterpret_cast<ip6_hdr*>(in_frame_buffer_ + ETH_HLEN); |
| 255 | icmp6_hdr* icmp6 = reinterpret_cast<icmp6_hdr*>( |
| 256 | in_frame_buffer_ + ETHER_HDR_LEN + sizeof(ip6_hdr)); |
| 257 | |
| 258 | if (ip6->ip6_nxt != IPPROTO_ICMPV6 || icmp6->icmp6_type < ND_ROUTER_SOLICIT || |
| 259 | icmp6->icmp6_type > ND_NEIGHBOR_ADVERT) |
| 260 | return; |
| 261 | |
Taoyu Li | c48871b | 2019-12-18 12:57:25 +0900 | [diff] [blame] | 262 | // Notify DeviceManager on receiving NA from guest, so a /128 route to the |
| 263 | // guest can be added on the host. |
| 264 | if (icmp6->icmp6_type == ND_NEIGHBOR_ADVERT && |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 265 | IsGuestInterface(dst_addr.sll_ifindex) && |
| 266 | !guest_discovery_handler_.is_null()) { |
Taoyu Li | c48871b | 2019-12-18 12:57:25 +0900 | [diff] [blame] | 267 | nd_neighbor_advert* na = reinterpret_cast<nd_neighbor_advert*>(icmp6); |
Taoyu Li | 76d8658 | 2020-03-27 14:19:50 +0900 | [diff] [blame] | 268 | if (((na->nd_na_target.s6_addr[0] & 0xe0) == 0x20) // Global Unicast |
| 269 | || ((na->nd_na_target.s6_addr[0] & 0xfe) == 0xfc)) { // Unique Local |
Taoyu Li | c48871b | 2019-12-18 12:57:25 +0900 | [diff] [blame] | 270 | char ifname[IFNAMSIZ]; |
| 271 | if_indextoname(dst_addr.sll_ifindex, ifname); |
| 272 | char ipv6_addr_str[INET6_ADDRSTRLEN]; |
| 273 | inet_ntop(AF_INET6, &(na->nd_na_target.s6_addr), ipv6_addr_str, |
| 274 | INET6_ADDRSTRLEN); |
| 275 | guest_discovery_handler_.Run(std::string(ifname), |
| 276 | std::string(ipv6_addr_str)); |
| 277 | } |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 278 | } |
| 279 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 280 | auto map_entry = MapForType(icmp6->icmp6_type)->find(dst_addr.sll_ifindex); |
| 281 | if (map_entry == MapForType(icmp6->icmp6_type)->end()) |
| 282 | return; |
| 283 | const auto& target_ifs = map_entry->second; |
| 284 | for (int target_if : target_ifs) { |
| 285 | MacAddress local_mac; |
| 286 | if (!GetLocalMac(target_if, &local_mac)) |
| 287 | continue; |
| 288 | int result = |
| 289 | TranslateNDFrame(in_frame_buffer_, len, local_mac, out_frame_buffer_); |
| 290 | if (result < 0) { |
| 291 | switch (result) { |
| 292 | case kTranslateErrorNotICMPv6Frame: |
| 293 | LOG(DFATAL) << "Attempt to TranslateNDFrame on a non-ICMPv6 frame"; |
| 294 | return; |
| 295 | case kTranslateErrorNotNDFrame: |
| 296 | LOG(DFATAL) << "Attempt to TranslateNDFrame on a non-NDP frame, " |
| 297 | "icmpv6 type = " |
| 298 | << static_cast<int>(reinterpret_cast<icmp6_hdr*>( |
| 299 | in_frame_buffer_ + ETHER_HDR_LEN + |
| 300 | sizeof(ip6_hdr)) |
| 301 | ->icmp6_type); |
| 302 | return; |
| 303 | case kTranslateErrorInsufficientLength: |
| 304 | LOG(DFATAL) << "TranslateNDFrame failed: frame_len = " << len |
| 305 | << " is too small"; |
| 306 | return; |
| 307 | default: |
| 308 | LOG(DFATAL) << "Unknown error in TranslateNDFrame"; |
| 309 | return; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | struct iovec iov_out = { |
| 314 | .iov_base = out_frame_buffer_, |
| 315 | .iov_len = static_cast<size_t>(len), |
| 316 | }; |
| 317 | sockaddr_ll addr = { |
| 318 | .sll_family = AF_PACKET, |
| 319 | .sll_protocol = htons(ETH_P_IPV6), |
| 320 | .sll_ifindex = target_if, |
| 321 | .sll_halen = ETHER_ADDR_LEN, |
| 322 | }; |
| 323 | memcpy(addr.sll_addr, reinterpret_cast<ethhdr*>(out_frame_buffer_)->h_dest, |
| 324 | ETHER_ADDR_LEN); |
| 325 | msghdr hdr = { |
| 326 | .msg_name = &addr, |
| 327 | .msg_namelen = sizeof(addr), |
| 328 | .msg_iov = &iov_out, |
| 329 | .msg_iovlen = 1, |
| 330 | .msg_control = nullptr, |
| 331 | .msg_controllen = 0, |
| 332 | }; |
| 333 | if (sendmsg(fd, &hdr, 0) < 0) { |
| 334 | PLOG(ERROR) << "sendmsg() failed on interface " << target_if; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | bool NDProxy::GetLocalMac(int if_id, MacAddress* mac_addr) { |
| 340 | ifreq ifr = { |
| 341 | .ifr_ifindex = if_id, |
| 342 | }; |
| 343 | if (ioctl(dummy_fd_.get(), SIOCGIFNAME, &ifr) < 0) { |
| 344 | PLOG(ERROR) << "ioctl() failed to get interface name on interface " |
| 345 | << if_id; |
| 346 | return false; |
| 347 | } |
| 348 | if (ioctl(dummy_fd_.get(), SIOCGIFHWADDR, &ifr) < 0) { |
| 349 | PLOG(ERROR) << "ioctl() failed to get MAC address on interface " << if_id; |
| 350 | return false; |
| 351 | } |
| 352 | memcpy(mac_addr->data(), ifr.ifr_addr.sa_data, ETHER_ADDR_LEN); |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | bool NDProxy::GetNeighborMac(const in6_addr& ipv6_addr, MacAddress* mac_addr) { |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 357 | sockaddr_nl kernel = { |
| 358 | .nl_family = AF_NETLINK, |
| 359 | .nl_groups = 0, |
| 360 | }; |
| 361 | struct nl_req { |
| 362 | nlmsghdr hdr; |
| 363 | rtgenmsg gen; |
| 364 | } req = { |
| 365 | .hdr = |
| 366 | { |
| 367 | .nlmsg_len = NLMSG_LENGTH(sizeof(rtgenmsg)), |
| 368 | .nlmsg_type = RTM_GETNEIGH, |
| 369 | .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP, |
| 370 | .nlmsg_seq = 1, |
| 371 | }, |
| 372 | .gen = |
| 373 | { |
| 374 | .rtgen_family = AF_INET6, |
| 375 | }, |
| 376 | }; |
| 377 | iovec io_req = { |
| 378 | .iov_base = &req, |
| 379 | .iov_len = req.hdr.nlmsg_len, |
| 380 | }; |
| 381 | msghdr rtnl_req = { |
| 382 | .msg_name = &kernel, |
| 383 | .msg_namelen = sizeof(kernel), |
| 384 | .msg_iov = &io_req, |
| 385 | .msg_iovlen = 1, |
| 386 | }; |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 387 | if (sendmsg(rtnl_fd_.get(), &rtnl_req, 0) < 0) { |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 388 | PLOG(ERROR) << "sendmsg() failed on rtnetlink socket"; |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | static constexpr size_t kRtnlReplyBufferSize = 32768; |
| 393 | char reply_buffer[kRtnlReplyBufferSize]; |
| 394 | iovec io_reply = { |
| 395 | .iov_base = reply_buffer, |
| 396 | .iov_len = kRtnlReplyBufferSize, |
| 397 | }; |
| 398 | msghdr rtnl_reply = { |
| 399 | .msg_name = &kernel, |
| 400 | .msg_namelen = sizeof(kernel), |
| 401 | .msg_iov = &io_reply, |
| 402 | .msg_iovlen = 1, |
| 403 | }; |
| 404 | |
| 405 | bool any_entry_matched = false; |
| 406 | bool done = false; |
| 407 | while (!done) { |
| 408 | ssize_t len; |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 409 | if ((len = recvmsg(rtnl_fd_.get(), &rtnl_reply, 0)) < 0) { |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 410 | PLOG(ERROR) << "recvmsg() failed on rtnetlink socket"; |
| 411 | return false; |
| 412 | } |
| 413 | for (nlmsghdr* msg_ptr = reinterpret_cast<nlmsghdr*>(reply_buffer); |
| 414 | NLMSG_OK(msg_ptr, len); msg_ptr = NLMSG_NEXT(msg_ptr, len)) { |
| 415 | switch (msg_ptr->nlmsg_type) { |
| 416 | case NLMSG_DONE: { |
| 417 | done = true; |
| 418 | break; |
| 419 | } |
| 420 | case RTM_NEWNEIGH: { |
| 421 | // Bitmap - 0x1: Found IP match; 0x2: found MAC address; |
| 422 | uint8_t current_entry_status = 0x0; |
| 423 | uint8_t current_mac[ETHER_ADDR_LEN]; |
| 424 | ndmsg* nd_msg = reinterpret_cast<ndmsg*>(NLMSG_DATA(msg_ptr)); |
| 425 | rtattr* rt_attr = reinterpret_cast<rtattr*>(RTM_RTA(nd_msg)); |
| 426 | size_t rt_attr_len = RTM_PAYLOAD(msg_ptr); |
| 427 | for (; RTA_OK(rt_attr, rt_attr_len); |
| 428 | rt_attr = RTA_NEXT(rt_attr, rt_attr_len)) { |
| 429 | if (rt_attr->rta_type == NDA_DST && |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 430 | memcmp(&ipv6_addr, RTA_DATA(rt_attr), sizeof(in6_addr)) == 0) { |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 431 | current_entry_status |= 0x1; |
| 432 | } else if (rt_attr->rta_type == NDA_LLADDR) { |
| 433 | current_entry_status |= 0x2; |
| 434 | memcpy(current_mac, RTA_DATA(rt_attr), ETHER_ADDR_LEN); |
| 435 | } |
| 436 | } |
| 437 | if (current_entry_status == 0x3) { |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 438 | memcpy(mac_addr->data(), current_mac, ETHER_ADDR_LEN); |
Taoyu Li | 43f6688 | 2019-10-25 16:59:34 +0900 | [diff] [blame] | 439 | any_entry_matched = true; |
| 440 | } |
| 441 | break; |
| 442 | } |
| 443 | default: { |
| 444 | LOG(WARNING) << "received unexpected rtnetlink message type " |
| 445 | << msg_ptr->nlmsg_type << ", length " |
| 446 | << msg_ptr->nlmsg_len; |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | return any_entry_matched; |
| 453 | } |
| 454 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 455 | void NDProxy::RegisterOnGuestIpDiscoveryHandler( |
| 456 | const base::Callback<void(const std::string&, const std::string&)>& |
| 457 | handler) { |
| 458 | guest_discovery_handler_ = handler; |
| 459 | } |
| 460 | |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 461 | NDProxy::interface_mapping* NDProxy::MapForType(uint8_t type) { |
| 462 | switch (type) { |
| 463 | case ND_ROUTER_SOLICIT: |
| 464 | return &if_map_rs_; |
| 465 | case ND_ROUTER_ADVERT: |
| 466 | return &if_map_ra_; |
| 467 | case ND_NEIGHBOR_SOLICIT: |
| 468 | return &if_map_ns_na_; |
| 469 | case ND_NEIGHBOR_ADVERT: |
| 470 | return &if_map_ns_na_; |
| 471 | default: |
| 472 | LOG(DFATAL) << "Attempt to get interface map on illegal icmpv6 type " |
| 473 | << static_cast<int>(type); |
| 474 | return nullptr; |
| 475 | } |
| 476 | } |
| 477 | |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 478 | bool NDProxy::AddInterfacePair(const std::string& ifname_physical, |
| 479 | const std::string& ifname_guest) { |
| 480 | LOG(INFO) << "Adding interface pair between physical: " << ifname_physical |
| 481 | << ", guest: " << ifname_guest; |
| 482 | int ifid_physical = if_nametoindex(ifname_physical.c_str()); |
| 483 | if (ifid_physical == 0) { |
| 484 | PLOG(ERROR) << "Get interface index failed on " << ifname_physical; |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 485 | return false; |
| 486 | } |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 487 | int ifid_guest = if_nametoindex(ifname_guest.c_str()); |
| 488 | if (ifid_guest == 0) { |
| 489 | PLOG(ERROR) << "Get interface index failed on " << ifname_guest; |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 490 | return false; |
| 491 | } |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 492 | if (ifid_physical == ifid_guest) { |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 493 | LOG(ERROR) << "Rejected attempt to forward between same interface " |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 494 | << ifname_physical << " and " << ifname_guest; |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 495 | return false; |
| 496 | } |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 497 | if_map_rs_[ifid_guest].insert(ifid_physical); |
| 498 | if_map_ra_[ifid_physical].insert(ifid_guest); |
| 499 | if_map_ns_na_[ifid_physical].insert(ifid_guest); |
| 500 | if_map_ns_na_[ifid_guest].insert(ifid_physical); |
| 501 | for (int ifid_other_guest : if_map_ra_[ifid_physical]) { |
| 502 | if (ifid_other_guest != ifid_guest) { |
| 503 | if_map_ns_na_[ifid_other_guest].insert(ifid_guest); |
| 504 | if_map_ns_na_[ifid_guest].insert(ifid_other_guest); |
| 505 | } |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 506 | } |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 507 | return true; |
| 508 | } |
| 509 | |
| 510 | bool NDProxy::RemoveInterfacePair(const std::string& ifname_physical, |
| 511 | const std::string& ifname_guest) { |
| 512 | LOG(INFO) << "Removing interface pair between physical: " << ifname_physical |
| 513 | << ", guest: " << ifname_guest; |
| 514 | int ifid_physical = if_nametoindex(ifname_physical.c_str()); |
| 515 | if (ifid_physical == 0) { |
| 516 | PLOG(ERROR) << "Get interface index failed on " << ifname_physical; |
| 517 | return false; |
| 518 | } |
| 519 | int ifid_guest = if_nametoindex(ifname_guest.c_str()); |
| 520 | if (ifid_guest == 0) { |
| 521 | PLOG(ERROR) << "Get interface index failed on " << ifname_guest; |
| 522 | return false; |
| 523 | } |
| 524 | if (ifid_physical == ifid_guest) { |
| 525 | LOG(ERROR) << "Rejected attempt to forward between same interface " |
| 526 | << ifname_physical << " and " << ifname_guest; |
| 527 | return false; |
| 528 | } |
| 529 | if_map_rs_.erase(ifid_guest); |
| 530 | if_map_ra_[ifid_physical].erase(ifid_guest); |
| 531 | if_map_ns_na_.erase(ifid_guest); |
| 532 | if_map_ns_na_[ifid_physical].erase(ifid_guest); |
| 533 | for (int ifid_other_guest : if_map_ra_[ifid_physical]) { |
| 534 | if_map_ns_na_[ifid_other_guest].erase(ifid_guest); |
| 535 | } |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
| 539 | bool NDProxy::RemoveInterface(const std::string& ifname) { |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 540 | LOG(INFO) << "Removing physical interface " << ifname; |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 541 | int ifindex = if_nametoindex(ifname.c_str()); |
| 542 | if (ifindex == 0) { |
| 543 | PLOG(ERROR) << "Get interface index failed on " << ifname; |
| 544 | return false; |
| 545 | } |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 546 | for (int ifid_guest : if_map_ra_[ifindex]) { |
| 547 | if_map_rs_.erase(ifid_guest); |
| 548 | if_map_ns_na_.erase(ifid_guest); |
| 549 | } |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 550 | if_map_ra_.erase(ifindex); |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 551 | if_map_ns_na_.erase(ifindex); |
Taoyu Li | aa6238b | 2019-09-06 17:38:52 +0900 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 555 | bool NDProxy::IsGuestInterface(int ifindex) { |
| 556 | return if_map_rs_.find(ifindex) != if_map_rs_.end(); |
| 557 | } |
| 558 | |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 559 | NDProxyDaemon::NDProxyDaemon(base::ScopedFD control_fd) |
| 560 | : msg_dispatcher_( |
| 561 | std::make_unique<MessageDispatcher>(std::move(control_fd))) {} |
| 562 | |
| 563 | NDProxyDaemon::~NDProxyDaemon() {} |
| 564 | |
| 565 | int NDProxyDaemon::OnInit() { |
| 566 | // Prevent the main process from sending us any signals. |
| 567 | if (setsid() < 0) { |
| 568 | PLOG(ERROR) << "Failed to created a new session with setsid: exiting"; |
| 569 | return EX_OSERR; |
| 570 | } |
| 571 | |
| 572 | EnterChildProcessJail(); |
| 573 | |
| 574 | // Register control fd callbacks |
| 575 | if (msg_dispatcher_) { |
| 576 | msg_dispatcher_->RegisterFailureHandler(base::Bind( |
| 577 | &NDProxyDaemon::OnParentProcessExit, weak_factory_.GetWeakPtr())); |
| 578 | msg_dispatcher_->RegisterDeviceMessageHandler(base::Bind( |
| 579 | &NDProxyDaemon::OnDeviceMessage, weak_factory_.GetWeakPtr())); |
| 580 | } |
| 581 | |
| 582 | // Initialize NDProxy and register guest IP discovery callback |
| 583 | if (!proxy_.Init()) { |
| 584 | PLOG(ERROR) << "Failed to initialize NDProxy internal state"; |
| 585 | return EX_OSERR; |
| 586 | } |
| 587 | proxy_.RegisterOnGuestIpDiscoveryHandler(base::Bind( |
| 588 | &NDProxyDaemon::OnGuestIpDiscovery, weak_factory_.GetWeakPtr())); |
| 589 | |
| 590 | // Initialize data fd |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 591 | fd_ = NDProxy::PreparePacketSocket(); |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 592 | if (!fd_.is_valid()) { |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 593 | return EX_OSERR; |
| 594 | } |
| 595 | |
| 596 | // Start watching on data fd |
| 597 | watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 598 | fd_.get(), base::Bind(&NDProxyDaemon::OnDataSocketReadReady, |
| 599 | weak_factory_.GetWeakPtr())); |
| 600 | LOG(INFO) << "Started watching on packet fd..."; |
| 601 | |
| 602 | return Daemon::OnInit(); |
| 603 | } |
| 604 | |
| 605 | void NDProxyDaemon::OnDataSocketReadReady() { |
| 606 | proxy_.ReadAndProcessOneFrame(fd_.get()); |
| 607 | } |
| 608 | |
| 609 | void NDProxyDaemon::OnParentProcessExit() { |
| 610 | LOG(ERROR) << "Quitting because the parent process died"; |
| 611 | Quit(); |
| 612 | } |
| 613 | |
| 614 | void NDProxyDaemon::OnDeviceMessage(const DeviceMessage& msg) { |
| 615 | const std::string& dev_ifname = msg.dev_ifname(); |
| 616 | LOG_IF(DFATAL, dev_ifname.empty()) |
| 617 | << "Received DeviceMessage w/ empty dev_ifname"; |
| 618 | if (msg.has_teardown()) { |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 619 | if (msg.has_br_ifname()) { |
| 620 | proxy_.RemoveInterfacePair(dev_ifname, msg.br_ifname()); |
| 621 | } else { |
| 622 | proxy_.RemoveInterface(dev_ifname); |
| 623 | } |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 624 | } else if (msg.has_br_ifname()) { |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 625 | proxy_.AddInterfacePair(dev_ifname, msg.br_ifname()); |
Taoyu Li | e47df4a | 2019-11-08 12:48:57 +0900 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | void NDProxyDaemon::OnGuestIpDiscovery(const std::string& ifname, |
| 630 | const std::string& ip6addr) { |
| 631 | // Send information back to DeviceManager |
| 632 | if (!msg_dispatcher_) |
| 633 | return; |
| 634 | DeviceMessage msg; |
| 635 | msg.set_dev_ifname(ifname); |
| 636 | msg.set_guest_ip6addr(ip6addr); |
| 637 | IpHelperMessage ipm; |
| 638 | *ipm.mutable_device_message() = msg; |
| 639 | msg_dispatcher_->SendMessage(ipm); |
| 640 | } |
| 641 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 642 | } // namespace patchpanel |