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