blob: 53f107e35f123ed9e101dd79db5a676922f1868d [file] [log] [blame]
Garrick Evansf0ab7132019-06-18 14:50:42 +09001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Garrick Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_DATAPATH_H_
6#define PATCHPANEL_DATAPATH_H_
Garrick Evansf0ab7132019-06-18 14:50:42 +09007
Hugo Benichie8758b52020-04-03 14:49:01 +09008#include <net/route.h>
Hugo Benichi33860d72020-07-09 16:34:01 +09009#include <sys/types.h>
Hugo Benichie8758b52020-04-03 14:49:01 +090010
Hugo Benichi2a940542020-10-26 18:50:49 +090011#include <set>
Garrick Evansf0ab7132019-06-18 14:50:42 +090012#include <string>
Hugo Benichi2a940542020-10-26 18:50:49 +090013#include <vector>
Garrick Evansf0ab7132019-06-18 14:50:42 +090014
15#include <base/macros.h>
Hugo Benichi82ed5cf2020-09-08 21:30:22 +090016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evansf0ab7132019-06-18 14:50:42 +090017
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090018#include "patchpanel/firewall.h"
Garrick Evans3388a032020-03-24 11:25:55 +090019#include "patchpanel/mac_address_generator.h"
20#include "patchpanel/minijailed_process_runner.h"
Hugo Benichi8d622b52020-08-13 15:24:12 +090021#include "patchpanel/routing_service.h"
Garrick Evans3388a032020-03-24 11:25:55 +090022#include "patchpanel/subnet.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +090023
Garrick Evans3388a032020-03-24 11:25:55 +090024namespace patchpanel {
Garrick Evansf0ab7132019-06-18 14:50:42 +090025
Hugo Benichid82d8832020-08-14 10:05:03 +090026// Simple enum of bitmasks used for specifying a set of IP family values.
27enum IpFamily {
28 NONE = 0,
29 IPv4 = 1 << 0,
30 IPv6 = 1 << 1,
Taoyu Lia0727dc2020-09-24 19:54:59 +090031 Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1);
Hugo Benichid82d8832020-08-14 10:05:03 +090032};
33
Taoyu Li90c13912019-11-26 17:56:54 +090034// cros lint will yell to force using int16/int64 instead of long here, however
35// note that unsigned long IS the correct signature for ioctl in Linux kernel -
36// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
37using ioctl_req_t = unsigned long;
38typedef int (*ioctl_t)(int, ioctl_req_t, ...);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090039
Garrick Evans54861622019-07-19 09:05:09 +090040// Returns for given interface name the host name of a ARC veth pair.
Garrick Evans2f581a02020-05-11 10:43:35 +090041std::string ArcVethHostName(const std::string& ifname);
Garrick Evans54861622019-07-19 09:05:09 +090042
Garrick Evans8a067562020-05-11 12:47:30 +090043// Returns the ARC bridge interface name for the given interface.
44std::string ArcBridgeName(const std::string& ifname);
45
Garrick Evansf0ab7132019-06-18 14:50:42 +090046// ARC networking data path configuration utility.
Garrick Evans54861622019-07-19 09:05:09 +090047// IPV4 addresses are always specified in singular dotted-form (a.b.c.d)
48// (not in CIDR representation
Garrick Evansf0ab7132019-06-18 14:50:42 +090049class Datapath {
50 public:
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090051 // |process_runner| and |firewall| must not be null; it is not owned.
52 Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090053 // Provided for testing only.
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090054 Datapath(MinijailedProcessRunner* process_runner,
55 Firewall* firewall,
56 ioctl_t ioctl_hook);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090057 Datapath(const Datapath&) = delete;
58 Datapath& operator=(const Datapath&) = delete;
59
Garrick Evansf0ab7132019-06-18 14:50:42 +090060 virtual ~Datapath() = default;
61
Hugo Benichibf811c62020-09-07 17:30:45 +090062 // Start and stop the Datapath, creating or destroying the initial iptables
63 // setup needed for forwarding traffic from VMs and containers and for
64 // fwmark based routing.
65 virtual void Start();
66 virtual void Stop();
67
Hugo Benichi33860d72020-07-09 16:34:01 +090068 // Attaches the name |netns_name| to a network namespace identified by
69 // |netns_pid|. If |netns_name| had already been created, it will be deleted
70 // first.
71 virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
72
73 // Deletes the name |netns_name| of a network namespace.
74 virtual bool NetnsDeleteName(const std::string& netns_name);
75
Garrick Evans8a949dc2019-07-18 16:17:53 +090076 virtual bool AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +090077 uint32_t ipv4_addr,
78 uint32_t ipv4_prefix_len);
Garrick Evans8a949dc2019-07-18 16:17:53 +090079 virtual void RemoveBridge(const std::string& ifname);
80
Garrick Evans621ed262019-11-13 12:28:43 +090081 virtual bool AddToBridge(const std::string& br_ifname,
82 const std::string& ifname);
83
Garrick Evansc7ae82c2019-09-04 16:25:10 +090084 // Adds a new TAP device.
85 // |name| may be empty, in which case a default device name will be used;
86 // it may be a template (e.g. vmtap%d), in which case the kernel will
87 // generate the name; or it may be fully defined. In all cases, upon success,
88 // the function returns the actual name of the interface.
Garrick Evans621ed262019-11-13 12:28:43 +090089 // |mac_addr| and |ipv4_addr| should be null if this interface will be later
90 // bridged.
Garrick Evans4f9f5572019-11-26 10:25:16 +090091 // If |user| is empty, no owner will be set
Garrick Evansc7ae82c2019-09-04 16:25:10 +090092 virtual std::string AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +090093 const MacAddress* mac_addr,
94 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +090095 const std::string& user);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090096
97 // |ifname| must be the actual name of the interface.
98 virtual void RemoveTAP(const std::string& ifname);
99
100 // The following are iptables methods.
101 // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d)
102 // IPv4 address (not a CIDR representation).
103
Hugo Benichi76675592020-04-08 14:29:57 +0900104 // Creates a virtual interface pair split across the current namespace and the
105 // namespace corresponding to |pid|, and set up the remote interface
106 // |peer_ifname| according // to the given parameters.
107 virtual bool ConnectVethPair(pid_t pid,
Hugo Benichi33860d72020-07-09 16:34:01 +0900108 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900109 const std::string& veth_ifname,
110 const std::string& peer_ifname,
111 const MacAddress& remote_mac_addr,
112 uint32_t remote_ipv4_addr,
113 uint32_t remote_ipv4_prefix_len,
114 bool remote_multicast_flag);
115
Garrick Evans54861622019-07-19 09:05:09 +0900116 virtual void RemoveInterface(const std::string& ifname);
117
Hugo Benichi321f23b2020-09-25 15:42:05 +0900118 // Create (or delete) an OUTPUT DROP rule for any locally originated traffic
119 // whose src IPv4 matches |src_ip| and would exit |oif|. This is mainly used
120 // for dropping Chrome webRTC traffic incorrectly bound on ARC and other
121 // guests virtual interfaces (chromium:898210).
122 virtual bool AddSourceIPv4DropRule(const std::string& oif,
123 const std::string& src_ip);
124 virtual bool RemoveSourceIPv4DropRule(const std::string& oif,
125 const std::string& src_ip);
126
Hugo Benichi7c342672020-09-08 09:18:14 +0900127 // Creates a virtual ethernet interface pair shared with the client namespace
128 // of |pid| and sets up routing outside and inside the client namespace for
129 // connecting the client namespace to the network.
130 bool StartRoutingNamespace(pid_t pid,
131 const std::string& netns_name,
132 const std::string& host_ifname,
133 const std::string& peer_ifname,
134 uint32_t subnet_ipv4_addr,
135 uint32_t subnet_prefixlen,
136 uint32_t host_ipv4_addr,
137 uint32_t peer_ipv4_addr,
138 const MacAddress& peer_mac_addr);
139 // Destroys the virtual ethernet interface, routing, and network namespace
140 // name set for |netns_name| by StartRoutingNamespace. The default route set
141 // inside the |netns_name| by patchpanel is not destroyed and it is assumed
142 // the client will teardown the namespace.
143 void StopRoutingNamespace(const std::string& netns_name,
144 const std::string& host_ifname,
145 uint32_t subnet_ipv4_addr,
146 uint32_t subnet_prefixlen,
147 uint32_t host_ipv4_addr);
148
Hugo Benichi8d622b52020-08-13 15:24:12 +0900149 // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given
150 // virtual device |int_ifname| associated to |source|. if |ext_ifname| is
151 // empty, the device is implicitly routed through the highest priority
152 // network.
153 virtual void StartRoutingDevice(const std::string& ext_ifname,
154 const std::string& int_ifname,
155 uint32_t int_ipv4_addr,
156 TrafficSource source);
157
158 // Removes IPv4 iptables, IP forwarding, and traffic marking for the given
159 // virtual device |int_ifname|.
160 virtual void StopRoutingDevice(const std::string& ext_ifname,
161 const std::string& int_ifname,
162 uint32_t int_ipv4_addr,
163 TrafficSource source);
164
Hugo Benichi76be34a2020-08-26 22:35:54 +0900165 // Starts or stops marking conntrack entries routed to |ext_ifname| with its
166 // associated fwmark routing tag. Once a conntrack entry is marked with the
167 // fwmark routing tag of a external device, the connection will be pinned
168 // to that deviced if conntrack fwmark restore is set for the source.
169 virtual void StartConnectionPinning(const std::string& ext_ifname);
170 virtual void StopConnectionPinning(const std::string& ext_ifname);
Hugo Benichi2a940542020-10-26 18:50:49 +0900171 // Starts or stops VPN routing for:
172 // - Local sockets of binaries running under uids eligible to be routed
173 // through VPN connections. These uids are defined by |kLocalSourceTypes|
174 // in routing_service.h
175 // - Forwarded virtual devices tracking the default network.
176 virtual void StartVpnRouting(const std::string& vpn_ifname);
177 virtual void StopVpnRouting(const std::string& vpn_ifname);
Hugo Benichi76be34a2020-08-26 22:35:54 +0900178
Taoyu Li90c13912019-11-26 17:56:54 +0900179 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900180 virtual bool MaskInterfaceFlags(const std::string& ifname,
181 uint16_t on,
182 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900183
Hugo Benichid82d8832020-08-14 10:05:03 +0900184 // Convenience functions for enabling or disabling IPv6 forwarding in both
185 // directions between a pair of interfaces
Taoyu Li90c13912019-11-26 17:56:54 +0900186 virtual bool AddIPv6Forwarding(const std::string& ifname1,
187 const std::string& ifname2);
188 virtual void RemoveIPv6Forwarding(const std::string& ifname1,
189 const std::string& ifname2);
190
Garrick Evans260ff302019-07-25 11:22:50 +0900191 virtual bool AddIPv6HostRoute(const std::string& ifname,
192 const std::string& ipv6_addr,
193 int ipv6_prefix_len);
194 virtual void RemoveIPv6HostRoute(const std::string& ifname,
195 const std::string& ipv6_addr,
196 int ipv6_prefix_len);
197
Taoyu Lia0727dc2020-09-24 19:54:59 +0900198 virtual bool AddIPv6Address(const std::string& ifname,
199 const std::string& ipv6_addr);
200 virtual void RemoveIPv6Address(const std::string& ifname,
201 const std::string& ipv6_addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900202
Hugo Benichie8758b52020-04-03 14:49:01 +0900203 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
204 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900205 virtual bool AddIPv4Route(uint32_t gateway_addr,
206 uint32_t addr,
207 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900208 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
209 uint32_t addr,
210 uint32_t netmask);
211 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
212 // subnet defined by |addr| and |netmask|.
213 virtual bool AddIPv4Route(const std::string& ifname,
214 uint32_t addr,
215 uint32_t netmask);
216 virtual bool DeleteIPv4Route(const std::string& ifname,
217 uint32_t addr,
218 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900219
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900220 // Adds (or deletes) an iptables rule for ADB port forwarding.
221 virtual bool AddAdbPortForwardRule(const std::string& ifname);
222 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
223
224 // Adds (or deletes) an iptables rule for ADB port access.
225 virtual bool AddAdbPortAccessRule(const std::string& ifname);
226 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
227
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900228 // Set or override the interface name to index mapping for |ifname|.
229 // Only used for testing.
230 void SetIfnameIndex(const std::string& ifname, int ifindex);
231
Hugo Benichicd27f4e2020-11-19 18:32:23 +0900232 // Manipulates a chain |chain| in table |table|.
233 bool ModifyChain(IpFamily family,
234 const std::string& table,
235 const std::string& op,
236 const std::string& chain,
237 bool log_failures = true);
238
Garrick Evans260ff302019-07-25 11:22:50 +0900239 MinijailedProcessRunner& runner() const;
240
Garrick Evansf0ab7132019-06-18 14:50:42 +0900241 private:
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900242 // Creates a virtual interface pair.
243 bool AddVirtualInterfacePair(const std::string& netns_name,
244 const std::string& veth_ifname,
245 const std::string& peer_ifname);
246 // Sets the configuration of an interface.
247 bool ConfigureInterface(const std::string& ifname,
248 const MacAddress& mac_addr,
249 uint32_t ipv4_addr,
250 uint32_t ipv4_prefix_len,
251 bool up,
252 bool enable_multicast);
253 // Sets the link status.
254 bool ToggleInterface(const std::string& ifname, bool up);
255 // Starts or stops accepting IP traffic forwarded between |iif| and |oif|
256 // by adding or removing ACCEPT rules in the filter FORWARD chain of iptables
257 // and/or ip6tables. If |iif| is empty, only specifies |oif| as the output
258 // interface. If |iif| is empty, only specifies |iif| as the input interface.
259 // |oif| and |iif| cannot be both empty.
260 bool StartIpForwarding(IpFamily family,
261 const std::string& iif,
262 const std::string& oif);
263 bool StopIpForwarding(IpFamily family,
264 const std::string& iif,
265 const std::string& oif);
266 // Create (or delete) pre-routing rules allowing direct ingress on |ifname|
267 // to guest destination |ipv4_addr|.
268 bool AddInboundIPv4DNAT(const std::string& ifname,
269 const std::string& ipv4_addr);
270 void RemoveInboundIPv4DNAT(const std::string& ifname,
271 const std::string& ipv4_addr);
272 // Create (or delete) a forwarding rule for |ifname|.
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900273 // Creates (or deletes) the forwarding and postrouting rules for SNAT
274 // fwmarked IPv4 traffic.
275 bool AddSNATMarkRules();
276 void RemoveSNATMarkRules();
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900277 // Create (or delete) a mangle PREROUTING rule for marking IPv4 traffic
278 // outgoing of |ifname| with the SNAT fwmark value 0x1.
279 // TODO(hugobenichi) Refer to RoutingService to obtain the fwmark value and
280 // add a fwmark mask in the generated rule.
281 bool AddOutboundIPv4SNATMark(const std::string& ifname);
282 void RemoveOutboundIPv4SNATMark(const std::string& ifname);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900283
Hugo Benichi76be34a2020-08-26 22:35:54 +0900284 bool ModifyConnmarkSetPostrouting(IpFamily family,
285 const std::string& op,
286 const std::string& oif);
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900287 bool ModifyConnmarkSet(IpFamily family,
288 const std::string& chain,
289 const std::string& op,
290 const std::string& oif,
291 Fwmark mark,
292 Fwmark mask);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900293 bool ModifyConnmarkRestore(IpFamily family,
294 const std::string& chain,
295 const std::string& op,
Hugo Benichi1af52392020-11-27 18:09:32 +0900296 const std::string& iif,
297 Fwmark mask);
298 bool ModifyConnmarkSave(IpFamily family,
299 const std::string& chain,
300 const std::string& op,
301 const std::string& oif,
302 Fwmark mask);
Hugo Benichi2a940542020-10-26 18:50:49 +0900303 bool ModifyFwmarkRoutingTag(const std::string& chain,
304 const std::string& op,
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900305 const std::string& ext_ifname,
306 const std::string& int_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900307 bool ModifyFwmarkSourceTag(const std::string& op,
308 const std::string& iif,
309 TrafficSource source);
Hugo Benichi3a9162b2020-09-09 15:47:40 +0900310 bool ModifyFwmarkDefaultLocalSourceTag(const std::string& op,
311 TrafficSource source);
312 bool ModifyFwmarkLocalSourceTag(const std::string& op,
313 const LocalSourceSpecs& source);
314 bool ModifyFwmark(IpFamily family,
315 const std::string& chain,
316 const std::string& op,
317 const std::string& iif,
318 const std::string& uid_name,
319 Fwmark mark,
320 Fwmark mask,
321 bool log_failures = true);
Hugo Benichid82d8832020-08-14 10:05:03 +0900322 bool ModifyIpForwarding(IpFamily family,
323 const std::string& op,
324 const std::string& iif,
325 const std::string& oif,
326 bool log_failures = true);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900327 bool ModifyFwmarkVpnJumpRule(const std::string& chain,
328 const std::string& op,
329 const std::string& iif,
330 Fwmark mark,
331 Fwmark mask);
Hugo Benichi3ef370b2020-11-16 19:07:17 +0900332 bool ModifyIptables(IpFamily family,
333 const std::string& table,
Hugo Benichi58f264a2020-10-16 18:16:05 +0900334 const std::vector<std::string>& argv,
335 bool log_failures = true);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900336 bool ModifyRtentry(ioctl_req_t op, struct rtentry* route);
337 int FindIfIndex(const std::string& ifname);
Hugo Benichid82d8832020-08-14 10:05:03 +0900338
Garrick Evansf0ab7132019-06-18 14:50:42 +0900339 MinijailedProcessRunner* process_runner_;
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900340 Firewall* firewall_;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900341 ioctl_t ioctl_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900342
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900343 FRIEND_TEST(DatapathTest, AddInboundIPv4DNAT);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900344 FRIEND_TEST(DatapathTest, AddOutboundIPv4SNATMark);
345 FRIEND_TEST(DatapathTest, AddSNATMarkRules);
346 FRIEND_TEST(DatapathTest, AddVirtualInterfacePair);
347 FRIEND_TEST(DatapathTest, ConfigureInterface);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900348 FRIEND_TEST(DatapathTest, RemoveInboundIPv4DNAT);
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900349 FRIEND_TEST(DatapathTest, RemoveOutboundIPv4SNATMark);
350 FRIEND_TEST(DatapathTest, RemoveSNATMarkRules);
351 FRIEND_TEST(DatapathTest, StartStopIpForwarding);
352 FRIEND_TEST(DatapathTest, ToggleInterface);
353
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900354 // A map used for remembering the interface index of an interface. This
355 // information is necessary when cleaning up iptables fwmark rules that
356 // directly references the interface index. When removing these rules on
357 // an RTM_DELLINK event, the interface index cannot be retrieved anymore.
358 // A new entry is only added when a new physical device appears, and entries
359 // are not removed.
360 // TODO(b/161507671) Rely on RoutingService to obtain this information once
361 // shill/routing_table.cc has been migrated to patchpanel.
362 std::map<std::string, int> if_nametoindex_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900363};
364
Garrick Evans3388a032020-03-24 11:25:55 +0900365} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900366
Garrick Evans3388a032020-03-24 11:25:55 +0900367#endif // PATCHPANEL_DATAPATH_H_