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