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