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