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