blob: 5f6183c4f8b83a6cbd7c844b7f45fba800b504a8 [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 Benichi2a940542020-10-26 18:50:49 +090018#include <algorithm>
Hugo Benichid82d8832020-08-14 10:05:03 +090019
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>
Hugo Benichi58f264a2020-10-16 18:16:05 +090023#include <base/strings/string_util.h>
Garrick Evans54861622019-07-19 09:05:09 +090024#include <base/strings/string_number_conversions.h>
Garrick Evans4f9f5572019-11-26 10:25:16 +090025#include <brillo/userdb_utils.h>
Garrick Evans54861622019-07-19 09:05:09 +090026
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090027#include "patchpanel/adb_proxy.h"
Garrick Evans3388a032020-03-24 11:25:55 +090028#include "patchpanel/net_util.h"
29#include "patchpanel/scoped_ns.h"
Garrick Evansc7ae82c2019-09-04 16:25:10 +090030
Garrick Evans3388a032020-03-24 11:25:55 +090031namespace patchpanel {
Garrick Evans54861622019-07-19 09:05:09 +090032
Garrick Evansc7ae82c2019-09-04 16:25:10 +090033namespace {
Hugo Benichi76675592020-04-08 14:29:57 +090034// TODO(hugobenichi) Consolidate this constant definition in a single place.
35constexpr pid_t kTestPID = -2;
Garrick Evansc7ae82c2019-09-04 16:25:10 +090036constexpr char kDefaultIfname[] = "vmtap%d";
37constexpr char kTunDev[] = "/dev/net/tun";
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090038constexpr char kArcAddr[] = "100.115.92.2";
39constexpr char kLocalhostAddr[] = "127.0.0.1";
40constexpr uint16_t kAdbServerPort = 5555;
Hugo Benichie8758b52020-04-03 14:49:01 +090041
Hugo Benichibf811c62020-09-07 17:30:45 +090042// Constants used for dropping locally originated traffic bound to an incorrect
43// source IPv4 address.
44constexpr char kGuestIPv4Subnet[] = "100.115.92.0/23";
45constexpr std::array<const char*, 6> kPhysicalIfnamePrefixes{
46 {"eth+", "wlan+", "mlan+", "usb+", "wwan+", "rmnet+"}};
47
Hugo Benichi3a9162b2020-09-09 15:47:40 +090048constexpr char kApplyLocalSourceMarkChain[] = "apply_local_source_mark";
Hugo Benichi3ef370b2020-11-16 19:07:17 +090049constexpr char kApplyVpnMarkChain[] = "apply_vpn_mark";
50
Hugo Benichi2a940542020-10-26 18:50:49 +090051// Constant fwmark mask for matching local socket traffic that should be routed
52// through a VPN connection. The traffic must not be part of an existing
53// connection and must match exactly the VPN routing intent policy bit.
54const Fwmark kFwmarkVpnMatchingMask = kFwmarkRoutingMask | kFwmarkVpnMask;
55
Garrick Evans8a067562020-05-11 12:47:30 +090056std::string PrefixIfname(const std::string& prefix, const std::string& ifname) {
57 std::string n = prefix + ifname;
Garrick Evans2f581a02020-05-11 10:43:35 +090058 if (n.length() < IFNAMSIZ)
59 return n;
Garrick Evans54861622019-07-19 09:05:09 +090060
Garrick Evans2f581a02020-05-11 10:43:35 +090061 // Best effort attempt to preserve the interface number, assuming it's the
62 // last char in the name.
63 auto c = ifname[ifname.length() - 1];
64 n.resize(IFNAMSIZ - 1);
65 n[n.length() - 1] = c;
66 return n;
Garrick Evans54861622019-07-19 09:05:09 +090067}
Garrick Evansf0ab7132019-06-18 14:50:42 +090068
Garrick Evans8a067562020-05-11 12:47:30 +090069} // namespace
70
71std::string ArcVethHostName(const std::string& ifname) {
72 return PrefixIfname("veth", ifname);
73}
74
75std::string ArcBridgeName(const std::string& ifname) {
76 return PrefixIfname("arc_", ifname);
77}
78
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090079Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall)
80 : Datapath(process_runner, firewall, ioctl) {}
Garrick Evansc7ae82c2019-09-04 16:25:10 +090081
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090082Datapath::Datapath(MinijailedProcessRunner* process_runner,
83 Firewall* firewall,
84 ioctl_t ioctl_hook)
85 : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) {
Garrick Evansf0ab7132019-06-18 14:50:42 +090086 CHECK(process_runner_);
87}
88
Garrick Evans260ff302019-07-25 11:22:50 +090089MinijailedProcessRunner& Datapath::runner() const {
90 return *process_runner_;
91}
92
Hugo Benichibf811c62020-09-07 17:30:45 +090093void Datapath::Start() {
94 // Enable IPv4 packet forwarding
95 if (process_runner_->sysctl_w("net.ipv4.ip_forward", "1") != 0)
96 LOG(ERROR) << "Failed to update net.ipv4.ip_forward."
97 << " Guest connectivity will not work correctly.";
98
99 // Limit local port range: Android owns 47104-61000.
100 // TODO(garrick): The original history behind this tweak is gone. Some
101 // investigation is needed to see if it is still applicable.
102 if (process_runner_->sysctl_w("net.ipv4.ip_local_port_range",
103 "32768 47103") != 0)
104 LOG(ERROR) << "Failed to limit local port range. Some Android features or"
105 << " apps may not work correctly.";
106
107 // Enable IPv6 packet forwarding
108 if (process_runner_->sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0)
109 LOG(ERROR) << "Failed to update net.ipv6.conf.all.forwarding."
110 << " IPv6 functionality may be broken.";
111
112 if (!AddSNATMarkRules())
113 LOG(ERROR) << "Failed to install SNAT mark rules."
114 << " Guest connectivity may be broken.";
115
Hugo Benichi58125d32020-09-09 11:25:45 +0900116 // Create a FORWARD ACCEPT rule for connections already established.
117 if (process_runner_->iptables(
118 "filter", {"-A", "FORWARD", "-m", "state", "--state",
119 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) != 0)
Hugo Benichibf811c62020-09-07 17:30:45 +0900120 LOG(ERROR) << "Failed to install forwarding rule for established"
121 << " connections.";
122
123 // chromium:898210: Drop any locally originated traffic that would exit a
124 // physical interface with a source IPv4 address from the subnet of IPs used
125 // for VMs, containers, and connected namespaces This is needed to prevent
126 // packets leaking with an incorrect src IP when a local process binds to the
127 // wrong interface.
128 for (const auto& oif : kPhysicalIfnamePrefixes) {
129 if (!AddSourceIPv4DropRule(oif, kGuestIPv4Subnet))
130 LOG(WARNING) << "Failed to set up IPv4 drop rule for src ip "
131 << kGuestIPv4Subnet << " exiting " << oif;
132 }
133
134 if (!AddOutboundIPv4SNATMark("vmtap+"))
135 LOG(ERROR) << "Failed to set up NAT for TAP devices."
136 << " Guest connectivity may be broken.";
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900137
Hugo Benichi2a940542020-10-26 18:50:49 +0900138 // Applies the routing tag saved in conntrack for any established connection
139 // for sockets created in the host network namespace.
140 if (!ModifyConnmarkRestore(IpFamily::Dual, "OUTPUT", "-A", "" /*iif*/))
141 LOG(ERROR) << "Failed to add OUTPUT CONNMARK restore rule";
142
Hugo 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) {
Hugo Benichi620202f2020-11-27 10:14:38 +0900159 if (!ModifyFwmarkLocalSourceTag("-A", source))
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900160 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";
Hugo Benichibf811c62020-09-07 17:30:45 +0900187}
188
189void Datapath::Stop() {
190 RemoveOutboundIPv4SNATMark("vmtap+");
Hugo Benichi58125d32020-09-09 11:25:45 +0900191 process_runner_->iptables("filter",
192 {"-D", "FORWARD", "-m", "state", "--state",
193 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"});
Hugo Benichibf811c62020-09-07 17:30:45 +0900194 RemoveSNATMarkRules();
195 for (const auto& oif : kPhysicalIfnamePrefixes)
196 RemoveSourceIPv4DropRule(oif, kGuestIPv4Subnet);
197
198 // Restore original local port range.
199 // TODO(garrick): The original history behind this tweak is gone. Some
200 // investigation is needed to see if it is still applicable.
201 if (process_runner_->sysctl_w("net.ipv4.ip_local_port_range",
202 "32768 61000") != 0)
203 LOG(ERROR) << "Failed to restore local port range";
204
205 // Disable packet forwarding
206 if (process_runner_->sysctl_w("net.ipv6.conf.all.forwarding", "0") != 0)
207 LOG(ERROR) << "Failed to restore net.ipv6.conf.all.forwarding.";
208
209 if (process_runner_->sysctl_w("net.ipv4.ip_forward", "0") != 0)
210 LOG(ERROR) << "Failed to restore net.ipv4.ip_forward.";
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900211
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900212 // Detach the VPN marking mangle chain
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900213 if (!ModifyFwmarkVpnJumpRule("OUTPUT", "-D", "" /*iif*/, kFwmarkRouteOnVpn,
214 kFwmarkVpnMask))
215 LOG(ERROR)
216 << "Failed to remove from mangle OUTPUT chain jump rule to VPN chain";
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900217
218 // Detach apply_local_source_mark from mangle PREROUTING
219 if (!ModifyIptables(IpFamily::Dual, "mangle",
220 {"-D", "OUTPUT", "-j", kApplyLocalSourceMarkChain, "-w"}))
221 LOG(ERROR) << "Failed to detach " << kApplyLocalSourceMarkChain
222 << " from mangle OUTPUT";
223
Hugo Benichi2a940542020-10-26 18:50:49 +0900224 // Stops applying routing tags saved in conntrack for sockets created in the
225 // host network namespace.
226 if (!ModifyConnmarkRestore(IpFamily::Dual, "OUTPUT", "-D", "" /*iif*/))
227 LOG(ERROR) << "Failed to remove OUTPUT CONNMARK restore rule";
228
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900229 // Delete the mangle chains
230 for (const auto* chain : {kApplyLocalSourceMarkChain, kApplyVpnMarkChain}) {
231 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", chain))
232 LOG(ERROR) << "Failed to flush " << chain << " mangle chain";
233
234 if (!ModifyChain(IpFamily::Dual, "mangle", "-X", chain))
235 LOG(ERROR) << "Failed to delete " << chain << " mangle chain";
236 }
Hugo Benichibf811c62020-09-07 17:30:45 +0900237}
238
Hugo Benichi33860d72020-07-09 16:34:01 +0900239bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) {
240 // Try first to delete any netns with name |netns_name| in case patchpanel
241 // did not exit cleanly.
242 if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0)
243 LOG(INFO) << "Deleted left over network namespace name " << netns_name;
244 return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0;
245}
246
247bool Datapath::NetnsDeleteName(const std::string& netns_name) {
248 return process_runner_->ip_netns_delete(netns_name) == 0;
249}
250
Garrick Evans8a949dc2019-07-18 16:17:53 +0900251bool Datapath::AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900252 uint32_t ipv4_addr,
253 uint32_t ipv4_prefix_len) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900254 // Configure the persistent Chrome OS bridge interface with static IP.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900255 if (process_runner_->brctl("addbr", {ifname}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900256 return false;
257 }
258
Garrick Evans6f4fa3a2020-02-10 16:15:09 +0900259 if (process_runner_->ip(
260 "addr", "add",
261 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
262 IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
263 "dev", ifname}) != 0) {
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900264 RemoveBridge(ifname);
265 return false;
266 }
267
268 if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900269 RemoveBridge(ifname);
270 return false;
271 }
272
273 // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules.
Hugo Benichie8758b52020-04-03 14:49:01 +0900274 if (!AddOutboundIPv4SNATMark(ifname)) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900275 RemoveBridge(ifname);
276 return false;
277 }
278
279 return true;
280}
281
282void Datapath::RemoveBridge(const std::string& ifname) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900283 RemoveOutboundIPv4SNATMark(ifname);
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900284 process_runner_->ip("link", "set", {ifname, "down"});
Garrick Evans8e8e3472020-01-23 14:03:50 +0900285 process_runner_->brctl("delbr", {ifname});
Garrick Evans8a949dc2019-07-18 16:17:53 +0900286}
287
Garrick Evans621ed262019-11-13 12:28:43 +0900288bool Datapath::AddToBridge(const std::string& br_ifname,
289 const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900290 return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0);
Garrick Evans621ed262019-11-13 12:28:43 +0900291}
292
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900293std::string Datapath::AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900294 const MacAddress* mac_addr,
295 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900296 const std::string& user) {
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900297 base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK));
298 if (!dev.is_valid()) {
299 PLOG(ERROR) << "Failed to open " << kTunDev;
300 return "";
301 }
302
303 struct ifreq ifr;
304 memset(&ifr, 0, sizeof(ifr));
305 strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(),
306 sizeof(ifr.ifr_name));
307 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
308
309 // If a template was given as the name, ifr_name will be updated with the
310 // actual interface name.
311 if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900312 PLOG(ERROR) << "Failed to create tap interface " << name;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900313 return "";
314 }
315 const char* ifname = ifr.ifr_name;
316
317 if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900318 PLOG(ERROR) << "Failed to persist the interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900319 return "";
320 }
321
Garrick Evans4f9f5572019-11-26 10:25:16 +0900322 if (!user.empty()) {
323 uid_t uid = -1;
324 if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) {
325 PLOG(ERROR) << "Unable to look up UID for " << user;
326 RemoveTAP(ifname);
327 return "";
328 }
329 if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) {
330 PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface "
331 << ifname;
332 RemoveTAP(ifname);
333 return "";
334 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900335 }
336
Hugo Benichib9b93fe2019-10-25 23:36:01 +0900337 // Create control socket for configuring the interface.
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900338 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
339 if (!sock.is_valid()) {
340 PLOG(ERROR) << "Failed to create control socket for tap interface "
Garrick Evans621ed262019-11-13 12:28:43 +0900341 << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900342 RemoveTAP(ifname);
343 return "";
344 }
345
Garrick Evans621ed262019-11-13 12:28:43 +0900346 if (ipv4_addr) {
347 struct sockaddr_in* addr =
348 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr);
349 addr->sin_family = AF_INET;
350 addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address());
351 if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) {
352 PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname
353 << " {" << ipv4_addr->ToCidrString() << "}";
354 RemoveTAP(ifname);
355 return "";
356 }
357
358 struct sockaddr_in* netmask =
359 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask);
360 netmask->sin_family = AF_INET;
361 netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask());
362 if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) {
363 PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname
364 << " {" << ipv4_addr->ToCidrString() << "}";
365 RemoveTAP(ifname);
366 return "";
367 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900368 }
369
Garrick Evans621ed262019-11-13 12:28:43 +0900370 if (mac_addr) {
371 struct sockaddr* hwaddr = &ifr.ifr_hwaddr;
372 hwaddr->sa_family = ARPHRD_ETHER;
373 memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr));
374 if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) {
375 PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname
376 << " {" << MacAddressToString(*mac_addr) << "}";
377 RemoveTAP(ifname);
378 return "";
379 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900380 }
381
382 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900383 PLOG(ERROR) << "Failed to get flags for tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900384 RemoveTAP(ifname);
385 return "";
386 }
387
388 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
389 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900390 PLOG(ERROR) << "Failed to enable tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900391 RemoveTAP(ifname);
392 return "";
393 }
394
395 return ifname;
396}
397
398void Datapath::RemoveTAP(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900399 process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"});
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900400}
401
Hugo Benichi33860d72020-07-09 16:34:01 +0900402bool Datapath::ConnectVethPair(pid_t netns_pid,
403 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900404 const std::string& veth_ifname,
405 const std::string& peer_ifname,
406 const MacAddress& remote_mac_addr,
407 uint32_t remote_ipv4_addr,
408 uint32_t remote_ipv4_prefix_len,
409 bool remote_multicast_flag) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900410 // Set up the virtual pair across the current namespace and |netns_name|.
411 if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) {
412 LOG(ERROR) << "Failed to create veth pair " << veth_ifname << ","
413 << peer_ifname;
414 return false;
415 }
416
417 // Configure the remote veth in namespace |netns_name|.
Hugo Benichi76675592020-04-08 14:29:57 +0900418 {
Hugo Benichi33860d72020-07-09 16:34:01 +0900419 ScopedNS ns(netns_pid);
420 if (!ns.IsValid() && netns_pid != kTestPID) {
Hugo Benichi76675592020-04-08 14:29:57 +0900421 LOG(ERROR)
422 << "Cannot create virtual link -- invalid container namespace?";
423 return false;
424 }
425
Hugo Benichi76675592020-04-08 14:29:57 +0900426 if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr,
427 remote_ipv4_prefix_len, true /* link up */,
428 remote_multicast_flag)) {
429 LOG(ERROR) << "Failed to configure interface " << peer_ifname;
430 RemoveInterface(peer_ifname);
431 return false;
432 }
433 }
434
Hugo Benichi76675592020-04-08 14:29:57 +0900435 if (!ToggleInterface(veth_ifname, true /*up*/)) {
436 LOG(ERROR) << "Failed to bring up interface " << veth_ifname;
437 RemoveInterface(veth_ifname);
438 return false;
439 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900440
Hugo Benichi76675592020-04-08 14:29:57 +0900441 return true;
442}
443
Hugo Benichi33860d72020-07-09 16:34:01 +0900444bool Datapath::AddVirtualInterfacePair(const std::string& netns_name,
445 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +0900446 const std::string& peer_ifname) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900447 return process_runner_->ip("link", "add",
448 {veth_ifname, "type", "veth", "peer", "name",
449 peer_ifname, "netns", netns_name}) == 0;
Garrick Evans2470caa2020-03-04 14:15:41 +0900450}
Garrick Evans54861622019-07-19 09:05:09 +0900451
Garrick Evans2470caa2020-03-04 14:15:41 +0900452bool Datapath::ToggleInterface(const std::string& ifname, bool up) {
453 const std::string link = up ? "up" : "down";
454 return process_runner_->ip("link", "set", {ifname, link}) == 0;
455}
Garrick Evans54861622019-07-19 09:05:09 +0900456
Garrick Evans2470caa2020-03-04 14:15:41 +0900457bool Datapath::ConfigureInterface(const std::string& ifname,
458 const MacAddress& mac_addr,
459 uint32_t ipv4_addr,
460 uint32_t ipv4_prefix_len,
461 bool up,
462 bool enable_multicast) {
463 const std::string link = up ? "up" : "down";
464 const std::string multicast = enable_multicast ? "on" : "off";
465 return (process_runner_->ip(
466 "addr", "add",
467 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
468 IPv4AddressToString(
469 Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
470 "dev", ifname}) == 0) &&
471 (process_runner_->ip("link", "set",
472 {
473 "dev",
474 ifname,
475 link,
476 "addr",
477 MacAddressToString(mac_addr),
478 "multicast",
479 multicast,
480 }) == 0);
Garrick Evans54861622019-07-19 09:05:09 +0900481}
482
483void Datapath::RemoveInterface(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900484 process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/);
Garrick Evans54861622019-07-19 09:05:09 +0900485}
486
Hugo Benichi321f23b2020-09-25 15:42:05 +0900487bool Datapath::AddSourceIPv4DropRule(const std::string& oif,
488 const std::string& src_ip) {
489 return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s",
490 src_ip, "-j", "DROP", "-w"}) == 0;
491}
492
493bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif,
494 const std::string& src_ip) {
495 return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s",
496 src_ip, "-j", "DROP", "-w"}) == 0;
497}
498
Hugo Benichi7c342672020-09-08 09:18:14 +0900499bool Datapath::StartRoutingNamespace(pid_t pid,
500 const std::string& netns_name,
501 const std::string& host_ifname,
502 const std::string& peer_ifname,
503 uint32_t subnet_ipv4_addr,
504 uint32_t subnet_prefixlen,
505 uint32_t host_ipv4_addr,
506 uint32_t peer_ipv4_addr,
507 const MacAddress& peer_mac_addr) {
508 // Veth interface configuration and client routing configuration:
509 // - attach a name to the client namespace.
510 // - create veth pair across the current namespace and the client namespace.
511 // - configure IPv4 address on remote veth inside client namespace.
512 // - configure IPv4 address on local veth inside host namespace.
513 // - add a default IPv4 /0 route sending traffic to that remote veth.
514 if (!NetnsAttachName(netns_name, pid)) {
515 LOG(ERROR) << "Failed to attach name " << netns_name << " to namespace pid "
516 << pid;
517 return false;
518 }
519
520 if (!ConnectVethPair(pid, netns_name, host_ifname, peer_ifname, peer_mac_addr,
521 peer_ipv4_addr, subnet_prefixlen,
522 false /* enable_multicast */)) {
523 LOG(ERROR) << "Failed to create veth pair for"
524 " namespace pid "
525 << pid;
526 NetnsDeleteName(netns_name);
527 return false;
528 }
529
530 if (!ConfigureInterface(host_ifname, peer_mac_addr, host_ipv4_addr,
531 subnet_prefixlen, true /* link up */,
532 false /* enable_multicast */)) {
533 LOG(ERROR) << "Cannot configure host interface " << host_ifname;
534 RemoveInterface(host_ifname);
535 NetnsDeleteName(netns_name);
536 return false;
537 }
538
539 {
540 ScopedNS ns(pid);
541 if (!ns.IsValid() && pid != kTestPID) {
542 LOG(ERROR) << "Invalid namespace pid " << pid;
543 RemoveInterface(host_ifname);
544 NetnsDeleteName(netns_name);
545 return false;
546 }
547
548 if (!AddIPv4Route(host_ipv4_addr, INADDR_ANY, INADDR_ANY)) {
549 LOG(ERROR) << "Failed to add default /0 route to " << host_ifname
550 << " inside namespace pid " << pid;
551 RemoveInterface(host_ifname);
552 NetnsDeleteName(netns_name);
553 return false;
554 }
555 }
556
557 // Host namespace routing configuration
558 // - ingress: add route to client subnet via |host_ifname|.
559 // - egress: - allow forwarding for traffic outgoing |host_ifname|.
560 // - add SNAT mark 0x1/0x1 for traffic outgoing |host_ifname|.
561 // Note that by default unsolicited ingress traffic is not forwarded to the
562 // client namespace unless the client specifically set port forwarding
563 // through permission_broker DBus APIs.
564 // TODO(hugobenichi) If allow_user_traffic is false, then prevent forwarding
565 // both ways between client namespace and other guest containers and VMs.
566 // TODO(b/161507671) If outbound_physical_device is defined, then set strong
567 // routing to that interface routing table.
568 uint32_t netmask = Ipv4Netmask(subnet_prefixlen);
569 if (!AddIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask)) {
570 LOG(ERROR) << "Failed to set route to client namespace";
571 RemoveInterface(host_ifname);
572 NetnsDeleteName(netns_name);
573 return false;
574 }
575
Hugo Benichi58125d32020-09-09 11:25:45 +0900576 if (!StartIpForwarding(IpFamily::IPv4, "", host_ifname)) {
577 LOG(ERROR) << "Failed to allow FORWARD for ingress traffic into "
Hugo Benichi7c342672020-09-08 09:18:14 +0900578 << host_ifname;
579 RemoveInterface(host_ifname);
580 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask);
581 NetnsDeleteName(netns_name);
582 return false;
583 }
584
585 // TODO(b/161508179) Add fwmark source tagging based on client usage.
586 // TODO(b/161508179) Do not rely on legacy fwmark 1 for SNAT.
587 if (!AddOutboundIPv4SNATMark(host_ifname)) {
588 LOG(ERROR) << "Failed to set SNAT for traffic"
589 " outgoing from "
590 << host_ifname;
591 RemoveInterface(host_ifname);
592 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask);
Hugo Benichi58125d32020-09-09 11:25:45 +0900593 StopIpForwarding(IpFamily::IPv4, "", host_ifname);
Hugo Benichi7c342672020-09-08 09:18:14 +0900594 NetnsDeleteName(netns_name);
595 return false;
596 }
597
598 return true;
599}
600
601void Datapath::StopRoutingNamespace(const std::string& netns_name,
602 const std::string& host_ifname,
603 uint32_t subnet_ipv4_addr,
604 uint32_t subnet_prefixlen,
605 uint32_t host_ipv4_addr) {
606 RemoveInterface(host_ifname);
Hugo Benichi58125d32020-09-09 11:25:45 +0900607 StopIpForwarding(IpFamily::IPv4, "", host_ifname);
Hugo Benichi7c342672020-09-08 09:18:14 +0900608 RemoveOutboundIPv4SNATMark(host_ifname);
609 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr,
610 Ipv4Netmask(subnet_prefixlen));
611 NetnsDeleteName(netns_name);
612}
613
Hugo Benichi8d622b52020-08-13 15:24:12 +0900614void Datapath::StartRoutingDevice(const std::string& ext_ifname,
615 const std::string& int_ifname,
616 uint32_t int_ipv4_addr,
617 TrafficSource source) {
618 if (!ext_ifname.empty() &&
619 !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)))
620 LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname
621 << "->" << int_ifname;
622
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900623 if (!StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900624 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->"
625 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900626
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900627 if (!StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900628 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-"
629 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900630
Hugo Benichi2a940542020-10-26 18:50:49 +0900631 if (!ModifyFwmarkSourceTag("-A", int_ifname, source))
632 LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source "
633 << source << " for " << int_ifname;
634
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900635 if (!ext_ifname.empty()) {
636 // If |ext_ifname| is not null, mark egress traffic with the
637 // fwmark routing tag corresponding to |ext_ifname|.
Hugo Benichi2a940542020-10-26 18:50:49 +0900638 if (!ModifyFwmarkRoutingTag("PREROUTING", "-A", ext_ifname, int_ifname))
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900639 LOG(ERROR) << "Failed to add PREROUTING fwmark routing tag for "
640 << ext_ifname << "<-" << int_ifname;
641 } else {
642 // Otherwise if ext_ifname is null, set up a CONNMARK restore rule in
643 // PREROUTING to apply any fwmark routing tag saved for the current
644 // connection, and rely on implicit routing to the default logical network
645 // otherwise.
646 if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", int_ifname))
647 LOG(ERROR) << "Failed to add PREROUTING CONNMARK restore rule for "
648 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900649
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900650 // Forwarded traffic from downstream virtual devices routed to the system
651 // logical default network is always eligible to be routed through a VPN.
652 if (!ModifyFwmarkVpnJumpRule("PREROUTING", "-A", int_ifname, {}, {}))
653 LOG(ERROR) << "Failed to add jump rule to VPN chain for " << int_ifname;
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900654 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900655}
656
657void Datapath::StopRoutingDevice(const std::string& ext_ifname,
658 const std::string& int_ifname,
659 uint32_t int_ipv4_addr,
660 TrafficSource source) {
661 if (!ext_ifname.empty())
662 RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr));
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900663 StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname);
664 StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900665 ModifyFwmarkSourceTag("-D", int_ifname, source);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900666 if (!ext_ifname.empty()) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900667 ModifyFwmarkRoutingTag("PREROUTING", "-D", ext_ifname, int_ifname);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900668 } else {
669 ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", int_ifname);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900670 ModifyFwmarkVpnJumpRule("PREROUTING", "-D", int_ifname, {}, {});
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900671 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900672}
673
Garrick Evansf0ab7132019-06-18 14:50:42 +0900674bool Datapath::AddInboundIPv4DNAT(const std::string& ifname,
675 const std::string& ipv4_addr) {
676 // Direct ingress IP traffic to existing sockets.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900677 if (process_runner_->iptables(
678 "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket",
679 "--nowildcard", "-j", "ACCEPT", "-w"}) != 0)
Garrick Evansf0ab7132019-06-18 14:50:42 +0900680 return false;
681
682 // Direct ingress TCP & UDP traffic to ARC interface for new connections.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900683 if (process_runner_->iptables(
684 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
685 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900686 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
687 return false;
688 }
Garrick Evans8e8e3472020-01-23 14:03:50 +0900689 if (process_runner_->iptables(
690 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
691 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900692 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
693 return false;
694 }
695
696 return true;
697}
698
699void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname,
700 const std::string& ipv4_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900701 process_runner_->iptables(
702 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
703 "--to-destination", ipv4_addr, "-w"});
704 process_runner_->iptables(
705 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
706 "--to-destination", ipv4_addr, "-w"});
707 process_runner_->iptables(
708 "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard",
709 "-j", "ACCEPT", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900710}
711
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900712// TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded
713// egress traffic is routed through the fwmark routing tag.
Garrick Evansd291af62020-05-25 10:39:06 +0900714bool Datapath::AddSNATMarkRules() {
Taoyu Li79871c92020-07-02 16:09:39 +0900715 // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore
716 // need to be explicitly dropped.
717 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900718 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m",
Taoyu Li79871c92020-07-02 16:09:39 +0900719 "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) {
720 return false;
721 }
Garrick Evansd291af62020-05-25 10:39:06 +0900722 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900723 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900724 "ACCEPT", "-w"}) != 0) {
725 return false;
726 }
727 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900728 "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900729 "MASQUERADE", "-w"}) != 0) {
730 RemoveSNATMarkRules();
731 return false;
732 }
733 return true;
734}
735
736void Datapath::RemoveSNATMarkRules() {
737 process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900738 "1/1", "-j", "MASQUERADE", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900739 process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900740 "1/1", "-j", "ACCEPT", "-w"});
Taoyu Li79871c92020-07-02 16:09:39 +0900741 process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900742 "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state",
Taoyu Li79871c92020-07-02 16:09:39 +0900743 "--state", "INVALID", "-j", "DROP", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900744}
745
Hugo Benichie8758b52020-04-03 14:49:01 +0900746bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) {
747 return process_runner_->iptables(
748 "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK",
Hugo Benichi6c445322020-08-12 16:46:19 +0900749 "--set-mark", "1/1", "-w"}) == 0;
Hugo Benichie8758b52020-04-03 14:49:01 +0900750}
751
752void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) {
753 process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j",
Hugo Benichi6c445322020-08-12 16:46:19 +0900754 "MARK", "--set-mark", "1/1", "-w"});
Hugo Benichie8758b52020-04-03 14:49:01 +0900755}
756
Garrick Evans664a82f2019-12-17 12:18:05 +0900757bool Datapath::MaskInterfaceFlags(const std::string& ifname,
758 uint16_t on,
759 uint16_t off) {
Taoyu Li90c13912019-11-26 17:56:54 +0900760 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
761 if (!sock.is_valid()) {
762 PLOG(ERROR) << "Failed to create control socket";
763 return false;
764 }
765 ifreq ifr;
766 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str());
767 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) {
768 PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname;
769 return false;
770 }
Garrick Evans664a82f2019-12-17 12:18:05 +0900771 ifr.ifr_flags |= on;
772 ifr.ifr_flags &= ~off;
Taoyu Li90c13912019-11-26 17:56:54 +0900773 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900774 PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on
775 << " unset flag 0x" << std::hex << off << " on " << ifname;
Taoyu Li90c13912019-11-26 17:56:54 +0900776 return false;
777 }
778 return true;
779}
780
Garrick Evans260ff302019-07-25 11:22:50 +0900781bool Datapath::AddIPv6HostRoute(const std::string& ifname,
782 const std::string& ipv6_addr,
783 int ipv6_prefix_len) {
784 std::string ipv6_addr_cidr =
785 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
786
Garrick Evans8e8e3472020-01-23 14:03:50 +0900787 return process_runner_->ip6("route", "replace",
788 {ipv6_addr_cidr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900789}
790
791void Datapath::RemoveIPv6HostRoute(const std::string& ifname,
792 const std::string& ipv6_addr,
793 int ipv6_prefix_len) {
794 std::string ipv6_addr_cidr =
795 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
796
Garrick Evans8e8e3472020-01-23 14:03:50 +0900797 process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900798}
799
Taoyu Lia0727dc2020-09-24 19:54:59 +0900800bool Datapath::AddIPv6Address(const std::string& ifname,
801 const std::string& ipv6_addr) {
802 return process_runner_->ip6("addr", "add", {ipv6_addr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900803}
804
Taoyu Lia0727dc2020-09-24 19:54:59 +0900805void Datapath::RemoveIPv6Address(const std::string& ifname,
806 const std::string& ipv6_addr) {
807 process_runner_->ip6("addr", "del", {ipv6_addr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900808}
809
Hugo Benichi76be34a2020-08-26 22:35:54 +0900810void Datapath::StartConnectionPinning(const std::string& ext_ifname) {
811 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-A", ext_ifname))
812 LOG(ERROR) << "Could not start connection pinning on " << ext_ifname;
813}
814
815void Datapath::StopConnectionPinning(const std::string& ext_ifname) {
816 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-D", ext_ifname))
817 LOG(ERROR) << "Could not stop connection pinning on " << ext_ifname;
818}
819
Hugo Benichi2a940542020-10-26 18:50:49 +0900820void Datapath::StartVpnRouting(const std::string& vpn_ifname) {
821 StartConnectionPinning(vpn_ifname);
822 if (!ModifyFwmarkRoutingTag(kApplyVpnMarkChain, "-A", vpn_ifname, ""))
823 LOG(ERROR) << "Failed to set up VPN set-mark rule for " << vpn_ifname;
824}
825
826void Datapath::StopVpnRouting(const std::string& vpn_ifname) {
827 if (!ModifyFwmarkRoutingTag(kApplyVpnMarkChain, "-D", vpn_ifname, ""))
828 LOG(ERROR) << "Failed to remove VPN set-mark rule for " << vpn_ifname;
829 StopConnectionPinning(vpn_ifname);
830}
831
Hugo Benichi76be34a2020-08-26 22:35:54 +0900832bool Datapath::ModifyConnmarkSetPostrouting(IpFamily family,
833 const std::string& op,
834 const std::string& oif) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900835 int ifindex = FindIfIndex(oif);
836 if (ifindex == 0) {
837 PLOG(ERROR) << "if_nametoindex(" << oif << ") failed";
838 return false;
839 }
840
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900841 return ModifyConnmarkSet(family, "POSTROUTING", op, oif,
842 Fwmark::FromIfIndex(ifindex), kFwmarkRoutingMask);
843}
844
845bool Datapath::ModifyConnmarkSet(IpFamily family,
846 const std::string& chain,
847 const std::string& op,
848 const std::string& oif,
849 Fwmark mark,
850 Fwmark mask) {
851 if (chain != kApplyVpnMarkChain && (chain != "POSTROUTING" || oif.empty())) {
852 LOG(ERROR) << "Invalid arguments chain=" << chain << " oif=" << oif;
853 return false;
854 }
855
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900856 std::vector<std::string> args = {op, chain};
857 if (!oif.empty()) {
858 args.push_back("-o");
859 args.push_back(oif);
860 }
861 args.push_back("-j");
862 args.push_back("CONNMARK");
863 args.push_back("--set-mark");
864 args.push_back(mark.ToString() + "/" + mask.ToString());
865 args.push_back("-w");
Hugo Benichi76be34a2020-08-26 22:35:54 +0900866
Hugo Benichi58f264a2020-10-16 18:16:05 +0900867 return ModifyIptables(family, "mangle", args);
Hugo Benichi76be34a2020-08-26 22:35:54 +0900868}
869
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900870bool Datapath::ModifyConnmarkRestore(IpFamily family,
871 const std::string& chain,
872 const std::string& op,
873 const std::string& iif) {
874 if (chain != "OUTPUT" && (chain != "PREROUTING" || iif.empty())) {
875 LOG(ERROR) << "Invalid arguments chain=" << chain << " iif=" << iif;
876 return false;
877 }
878
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900879 std::vector<std::string> args = {op, chain};
880 if (!iif.empty()) {
881 args.push_back("-i");
882 args.push_back(iif);
883 }
884 args.insert(args.end(), {"-j", "CONNMARK", "--restore-mark", "--mask",
Hugo Benichi7c342672020-09-08 09:18:14 +0900885 kFwmarkRoutingMask.ToString(), "-w"});
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900886
Hugo Benichi58f264a2020-10-16 18:16:05 +0900887 return ModifyIptables(family, "mangle", args);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900888}
889
Hugo Benichi2a940542020-10-26 18:50:49 +0900890bool Datapath::ModifyFwmarkRoutingTag(const std::string& chain,
891 const std::string& op,
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900892 const std::string& ext_ifname,
893 const std::string& int_ifname) {
894 int ifindex = FindIfIndex(ext_ifname);
895 if (ifindex == 0) {
896 PLOG(ERROR) << "if_nametoindex(" << ext_ifname << ") failed";
897 return false;
898 }
899
Hugo Benichi2a940542020-10-26 18:50:49 +0900900 return ModifyFwmark(IpFamily::Dual, chain, op, int_ifname, "" /*uid_name*/,
901 Fwmark::FromIfIndex(ifindex), kFwmarkRoutingMask);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900902}
903
Hugo Benichi9be19b12020-08-14 15:33:40 +0900904bool Datapath::ModifyFwmarkSourceTag(const std::string& op,
905 const std::string& iif,
906 TrafficSource source) {
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900907 return ModifyFwmark(IpFamily::Dual, "PREROUTING", op, iif, "" /*uid_name*/,
908 Fwmark::FromSource(source), kFwmarkAllSourcesMask);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900909}
910
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900911bool Datapath::ModifyFwmarkDefaultLocalSourceTag(const std::string& op,
912 TrafficSource source) {
913 std::vector<std::string> args = {"-A",
914 kApplyLocalSourceMarkChain,
915 "-m",
916 "mark",
917 "--mark",
918 "0x0/" + kFwmarkAllSourcesMask.ToString(),
919 "-j",
920 "MARK",
921 "--set-mark",
922 Fwmark::FromSource(source).ToString() + "/" +
923 kFwmarkAllSourcesMask.ToString(),
924 "-w"};
925 return ModifyIptables(IpFamily::Dual, "mangle", args);
926}
Hugo Benichi9be19b12020-08-14 15:33:40 +0900927
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900928bool Datapath::ModifyFwmarkLocalSourceTag(const std::string& op,
929 const LocalSourceSpecs& source) {
930 Fwmark mark = Fwmark::FromSource(source.source_type);
931 if (source.is_on_vpn)
932 mark = mark | kFwmarkRouteOnVpn;
933
934 const std::string& uid_name = source.uid_name;
935 if (!uid_name.empty())
936 return ModifyFwmark(IpFamily::Dual, kApplyLocalSourceMarkChain, op,
937 "" /*iif*/, uid_name, mark, kFwmarkPolicyMask);
938
939 return false;
940 // TODO(b/167479541) Supports entries specifying a cgroup classid value.
941}
942
943bool Datapath::ModifyFwmark(IpFamily family,
944 const std::string& chain,
945 const std::string& op,
946 const std::string& iif,
947 const std::string& uid_name,
948 Fwmark mark,
949 Fwmark mask,
950 bool log_failures) {
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900951 std::vector<std::string> args = {op, chain};
952 if (!iif.empty()) {
953 args.push_back("-i");
954 args.push_back(iif);
955 }
956 if (!uid_name.empty()) {
957 args.push_back("-m");
958 args.push_back("owner");
959 args.push_back("--uid-owner");
960 args.push_back(uid_name);
961 }
962 args.push_back("-j");
963 args.push_back("MARK");
964 args.push_back("--set-mark");
965 args.push_back(mark.ToString() + "/" + mask.ToString());
966 args.push_back("-w");
Hugo Benichi9be19b12020-08-14 15:33:40 +0900967
Hugo Benichi58f264a2020-10-16 18:16:05 +0900968 return ModifyIptables(family, "mangle", args, log_failures);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900969}
970
Hugo Benichid82d8832020-08-14 10:05:03 +0900971bool Datapath::ModifyIpForwarding(IpFamily family,
972 const std::string& op,
973 const std::string& iif,
974 const std::string& oif,
975 bool log_failures) {
976 if (iif.empty() && oif.empty()) {
977 LOG(ERROR) << "Cannot change IP forwarding with no input or output "
978 "interface specified";
Garrick Evans260ff302019-07-25 11:22:50 +0900979 return false;
980 }
981
Hugo Benichid82d8832020-08-14 10:05:03 +0900982 std::vector<std::string> args = {op, "FORWARD"};
983 if (!iif.empty()) {
984 args.push_back("-i");
985 args.push_back(iif);
986 }
987 if (!oif.empty()) {
988 args.push_back("-o");
989 args.push_back(oif);
990 }
991 args.push_back("-j");
992 args.push_back("ACCEPT");
993 args.push_back("-w");
994
Hugo Benichi58f264a2020-10-16 18:16:05 +0900995 return ModifyIptables(family, "filter", args, log_failures);
Hugo Benichid82d8832020-08-14 10:05:03 +0900996}
997
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900998bool Datapath::ModifyFwmarkVpnJumpRule(const std::string& chain,
999 const std::string& op,
1000 const std::string& iif,
1001 Fwmark mark,
1002 Fwmark mask) {
1003 std::vector<std::string> args = {op, chain};
1004 if (!iif.empty()) {
1005 args.push_back("-i");
1006 args.push_back(iif);
1007 }
1008 if (mark.Value() != 0 && mask.Value() != 0) {
1009 args.push_back("-m");
1010 args.push_back("mark");
1011 args.push_back("--mark");
1012 args.push_back(mark.ToString() + "/" + mask.ToString());
1013 }
1014 args.insert(args.end(), {"-j", kApplyVpnMarkChain, "-w"});
1015 return ModifyIptables(IpFamily::Dual, "mangle", args);
1016}
1017
1018bool Datapath::ModifyChain(IpFamily family,
1019 const std::string& table,
1020 const std::string& op,
Hugo Benichi58f264a2020-10-16 18:16:05 +09001021 const std::string& chain,
1022 bool log_failures) {
1023 return ModifyIptables(family, table, {op, chain, "-w"}, log_failures);
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001024}
1025
1026bool Datapath::ModifyIptables(IpFamily family,
1027 const std::string& table,
Hugo Benichi58f264a2020-10-16 18:16:05 +09001028 const std::vector<std::string>& argv,
1029 bool log_failures) {
1030 switch (family) {
1031 case IPv4:
1032 case IPv6:
1033 case Dual:
1034 break;
1035 default:
1036 LOG(ERROR) << "Could not execute iptables command " << table
1037 << base::JoinString(argv, " ") << ": incorrect IP family "
1038 << family;
1039 return false;
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001040 }
1041
1042 bool success = true;
1043 if (family & IpFamily::IPv4)
Hugo Benichi58f264a2020-10-16 18:16:05 +09001044 success &= process_runner_->iptables(table, argv, log_failures) == 0;
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001045 if (family & IpFamily::IPv6)
Hugo Benichi58f264a2020-10-16 18:16:05 +09001046 success &= process_runner_->ip6tables(table, argv, log_failures) == 0;
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001047 return success;
1048}
1049
Hugo Benichid82d8832020-08-14 10:05:03 +09001050bool Datapath::StartIpForwarding(IpFamily family,
1051 const std::string& iif,
1052 const std::string& oif) {
1053 return ModifyIpForwarding(family, "-A", iif, oif);
1054}
1055
1056bool Datapath::StopIpForwarding(IpFamily family,
1057 const std::string& iif,
1058 const std::string& oif) {
1059 return ModifyIpForwarding(family, "-D", iif, oif);
1060}
1061
1062bool Datapath::AddIPv6Forwarding(const std::string& ifname1,
1063 const std::string& ifname2) {
1064 // Only start Ipv6 forwarding if -C returns false and it had not been
1065 // started yet.
1066 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2,
1067 false /*log_failures*/) &&
1068 !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) {
1069 return false;
1070 }
1071
1072 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1,
1073 false /*log_failures*/) &&
1074 !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) {
Garrick Evans260ff302019-07-25 11:22:50 +09001075 RemoveIPv6Forwarding(ifname1, ifname2);
1076 return false;
1077 }
1078
1079 return true;
1080}
1081
1082void Datapath::RemoveIPv6Forwarding(const std::string& ifname1,
1083 const std::string& ifname2) {
Hugo Benichid82d8832020-08-14 10:05:03 +09001084 StopIpForwarding(IpFamily::IPv6, ifname1, ifname2);
1085 StopIpForwarding(IpFamily::IPv6, ifname2, ifname1);
Garrick Evans260ff302019-07-25 11:22:50 +09001086}
1087
Garrick Evans3d97a392020-02-21 15:24:37 +09001088bool Datapath::AddIPv4Route(uint32_t gateway_addr,
1089 uint32_t addr,
1090 uint32_t netmask) {
1091 struct rtentry route;
1092 memset(&route, 0, sizeof(route));
Hugo Benichie8758b52020-04-03 14:49:01 +09001093 SetSockaddrIn(&route.rt_gateway, gateway_addr);
1094 SetSockaddrIn(&route.rt_dst, addr & netmask);
1095 SetSockaddrIn(&route.rt_genmask, netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +09001096 route.rt_flags = RTF_UP | RTF_GATEWAY;
Hugo Benichie8758b52020-04-03 14:49:01 +09001097 return ModifyRtentry(SIOCADDRT, &route);
1098}
Garrick Evans3d97a392020-02-21 15:24:37 +09001099
Hugo Benichie8758b52020-04-03 14:49:01 +09001100bool Datapath::DeleteIPv4Route(uint32_t gateway_addr,
1101 uint32_t addr,
1102 uint32_t netmask) {
1103 struct rtentry route;
1104 memset(&route, 0, sizeof(route));
1105 SetSockaddrIn(&route.rt_gateway, gateway_addr);
1106 SetSockaddrIn(&route.rt_dst, addr & netmask);
1107 SetSockaddrIn(&route.rt_genmask, netmask);
1108 route.rt_flags = RTF_UP | RTF_GATEWAY;
1109 return ModifyRtentry(SIOCDELRT, &route);
1110}
1111
1112bool Datapath::AddIPv4Route(const std::string& ifname,
1113 uint32_t addr,
1114 uint32_t netmask) {
1115 struct rtentry route;
1116 memset(&route, 0, sizeof(route));
1117 SetSockaddrIn(&route.rt_dst, addr & netmask);
1118 SetSockaddrIn(&route.rt_genmask, netmask);
1119 char rt_dev[IFNAMSIZ];
1120 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
1121 rt_dev[IFNAMSIZ - 1] = '\0';
1122 route.rt_dev = rt_dev;
1123 route.rt_flags = RTF_UP | RTF_GATEWAY;
1124 return ModifyRtentry(SIOCADDRT, &route);
1125}
1126
1127bool Datapath::DeleteIPv4Route(const std::string& ifname,
1128 uint32_t addr,
1129 uint32_t netmask) {
1130 struct rtentry route;
1131 memset(&route, 0, sizeof(route));
1132 SetSockaddrIn(&route.rt_dst, addr & netmask);
1133 SetSockaddrIn(&route.rt_genmask, netmask);
1134 char rt_dev[IFNAMSIZ];
1135 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
1136 rt_dev[IFNAMSIZ - 1] = '\0';
1137 route.rt_dev = rt_dev;
1138 route.rt_flags = RTF_UP | RTF_GATEWAY;
1139 return ModifyRtentry(SIOCDELRT, &route);
1140}
1141
Taoyu Lia0727dc2020-09-24 19:54:59 +09001142bool Datapath::ModifyRtentry(ioctl_req_t op, struct rtentry* route) {
Hugo Benichie8758b52020-04-03 14:49:01 +09001143 DCHECK(route);
1144 if (op != SIOCADDRT && op != SIOCDELRT) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001145 LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +09001146 return false;
1147 }
Hugo Benichie8758b52020-04-03 14:49:01 +09001148 base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
1149 if (!fd.is_valid()) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001150 PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route;
Hugo Benichie8758b52020-04-03 14:49:01 +09001151 return false;
1152 }
1153 if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) {
1154 std::string opname = op == SIOCADDRT ? "add" : "delete";
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001155 PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +09001156 return false;
1157 }
1158 return true;
1159}
1160
Jason Jeremy Imana7273a32020-08-04 11:25:31 +09001161bool Datapath::AddAdbPortForwardRule(const std::string& ifname) {
1162 return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
1163 kArcAddr, kAdbServerPort, ifname,
1164 kLocalhostAddr, kAdbProxyTcpListenPort);
1165}
1166
1167void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) {
1168 firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
1169 kArcAddr, kAdbServerPort, ifname,
1170 kLocalhostAddr, kAdbProxyTcpListenPort);
1171}
1172
1173bool Datapath::AddAdbPortAccessRule(const std::string& ifname) {
1174 return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
1175 kAdbProxyTcpListenPort, ifname);
1176}
1177
1178void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) {
1179 firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
1180 kAdbProxyTcpListenPort, ifname);
1181}
1182
Hugo Benichiaf9d8a72020-08-26 13:28:13 +09001183void Datapath::SetIfnameIndex(const std::string& ifname, int ifindex) {
1184 if_nametoindex_[ifname] = ifindex;
1185}
1186
1187int Datapath::FindIfIndex(const std::string& ifname) {
1188 uint32_t ifindex = if_nametoindex(ifname.c_str());
1189 if (ifindex > 0) {
1190 if_nametoindex_[ifname] = ifindex;
1191 return ifindex;
1192 }
1193
1194 const auto it = if_nametoindex_.find(ifname);
1195 if (it != if_nametoindex_.end())
1196 return it->second;
1197
1198 return 0;
1199}
1200
Garrick Evans3388a032020-03-24 11:25:55 +09001201} // namespace patchpanel