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