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