blob: 49c1b7e6b55c771ac8ed9d7d2a584effcdf397a1 [file] [log] [blame]
San Mehat9d10b342010-01-18 09:51:02 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
San Mehat9d10b342010-01-18 09:51:02 -080017#include <errno.h>
San Mehat18737842010-01-21 09:22:43 -080018#include <fcntl.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090019#include <inttypes.h>
Lorenzo Colittic2841282015-11-25 22:13:57 +090020#include <netdb.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080021#include <string.h>
San Mehat18737842010-01-21 09:22:43 -080022
San Mehat9d10b342010-01-18 09:51:02 -080023#include <sys/socket.h>
24#include <sys/stat.h>
San Mehat18737842010-01-21 09:22:43 -080025#include <sys/types.h>
26#include <sys/wait.h>
27
San Mehat9d10b342010-01-18 09:51:02 -080028#include <netinet/in.h>
29#include <arpa/inet.h>
30
Erik Kline5f0358b2018-02-16 15:08:09 +090031#include <array>
32#include <cstdlib>
33#include <string>
34#include <vector>
35
San Mehat9d10b342010-01-18 09:51:02 -080036#define LOG_TAG "TetherController"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090037#include <android-base/strings.h>
38#include <android-base/stringprintf.h>
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050039#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080040#include <log/log.h>
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090041#include <netdutils/StatusOr.h>
San Mehat9d10b342010-01-18 09:51:02 -080042
Erik Klineb31fd692018-06-06 20:50:11 +090043#include "Controllers.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070044#include "Fwmark.h"
JP Abgrall69261cb2014-06-19 18:35:24 -070045#include "NetdConstants.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070046#include "Permission.h"
Erik Kline1d065ba2016-06-08 13:24:45 +090047#include "InterfaceController.h"
Lorenzo Colittie20a5262017-05-09 18:30:44 +090048#include "NetworkController.h"
Lorenzo Colittia93126d2017-08-24 13:28:19 +090049#include "ResponseCode.h"
San Mehat9d10b342010-01-18 09:51:02 -080050#include "TetherController.h"
51
Bernie Innocenti4debf3b2018-09-12 13:54:50 +090052namespace android {
53namespace net {
54
Lorenzo Colittia93126d2017-08-24 13:28:19 +090055using android::base::Join;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090056using android::base::StringAppendF;
Erik Klineb31fd692018-06-06 20:50:11 +090057using android::base::StringPrintf;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090058using android::netdutils::statusFromErrno;
Erik Klineb31fd692018-06-06 20:50:11 +090059using android::netdutils::StatusOr;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090060
Lorenzo Colitti799625c2015-02-25 12:52:00 +090061namespace {
62
Erik Kline1d065ba2016-06-08 13:24:45 +090063const char BP_TOOLS_MODE[] = "bp-tools";
64const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
65const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
66const char SEPARATOR[] = "|";
Erik Kline93f9b222017-10-22 21:24:58 +090067constexpr const char kTcpBeLiberal[] = "/proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090068
Erik Kline5f0358b2018-02-16 15:08:09 +090069// Chosen to match AID_DNS_TETHER, as made "friendly" by fs_config_generator.py.
70constexpr const char kDnsmasqUsername[] = "dns_tether";
71
Lorenzo Colitti799625c2015-02-25 12:52:00 +090072bool writeToFile(const char* filename, const char* value) {
Nick Kralevichb95c60c2016-11-19 09:09:16 -080073 int fd = open(filename, O_WRONLY | O_CLOEXEC);
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000074 if (fd < 0) {
75 ALOGE("Failed to open %s: %s", filename, strerror(errno));
76 return false;
77 }
78
79 const ssize_t len = strlen(value);
80 if (write(fd, value, len) != len) {
81 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
82 close(fd);
83 return false;
84 }
85 close(fd);
86 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090087}
88
Erik Kline93f9b222017-10-22 21:24:58 +090089// TODO: Consider altering TCP and UDP timeouts as well.
90void configureForTethering(bool enabled) {
91 writeToFile(kTcpBeLiberal, enabled ? "1" : "0");
92}
93
Erik Kline1d065ba2016-06-08 13:24:45 +090094bool configureForIPv6Router(const char *interface) {
95 return (InterfaceController::setEnableIPv6(interface, 0) == 0)
96 && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0)
Erik Kline6cddf512016-08-09 15:28:42 +090097 && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0)
98 && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0)
Erik Kline1d065ba2016-06-08 13:24:45 +090099 && (InterfaceController::setEnableIPv6(interface, 1) == 0);
100}
101
102void configureForIPv6Client(const char *interface) {
103 InterfaceController::setAcceptIPv6Ra(interface, 1);
Erik Kline6cddf512016-08-09 15:28:42 +0900104 InterfaceController::setAcceptIPv6Dad(interface, 1);
105 InterfaceController::setIPv6DadTransmits(interface, "1");
Erik Kline1d065ba2016-06-08 13:24:45 +0900106 InterfaceController::setEnableIPv6(interface, 0);
107}
108
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900109bool inBpToolsMode() {
110 // In BP tools mode, do not disable IP forwarding
111 char bootmode[PROPERTY_VALUE_MAX] = {0};
112 property_get("ro.bootmode", bootmode, "unknown");
113 return !strcmp(BP_TOOLS_MODE, bootmode);
114}
115
116} // namespace
117
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900118auto TetherController::iptablesRestoreFunction = execIptablesRestoreWithOutput;
119
120const int MAX_IPT_OUTPUT_LINE_LEN = 256;
121
122const std::string GET_TETHER_STATS_COMMAND = StringPrintf(
123 "*filter\n"
124 "-nvx -L %s\n"
125 "COMMIT\n", android::net::TetherController::LOCAL_TETHER_COUNTERS_CHAIN);
126
Erik Kline15079dd2018-05-18 23:10:56 +0900127int TetherController::DnsmasqState::sendCmd(int daemonFd, const std::string& cmd) {
128 if (cmd.empty()) return 0;
129
Erik Klineb31fd692018-06-06 20:50:11 +0900130 gLog.log("Sending update msg to dnsmasq [%s]", cmd.c_str());
Erik Kline15079dd2018-05-18 23:10:56 +0900131 // Send the trailing \0 as well.
132 if (write(daemonFd, cmd.c_str(), cmd.size() + 1) < 0) {
Erik Klineb31fd692018-06-06 20:50:11 +0900133 gLog.error("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline15079dd2018-05-18 23:10:56 +0900134 errno = EREMOTEIO;
135 return -1;
136 }
137 return 0;
138}
139
140void TetherController::DnsmasqState::clear() {
141 update_ifaces_cmd.clear();
142 update_dns_cmd.clear();
143}
144
145int TetherController::DnsmasqState::sendAllState(int daemonFd) const {
146 return sendCmd(daemonFd, update_ifaces_cmd) | sendCmd(daemonFd, update_dns_cmd);
147}
148
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700149TetherController::TetherController() {
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900150 if (inBpToolsMode()) {
151 enableForwarding(BP_TOOLS_MODE);
152 } else {
153 setIpFwdEnabled();
154 }
San Mehat9d10b342010-01-18 09:51:02 -0800155}
156
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900157bool TetherController::setIpFwdEnabled() {
158 bool success = true;
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900159 bool disable = mForwardingRequests.empty();
160 const char* value = disable ? "0" : "1";
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900161 ALOGD("Setting IP forward enable = %s", value);
162 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
163 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900164 if (disable) {
165 // Turning off the forwarding sysconf in the kernel has the side effect
166 // of turning on ICMP redirect, which is a security hazard.
167 // Turn ICMP redirect back off immediately.
168 int rv = InterfaceController::disableIcmpRedirects();
169 success &= (rv == 0);
170 }
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900171 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800172}
173
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900174bool TetherController::enableForwarding(const char* requester) {
175 // Don't return an error if this requester already requested forwarding. Only return errors for
176 // things that the caller caller needs to care about, such as "couldn't write to the file to
177 // enable forwarding".
178 mForwardingRequests.insert(requester);
179 return setIpFwdEnabled();
180}
San Mehat9d10b342010-01-18 09:51:02 -0800181
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900182bool TetherController::disableForwarding(const char* requester) {
183 mForwardingRequests.erase(requester);
184 return setIpFwdEnabled();
185}
San Mehat9d10b342010-01-18 09:51:02 -0800186
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900187size_t TetherController::forwardingRequestCount() {
188 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800189}
190
Erik Kline13fa01f2015-11-12 17:49:23 +0900191int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800192 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000193 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800194 errno = EBUSY;
Luke Huangb5733d72018-08-21 17:17:19 +0800195 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800196 }
197
Steve Block7b984e32011-12-20 16:22:42 +0000198 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800199
200 pid_t pid;
201 int pipefd[2];
202
203 if (pipe(pipefd) < 0) {
Luke Huangb5733d72018-08-21 17:17:19 +0800204 int res = errno;
Steve Block5ea0c052012-01-06 19:18:11 +0000205 ALOGE("pipe failed (%s)", strerror(errno));
Luke Huangb5733d72018-08-21 17:17:19 +0800206 return -res;
San Mehat9d10b342010-01-18 09:51:02 -0800207 }
208
209 /*
210 * TODO: Create a monitoring thread to handle and restart
211 * the daemon if it exits prematurely
212 */
213 if ((pid = fork()) < 0) {
Luke Huangb5733d72018-08-21 17:17:19 +0800214 int res = errno;
Steve Block5ea0c052012-01-06 19:18:11 +0000215 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800216 close(pipefd[0]);
217 close(pipefd[1]);
Luke Huangb5733d72018-08-21 17:17:19 +0800218 return -res;
San Mehat9d10b342010-01-18 09:51:02 -0800219 }
220
221 if (!pid) {
222 close(pipefd[1]);
223 if (pipefd[0] != STDIN_FILENO) {
224 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Luke Huangb5733d72018-08-21 17:17:19 +0800225 int res = errno;
Steve Block5ea0c052012-01-06 19:18:11 +0000226 ALOGE("dup2 failed (%s)", strerror(errno));
Luke Huangb5733d72018-08-21 17:17:19 +0800227 return -res;
San Mehat9d10b342010-01-18 09:51:02 -0800228 }
229 close(pipefd[0]);
230 }
San Mehat9d10b342010-01-18 09:51:02 -0800231
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900232 Fwmark fwmark;
233 fwmark.netId = NetworkController::LOCAL_NET_ID;
234 fwmark.explicitlySelected = true;
235 fwmark.protectedFromVpn = true;
236 fwmark.permission = PERMISSION_SYSTEM;
237 char markStr[UINT32_HEX_STRLEN];
238 snprintf(markStr, sizeof(markStr), "0x%x", fwmark.intValue);
239
Erik Kline5f0358b2018-02-16 15:08:09 +0900240 std::vector<const std::string> argVector = {
241 "/system/bin/dnsmasq",
242 "--keep-in-foreground",
243 "--no-resolv",
244 "--no-poll",
245 "--dhcp-authoritative",
246 // TODO: pipe through metered status from ConnService
247 "--dhcp-option-force=43,ANDROID_METERED",
248 "--pid-file",
249 "--listen-mark", markStr,
250 "--user", kDnsmasqUsername,
251 };
San Mehat9d10b342010-01-18 09:51:02 -0800252
Remi NGUYEN VANedbf5f62018-08-06 15:03:18 +0900253 // DHCP server will be disabled if num_addrs == 0 and no --dhcp-range is passed.
Erik Kline13fa01f2015-11-12 17:49:23 +0900254 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
Remi NGUYEN VANedbf5f62018-08-06 15:03:18 +0900255 argVector.push_back(StringPrintf("--dhcp-range=%s,%s,1h", dhcp_ranges[addrIndex],
256 dhcp_ranges[addrIndex + 1]));
Erik Kline5f0358b2018-02-16 15:08:09 +0900257 }
258
259 auto args = (char**)std::calloc(argVector.size() + 1, sizeof(char*));
260 for (unsigned i = 0; i < argVector.size(); i++) {
261 args[i] = (char*)argVector[i].c_str();
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700262 }
263
264 if (execv(args[0], args)) {
Erik Kline5f0358b2018-02-16 15:08:09 +0900265 ALOGE("execv failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800266 }
Steve Block5ea0c052012-01-06 19:18:11 +0000267 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700268 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800269 } else {
270 close(pipefd[0]);
271 mDaemonPid = pid;
272 mDaemonFd = pipefd[1];
Erik Kline93f9b222017-10-22 21:24:58 +0900273 configureForTethering(true);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800274 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000275 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800276 }
277
278 return 0;
279}
280
Luke Huangb5733d72018-08-21 17:17:19 +0800281std::vector<char*> TetherController::toCstrVec(const std::vector<std::string>& addrs) {
282 std::vector<char*> addrsCstrVec{};
283 addrsCstrVec.reserve(addrs.size());
284 for (const auto& addr : addrs) {
285 addrsCstrVec.push_back(const_cast<char*>(addr.data()));
286 }
287 return addrsCstrVec;
288}
289
290int TetherController::startTethering(const std::vector<std::string>& dhcpRanges) {
291 struct in_addr v4_addr;
292 for (const auto& dhcpRange : dhcpRanges) {
293 if (!inet_aton(dhcpRange.c_str(), &v4_addr)) {
294 return -EINVAL;
295 }
296 }
297 auto dhcp_ranges = toCstrVec(dhcpRanges);
298 return startTethering(dhcp_ranges.size(), dhcp_ranges.data());
299}
300
San Mehat9d10b342010-01-18 09:51:02 -0800301int TetherController::stopTethering() {
Erik Kline93f9b222017-10-22 21:24:58 +0900302 configureForTethering(false);
San Mehat9d10b342010-01-18 09:51:02 -0800303
304 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000305 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800306 return 0;
307 }
308
Steve Block7b984e32011-12-20 16:22:42 +0000309 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800310
311 kill(mDaemonPid, SIGTERM);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700312 waitpid(mDaemonPid, nullptr, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800313 mDaemonPid = 0;
314 close(mDaemonFd);
315 mDaemonFd = -1;
Erik Kline15079dd2018-05-18 23:10:56 +0900316 mDnsmasqState.clear();
Steve Block7b984e32011-12-20 16:22:42 +0000317 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800318 return 0;
319}
Matthew Xie19944102012-07-12 16:42:07 -0700320
San Mehat9d10b342010-01-18 09:51:02 -0800321bool TetherController::isTetheringStarted() {
322 return (mDaemonPid == 0 ? false : true);
323}
324
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900325// dnsmasq can't parse commands larger than this due to the fixed-size buffer
326// in check_android_listeners(). The receiving buffer is 1024 bytes long, but
327// dnsmasq reads up to 1023 bytes.
328#define MAX_CMD_SIZE 1023
Kenny Rootcf52faf2010-02-18 09:59:55 -0800329
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700330int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800331 int i;
San Mehat9d10b342010-01-18 09:51:02 -0800332
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700333 Fwmark fwmark;
334 fwmark.netId = netId;
335 fwmark.explicitlySelected = true;
336 fwmark.protectedFromVpn = true;
337 fwmark.permission = PERMISSION_SYSTEM;
338
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900339 std::string daemonCmd = StringPrintf("update_dns%s0x%x", SEPARATOR, fwmark.intValue);
San Mehat9d10b342010-01-18 09:51:02 -0800340
Erik Kline1d065ba2016-06-08 13:24:45 +0900341 mDnsForwarders.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800342 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700343 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800344
Lorenzo Colittic2841282015-11-25 22:13:57 +0900345 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
Yi Kongbdfd57e2018-07-25 13:26:10 -0700346 int ret = getaddrinfo(servers[i], nullptr, &hints, &res);
Lorenzo Colittic2841282015-11-25 22:13:57 +0900347 freeaddrinfo(res);
348 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000349 ALOGE("Failed to parse DNS server '%s'", servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900350 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900351 errno = EINVAL;
Luke Huangb5733d72018-08-21 17:17:19 +0800352 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800353 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800354
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900355 if (daemonCmd.size() + 1 + strlen(servers[i]) >= MAX_CMD_SIZE) {
356 ALOGE("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800357 break;
358 }
359
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900360 daemonCmd += SEPARATOR;
361 daemonCmd += servers[i];
Erik Kline1d065ba2016-06-08 13:24:45 +0900362 mDnsForwarders.push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800363 }
364
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700365 mDnsNetId = netId;
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900366 mDnsmasqState.update_dns_cmd = std::move(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800367 if (mDaemonFd != -1) {
Erik Kline15079dd2018-05-18 23:10:56 +0900368 if (mDnsmasqState.sendAllState(mDaemonFd) != 0) {
Erik Kline1d065ba2016-06-08 13:24:45 +0900369 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900370 errno = EREMOTEIO;
Luke Huangb5733d72018-08-21 17:17:19 +0800371 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800372 }
373 }
374 return 0;
375}
376
Luke Huangb5733d72018-08-21 17:17:19 +0800377int TetherController::setDnsForwarders(unsigned netId, const std::vector<std::string>& servers) {
378 struct in_addr v4_addr;
379 struct in6_addr v6_addr;
380 for (const auto& server : servers) {
381 if (!inet_pton(AF_INET, server.c_str(), &v4_addr) &&
382 !inet_pton(AF_INET6, server.c_str(), &v6_addr)) {
383 return -EINVAL;
384 }
385 }
386 auto dnsServers = toCstrVec(servers);
387 return setDnsForwarders(netId, dnsServers.data(), dnsServers.size());
388}
389
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700390unsigned TetherController::getDnsNetId() {
391 return mDnsNetId;
392}
393
Erik Kline1d065ba2016-06-08 13:24:45 +0900394const std::list<std::string> &TetherController::getDnsForwarders() const {
San Mehat9d10b342010-01-18 09:51:02 -0800395 return mDnsForwarders;
396}
397
Erik Kline1d065ba2016-06-08 13:24:45 +0900398bool TetherController::applyDnsInterfaces() {
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900399 std::string daemonCmd = "update_ifaces";
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800400 bool haveInterfaces = false;
401
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900402 for (const auto& ifname : mInterfaces) {
403 if (daemonCmd.size() + 1 + ifname.size() >= MAX_CMD_SIZE) {
404 ALOGE("Too many DNS servers listed");
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800405 break;
406 }
407
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900408 daemonCmd += SEPARATOR;
409 daemonCmd += ifname;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800410 haveInterfaces = true;
411 }
412
Erik Kline15079dd2018-05-18 23:10:56 +0900413 if (!haveInterfaces) {
414 mDnsmasqState.update_ifaces_cmd.clear();
415 } else {
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900416 mDnsmasqState.update_ifaces_cmd = std::move(daemonCmd);
Erik Kline15079dd2018-05-18 23:10:56 +0900417 if (mDaemonFd != -1) return (mDnsmasqState.sendAllState(mDaemonFd) == 0);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800418 }
Erik Kline1d065ba2016-06-08 13:24:45 +0900419 return true;
San Mehat9d10b342010-01-18 09:51:02 -0800420}
421
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800422int TetherController::tetherInterface(const char *interface) {
423 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700424 if (!isIfaceName(interface)) {
425 errno = ENOENT;
Luke Huangb5733d72018-08-21 17:17:19 +0800426 return -errno;
JP Abgrall69261cb2014-06-19 18:35:24 -0700427 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800428
Erik Kline1d065ba2016-06-08 13:24:45 +0900429 if (!configureForIPv6Router(interface)) {
430 configureForIPv6Client(interface);
Luke Huangb5733d72018-08-21 17:17:19 +0800431 return -EREMOTEIO;
Erik Kline1d065ba2016-06-08 13:24:45 +0900432 }
433 mInterfaces.push_back(interface);
434
435 if (!applyDnsInterfaces()) {
436 mInterfaces.pop_back();
437 configureForIPv6Client(interface);
Luke Huangb5733d72018-08-21 17:17:19 +0800438 return -EREMOTEIO;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800439 } else {
440 return 0;
441 }
442}
443
San Mehat9d10b342010-01-18 09:51:02 -0800444int TetherController::untetherInterface(const char *interface) {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800445 ALOGD("untetherInterface(%s)", interface);
446
Erik Kline1d065ba2016-06-08 13:24:45 +0900447 for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) {
448 if (!strcmp(interface, it->c_str())) {
449 mInterfaces.erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800450
Erik Kline1d065ba2016-06-08 13:24:45 +0900451 configureForIPv6Client(interface);
Luke Huangb5733d72018-08-21 17:17:19 +0800452 return applyDnsInterfaces() ? 0 : -EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800453 }
454 }
455 errno = ENOENT;
Luke Huangb5733d72018-08-21 17:17:19 +0800456 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800457}
458
Erik Kline1d065ba2016-06-08 13:24:45 +0900459const std::list<std::string> &TetherController::getTetheredInterfaceList() const {
San Mehat9d10b342010-01-18 09:51:02 -0800460 return mInterfaces;
461}
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900462
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900463int TetherController::setupIptablesHooks() {
464 int res;
465 res = setDefaults();
466 if (res < 0) {
467 return res;
468 }
469
470 // Used to limit downstream mss to the upstream pmtu so we don't end up fragmenting every large
471 // packet tethered devices send. This is IPv4-only, because in IPv6 we send the MTU in the RA.
472 // This is no longer optional and tethering will fail to start if it fails.
473 std::string mssRewriteCommand = StringPrintf(
474 "*mangle\n"
475 "-A %s -p tcp --tcp-flags SYN SYN -j TCPMSS --clamp-mss-to-pmtu\n"
476 "COMMIT\n", LOCAL_MANGLE_FORWARD);
477
478 // This is for tethering counters. This chain is reached via --goto, and then RETURNS.
479 std::string defaultCommands = StringPrintf(
480 "*filter\n"
481 ":%s -\n"
482 "COMMIT\n", LOCAL_TETHER_COUNTERS_CHAIN);
483
484 res = iptablesRestoreFunction(V4, mssRewriteCommand, nullptr);
485 if (res < 0) {
486 return res;
487 }
488
489 res = iptablesRestoreFunction(V4V6, defaultCommands, nullptr);
490 if (res < 0) {
491 return res;
492 }
493
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900494 mFwdIfaces.clear();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900495
496 return 0;
497}
498
499int TetherController::setDefaults() {
500 std::string v4Cmd = StringPrintf(
501 "*filter\n"
502 ":%s -\n"
503 "-A %s -j DROP\n"
504 "COMMIT\n"
505 "*nat\n"
506 ":%s -\n"
507 "COMMIT\n", LOCAL_FORWARD, LOCAL_FORWARD, LOCAL_NAT_POSTROUTING);
508
509 std::string v6Cmd = StringPrintf(
510 "*filter\n"
511 ":%s -\n"
512 "COMMIT\n"
513 "*raw\n"
514 ":%s -\n"
515 "COMMIT\n", LOCAL_FORWARD, LOCAL_RAW_PREROUTING);
516
517 int res = iptablesRestoreFunction(V4, v4Cmd, nullptr);
518 if (res < 0) {
519 return res;
520 }
521
522 res = iptablesRestoreFunction(V6, v6Cmd, nullptr);
523 if (res < 0) {
524 return res;
525 }
526
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900527 return 0;
528}
529
530int TetherController::enableNat(const char* intIface, const char* extIface) {
531 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
532
533 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Luke Huang19b49c52018-10-22 12:12:05 +0900534 return -ENODEV;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900535 }
536
537 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
538 if (!strcmp(intIface, extIface)) {
539 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
Luke Huang19b49c52018-10-22 12:12:05 +0900540 return -EINVAL;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900541 }
542
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900543 if (isForwardingPairEnabled(intIface, extIface)) {
544 return 0;
545 }
546
547 // add this if we are the first enabled nat for this upstream
548 if (!isAnyForwardingEnabledOnUpstream(extIface)) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900549 std::vector<std::string> v4Cmds = {
550 "*nat",
551 StringPrintf("-A %s -o %s -j MASQUERADE", LOCAL_NAT_POSTROUTING, extIface),
552 "COMMIT\n"
553 };
554
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900555 if (iptablesRestoreFunction(V4, Join(v4Cmds, '\n'), nullptr) ||
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900556 setupIPv6CountersChain()) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900557 ALOGE("Error setting postroute rule: iface=%s", extIface);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900558 if (!isAnyForwardingPairEnabled()) {
559 // unwind what's been done, but don't care about success - what more could we do?
560 setDefaults();
561 }
Luke Huang19b49c52018-10-22 12:12:05 +0900562 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900563 }
564 }
565
566 if (setForwardRules(true, intIface, extIface) != 0) {
567 ALOGE("Error setting forward rules");
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900568 if (!isAnyForwardingPairEnabled()) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900569 setDefaults();
570 }
Luke Huang19b49c52018-10-22 12:12:05 +0900571 return -ENODEV;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900572 }
573
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900574 return 0;
575}
576
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900577int TetherController::setupIPv6CountersChain() {
578 // Only add this if we are the first enabled nat
579 if (isAnyForwardingPairEnabled()) {
580 return 0;
581 }
582
583 /*
584 * IPv6 tethering doesn't need the state-based conntrack rules, so
585 * it unconditionally jumps to the tether counters chain all the time.
586 */
587 std::vector<std::string> v6Cmds = {
588 "*filter",
589 StringPrintf("-A %s -g %s", LOCAL_FORWARD, LOCAL_TETHER_COUNTERS_CHAIN),
590 "COMMIT\n"
591 };
592
593 return iptablesRestoreFunction(V6, Join(v6Cmds, '\n'), nullptr);
594}
595
596// Gets a pointer to the ForwardingDownstream for an interface pair in the map, or nullptr
597TetherController::ForwardingDownstream* TetherController::findForwardingDownstream(
598 const std::string& intIface, const std::string& extIface) {
599 auto extIfaceMatches = mFwdIfaces.equal_range(extIface);
600 for (auto it = extIfaceMatches.first; it != extIfaceMatches.second; ++it) {
601 if (it->second.iface == intIface) {
602 return &(it->second);
603 }
604 }
605 return nullptr;
606}
607
608void TetherController::addForwardingPair(const std::string& intIface, const std::string& extIface) {
609 ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface);
610 if (existingEntry != nullptr) {
611 existingEntry->active = true;
612 return;
613 }
614
615 mFwdIfaces.insert(std::pair<std::string, ForwardingDownstream>(extIface, {
616 .iface = intIface,
617 .active = true
618 }));
619}
620
621void TetherController::markForwardingPairDisabled(
622 const std::string& intIface, const std::string& extIface) {
623 ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface);
624 if (existingEntry == nullptr) {
625 return;
626 }
627
628 existingEntry->active = false;
629}
630
631bool TetherController::isForwardingPairEnabled(
632 const std::string& intIface, const std::string& extIface) {
633 ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface);
634 return existingEntry != nullptr && existingEntry->active;
635}
636
637bool TetherController::isAnyForwardingEnabledOnUpstream(const std::string& extIface) {
638 auto extIfaceMatches = mFwdIfaces.equal_range(extIface);
639 for (auto it = extIfaceMatches.first; it != extIfaceMatches.second; ++it) {
640 if (it->second.active) {
641 return true;
642 }
643 }
644 return false;
645}
646
647bool TetherController::isAnyForwardingPairEnabled() {
648 for (auto& it : mFwdIfaces) {
649 if (it.second.active) {
650 return true;
651 }
652 }
653 return false;
654}
655
656bool TetherController::tetherCountingRuleExists(
657 const std::string& iface1, const std::string& iface2) {
658 // A counting rule exists if NAT was ever enabled for this interface pair, so if the pair
659 // is in the map regardless of its active status. Rules are added both ways so we check with
660 // the 2 combinations.
661 return findForwardingDownstream(iface1, iface2) != nullptr
662 || findForwardingDownstream(iface2, iface1) != nullptr;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900663}
664
665/* static */
666std::string TetherController::makeTetherCountingRule(const char *if1, const char *if2) {
667 return StringPrintf("-A %s -i %s -o %s -j RETURN", LOCAL_TETHER_COUNTERS_CHAIN, if1, if2);
668}
669
670int TetherController::setForwardRules(bool add, const char *intIface, const char *extIface) {
671 const char *op = add ? "-A" : "-D";
672
673 std::string rpfilterCmd = StringPrintf(
674 "*raw\n"
675 "%s %s -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n"
676 "COMMIT\n", op, LOCAL_RAW_PREROUTING, intIface);
677 if (iptablesRestoreFunction(V6, rpfilterCmd, nullptr) == -1 && add) {
Luke Huang19b49c52018-10-22 12:12:05 +0900678 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900679 }
680
681 std::vector<std::string> v4 = {
hiroaki.yokoyama04f2cb52018-06-13 18:47:30 +0900682 "*raw",
683 StringPrintf("%s %s -p tcp --dport 21 -i %s -j CT --helper ftp", op,
684 LOCAL_RAW_PREROUTING, intIface),
685 StringPrintf("%s %s -p tcp --dport 1723 -i %s -j CT --helper pptp", op,
686 LOCAL_RAW_PREROUTING, intIface),
687 "COMMIT",
688 "*filter",
689 StringPrintf("%s %s -i %s -o %s -m state --state ESTABLISHED,RELATED -g %s", op,
690 LOCAL_FORWARD, extIface, intIface, LOCAL_TETHER_COUNTERS_CHAIN),
691 StringPrintf("%s %s -i %s -o %s -m state --state INVALID -j DROP", op, LOCAL_FORWARD,
692 intIface, extIface),
693 StringPrintf("%s %s -i %s -o %s -g %s", op, LOCAL_FORWARD, intIface, extIface,
694 LOCAL_TETHER_COUNTERS_CHAIN),
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900695 };
696
697 std::vector<std::string> v6 = {
698 "*filter",
699 };
700
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900701 // We only ever add tethering quota rules so that they stick.
702 if (add && !tetherCountingRuleExists(intIface, extIface)) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900703 v4.push_back(makeTetherCountingRule(intIface, extIface));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900704 v4.push_back(makeTetherCountingRule(extIface, intIface));
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900705 v6.push_back(makeTetherCountingRule(intIface, extIface));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900706 v6.push_back(makeTetherCountingRule(extIface, intIface));
707 }
708
709 // Always make sure the drop rule is at the end.
710 // TODO: instead of doing this, consider just rebuilding LOCAL_FORWARD completely from scratch
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900711 // every time, starting with ":tetherctrl_FORWARD -\n". This would likely be a bit simpler.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900712 if (add) {
713 v4.push_back(StringPrintf("-D %s -j DROP", LOCAL_FORWARD));
714 v4.push_back(StringPrintf("-A %s -j DROP", LOCAL_FORWARD));
715 }
716
717 v4.push_back("COMMIT\n");
718 v6.push_back("COMMIT\n");
719
720 // We only add IPv6 rules here, never remove them.
721 if (iptablesRestoreFunction(V4, Join(v4, '\n'), nullptr) == -1 ||
722 (add && iptablesRestoreFunction(V6, Join(v6, '\n'), nullptr) == -1)) {
723 // unwind what's been done, but don't care about success - what more could we do?
724 if (add) {
725 setForwardRules(false, intIface, extIface);
726 }
Luke Huang19b49c52018-10-22 12:12:05 +0900727 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900728 }
729
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900730 if (add) {
731 addForwardingPair(intIface, extIface);
732 } else {
733 markForwardingPairDisabled(intIface, extIface);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900734 }
735
736 return 0;
737}
738
739int TetherController::disableNat(const char* intIface, const char* extIface) {
740 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Luke Huang19b49c52018-10-22 12:12:05 +0900741 return -ENODEV;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900742 }
743
744 setForwardRules(false, intIface, extIface);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900745 if (!isAnyForwardingPairEnabled()) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900746 setDefaults();
747 }
748 return 0;
749}
750
751void TetherController::addStats(TetherStatsList& statsList, const TetherStats& stats) {
752 for (TetherStats& existing : statsList) {
753 if (existing.addStatsIfMatch(stats)) {
754 return;
755 }
756 }
757 // No match. Insert a new interface pair.
758 statsList.push_back(stats);
759}
760
761/*
762 * Parse the ptks and bytes out of:
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900763 * Chain tetherctrl_counters (4 references)
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900764 * pkts bytes target prot opt in out source destination
765 * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
766 * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
767 * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
768 * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
769 * or:
Lorenzo Colitti4604b4a2017-08-24 19:21:50 +0900770 * Chain tetherctrl_counters (0 references)
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900771 * pkts bytes target prot opt in out source destination
772 * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0
773 * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0
774 *
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900775 */
Lorenzo Colitti09353392017-08-24 14:20:32 +0900776int TetherController::addForwardChainStats(TetherStatsList& statsList,
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900777 const std::string& statsOutput,
778 std::string &extraProcessingInfo) {
779 int res;
780 std::string statsLine;
781 char iface0[MAX_IPT_OUTPUT_LINE_LEN];
782 char iface1[MAX_IPT_OUTPUT_LINE_LEN];
783 char rest[MAX_IPT_OUTPUT_LINE_LEN];
784
785 TetherStats stats;
Lorenzo Colitti09353392017-08-24 14:20:32 +0900786 const TetherStats empty;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900787 const char *buffPtr;
788 int64_t packets, bytes;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900789
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900790 std::stringstream stream(statsOutput);
Lorenzo Colitti09353392017-08-24 14:20:32 +0900791
792 // Skip headers.
793 for (int i = 0; i < 2; i++) {
794 std::getline(stream, statsLine, '\n');
795 extraProcessingInfo += statsLine + "\n";
796 if (statsLine.empty()) {
797 ALOGE("Empty header while parsing tethering stats");
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900798 return -EREMOTEIO;
Lorenzo Colitti09353392017-08-24 14:20:32 +0900799 }
800 }
801
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900802 while (std::getline(stream, statsLine, '\n')) {
803 buffPtr = statsLine.c_str();
804
805 /* Clean up, so a failed parse can still print info */
806 iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
807 if (strstr(buffPtr, "0.0.0.0")) {
808 // IPv4 has -- indicating what to do with fragments...
809 // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
810 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
811 &packets, &bytes, iface0, iface1, rest);
812 } else {
813 // ... but IPv6 does not.
814 // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0
815 res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all %s %s ::/%s",
816 &packets, &bytes, iface0, iface1, rest);
817 }
818 ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%" PRId64" bytes=%" PRId64" rest=<%s> orig line=<%s>", res,
819 iface0, iface1, packets, bytes, rest, buffPtr);
820 extraProcessingInfo += buffPtr;
821 extraProcessingInfo += "\n";
822
823 if (res != 5) {
Lorenzo Colitti09353392017-08-24 14:20:32 +0900824 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900825 }
826 /*
827 * The following assumes that the 1st rule has in:extIface out:intIface,
828 * which is what TetherController sets up.
Lorenzo Colitti09353392017-08-24 14:20:32 +0900829 * The 1st matches rx, and sets up the pair for the tx side.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900830 */
Lorenzo Colitti09353392017-08-24 14:20:32 +0900831 if (!stats.intIface[0]) {
832 ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
833 stats.intIface = iface0;
834 stats.extIface = iface1;
Lorenzo Colitti09353392017-08-24 14:20:32 +0900835 stats.txPackets = packets;
836 stats.txBytes = bytes;
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900837 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
838 ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets);
839 stats.rxPackets = packets;
840 stats.rxBytes = bytes;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900841 }
842 if (stats.rxBytes != -1 && stats.txBytes != -1) {
Lorenzo Colitti09353392017-08-24 14:20:32 +0900843 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64, stats.rxBytes, stats.txBytes);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900844 addStats(statsList, stats);
Lorenzo Colitti09353392017-08-24 14:20:32 +0900845 stats = empty;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900846 }
847 }
848
849 /* It is always an error to find only one side of the stats. */
Lorenzo Colitti38fd1362017-09-15 11:40:01 +0900850 if (((stats.rxBytes == -1) != (stats.txBytes == -1))) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900851 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900852 }
853 return 0;
854}
855
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900856StatusOr<TetherController::TetherStatsList> TetherController::getTetherStats() {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900857 TetherStatsList statsList;
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900858 std::string parsedIptablesOutput;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900859
860 for (const IptablesTarget target : {V4, V6}) {
861 std::string statsString;
Lorenzo Colitti09353392017-08-24 14:20:32 +0900862 if (int ret = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString)) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900863 return statusFromErrno(-ret, StringPrintf("failed to fetch tether stats (%d): %d",
864 target, ret));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900865 }
866
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900867 if (int ret = addForwardChainStats(statsList, statsString, parsedIptablesOutput)) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900868 return statusFromErrno(-ret, StringPrintf("failed to parse %s tether stats:\n%s",
869 target == V4 ? "IPv4": "IPv6",
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900870 parsedIptablesOutput.c_str()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900871 }
872 }
873
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900874 return statsList;
875}
876
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900877} // namespace net
878} // namespace android