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 | #include "patchpanel/datapath.h" |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 6 | |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 8 | #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 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 20 | #include <base/files/scoped_file.h> |
| 21 | #include <base/logging.h> |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 22 | #include <base/posix/eintr_wrapper.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 23 | #include <base/strings/string_number_conversions.h> |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 24 | #include <brillo/userdb_utils.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 26 | #include "patchpanel/adb_proxy.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 27 | #include "patchpanel/net_util.h" |
| 28 | #include "patchpanel/scoped_ns.h" |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 29 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 30 | namespace patchpanel { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 31 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 32 | namespace { |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 33 | // TODO(hugobenichi) Consolidate this constant definition in a single place. |
| 34 | constexpr pid_t kTestPID = -2; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 35 | constexpr char kDefaultIfname[] = "vmtap%d"; |
| 36 | constexpr char kTunDev[] = "/dev/net/tun"; |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 37 | constexpr char kArcAddr[] = "100.115.92.2"; |
| 38 | constexpr char kLocalhostAddr[] = "127.0.0.1"; |
| 39 | constexpr uint16_t kAdbServerPort = 5555; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 40 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 41 | std::string PrefixIfname(const std::string& prefix, const std::string& ifname) { |
| 42 | std::string n = prefix + ifname; |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 43 | if (n.length() < IFNAMSIZ) |
| 44 | return n; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 45 | |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 46 | // Best effort attempt to preserve the interface number, assuming it's the |
| 47 | // last char in the name. |
| 48 | auto c = ifname[ifname.length() - 1]; |
| 49 | n.resize(IFNAMSIZ - 1); |
| 50 | n[n.length() - 1] = c; |
| 51 | return n; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 52 | } |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 53 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 54 | bool IsValidIpFamily(IpFamily family) { |
| 55 | switch (family) { |
| 56 | case IPv4: |
| 57 | case IPv6: |
| 58 | case Dual: |
| 59 | return true; |
| 60 | default: |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 65 | } // namespace |
| 66 | |
| 67 | std::string ArcVethHostName(const std::string& ifname) { |
| 68 | return PrefixIfname("veth", ifname); |
| 69 | } |
| 70 | |
| 71 | std::string ArcBridgeName(const std::string& ifname) { |
| 72 | return PrefixIfname("arc_", ifname); |
| 73 | } |
| 74 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 75 | Datapath::Datapath(MinijailedProcessRunner* process_runner, Firewall* firewall) |
| 76 | : Datapath(process_runner, firewall, ioctl) {} |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 77 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 78 | Datapath::Datapath(MinijailedProcessRunner* process_runner, |
| 79 | Firewall* firewall, |
| 80 | ioctl_t ioctl_hook) |
| 81 | : process_runner_(process_runner), firewall_(firewall), ioctl_(ioctl_hook) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 82 | CHECK(process_runner_); |
| 83 | } |
| 84 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 85 | MinijailedProcessRunner& Datapath::runner() const { |
| 86 | return *process_runner_; |
| 87 | } |
| 88 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 89 | bool Datapath::NetnsAttachName(const std::string& netns_name, pid_t netns_pid) { |
| 90 | // Try first to delete any netns with name |netns_name| in case patchpanel |
| 91 | // did not exit cleanly. |
| 92 | if (process_runner_->ip_netns_delete(netns_name, false /*log_failures*/) == 0) |
| 93 | LOG(INFO) << "Deleted left over network namespace name " << netns_name; |
| 94 | return process_runner_->ip_netns_attach(netns_name, netns_pid) == 0; |
| 95 | } |
| 96 | |
| 97 | bool Datapath::NetnsDeleteName(const std::string& netns_name) { |
| 98 | return process_runner_->ip_netns_delete(netns_name) == 0; |
| 99 | } |
| 100 | |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 101 | bool Datapath::AddBridge(const std::string& ifname, |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 102 | uint32_t ipv4_addr, |
| 103 | uint32_t ipv4_prefix_len) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 104 | // Configure the persistent Chrome OS bridge interface with static IP. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 105 | if (process_runner_->brctl("addbr", {ifname}) != 0) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 109 | if (process_runner_->ip( |
| 110 | "addr", "add", |
| 111 | {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd", |
| 112 | IPv4AddressToString(Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)), |
| 113 | "dev", ifname}) != 0) { |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 114 | RemoveBridge(ifname); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | if (process_runner_->ip("link", "set", {ifname, "up"}) != 0) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 119 | RemoveBridge(ifname); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // See nat.conf in chromeos-nat-init for the rest of the NAT setup rules. |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 124 | if (!AddOutboundIPv4SNATMark(ifname)) { |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 125 | RemoveBridge(ifname); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | void Datapath::RemoveBridge(const std::string& ifname) { |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 133 | RemoveOutboundIPv4SNATMark(ifname); |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 134 | process_runner_->ip("link", "set", {ifname, "down"}); |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 135 | process_runner_->brctl("delbr", {ifname}); |
Garrick Evans | 8a949dc | 2019-07-18 16:17:53 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 138 | bool Datapath::AddToBridge(const std::string& br_ifname, |
| 139 | const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 140 | return (process_runner_->brctl("addif", {br_ifname, ifname}) == 0); |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 143 | std::string Datapath::AddTAP(const std::string& name, |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 144 | const MacAddress* mac_addr, |
| 145 | const SubnetAddress* ipv4_addr, |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 146 | const std::string& user) { |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 147 | base::ScopedFD dev(open(kTunDev, O_RDWR | O_NONBLOCK)); |
| 148 | if (!dev.is_valid()) { |
| 149 | PLOG(ERROR) << "Failed to open " << kTunDev; |
| 150 | return ""; |
| 151 | } |
| 152 | |
| 153 | struct ifreq ifr; |
| 154 | memset(&ifr, 0, sizeof(ifr)); |
| 155 | strncpy(ifr.ifr_name, name.empty() ? kDefaultIfname : name.c_str(), |
| 156 | sizeof(ifr.ifr_name)); |
| 157 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 158 | |
| 159 | // If a template was given as the name, ifr_name will be updated with the |
| 160 | // actual interface name. |
| 161 | if ((*ioctl_)(dev.get(), TUNSETIFF, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 162 | PLOG(ERROR) << "Failed to create tap interface " << name; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 163 | return ""; |
| 164 | } |
| 165 | const char* ifname = ifr.ifr_name; |
| 166 | |
| 167 | if ((*ioctl_)(dev.get(), TUNSETPERSIST, 1) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 168 | PLOG(ERROR) << "Failed to persist the interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 169 | return ""; |
| 170 | } |
| 171 | |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 172 | if (!user.empty()) { |
| 173 | uid_t uid = -1; |
| 174 | if (!brillo::userdb::GetUserInfo(user, &uid, nullptr)) { |
| 175 | PLOG(ERROR) << "Unable to look up UID for " << user; |
| 176 | RemoveTAP(ifname); |
| 177 | return ""; |
| 178 | } |
| 179 | if ((*ioctl_)(dev.get(), TUNSETOWNER, uid) != 0) { |
| 180 | PLOG(ERROR) << "Failed to set owner " << uid << " of tap interface " |
| 181 | << ifname; |
| 182 | RemoveTAP(ifname); |
| 183 | return ""; |
| 184 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 185 | } |
| 186 | |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 187 | // Create control socket for configuring the interface. |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 188 | base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 189 | if (!sock.is_valid()) { |
| 190 | PLOG(ERROR) << "Failed to create control socket for tap interface " |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 191 | << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 192 | RemoveTAP(ifname); |
| 193 | return ""; |
| 194 | } |
| 195 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 196 | if (ipv4_addr) { |
| 197 | struct sockaddr_in* addr = |
| 198 | reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_addr); |
| 199 | addr->sin_family = AF_INET; |
| 200 | addr->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Address()); |
| 201 | if ((*ioctl_)(sock.get(), SIOCSIFADDR, &ifr) != 0) { |
| 202 | PLOG(ERROR) << "Failed to set ip address for vmtap interface " << ifname |
| 203 | << " {" << ipv4_addr->ToCidrString() << "}"; |
| 204 | RemoveTAP(ifname); |
| 205 | return ""; |
| 206 | } |
| 207 | |
| 208 | struct sockaddr_in* netmask = |
| 209 | reinterpret_cast<struct sockaddr_in*>(&ifr.ifr_netmask); |
| 210 | netmask->sin_family = AF_INET; |
| 211 | netmask->sin_addr.s_addr = static_cast<in_addr_t>(ipv4_addr->Netmask()); |
| 212 | if ((*ioctl_)(sock.get(), SIOCSIFNETMASK, &ifr) != 0) { |
| 213 | PLOG(ERROR) << "Failed to set netmask for vmtap interface " << ifname |
| 214 | << " {" << ipv4_addr->ToCidrString() << "}"; |
| 215 | RemoveTAP(ifname); |
| 216 | return ""; |
| 217 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 218 | } |
| 219 | |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 220 | if (mac_addr) { |
| 221 | struct sockaddr* hwaddr = &ifr.ifr_hwaddr; |
| 222 | hwaddr->sa_family = ARPHRD_ETHER; |
| 223 | memcpy(&hwaddr->sa_data, mac_addr, sizeof(*mac_addr)); |
| 224 | if ((*ioctl_)(sock.get(), SIOCSIFHWADDR, &ifr) != 0) { |
| 225 | PLOG(ERROR) << "Failed to set mac address for vmtap interface " << ifname |
| 226 | << " {" << MacAddressToString(*mac_addr) << "}"; |
| 227 | RemoveTAP(ifname); |
| 228 | return ""; |
| 229 | } |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 233 | PLOG(ERROR) << "Failed to get flags for tap interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 234 | RemoveTAP(ifname); |
| 235 | return ""; |
| 236 | } |
| 237 | |
| 238 | ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
| 239 | if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) != 0) { |
Garrick Evans | 621ed26 | 2019-11-13 12:28:43 +0900 | [diff] [blame] | 240 | PLOG(ERROR) << "Failed to enable tap interface " << ifname; |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 241 | RemoveTAP(ifname); |
| 242 | return ""; |
| 243 | } |
| 244 | |
| 245 | return ifname; |
| 246 | } |
| 247 | |
| 248 | void Datapath::RemoveTAP(const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 249 | process_runner_->ip("tuntap", "del", {ifname, "mode", "tap"}); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 250 | } |
| 251 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 252 | bool Datapath::ConnectVethPair(pid_t netns_pid, |
| 253 | const std::string& netns_name, |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 254 | const std::string& veth_ifname, |
| 255 | const std::string& peer_ifname, |
| 256 | const MacAddress& remote_mac_addr, |
| 257 | uint32_t remote_ipv4_addr, |
| 258 | uint32_t remote_ipv4_prefix_len, |
| 259 | bool remote_multicast_flag) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 260 | // Set up the virtual pair across the current namespace and |netns_name|. |
| 261 | if (!AddVirtualInterfacePair(netns_name, veth_ifname, peer_ifname)) { |
| 262 | LOG(ERROR) << "Failed to create veth pair " << veth_ifname << "," |
| 263 | << peer_ifname; |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | // Configure the remote veth in namespace |netns_name|. |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 268 | { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 269 | ScopedNS ns(netns_pid); |
| 270 | if (!ns.IsValid() && netns_pid != kTestPID) { |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 271 | LOG(ERROR) |
| 272 | << "Cannot create virtual link -- invalid container namespace?"; |
| 273 | return false; |
| 274 | } |
| 275 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 276 | if (!ConfigureInterface(peer_ifname, remote_mac_addr, remote_ipv4_addr, |
| 277 | remote_ipv4_prefix_len, true /* link up */, |
| 278 | remote_multicast_flag)) { |
| 279 | LOG(ERROR) << "Failed to configure interface " << peer_ifname; |
| 280 | RemoveInterface(peer_ifname); |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 285 | if (!ToggleInterface(veth_ifname, true /*up*/)) { |
| 286 | LOG(ERROR) << "Failed to bring up interface " << veth_ifname; |
| 287 | RemoveInterface(veth_ifname); |
| 288 | return false; |
| 289 | } |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 290 | |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 291 | return true; |
| 292 | } |
| 293 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 294 | bool Datapath::AddVirtualInterfacePair(const std::string& netns_name, |
| 295 | const std::string& veth_ifname, |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 296 | const std::string& peer_ifname) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 297 | return process_runner_->ip("link", "add", |
| 298 | {veth_ifname, "type", "veth", "peer", "name", |
| 299 | peer_ifname, "netns", netns_name}) == 0; |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 300 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 301 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 302 | bool Datapath::ToggleInterface(const std::string& ifname, bool up) { |
| 303 | const std::string link = up ? "up" : "down"; |
| 304 | return process_runner_->ip("link", "set", {ifname, link}) == 0; |
| 305 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 306 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 307 | bool Datapath::ConfigureInterface(const std::string& ifname, |
| 308 | const MacAddress& mac_addr, |
| 309 | uint32_t ipv4_addr, |
| 310 | uint32_t ipv4_prefix_len, |
| 311 | bool up, |
| 312 | bool enable_multicast) { |
| 313 | const std::string link = up ? "up" : "down"; |
| 314 | const std::string multicast = enable_multicast ? "on" : "off"; |
| 315 | return (process_runner_->ip( |
| 316 | "addr", "add", |
| 317 | {IPv4AddressToCidrString(ipv4_addr, ipv4_prefix_len), "brd", |
| 318 | IPv4AddressToString( |
| 319 | Ipv4BroadcastAddr(ipv4_addr, ipv4_prefix_len)), |
| 320 | "dev", ifname}) == 0) && |
| 321 | (process_runner_->ip("link", "set", |
| 322 | { |
| 323 | "dev", |
| 324 | ifname, |
| 325 | link, |
| 326 | "addr", |
| 327 | MacAddressToString(mac_addr), |
| 328 | "multicast", |
| 329 | multicast, |
| 330 | }) == 0); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void Datapath::RemoveInterface(const std::string& ifname) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 334 | process_runner_->ip("link", "delete", {ifname}, false /*log_failures*/); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Hugo Benichi | 321f23b | 2020-09-25 15:42:05 +0900 | [diff] [blame] | 337 | bool Datapath::AddSourceIPv4DropRule(const std::string& oif, |
| 338 | const std::string& src_ip) { |
| 339 | return process_runner_->iptables("filter", {"-I", "OUTPUT", "-o", oif, "-s", |
| 340 | src_ip, "-j", "DROP", "-w"}) == 0; |
| 341 | } |
| 342 | |
| 343 | bool Datapath::RemoveSourceIPv4DropRule(const std::string& oif, |
| 344 | const std::string& src_ip) { |
| 345 | return process_runner_->iptables("filter", {"-D", "OUTPUT", "-o", oif, "-s", |
| 346 | src_ip, "-j", "DROP", "-w"}) == 0; |
| 347 | } |
| 348 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 349 | void Datapath::StartRoutingDevice(const std::string& ext_ifname, |
| 350 | const std::string& int_ifname, |
| 351 | uint32_t int_ipv4_addr, |
| 352 | TrafficSource source) { |
| 353 | if (!ext_ifname.empty() && |
| 354 | !AddInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr))) |
| 355 | LOG(ERROR) << "Failed to configure ingress traffic rules for " << ext_ifname |
| 356 | << "->" << int_ifname; |
| 357 | |
Hugo Benichi | fa97b3b | 2020-10-06 22:45:26 +0900 | [diff] [blame] | 358 | if (!StartIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname)) |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 359 | LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "->" |
| 360 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 361 | |
Hugo Benichi | fa97b3b | 2020-10-06 22:45:26 +0900 | [diff] [blame] | 362 | if (!StartIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname)) |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 363 | LOG(ERROR) << "Failed to enable IP forwarding for " << ext_ifname << "<-" |
| 364 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 365 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 366 | if (!ext_ifname.empty()) { |
| 367 | // If |ext_ifname| is not null, mark egress traffic with the |
| 368 | // fwmark routing tag corresponding to |ext_ifname|. |
| 369 | if (!ModifyFwmarkRoutingTag("-A", ext_ifname, int_ifname)) |
| 370 | LOG(ERROR) << "Failed to add PREROUTING fwmark routing tag for " |
| 371 | << ext_ifname << "<-" << int_ifname; |
| 372 | } else { |
| 373 | // Otherwise if ext_ifname is null, set up a CONNMARK restore rule in |
| 374 | // PREROUTING to apply any fwmark routing tag saved for the current |
| 375 | // connection, and rely on implicit routing to the default logical network |
| 376 | // otherwise. |
| 377 | if (!ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-A", int_ifname)) |
| 378 | LOG(ERROR) << "Failed to add PREROUTING CONNMARK restore rule for " |
| 379 | << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 380 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 381 | // TODO(b/161507671) When a VPN service starts, tag new connections with the |
| 382 | // fwmark routing tag for VPNs. |
| 383 | } |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 384 | |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 385 | if (!ModifyFwmarkSourceTag("-A", int_ifname, source)) |
| 386 | LOG(ERROR) << "Failed to add PREROUTING fwmark tagging rule for source " |
| 387 | << source << " for " << int_ifname; |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void Datapath::StopRoutingDevice(const std::string& ext_ifname, |
| 391 | const std::string& int_ifname, |
| 392 | uint32_t int_ipv4_addr, |
| 393 | TrafficSource source) { |
| 394 | if (!ext_ifname.empty()) |
| 395 | RemoveInboundIPv4DNAT(ext_ifname, IPv4AddressToString(int_ipv4_addr)); |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 396 | StopIpForwarding(IpFamily::IPv4, ext_ifname, int_ifname); |
| 397 | StopIpForwarding(IpFamily::IPv4, int_ifname, ext_ifname); |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 398 | ModifyFwmarkSourceTag("-D", int_ifname, source); |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 399 | if (!ext_ifname.empty()) { |
| 400 | ModifyFwmarkRoutingTag("-D", ext_ifname, int_ifname); |
| 401 | } else { |
| 402 | ModifyConnmarkRestore(IpFamily::Dual, "PREROUTING", "-D", int_ifname); |
| 403 | } |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 404 | } |
| 405 | |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 406 | bool Datapath::AddInboundIPv4DNAT(const std::string& ifname, |
| 407 | const std::string& ipv4_addr) { |
| 408 | // Direct ingress IP traffic to existing sockets. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 409 | if (process_runner_->iptables( |
| 410 | "nat", {"-A", "PREROUTING", "-i", ifname, "-m", "socket", |
| 411 | "--nowildcard", "-j", "ACCEPT", "-w"}) != 0) |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 412 | return false; |
| 413 | |
| 414 | // Direct ingress TCP & UDP traffic to ARC interface for new connections. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 415 | if (process_runner_->iptables( |
| 416 | "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT", |
| 417 | "--to-destination", ipv4_addr, "-w"}) != 0) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 418 | RemoveInboundIPv4DNAT(ifname, ipv4_addr); |
| 419 | return false; |
| 420 | } |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 421 | if (process_runner_->iptables( |
| 422 | "nat", {"-A", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT", |
| 423 | "--to-destination", ipv4_addr, "-w"}) != 0) { |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 424 | RemoveInboundIPv4DNAT(ifname, ipv4_addr); |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | void Datapath::RemoveInboundIPv4DNAT(const std::string& ifname, |
| 432 | const std::string& ipv4_addr) { |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 433 | process_runner_->iptables( |
| 434 | "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "udp", "-j", "DNAT", |
| 435 | "--to-destination", ipv4_addr, "-w"}); |
| 436 | process_runner_->iptables( |
| 437 | "nat", {"-D", "PREROUTING", "-i", ifname, "-p", "tcp", "-j", "DNAT", |
| 438 | "--to-destination", ipv4_addr, "-w"}); |
| 439 | process_runner_->iptables( |
| 440 | "nat", {"-D", "PREROUTING", "-i", ifname, "-m", "socket", "--nowildcard", |
| 441 | "-j", "ACCEPT", "-w"}); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 442 | } |
| 443 | |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 444 | // TODO(hugobenichi) The name incorrectly refers to egress traffic, but this |
| 445 | // FORWARD rule actually enables forwarding for ingress traffic. Fix the name. |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 446 | bool Datapath::AddOutboundIPv4(const std::string& ifname) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 447 | return StartIpForwarding(IpFamily::IPv4, "", ifname); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void Datapath::RemoveOutboundIPv4(const std::string& ifname) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 451 | StopIpForwarding(IpFamily::IPv4, "", ifname); |
Garrick Evans | f0ab713 | 2019-06-18 14:50:42 +0900 | [diff] [blame] | 452 | } |
| 453 | |
Hugo Benichi | c6ae67c | 2020-08-14 15:02:13 +0900 | [diff] [blame] | 454 | // TODO(b/161507671) Stop relying on the traffic fwmark 1/1 once forwarded |
| 455 | // egress traffic is routed through the fwmark routing tag. |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 456 | bool Datapath::AddSNATMarkRules() { |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 457 | // chromium:1050579: INVALID packets cannot be tracked by conntrack therefore |
| 458 | // need to be explicitly dropped. |
| 459 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 460 | "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 461 | "state", "--state", "INVALID", "-j", "DROP", "-w"}) != 0) { |
| 462 | return false; |
| 463 | } |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 464 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 465 | "filter", {"-A", "FORWARD", "-m", "mark", "--mark", "1/1", "-j", |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 466 | "ACCEPT", "-w"}) != 0) { |
| 467 | return false; |
| 468 | } |
| 469 | if (process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 470 | "nat", {"-A", "POSTROUTING", "-m", "mark", "--mark", "1/1", "-j", |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 471 | "MASQUERADE", "-w"}) != 0) { |
| 472 | RemoveSNATMarkRules(); |
| 473 | return false; |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | void Datapath::RemoveSNATMarkRules() { |
| 479 | process_runner_->iptables("nat", {"-D", "POSTROUTING", "-m", "mark", "--mark", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 480 | "1/1", "-j", "MASQUERADE", "-w"}); |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 481 | process_runner_->iptables("filter", {"-D", "FORWARD", "-m", "mark", "--mark", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 482 | "1/1", "-j", "ACCEPT", "-w"}); |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 483 | process_runner_->iptables( |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 484 | "filter", {"-D", "FORWARD", "-m", "mark", "--mark", "1/1", "-m", "state", |
Taoyu Li | 79871c9 | 2020-07-02 16:09:39 +0900 | [diff] [blame] | 485 | "--state", "INVALID", "-j", "DROP", "-w"}); |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 486 | } |
| 487 | |
Garrick Evans | ff6e37f | 2020-05-25 10:54:47 +0900 | [diff] [blame] | 488 | bool Datapath::AddInterfaceSNAT(const std::string& ifname) { |
| 489 | return process_runner_->iptables("nat", {"-A", "POSTROUTING", "-o", ifname, |
| 490 | "-j", "MASQUERADE", "-w"}) == 0; |
| 491 | } |
| 492 | |
| 493 | void Datapath::RemoveInterfaceSNAT(const std::string& ifname) { |
| 494 | process_runner_->iptables( |
| 495 | "nat", {"-D", "POSTROUTING", "-o", ifname, "-j", "MASQUERADE", "-w"}); |
| 496 | } |
| 497 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 498 | bool Datapath::AddOutboundIPv4SNATMark(const std::string& ifname) { |
| 499 | return process_runner_->iptables( |
| 500 | "mangle", {"-A", "PREROUTING", "-i", ifname, "-j", "MARK", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 501 | "--set-mark", "1/1", "-w"}) == 0; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void Datapath::RemoveOutboundIPv4SNATMark(const std::string& ifname) { |
| 505 | process_runner_->iptables("mangle", {"-D", "PREROUTING", "-i", ifname, "-j", |
Hugo Benichi | 6c44532 | 2020-08-12 16:46:19 +0900 | [diff] [blame] | 506 | "MARK", "--set-mark", "1/1", "-w"}); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 507 | } |
| 508 | |
Garrick Evans | d291af6 | 2020-05-25 10:39:06 +0900 | [diff] [blame] | 509 | bool Datapath::AddForwardEstablishedRule() { |
| 510 | return process_runner_->iptables( |
| 511 | "filter", {"-A", "FORWARD", "-m", "state", "--state", |
| 512 | "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}) == 0; |
| 513 | } |
| 514 | |
| 515 | void Datapath::RemoveForwardEstablishedRule() { |
| 516 | process_runner_->iptables("filter", |
| 517 | {"-D", "FORWARD", "-m", "state", "--state", |
| 518 | "ESTABLISHED,RELATED", "-j", "ACCEPT", "-w"}); |
| 519 | } |
| 520 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 521 | bool Datapath::MaskInterfaceFlags(const std::string& ifname, |
| 522 | uint16_t on, |
| 523 | uint16_t off) { |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 524 | base::ScopedFD sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 525 | if (!sock.is_valid()) { |
| 526 | PLOG(ERROR) << "Failed to create control socket"; |
| 527 | return false; |
| 528 | } |
| 529 | ifreq ifr; |
| 530 | snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname.c_str()); |
| 531 | if ((*ioctl_)(sock.get(), SIOCGIFFLAGS, &ifr) < 0) { |
| 532 | PLOG(WARNING) << "ioctl() failed to get interface flag on " << ifname; |
| 533 | return false; |
| 534 | } |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 535 | ifr.ifr_flags |= on; |
| 536 | ifr.ifr_flags &= ~off; |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 537 | if ((*ioctl_)(sock.get(), SIOCSIFFLAGS, &ifr) < 0) { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 538 | PLOG(WARNING) << "ioctl() failed to set flag 0x" << std::hex << on |
| 539 | << " unset flag 0x" << std::hex << off << " on " << ifname; |
Taoyu Li | 90c1391 | 2019-11-26 17:56:54 +0900 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 545 | bool Datapath::AddIPv6HostRoute(const std::string& ifname, |
| 546 | const std::string& ipv6_addr, |
| 547 | int ipv6_prefix_len) { |
| 548 | std::string ipv6_addr_cidr = |
| 549 | ipv6_addr + "/" + std::to_string(ipv6_prefix_len); |
| 550 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 551 | return process_runner_->ip6("route", "replace", |
| 552 | {ipv6_addr_cidr, "dev", ifname}) == 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | void Datapath::RemoveIPv6HostRoute(const std::string& ifname, |
| 556 | const std::string& ipv6_addr, |
| 557 | int ipv6_prefix_len) { |
| 558 | std::string ipv6_addr_cidr = |
| 559 | ipv6_addr + "/" + std::to_string(ipv6_prefix_len); |
| 560 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 561 | process_runner_->ip6("route", "del", {ipv6_addr_cidr, "dev", ifname}); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 562 | } |
| 563 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 564 | bool Datapath::AddIPv6Address(const std::string& ifname, |
| 565 | const std::string& ipv6_addr) { |
| 566 | return process_runner_->ip6("addr", "add", {ipv6_addr, "dev", ifname}) == 0; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 567 | } |
| 568 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 569 | void Datapath::RemoveIPv6Address(const std::string& ifname, |
| 570 | const std::string& ipv6_addr) { |
| 571 | process_runner_->ip6("addr", "del", {ipv6_addr, "dev", ifname}); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 572 | } |
| 573 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 574 | bool Datapath::ModifyConnmarkRestore(IpFamily family, |
| 575 | const std::string& chain, |
| 576 | const std::string& op, |
| 577 | const std::string& iif) { |
| 578 | if (chain != "OUTPUT" && (chain != "PREROUTING" || iif.empty())) { |
| 579 | LOG(ERROR) << "Invalid arguments chain=" << chain << " iif=" << iif; |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | if (!IsValidIpFamily(family)) { |
| 584 | LOG(ERROR) << "Cannot change " << chain << " -j CONNMARK restore-mark" |
| 585 | << " for " << iif << ": incorrect IP family " << family; |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | std::vector<std::string> args = {op, chain}; |
| 590 | if (!iif.empty()) { |
| 591 | args.push_back("-i"); |
| 592 | args.push_back(iif); |
| 593 | } |
| 594 | args.insert(args.end(), {"-j", "CONNMARK", "--restore-mark", "--mask", |
| 595 | kFwmarkRoutingMask.ToString(), "-w"}); |
| 596 | |
| 597 | bool success = true; |
| 598 | if (family & IPv4) |
| 599 | success &= process_runner_->iptables("mangle", args) == 0; |
| 600 | if (family & IPv6) |
| 601 | success &= process_runner_->ip6tables("mangle", args) == 0; |
| 602 | return success; |
| 603 | } |
| 604 | |
| 605 | bool Datapath::ModifyFwmarkRoutingTag(const std::string& op, |
| 606 | const std::string& ext_ifname, |
| 607 | const std::string& int_ifname) { |
| 608 | int ifindex = FindIfIndex(ext_ifname); |
| 609 | if (ifindex == 0) { |
| 610 | PLOG(ERROR) << "if_nametoindex(" << ext_ifname << ") failed"; |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | return ModifyFwmarkPrerouting(IpFamily::Dual, op, int_ifname, |
| 615 | Fwmark::FromIfIndex(ifindex), |
| 616 | kFwmarkRoutingMask); |
| 617 | } |
| 618 | |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 619 | bool Datapath::ModifyFwmarkSourceTag(const std::string& op, |
| 620 | const std::string& iif, |
| 621 | TrafficSource source) { |
| 622 | return ModifyFwmarkPrerouting(IpFamily::Dual, op, iif, |
| 623 | Fwmark::FromSource(source), |
| 624 | kFwmarkAllSourcesMask); |
| 625 | } |
| 626 | |
| 627 | bool Datapath::ModifyFwmarkPrerouting(IpFamily family, |
| 628 | const std::string& op, |
| 629 | const std::string& iif, |
| 630 | Fwmark mark, |
| 631 | Fwmark mask, |
| 632 | bool log_failures) { |
| 633 | if (iif.empty()) { |
| 634 | LOG(ERROR) |
| 635 | << "Cannot change PREROUTING set-fwmark with no interface specified"; |
| 636 | return false; |
| 637 | } |
| 638 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 639 | if (!IsValidIpFamily(family)) { |
| 640 | LOG(ERROR) << "Cannot change PREROUTING set-fwmark for " << iif |
| 641 | << ": incorrect IP family " << family; |
| 642 | return false; |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | std::vector<std::string> args = { |
| 646 | op, "PREROUTING", "-i", iif, |
| 647 | "-j", "MARK", "--set-mark", mark.ToString() + "/" + mask.ToString(), |
| 648 | "-w"}; |
| 649 | |
| 650 | bool success = true; |
Hugo Benichi | 5c9c11c | 2020-09-15 17:25:26 +0900 | [diff] [blame] | 651 | if (family & IPv4) |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 652 | success &= process_runner_->iptables("mangle", args, log_failures) == 0; |
Hugo Benichi | 5c9c11c | 2020-09-15 17:25:26 +0900 | [diff] [blame] | 653 | if (family & IPv6) |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 654 | success &= process_runner_->ip6tables("mangle", args, log_failures) == 0; |
Hugo Benichi | fa97b3b | 2020-10-06 22:45:26 +0900 | [diff] [blame] | 655 | return success; |
Hugo Benichi | 9be19b1 | 2020-08-14 15:33:40 +0900 | [diff] [blame] | 656 | } |
| 657 | |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 658 | bool Datapath::ModifyIpForwarding(IpFamily family, |
| 659 | const std::string& op, |
| 660 | const std::string& iif, |
| 661 | const std::string& oif, |
| 662 | bool log_failures) { |
| 663 | if (iif.empty() && oif.empty()) { |
| 664 | LOG(ERROR) << "Cannot change IP forwarding with no input or output " |
| 665 | "interface specified"; |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 669 | if (!IsValidIpFamily(family)) { |
| 670 | LOG(ERROR) << "Cannot change IP forwarding from \"" << iif << "\" to \"" |
| 671 | << oif << "\": incorrect IP family " << family; |
| 672 | return false; |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | std::vector<std::string> args = {op, "FORWARD"}; |
| 676 | if (!iif.empty()) { |
| 677 | args.push_back("-i"); |
| 678 | args.push_back(iif); |
| 679 | } |
| 680 | if (!oif.empty()) { |
| 681 | args.push_back("-o"); |
| 682 | args.push_back(oif); |
| 683 | } |
| 684 | args.push_back("-j"); |
| 685 | args.push_back("ACCEPT"); |
| 686 | args.push_back("-w"); |
| 687 | |
| 688 | bool success = true; |
| 689 | if (family & IpFamily::IPv4) |
| 690 | success &= process_runner_->iptables("filter", args, log_failures) == 0; |
| 691 | if (family & IpFamily::IPv6) |
| 692 | success &= process_runner_->ip6tables("filter", args, log_failures) == 0; |
| 693 | return success; |
| 694 | } |
| 695 | |
| 696 | bool Datapath::StartIpForwarding(IpFamily family, |
| 697 | const std::string& iif, |
| 698 | const std::string& oif) { |
| 699 | return ModifyIpForwarding(family, "-A", iif, oif); |
| 700 | } |
| 701 | |
| 702 | bool Datapath::StopIpForwarding(IpFamily family, |
| 703 | const std::string& iif, |
| 704 | const std::string& oif) { |
| 705 | return ModifyIpForwarding(family, "-D", iif, oif); |
| 706 | } |
| 707 | |
| 708 | bool Datapath::AddIPv6Forwarding(const std::string& ifname1, |
| 709 | const std::string& ifname2) { |
| 710 | // Only start Ipv6 forwarding if -C returns false and it had not been |
| 711 | // started yet. |
| 712 | if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname1, ifname2, |
| 713 | false /*log_failures*/) && |
| 714 | !StartIpForwarding(IpFamily::IPv6, ifname1, ifname2)) { |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | if (!ModifyIpForwarding(IpFamily::IPv6, "-C", ifname2, ifname1, |
| 719 | false /*log_failures*/) && |
| 720 | !StartIpForwarding(IpFamily::IPv6, ifname2, ifname1)) { |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 721 | RemoveIPv6Forwarding(ifname1, ifname2); |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | void Datapath::RemoveIPv6Forwarding(const std::string& ifname1, |
| 729 | const std::string& ifname2) { |
Hugo Benichi | d82d883 | 2020-08-14 10:05:03 +0900 | [diff] [blame] | 730 | StopIpForwarding(IpFamily::IPv6, ifname1, ifname2); |
| 731 | StopIpForwarding(IpFamily::IPv6, ifname2, ifname1); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 732 | } |
| 733 | |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 734 | bool Datapath::AddIPv4Route(uint32_t gateway_addr, |
| 735 | uint32_t addr, |
| 736 | uint32_t netmask) { |
| 737 | struct rtentry route; |
| 738 | memset(&route, 0, sizeof(route)); |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 739 | SetSockaddrIn(&route.rt_gateway, gateway_addr); |
| 740 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 741 | SetSockaddrIn(&route.rt_genmask, netmask); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 742 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 743 | return ModifyRtentry(SIOCADDRT, &route); |
| 744 | } |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 745 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 746 | bool Datapath::DeleteIPv4Route(uint32_t gateway_addr, |
| 747 | uint32_t addr, |
| 748 | uint32_t netmask) { |
| 749 | struct rtentry route; |
| 750 | memset(&route, 0, sizeof(route)); |
| 751 | SetSockaddrIn(&route.rt_gateway, gateway_addr); |
| 752 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 753 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 754 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 755 | return ModifyRtentry(SIOCDELRT, &route); |
| 756 | } |
| 757 | |
| 758 | bool Datapath::AddIPv4Route(const std::string& ifname, |
| 759 | uint32_t addr, |
| 760 | uint32_t netmask) { |
| 761 | struct rtentry route; |
| 762 | memset(&route, 0, sizeof(route)); |
| 763 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 764 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 765 | char rt_dev[IFNAMSIZ]; |
| 766 | strncpy(rt_dev, ifname.c_str(), IFNAMSIZ); |
| 767 | rt_dev[IFNAMSIZ - 1] = '\0'; |
| 768 | route.rt_dev = rt_dev; |
| 769 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 770 | return ModifyRtentry(SIOCADDRT, &route); |
| 771 | } |
| 772 | |
| 773 | bool Datapath::DeleteIPv4Route(const std::string& ifname, |
| 774 | uint32_t addr, |
| 775 | uint32_t netmask) { |
| 776 | struct rtentry route; |
| 777 | memset(&route, 0, sizeof(route)); |
| 778 | SetSockaddrIn(&route.rt_dst, addr & netmask); |
| 779 | SetSockaddrIn(&route.rt_genmask, netmask); |
| 780 | char rt_dev[IFNAMSIZ]; |
| 781 | strncpy(rt_dev, ifname.c_str(), IFNAMSIZ); |
| 782 | rt_dev[IFNAMSIZ - 1] = '\0'; |
| 783 | route.rt_dev = rt_dev; |
| 784 | route.rt_flags = RTF_UP | RTF_GATEWAY; |
| 785 | return ModifyRtentry(SIOCDELRT, &route); |
| 786 | } |
| 787 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 788 | bool Datapath::ModifyRtentry(ioctl_req_t op, struct rtentry* route) { |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 789 | DCHECK(route); |
| 790 | if (op != SIOCADDRT && op != SIOCDELRT) { |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 791 | LOG(ERROR) << "Invalid operation " << op << " for rtentry " << *route; |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 792 | return false; |
| 793 | } |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 794 | base::ScopedFD fd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 795 | if (!fd.is_valid()) { |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 796 | PLOG(ERROR) << "Failed to create socket for adding rtentry " << *route; |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 797 | return false; |
| 798 | } |
| 799 | if (HANDLE_EINTR(ioctl_(fd.get(), op, route)) != 0) { |
| 800 | std::string opname = op == SIOCADDRT ? "add" : "delete"; |
Andreea Costinas | 34aa7a9 | 2020-08-04 10:36:10 +0200 | [diff] [blame] | 801 | PLOG(ERROR) << "Failed to " << opname << " rtentry " << *route; |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 802 | return false; |
| 803 | } |
| 804 | return true; |
| 805 | } |
| 806 | |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 807 | bool Datapath::AddAdbPortForwardRule(const std::string& ifname) { |
| 808 | return firewall_->AddIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP, |
| 809 | kArcAddr, kAdbServerPort, ifname, |
| 810 | kLocalhostAddr, kAdbProxyTcpListenPort); |
| 811 | } |
| 812 | |
| 813 | void Datapath::DeleteAdbPortForwardRule(const std::string& ifname) { |
| 814 | firewall_->DeleteIpv4ForwardRule(patchpanel::ModifyPortRuleRequest::TCP, |
| 815 | kArcAddr, kAdbServerPort, ifname, |
| 816 | kLocalhostAddr, kAdbProxyTcpListenPort); |
| 817 | } |
| 818 | |
| 819 | bool Datapath::AddAdbPortAccessRule(const std::string& ifname) { |
| 820 | return firewall_->AddAcceptRules(patchpanel::ModifyPortRuleRequest::TCP, |
| 821 | kAdbProxyTcpListenPort, ifname); |
| 822 | } |
| 823 | |
| 824 | void Datapath::DeleteAdbPortAccessRule(const std::string& ifname) { |
| 825 | firewall_->DeleteAcceptRules(patchpanel::ModifyPortRuleRequest::TCP, |
| 826 | kAdbProxyTcpListenPort, ifname); |
| 827 | } |
| 828 | |
Hugo Benichi | af9d8a7 | 2020-08-26 13:28:13 +0900 | [diff] [blame^] | 829 | void Datapath::SetIfnameIndex(const std::string& ifname, int ifindex) { |
| 830 | if_nametoindex_[ifname] = ifindex; |
| 831 | } |
| 832 | |
| 833 | int Datapath::FindIfIndex(const std::string& ifname) { |
| 834 | uint32_t ifindex = if_nametoindex(ifname.c_str()); |
| 835 | if (ifindex > 0) { |
| 836 | if_nametoindex_[ifname] = ifindex; |
| 837 | return ifindex; |
| 838 | } |
| 839 | |
| 840 | const auto it = if_nametoindex_.find(ifname); |
| 841 | if (it != if_nametoindex_.end()) |
| 842 | return it->second; |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 847 | } // namespace patchpanel |