San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 17 | #include <errno.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 19 | #include <inttypes.h> |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 20 | #include <netdb.h> |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 21 | #include <spawn.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 22 | #include <string.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 23 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <sys/stat.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
| 28 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 29 | #include <netinet/in.h> |
| 30 | #include <arpa/inet.h> |
| 31 | |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 32 | #include <array> |
| 33 | #include <cstdlib> |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 34 | #include <regex> |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 35 | #include <string> |
| 36 | #include <vector> |
| 37 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 38 | #define LOG_TAG "TetherController" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 39 | #include <android-base/strings.h> |
| 40 | #include <android-base/stringprintf.h> |
Kazuhiro Ondo | 6b858eb | 2011-06-24 20:31:03 -0500 | [diff] [blame] | 41 | #include <cutils/properties.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 42 | #include <log/log.h> |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 43 | #include <netdutils/StatusOr.h> |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 44 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 45 | #include "Controllers.h" |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 46 | #include "Fwmark.h" |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 47 | #include "NetdConstants.h" |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 48 | #include "Permission.h" |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 49 | #include "InterfaceController.h" |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 50 | #include "NetworkController.h" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 51 | #include "ResponseCode.h" |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 52 | #include "TetherController.h" |
| 53 | |
Bernie Innocenti | 4debf3b | 2018-09-12 13:54:50 +0900 | [diff] [blame] | 54 | namespace android { |
| 55 | namespace net { |
| 56 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 57 | using android::base::Join; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 58 | using android::base::StringPrintf; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 59 | using android::netdutils::statusFromErrno; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 60 | using android::netdutils::StatusOr; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 61 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 62 | namespace { |
| 63 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 64 | const char BP_TOOLS_MODE[] = "bp-tools"; |
| 65 | const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward"; |
| 66 | const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding"; |
| 67 | const char SEPARATOR[] = "|"; |
Erik Kline | 93f9b22 | 2017-10-22 21:24:58 +0900 | [diff] [blame] | 68 | constexpr const char kTcpBeLiberal[] = "/proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal"; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 69 | |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 70 | // Chosen to match AID_DNS_TETHER, as made "friendly" by fs_config_generator.py. |
| 71 | constexpr const char kDnsmasqUsername[] = "dns_tether"; |
| 72 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 73 | bool writeToFile(const char* filename, const char* value) { |
Nick Kralevich | b95c60c | 2016-11-19 09:09:16 -0800 | [diff] [blame] | 74 | int fd = open(filename, O_WRONLY | O_CLOEXEC); |
Nicolas Geoffray | afd4037 | 2015-03-16 11:58:06 +0000 | [diff] [blame] | 75 | if (fd < 0) { |
| 76 | ALOGE("Failed to open %s: %s", filename, strerror(errno)); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | const ssize_t len = strlen(value); |
| 81 | if (write(fd, value, len) != len) { |
| 82 | ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno)); |
| 83 | close(fd); |
| 84 | return false; |
| 85 | } |
| 86 | close(fd); |
| 87 | return true; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Erik Kline | 93f9b22 | 2017-10-22 21:24:58 +0900 | [diff] [blame] | 90 | // TODO: Consider altering TCP and UDP timeouts as well. |
| 91 | void configureForTethering(bool enabled) { |
| 92 | writeToFile(kTcpBeLiberal, enabled ? "1" : "0"); |
| 93 | } |
| 94 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 95 | bool configureForIPv6Router(const char *interface) { |
| 96 | return (InterfaceController::setEnableIPv6(interface, 0) == 0) |
| 97 | && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0) |
Erik Kline | 6cddf51 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 98 | && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0) |
| 99 | && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0) |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 100 | && (InterfaceController::setEnableIPv6(interface, 1) == 0); |
| 101 | } |
| 102 | |
| 103 | void configureForIPv6Client(const char *interface) { |
| 104 | InterfaceController::setAcceptIPv6Ra(interface, 1); |
Erik Kline | 6cddf51 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 105 | InterfaceController::setAcceptIPv6Dad(interface, 1); |
| 106 | InterfaceController::setIPv6DadTransmits(interface, "1"); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 107 | InterfaceController::setEnableIPv6(interface, 0); |
| 108 | } |
| 109 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 110 | bool inBpToolsMode() { |
| 111 | // In BP tools mode, do not disable IP forwarding |
| 112 | char bootmode[PROPERTY_VALUE_MAX] = {0}; |
| 113 | property_get("ro.bootmode", bootmode, "unknown"); |
| 114 | return !strcmp(BP_TOOLS_MODE, bootmode); |
| 115 | } |
| 116 | |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 117 | int setPosixSpawnFileActionsAddDup2(posix_spawn_file_actions_t* fa, int fd, int new_fd) { |
| 118 | int res = posix_spawn_file_actions_init(fa); |
| 119 | if (res) { |
| 120 | return res; |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 121 | } |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 122 | return posix_spawn_file_actions_adddup2(fa, fd, new_fd); |
| 123 | } |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 124 | |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 125 | int setPosixSpawnAttrFlags(posix_spawnattr_t* attr, short flags) { |
| 126 | int res = posix_spawnattr_init(attr); |
| 127 | if (res) { |
| 128 | return res; |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 129 | } |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 130 | return posix_spawnattr_setflags(attr, flags); |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 131 | } |
| 132 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 133 | } // namespace |
| 134 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 135 | auto TetherController::iptablesRestoreFunction = execIptablesRestoreWithOutput; |
| 136 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 137 | const std::string GET_TETHER_STATS_COMMAND = StringPrintf( |
| 138 | "*filter\n" |
| 139 | "-nvx -L %s\n" |
| 140 | "COMMIT\n", android::net::TetherController::LOCAL_TETHER_COUNTERS_CHAIN); |
| 141 | |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 142 | int TetherController::DnsmasqState::sendCmd(int daemonFd, const std::string& cmd) { |
| 143 | if (cmd.empty()) return 0; |
| 144 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 145 | gLog.log("Sending update msg to dnsmasq [%s]", cmd.c_str()); |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 146 | // Send the trailing \0 as well. |
| 147 | if (write(daemonFd, cmd.c_str(), cmd.size() + 1) < 0) { |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 148 | gLog.error("Failed to send update command to dnsmasq (%s)", strerror(errno)); |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 149 | errno = EREMOTEIO; |
| 150 | return -1; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | void TetherController::DnsmasqState::clear() { |
| 156 | update_ifaces_cmd.clear(); |
| 157 | update_dns_cmd.clear(); |
| 158 | } |
| 159 | |
| 160 | int TetherController::DnsmasqState::sendAllState(int daemonFd) const { |
| 161 | return sendCmd(daemonFd, update_ifaces_cmd) | sendCmd(daemonFd, update_dns_cmd); |
| 162 | } |
| 163 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 164 | TetherController::TetherController() { |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 165 | if (inBpToolsMode()) { |
| 166 | enableForwarding(BP_TOOLS_MODE); |
| 167 | } else { |
| 168 | setIpFwdEnabled(); |
| 169 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 172 | bool TetherController::setIpFwdEnabled() { |
| 173 | bool success = true; |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 174 | bool disable = mForwardingRequests.empty(); |
| 175 | const char* value = disable ? "0" : "1"; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 176 | ALOGD("Setting IP forward enable = %s", value); |
| 177 | success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value); |
| 178 | success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value); |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 179 | if (disable) { |
| 180 | // Turning off the forwarding sysconf in the kernel has the side effect |
| 181 | // of turning on ICMP redirect, which is a security hazard. |
| 182 | // Turn ICMP redirect back off immediately. |
| 183 | int rv = InterfaceController::disableIcmpRedirects(); |
| 184 | success &= (rv == 0); |
| 185 | } |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 186 | return success; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 189 | bool TetherController::enableForwarding(const char* requester) { |
| 190 | // Don't return an error if this requester already requested forwarding. Only return errors for |
| 191 | // things that the caller caller needs to care about, such as "couldn't write to the file to |
| 192 | // enable forwarding". |
| 193 | mForwardingRequests.insert(requester); |
| 194 | return setIpFwdEnabled(); |
| 195 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 196 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 197 | bool TetherController::disableForwarding(const char* requester) { |
| 198 | mForwardingRequests.erase(requester); |
| 199 | return setIpFwdEnabled(); |
| 200 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 201 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 202 | size_t TetherController::forwardingRequestCount() { |
| 203 | return mForwardingRequests.size(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 206 | int TetherController::startTethering(int num_addrs, char **dhcp_ranges) { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 207 | if (mDaemonPid != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 208 | ALOGE("Tethering already started"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 209 | errno = EBUSY; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 210 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 213 | ALOGD("Starting tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 214 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 215 | int pipefd[2]; |
| 216 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 217 | if (pipe2(pipefd, O_CLOEXEC) < 0) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 218 | int res = errno; |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 219 | ALOGE("pipe2() failed (%s)", strerror(errno)); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 220 | return -res; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 223 | // Set parameters |
| 224 | Fwmark fwmark; |
| 225 | fwmark.netId = NetworkController::LOCAL_NET_ID; |
| 226 | fwmark.explicitlySelected = true; |
| 227 | fwmark.protectedFromVpn = true; |
| 228 | fwmark.permission = PERMISSION_SYSTEM; |
| 229 | char markStr[UINT32_HEX_STRLEN]; |
| 230 | snprintf(markStr, sizeof(markStr), "0x%x", fwmark.intValue); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 231 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 232 | std::vector<const std::string> argVector = { |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 233 | "/system/bin/dnsmasq", |
| 234 | "--keep-in-foreground", |
| 235 | "--no-resolv", |
| 236 | "--no-poll", |
| 237 | "--dhcp-authoritative", |
| 238 | // TODO: pipe through metered status from ConnService |
| 239 | "--dhcp-option-force=43,ANDROID_METERED", |
| 240 | "--pid-file", |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 241 | "--listen-mark", |
| 242 | markStr, |
| 243 | "--user", |
| 244 | kDnsmasqUsername, |
| 245 | }; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 246 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 247 | // DHCP server will be disabled if num_addrs == 0 and no --dhcp-range is |
| 248 | // passed. |
| 249 | for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) { |
| 250 | argVector.push_back(StringPrintf("--dhcp-range=%s,%s,1h", dhcp_ranges[addrIndex], |
| 251 | dhcp_ranges[addrIndex + 1])); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 254 | auto args = (char**)std::calloc(argVector.size() + 1, sizeof(char*)); |
| 255 | for (unsigned i = 0; i < argVector.size(); i++) { |
| 256 | args[i] = (char*)argVector[i].c_str(); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * TODO: Create a monitoring thread to handle and restart |
| 261 | * the daemon if it exits prematurely |
| 262 | */ |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 263 | |
| 264 | // Note that don't modify any memory between vfork and execv. |
| 265 | // Changing state of file descriptors would be fine. See posix_spawn_file_actions_add* |
| 266 | // dup2 creates fd without CLOEXEC, dnsmasq will receive commands through the |
| 267 | // duplicated fd. |
| 268 | posix_spawn_file_actions_t fa; |
| 269 | int res = setPosixSpawnFileActionsAddDup2(&fa, pipefd[0], STDIN_FILENO); |
| 270 | if (res) { |
| 271 | ALOGE("posix_spawn set fa failed (%s)", strerror(res)); |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 272 | return -res; |
| 273 | } |
| 274 | |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 275 | posix_spawnattr_t attr; |
| 276 | res = setPosixSpawnAttrFlags(&attr, POSIX_SPAWN_USEVFORK); |
| 277 | if (res) { |
| 278 | ALOGE("posix_spawn set attr flag failed (%s)", strerror(res)); |
| 279 | return -res; |
| 280 | } |
| 281 | |
| 282 | pid_t pid; |
| 283 | res = posix_spawn(&pid, args[0], &fa, &attr, args, nullptr); |
| 284 | close(pipefd[0]); |
| 285 | free(args); |
| 286 | posix_spawnattr_destroy(&attr); |
| 287 | posix_spawn_file_actions_destroy(&fa); |
| 288 | if (res) { |
| 289 | ALOGE("posix_spawn failed (%s)", strerror(res)); |
| 290 | close(pipefd[1]); |
| 291 | return -res; |
| 292 | } |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 293 | mDaemonPid = pid; |
| 294 | mDaemonFd = pipefd[1]; |
| 295 | configureForTethering(true); |
| 296 | applyDnsInterfaces(); |
| 297 | ALOGD("Tethering services running"); |
| 298 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 302 | std::vector<char*> TetherController::toCstrVec(const std::vector<std::string>& addrs) { |
| 303 | std::vector<char*> addrsCstrVec{}; |
| 304 | addrsCstrVec.reserve(addrs.size()); |
| 305 | for (const auto& addr : addrs) { |
| 306 | addrsCstrVec.push_back(const_cast<char*>(addr.data())); |
| 307 | } |
| 308 | return addrsCstrVec; |
| 309 | } |
| 310 | |
| 311 | int TetherController::startTethering(const std::vector<std::string>& dhcpRanges) { |
| 312 | struct in_addr v4_addr; |
| 313 | for (const auto& dhcpRange : dhcpRanges) { |
| 314 | if (!inet_aton(dhcpRange.c_str(), &v4_addr)) { |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | } |
| 318 | auto dhcp_ranges = toCstrVec(dhcpRanges); |
| 319 | return startTethering(dhcp_ranges.size(), dhcp_ranges.data()); |
| 320 | } |
| 321 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 322 | int TetherController::stopTethering() { |
Erik Kline | 93f9b22 | 2017-10-22 21:24:58 +0900 | [diff] [blame] | 323 | configureForTethering(false); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 324 | |
| 325 | if (mDaemonPid == 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 326 | ALOGE("Tethering already stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 330 | ALOGD("Stopping tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 331 | |
| 332 | kill(mDaemonPid, SIGTERM); |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 333 | waitpid(mDaemonPid, nullptr, 0); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 334 | mDaemonPid = 0; |
| 335 | close(mDaemonFd); |
| 336 | mDaemonFd = -1; |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 337 | mDnsmasqState.clear(); |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 338 | ALOGD("Tethering services stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 339 | return 0; |
| 340 | } |
Matthew Xie | 1994410 | 2012-07-12 16:42:07 -0700 | [diff] [blame] | 341 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 342 | bool TetherController::isTetheringStarted() { |
| 343 | return (mDaemonPid == 0 ? false : true); |
| 344 | } |
| 345 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 346 | // dnsmasq can't parse commands larger than this due to the fixed-size buffer |
| 347 | // in check_android_listeners(). The receiving buffer is 1024 bytes long, but |
| 348 | // dnsmasq reads up to 1023 bytes. |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 349 | const size_t MAX_CMD_SIZE = 1023; |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 350 | |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 351 | // TODO: convert callers to the overload taking a vector<string> |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 352 | int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 353 | Fwmark fwmark; |
| 354 | fwmark.netId = netId; |
| 355 | fwmark.explicitlySelected = true; |
| 356 | fwmark.protectedFromVpn = true; |
| 357 | fwmark.permission = PERMISSION_SYSTEM; |
| 358 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 359 | std::string daemonCmd = StringPrintf("update_dns%s0x%x", SEPARATOR, fwmark.intValue); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 360 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 361 | mDnsForwarders.clear(); |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 362 | for (int i = 0; i < numServers; i++) { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 363 | ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 364 | |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 365 | addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST }; |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 366 | int ret = getaddrinfo(servers[i], nullptr, &hints, &res); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 367 | freeaddrinfo(res); |
| 368 | if (ret) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 369 | ALOGE("Failed to parse DNS server '%s'", servers[i]); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 370 | mDnsForwarders.clear(); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 371 | errno = EINVAL; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 372 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 373 | } |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 374 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 375 | if (daemonCmd.size() + 1 + strlen(servers[i]) >= MAX_CMD_SIZE) { |
| 376 | ALOGE("Too many DNS servers listed"); |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 380 | daemonCmd += SEPARATOR; |
| 381 | daemonCmd += servers[i]; |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 382 | mDnsForwarders.push_back(servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 385 | mDnsNetId = netId; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 386 | mDnsmasqState.update_dns_cmd = std::move(daemonCmd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 387 | if (mDaemonFd != -1) { |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 388 | if (mDnsmasqState.sendAllState(mDaemonFd) != 0) { |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 389 | mDnsForwarders.clear(); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 390 | errno = EREMOTEIO; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 391 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 397 | int TetherController::setDnsForwarders(unsigned netId, const std::vector<std::string>& servers) { |
| 398 | struct in_addr v4_addr; |
| 399 | struct in6_addr v6_addr; |
| 400 | for (const auto& server : servers) { |
| 401 | if (!inet_pton(AF_INET, server.c_str(), &v4_addr) && |
| 402 | !inet_pton(AF_INET6, server.c_str(), &v6_addr)) { |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | } |
| 406 | auto dnsServers = toCstrVec(servers); |
| 407 | return setDnsForwarders(netId, dnsServers.data(), dnsServers.size()); |
| 408 | } |
| 409 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 410 | unsigned TetherController::getDnsNetId() { |
| 411 | return mDnsNetId; |
| 412 | } |
| 413 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 414 | const std::list<std::string> &TetherController::getDnsForwarders() const { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 415 | return mDnsForwarders; |
| 416 | } |
| 417 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 418 | bool TetherController::applyDnsInterfaces() { |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 419 | std::string daemonCmd = "update_ifaces"; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 420 | bool haveInterfaces = false; |
| 421 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 422 | for (const auto& ifname : mInterfaces) { |
| 423 | if (daemonCmd.size() + 1 + ifname.size() >= MAX_CMD_SIZE) { |
| 424 | ALOGE("Too many DNS servers listed"); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 428 | daemonCmd += SEPARATOR; |
| 429 | daemonCmd += ifname; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 430 | haveInterfaces = true; |
| 431 | } |
| 432 | |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 433 | if (!haveInterfaces) { |
| 434 | mDnsmasqState.update_ifaces_cmd.clear(); |
| 435 | } else { |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 436 | mDnsmasqState.update_ifaces_cmd = std::move(daemonCmd); |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 437 | if (mDaemonFd != -1) return (mDnsmasqState.sendAllState(mDaemonFd) == 0); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 438 | } |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 439 | return true; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 442 | int TetherController::tetherInterface(const char *interface) { |
| 443 | ALOGD("tetherInterface(%s)", interface); |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 444 | if (!isIfaceName(interface)) { |
| 445 | errno = ENOENT; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 446 | return -errno; |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 447 | } |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 448 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 449 | if (!configureForIPv6Router(interface)) { |
| 450 | configureForIPv6Client(interface); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 451 | return -EREMOTEIO; |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 452 | } |
| 453 | mInterfaces.push_back(interface); |
| 454 | |
| 455 | if (!applyDnsInterfaces()) { |
| 456 | mInterfaces.pop_back(); |
| 457 | configureForIPv6Client(interface); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 458 | return -EREMOTEIO; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 459 | } else { |
| 460 | return 0; |
| 461 | } |
| 462 | } |
| 463 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 464 | int TetherController::untetherInterface(const char *interface) { |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 465 | ALOGD("untetherInterface(%s)", interface); |
| 466 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 467 | for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) { |
| 468 | if (!strcmp(interface, it->c_str())) { |
| 469 | mInterfaces.erase(it); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 470 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 471 | configureForIPv6Client(interface); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 472 | return applyDnsInterfaces() ? 0 : -EREMOTEIO; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | errno = ENOENT; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 476 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 479 | const std::list<std::string> &TetherController::getTetheredInterfaceList() const { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 480 | return mInterfaces; |
| 481 | } |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 482 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 483 | int TetherController::setupIptablesHooks() { |
| 484 | int res; |
| 485 | res = setDefaults(); |
| 486 | if (res < 0) { |
| 487 | return res; |
| 488 | } |
| 489 | |
| 490 | // Used to limit downstream mss to the upstream pmtu so we don't end up fragmenting every large |
| 491 | // packet tethered devices send. This is IPv4-only, because in IPv6 we send the MTU in the RA. |
| 492 | // This is no longer optional and tethering will fail to start if it fails. |
| 493 | std::string mssRewriteCommand = StringPrintf( |
| 494 | "*mangle\n" |
| 495 | "-A %s -p tcp --tcp-flags SYN SYN -j TCPMSS --clamp-mss-to-pmtu\n" |
| 496 | "COMMIT\n", LOCAL_MANGLE_FORWARD); |
| 497 | |
| 498 | // This is for tethering counters. This chain is reached via --goto, and then RETURNS. |
| 499 | std::string defaultCommands = StringPrintf( |
| 500 | "*filter\n" |
| 501 | ":%s -\n" |
| 502 | "COMMIT\n", LOCAL_TETHER_COUNTERS_CHAIN); |
| 503 | |
| 504 | res = iptablesRestoreFunction(V4, mssRewriteCommand, nullptr); |
| 505 | if (res < 0) { |
| 506 | return res; |
| 507 | } |
| 508 | |
| 509 | res = iptablesRestoreFunction(V4V6, defaultCommands, nullptr); |
| 510 | if (res < 0) { |
| 511 | return res; |
| 512 | } |
| 513 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 514 | mFwdIfaces.clear(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | int TetherController::setDefaults() { |
| 520 | std::string v4Cmd = StringPrintf( |
| 521 | "*filter\n" |
| 522 | ":%s -\n" |
| 523 | "-A %s -j DROP\n" |
| 524 | "COMMIT\n" |
| 525 | "*nat\n" |
| 526 | ":%s -\n" |
| 527 | "COMMIT\n", LOCAL_FORWARD, LOCAL_FORWARD, LOCAL_NAT_POSTROUTING); |
| 528 | |
| 529 | std::string v6Cmd = StringPrintf( |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 530 | "*filter\n" |
| 531 | ":%s -\n" |
| 532 | "COMMIT\n" |
| 533 | "*raw\n" |
| 534 | ":%s -\n" |
| 535 | "COMMIT\n", |
| 536 | LOCAL_FORWARD, LOCAL_RAW_PREROUTING); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 537 | |
| 538 | int res = iptablesRestoreFunction(V4, v4Cmd, nullptr); |
| 539 | if (res < 0) { |
| 540 | return res; |
| 541 | } |
| 542 | |
| 543 | res = iptablesRestoreFunction(V6, v6Cmd, nullptr); |
| 544 | if (res < 0) { |
| 545 | return res; |
| 546 | } |
| 547 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | int TetherController::enableNat(const char* intIface, const char* extIface) { |
| 552 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 553 | |
| 554 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 555 | return -ENODEV; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */ |
| 559 | if (!strcmp(intIface, extIface)) { |
| 560 | ALOGE("Duplicate interface specified: %s %s", intIface, extIface); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 561 | return -EINVAL; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 562 | } |
| 563 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 564 | if (isForwardingPairEnabled(intIface, extIface)) { |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | // add this if we are the first enabled nat for this upstream |
| 569 | if (!isAnyForwardingEnabledOnUpstream(extIface)) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 570 | std::vector<std::string> v4Cmds = { |
| 571 | "*nat", |
| 572 | StringPrintf("-A %s -o %s -j MASQUERADE", LOCAL_NAT_POSTROUTING, extIface), |
| 573 | "COMMIT\n" |
| 574 | }; |
| 575 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 576 | if (iptablesRestoreFunction(V4, Join(v4Cmds, '\n'), nullptr) || setupIPv6CountersChain() || |
| 577 | setTetherGlobalAlertRule()) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 578 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 579 | if (!isAnyForwardingPairEnabled()) { |
| 580 | // unwind what's been done, but don't care about success - what more could we do? |
| 581 | setDefaults(); |
| 582 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 583 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | if (setForwardRules(true, intIface, extIface) != 0) { |
| 588 | ALOGE("Error setting forward rules"); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 589 | if (!isAnyForwardingPairEnabled()) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 590 | setDefaults(); |
| 591 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 592 | return -ENODEV; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 593 | } |
| 594 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 598 | int TetherController::setTetherGlobalAlertRule() { |
| 599 | // Only add this if we are the first enabled nat |
| 600 | if (isAnyForwardingPairEnabled()) { |
| 601 | return 0; |
| 602 | } |
| 603 | const std::string cmds = |
| 604 | "*filter\n" + |
| 605 | StringPrintf("-I %s -j %s\n", LOCAL_FORWARD, BandwidthController::LOCAL_GLOBAL_ALERT) + |
| 606 | "COMMIT\n"; |
| 607 | |
| 608 | return iptablesRestoreFunction(V4V6, cmds, nullptr); |
| 609 | } |
| 610 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 611 | int TetherController::setupIPv6CountersChain() { |
| 612 | // Only add this if we are the first enabled nat |
| 613 | if (isAnyForwardingPairEnabled()) { |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * IPv6 tethering doesn't need the state-based conntrack rules, so |
| 619 | * it unconditionally jumps to the tether counters chain all the time. |
| 620 | */ |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 621 | const std::string v6Cmds = |
| 622 | "*filter\n" + |
| 623 | StringPrintf("-A %s -g %s\n", LOCAL_FORWARD, LOCAL_TETHER_COUNTERS_CHAIN) + "COMMIT\n"; |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 624 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 625 | return iptablesRestoreFunction(V6, v6Cmds, nullptr); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // Gets a pointer to the ForwardingDownstream for an interface pair in the map, or nullptr |
| 629 | TetherController::ForwardingDownstream* TetherController::findForwardingDownstream( |
| 630 | const std::string& intIface, const std::string& extIface) { |
| 631 | auto extIfaceMatches = mFwdIfaces.equal_range(extIface); |
| 632 | for (auto it = extIfaceMatches.first; it != extIfaceMatches.second; ++it) { |
| 633 | if (it->second.iface == intIface) { |
| 634 | return &(it->second); |
| 635 | } |
| 636 | } |
| 637 | return nullptr; |
| 638 | } |
| 639 | |
| 640 | void TetherController::addForwardingPair(const std::string& intIface, const std::string& extIface) { |
| 641 | ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface); |
| 642 | if (existingEntry != nullptr) { |
| 643 | existingEntry->active = true; |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | mFwdIfaces.insert(std::pair<std::string, ForwardingDownstream>(extIface, { |
| 648 | .iface = intIface, |
| 649 | .active = true |
| 650 | })); |
| 651 | } |
| 652 | |
| 653 | void TetherController::markForwardingPairDisabled( |
| 654 | const std::string& intIface, const std::string& extIface) { |
| 655 | ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface); |
| 656 | if (existingEntry == nullptr) { |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | existingEntry->active = false; |
| 661 | } |
| 662 | |
| 663 | bool TetherController::isForwardingPairEnabled( |
| 664 | const std::string& intIface, const std::string& extIface) { |
| 665 | ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface); |
| 666 | return existingEntry != nullptr && existingEntry->active; |
| 667 | } |
| 668 | |
| 669 | bool TetherController::isAnyForwardingEnabledOnUpstream(const std::string& extIface) { |
| 670 | auto extIfaceMatches = mFwdIfaces.equal_range(extIface); |
| 671 | for (auto it = extIfaceMatches.first; it != extIfaceMatches.second; ++it) { |
| 672 | if (it->second.active) { |
| 673 | return true; |
| 674 | } |
| 675 | } |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | bool TetherController::isAnyForwardingPairEnabled() { |
| 680 | for (auto& it : mFwdIfaces) { |
| 681 | if (it.second.active) { |
| 682 | return true; |
| 683 | } |
| 684 | } |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | bool TetherController::tetherCountingRuleExists( |
| 689 | const std::string& iface1, const std::string& iface2) { |
| 690 | // A counting rule exists if NAT was ever enabled for this interface pair, so if the pair |
| 691 | // is in the map regardless of its active status. Rules are added both ways so we check with |
| 692 | // the 2 combinations. |
| 693 | return findForwardingDownstream(iface1, iface2) != nullptr |
| 694 | || findForwardingDownstream(iface2, iface1) != nullptr; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* static */ |
| 698 | std::string TetherController::makeTetherCountingRule(const char *if1, const char *if2) { |
| 699 | return StringPrintf("-A %s -i %s -o %s -j RETURN", LOCAL_TETHER_COUNTERS_CHAIN, if1, if2); |
| 700 | } |
| 701 | |
| 702 | int TetherController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
| 703 | const char *op = add ? "-A" : "-D"; |
| 704 | |
| 705 | std::string rpfilterCmd = StringPrintf( |
| 706 | "*raw\n" |
| 707 | "%s %s -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n" |
| 708 | "COMMIT\n", op, LOCAL_RAW_PREROUTING, intIface); |
| 709 | if (iptablesRestoreFunction(V6, rpfilterCmd, nullptr) == -1 && add) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 710 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | std::vector<std::string> v4 = { |
hiroaki.yokoyama | 04f2cb5 | 2018-06-13 18:47:30 +0900 | [diff] [blame] | 714 | "*raw", |
| 715 | StringPrintf("%s %s -p tcp --dport 21 -i %s -j CT --helper ftp", op, |
| 716 | LOCAL_RAW_PREROUTING, intIface), |
| 717 | StringPrintf("%s %s -p tcp --dport 1723 -i %s -j CT --helper pptp", op, |
| 718 | LOCAL_RAW_PREROUTING, intIface), |
| 719 | "COMMIT", |
| 720 | "*filter", |
| 721 | StringPrintf("%s %s -i %s -o %s -m state --state ESTABLISHED,RELATED -g %s", op, |
| 722 | LOCAL_FORWARD, extIface, intIface, LOCAL_TETHER_COUNTERS_CHAIN), |
| 723 | StringPrintf("%s %s -i %s -o %s -m state --state INVALID -j DROP", op, LOCAL_FORWARD, |
| 724 | intIface, extIface), |
| 725 | StringPrintf("%s %s -i %s -o %s -g %s", op, LOCAL_FORWARD, intIface, extIface, |
| 726 | LOCAL_TETHER_COUNTERS_CHAIN), |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | std::vector<std::string> v6 = { |
| 730 | "*filter", |
| 731 | }; |
| 732 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 733 | // We only ever add tethering quota rules so that they stick. |
| 734 | if (add && !tetherCountingRuleExists(intIface, extIface)) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 735 | v4.push_back(makeTetherCountingRule(intIface, extIface)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 736 | v4.push_back(makeTetherCountingRule(extIface, intIface)); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 737 | v6.push_back(makeTetherCountingRule(intIface, extIface)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 738 | v6.push_back(makeTetherCountingRule(extIface, intIface)); |
| 739 | } |
| 740 | |
| 741 | // Always make sure the drop rule is at the end. |
| 742 | // TODO: instead of doing this, consider just rebuilding LOCAL_FORWARD completely from scratch |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 743 | // every time, starting with ":tetherctrl_FORWARD -\n". This would likely be a bit simpler. |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 744 | if (add) { |
| 745 | v4.push_back(StringPrintf("-D %s -j DROP", LOCAL_FORWARD)); |
| 746 | v4.push_back(StringPrintf("-A %s -j DROP", LOCAL_FORWARD)); |
| 747 | } |
| 748 | |
| 749 | v4.push_back("COMMIT\n"); |
| 750 | v6.push_back("COMMIT\n"); |
| 751 | |
| 752 | // We only add IPv6 rules here, never remove them. |
| 753 | if (iptablesRestoreFunction(V4, Join(v4, '\n'), nullptr) == -1 || |
| 754 | (add && iptablesRestoreFunction(V6, Join(v6, '\n'), nullptr) == -1)) { |
| 755 | // unwind what's been done, but don't care about success - what more could we do? |
| 756 | if (add) { |
| 757 | setForwardRules(false, intIface, extIface); |
| 758 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 759 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 760 | } |
| 761 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 762 | if (add) { |
| 763 | addForwardingPair(intIface, extIface); |
| 764 | } else { |
| 765 | markForwardingPairDisabled(intIface, extIface); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | int TetherController::disableNat(const char* intIface, const char* extIface) { |
| 772 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 773 | errno = ENODEV; |
| 774 | return -errno; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | setForwardRules(false, intIface, extIface); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 778 | if (!isAnyForwardingPairEnabled()) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 779 | setDefaults(); |
| 780 | } |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | void TetherController::addStats(TetherStatsList& statsList, const TetherStats& stats) { |
| 785 | for (TetherStats& existing : statsList) { |
| 786 | if (existing.addStatsIfMatch(stats)) { |
| 787 | return; |
| 788 | } |
| 789 | } |
| 790 | // No match. Insert a new interface pair. |
| 791 | statsList.push_back(stats); |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Parse the ptks and bytes out of: |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 796 | * Chain tetherctrl_counters (4 references) |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 797 | * pkts bytes target prot opt in out source destination |
| 798 | * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 799 | * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 |
| 800 | * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 801 | * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 |
| 802 | * or: |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 803 | * Chain tetherctrl_counters (0 references) |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 804 | * pkts bytes target prot opt in out source destination |
| 805 | * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0 |
| 806 | * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0 |
| 807 | * |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 808 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 809 | int TetherController::addForwardChainStats(TetherStatsList& statsList, |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 810 | const std::string& statsOutput, |
| 811 | std::string &extraProcessingInfo) { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 812 | enum IndexOfIptChain { |
| 813 | ORIG_LINE, |
| 814 | PACKET_COUNTS, |
| 815 | BYTE_COUNTS, |
| 816 | HYPHEN, |
| 817 | IFACE0_NAME, |
| 818 | IFACE1_NAME, |
| 819 | SOURCE, |
| 820 | DESTINATION |
| 821 | }; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 822 | TetherStats stats; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 823 | const TetherStats empty; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 824 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 825 | static const std::string NUM = "(\\d+)"; |
| 826 | static const std::string IFACE = "([^\\s]+)"; |
| 827 | static const std::string DST = "(0.0.0.0/0|::/0)"; |
| 828 | static const std::string COUNTERS = "\\s*" + NUM + "\\s+" + NUM + |
| 829 | " RETURN all( -- | )" + IFACE + "\\s+" + IFACE + |
| 830 | "\\s+" + DST + "\\s+" + DST; |
| 831 | static const std::regex IP_RE(COUNTERS); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 832 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 833 | const std::vector<std::string> lines = base::Split(statsOutput, "\n"); |
| 834 | int headerLine = 0; |
| 835 | for (const std::string& line : lines) { |
| 836 | // Skip headers. |
| 837 | if (headerLine < 2) { |
| 838 | if (line.empty()) { |
| 839 | ALOGV("Empty header while parsing tethering stats"); |
| 840 | return -EREMOTEIO; |
| 841 | } |
| 842 | headerLine++; |
| 843 | continue; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 844 | } |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 845 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 846 | if (line.empty()) continue; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 847 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 848 | extraProcessingInfo = line; |
| 849 | std::smatch matches; |
| 850 | if (!std::regex_search(line, matches, IP_RE)) return -EREMOTEIO; |
| 851 | // Here use IP_RE to distiguish IPv4 and IPv6 iptables. |
| 852 | // IPv4 has "--" indicating what to do with fragments... |
| 853 | // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 854 | // ... but IPv6 does not. |
| 855 | // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0 |
| 856 | // TODO: Replace strtoXX() calls with ParseUint() /ParseInt() |
| 857 | int64_t packets = strtoul(matches[PACKET_COUNTS].str().c_str(), nullptr, 10); |
| 858 | int64_t bytes = strtoul(matches[BYTE_COUNTS].str().c_str(), nullptr, 10); |
| 859 | std::string iface0 = matches[IFACE0_NAME].str(); |
| 860 | std::string iface1 = matches[IFACE1_NAME].str(); |
| 861 | std::string rest = matches[SOURCE].str(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 862 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 863 | ALOGV("parse iface0=<%s> iface1=<%s> pkts=%" PRId64 " bytes=%" PRId64 |
| 864 | " rest=<%s> orig line=<%s>", |
| 865 | iface0.c_str(), iface1.c_str(), packets, bytes, rest.c_str(), line.c_str()); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 866 | /* |
| 867 | * The following assumes that the 1st rule has in:extIface out:intIface, |
| 868 | * which is what TetherController sets up. |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 869 | * The 1st matches rx, and sets up the pair for the tx side. |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 870 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 871 | if (!stats.intIface[0]) { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 872 | ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64 " rx_packets=%" PRId64 |
| 873 | " ", iface0.c_str(), iface1.c_str(), bytes, packets); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 874 | stats.intIface = iface0; |
| 875 | stats.extIface = iface1; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 876 | stats.txPackets = packets; |
| 877 | stats.txBytes = bytes; |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 878 | } else if (stats.intIface == iface1 && stats.extIface == iface0) { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 879 | ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64 " rx_packets=%" PRId64 |
| 880 | " ", iface0.c_str(), iface1.c_str(), bytes, packets); |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 881 | stats.rxPackets = packets; |
| 882 | stats.rxBytes = bytes; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 883 | } |
| 884 | if (stats.rxBytes != -1 && stats.txBytes != -1) { |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 885 | ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64, stats.rxBytes, stats.txBytes); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 886 | addStats(statsList, stats); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 887 | stats = empty; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | /* It is always an error to find only one side of the stats. */ |
Lorenzo Colitti | 38fd136 | 2017-09-15 11:40:01 +0900 | [diff] [blame] | 892 | if (((stats.rxBytes == -1) != (stats.txBytes == -1))) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 893 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 894 | } |
| 895 | return 0; |
| 896 | } |
| 897 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 898 | StatusOr<TetherController::TetherStatsList> TetherController::getTetherStats() { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 899 | TetherStatsList statsList; |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 900 | std::string parsedIptablesOutput; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 901 | |
| 902 | for (const IptablesTarget target : {V4, V6}) { |
| 903 | std::string statsString; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 904 | if (int ret = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString)) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 905 | return statusFromErrno(-ret, StringPrintf("failed to fetch tether stats (%d): %d", |
| 906 | target, ret)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 907 | } |
| 908 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 909 | if (int ret = addForwardChainStats(statsList, statsString, parsedIptablesOutput)) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 910 | return statusFromErrno(-ret, StringPrintf("failed to parse %s tether stats:\n%s", |
| 911 | target == V4 ? "IPv4": "IPv6", |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 912 | parsedIptablesOutput.c_str())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 916 | return statsList; |
| 917 | } |
| 918 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 919 | } // namespace net |
| 920 | } // namespace android |