blob: 2f50daf44d02bda4daac4fdd7dcca57602ff285a [file] [log] [blame]
Garrick Evansf0ab7132019-06-18 14:50:42 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/datapath.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +09006
Garrick Evans3d97a392020-02-21 15:24:37 +09007#include <arpa/inet.h>
Garrick Evansc7ae82c2019-09-04 16:25:10 +09008#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 Benichid82d8832020-08-14 10:05:03 +090018#include <vector>
19
Garrick Evansc7ae82c2019-09-04 16:25:10 +090020#include <base/files/scoped_file.h>
21#include <base/logging.h>
Taoyu Li79871c92020-07-02 16:09:39 +090022#include <base/posix/eintr_wrapper.h>
Garrick Evans54861622019-07-19 09:05:09 +090023#include <base/strings/string_number_conversions.h>
Garrick Evans4f9f5572019-11-26 10:25:16 +090024#include <brillo/userdb_utils.h>
Garrick Evans54861622019-07-19 09:05:09 +090025
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090026#include "patchpanel/adb_proxy.h"
Garrick Evans3388a032020-03-24 11:25:55 +090027#include "patchpanel/net_util.h"
28#include "patchpanel/scoped_ns.h"
Garrick Evansc7ae82c2019-09-04 16:25:10 +090029
Garrick Evans3388a032020-03-24 11:25:55 +090030namespace patchpanel {
Garrick Evans54861622019-07-19 09:05:09 +090031
Garrick Evansc7ae82c2019-09-04 16:25:10 +090032namespace {
Hugo Benichi76675592020-04-08 14:29:57 +090033// TODO(hugobenichi) Consolidate this constant definition in a single place.
34constexpr pid_t kTestPID = -2;
Garrick Evansc7ae82c2019-09-04 16:25:10 +090035constexpr char kDefaultIfname[] = "vmtap%d";
36constexpr char kTunDev[] = "/dev/net/tun";
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090037constexpr char kArcAddr[] = "100.115.92.2";
38constexpr char kLocalhostAddr[] = "127.0.0.1";
39constexpr uint16_t kAdbServerPort = 5555;
Hugo Benichie8758b52020-04-03 14:49:01 +090040
Hugo Benichibf811c62020-09-07 17:30:45 +090041// Constants used for dropping locally originated traffic bound to an incorrect
42// source IPv4 address.
43constexpr char kGuestIPv4Subnet[] = "100.115.92.0/23";
44constexpr std::array<const char*, 6> kPhysicalIfnamePrefixes{
45 {"eth+", "wlan+", "mlan+", "usb+", "wwan+", "rmnet+"}};
46
Hugo Benichi3a9162b2020-09-09 15:47:40 +090047constexpr char kApplyLocalSourceMarkChain[] = "apply_local_source_mark";
Hugo Benichi3ef370b2020-11-16 19:07:17 +090048constexpr char kApplyVpnMarkChain[] = "apply_vpn_mark";
49
Garrick Evans8a067562020-05-11 12:47:30 +090050std::string PrefixIfname(const std::string& prefix, const std::string& ifname) {
51 std::string n = prefix + ifname;
Garrick Evans2f581a02020-05-11 10:43:35 +090052 if (n.length() < IFNAMSIZ)
53 return n;
Garrick Evans54861622019-07-19 09:05:09 +090054
Garrick Evans2f581a02020-05-11 10:43:35 +090055 // Best effort attempt to preserve the interface number, assuming it's the
56 // last char in the name.
57 auto c = ifname[ifname.length() - 1];
58 n.resize(IFNAMSIZ - 1);
59 n[n.length() - 1] = c;
60 return n;
Garrick Evans54861622019-07-19 09:05:09 +090061}
Garrick Evansf0ab7132019-06-18 14:50:42 +090062
Hugo Benichiaf9d8a72020-08-26 13:28:13 +090063bool IsValidIpFamily(IpFamily family) {
64 switch (family) {
65 case IPv4:
66 case IPv6:
67 case Dual:
68 return true;
69 default:
70 return false;
71 }
72}
73
Garrick Evans8a067562020-05-11 12:47:30 +090074} // namespace
75
76std::string ArcVethHostName(const std::string& ifname) {
77 return PrefixIfname("veth", ifname);
78}
79
80std::string ArcBridgeName(const std::string& ifname) {
81 return PrefixIfname("arc_", ifname);
82}
83
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090084Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall)
85 : Datapath(process_runner, firewall, ioctl) {}
Garrick Evansc7ae82c2019-09-04 16:25:10 +090086
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090087Datapath::Datapath(MinijailedProcessRunner* process_runner,
88 Firewall* firewall,
89 ioctl_t ioctl_hook)
90 : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) {
Garrick Evansf0ab7132019-06-18 14:50:42 +090091 CHECK(process_runner_);
92}
93
Garrick Evans260ff302019-07-25 11:22:50 +090094MinijailedProcessRunner& Datapath::runner() const {
95 return *process_runner_;
96}
97
Hugo Benichibf811c62020-09-07 17:30:45 +090098void Datapath::Start() {
99 // Enable IPv4 packet forwarding
100 if (process_runner_->sysctl_w("net.ipv4.ip_forward", "1") != 0)
101 LOG(ERROR) << "Failed to update net.ipv4.ip_forward."
102 << " Guest connectivity will not work correctly.";
103
104 // Limit local port range: Android owns 47104-61000.
105 // TODO(garrick): The original history behind this tweak is gone. Some
106 // investigation is needed to see if it is still applicable.
107 if (process_runner_->sysctl_w("net.ipv4.ip_local_port_range",
108 "32768 47103") != 0)
109 LOG(ERROR) << "Failed to limit local port range. Some Android features or"
110 << " apps may not work correctly.";
111
112 // Enable IPv6 packet forwarding
113 if (process_runner_->sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0)
114 LOG(ERROR) << "Failed to update net.ipv6.conf.all.forwarding."
115 << " IPv6 functionality may be broken.";
116
117 if (!AddSNATMarkRules())
118 LOG(ERROR) << "Failed to install SNAT mark rules."
119 << " Guest connectivity may be broken.";
120
Hugo Benichi58125d32020-09-09 11:25:45 +0900121 // Create a FORWARD ACCEPT rule for connections already established.
122 if (process_runner_->iptables(
123 "filter", {"-A", "FORWARD", "-m", "state", "--state",
124 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) != 0)
Hugo Benichibf811c62020-09-07 17:30:45 +0900125 LOG(ERROR) << "Failed to install forwarding rule for established"
126 << " connections.";
127
128 // chromium:898210: Drop any locally originated traffic that would exit a
129 // physical interface with a source IPv4 address from the subnet of IPs used
130 // for VMs, containers, and connected namespaces This is needed to prevent
131 // packets leaking with an incorrect src IP when a local process binds to the
132 // wrong interface.
133 for (const auto& oif : kPhysicalIfnamePrefixes) {
134 if (!AddSourceIPv4DropRule(oif, kGuestIPv4Subnet))
135 LOG(WARNING) << "Failed to set up IPv4 drop rule for src ip "
136 << kGuestIPv4Subnet << " exiting " << oif;
137 }
138
139 if (!AddOutboundIPv4SNATMark("vmtap+"))
140 LOG(ERROR) << "Failed to set up NAT for TAP devices."
141 << " Guest connectivity may be broken.";
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900142
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900143 // Set up a mangle chain used in OUTPUT for applying the fwmark TrafficSource
144 // tag and tagging the local traffic that should be routed through a VPN.
145 if (!ModifyChain(IpFamily::Dual, "mangle", "-N", kApplyLocalSourceMarkChain))
146 LOG(ERROR) << "Failed to set up " << kApplyLocalSourceMarkChain
147 << " mangle chain";
148 // Ensure that the chain is empty if patchpanel is restarting after a crash.
149 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", kApplyLocalSourceMarkChain))
150 LOG(ERROR) << "Failed to flush " << kApplyLocalSourceMarkChain
151 << " mangle chain";
152 if (!ModifyIptables(IpFamily::Dual, "mangle",
153 {"-A", "OUTPUT", "-j", kApplyLocalSourceMarkChain, "-w"}))
154 LOG(ERROR) << "Failed to attach " << kApplyLocalSourceMarkChain
155 << " to mangle OUTPUT";
156 // Create rules for tagging local sources with the source tag and the vpn
157 // policy tag.
158 for (const auto& source : kLocalSourceTypes) {
159 if (ModifyFwmarkLocalSourceTag("-A", source))
160 LOG(ERROR) << "Failed to create fwmark tagging rule for uid " << source
161 << " in " << kApplyLocalSourceMarkChain;
162 }
163 // Finally add a catch-all rule for tagging any remaining local sources with
164 // the SYSTEM source tag
165 if (!ModifyFwmarkDefaultLocalSourceTag("-A", TrafficSource::SYSTEM))
166 LOG(ERROR) << "Failed to set up rule tagging traffic with default source";
167
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900168 // Sets up a mangle chain used in OUTPUT and PREROUTING for tagging "user"
169 // traffic that should be routed through a VPN.
170 if (!ModifyChain(IpFamily::Dual, "mangle", "-N", kApplyVpnMarkChain))
171 LOG(ERROR) << "Failed to set up " << kApplyVpnMarkChain << " mangle chain";
172 // Ensure that the chain is empty if patchpanel is restarting after a crash.
173 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", kApplyVpnMarkChain))
174 LOG(ERROR) << "Failed to flush " << kApplyVpnMarkChain << " mangle chain";
175 // All local outgoing traffic eligible to VPN routing should traverse the VPN
176 // marking chain.
177 if (!ModifyFwmarkVpnJumpRule("OUTPUT", "-A", "" /*iif*/, kFwmarkRouteOnVpn,
178 kFwmarkVpnMask))
179 LOG(ERROR) << "Failed to add jump rule to VPN chain in mangle OUTPUT chain";
180 // Any traffic that already has a routing tag applied is accepted.
181 if (!ModifyIptables(
182 IpFamily::Dual, "mangle",
183 {"-A", kApplyVpnMarkChain, "-m", "mark", "!", "--mark",
184 "0x0/" + kFwmarkRoutingMask.ToString(), "-j", "ACCEPT", "-w"}))
185 LOG(ERROR) << "Failed to add ACCEPT rule to VPN tagging chain for marked "
186 "connections";
187 // TODO(b/161507671) Dynamically add fwmark routing tagging rules based on
188 // the VPN tunnel interface.
Hugo Benichibf811c62020-09-07 17:30:45 +0900189}
190
191void Datapath::Stop() {
192 RemoveOutboundIPv4SNATMark("vmtap+");
Hugo Benichi58125d32020-09-09 11:25:45 +0900193 process_runner_->iptables("filter",
194 {"-D", "FORWARD", "-m", "state", "--state",
195 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"});
Hugo Benichibf811c62020-09-07 17:30:45 +0900196 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 Benichi3ef370b2020-11-16 19:07:17 +0900213
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900214 // Detach the VPN marking mangle chain
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900215 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 Benichi3a9162b2020-09-09 15:47:40 +0900219
220 // Detach apply_local_source_mark from mangle PREROUTING
221 if (!ModifyIptables(IpFamily::Dual, "mangle",
222 {"-D", "OUTPUT", "-j", kApplyLocalSourceMarkChain, "-w"}))
223 LOG(ERROR) << "Failed to detach " << kApplyLocalSourceMarkChain
224 << " from mangle OUTPUT";
225
226 // Delete the mangle chains
227 for (const auto* chain : {kApplyLocalSourceMarkChain, kApplyVpnMarkChain}) {
228 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", chain))
229 LOG(ERROR) << "Failed to flush " << chain << " mangle chain";
230
231 if (!ModifyChain(IpFamily::Dual, "mangle", "-X", chain))
232 LOG(ERROR) << "Failed to delete " << chain << " mangle chain";
233 }
Hugo Benichibf811c62020-09-07 17:30:45 +0900234}
235
Hugo Benichi33860d72020-07-09 16:34:01 +0900236bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) {
237 // Try first to delete any netns with name |netns_name| in case patchpanel
238 // did not exit cleanly.
239 if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0)
240 LOG(INFO) << "Deleted left over network namespace name " << netns_name;
241 return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0;
242}
243
244bool Datapath::NetnsDeleteName(const std::string& netns_name) {
245 return process_runner_->ip_netns_delete(netns_name) == 0;
246}
247
Garrick Evans8a949dc2019-07-18 16:17:53 +0900248bool Datapath::AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900249 uint32_t ipv4_addr,
250 uint32_t ipv4_prefix_len) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900251 // Configure the persistent Chrome OS bridge interface with static IP.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900252 if (process_runner_->brctl("addbr", {ifname}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900253 return false;
254 }
255
Garrick Evans6f4fa3a2020-02-10 16:15:09 +0900256 if (process_runner_->ip(
257 "addr", "add",
258 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
259 IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
260 "dev", ifname}) != 0) {
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900261 RemoveBridge(ifname);
262 return false;
263 }
264
265 if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900266 RemoveBridge(ifname);
267 return false;
268 }
269
270 // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules.
Hugo Benichie8758b52020-04-03 14:49:01 +0900271 if (!AddOutboundIPv4SNATMark(ifname)) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900272 RemoveBridge(ifname);
273 return false;
274 }
275
276 return true;
277}
278
279void Datapath::RemoveBridge(const std::string& ifname) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900280 RemoveOutboundIPv4SNATMark(ifname);
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900281 process_runner_->ip("link", "set", {ifname, "down"});
Garrick Evans8e8e3472020-01-23 14:03:50 +0900282 process_runner_->brctl("delbr", {ifname});
Garrick Evans8a949dc2019-07-18 16:17:53 +0900283}
284
Garrick Evans621ed262019-11-13 12:28:43 +0900285bool Datapath::AddToBridge(const std::string& br_ifname,
286 const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900287 return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0);
Garrick Evans621ed262019-11-13 12:28:43 +0900288}
289
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900290std::string Datapath::AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900291 const MacAddress* mac_addr,
292 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900293 const std::string& user) {
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900294 base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK));
295 if (!dev.is_valid()) {
296 PLOG(ERROR) << "Failed to open " << kTunDev;
297 return "";
298 }
299
300 struct ifreq ifr;
301 memset(&ifr, 0, sizeof(ifr));
302 strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(),
303 sizeof(ifr.ifr_name));
304 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
305
306 // If a template was given as the name, ifr_name will be updated with the
307 // actual interface name.
308 if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900309 PLOG(ERROR) << "Failed to create tap interface " << name;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900310 return "";
311 }
312 const char* ifname = ifr.ifr_name;
313
314 if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900315 PLOG(ERROR) << "Failed to persist the interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900316 return "";
317 }
318
Garrick Evans4f9f5572019-11-26 10:25:16 +0900319 if (!user.empty()) {
320 uid_t uid = -1;
321 if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) {
322 PLOG(ERROR) << "Unable to look up UID for " << user;
323 RemoveTAP(ifname);
324 return "";
325 }
326 if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) {
327 PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface "
328 << ifname;
329 RemoveTAP(ifname);
330 return "";
331 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900332 }
333
Hugo Benichib9b93fe2019-10-25 23:36:01 +0900334 // Create control socket for configuring the interface.
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900335 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
336 if (!sock.is_valid()) {
337 PLOG(ERROR) << "Failed to create control socket for tap interface "
Garrick Evans621ed262019-11-13 12:28:43 +0900338 << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900339 RemoveTAP(ifname);
340 return "";
341 }
342
Garrick Evans621ed262019-11-13 12:28:43 +0900343 if (ipv4_addr) {
344 struct sockaddr_in* addr =
345 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr);
346 addr->sin_family = AF_INET;
347 addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address());
348 if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) {
349 PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname
350 << " {" << ipv4_addr->ToCidrString() << "}";
351 RemoveTAP(ifname);
352 return "";
353 }
354
355 struct sockaddr_in* netmask =
356 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask);
357 netmask->sin_family = AF_INET;
358 netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask());
359 if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) {
360 PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname
361 << " {" << ipv4_addr->ToCidrString() << "}";
362 RemoveTAP(ifname);
363 return "";
364 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900365 }
366
Garrick Evans621ed262019-11-13 12:28:43 +0900367 if (mac_addr) {
368 struct sockaddr* hwaddr = &ifr.ifr_hwaddr;
369 hwaddr->sa_family = ARPHRD_ETHER;
370 memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr));
371 if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) {
372 PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname
373 << " {" << MacAddressToString(*mac_addr) << "}";
374 RemoveTAP(ifname);
375 return "";
376 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900377 }
378
379 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900380 PLOG(ERROR) << "Failed to get flags for tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900381 RemoveTAP(ifname);
382 return "";
383 }
384
385 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
386 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900387 PLOG(ERROR) << "Failed to enable tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900388 RemoveTAP(ifname);
389 return "";
390 }
391
392 return ifname;
393}
394
395void Datapath::RemoveTAP(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900396 process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"});
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900397}
398
Hugo Benichi33860d72020-07-09 16:34:01 +0900399bool Datapath::ConnectVethPair(pid_t netns_pid,
400 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900401 const std::string& veth_ifname,
402 const std::string& peer_ifname,
403 const MacAddress& remote_mac_addr,
404 uint32_t remote_ipv4_addr,
405 uint32_t remote_ipv4_prefix_len,
406 bool remote_multicast_flag) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900407 // Set up the virtual pair across the current namespace and |netns_name|.
408 if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) {
409 LOG(ERROR) << "Failed to create veth pair " << veth_ifname << ","
410 << peer_ifname;
411 return false;
412 }
413
414 // Configure the remote veth in namespace |netns_name|.
Hugo Benichi76675592020-04-08 14:29:57 +0900415 {
Hugo Benichi33860d72020-07-09 16:34:01 +0900416 ScopedNS ns(netns_pid);
417 if (!ns.IsValid() && netns_pid != kTestPID) {
Hugo Benichi76675592020-04-08 14:29:57 +0900418 LOG(ERROR)
419 << "Cannot create virtual link -- invalid container namespace?";
420 return false;
421 }
422
Hugo Benichi76675592020-04-08 14:29:57 +0900423 if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr,
424 remote_ipv4_prefix_len, true /* link up */,
425 remote_multicast_flag)) {
426 LOG(ERROR) << "Failed to configure interface " << peer_ifname;
427 RemoveInterface(peer_ifname);
428 return false;
429 }
430 }
431
Hugo Benichi76675592020-04-08 14:29:57 +0900432 if (!ToggleInterface(veth_ifname, true /*up*/)) {
433 LOG(ERROR) << "Failed to bring up interface " << veth_ifname;
434 RemoveInterface(veth_ifname);
435 return false;
436 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900437
Hugo Benichi76675592020-04-08 14:29:57 +0900438 return true;
439}
440
Hugo Benichi33860d72020-07-09 16:34:01 +0900441bool Datapath::AddVirtualInterfacePair(const std::string& netns_name,
442 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +0900443 const std::string& peer_ifname) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900444 return process_runner_->ip("link", "add",
445 {veth_ifname, "type", "veth", "peer", "name",
446 peer_ifname, "netns", netns_name}) == 0;
Garrick Evans2470caa2020-03-04 14:15:41 +0900447}
Garrick Evans54861622019-07-19 09:05:09 +0900448
Garrick Evans2470caa2020-03-04 14:15:41 +0900449bool Datapath::ToggleInterface(const std::string& ifname, bool up) {
450 const std::string link = up ? "up" : "down";
451 return process_runner_->ip("link", "set", {ifname, link}) == 0;
452}
Garrick Evans54861622019-07-19 09:05:09 +0900453
Garrick Evans2470caa2020-03-04 14:15:41 +0900454bool Datapath::ConfigureInterface(const std::string& ifname,
455 const MacAddress& mac_addr,
456 uint32_t ipv4_addr,
457 uint32_t ipv4_prefix_len,
458 bool up,
459 bool enable_multicast) {
460 const std::string link = up ? "up" : "down";
461 const std::string multicast = enable_multicast ? "on" : "off";
462 return (process_runner_->ip(
463 "addr", "add",
464 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
465 IPv4AddressToString(
466 Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
467 "dev", ifname}) == 0) &&
468 (process_runner_->ip("link", "set",
469 {
470 "dev",
471 ifname,
472 link,
473 "addr",
474 MacAddressToString(mac_addr),
475 "multicast",
476 multicast,
477 }) == 0);
Garrick Evans54861622019-07-19 09:05:09 +0900478}
479
480void Datapath::RemoveInterface(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900481 process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/);
Garrick Evans54861622019-07-19 09:05:09 +0900482}
483
Hugo Benichi321f23b2020-09-25 15:42:05 +0900484bool Datapath::AddSourceIPv4DropRule(const std::string& oif,
485 const std::string& src_ip) {
486 return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s",
487 src_ip, "-j", "DROP", "-w"}) == 0;
488}
489
490bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif,
491 const std::string& src_ip) {
492 return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s",
493 src_ip, "-j", "DROP", "-w"}) == 0;
494}
495
Hugo Benichi7c342672020-09-08 09:18:14 +0900496bool Datapath::StartRoutingNamespace(pid_t pid,
497 const std::string& netns_name,
498 const std::string& host_ifname,
499 const std::string& peer_ifname,
500 uint32_t subnet_ipv4_addr,
501 uint32_t subnet_prefixlen,
502 uint32_t host_ipv4_addr,
503 uint32_t peer_ipv4_addr,
504 const MacAddress& peer_mac_addr) {
505 // Veth interface configuration and client routing configuration:
506 // - attach a name to the client namespace.
507 // - create veth pair across the current namespace and the client namespace.
508 // - configure IPv4 address on remote veth inside client namespace.
509 // - configure IPv4 address on local veth inside host namespace.
510 // - add a default IPv4 /0 route sending traffic to that remote veth.
511 if (!NetnsAttachName(netns_name, pid)) {
512 LOG(ERROR) << "Failed to attach name " << netns_name << " to namespace pid "
513 << pid;
514 return false;
515 }
516
517 if (!ConnectVethPair(pid, netns_name, host_ifname, peer_ifname, peer_mac_addr,
518 peer_ipv4_addr, subnet_prefixlen,
519 false /* enable_multicast */)) {
520 LOG(ERROR) << "Failed to create veth pair for"
521 " namespace pid "
522 << pid;
523 NetnsDeleteName(netns_name);
524 return false;
525 }
526
527 if (!ConfigureInterface(host_ifname, peer_mac_addr, host_ipv4_addr,
528 subnet_prefixlen, true /* link up */,
529 false /* enable_multicast */)) {
530 LOG(ERROR) << "Cannot configure host interface " << host_ifname;
531 RemoveInterface(host_ifname);
532 NetnsDeleteName(netns_name);
533 return false;
534 }
535
536 {
537 ScopedNS ns(pid);
538 if (!ns.IsValid() && pid != kTestPID) {
539 LOG(ERROR) << "Invalid namespace pid " << pid;
540 RemoveInterface(host_ifname);
541 NetnsDeleteName(netns_name);
542 return false;
543 }
544
545 if (!AddIPv4Route(host_ipv4_addr, INADDR_ANY, INADDR_ANY)) {
546 LOG(ERROR) << "Failed to add default /0 route to " << host_ifname
547 << " inside namespace pid " << pid;
548 RemoveInterface(host_ifname);
549 NetnsDeleteName(netns_name);
550 return false;
551 }
552 }
553
554 // Host namespace routing configuration
555 // - ingress: add route to client subnet via |host_ifname|.
556 // - egress: - allow forwarding for traffic outgoing |host_ifname|.
557 // - add SNAT mark 0x1/0x1 for traffic outgoing |host_ifname|.
558 // Note that by default unsolicited ingress traffic is not forwarded to the
559 // client namespace unless the client specifically set port forwarding
560 // through permission_broker DBus APIs.
561 // TODO(hugobenichi) If allow_user_traffic is false, then prevent forwarding
562 // both ways between client namespace and other guest containers and VMs.
563 // TODO(b/161507671) If outbound_physical_device is defined, then set strong
564 // routing to that interface routing table.
565 uint32_t netmask = Ipv4Netmask(subnet_prefixlen);
566 if (!AddIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask)) {
567 LOG(ERROR) << "Failed to set route to client namespace";
568 RemoveInterface(host_ifname);
569 NetnsDeleteName(netns_name);
570 return false;
571 }
572
Hugo Benichi58125d32020-09-09 11:25:45 +0900573 if (!StartIpForwarding(IpFamily::IPv4, "", host_ifname)) {
574 LOG(ERROR) << "Failed to allow FORWARD for ingress traffic into "
Hugo Benichi7c342672020-09-08 09:18:14 +0900575 << host_ifname;
576 RemoveInterface(host_ifname);
577 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask);
578 NetnsDeleteName(netns_name);
579 return false;
580 }
581
582 // TODO(b/161508179) Add fwmark source tagging based on client usage.
583 // TODO(b/161508179) Do not rely on legacy fwmark 1 for SNAT.
584 if (!AddOutboundIPv4SNATMark(host_ifname)) {
585 LOG(ERROR) << "Failed to set SNAT for traffic"
586 " outgoing from "
587 << host_ifname;
588 RemoveInterface(host_ifname);
589 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask);
Hugo Benichi58125d32020-09-09 11:25:45 +0900590 StopIpForwarding(IpFamily::IPv4, "", host_ifname);
Hugo Benichi7c342672020-09-08 09:18:14 +0900591 NetnsDeleteName(netns_name);
592 return false;
593 }
594
595 return true;
596}
597
598void Datapath::StopRoutingNamespace(const std::string& netns_name,
599 const std::string& host_ifname,
600 uint32_t subnet_ipv4_addr,
601 uint32_t subnet_prefixlen,
602 uint32_t host_ipv4_addr) {
603 RemoveInterface(host_ifname);
Hugo Benichi58125d32020-09-09 11:25:45 +0900604 StopIpForwarding(IpFamily::IPv4, "", host_ifname);
Hugo Benichi7c342672020-09-08 09:18:14 +0900605 RemoveOutboundIPv4SNATMark(host_ifname);
606 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr,
607 Ipv4Netmask(subnet_prefixlen));
608 NetnsDeleteName(netns_name);
609}
610
Hugo Benichi8d622b52020-08-13 15:24:12 +0900611void Datapath::StartRoutingDevice(const std::string& ext_ifname,
612 const std::string& int_ifname,
613 uint32_t int_ipv4_addr,
614 TrafficSource source) {
615 if (!ext_ifname.empty() &&
616 !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)))
617 LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname
618 << "->" << int_ifname;
619
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900620 if (!StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900621 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->"
622 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900623
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900624 if (!StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900625 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-"
626 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900627
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900628 if (!ext_ifname.empty()) {
629 // If |ext_ifname| is not null, mark egress traffic with the
630 // fwmark routing tag corresponding to |ext_ifname|.
631 if (!ModifyFwmarkRoutingTag("-A", ext_ifname, int_ifname))
632 LOG(ERROR) << "Failed to add PREROUTING fwmark routing tag for "
633 << ext_ifname << "<-" << int_ifname;
634 } else {
635 // Otherwise if ext_ifname is null, set up a CONNMARK restore rule in
636 // PREROUTING to apply any fwmark routing tag saved for the current
637 // connection, and rely on implicit routing to the default logical network
638 // otherwise.
639 if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", int_ifname))
640 LOG(ERROR) << "Failed to add PREROUTING CONNMARK restore rule for "
641 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900642
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900643 // Forwarded traffic from downstream virtual devices routed to the system
644 // logical default network is always eligible to be routed through a VPN.
645 if (!ModifyFwmarkVpnJumpRule("PREROUTING", "-A", int_ifname, {}, {}))
646 LOG(ERROR) << "Failed to add jump rule to VPN chain for " << int_ifname;
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900647 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900648
Hugo Benichi9be19b12020-08-14 15:33:40 +0900649 if (!ModifyFwmarkSourceTag("-A", int_ifname, source))
650 LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source "
651 << source << " for " << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900652}
653
654void Datapath::StopRoutingDevice(const std::string& ext_ifname,
655 const std::string& int_ifname,
656 uint32_t int_ipv4_addr,
657 TrafficSource source) {
658 if (!ext_ifname.empty())
659 RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr));
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900660 StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname);
661 StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900662 ModifyFwmarkSourceTag("-D", int_ifname, source);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900663 if (!ext_ifname.empty()) {
664 ModifyFwmarkRoutingTag("-D", ext_ifname, int_ifname);
665 } else {
666 ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", int_ifname);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900667 ModifyFwmarkVpnJumpRule("PREROUTING", "-D", int_ifname, {}, {});
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900668 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900669}
670
Garrick Evansf0ab7132019-06-18 14:50:42 +0900671bool Datapath::AddInboundIPv4DNAT(const std::string& ifname,
672 const std::string& ipv4_addr) {
673 // Direct ingress IP traffic to existing sockets.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900674 if (process_runner_->iptables(
675 "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket",
676 "--nowildcard", "-j", "ACCEPT", "-w"}) != 0)
Garrick Evansf0ab7132019-06-18 14:50:42 +0900677 return false;
678
679 // Direct ingress TCP & UDP traffic to ARC interface for new connections.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900680 if (process_runner_->iptables(
681 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
682 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900683 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
684 return false;
685 }
Garrick Evans8e8e3472020-01-23 14:03:50 +0900686 if (process_runner_->iptables(
687 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
688 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900689 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
690 return false;
691 }
692
693 return true;
694}
695
696void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname,
697 const std::string& ipv4_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900698 process_runner_->iptables(
699 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
700 "--to-destination", ipv4_addr, "-w"});
701 process_runner_->iptables(
702 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
703 "--to-destination", ipv4_addr, "-w"});
704 process_runner_->iptables(
705 "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard",
706 "-j", "ACCEPT", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900707}
708
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900709// TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded
710// egress traffic is routed through the fwmark routing tag.
Garrick Evansd291af62020-05-25 10:39:06 +0900711bool Datapath::AddSNATMarkRules() {
Taoyu Li79871c92020-07-02 16:09:39 +0900712 // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore
713 // need to be explicitly dropped.
714 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900715 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m",
Taoyu Li79871c92020-07-02 16:09:39 +0900716 "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) {
717 return false;
718 }
Garrick Evansd291af62020-05-25 10:39:06 +0900719 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900720 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900721 "ACCEPT", "-w"}) != 0) {
722 return false;
723 }
724 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900725 "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900726 "MASQUERADE", "-w"}) != 0) {
727 RemoveSNATMarkRules();
728 return false;
729 }
730 return true;
731}
732
733void Datapath::RemoveSNATMarkRules() {
734 process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900735 "1/1", "-j", "MASQUERADE", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900736 process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900737 "1/1", "-j", "ACCEPT", "-w"});
Taoyu Li79871c92020-07-02 16:09:39 +0900738 process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900739 "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state",
Taoyu Li79871c92020-07-02 16:09:39 +0900740 "--state", "INVALID", "-j", "DROP", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900741}
742
Hugo Benichie8758b52020-04-03 14:49:01 +0900743bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) {
744 return process_runner_->iptables(
745 "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK",
Hugo Benichi6c445322020-08-12 16:46:19 +0900746 "--set-mark", "1/1", "-w"}) == 0;
Hugo Benichie8758b52020-04-03 14:49:01 +0900747}
748
749void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) {
750 process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j",
Hugo Benichi6c445322020-08-12 16:46:19 +0900751 "MARK", "--set-mark", "1/1", "-w"});
Hugo Benichie8758b52020-04-03 14:49:01 +0900752}
753
Garrick Evans664a82f2019-12-17 12:18:05 +0900754bool Datapath::MaskInterfaceFlags(const std::string& ifname,
755 uint16_t on,
756 uint16_t off) {
Taoyu Li90c13912019-11-26 17:56:54 +0900757 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
758 if (!sock.is_valid()) {
759 PLOG(ERROR) << "Failed to create control socket";
760 return false;
761 }
762 ifreq ifr;
763 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str());
764 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) {
765 PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname;
766 return false;
767 }
Garrick Evans664a82f2019-12-17 12:18:05 +0900768 ifr.ifr_flags |= on;
769 ifr.ifr_flags &= ~off;
Taoyu Li90c13912019-11-26 17:56:54 +0900770 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900771 PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on
772 << " unset flag 0x" << std::hex << off << " on " << ifname;
Taoyu Li90c13912019-11-26 17:56:54 +0900773 return false;
774 }
775 return true;
776}
777
Garrick Evans260ff302019-07-25 11:22:50 +0900778bool Datapath::AddIPv6HostRoute(const std::string& ifname,
779 const std::string& ipv6_addr,
780 int ipv6_prefix_len) {
781 std::string ipv6_addr_cidr =
782 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
783
Garrick Evans8e8e3472020-01-23 14:03:50 +0900784 return process_runner_->ip6("route", "replace",
785 {ipv6_addr_cidr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900786}
787
788void Datapath::RemoveIPv6HostRoute(const std::string& ifname,
789 const std::string& ipv6_addr,
790 int ipv6_prefix_len) {
791 std::string ipv6_addr_cidr =
792 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
793
Garrick Evans8e8e3472020-01-23 14:03:50 +0900794 process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900795}
796
Taoyu Lia0727dc2020-09-24 19:54:59 +0900797bool Datapath::AddIPv6Address(const std::string& ifname,
798 const std::string& ipv6_addr) {
799 return process_runner_->ip6("addr", "add", {ipv6_addr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900800}
801
Taoyu Lia0727dc2020-09-24 19:54:59 +0900802void Datapath::RemoveIPv6Address(const std::string& ifname,
803 const std::string& ipv6_addr) {
804 process_runner_->ip6("addr", "del", {ipv6_addr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900805}
806
Hugo Benichi76be34a2020-08-26 22:35:54 +0900807void Datapath::StartConnectionPinning(const std::string& ext_ifname) {
808 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-A", ext_ifname))
809 LOG(ERROR) << "Could not start connection pinning on " << ext_ifname;
810}
811
812void Datapath::StopConnectionPinning(const std::string& ext_ifname) {
813 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-D", ext_ifname))
814 LOG(ERROR) << "Could not stop connection pinning on " << ext_ifname;
815}
816
817bool Datapath::ModifyConnmarkSetPostrouting(IpFamily family,
818 const std::string& op,
819 const std::string& oif) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900820 int ifindex = FindIfIndex(oif);
821 if (ifindex == 0) {
822 PLOG(ERROR) << "if_nametoindex(" << oif << ") failed";
823 return false;
824 }
825
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900826 return ModifyConnmarkSet(family, "POSTROUTING", op, oif,
827 Fwmark::FromIfIndex(ifindex), kFwmarkRoutingMask);
828}
829
830bool Datapath::ModifyConnmarkSet(IpFamily family,
831 const std::string& chain,
832 const std::string& op,
833 const std::string& oif,
834 Fwmark mark,
835 Fwmark mask) {
836 if (chain != kApplyVpnMarkChain && (chain != "POSTROUTING" || oif.empty())) {
837 LOG(ERROR) << "Invalid arguments chain=" << chain << " oif=" << oif;
838 return false;
839 }
840
841 if (!IsValidIpFamily(family)) {
842 LOG(ERROR) << "Cannot change " << chain << " CONNMARK set-mark for " << oif
843 << ": incorrect IP family " << family;
844 return false;
845 }
846
847 std::vector<std::string> args = {op, chain};
848 if (!oif.empty()) {
849 args.push_back("-o");
850 args.push_back(oif);
851 }
852 args.push_back("-j");
853 args.push_back("CONNMARK");
854 args.push_back("--set-mark");
855 args.push_back(mark.ToString() + "/" + mask.ToString());
856 args.push_back("-w");
Hugo Benichi76be34a2020-08-26 22:35:54 +0900857
858 bool success = true;
859 if (family & IPv4)
860 success &= process_runner_->iptables("mangle", args) == 0;
861 if (family & IPv6)
862 success &= process_runner_->ip6tables("mangle", args) == 0;
863 return false;
864}
865
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900866bool Datapath::ModifyConnmarkRestore(IpFamily family,
867 const std::string& chain,
868 const std::string& op,
869 const std::string& iif) {
870 if (chain != "OUTPUT" && (chain != "PREROUTING" || iif.empty())) {
871 LOG(ERROR) << "Invalid arguments chain=" << chain << " iif=" << iif;
872 return false;
873 }
874
875 if (!IsValidIpFamily(family)) {
876 LOG(ERROR) << "Cannot change " << chain << " -j CONNMARK restore-mark"
877 << " for " << iif << ": incorrect IP family " << family;
878 return false;
879 }
880
881 std::vector<std::string> args = {op, chain};
882 if (!iif.empty()) {
883 args.push_back("-i");
884 args.push_back(iif);
885 }
886 args.insert(args.end(), {"-j", "CONNMARK", "--restore-mark", "--mask",
Hugo Benichi7c342672020-09-08 09:18:14 +0900887 kFwmarkRoutingMask.ToString(), "-w"});
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900888
889 bool success = true;
890 if (family & IPv4)
891 success &= process_runner_->iptables("mangle", args) == 0;
892 if (family & IPv6)
893 success &= process_runner_->ip6tables("mangle", args) == 0;
894 return success;
895}
896
897bool Datapath::ModifyFwmarkRoutingTag(const std::string& op,
898 const std::string& ext_ifname,
899 const std::string& int_ifname) {
900 int ifindex = FindIfIndex(ext_ifname);
901 if (ifindex == 0) {
902 PLOG(ERROR) << "if_nametoindex(" << ext_ifname << ") failed";
903 return false;
904 }
905
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900906 return ModifyFwmark(IpFamily::Dual, "PREROUTING", op, int_ifname,
907 "" /*uid_name*/, Fwmark::FromIfIndex(ifindex),
908 kFwmarkRoutingMask);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900909}
910
Hugo Benichi9be19b12020-08-14 15:33:40 +0900911bool Datapath::ModifyFwmarkSourceTag(const std::string& op,
912 const std::string& iif,
913 TrafficSource source) {
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900914 return ModifyFwmark(IpFamily::Dual, "PREROUTING", op, iif, "" /*uid_name*/,
915 Fwmark::FromSource(source), kFwmarkAllSourcesMask);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900916}
917
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900918bool Datapath::ModifyFwmarkDefaultLocalSourceTag(const std::string& op,
919 TrafficSource source) {
920 std::vector<std::string> args = {"-A",
921 kApplyLocalSourceMarkChain,
922 "-m",
923 "mark",
924 "--mark",
925 "0x0/" + kFwmarkAllSourcesMask.ToString(),
926 "-j",
927 "MARK",
928 "--set-mark",
929 Fwmark::FromSource(source).ToString() + "/" +
930 kFwmarkAllSourcesMask.ToString(),
931 "-w"};
932 return ModifyIptables(IpFamily::Dual, "mangle", args);
933}
Hugo Benichi9be19b12020-08-14 15:33:40 +0900934
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900935bool Datapath::ModifyFwmarkLocalSourceTag(const std::string& op,
936 const LocalSourceSpecs& source) {
937 Fwmark mark = Fwmark::FromSource(source.source_type);
938 if (source.is_on_vpn)
939 mark = mark | kFwmarkRouteOnVpn;
940
941 const std::string& uid_name = source.uid_name;
942 if (!uid_name.empty())
943 return ModifyFwmark(IpFamily::Dual, kApplyLocalSourceMarkChain, op,
944 "" /*iif*/, uid_name, mark, kFwmarkPolicyMask);
945
946 return false;
947 // TODO(b/167479541) Supports entries specifying a cgroup classid value.
948}
949
950bool Datapath::ModifyFwmark(IpFamily family,
951 const std::string& chain,
952 const std::string& op,
953 const std::string& iif,
954 const std::string& uid_name,
955 Fwmark mark,
956 Fwmark mask,
957 bool log_failures) {
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900958 if (!IsValidIpFamily(family)) {
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900959 LOG(ERROR) << "Cannot change " << chain << " set-fwmark for " << iif
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900960 << ": incorrect IP family " << family;
961 return false;
Hugo Benichi9be19b12020-08-14 15:33:40 +0900962 }
963
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900964 std::vector<std::string> args = {op, chain};
965 if (!iif.empty()) {
966 args.push_back("-i");
967 args.push_back(iif);
968 }
969 if (!uid_name.empty()) {
970 args.push_back("-m");
971 args.push_back("owner");
972 args.push_back("--uid-owner");
973 args.push_back(uid_name);
974 }
975 args.push_back("-j");
976 args.push_back("MARK");
977 args.push_back("--set-mark");
978 args.push_back(mark.ToString() + "/" + mask.ToString());
979 args.push_back("-w");
Hugo Benichi9be19b12020-08-14 15:33:40 +0900980
981 bool success = true;
Hugo Benichi5c9c11c2020-09-15 17:25:26 +0900982 if (family & IPv4)
Hugo Benichi9be19b12020-08-14 15:33:40 +0900983 success &= process_runner_->iptables("mangle", args, log_failures) == 0;
Hugo Benichi5c9c11c2020-09-15 17:25:26 +0900984 if (family & IPv6)
Hugo Benichi9be19b12020-08-14 15:33:40 +0900985 success &= process_runner_->ip6tables("mangle", args, log_failures) == 0;
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900986 return success;
Hugo Benichi9be19b12020-08-14 15:33:40 +0900987}
988
Hugo Benichid82d8832020-08-14 10:05:03 +0900989bool Datapath::ModifyIpForwarding(IpFamily family,
990 const std::string& op,
991 const std::string& iif,
992 const std::string& oif,
993 bool log_failures) {
994 if (iif.empty() && oif.empty()) {
995 LOG(ERROR) << "Cannot change IP forwarding with no input or output "
996 "interface specified";
Garrick Evans260ff302019-07-25 11:22:50 +0900997 return false;
998 }
999
Hugo Benichiaf9d8a72020-08-26 13:28:13 +09001000 if (!IsValidIpFamily(family)) {
1001 LOG(ERROR) << "Cannot change IP forwarding from \"" << iif << "\" to \""
1002 << oif << "\": incorrect IP family " << family;
1003 return false;
Hugo Benichid82d8832020-08-14 10:05:03 +09001004 }
1005
1006 std::vector<std::string> args = {op, "FORWARD"};
1007 if (!iif.empty()) {
1008 args.push_back("-i");
1009 args.push_back(iif);
1010 }
1011 if (!oif.empty()) {
1012 args.push_back("-o");
1013 args.push_back(oif);
1014 }
1015 args.push_back("-j");
1016 args.push_back("ACCEPT");
1017 args.push_back("-w");
1018
1019 bool success = true;
1020 if (family & IpFamily::IPv4)
1021 success &= process_runner_->iptables("filter", args, log_failures) == 0;
1022 if (family & IpFamily::IPv6)
1023 success &= process_runner_->ip6tables("filter", args, log_failures) == 0;
1024 return success;
1025}
1026
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001027bool Datapath::ModifyFwmarkVpnJumpRule(const std::string& chain,
1028 const std::string& op,
1029 const std::string& iif,
1030 Fwmark mark,
1031 Fwmark mask) {
1032 std::vector<std::string> args = {op, chain};
1033 if (!iif.empty()) {
1034 args.push_back("-i");
1035 args.push_back(iif);
1036 }
1037 if (mark.Value() != 0 && mask.Value() != 0) {
1038 args.push_back("-m");
1039 args.push_back("mark");
1040 args.push_back("--mark");
1041 args.push_back(mark.ToString() + "/" + mask.ToString());
1042 }
1043 args.insert(args.end(), {"-j", kApplyVpnMarkChain, "-w"});
1044 return ModifyIptables(IpFamily::Dual, "mangle", args);
1045}
1046
1047bool Datapath::ModifyChain(IpFamily family,
1048 const std::string& table,
1049 const std::string& op,
1050 const std::string& chain) {
1051 return ModifyIptables(family, table, {op, chain, "-w"});
1052}
1053
1054bool Datapath::ModifyIptables(IpFamily family,
1055 const std::string& table,
1056 const std::vector<std::string>& argv) {
1057 if (!IsValidIpFamily(family)) {
1058 LOG(ERROR) << "Incorrect IP family " << family;
1059 return false;
1060 }
1061
1062 bool success = true;
1063 if (family & IpFamily::IPv4)
1064 success &= process_runner_->iptables(table, argv) == 0;
1065 if (family & IpFamily::IPv6)
1066 success &= process_runner_->ip6tables(table, argv) == 0;
1067 return success;
1068}
1069
Hugo Benichid82d8832020-08-14 10:05:03 +09001070bool Datapath::StartIpForwarding(IpFamily family,
1071 const std::string& iif,
1072 const std::string& oif) {
1073 return ModifyIpForwarding(family, "-A", iif, oif);
1074}
1075
1076bool Datapath::StopIpForwarding(IpFamily family,
1077 const std::string& iif,
1078 const std::string& oif) {
1079 return ModifyIpForwarding(family, "-D", iif, oif);
1080}
1081
1082bool Datapath::AddIPv6Forwarding(const std::string& ifname1,
1083 const std::string& ifname2) {
1084 // Only start Ipv6 forwarding if -C returns false and it had not been
1085 // started yet.
1086 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2,
1087 false /*log_failures*/) &&
1088 !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) {
1089 return false;
1090 }
1091
1092 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1,
1093 false /*log_failures*/) &&
1094 !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) {
Garrick Evans260ff302019-07-25 11:22:50 +09001095 RemoveIPv6Forwarding(ifname1, ifname2);
1096 return false;
1097 }
1098
1099 return true;
1100}
1101
1102void Datapath::RemoveIPv6Forwarding(const std::string& ifname1,
1103 const std::string& ifname2) {
Hugo Benichid82d8832020-08-14 10:05:03 +09001104 StopIpForwarding(IpFamily::IPv6, ifname1, ifname2);
1105 StopIpForwarding(IpFamily::IPv6, ifname2, ifname1);
Garrick Evans260ff302019-07-25 11:22:50 +09001106}
1107
Garrick Evans3d97a392020-02-21 15:24:37 +09001108bool Datapath::AddIPv4Route(uint32_t gateway_addr,
1109 uint32_t addr,
1110 uint32_t netmask) {
1111 struct rtentry route;
1112 memset(&route, 0, sizeof(route));
Hugo Benichie8758b52020-04-03 14:49:01 +09001113 SetSockaddrIn(&route.rt_gateway, gateway_addr);
1114 SetSockaddrIn(&route.rt_dst, addr & netmask);
1115 SetSockaddrIn(&route.rt_genmask, netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +09001116 route.rt_flags = RTF_UP | RTF_GATEWAY;
Hugo Benichie8758b52020-04-03 14:49:01 +09001117 return ModifyRtentry(SIOCADDRT, &route);
1118}
Garrick Evans3d97a392020-02-21 15:24:37 +09001119
Hugo Benichie8758b52020-04-03 14:49:01 +09001120bool Datapath::DeleteIPv4Route(uint32_t gateway_addr,
1121 uint32_t addr,
1122 uint32_t netmask) {
1123 struct rtentry route;
1124 memset(&route, 0, sizeof(route));
1125 SetSockaddrIn(&route.rt_gateway, gateway_addr);
1126 SetSockaddrIn(&route.rt_dst, addr & netmask);
1127 SetSockaddrIn(&route.rt_genmask, netmask);
1128 route.rt_flags = RTF_UP | RTF_GATEWAY;
1129 return ModifyRtentry(SIOCDELRT, &route);
1130}
1131
1132bool Datapath::AddIPv4Route(const std::string& ifname,
1133 uint32_t addr,
1134 uint32_t netmask) {
1135 struct rtentry route;
1136 memset(&route, 0, sizeof(route));
1137 SetSockaddrIn(&route.rt_dst, addr & netmask);
1138 SetSockaddrIn(&route.rt_genmask, netmask);
1139 char rt_dev[IFNAMSIZ];
1140 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
1141 rt_dev[IFNAMSIZ - 1] = '\0';
1142 route.rt_dev = rt_dev;
1143 route.rt_flags = RTF_UP | RTF_GATEWAY;
1144 return ModifyRtentry(SIOCADDRT, &route);
1145}
1146
1147bool Datapath::DeleteIPv4Route(const std::string& ifname,
1148 uint32_t addr,
1149 uint32_t netmask) {
1150 struct rtentry route;
1151 memset(&route, 0, sizeof(route));
1152 SetSockaddrIn(&route.rt_dst, addr & netmask);
1153 SetSockaddrIn(&route.rt_genmask, netmask);
1154 char rt_dev[IFNAMSIZ];
1155 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
1156 rt_dev[IFNAMSIZ - 1] = '\0';
1157 route.rt_dev = rt_dev;
1158 route.rt_flags = RTF_UP | RTF_GATEWAY;
1159 return ModifyRtentry(SIOCDELRT, &route);
1160}
1161
Taoyu Lia0727dc2020-09-24 19:54:59 +09001162bool Datapath::ModifyRtentry(ioctl_req_t op, struct rtentry* route) {
Hugo Benichie8758b52020-04-03 14:49:01 +09001163 DCHECK(route);
1164 if (op != SIOCADDRT && op != SIOCDELRT) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001165 LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +09001166 return false;
1167 }
Hugo Benichie8758b52020-04-03 14:49:01 +09001168 base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
1169 if (!fd.is_valid()) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001170 PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route;
Hugo Benichie8758b52020-04-03 14:49:01 +09001171 return false;
1172 }
1173 if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) {
1174 std::string opname = op == SIOCADDRT ? "add" : "delete";
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001175 PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +09001176 return false;
1177 }
1178 return true;
1179}
1180
Jason Jeremy Imana7273a32020-08-04 11:25:31 +09001181bool Datapath::AddAdbPortForwardRule(const std::string& ifname) {
1182 return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
1183 kArcAddr, kAdbServerPort, ifname,
1184 kLocalhostAddr, kAdbProxyTcpListenPort);
1185}
1186
1187void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) {
1188 firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
1189 kArcAddr, kAdbServerPort, ifname,
1190 kLocalhostAddr, kAdbProxyTcpListenPort);
1191}
1192
1193bool Datapath::AddAdbPortAccessRule(const std::string& ifname) {
1194 return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
1195 kAdbProxyTcpListenPort, ifname);
1196}
1197
1198void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) {
1199 firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
1200 kAdbProxyTcpListenPort, ifname);
1201}
1202
Hugo Benichiaf9d8a72020-08-26 13:28:13 +09001203void Datapath::SetIfnameIndex(const std::string& ifname, int ifindex) {
1204 if_nametoindex_[ifname] = ifindex;
1205}
1206
1207int Datapath::FindIfIndex(const std::string& ifname) {
1208 uint32_t ifindex = if_nametoindex(ifname.c_str());
1209 if (ifindex > 0) {
1210 if_nametoindex_[ifname] = ifindex;
1211 return ifindex;
1212 }
1213
1214 const auto it = if_nametoindex_.find(ifname);
1215 if (it != if_nametoindex_.end())
1216 return it->second;
1217
1218 return 0;
1219}
1220
Garrick Evans3388a032020-03-24 11:25:55 +09001221} // namespace patchpanel