Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 1 | // 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_DATAPATH_H_ |
| 6 | #define PATCHPANEL_DATAPATH_H_ |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 7 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 8 | #include <net/route.h> |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 9 | #include <sys/types.h> |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 10 | |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 11 | #include <iostream> |
Hugo Benichi | bb38bdd | 2021-05-14 10:36:11 +0900 | [diff] [blame] | 12 | #include <map> |
| 13 | #include <memory> |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 14 | #include <set> |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 15 | #include <string> |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 16 | #include <vector> |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 17 | |
| 18 | #include <base/macros.h> |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 19 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 20 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 21 | #include "patchpanel/firewall.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 22 | #include "patchpanel/mac_address_generator.h" |
| 23 | #include "patchpanel/minijailed_process_runner.h" |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 24 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 25 | #include "patchpanel/routing_service.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 26 | #include "patchpanel/subnet.h" |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 27 | #include "patchpanel/system.h" |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 28 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 29 | namespace patchpanel { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 30 | |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 31 | // Struct holding parameters for Datapath::StartRoutingNamespace requests. |
| 32 | struct ConnectedNamespace { |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 33 | // The special pid which indicates this namespace is not attached to an |
| 34 | // associated process but should be/was created by `ip netns add`. |
| 35 | static constexpr pid_t kNewNetnsPid = -1; |
| 36 | |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 37 | // The pid of the client network namespace. |
| 38 | pid_t pid; |
| 39 | // The name attached to the client network namespace. |
| 40 | std::string netns_name; |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 41 | // Source to which traffic from |host_ifname| will be attributed. |
| 42 | TrafficSource source; |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 43 | // Name of the shill device for routing outbound traffic from the client |
| 44 | // namespace. Empty if outbound traffic should be forwarded to the highest |
| 45 | // priority network (physical or virtual). |
| 46 | std::string outbound_ifname; |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 47 | // If |outbound_ifname| is empty and |route_on_vpn| is false, the traffic from |
| 48 | // the client namespace will be routed to the highest priority physical |
| 49 | // device. If |outbound_ifname| is empty and |route_on_vpn| is true, the |
| 50 | // traffic will be routed through VPN connections. If |outbound_ifname| |
| 51 | // specifies a valid physical device, |route_on_vpn| is ignored. |
| 52 | bool route_on_vpn; |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 53 | // Name of the "local" veth device visible on the host namespace. |
| 54 | std::string host_ifname; |
| 55 | // Name of the "remote" veth device moved into the client namespace. |
| 56 | std::string peer_ifname; |
| 57 | // IPv4 subnet assigned to the client namespace. |
| 58 | std::unique_ptr<Subnet> peer_subnet; |
| 59 | // MAC address of the "remote" veth device. |
| 60 | MacAddress peer_mac_addr; |
| 61 | }; |
| 62 | |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 63 | struct DnsRedirectionRule { |
| 64 | patchpanel::SetDnsRedirectionRuleRequest::RuleType type; |
| 65 | std::string input_ifname; |
| 66 | std::string proxy_address; |
| 67 | std::vector<std::string> nameservers; |
| 68 | }; |
| 69 | |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 70 | std::ostream& operator<<(std::ostream& stream, |
| 71 | const ConnectedNamespace& nsinfo); |
| 72 | |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 73 | std::ostream& operator<<(std::ostream& stream, const DnsRedirectionRule& rule); |
| 74 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 75 | // Simple enum of bitmasks used for specifying a set of IP family values. |
| 76 | enum IpFamily { |
| 77 | NONE = 0, |
| 78 | IPv4 = 1 << 0, |
| 79 | IPv6 = 1 << 1, |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 80 | Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1); |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 81 | }; |
| 82 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 83 | // Returns for given interface name the host name of a ARC veth pair. |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 84 | std::string ArcVethHostName(const std::string& ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 85 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 86 | // Returns the ARC bridge interface name for the given interface. |
| 87 | std::string ArcBridgeName(const std::string& ifname); |
| 88 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 89 | // ARC networking data path configuration utility. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 90 | // IPV4 addresses are always specified in singular dotted-form (a.b.c.d) |
| 91 | // (not in CIDR representation |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 92 | class Datapath { |
| 93 | public: |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame] | 94 | Datapath(); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 95 | // Provided for testing only. |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 96 | Datapath(MinijailedProcessRunner* process_runner, |
| 97 | Firewall* firewall, |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 98 | System* system); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 99 | Datapath(const Datapath&) = delete; |
| 100 | Datapath& operator=(const Datapath&) = delete; |
| 101 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 102 | virtual ~Datapath() = default; |
| 103 | |
Hugo Benichi | bf811c6 | 2020-09-07 17:30:45 +0900 | [diff] [blame] | 104 | // Start and stop the Datapath, creating or destroying the initial iptables |
| 105 | // setup needed for forwarding traffic from VMs and containers and for |
| 106 | // fwmark based routing. |
| 107 | virtual void Start(); |
| 108 | virtual void Stop(); |
| 109 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 110 | // Attaches the name |netns_name| to a network namespace identified by |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 111 | // |netns_pid|. If |netns_pid| is -1, a new namespace with name |netns_name| |
| 112 | // will be created instead. If |netns_name| had already been created, it will |
| 113 | // be deleted first. |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 114 | virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid); |
| 115 | |
| 116 | // Deletes the name |netns_name| of a network namespace. |
| 117 | virtual bool NetnsDeleteName(const std::string& netns_name); |
| 118 | |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 119 | virtual bool AddBridge(const std::string& ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 120 | uint32_t ipv4_addr, |
| 121 | uint32_t ipv4_prefix_len); |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 122 | virtual void RemoveBridge(const std::string& ifname); |
| 123 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 124 | virtual bool AddToBridge(const std::string& br_ifname, |
| 125 | const std::string& ifname); |
| 126 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 127 | // Adds a new TAP device. |
| 128 | // |name| may be empty, in which case a default device name will be used; |
| 129 | // it may be a template (e.g. vmtap%d), in which case the kernel will |
| 130 | // generate the name; or it may be fully defined. In all cases, upon success, |
| 131 | // the function returns the actual name of the interface. |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 132 | // |mac_addr| and |ipv4_addr| should be null if this interface will be later |
| 133 | // bridged. |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 134 | // If |user| is empty, no owner will be set |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 135 | virtual std::string AddTAP(const std::string& name, |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 136 | const MacAddress* mac_addr, |
| 137 | const SubnetAddress* ipv4_addr, |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 138 | const std::string& user); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 139 | |
| 140 | // |ifname| must be the actual name of the interface. |
| 141 | virtual void RemoveTAP(const std::string& ifname); |
| 142 | |
| 143 | // The following are iptables methods. |
| 144 | // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d) |
| 145 | // IPv4 address (not a CIDR representation). |
| 146 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 147 | // Creates a virtual interface pair split across the current namespace and the |
| 148 | // namespace corresponding to |pid|, and set up the remote interface |
| 149 | // |peer_ifname| according // to the given parameters. |
| 150 | virtual bool ConnectVethPair(pid_t pid, |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 151 | const std::string& netns_name, |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 152 | const std::string& veth_ifname, |
| 153 | const std::string& peer_ifname, |
| 154 | const MacAddress& remote_mac_addr, |
| 155 | uint32_t remote_ipv4_addr, |
| 156 | uint32_t remote_ipv4_prefix_len, |
| 157 | bool remote_multicast_flag); |
| 158 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 159 | virtual void RemoveInterface(const std::string& ifname); |
| 160 | |
Hugo Benichi | 954bae6 | 2021-04-09 09:12:30 +0900 | [diff] [blame] | 161 | // Create an OUTPUT DROP rule for any locally originated traffic |
Hugo Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 162 | // whose src IPv4 matches |src_ip| and would exit |oif|. This is mainly used |
| 163 | // for dropping Chrome webRTC traffic incorrectly bound on ARC and other |
| 164 | // guests virtual interfaces (chromium:898210). |
| 165 | virtual bool AddSourceIPv4DropRule(const std::string& oif, |
| 166 | const std::string& src_ip); |
Hugo Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 167 | |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 168 | // Creates a virtual ethernet interface pair shared with the client namespace |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 169 | // of |nsinfo.pid| and sets up routing outside and inside the client namespace |
| 170 | // for connecting the client namespace to the network. |
| 171 | bool StartRoutingNamespace(const ConnectedNamespace& nsinfo); |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 172 | // Destroys the virtual ethernet interface, routing, and network namespace |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 173 | // name set for |nsinfo.netns_name| by StartRoutingNamespace. The default |
| 174 | // route set inside the |nsinfo.netns_name| by patchpanel is not destroyed and |
| 175 | // it is assumed the client will teardown the namespace. |
| 176 | void StopRoutingNamespace(const ConnectedNamespace& nsinfo); |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 177 | |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 178 | // Start or stop DNS traffic redirection to DNS proxy. The rules created |
| 179 | // depend on the type requested. |
| 180 | bool StartDnsRedirection(const DnsRedirectionRule& rule); |
| 181 | void StopDnsRedirection(const DnsRedirectionRule& rule); |
| 182 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 183 | // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given |
| 184 | // virtual device |int_ifname| associated to |source|. if |ext_ifname| is |
| 185 | // empty, the device is implicitly routed through the highest priority |
Hugo Benichi | bfc4911 | 2020-12-14 12:54:44 +0900 | [diff] [blame] | 186 | // physical network when |route_on_vpn| is false, or through the highest |
| 187 | // priority logical network when |route_on_vpn| is true. If |ext_ifname| is |
| 188 | // defined, the device is routed to |ext_ifname| and |route_on_vpn| is |
| 189 | // ignored. |
Jason Jeremy Iman | 72e6110 | 2021-04-23 03:37:14 +0900 | [diff] [blame] | 190 | // If the device is associated to a connected namespace and a VPN is |
| 191 | // connected, an additional IPv4 VPN fwmark tagging bypass rule is needed |
| 192 | // to allow return traffic to reach to the IPv4 local source. |peer_ipv4_addr| |
| 193 | // is the address of the interface inside the connected namespace needed to |
| 194 | // create this rule. If |peer_ipv4_addr| is 0, no additional rule will be |
| 195 | // added. |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 196 | virtual void StartRoutingDevice(const std::string& ext_ifname, |
| 197 | const std::string& int_ifname, |
| 198 | uint32_t int_ipv4_addr, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 199 | TrafficSource source, |
Jason Jeremy Iman | 72e6110 | 2021-04-23 03:37:14 +0900 | [diff] [blame] | 200 | bool route_on_vpn, |
| 201 | uint32_t peer_ipv4_addr = 0); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 202 | |
| 203 | // Removes IPv4 iptables, IP forwarding, and traffic marking for the given |
| 204 | // virtual device |int_ifname|. |
| 205 | virtual void StopRoutingDevice(const std::string& ext_ifname, |
| 206 | const std::string& int_ifname, |
| 207 | uint32_t int_ipv4_addr, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 208 | TrafficSource source, |
| 209 | bool route_on_vpn); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 210 | |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 211 | // Starts or stops marking conntrack entries routed to |ext_ifname| with its |
| 212 | // associated fwmark routing tag. Once a conntrack entry is marked with the |
| 213 | // fwmark routing tag of a external device, the connection will be pinned |
| 214 | // to that deviced if conntrack fwmark restore is set for the source. |
| 215 | virtual void StartConnectionPinning(const std::string& ext_ifname); |
| 216 | virtual void StopConnectionPinning(const std::string& ext_ifname); |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 217 | // Starts or stops VPN routing for: |
| 218 | // - Local sockets of binaries running under uids eligible to be routed |
| 219 | // through VPN connections. These uids are defined by |kLocalSourceTypes| |
| 220 | // in routing_service.h |
| 221 | // - Forwarded virtual devices tracking the default network. |
| 222 | virtual void StartVpnRouting(const std::string& vpn_ifname); |
| 223 | virtual void StopVpnRouting(const std::string& vpn_ifname); |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 224 | |
Hugo Benichi | bb38bdd | 2021-05-14 10:36:11 +0900 | [diff] [blame] | 225 | // Starts and stops VPN lockdown mode. When patchpanel VPN lockdown is enabled |
| 226 | // and no VPN connection exists, any non-ARC traffic that would be routed to a |
| 227 | // VPN connection is instead rejected in iptables. ARC traffic is ignored |
| 228 | // because Android already implements VPN lockdown. |
| 229 | virtual void SetVpnLockdown(bool enable_vpn_lockdown); |
| 230 | |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 231 | // Methods supporting IPv6 configuration for ARC. |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 232 | virtual bool MaskInterfaceFlags(const std::string& ifname, |
| 233 | uint16_t on, |
| 234 | uint16_t off = 0); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 235 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 236 | // Convenience functions for enabling or disabling IPv6 forwarding in both |
| 237 | // directions between a pair of interfaces |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 238 | virtual bool AddIPv6Forwarding(const std::string& ifname1, |
| 239 | const std::string& ifname2); |
| 240 | virtual void RemoveIPv6Forwarding(const std::string& ifname1, |
| 241 | const std::string& ifname2); |
| 242 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 243 | virtual bool AddIPv6HostRoute(const std::string& ifname, |
| 244 | const std::string& ipv6_addr, |
| 245 | int ipv6_prefix_len); |
| 246 | virtual void RemoveIPv6HostRoute(const std::string& ifname, |
| 247 | const std::string& ipv6_addr, |
| 248 | int ipv6_prefix_len); |
| 249 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 250 | virtual bool AddIPv6Address(const std::string& ifname, |
| 251 | const std::string& ipv6_addr); |
| 252 | virtual void RemoveIPv6Address(const std::string& ifname, |
| 253 | const std::string& ipv6_addr); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 254 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 255 | // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined |
| 256 | // to the subnet defined by |addr| and |netmask|. |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 257 | virtual bool AddIPv4Route(uint32_t gateway_addr, |
| 258 | uint32_t addr, |
| 259 | uint32_t netmask); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 260 | virtual bool DeleteIPv4Route(uint32_t gateway_addr, |
| 261 | uint32_t addr, |
| 262 | uint32_t netmask); |
| 263 | // Adds (or deletes) a route to direct to |ifname| the traffic destined to the |
| 264 | // subnet defined by |addr| and |netmask|. |
| 265 | virtual bool AddIPv4Route(const std::string& ifname, |
| 266 | uint32_t addr, |
| 267 | uint32_t netmask); |
| 268 | virtual bool DeleteIPv4Route(const std::string& ifname, |
| 269 | uint32_t addr, |
| 270 | uint32_t netmask); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 271 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 272 | // Adds (or deletes) an iptables rule for ADB port forwarding. |
| 273 | virtual bool AddAdbPortForwardRule(const std::string& ifname); |
| 274 | virtual void DeleteAdbPortForwardRule(const std::string& ifname); |
| 275 | |
| 276 | // Adds (or deletes) an iptables rule for ADB port access. |
| 277 | virtual bool AddAdbPortAccessRule(const std::string& ifname); |
| 278 | virtual void DeleteAdbPortAccessRule(const std::string& ifname); |
| 279 | |
Damien Dejean | 40e1598 | 2021-05-21 07:11:53 +0000 | [diff] [blame] | 280 | // Enables or disables netfilter conntrack helpers. |
| 281 | virtual bool SetConntrackHelpers(bool enable_helpers); |
Hugo Benichi | 48ca4eb | 2021-06-03 15:42:42 +0900 | [diff] [blame] | 282 | // Allows (or stops allowing) loopback IPv4 addresses as valid sources or |
| 283 | // destinations during IPv4 routing for |ifname|. This lets connections |
| 284 | // originated from guests like ARC or Crostini be accepted on the host and |
| 285 | // should be used carefully in conjunction with firewall port access rules to |
| 286 | // only allow very specific connection patterns. |
| 287 | virtual bool SetRouteLocalnet(const std::string& ifname, bool enable); |
| 288 | // Adds all |modules| into the kernel using modprobe. |
| 289 | virtual bool ModprobeAll(const std::vector<std::string>& modules); |
Damien Dejean | 40e1598 | 2021-05-21 07:11:53 +0000 | [diff] [blame] | 290 | |
Hugo Benichi | 1e0656f | 2021-02-15 15:43:38 +0900 | [diff] [blame] | 291 | // Create (or delete) DNAT rules for redirecting DNS queries from system |
| 292 | // services to the nameservers of a particular physical networks. These |
| 293 | // DNAT rules are only applied if a VPN is connected and allows system |
| 294 | // services to resolve hostnames even if a VPN application configures DNS |
| 295 | // addresses only routable through the VPN (b/178331695). |
| 296 | // TODO(b/171157837) Replaces these rules with the system DNS proxy. |
| 297 | bool AddRedirectDnsRule(const std::string& ifname, |
| 298 | const std::string dns_ipv4_addr); |
| 299 | bool RemoveRedirectDnsRule(const std::string& ifname); |
| 300 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 301 | // Set or override the interface name to index mapping for |ifname|. |
| 302 | // Only used for testing. |
| 303 | void SetIfnameIndex(const std::string& ifname, int ifindex); |
| 304 | |
Hugo Benichi | f0f5556 | 2021-04-02 15:25:02 +0900 | [diff] [blame] | 305 | // Add, remove, or flush chain |chain| in table |table|. |
| 306 | bool AddChain(IpFamily family, |
| 307 | const std::string& table, |
| 308 | const std::string& name); |
| 309 | bool RemoveChain(IpFamily family, |
| 310 | const std::string& table, |
| 311 | const std::string& name); |
| 312 | bool FlushChain(IpFamily family, |
| 313 | const std::string& table, |
| 314 | const std::string& name); |
Hugo Benichi | cd27f4e | 2020-11-19 18:32:23 +0900 | [diff] [blame] | 315 | // Manipulates a chain |chain| in table |table|. |
Hugo Benichi | a2ed443 | 2021-06-08 00:21:49 +0900 | [diff] [blame] | 316 | virtual bool ModifyChain(IpFamily family, |
| 317 | const std::string& table, |
| 318 | const std::string& op, |
| 319 | const std::string& chain, |
| 320 | bool log_failures = true); |
Hugo Benichi | ddf0084 | 2020-11-20 10:24:08 +0900 | [diff] [blame] | 321 | // Sends an iptables command for table |table|. |
Hugo Benichi | a2ed443 | 2021-06-08 00:21:49 +0900 | [diff] [blame] | 322 | virtual bool ModifyIptables(IpFamily family, |
| 323 | const std::string& table, |
| 324 | const std::vector<std::string>& argv, |
| 325 | bool log_failures = true); |
Hugo Benichi | 48ca4eb | 2021-06-03 15:42:42 +0900 | [diff] [blame] | 326 | // Dumps the iptables chains rules for the table |table|. |family| must be |
| 327 | // either IPv4 or IPv6. |
| 328 | virtual std::string DumpIptables(IpFamily family, const std::string& table); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 329 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame] | 330 | // Changes firewall rules based on |request|, allowing ingress traffic to a |
| 331 | // port, forwarding ingress traffic to a port into ARC or Crostini, or |
| 332 | // restricting localhost ports for listen(). This function corresponds to |
| 333 | // the ModifyPortRule method of patchpanel DBus API. |
| 334 | virtual bool ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request); |
| 335 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 336 | private: |
Hugo Benichi | 91ee09f | 2020-12-03 22:24:22 +0900 | [diff] [blame] | 337 | // Attempts to flush all built-in iptables chains used by patchpanel, and to |
| 338 | // delete all additionals chains created by patchpanel for routing. Traffic |
| 339 | // accounting chains are not deleted. |
| 340 | void ResetIptables(); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 341 | // Creates a virtual interface pair. |
| 342 | bool AddVirtualInterfacePair(const std::string& netns_name, |
| 343 | const std::string& veth_ifname, |
| 344 | const std::string& peer_ifname); |
| 345 | // Sets the configuration of an interface. |
| 346 | bool ConfigureInterface(const std::string& ifname, |
| 347 | const MacAddress& mac_addr, |
| 348 | uint32_t ipv4_addr, |
| 349 | uint32_t ipv4_prefix_len, |
| 350 | bool up, |
| 351 | bool enable_multicast); |
| 352 | // Sets the link status. |
| 353 | bool ToggleInterface(const std::string& ifname, bool up); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 354 | // Create (or delete) pre-routing rules allowing direct ingress on |ifname| |
| 355 | // to guest destination |ipv4_addr|. |
| 356 | bool AddInboundIPv4DNAT(const std::string& ifname, |
| 357 | const std::string& ipv4_addr); |
| 358 | void RemoveInboundIPv4DNAT(const std::string& ifname, |
| 359 | const std::string& ipv4_addr); |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 360 | bool ModifyChromeDnsRedirect(IpFamily family, |
| 361 | const DnsRedirectionRule& rule, |
| 362 | const std::string& op); |
Hugo Benichi | 1e0656f | 2021-02-15 15:43:38 +0900 | [diff] [blame] | 363 | bool ModifyRedirectDnsDNATRule(const std::string& op, |
| 364 | const std::string& protocol, |
| 365 | const std::string& ifname, |
| 366 | const std::string& dns_ipv4_addr); |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 367 | bool ModifyRedirectDnsJumpRule(IpFamily family, |
| 368 | const std::string& op, |
| 369 | const std::string& chain, |
| 370 | const std::string& ifname, |
| 371 | const std::string& target_chain, |
| 372 | Fwmark mark = {}, |
| 373 | Fwmark mask = {}, |
| 374 | bool redirect_on_mark = false); |
| 375 | bool ModifyDnsRedirectionSkipVpnRule(IpFamily family, const std::string& op); |
| 376 | |
| 377 | // Create (or delete) DNAT rules for redirecting DNS queries to a DNS proxy. |
| 378 | bool ModifyDnsProxyDNAT(IpFamily family, |
| 379 | const DnsRedirectionRule& rule, |
| 380 | const std::string& op, |
| 381 | const std::string& ifname, |
| 382 | const std::string& chain); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 383 | |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 384 | bool ModifyConnmarkSet(IpFamily family, |
| 385 | const std::string& chain, |
| 386 | const std::string& op, |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 387 | Fwmark mark, |
| 388 | Fwmark mask); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 389 | bool ModifyConnmarkRestore(IpFamily family, |
| 390 | const std::string& chain, |
| 391 | const std::string& op, |
Hugo Benichi | 1af5239 | 2020-11-27 18:09:32 +0900 | [diff] [blame] | 392 | const std::string& iif, |
| 393 | Fwmark mask); |
| 394 | bool ModifyConnmarkSave(IpFamily family, |
| 395 | const std::string& chain, |
| 396 | const std::string& op, |
Hugo Benichi | 1af5239 | 2020-11-27 18:09:32 +0900 | [diff] [blame] | 397 | Fwmark mask); |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 398 | bool ModifyFwmarkRoutingTag(const std::string& chain, |
| 399 | const std::string& op, |
Hugo Benichi | d872d3d | 2021-03-29 10:20:53 +0900 | [diff] [blame] | 400 | Fwmark routing_mark); |
| 401 | bool ModifyFwmarkSourceTag(const std::string& chain, |
| 402 | const std::string& op, |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 403 | TrafficSource source); |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 404 | bool ModifyFwmarkDefaultLocalSourceTag(const std::string& op, |
| 405 | TrafficSource source); |
| 406 | bool ModifyFwmarkLocalSourceTag(const std::string& op, |
| 407 | const LocalSourceSpecs& source); |
| 408 | bool ModifyFwmark(IpFamily family, |
| 409 | const std::string& chain, |
| 410 | const std::string& op, |
| 411 | const std::string& iif, |
| 412 | const std::string& uid_name, |
Hugo Benichi | 7e3b1fc | 2020-11-19 15:47:05 +0900 | [diff] [blame] | 413 | uint32_t classid, |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 414 | Fwmark mark, |
| 415 | Fwmark mask, |
| 416 | bool log_failures = true); |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 417 | bool ModifyIpForwarding(IpFamily family, |
| 418 | const std::string& op, |
| 419 | const std::string& iif, |
| 420 | const std::string& oif, |
| 421 | bool log_failures = true); |
Hugo Benichi | ff3cbcf | 2021-04-03 00:22:06 +0900 | [diff] [blame] | 422 | bool ModifyJumpRule(IpFamily family, |
| 423 | const std::string& table, |
| 424 | const std::string& op, |
| 425 | const std::string& chain, |
| 426 | const std::string& target, |
| 427 | const std::string& iif, |
| 428 | const std::string& oif, |
| 429 | bool log_failures = true); |
Hugo Benichi | 3ef370b | 2020-11-16 19:07:17 +0900 | [diff] [blame] | 430 | bool ModifyFwmarkVpnJumpRule(const std::string& chain, |
| 431 | const std::string& op, |
Hugo Benichi | 3ef370b | 2020-11-16 19:07:17 +0900 | [diff] [blame] | 432 | Fwmark mark, |
| 433 | Fwmark mask); |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 434 | bool ModifyFwmarkSkipVpnJumpRule(const std::string& chain, |
| 435 | const std::string& op, |
| 436 | const std::string& uid); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 437 | bool ModifyRtentry(ioctl_req_t op, struct rtentry* route); |
Hugo Benichi | 8c526e9 | 2021-03-25 14:59:59 +0900 | [diff] [blame] | 438 | // Uses if_nametoindex to return the interface index of |ifname|. If |ifname| |
| 439 | // does not exist anymore, looks up the cache |if_nametoindex_|. It is |
| 440 | // incorrect to use this function in situations where the interface has been |
| 441 | // recreated and the older value must be recovered (b/183679000). |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 442 | int FindIfIndex(const std::string& ifname); |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 443 | |
Hugo Benichi | a2ed443 | 2021-06-08 00:21:49 +0900 | [diff] [blame] | 444 | std::unique_ptr<MinijailedProcessRunner> process_runner_; |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame] | 445 | std::unique_ptr<Firewall> firewall_; |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 446 | std::unique_ptr<System> system_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 447 | |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 448 | FRIEND_TEST(DatapathTest, AddInboundIPv4DNAT); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 449 | FRIEND_TEST(DatapathTest, AddVirtualInterfacePair); |
| 450 | FRIEND_TEST(DatapathTest, ConfigureInterface); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 451 | FRIEND_TEST(DatapathTest, RemoveInboundIPv4DNAT); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 452 | FRIEND_TEST(DatapathTest, RemoveOutboundIPv4SNATMark); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 453 | FRIEND_TEST(DatapathTest, ToggleInterface); |
| 454 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 455 | // A map used for remembering the interface index of an interface. This |
| 456 | // information is necessary when cleaning up iptables fwmark rules that |
| 457 | // directly references the interface index. When removing these rules on |
| 458 | // an RTM_DELLINK event, the interface index cannot be retrieved anymore. |
| 459 | // A new entry is only added when a new physical device appears, and entries |
| 460 | // are not removed. |
| 461 | // TODO(b/161507671) Rely on RoutingService to obtain this information once |
| 462 | // shill/routing_table.cc has been migrated to patchpanel. |
| 463 | std::map<std::string, int> if_nametoindex_; |
Hugo Benichi | 1e0656f | 2021-02-15 15:43:38 +0900 | [diff] [blame] | 464 | |
| 465 | // A map used for tracking the primary IPv4 dns address associated to a given |
| 466 | // Shill Device known by its interface name. This is used for redirecting |
| 467 | // DNS queries of system services when a VPN is connected. |
| 468 | std::map<std::string, std::string> physical_dns_addresses_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 469 | }; |
| 470 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 471 | } // namespace patchpanel |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 472 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 473 | #endif // PATCHPANEL_DATAPATH_H_ |