blob: 5ec53a55e42c5a0d686431732099c56b46cb552b [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 Benichi8d622b52020-08-13 15:24:12 +0900137 // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given
138 // virtual device |int_ifname| associated to |source|. if |ext_ifname| is
139 // empty, the device is implicitly routed through the highest priority
140 // network.
141 virtual void StartRoutingDevice(const std::string& ext_ifname,
142 const std::string& int_ifname,
143 uint32_t int_ipv4_addr,
144 TrafficSource source);
145
146 // Removes IPv4 iptables, IP forwarding, and traffic marking for the given
147 // virtual device |int_ifname|.
148 virtual void StopRoutingDevice(const std::string& ext_ifname,
149 const std::string& int_ifname,
150 uint32_t int_ipv4_addr,
151 TrafficSource source);
152
Hugo Benichi76be34a2020-08-26 22:35:54 +0900153 // Starts or stops marking conntrack entries routed to |ext_ifname| with its
154 // associated fwmark routing tag. Once a conntrack entry is marked with the
155 // fwmark routing tag of a external device, the connection will be pinned
156 // to that deviced if conntrack fwmark restore is set for the source.
157 virtual void StartConnectionPinning(const std::string& ext_ifname);
158 virtual void StopConnectionPinning(const std::string& ext_ifname);
159
Garrick Evansf0ab7132019-06-18 14:50:42 +0900160 // Create (or delete) pre-routing rules allowing direct ingress on |ifname|
161 // to guest desintation |ipv4_addr|.
162 virtual bool AddInboundIPv4DNAT(const std::string& ifname,
163 const std::string& ipv4_addr);
164 virtual void RemoveInboundIPv4DNAT(const std::string& ifname,
165 const std::string& ipv4_addr);
166
167 // Create (or delete) a forwarding rule for |ifname|.
168 virtual bool AddOutboundIPv4(const std::string& ifname);
169 virtual void RemoveOutboundIPv4(const std::string& ifname);
170
Garrick Evansd291af62020-05-25 10:39:06 +0900171 // Creates (or deletes) the forwarding and postrouting rules for SNAT
172 // fwmarked IPv4 traffic.
173 virtual bool AddSNATMarkRules();
174 virtual void RemoveSNATMarkRules();
175
Garrick Evansff6e37f2020-05-25 10:54:47 +0900176 virtual bool AddInterfaceSNAT(const std::string& ifname);
177 virtual void RemoveInterfaceSNAT(const std::string& ifname);
178
Hugo Benichie8758b52020-04-03 14:49:01 +0900179 // Create (or delete) a mangle PREROUTING rule for marking IPv4 traffic
180 // outgoing of |ifname| with the SNAT fwmark value 0x1.
181 // TODO(hugobenichi) Refer to RoutingService to obtain the fwmark value and
182 // add a fwmark mask in the generated rule.
183 virtual bool AddOutboundIPv4SNATMark(const std::string& ifname);
184 virtual void RemoveOutboundIPv4SNATMark(const std::string& ifname);
185
Garrick Evansd291af62020-05-25 10:39:06 +0900186 // Create (or delete) a forward rule for established connections.
187 virtual bool AddForwardEstablishedRule();
188 virtual void RemoveForwardEstablishedRule();
189
Taoyu Li90c13912019-11-26 17:56:54 +0900190 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900191 virtual bool MaskInterfaceFlags(const std::string& ifname,
192 uint16_t on,
193 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900194
Hugo Benichid82d8832020-08-14 10:05:03 +0900195 // Starts or stops accepting IP traffic forwarded between |iif| and |oif|
196 // by adding or removing ACCEPT rules in the filter FORWARD chain of iptables
197 // and/or ip6tables. If |iif| is empty, only specifies |oif| as the output
198 // interface. If |iif| is empty, only specifies |iif| as the input interface.
199 // |oif| and |iif| cannot be both empty.
200 virtual bool StartIpForwarding(IpFamily family,
201 const std::string& iif,
202 const std::string& oif);
203 virtual bool StopIpForwarding(IpFamily family,
204 const std::string& iif,
205 const std::string& oif);
206
207 // Convenience functions for enabling or disabling IPv6 forwarding in both
208 // directions between a pair of interfaces
Taoyu Li90c13912019-11-26 17:56:54 +0900209 virtual bool AddIPv6Forwarding(const std::string& ifname1,
210 const std::string& ifname2);
211 virtual void RemoveIPv6Forwarding(const std::string& ifname1,
212 const std::string& ifname2);
213
Garrick Evans260ff302019-07-25 11:22:50 +0900214 virtual bool AddIPv6HostRoute(const std::string& ifname,
215 const std::string& ipv6_addr,
216 int ipv6_prefix_len);
217 virtual void RemoveIPv6HostRoute(const std::string& ifname,
218 const std::string& ipv6_addr,
219 int ipv6_prefix_len);
220
Taoyu Lia0727dc2020-09-24 19:54:59 +0900221 virtual bool AddIPv6Address(const std::string& ifname,
222 const std::string& ipv6_addr);
223 virtual void RemoveIPv6Address(const std::string& ifname,
224 const std::string& ipv6_addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900225
Hugo Benichie8758b52020-04-03 14:49:01 +0900226 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
227 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900228 virtual bool AddIPv4Route(uint32_t gateway_addr,
229 uint32_t addr,
230 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900231 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
232 uint32_t addr,
233 uint32_t netmask);
234 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
235 // subnet defined by |addr| and |netmask|.
236 virtual bool AddIPv4Route(const std::string& ifname,
237 uint32_t addr,
238 uint32_t netmask);
239 virtual bool DeleteIPv4Route(const std::string& ifname,
240 uint32_t addr,
241 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900242
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900243 // Adds (or deletes) an iptables rule for ADB port forwarding.
244 virtual bool AddAdbPortForwardRule(const std::string& ifname);
245 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
246
247 // Adds (or deletes) an iptables rule for ADB port access.
248 virtual bool AddAdbPortAccessRule(const std::string& ifname);
249 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
250
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900251 // Set or override the interface name to index mapping for |ifname|.
252 // Only used for testing.
253 void SetIfnameIndex(const std::string& ifname, int ifindex);
254
Garrick Evans260ff302019-07-25 11:22:50 +0900255 MinijailedProcessRunner& runner() const;
256
Garrick Evansf0ab7132019-06-18 14:50:42 +0900257 private:
Hugo Benichi76be34a2020-08-26 22:35:54 +0900258 bool ModifyConnmarkSetPostrouting(IpFamily family,
259 const std::string& op,
260 const std::string& oif);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900261 bool ModifyConnmarkRestore(IpFamily family,
262 const std::string& chain,
263 const std::string& op,
264 const std::string& iif);
265 bool ModifyFwmarkRoutingTag(const std::string& op,
266 const std::string& ext_ifname,
267 const std::string& int_ifname);
Hugo Benichi9be19b12020-08-14 15:33:40 +0900268 bool ModifyFwmarkSourceTag(const std::string& op,
269 const std::string& iif,
270 TrafficSource source);
271 bool ModifyFwmarkPrerouting(IpFamily family,
272 const std::string& op,
273 const std::string& iif,
274 Fwmark mark,
275 Fwmark mask,
276 bool log_failures = true);
Hugo Benichid82d8832020-08-14 10:05:03 +0900277 bool ModifyIpForwarding(IpFamily family,
278 const std::string& op,
279 const std::string& iif,
280 const std::string& oif,
281 bool log_failures = true);
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900282 bool ModifyRtentry(ioctl_req_t op, struct rtentry* route);
283 int FindIfIndex(const std::string& ifname);
Hugo Benichid82d8832020-08-14 10:05:03 +0900284
Garrick Evansf0ab7132019-06-18 14:50:42 +0900285 MinijailedProcessRunner* process_runner_;
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900286 Firewall* firewall_;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900287 ioctl_t ioctl_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900288
Hugo Benichiaf9d8a72020-08-26 13:28:13 +0900289 // A map used for remembering the interface index of an interface. This
290 // information is necessary when cleaning up iptables fwmark rules that
291 // directly references the interface index. When removing these rules on
292 // an RTM_DELLINK event, the interface index cannot be retrieved anymore.
293 // A new entry is only added when a new physical device appears, and entries
294 // are not removed.
295 // TODO(b/161507671) Rely on RoutingService to obtain this information once
296 // shill/routing_table.cc has been migrated to patchpanel.
297 std::map<std::string, int> if_nametoindex_;
Hugo Benichie8758b52020-04-03 14:49:01 +0900298
Garrick Evansf0ab7132019-06-18 14:50:42 +0900299 DISALLOW_COPY_AND_ASSIGN(Datapath);
300};
301
Garrick Evans3388a032020-03-24 11:25:55 +0900302} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900303
Garrick Evans3388a032020-03-24 11:25:55 +0900304#endif // PATCHPANEL_DATAPATH_H_