Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +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/datapath.h" |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 6 | |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <linux/if_tun.h> |
| 10 | #include <linux/sockios.h> |
| 11 | #include <net/if.h> |
| 12 | #include <net/if_arp.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/ioctl.h> |
| 16 | #include <sys/socket.h> |
| 17 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 20 | #include <base/files/scoped_file.h> |
| 21 | #include <base/logging.h> |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 22 | #include <base/posix/eintr_wrapper.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 23 | #include <base/strings/string_number_conversions.h> |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 24 | #include <brillo/userdb_utils.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 26 | #include "patchpanel/adb_proxy.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 27 | #include "patchpanel/net_util.h" |
| 28 | #include "patchpanel/scoped_ns.h" |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 29 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 30 | namespace patchpanel { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 31 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 32 | namespace { |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 33 | // TODO(hugobenichi) Consolidate this constant definition in a single place. |
| 34 | constexpr pid_t kTestPID = -2; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 35 | constexpr char kDefaultIfname[] = "vmtap%d"; |
| 36 | constexpr char kTunDev[] = "/dev/net/tun"; |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 37 | constexpr char kArcAddr[] = "100.115.92.2"; |
| 38 | constexpr char kLocalhostAddr[] = "127.0.0.1"; |
| 39 | constexpr uint16_t kAdbServerPort = 5555; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 40 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 41 | std::string PrefixIfname(const std::string& prefix, const std::string& ifname) { |
| 42 | std::string n = prefix + ifname; |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 43 | if (n.length() < IFNAMSIZ) |
| 44 | return n; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 45 | |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 46 | // Best effort attempt to preserve the interface number, assuming it's the |
| 47 | // last char in the name. |
| 48 | auto c = ifname[ifname.length() - 1]; |
| 49 | n.resize(IFNAMSIZ - 1); |
| 50 | n[n.length() - 1] = c; |
| 51 | return n; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 52 | } |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 53 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 54 | } // namespace |
| 55 | |
| 56 | std::string ArcVethHostName(const std::string& ifname) { |
| 57 | return PrefixIfname("veth", ifname); |
| 58 | } |
| 59 | |
| 60 | std::string ArcBridgeName(const std::string& ifname) { |
| 61 | return PrefixIfname("arc_", ifname); |
| 62 | } |
| 63 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 64 | Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall) |
| 65 | : Datapath(process_runner, firewall, ioctl) {} |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 66 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 67 | Datapath::Datapath(MinijailedProcessRunner* process_runner, |
| 68 | Firewall* firewall, |
| 69 | ioctl_t ioctl_hook) |
| 70 | : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 71 | CHECK(process_runner_); |
| 72 | } |
| 73 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 74 | MinijailedProcessRunner& Datapath::runner() const { |
| 75 | return *process_runner_; |
| 76 | } |
| 77 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 78 | bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) { |
| 79 | // Try first to delete any netns with name |netns_name| in case patchpanel |
| 80 | // did not exit cleanly. |
| 81 | if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0) |
| 82 | LOG(INFO) << "Deleted left over network namespace name " << netns_name; |
| 83 | return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0; |
| 84 | } |
| 85 | |
| 86 | bool Datapath::NetnsDeleteName(const std::string& netns_name) { |
| 87 | return process_runner_->ip_netns_delete(netns_name) == 0; |
| 88 | } |
| 89 | |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 90 | bool Datapath::AddBridge(const std::string& ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 91 | uint32_t ipv4_addr, |
| 92 | uint32_t ipv4_prefix_len) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 93 | // Configure the persistent Chrome OS bridge interface with static IP. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 94 | if (process_runner_->brctl("addbr", {ifname}) != 0) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 98 | if (process_runner_->ip( |
| 99 | "addr", "add", |
| 100 | {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd", |
| 101 | IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)), |
| 102 | "dev", ifname}) != 0) { |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 103 | RemoveBridge(ifname); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 108 | RemoveBridge(ifname); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules. |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 113 | if (!AddOutboundIPv4SNATMark(ifname)) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 114 | RemoveBridge(ifname); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void Datapath::RemoveBridge(const std::string& ifname) { |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 122 | RemoveOutboundIPv4SNATMark(ifname); |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 123 | process_runner_->ip("link", "set", {ifname, "down"}); |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 124 | process_runner_->brctl("delbr", {ifname}); |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 127 | bool Datapath::AddToBridge(const std::string& br_ifname, |
| 128 | const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 129 | return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0); |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 132 | std::string Datapath::AddTAP(const std::string& name, |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 133 | const MacAddress* mac_addr, |
| 134 | const SubnetAddress* ipv4_addr, |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 135 | const std::string& user) { |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 136 | base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK)); |
| 137 | if (!dev.is_valid()) { |
| 138 | PLOG(ERROR) << "Failed to open " << kTunDev; |
| 139 | return ""; |
| 140 | } |
| 141 | |
| 142 | struct ifreq ifr; |
| 143 | memset(&ifr, 0, sizeof(ifr)); |
| 144 | strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(), |
| 145 | sizeof(ifr.ifr_name)); |
| 146 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 147 | |
| 148 | // If a template was given as the name, ifr_name will be updated with the |
| 149 | // actual interface name. |
| 150 | if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 151 | PLOG(ERROR) << "Failed to create tap interface " << name; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 152 | return ""; |
| 153 | } |
| 154 | const char* ifname = ifr.ifr_name; |
| 155 | |
| 156 | if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 157 | PLOG(ERROR) << "Failed to persist the interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 158 | return ""; |
| 159 | } |
| 160 | |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 161 | if (!user.empty()) { |
| 162 | uid_t uid = -1; |
| 163 | if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) { |
| 164 | PLOG(ERROR) << "Unable to look up UID for " << user; |
| 165 | RemoveTAP(ifname); |
| 166 | return ""; |
| 167 | } |
| 168 | if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) { |
| 169 | PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface " |
| 170 | << ifname; |
| 171 | RemoveTAP(ifname); |
| 172 | return ""; |
| 173 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 174 | } |
| 175 | |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 176 | // Create control socket for configuring the interface. |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 177 | base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 178 | if (!sock.is_valid()) { |
| 179 | PLOG(ERROR) << "Failed to create control socket for tap interface " |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 180 | << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 181 | RemoveTAP(ifname); |
| 182 | return ""; |
| 183 | } |
| 184 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 185 | if (ipv4_addr) { |
| 186 | struct sockaddr_in* addr = |
| 187 | reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr); |
| 188 | addr->sin_family = AF_INET; |
| 189 | addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address()); |
| 190 | if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) { |
| 191 | PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname |
| 192 | << " {" << ipv4_addr->ToCidrString() << "}"; |
| 193 | RemoveTAP(ifname); |
| 194 | return ""; |
| 195 | } |
| 196 | |
| 197 | struct sockaddr_in* netmask = |
| 198 | reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask); |
| 199 | netmask->sin_family = AF_INET; |
| 200 | netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask()); |
| 201 | if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) { |
| 202 | PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname |
| 203 | << " {" << ipv4_addr->ToCidrString() << "}"; |
| 204 | RemoveTAP(ifname); |
| 205 | return ""; |
| 206 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 209 | if (mac_addr) { |
| 210 | struct sockaddr* hwaddr = &ifr.ifr_hwaddr; |
| 211 | hwaddr->sa_family = ARPHRD_ETHER; |
| 212 | memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr)); |
| 213 | if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) { |
| 214 | PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname |
| 215 | << " {" << MacAddressToString(*mac_addr) << "}"; |
| 216 | RemoveTAP(ifname); |
| 217 | return ""; |
| 218 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 222 | PLOG(ERROR) << "Failed to get flags for tap interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 223 | RemoveTAP(ifname); |
| 224 | return ""; |
| 225 | } |
| 226 | |
| 227 | ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
| 228 | if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 229 | PLOG(ERROR) << "Failed to enable tap interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 230 | RemoveTAP(ifname); |
| 231 | return ""; |
| 232 | } |
| 233 | |
| 234 | return ifname; |
| 235 | } |
| 236 | |
| 237 | void Datapath::RemoveTAP(const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 238 | process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"}); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 241 | bool Datapath::ConnectVethPair(pid_t netns_pid, |
| 242 | const std::string& netns_name, |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 243 | const std::string& veth_ifname, |
| 244 | const std::string& peer_ifname, |
| 245 | const MacAddress& remote_mac_addr, |
| 246 | uint32_t remote_ipv4_addr, |
| 247 | uint32_t remote_ipv4_prefix_len, |
| 248 | bool remote_multicast_flag) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 249 | // Set up the virtual pair across the current namespace and |netns_name|. |
| 250 | if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) { |
| 251 | LOG(ERROR) << "Failed to create veth pair " << veth_ifname << "," |
| 252 | << peer_ifname; |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | // Configure the remote veth in namespace |netns_name|. |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 257 | { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 258 | ScopedNS ns(netns_pid); |
| 259 | if (!ns.IsValid() && netns_pid != kTestPID) { |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 260 | LOG(ERROR) |
| 261 | << "Cannot create virtual link -- invalid container namespace?"; |
| 262 | return false; |
| 263 | } |
| 264 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 265 | if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr, |
| 266 | remote_ipv4_prefix_len, true /* link up */, |
| 267 | remote_multicast_flag)) { |
| 268 | LOG(ERROR) << "Failed to configure interface " << peer_ifname; |
| 269 | RemoveInterface(peer_ifname); |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 274 | if (!ToggleInterface(veth_ifname, true /*up*/)) { |
| 275 | LOG(ERROR) << "Failed to bring up interface " << veth_ifname; |
| 276 | RemoveInterface(veth_ifname); |
| 277 | return false; |
| 278 | } |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 279 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 283 | bool Datapath::AddVirtualInterfacePair(const std::string& netns_name, |
| 284 | const std::string& veth_ifname, |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 285 | const std::string& peer_ifname) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 286 | return process_runner_->ip("link", "add", |
| 287 | {veth_ifname, "type", "veth", "peer", "name", |
| 288 | peer_ifname, "netns", netns_name}) == 0; |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 289 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 290 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 291 | bool Datapath::ToggleInterface(const std::string& ifname, bool up) { |
| 292 | const std::string link = up ? "up" : "down"; |
| 293 | return process_runner_->ip("link", "set", {ifname, link}) == 0; |
| 294 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 295 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 296 | bool Datapath::ConfigureInterface(const std::string& ifname, |
| 297 | const MacAddress& mac_addr, |
| 298 | uint32_t ipv4_addr, |
| 299 | uint32_t ipv4_prefix_len, |
| 300 | bool up, |
| 301 | bool enable_multicast) { |
| 302 | const std::string link = up ? "up" : "down"; |
| 303 | const std::string multicast = enable_multicast ? "on" : "off"; |
| 304 | return (process_runner_->ip( |
| 305 | "addr", "add", |
| 306 | {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd", |
| 307 | IPv4AddressToString( |
| 308 | Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)), |
| 309 | "dev", ifname}) == 0) && |
| 310 | (process_runner_->ip("link", "set", |
| 311 | { |
| 312 | "dev", |
| 313 | ifname, |
| 314 | link, |
| 315 | "addr", |
| 316 | MacAddressToString(mac_addr), |
| 317 | "multicast", |
| 318 | multicast, |
| 319 | }) == 0); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void Datapath::RemoveInterface(const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 323 | process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 324 | } |
| 325 | |
Hugo Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 326 | bool Datapath::AddSourceIPv4DropRule(const std::string& oif, |
| 327 | const std::string& src_ip) { |
| 328 | return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s", |
| 329 | src_ip, "-j", "DROP", "-w"}) == 0; |
| 330 | } |
| 331 | |
| 332 | bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif, |
| 333 | const std::string& src_ip) { |
| 334 | return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s", |
| 335 | src_ip, "-j", "DROP", "-w"}) == 0; |
| 336 | } |
| 337 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 338 | void Datapath::StartRoutingDevice(const std::string& ext_ifname, |
| 339 | const std::string& int_ifname, |
| 340 | uint32_t int_ipv4_addr, |
| 341 | TrafficSource source) { |
| 342 | if (!ext_ifname.empty() && |
| 343 | !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr))) |
| 344 | LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname |
| 345 | << "->" << int_ifname; |
| 346 | |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 347 | if (StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname)) |
| 348 | LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->" |
| 349 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 350 | |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 351 | if (StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname)) |
| 352 | LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-" |
| 353 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 354 | |
| 355 | // TODO(b/161507671) If ext_ifname is not null, mark egress traffic with the |
| 356 | // fwmark routing tag corresponding to |ext_ifname|, and set up strong routing |
| 357 | // in ip rule. |
| 358 | |
| 359 | // TODO(b/161507671) If ext_ifname is null, set up connection tracking for the |
| 360 | // current default interface. |
| 361 | |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 362 | if (!ModifyFwmarkSourceTag("-A", int_ifname, source)) |
| 363 | LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source " |
| 364 | << source << " for " << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void Datapath::StopRoutingDevice(const std::string& ext_ifname, |
| 368 | const std::string& int_ifname, |
| 369 | uint32_t int_ipv4_addr, |
| 370 | TrafficSource source) { |
| 371 | if (!ext_ifname.empty()) |
| 372 | RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)); |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 373 | StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname); |
| 374 | StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname); |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 375 | ModifyFwmarkSourceTag("-D", int_ifname, source); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 376 | // TODO(b/161507671) Remove routing tag marking for egress traffic. |
| 377 | } |
| 378 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 379 | bool Datapath::AddInboundIPv4DNAT(const std::string& ifname, |
| 380 | const std::string& ipv4_addr) { |
| 381 | // Direct ingress IP traffic to existing sockets. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 382 | if (process_runner_->iptables( |
| 383 | "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket", |
| 384 | "--nowildcard", "-j", "ACCEPT", "-w"}) != 0) |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 385 | return false; |
| 386 | |
| 387 | // Direct ingress TCP & UDP traffic to ARC interface for new connections. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 388 | if (process_runner_->iptables( |
| 389 | "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT", |
| 390 | "--to-destination", ipv4_addr, "-w"}) != 0) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 391 | RemoveInboundIPv4DNAT(ifname, ipv4_addr); |
| 392 | return false; |
| 393 | } |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 394 | if (process_runner_->iptables( |
| 395 | "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT", |
| 396 | "--to-destination", ipv4_addr, "-w"}) != 0) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 397 | RemoveInboundIPv4DNAT(ifname, ipv4_addr); |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname, |
| 405 | const std::string& ipv4_addr) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 406 | process_runner_->iptables( |
| 407 | "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT", |
| 408 | "--to-destination", ipv4_addr, "-w"}); |
| 409 | process_runner_->iptables( |
| 410 | "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT", |
| 411 | "--to-destination", ipv4_addr, "-w"}); |
| 412 | process_runner_->iptables( |
| 413 | "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard", |
| 414 | "-j", "ACCEPT", "-w"}); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 415 | } |
| 416 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 417 | // TODO(hugobenichi) The name incorrectly refers to egress traffic, but this |
| 418 | // FORWARD rule actually enables forwarding for ingress traffic. Fix the name. |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 419 | bool Datapath::AddOutboundIPv4(const std::string& ifname) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 420 | return StartIpForwarding(IpFamily::IPv4, "", ifname); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void Datapath::RemoveOutboundIPv4(const std::string& ifname) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 424 | StopIpForwarding(IpFamily::IPv4, "", ifname); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 425 | } |
| 426 | |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 427 | // TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded |
| 428 | // egress traffic is routed through the fwmark routing tag. |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 429 | bool Datapath::AddSNATMarkRules() { |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 430 | // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore |
| 431 | // need to be explicitly dropped. |
| 432 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 433 | "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 434 | "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) { |
| 435 | return false; |
| 436 | } |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 437 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 438 | "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j", |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 439 | "ACCEPT", "-w"}) != 0) { |
| 440 | return false; |
| 441 | } |
| 442 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 443 | "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j", |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 444 | "MASQUERADE", "-w"}) != 0) { |
| 445 | RemoveSNATMarkRules(); |
| 446 | return false; |
| 447 | } |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | void Datapath::RemoveSNATMarkRules() { |
| 452 | process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 453 | "1/1", "-j", "MASQUERADE", "-w"}); |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 454 | process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 455 | "1/1", "-j", "ACCEPT", "-w"}); |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 456 | process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 457 | "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state", |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 458 | "--state", "INVALID", "-j", "DROP", "-w"}); |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 459 | } |
| 460 | |
Garrick Evans | ff6e37f | 2020-05-25 10:54:47 +0900 | [diff] [blame] | 461 | bool Datapath::AddInterfaceSNAT(const std::string& ifname) { |
| 462 | return process_runner_->iptables("nat", {"-A", "POSTROUTING", "-o", ifname, |
| 463 | "-j", "MASQUERADE", "-w"}) == 0; |
| 464 | } |
| 465 | |
| 466 | void Datapath::RemoveInterfaceSNAT(const std::string& ifname) { |
| 467 | process_runner_->iptables( |
| 468 | "nat", {"-D", "POSTROUTING", "-o", ifname, "-j", "MASQUERADE", "-w"}); |
| 469 | } |
| 470 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 471 | bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) { |
| 472 | return process_runner_->iptables( |
| 473 | "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 474 | "--set-mark", "1/1", "-w"}) == 0; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) { |
| 478 | process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 479 | "MARK", "--set-mark", "1/1", "-w"}); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 480 | } |
| 481 | |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 482 | bool Datapath::AddForwardEstablishedRule() { |
| 483 | return process_runner_->iptables( |
| 484 | "filter", {"-A", "FORWARD", "-m", "state", "--state", |
| 485 | "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) == 0; |
| 486 | } |
| 487 | |
| 488 | void Datapath::RemoveForwardEstablishedRule() { |
| 489 | process_runner_->iptables("filter", |
| 490 | {"-D", "FORWARD", "-m", "state", "--state", |
| 491 | "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}); |
| 492 | } |
| 493 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 494 | bool Datapath::MaskInterfaceFlags(const std::string& ifname, |
| 495 | uint16_t on, |
| 496 | uint16_t off) { |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 497 | base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 498 | if (!sock.is_valid()) { |
| 499 | PLOG(ERROR) << "Failed to create control socket"; |
| 500 | return false; |
| 501 | } |
| 502 | ifreq ifr; |
| 503 | snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str()); |
| 504 | if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) { |
| 505 | PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname; |
| 506 | return false; |
| 507 | } |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 508 | ifr.ifr_flags |= on; |
| 509 | ifr.ifr_flags &= ~off; |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 510 | if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 511 | PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on |
| 512 | << " unset flag 0x" << std::hex << off << " on " << ifname; |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | return true; |
| 516 | } |
| 517 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 518 | bool Datapath::AddIPv6HostRoute(const std::string& ifname, |
| 519 | const std::string& ipv6_addr, |
| 520 | int ipv6_prefix_len) { |
| 521 | std::string ipv6_addr_cidr = |
| 522 | ipv6_addr + "/" + std::to_string(ipv6_prefix_len); |
| 523 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 524 | return process_runner_->ip6("route", "replace", |
| 525 | {ipv6_addr_cidr, "dev", ifname}) == 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void Datapath::RemoveIPv6HostRoute(const std::string& ifname, |
| 529 | const std::string& ipv6_addr, |
| 530 | int ipv6_prefix_len) { |
| 531 | std::string ipv6_addr_cidr = |
| 532 | ipv6_addr + "/" + std::to_string(ipv6_prefix_len); |
| 533 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 534 | process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname}); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | bool Datapath::AddIPv6Neighbor(const std::string& ifname, |
| 538 | const std::string& ipv6_addr) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 539 | return process_runner_->ip6("neigh", "add", |
| 540 | {"proxy", ipv6_addr, "dev", ifname}) == 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void Datapath::RemoveIPv6Neighbor(const std::string& ifname, |
| 544 | const std::string& ipv6_addr) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 545 | process_runner_->ip6("neigh", "del", {"proxy", ipv6_addr, "dev", ifname}); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 546 | } |
| 547 | |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 548 | bool Datapath::ModifyFwmarkSourceTag(const std::string& op, |
| 549 | const std::string& iif, |
| 550 | TrafficSource source) { |
| 551 | return ModifyFwmarkPrerouting(IpFamily::Dual, op, iif, |
| 552 | Fwmark::FromSource(source), |
| 553 | kFwmarkAllSourcesMask); |
| 554 | } |
| 555 | |
| 556 | bool Datapath::ModifyFwmarkPrerouting(IpFamily family, |
| 557 | const std::string& op, |
| 558 | const std::string& iif, |
| 559 | Fwmark mark, |
| 560 | Fwmark mask, |
| 561 | bool log_failures) { |
| 562 | if (iif.empty()) { |
| 563 | LOG(ERROR) |
| 564 | << "Cannot change PREROUTING set-fwmark with no interface specified"; |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | switch (family) { |
| 569 | case IPv4: |
| 570 | case IPv6: |
| 571 | case Dual: |
| 572 | break; |
| 573 | default: |
| 574 | LOG(ERROR) << "Cannot change PREROUTING set-fwmark for " << iif |
| 575 | << ": incorrect IP family " << family; |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | std::vector<std::string> args = { |
| 580 | op, "PREROUTING", "-i", iif, |
| 581 | "-j", "MARK", "--set-mark", mark.ToString() + "/" + mask.ToString(), |
| 582 | "-w"}; |
| 583 | |
| 584 | bool success = true; |
Hugo Benichi | 5c9c11c | 2020-09-15 17:25:26 +0900 | [diff] [blame] | 585 | if (family & IPv4) |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 586 | success &= process_runner_->iptables("mangle", args, log_failures) == 0; |
Hugo Benichi | 5c9c11c | 2020-09-15 17:25:26 +0900 | [diff] [blame] | 587 | if (family & IPv6) |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 588 | success &= process_runner_->ip6tables("mangle", args, log_failures) == 0; |
| 589 | return false; |
| 590 | } |
| 591 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 592 | bool Datapath::ModifyIpForwarding(IpFamily family, |
| 593 | const std::string& op, |
| 594 | const std::string& iif, |
| 595 | const std::string& oif, |
| 596 | bool log_failures) { |
| 597 | if (iif.empty() && oif.empty()) { |
| 598 | LOG(ERROR) << "Cannot change IP forwarding with no input or output " |
| 599 | "interface specified"; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 603 | switch (family) { |
| 604 | case IPv4: |
| 605 | case IPv6: |
| 606 | case Dual: |
| 607 | break; |
| 608 | default: |
| 609 | |
| 610 | LOG(ERROR) << "Cannot change IP forwarding from \"" << iif << "\" to \"" |
| 611 | << oif << "\": incorrect IP family " << family; |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | std::vector<std::string> args = {op, "FORWARD"}; |
| 616 | if (!iif.empty()) { |
| 617 | args.push_back("-i"); |
| 618 | args.push_back(iif); |
| 619 | } |
| 620 | if (!oif.empty()) { |
| 621 | args.push_back("-o"); |
| 622 | args.push_back(oif); |
| 623 | } |
| 624 | args.push_back("-j"); |
| 625 | args.push_back("ACCEPT"); |
| 626 | args.push_back("-w"); |
| 627 | |
| 628 | bool success = true; |
| 629 | if (family & IpFamily::IPv4) |
| 630 | success &= process_runner_->iptables("filter", args, log_failures) == 0; |
| 631 | if (family & IpFamily::IPv6) |
| 632 | success &= process_runner_->ip6tables("filter", args, log_failures) == 0; |
| 633 | return success; |
| 634 | } |
| 635 | |
| 636 | bool Datapath::StartIpForwarding(IpFamily family, |
| 637 | const std::string& iif, |
| 638 | const std::string& oif) { |
| 639 | return ModifyIpForwarding(family, "-A", iif, oif); |
| 640 | } |
| 641 | |
| 642 | bool Datapath::StopIpForwarding(IpFamily family, |
| 643 | const std::string& iif, |
| 644 | const std::string& oif) { |
| 645 | return ModifyIpForwarding(family, "-D", iif, oif); |
| 646 | } |
| 647 | |
| 648 | bool Datapath::AddIPv6Forwarding(const std::string& ifname1, |
| 649 | const std::string& ifname2) { |
| 650 | // Only start Ipv6 forwarding if -C returns false and it had not been |
| 651 | // started yet. |
| 652 | if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2, |
| 653 | false /*log_failures*/) && |
| 654 | !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1, |
| 659 | false /*log_failures*/) && |
| 660 | !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) { |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 661 | RemoveIPv6Forwarding(ifname1, ifname2); |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | void Datapath::RemoveIPv6Forwarding(const std::string& ifname1, |
| 669 | const std::string& ifname2) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 670 | StopIpForwarding(IpFamily::IPv6, ifname1, ifname2); |
| 671 | StopIpForwarding(IpFamily::IPv6, ifname2, ifname1); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 672 | } |
| 673 | |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 674 | bool Datapath::AddIPv4Route(uint32_t gateway_addr, |
| 675 | uint32_t addr, |
| 676 | uint32_t netmask) { |
| 677 | struct rtentry route; |
| 678 | memset(&route, 0, sizeof(route)); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 679 | SetSockaddrIn(&route.rt_gateway, gateway_addr); |
| 680 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 681 | SetSockaddrIn(&route.rt_genmask, netmask); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 682 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 683 | return ModifyRtentry(SIOCADDRT, &route); |
| 684 | } |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 685 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 686 | bool Datapath::DeleteIPv4Route(uint32_t gateway_addr, |
| 687 | uint32_t addr, |
| 688 | uint32_t netmask) { |
| 689 | struct rtentry route; |
| 690 | memset(&route, 0, sizeof(route)); |
| 691 | SetSockaddrIn(&route.rt_gateway, gateway_addr); |
| 692 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 693 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 694 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 695 | return ModifyRtentry(SIOCDELRT, &route); |
| 696 | } |
| 697 | |
| 698 | bool Datapath::AddIPv4Route(const std::string& ifname, |
| 699 | uint32_t addr, |
| 700 | uint32_t netmask) { |
| 701 | struct rtentry route; |
| 702 | memset(&route, 0, sizeof(route)); |
| 703 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 704 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 705 | char rt_dev[IFNAMSIZ]; |
| 706 | strncpy(rt_dev, ifname.c_str(), IFNAMSIZ); |
| 707 | rt_dev[IFNAMSIZ - 1] = '\0'; |
| 708 | route.rt_dev = rt_dev; |
| 709 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 710 | return ModifyRtentry(SIOCADDRT, &route); |
| 711 | } |
| 712 | |
| 713 | bool Datapath::DeleteIPv4Route(const std::string& ifname, |
| 714 | uint32_t addr, |
| 715 | uint32_t netmask) { |
| 716 | struct rtentry route; |
| 717 | memset(&route, 0, sizeof(route)); |
| 718 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 719 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 720 | char rt_dev[IFNAMSIZ]; |
| 721 | strncpy(rt_dev, ifname.c_str(), IFNAMSIZ); |
| 722 | rt_dev[IFNAMSIZ - 1] = '\0'; |
| 723 | route.rt_dev = rt_dev; |
| 724 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 725 | return ModifyRtentry(SIOCDELRT, &route); |
| 726 | } |
| 727 | |
| 728 | bool Datapath::ModifyRtentry(unsigned long op, struct rtentry* route) { |
| 729 | DCHECK(route); |
| 730 | if (op != SIOCADDRT && op != SIOCDELRT) { |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 731 | LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route; |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 732 | return false; |
| 733 | } |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 734 | base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 735 | if (!fd.is_valid()) { |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 736 | PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 737 | return false; |
| 738 | } |
| 739 | if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) { |
| 740 | std::string opname = op == SIOCADDRT ? "add" : "delete"; |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 741 | PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route; |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 742 | return false; |
| 743 | } |
| 744 | return true; |
| 745 | } |
| 746 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 747 | bool Datapath::AddAdbPortForwardRule(const std::string& ifname) { |
| 748 | return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP, |
| 749 | kArcAddr, kAdbServerPort, ifname, |
| 750 | kLocalhostAddr, kAdbProxyTcpListenPort); |
| 751 | } |
| 752 | |
| 753 | void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) { |
| 754 | firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP, |
| 755 | kArcAddr, kAdbServerPort, ifname, |
| 756 | kLocalhostAddr, kAdbProxyTcpListenPort); |
| 757 | } |
| 758 | |
| 759 | bool Datapath::AddAdbPortAccessRule(const std::string& ifname) { |
| 760 | return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP, |
| 761 | kAdbProxyTcpListenPort, ifname); |
| 762 | } |
| 763 | |
| 764 | void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) { |
| 765 | firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP, |
| 766 | kAdbProxyTcpListenPort, ifname); |
| 767 | } |
| 768 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 769 | } // namespace patchpanel |