blob: 91467f68515ad6e49806ca1781b44242ac46c8b6 [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>
14
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090015#include "patchpanel/firewall.h"
Garrick Evans3388a032020-03-24 11:25:55 +090016#include "patchpanel/mac_address_generator.h"
17#include "patchpanel/minijailed_process_runner.h"
Hugo Benichi8d622b52020-08-13 15:24:12 +090018#include "patchpanel/routing_service.h"
Garrick Evans3388a032020-03-24 11:25:55 +090019#include "patchpanel/subnet.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +090020
Garrick Evans3388a032020-03-24 11:25:55 +090021namespace patchpanel {
Garrick Evansf0ab7132019-06-18 14:50:42 +090022
Hugo Benichid82d8832020-08-14 10:05:03 +090023// Simple enum of bitmasks used for specifying a set of IP family values.
24enum IpFamily {
25 NONE = 0,
26 IPv4 = 1 << 0,
27 IPv6 = 1 << 1,
Taoyu Lia0727dc2020-09-24 19:54:59 +090028 Dual = IPv4 | IPv6, // (1 << 0) | (1 << 1);
Hugo Benichid82d8832020-08-14 10:05:03 +090029};
30
Taoyu Li90c13912019-11-26 17:56:54 +090031// cros lint will yell to force using int16/int64 instead of long here, however
32// note that unsigned long IS the correct signature for ioctl in Linux kernel -
33// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
34using ioctl_req_t = unsigned long;
35typedef int (*ioctl_t)(int, ioctl_req_t, ...);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090036
Garrick Evans54861622019-07-19 09:05:09 +090037// Returns for given interface name the host name of a ARC veth pair.
Garrick Evans2f581a02020-05-11 10:43:35 +090038std::string ArcVethHostName(const std::string& ifname);
Garrick Evans54861622019-07-19 09:05:09 +090039
Garrick Evans8a067562020-05-11 12:47:30 +090040// Returns the ARC bridge interface name for the given interface.
41std::string ArcBridgeName(const std::string& ifname);
42
Garrick Evansf0ab7132019-06-18 14:50:42 +090043// ARC networking data path configuration utility.
Garrick Evans54861622019-07-19 09:05:09 +090044// IPV4 addresses are always specified in singular dotted-form (a.b.c.d)
45// (not in CIDR representation
Garrick Evansf0ab7132019-06-18 14:50:42 +090046class Datapath {
47 public:
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090048 // |process_runner| and |firewall| must not be null; it is not owned.
49 Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090050 // Provided for testing only.
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090051 Datapath(MinijailedProcessRunner* process_runner,
52 Firewall* firewall,
53 ioctl_t ioctl_hook);
Garrick Evansf0ab7132019-06-18 14:50:42 +090054 virtual ~Datapath() = default;
55
Hugo Benichibf811c62020-09-07 17:30:45 +090056 // Start and stop the Datapath, creating or destroying the initial iptables
57 // setup needed for forwarding traffic from VMs and containers and for
58 // fwmark based routing.
59 virtual void Start();
60 virtual void Stop();
61
Hugo Benichi33860d72020-07-09 16:34:01 +090062 // Attaches the name |netns_name| to a network namespace identified by
63 // |netns_pid|. If |netns_name| had already been created, it will be deleted
64 // first.
65 virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
66
67 // Deletes the name |netns_name| of a network namespace.
68 virtual bool NetnsDeleteName(const std::string& netns_name);
69
Garrick Evans8a949dc2019-07-18 16:17:53 +090070 virtual bool AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +090071 uint32_t ipv4_addr,
72 uint32_t ipv4_prefix_len);
Garrick Evans8a949dc2019-07-18 16:17:53 +090073 virtual void RemoveBridge(const std::string& ifname);
74
Garrick Evans621ed262019-11-13 12:28:43 +090075 virtual bool AddToBridge(const std::string& br_ifname,
76 const std::string& ifname);
77
Garrick Evansc7ae82c2019-09-04 16:25:10 +090078 // Adds a new TAP device.
79 // |name| may be empty, in which case a default device name will be used;
80 // it may be a template (e.g. vmtap%d), in which case the kernel will
81 // generate the name; or it may be fully defined. In all cases, upon success,
82 // the function returns the actual name of the interface.
Garrick Evans621ed262019-11-13 12:28:43 +090083 // |mac_addr| and |ipv4_addr| should be null if this interface will be later
84 // bridged.
Garrick Evans4f9f5572019-11-26 10:25:16 +090085 // If |user| is empty, no owner will be set
Garrick Evansc7ae82c2019-09-04 16:25:10 +090086 virtual std::string AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +090087 const MacAddress* mac_addr,
88 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +090089 const std::string& user);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090090
91 // |ifname| must be the actual name of the interface.
92 virtual void RemoveTAP(const std::string& ifname);
93
94 // The following are iptables methods.
95 // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d)
96 // IPv4 address (not a CIDR representation).
97
Hugo Benichi76675592020-04-08 14:29:57 +090098 // Creates a virtual interface pair split across the current namespace and the
99 // namespace corresponding to |pid|, and set up the remote interface
100 // |peer_ifname| according // to the given parameters.
101 virtual bool ConnectVethPair(pid_t pid,
Hugo Benichi33860d72020-07-09 16:34:01 +0900102 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900103 const std::string& veth_ifname,
104 const std::string& peer_ifname,
105 const MacAddress& remote_mac_addr,
106 uint32_t remote_ipv4_addr,
107 uint32_t remote_ipv4_prefix_len,
108 bool remote_multicast_flag);
109
Garrick Evans2470caa2020-03-04 14:15:41 +0900110 // Creates a virtual interface pair.
Hugo Benichi9ab5a052020-07-28 11:29:01 +0900111 virtual bool AddVirtualInterfacePair(const std::string& netns_name,
Hugo Benichi33860d72020-07-09 16:34:01 +0900112 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +0900113 const std::string& peer_ifname);
114
115 // Sets the link status.
116 virtual bool ToggleInterface(const std::string& ifname, bool up);
117
118 // Sets the configuration of an interface.
119 virtual bool ConfigureInterface(const std::string& ifname,
120 const MacAddress& mac_addr,
121 uint32_t ipv4_addr,
122 uint32_t ipv4_prefix_len,
123 bool up,
124 bool enable_multicast);
125
Garrick Evans54861622019-07-19 09:05:09 +0900126 virtual void RemoveInterface(const std::string& ifname);
127
Hugo Benichi321f23b2020-09-25 15:42:05 +0900128 // Create (or delete) an OUTPUT DROP rule for any locally originated traffic
129 // whose src IPv4 matches |src_ip| and would exit |oif|. This is mainly used
130 // for dropping Chrome webRTC traffic incorrectly bound on ARC and other
131 // guests virtual interfaces (chromium:898210).
132 virtual bool AddSourceIPv4DropRule(const std::string& oif,
133 const std::string& src_ip);
134 virtual bool RemoveSourceIPv4DropRule(const std::string& oif,
135 const std::string& src_ip);
136
Hugo Benichi7c342672020-09-08 09:18:14 +0900137 // Creates a virtual ethernet interface pair shared with the client namespace
138 // of |pid| and sets up routing outside and inside the client namespace for
139 // connecting the client namespace to the network.
140 bool StartRoutingNamespace(pid_t pid,
141 const std::string& netns_name,
142 const std::string& host_ifname,
143 const std::string& peer_ifname,
144 uint32_t subnet_ipv4_addr,
145 uint32_t subnet_prefixlen,
146 uint32_t host_ipv4_addr,
147 uint32_t peer_ipv4_addr,
148 const MacAddress& peer_mac_addr);
149 // Destroys the virtual ethernet interface, routing, and network namespace
150 // name set for |netns_name| by StartRoutingNamespace. The default route set
151 // inside the |netns_name| by patchpanel is not destroyed and it is assumed
152 // the client will teardown the namespace.
153 void StopRoutingNamespace(const std::string& netns_name,
154 const std::string& host_ifname,
155 uint32_t subnet_ipv4_addr,
156 uint32_t subnet_prefixlen,
157 uint32_t host_ipv4_addr);
158
Hugo Benichi8d622b52020-08-13 15:24:12 +0900159 // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given
160 // virtual device |int_ifname| associated to |source|. if |ext_ifname| is
161 // empty, the device is implicitly routed through the highest priority
162 // network.
163 virtual void StartRoutingDevice(const std::string& ext_ifname,
164 const std::string& int_ifname,
165 uint32_t int_ipv4_addr,
166 TrafficSource source);
167
168 // Removes IPv4 iptables, IP forwarding, and traffic marking for the given
169 // virtual device |int_ifname|.
170 virtual void StopRoutingDevice(const std::string& ext_ifname,
171 const std::string& int_ifname,
172 uint32_t int_ipv4_addr,
173 TrafficSource source);
174
Hugo Benichi76be34a2020-08-26 22:35:54 +0900175 // Starts or stops marking conntrack entries routed to |ext_ifname| with its
176 // associated fwmark routing tag. Once a conntrack entry is marked with the
177 // fwmark routing tag of a external device, the connection will be pinned
178 // to that deviced if conntrack fwmark restore is set for the source.
179 virtual void StartConnectionPinning(const std::string& ext_ifname);
180 virtual void StopConnectionPinning(const std::string& ext_ifname);
181
Garrick Evansf0ab7132019-06-18 14:50:42 +0900182 // Create (or delete) pre-routing rules allowing direct ingress on |ifname|
183 // to guest desintation |ipv4_addr|.
184 virtual bool AddInboundIPv4DNAT(const std::string& ifname,
185 const std::string& ipv4_addr);
186 virtual void RemoveInboundIPv4DNAT(const std::string& ifname,
187 const std::string& ipv4_addr);
188
189 // Create (or delete) a forwarding rule for |ifname|.
190 virtual bool AddOutboundIPv4(const std::string& ifname);
191 virtual void RemoveOutboundIPv4(const std::string& ifname);
192
Garrick Evansd291af62020-05-25 10:39:06 +0900193 // Creates (or deletes) the forwarding and postrouting rules for SNAT
194 // fwmarked IPv4 traffic.
195 virtual bool AddSNATMarkRules();
196 virtual void RemoveSNATMarkRules();
197
Garrick Evansff6e37f2020-05-25 10:54:47 +0900198 virtual bool AddInterfaceSNAT(const std::string& ifname);
199 virtual void RemoveInterfaceSNAT(const std::string& ifname);
200
Hugo Benichie8758b52020-04-03 14:49:01 +0900201 // Create (or delete) a mangle PREROUTING rule for marking IPv4 traffic
202 // outgoing of |ifname| with the SNAT fwmark value 0x1.
203 // TODO(hugobenichi) Refer to RoutingService to obtain the fwmark value and
204 // add a fwmark mask in the generated rule.
205 virtual bool AddOutboundIPv4SNATMark(const std::string& ifname);
206 virtual void RemoveOutboundIPv4SNATMark(const std::string& ifname);
207
Garrick Evansd291af62020-05-25 10:39:06 +0900208 // Create (or delete) a forward rule for established connections.
209 virtual bool AddForwardEstablishedRule();
210 virtual void RemoveForwardEstablishedRule();
211
Taoyu Li90c13912019-11-26 17:56:54 +0900212 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900213 virtual bool MaskInterfaceFlags(const std::string& ifname,
214 uint16_t on,
215 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900216
Hugo Benichid82d8832020-08-14 10:05:03 +0900217 // Starts or stops accepting IP traffic forwarded between |iif| and |oif|
218 // by adding or removing ACCEPT rules in the filter FORWARD chain of iptables
219 // and/or ip6tables. If |iif| is empty, only specifies |oif| as the output
220 // interface. If |iif| is empty, only specifies |iif| as the input interface.
221 // |oif| and |iif| cannot be both empty.
222 virtual bool StartIpForwarding(IpFamily family,
223 const std::string& iif,
224 const std::string& oif);
225 virtual bool StopIpForwarding(IpFamily family,
226 const std::string& iif,
227 const std::string& oif);
228
229 // Convenience functions for enabling or disabling IPv6 forwarding in both
230 // directions between a pair of interfaces
Taoyu Li90c13912019-11-26 17:56:54 +0900231 virtual bool AddIPv6Forwarding(const std::string& ifname1,
232 const std::string& ifname2);
233 virtual void RemoveIPv6Forwarding(const std::string& ifname1,
234 const std::string& ifname2);
235
Garrick Evans260ff302019-07-25 11:22:50 +0900236 virtual bool AddIPv6HostRoute(const std::string& ifname,
237 const std::string& ipv6_addr,
238 int ipv6_prefix_len);
239 virtual void RemoveIPv6HostRoute(const std::string& ifname,
240 const std::string& ipv6_addr,
241 int ipv6_prefix_len);
242
Taoyu Lia0727dc2020-09-24 19:54:59 +0900243 virtual bool AddIPv6Address(const std::string& ifname,
244 const std::string& ipv6_addr);
245 virtual void RemoveIPv6Address(const std::string& ifname,
246 const std::string& ipv6_addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900247
Hugo Benichie8758b52020-04-03 14:49:01 +0900248 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
249 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900250 virtual bool AddIPv4Route(uint32_t gateway_addr,
251 uint32_t addr,
252 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900253 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
254 uint32_t addr,
255 uint32_t netmask);
256 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
257 // subnet defined by |addr| and |netmask|.
258 virtual bool AddIPv4Route(const std::string& ifname,
259 uint32_t addr,
260 uint32_t netmask);
261 virtual bool DeleteIPv4Route(const std::string& ifname,
262 uint32_t addr,
263 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900264
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900265 // Adds (or deletes) an iptables rule for ADB port forwarding.
266 virtual bool AddAdbPortForwardRule(const std::string& ifname);
267 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
268
269 // Adds (or deletes) an iptables rule for ADB port access.
270 virtual bool AddAdbPortAccessRule(const std::string& ifname);
271 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
272
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900273 // Set or override the interface name to index mapping for |ifname|.
274 // Only used for testing.
275 void SetIfnameIndex(const std::string& ifname, int ifindex);
276
Garrick Evans260ff302019-07-25 11:22:50 +0900277 MinijailedProcessRunner& runner() const;
278
Garrick Evansf0ab7132019-06-18 14:50:42 +0900279 private:
Hugo Benichi76be34a2020-08-26 22:35:54 +0900280 bool ModifyConnmarkSetPostrouting(IpFamily family,
281 const std::string& op,
282 const std::string& oif);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900283 bool ModifyConnmarkRestore(IpFamily family,
284 const std::string& chain,
285 const std::string& op,
286 const std::string& iif);
287 bool ModifyFwmarkRoutingTag(const std::string& op,
288 const std::string& ext_ifname,
289 const std::string& int_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900290 bool ModifyFwmarkSourceTag(const std::string& op,
291 const std::string& iif,
292 TrafficSource source);
293 bool ModifyFwmarkPrerouting(IpFamily family,
294 const std::string& op,
295 const std::string& iif,
296 Fwmark mark,
297 Fwmark mask,
298 bool log_failures = true);
Hugo Benichid82d8832020-08-14 10:05:03 +0900299 bool ModifyIpForwarding(IpFamily family,
300 const std::string& op,
301 const std::string& iif,
302 const std::string& oif,
303 bool log_failures = true);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900304 bool ModifyRtentry(ioctl_req_t op, struct rtentry* route);
305 int FindIfIndex(const std::string& ifname);
Hugo Benichid82d8832020-08-14 10:05:03 +0900306
Garrick Evansf0ab7132019-06-18 14:50:42 +0900307 MinijailedProcessRunner* process_runner_;
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900308 Firewall* firewall_;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900309 ioctl_t ioctl_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900310
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900311 // A map used for remembering the interface index of an interface. This
312 // information is necessary when cleaning up iptables fwmark rules that
313 // directly references the interface index. When removing these rules on
314 // an RTM_DELLINK event, the interface index cannot be retrieved anymore.
315 // A new entry is only added when a new physical device appears, and entries
316 // are not removed.
317 // TODO(b/161507671) Rely on RoutingService to obtain this information once
318 // shill/routing_table.cc has been migrated to patchpanel.
319 std::map<std::string, int> if_nametoindex_;
Hugo Benichie8758b52020-04-03 14:49:01 +0900320
Garrick Evansf0ab7132019-06-18 14:50:42 +0900321 DISALLOW_COPY_AND_ASSIGN(Datapath);
322};
323
Garrick Evans3388a032020-03-24 11:25:55 +0900324} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900325
Garrick Evans3388a032020-03-24 11:25:55 +0900326#endif // PATCHPANEL_DATAPATH_H_