blob: 900d158780eb399afbd6783107b3ed3a4b633a4d [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#ifndef PATCHPANEL_DATAPATH_H_
6#define PATCHPANEL_DATAPATH_H_
Garrick Evansf0ab7132019-06-18 14:50:42 +09007
Hugo Benichie8758b52020-04-03 14:49:01 +09008#include <net/route.h>
Hugo Benichi33860d72020-07-09 16:34:01 +09009#include <sys/types.h>
Hugo Benichie8758b52020-04-03 14:49:01 +090010
Hugo Benichifcf81022020-12-04 11:01:37 +090011#include <iostream>
Hugo Benichibb38bdd2021-05-14 10:36:11 +090012#include <map>
13#include <memory>
Hugo Benichi2a940542020-10-26 18:50:49 +090014#include <set>
Garrick Evansf0ab7132019-06-18 14:50:42 +090015#include <string>
Hugo Benichi2a940542020-10-26 18:50:49 +090016#include <vector>
Garrick Evansf0ab7132019-06-18 14:50:42 +090017
18#include <base/macros.h>
Hugo Benichi82ed5cf2020-09-08 21:30:22 +090019#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evansf0ab7132019-06-18 14:50:42 +090020
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090021#include "patchpanel/firewall.h"
Garrick Evans3388a032020-03-24 11:25:55 +090022#include "patchpanel/mac_address_generator.h"
23#include "patchpanel/minijailed_process_runner.h"
Hugo Benichi153c7112021-02-22 17:46:33 +090024#include "patchpanel/net_util.h"
Hugo Benichi8d622b52020-08-13 15:24:12 +090025#include "patchpanel/routing_service.h"
Jason Jeremy Imana183d7a2021-08-06 01:35:40 +090026#include "patchpanel/scoped_ns.h"
Garrick Evans3388a032020-03-24 11:25:55 +090027#include "patchpanel/subnet.h"
Hugo Benichif818c782021-04-10 00:09:50 +090028#include "patchpanel/system.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +090029
Garrick Evans3388a032020-03-24 11:25:55 +090030namespace patchpanel {
Garrick Evansf0ab7132019-06-18 14:50:42 +090031
Hugo Benichiad5947b2021-09-03 16:56:40 +090032// filter INPUT chain for ingress port access rules controlled by
33// permission_broker.
34constexpr char kIngressPortFirewallChain[] = "ingress_port_firewall";
35// filter OUTPUT chain for egress port restriction rules controlled by
36// permission_broker.
37constexpr char kEgressPortFirewallChain[] = "egress_port_firewall";
38// nat PREROUTING chain for ingress DNAT forwarding rules controlled by
39// permission_broker.
40constexpr char kIngressPortForwardingChain[] = "ingress_port_forwarding";
41
Hugo Benichifcf81022020-12-04 11:01:37 +090042// Struct holding parameters for Datapath::StartRoutingNamespace requests.
43struct ConnectedNamespace {
Jie Jiangf6799312021-05-14 16:27:03 +090044 // The special pid which indicates this namespace is not attached to an
45 // associated process but should be/was created by `ip netns add`.
46 static constexpr pid_t kNewNetnsPid = -1;
47
Hugo Benichifcf81022020-12-04 11:01:37 +090048 // The pid of the client network namespace.
49 pid_t pid;
50 // The name attached to the client network namespace.
51 std::string netns_name;
Hugo Benichi93306e52020-12-04 16:08:00 +090052 // Source to which traffic from |host_ifname| will be attributed.
53 TrafficSource source;
Hugo Benichi600a9432021-07-06 11:57:29 +090054 // Interface name of the shill Device for routing outbound traffic from the
55 // client namespace. Empty if outbound traffic should be forwarded to the
56 // highest priority network (physical or virtual).
Hugo Benichifcf81022020-12-04 11:01:37 +090057 std::string outbound_ifname;
Hugo Benichi93306e52020-12-04 16:08:00 +090058 // If |outbound_ifname| is empty and |route_on_vpn| is false, the traffic from
59 // the client namespace will be routed to the highest priority physical
Hugo Benichi600a9432021-07-06 11:57:29 +090060 // network. If |outbound_ifname| is empty and |route_on_vpn| is true, the
Hugo Benichi93306e52020-12-04 16:08:00 +090061 // traffic will be routed through VPN connections. If |outbound_ifname|
Hugo Benichi600a9432021-07-06 11:57:29 +090062 // specifies a valid physical interface, |route_on_vpn| is ignored.
Hugo Benichi93306e52020-12-04 16:08:00 +090063 bool route_on_vpn;
Hugo Benichi600a9432021-07-06 11:57:29 +090064 // Name of the "local" veth interface visible on the host namespace.
Hugo Benichifcf81022020-12-04 11:01:37 +090065 std::string host_ifname;
Hugo Benichi600a9432021-07-06 11:57:29 +090066 // Name of the "remote" veth interface moved into the client namespace.
Hugo Benichifcf81022020-12-04 11:01:37 +090067 std::string peer_ifname;
68 // IPv4 subnet assigned to the client namespace.
69 std::unique_ptr<Subnet> peer_subnet;
Jason Jeremy Iman6a7c3952021-07-05 23:15:10 +090070 // MAC address of the "local" veth interface visible on the host namespace.
71 MacAddress host_mac_addr;
Hugo Benichi600a9432021-07-06 11:57:29 +090072 // MAC address of the "remote" veth interface.
Hugo Benichifcf81022020-12-04 11:01:37 +090073 MacAddress peer_mac_addr;
Jason Jeremy Iman6a7c3952021-07-05 23:15:10 +090074 // Interface name of the shill device for routing outbound traffic from the
75 // client namespace. This will be filled to keep track of the upstream
76 // interface if |outbound_ifname| is empty.
77 std::string tracked_outbound_ifname;
Hugo Benichifcf81022020-12-04 11:01:37 +090078};
79
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +090080struct DnsRedirectionRule {
81 patchpanel::SetDnsRedirectionRuleRequest::RuleType type;
82 std::string input_ifname;
83 std::string proxy_address;
84 std::vector<std::string> nameservers;
85};
86
Hugo Benichifcf81022020-12-04 11:01:37 +090087std::ostream& operator<<(std::ostream& stream,
88 const ConnectedNamespace& nsinfo);
89
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +090090std::ostream& operator<<(std::ostream& stream, const DnsRedirectionRule& rule);
91
Hugo Benichid82d8832020-08-14 10:05:03 +090092// Simple enum of bitmasks used for specifying a set of IP family values.
93enum IpFamily {
94 NONE = 0,
95 IPv4 = 1 << 0,
96 IPv6 = 1 << 1,
Taoyu Lia0727dc2020-09-24 19:54:59 +090097 Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1);
Hugo Benichid82d8832020-08-14 10:05:03 +090098};
99
Garrick Evans54861622019-07-19 09:05:09 +0900100// Returns for given interface name the host name of a ARC veth pair.
Garrick Evans2f581a02020-05-11 10:43:35 +0900101std::string ArcVethHostName(const std::string& ifname);
Garrick Evans54861622019-07-19 09:05:09 +0900102
Garrick Evans8a067562020-05-11 12:47:30 +0900103// Returns the ARC bridge interface name for the given interface.
104std::string ArcBridgeName(const std::string& ifname);
105
Garrick Evansf0ab7132019-06-18 14:50:42 +0900106// ARC networking data path configuration utility.
Garrick Evans54861622019-07-19 09:05:09 +0900107// IPV4 addresses are always specified in singular dotted-form (a.b.c.d)
108// (not in CIDR representation
Garrick Evansf0ab7132019-06-18 14:50:42 +0900109class Datapath {
110 public:
Hugo Benichi03be8962022-03-17 13:16:38 +0900111 explicit Datapath(System* system);
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900112 // Provided for testing only.
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900113 Datapath(MinijailedProcessRunner* process_runner,
114 Firewall* firewall,
Hugo Benichif818c782021-04-10 00:09:50 +0900115 System* system);
Qijiang Fan6bc59e12020-11-11 02:51:06 +0900116 Datapath(const Datapath&) = delete;
117 Datapath& operator=(const Datapath&) = delete;
118
Garrick Evansf0ab7132019-06-18 14:50:42 +0900119 virtual ~Datapath() = default;
120
Hugo Benichibf811c62020-09-07 17:30:45 +0900121 // Start and stop the Datapath, creating or destroying the initial iptables
122 // setup needed for forwarding traffic from VMs and containers and for
123 // fwmark based routing.
124 virtual void Start();
125 virtual void Stop();
126
Hugo Benichi33860d72020-07-09 16:34:01 +0900127 // Attaches the name |netns_name| to a network namespace identified by
Jie Jiangf6799312021-05-14 16:27:03 +0900128 // |netns_pid|. If |netns_pid| is -1, a new namespace with name |netns_name|
129 // will be created instead. If |netns_name| had already been created, it will
130 // be deleted first.
Hugo Benichi33860d72020-07-09 16:34:01 +0900131 virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
132
133 // Deletes the name |netns_name| of a network namespace.
134 virtual bool NetnsDeleteName(const std::string& netns_name);
135
Garrick Evans8a949dc2019-07-18 16:17:53 +0900136 virtual bool AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900137 uint32_t ipv4_addr,
138 uint32_t ipv4_prefix_len);
Garrick Evans8a949dc2019-07-18 16:17:53 +0900139 virtual void RemoveBridge(const std::string& ifname);
140
Garrick Evans621ed262019-11-13 12:28:43 +0900141 virtual bool AddToBridge(const std::string& br_ifname,
142 const std::string& ifname);
143
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900144 // Adds a new TAP device.
145 // |name| may be empty, in which case a default device name will be used;
146 // it may be a template (e.g. vmtap%d), in which case the kernel will
147 // generate the name; or it may be fully defined. In all cases, upon success,
148 // the function returns the actual name of the interface.
Garrick Evans621ed262019-11-13 12:28:43 +0900149 // |mac_addr| and |ipv4_addr| should be null if this interface will be later
150 // bridged.
Garrick Evans4f9f5572019-11-26 10:25:16 +0900151 // If |user| is empty, no owner will be set
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900152 virtual std::string AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900153 const MacAddress* mac_addr,
154 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900155 const std::string& user);
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900156
157 // |ifname| must be the actual name of the interface.
158 virtual void RemoveTAP(const std::string& ifname);
159
160 // The following are iptables methods.
161 // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d)
162 // IPv4 address (not a CIDR representation).
163
Hugo Benichi76675592020-04-08 14:29:57 +0900164 // Creates a virtual interface pair split across the current namespace and the
165 // namespace corresponding to |pid|, and set up the remote interface
166 // |peer_ifname| according // to the given parameters.
167 virtual bool ConnectVethPair(pid_t pid,
Hugo Benichi33860d72020-07-09 16:34:01 +0900168 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900169 const std::string& veth_ifname,
170 const std::string& peer_ifname,
171 const MacAddress& remote_mac_addr,
172 uint32_t remote_ipv4_addr,
173 uint32_t remote_ipv4_prefix_len,
174 bool remote_multicast_flag);
175
Jason Jeremy Imana183d7a2021-08-06 01:35:40 +0900176 // Disable and re-enable IPv6.
177 virtual void RestartIPv6();
178
Garrick Evans54861622019-07-19 09:05:09 +0900179 virtual void RemoveInterface(const std::string& ifname);
180
Hugo Benichi954bae62021-04-09 09:12:30 +0900181 // Create an OUTPUT DROP rule for any locally originated traffic
Hugo Benichi321f23b2020-09-25 15:42:05 +0900182 // whose src IPv4 matches |src_ip| and would exit |oif|. This is mainly used
183 // for dropping Chrome webRTC traffic incorrectly bound on ARC and other
184 // guests virtual interfaces (chromium:898210).
185 virtual bool AddSourceIPv4DropRule(const std::string& oif,
186 const std::string& src_ip);
Hugo Benichi321f23b2020-09-25 15:42:05 +0900187
Hugo Benichi7c342672020-09-08 09:18:14 +0900188 // Creates a virtual ethernet interface pair shared with the client namespace
Hugo Benichifcf81022020-12-04 11:01:37 +0900189 // of |nsinfo.pid| and sets up routing outside and inside the client namespace
190 // for connecting the client namespace to the network.
191 bool StartRoutingNamespace(const ConnectedNamespace& nsinfo);
Hugo Benichi7c342672020-09-08 09:18:14 +0900192 // Destroys the virtual ethernet interface, routing, and network namespace
Hugo Benichifcf81022020-12-04 11:01:37 +0900193 // name set for |nsinfo.netns_name| by StartRoutingNamespace. The default
194 // route set inside the |nsinfo.netns_name| by patchpanel is not destroyed and
195 // it is assumed the client will teardown the namespace.
196 void StopRoutingNamespace(const ConnectedNamespace& nsinfo);
Hugo Benichi7c342672020-09-08 09:18:14 +0900197
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900198 // Start or stop DNS traffic redirection to DNS proxy. The rules created
199 // depend on the type requested.
200 bool StartDnsRedirection(const DnsRedirectionRule& rule);
201 void StopDnsRedirection(const DnsRedirectionRule& rule);
202
Hugo Benichi8d622b52020-08-13 15:24:12 +0900203 // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given
Hugo Benichi600a9432021-07-06 11:57:29 +0900204 // downstream network interface |int_ifname| associated to |source|. if
205 // |ext_ifname| is empty, traffic from the downstream interface is implicitly
206 // routed through the highest priority physical network when |route_on_vpn| is
207 // false, or through the highest priority logical network when |route_on_vpn|
208 // is true. If |ext_ifname| is defined, traffic from the downstream interface
209 // is routed to |ext_ifname| and |route_on_vpn| is ignored. If |int_ifname| is
210 // associated to a connected namespace and a VPN is connected, an additional
211 // IPv4 VPN fwmark tagging bypass rule is needed to allow return traffic to
212 // reach to the IPv4 local source. |peer_ipv4_addr| is the address of the
213 // interface inside the connected namespace needed to create this rule. If
214 // |peer_ipv4_addr| is 0, no additional rule will be added.
Hugo Benichi8d622b52020-08-13 15:24:12 +0900215 virtual void StartRoutingDevice(const std::string& ext_ifname,
216 const std::string& int_ifname,
217 uint32_t int_ipv4_addr,
Hugo Benichi93306e52020-12-04 16:08:00 +0900218 TrafficSource source,
Jason Jeremy Iman72e61102021-04-23 03:37:14 +0900219 bool route_on_vpn,
220 uint32_t peer_ipv4_addr = 0);
Hugo Benichi8d622b52020-08-13 15:24:12 +0900221
222 // Removes IPv4 iptables, IP forwarding, and traffic marking for the given
Hugo Benichi600a9432021-07-06 11:57:29 +0900223 // downstream network interface |int_ifname|.
Hugo Benichi8d622b52020-08-13 15:24:12 +0900224 virtual void StopRoutingDevice(const std::string& ext_ifname,
225 const std::string& int_ifname,
226 uint32_t int_ipv4_addr,
Hugo Benichi93306e52020-12-04 16:08:00 +0900227 TrafficSource source,
228 bool route_on_vpn);
Hugo Benichi8d622b52020-08-13 15:24:12 +0900229
Hugo Benichi76be34a2020-08-26 22:35:54 +0900230 // Starts or stops marking conntrack entries routed to |ext_ifname| with its
231 // associated fwmark routing tag. Once a conntrack entry is marked with the
Hugo Benichi600a9432021-07-06 11:57:29 +0900232 // fwmark routing tag of an upstream network interface, the connection will be
233 // pinned to that network interface if conntrack fwmark restore is set for the
234 // source.
Hugo Benichi76be34a2020-08-26 22:35:54 +0900235 virtual void StartConnectionPinning(const std::string& ext_ifname);
236 virtual void StopConnectionPinning(const std::string& ext_ifname);
Hugo Benichi2a940542020-10-26 18:50:49 +0900237 // Starts or stops VPN routing for:
Hugo Benichi600a9432021-07-06 11:57:29 +0900238 // - Local traffic from sockets of binaries running under uids eligible to be
239 // routed
Hugo Benichi2a940542020-10-26 18:50:49 +0900240 // through VPN connections. These uids are defined by |kLocalSourceTypes|
241 // in routing_service.h
Hugo Benichi600a9432021-07-06 11:57:29 +0900242 // - Forwarded traffic from downstream network interfaces tracking the
243 // default network.
Hugo Benichi2a940542020-10-26 18:50:49 +0900244 virtual void StartVpnRouting(const std::string& vpn_ifname);
245 virtual void StopVpnRouting(const std::string& vpn_ifname);
Hugo Benichi76be34a2020-08-26 22:35:54 +0900246
Hugo Benichibb38bdd2021-05-14 10:36:11 +0900247 // Starts and stops VPN lockdown mode. When patchpanel VPN lockdown is enabled
248 // and no VPN connection exists, any non-ARC traffic that would be routed to a
249 // VPN connection is instead rejected in iptables. ARC traffic is ignored
250 // because Android already implements VPN lockdown.
251 virtual void SetVpnLockdown(bool enable_vpn_lockdown);
252
Taoyu Li90c13912019-11-26 17:56:54 +0900253 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900254 virtual bool MaskInterfaceFlags(const std::string& ifname,
255 uint16_t on,
256 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900257
Garrick Evans260ff302019-07-25 11:22:50 +0900258 virtual bool AddIPv6HostRoute(const std::string& ifname,
259 const std::string& ipv6_addr,
260 int ipv6_prefix_len);
261 virtual void RemoveIPv6HostRoute(const std::string& ifname,
262 const std::string& ipv6_addr,
263 int ipv6_prefix_len);
264
Taoyu Lia0727dc2020-09-24 19:54:59 +0900265 virtual bool AddIPv6Address(const std::string& ifname,
266 const std::string& ipv6_addr);
267 virtual void RemoveIPv6Address(const std::string& ifname,
268 const std::string& ipv6_addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900269
Hugo Benichie8758b52020-04-03 14:49:01 +0900270 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
271 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900272 virtual bool AddIPv4Route(uint32_t gateway_addr,
273 uint32_t addr,
274 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900275 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
276 uint32_t addr,
277 uint32_t netmask);
278 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
279 // subnet defined by |addr| and |netmask|.
280 virtual bool AddIPv4Route(const std::string& ifname,
281 uint32_t addr,
282 uint32_t netmask);
283 virtual bool DeleteIPv4Route(const std::string& ifname,
284 uint32_t addr,
285 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900286
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900287 // Adds (or deletes) an iptables rule for ADB port forwarding.
288 virtual bool AddAdbPortForwardRule(const std::string& ifname);
289 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
290
291 // Adds (or deletes) an iptables rule for ADB port access.
292 virtual bool AddAdbPortAccessRule(const std::string& ifname);
293 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
294
Damien Dejean40e15982021-05-21 07:11:53 +0000295 // Enables or disables netfilter conntrack helpers.
296 virtual bool SetConntrackHelpers(bool enable_helpers);
Hugo Benichi48ca4eb2021-06-03 15:42:42 +0900297 // Allows (or stops allowing) loopback IPv4 addresses as valid sources or
298 // destinations during IPv4 routing for |ifname|. This lets connections
299 // originated from guests like ARC or Crostini be accepted on the host and
300 // should be used carefully in conjunction with firewall port access rules to
301 // only allow very specific connection patterns.
302 virtual bool SetRouteLocalnet(const std::string& ifname, bool enable);
303 // Adds all |modules| into the kernel using modprobe.
304 virtual bool ModprobeAll(const std::vector<std::string>& modules);
Damien Dejean40e15982021-05-21 07:11:53 +0000305
Hugo Benichi62fd1252021-08-19 16:55:54 +0900306 // Create (or delete) DNAT rules for sending unsollicited traffic inbound on
307 // interface |ifname| to |ipv4_addr|. This is used for implementing
308 // transparent ARC inbound connections to Android Apps listening on the
309 // network.
310 virtual void AddInboundIPv4DNAT(const std::string& ifname,
311 const std::string& ipv4_addr);
312 virtual void RemoveInboundIPv4DNAT(const std::string& ifname,
313 const std::string& ipv4_addr);
314
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900315 // Create (or delete) DNAT rules for redirecting DNS queries from system
316 // services to the nameservers of a particular physical networks. These
317 // DNAT rules are only applied if a VPN is connected and allows system
318 // services to resolve hostnames even if a VPN application configures DNS
319 // addresses only routable through the VPN (b/178331695).
320 // TODO(b/171157837) Replaces these rules with the system DNS proxy.
321 bool AddRedirectDnsRule(const std::string& ifname,
322 const std::string dns_ipv4_addr);
323 bool RemoveRedirectDnsRule(const std::string& ifname);
324
Hugo Benichif0f55562021-04-02 15:25:02 +0900325 // Add, remove, or flush chain |chain| in table |table|.
326 bool AddChain(IpFamily family,
327 const std::string& table,
328 const std::string& name);
329 bool RemoveChain(IpFamily family,
330 const std::string& table,
331 const std::string& name);
332 bool FlushChain(IpFamily family,
333 const std::string& table,
334 const std::string& name);
Hugo Benichicd27f4e2020-11-19 18:32:23 +0900335 // Manipulates a chain |chain| in table |table|.
Hugo Benichia2ed4432021-06-08 00:21:49 +0900336 virtual bool ModifyChain(IpFamily family,
337 const std::string& table,
338 const std::string& op,
339 const std::string& chain,
340 bool log_failures = true);
Hugo Benichiddf00842020-11-20 10:24:08 +0900341 // Sends an iptables command for table |table|.
Hugo Benichia2ed4432021-06-08 00:21:49 +0900342 virtual bool ModifyIptables(IpFamily family,
343 const std::string& table,
344 const std::vector<std::string>& argv,
345 bool log_failures = true);
Hugo Benichi48ca4eb2021-06-03 15:42:42 +0900346 // Dumps the iptables chains rules for the table |table|. |family| must be
347 // either IPv4 or IPv6.
348 virtual std::string DumpIptables(IpFamily family, const std::string& table);
Garrick Evans260ff302019-07-25 11:22:50 +0900349
Hugo Benichi283a7812021-06-08 00:47:54 +0900350 // Changes firewall rules based on |request|, allowing ingress traffic to a
351 // port, forwarding ingress traffic to a port into ARC or Crostini, or
352 // restricting localhost ports for listen(). This function corresponds to
353 // the ModifyPortRule method of patchpanel DBus API.
354 virtual bool ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request);
355
Garrick Evansf0ab7132019-06-18 14:50:42 +0900356 private:
Hugo Benichi91ee09f2020-12-03 22:24:22 +0900357 // Attempts to flush all built-in iptables chains used by patchpanel, and to
358 // delete all additionals chains created by patchpanel for routing. Traffic
359 // accounting chains are not deleted.
360 void ResetIptables();
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900361 // Creates a virtual interface pair.
362 bool AddVirtualInterfacePair(const std::string& netns_name,
363 const std::string& veth_ifname,
364 const std::string& peer_ifname);
365 // Sets the configuration of an interface.
366 bool ConfigureInterface(const std::string& ifname,
367 const MacAddress& mac_addr,
368 uint32_t ipv4_addr,
369 uint32_t ipv4_prefix_len,
370 bool up,
371 bool enable_multicast);
372 // Sets the link status.
373 bool ToggleInterface(const std::string& ifname, bool up);
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900374 bool ModifyChromeDnsRedirect(IpFamily family,
375 const DnsRedirectionRule& rule,
376 const std::string& op);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900377 bool ModifyRedirectDnsDNATRule(const std::string& op,
378 const std::string& protocol,
379 const std::string& ifname,
380 const std::string& dns_ipv4_addr);
Jason Jeremy Iman1d0544c2021-11-05 12:10:34 +0900381 bool ModifyDnsProxyMasquerade(IpFamily family,
382 const std::string& op,
383 const std::string& chain);
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900384 bool ModifyRedirectDnsJumpRule(IpFamily family,
385 const std::string& op,
386 const std::string& chain,
387 const std::string& ifname,
388 const std::string& target_chain,
389 Fwmark mark = {},
390 Fwmark mask = {},
391 bool redirect_on_mark = false);
392 bool ModifyDnsRedirectionSkipVpnRule(IpFamily family, const std::string& op);
393
394 // Create (or delete) DNAT rules for redirecting DNS queries to a DNS proxy.
395 bool ModifyDnsProxyDNAT(IpFamily family,
396 const DnsRedirectionRule& rule,
397 const std::string& op,
398 const std::string& ifname,
399 const std::string& chain);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900400
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900401 bool ModifyConnmarkSet(IpFamily family,
402 const std::string& chain,
403 const std::string& op,
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900404 Fwmark mark,
405 Fwmark mask);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900406 bool ModifyConnmarkRestore(IpFamily family,
407 const std::string& chain,
408 const std::string& op,
Hugo Benichi1af52392020-11-27 18:09:32 +0900409 const std::string& iif,
410 Fwmark mask);
411 bool ModifyConnmarkSave(IpFamily family,
412 const std::string& chain,
413 const std::string& op,
Hugo Benichi1af52392020-11-27 18:09:32 +0900414 Fwmark mask);
Hugo Benichi2a940542020-10-26 18:50:49 +0900415 bool ModifyFwmarkRoutingTag(const std::string& chain,
416 const std::string& op,
Hugo Benichid872d3d2021-03-29 10:20:53 +0900417 Fwmark routing_mark);
418 bool ModifyFwmarkSourceTag(const std::string& chain,
419 const std::string& op,
Hugo Benichi9be19b12020-08-14 15:33:40 +0900420 TrafficSource source);
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900421 bool ModifyFwmarkDefaultLocalSourceTag(const std::string& op,
422 TrafficSource source);
423 bool ModifyFwmarkLocalSourceTag(const std::string& op,
424 const LocalSourceSpecs& source);
425 bool ModifyFwmark(IpFamily family,
426 const std::string& chain,
427 const std::string& op,
428 const std::string& iif,
429 const std::string& uid_name,
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900430 uint32_t classid,
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900431 Fwmark mark,
432 Fwmark mask,
433 bool log_failures = true);
Hugo Benichiff3cbcf2021-04-03 00:22:06 +0900434 bool ModifyJumpRule(IpFamily family,
435 const std::string& table,
436 const std::string& op,
437 const std::string& chain,
438 const std::string& target,
439 const std::string& iif,
440 const std::string& oif,
441 bool log_failures = true);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900442 bool ModifyFwmarkVpnJumpRule(const std::string& chain,
443 const std::string& op,
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900444 Fwmark mark,
445 Fwmark mask);
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900446 bool ModifyFwmarkSkipVpnJumpRule(const std::string& chain,
447 const std::string& op,
Hugo Benichi5fbb9362021-07-09 15:41:33 +0900448 const std::string& uid,
449 bool log_failures = true);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900450 bool ModifyRtentry(ioctl_req_t op, struct rtentry* route);
Hugo Benichid82d8832020-08-14 10:05:03 +0900451
Hugo Benichia2ed4432021-06-08 00:21:49 +0900452 std::unique_ptr<MinijailedProcessRunner> process_runner_;
Hugo Benichi283a7812021-06-08 00:47:54 +0900453 std::unique_ptr<Firewall> firewall_;
Hugo Benichi03be8962022-03-17 13:16:38 +0900454 // Owned by Manager
455 System* system_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900456
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900457 FRIEND_TEST(DatapathTest, AddInboundIPv4DNAT);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900458 FRIEND_TEST(DatapathTest, AddVirtualInterfacePair);
459 FRIEND_TEST(DatapathTest, ConfigureInterface);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900460 FRIEND_TEST(DatapathTest, RemoveInboundIPv4DNAT);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900461 FRIEND_TEST(DatapathTest, RemoveOutboundIPv4SNATMark);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900462 FRIEND_TEST(DatapathTest, ToggleInterface);
463
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900464 // A map used for remembering the interface index of an interface. This
465 // information is necessary when cleaning up iptables fwmark rules that
466 // directly references the interface index. When removing these rules on
467 // an RTM_DELLINK event, the interface index cannot be retrieved anymore.
Hugo Benichi600a9432021-07-06 11:57:29 +0900468 // A new entry is only added when a new upstream network interface appears,
469 // and entries are not removed.
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900470 // TODO(b/161507671) Rely on RoutingService to obtain this information once
471 // shill/routing_table.cc has been migrated to patchpanel.
472 std::map<std::string, int> if_nametoindex_;
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900473
474 // A map used for tracking the primary IPv4 dns address associated to a given
475 // Shill Device known by its interface name. This is used for redirecting
476 // DNS queries of system services when a VPN is connected.
477 std::map<std::string, std::string> physical_dns_addresses_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900478};
479
Garrick Evans3388a032020-03-24 11:25:55 +0900480} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900481
Garrick Evans3388a032020-03-24 11:25:55 +0900482#endif // PATCHPANEL_DATAPATH_H_