blob: b9299aa3a9c31f9921a6749db8da6ac26cd03cdd [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>
Garrick Evans54861622019-07-19 09:05:09 +090023#include <base/strings/string_number_conversions.h>
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +090024#include <base/strings/string_util.h>
25#include <base/strings/stringprintf.h>
Garrick Evans4f9f5572019-11-26 10:25:16 +090026#include <brillo/userdb_utils.h>
Garrick Evans54861622019-07-19 09:05:09 +090027
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090028#include "patchpanel/adb_proxy.h"
Garrick Evans3388a032020-03-24 11:25:55 +090029#include "patchpanel/net_util.h"
30#include "patchpanel/scoped_ns.h"
Garrick Evansc7ae82c2019-09-04 16:25:10 +090031
Garrick Evans3388a032020-03-24 11:25:55 +090032namespace patchpanel {
Garrick Evans54861622019-07-19 09:05:09 +090033
Garrick Evansc7ae82c2019-09-04 16:25:10 +090034namespace {
Hugo Benichi76675592020-04-08 14:29:57 +090035// TODO(hugobenichi) Consolidate this constant definition in a single place.
36constexpr pid_t kTestPID = -2;
Garrick Evansc7ae82c2019-09-04 16:25:10 +090037constexpr char kDefaultIfname[] = "vmtap%d";
38constexpr char kTunDev[] = "/dev/net/tun";
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090039constexpr char kArcAddr[] = "100.115.92.2";
40constexpr char kLocalhostAddr[] = "127.0.0.1";
41constexpr uint16_t kAdbServerPort = 5555;
Hugo Benichie8758b52020-04-03 14:49:01 +090042
Hugo Benichibf811c62020-09-07 17:30:45 +090043// Constants used for dropping locally originated traffic bound to an incorrect
44// source IPv4 address.
45constexpr char kGuestIPv4Subnet[] = "100.115.92.0/23";
46constexpr std::array<const char*, 6> kPhysicalIfnamePrefixes{
47 {"eth+", "wlan+", "mlan+", "usb+", "wwan+", "rmnet+"}};
48
Hugo Benichi3a9162b2020-09-09 15:47:40 +090049constexpr char kApplyLocalSourceMarkChain[] = "apply_local_source_mark";
Hugo Benichi3ef370b2020-11-16 19:07:17 +090050constexpr char kApplyVpnMarkChain[] = "apply_vpn_mark";
51
Hugo Benichi2a940542020-10-26 18:50:49 +090052// Constant fwmark mask for matching local socket traffic that should be routed
53// through a VPN connection. The traffic must not be part of an existing
54// connection and must match exactly the VPN routing intent policy bit.
55const Fwmark kFwmarkVpnMatchingMask = kFwmarkRoutingMask | kFwmarkVpnMask;
56
Garrick Evans8a067562020-05-11 12:47:30 +090057std::string PrefixIfname(const std::string& prefix, const std::string& ifname) {
58 std::string n = prefix + ifname;
Garrick Evans2f581a02020-05-11 10:43:35 +090059 if (n.length() < IFNAMSIZ)
60 return n;
Garrick Evans54861622019-07-19 09:05:09 +090061
Garrick Evans2f581a02020-05-11 10:43:35 +090062 // Best effort attempt to preserve the interface number, assuming it's the
63 // last char in the name.
64 auto c = ifname[ifname.length() - 1];
65 n.resize(IFNAMSIZ - 1);
66 n[n.length() - 1] = c;
67 return n;
Garrick Evans54861622019-07-19 09:05:09 +090068}
Garrick Evansf0ab7132019-06-18 14:50:42 +090069
Garrick Evans8a067562020-05-11 12:47:30 +090070} // namespace
71
72std::string ArcVethHostName(const std::string& ifname) {
73 return PrefixIfname("veth", ifname);
74}
75
76std::string ArcBridgeName(const std::string& ifname) {
77 return PrefixIfname("arc_", ifname);
78}
79
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090080Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall)
81 : Datapath(process_runner, firewall, ioctl) {}
Garrick Evansc7ae82c2019-09-04 16:25:10 +090082
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090083Datapath::Datapath(MinijailedProcessRunner* process_runner,
84 Firewall* firewall,
85 ioctl_t ioctl_hook)
86 : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) {
Garrick Evansf0ab7132019-06-18 14:50:42 +090087 CHECK(process_runner_);
88}
89
Garrick Evans260ff302019-07-25 11:22:50 +090090MinijailedProcessRunner& Datapath::runner() const {
91 return *process_runner_;
92}
93
Hugo Benichibf811c62020-09-07 17:30:45 +090094void Datapath::Start() {
95 // Enable IPv4 packet forwarding
96 if (process_runner_->sysctl_w("net.ipv4.ip_forward", "1") != 0)
97 LOG(ERROR) << "Failed to update net.ipv4.ip_forward."
98 << " Guest connectivity will not work correctly.";
99
100 // Limit local port range: Android owns 47104-61000.
101 // TODO(garrick): The original history behind this tweak is gone. Some
102 // investigation is needed to see if it is still applicable.
103 if (process_runner_->sysctl_w("net.ipv4.ip_local_port_range",
104 "32768 47103") != 0)
105 LOG(ERROR) << "Failed to limit local port range. Some Android features or"
106 << " apps may not work correctly.";
107
108 // Enable IPv6 packet forwarding
109 if (process_runner_->sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0)
110 LOG(ERROR) << "Failed to update net.ipv6.conf.all.forwarding."
111 << " IPv6 functionality may be broken.";
112
113 if (!AddSNATMarkRules())
114 LOG(ERROR) << "Failed to install SNAT mark rules."
115 << " Guest connectivity may be broken.";
116
Hugo Benichi58125d32020-09-09 11:25:45 +0900117 // Create a FORWARD ACCEPT rule for connections already established.
118 if (process_runner_->iptables(
119 "filter", {"-A", "FORWARD", "-m", "state", "--state",
120 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) != 0)
Hugo Benichibf811c62020-09-07 17:30:45 +0900121 LOG(ERROR) << "Failed to install forwarding rule for established"
122 << " connections.";
123
124 // chromium:898210: Drop any locally originated traffic that would exit a
125 // physical interface with a source IPv4 address from the subnet of IPs used
126 // for VMs, containers, and connected namespaces This is needed to prevent
127 // packets leaking with an incorrect src IP when a local process binds to the
128 // wrong interface.
129 for (const auto& oif : kPhysicalIfnamePrefixes) {
130 if (!AddSourceIPv4DropRule(oif, kGuestIPv4Subnet))
131 LOG(WARNING) << "Failed to set up IPv4 drop rule for src ip "
132 << kGuestIPv4Subnet << " exiting " << oif;
133 }
134
135 if (!AddOutboundIPv4SNATMark("vmtap+"))
136 LOG(ERROR) << "Failed to set up NAT for TAP devices."
137 << " Guest connectivity may be broken.";
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900138
Hugo Benichi2a940542020-10-26 18:50:49 +0900139 // Applies the routing tag saved in conntrack for any established connection
140 // for sockets created in the host network namespace.
Hugo Benichi1af52392020-11-27 18:09:32 +0900141 if (!ModifyConnmarkRestore(IpFamily::Dual, "OUTPUT", "-A", "" /*iif*/,
142 kFwmarkRoutingMask))
Hugo Benichi2a940542020-10-26 18:50:49 +0900143 LOG(ERROR) << "Failed to add OUTPUT CONNMARK restore rule";
144
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900145 // Set up a mangle chain used in OUTPUT for applying the fwmark TrafficSource
146 // tag and tagging the local traffic that should be routed through a VPN.
147 if (!ModifyChain(IpFamily::Dual, "mangle", "-N", kApplyLocalSourceMarkChain))
148 LOG(ERROR) << "Failed to set up " << kApplyLocalSourceMarkChain
149 << " mangle chain";
150 // Ensure that the chain is empty if patchpanel is restarting after a crash.
151 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", kApplyLocalSourceMarkChain))
152 LOG(ERROR) << "Failed to flush " << kApplyLocalSourceMarkChain
153 << " mangle chain";
154 if (!ModifyIptables(IpFamily::Dual, "mangle",
155 {"-A", "OUTPUT", "-j", kApplyLocalSourceMarkChain, "-w"}))
156 LOG(ERROR) << "Failed to attach " << kApplyLocalSourceMarkChain
157 << " to mangle OUTPUT";
158 // Create rules for tagging local sources with the source tag and the vpn
159 // policy tag.
160 for (const auto& source : kLocalSourceTypes) {
Hugo Benichi620202f2020-11-27 10:14:38 +0900161 if (!ModifyFwmarkLocalSourceTag("-A", source))
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900162 LOG(ERROR) << "Failed to create fwmark tagging rule for uid " << source
163 << " in " << kApplyLocalSourceMarkChain;
164 }
165 // Finally add a catch-all rule for tagging any remaining local sources with
166 // the SYSTEM source tag
167 if (!ModifyFwmarkDefaultLocalSourceTag("-A", TrafficSource::SYSTEM))
168 LOG(ERROR) << "Failed to set up rule tagging traffic with default source";
169
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900170 // Sets up a mangle chain used in OUTPUT and PREROUTING for tagging "user"
171 // traffic that should be routed through a VPN.
172 if (!ModifyChain(IpFamily::Dual, "mangle", "-N", kApplyVpnMarkChain))
173 LOG(ERROR) << "Failed to set up " << kApplyVpnMarkChain << " mangle chain";
174 // Ensure that the chain is empty if patchpanel is restarting after a crash.
175 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", kApplyVpnMarkChain))
176 LOG(ERROR) << "Failed to flush " << kApplyVpnMarkChain << " mangle chain";
177 // All local outgoing traffic eligible to VPN routing should traverse the VPN
178 // marking chain.
179 if (!ModifyFwmarkVpnJumpRule("OUTPUT", "-A", "" /*iif*/, kFwmarkRouteOnVpn,
180 kFwmarkVpnMask))
181 LOG(ERROR) << "Failed to add jump rule to VPN chain in mangle OUTPUT chain";
182 // Any traffic that already has a routing tag applied is accepted.
183 if (!ModifyIptables(
184 IpFamily::Dual, "mangle",
185 {"-A", kApplyVpnMarkChain, "-m", "mark", "!", "--mark",
186 "0x0/" + kFwmarkRoutingMask.ToString(), "-j", "ACCEPT", "-w"}))
187 LOG(ERROR) << "Failed to add ACCEPT rule to VPN tagging chain for marked "
188 "connections";
Hugo 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
Hugo Benichi2a940542020-10-26 18:50:49 +0900226 // Stops applying routing tags saved in conntrack for sockets created in the
227 // host network namespace.
Hugo Benichi1af52392020-11-27 18:09:32 +0900228 if (!ModifyConnmarkRestore(IpFamily::Dual, "OUTPUT", "-D", "" /*iif*/,
229 kFwmarkRoutingMask))
Hugo Benichi2a940542020-10-26 18:50:49 +0900230 LOG(ERROR) << "Failed to remove OUTPUT CONNMARK restore rule";
231
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900232 // Delete the mangle chains
233 for (const auto* chain : {kApplyLocalSourceMarkChain, kApplyVpnMarkChain}) {
234 if (!ModifyChain(IpFamily::Dual, "mangle", "-F", chain))
235 LOG(ERROR) << "Failed to flush " << chain << " mangle chain";
236
237 if (!ModifyChain(IpFamily::Dual, "mangle", "-X", chain))
238 LOG(ERROR) << "Failed to delete " << chain << " mangle chain";
239 }
Hugo Benichibf811c62020-09-07 17:30:45 +0900240}
241
Hugo Benichi33860d72020-07-09 16:34:01 +0900242bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) {
243 // Try first to delete any netns with name |netns_name| in case patchpanel
244 // did not exit cleanly.
245 if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0)
246 LOG(INFO) << "Deleted left over network namespace name " << netns_name;
247 return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0;
248}
249
250bool Datapath::NetnsDeleteName(const std::string& netns_name) {
251 return process_runner_->ip_netns_delete(netns_name) == 0;
252}
253
Garrick Evans8a949dc2019-07-18 16:17:53 +0900254bool Datapath::AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900255 uint32_t ipv4_addr,
256 uint32_t ipv4_prefix_len) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900257 // Configure the persistent Chrome OS bridge interface with static IP.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900258 if (process_runner_->brctl("addbr", {ifname}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900259 return false;
260 }
261
Garrick Evans6f4fa3a2020-02-10 16:15:09 +0900262 if (process_runner_->ip(
263 "addr", "add",
264 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
265 IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
266 "dev", ifname}) != 0) {
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900267 RemoveBridge(ifname);
268 return false;
269 }
270
271 if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900272 RemoveBridge(ifname);
273 return false;
274 }
275
276 // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules.
Hugo Benichie8758b52020-04-03 14:49:01 +0900277 if (!AddOutboundIPv4SNATMark(ifname)) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900278 RemoveBridge(ifname);
279 return false;
280 }
281
282 return true;
283}
284
285void Datapath::RemoveBridge(const std::string& ifname) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900286 RemoveOutboundIPv4SNATMark(ifname);
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900287 process_runner_->ip("link", "set", {ifname, "down"});
Garrick Evans8e8e3472020-01-23 14:03:50 +0900288 process_runner_->brctl("delbr", {ifname});
Garrick Evans8a949dc2019-07-18 16:17:53 +0900289}
290
Garrick Evans621ed262019-11-13 12:28:43 +0900291bool Datapath::AddToBridge(const std::string& br_ifname,
292 const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900293 return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0);
Garrick Evans621ed262019-11-13 12:28:43 +0900294}
295
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900296std::string Datapath::AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900297 const MacAddress* mac_addr,
298 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900299 const std::string& user) {
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900300 base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK));
301 if (!dev.is_valid()) {
302 PLOG(ERROR) << "Failed to open " << kTunDev;
303 return "";
304 }
305
306 struct ifreq ifr;
307 memset(&ifr, 0, sizeof(ifr));
308 strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(),
309 sizeof(ifr.ifr_name));
310 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
311
312 // If a template was given as the name, ifr_name will be updated with the
313 // actual interface name.
314 if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900315 PLOG(ERROR) << "Failed to create tap interface " << name;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900316 return "";
317 }
318 const char* ifname = ifr.ifr_name;
319
320 if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900321 PLOG(ERROR) << "Failed to persist the interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900322 return "";
323 }
324
Garrick Evans4f9f5572019-11-26 10:25:16 +0900325 if (!user.empty()) {
326 uid_t uid = -1;
327 if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) {
328 PLOG(ERROR) << "Unable to look up UID for " << user;
329 RemoveTAP(ifname);
330 return "";
331 }
332 if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) {
333 PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface "
334 << ifname;
335 RemoveTAP(ifname);
336 return "";
337 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900338 }
339
Hugo Benichib9b93fe2019-10-25 23:36:01 +0900340 // Create control socket for configuring the interface.
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900341 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
342 if (!sock.is_valid()) {
343 PLOG(ERROR) << "Failed to create control socket for tap interface "
Garrick Evans621ed262019-11-13 12:28:43 +0900344 << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900345 RemoveTAP(ifname);
346 return "";
347 }
348
Garrick Evans621ed262019-11-13 12:28:43 +0900349 if (ipv4_addr) {
350 struct sockaddr_in* addr =
351 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr);
352 addr->sin_family = AF_INET;
353 addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address());
354 if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) {
355 PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname
356 << " {" << ipv4_addr->ToCidrString() << "}";
357 RemoveTAP(ifname);
358 return "";
359 }
360
361 struct sockaddr_in* netmask =
362 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask);
363 netmask->sin_family = AF_INET;
364 netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask());
365 if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) {
366 PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname
367 << " {" << ipv4_addr->ToCidrString() << "}";
368 RemoveTAP(ifname);
369 return "";
370 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900371 }
372
Garrick Evans621ed262019-11-13 12:28:43 +0900373 if (mac_addr) {
374 struct sockaddr* hwaddr = &ifr.ifr_hwaddr;
375 hwaddr->sa_family = ARPHRD_ETHER;
376 memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr));
377 if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) {
378 PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname
379 << " {" << MacAddressToString(*mac_addr) << "}";
380 RemoveTAP(ifname);
381 return "";
382 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900383 }
384
385 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900386 PLOG(ERROR) << "Failed to get flags for tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900387 RemoveTAP(ifname);
388 return "";
389 }
390
391 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
392 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900393 PLOG(ERROR) << "Failed to enable tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900394 RemoveTAP(ifname);
395 return "";
396 }
397
398 return ifname;
399}
400
401void Datapath::RemoveTAP(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900402 process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"});
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900403}
404
Hugo Benichi33860d72020-07-09 16:34:01 +0900405bool Datapath::ConnectVethPair(pid_t netns_pid,
406 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900407 const std::string& veth_ifname,
408 const std::string& peer_ifname,
409 const MacAddress& remote_mac_addr,
410 uint32_t remote_ipv4_addr,
411 uint32_t remote_ipv4_prefix_len,
412 bool remote_multicast_flag) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900413 // Set up the virtual pair across the current namespace and |netns_name|.
414 if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) {
415 LOG(ERROR) << "Failed to create veth pair " << veth_ifname << ","
416 << peer_ifname;
417 return false;
418 }
419
420 // Configure the remote veth in namespace |netns_name|.
Hugo Benichi76675592020-04-08 14:29:57 +0900421 {
Hugo Benichi33860d72020-07-09 16:34:01 +0900422 ScopedNS ns(netns_pid);
423 if (!ns.IsValid() && netns_pid != kTestPID) {
Hugo Benichi76675592020-04-08 14:29:57 +0900424 LOG(ERROR)
425 << "Cannot create virtual link -- invalid container namespace?";
426 return false;
427 }
428
Hugo Benichi76675592020-04-08 14:29:57 +0900429 if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr,
430 remote_ipv4_prefix_len, true /* link up */,
431 remote_multicast_flag)) {
432 LOG(ERROR) << "Failed to configure interface " << peer_ifname;
433 RemoveInterface(peer_ifname);
434 return false;
435 }
436 }
437
Hugo Benichi76675592020-04-08 14:29:57 +0900438 if (!ToggleInterface(veth_ifname, true /*up*/)) {
439 LOG(ERROR) << "Failed to bring up interface " << veth_ifname;
440 RemoveInterface(veth_ifname);
441 return false;
442 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900443
Hugo Benichi76675592020-04-08 14:29:57 +0900444 return true;
445}
446
Hugo Benichi33860d72020-07-09 16:34:01 +0900447bool Datapath::AddVirtualInterfacePair(const std::string& netns_name,
448 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +0900449 const std::string& peer_ifname) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900450 return process_runner_->ip("link", "add",
451 {veth_ifname, "type", "veth", "peer", "name",
452 peer_ifname, "netns", netns_name}) == 0;
Garrick Evans2470caa2020-03-04 14:15:41 +0900453}
Garrick Evans54861622019-07-19 09:05:09 +0900454
Garrick Evans2470caa2020-03-04 14:15:41 +0900455bool Datapath::ToggleInterface(const std::string& ifname, bool up) {
456 const std::string link = up ? "up" : "down";
457 return process_runner_->ip("link", "set", {ifname, link}) == 0;
458}
Garrick Evans54861622019-07-19 09:05:09 +0900459
Garrick Evans2470caa2020-03-04 14:15:41 +0900460bool Datapath::ConfigureInterface(const std::string& ifname,
461 const MacAddress& mac_addr,
462 uint32_t ipv4_addr,
463 uint32_t ipv4_prefix_len,
464 bool up,
465 bool enable_multicast) {
466 const std::string link = up ? "up" : "down";
467 const std::string multicast = enable_multicast ? "on" : "off";
468 return (process_runner_->ip(
469 "addr", "add",
470 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
471 IPv4AddressToString(
472 Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
473 "dev", ifname}) == 0) &&
474 (process_runner_->ip("link", "set",
475 {
476 "dev",
477 ifname,
478 link,
479 "addr",
480 MacAddressToString(mac_addr),
481 "multicast",
482 multicast,
483 }) == 0);
Garrick Evans54861622019-07-19 09:05:09 +0900484}
485
486void Datapath::RemoveInterface(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900487 process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/);
Garrick Evans54861622019-07-19 09:05:09 +0900488}
489
Hugo Benichi321f23b2020-09-25 15:42:05 +0900490bool Datapath::AddSourceIPv4DropRule(const std::string& oif,
491 const std::string& src_ip) {
492 return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s",
493 src_ip, "-j", "DROP", "-w"}) == 0;
494}
495
496bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif,
497 const std::string& src_ip) {
498 return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s",
499 src_ip, "-j", "DROP", "-w"}) == 0;
500}
501
Hugo Benichi7c342672020-09-08 09:18:14 +0900502bool Datapath::StartRoutingNamespace(pid_t pid,
503 const std::string& netns_name,
504 const std::string& host_ifname,
505 const std::string& peer_ifname,
506 uint32_t subnet_ipv4_addr,
507 uint32_t subnet_prefixlen,
508 uint32_t host_ipv4_addr,
509 uint32_t peer_ipv4_addr,
510 const MacAddress& peer_mac_addr) {
511 // Veth interface configuration and client routing configuration:
512 // - attach a name to the client namespace.
513 // - create veth pair across the current namespace and the client namespace.
514 // - configure IPv4 address on remote veth inside client namespace.
515 // - configure IPv4 address on local veth inside host namespace.
516 // - add a default IPv4 /0 route sending traffic to that remote veth.
517 if (!NetnsAttachName(netns_name, pid)) {
518 LOG(ERROR) << "Failed to attach name " << netns_name << " to namespace pid "
519 << pid;
520 return false;
521 }
522
523 if (!ConnectVethPair(pid, netns_name, host_ifname, peer_ifname, peer_mac_addr,
524 peer_ipv4_addr, subnet_prefixlen,
525 false /* enable_multicast */)) {
526 LOG(ERROR) << "Failed to create veth pair for"
527 " namespace pid "
528 << pid;
529 NetnsDeleteName(netns_name);
530 return false;
531 }
532
533 if (!ConfigureInterface(host_ifname, peer_mac_addr, host_ipv4_addr,
534 subnet_prefixlen, true /* link up */,
535 false /* enable_multicast */)) {
536 LOG(ERROR) << "Cannot configure host interface " << host_ifname;
537 RemoveInterface(host_ifname);
538 NetnsDeleteName(netns_name);
539 return false;
540 }
541
542 {
543 ScopedNS ns(pid);
544 if (!ns.IsValid() && pid != kTestPID) {
545 LOG(ERROR) << "Invalid namespace pid " << pid;
546 RemoveInterface(host_ifname);
547 NetnsDeleteName(netns_name);
548 return false;
549 }
550
551 if (!AddIPv4Route(host_ipv4_addr, INADDR_ANY, INADDR_ANY)) {
552 LOG(ERROR) << "Failed to add default /0 route to " << host_ifname
553 << " inside namespace pid " << pid;
554 RemoveInterface(host_ifname);
555 NetnsDeleteName(netns_name);
556 return false;
557 }
558 }
559
560 // Host namespace routing configuration
561 // - ingress: add route to client subnet via |host_ifname|.
562 // - egress: - allow forwarding for traffic outgoing |host_ifname|.
563 // - add SNAT mark 0x1/0x1 for traffic outgoing |host_ifname|.
564 // Note that by default unsolicited ingress traffic is not forwarded to the
565 // client namespace unless the client specifically set port forwarding
566 // through permission_broker DBus APIs.
567 // TODO(hugobenichi) If allow_user_traffic is false, then prevent forwarding
568 // both ways between client namespace and other guest containers and VMs.
569 // TODO(b/161507671) If outbound_physical_device is defined, then set strong
570 // routing to that interface routing table.
571 uint32_t netmask = Ipv4Netmask(subnet_prefixlen);
572 if (!AddIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask)) {
573 LOG(ERROR) << "Failed to set route to client namespace";
574 RemoveInterface(host_ifname);
575 NetnsDeleteName(netns_name);
576 return false;
577 }
578
Hugo Benichi58125d32020-09-09 11:25:45 +0900579 if (!StartIpForwarding(IpFamily::IPv4, "", host_ifname)) {
580 LOG(ERROR) << "Failed to allow FORWARD for ingress traffic into "
Hugo Benichi7c342672020-09-08 09:18:14 +0900581 << host_ifname;
582 RemoveInterface(host_ifname);
583 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask);
584 NetnsDeleteName(netns_name);
585 return false;
586 }
587
588 // TODO(b/161508179) Add fwmark source tagging based on client usage.
589 // TODO(b/161508179) Do not rely on legacy fwmark 1 for SNAT.
590 if (!AddOutboundIPv4SNATMark(host_ifname)) {
591 LOG(ERROR) << "Failed to set SNAT for traffic"
592 " outgoing from "
593 << host_ifname;
594 RemoveInterface(host_ifname);
595 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr, netmask);
Hugo Benichi58125d32020-09-09 11:25:45 +0900596 StopIpForwarding(IpFamily::IPv4, "", host_ifname);
Hugo Benichi7c342672020-09-08 09:18:14 +0900597 NetnsDeleteName(netns_name);
598 return false;
599 }
600
601 return true;
602}
603
604void Datapath::StopRoutingNamespace(const std::string& netns_name,
605 const std::string& host_ifname,
606 uint32_t subnet_ipv4_addr,
607 uint32_t subnet_prefixlen,
608 uint32_t host_ipv4_addr) {
609 RemoveInterface(host_ifname);
Hugo Benichi58125d32020-09-09 11:25:45 +0900610 StopIpForwarding(IpFamily::IPv4, "", host_ifname);
Hugo Benichi7c342672020-09-08 09:18:14 +0900611 RemoveOutboundIPv4SNATMark(host_ifname);
612 DeleteIPv4Route(host_ipv4_addr, subnet_ipv4_addr,
613 Ipv4Netmask(subnet_prefixlen));
614 NetnsDeleteName(netns_name);
615}
616
Hugo Benichi8d622b52020-08-13 15:24:12 +0900617void Datapath::StartRoutingDevice(const std::string& ext_ifname,
618 const std::string& int_ifname,
619 uint32_t int_ipv4_addr,
620 TrafficSource source) {
621 if (!ext_ifname.empty() &&
622 !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)))
623 LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname
624 << "->" << int_ifname;
625
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900626 if (!StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900627 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->"
628 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900629
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900630 if (!StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900631 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-"
632 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900633
Hugo Benichi2a940542020-10-26 18:50:49 +0900634 if (!ModifyFwmarkSourceTag("-A", int_ifname, source))
635 LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source "
636 << source << " for " << int_ifname;
637
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900638 if (!ext_ifname.empty()) {
639 // If |ext_ifname| is not null, mark egress traffic with the
640 // fwmark routing tag corresponding to |ext_ifname|.
Hugo Benichi2a940542020-10-26 18:50:49 +0900641 if (!ModifyFwmarkRoutingTag("PREROUTING", "-A", ext_ifname, int_ifname))
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900642 LOG(ERROR) << "Failed to add PREROUTING fwmark routing tag for "
643 << ext_ifname << "<-" << int_ifname;
644 } else {
645 // Otherwise if ext_ifname is null, set up a CONNMARK restore rule in
646 // PREROUTING to apply any fwmark routing tag saved for the current
647 // connection, and rely on implicit routing to the default logical network
648 // otherwise.
Hugo Benichi1af52392020-11-27 18:09:32 +0900649 if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", int_ifname,
650 kFwmarkRoutingMask))
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900651 LOG(ERROR) << "Failed to add PREROUTING CONNMARK restore rule for "
652 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900653
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900654 // Forwarded traffic from downstream virtual devices routed to the system
655 // logical default network is always eligible to be routed through a VPN.
656 if (!ModifyFwmarkVpnJumpRule("PREROUTING", "-A", int_ifname, {}, {}))
657 LOG(ERROR) << "Failed to add jump rule to VPN chain for " << int_ifname;
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900658 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900659}
660
661void Datapath::StopRoutingDevice(const std::string& ext_ifname,
662 const std::string& int_ifname,
663 uint32_t int_ipv4_addr,
664 TrafficSource source) {
665 if (!ext_ifname.empty())
666 RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr));
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900667 StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname);
668 StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900669 ModifyFwmarkSourceTag("-D", int_ifname, source);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900670 if (!ext_ifname.empty()) {
Hugo Benichi2a940542020-10-26 18:50:49 +0900671 ModifyFwmarkRoutingTag("PREROUTING", "-D", ext_ifname, int_ifname);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900672 } else {
Hugo Benichi1af52392020-11-27 18:09:32 +0900673 ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", int_ifname,
674 kFwmarkRoutingMask);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900675 ModifyFwmarkVpnJumpRule("PREROUTING", "-D", int_ifname, {}, {});
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900676 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900677}
678
Garrick Evansf0ab7132019-06-18 14:50:42 +0900679bool Datapath::AddInboundIPv4DNAT(const std::string& ifname,
680 const std::string& ipv4_addr) {
681 // Direct ingress IP traffic to existing sockets.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900682 if (process_runner_->iptables(
683 "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket",
684 "--nowildcard", "-j", "ACCEPT", "-w"}) != 0)
Garrick Evansf0ab7132019-06-18 14:50:42 +0900685 return false;
686
687 // Direct ingress TCP & UDP traffic to ARC interface for new connections.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900688 if (process_runner_->iptables(
689 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
690 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900691 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
692 return false;
693 }
Garrick Evans8e8e3472020-01-23 14:03:50 +0900694 if (process_runner_->iptables(
695 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
696 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900697 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
698 return false;
699 }
700
701 return true;
702}
703
704void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname,
705 const std::string& ipv4_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900706 process_runner_->iptables(
707 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
708 "--to-destination", ipv4_addr, "-w"});
709 process_runner_->iptables(
710 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
711 "--to-destination", ipv4_addr, "-w"});
712 process_runner_->iptables(
713 "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard",
714 "-j", "ACCEPT", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900715}
716
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900717// TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded
718// egress traffic is routed through the fwmark routing tag.
Garrick Evansd291af62020-05-25 10:39:06 +0900719bool Datapath::AddSNATMarkRules() {
Taoyu Li79871c92020-07-02 16:09:39 +0900720 // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore
721 // need to be explicitly dropped.
722 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900723 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m",
Taoyu Li79871c92020-07-02 16:09:39 +0900724 "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) {
725 return false;
726 }
Garrick Evansd291af62020-05-25 10:39:06 +0900727 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900728 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900729 "ACCEPT", "-w"}) != 0) {
730 return false;
731 }
732 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900733 "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900734 "MASQUERADE", "-w"}) != 0) {
735 RemoveSNATMarkRules();
736 return false;
737 }
738 return true;
739}
740
741void Datapath::RemoveSNATMarkRules() {
742 process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900743 "1/1", "-j", "MASQUERADE", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900744 process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900745 "1/1", "-j", "ACCEPT", "-w"});
Taoyu Li79871c92020-07-02 16:09:39 +0900746 process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900747 "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state",
Taoyu Li79871c92020-07-02 16:09:39 +0900748 "--state", "INVALID", "-j", "DROP", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900749}
750
Hugo Benichie8758b52020-04-03 14:49:01 +0900751bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) {
752 return process_runner_->iptables(
753 "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK",
Hugo Benichi6c445322020-08-12 16:46:19 +0900754 "--set-mark", "1/1", "-w"}) == 0;
Hugo Benichie8758b52020-04-03 14:49:01 +0900755}
756
757void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) {
758 process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j",
Hugo Benichi6c445322020-08-12 16:46:19 +0900759 "MARK", "--set-mark", "1/1", "-w"});
Hugo Benichie8758b52020-04-03 14:49:01 +0900760}
761
Garrick Evans664a82f2019-12-17 12:18:05 +0900762bool Datapath::MaskInterfaceFlags(const std::string& ifname,
763 uint16_t on,
764 uint16_t off) {
Taoyu Li90c13912019-11-26 17:56:54 +0900765 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
766 if (!sock.is_valid()) {
767 PLOG(ERROR) << "Failed to create control socket";
768 return false;
769 }
770 ifreq ifr;
771 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str());
772 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) {
773 PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname;
774 return false;
775 }
Garrick Evans664a82f2019-12-17 12:18:05 +0900776 ifr.ifr_flags |= on;
777 ifr.ifr_flags &= ~off;
Taoyu Li90c13912019-11-26 17:56:54 +0900778 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900779 PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on
780 << " unset flag 0x" << std::hex << off << " on " << ifname;
Taoyu Li90c13912019-11-26 17:56:54 +0900781 return false;
782 }
783 return true;
784}
785
Garrick Evans260ff302019-07-25 11:22:50 +0900786bool Datapath::AddIPv6HostRoute(const std::string& ifname,
787 const std::string& ipv6_addr,
788 int ipv6_prefix_len) {
789 std::string ipv6_addr_cidr =
790 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
791
Garrick Evans8e8e3472020-01-23 14:03:50 +0900792 return process_runner_->ip6("route", "replace",
793 {ipv6_addr_cidr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900794}
795
796void Datapath::RemoveIPv6HostRoute(const std::string& ifname,
797 const std::string& ipv6_addr,
798 int ipv6_prefix_len) {
799 std::string ipv6_addr_cidr =
800 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
801
Garrick Evans8e8e3472020-01-23 14:03:50 +0900802 process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900803}
804
Taoyu Lia0727dc2020-09-24 19:54:59 +0900805bool Datapath::AddIPv6Address(const std::string& ifname,
806 const std::string& ipv6_addr) {
807 return process_runner_->ip6("addr", "add", {ipv6_addr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900808}
809
Taoyu Lia0727dc2020-09-24 19:54:59 +0900810void Datapath::RemoveIPv6Address(const std::string& ifname,
811 const std::string& ipv6_addr) {
812 process_runner_->ip6("addr", "del", {ipv6_addr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900813}
814
Hugo Benichi76be34a2020-08-26 22:35:54 +0900815void Datapath::StartConnectionPinning(const std::string& ext_ifname) {
Hugo Benichi1af52392020-11-27 18:09:32 +0900816 // Set in CONNMARK the routing tag associated with |ext_ifname|.
Hugo Benichi76be34a2020-08-26 22:35:54 +0900817 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-A", ext_ifname))
818 LOG(ERROR) << "Could not start connection pinning on " << ext_ifname;
Hugo Benichi1af52392020-11-27 18:09:32 +0900819 // Save in CONNMARK the source tag for egress traffic of this connection.
820 if (!ModifyConnmarkSave(IpFamily::Dual, "POSTROUTING", "-A", ext_ifname,
821 kFwmarkAllSourcesMask))
822 LOG(ERROR) << "Failed to add POSTROUTING CONNMARK rule for saving fwmark "
823 "source tag on "
824 << ext_ifname;
825 // Restore from CONNMARK the source tag for ingress traffic of this connection
826 // (returned traffic).
827 if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", ext_ifname,
828 kFwmarkAllSourcesMask))
829 LOG(ERROR) << "Could not setup fwmark source tagging rule for return "
830 "traffic received on "
831 << ext_ifname;
Hugo Benichi76be34a2020-08-26 22:35:54 +0900832}
833
834void Datapath::StopConnectionPinning(const std::string& ext_ifname) {
835 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-D", ext_ifname))
836 LOG(ERROR) << "Could not stop connection pinning on " << ext_ifname;
Hugo Benichi1af52392020-11-27 18:09:32 +0900837 if (!ModifyConnmarkSave(IpFamily::Dual, "POSTROUTING", "-D", ext_ifname,
838 kFwmarkAllSourcesMask))
839 LOG(ERROR) << "Could not remove POSTROUTING CONNMARK rule for saving "
840 "fwmark source tag on "
841 << ext_ifname;
842 if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", ext_ifname,
843 kFwmarkAllSourcesMask))
844 LOG(ERROR) << "Could not remove fwmark source tagging rule for return "
845 "traffic received on "
846 << ext_ifname;
Hugo Benichi76be34a2020-08-26 22:35:54 +0900847}
848
Hugo Benichi2a940542020-10-26 18:50:49 +0900849void Datapath::StartVpnRouting(const std::string& vpn_ifname) {
850 StartConnectionPinning(vpn_ifname);
851 if (!ModifyFwmarkRoutingTag(kApplyVpnMarkChain, "-A", vpn_ifname, ""))
852 LOG(ERROR) << "Failed to set up VPN set-mark rule for " << vpn_ifname;
853}
854
855void Datapath::StopVpnRouting(const std::string& vpn_ifname) {
856 if (!ModifyFwmarkRoutingTag(kApplyVpnMarkChain, "-D", vpn_ifname, ""))
857 LOG(ERROR) << "Failed to remove VPN set-mark rule for " << vpn_ifname;
858 StopConnectionPinning(vpn_ifname);
859}
860
Hugo Benichi76be34a2020-08-26 22:35:54 +0900861bool Datapath::ModifyConnmarkSetPostrouting(IpFamily family,
862 const std::string& op,
863 const std::string& oif) {
Hugo Benichi76be34a2020-08-26 22:35:54 +0900864 int ifindex = FindIfIndex(oif);
865 if (ifindex == 0) {
866 PLOG(ERROR) << "if_nametoindex(" << oif << ") failed";
867 return false;
868 }
869
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900870 return ModifyConnmarkSet(family, "POSTROUTING", op, oif,
871 Fwmark::FromIfIndex(ifindex), kFwmarkRoutingMask);
872}
873
874bool Datapath::ModifyConnmarkSet(IpFamily family,
875 const std::string& chain,
876 const std::string& op,
877 const std::string& oif,
878 Fwmark mark,
879 Fwmark mask) {
880 if (chain != kApplyVpnMarkChain && (chain != "POSTROUTING" || oif.empty())) {
881 LOG(ERROR) << "Invalid arguments chain=" << chain << " oif=" << oif;
882 return false;
883 }
884
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900885 std::vector<std::string> args = {op, chain};
886 if (!oif.empty()) {
887 args.push_back("-o");
888 args.push_back(oif);
889 }
890 args.push_back("-j");
891 args.push_back("CONNMARK");
892 args.push_back("--set-mark");
893 args.push_back(mark.ToString() + "/" + mask.ToString());
894 args.push_back("-w");
Hugo Benichi76be34a2020-08-26 22:35:54 +0900895
Hugo Benichi58f264a2020-10-16 18:16:05 +0900896 return ModifyIptables(family, "mangle", args);
Hugo Benichi76be34a2020-08-26 22:35:54 +0900897}
898
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900899bool Datapath::ModifyConnmarkRestore(IpFamily family,
900 const std::string& chain,
901 const std::string& op,
Hugo Benichi1af52392020-11-27 18:09:32 +0900902 const std::string& iif,
903 Fwmark mask) {
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900904 if (chain != "OUTPUT" && (chain != "PREROUTING" || iif.empty())) {
905 LOG(ERROR) << "Invalid arguments chain=" << chain << " iif=" << iif;
906 return false;
907 }
908
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900909 std::vector<std::string> args = {op, chain};
910 if (!iif.empty()) {
911 args.push_back("-i");
912 args.push_back(iif);
913 }
914 args.insert(args.end(), {"-j", "CONNMARK", "--restore-mark", "--mask",
Hugo Benichi1af52392020-11-27 18:09:32 +0900915 mask.ToString(), "-w"});
916 return ModifyIptables(family, "mangle", args);
917}
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900918
Hugo Benichi1af52392020-11-27 18:09:32 +0900919bool Datapath::ModifyConnmarkSave(IpFamily family,
920 const std::string& chain,
921 const std::string& op,
922 const std::string& oif,
923 Fwmark mask) {
924 std::vector<std::string> args = {op, chain};
925 if (!oif.empty()) {
926 args.push_back("-o");
927 args.push_back(oif);
928 }
929 args.insert(args.end(), {"-j", "CONNMARK", "--save-mark", "--mask",
930 mask.ToString(), "-w"});
Hugo Benichi58f264a2020-10-16 18:16:05 +0900931 return ModifyIptables(family, "mangle", args);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900932}
933
Hugo Benichi2a940542020-10-26 18:50:49 +0900934bool Datapath::ModifyFwmarkRoutingTag(const std::string& chain,
935 const std::string& op,
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900936 const std::string& ext_ifname,
937 const std::string& int_ifname) {
938 int ifindex = FindIfIndex(ext_ifname);
939 if (ifindex == 0) {
940 PLOG(ERROR) << "if_nametoindex(" << ext_ifname << ") failed";
941 return false;
942 }
943
Hugo Benichi2a940542020-10-26 18:50:49 +0900944 return ModifyFwmark(IpFamily::Dual, chain, op, int_ifname, "" /*uid_name*/,
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900945 0 /*classid*/, Fwmark::FromIfIndex(ifindex),
946 kFwmarkRoutingMask);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900947}
948
Hugo Benichi9be19b12020-08-14 15:33:40 +0900949bool Datapath::ModifyFwmarkSourceTag(const std::string& op,
950 const std::string& iif,
951 TrafficSource source) {
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900952 return ModifyFwmark(IpFamily::Dual, "PREROUTING", op, iif, "" /*uid_name*/,
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900953 0 /*classid*/, Fwmark::FromSource(source),
954 kFwmarkAllSourcesMask);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900955}
956
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900957bool Datapath::ModifyFwmarkDefaultLocalSourceTag(const std::string& op,
958 TrafficSource source) {
959 std::vector<std::string> args = {"-A",
960 kApplyLocalSourceMarkChain,
961 "-m",
962 "mark",
963 "--mark",
964 "0x0/" + kFwmarkAllSourcesMask.ToString(),
965 "-j",
966 "MARK",
967 "--set-mark",
968 Fwmark::FromSource(source).ToString() + "/" +
969 kFwmarkAllSourcesMask.ToString(),
970 "-w"};
971 return ModifyIptables(IpFamily::Dual, "mangle", args);
972}
Hugo Benichi9be19b12020-08-14 15:33:40 +0900973
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900974bool Datapath::ModifyFwmarkLocalSourceTag(const std::string& op,
975 const LocalSourceSpecs& source) {
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900976 if (std::string(source.uid_name).empty() && source.classid == 0)
977 return false;
978
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900979 Fwmark mark = Fwmark::FromSource(source.source_type);
980 if (source.is_on_vpn)
981 mark = mark | kFwmarkRouteOnVpn;
982
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900983 return ModifyFwmark(IpFamily::Dual, kApplyLocalSourceMarkChain, op,
984 "" /*iif*/, source.uid_name, source.classid, mark,
985 kFwmarkPolicyMask);
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900986}
987
988bool Datapath::ModifyFwmark(IpFamily family,
989 const std::string& chain,
990 const std::string& op,
991 const std::string& iif,
992 const std::string& uid_name,
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900993 uint32_t classid,
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900994 Fwmark mark,
995 Fwmark mask,
996 bool log_failures) {
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900997 std::vector<std::string> args = {op, chain};
998 if (!iif.empty()) {
999 args.push_back("-i");
1000 args.push_back(iif);
1001 }
1002 if (!uid_name.empty()) {
1003 args.push_back("-m");
1004 args.push_back("owner");
1005 args.push_back("--uid-owner");
1006 args.push_back(uid_name);
1007 }
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +09001008 if (classid != 0) {
1009 args.push_back("-m");
1010 args.push_back("cgroup");
1011 args.push_back("--cgroup");
1012 args.push_back(base::StringPrintf("0x%08x", classid));
1013 }
Hugo Benichi3a9162b2020-09-09 15:47:40 +09001014 args.push_back("-j");
1015 args.push_back("MARK");
1016 args.push_back("--set-mark");
1017 args.push_back(mark.ToString() + "/" + mask.ToString());
1018 args.push_back("-w");
Hugo Benichi9be19b12020-08-14 15:33:40 +09001019
Hugo Benichi58f264a2020-10-16 18:16:05 +09001020 return ModifyIptables(family, "mangle", args, log_failures);
Hugo Benichi9be19b12020-08-14 15:33:40 +09001021}
1022
Hugo Benichid82d8832020-08-14 10:05:03 +09001023bool Datapath::ModifyIpForwarding(IpFamily family,
1024 const std::string& op,
1025 const std::string& iif,
1026 const std::string& oif,
1027 bool log_failures) {
1028 if (iif.empty() && oif.empty()) {
1029 LOG(ERROR) << "Cannot change IP forwarding with no input or output "
1030 "interface specified";
Garrick Evans260ff302019-07-25 11:22:50 +09001031 return false;
1032 }
1033
Hugo Benichid82d8832020-08-14 10:05:03 +09001034 std::vector<std::string> args = {op, "FORWARD"};
1035 if (!iif.empty()) {
1036 args.push_back("-i");
1037 args.push_back(iif);
1038 }
1039 if (!oif.empty()) {
1040 args.push_back("-o");
1041 args.push_back(oif);
1042 }
1043 args.push_back("-j");
1044 args.push_back("ACCEPT");
1045 args.push_back("-w");
1046
Hugo Benichi58f264a2020-10-16 18:16:05 +09001047 return ModifyIptables(family, "filter", args, log_failures);
Hugo Benichid82d8832020-08-14 10:05:03 +09001048}
1049
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001050bool Datapath::ModifyFwmarkVpnJumpRule(const std::string& chain,
1051 const std::string& op,
1052 const std::string& iif,
1053 Fwmark mark,
1054 Fwmark mask) {
1055 std::vector<std::string> args = {op, chain};
1056 if (!iif.empty()) {
1057 args.push_back("-i");
1058 args.push_back(iif);
1059 }
1060 if (mark.Value() != 0 && mask.Value() != 0) {
1061 args.push_back("-m");
1062 args.push_back("mark");
1063 args.push_back("--mark");
1064 args.push_back(mark.ToString() + "/" + mask.ToString());
1065 }
1066 args.insert(args.end(), {"-j", kApplyVpnMarkChain, "-w"});
1067 return ModifyIptables(IpFamily::Dual, "mangle", args);
1068}
1069
1070bool Datapath::ModifyChain(IpFamily family,
1071 const std::string& table,
1072 const std::string& op,
Hugo Benichi58f264a2020-10-16 18:16:05 +09001073 const std::string& chain,
1074 bool log_failures) {
1075 return ModifyIptables(family, table, {op, chain, "-w"}, log_failures);
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001076}
1077
1078bool Datapath::ModifyIptables(IpFamily family,
1079 const std::string& table,
Hugo Benichi58f264a2020-10-16 18:16:05 +09001080 const std::vector<std::string>& argv,
1081 bool log_failures) {
1082 switch (family) {
1083 case IPv4:
1084 case IPv6:
1085 case Dual:
1086 break;
1087 default:
1088 LOG(ERROR) << "Could not execute iptables command " << table
1089 << base::JoinString(argv, " ") << ": incorrect IP family "
1090 << family;
1091 return false;
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001092 }
1093
1094 bool success = true;
1095 if (family & IpFamily::IPv4)
Hugo Benichi58f264a2020-10-16 18:16:05 +09001096 success &= process_runner_->iptables(table, argv, log_failures) == 0;
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001097 if (family & IpFamily::IPv6)
Hugo Benichi58f264a2020-10-16 18:16:05 +09001098 success &= process_runner_->ip6tables(table, argv, log_failures) == 0;
Hugo Benichi3ef370b2020-11-16 19:07:17 +09001099 return success;
1100}
1101
Hugo Benichid82d8832020-08-14 10:05:03 +09001102bool Datapath::StartIpForwarding(IpFamily family,
1103 const std::string& iif,
1104 const std::string& oif) {
1105 return ModifyIpForwarding(family, "-A", iif, oif);
1106}
1107
1108bool Datapath::StopIpForwarding(IpFamily family,
1109 const std::string& iif,
1110 const std::string& oif) {
1111 return ModifyIpForwarding(family, "-D", iif, oif);
1112}
1113
1114bool Datapath::AddIPv6Forwarding(const std::string& ifname1,
1115 const std::string& ifname2) {
1116 // Only start Ipv6 forwarding if -C returns false and it had not been
1117 // started yet.
1118 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2,
1119 false /*log_failures*/) &&
1120 !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) {
1121 return false;
1122 }
1123
1124 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1,
1125 false /*log_failures*/) &&
1126 !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) {
Garrick Evans260ff302019-07-25 11:22:50 +09001127 RemoveIPv6Forwarding(ifname1, ifname2);
1128 return false;
1129 }
1130
1131 return true;
1132}
1133
1134void Datapath::RemoveIPv6Forwarding(const std::string& ifname1,
1135 const std::string& ifname2) {
Hugo Benichid82d8832020-08-14 10:05:03 +09001136 StopIpForwarding(IpFamily::IPv6, ifname1, ifname2);
1137 StopIpForwarding(IpFamily::IPv6, ifname2, ifname1);
Garrick Evans260ff302019-07-25 11:22:50 +09001138}
1139
Garrick Evans3d97a392020-02-21 15:24:37 +09001140bool Datapath::AddIPv4Route(uint32_t gateway_addr,
1141 uint32_t addr,
1142 uint32_t netmask) {
1143 struct rtentry route;
1144 memset(&route, 0, sizeof(route));
Hugo Benichie8758b52020-04-03 14:49:01 +09001145 SetSockaddrIn(&route.rt_gateway, gateway_addr);
1146 SetSockaddrIn(&route.rt_dst, addr & netmask);
1147 SetSockaddrIn(&route.rt_genmask, netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +09001148 route.rt_flags = RTF_UP | RTF_GATEWAY;
Hugo Benichie8758b52020-04-03 14:49:01 +09001149 return ModifyRtentry(SIOCADDRT, &route);
1150}
Garrick Evans3d97a392020-02-21 15:24:37 +09001151
Hugo Benichie8758b52020-04-03 14:49:01 +09001152bool Datapath::DeleteIPv4Route(uint32_t gateway_addr,
1153 uint32_t addr,
1154 uint32_t netmask) {
1155 struct rtentry route;
1156 memset(&route, 0, sizeof(route));
1157 SetSockaddrIn(&route.rt_gateway, gateway_addr);
1158 SetSockaddrIn(&route.rt_dst, addr & netmask);
1159 SetSockaddrIn(&route.rt_genmask, netmask);
1160 route.rt_flags = RTF_UP | RTF_GATEWAY;
1161 return ModifyRtentry(SIOCDELRT, &route);
1162}
1163
1164bool Datapath::AddIPv4Route(const std::string& ifname,
1165 uint32_t addr,
1166 uint32_t netmask) {
1167 struct rtentry route;
1168 memset(&route, 0, sizeof(route));
1169 SetSockaddrIn(&route.rt_dst, addr & netmask);
1170 SetSockaddrIn(&route.rt_genmask, netmask);
1171 char rt_dev[IFNAMSIZ];
1172 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
1173 rt_dev[IFNAMSIZ - 1] = '\0';
1174 route.rt_dev = rt_dev;
1175 route.rt_flags = RTF_UP | RTF_GATEWAY;
1176 return ModifyRtentry(SIOCADDRT, &route);
1177}
1178
1179bool Datapath::DeleteIPv4Route(const std::string& ifname,
1180 uint32_t addr,
1181 uint32_t netmask) {
1182 struct rtentry route;
1183 memset(&route, 0, sizeof(route));
1184 SetSockaddrIn(&route.rt_dst, addr & netmask);
1185 SetSockaddrIn(&route.rt_genmask, netmask);
1186 char rt_dev[IFNAMSIZ];
1187 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
1188 rt_dev[IFNAMSIZ - 1] = '\0';
1189 route.rt_dev = rt_dev;
1190 route.rt_flags = RTF_UP | RTF_GATEWAY;
1191 return ModifyRtentry(SIOCDELRT, &route);
1192}
1193
Taoyu Lia0727dc2020-09-24 19:54:59 +09001194bool Datapath::ModifyRtentry(ioctl_req_t op, struct rtentry* route) {
Hugo Benichie8758b52020-04-03 14:49:01 +09001195 DCHECK(route);
1196 if (op != SIOCADDRT && op != SIOCDELRT) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001197 LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +09001198 return false;
1199 }
Hugo Benichie8758b52020-04-03 14:49:01 +09001200 base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
1201 if (!fd.is_valid()) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001202 PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route;
Hugo Benichie8758b52020-04-03 14:49:01 +09001203 return false;
1204 }
1205 if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) {
1206 std::string opname = op == SIOCADDRT ? "add" : "delete";
Andreea Costinas34aa7a92020-08-04 10:36:10 +02001207 PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +09001208 return false;
1209 }
1210 return true;
1211}
1212
Jason Jeremy Imana7273a32020-08-04 11:25:31 +09001213bool Datapath::AddAdbPortForwardRule(const std::string& ifname) {
1214 return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
1215 kArcAddr, kAdbServerPort, ifname,
1216 kLocalhostAddr, kAdbProxyTcpListenPort);
1217}
1218
1219void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) {
1220 firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
1221 kArcAddr, kAdbServerPort, ifname,
1222 kLocalhostAddr, kAdbProxyTcpListenPort);
1223}
1224
1225bool Datapath::AddAdbPortAccessRule(const std::string& ifname) {
1226 return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
1227 kAdbProxyTcpListenPort, ifname);
1228}
1229
1230void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) {
1231 firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
1232 kAdbProxyTcpListenPort, ifname);
1233}
1234
Hugo Benichiaf9d8a72020-08-26 13:28:13 +09001235void Datapath::SetIfnameIndex(const std::string& ifname, int ifindex) {
1236 if_nametoindex_[ifname] = ifindex;
1237}
1238
1239int Datapath::FindIfIndex(const std::string& ifname) {
1240 uint32_t ifindex = if_nametoindex(ifname.c_str());
1241 if (ifindex > 0) {
1242 if_nametoindex_[ifname] = ifindex;
1243 return ifindex;
1244 }
1245
1246 const auto it = if_nametoindex_.find(ifname);
1247 if (it != if_nametoindex_.end())
1248 return it->second;
1249
1250 return 0;
1251}
1252
Garrick Evans3388a032020-03-24 11:25:55 +09001253} // namespace patchpanel