blob: 17079f48dc25b7681e21303ad2b5316a2f88c34c [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"
18#include "patchpanel/subnet.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +090019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Garrick Evansf0ab7132019-06-18 14:50:42 +090021
Taoyu Li90c13912019-11-26 17:56:54 +090022// cros lint will yell to force using int16/int64 instead of long here, however
23// note that unsigned long IS the correct signature for ioctl in Linux kernel -
24// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
25using ioctl_req_t = unsigned long;
26typedef int (*ioctl_t)(int, ioctl_req_t, ...);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090027
Garrick Evans54861622019-07-19 09:05:09 +090028// Returns for given interface name the host name of a ARC veth pair.
Garrick Evans2f581a02020-05-11 10:43:35 +090029std::string ArcVethHostName(const std::string& ifname);
Garrick Evans54861622019-07-19 09:05:09 +090030
Garrick Evans8a067562020-05-11 12:47:30 +090031// Returns the ARC bridge interface name for the given interface.
32std::string ArcBridgeName(const std::string& ifname);
33
Garrick Evansf0ab7132019-06-18 14:50:42 +090034// ARC networking data path configuration utility.
Garrick Evans54861622019-07-19 09:05:09 +090035// IPV4 addresses are always specified in singular dotted-form (a.b.c.d)
36// (not in CIDR representation
Garrick Evansf0ab7132019-06-18 14:50:42 +090037class Datapath {
38 public:
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090039 // |process_runner| and |firewall| must not be null; it is not owned.
40 Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090041 // Provided for testing only.
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090042 Datapath(MinijailedProcessRunner* process_runner,
43 Firewall* firewall,
44 ioctl_t ioctl_hook);
Garrick Evansf0ab7132019-06-18 14:50:42 +090045 virtual ~Datapath() = default;
46
Hugo Benichi33860d72020-07-09 16:34:01 +090047 // Attaches the name |netns_name| to a network namespace identified by
48 // |netns_pid|. If |netns_name| had already been created, it will be deleted
49 // first.
50 virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
51
52 // Deletes the name |netns_name| of a network namespace.
53 virtual bool NetnsDeleteName(const std::string& netns_name);
54
Garrick Evans8a949dc2019-07-18 16:17:53 +090055 virtual bool AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +090056 uint32_t ipv4_addr,
57 uint32_t ipv4_prefix_len);
Garrick Evans8a949dc2019-07-18 16:17:53 +090058 virtual void RemoveBridge(const std::string& ifname);
59
Garrick Evans621ed262019-11-13 12:28:43 +090060 virtual bool AddToBridge(const std::string& br_ifname,
61 const std::string& ifname);
62
Garrick Evansc7ae82c2019-09-04 16:25:10 +090063 // Adds a new TAP device.
64 // |name| may be empty, in which case a default device name will be used;
65 // it may be a template (e.g. vmtap%d), in which case the kernel will
66 // generate the name; or it may be fully defined. In all cases, upon success,
67 // the function returns the actual name of the interface.
Garrick Evans621ed262019-11-13 12:28:43 +090068 // |mac_addr| and |ipv4_addr| should be null if this interface will be later
69 // bridged.
Garrick Evans4f9f5572019-11-26 10:25:16 +090070 // If |user| is empty, no owner will be set
Garrick Evansc7ae82c2019-09-04 16:25:10 +090071 virtual std::string AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +090072 const MacAddress* mac_addr,
73 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +090074 const std::string& user);
Garrick Evansc7ae82c2019-09-04 16:25:10 +090075
76 // |ifname| must be the actual name of the interface.
77 virtual void RemoveTAP(const std::string& ifname);
78
79 // The following are iptables methods.
80 // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d)
81 // IPv4 address (not a CIDR representation).
82
Hugo Benichi76675592020-04-08 14:29:57 +090083 // Creates a virtual interface pair split across the current namespace and the
84 // namespace corresponding to |pid|, and set up the remote interface
85 // |peer_ifname| according // to the given parameters.
86 virtual bool ConnectVethPair(pid_t pid,
Hugo Benichi33860d72020-07-09 16:34:01 +090087 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +090088 const std::string& veth_ifname,
89 const std::string& peer_ifname,
90 const MacAddress& remote_mac_addr,
91 uint32_t remote_ipv4_addr,
92 uint32_t remote_ipv4_prefix_len,
93 bool remote_multicast_flag);
94
Garrick Evans2470caa2020-03-04 14:15:41 +090095 // Creates a virtual interface pair.
Hugo Benichi9ab5a052020-07-28 11:29:01 +090096 virtual bool AddVirtualInterfacePair(const std::string& netns_name,
Hugo Benichi33860d72020-07-09 16:34:01 +090097 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +090098 const std::string& peer_ifname);
99
100 // Sets the link status.
101 virtual bool ToggleInterface(const std::string& ifname, bool up);
102
103 // Sets the configuration of an interface.
104 virtual bool ConfigureInterface(const std::string& ifname,
105 const MacAddress& mac_addr,
106 uint32_t ipv4_addr,
107 uint32_t ipv4_prefix_len,
108 bool up,
109 bool enable_multicast);
110
Garrick Evans54861622019-07-19 09:05:09 +0900111 virtual void RemoveInterface(const std::string& ifname);
112
Garrick Evansf0ab7132019-06-18 14:50:42 +0900113 // Create (or delete) pre-routing rules allowing direct ingress on |ifname|
114 // to guest desintation |ipv4_addr|.
115 virtual bool AddInboundIPv4DNAT(const std::string& ifname,
116 const std::string& ipv4_addr);
117 virtual void RemoveInboundIPv4DNAT(const std::string& ifname,
118 const std::string& ipv4_addr);
119
120 // Create (or delete) a forwarding rule for |ifname|.
121 virtual bool AddOutboundIPv4(const std::string& ifname);
122 virtual void RemoveOutboundIPv4(const std::string& ifname);
123
Garrick Evansd291af62020-05-25 10:39:06 +0900124 // Creates (or deletes) the forwarding and postrouting rules for SNAT
125 // fwmarked IPv4 traffic.
126 virtual bool AddSNATMarkRules();
127 virtual void RemoveSNATMarkRules();
128
Garrick Evansff6e37f2020-05-25 10:54:47 +0900129 virtual bool AddInterfaceSNAT(const std::string& ifname);
130 virtual void RemoveInterfaceSNAT(const std::string& ifname);
131
Hugo Benichie8758b52020-04-03 14:49:01 +0900132 // Create (or delete) a mangle PREROUTING rule for marking IPv4 traffic
133 // outgoing of |ifname| with the SNAT fwmark value 0x1.
134 // TODO(hugobenichi) Refer to RoutingService to obtain the fwmark value and
135 // add a fwmark mask in the generated rule.
136 virtual bool AddOutboundIPv4SNATMark(const std::string& ifname);
137 virtual void RemoveOutboundIPv4SNATMark(const std::string& ifname);
138
Garrick Evansd291af62020-05-25 10:39:06 +0900139 // Create (or delete) a forward rule for established connections.
140 virtual bool AddForwardEstablishedRule();
141 virtual void RemoveForwardEstablishedRule();
142
Taoyu Li90c13912019-11-26 17:56:54 +0900143 // Methods supporting IPv6 configuration for ARC.
Garrick Evans664a82f2019-12-17 12:18:05 +0900144 virtual bool MaskInterfaceFlags(const std::string& ifname,
145 uint16_t on,
146 uint16_t off = 0);
Garrick Evans260ff302019-07-25 11:22:50 +0900147
Taoyu Li90c13912019-11-26 17:56:54 +0900148 virtual bool AddIPv6Forwarding(const std::string& ifname1,
149 const std::string& ifname2);
150 virtual void RemoveIPv6Forwarding(const std::string& ifname1,
151 const std::string& ifname2);
152
Garrick Evans260ff302019-07-25 11:22:50 +0900153 virtual bool AddIPv6HostRoute(const std::string& ifname,
154 const std::string& ipv6_addr,
155 int ipv6_prefix_len);
156 virtual void RemoveIPv6HostRoute(const std::string& ifname,
157 const std::string& ipv6_addr,
158 int ipv6_prefix_len);
159
160 virtual bool AddIPv6Neighbor(const std::string& ifname,
161 const std::string& ipv6_addr);
162 virtual void RemoveIPv6Neighbor(const std::string& ifname,
163 const std::string& ipv6_addr);
164
Hugo Benichie8758b52020-04-03 14:49:01 +0900165 // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined
166 // to the subnet defined by |addr| and |netmask|.
Garrick Evans3d97a392020-02-21 15:24:37 +0900167 virtual bool AddIPv4Route(uint32_t gateway_addr,
168 uint32_t addr,
169 uint32_t netmask);
Hugo Benichie8758b52020-04-03 14:49:01 +0900170 virtual bool DeleteIPv4Route(uint32_t gateway_addr,
171 uint32_t addr,
172 uint32_t netmask);
173 // Adds (or deletes) a route to direct to |ifname| the traffic destined to the
174 // subnet defined by |addr| and |netmask|.
175 virtual bool AddIPv4Route(const std::string& ifname,
176 uint32_t addr,
177 uint32_t netmask);
178 virtual bool DeleteIPv4Route(const std::string& ifname,
179 uint32_t addr,
180 uint32_t netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900181
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900182 // Adds (or deletes) an iptables rule for ADB port forwarding.
183 virtual bool AddAdbPortForwardRule(const std::string& ifname);
184 virtual void DeleteAdbPortForwardRule(const std::string& ifname);
185
186 // Adds (or deletes) an iptables rule for ADB port access.
187 virtual bool AddAdbPortAccessRule(const std::string& ifname);
188 virtual void DeleteAdbPortAccessRule(const std::string& ifname);
189
Garrick Evans260ff302019-07-25 11:22:50 +0900190 MinijailedProcessRunner& runner() const;
191
Garrick Evansf0ab7132019-06-18 14:50:42 +0900192 private:
193 MinijailedProcessRunner* process_runner_;
Jason Jeremy Imana7273a32020-08-04 11:25:31 +0900194 Firewall* firewall_;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900195 ioctl_t ioctl_;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900196
Hugo Benichie8758b52020-04-03 14:49:01 +0900197 bool ModifyRtentry(unsigned long op, struct rtentry* route);
198
Garrick Evansf0ab7132019-06-18 14:50:42 +0900199 DISALLOW_COPY_AND_ASSIGN(Datapath);
200};
201
Garrick Evans3388a032020-03-24 11:25:55 +0900202} // namespace patchpanel
Garrick Evansf0ab7132019-06-18 14:50:42 +0900203
Garrick Evans3388a032020-03-24 11:25:55 +0900204#endif // PATCHPANEL_DATAPATH_H_