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