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