Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 1 | // 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_DATAPATH_H_ |
| 6 | #define PATCHPANEL_DATAPATH_H_ |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 7 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 8 | #include <net/route.h> |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 9 | #include <sys/types.h> |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 10 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include <base/macros.h> |
| 14 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 15 | #include "patchpanel/firewall.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 16 | #include "patchpanel/mac_address_generator.h" |
| 17 | #include "patchpanel/minijailed_process_runner.h" |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 18 | #include "patchpanel/routing_service.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 19 | #include "patchpanel/subnet.h" |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 20 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 21 | namespace patchpanel { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 22 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 23 | // Simple enum of bitmasks used for specifying a set of IP family values. |
| 24 | enum IpFamily { |
| 25 | NONE = 0, |
| 26 | IPv4 = 1 << 0, |
| 27 | IPv6 = 1 << 1, |
| 28 | Dual = IPv4 | IPv6, //(1 << 0) | (1 << 1); |
| 29 | }; |
| 30 | |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 31 | // 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. |
| 34 | using ioctl_req_t = unsigned long; |
| 35 | typedef int (*ioctl_t)(int, ioctl_req_t, ...); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 36 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 37 | // Returns for given interface name the host name of a ARC veth pair. |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 38 | std::string ArcVethHostName(const std::string& ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 39 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 40 | // Returns the ARC bridge interface name for the given interface. |
| 41 | std::string ArcBridgeName(const std::string& ifname); |
| 42 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 43 | // ARC networking data path configuration utility. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 44 | // IPV4 addresses are always specified in singular dotted-form (a.b.c.d) |
| 45 | // (not in CIDR representation |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 46 | class Datapath { |
| 47 | public: |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 48 | // |process_runner| and |firewall| must not be null; it is not owned. |
| 49 | Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 50 | // Provided for testing only. |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 51 | Datapath(MinijailedProcessRunner* process_runner, |
| 52 | Firewall* firewall, |
| 53 | ioctl_t ioctl_hook); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 54 | virtual ~Datapath() = default; |
| 55 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 56 | // Attaches the name |netns_name| to a network namespace identified by |
| 57 | // |netns_pid|. If |netns_name| had already been created, it will be deleted |
| 58 | // first. |
| 59 | virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid); |
| 60 | |
| 61 | // Deletes the name |netns_name| of a network namespace. |
| 62 | virtual bool NetnsDeleteName(const std::string& netns_name); |
| 63 | |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 64 | virtual bool AddBridge(const std::string& ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 65 | uint32_t ipv4_addr, |
| 66 | uint32_t ipv4_prefix_len); |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 67 | virtual void RemoveBridge(const std::string& ifname); |
| 68 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 69 | virtual bool AddToBridge(const std::string& br_ifname, |
| 70 | const std::string& ifname); |
| 71 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 72 | // Adds a new TAP device. |
| 73 | // |name| may be empty, in which case a default device name will be used; |
| 74 | // it may be a template (e.g. vmtap%d), in which case the kernel will |
| 75 | // generate the name; or it may be fully defined. In all cases, upon success, |
| 76 | // the function returns the actual name of the interface. |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 77 | // |mac_addr| and |ipv4_addr| should be null if this interface will be later |
| 78 | // bridged. |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 79 | // If |user| is empty, no owner will be set |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 80 | virtual std::string AddTAP(const std::string& name, |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 81 | const MacAddress* mac_addr, |
| 82 | const SubnetAddress* ipv4_addr, |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 83 | const std::string& user); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 84 | |
| 85 | // |ifname| must be the actual name of the interface. |
| 86 | virtual void RemoveTAP(const std::string& ifname); |
| 87 | |
| 88 | // The following are iptables methods. |
| 89 | // When specified, |ipv4_addr| is always singlar dotted-form (a.b.c.d) |
| 90 | // IPv4 address (not a CIDR representation). |
| 91 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 92 | // Creates a virtual interface pair split across the current namespace and the |
| 93 | // namespace corresponding to |pid|, and set up the remote interface |
| 94 | // |peer_ifname| according // to the given parameters. |
| 95 | virtual bool ConnectVethPair(pid_t pid, |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 96 | const std::string& netns_name, |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 97 | const std::string& veth_ifname, |
| 98 | const std::string& peer_ifname, |
| 99 | const MacAddress& remote_mac_addr, |
| 100 | uint32_t remote_ipv4_addr, |
| 101 | uint32_t remote_ipv4_prefix_len, |
| 102 | bool remote_multicast_flag); |
| 103 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 104 | // Creates a virtual interface pair. |
Hugo Benichi | 9ab5a05 | 2020-07-28 11:29:01 +0900 | [diff] [blame] | 105 | virtual bool AddVirtualInterfacePair(const std::string& netns_name, |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 106 | const std::string& veth_ifname, |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 107 | const std::string& peer_ifname); |
| 108 | |
| 109 | // Sets the link status. |
| 110 | virtual bool ToggleInterface(const std::string& ifname, bool up); |
| 111 | |
| 112 | // Sets the configuration of an interface. |
| 113 | virtual bool ConfigureInterface(const std::string& ifname, |
| 114 | const MacAddress& mac_addr, |
| 115 | uint32_t ipv4_addr, |
| 116 | uint32_t ipv4_prefix_len, |
| 117 | bool up, |
| 118 | bool enable_multicast); |
| 119 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 120 | virtual void RemoveInterface(const std::string& ifname); |
| 121 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 122 | // Sets up IPv4 SNAT, IP forwarding, and traffic marking for the given |
| 123 | // virtual device |int_ifname| associated to |source|. if |ext_ifname| is |
| 124 | // empty, the device is implicitly routed through the highest priority |
| 125 | // network. |
| 126 | virtual void StartRoutingDevice(const std::string& ext_ifname, |
| 127 | const std::string& int_ifname, |
| 128 | uint32_t int_ipv4_addr, |
| 129 | TrafficSource source); |
| 130 | |
| 131 | // Removes IPv4 iptables, IP forwarding, and traffic marking for the given |
| 132 | // virtual device |int_ifname|. |
| 133 | virtual void StopRoutingDevice(const std::string& ext_ifname, |
| 134 | const std::string& int_ifname, |
| 135 | uint32_t int_ipv4_addr, |
| 136 | TrafficSource source); |
| 137 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 138 | // Create (or delete) pre-routing rules allowing direct ingress on |ifname| |
| 139 | // to guest desintation |ipv4_addr|. |
| 140 | virtual bool AddInboundIPv4DNAT(const std::string& ifname, |
| 141 | const std::string& ipv4_addr); |
| 142 | virtual void RemoveInboundIPv4DNAT(const std::string& ifname, |
| 143 | const std::string& ipv4_addr); |
| 144 | |
| 145 | // Create (or delete) a forwarding rule for |ifname|. |
| 146 | virtual bool AddOutboundIPv4(const std::string& ifname); |
| 147 | virtual void RemoveOutboundIPv4(const std::string& ifname); |
| 148 | |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 149 | // Creates (or deletes) the forwarding and postrouting rules for SNAT |
| 150 | // fwmarked IPv4 traffic. |
| 151 | virtual bool AddSNATMarkRules(); |
| 152 | virtual void RemoveSNATMarkRules(); |
| 153 | |
Garrick Evans | ff6e37f | 2020-05-25 10:54:47 +0900 | [diff] [blame] | 154 | virtual bool AddInterfaceSNAT(const std::string& ifname); |
| 155 | virtual void RemoveInterfaceSNAT(const std::string& ifname); |
| 156 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 157 | // Create (or delete) a mangle PREROUTING rule for marking IPv4 traffic |
| 158 | // outgoing of |ifname| with the SNAT fwmark value 0x1. |
| 159 | // TODO(hugobenichi) Refer to RoutingService to obtain the fwmark value and |
| 160 | // add a fwmark mask in the generated rule. |
| 161 | virtual bool AddOutboundIPv4SNATMark(const std::string& ifname); |
| 162 | virtual void RemoveOutboundIPv4SNATMark(const std::string& ifname); |
| 163 | |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 164 | // Create (or delete) a forward rule for established connections. |
| 165 | virtual bool AddForwardEstablishedRule(); |
| 166 | virtual void RemoveForwardEstablishedRule(); |
| 167 | |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 168 | // Methods supporting IPv6 configuration for ARC. |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 169 | virtual bool MaskInterfaceFlags(const std::string& ifname, |
| 170 | uint16_t on, |
| 171 | uint16_t off = 0); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 172 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 173 | // Starts or stops accepting IP traffic forwarded between |iif| and |oif| |
| 174 | // by adding or removing ACCEPT rules in the filter FORWARD chain of iptables |
| 175 | // and/or ip6tables. If |iif| is empty, only specifies |oif| as the output |
| 176 | // interface. If |iif| is empty, only specifies |iif| as the input interface. |
| 177 | // |oif| and |iif| cannot be both empty. |
| 178 | virtual bool StartIpForwarding(IpFamily family, |
| 179 | const std::string& iif, |
| 180 | const std::string& oif); |
| 181 | virtual bool StopIpForwarding(IpFamily family, |
| 182 | const std::string& iif, |
| 183 | const std::string& oif); |
| 184 | |
| 185 | // Convenience functions for enabling or disabling IPv6 forwarding in both |
| 186 | // directions between a pair of interfaces |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 187 | virtual bool AddIPv6Forwarding(const std::string& ifname1, |
| 188 | const std::string& ifname2); |
| 189 | virtual void RemoveIPv6Forwarding(const std::string& ifname1, |
| 190 | const std::string& ifname2); |
| 191 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 192 | virtual bool AddIPv6HostRoute(const std::string& ifname, |
| 193 | const std::string& ipv6_addr, |
| 194 | int ipv6_prefix_len); |
| 195 | virtual void RemoveIPv6HostRoute(const std::string& ifname, |
| 196 | const std::string& ipv6_addr, |
| 197 | int ipv6_prefix_len); |
| 198 | |
| 199 | virtual bool AddIPv6Neighbor(const std::string& ifname, |
| 200 | const std::string& ipv6_addr); |
| 201 | virtual void RemoveIPv6Neighbor(const std::string& ifname, |
| 202 | const std::string& ipv6_addr); |
| 203 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 204 | // Adds (or deletes) a route to direct to |gateway_addr| the traffic destined |
| 205 | // to the subnet defined by |addr| and |netmask|. |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 206 | virtual bool AddIPv4Route(uint32_t gateway_addr, |
| 207 | uint32_t addr, |
| 208 | uint32_t netmask); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 209 | virtual bool DeleteIPv4Route(uint32_t gateway_addr, |
| 210 | uint32_t addr, |
| 211 | uint32_t netmask); |
| 212 | // Adds (or deletes) a route to direct to |ifname| the traffic destined to the |
| 213 | // subnet defined by |addr| and |netmask|. |
| 214 | virtual bool AddIPv4Route(const std::string& ifname, |
| 215 | uint32_t addr, |
| 216 | uint32_t netmask); |
| 217 | virtual bool DeleteIPv4Route(const std::string& ifname, |
| 218 | uint32_t addr, |
| 219 | uint32_t netmask); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 220 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 221 | // Adds (or deletes) an iptables rule for ADB port forwarding. |
| 222 | virtual bool AddAdbPortForwardRule(const std::string& ifname); |
| 223 | virtual void DeleteAdbPortForwardRule(const std::string& ifname); |
| 224 | |
| 225 | // Adds (or deletes) an iptables rule for ADB port access. |
| 226 | virtual bool AddAdbPortAccessRule(const std::string& ifname); |
| 227 | virtual void DeleteAdbPortAccessRule(const std::string& ifname); |
| 228 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 229 | MinijailedProcessRunner& runner() const; |
| 230 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 231 | private: |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 232 | bool ModifyIpForwarding(IpFamily family, |
| 233 | const std::string& op, |
| 234 | const std::string& iif, |
| 235 | const std::string& oif, |
| 236 | bool log_failures = true); |
| 237 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 238 | MinijailedProcessRunner* process_runner_; |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 239 | Firewall* firewall_; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 240 | ioctl_t ioctl_; |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 241 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 242 | bool ModifyRtentry(unsigned long op, struct rtentry* route); |
| 243 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 244 | DISALLOW_COPY_AND_ASSIGN(Datapath); |
| 245 | }; |
| 246 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 247 | } // namespace patchpanel |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 248 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 249 | #endif // PATCHPANEL_DATAPATH_H_ |