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