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