blob: 61557496d593c4fd2b75de45381e1986f404e53f [file] [log] [blame]
Garrick Evansf0ab7132019-06-18 14:50:42 +09001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Garrick Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/datapath.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +09006
Garrick Evans3d97a392020-02-21 15:24:37 +09007#include <arpa/inet.h>
Garrick Evansc7ae82c2019-09-04 16:25:10 +09008#include <fcntl.h>
9#include <linux/if_tun.h>
10#include <linux/sockios.h>
11#include <net/if.h>
12#include <net/if_arp.h>
13#include <netinet/in.h>
14#include <string.h>
15#include <sys/ioctl.h>
16#include <sys/socket.h>
17
Hugo Benichid82d8832020-08-14 10:05:03 +090018#include <vector>
19
Garrick Evansc7ae82c2019-09-04 16:25:10 +090020#include <base/files/scoped_file.h>
21#include <base/logging.h>
Taoyu Li79871c92020-07-02 16:09:39 +090022#include <base/posix/eintr_wrapper.h>
Garrick Evans54861622019-07-19 09:05:09 +090023#include <base/strings/string_number_conversions.h>
Garrick Evans4f9f5572019-11-26 10:25:16 +090024#include <brillo/userdb_utils.h>
Garrick Evans54861622019-07-19 09:05:09 +090025
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090026#include "patchpanel/adb_proxy.h"
Garrick Evans3388a032020-03-24 11:25:55 +090027#include "patchpanel/net_util.h"
28#include "patchpanel/scoped_ns.h"
Garrick Evansc7ae82c2019-09-04 16:25:10 +090029
Garrick Evans3388a032020-03-24 11:25:55 +090030namespace patchpanel {
Garrick Evans54861622019-07-19 09:05:09 +090031
Garrick Evansc7ae82c2019-09-04 16:25:10 +090032namespace {
Hugo Benichi76675592020-04-08 14:29:57 +090033// TODO(hugobenichi) Consolidate this constant definition in a single place.
34constexpr pid_t kTestPID = -2;
Garrick Evansc7ae82c2019-09-04 16:25:10 +090035constexpr char kDefaultIfname[] = "vmtap%d";
36constexpr char kTunDev[] = "/dev/net/tun";
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090037constexpr char kArcAddr[] = "100.115.92.2";
38constexpr char kLocalhostAddr[] = "127.0.0.1";
39constexpr uint16_t kAdbServerPort = 5555;
Hugo Benichie8758b52020-04-03 14:49:01 +090040
Garrick Evans8a067562020-05-11 12:47:30 +090041std::string PrefixIfname(const std::string& prefix, const std::string& ifname) {
42 std::string n = prefix + ifname;
Garrick Evans2f581a02020-05-11 10:43:35 +090043 if (n.length() < IFNAMSIZ)
44 return n;
Garrick Evans54861622019-07-19 09:05:09 +090045
Garrick Evans2f581a02020-05-11 10:43:35 +090046 // Best effort attempt to preserve the interface number, assuming it's the
47 // last char in the name.
48 auto c = ifname[ifname.length() - 1];
49 n.resize(IFNAMSIZ - 1);
50 n[n.length() - 1] = c;
51 return n;
Garrick Evans54861622019-07-19 09:05:09 +090052}
Garrick Evansf0ab7132019-06-18 14:50:42 +090053
Hugo Benichiaf9d8a72020-08-26 13:28:13 +090054bool IsValidIpFamily(IpFamily family) {
55 switch (family) {
56 case IPv4:
57 case IPv6:
58 case Dual:
59 return true;
60 default:
61 return false;
62 }
63}
64
Garrick Evans8a067562020-05-11 12:47:30 +090065} // namespace
66
67std::string ArcVethHostName(const std::string& ifname) {
68 return PrefixIfname("veth", ifname);
69}
70
71std::string ArcBridgeName(const std::string& ifname) {
72 return PrefixIfname("arc_", ifname);
73}
74
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090075Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall)
76 : Datapath(process_runner, firewall, ioctl) {}
Garrick Evansc7ae82c2019-09-04 16:25:10 +090077
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090078Datapath::Datapath(MinijailedProcessRunner* process_runner,
79 Firewall* firewall,
80 ioctl_t ioctl_hook)
81 : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) {
Garrick Evansf0ab7132019-06-18 14:50:42 +090082 CHECK(process_runner_);
83}
84
Garrick Evans260ff302019-07-25 11:22:50 +090085MinijailedProcessRunner& Datapath::runner() const {
86 return *process_runner_;
87}
88
Hugo Benichi33860d72020-07-09 16:34:01 +090089bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) {
90 // Try first to delete any netns with name |netns_name| in case patchpanel
91 // did not exit cleanly.
92 if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0)
93 LOG(INFO) << "Deleted left over network namespace name " << netns_name;
94 return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0;
95}
96
97bool Datapath::NetnsDeleteName(const std::string& netns_name) {
98 return process_runner_->ip_netns_delete(netns_name) == 0;
99}
100
Garrick Evans8a949dc2019-07-18 16:17:53 +0900101bool Datapath::AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900102 uint32_t ipv4_addr,
103 uint32_t ipv4_prefix_len) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900104 // Configure the persistent Chrome OS bridge interface with static IP.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900105 if (process_runner_->brctl("addbr", {ifname}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900106 return false;
107 }
108
Garrick Evans6f4fa3a2020-02-10 16:15:09 +0900109 if (process_runner_->ip(
110 "addr", "add",
111 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
112 IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
113 "dev", ifname}) != 0) {
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900114 RemoveBridge(ifname);
115 return false;
116 }
117
118 if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900119 RemoveBridge(ifname);
120 return false;
121 }
122
123 // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules.
Hugo Benichie8758b52020-04-03 14:49:01 +0900124 if (!AddOutboundIPv4SNATMark(ifname)) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900125 RemoveBridge(ifname);
126 return false;
127 }
128
129 return true;
130}
131
132void Datapath::RemoveBridge(const std::string& ifname) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900133 RemoveOutboundIPv4SNATMark(ifname);
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900134 process_runner_->ip("link", "set", {ifname, "down"});
Garrick Evans8e8e3472020-01-23 14:03:50 +0900135 process_runner_->brctl("delbr", {ifname});
Garrick Evans8a949dc2019-07-18 16:17:53 +0900136}
137
Garrick Evans621ed262019-11-13 12:28:43 +0900138bool Datapath::AddToBridge(const std::string& br_ifname,
139 const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900140 return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0);
Garrick Evans621ed262019-11-13 12:28:43 +0900141}
142
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900143std::string Datapath::AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900144 const MacAddress* mac_addr,
145 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900146 const std::string& user) {
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900147 base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK));
148 if (!dev.is_valid()) {
149 PLOG(ERROR) << "Failed to open " << kTunDev;
150 return "";
151 }
152
153 struct ifreq ifr;
154 memset(&ifr, 0, sizeof(ifr));
155 strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(),
156 sizeof(ifr.ifr_name));
157 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
158
159 // If a template was given as the name, ifr_name will be updated with the
160 // actual interface name.
161 if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900162 PLOG(ERROR) << "Failed to create tap interface " << name;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900163 return "";
164 }
165 const char* ifname = ifr.ifr_name;
166
167 if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900168 PLOG(ERROR) << "Failed to persist the interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900169 return "";
170 }
171
Garrick Evans4f9f5572019-11-26 10:25:16 +0900172 if (!user.empty()) {
173 uid_t uid = -1;
174 if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) {
175 PLOG(ERROR) << "Unable to look up UID for " << user;
176 RemoveTAP(ifname);
177 return "";
178 }
179 if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) {
180 PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface "
181 << ifname;
182 RemoveTAP(ifname);
183 return "";
184 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900185 }
186
Hugo Benichib9b93fe2019-10-25 23:36:01 +0900187 // Create control socket for configuring the interface.
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900188 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
189 if (!sock.is_valid()) {
190 PLOG(ERROR) << "Failed to create control socket for tap interface "
Garrick Evans621ed262019-11-13 12:28:43 +0900191 << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900192 RemoveTAP(ifname);
193 return "";
194 }
195
Garrick Evans621ed262019-11-13 12:28:43 +0900196 if (ipv4_addr) {
197 struct sockaddr_in* addr =
198 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr);
199 addr->sin_family = AF_INET;
200 addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address());
201 if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) {
202 PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname
203 << " {" << ipv4_addr->ToCidrString() << "}";
204 RemoveTAP(ifname);
205 return "";
206 }
207
208 struct sockaddr_in* netmask =
209 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask);
210 netmask->sin_family = AF_INET;
211 netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask());
212 if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) {
213 PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname
214 << " {" << ipv4_addr->ToCidrString() << "}";
215 RemoveTAP(ifname);
216 return "";
217 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900218 }
219
Garrick Evans621ed262019-11-13 12:28:43 +0900220 if (mac_addr) {
221 struct sockaddr* hwaddr = &ifr.ifr_hwaddr;
222 hwaddr->sa_family = ARPHRD_ETHER;
223 memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr));
224 if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) {
225 PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname
226 << " {" << MacAddressToString(*mac_addr) << "}";
227 RemoveTAP(ifname);
228 return "";
229 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900230 }
231
232 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900233 PLOG(ERROR) << "Failed to get flags for tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900234 RemoveTAP(ifname);
235 return "";
236 }
237
238 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
239 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900240 PLOG(ERROR) << "Failed to enable tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900241 RemoveTAP(ifname);
242 return "";
243 }
244
245 return ifname;
246}
247
248void Datapath::RemoveTAP(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900249 process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"});
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900250}
251
Hugo Benichi33860d72020-07-09 16:34:01 +0900252bool Datapath::ConnectVethPair(pid_t netns_pid,
253 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900254 const std::string& veth_ifname,
255 const std::string& peer_ifname,
256 const MacAddress& remote_mac_addr,
257 uint32_t remote_ipv4_addr,
258 uint32_t remote_ipv4_prefix_len,
259 bool remote_multicast_flag) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900260 // Set up the virtual pair across the current namespace and |netns_name|.
261 if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) {
262 LOG(ERROR) << "Failed to create veth pair " << veth_ifname << ","
263 << peer_ifname;
264 return false;
265 }
266
267 // Configure the remote veth in namespace |netns_name|.
Hugo Benichi76675592020-04-08 14:29:57 +0900268 {
Hugo Benichi33860d72020-07-09 16:34:01 +0900269 ScopedNS ns(netns_pid);
270 if (!ns.IsValid() && netns_pid != kTestPID) {
Hugo Benichi76675592020-04-08 14:29:57 +0900271 LOG(ERROR)
272 << "Cannot create virtual link -- invalid container namespace?";
273 return false;
274 }
275
Hugo Benichi76675592020-04-08 14:29:57 +0900276 if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr,
277 remote_ipv4_prefix_len, true /* link up */,
278 remote_multicast_flag)) {
279 LOG(ERROR) << "Failed to configure interface " << peer_ifname;
280 RemoveInterface(peer_ifname);
281 return false;
282 }
283 }
284
Hugo Benichi76675592020-04-08 14:29:57 +0900285 if (!ToggleInterface(veth_ifname, true /*up*/)) {
286 LOG(ERROR) << "Failed to bring up interface " << veth_ifname;
287 RemoveInterface(veth_ifname);
288 return false;
289 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900290
Hugo Benichi76675592020-04-08 14:29:57 +0900291 return true;
292}
293
Hugo Benichi33860d72020-07-09 16:34:01 +0900294bool Datapath::AddVirtualInterfacePair(const std::string& netns_name,
295 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +0900296 const std::string& peer_ifname) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900297 return process_runner_->ip("link", "add",
298 {veth_ifname, "type", "veth", "peer", "name",
299 peer_ifname, "netns", netns_name}) == 0;
Garrick Evans2470caa2020-03-04 14:15:41 +0900300}
Garrick Evans54861622019-07-19 09:05:09 +0900301
Garrick Evans2470caa2020-03-04 14:15:41 +0900302bool Datapath::ToggleInterface(const std::string& ifname, bool up) {
303 const std::string link = up ? "up" : "down";
304 return process_runner_->ip("link", "set", {ifname, link}) == 0;
305}
Garrick Evans54861622019-07-19 09:05:09 +0900306
Garrick Evans2470caa2020-03-04 14:15:41 +0900307bool Datapath::ConfigureInterface(const std::string& ifname,
308 const MacAddress& mac_addr,
309 uint32_t ipv4_addr,
310 uint32_t ipv4_prefix_len,
311 bool up,
312 bool enable_multicast) {
313 const std::string link = up ? "up" : "down";
314 const std::string multicast = enable_multicast ? "on" : "off";
315 return (process_runner_->ip(
316 "addr", "add",
317 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
318 IPv4AddressToString(
319 Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
320 "dev", ifname}) == 0) &&
321 (process_runner_->ip("link", "set",
322 {
323 "dev",
324 ifname,
325 link,
326 "addr",
327 MacAddressToString(mac_addr),
328 "multicast",
329 multicast,
330 }) == 0);
Garrick Evans54861622019-07-19 09:05:09 +0900331}
332
333void Datapath::RemoveInterface(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900334 process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/);
Garrick Evans54861622019-07-19 09:05:09 +0900335}
336
Hugo Benichi321f23b2020-09-25 15:42:05 +0900337bool Datapath::AddSourceIPv4DropRule(const std::string& oif,
338 const std::string& src_ip) {
339 return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s",
340 src_ip, "-j", "DROP", "-w"}) == 0;
341}
342
343bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif,
344 const std::string& src_ip) {
345 return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s",
346 src_ip, "-j", "DROP", "-w"}) == 0;
347}
348
Hugo Benichi8d622b52020-08-13 15:24:12 +0900349void Datapath::StartRoutingDevice(const std::string& ext_ifname,
350 const std::string& int_ifname,
351 uint32_t int_ipv4_addr,
352 TrafficSource source) {
353 if (!ext_ifname.empty() &&
354 !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)))
355 LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname
356 << "->" << int_ifname;
357
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900358 if (!StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900359 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->"
360 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900361
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900362 if (!StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname))
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900363 LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-"
364 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900365
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900366 if (!ext_ifname.empty()) {
367 // If |ext_ifname| is not null, mark egress traffic with the
368 // fwmark routing tag corresponding to |ext_ifname|.
369 if (!ModifyFwmarkRoutingTag("-A", ext_ifname, int_ifname))
370 LOG(ERROR) << "Failed to add PREROUTING fwmark routing tag for "
371 << ext_ifname << "<-" << int_ifname;
372 } else {
373 // Otherwise if ext_ifname is null, set up a CONNMARK restore rule in
374 // PREROUTING to apply any fwmark routing tag saved for the current
375 // connection, and rely on implicit routing to the default logical network
376 // otherwise.
377 if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", int_ifname))
378 LOG(ERROR) << "Failed to add PREROUTING CONNMARK restore rule for "
379 << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900380
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900381 // TODO(b/161507671) When a VPN service starts, tag new connections with the
382 // fwmark routing tag for VPNs.
383 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900384
Hugo Benichi9be19b12020-08-14 15:33:40 +0900385 if (!ModifyFwmarkSourceTag("-A", int_ifname, source))
386 LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source "
387 << source << " for " << int_ifname;
Hugo Benichi8d622b52020-08-13 15:24:12 +0900388}
389
390void Datapath::StopRoutingDevice(const std::string& ext_ifname,
391 const std::string& int_ifname,
392 uint32_t int_ipv4_addr,
393 TrafficSource source) {
394 if (!ext_ifname.empty())
395 RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr));
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900396 StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname);
397 StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900398 ModifyFwmarkSourceTag("-D", int_ifname, source);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900399 if (!ext_ifname.empty()) {
400 ModifyFwmarkRoutingTag("-D", ext_ifname, int_ifname);
401 } else {
402 ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", int_ifname);
403 }
Hugo Benichi8d622b52020-08-13 15:24:12 +0900404}
405
Garrick Evansf0ab7132019-06-18 14:50:42 +0900406bool Datapath::AddInboundIPv4DNAT(const std::string& ifname,
407 const std::string& ipv4_addr) {
408 // Direct ingress IP traffic to existing sockets.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900409 if (process_runner_->iptables(
410 "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket",
411 "--nowildcard", "-j", "ACCEPT", "-w"}) != 0)
Garrick Evansf0ab7132019-06-18 14:50:42 +0900412 return false;
413
414 // Direct ingress TCP & UDP traffic to ARC interface for new connections.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900415 if (process_runner_->iptables(
416 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
417 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900418 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
419 return false;
420 }
Garrick Evans8e8e3472020-01-23 14:03:50 +0900421 if (process_runner_->iptables(
422 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
423 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900424 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
425 return false;
426 }
427
428 return true;
429}
430
431void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname,
432 const std::string& ipv4_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900433 process_runner_->iptables(
434 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
435 "--to-destination", ipv4_addr, "-w"});
436 process_runner_->iptables(
437 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
438 "--to-destination", ipv4_addr, "-w"});
439 process_runner_->iptables(
440 "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard",
441 "-j", "ACCEPT", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900442}
443
Hugo Benichi8d622b52020-08-13 15:24:12 +0900444// TODO(hugobenichi) The name incorrectly refers to egress traffic, but this
445// FORWARD rule actually enables forwarding for ingress traffic. Fix the name.
Garrick Evansf0ab7132019-06-18 14:50:42 +0900446bool Datapath::AddOutboundIPv4(const std::string& ifname) {
Hugo Benichid82d8832020-08-14 10:05:03 +0900447 return StartIpForwarding(IpFamily::IPv4, "", ifname);
Garrick Evansf0ab7132019-06-18 14:50:42 +0900448}
449
450void Datapath::RemoveOutboundIPv4(const std::string& ifname) {
Hugo Benichid82d8832020-08-14 10:05:03 +0900451 StopIpForwarding(IpFamily::IPv4, "", ifname);
Garrick Evansf0ab7132019-06-18 14:50:42 +0900452}
453
Hugo Benichic6ae67c2020-08-14 15:02:13 +0900454// TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded
455// egress traffic is routed through the fwmark routing tag.
Garrick Evansd291af62020-05-25 10:39:06 +0900456bool Datapath::AddSNATMarkRules() {
Taoyu Li79871c92020-07-02 16:09:39 +0900457 // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore
458 // need to be explicitly dropped.
459 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900460 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m",
Taoyu Li79871c92020-07-02 16:09:39 +0900461 "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) {
462 return false;
463 }
Garrick Evansd291af62020-05-25 10:39:06 +0900464 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900465 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900466 "ACCEPT", "-w"}) != 0) {
467 return false;
468 }
469 if (process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900470 "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j",
Garrick Evansd291af62020-05-25 10:39:06 +0900471 "MASQUERADE", "-w"}) != 0) {
472 RemoveSNATMarkRules();
473 return false;
474 }
475 return true;
476}
477
478void Datapath::RemoveSNATMarkRules() {
479 process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900480 "1/1", "-j", "MASQUERADE", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900481 process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark",
Hugo Benichi6c445322020-08-12 16:46:19 +0900482 "1/1", "-j", "ACCEPT", "-w"});
Taoyu Li79871c92020-07-02 16:09:39 +0900483 process_runner_->iptables(
Hugo Benichi6c445322020-08-12 16:46:19 +0900484 "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state",
Taoyu Li79871c92020-07-02 16:09:39 +0900485 "--state", "INVALID", "-j", "DROP", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900486}
487
Garrick Evansff6e37f2020-05-25 10:54:47 +0900488bool Datapath::AddInterfaceSNAT(const std::string& ifname) {
489 return process_runner_->iptables("nat", {"-A", "POSTROUTING", "-o", ifname,
490 "-j", "MASQUERADE", "-w"}) == 0;
491}
492
493void Datapath::RemoveInterfaceSNAT(const std::string& ifname) {
494 process_runner_->iptables(
495 "nat", {"-D", "POSTROUTING", "-o", ifname, "-j", "MASQUERADE", "-w"});
496}
497
Hugo Benichie8758b52020-04-03 14:49:01 +0900498bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) {
499 return process_runner_->iptables(
500 "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK",
Hugo Benichi6c445322020-08-12 16:46:19 +0900501 "--set-mark", "1/1", "-w"}) == 0;
Hugo Benichie8758b52020-04-03 14:49:01 +0900502}
503
504void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) {
505 process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j",
Hugo Benichi6c445322020-08-12 16:46:19 +0900506 "MARK", "--set-mark", "1/1", "-w"});
Hugo Benichie8758b52020-04-03 14:49:01 +0900507}
508
Garrick Evansd291af62020-05-25 10:39:06 +0900509bool Datapath::AddForwardEstablishedRule() {
510 return process_runner_->iptables(
511 "filter", {"-A", "FORWARD", "-m", "state", "--state",
512 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) == 0;
513}
514
515void Datapath::RemoveForwardEstablishedRule() {
516 process_runner_->iptables("filter",
517 {"-D", "FORWARD", "-m", "state", "--state",
518 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"});
519}
520
Garrick Evans664a82f2019-12-17 12:18:05 +0900521bool Datapath::MaskInterfaceFlags(const std::string& ifname,
522 uint16_t on,
523 uint16_t off) {
Taoyu Li90c13912019-11-26 17:56:54 +0900524 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
525 if (!sock.is_valid()) {
526 PLOG(ERROR) << "Failed to create control socket";
527 return false;
528 }
529 ifreq ifr;
530 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str());
531 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) {
532 PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname;
533 return false;
534 }
Garrick Evans664a82f2019-12-17 12:18:05 +0900535 ifr.ifr_flags |= on;
536 ifr.ifr_flags &= ~off;
Taoyu Li90c13912019-11-26 17:56:54 +0900537 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900538 PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on
539 << " unset flag 0x" << std::hex << off << " on " << ifname;
Taoyu Li90c13912019-11-26 17:56:54 +0900540 return false;
541 }
542 return true;
543}
544
Garrick Evans260ff302019-07-25 11:22:50 +0900545bool Datapath::AddIPv6HostRoute(const std::string& ifname,
546 const std::string& ipv6_addr,
547 int ipv6_prefix_len) {
548 std::string ipv6_addr_cidr =
549 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
550
Garrick Evans8e8e3472020-01-23 14:03:50 +0900551 return process_runner_->ip6("route", "replace",
552 {ipv6_addr_cidr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900553}
554
555void Datapath::RemoveIPv6HostRoute(const std::string& ifname,
556 const std::string& ipv6_addr,
557 int ipv6_prefix_len) {
558 std::string ipv6_addr_cidr =
559 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
560
Garrick Evans8e8e3472020-01-23 14:03:50 +0900561 process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900562}
563
Taoyu Lia0727dc2020-09-24 19:54:59 +0900564bool Datapath::AddIPv6Address(const std::string& ifname,
565 const std::string& ipv6_addr) {
566 return process_runner_->ip6("addr", "add", {ipv6_addr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900567}
568
Taoyu Lia0727dc2020-09-24 19:54:59 +0900569void Datapath::RemoveIPv6Address(const std::string& ifname,
570 const std::string& ipv6_addr) {
571 process_runner_->ip6("addr", "del", {ipv6_addr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900572}
573
Hugo Benichi76be34a2020-08-26 22:35:54 +0900574void Datapath::StartConnectionPinning(const std::string& ext_ifname) {
575 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-A", ext_ifname))
576 LOG(ERROR) << "Could not start connection pinning on " << ext_ifname;
577}
578
579void Datapath::StopConnectionPinning(const std::string& ext_ifname) {
580 if (!ModifyConnmarkSetPostrouting(IpFamily::Dual, "-D", ext_ifname))
581 LOG(ERROR) << "Could not stop connection pinning on " << ext_ifname;
582}
583
584bool Datapath::ModifyConnmarkSetPostrouting(IpFamily family,
585 const std::string& op,
586 const std::string& oif) {
587 if (oif.empty()) {
588 LOG(ERROR) << "Cannot change POSTROUTING CONNMARK set-mark with no "
589 "interface specified";
590 return false;
591 }
592
593 if (!IsValidIpFamily(family)) {
594 LOG(ERROR) << "Cannot change POSTROUTING CONNMARK set-mark for " << oif
595 << ": incorrect IP family " << family;
596 return false;
597 }
598
599 int ifindex = FindIfIndex(oif);
600 if (ifindex == 0) {
601 PLOG(ERROR) << "if_nametoindex(" << oif << ") failed";
602 return false;
603 }
604
605 std::vector<std::string> args = {op,
606 "POSTROUTING",
607 "-o",
608 oif,
609 "-j",
610 "CONNMARK",
611 "--set-mark",
612 Fwmark::FromIfIndex(ifindex).ToString() +
613 "/" + kFwmarkRoutingMask.ToString(),
614 "-w"};
615
616 bool success = true;
617 if (family & IPv4)
618 success &= process_runner_->iptables("mangle", args) == 0;
619 if (family & IPv6)
620 success &= process_runner_->ip6tables("mangle", args) == 0;
621 return false;
622}
623
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900624bool Datapath::ModifyConnmarkRestore(IpFamily family,
625 const std::string& chain,
626 const std::string& op,
627 const std::string& iif) {
628 if (chain != "OUTPUT" && (chain != "PREROUTING" || iif.empty())) {
629 LOG(ERROR) << "Invalid arguments chain=" << chain << " iif=" << iif;
630 return false;
631 }
632
633 if (!IsValidIpFamily(family)) {
634 LOG(ERROR) << "Cannot change " << chain << " -j CONNMARK restore-mark"
635 << " for " << iif << ": incorrect IP family " << family;
636 return false;
637 }
638
639 std::vector<std::string> args = {op, chain};
640 if (!iif.empty()) {
641 args.push_back("-i");
642 args.push_back(iif);
643 }
644 args.insert(args.end(), {"-j", "CONNMARK", "--restore-mark", "--mask",
645 kFwmarkRoutingMask.ToString(), "-w"});
646
647 bool success = true;
648 if (family & IPv4)
649 success &= process_runner_->iptables("mangle", args) == 0;
650 if (family & IPv6)
651 success &= process_runner_->ip6tables("mangle", args) == 0;
652 return success;
653}
654
655bool Datapath::ModifyFwmarkRoutingTag(const std::string& op,
656 const std::string& ext_ifname,
657 const std::string& int_ifname) {
658 int ifindex = FindIfIndex(ext_ifname);
659 if (ifindex == 0) {
660 PLOG(ERROR) << "if_nametoindex(" << ext_ifname << ") failed";
661 return false;
662 }
663
664 return ModifyFwmarkPrerouting(IpFamily::Dual, op, int_ifname,
665 Fwmark::FromIfIndex(ifindex),
666 kFwmarkRoutingMask);
667}
668
Hugo Benichi9be19b12020-08-14 15:33:40 +0900669bool Datapath::ModifyFwmarkSourceTag(const std::string& op,
670 const std::string& iif,
671 TrafficSource source) {
672 return ModifyFwmarkPrerouting(IpFamily::Dual, op, iif,
673 Fwmark::FromSource(source),
674 kFwmarkAllSourcesMask);
675}
676
677bool Datapath::ModifyFwmarkPrerouting(IpFamily family,
678 const std::string& op,
679 const std::string& iif,
680 Fwmark mark,
681 Fwmark mask,
682 bool log_failures) {
683 if (iif.empty()) {
684 LOG(ERROR)
685 << "Cannot change PREROUTING set-fwmark with no interface specified";
686 return false;
687 }
688
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900689 if (!IsValidIpFamily(family)) {
690 LOG(ERROR) << "Cannot change PREROUTING set-fwmark for " << iif
691 << ": incorrect IP family " << family;
692 return false;
Hugo Benichi9be19b12020-08-14 15:33:40 +0900693 }
694
695 std::vector<std::string> args = {
696 op, "PREROUTING", "-i", iif,
697 "-j", "MARK", "--set-mark", mark.ToString() + "/" + mask.ToString(),
698 "-w"};
699
700 bool success = true;
Hugo Benichi5c9c11c2020-09-15 17:25:26 +0900701 if (family & IPv4)
Hugo Benichi9be19b12020-08-14 15:33:40 +0900702 success &= process_runner_->iptables("mangle", args, log_failures) == 0;
Hugo Benichi5c9c11c2020-09-15 17:25:26 +0900703 if (family & IPv6)
Hugo Benichi9be19b12020-08-14 15:33:40 +0900704 success &= process_runner_->ip6tables("mangle", args, log_failures) == 0;
Hugo Benichifa97b3b2020-10-06 22:45:26 +0900705 return success;
Hugo Benichi9be19b12020-08-14 15:33:40 +0900706}
707
Hugo Benichid82d8832020-08-14 10:05:03 +0900708bool Datapath::ModifyIpForwarding(IpFamily family,
709 const std::string& op,
710 const std::string& iif,
711 const std::string& oif,
712 bool log_failures) {
713 if (iif.empty() && oif.empty()) {
714 LOG(ERROR) << "Cannot change IP forwarding with no input or output "
715 "interface specified";
Garrick Evans260ff302019-07-25 11:22:50 +0900716 return false;
717 }
718
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900719 if (!IsValidIpFamily(family)) {
720 LOG(ERROR) << "Cannot change IP forwarding from \"" << iif << "\" to \""
721 << oif << "\": incorrect IP family " << family;
722 return false;
Hugo Benichid82d8832020-08-14 10:05:03 +0900723 }
724
725 std::vector<std::string> args = {op, "FORWARD"};
726 if (!iif.empty()) {
727 args.push_back("-i");
728 args.push_back(iif);
729 }
730 if (!oif.empty()) {
731 args.push_back("-o");
732 args.push_back(oif);
733 }
734 args.push_back("-j");
735 args.push_back("ACCEPT");
736 args.push_back("-w");
737
738 bool success = true;
739 if (family & IpFamily::IPv4)
740 success &= process_runner_->iptables("filter", args, log_failures) == 0;
741 if (family & IpFamily::IPv6)
742 success &= process_runner_->ip6tables("filter", args, log_failures) == 0;
743 return success;
744}
745
746bool Datapath::StartIpForwarding(IpFamily family,
747 const std::string& iif,
748 const std::string& oif) {
749 return ModifyIpForwarding(family, "-A", iif, oif);
750}
751
752bool Datapath::StopIpForwarding(IpFamily family,
753 const std::string& iif,
754 const std::string& oif) {
755 return ModifyIpForwarding(family, "-D", iif, oif);
756}
757
758bool Datapath::AddIPv6Forwarding(const std::string& ifname1,
759 const std::string& ifname2) {
760 // Only start Ipv6 forwarding if -C returns false and it had not been
761 // started yet.
762 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2,
763 false /*log_failures*/) &&
764 !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) {
765 return false;
766 }
767
768 if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1,
769 false /*log_failures*/) &&
770 !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) {
Garrick Evans260ff302019-07-25 11:22:50 +0900771 RemoveIPv6Forwarding(ifname1, ifname2);
772 return false;
773 }
774
775 return true;
776}
777
778void Datapath::RemoveIPv6Forwarding(const std::string& ifname1,
779 const std::string& ifname2) {
Hugo Benichid82d8832020-08-14 10:05:03 +0900780 StopIpForwarding(IpFamily::IPv6, ifname1, ifname2);
781 StopIpForwarding(IpFamily::IPv6, ifname2, ifname1);
Garrick Evans260ff302019-07-25 11:22:50 +0900782}
783
Garrick Evans3d97a392020-02-21 15:24:37 +0900784bool Datapath::AddIPv4Route(uint32_t gateway_addr,
785 uint32_t addr,
786 uint32_t netmask) {
787 struct rtentry route;
788 memset(&route, 0, sizeof(route));
Hugo Benichie8758b52020-04-03 14:49:01 +0900789 SetSockaddrIn(&route.rt_gateway, gateway_addr);
790 SetSockaddrIn(&route.rt_dst, addr & netmask);
791 SetSockaddrIn(&route.rt_genmask, netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900792 route.rt_flags = RTF_UP | RTF_GATEWAY;
Hugo Benichie8758b52020-04-03 14:49:01 +0900793 return ModifyRtentry(SIOCADDRT, &route);
794}
Garrick Evans3d97a392020-02-21 15:24:37 +0900795
Hugo Benichie8758b52020-04-03 14:49:01 +0900796bool Datapath::DeleteIPv4Route(uint32_t gateway_addr,
797 uint32_t addr,
798 uint32_t netmask) {
799 struct rtentry route;
800 memset(&route, 0, sizeof(route));
801 SetSockaddrIn(&route.rt_gateway, gateway_addr);
802 SetSockaddrIn(&route.rt_dst, addr & netmask);
803 SetSockaddrIn(&route.rt_genmask, netmask);
804 route.rt_flags = RTF_UP | RTF_GATEWAY;
805 return ModifyRtentry(SIOCDELRT, &route);
806}
807
808bool Datapath::AddIPv4Route(const std::string& ifname,
809 uint32_t addr,
810 uint32_t netmask) {
811 struct rtentry route;
812 memset(&route, 0, sizeof(route));
813 SetSockaddrIn(&route.rt_dst, addr & netmask);
814 SetSockaddrIn(&route.rt_genmask, netmask);
815 char rt_dev[IFNAMSIZ];
816 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
817 rt_dev[IFNAMSIZ - 1] = '\0';
818 route.rt_dev = rt_dev;
819 route.rt_flags = RTF_UP | RTF_GATEWAY;
820 return ModifyRtentry(SIOCADDRT, &route);
821}
822
823bool Datapath::DeleteIPv4Route(const std::string& ifname,
824 uint32_t addr,
825 uint32_t netmask) {
826 struct rtentry route;
827 memset(&route, 0, sizeof(route));
828 SetSockaddrIn(&route.rt_dst, addr & netmask);
829 SetSockaddrIn(&route.rt_genmask, netmask);
830 char rt_dev[IFNAMSIZ];
831 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
832 rt_dev[IFNAMSIZ - 1] = '\0';
833 route.rt_dev = rt_dev;
834 route.rt_flags = RTF_UP | RTF_GATEWAY;
835 return ModifyRtentry(SIOCDELRT, &route);
836}
837
Taoyu Lia0727dc2020-09-24 19:54:59 +0900838bool Datapath::ModifyRtentry(ioctl_req_t op, struct rtentry* route) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900839 DCHECK(route);
840 if (op != SIOCADDRT && op != SIOCDELRT) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +0200841 LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +0900842 return false;
843 }
Hugo Benichie8758b52020-04-03 14:49:01 +0900844 base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
845 if (!fd.is_valid()) {
Andreea Costinas34aa7a92020-08-04 10:36:10 +0200846 PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route;
Hugo Benichie8758b52020-04-03 14:49:01 +0900847 return false;
848 }
849 if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) {
850 std::string opname = op == SIOCADDRT ? "add" : "delete";
Andreea Costinas34aa7a92020-08-04 10:36:10 +0200851 PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route;
Garrick Evans3d97a392020-02-21 15:24:37 +0900852 return false;
853 }
854 return true;
855}
856
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900857bool Datapath::AddAdbPortForwardRule(const std::string& ifname) {
858 return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
859 kArcAddr, kAdbServerPort, ifname,
860 kLocalhostAddr, kAdbProxyTcpListenPort);
861}
862
863void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) {
864 firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP,
865 kArcAddr, kAdbServerPort, ifname,
866 kLocalhostAddr, kAdbProxyTcpListenPort);
867}
868
869bool Datapath::AddAdbPortAccessRule(const std::string& ifname) {
870 return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
871 kAdbProxyTcpListenPort, ifname);
872}
873
874void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) {
875 firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP,
876 kAdbProxyTcpListenPort, ifname);
877}
878
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900879void Datapath::SetIfnameIndex(const std::string& ifname, int ifindex) {
880 if_nametoindex_[ifname] = ifindex;
881}
882
883int Datapath::FindIfIndex(const std::string& ifname) {
884 uint32_t ifindex = if_nametoindex(ifname.c_str());
885 if (ifindex > 0) {
886 if_nametoindex_[ifname] = ifindex;
887 return ifindex;
888 }
889
890 const auto it = if_nametoindex_.find(ifname);
891 if (it != if_nametoindex_.end())
892 return it->second;
893
894 return 0;
895}
896
Garrick Evans3388a032020-03-24 11:25:55 +0900897} // namespace patchpanel