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