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> |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 21 | #include <stdlib.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 22 | #include <string.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 23 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <sys/stat.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
| 28 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 29 | #include <netinet/in.h> |
| 30 | #include <arpa/inet.h> |
| 31 | |
| 32 | #define LOG_TAG "TetherController" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 33 | #include <android-base/strings.h> |
| 34 | #include <android-base/stringprintf.h> |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 35 | #include <cutils/log.h> |
Kazuhiro Ondo | 6b858eb | 2011-06-24 20:31:03 -0500 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 37 | #include <netdutils/StatusOr.h> |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 38 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 39 | #include "Fwmark.h" |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 40 | #include "NetdConstants.h" |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 41 | #include "Permission.h" |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 42 | #include "InterfaceController.h" |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 43 | #include "NetworkController.h" |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 44 | #include "ResponseCode.h" |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 45 | #include "TetherController.h" |
| 46 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 47 | using android::base::Join; |
| 48 | using android::base::StringPrintf; |
| 49 | using android::base::StringAppendF; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 50 | using android::netdutils::StatusOr; |
| 51 | using android::netdutils::statusFromErrno; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 52 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 53 | namespace { |
| 54 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 55 | const char BP_TOOLS_MODE[] = "bp-tools"; |
| 56 | const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward"; |
| 57 | const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding"; |
| 58 | const char SEPARATOR[] = "|"; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 59 | |
| 60 | bool writeToFile(const char* filename, const char* value) { |
Nick Kralevich | b95c60c | 2016-11-19 09:09:16 -0800 | [diff] [blame] | 61 | int fd = open(filename, O_WRONLY | O_CLOEXEC); |
Nicolas Geoffray | afd4037 | 2015-03-16 11:58:06 +0000 | [diff] [blame] | 62 | if (fd < 0) { |
| 63 | ALOGE("Failed to open %s: %s", filename, strerror(errno)); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | const ssize_t len = strlen(value); |
| 68 | if (write(fd, value, len) != len) { |
| 69 | ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno)); |
| 70 | close(fd); |
| 71 | return false; |
| 72 | } |
| 73 | close(fd); |
| 74 | return true; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 77 | bool configureForIPv6Router(const char *interface) { |
| 78 | return (InterfaceController::setEnableIPv6(interface, 0) == 0) |
| 79 | && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0) |
Erik Kline | 6cddf51 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 80 | && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0) |
| 81 | && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0) |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 82 | && (InterfaceController::setEnableIPv6(interface, 1) == 0); |
| 83 | } |
| 84 | |
| 85 | void configureForIPv6Client(const char *interface) { |
| 86 | InterfaceController::setAcceptIPv6Ra(interface, 1); |
Erik Kline | 6cddf51 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 87 | InterfaceController::setAcceptIPv6Dad(interface, 1); |
| 88 | InterfaceController::setIPv6DadTransmits(interface, "1"); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 89 | InterfaceController::setEnableIPv6(interface, 0); |
| 90 | } |
| 91 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 92 | bool inBpToolsMode() { |
| 93 | // In BP tools mode, do not disable IP forwarding |
| 94 | char bootmode[PROPERTY_VALUE_MAX] = {0}; |
| 95 | property_get("ro.bootmode", bootmode, "unknown"); |
| 96 | return !strcmp(BP_TOOLS_MODE, bootmode); |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 101 | namespace android { |
| 102 | namespace net { |
| 103 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 104 | auto TetherController::iptablesRestoreFunction = execIptablesRestoreWithOutput; |
| 105 | |
| 106 | const int MAX_IPT_OUTPUT_LINE_LEN = 256; |
| 107 | |
| 108 | const std::string GET_TETHER_STATS_COMMAND = StringPrintf( |
| 109 | "*filter\n" |
| 110 | "-nvx -L %s\n" |
| 111 | "COMMIT\n", android::net::TetherController::LOCAL_TETHER_COUNTERS_CHAIN); |
| 112 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 113 | TetherController::TetherController() { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 114 | mDnsNetId = 0; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 115 | mDaemonFd = -1; |
| 116 | mDaemonPid = 0; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 117 | if (inBpToolsMode()) { |
| 118 | enableForwarding(BP_TOOLS_MODE); |
| 119 | } else { |
| 120 | setIpFwdEnabled(); |
| 121 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TetherController::~TetherController() { |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 125 | mInterfaces.clear(); |
| 126 | mDnsForwarders.clear(); |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 127 | mForwardingRequests.clear(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 128 | ifacePairList.clear(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 131 | bool TetherController::setIpFwdEnabled() { |
| 132 | bool success = true; |
| 133 | const char* value = mForwardingRequests.empty() ? "0" : "1"; |
| 134 | ALOGD("Setting IP forward enable = %s", value); |
| 135 | success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value); |
| 136 | success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value); |
| 137 | return success; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 140 | bool TetherController::enableForwarding(const char* requester) { |
| 141 | // Don't return an error if this requester already requested forwarding. Only return errors for |
| 142 | // things that the caller caller needs to care about, such as "couldn't write to the file to |
| 143 | // enable forwarding". |
| 144 | mForwardingRequests.insert(requester); |
| 145 | return setIpFwdEnabled(); |
| 146 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 147 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 148 | bool TetherController::disableForwarding(const char* requester) { |
| 149 | mForwardingRequests.erase(requester); |
| 150 | return setIpFwdEnabled(); |
| 151 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 152 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 153 | size_t TetherController::forwardingRequestCount() { |
| 154 | return mForwardingRequests.size(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 157 | #define TETHER_START_CONST_ARG 10 |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 158 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 159 | int TetherController::startTethering(int num_addrs, char **dhcp_ranges) { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 160 | if (mDaemonPid != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 161 | ALOGE("Tethering already started"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 162 | errno = EBUSY; |
| 163 | return -1; |
| 164 | } |
| 165 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 166 | ALOGD("Starting tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 167 | |
| 168 | pid_t pid; |
| 169 | int pipefd[2]; |
| 170 | |
| 171 | if (pipe(pipefd) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 172 | ALOGE("pipe failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * TODO: Create a monitoring thread to handle and restart |
| 178 | * the daemon if it exits prematurely |
| 179 | */ |
| 180 | if ((pid = fork()) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 181 | ALOGE("fork failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 182 | close(pipefd[0]); |
| 183 | close(pipefd[1]); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | if (!pid) { |
| 188 | close(pipefd[1]); |
| 189 | if (pipefd[0] != STDIN_FILENO) { |
| 190 | if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 191 | ALOGE("dup2 failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 192 | return -1; |
| 193 | } |
| 194 | close(pipefd[0]); |
| 195 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 196 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 197 | Fwmark fwmark; |
| 198 | fwmark.netId = NetworkController::LOCAL_NET_ID; |
| 199 | fwmark.explicitlySelected = true; |
| 200 | fwmark.protectedFromVpn = true; |
| 201 | fwmark.permission = PERMISSION_SYSTEM; |
| 202 | char markStr[UINT32_HEX_STRLEN]; |
| 203 | snprintf(markStr, sizeof(markStr), "0x%x", fwmark.intValue); |
| 204 | |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 205 | int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1; |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 206 | char **args = (char **)malloc(sizeof(char *) * num_processed_args); |
| 207 | args[num_processed_args - 1] = NULL; |
| 208 | args[0] = (char *)"/system/bin/dnsmasq"; |
Peter Nilsson | b756f69 | 2011-09-08 09:48:31 -0700 | [diff] [blame] | 209 | args[1] = (char *)"--keep-in-foreground"; |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 210 | args[2] = (char *)"--no-resolv"; |
| 211 | args[3] = (char *)"--no-poll"; |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 212 | args[4] = (char *)"--dhcp-authoritative"; |
Jeff Sharkey | 6df79da | 2012-04-18 21:53:35 -0700 | [diff] [blame] | 213 | // TODO: pipe through metered status from ConnService |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 214 | args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED"; |
| 215 | args[6] = (char *)"--pid-file"; |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 216 | args[7] = (char *)"--listen-mark"; |
| 217 | args[8] = (char *)markStr; |
| 218 | args[9] = (char *)""; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 219 | |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 220 | int nextArg = TETHER_START_CONST_ARG; |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 221 | for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) { |
| 222 | asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", |
| 223 | dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]); |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (execv(args[0], args)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 227 | ALOGE("execl failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 228 | } |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 229 | ALOGE("Should never get here!"); |
JP Abgrall | ce4f379 | 2012-08-06 13:44:44 -0700 | [diff] [blame] | 230 | _exit(-1); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 231 | } else { |
| 232 | close(pipefd[0]); |
| 233 | mDaemonPid = pid; |
| 234 | mDaemonFd = pipefd[1]; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 235 | applyDnsInterfaces(); |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 236 | ALOGD("Tethering services running"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | int TetherController::stopTethering() { |
| 243 | |
| 244 | if (mDaemonPid == 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 245 | ALOGE("Tethering already stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 249 | ALOGD("Stopping tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 250 | |
| 251 | kill(mDaemonPid, SIGTERM); |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 252 | waitpid(mDaemonPid, NULL, 0); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 253 | mDaemonPid = 0; |
| 254 | close(mDaemonFd); |
| 255 | mDaemonFd = -1; |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 256 | ALOGD("Tethering services stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 257 | return 0; |
| 258 | } |
Matthew Xie | 1994410 | 2012-07-12 16:42:07 -0700 | [diff] [blame] | 259 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 260 | bool TetherController::isTetheringStarted() { |
| 261 | return (mDaemonPid == 0 ? false : true); |
| 262 | } |
| 263 | |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 264 | #define MAX_CMD_SIZE 1024 |
| 265 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 266 | int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 267 | int i; |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 268 | char daemonCmd[MAX_CMD_SIZE]; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 269 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 270 | Fwmark fwmark; |
| 271 | fwmark.netId = netId; |
| 272 | fwmark.explicitlySelected = true; |
| 273 | fwmark.protectedFromVpn = true; |
| 274 | fwmark.permission = PERMISSION_SYSTEM; |
| 275 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 276 | snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue); |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 277 | int cmdLen = strlen(daemonCmd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 278 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 279 | mDnsForwarders.clear(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 280 | for (i = 0; i < numServers; i++) { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 281 | ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 282 | |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 283 | addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST }; |
| 284 | int ret = getaddrinfo(servers[i], NULL, &hints, &res); |
| 285 | freeaddrinfo(res); |
| 286 | if (ret) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 287 | ALOGE("Failed to parse DNS server '%s'", servers[i]); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 288 | mDnsForwarders.clear(); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 289 | errno = EINVAL; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 290 | return -1; |
| 291 | } |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 292 | |
Nick Kralevich | ad5b41f | 2012-07-19 18:48:05 -0700 | [diff] [blame] | 293 | cmdLen += (strlen(servers[i]) + 1); |
| 294 | if (cmdLen + 1 >= MAX_CMD_SIZE) { |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 295 | ALOGD("Too many DNS servers listed"); |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 299 | strcat(daemonCmd, SEPARATOR); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 300 | strcat(daemonCmd, servers[i]); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 301 | mDnsForwarders.push_back(servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 304 | mDnsNetId = netId; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 305 | if (mDaemonFd != -1) { |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 306 | ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 307 | if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 308 | ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno)); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 309 | mDnsForwarders.clear(); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 310 | errno = EREMOTEIO; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 311 | return -1; |
| 312 | } |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 317 | unsigned TetherController::getDnsNetId() { |
| 318 | return mDnsNetId; |
| 319 | } |
| 320 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 321 | const std::list<std::string> &TetherController::getDnsForwarders() const { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 322 | return mDnsForwarders; |
| 323 | } |
| 324 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 325 | bool TetherController::applyDnsInterfaces() { |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 326 | char daemonCmd[MAX_CMD_SIZE]; |
| 327 | |
| 328 | strcpy(daemonCmd, "update_ifaces"); |
| 329 | int cmdLen = strlen(daemonCmd); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 330 | bool haveInterfaces = false; |
| 331 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 332 | for (const auto &ifname : mInterfaces) { |
| 333 | cmdLen += (ifname.size() + 1); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 334 | if (cmdLen + 1 >= MAX_CMD_SIZE) { |
| 335 | ALOGD("Too many DNS ifaces listed"); |
| 336 | break; |
| 337 | } |
| 338 | |
Erik Kline | 13fa01f | 2015-11-12 17:49:23 +0900 | [diff] [blame] | 339 | strcat(daemonCmd, SEPARATOR); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 340 | strcat(daemonCmd, ifname.c_str()); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 341 | haveInterfaces = true; |
| 342 | } |
| 343 | |
| 344 | if ((mDaemonFd != -1) && haveInterfaces) { |
| 345 | ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd); |
| 346 | if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) { |
| 347 | ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno)); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 348 | return false; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 351 | return true; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 354 | int TetherController::tetherInterface(const char *interface) { |
| 355 | ALOGD("tetherInterface(%s)", interface); |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 356 | if (!isIfaceName(interface)) { |
| 357 | errno = ENOENT; |
| 358 | return -1; |
| 359 | } |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 360 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 361 | if (!configureForIPv6Router(interface)) { |
| 362 | configureForIPv6Client(interface); |
| 363 | return -1; |
| 364 | } |
| 365 | mInterfaces.push_back(interface); |
| 366 | |
| 367 | if (!applyDnsInterfaces()) { |
| 368 | mInterfaces.pop_back(); |
| 369 | configureForIPv6Client(interface); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 370 | return -1; |
| 371 | } else { |
| 372 | return 0; |
| 373 | } |
| 374 | } |
| 375 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 376 | int TetherController::untetherInterface(const char *interface) { |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 377 | ALOGD("untetherInterface(%s)", interface); |
| 378 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 379 | for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) { |
| 380 | if (!strcmp(interface, it->c_str())) { |
| 381 | mInterfaces.erase(it); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 382 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 383 | configureForIPv6Client(interface); |
| 384 | return applyDnsInterfaces() ? 0 : -1; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | errno = ENOENT; |
| 388 | return -1; |
| 389 | } |
| 390 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 391 | const std::list<std::string> &TetherController::getTetheredInterfaceList() const { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 392 | return mInterfaces; |
| 393 | } |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 394 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 395 | int TetherController::setupIptablesHooks() { |
| 396 | int res; |
| 397 | res = setDefaults(); |
| 398 | if (res < 0) { |
| 399 | return res; |
| 400 | } |
| 401 | |
| 402 | // Used to limit downstream mss to the upstream pmtu so we don't end up fragmenting every large |
| 403 | // packet tethered devices send. This is IPv4-only, because in IPv6 we send the MTU in the RA. |
| 404 | // This is no longer optional and tethering will fail to start if it fails. |
| 405 | std::string mssRewriteCommand = StringPrintf( |
| 406 | "*mangle\n" |
| 407 | "-A %s -p tcp --tcp-flags SYN SYN -j TCPMSS --clamp-mss-to-pmtu\n" |
| 408 | "COMMIT\n", LOCAL_MANGLE_FORWARD); |
| 409 | |
| 410 | // This is for tethering counters. This chain is reached via --goto, and then RETURNS. |
| 411 | std::string defaultCommands = StringPrintf( |
| 412 | "*filter\n" |
| 413 | ":%s -\n" |
| 414 | "COMMIT\n", LOCAL_TETHER_COUNTERS_CHAIN); |
| 415 | |
| 416 | res = iptablesRestoreFunction(V4, mssRewriteCommand, nullptr); |
| 417 | if (res < 0) { |
| 418 | return res; |
| 419 | } |
| 420 | |
| 421 | res = iptablesRestoreFunction(V4V6, defaultCommands, nullptr); |
| 422 | if (res < 0) { |
| 423 | return res; |
| 424 | } |
| 425 | |
| 426 | ifacePairList.clear(); |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | int TetherController::setDefaults() { |
| 432 | std::string v4Cmd = StringPrintf( |
| 433 | "*filter\n" |
| 434 | ":%s -\n" |
| 435 | "-A %s -j DROP\n" |
| 436 | "COMMIT\n" |
| 437 | "*nat\n" |
| 438 | ":%s -\n" |
| 439 | "COMMIT\n", LOCAL_FORWARD, LOCAL_FORWARD, LOCAL_NAT_POSTROUTING); |
| 440 | |
| 441 | std::string v6Cmd = StringPrintf( |
| 442 | "*filter\n" |
| 443 | ":%s -\n" |
| 444 | "COMMIT\n" |
| 445 | "*raw\n" |
| 446 | ":%s -\n" |
| 447 | "COMMIT\n", LOCAL_FORWARD, LOCAL_RAW_PREROUTING); |
| 448 | |
| 449 | int res = iptablesRestoreFunction(V4, v4Cmd, nullptr); |
| 450 | if (res < 0) { |
| 451 | return res; |
| 452 | } |
| 453 | |
| 454 | res = iptablesRestoreFunction(V6, v6Cmd, nullptr); |
| 455 | if (res < 0) { |
| 456 | return res; |
| 457 | } |
| 458 | |
| 459 | natCount = 0; |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | int TetherController::enableNat(const char* intIface, const char* extIface) { |
| 465 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 466 | |
| 467 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
| 468 | errno = ENODEV; |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */ |
| 473 | if (!strcmp(intIface, extIface)) { |
| 474 | ALOGE("Duplicate interface specified: %s %s", intIface, extIface); |
| 475 | errno = EINVAL; |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | // add this if we are the first added nat |
| 480 | if (natCount == 0) { |
| 481 | std::vector<std::string> v4Cmds = { |
| 482 | "*nat", |
| 483 | StringPrintf("-A %s -o %s -j MASQUERADE", LOCAL_NAT_POSTROUTING, extIface), |
| 484 | "COMMIT\n" |
| 485 | }; |
| 486 | |
| 487 | /* |
| 488 | * IPv6 tethering doesn't need the state-based conntrack rules, so |
| 489 | * it unconditionally jumps to the tether counters chain all the time. |
| 490 | */ |
| 491 | std::vector<std::string> v6Cmds = { |
| 492 | "*filter", |
| 493 | StringPrintf("-A %s -g %s", LOCAL_FORWARD, LOCAL_TETHER_COUNTERS_CHAIN), |
| 494 | "COMMIT\n" |
| 495 | }; |
| 496 | |
| 497 | if (iptablesRestoreFunction(V4, Join(v4Cmds, '\n'), nullptr) || |
| 498 | iptablesRestoreFunction(V6, Join(v6Cmds, '\n'), nullptr)) { |
| 499 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
| 500 | // unwind what's been done, but don't care about success - what more could we do? |
| 501 | setDefaults(); |
| 502 | return -1; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if (setForwardRules(true, intIface, extIface) != 0) { |
| 507 | ALOGE("Error setting forward rules"); |
| 508 | if (natCount == 0) { |
| 509 | setDefaults(); |
| 510 | } |
| 511 | errno = ENODEV; |
| 512 | return -1; |
| 513 | } |
| 514 | |
| 515 | natCount++; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | bool TetherController::checkTetherCountingRuleExist(const std::string& pair_name) { |
| 520 | return std::find(ifacePairList.begin(), ifacePairList.end(), pair_name) != ifacePairList.end(); |
| 521 | } |
| 522 | |
| 523 | /* static */ |
| 524 | std::string TetherController::makeTetherCountingRule(const char *if1, const char *if2) { |
| 525 | return StringPrintf("-A %s -i %s -o %s -j RETURN", LOCAL_TETHER_COUNTERS_CHAIN, if1, if2); |
| 526 | } |
| 527 | |
| 528 | int TetherController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
| 529 | const char *op = add ? "-A" : "-D"; |
| 530 | |
| 531 | std::string rpfilterCmd = StringPrintf( |
| 532 | "*raw\n" |
| 533 | "%s %s -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n" |
| 534 | "COMMIT\n", op, LOCAL_RAW_PREROUTING, intIface); |
| 535 | if (iptablesRestoreFunction(V6, rpfilterCmd, nullptr) == -1 && add) { |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | std::vector<std::string> v4 = { |
| 540 | "*filter", |
| 541 | StringPrintf("%s %s -i %s -o %s -m state --state ESTABLISHED,RELATED -g %s", |
| 542 | op, LOCAL_FORWARD, extIface, intIface, LOCAL_TETHER_COUNTERS_CHAIN), |
| 543 | StringPrintf("%s %s -i %s -o %s -m state --state INVALID -j DROP", |
| 544 | op, LOCAL_FORWARD, intIface, extIface), |
| 545 | StringPrintf("%s %s -i %s -o %s -g %s", |
| 546 | op, LOCAL_FORWARD, intIface, extIface, LOCAL_TETHER_COUNTERS_CHAIN), |
| 547 | }; |
| 548 | |
| 549 | std::vector<std::string> v6 = { |
| 550 | "*filter", |
| 551 | }; |
| 552 | |
| 553 | /* We only ever add tethering quota rules so that they stick. */ |
| 554 | std::string pair1 = StringPrintf("%s_%s", intIface, extIface); |
| 555 | if (add && !checkTetherCountingRuleExist(pair1)) { |
| 556 | v4.push_back(makeTetherCountingRule(intIface, extIface)); |
| 557 | v6.push_back(makeTetherCountingRule(intIface, extIface)); |
| 558 | } |
| 559 | std::string pair2 = StringPrintf("%s_%s", extIface, intIface); |
| 560 | if (add && !checkTetherCountingRuleExist(pair2)) { |
| 561 | v4.push_back(makeTetherCountingRule(extIface, intIface)); |
| 562 | v6.push_back(makeTetherCountingRule(extIface, intIface)); |
| 563 | } |
| 564 | |
| 565 | // Always make sure the drop rule is at the end. |
| 566 | // 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] | 567 | // 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] | 568 | if (add) { |
| 569 | v4.push_back(StringPrintf("-D %s -j DROP", LOCAL_FORWARD)); |
| 570 | v4.push_back(StringPrintf("-A %s -j DROP", LOCAL_FORWARD)); |
| 571 | } |
| 572 | |
| 573 | v4.push_back("COMMIT\n"); |
| 574 | v6.push_back("COMMIT\n"); |
| 575 | |
| 576 | // We only add IPv6 rules here, never remove them. |
| 577 | if (iptablesRestoreFunction(V4, Join(v4, '\n'), nullptr) == -1 || |
| 578 | (add && iptablesRestoreFunction(V6, Join(v6, '\n'), nullptr) == -1)) { |
| 579 | // unwind what's been done, but don't care about success - what more could we do? |
| 580 | if (add) { |
| 581 | setForwardRules(false, intIface, extIface); |
| 582 | } |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | if (add && !checkTetherCountingRuleExist(pair1)) { |
| 587 | ifacePairList.push_front(pair1); |
| 588 | } |
| 589 | if (add && !checkTetherCountingRuleExist(pair2)) { |
| 590 | ifacePairList.push_front(pair2); |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | int TetherController::disableNat(const char* intIface, const char* extIface) { |
| 597 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
| 598 | errno = ENODEV; |
| 599 | return -1; |
| 600 | } |
| 601 | |
| 602 | setForwardRules(false, intIface, extIface); |
| 603 | if (--natCount <= 0) { |
| 604 | // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0 |
| 605 | setDefaults(); |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | void TetherController::addStats(TetherStatsList& statsList, const TetherStats& stats) { |
| 611 | for (TetherStats& existing : statsList) { |
| 612 | if (existing.addStatsIfMatch(stats)) { |
| 613 | return; |
| 614 | } |
| 615 | } |
| 616 | // No match. Insert a new interface pair. |
| 617 | statsList.push_back(stats); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Parse the ptks and bytes out of: |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 622 | * Chain tetherctrl_counters (4 references) |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 623 | * pkts bytes target prot opt in out source destination |
| 624 | * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 625 | * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 |
| 626 | * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 627 | * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 |
| 628 | * or: |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 629 | * Chain tetherctrl_counters (0 references) |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 630 | * pkts bytes target prot opt in out source destination |
| 631 | * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0 |
| 632 | * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0 |
| 633 | * |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 634 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 635 | int TetherController::addForwardChainStats(TetherStatsList& statsList, |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 636 | const std::string& statsOutput, |
| 637 | std::string &extraProcessingInfo) { |
| 638 | int res; |
| 639 | std::string statsLine; |
| 640 | char iface0[MAX_IPT_OUTPUT_LINE_LEN]; |
| 641 | char iface1[MAX_IPT_OUTPUT_LINE_LEN]; |
| 642 | char rest[MAX_IPT_OUTPUT_LINE_LEN]; |
| 643 | |
| 644 | TetherStats stats; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 645 | const TetherStats empty; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 646 | const char *buffPtr; |
| 647 | int64_t packets, bytes; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 648 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 649 | std::stringstream stream(statsOutput); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 650 | |
| 651 | // Skip headers. |
| 652 | for (int i = 0; i < 2; i++) { |
| 653 | std::getline(stream, statsLine, '\n'); |
| 654 | extraProcessingInfo += statsLine + "\n"; |
| 655 | if (statsLine.empty()) { |
| 656 | ALOGE("Empty header while parsing tethering stats"); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 657 | return -EREMOTEIO; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 661 | while (std::getline(stream, statsLine, '\n')) { |
| 662 | buffPtr = statsLine.c_str(); |
| 663 | |
| 664 | /* Clean up, so a failed parse can still print info */ |
| 665 | iface0[0] = iface1[0] = rest[0] = packets = bytes = 0; |
| 666 | if (strstr(buffPtr, "0.0.0.0")) { |
| 667 | // IPv4 has -- indicating what to do with fragments... |
| 668 | // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 669 | res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s", |
| 670 | &packets, &bytes, iface0, iface1, rest); |
| 671 | } else { |
| 672 | // ... but IPv6 does not. |
| 673 | // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0 |
| 674 | res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all %s %s ::/%s", |
| 675 | &packets, &bytes, iface0, iface1, rest); |
| 676 | } |
| 677 | ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%" PRId64" bytes=%" PRId64" rest=<%s> orig line=<%s>", res, |
| 678 | iface0, iface1, packets, bytes, rest, buffPtr); |
| 679 | extraProcessingInfo += buffPtr; |
| 680 | extraProcessingInfo += "\n"; |
| 681 | |
| 682 | if (res != 5) { |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 683 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 684 | } |
| 685 | /* |
| 686 | * The following assumes that the 1st rule has in:extIface out:intIface, |
| 687 | * which is what TetherController sets up. |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 688 | * 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] | 689 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 690 | if (!stats.intIface[0]) { |
| 691 | ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
| 692 | stats.intIface = iface0; |
| 693 | stats.extIface = iface1; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 694 | stats.txPackets = packets; |
| 695 | stats.txBytes = bytes; |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 696 | } else if (stats.intIface == iface1 && stats.extIface == iface0) { |
| 697 | ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64" rx_packets=%" PRId64" ", iface0, iface1, bytes, packets); |
| 698 | stats.rxPackets = packets; |
| 699 | stats.rxBytes = bytes; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 700 | } |
| 701 | if (stats.rxBytes != -1 && stats.txBytes != -1) { |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 702 | ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64, stats.rxBytes, stats.txBytes); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 703 | addStats(statsList, stats); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 704 | stats = empty; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | /* 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^] | 709 | if (((stats.rxBytes == -1) != (stats.txBytes == -1))) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 710 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 711 | } |
| 712 | return 0; |
| 713 | } |
| 714 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 715 | StatusOr<TetherController::TetherStatsList> TetherController::getTetherStats() { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 716 | TetherStatsList statsList; |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 717 | std::string parsedIptablesOutput; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 718 | |
| 719 | for (const IptablesTarget target : {V4, V6}) { |
| 720 | std::string statsString; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 721 | if (int ret = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString)) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 722 | return statusFromErrno(-ret, StringPrintf("failed to fetch tether stats (%d): %d", |
| 723 | target, ret)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 724 | } |
| 725 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 726 | if (int ret = addForwardChainStats(statsList, statsString, parsedIptablesOutput)) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 727 | return statusFromErrno(-ret, StringPrintf("failed to parse %s tether stats:\n%s", |
| 728 | target == V4 ? "IPv4": "IPv6", |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 729 | parsedIptablesOutput.c_str())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 733 | return statsList; |
| 734 | } |
| 735 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 736 | } // namespace net |
| 737 | } // namespace android |