blob: 0af1b9dc26ef667f919d1bdc9a44b0c1d6f8bdb4 [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
Garrick Evansf0ab7132019-06-18 14:50:42 +090011#include <string>
12
13#include <base/macros.h>
Hugo Benichi82ed5cf2020-09-08 21:30:22 +090014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evansf0ab7132019-06-18 14:50:42 +090015
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090016#include "patchpanel/firewall.h"
Garrick Evans3388a032020-03-24 11:25:55 +090017#include "patchpanel/mac_address_generator.h"
18#include "patchpanel/minijailed_process_runner.h"
Hugo Benichi8d622b52020-08-13 15:24:12 +090019#include "patchpanel/routing_service.h"
Garrick Evans3388a032020-03-24 11:25:55 +090020#include "patchpanel/subnet.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +090021
Garrick Evans3388a032020-03-24 11:25:55 +090022namespace patchpanel {
Garrick Evansf0ab7132019-06-18 14:50:42 +090023
Hugo Benichid82d8832020-08-14 10:05:03 +090024// Simple enum of bitmasks used for specifying a set of IP family values.
25enum IpFamily {
26 NONE = 0,
27 IPv4 = 1 << 0,
28 IPv6 = 1 << 1,
Taoyu Lia0727dc2020-09-24 19:54:59 +090029 Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1);
Hugo Benichid82d8832020-08-14 10:05:03 +090030};
31
Taoyu Li90c13912019-11-26 17:56:54 +090032// cros lint will yell to force using int16/int64 instead of long here, however
33// note that unsigned long IS the correct signature for ioctl in Linux kernel -
34// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
35using ioctl_req_t = unsigned long;
36typedef int (*ioctl_t)(int, ioctl_req_t, ...);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090037
Garrick Evans54861622019-07-19 09:05:09 +090038// Returns for given interface name the host name of a ARC veth pair.
Garrick Evans2f581a02020-05-11 10:43:35 +090039std::string ArcVethHostName(const std::string& ifname);
Garrick Evans54861622019-07-19 09:05:09 +090040
Garrick Evans8a067562020-05-11 12:47:30 +090041// Returns the ARC bridge interface name for the given interface.
42std::string ArcBridgeName(const std::string& ifname);
43
Garrick Evansf0ab7132019-06-18 14:50:42 +090044// ARC networking data path configuration utility.
Garrick Evans54861622019-07-19 09:05:09 +090045// IPV4 addresses are always specified in singular dotted-form (a.b.c.d)
46// (not in CIDR representation
Garrick Evansf0ab7132019-06-18 14:50:42 +090047class Datapath {
48 public:
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090049 // |process_runner| and |firewall| must not be null; it is not owned.
50 Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090051 // Provided for testing only.
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090052 Datapath(MinijailedProcessRunner* process_runner,
53 Firewall* firewall,
54 ioctl_t ioctl_hook);
Garrick Evansf0ab7132019-06-18 14:50:42 +090055 virtual ~Datapath() = default;
56
Hugo Benichibf811c62020-09-07 17:30:45 +090057 // Start and stop the Datapath, creating or destroying the initial iptables
58 // setup needed for forwarding traffic from VMs and containers and for
59 // fwmark based routing.
60 virtual void Start();
61 virtual void Stop();
62
Hugo Benichi33860d72020-07-09 16:34:01 +090063 // Attaches the name |netns_name| to a network namespace identified by
64 // |netns_pid|. If |netns_name| had already been created, it will be deleted
65 // first.
66 virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
67
68 // Deletes the name |netns_name| of a network namespace.
69 virtual bool NetnsDeleteName(const std::string& netns_name);
70
Garrick Evans8a949dc2019-07-18 16:17:53 +090071 virtual bool AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +090072 uint32_t ipv4_addr,
73 uint32_t ipv4_prefix_len);
Garrick Evans8a949dc2019-07-18 16:17:53 +090074 virtual void RemoveBridge(const std::string& ifname);
75
Garrick Evans621ed262019-11-13 12:28:43 +090076 virtual bool AddToBridge(const std::string& br_ifname,
77 const std::string& ifname);
78
Garrick Evansc7ae82c2019-09-04 16:25:10 +090079 // Adds a new TAP device.
80 // |name| may be empty, in which case a default device name will be used;
81 // it may be a template (e.g. vmtap%d), in which case the kernel will
82 // generate the name; or it may be fully defined. In all cases, upon success,
83 // the function returns the actual name of the interface.
Garrick Evans621ed262019-11-13 12:28:43 +090084 // |mac_addr| and |ipv4_addr| should be null if this interface will be later
85 // bridged.
Garrick Evans4f9f5572019-11-26 10:25:16 +090086 // If |user| is empty, no owner will be set
Garrick Evansc7ae82c2019-09-04 16:25:10 +090087 virtual std::string AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +090088 const MacAddress* mac_addr,
89 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +090090 const std::string& user);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090091
92 // |ifname| must be the actual name of the interface.
93 virtual void RemoveTAP(const std::string& ifname);
94
95 // The following are iptables methods.
96 // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d)
97 // IPv4 address (not a CIDR representation).
98
Hugo Benichi76675592020-04-08 14:29:57 +090099 // Creates a virtual interface pair split across the current namespace and the
100 // namespace corresponding to |pid|, and set up the remote interface
101 // |peer_ifname| according // to the given parameters.
102 virtual bool ConnectVethPair(pid_t pid,
Hugo Benichi33860d72020-07-09 16:34:01 +0900103 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900104 const std::string& veth_ifname,
105 const std::string& peer_ifname,
106 const MacAddress& remote_mac_addr,
107 uint32_t remote_ipv4_addr,
108 uint32_t remote_ipv4_prefix_len,
109 bool remote_multicast_flag);
110
Garrick Evans54861622019-07-19 09:05:09 +0900111 virtual void RemoveInterface(const std::string& ifname);
112
Hugo Benichi321f23b2020-09-25 15:42:05 +0900113 // Create (or delete) an OUTPUT DROP rule for any locally originated traffic
114 // whose src IPv4 matches |src_ip| and would exit |oif|. This is mainly used
115 // for dropping Chrome webRTC traffic incorrectly bound on ARC and other
116 // guests virtual interfaces (chromium:898210).
117 virtual bool AddSourceIPv4DropRule(const std::string& oif,
118 const std::string& src_ip);
119 virtual bool RemoveSourceIPv4DropRule(const std::string& oif,
120 const std::string& src_ip);
121
Hugo Benichi7c342672020-09-08 09:18:14 +0900122 // Creates a virtual ethernet interface pair shared with the client namespace
123 // of |pid| and sets up routing outside and inside the client namespace for
124 // connecting the client namespace to the network.
125 bool StartRoutingNamespace(pid_t pid,
126 const std::string& netns_name,
127 const std::string& host_ifname,
128 const std::string& peer_ifname,
129 uint32_t subnet_ipv4_addr,
130 uint32_t subnet_prefixlen,
131 uint32_t host_ipv4_addr,
132 uint32_t peer_ipv4_addr,
133 const MacAddress& peer_mac_addr);
134 // Destroys the virtual ethernet interface, routing, and network namespace
135 // name set for |netns_name| by StartRoutingNamespace. The default route set
136 // inside the |netns_name| by patchpanel is not destroyed and it is assumed
137 // the client will teardown the namespace.
138 void StopRoutingNamespace(const std::string& netns_name,
139 const std::string& host_ifname,
140 uint32_t subnet_ipv4_addr,
141 uint32_t subnet_prefixlen,
142 uint32_t host_ipv4_addr);
143
Hugo Benichi8d622b52020-08-13 15:24:12 +0900144 // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given
145 // virtual device |int_ifname| associated to |source|. if |ext_ifname| is
146 // empty, the device is implicitly routed through the highest priority
147 // network.
148 virtual void StartRoutingDevice(const std::string& ext_ifname,
149 const std::string& int_ifname,
150 uint32_t int_ipv4_addr,
151 TrafficSource source);
152
153 // Removes IPv4 iptables, IP forwarding, and traffic marking for the given
154 // virtual device |int_ifname|.
155 virtual void StopRoutingDevice(const std::string& ext_ifname,
156 const std::string& int_ifname,
157 uint32_t int_ipv4_addr,
158 TrafficSource source);
159
Hugo Benichi76be34a2020-08-26 22:35:54 +0900160 // Starts or stops marking conntrack entries routed to |ext_ifname| with its
161 // associated fwmark routing tag. Once a conntrack entry is marked with the
162 // fwmark routing tag of a external device, the connection will be pinned
163 // to that deviced if conntrack fwmark restore is set for the source.
164 virtual void StartConnectionPinning(const std::string& ext_ifname);
165 virtual void StopConnectionPinning(const std::string& ext_ifname);
166
Taoyu Li90c13912019-11-26 17:56:54 +0900167 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900168 virtual bool MaskInterfaceFlags(const std::string& ifname,
169 uint16_t on,
170 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900171
Hugo Benichid82d8832020-08-14 10:05:03 +0900172 // Convenience functions for enabling or disabling IPv6 forwarding in both
173 // directions between a pair of interfaces
Taoyu Li90c13912019-11-26 17:56:54 +0900174 virtual bool AddIPv6Forwarding(const std::string& ifname1,
175 const std::string& ifname2);
176 virtual void RemoveIPv6Forwarding(const std::string& ifname1,
177 const std::string& ifname2);
178
Garrick Evans260ff302019-07-25 11:22:50 +0900179 virtual bool AddIPv6HostRoute(const std::string& ifname,
180 const std::string& ipv6_addr,
181 int ipv6_prefix_len);
182 virtual void RemoveIPv6HostRoute(const std::string& ifname,
183 const std::string& ipv6_addr,
184 int ipv6_prefix_len);
185
Taoyu Lia0727dc2020-09-24 19:54:59 +0900186 virtual bool AddIPv6Address(const std::string& ifname,
187 const std::string& ipv6_addr);
188 virtual void RemoveIPv6Address(const std::string& ifname,
189 const std::string& ipv6_addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900190
Hugo Benichie8758b52020-04-03 14:49:01 +0900191 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
192 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900193 virtual bool AddIPv4Route(uint32_t gateway_addr,
194 uint32_t addr,
195 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900196 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
197 uint32_t addr,
198 uint32_t netmask);
199 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
200 // subnet defined by |addr| and |netmask|.
201 virtual bool AddIPv4Route(const std::string& ifname,
202 uint32_t addr,
203 uint32_t netmask);
204 virtual bool DeleteIPv4Route(const std::string& ifname,
205 uint32_t addr,
206 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900207
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900208 // Adds (or deletes) an iptables rule for ADB port forwarding.
209 virtual bool AddAdbPortForwardRule(const std::string& ifname);
210 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
211
212 // Adds (or deletes) an iptables rule for ADB port access.
213 virtual bool AddAdbPortAccessRule(const std::string& ifname);
214 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
215
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900216 // Set or override the interface name to index mapping for |ifname|.
217 // Only used for testing.
218 void SetIfnameIndex(const std::string& ifname, int ifindex);
219
Garrick Evans260ff302019-07-25 11:22:50 +0900220 MinijailedProcessRunner& runner() const;
221
Garrick Evansf0ab7132019-06-18 14:50:42 +0900222 private:
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900223 // Creates a virtual interface pair.
224 bool AddVirtualInterfacePair(const std::string& netns_name,
225 const std::string& veth_ifname,
226 const std::string& peer_ifname);
227 // Sets the configuration of an interface.
228 bool ConfigureInterface(const std::string& ifname,
229 const MacAddress& mac_addr,
230 uint32_t ipv4_addr,
231 uint32_t ipv4_prefix_len,
232 bool up,
233 bool enable_multicast);
234 // Sets the link status.
235 bool ToggleInterface(const std::string& ifname, bool up);
236 // Starts or stops accepting IP traffic forwarded between |iif| and |oif|
237 // by adding or removing ACCEPT rules in the filter FORWARD chain of iptables
238 // and/or ip6tables. If |iif| is empty, only specifies |oif| as the output
239 // interface. If |iif| is empty, only specifies |iif| as the input interface.
240 // |oif| and |iif| cannot be both empty.
241 bool StartIpForwarding(IpFamily family,
242 const std::string& iif,
243 const std::string& oif);
244 bool StopIpForwarding(IpFamily family,
245 const std::string& iif,
246 const std::string& oif);
247 // Create (or delete) pre-routing rules allowing direct ingress on |ifname|
248 // to guest destination |ipv4_addr|.
249 bool AddInboundIPv4DNAT(const std::string& ifname,
250 const std::string& ipv4_addr);
251 void RemoveInboundIPv4DNAT(const std::string& ifname,
252 const std::string& ipv4_addr);
253 // Create (or delete) a forwarding rule for |ifname|.
254 bool AddOutboundIPv4(const std::string& ifname);
255 void RemoveOutboundIPv4(const std::string& ifname);
256 // Creates (or deletes) the forwarding and postrouting rules for SNAT
257 // fwmarked IPv4 traffic.
258 bool AddSNATMarkRules();
259 void RemoveSNATMarkRules();
260 // Creates (or delete) a SNAT rule for traffic exiting |ifname|.
261 bool AddInterfaceSNAT(const std::string& ifname);
262 void RemoveInterfaceSNAT(const std::string& ifname);
263 // Create (or delete) a mangle PREROUTING rule for marking IPv4 traffic
264 // outgoing of |ifname| with the SNAT fwmark value 0x1.
265 // TODO(hugobenichi) Refer to RoutingService to obtain the fwmark value and
266 // add a fwmark mask in the generated rule.
267 bool AddOutboundIPv4SNATMark(const std::string& ifname);
268 void RemoveOutboundIPv4SNATMark(const std::string& ifname);
269 // Create (or delete) a forward rule for established connections.
270 bool AddForwardEstablishedRule();
271 void RemoveForwardEstablishedRule();
272
Hugo Benichi76be34a2020-08-26 22:35:54 +0900273 bool ModifyConnmarkSetPostrouting(IpFamily family,
274 const std::string& op,
275 const std::string& oif);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900276 bool ModifyConnmarkRestore(IpFamily family,
277 const std::string& chain,
278 const std::string& op,
279 const std::string& iif);
280 bool ModifyFwmarkRoutingTag(const std::string& op,
281 const std::string& ext_ifname,
282 const std::string& int_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900283 bool ModifyFwmarkSourceTag(const std::string& op,
284 const std::string& iif,
285 TrafficSource source);
286 bool ModifyFwmarkPrerouting(IpFamily family,
287 const std::string& op,
288 const std::string& iif,
289 Fwmark mark,
290 Fwmark mask,
291 bool log_failures = true);
Hugo Benichid82d8832020-08-14 10:05:03 +0900292 bool ModifyIpForwarding(IpFamily family,
293 const std::string& op,
294 const std::string& iif,
295 const std::string& oif,
296 bool log_failures = true);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900297 bool ModifyRtentry(ioctl_req_t op, struct rtentry* route);
298 int FindIfIndex(const std::string& ifname);
Hugo Benichid82d8832020-08-14 10:05:03 +0900299
Garrick Evansf0ab7132019-06-18 14:50:42 +0900300 MinijailedProcessRunner* process_runner_;
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900301 Firewall* firewall_;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900302 ioctl_t ioctl_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900303
Hugo Benichi82ed5cf2020-09-08 21:30:22 +0900304 FRIEND_TEST(DatapathTest, AddForwardEstablishedRule);
305 FRIEND_TEST(DatapathTest, AddInboundIPv4DNAT);
306 FRIEND_TEST(DatapathTest, AddInterfaceSNAT);
307 FRIEND_TEST(DatapathTest, AddOutboundIPv4);
308 FRIEND_TEST(DatapathTest, AddOutboundIPv4SNATMark);
309 FRIEND_TEST(DatapathTest, AddSNATMarkRules);
310 FRIEND_TEST(DatapathTest, AddVirtualInterfacePair);
311 FRIEND_TEST(DatapathTest, ConfigureInterface);
312 FRIEND_TEST(DatapathTest, RemoveForwardEstablishedRule);
313 FRIEND_TEST(DatapathTest, RemoveInboundIPv4DNAT);
314 FRIEND_TEST(DatapathTest, RemoveInterfaceSNAT);
315 FRIEND_TEST(DatapathTest, RemoveOutboundIPv4);
316 FRIEND_TEST(DatapathTest, RemoveOutboundIPv4SNATMark);
317 FRIEND_TEST(DatapathTest, RemoveSNATMarkRules);
318 FRIEND_TEST(DatapathTest, StartStopIpForwarding);
319 FRIEND_TEST(DatapathTest, ToggleInterface);
320
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900321 // A map used for remembering the interface index of an interface. This
322 // information is necessary when cleaning up iptables fwmark rules that
323 // directly references the interface index. When removing these rules on
324 // an RTM_DELLINK event, the interface index cannot be retrieved anymore.
325 // A new entry is only added when a new physical device appears, and entries
326 // are not removed.
327 // TODO(b/161507671) Rely on RoutingService to obtain this information once
328 // shill/routing_table.cc has been migrated to patchpanel.
329 std::map<std::string, int> if_nametoindex_;
Hugo Benichie8758b52020-04-03 14:49:01 +0900330
Garrick Evansf0ab7132019-06-18 14:50:42 +0900331 DISALLOW_COPY_AND_ASSIGN(Datapath);
332};
333
Garrick Evans3388a032020-03-24 11:25:55 +0900334} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900335
Garrick Evans3388a032020-03-24 11:25:55 +0900336#endif // PATCHPANEL_DATAPATH_H_