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 | |
Hugo Benichi | bf811c6 | 2020-09-07 17:30:45 +0900 | [diff] [blame] | 41 | // Constants used for dropping locally originated traffic bound to an incorrect |
| 42 | // source IPv4 address. |
| 43 | constexpr char kGuestIPv4Subnet[] = "100.115.92.0/23"; |
| 44 | constexpr std::array<const char*, 6> kPhysicalIfnamePrefixes{ |
| 45 | {"eth+", "wlan+", "mlan+", "usb+", "wwan+", "rmnet+"}}; |
| 46 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 47 | std::string PrefixIfname(const std::string& prefix, const std::string& ifname) { |
| 48 | std::string n = prefix + ifname; |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 49 | if (n.length() < IFNAMSIZ) |
| 50 | return n; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 51 | |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 52 | // Best effort attempt to preserve the interface number, assuming it's the |
| 53 | // last char in the name. |
| 54 | auto c = ifname[ifname.length() - 1]; |
| 55 | n.resize(IFNAMSIZ - 1); |
| 56 | n[n.length() - 1] = c; |
| 57 | return n; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 58 | } |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 59 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 60 | bool IsValidIpFamily(IpFamily family) { |
| 61 | switch (family) { |
| 62 | case IPv4: |
| 63 | case IPv6: |
| 64 | case Dual: |
| 65 | return true; |
| 66 | default: |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 71 | } // namespace |
| 72 | |
| 73 | std::string ArcVethHostName(const std::string& ifname) { |
| 74 | return PrefixIfname("veth", ifname); |
| 75 | } |
| 76 | |
| 77 | std::string ArcBridgeName(const std::string& ifname) { |
| 78 | return PrefixIfname("arc_", ifname); |
| 79 | } |
| 80 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 81 | Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall) |
| 82 | : Datapath(process_runner, firewall, ioctl) {} |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 83 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 84 | Datapath::Datapath(MinijailedProcessRunner* process_runner, |
| 85 | Firewall* firewall, |
| 86 | ioctl_t ioctl_hook) |
| 87 | : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 88 | CHECK(process_runner_); |
| 89 | } |
| 90 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 91 | MinijailedProcessRunner& Datapath::runner() const { |
| 92 | return *process_runner_; |
| 93 | } |
| 94 | |
Hugo Benichi | bf811c6 | 2020-09-07 17:30:45 +0900 | [diff] [blame] | 95 | void Datapath::Start() { |
| 96 | // Enable IPv4 packet forwarding |
| 97 | if (process_runner_->sysctl_w("net.ipv4.ip_forward", "1") != 0) |
| 98 | LOG(ERROR) << "Failed to update net.ipv4.ip_forward." |
| 99 | << " Guest connectivity will not work correctly."; |
| 100 | |
| 101 | // Limit local port range: Android owns 47104-61000. |
| 102 | // TODO(garrick): The original history behind this tweak is gone. Some |
| 103 | // investigation is needed to see if it is still applicable. |
| 104 | if (process_runner_->sysctl_w("net.ipv4.ip_local_port_range", |
| 105 | "32768 47103") != 0) |
| 106 | LOG(ERROR) << "Failed to limit local port range. Some Android features or" |
| 107 | << " apps may not work correctly."; |
| 108 | |
| 109 | // Enable IPv6 packet forwarding |
| 110 | if (process_runner_->sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0) |
| 111 | LOG(ERROR) << "Failed to update net.ipv6.conf.all.forwarding." |
| 112 | << " IPv6 functionality may be broken."; |
| 113 | |
| 114 | if (!AddSNATMarkRules()) |
| 115 | LOG(ERROR) << "Failed to install SNAT mark rules." |
| 116 | << " Guest connectivity may be broken."; |
| 117 | |
| 118 | if (!AddForwardEstablishedRule()) |
| 119 | LOG(ERROR) << "Failed to install forwarding rule for established" |
| 120 | << " connections."; |
| 121 | |
| 122 | // chromium:898210: Drop any locally originated traffic that would exit a |
| 123 | // physical interface with a source IPv4 address from the subnet of IPs used |
| 124 | // for VMs, containers, and connected namespaces This is needed to prevent |
| 125 | // packets leaking with an incorrect src IP when a local process binds to the |
| 126 | // wrong interface. |
| 127 | for (const auto& oif : kPhysicalIfnamePrefixes) { |
| 128 | if (!AddSourceIPv4DropRule(oif, kGuestIPv4Subnet)) |
| 129 | LOG(WARNING) << "Failed to set up IPv4 drop rule for src ip " |
| 130 | << kGuestIPv4Subnet << " exiting " << oif; |
| 131 | } |
| 132 | |
| 133 | if (!AddOutboundIPv4SNATMark("vmtap+")) |
| 134 | LOG(ERROR) << "Failed to set up NAT for TAP devices." |
| 135 | << " Guest connectivity may be broken."; |
| 136 | } |
| 137 | |
| 138 | void Datapath::Stop() { |
| 139 | RemoveOutboundIPv4SNATMark("vmtap+"); |
| 140 | RemoveForwardEstablishedRule(); |
| 141 | RemoveSNATMarkRules(); |
| 142 | for (const auto& oif : kPhysicalIfnamePrefixes) |
| 143 | RemoveSourceIPv4DropRule(oif, kGuestIPv4Subnet); |
| 144 | |
| 145 | // Restore original local port range. |
| 146 | // TODO(garrick): The original history behind this tweak is gone. Some |
| 147 | // investigation is needed to see if it is still applicable. |
| 148 | if (process_runner_->sysctl_w("net.ipv4.ip_local_port_range", |
| 149 | "32768 61000") != 0) |
| 150 | LOG(ERROR) << "Failed to restore local port range"; |
| 151 | |
| 152 | // Disable packet forwarding |
| 153 | if (process_runner_->sysctl_w("net.ipv6.conf.all.forwarding", "0") != 0) |
| 154 | LOG(ERROR) << "Failed to restore net.ipv6.conf.all.forwarding."; |
| 155 | |
| 156 | if (process_runner_->sysctl_w("net.ipv4.ip_forward", "0") != 0) |
| 157 | LOG(ERROR) << "Failed to restore net.ipv4.ip_forward."; |
| 158 | } |
| 159 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 160 | bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) { |
| 161 | // Try first to delete any netns with name |netns_name| in case patchpanel |
| 162 | // did not exit cleanly. |
| 163 | if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0) |
| 164 | LOG(INFO) << "Deleted left over network namespace name " << netns_name; |
| 165 | return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0; |
| 166 | } |
| 167 | |
| 168 | bool Datapath::NetnsDeleteName(const std::string& netns_name) { |
| 169 | return process_runner_->ip_netns_delete(netns_name) == 0; |
| 170 | } |
| 171 | |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 172 | bool Datapath::AddBridge(const std::string& ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 173 | uint32_t ipv4_addr, |
| 174 | uint32_t ipv4_prefix_len) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 175 | // Configure the persistent Chrome OS bridge interface with static IP. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 176 | if (process_runner_->brctl("addbr", {ifname}) != 0) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 180 | if (process_runner_->ip( |
| 181 | "addr", "add", |
| 182 | {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd", |
| 183 | IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)), |
| 184 | "dev", ifname}) != 0) { |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 185 | RemoveBridge(ifname); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 190 | RemoveBridge(ifname); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // 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] | 195 | if (!AddOutboundIPv4SNATMark(ifname)) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 196 | RemoveBridge(ifname); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | void Datapath::RemoveBridge(const std::string& ifname) { |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 204 | RemoveOutboundIPv4SNATMark(ifname); |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 205 | process_runner_->ip("link", "set", {ifname, "down"}); |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 206 | process_runner_->brctl("delbr", {ifname}); |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 209 | bool Datapath::AddToBridge(const std::string& br_ifname, |
| 210 | const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 211 | return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0); |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 212 | } |
| 213 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 214 | std::string Datapath::AddTAP(const std::string& name, |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 215 | const MacAddress* mac_addr, |
| 216 | const SubnetAddress* ipv4_addr, |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 217 | const std::string& user) { |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 218 | base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK)); |
| 219 | if (!dev.is_valid()) { |
| 220 | PLOG(ERROR) << "Failed to open " << kTunDev; |
| 221 | return ""; |
| 222 | } |
| 223 | |
| 224 | struct ifreq ifr; |
| 225 | memset(&ifr, 0, sizeof(ifr)); |
| 226 | strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(), |
| 227 | sizeof(ifr.ifr_name)); |
| 228 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 229 | |
| 230 | // If a template was given as the name, ifr_name will be updated with the |
| 231 | // actual interface name. |
| 232 | if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 233 | PLOG(ERROR) << "Failed to create tap interface " << name; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 234 | return ""; |
| 235 | } |
| 236 | const char* ifname = ifr.ifr_name; |
| 237 | |
| 238 | if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 239 | PLOG(ERROR) << "Failed to persist the interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 240 | return ""; |
| 241 | } |
| 242 | |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 243 | if (!user.empty()) { |
| 244 | uid_t uid = -1; |
| 245 | if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) { |
| 246 | PLOG(ERROR) << "Unable to look up UID for " << user; |
| 247 | RemoveTAP(ifname); |
| 248 | return ""; |
| 249 | } |
| 250 | if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) { |
| 251 | PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface " |
| 252 | << ifname; |
| 253 | RemoveTAP(ifname); |
| 254 | return ""; |
| 255 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 258 | // Create control socket for configuring the interface. |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 259 | base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 260 | if (!sock.is_valid()) { |
| 261 | PLOG(ERROR) << "Failed to create control socket for tap interface " |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 262 | << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 263 | RemoveTAP(ifname); |
| 264 | return ""; |
| 265 | } |
| 266 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 267 | if (ipv4_addr) { |
| 268 | struct sockaddr_in* addr = |
| 269 | reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr); |
| 270 | addr->sin_family = AF_INET; |
| 271 | addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address()); |
| 272 | if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) { |
| 273 | PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname |
| 274 | << " {" << ipv4_addr->ToCidrString() << "}"; |
| 275 | RemoveTAP(ifname); |
| 276 | return ""; |
| 277 | } |
| 278 | |
| 279 | struct sockaddr_in* netmask = |
| 280 | reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask); |
| 281 | netmask->sin_family = AF_INET; |
| 282 | netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask()); |
| 283 | if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) { |
| 284 | PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname |
| 285 | << " {" << ipv4_addr->ToCidrString() << "}"; |
| 286 | RemoveTAP(ifname); |
| 287 | return ""; |
| 288 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 289 | } |
| 290 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 291 | if (mac_addr) { |
| 292 | struct sockaddr* hwaddr = &ifr.ifr_hwaddr; |
| 293 | hwaddr->sa_family = ARPHRD_ETHER; |
| 294 | memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr)); |
| 295 | if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) { |
| 296 | PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname |
| 297 | << " {" << MacAddressToString(*mac_addr) << "}"; |
| 298 | RemoveTAP(ifname); |
| 299 | return ""; |
| 300 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 304 | PLOG(ERROR) << "Failed to get flags for tap interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 305 | RemoveTAP(ifname); |
| 306 | return ""; |
| 307 | } |
| 308 | |
| 309 | ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
| 310 | if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 311 | PLOG(ERROR) << "Failed to enable tap interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 312 | RemoveTAP(ifname); |
| 313 | return ""; |
| 314 | } |
| 315 | |
| 316 | return ifname; |
| 317 | } |
| 318 | |
| 319 | void Datapath::RemoveTAP(const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 320 | process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"}); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 321 | } |
| 322 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 323 | bool Datapath::ConnectVethPair(pid_t netns_pid, |
| 324 | const std::string& netns_name, |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 325 | const std::string& veth_ifname, |
| 326 | const std::string& peer_ifname, |
| 327 | const MacAddress& remote_mac_addr, |
| 328 | uint32_t remote_ipv4_addr, |
| 329 | uint32_t remote_ipv4_prefix_len, |
| 330 | bool remote_multicast_flag) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 331 | // Set up the virtual pair across the current namespace and |netns_name|. |
| 332 | if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) { |
| 333 | LOG(ERROR) << "Failed to create veth pair " << veth_ifname << "," |
| 334 | << peer_ifname; |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | // Configure the remote veth in namespace |netns_name|. |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 339 | { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 340 | ScopedNS ns(netns_pid); |
| 341 | if (!ns.IsValid() && netns_pid != kTestPID) { |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 342 | LOG(ERROR) |
| 343 | << "Cannot create virtual link -- invalid container namespace?"; |
| 344 | return false; |
| 345 | } |
| 346 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 347 | if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr, |
| 348 | remote_ipv4_prefix_len, true /* link up */, |
| 349 | remote_multicast_flag)) { |
| 350 | LOG(ERROR) << "Failed to configure interface " << peer_ifname; |
| 351 | RemoveInterface(peer_ifname); |
| 352 | return false; |
| 353 | } |
| 354 | } |
| 355 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 356 | if (!ToggleInterface(veth_ifname, true /*up*/)) { |
| 357 | LOG(ERROR) << "Failed to bring up interface " << veth_ifname; |
| 358 | RemoveInterface(veth_ifname); |
| 359 | return false; |
| 360 | } |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 361 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 365 | bool Datapath::AddVirtualInterfacePair(const std::string& netns_name, |
| 366 | const std::string& veth_ifname, |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 367 | const std::string& peer_ifname) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 368 | return process_runner_->ip("link", "add", |
| 369 | {veth_ifname, "type", "veth", "peer", "name", |
| 370 | peer_ifname, "netns", netns_name}) == 0; |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 371 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 372 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 373 | bool Datapath::ToggleInterface(const std::string& ifname, bool up) { |
| 374 | const std::string link = up ? "up" : "down"; |
| 375 | return process_runner_->ip("link", "set", {ifname, link}) == 0; |
| 376 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 377 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 378 | bool Datapath::ConfigureInterface(const std::string& ifname, |
| 379 | const MacAddress& mac_addr, |
| 380 | uint32_t ipv4_addr, |
| 381 | uint32_t ipv4_prefix_len, |
| 382 | bool up, |
| 383 | bool enable_multicast) { |
| 384 | const std::string link = up ? "up" : "down"; |
| 385 | const std::string multicast = enable_multicast ? "on" : "off"; |
| 386 | return (process_runner_->ip( |
| 387 | "addr", "add", |
| 388 | {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd", |
| 389 | IPv4AddressToString( |
| 390 | Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)), |
| 391 | "dev", ifname}) == 0) && |
| 392 | (process_runner_->ip("link", "set", |
| 393 | { |
| 394 | "dev", |
| 395 | ifname, |
| 396 | link, |
| 397 | "addr", |
| 398 | MacAddressToString(mac_addr), |
| 399 | "multicast", |
| 400 | multicast, |
| 401 | }) == 0); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void Datapath::RemoveInterface(const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 405 | process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 406 | } |
| 407 | |
Hugo Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 408 | bool Datapath::AddSourceIPv4DropRule(const std::string& oif, |
| 409 | const std::string& src_ip) { |
| 410 | return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s", |
| 411 | src_ip, "-j", "DROP", "-w"}) == 0; |
| 412 | } |
| 413 | |
| 414 | bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif, |
| 415 | const std::string& src_ip) { |
| 416 | return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s", |
| 417 | src_ip, "-j", "DROP", "-w"}) == 0; |
| 418 | } |
| 419 | |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame^] | 420 | bool Datapath::StartRoutingNamespace(pid_t pid, |
| 421 | const std::string& netns_name, |
| 422 | const std::string& host_ifname, |
| 423 | const std::string& peer_ifname, |
| 424 | uint32_t subnet_ipv4_addr, |
| 425 | uint32_t subnet_prefixlen, |
| 426 | uint32_t host_ipv4_addr, |
| 427 | uint32_t peer_ipv4_addr, |
| 428 | const MacAddress& peer_mac_addr) { |
| 429 | // Veth interface configuration and client routing configuration: |
| 430 | // - attach a name to the client namespace. |
| 431 | // - create veth pair across the current namespace and the client namespace. |
| 432 | // - configure IPv4 address on remote veth inside client namespace. |
| 433 | // - configure IPv4 address on local veth inside host namespace. |
| 434 | // - add a default IPv4 /0 route sending traffic to that remote veth. |
| 435 | if (!NetnsAttachName(netns_name, pid)) { |
| 436 | LOG(ERROR) << "Failed to attach name " << netns_name << " to namespace pid " |
| 437 | << pid; |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | if (!ConnectVethPair(pid, netns_name, host_ifname, peer_ifname, peer_mac_addr, |
| 442 | peer_ipv4_addr, subnet_prefixlen, |
| 443 | false /* enable_multicast */)) { |
| 444 | LOG(ERROR) << "Failed to create veth pair for" |
| 445 | " namespace pid " |
| 446 | << pid; |
| 447 | NetnsDeleteName(netns_name); |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | if (!ConfigureInterface(host_ifname, peer_mac_addr, host_ipv4_addr, |
| 452 | subnet_prefixlen, true /* link up */, |
| 453 | false /* enable_multicast */)) { |
| 454 | LOG(ERROR) << "Cannot configure host interface " << host_ifname; |
| 455 | RemoveInterface(host_ifname); |
| 456 | NetnsDeleteName(netns_name); |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | { |
| 461 | ScopedNS ns(pid); |
| 462 | if (!ns.IsValid() && pid != kTestPID) { |
| 463 | LOG(ERROR) << "Invalid namespace pid " << pid; |
| 464 | RemoveInterface(host_ifname); |
| 465 | NetnsDeleteName(netns_name); |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | if (!AddIPv4Route(host_ipv4_addr, INADDR_ANY, INADDR_ANY)) { |
| 470 | LOG(ERROR) << "Failed to add default /0 route to " << host_ifname |
| 471 | << " inside namespace pid " << pid; |
| 472 | RemoveInterface(host_ifname); |
| 473 | NetnsDeleteName(netns_name); |
| 474 | return false; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // Host namespace routing configuration |
| 479 | // - ingress: add route to client subnet via |host_ifname|. |
| 480 | // - egress: - allow forwarding for traffic outgoing |host_ifname|. |
| 481 | // - add SNAT mark 0x1/0x1 for traffic outgoing |host_ifname|. |
| 482 | // Note that by default unsolicited ingress traffic is not forwarded to the |
| 483 | // client namespace unless the client specifically set port forwarding |
| 484 | // through permission_broker DBus APIs. |
| 485 | // TODO(hugobenichi) If allow_user_traffic is false, then prevent forwarding |
| 486 | // both ways between client namespace and other guest containers and VMs. |
| 487 | // TODO(b/161507671) If outbound_physical_device is defined, then set strong |
| 488 | // routing to that interface routing table. |
| 489 | uint32_t netmask = Ipv4Netmask(subnet_prefixlen); |
| 490 | if (!AddIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask)) { |
| 491 | LOG(ERROR) << "Failed to set route to client namespace"; |
| 492 | RemoveInterface(host_ifname); |
| 493 | NetnsDeleteName(netns_name); |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | if (!AddOutboundIPv4(host_ifname)) { |
| 498 | LOG(ERROR) << "Failed to allow FORWARD for" |
| 499 | " traffic outgoing from " |
| 500 | << host_ifname; |
| 501 | RemoveInterface(host_ifname); |
| 502 | DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask); |
| 503 | NetnsDeleteName(netns_name); |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | // TODO(b/161508179) Add fwmark source tagging based on client usage. |
| 508 | // TODO(b/161508179) Do not rely on legacy fwmark 1 for SNAT. |
| 509 | if (!AddOutboundIPv4SNATMark(host_ifname)) { |
| 510 | LOG(ERROR) << "Failed to set SNAT for traffic" |
| 511 | " outgoing from " |
| 512 | << host_ifname; |
| 513 | RemoveInterface(host_ifname); |
| 514 | DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask); |
| 515 | RemoveOutboundIPv4(host_ifname); |
| 516 | NetnsDeleteName(netns_name); |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | void Datapath::StopRoutingNamespace(const std::string& netns_name, |
| 524 | const std::string& host_ifname, |
| 525 | uint32_t subnet_ipv4_addr, |
| 526 | uint32_t subnet_prefixlen, |
| 527 | uint32_t host_ipv4_addr) { |
| 528 | RemoveInterface(host_ifname); |
| 529 | RemoveOutboundIPv4(host_ifname); |
| 530 | RemoveOutboundIPv4SNATMark(host_ifname); |
| 531 | DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, |
| 532 | Ipv4Netmask(subnet_prefixlen)); |
| 533 | NetnsDeleteName(netns_name); |
| 534 | } |
| 535 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 536 | void Datapath::StartRoutingDevice(const std::string& ext_ifname, |
| 537 | const std::string& int_ifname, |
| 538 | uint32_t int_ipv4_addr, |
| 539 | TrafficSource source) { |
| 540 | if (!ext_ifname.empty() && |
| 541 | !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr))) |
| 542 | LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname |
| 543 | << "->" << int_ifname; |
| 544 | |
Hugo Benichi | fa97b3b | 2020-10-06 22:45:26 +0900 | [diff] [blame] | 545 | if (!StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname)) |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 546 | LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->" |
| 547 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 548 | |
Hugo Benichi | fa97b3b | 2020-10-06 22:45:26 +0900 | [diff] [blame] | 549 | if (!StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname)) |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 550 | LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-" |
| 551 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 552 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 553 | if (!ext_ifname.empty()) { |
| 554 | // If |ext_ifname| is not null, mark egress traffic with the |
| 555 | // fwmark routing tag corresponding to |ext_ifname|. |
| 556 | if (!ModifyFwmarkRoutingTag("-A", ext_ifname, int_ifname)) |
| 557 | LOG(ERROR) << "Failed to add PREROUTING fwmark routing tag for " |
| 558 | << ext_ifname << "<-" << int_ifname; |
| 559 | } else { |
| 560 | // Otherwise if ext_ifname is null, set up a CONNMARK restore rule in |
| 561 | // PREROUTING to apply any fwmark routing tag saved for the current |
| 562 | // connection, and rely on implicit routing to the default logical network |
| 563 | // otherwise. |
| 564 | if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", int_ifname)) |
| 565 | LOG(ERROR) << "Failed to add PREROUTING CONNMARK restore rule for " |
| 566 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 567 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 568 | // TODO(b/161507671) When a VPN service starts, tag new connections with the |
| 569 | // fwmark routing tag for VPNs. |
| 570 | } |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 571 | |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 572 | if (!ModifyFwmarkSourceTag("-A", int_ifname, source)) |
| 573 | LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source " |
| 574 | << source << " for " << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void Datapath::StopRoutingDevice(const std::string& ext_ifname, |
| 578 | const std::string& int_ifname, |
| 579 | uint32_t int_ipv4_addr, |
| 580 | TrafficSource source) { |
| 581 | if (!ext_ifname.empty()) |
| 582 | RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)); |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 583 | StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname); |
| 584 | StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname); |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 585 | ModifyFwmarkSourceTag("-D", int_ifname, source); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 586 | if (!ext_ifname.empty()) { |
| 587 | ModifyFwmarkRoutingTag("-D", ext_ifname, int_ifname); |
| 588 | } else { |
| 589 | ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", int_ifname); |
| 590 | } |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 591 | } |
| 592 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 593 | bool Datapath::AddInboundIPv4DNAT(const std::string& ifname, |
| 594 | const std::string& ipv4_addr) { |
| 595 | // Direct ingress IP traffic to existing sockets. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 596 | if (process_runner_->iptables( |
| 597 | "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket", |
| 598 | "--nowildcard", "-j", "ACCEPT", "-w"}) != 0) |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 599 | return false; |
| 600 | |
| 601 | // Direct ingress TCP & UDP traffic to ARC interface for new connections. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 602 | if (process_runner_->iptables( |
| 603 | "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT", |
| 604 | "--to-destination", ipv4_addr, "-w"}) != 0) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 605 | RemoveInboundIPv4DNAT(ifname, ipv4_addr); |
| 606 | return false; |
| 607 | } |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 608 | if (process_runner_->iptables( |
| 609 | "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT", |
| 610 | "--to-destination", ipv4_addr, "-w"}) != 0) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 611 | RemoveInboundIPv4DNAT(ifname, ipv4_addr); |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname, |
| 619 | const std::string& ipv4_addr) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 620 | process_runner_->iptables( |
| 621 | "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT", |
| 622 | "--to-destination", ipv4_addr, "-w"}); |
| 623 | process_runner_->iptables( |
| 624 | "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT", |
| 625 | "--to-destination", ipv4_addr, "-w"}); |
| 626 | process_runner_->iptables( |
| 627 | "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard", |
| 628 | "-j", "ACCEPT", "-w"}); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 629 | } |
| 630 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 631 | // TODO(hugobenichi) The name incorrectly refers to egress traffic, but this |
| 632 | // FORWARD rule actually enables forwarding for ingress traffic. Fix the name. |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 633 | bool Datapath::AddOutboundIPv4(const std::string& ifname) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 634 | return StartIpForwarding(IpFamily::IPv4, "", ifname); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void Datapath::RemoveOutboundIPv4(const std::string& ifname) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 638 | StopIpForwarding(IpFamily::IPv4, "", ifname); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 639 | } |
| 640 | |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 641 | // TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded |
| 642 | // egress traffic is routed through the fwmark routing tag. |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 643 | bool Datapath::AddSNATMarkRules() { |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 644 | // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore |
| 645 | // need to be explicitly dropped. |
| 646 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 647 | "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 648 | "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) { |
| 649 | return false; |
| 650 | } |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 651 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 652 | "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j", |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 653 | "ACCEPT", "-w"}) != 0) { |
| 654 | return false; |
| 655 | } |
| 656 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 657 | "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j", |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 658 | "MASQUERADE", "-w"}) != 0) { |
| 659 | RemoveSNATMarkRules(); |
| 660 | return false; |
| 661 | } |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | void Datapath::RemoveSNATMarkRules() { |
| 666 | process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 667 | "1/1", "-j", "MASQUERADE", "-w"}); |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 668 | process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 669 | "1/1", "-j", "ACCEPT", "-w"}); |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 670 | process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 671 | "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state", |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 672 | "--state", "INVALID", "-j", "DROP", "-w"}); |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 673 | } |
| 674 | |
Garrick Evans | ff6e37f | 2020-05-25 10:54:47 +0900 | [diff] [blame] | 675 | bool Datapath::AddInterfaceSNAT(const std::string& ifname) { |
| 676 | return process_runner_->iptables("nat", {"-A", "POSTROUTING", "-o", ifname, |
| 677 | "-j", "MASQUERADE", "-w"}) == 0; |
| 678 | } |
| 679 | |
| 680 | void Datapath::RemoveInterfaceSNAT(const std::string& ifname) { |
| 681 | process_runner_->iptables( |
| 682 | "nat", {"-D", "POSTROUTING", "-o", ifname, "-j", "MASQUERADE", "-w"}); |
| 683 | } |
| 684 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 685 | bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) { |
| 686 | return process_runner_->iptables( |
| 687 | "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 688 | "--set-mark", "1/1", "-w"}) == 0; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) { |
| 692 | process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 693 | "MARK", "--set-mark", "1/1", "-w"}); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 694 | } |
| 695 | |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 696 | bool Datapath::AddForwardEstablishedRule() { |
| 697 | return process_runner_->iptables( |
| 698 | "filter", {"-A", "FORWARD", "-m", "state", "--state", |
| 699 | "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) == 0; |
| 700 | } |
| 701 | |
| 702 | void Datapath::RemoveForwardEstablishedRule() { |
| 703 | process_runner_->iptables("filter", |
| 704 | {"-D", "FORWARD", "-m", "state", "--state", |
| 705 | "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}); |
| 706 | } |
| 707 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 708 | bool Datapath::MaskInterfaceFlags(const std::string& ifname, |
| 709 | uint16_t on, |
| 710 | uint16_t off) { |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 711 | base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 712 | if (!sock.is_valid()) { |
| 713 | PLOG(ERROR) << "Failed to create control socket"; |
| 714 | return false; |
| 715 | } |
| 716 | ifreq ifr; |
| 717 | snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str()); |
| 718 | if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) { |
| 719 | PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname; |
| 720 | return false; |
| 721 | } |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 722 | ifr.ifr_flags |= on; |
| 723 | ifr.ifr_flags &= ~off; |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 724 | if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 725 | PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on |
| 726 | << " unset flag 0x" << std::hex << off << " on " << ifname; |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 727 | return false; |
| 728 | } |
| 729 | return true; |
| 730 | } |
| 731 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 732 | bool Datapath::AddIPv6HostRoute(const std::string& ifname, |
| 733 | const std::string& ipv6_addr, |
| 734 | int ipv6_prefix_len) { |
| 735 | std::string ipv6_addr_cidr = |
| 736 | ipv6_addr + "/" + std::to_string(ipv6_prefix_len); |
| 737 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 738 | return process_runner_->ip6("route", "replace", |
| 739 | {ipv6_addr_cidr, "dev", ifname}) == 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | void Datapath::RemoveIPv6HostRoute(const std::string& ifname, |
| 743 | const std::string& ipv6_addr, |
| 744 | int ipv6_prefix_len) { |
| 745 | std::string ipv6_addr_cidr = |
| 746 | ipv6_addr + "/" + std::to_string(ipv6_prefix_len); |
| 747 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 748 | process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname}); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 749 | } |
| 750 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 751 | bool Datapath::AddIPv6Address(const std::string& ifname, |
| 752 | const std::string& ipv6_addr) { |
| 753 | return process_runner_->ip6("addr", "add", {ipv6_addr, "dev", ifname}) == 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 754 | } |
| 755 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 756 | void Datapath::RemoveIPv6Address(const std::string& ifname, |
| 757 | const std::string& ipv6_addr) { |
| 758 | process_runner_->ip6("addr", "del", {ipv6_addr, "dev", ifname}); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 759 | } |
| 760 | |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 761 | void Datapath::StartConnectionPinning(const std::string& ext_ifname) { |
| 762 | if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-A", ext_ifname)) |
| 763 | LOG(ERROR) << "Could not start connection pinning on " << ext_ifname; |
| 764 | } |
| 765 | |
| 766 | void Datapath::StopConnectionPinning(const std::string& ext_ifname) { |
| 767 | if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-D", ext_ifname)) |
| 768 | LOG(ERROR) << "Could not stop connection pinning on " << ext_ifname; |
| 769 | } |
| 770 | |
| 771 | bool Datapath::ModifyConnmarkSetPostrouting(IpFamily family, |
| 772 | const std::string& op, |
| 773 | const std::string& oif) { |
| 774 | if (oif.empty()) { |
Hugo Benichi | bf811c6 | 2020-09-07 17:30:45 +0900 | [diff] [blame] | 775 | LOG(ERROR) << "Cannot change POSTROUTING CONNMARK set-mark with no" |
| 776 | " interface specified"; |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 777 | return false; |
| 778 | } |
| 779 | |
| 780 | if (!IsValidIpFamily(family)) { |
| 781 | LOG(ERROR) << "Cannot change POSTROUTING CONNMARK set-mark for " << oif |
| 782 | << ": incorrect IP family " << family; |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | int ifindex = FindIfIndex(oif); |
| 787 | if (ifindex == 0) { |
| 788 | PLOG(ERROR) << "if_nametoindex(" << oif << ") failed"; |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | std::vector<std::string> args = {op, |
| 793 | "POSTROUTING", |
| 794 | "-o", |
| 795 | oif, |
| 796 | "-j", |
| 797 | "CONNMARK", |
| 798 | "--set-mark", |
| 799 | Fwmark::FromIfIndex(ifindex).ToString() + |
| 800 | "/" + kFwmarkRoutingMask.ToString(), |
| 801 | "-w"}; |
| 802 | |
| 803 | bool success = true; |
| 804 | if (family & IPv4) |
| 805 | success &= process_runner_->iptables("mangle", args) == 0; |
| 806 | if (family & IPv6) |
| 807 | success &= process_runner_->ip6tables("mangle", args) == 0; |
| 808 | return false; |
| 809 | } |
| 810 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 811 | bool Datapath::ModifyConnmarkRestore(IpFamily family, |
| 812 | const std::string& chain, |
| 813 | const std::string& op, |
| 814 | const std::string& iif) { |
| 815 | if (chain != "OUTPUT" && (chain != "PREROUTING" || iif.empty())) { |
| 816 | LOG(ERROR) << "Invalid arguments chain=" << chain << " iif=" << iif; |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | if (!IsValidIpFamily(family)) { |
| 821 | LOG(ERROR) << "Cannot change " << chain << " -j CONNMARK restore-mark" |
| 822 | << " for " << iif << ": incorrect IP family " << family; |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | std::vector<std::string> args = {op, chain}; |
| 827 | if (!iif.empty()) { |
| 828 | args.push_back("-i"); |
| 829 | args.push_back(iif); |
| 830 | } |
| 831 | args.insert(args.end(), {"-j", "CONNMARK", "--restore-mark", "--mask", |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame^] | 832 | kFwmarkRoutingMask.ToString(), "-w"}); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 833 | |
| 834 | bool success = true; |
| 835 | if (family & IPv4) |
| 836 | success &= process_runner_->iptables("mangle", args) == 0; |
| 837 | if (family & IPv6) |
| 838 | success &= process_runner_->ip6tables("mangle", args) == 0; |
| 839 | return success; |
| 840 | } |
| 841 | |
| 842 | bool Datapath::ModifyFwmarkRoutingTag(const std::string& op, |
| 843 | const std::string& ext_ifname, |
| 844 | const std::string& int_ifname) { |
| 845 | int ifindex = FindIfIndex(ext_ifname); |
| 846 | if (ifindex == 0) { |
| 847 | PLOG(ERROR) << "if_nametoindex(" << ext_ifname << ") failed"; |
| 848 | return false; |
| 849 | } |
| 850 | |
| 851 | return ModifyFwmarkPrerouting(IpFamily::Dual, op, int_ifname, |
| 852 | Fwmark::FromIfIndex(ifindex), |
| 853 | kFwmarkRoutingMask); |
| 854 | } |
| 855 | |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 856 | bool Datapath::ModifyFwmarkSourceTag(const std::string& op, |
| 857 | const std::string& iif, |
| 858 | TrafficSource source) { |
| 859 | return ModifyFwmarkPrerouting(IpFamily::Dual, op, iif, |
| 860 | Fwmark::FromSource(source), |
| 861 | kFwmarkAllSourcesMask); |
| 862 | } |
| 863 | |
| 864 | bool Datapath::ModifyFwmarkPrerouting(IpFamily family, |
| 865 | const std::string& op, |
| 866 | const std::string& iif, |
| 867 | Fwmark mark, |
| 868 | Fwmark mask, |
| 869 | bool log_failures) { |
| 870 | if (iif.empty()) { |
| 871 | LOG(ERROR) |
| 872 | << "Cannot change PREROUTING set-fwmark with no interface specified"; |
| 873 | return false; |
| 874 | } |
| 875 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 876 | if (!IsValidIpFamily(family)) { |
| 877 | LOG(ERROR) << "Cannot change PREROUTING set-fwmark for " << iif |
| 878 | << ": incorrect IP family " << family; |
| 879 | return false; |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | std::vector<std::string> args = { |
| 883 | op, "PREROUTING", "-i", iif, |
| 884 | "-j", "MARK", "--set-mark", mark.ToString() + "/" + mask.ToString(), |
| 885 | "-w"}; |
| 886 | |
| 887 | bool success = true; |
Hugo Benichi | 5c9c11c | 2020-09-15 17:25:26 +0900 | [diff] [blame] | 888 | if (family & IPv4) |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 889 | success &= process_runner_->iptables("mangle", args, log_failures) == 0; |
Hugo Benichi | 5c9c11c | 2020-09-15 17:25:26 +0900 | [diff] [blame] | 890 | if (family & IPv6) |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 891 | success &= process_runner_->ip6tables("mangle", args, log_failures) == 0; |
Hugo Benichi | fa97b3b | 2020-10-06 22:45:26 +0900 | [diff] [blame] | 892 | return success; |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 893 | } |
| 894 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 895 | bool Datapath::ModifyIpForwarding(IpFamily family, |
| 896 | const std::string& op, |
| 897 | const std::string& iif, |
| 898 | const std::string& oif, |
| 899 | bool log_failures) { |
| 900 | if (iif.empty() && oif.empty()) { |
| 901 | LOG(ERROR) << "Cannot change IP forwarding with no input or output " |
| 902 | "interface specified"; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 903 | return false; |
| 904 | } |
| 905 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 906 | if (!IsValidIpFamily(family)) { |
| 907 | LOG(ERROR) << "Cannot change IP forwarding from \"" << iif << "\" to \"" |
| 908 | << oif << "\": incorrect IP family " << family; |
| 909 | return false; |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | std::vector<std::string> args = {op, "FORWARD"}; |
| 913 | if (!iif.empty()) { |
| 914 | args.push_back("-i"); |
| 915 | args.push_back(iif); |
| 916 | } |
| 917 | if (!oif.empty()) { |
| 918 | args.push_back("-o"); |
| 919 | args.push_back(oif); |
| 920 | } |
| 921 | args.push_back("-j"); |
| 922 | args.push_back("ACCEPT"); |
| 923 | args.push_back("-w"); |
| 924 | |
| 925 | bool success = true; |
| 926 | if (family & IpFamily::IPv4) |
| 927 | success &= process_runner_->iptables("filter", args, log_failures) == 0; |
| 928 | if (family & IpFamily::IPv6) |
| 929 | success &= process_runner_->ip6tables("filter", args, log_failures) == 0; |
| 930 | return success; |
| 931 | } |
| 932 | |
| 933 | bool Datapath::StartIpForwarding(IpFamily family, |
| 934 | const std::string& iif, |
| 935 | const std::string& oif) { |
| 936 | return ModifyIpForwarding(family, "-A", iif, oif); |
| 937 | } |
| 938 | |
| 939 | bool Datapath::StopIpForwarding(IpFamily family, |
| 940 | const std::string& iif, |
| 941 | const std::string& oif) { |
| 942 | return ModifyIpForwarding(family, "-D", iif, oif); |
| 943 | } |
| 944 | |
| 945 | bool Datapath::AddIPv6Forwarding(const std::string& ifname1, |
| 946 | const std::string& ifname2) { |
| 947 | // Only start Ipv6 forwarding if -C returns false and it had not been |
| 948 | // started yet. |
| 949 | if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2, |
| 950 | false /*log_failures*/) && |
| 951 | !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) { |
| 952 | return false; |
| 953 | } |
| 954 | |
| 955 | if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1, |
| 956 | false /*log_failures*/) && |
| 957 | !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) { |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 958 | RemoveIPv6Forwarding(ifname1, ifname2); |
| 959 | return false; |
| 960 | } |
| 961 | |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | void Datapath::RemoveIPv6Forwarding(const std::string& ifname1, |
| 966 | const std::string& ifname2) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 967 | StopIpForwarding(IpFamily::IPv6, ifname1, ifname2); |
| 968 | StopIpForwarding(IpFamily::IPv6, ifname2, ifname1); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 969 | } |
| 970 | |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 971 | bool Datapath::AddIPv4Route(uint32_t gateway_addr, |
| 972 | uint32_t addr, |
| 973 | uint32_t netmask) { |
| 974 | struct rtentry route; |
| 975 | memset(&route, 0, sizeof(route)); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 976 | SetSockaddrIn(&route.rt_gateway, gateway_addr); |
| 977 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 978 | SetSockaddrIn(&route.rt_genmask, netmask); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 979 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 980 | return ModifyRtentry(SIOCADDRT, &route); |
| 981 | } |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 982 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 983 | bool Datapath::DeleteIPv4Route(uint32_t gateway_addr, |
| 984 | uint32_t addr, |
| 985 | uint32_t netmask) { |
| 986 | struct rtentry route; |
| 987 | memset(&route, 0, sizeof(route)); |
| 988 | SetSockaddrIn(&route.rt_gateway, gateway_addr); |
| 989 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 990 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 991 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 992 | return ModifyRtentry(SIOCDELRT, &route); |
| 993 | } |
| 994 | |
| 995 | bool Datapath::AddIPv4Route(const std::string& ifname, |
| 996 | uint32_t addr, |
| 997 | uint32_t netmask) { |
| 998 | struct rtentry route; |
| 999 | memset(&route, 0, sizeof(route)); |
| 1000 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 1001 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 1002 | char rt_dev[IFNAMSIZ]; |
| 1003 | strncpy(rt_dev, ifname.c_str(), IFNAMSIZ); |
| 1004 | rt_dev[IFNAMSIZ - 1] = '\0'; |
| 1005 | route.rt_dev = rt_dev; |
| 1006 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 1007 | return ModifyRtentry(SIOCADDRT, &route); |
| 1008 | } |
| 1009 | |
| 1010 | bool Datapath::DeleteIPv4Route(const std::string& ifname, |
| 1011 | uint32_t addr, |
| 1012 | uint32_t netmask) { |
| 1013 | struct rtentry route; |
| 1014 | memset(&route, 0, sizeof(route)); |
| 1015 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 1016 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 1017 | char rt_dev[IFNAMSIZ]; |
| 1018 | strncpy(rt_dev, ifname.c_str(), IFNAMSIZ); |
| 1019 | rt_dev[IFNAMSIZ - 1] = '\0'; |
| 1020 | route.rt_dev = rt_dev; |
| 1021 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 1022 | return ModifyRtentry(SIOCDELRT, &route); |
| 1023 | } |
| 1024 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 1025 | bool Datapath::ModifyRtentry(ioctl_req_t op, struct rtentry* route) { |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 1026 | DCHECK(route); |
| 1027 | if (op != SIOCADDRT && op != SIOCDELRT) { |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 1028 | LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route; |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 1029 | return false; |
| 1030 | } |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 1031 | base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 1032 | if (!fd.is_valid()) { |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 1033 | PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 1034 | return false; |
| 1035 | } |
| 1036 | if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) { |
| 1037 | std::string opname = op == SIOCADDRT ? "add" : "delete"; |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 1038 | PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route; |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 1039 | return false; |
| 1040 | } |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 1044 | bool Datapath::AddAdbPortForwardRule(const std::string& ifname) { |
| 1045 | return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP, |
| 1046 | kArcAddr, kAdbServerPort, ifname, |
| 1047 | kLocalhostAddr, kAdbProxyTcpListenPort); |
| 1048 | } |
| 1049 | |
| 1050 | void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) { |
| 1051 | firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP, |
| 1052 | kArcAddr, kAdbServerPort, ifname, |
| 1053 | kLocalhostAddr, kAdbProxyTcpListenPort); |
| 1054 | } |
| 1055 | |
| 1056 | bool Datapath::AddAdbPortAccessRule(const std::string& ifname) { |
| 1057 | return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP, |
| 1058 | kAdbProxyTcpListenPort, ifname); |
| 1059 | } |
| 1060 | |
| 1061 | void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) { |
| 1062 | firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP, |
| 1063 | kAdbProxyTcpListenPort, ifname); |
| 1064 | } |
| 1065 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 1066 | void Datapath::SetIfnameIndex(const std::string& ifname, int ifindex) { |
| 1067 | if_nametoindex_[ifname] = ifindex; |
| 1068 | } |
| 1069 | |
| 1070 | int Datapath::FindIfIndex(const std::string& ifname) { |
| 1071 | uint32_t ifindex = if_nametoindex(ifname.c_str()); |
| 1072 | if (ifindex > 0) { |
| 1073 | if_nametoindex_[ifname] = ifindex; |
| 1074 | return ifindex; |
| 1075 | } |
| 1076 | |
| 1077 | const auto it = if_nametoindex_.find(ifname); |
| 1078 | if (it != if_nametoindex_.end()) |
| 1079 | return it->second; |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 1084 | } // namespace patchpanel |