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" |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 24 | #include "patchpanel/net_util.h" |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 25 | #include "patchpanel/routing_service.h" |
Jason Jeremy Iman | a183d7a | 2021-08-06 01:35:40 +0900 | [diff] [blame] | 26 | #include "patchpanel/scoped_ns.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 27 | #include "patchpanel/subnet.h" |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 28 | #include "patchpanel/system.h" |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 29 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 30 | namespace patchpanel { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 31 | |
Hugo Benichi | ad5947b | 2021-09-03 16:56:40 +0900 | [diff] [blame] | 32 | // filter INPUT chain for ingress port access rules controlled by |
| 33 | // permission_broker. |
| 34 | constexpr char kIngressPortFirewallChain[] = "ingress_port_firewall"; |
| 35 | // filter OUTPUT chain for egress port restriction rules controlled by |
| 36 | // permission_broker. |
| 37 | constexpr char kEgressPortFirewallChain[] = "egress_port_firewall"; |
| 38 | // nat PREROUTING chain for ingress DNAT forwarding rules controlled by |
| 39 | // permission_broker. |
| 40 | constexpr char kIngressPortForwardingChain[] = "ingress_port_forwarding"; |
| 41 | |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 42 | // Struct holding parameters for Datapath::StartRoutingNamespace requests. |
| 43 | struct ConnectedNamespace { |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 44 | // 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 Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 48 | // 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 Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 52 | // Source to which traffic from |host_ifname| will be attributed. |
| 53 | TrafficSource source; |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 54 | // 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 Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 57 | std::string outbound_ifname; |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 58 | // 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 Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 60 | // network. If |outbound_ifname| is empty and |route_on_vpn| is true, the |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 61 | // traffic will be routed through VPN connections. If |outbound_ifname| |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 62 | // specifies a valid physical interface, |route_on_vpn| is ignored. |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 63 | bool route_on_vpn; |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 64 | // Name of the "local" veth interface visible on the host namespace. |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 65 | std::string host_ifname; |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 66 | // Name of the "remote" veth interface moved into the client namespace. |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 67 | std::string peer_ifname; |
| 68 | // IPv4 subnet assigned to the client namespace. |
| 69 | std::unique_ptr<Subnet> peer_subnet; |
Jason Jeremy Iman | 6a7c395 | 2021-07-05 23:15:10 +0900 | [diff] [blame] | 70 | // MAC address of the "local" veth interface visible on the host namespace. |
| 71 | MacAddress host_mac_addr; |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 72 | // MAC address of the "remote" veth interface. |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 73 | MacAddress peer_mac_addr; |
Jason Jeremy Iman | 6a7c395 | 2021-07-05 23:15:10 +0900 | [diff] [blame] | 74 | // 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 Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 78 | }; |
| 79 | |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 80 | struct 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 Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 87 | std::ostream& operator<<(std::ostream& stream, |
| 88 | const ConnectedNamespace& nsinfo); |
| 89 | |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 90 | std::ostream& operator<<(std::ostream& stream, const DnsRedirectionRule& rule); |
| 91 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 92 | // Simple enum of bitmasks used for specifying a set of IP family values. |
| 93 | enum IpFamily { |
| 94 | NONE = 0, |
| 95 | IPv4 = 1 << 0, |
| 96 | IPv6 = 1 << 1, |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 97 | Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1); |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 98 | }; |
| 99 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 100 | // 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] | 101 | std::string ArcVethHostName(const std::string& ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 102 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 103 | // Returns the ARC bridge interface name for the given interface. |
| 104 | std::string ArcBridgeName(const std::string& ifname); |
| 105 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 106 | // ARC networking data path configuration utility. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 107 | // IPV4 addresses are always specified in singular dotted-form (a.b.c.d) |
| 108 | // (not in CIDR representation |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 109 | class Datapath { |
| 110 | public: |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame] | 111 | Datapath(); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 112 | // Provided for testing only. |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 113 | Datapath(MinijailedProcessRunner* process_runner, |
| 114 | Firewall* firewall, |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 115 | System* system); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 116 | Datapath(const Datapath&) = delete; |
| 117 | Datapath& operator=(const Datapath&) = delete; |
| 118 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 119 | virtual ~Datapath() = default; |
| 120 | |
Hugo Benichi | bf811c6 | 2020-09-07 17:30:45 +0900 | [diff] [blame] | 121 | // 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 Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 127 | // Attaches the name |netns_name| to a network namespace identified by |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 128 | // |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 Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 131 | 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 Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 136 | virtual bool AddBridge(const std::string& ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 137 | uint32_t ipv4_addr, |
| 138 | uint32_t ipv4_prefix_len); |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 139 | virtual void RemoveBridge(const std::string& ifname); |
| 140 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 141 | virtual bool AddToBridge(const std::string& br_ifname, |
| 142 | const std::string& ifname); |
| 143 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 144 | // 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 Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 149 | // |mac_addr| and |ipv4_addr| should be null if this interface will be later |
| 150 | // bridged. |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 151 | // If |user| is empty, no owner will be set |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 152 | virtual std::string AddTAP(const std::string& name, |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 153 | const MacAddress* mac_addr, |
| 154 | const SubnetAddress* ipv4_addr, |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 155 | const std::string& user); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 156 | |
| 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 Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 164 | // 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 Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 168 | const std::string& netns_name, |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 169 | 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 Iman | a183d7a | 2021-08-06 01:35:40 +0900 | [diff] [blame] | 176 | // Disable and re-enable IPv6. |
| 177 | virtual void RestartIPv6(); |
| 178 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 179 | virtual void RemoveInterface(const std::string& ifname); |
| 180 | |
Hugo Benichi | 954bae6 | 2021-04-09 09:12:30 +0900 | [diff] [blame] | 181 | // Create an OUTPUT DROP rule for any locally originated traffic |
Hugo Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 182 | // 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 Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 187 | |
Hugo Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 188 | // Creates a virtual ethernet interface pair shared with the client namespace |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 189 | // 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 Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 192 | // Destroys the virtual ethernet interface, routing, and network namespace |
Hugo Benichi | fcf8102 | 2020-12-04 11:01:37 +0900 | [diff] [blame] | 193 | // 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 Benichi | 7c34267 | 2020-09-08 09:18:14 +0900 | [diff] [blame] | 197 | |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 198 | // 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 Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 203 | // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 204 | // 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 Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 215 | virtual void StartRoutingDevice(const std::string& ext_ifname, |
| 216 | const std::string& int_ifname, |
| 217 | uint32_t int_ipv4_addr, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 218 | TrafficSource source, |
Jason Jeremy Iman | 72e6110 | 2021-04-23 03:37:14 +0900 | [diff] [blame] | 219 | bool route_on_vpn, |
| 220 | uint32_t peer_ipv4_addr = 0); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 221 | |
| 222 | // Removes IPv4 iptables, IP forwarding, and traffic marking for the given |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 223 | // downstream network interface |int_ifname|. |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 224 | virtual void StopRoutingDevice(const std::string& ext_ifname, |
| 225 | const std::string& int_ifname, |
| 226 | uint32_t int_ipv4_addr, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 227 | TrafficSource source, |
| 228 | bool route_on_vpn); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 229 | |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 230 | // 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 Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 232 | // 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 Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 235 | virtual void StartConnectionPinning(const std::string& ext_ifname); |
| 236 | virtual void StopConnectionPinning(const std::string& ext_ifname); |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 237 | // Starts or stops VPN routing for: |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 238 | // - Local traffic from sockets of binaries running under uids eligible to be |
| 239 | // routed |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 240 | // through VPN connections. These uids are defined by |kLocalSourceTypes| |
| 241 | // in routing_service.h |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 242 | // - Forwarded traffic from downstream network interfaces tracking the |
| 243 | // default network. |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 244 | virtual void StartVpnRouting(const std::string& vpn_ifname); |
| 245 | virtual void StopVpnRouting(const std::string& vpn_ifname); |
Hugo Benichi | 76be34a | 2020-08-26 22:35:54 +0900 | [diff] [blame] | 246 | |
Hugo Benichi | bb38bdd | 2021-05-14 10:36:11 +0900 | [diff] [blame] | 247 | // 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 Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 253 | // Methods supporting IPv6 configuration for ARC. |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 254 | virtual bool MaskInterfaceFlags(const std::string& ifname, |
| 255 | uint16_t on, |
| 256 | uint16_t off = 0); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 257 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 258 | // Convenience functions for enabling or disabling IPv6 forwarding in both |
| 259 | // directions between a pair of interfaces |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 260 | virtual bool AddIPv6Forwarding(const std::string& ifname1, |
| 261 | const std::string& ifname2); |
| 262 | virtual void RemoveIPv6Forwarding(const std::string& ifname1, |
| 263 | const std::string& ifname2); |
| 264 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 265 | virtual bool AddIPv6HostRoute(const std::string& ifname, |
| 266 | const std::string& ipv6_addr, |
| 267 | int ipv6_prefix_len); |
| 268 | virtual void RemoveIPv6HostRoute(const std::string& ifname, |
| 269 | const std::string& ipv6_addr, |
| 270 | int ipv6_prefix_len); |
| 271 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 272 | virtual bool AddIPv6Address(const std::string& ifname, |
| 273 | const std::string& ipv6_addr); |
| 274 | virtual void RemoveIPv6Address(const std::string& ifname, |
| 275 | const std::string& ipv6_addr); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 276 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 277 | // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined |
| 278 | // to the subnet defined by |addr| and |netmask|. |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 279 | virtual bool AddIPv4Route(uint32_t gateway_addr, |
| 280 | uint32_t addr, |
| 281 | uint32_t netmask); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 282 | virtual bool DeleteIPv4Route(uint32_t gateway_addr, |
| 283 | uint32_t addr, |
| 284 | uint32_t netmask); |
| 285 | // Adds (or deletes) a route to direct to |ifname| the traffic destined to the |
| 286 | // subnet defined by |addr| and |netmask|. |
| 287 | virtual bool AddIPv4Route(const std::string& ifname, |
| 288 | uint32_t addr, |
| 289 | uint32_t netmask); |
| 290 | virtual bool DeleteIPv4Route(const std::string& ifname, |
| 291 | uint32_t addr, |
| 292 | uint32_t netmask); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 293 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 294 | // Adds (or deletes) an iptables rule for ADB port forwarding. |
| 295 | virtual bool AddAdbPortForwardRule(const std::string& ifname); |
| 296 | virtual void DeleteAdbPortForwardRule(const std::string& ifname); |
| 297 | |
| 298 | // Adds (or deletes) an iptables rule for ADB port access. |
| 299 | virtual bool AddAdbPortAccessRule(const std::string& ifname); |
| 300 | virtual void DeleteAdbPortAccessRule(const std::string& ifname); |
| 301 | |
Damien Dejean | 40e1598 | 2021-05-21 07:11:53 +0000 | [diff] [blame] | 302 | // Enables or disables netfilter conntrack helpers. |
| 303 | virtual bool SetConntrackHelpers(bool enable_helpers); |
Hugo Benichi | 48ca4eb | 2021-06-03 15:42:42 +0900 | [diff] [blame] | 304 | // Allows (or stops allowing) loopback IPv4 addresses as valid sources or |
| 305 | // destinations during IPv4 routing for |ifname|. This lets connections |
| 306 | // originated from guests like ARC or Crostini be accepted on the host and |
| 307 | // should be used carefully in conjunction with firewall port access rules to |
| 308 | // only allow very specific connection patterns. |
| 309 | virtual bool SetRouteLocalnet(const std::string& ifname, bool enable); |
| 310 | // Adds all |modules| into the kernel using modprobe. |
| 311 | virtual bool ModprobeAll(const std::vector<std::string>& modules); |
Damien Dejean | 40e1598 | 2021-05-21 07:11:53 +0000 | [diff] [blame] | 312 | |
Hugo Benichi | 62fd125 | 2021-08-19 16:55:54 +0900 | [diff] [blame] | 313 | // Create (or delete) DNAT rules for sending unsollicited traffic inbound on |
| 314 | // interface |ifname| to |ipv4_addr|. This is used for implementing |
| 315 | // transparent ARC inbound connections to Android Apps listening on the |
| 316 | // network. |
| 317 | virtual void AddInboundIPv4DNAT(const std::string& ifname, |
| 318 | const std::string& ipv4_addr); |
| 319 | virtual void RemoveInboundIPv4DNAT(const std::string& ifname, |
| 320 | const std::string& ipv4_addr); |
| 321 | |
Hugo Benichi | 1e0656f | 2021-02-15 15:43:38 +0900 | [diff] [blame] | 322 | // Create (or delete) DNAT rules for redirecting DNS queries from system |
| 323 | // services to the nameservers of a particular physical networks. These |
| 324 | // DNAT rules are only applied if a VPN is connected and allows system |
| 325 | // services to resolve hostnames even if a VPN application configures DNS |
| 326 | // addresses only routable through the VPN (b/178331695). |
| 327 | // TODO(b/171157837) Replaces these rules with the system DNS proxy. |
| 328 | bool AddRedirectDnsRule(const std::string& ifname, |
| 329 | const std::string dns_ipv4_addr); |
| 330 | bool RemoveRedirectDnsRule(const std::string& ifname); |
| 331 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 332 | // Set or override the interface name to index mapping for |ifname|. |
| 333 | // Only used for testing. |
| 334 | void SetIfnameIndex(const std::string& ifname, int ifindex); |
| 335 | |
Hugo Benichi | f0f5556 | 2021-04-02 15:25:02 +0900 | [diff] [blame] | 336 | // Add, remove, or flush chain |chain| in table |table|. |
| 337 | bool AddChain(IpFamily family, |
| 338 | const std::string& table, |
| 339 | const std::string& name); |
| 340 | bool RemoveChain(IpFamily family, |
| 341 | const std::string& table, |
| 342 | const std::string& name); |
| 343 | bool FlushChain(IpFamily family, |
| 344 | const std::string& table, |
| 345 | const std::string& name); |
Hugo Benichi | cd27f4e | 2020-11-19 18:32:23 +0900 | [diff] [blame] | 346 | // Manipulates a chain |chain| in table |table|. |
Hugo Benichi | a2ed443 | 2021-06-08 00:21:49 +0900 | [diff] [blame] | 347 | virtual bool ModifyChain(IpFamily family, |
| 348 | const std::string& table, |
| 349 | const std::string& op, |
| 350 | const std::string& chain, |
| 351 | bool log_failures = true); |
Hugo Benichi | ddf0084 | 2020-11-20 10:24:08 +0900 | [diff] [blame] | 352 | // Sends an iptables command for table |table|. |
Hugo Benichi | a2ed443 | 2021-06-08 00:21:49 +0900 | [diff] [blame] | 353 | virtual bool ModifyIptables(IpFamily family, |
| 354 | const std::string& table, |
| 355 | const std::vector<std::string>& argv, |
| 356 | bool log_failures = true); |
Hugo Benichi | 48ca4eb | 2021-06-03 15:42:42 +0900 | [diff] [blame] | 357 | // Dumps the iptables chains rules for the table |table|. |family| must be |
| 358 | // either IPv4 or IPv6. |
| 359 | virtual std::string DumpIptables(IpFamily family, const std::string& table); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 360 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame] | 361 | // Changes firewall rules based on |request|, allowing ingress traffic to a |
| 362 | // port, forwarding ingress traffic to a port into ARC or Crostini, or |
| 363 | // restricting localhost ports for listen(). This function corresponds to |
| 364 | // the ModifyPortRule method of patchpanel DBus API. |
| 365 | virtual bool ModifyPortRule(const patchpanel::ModifyPortRuleRequest& request); |
| 366 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 367 | private: |
Hugo Benichi | 91ee09f | 2020-12-03 22:24:22 +0900 | [diff] [blame] | 368 | // Attempts to flush all built-in iptables chains used by patchpanel, and to |
| 369 | // delete all additionals chains created by patchpanel for routing. Traffic |
| 370 | // accounting chains are not deleted. |
| 371 | void ResetIptables(); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 372 | // Creates a virtual interface pair. |
| 373 | bool AddVirtualInterfacePair(const std::string& netns_name, |
| 374 | const std::string& veth_ifname, |
| 375 | const std::string& peer_ifname); |
| 376 | // Sets the configuration of an interface. |
| 377 | bool ConfigureInterface(const std::string& ifname, |
| 378 | const MacAddress& mac_addr, |
| 379 | uint32_t ipv4_addr, |
| 380 | uint32_t ipv4_prefix_len, |
| 381 | bool up, |
| 382 | bool enable_multicast); |
| 383 | // Sets the link status. |
| 384 | bool ToggleInterface(const std::string& ifname, bool up); |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 385 | bool ModifyChromeDnsRedirect(IpFamily family, |
| 386 | const DnsRedirectionRule& rule, |
| 387 | const std::string& op); |
Hugo Benichi | 1e0656f | 2021-02-15 15:43:38 +0900 | [diff] [blame] | 388 | bool ModifyRedirectDnsDNATRule(const std::string& op, |
| 389 | const std::string& protocol, |
| 390 | const std::string& ifname, |
| 391 | const std::string& dns_ipv4_addr); |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 392 | bool ModifyRedirectDnsJumpRule(IpFamily family, |
| 393 | const std::string& op, |
| 394 | const std::string& chain, |
| 395 | const std::string& ifname, |
| 396 | const std::string& target_chain, |
| 397 | Fwmark mark = {}, |
| 398 | Fwmark mask = {}, |
| 399 | bool redirect_on_mark = false); |
| 400 | bool ModifyDnsRedirectionSkipVpnRule(IpFamily family, const std::string& op); |
| 401 | |
| 402 | // Create (or delete) DNAT rules for redirecting DNS queries to a DNS proxy. |
| 403 | bool ModifyDnsProxyDNAT(IpFamily family, |
| 404 | const DnsRedirectionRule& rule, |
| 405 | const std::string& op, |
| 406 | const std::string& ifname, |
| 407 | const std::string& chain); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 408 | |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 409 | bool ModifyConnmarkSet(IpFamily family, |
| 410 | const std::string& chain, |
| 411 | const std::string& op, |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 412 | Fwmark mark, |
| 413 | Fwmark mask); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 414 | bool ModifyConnmarkRestore(IpFamily family, |
| 415 | const std::string& chain, |
| 416 | const std::string& op, |
Hugo Benichi | 1af5239 | 2020-11-27 18:09:32 +0900 | [diff] [blame] | 417 | const std::string& iif, |
| 418 | Fwmark mask); |
| 419 | bool ModifyConnmarkSave(IpFamily family, |
| 420 | const std::string& chain, |
| 421 | const std::string& op, |
Hugo Benichi | 1af5239 | 2020-11-27 18:09:32 +0900 | [diff] [blame] | 422 | Fwmark mask); |
Hugo Benichi | 2a94054 | 2020-10-26 18:50:49 +0900 | [diff] [blame] | 423 | bool ModifyFwmarkRoutingTag(const std::string& chain, |
| 424 | const std::string& op, |
Hugo Benichi | d872d3d | 2021-03-29 10:20:53 +0900 | [diff] [blame] | 425 | Fwmark routing_mark); |
| 426 | bool ModifyFwmarkSourceTag(const std::string& chain, |
| 427 | const std::string& op, |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 428 | TrafficSource source); |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 429 | bool ModifyFwmarkDefaultLocalSourceTag(const std::string& op, |
| 430 | TrafficSource source); |
| 431 | bool ModifyFwmarkLocalSourceTag(const std::string& op, |
| 432 | const LocalSourceSpecs& source); |
| 433 | bool ModifyFwmark(IpFamily family, |
| 434 | const std::string& chain, |
| 435 | const std::string& op, |
| 436 | const std::string& iif, |
| 437 | const std::string& uid_name, |
Hugo Benichi | 7e3b1fc | 2020-11-19 15:47:05 +0900 | [diff] [blame] | 438 | uint32_t classid, |
Hugo Benichi | 3a9162b | 2020-09-09 15:47:40 +0900 | [diff] [blame] | 439 | Fwmark mark, |
| 440 | Fwmark mask, |
| 441 | bool log_failures = true); |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 442 | bool ModifyIpForwarding(IpFamily family, |
| 443 | const std::string& op, |
| 444 | const std::string& iif, |
| 445 | const std::string& oif, |
| 446 | bool log_failures = true); |
Hugo Benichi | ff3cbcf | 2021-04-03 00:22:06 +0900 | [diff] [blame] | 447 | bool ModifyJumpRule(IpFamily family, |
| 448 | const std::string& table, |
| 449 | const std::string& op, |
| 450 | const std::string& chain, |
| 451 | const std::string& target, |
| 452 | const std::string& iif, |
| 453 | const std::string& oif, |
| 454 | bool log_failures = true); |
Hugo Benichi | 3ef370b | 2020-11-16 19:07:17 +0900 | [diff] [blame] | 455 | bool ModifyFwmarkVpnJumpRule(const std::string& chain, |
| 456 | const std::string& op, |
Hugo Benichi | 3ef370b | 2020-11-16 19:07:17 +0900 | [diff] [blame] | 457 | Fwmark mark, |
| 458 | Fwmark mask); |
Jason Jeremy Iman | f4fb64f | 2021-04-20 21:54:19 +0900 | [diff] [blame] | 459 | bool ModifyFwmarkSkipVpnJumpRule(const std::string& chain, |
| 460 | const std::string& op, |
Hugo Benichi | 5fbb936 | 2021-07-09 15:41:33 +0900 | [diff] [blame] | 461 | const std::string& uid, |
| 462 | bool log_failures = true); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 463 | bool ModifyRtentry(ioctl_req_t op, struct rtentry* route); |
Hugo Benichi | 8c526e9 | 2021-03-25 14:59:59 +0900 | [diff] [blame] | 464 | // Uses if_nametoindex to return the interface index of |ifname|. If |ifname| |
| 465 | // does not exist anymore, looks up the cache |if_nametoindex_|. It is |
| 466 | // incorrect to use this function in situations where the interface has been |
| 467 | // recreated and the older value must be recovered (b/183679000). |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 468 | int FindIfIndex(const std::string& ifname); |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 469 | |
Hugo Benichi | a2ed443 | 2021-06-08 00:21:49 +0900 | [diff] [blame] | 470 | std::unique_ptr<MinijailedProcessRunner> process_runner_; |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame] | 471 | std::unique_ptr<Firewall> firewall_; |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 472 | std::unique_ptr<System> system_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 473 | |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 474 | FRIEND_TEST(DatapathTest, AddInboundIPv4DNAT); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 475 | FRIEND_TEST(DatapathTest, AddVirtualInterfacePair); |
| 476 | FRIEND_TEST(DatapathTest, ConfigureInterface); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 477 | FRIEND_TEST(DatapathTest, RemoveInboundIPv4DNAT); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 478 | FRIEND_TEST(DatapathTest, RemoveOutboundIPv4SNATMark); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 479 | FRIEND_TEST(DatapathTest, ToggleInterface); |
| 480 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 481 | // A map used for remembering the interface index of an interface. This |
| 482 | // information is necessary when cleaning up iptables fwmark rules that |
| 483 | // directly references the interface index. When removing these rules on |
| 484 | // an RTM_DELLINK event, the interface index cannot be retrieved anymore. |
Hugo Benichi | 600a943 | 2021-07-06 11:57:29 +0900 | [diff] [blame] | 485 | // A new entry is only added when a new upstream network interface appears, |
| 486 | // and entries are not removed. |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame] | 487 | // TODO(b/161507671) Rely on RoutingService to obtain this information once |
| 488 | // shill/routing_table.cc has been migrated to patchpanel. |
| 489 | std::map<std::string, int> if_nametoindex_; |
Hugo Benichi | 1e0656f | 2021-02-15 15:43:38 +0900 | [diff] [blame] | 490 | |
| 491 | // A map used for tracking the primary IPv4 dns address associated to a given |
| 492 | // Shill Device known by its interface name. This is used for redirecting |
| 493 | // DNS queries of system services when a VPN is connected. |
| 494 | std::map<std::string, std::string> physical_dns_addresses_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 495 | }; |
| 496 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 497 | } // namespace patchpanel |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 498 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 499 | #endif // PATCHPANEL_DATAPATH_H_ |