blob: d8e15803c4c23c9bd09ab205f0faadc6303d6642 [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#include "patchpanel/datapath.h"
Garrick Evansf0ab7132019-06-18 14:50:42 +09006
Garrick Evans3d97a392020-02-21 15:24:37 +09007#include <arpa/inet.h>
Garrick Evansc7ae82c2019-09-04 16:25:10 +09008#include <fcntl.h>
9#include <linux/if_tun.h>
10#include <linux/sockios.h>
11#include <net/if.h>
12#include <net/if_arp.h>
13#include <netinet/in.h>
14#include <string.h>
15#include <sys/ioctl.h>
16#include <sys/socket.h>
17
18#include <base/files/scoped_file.h>
19#include <base/logging.h>
Taoyu Li79871c92020-07-02 16:09:39 +090020#include <base/posix/eintr_wrapper.h>
Garrick Evans54861622019-07-19 09:05:09 +090021#include <base/strings/string_number_conversions.h>
Garrick Evans4f9f5572019-11-26 10:25:16 +090022#include <brillo/userdb_utils.h>
Garrick Evans54861622019-07-19 09:05:09 +090023
Garrick Evans3388a032020-03-24 11:25:55 +090024#include "patchpanel/net_util.h"
25#include "patchpanel/scoped_ns.h"
Garrick Evansc7ae82c2019-09-04 16:25:10 +090026
Garrick Evans3388a032020-03-24 11:25:55 +090027namespace patchpanel {
Garrick Evans54861622019-07-19 09:05:09 +090028
Garrick Evansc7ae82c2019-09-04 16:25:10 +090029namespace {
Hugo Benichi76675592020-04-08 14:29:57 +090030// TODO(hugobenichi) Consolidate this constant definition in a single place.
31constexpr pid_t kTestPID = -2;
Garrick Evansc7ae82c2019-09-04 16:25:10 +090032constexpr char kDefaultIfname[] = "vmtap%d";
33constexpr char kTunDev[] = "/dev/net/tun";
Hugo Benichie8758b52020-04-03 14:49:01 +090034
Garrick Evans8a067562020-05-11 12:47:30 +090035std::string PrefixIfname(const std::string& prefix, const std::string& ifname) {
36 std::string n = prefix + ifname;
Garrick Evans2f581a02020-05-11 10:43:35 +090037 if (n.length() < IFNAMSIZ)
38 return n;
Garrick Evans54861622019-07-19 09:05:09 +090039
Garrick Evans2f581a02020-05-11 10:43:35 +090040 // Best effort attempt to preserve the interface number, assuming it's the
41 // last char in the name.
42 auto c = ifname[ifname.length() - 1];
43 n.resize(IFNAMSIZ - 1);
44 n[n.length() - 1] = c;
45 return n;
Garrick Evans54861622019-07-19 09:05:09 +090046}
Garrick Evansf0ab7132019-06-18 14:50:42 +090047
Garrick Evans8a067562020-05-11 12:47:30 +090048} // namespace
49
50std::string ArcVethHostName(const std::string& ifname) {
51 return PrefixIfname("veth", ifname);
52}
53
54std::string ArcBridgeName(const std::string& ifname) {
55 return PrefixIfname("arc_", ifname);
56}
57
Garrick Evansf0ab7132019-06-18 14:50:42 +090058Datapath::Datapath(MinijailedProcessRunner* process_runner)
Garrick Evansc7ae82c2019-09-04 16:25:10 +090059 : Datapath(process_runner, ioctl) {}
60
61Datapath::Datapath(MinijailedProcessRunner* process_runner, ioctl_t ioctl_hook)
62 : process_runner_(process_runner), ioctl_(ioctl_hook) {
Garrick Evansf0ab7132019-06-18 14:50:42 +090063 CHECK(process_runner_);
64}
65
Garrick Evans260ff302019-07-25 11:22:50 +090066MinijailedProcessRunner& Datapath::runner() const {
67 return *process_runner_;
68}
69
Hugo Benichi33860d72020-07-09 16:34:01 +090070bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) {
71 // Try first to delete any netns with name |netns_name| in case patchpanel
72 // did not exit cleanly.
73 if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0)
74 LOG(INFO) << "Deleted left over network namespace name " << netns_name;
75 return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0;
76}
77
78bool Datapath::NetnsDeleteName(const std::string& netns_name) {
79 return process_runner_->ip_netns_delete(netns_name) == 0;
80}
81
Garrick Evans8a949dc2019-07-18 16:17:53 +090082bool Datapath::AddBridge(const std::string& ifname,
Garrick Evans7a1a9ee2020-01-28 11:03:57 +090083 uint32_t ipv4_addr,
84 uint32_t ipv4_prefix_len) {
Garrick Evans8a949dc2019-07-18 16:17:53 +090085 // Configure the persistent Chrome OS bridge interface with static IP.
Garrick Evans8e8e3472020-01-23 14:03:50 +090086 if (process_runner_->brctl("addbr", {ifname}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +090087 return false;
88 }
89
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090090 if (process_runner_->ip(
91 "addr", "add",
92 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
93 IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
94 "dev", ifname}) != 0) {
Garrick Evans7a1a9ee2020-01-28 11:03:57 +090095 RemoveBridge(ifname);
96 return false;
97 }
98
99 if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900100 RemoveBridge(ifname);
101 return false;
102 }
103
104 // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules.
Hugo Benichie8758b52020-04-03 14:49:01 +0900105 if (!AddOutboundIPv4SNATMark(ifname)) {
Garrick Evans8a949dc2019-07-18 16:17:53 +0900106 RemoveBridge(ifname);
107 return false;
108 }
109
110 return true;
111}
112
113void Datapath::RemoveBridge(const std::string& ifname) {
Hugo Benichie8758b52020-04-03 14:49:01 +0900114 RemoveOutboundIPv4SNATMark(ifname);
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900115 process_runner_->ip("link", "set", {ifname, "down"});
Garrick Evans8e8e3472020-01-23 14:03:50 +0900116 process_runner_->brctl("delbr", {ifname});
Garrick Evans8a949dc2019-07-18 16:17:53 +0900117}
118
Garrick Evans621ed262019-11-13 12:28:43 +0900119bool Datapath::AddToBridge(const std::string& br_ifname,
120 const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900121 return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0);
Garrick Evans621ed262019-11-13 12:28:43 +0900122}
123
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900124std::string Datapath::AddTAP(const std::string& name,
Garrick Evans621ed262019-11-13 12:28:43 +0900125 const MacAddress* mac_addr,
126 const SubnetAddress* ipv4_addr,
Garrick Evans4f9f5572019-11-26 10:25:16 +0900127 const std::string& user) {
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900128 base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK));
129 if (!dev.is_valid()) {
130 PLOG(ERROR) << "Failed to open " << kTunDev;
131 return "";
132 }
133
134 struct ifreq ifr;
135 memset(&ifr, 0, sizeof(ifr));
136 strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(),
137 sizeof(ifr.ifr_name));
138 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
139
140 // If a template was given as the name, ifr_name will be updated with the
141 // actual interface name.
142 if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900143 PLOG(ERROR) << "Failed to create tap interface " << name;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900144 return "";
145 }
146 const char* ifname = ifr.ifr_name;
147
148 if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900149 PLOG(ERROR) << "Failed to persist the interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900150 return "";
151 }
152
Garrick Evans4f9f5572019-11-26 10:25:16 +0900153 if (!user.empty()) {
154 uid_t uid = -1;
155 if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) {
156 PLOG(ERROR) << "Unable to look up UID for " << user;
157 RemoveTAP(ifname);
158 return "";
159 }
160 if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) {
161 PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface "
162 << ifname;
163 RemoveTAP(ifname);
164 return "";
165 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900166 }
167
Hugo Benichib9b93fe2019-10-25 23:36:01 +0900168 // Create control socket for configuring the interface.
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900169 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
170 if (!sock.is_valid()) {
171 PLOG(ERROR) << "Failed to create control socket for tap interface "
Garrick Evans621ed262019-11-13 12:28:43 +0900172 << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900173 RemoveTAP(ifname);
174 return "";
175 }
176
Garrick Evans621ed262019-11-13 12:28:43 +0900177 if (ipv4_addr) {
178 struct sockaddr_in* addr =
179 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr);
180 addr->sin_family = AF_INET;
181 addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address());
182 if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) {
183 PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname
184 << " {" << ipv4_addr->ToCidrString() << "}";
185 RemoveTAP(ifname);
186 return "";
187 }
188
189 struct sockaddr_in* netmask =
190 reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask);
191 netmask->sin_family = AF_INET;
192 netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask());
193 if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) {
194 PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname
195 << " {" << ipv4_addr->ToCidrString() << "}";
196 RemoveTAP(ifname);
197 return "";
198 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900199 }
200
Garrick Evans621ed262019-11-13 12:28:43 +0900201 if (mac_addr) {
202 struct sockaddr* hwaddr = &ifr.ifr_hwaddr;
203 hwaddr->sa_family = ARPHRD_ETHER;
204 memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr));
205 if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) {
206 PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname
207 << " {" << MacAddressToString(*mac_addr) << "}";
208 RemoveTAP(ifname);
209 return "";
210 }
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900211 }
212
213 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900214 PLOG(ERROR) << "Failed to get flags for tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900215 RemoveTAP(ifname);
216 return "";
217 }
218
219 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
220 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) {
Garrick Evans621ed262019-11-13 12:28:43 +0900221 PLOG(ERROR) << "Failed to enable tap interface " << ifname;
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900222 RemoveTAP(ifname);
223 return "";
224 }
225
226 return ifname;
227}
228
229void Datapath::RemoveTAP(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900230 process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"});
Garrick Evansc7ae82c2019-09-04 16:25:10 +0900231}
232
Hugo Benichi33860d72020-07-09 16:34:01 +0900233bool Datapath::ConnectVethPair(pid_t netns_pid,
234 const std::string& netns_name,
Hugo Benichi76675592020-04-08 14:29:57 +0900235 const std::string& veth_ifname,
236 const std::string& peer_ifname,
237 const MacAddress& remote_mac_addr,
238 uint32_t remote_ipv4_addr,
239 uint32_t remote_ipv4_prefix_len,
240 bool remote_multicast_flag) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900241 // Set up the virtual pair across the current namespace and |netns_name|.
242 if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) {
243 LOG(ERROR) << "Failed to create veth pair " << veth_ifname << ","
244 << peer_ifname;
245 return false;
246 }
247
248 // Configure the remote veth in namespace |netns_name|.
Hugo Benichi76675592020-04-08 14:29:57 +0900249 {
Hugo Benichi33860d72020-07-09 16:34:01 +0900250 ScopedNS ns(netns_pid);
251 if (!ns.IsValid() && netns_pid != kTestPID) {
Hugo Benichi76675592020-04-08 14:29:57 +0900252 LOG(ERROR)
253 << "Cannot create virtual link -- invalid container namespace?";
254 return false;
255 }
256
Hugo Benichi76675592020-04-08 14:29:57 +0900257 if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr,
258 remote_ipv4_prefix_len, true /* link up */,
259 remote_multicast_flag)) {
260 LOG(ERROR) << "Failed to configure interface " << peer_ifname;
261 RemoveInterface(peer_ifname);
262 return false;
263 }
264 }
265
Hugo Benichi76675592020-04-08 14:29:57 +0900266 if (!ToggleInterface(veth_ifname, true /*up*/)) {
267 LOG(ERROR) << "Failed to bring up interface " << veth_ifname;
268 RemoveInterface(veth_ifname);
269 return false;
270 }
Hugo Benichi33860d72020-07-09 16:34:01 +0900271
Hugo Benichi76675592020-04-08 14:29:57 +0900272 return true;
273}
274
Hugo Benichi33860d72020-07-09 16:34:01 +0900275bool Datapath::AddVirtualInterfacePair(const std::string& netns_name,
276 const std::string& veth_ifname,
Garrick Evans2470caa2020-03-04 14:15:41 +0900277 const std::string& peer_ifname) {
Hugo Benichi33860d72020-07-09 16:34:01 +0900278 return process_runner_->ip("link", "add",
279 {veth_ifname, "type", "veth", "peer", "name",
280 peer_ifname, "netns", netns_name}) == 0;
Garrick Evans2470caa2020-03-04 14:15:41 +0900281}
Garrick Evans54861622019-07-19 09:05:09 +0900282
Garrick Evans2470caa2020-03-04 14:15:41 +0900283bool Datapath::ToggleInterface(const std::string& ifname, bool up) {
284 const std::string link = up ? "up" : "down";
285 return process_runner_->ip("link", "set", {ifname, link}) == 0;
286}
Garrick Evans54861622019-07-19 09:05:09 +0900287
Garrick Evans2470caa2020-03-04 14:15:41 +0900288bool Datapath::ConfigureInterface(const std::string& ifname,
289 const MacAddress& mac_addr,
290 uint32_t ipv4_addr,
291 uint32_t ipv4_prefix_len,
292 bool up,
293 bool enable_multicast) {
294 const std::string link = up ? "up" : "down";
295 const std::string multicast = enable_multicast ? "on" : "off";
296 return (process_runner_->ip(
297 "addr", "add",
298 {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd",
299 IPv4AddressToString(
300 Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)),
301 "dev", ifname}) == 0) &&
302 (process_runner_->ip("link", "set",
303 {
304 "dev",
305 ifname,
306 link,
307 "addr",
308 MacAddressToString(mac_addr),
309 "multicast",
310 multicast,
311 }) == 0);
Garrick Evans54861622019-07-19 09:05:09 +0900312}
313
314void Datapath::RemoveInterface(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900315 process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/);
Garrick Evans54861622019-07-19 09:05:09 +0900316}
317
Garrick Evansf0ab7132019-06-18 14:50:42 +0900318bool Datapath::AddLegacyIPv4DNAT(const std::string& ipv4_addr) {
319 // Forward "unclaimed" packets to Android to allow inbound connections
320 // from devices on the LAN.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900321 if (process_runner_->iptables("nat", {"-N", "dnat_arc", "-w"}) != 0)
Garrick Evansf0ab7132019-06-18 14:50:42 +0900322 return false;
323
Garrick Evans8e8e3472020-01-23 14:03:50 +0900324 if (process_runner_->iptables("nat", {"-A", "dnat_arc", "-j", "DNAT",
325 "--to-destination", ipv4_addr, "-w"}) !=
Garrick Evansf0ab7132019-06-18 14:50:42 +0900326 0) {
Garrick Evans54861622019-07-19 09:05:09 +0900327 RemoveLegacyIPv4DNAT();
Garrick Evansf0ab7132019-06-18 14:50:42 +0900328 return false;
329 }
330
331 // This chain is dynamically updated whenever the default interface
332 // changes.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900333 if (process_runner_->iptables("nat", {"-N", "try_arc", "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900334 RemoveLegacyIPv4DNAT();
335 return false;
336 }
337
Garrick Evans8e8e3472020-01-23 14:03:50 +0900338 if (process_runner_->iptables(
339 "nat", {"-A", "PREROUTING", "-m", "socket", "--nowildcard", "-j",
340 "ACCEPT", "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900341 RemoveLegacyIPv4DNAT();
342 return false;
343 }
344
Garrick Evans8e8e3472020-01-23 14:03:50 +0900345 if (process_runner_->iptables("nat", {"-A", "PREROUTING", "-p", "tcp", "-j",
346 "try_arc", "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900347 RemoveLegacyIPv4DNAT();
348 return false;
349 }
350
Garrick Evans8e8e3472020-01-23 14:03:50 +0900351 if (process_runner_->iptables("nat", {"-A", "PREROUTING", "-p", "udp", "-j",
352 "try_arc", "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900353 RemoveLegacyIPv4DNAT();
354 return false;
355 }
356
357 return true;
358}
359
360void Datapath::RemoveLegacyIPv4DNAT() {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900361 process_runner_->iptables(
362 "nat", {"-D", "PREROUTING", "-p", "udp", "-j", "try_arc", "-w"});
363 process_runner_->iptables(
364 "nat", {"-D", "PREROUTING", "-p", "tcp", "-j", "try_arc", "-w"});
365 process_runner_->iptables("nat", {"-D", "PREROUTING", "-m", "socket",
366 "--nowildcard", "-j", "ACCEPT", "-w"});
367 process_runner_->iptables("nat", {"-F", "try_arc", "-w"});
368 process_runner_->iptables("nat", {"-X", "try_arc", "-w"});
369 process_runner_->iptables("nat", {"-F", "dnat_arc", "-w"});
370 process_runner_->iptables("nat", {"-X", "dnat_arc", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900371}
372
Garrick Evans54861622019-07-19 09:05:09 +0900373bool Datapath::AddLegacyIPv4InboundDNAT(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900374 return (process_runner_->iptables("nat", {"-A", "try_arc", "-i", ifname, "-j",
375 "dnat_arc", "-w"}) != 0);
Garrick Evans54861622019-07-19 09:05:09 +0900376}
377
378void Datapath::RemoveLegacyIPv4InboundDNAT() {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900379 process_runner_->iptables("nat", {"-F", "try_arc", "-w"});
Garrick Evans54861622019-07-19 09:05:09 +0900380}
381
Garrick Evansf0ab7132019-06-18 14:50:42 +0900382bool Datapath::AddInboundIPv4DNAT(const std::string& ifname,
383 const std::string& ipv4_addr) {
384 // Direct ingress IP traffic to existing sockets.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900385 if (process_runner_->iptables(
386 "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket",
387 "--nowildcard", "-j", "ACCEPT", "-w"}) != 0)
Garrick Evansf0ab7132019-06-18 14:50:42 +0900388 return false;
389
390 // Direct ingress TCP & UDP traffic to ARC interface for new connections.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900391 if (process_runner_->iptables(
392 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
393 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900394 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
395 return false;
396 }
Garrick Evans8e8e3472020-01-23 14:03:50 +0900397 if (process_runner_->iptables(
398 "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
399 "--to-destination", ipv4_addr, "-w"}) != 0) {
Garrick Evansf0ab7132019-06-18 14:50:42 +0900400 RemoveInboundIPv4DNAT(ifname, ipv4_addr);
401 return false;
402 }
403
404 return true;
405}
406
407void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname,
408 const std::string& ipv4_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900409 process_runner_->iptables(
410 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT",
411 "--to-destination", ipv4_addr, "-w"});
412 process_runner_->iptables(
413 "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT",
414 "--to-destination", ipv4_addr, "-w"});
415 process_runner_->iptables(
416 "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard",
417 "-j", "ACCEPT", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900418}
419
420bool Datapath::AddOutboundIPv4(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900421 return process_runner_->iptables("filter", {"-A", "FORWARD", "-o", ifname,
422 "-j", "ACCEPT", "-w"}) == 0;
Garrick Evansf0ab7132019-06-18 14:50:42 +0900423}
424
425void Datapath::RemoveOutboundIPv4(const std::string& ifname) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900426 process_runner_->iptables(
427 "filter", {"-D", "FORWARD", "-o", ifname, "-j", "ACCEPT", "-w"});
Garrick Evansf0ab7132019-06-18 14:50:42 +0900428}
429
Garrick Evansd291af62020-05-25 10:39:06 +0900430bool Datapath::AddSNATMarkRules() {
Taoyu Li79871c92020-07-02 16:09:39 +0900431 // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore
432 // need to be explicitly dropped.
433 if (process_runner_->iptables(
434 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1", "-m",
435 "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) {
436 return false;
437 }
Garrick Evansd291af62020-05-25 10:39:06 +0900438 if (process_runner_->iptables(
439 "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1", "-j",
440 "ACCEPT", "-w"}) != 0) {
441 return false;
442 }
443 if (process_runner_->iptables(
444 "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1", "-j",
445 "MASQUERADE", "-w"}) != 0) {
446 RemoveSNATMarkRules();
447 return false;
448 }
449 return true;
450}
451
452void Datapath::RemoveSNATMarkRules() {
453 process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark",
454 "1", "-j", "MASQUERADE", "-w"});
455 process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark",
456 "1", "-j", "ACCEPT", "-w"});
Taoyu Li79871c92020-07-02 16:09:39 +0900457 process_runner_->iptables(
458 "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1", "-m", "state",
459 "--state", "INVALID", "-j", "DROP", "-w"});
Garrick Evansd291af62020-05-25 10:39:06 +0900460}
461
Garrick Evansff6e37f2020-05-25 10:54:47 +0900462bool Datapath::AddInterfaceSNAT(const std::string& ifname) {
463 return process_runner_->iptables("nat", {"-A", "POSTROUTING", "-o", ifname,
464 "-j", "MASQUERADE", "-w"}) == 0;
465}
466
467void Datapath::RemoveInterfaceSNAT(const std::string& ifname) {
468 process_runner_->iptables(
469 "nat", {"-D", "POSTROUTING", "-o", ifname, "-j", "MASQUERADE", "-w"});
470}
471
Hugo Benichie8758b52020-04-03 14:49:01 +0900472bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) {
473 return process_runner_->iptables(
474 "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK",
475 "--set-mark", "1", "-w"}) == 0;
476}
477
478void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) {
479 process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j",
480 "MARK", "--set-mark", "1", "-w"});
481}
482
Garrick Evansd291af62020-05-25 10:39:06 +0900483bool Datapath::AddForwardEstablishedRule() {
484 return process_runner_->iptables(
485 "filter", {"-A", "FORWARD", "-m", "state", "--state",
486 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) == 0;
487}
488
489void Datapath::RemoveForwardEstablishedRule() {
490 process_runner_->iptables("filter",
491 {"-D", "FORWARD", "-m", "state", "--state",
492 "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"});
493}
494
Garrick Evans664a82f2019-12-17 12:18:05 +0900495bool Datapath::MaskInterfaceFlags(const std::string& ifname,
496 uint16_t on,
497 uint16_t off) {
Taoyu Li90c13912019-11-26 17:56:54 +0900498 base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
499 if (!sock.is_valid()) {
500 PLOG(ERROR) << "Failed to create control socket";
501 return false;
502 }
503 ifreq ifr;
504 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str());
505 if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) {
506 PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname;
507 return false;
508 }
Garrick Evans664a82f2019-12-17 12:18:05 +0900509 ifr.ifr_flags |= on;
510 ifr.ifr_flags &= ~off;
Taoyu Li90c13912019-11-26 17:56:54 +0900511 if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900512 PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on
513 << " unset flag 0x" << std::hex << off << " on " << ifname;
Taoyu Li90c13912019-11-26 17:56:54 +0900514 return false;
515 }
516 return true;
517}
518
Garrick Evans260ff302019-07-25 11:22:50 +0900519bool Datapath::AddIPv6HostRoute(const std::string& ifname,
520 const std::string& ipv6_addr,
521 int ipv6_prefix_len) {
522 std::string ipv6_addr_cidr =
523 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
524
Garrick Evans8e8e3472020-01-23 14:03:50 +0900525 return process_runner_->ip6("route", "replace",
526 {ipv6_addr_cidr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900527}
528
529void Datapath::RemoveIPv6HostRoute(const std::string& ifname,
530 const std::string& ipv6_addr,
531 int ipv6_prefix_len) {
532 std::string ipv6_addr_cidr =
533 ipv6_addr + "/" + std::to_string(ipv6_prefix_len);
534
Garrick Evans8e8e3472020-01-23 14:03:50 +0900535 process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900536}
537
538bool Datapath::AddIPv6Neighbor(const std::string& ifname,
539 const std::string& ipv6_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900540 return process_runner_->ip6("neigh", "add",
541 {"proxy", ipv6_addr, "dev", ifname}) == 0;
Garrick Evans260ff302019-07-25 11:22:50 +0900542}
543
544void Datapath::RemoveIPv6Neighbor(const std::string& ifname,
545 const std::string& ipv6_addr) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900546 process_runner_->ip6("neigh", "del", {"proxy", ipv6_addr, "dev", ifname});
Garrick Evans260ff302019-07-25 11:22:50 +0900547}
548
549bool Datapath::AddIPv6Forwarding(const std::string& ifname1,
550 const std::string& ifname2) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900551 if (process_runner_->ip6tables(
552 "filter",
553 {"-C", "FORWARD", "-i", ifname1, "-o", ifname2, "-j", "ACCEPT", "-w"},
554 false /*log_failures*/) != 0 &&
555 process_runner_->ip6tables(
556 "filter", {"-A", "FORWARD", "-i", ifname1, "-o", ifname2, "-j",
557 "ACCEPT", "-w"}) != 0) {
Garrick Evans260ff302019-07-25 11:22:50 +0900558 return false;
559 }
560
Garrick Evans8e8e3472020-01-23 14:03:50 +0900561 if (process_runner_->ip6tables(
562 "filter",
563 {"-C", "FORWARD", "-i", ifname2, "-o", ifname1, "-j", "ACCEPT", "-w"},
564 false /*log_failures*/) != 0 &&
565 process_runner_->ip6tables(
566 "filter", {"-A", "FORWARD", "-i", ifname2, "-o", ifname1, "-j",
567 "ACCEPT", "-w"}) != 0) {
Garrick Evans260ff302019-07-25 11:22:50 +0900568 RemoveIPv6Forwarding(ifname1, ifname2);
569 return false;
570 }
571
572 return true;
573}
574
575void Datapath::RemoveIPv6Forwarding(const std::string& ifname1,
576 const std::string& ifname2) {
Garrick Evans8e8e3472020-01-23 14:03:50 +0900577 process_runner_->ip6tables("filter", {"-D", "FORWARD", "-i", ifname1, "-o",
578 ifname2, "-j", "ACCEPT", "-w"});
Garrick Evans260ff302019-07-25 11:22:50 +0900579
Garrick Evans8e8e3472020-01-23 14:03:50 +0900580 process_runner_->ip6tables("filter", {"-D", "FORWARD", "-i", ifname2, "-o",
581 ifname1, "-j", "ACCEPT", "-w"});
Garrick Evans260ff302019-07-25 11:22:50 +0900582}
583
Garrick Evans3d97a392020-02-21 15:24:37 +0900584bool Datapath::AddIPv4Route(uint32_t gateway_addr,
585 uint32_t addr,
586 uint32_t netmask) {
587 struct rtentry route;
588 memset(&route, 0, sizeof(route));
Hugo Benichie8758b52020-04-03 14:49:01 +0900589 SetSockaddrIn(&route.rt_gateway, gateway_addr);
590 SetSockaddrIn(&route.rt_dst, addr & netmask);
591 SetSockaddrIn(&route.rt_genmask, netmask);
Garrick Evans3d97a392020-02-21 15:24:37 +0900592 route.rt_flags = RTF_UP | RTF_GATEWAY;
Hugo Benichie8758b52020-04-03 14:49:01 +0900593 return ModifyRtentry(SIOCADDRT, &route);
594}
Garrick Evans3d97a392020-02-21 15:24:37 +0900595
Hugo Benichie8758b52020-04-03 14:49:01 +0900596bool Datapath::DeleteIPv4Route(uint32_t gateway_addr,
597 uint32_t addr,
598 uint32_t netmask) {
599 struct rtentry route;
600 memset(&route, 0, sizeof(route));
601 SetSockaddrIn(&route.rt_gateway, gateway_addr);
602 SetSockaddrIn(&route.rt_dst, addr & netmask);
603 SetSockaddrIn(&route.rt_genmask, netmask);
604 route.rt_flags = RTF_UP | RTF_GATEWAY;
605 return ModifyRtentry(SIOCDELRT, &route);
606}
607
608bool Datapath::AddIPv4Route(const std::string& ifname,
609 uint32_t addr,
610 uint32_t netmask) {
611 struct rtentry route;
612 memset(&route, 0, sizeof(route));
613 SetSockaddrIn(&route.rt_dst, addr & netmask);
614 SetSockaddrIn(&route.rt_genmask, netmask);
615 char rt_dev[IFNAMSIZ];
616 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
617 rt_dev[IFNAMSIZ - 1] = '\0';
618 route.rt_dev = rt_dev;
619 route.rt_flags = RTF_UP | RTF_GATEWAY;
620 return ModifyRtentry(SIOCADDRT, &route);
621}
622
623bool Datapath::DeleteIPv4Route(const std::string& ifname,
624 uint32_t addr,
625 uint32_t netmask) {
626 struct rtentry route;
627 memset(&route, 0, sizeof(route));
628 SetSockaddrIn(&route.rt_dst, addr & netmask);
629 SetSockaddrIn(&route.rt_genmask, netmask);
630 char rt_dev[IFNAMSIZ];
631 strncpy(rt_dev, ifname.c_str(), IFNAMSIZ);
632 rt_dev[IFNAMSIZ - 1] = '\0';
633 route.rt_dev = rt_dev;
634 route.rt_flags = RTF_UP | RTF_GATEWAY;
635 return ModifyRtentry(SIOCDELRT, &route);
636}
637
638bool Datapath::ModifyRtentry(unsigned long op, struct rtentry* route) {
639 DCHECK(route);
640 if (op != SIOCADDRT && op != SIOCDELRT) {
641 LOG(ERROR) << "Invalid operation " << op << " for rtentry " << route;
Garrick Evans3d97a392020-02-21 15:24:37 +0900642 return false;
643 }
Hugo Benichie8758b52020-04-03 14:49:01 +0900644 base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
645 if (!fd.is_valid()) {
646 PLOG(ERROR) << "Failed to create socket for adding rtentry " << route;
647 return false;
648 }
649 if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) {
650 std::string opname = op == SIOCADDRT ? "add" : "delete";
651 PLOG(ERROR) << "Failed to " << opname << " rtentry " << route;
Garrick Evans3d97a392020-02-21 15:24:37 +0900652 return false;
653 }
654 return true;
655}
656
Garrick Evans3388a032020-03-24 11:25:55 +0900657} // namespace patchpanel