blob: f133a76c1ccf62f2770a06c43753cb6522d5c002 [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"
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +090024#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Hugo Benichi8d622b52020-08-13 15:24:12 +090025#include "patchpanel/routing_service.h"
Garrick Evans3388a032020-03-24 11:25:55 +090026#include "patchpanel/subnet.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +090027
Garrick Evans3388a032020-03-24 11:25:55 +090028namespace patchpanel {
Garrick Evansf0ab7132019-06-18 14:50:42 +090029
Hugo Benichifcf81022020-12-04 11:01:37 +090030// Struct holding parameters for Datapath::StartRoutingNamespace requests.
31struct ConnectedNamespace {
Jie Jiangf6799312021-05-14 16:27:03 +090032 // The special pid which indicates this namespace is not attached to an
33 // associated process but should be/was created by `ip netns add`.
34 static constexpr pid_t kNewNetnsPid = -1;
35
Hugo Benichifcf81022020-12-04 11:01:37 +090036 // The pid of the client network namespace.
37 pid_t pid;
38 // The name attached to the client network namespace.
39 std::string netns_name;
Hugo Benichi93306e52020-12-04 16:08:00 +090040 // Source to which traffic from |host_ifname| will be attributed.
41 TrafficSource source;
Hugo Benichifcf81022020-12-04 11:01:37 +090042 // Name of the shill device for routing outbound traffic from the client
43 // namespace. Empty if outbound traffic should be forwarded to the highest
44 // priority network (physical or virtual).
45 std::string outbound_ifname;
Hugo Benichi93306e52020-12-04 16:08:00 +090046 // If |outbound_ifname| is empty and |route_on_vpn| is false, the traffic from
47 // the client namespace will be routed to the highest priority physical
48 // device. If |outbound_ifname| is empty and |route_on_vpn| is true, the
49 // traffic will be routed through VPN connections. If |outbound_ifname|
50 // specifies a valid physical device, |route_on_vpn| is ignored.
51 bool route_on_vpn;
Hugo Benichifcf81022020-12-04 11:01:37 +090052 // Name of the "local" veth device visible on the host namespace.
53 std::string host_ifname;
54 // Name of the "remote" veth device moved into the client namespace.
55 std::string peer_ifname;
56 // IPv4 subnet assigned to the client namespace.
57 std::unique_ptr<Subnet> peer_subnet;
58 // MAC address of the "remote" veth device.
59 MacAddress peer_mac_addr;
60};
61
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +090062struct DnsRedirectionRule {
63 patchpanel::SetDnsRedirectionRuleRequest::RuleType type;
64 std::string input_ifname;
65 std::string proxy_address;
66 std::vector<std::string> nameservers;
67};
68
Hugo Benichifcf81022020-12-04 11:01:37 +090069std::ostream& operator<<(std::ostream& stream,
70 const ConnectedNamespace& nsinfo);
71
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +090072std::ostream& operator<<(std::ostream& stream, const DnsRedirectionRule& rule);
73
Hugo Benichid82d8832020-08-14 10:05:03 +090074// Simple enum of bitmasks used for specifying a set of IP family values.
75enum IpFamily {
76 NONE = 0,
77 IPv4 = 1 << 0,
78 IPv6 = 1 << 1,
Taoyu Lia0727dc2020-09-24 19:54:59 +090079 Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1);
Hugo Benichid82d8832020-08-14 10:05:03 +090080};
81
Taoyu Li90c13912019-11-26 17:56:54 +090082// cros lint will yell to force using int16/int64 instead of long here, however
83// note that unsigned long IS the correct signature for ioctl in Linux kernel -
84// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
Jie Jiang040ad0f2021-05-17 16:50:48 +090085using ioctl_req_t = unsigned long; // NOLINT(runtime/int)
Taoyu Li90c13912019-11-26 17:56:54 +090086typedef int (*ioctl_t)(int, ioctl_req_t, ...);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090087
Garrick Evans54861622019-07-19 09:05:09 +090088// Returns for given interface name the host name of a ARC veth pair.
Garrick Evans2f581a02020-05-11 10:43:35 +090089std::string ArcVethHostName(const std::string& ifname);
Garrick Evans54861622019-07-19 09:05:09 +090090
Garrick Evans8a067562020-05-11 12:47:30 +090091// Returns the ARC bridge interface name for the given interface.
92std::string ArcBridgeName(const std::string& ifname);
93
Garrick Evansf0ab7132019-06-18 14:50:42 +090094// ARC networking data path configuration utility.
Garrick Evans54861622019-07-19 09:05:09 +090095// IPV4 addresses are always specified in singular dotted-form (a.b.c.d)
96// (not in CIDR representation
Garrick Evansf0ab7132019-06-18 14:50:42 +090097class Datapath {
98 public:
Hugo Benichi283a7812021-06-08 00:47:54 +090099 Datapath();
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900100 // Provided for testing only.
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900101 Datapath(MinijailedProcessRunner* process_runner,
102 Firewall* firewall,
103 ioctl_t ioctl_hook);
Qijiang Fan6bc59e12020-11-11 02:51:06 +0900104 Datapath(const Datapath&) = delete;
105 Datapath& operator=(const Datapath&) = delete;
106
Garrick Evansf0ab7132019-06-18 14:50:42 +0900107 virtual ~Datapath() = default;
108
Hugo Benichibf811c62020-09-07 17:30:45 +0900109 // Start and stop the Datapath, creating or destroying the initial iptables
110 // setup needed for forwarding traffic from VMs and containers and for
111 // fwmark based routing.
112 virtual void Start();
113 virtual void Stop();
114
Hugo Benichi33860d72020-07-09 16:34:01 +0900115 // Attaches the name |netns_name| to a network namespace identified by
Jie Jiangf6799312021-05-14 16:27:03 +0900116 // |netns_pid|. If |netns_pid| is -1, a new namespace with name |netns_name|
117 // will be created instead. If |netns_name| had already been created, it will
118 // be deleted first.
Hugo Benichi33860d72020-07-09 16:34:01 +0900119 virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
120
121 // Deletes the name |netns_name| of a network namespace.
122 virtual bool NetnsDeleteName(const std::string& netns_name);
123
Garrick Evans8a949dc2019-07-18 16:17:53 +0900124 virtual bool AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900125 uint32_t ipv4_addr,
126 uint32_t ipv4_prefix_len);
Garrick Evans8a949dc2019-07-18 16:17:53 +0900127 virtual void RemoveBridge(const std::string& ifname);
128
Garrick Evans621ed262019-11-13 12:28:43 +0900129 virtual bool AddToBridge(const std::string& br_ifname,
130 const std::string& ifname);
131
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900132 // Adds a new TAP device.
133 // |name| may be empty, in which case a default device name will be used;
134 // it may be a template (e.g. vmtap%d), in which case the kernel will
135 // generate the name; or it may be fully defined. In all cases, upon success,
136 // the function returns the actual name of the interface.
Garrick Evans621ed262019-11-13 12:28:43 +0900137 // |mac_addr| and |ipv4_addr| should be null if this interface will be later
138 // bridged.
Garrick Evans4f9f5572019-11-26 10:25:16 +0900139 // If |user| is empty, no owner will be set
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900140 virtual std::string AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900141 const MacAddress* mac_addr,
142 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900143 const std::string& user);
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900144
145 // |ifname| must be the actual name of the interface.
146 virtual void RemoveTAP(const std::string& ifname);
147
148 // The following are iptables methods.
149 // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d)
150 // IPv4 address (not a CIDR representation).
151
Hugo Benichi76675592020-04-08 14:29:57 +0900152 // Creates a virtual interface pair split across the current namespace and the
153 // namespace corresponding to |pid|, and set up the remote interface
154 // |peer_ifname| according // to the given parameters.
155 virtual bool ConnectVethPair(pid_t pid,
Hugo Benichi33860d72020-07-09 16:34:01 +0900156 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900157 const std::string& veth_ifname,
158 const std::string& peer_ifname,
159 const MacAddress& remote_mac_addr,
160 uint32_t remote_ipv4_addr,
161 uint32_t remote_ipv4_prefix_len,
162 bool remote_multicast_flag);
163
Garrick Evans54861622019-07-19 09:05:09 +0900164 virtual void RemoveInterface(const std::string& ifname);
165
Hugo Benichi954bae62021-04-09 09:12:30 +0900166 // Create an OUTPUT DROP rule for any locally originated traffic
Hugo Benichi321f23b2020-09-25 15:42:05 +0900167 // whose src IPv4 matches |src_ip| and would exit |oif|. This is mainly used
168 // for dropping Chrome webRTC traffic incorrectly bound on ARC and other
169 // guests virtual interfaces (chromium:898210).
170 virtual bool AddSourceIPv4DropRule(const std::string& oif,
171 const std::string& src_ip);
Hugo Benichi321f23b2020-09-25 15:42:05 +0900172
Hugo Benichi7c342672020-09-08 09:18:14 +0900173 // Creates a virtual ethernet interface pair shared with the client namespace
Hugo Benichifcf81022020-12-04 11:01:37 +0900174 // of |nsinfo.pid| and sets up routing outside and inside the client namespace
175 // for connecting the client namespace to the network.
176 bool StartRoutingNamespace(const ConnectedNamespace& nsinfo);
Hugo Benichi7c342672020-09-08 09:18:14 +0900177 // Destroys the virtual ethernet interface, routing, and network namespace
Hugo Benichifcf81022020-12-04 11:01:37 +0900178 // name set for |nsinfo.netns_name| by StartRoutingNamespace. The default
179 // route set inside the |nsinfo.netns_name| by patchpanel is not destroyed and
180 // it is assumed the client will teardown the namespace.
181 void StopRoutingNamespace(const ConnectedNamespace& nsinfo);
Hugo Benichi7c342672020-09-08 09:18:14 +0900182
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900183 // Start or stop DNS traffic redirection to DNS proxy. The rules created
184 // depend on the type requested.
185 bool StartDnsRedirection(const DnsRedirectionRule& rule);
186 void StopDnsRedirection(const DnsRedirectionRule& rule);
187
Hugo Benichi8d622b52020-08-13 15:24:12 +0900188 // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given
189 // virtual device |int_ifname| associated to |source|. if |ext_ifname| is
190 // empty, the device is implicitly routed through the highest priority
Hugo Benichibfc49112020-12-14 12:54:44 +0900191 // physical network when |route_on_vpn| is false, or through the highest
192 // priority logical network when |route_on_vpn| is true. If |ext_ifname| is
193 // defined, the device is routed to |ext_ifname| and |route_on_vpn| is
194 // ignored.
Jason Jeremy Iman72e61102021-04-23 03:37:14 +0900195 // If the device is associated to a connected namespace and a VPN is
196 // connected, an additional IPv4 VPN fwmark tagging bypass rule is needed
197 // to allow return traffic to reach to the IPv4 local source. |peer_ipv4_addr|
198 // is the address of the interface inside the connected namespace needed to
199 // create this rule. If |peer_ipv4_addr| is 0, no additional rule will be
200 // added.
Hugo Benichi8d622b52020-08-13 15:24:12 +0900201 virtual void StartRoutingDevice(const std::string& ext_ifname,
202 const std::string& int_ifname,
203 uint32_t int_ipv4_addr,
Hugo Benichi93306e52020-12-04 16:08:00 +0900204 TrafficSource source,
Jason Jeremy Iman72e61102021-04-23 03:37:14 +0900205 bool route_on_vpn,
206 uint32_t peer_ipv4_addr = 0);
Hugo Benichi8d622b52020-08-13 15:24:12 +0900207
208 // Removes IPv4 iptables, IP forwarding, and traffic marking for the given
209 // virtual device |int_ifname|.
210 virtual void StopRoutingDevice(const std::string& ext_ifname,
211 const std::string& int_ifname,
212 uint32_t int_ipv4_addr,
Hugo Benichi93306e52020-12-04 16:08:00 +0900213 TrafficSource source,
214 bool route_on_vpn);
Hugo Benichi8d622b52020-08-13 15:24:12 +0900215
Hugo Benichi76be34a2020-08-26 22:35:54 +0900216 // Starts or stops marking conntrack entries routed to |ext_ifname| with its
217 // associated fwmark routing tag. Once a conntrack entry is marked with the
218 // fwmark routing tag of a external device, the connection will be pinned
219 // to that deviced if conntrack fwmark restore is set for the source.
220 virtual void StartConnectionPinning(const std::string& ext_ifname);
221 virtual void StopConnectionPinning(const std::string& ext_ifname);
Hugo Benichi2a940542020-10-26 18:50:49 +0900222 // Starts or stops VPN routing for:
223 // - Local sockets of binaries running under uids eligible to be routed
224 // through VPN connections. These uids are defined by |kLocalSourceTypes|
225 // in routing_service.h
226 // - Forwarded virtual devices tracking the default network.
227 virtual void StartVpnRouting(const std::string& vpn_ifname);
228 virtual void StopVpnRouting(const std::string& vpn_ifname);
Hugo Benichi76be34a2020-08-26 22:35:54 +0900229
Hugo Benichibb38bdd2021-05-14 10:36:11 +0900230 // Starts and stops VPN lockdown mode. When patchpanel VPN lockdown is enabled
231 // and no VPN connection exists, any non-ARC traffic that would be routed to a
232 // VPN connection is instead rejected in iptables. ARC traffic is ignored
233 // because Android already implements VPN lockdown.
234 virtual void SetVpnLockdown(bool enable_vpn_lockdown);
235
Taoyu Li90c13912019-11-26 17:56:54 +0900236 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900237 virtual bool MaskInterfaceFlags(const std::string& ifname,
238 uint16_t on,
239 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900240
Hugo Benichid82d8832020-08-14 10:05:03 +0900241 // Convenience functions for enabling or disabling IPv6 forwarding in both
242 // directions between a pair of interfaces
Taoyu Li90c13912019-11-26 17:56:54 +0900243 virtual bool AddIPv6Forwarding(const std::string& ifname1,
244 const std::string& ifname2);
245 virtual void RemoveIPv6Forwarding(const std::string& ifname1,
246 const std::string& ifname2);
247
Garrick Evans260ff302019-07-25 11:22:50 +0900248 virtual bool AddIPv6HostRoute(const std::string& ifname,
249 const std::string& ipv6_addr,
250 int ipv6_prefix_len);
251 virtual void RemoveIPv6HostRoute(const std::string& ifname,
252 const std::string& ipv6_addr,
253 int ipv6_prefix_len);
254
Taoyu Lia0727dc2020-09-24 19:54:59 +0900255 virtual bool AddIPv6Address(const std::string& ifname,
256 const std::string& ipv6_addr);
257 virtual void RemoveIPv6Address(const std::string& ifname,
258 const std::string& ipv6_addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900259
Hugo Benichie8758b52020-04-03 14:49:01 +0900260 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
261 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900262 virtual bool AddIPv4Route(uint32_t gateway_addr,
263 uint32_t addr,
264 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900265 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
266 uint32_t addr,
267 uint32_t netmask);
268 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
269 // subnet defined by |addr| and |netmask|.
270 virtual bool AddIPv4Route(const std::string& ifname,
271 uint32_t addr,
272 uint32_t netmask);
273 virtual bool DeleteIPv4Route(const std::string& ifname,
274 uint32_t addr,
275 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900276
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900277 // Adds (or deletes) an iptables rule for ADB port forwarding.
278 virtual bool AddAdbPortForwardRule(const std::string& ifname);
279 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
280
281 // Adds (or deletes) an iptables rule for ADB port access.
282 virtual bool AddAdbPortAccessRule(const std::string& ifname);
283 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
284
Damien Dejean40e15982021-05-21 07:11:53 +0000285 // Enables or disables netfilter conntrack helpers.
286 virtual bool SetConntrackHelpers(bool enable_helpers);
Hugo Benichi48ca4eb2021-06-03 15:42:42 +0900287 // Allows (or stops allowing) loopback IPv4 addresses as valid sources or
288 // destinations during IPv4 routing for |ifname|. This lets connections
289 // originated from guests like ARC or Crostini be accepted on the host and
290 // should be used carefully in conjunction with firewall port access rules to
291 // only allow very specific connection patterns.
292 virtual bool SetRouteLocalnet(const std::string& ifname, bool enable);
293 // Adds all |modules| into the kernel using modprobe.
294 virtual bool ModprobeAll(const std::vector<std::string>& modules);
Damien Dejean40e15982021-05-21 07:11:53 +0000295
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900296 // Create (or delete) DNAT rules for redirecting DNS queries from system
297 // services to the nameservers of a particular physical networks. These
298 // DNAT rules are only applied if a VPN is connected and allows system
299 // services to resolve hostnames even if a VPN application configures DNS
300 // addresses only routable through the VPN (b/178331695).
301 // TODO(b/171157837) Replaces these rules with the system DNS proxy.
302 bool AddRedirectDnsRule(const std::string& ifname,
303 const std::string dns_ipv4_addr);
304 bool RemoveRedirectDnsRule(const std::string& ifname);
305
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900306 // Set or override the interface name to index mapping for |ifname|.
307 // Only used for testing.
308 void SetIfnameIndex(const std::string& ifname, int ifindex);
309
Hugo Benichif0f55562021-04-02 15:25:02 +0900310 // Add, remove, or flush chain |chain| in table |table|.
311 bool AddChain(IpFamily family,
312 const std::string& table,
313 const std::string& name);
314 bool RemoveChain(IpFamily family,
315 const std::string& table,
316 const std::string& name);
317 bool FlushChain(IpFamily family,
318 const std::string& table,
319 const std::string& name);
Hugo Benichicd27f4e2020-11-19 18:32:23 +0900320 // Manipulates a chain |chain| in table |table|.
Hugo Benichia2ed4432021-06-08 00:21:49 +0900321 virtual bool ModifyChain(IpFamily family,
322 const std::string& table,
323 const std::string& op,
324 const std::string& chain,
325 bool log_failures = true);
Hugo Benichiddf00842020-11-20 10:24:08 +0900326 // Sends an iptables command for table |table|.
Hugo Benichia2ed4432021-06-08 00:21:49 +0900327 virtual bool ModifyIptables(IpFamily family,
328 const std::string& table,
329 const std::vector<std::string>& argv,
330 bool log_failures = true);
Hugo Benichi48ca4eb2021-06-03 15:42:42 +0900331 // Dumps the iptables chains rules for the table |table|. |family| must be
332 // either IPv4 or IPv6.
333 virtual std::string DumpIptables(IpFamily family, const std::string& table);
Garrick Evans260ff302019-07-25 11:22:50 +0900334
Hugo Benichi283a7812021-06-08 00:47:54 +0900335 // Changes firewall rules based on |request|, allowing ingress traffic to a
336 // port, forwarding ingress traffic to a port into ARC or Crostini, or
337 // restricting localhost ports for listen(). This function corresponds to
338 // the ModifyPortRule method of patchpanel DBus API.
339 virtual bool ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request);
340
Garrick Evansf0ab7132019-06-18 14:50:42 +0900341 private:
Hugo Benichi91ee09f2020-12-03 22:24:22 +0900342 // Attempts to flush all built-in iptables chains used by patchpanel, and to
343 // delete all additionals chains created by patchpanel for routing. Traffic
344 // accounting chains are not deleted.
345 void ResetIptables();
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900346 // Creates a virtual interface pair.
347 bool AddVirtualInterfacePair(const std::string& netns_name,
348 const std::string& veth_ifname,
349 const std::string& peer_ifname);
350 // Sets the configuration of an interface.
351 bool ConfigureInterface(const std::string& ifname,
352 const MacAddress& mac_addr,
353 uint32_t ipv4_addr,
354 uint32_t ipv4_prefix_len,
355 bool up,
356 bool enable_multicast);
357 // Sets the link status.
358 bool ToggleInterface(const std::string& ifname, bool up);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900359 // Create (or delete) pre-routing rules allowing direct ingress on |ifname|
360 // to guest destination |ipv4_addr|.
361 bool AddInboundIPv4DNAT(const std::string& ifname,
362 const std::string& ipv4_addr);
363 void RemoveInboundIPv4DNAT(const std::string& ifname,
364 const std::string& ipv4_addr);
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900365 bool ModifyChromeDnsRedirect(IpFamily family,
366 const DnsRedirectionRule& rule,
367 const std::string& op);
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900368 bool ModifyRedirectDnsDNATRule(const std::string& op,
369 const std::string& protocol,
370 const std::string& ifname,
371 const std::string& dns_ipv4_addr);
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900372 bool ModifyRedirectDnsJumpRule(IpFamily family,
373 const std::string& op,
374 const std::string& chain,
375 const std::string& ifname,
376 const std::string& target_chain,
377 Fwmark mark = {},
378 Fwmark mask = {},
379 bool redirect_on_mark = false);
380 bool ModifyDnsRedirectionSkipVpnRule(IpFamily family, const std::string& op);
381
382 // Create (or delete) DNAT rules for redirecting DNS queries to a DNS proxy.
383 bool ModifyDnsProxyDNAT(IpFamily family,
384 const DnsRedirectionRule& rule,
385 const std::string& op,
386 const std::string& ifname,
387 const std::string& chain);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900388
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900389 bool ModifyConnmarkSet(IpFamily family,
390 const std::string& chain,
391 const std::string& op,
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900392 Fwmark mark,
393 Fwmark mask);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900394 bool ModifyConnmarkRestore(IpFamily family,
395 const std::string& chain,
396 const std::string& op,
Hugo Benichi1af52392020-11-27 18:09:32 +0900397 const std::string& iif,
398 Fwmark mask);
399 bool ModifyConnmarkSave(IpFamily family,
400 const std::string& chain,
401 const std::string& op,
Hugo Benichi1af52392020-11-27 18:09:32 +0900402 Fwmark mask);
Hugo Benichi2a940542020-10-26 18:50:49 +0900403 bool ModifyFwmarkRoutingTag(const std::string& chain,
404 const std::string& op,
Hugo Benichid872d3d2021-03-29 10:20:53 +0900405 Fwmark routing_mark);
406 bool ModifyFwmarkSourceTag(const std::string& chain,
407 const std::string& op,
Hugo Benichi9be19b12020-08-14 15:33:40 +0900408 TrafficSource source);
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900409 bool ModifyFwmarkDefaultLocalSourceTag(const std::string& op,
410 TrafficSource source);
411 bool ModifyFwmarkLocalSourceTag(const std::string& op,
412 const LocalSourceSpecs& source);
413 bool ModifyFwmark(IpFamily family,
414 const std::string& chain,
415 const std::string& op,
416 const std::string& iif,
417 const std::string& uid_name,
Hugo Benichi7e3b1fc2020-11-19 15:47:05 +0900418 uint32_t classid,
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900419 Fwmark mark,
420 Fwmark mask,
421 bool log_failures = true);
Hugo Benichid82d8832020-08-14 10:05:03 +0900422 bool ModifyIpForwarding(IpFamily family,
423 const std::string& op,
424 const std::string& iif,
425 const std::string& oif,
426 bool log_failures = true);
Hugo Benichiff3cbcf2021-04-03 00:22:06 +0900427 bool ModifyJumpRule(IpFamily family,
428 const std::string& table,
429 const std::string& op,
430 const std::string& chain,
431 const std::string& target,
432 const std::string& iif,
433 const std::string& oif,
434 bool log_failures = true);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900435 bool ModifyFwmarkVpnJumpRule(const std::string& chain,
436 const std::string& op,
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900437 Fwmark mark,
438 Fwmark mask);
Jason Jeremy Imanf4fb64f2021-04-20 21:54:19 +0900439 bool ModifyFwmarkSkipVpnJumpRule(const std::string& chain,
440 const std::string& op,
441 const std::string& uid);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900442 bool ModifyRtentry(ioctl_req_t op, struct rtentry* route);
Hugo Benichi8c526e92021-03-25 14:59:59 +0900443 // Uses if_nametoindex to return the interface index of |ifname|. If |ifname|
444 // does not exist anymore, looks up the cache |if_nametoindex_|. It is
445 // incorrect to use this function in situations where the interface has been
446 // recreated and the older value must be recovered (b/183679000).
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900447 int FindIfIndex(const std::string& ifname);
Hugo Benichid82d8832020-08-14 10:05:03 +0900448
Hugo Benichia2ed4432021-06-08 00:21:49 +0900449 std::unique_ptr<MinijailedProcessRunner> process_runner_;
Hugo Benichi283a7812021-06-08 00:47:54 +0900450 std::unique_ptr<Firewall> firewall_;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900451 ioctl_t ioctl_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900452
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900453 FRIEND_TEST(DatapathTest, AddInboundIPv4DNAT);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900454 FRIEND_TEST(DatapathTest, AddVirtualInterfacePair);
455 FRIEND_TEST(DatapathTest, ConfigureInterface);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900456 FRIEND_TEST(DatapathTest, RemoveInboundIPv4DNAT);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900457 FRIEND_TEST(DatapathTest, RemoveOutboundIPv4SNATMark);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900458 FRIEND_TEST(DatapathTest, ToggleInterface);
459
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900460 // A map used for remembering the interface index of an interface. This
461 // information is necessary when cleaning up iptables fwmark rules that
462 // directly references the interface index. When removing these rules on
463 // an RTM_DELLINK event, the interface index cannot be retrieved anymore.
464 // A new entry is only added when a new physical device appears, and entries
465 // are not removed.
466 // TODO(b/161507671) Rely on RoutingService to obtain this information once
467 // shill/routing_table.cc has been migrated to patchpanel.
468 std::map<std::string, int> if_nametoindex_;
Hugo Benichi1e0656f2021-02-15 15:43:38 +0900469
470 // A map used for tracking the primary IPv4 dns address associated to a given
471 // Shill Device known by its interface name. This is used for redirecting
472 // DNS queries of system services when a VPN is connected.
473 std::map<std::string, std::string> physical_dns_addresses_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900474};
475
Garrick Evans3388a032020-03-24 11:25:55 +0900476} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900477
Garrick Evans3388a032020-03-24 11:25:55 +0900478#endif // PATCHPANEL_DATAPATH_H_