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> |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 21 | #include <spawn.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 | |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 32 | #include <array> |
| 33 | #include <cstdlib> |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 34 | #include <regex> |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 35 | #include <string> |
| 36 | #include <vector> |
| 37 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 38 | #define LOG_TAG "TetherController" |
Maciej Żenczykowski | 2cffd94 | 2019-05-05 13:40:35 -0700 | [diff] [blame] | 39 | #include <android-base/scopeguard.h> |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 41 | #include <android-base/strings.h> |
| 42 | #include <android-base/unique_fd.h> |
Kazuhiro Ondo | 6b858eb | 2011-06-24 20:31:03 -0500 | [diff] [blame] | 43 | #include <cutils/properties.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 44 | #include <log/log.h> |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 45 | #include <net/if.h> |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 46 | #include <netdutils/DumpWriter.h> |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 47 | #include <netdutils/StatusOr.h> |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 48 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 49 | #include "Controllers.h" |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 50 | #include "Fwmark.h" |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 51 | #include "InterfaceController.h" |
Mike Yu | f0e019f | 2019-03-13 14:43:39 +0800 | [diff] [blame] | 52 | #include "NetdConstants.h" |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 53 | #include "NetworkController.h" |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 54 | #include "OffloadUtils.h" |
Mike Yu | f0e019f | 2019-03-13 14:43:39 +0800 | [diff] [blame] | 55 | #include "Permission.h" |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 56 | #include "TetherController.h" |
| 57 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 58 | #include "android/net/TetherOffloadRuleParcel.h" |
| 59 | |
Bernie Innocenti | 4debf3b | 2018-09-12 13:54:50 +0900 | [diff] [blame] | 60 | namespace android { |
| 61 | namespace net { |
| 62 | |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 63 | using android::base::Error; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 64 | using android::base::Join; |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 65 | using android::base::Pipe; |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 66 | using android::base::Result; |
| 67 | using android::base::StringAppendF; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 68 | using android::base::StringPrintf; |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 69 | using android::base::unique_fd; |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 70 | using android::net::TetherOffloadRuleParcel; |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 71 | using android::netdutils::DumpWriter; |
| 72 | using android::netdutils::ScopedIndent; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 73 | using android::netdutils::statusFromErrno; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 74 | using android::netdutils::StatusOr; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 75 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 76 | namespace { |
| 77 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 78 | const char BP_TOOLS_MODE[] = "bp-tools"; |
| 79 | const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward"; |
| 80 | const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding"; |
| 81 | const char SEPARATOR[] = "|"; |
Erik Kline | 93f9b22 | 2017-10-22 21:24:58 +0900 | [diff] [blame] | 82 | constexpr const char kTcpBeLiberal[] = "/proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal"; |
Hungming Chen | 68dade3 | 2020-02-20 13:50:56 +0800 | [diff] [blame] | 83 | |
| 84 | // Dummy interface name, the name needs to be longer than 16 characters so it can never conflict |
| 85 | // with a real interface name. See also IFNAMSIZ. |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 86 | constexpr const char kBpfOffloadInterface[] = "BPFOffloadInterface"; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 87 | |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 88 | // Chosen to match AID_DNS_TETHER, as made "friendly" by fs_config_generator.py. |
| 89 | constexpr const char kDnsmasqUsername[] = "dns_tether"; |
| 90 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 91 | bool writeToFile(const char* filename, const char* value) { |
Nick Kralevich | b95c60c | 2016-11-19 09:09:16 -0800 | [diff] [blame] | 92 | int fd = open(filename, O_WRONLY | O_CLOEXEC); |
Nicolas Geoffray | afd4037 | 2015-03-16 11:58:06 +0000 | [diff] [blame] | 93 | if (fd < 0) { |
| 94 | ALOGE("Failed to open %s: %s", filename, strerror(errno)); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | const ssize_t len = strlen(value); |
| 99 | if (write(fd, value, len) != len) { |
| 100 | ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno)); |
| 101 | close(fd); |
| 102 | return false; |
| 103 | } |
| 104 | close(fd); |
| 105 | return true; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Erik Kline | 93f9b22 | 2017-10-22 21:24:58 +0900 | [diff] [blame] | 108 | // TODO: Consider altering TCP and UDP timeouts as well. |
| 109 | void configureForTethering(bool enabled) { |
| 110 | writeToFile(kTcpBeLiberal, enabled ? "1" : "0"); |
| 111 | } |
| 112 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 113 | bool configureForIPv6Router(const char *interface) { |
| 114 | return (InterfaceController::setEnableIPv6(interface, 0) == 0) |
| 115 | && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0) |
Erik Kline | 6cddf51 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 116 | && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0) |
| 117 | && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0) |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 118 | && (InterfaceController::setEnableIPv6(interface, 1) == 0); |
| 119 | } |
| 120 | |
| 121 | void configureForIPv6Client(const char *interface) { |
| 122 | InterfaceController::setAcceptIPv6Ra(interface, 1); |
Erik Kline | 6cddf51 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 123 | InterfaceController::setAcceptIPv6Dad(interface, 1); |
| 124 | InterfaceController::setIPv6DadTransmits(interface, "1"); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 125 | InterfaceController::setEnableIPv6(interface, 0); |
| 126 | } |
| 127 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 128 | bool inBpToolsMode() { |
| 129 | // In BP tools mode, do not disable IP forwarding |
| 130 | char bootmode[PROPERTY_VALUE_MAX] = {0}; |
| 131 | property_get("ro.bootmode", bootmode, "unknown"); |
| 132 | return !strcmp(BP_TOOLS_MODE, bootmode); |
| 133 | } |
| 134 | |
| 135 | } // namespace |
| 136 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 137 | auto TetherController::iptablesRestoreFunction = execIptablesRestoreWithOutput; |
| 138 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 139 | const std::string GET_TETHER_STATS_COMMAND = StringPrintf( |
| 140 | "*filter\n" |
| 141 | "-nvx -L %s\n" |
| 142 | "COMMIT\n", android::net::TetherController::LOCAL_TETHER_COUNTERS_CHAIN); |
| 143 | |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 144 | int TetherController::DnsmasqState::sendCmd(int daemonFd, const std::string& cmd) { |
| 145 | if (cmd.empty()) return 0; |
| 146 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 147 | gLog.log("Sending update msg to dnsmasq [%s]", cmd.c_str()); |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 148 | // Send the trailing \0 as well. |
| 149 | if (write(daemonFd, cmd.c_str(), cmd.size() + 1) < 0) { |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 150 | gLog.error("Failed to send update command to dnsmasq (%s)", strerror(errno)); |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 151 | errno = EREMOTEIO; |
| 152 | return -1; |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | void TetherController::DnsmasqState::clear() { |
| 158 | update_ifaces_cmd.clear(); |
| 159 | update_dns_cmd.clear(); |
| 160 | } |
| 161 | |
| 162 | int TetherController::DnsmasqState::sendAllState(int daemonFd) const { |
| 163 | return sendCmd(daemonFd, update_ifaces_cmd) | sendCmd(daemonFd, update_dns_cmd); |
| 164 | } |
| 165 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 166 | TetherController::TetherController() { |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 167 | if (inBpToolsMode()) { |
| 168 | enableForwarding(BP_TOOLS_MODE); |
| 169 | } else { |
| 170 | setIpFwdEnabled(); |
| 171 | } |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 172 | maybeInitMaps(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 175 | bool TetherController::setIpFwdEnabled() { |
| 176 | bool success = true; |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 177 | bool disable = mForwardingRequests.empty(); |
| 178 | const char* value = disable ? "0" : "1"; |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 179 | ALOGD("Setting IP forward enable = %s", value); |
| 180 | success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value); |
| 181 | success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value); |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 182 | if (disable) { |
| 183 | // Turning off the forwarding sysconf in the kernel has the side effect |
| 184 | // of turning on ICMP redirect, which is a security hazard. |
| 185 | // Turn ICMP redirect back off immediately. |
| 186 | int rv = InterfaceController::disableIcmpRedirects(); |
| 187 | success &= (rv == 0); |
| 188 | } |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 189 | return success; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 192 | bool TetherController::enableForwarding(const char* requester) { |
| 193 | // Don't return an error if this requester already requested forwarding. Only return errors for |
| 194 | // things that the caller caller needs to care about, such as "couldn't write to the file to |
| 195 | // enable forwarding". |
| 196 | mForwardingRequests.insert(requester); |
| 197 | return setIpFwdEnabled(); |
| 198 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 199 | |
Lorenzo Colitti | 799625c | 2015-02-25 12:52:00 +0900 | [diff] [blame] | 200 | bool TetherController::disableForwarding(const char* requester) { |
| 201 | mForwardingRequests.erase(requester); |
| 202 | return setIpFwdEnabled(); |
| 203 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 204 | |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 205 | void TetherController::maybeInitMaps() { |
| 206 | if (!bpf::isBpfSupported()) return; |
| 207 | |
| 208 | // Used for parsing tether stats from BPF maps. If open the map failed, skip to open |
| 209 | // tether BPF offload maps. |
| 210 | mIfaceIndexNameMap.init(IFACE_INDEX_NAME_MAP_PATH); |
| 211 | if (!mIfaceIndexNameMap.isValid()) return; |
| 212 | |
| 213 | // Open BPF maps, ignoring errors because the device might not support BPF offload. |
| 214 | int fd = getTetherIngressMapFd(); |
Maciej Żenczykowski | 109ac53 | 2020-02-19 10:36:44 -0800 | [diff] [blame] | 215 | if (fd >= 0) { |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 216 | mBpfIngressMap.reset(fd); |
Maciej Żenczykowski | 109ac53 | 2020-02-19 10:36:44 -0800 | [diff] [blame] | 217 | mBpfIngressMap.clear(); |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 218 | } |
| 219 | fd = getTetherStatsMapFd(); |
Maciej Żenczykowski | 109ac53 | 2020-02-19 10:36:44 -0800 | [diff] [blame] | 220 | if (fd >= 0) { |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 221 | mBpfStatsMap.reset(fd); |
Maciej Żenczykowski | 109ac53 | 2020-02-19 10:36:44 -0800 | [diff] [blame] | 222 | mBpfStatsMap.clear(); |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 223 | } |
Maciej Żenczykowski | d25a734 | 2020-05-04 18:18:38 -0700 | [diff] [blame] | 224 | fd = getTetherLimitMapFd(); |
| 225 | if (fd >= 0) { |
| 226 | mBpfLimitMap.reset(fd); |
| 227 | mBpfLimitMap.clear(); |
| 228 | } |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 231 | const std::set<std::string>& TetherController::getIpfwdRequesterList() const { |
| 232 | return mForwardingRequests; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 235 | int TetherController::startTethering(bool usingLegacyDnsProxy, int num_addrs, char** dhcp_ranges) { |
| 236 | if (!usingLegacyDnsProxy && num_addrs == 0) { |
| 237 | // Both DHCP and DnsProxy are disabled, we don't need to start dnsmasq |
| 238 | configureForTethering(true); |
| 239 | mIsTetheringStarted = true; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | if (mIsTetheringStarted) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 244 | ALOGE("Tethering already started"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 245 | errno = EBUSY; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 246 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 249 | ALOGD("Starting tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 250 | |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 251 | unique_fd pipeRead, pipeWrite; |
| 252 | if (!Pipe(&pipeRead, &pipeWrite, O_CLOEXEC)) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 253 | int res = errno; |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 254 | ALOGE("pipe2() failed (%s)", strerror(errno)); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 255 | return -res; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 258 | // Set parameters |
| 259 | Fwmark fwmark; |
| 260 | fwmark.netId = NetworkController::LOCAL_NET_ID; |
| 261 | fwmark.explicitlySelected = true; |
| 262 | fwmark.protectedFromVpn = true; |
| 263 | fwmark.permission = PERMISSION_SYSTEM; |
| 264 | char markStr[UINT32_HEX_STRLEN]; |
| 265 | snprintf(markStr, sizeof(markStr), "0x%x", fwmark.intValue); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 266 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 267 | std::vector<const std::string> argVector = { |
Erik Kline | 5f0358b | 2018-02-16 15:08:09 +0900 | [diff] [blame] | 268 | "/system/bin/dnsmasq", |
| 269 | "--keep-in-foreground", |
| 270 | "--no-resolv", |
| 271 | "--no-poll", |
| 272 | "--dhcp-authoritative", |
| 273 | // TODO: pipe through metered status from ConnService |
| 274 | "--dhcp-option-force=43,ANDROID_METERED", |
| 275 | "--pid-file", |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 276 | "--listen-mark", |
| 277 | markStr, |
| 278 | "--user", |
| 279 | kDnsmasqUsername, |
| 280 | }; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 281 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 282 | if (!usingLegacyDnsProxy) { |
| 283 | argVector.push_back("--port=0"); |
| 284 | } |
| 285 | |
| 286 | // DHCP server will be disabled if num_addrs == 0 and no --dhcp-range is passed. |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 287 | for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) { |
| 288 | argVector.push_back(StringPrintf("--dhcp-range=%s,%s,1h", dhcp_ranges[addrIndex], |
| 289 | dhcp_ranges[addrIndex + 1])); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 290 | } |
| 291 | |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 292 | std::vector<char*> args(argVector.size() + 1); |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 293 | for (unsigned i = 0; i < argVector.size(); i++) { |
| 294 | args[i] = (char*)argVector[i].c_str(); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * TODO: Create a monitoring thread to handle and restart |
| 299 | * the daemon if it exits prematurely |
| 300 | */ |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 301 | |
| 302 | // Note that don't modify any memory between vfork and execv. |
| 303 | // Changing state of file descriptors would be fine. See posix_spawn_file_actions_add* |
| 304 | // dup2 creates fd without CLOEXEC, dnsmasq will receive commands through the |
| 305 | // duplicated fd. |
| 306 | posix_spawn_file_actions_t fa; |
Maciej Żenczykowski | 2cffd94 | 2019-05-05 13:40:35 -0700 | [diff] [blame] | 307 | int res = posix_spawn_file_actions_init(&fa); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 308 | if (res) { |
Maciej Żenczykowski | 2cffd94 | 2019-05-05 13:40:35 -0700 | [diff] [blame] | 309 | ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res)); |
| 310 | return -res; |
| 311 | } |
| 312 | const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); }; |
| 313 | res = posix_spawn_file_actions_adddup2(&fa, pipeRead.get(), STDIN_FILENO); |
| 314 | if (res) { |
| 315 | ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res)); |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 316 | return -res; |
| 317 | } |
| 318 | |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 319 | posix_spawnattr_t attr; |
Maciej Żenczykowski | 2cffd94 | 2019-05-05 13:40:35 -0700 | [diff] [blame] | 320 | res = posix_spawnattr_init(&attr); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 321 | if (res) { |
Maciej Żenczykowski | 2cffd94 | 2019-05-05 13:40:35 -0700 | [diff] [blame] | 322 | ALOGE("posix_spawnattr_init failed (%s)", strerror(res)); |
| 323 | return -res; |
| 324 | } |
| 325 | const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); }; |
| 326 | res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK); |
| 327 | if (res) { |
| 328 | ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res)); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 329 | return -res; |
| 330 | } |
| 331 | |
| 332 | pid_t pid; |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 333 | res = posix_spawn(&pid, args[0], &fa, &attr, &args[0], nullptr); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 334 | if (res) { |
| 335 | ALOGE("posix_spawn failed (%s)", strerror(res)); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 336 | return -res; |
| 337 | } |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 338 | mDaemonPid = pid; |
George Burgess IV | e3dc131 | 2019-02-28 11:27:04 -0800 | [diff] [blame] | 339 | mDaemonFd = pipeWrite.release(); |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 340 | configureForTethering(true); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 341 | mIsTetheringStarted = true; |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 342 | applyDnsInterfaces(); |
| 343 | ALOGD("Tethering services running"); |
| 344 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 348 | std::vector<char*> TetherController::toCstrVec(const std::vector<std::string>& addrs) { |
| 349 | std::vector<char*> addrsCstrVec{}; |
| 350 | addrsCstrVec.reserve(addrs.size()); |
| 351 | for (const auto& addr : addrs) { |
| 352 | addrsCstrVec.push_back(const_cast<char*>(addr.data())); |
| 353 | } |
| 354 | return addrsCstrVec; |
| 355 | } |
| 356 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 357 | int TetherController::startTethering(bool usingLegacyDnsProxy, |
| 358 | const std::vector<std::string>& dhcpRanges) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 359 | struct in_addr v4_addr; |
| 360 | for (const auto& dhcpRange : dhcpRanges) { |
| 361 | if (!inet_aton(dhcpRange.c_str(), &v4_addr)) { |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | } |
| 365 | auto dhcp_ranges = toCstrVec(dhcpRanges); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 366 | return startTethering(usingLegacyDnsProxy, dhcp_ranges.size(), dhcp_ranges.data()); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 367 | } |
| 368 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 369 | int TetherController::stopTethering() { |
Erik Kline | 93f9b22 | 2017-10-22 21:24:58 +0900 | [diff] [blame] | 370 | configureForTethering(false); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 371 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 372 | if (!mIsTetheringStarted) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 373 | ALOGE("Tethering already stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 377 | mIsTetheringStarted = false; |
| 378 | // dnsmasq is not started |
| 379 | if (mDaemonPid == 0) { |
| 380 | return 0; |
| 381 | } |
| 382 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 383 | ALOGD("Stopping tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 384 | |
| 385 | kill(mDaemonPid, SIGTERM); |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 386 | waitpid(mDaemonPid, nullptr, 0); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 387 | mDaemonPid = 0; |
| 388 | close(mDaemonFd); |
| 389 | mDaemonFd = -1; |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 390 | mDnsmasqState.clear(); |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 391 | ALOGD("Tethering services stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 392 | return 0; |
| 393 | } |
Matthew Xie | 1994410 | 2012-07-12 16:42:07 -0700 | [diff] [blame] | 394 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 395 | bool TetherController::isTetheringStarted() { |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 396 | return mIsTetheringStarted; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 399 | // dnsmasq can't parse commands larger than this due to the fixed-size buffer |
| 400 | // in check_android_listeners(). The receiving buffer is 1024 bytes long, but |
| 401 | // dnsmasq reads up to 1023 bytes. |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 402 | const size_t MAX_CMD_SIZE = 1023; |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 403 | |
Luke Huang | 8dc1cac | 2019-03-30 16:12:31 +0800 | [diff] [blame] | 404 | // TODO: Remove overload function and update this after NDC migration. |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 405 | int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 406 | Fwmark fwmark; |
| 407 | fwmark.netId = netId; |
| 408 | fwmark.explicitlySelected = true; |
| 409 | fwmark.protectedFromVpn = true; |
| 410 | fwmark.permission = PERMISSION_SYSTEM; |
| 411 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 412 | std::string daemonCmd = StringPrintf("update_dns%s0x%x", SEPARATOR, fwmark.intValue); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 413 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 414 | mDnsForwarders.clear(); |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 415 | for (int i = 0; i < numServers; i++) { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 416 | ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 417 | |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 418 | addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST }; |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 419 | int ret = getaddrinfo(servers[i], nullptr, &hints, &res); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 420 | freeaddrinfo(res); |
| 421 | if (ret) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 422 | ALOGE("Failed to parse DNS server '%s'", servers[i]); |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 423 | mDnsForwarders.clear(); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 424 | errno = EINVAL; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 425 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 426 | } |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 427 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 428 | if (daemonCmd.size() + 1 + strlen(servers[i]) >= MAX_CMD_SIZE) { |
| 429 | ALOGE("Too many DNS servers listed"); |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 433 | daemonCmd += SEPARATOR; |
| 434 | daemonCmd += servers[i]; |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 435 | mDnsForwarders.push_back(servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 438 | mDnsNetId = netId; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 439 | mDnsmasqState.update_dns_cmd = std::move(daemonCmd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 440 | if (mDaemonFd != -1) { |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 441 | if (mDnsmasqState.sendAllState(mDaemonFd) != 0) { |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 442 | mDnsForwarders.clear(); |
Lorenzo Colitti | c284128 | 2015-11-25 22:13:57 +0900 | [diff] [blame] | 443 | errno = EREMOTEIO; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 444 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 450 | int TetherController::setDnsForwarders(unsigned netId, const std::vector<std::string>& servers) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 451 | auto dnsServers = toCstrVec(servers); |
| 452 | return setDnsForwarders(netId, dnsServers.data(), dnsServers.size()); |
| 453 | } |
| 454 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 455 | unsigned TetherController::getDnsNetId() { |
| 456 | return mDnsNetId; |
| 457 | } |
| 458 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 459 | const std::list<std::string> &TetherController::getDnsForwarders() const { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 460 | return mDnsForwarders; |
| 461 | } |
| 462 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 463 | bool TetherController::applyDnsInterfaces() { |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 464 | std::string daemonCmd = "update_ifaces"; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 465 | bool haveInterfaces = false; |
| 466 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 467 | for (const auto& ifname : mInterfaces) { |
| 468 | if (daemonCmd.size() + 1 + ifname.size() >= MAX_CMD_SIZE) { |
| 469 | ALOGE("Too many DNS servers listed"); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 473 | daemonCmd += SEPARATOR; |
| 474 | daemonCmd += ifname; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 475 | haveInterfaces = true; |
| 476 | } |
| 477 | |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 478 | if (!haveInterfaces) { |
| 479 | mDnsmasqState.update_ifaces_cmd.clear(); |
| 480 | } else { |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 481 | mDnsmasqState.update_ifaces_cmd = std::move(daemonCmd); |
Erik Kline | 15079dd | 2018-05-18 23:10:56 +0900 | [diff] [blame] | 482 | if (mDaemonFd != -1) return (mDnsmasqState.sendAllState(mDaemonFd) == 0); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 483 | } |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 484 | return true; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 487 | int TetherController::tetherInterface(const char *interface) { |
| 488 | ALOGD("tetherInterface(%s)", interface); |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 489 | if (!isIfaceName(interface)) { |
| 490 | errno = ENOENT; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 491 | return -errno; |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 492 | } |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 493 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 494 | if (!configureForIPv6Router(interface)) { |
| 495 | configureForIPv6Client(interface); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 496 | return -EREMOTEIO; |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 497 | } |
| 498 | mInterfaces.push_back(interface); |
| 499 | |
| 500 | if (!applyDnsInterfaces()) { |
| 501 | mInterfaces.pop_back(); |
| 502 | configureForIPv6Client(interface); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 503 | return -EREMOTEIO; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 504 | } else { |
| 505 | return 0; |
| 506 | } |
| 507 | } |
| 508 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 509 | int TetherController::untetherInterface(const char *interface) { |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 510 | ALOGD("untetherInterface(%s)", interface); |
| 511 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 512 | for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) { |
| 513 | if (!strcmp(interface, it->c_str())) { |
| 514 | mInterfaces.erase(it); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 515 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 516 | configureForIPv6Client(interface); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 517 | return applyDnsInterfaces() ? 0 : -EREMOTEIO; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | errno = ENOENT; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 521 | return -errno; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Erik Kline | 1d065ba | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 524 | const std::list<std::string> &TetherController::getTetheredInterfaceList() const { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 525 | return mInterfaces; |
| 526 | } |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 527 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 528 | int TetherController::setupIptablesHooks() { |
| 529 | int res; |
| 530 | res = setDefaults(); |
| 531 | if (res < 0) { |
| 532 | return res; |
| 533 | } |
| 534 | |
| 535 | // Used to limit downstream mss to the upstream pmtu so we don't end up fragmenting every large |
| 536 | // packet tethered devices send. This is IPv4-only, because in IPv6 we send the MTU in the RA. |
| 537 | // This is no longer optional and tethering will fail to start if it fails. |
| 538 | std::string mssRewriteCommand = StringPrintf( |
| 539 | "*mangle\n" |
| 540 | "-A %s -p tcp --tcp-flags SYN SYN -j TCPMSS --clamp-mss-to-pmtu\n" |
| 541 | "COMMIT\n", LOCAL_MANGLE_FORWARD); |
| 542 | |
| 543 | // This is for tethering counters. This chain is reached via --goto, and then RETURNS. |
| 544 | std::string defaultCommands = StringPrintf( |
| 545 | "*filter\n" |
| 546 | ":%s -\n" |
| 547 | "COMMIT\n", LOCAL_TETHER_COUNTERS_CHAIN); |
| 548 | |
| 549 | res = iptablesRestoreFunction(V4, mssRewriteCommand, nullptr); |
| 550 | if (res < 0) { |
| 551 | return res; |
| 552 | } |
| 553 | |
| 554 | res = iptablesRestoreFunction(V4V6, defaultCommands, nullptr); |
| 555 | if (res < 0) { |
| 556 | return res; |
| 557 | } |
| 558 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 559 | mFwdIfaces.clear(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | int TetherController::setDefaults() { |
| 565 | std::string v4Cmd = StringPrintf( |
| 566 | "*filter\n" |
| 567 | ":%s -\n" |
| 568 | "-A %s -j DROP\n" |
| 569 | "COMMIT\n" |
| 570 | "*nat\n" |
| 571 | ":%s -\n" |
| 572 | "COMMIT\n", LOCAL_FORWARD, LOCAL_FORWARD, LOCAL_NAT_POSTROUTING); |
| 573 | |
| 574 | std::string v6Cmd = StringPrintf( |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 575 | "*filter\n" |
| 576 | ":%s -\n" |
| 577 | "COMMIT\n" |
| 578 | "*raw\n" |
| 579 | ":%s -\n" |
| 580 | "COMMIT\n", |
| 581 | LOCAL_FORWARD, LOCAL_RAW_PREROUTING); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 582 | |
| 583 | int res = iptablesRestoreFunction(V4, v4Cmd, nullptr); |
| 584 | if (res < 0) { |
| 585 | return res; |
| 586 | } |
| 587 | |
| 588 | res = iptablesRestoreFunction(V6, v6Cmd, nullptr); |
| 589 | if (res < 0) { |
| 590 | return res; |
| 591 | } |
| 592 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | int TetherController::enableNat(const char* intIface, const char* extIface) { |
| 597 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 598 | |
| 599 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 600 | return -ENODEV; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */ |
| 604 | if (!strcmp(intIface, extIface)) { |
| 605 | ALOGE("Duplicate interface specified: %s %s", intIface, extIface); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 606 | return -EINVAL; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 607 | } |
| 608 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 609 | if (isForwardingPairEnabled(intIface, extIface)) { |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | // add this if we are the first enabled nat for this upstream |
Maciej Żenczykowski | 99665c3 | 2020-04-09 10:50:42 +0000 | [diff] [blame] | 614 | bool firstDownstreamForThisUpstream = !isAnyForwardingEnabledOnUpstream(extIface); |
| 615 | if (firstDownstreamForThisUpstream) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 616 | std::vector<std::string> v4Cmds = { |
| 617 | "*nat", |
| 618 | StringPrintf("-A %s -o %s -j MASQUERADE", LOCAL_NAT_POSTROUTING, extIface), |
| 619 | "COMMIT\n" |
| 620 | }; |
| 621 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 622 | if (iptablesRestoreFunction(V4, Join(v4Cmds, '\n'), nullptr) || setupIPv6CountersChain() || |
| 623 | setTetherGlobalAlertRule()) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 624 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 625 | if (!isAnyForwardingPairEnabled()) { |
| 626 | // unwind what's been done, but don't care about success - what more could we do? |
| 627 | setDefaults(); |
| 628 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 629 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | if (setForwardRules(true, intIface, extIface) != 0) { |
| 634 | ALOGE("Error setting forward rules"); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 635 | if (!isAnyForwardingPairEnabled()) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 636 | setDefaults(); |
| 637 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 638 | return -ENODEV; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 639 | } |
| 640 | |
Maciej Żenczykowski | 99665c3 | 2020-04-09 10:50:42 +0000 | [diff] [blame] | 641 | if (firstDownstreamForThisUpstream) maybeStartBpf(extIface); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 645 | int TetherController::setTetherGlobalAlertRule() { |
| 646 | // Only add this if we are the first enabled nat |
| 647 | if (isAnyForwardingPairEnabled()) { |
| 648 | return 0; |
| 649 | } |
| 650 | const std::string cmds = |
| 651 | "*filter\n" + |
| 652 | StringPrintf("-I %s -j %s\n", LOCAL_FORWARD, BandwidthController::LOCAL_GLOBAL_ALERT) + |
| 653 | "COMMIT\n"; |
| 654 | |
| 655 | return iptablesRestoreFunction(V4V6, cmds, nullptr); |
| 656 | } |
| 657 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 658 | int TetherController::setupIPv6CountersChain() { |
| 659 | // Only add this if we are the first enabled nat |
| 660 | if (isAnyForwardingPairEnabled()) { |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * IPv6 tethering doesn't need the state-based conntrack rules, so |
| 666 | * it unconditionally jumps to the tether counters chain all the time. |
| 667 | */ |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 668 | const std::string v6Cmds = |
| 669 | "*filter\n" + |
| 670 | 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] | 671 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 672 | return iptablesRestoreFunction(V6, v6Cmds, nullptr); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // Gets a pointer to the ForwardingDownstream for an interface pair in the map, or nullptr |
| 676 | TetherController::ForwardingDownstream* TetherController::findForwardingDownstream( |
| 677 | const std::string& intIface, const std::string& extIface) { |
| 678 | auto extIfaceMatches = mFwdIfaces.equal_range(extIface); |
| 679 | for (auto it = extIfaceMatches.first; it != extIfaceMatches.second; ++it) { |
| 680 | if (it->second.iface == intIface) { |
| 681 | return &(it->second); |
| 682 | } |
| 683 | } |
| 684 | return nullptr; |
| 685 | } |
| 686 | |
| 687 | void TetherController::addForwardingPair(const std::string& intIface, const std::string& extIface) { |
| 688 | ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface); |
| 689 | if (existingEntry != nullptr) { |
| 690 | existingEntry->active = true; |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | mFwdIfaces.insert(std::pair<std::string, ForwardingDownstream>(extIface, { |
| 695 | .iface = intIface, |
| 696 | .active = true |
| 697 | })); |
| 698 | } |
| 699 | |
| 700 | void TetherController::markForwardingPairDisabled( |
| 701 | const std::string& intIface, const std::string& extIface) { |
| 702 | ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface); |
| 703 | if (existingEntry == nullptr) { |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | existingEntry->active = false; |
| 708 | } |
| 709 | |
| 710 | bool TetherController::isForwardingPairEnabled( |
| 711 | const std::string& intIface, const std::string& extIface) { |
| 712 | ForwardingDownstream* existingEntry = findForwardingDownstream(intIface, extIface); |
| 713 | return existingEntry != nullptr && existingEntry->active; |
| 714 | } |
| 715 | |
| 716 | bool TetherController::isAnyForwardingEnabledOnUpstream(const std::string& extIface) { |
| 717 | auto extIfaceMatches = mFwdIfaces.equal_range(extIface); |
| 718 | for (auto it = extIfaceMatches.first; it != extIfaceMatches.second; ++it) { |
| 719 | if (it->second.active) { |
| 720 | return true; |
| 721 | } |
| 722 | } |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | bool TetherController::isAnyForwardingPairEnabled() { |
| 727 | for (auto& it : mFwdIfaces) { |
| 728 | if (it.second.active) { |
| 729 | return true; |
| 730 | } |
| 731 | } |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | bool TetherController::tetherCountingRuleExists( |
| 736 | const std::string& iface1, const std::string& iface2) { |
| 737 | // A counting rule exists if NAT was ever enabled for this interface pair, so if the pair |
| 738 | // is in the map regardless of its active status. Rules are added both ways so we check with |
| 739 | // the 2 combinations. |
| 740 | return findForwardingDownstream(iface1, iface2) != nullptr |
| 741 | || findForwardingDownstream(iface2, iface1) != nullptr; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* static */ |
| 745 | std::string TetherController::makeTetherCountingRule(const char *if1, const char *if2) { |
| 746 | return StringPrintf("-A %s -i %s -o %s -j RETURN", LOCAL_TETHER_COUNTERS_CHAIN, if1, if2); |
| 747 | } |
| 748 | |
| 749 | int TetherController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
| 750 | const char *op = add ? "-A" : "-D"; |
| 751 | |
| 752 | std::string rpfilterCmd = StringPrintf( |
| 753 | "*raw\n" |
| 754 | "%s %s -i %s -m rpfilter --invert ! -s fe80::/64 -j DROP\n" |
| 755 | "COMMIT\n", op, LOCAL_RAW_PREROUTING, intIface); |
| 756 | if (iptablesRestoreFunction(V6, rpfilterCmd, nullptr) == -1 && add) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 757 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | std::vector<std::string> v4 = { |
hiroaki.yokoyama | 04f2cb5 | 2018-06-13 18:47:30 +0900 | [diff] [blame] | 761 | "*raw", |
| 762 | StringPrintf("%s %s -p tcp --dport 21 -i %s -j CT --helper ftp", op, |
| 763 | LOCAL_RAW_PREROUTING, intIface), |
| 764 | StringPrintf("%s %s -p tcp --dport 1723 -i %s -j CT --helper pptp", op, |
| 765 | LOCAL_RAW_PREROUTING, intIface), |
| 766 | "COMMIT", |
| 767 | "*filter", |
| 768 | StringPrintf("%s %s -i %s -o %s -m state --state ESTABLISHED,RELATED -g %s", op, |
| 769 | LOCAL_FORWARD, extIface, intIface, LOCAL_TETHER_COUNTERS_CHAIN), |
| 770 | StringPrintf("%s %s -i %s -o %s -m state --state INVALID -j DROP", op, LOCAL_FORWARD, |
| 771 | intIface, extIface), |
| 772 | StringPrintf("%s %s -i %s -o %s -g %s", op, LOCAL_FORWARD, intIface, extIface, |
| 773 | LOCAL_TETHER_COUNTERS_CHAIN), |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 774 | }; |
| 775 | |
| 776 | std::vector<std::string> v6 = { |
| 777 | "*filter", |
| 778 | }; |
| 779 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 780 | // We only ever add tethering quota rules so that they stick. |
| 781 | if (add && !tetherCountingRuleExists(intIface, extIface)) { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 782 | v4.push_back(makeTetherCountingRule(intIface, extIface)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 783 | v4.push_back(makeTetherCountingRule(extIface, intIface)); |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 784 | v6.push_back(makeTetherCountingRule(intIface, extIface)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 785 | v6.push_back(makeTetherCountingRule(extIface, intIface)); |
| 786 | } |
| 787 | |
| 788 | // Always make sure the drop rule is at the end. |
| 789 | // 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] | 790 | // 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] | 791 | if (add) { |
| 792 | v4.push_back(StringPrintf("-D %s -j DROP", LOCAL_FORWARD)); |
| 793 | v4.push_back(StringPrintf("-A %s -j DROP", LOCAL_FORWARD)); |
| 794 | } |
| 795 | |
| 796 | v4.push_back("COMMIT\n"); |
| 797 | v6.push_back("COMMIT\n"); |
| 798 | |
| 799 | // We only add IPv6 rules here, never remove them. |
| 800 | if (iptablesRestoreFunction(V4, Join(v4, '\n'), nullptr) == -1 || |
| 801 | (add && iptablesRestoreFunction(V6, Join(v6, '\n'), nullptr) == -1)) { |
| 802 | // unwind what's been done, but don't care about success - what more could we do? |
| 803 | if (add) { |
| 804 | setForwardRules(false, intIface, extIface); |
| 805 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 806 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 807 | } |
| 808 | |
Remi NGUYEN VAN | 3b47c79 | 2018-03-20 14:44:12 +0900 | [diff] [blame] | 809 | if (add) { |
| 810 | addForwardingPair(intIface, extIface); |
| 811 | } else { |
| 812 | markForwardingPairDisabled(intIface, extIface); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | int TetherController::disableNat(const char* intIface, const char* extIface) { |
| 819 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 820 | errno = ENODEV; |
| 821 | return -errno; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | setForwardRules(false, intIface, extIface); |
Maciej Żenczykowski | 99665c3 | 2020-04-09 10:50:42 +0000 | [diff] [blame] | 825 | if (!isAnyForwardingEnabledOnUpstream(extIface)) maybeStopBpf(extIface); |
| 826 | if (!isAnyForwardingPairEnabled()) setDefaults(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 830 | namespace { |
| 831 | Result<void> validateOffloadRule(const TetherOffloadRuleParcel& rule) { |
| 832 | struct ethhdr hdr; |
| 833 | |
| 834 | if (rule.inputInterfaceIndex <= 0) { |
| 835 | return Error(ENODEV) << "Invalid input interface " << rule.inputInterfaceIndex; |
| 836 | } |
| 837 | if (rule.outputInterfaceIndex <= 0) { |
| 838 | return Error(ENODEV) << "Invalid output interface " << rule.inputInterfaceIndex; |
| 839 | } |
| 840 | if (rule.prefixLength != 128) { |
| 841 | return Error(EINVAL) << "Prefix length must be 128, not " << rule.prefixLength; |
| 842 | } |
| 843 | if (rule.destination.size() != sizeof(in6_addr)) { |
| 844 | return Error(EAFNOSUPPORT) << "Invalid IP address length " << rule.destination.size(); |
| 845 | } |
| 846 | if (rule.srcL2Address.size() != sizeof(hdr.h_source)) { |
| 847 | return Error(ENXIO) << "Invalid L2 src address length " << rule.srcL2Address.size(); |
| 848 | } |
| 849 | if (rule.dstL2Address.size() != sizeof(hdr.h_dest)) { |
| 850 | return Error(ENXIO) << "Invalid L2 dst address length " << rule.dstL2Address.size(); |
| 851 | } |
| 852 | return Result<void>(); |
| 853 | } |
| 854 | } // namespace |
| 855 | |
| 856 | Result<void> TetherController::addOffloadRule(const TetherOffloadRuleParcel& rule) { |
| 857 | Result<void> res = validateOffloadRule(rule); |
| 858 | if (!res.ok()) return res; |
| 859 | |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 860 | ethhdr hdr = { |
| 861 | .h_proto = htons(ETH_P_IPV6), |
| 862 | }; |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 863 | memcpy(&hdr.h_dest, rule.dstL2Address.data(), sizeof(hdr.h_dest)); |
| 864 | memcpy(&hdr.h_source, rule.srcL2Address.data(), sizeof(hdr.h_source)); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 865 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 866 | // Only downstream supported for now. |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 867 | TetherIngressKey key = { |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 868 | .iif = static_cast<uint32_t>(rule.inputInterfaceIndex), |
| 869 | .neigh6 = *(const in6_addr*)rule.destination.data(), |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | TetherIngressValue value = { |
Maciej Żenczykowski | 1ec2453 | 2020-05-23 22:07:00 +0000 | [diff] [blame] | 873 | .oif = static_cast<uint32_t>(rule.outputInterfaceIndex), |
| 874 | .macHeader = hdr, |
| 875 | .pmtu = 1500, // TODO: don't just blindly use this default |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 876 | }; |
| 877 | |
| 878 | return mBpfIngressMap.writeValue(key, value, BPF_ANY); |
| 879 | } |
| 880 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 881 | Result<void> TetherController::removeOffloadRule(const TetherOffloadRuleParcel& rule) { |
| 882 | Result<void> res = validateOffloadRule(rule); |
| 883 | if (!res.ok()) return res; |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 884 | |
| 885 | TetherIngressKey key = { |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 886 | .iif = static_cast<uint32_t>(rule.inputInterfaceIndex), |
| 887 | .neigh6 = *(const in6_addr*)rule.destination.data(), |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 888 | }; |
| 889 | |
| 890 | Result<void> ret = mBpfIngressMap.deleteValue(key); |
| 891 | |
| 892 | // Silently return success if the rule did not exist. |
| 893 | if (!ret.ok() && ret.error().code() == ENOENT) return {}; |
| 894 | |
| 895 | return ret; |
| 896 | } |
| 897 | |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 898 | void TetherController::addStats(TetherStatsList& statsList, const TetherStats& stats) { |
| 899 | for (TetherStats& existing : statsList) { |
| 900 | if (existing.addStatsIfMatch(stats)) { |
| 901 | return; |
| 902 | } |
| 903 | } |
| 904 | // No match. Insert a new interface pair. |
| 905 | statsList.push_back(stats); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Parse the ptks and bytes out of: |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 910 | * Chain tetherctrl_counters (4 references) |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 911 | * pkts bytes target prot opt in out source destination |
| 912 | * 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 913 | * 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0 |
| 914 | * 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 915 | * 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0 |
| 916 | * or: |
Lorenzo Colitti | 4604b4a | 2017-08-24 19:21:50 +0900 | [diff] [blame] | 917 | * Chain tetherctrl_counters (0 references) |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 918 | * pkts bytes target prot opt in out source destination |
| 919 | * 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0 |
| 920 | * 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0 |
| 921 | * |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 922 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 923 | int TetherController::addForwardChainStats(TetherStatsList& statsList, |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 924 | const std::string& statsOutput, |
| 925 | std::string &extraProcessingInfo) { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 926 | enum IndexOfIptChain { |
| 927 | ORIG_LINE, |
| 928 | PACKET_COUNTS, |
| 929 | BYTE_COUNTS, |
| 930 | HYPHEN, |
| 931 | IFACE0_NAME, |
| 932 | IFACE1_NAME, |
| 933 | SOURCE, |
| 934 | DESTINATION |
| 935 | }; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 936 | TetherStats stats; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 937 | const TetherStats empty; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 938 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 939 | static const std::string NUM = "(\\d+)"; |
| 940 | static const std::string IFACE = "([^\\s]+)"; |
| 941 | static const std::string DST = "(0.0.0.0/0|::/0)"; |
| 942 | static const std::string COUNTERS = "\\s*" + NUM + "\\s+" + NUM + |
| 943 | " RETURN all( -- | )" + IFACE + "\\s+" + IFACE + |
| 944 | "\\s+" + DST + "\\s+" + DST; |
| 945 | static const std::regex IP_RE(COUNTERS); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 946 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 947 | const std::vector<std::string> lines = base::Split(statsOutput, "\n"); |
| 948 | int headerLine = 0; |
| 949 | for (const std::string& line : lines) { |
| 950 | // Skip headers. |
| 951 | if (headerLine < 2) { |
| 952 | if (line.empty()) { |
| 953 | ALOGV("Empty header while parsing tethering stats"); |
| 954 | return -EREMOTEIO; |
| 955 | } |
| 956 | headerLine++; |
| 957 | continue; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 958 | } |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 959 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 960 | if (line.empty()) continue; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 961 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 962 | extraProcessingInfo = line; |
| 963 | std::smatch matches; |
| 964 | if (!std::regex_search(line, matches, IP_RE)) return -EREMOTEIO; |
| 965 | // Here use IP_RE to distiguish IPv4 and IPv6 iptables. |
| 966 | // IPv4 has "--" indicating what to do with fragments... |
| 967 | // 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0 |
| 968 | // ... but IPv6 does not. |
| 969 | // 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0 |
| 970 | // TODO: Replace strtoXX() calls with ParseUint() /ParseInt() |
| 971 | int64_t packets = strtoul(matches[PACKET_COUNTS].str().c_str(), nullptr, 10); |
| 972 | int64_t bytes = strtoul(matches[BYTE_COUNTS].str().c_str(), nullptr, 10); |
| 973 | std::string iface0 = matches[IFACE0_NAME].str(); |
| 974 | std::string iface1 = matches[IFACE1_NAME].str(); |
| 975 | std::string rest = matches[SOURCE].str(); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 976 | |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 977 | ALOGV("parse iface0=<%s> iface1=<%s> pkts=%" PRId64 " bytes=%" PRId64 |
| 978 | " rest=<%s> orig line=<%s>", |
| 979 | 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] | 980 | /* |
| 981 | * The following assumes that the 1st rule has in:extIface out:intIface, |
| 982 | * which is what TetherController sets up. |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 983 | * 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] | 984 | */ |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 985 | if (!stats.intIface[0]) { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 986 | ALOGV("0Filter RX iface_in=%s iface_out=%s rx_bytes=%" PRId64 " rx_packets=%" PRId64 |
| 987 | " ", iface0.c_str(), iface1.c_str(), bytes, packets); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 988 | stats.intIface = iface0; |
| 989 | stats.extIface = iface1; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 990 | stats.txPackets = packets; |
| 991 | stats.txBytes = bytes; |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 992 | } else if (stats.intIface == iface1 && stats.extIface == iface0) { |
waynema | 71a0b59 | 2018-11-21 13:31:34 +0800 | [diff] [blame] | 993 | ALOGV("0Filter TX iface_in=%s iface_out=%s rx_bytes=%" PRId64 " rx_packets=%" PRId64 |
| 994 | " ", iface0.c_str(), iface1.c_str(), bytes, packets); |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 995 | stats.rxPackets = packets; |
| 996 | stats.rxBytes = bytes; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 997 | } |
| 998 | if (stats.rxBytes != -1 && stats.txBytes != -1) { |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 999 | ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64, stats.rxBytes, stats.txBytes); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1000 | addStats(statsList, stats); |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 1001 | stats = empty; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* 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] | 1006 | if (((stats.rxBytes == -1) != (stats.txBytes == -1))) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1007 | return -EREMOTEIO; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1008 | } |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 1012 | StatusOr<TetherController::TetherStatsList> TetherController::getTetherStats() { |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1013 | TetherStatsList statsList; |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 1014 | std::string parsedIptablesOutput; |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1015 | |
| 1016 | for (const IptablesTarget target : {V4, V6}) { |
| 1017 | std::string statsString; |
Lorenzo Colitti | 0935339 | 2017-08-24 14:20:32 +0900 | [diff] [blame] | 1018 | if (int ret = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString)) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1019 | return statusFromErrno(-ret, StringPrintf("failed to fetch tether stats (%d): %d", |
| 1020 | target, ret)); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1021 | } |
| 1022 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 1023 | if (int ret = addForwardChainStats(statsList, statsString, parsedIptablesOutput)) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1024 | return statusFromErrno(-ret, StringPrintf("failed to parse %s tether stats:\n%s", |
| 1025 | target == V4 ? "IPv4": "IPv6", |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 1026 | parsedIptablesOutput.c_str())); |
Lorenzo Colitti | a93126d | 2017-08-24 13:28:19 +0900 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Hungming Chen | 68dade3 | 2020-02-20 13:50:56 +0800 | [diff] [blame] | 1030 | if (!mBpfStatsMap.isValid()) { |
| 1031 | return statsList; |
| 1032 | } |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 1033 | |
Hungming Chen | 68dade3 | 2020-02-20 13:50:56 +0800 | [diff] [blame] | 1034 | const auto processTetherStats = [this, &statsList](const uint32_t& key, |
| 1035 | const TetherStatsValue& value, |
| 1036 | const BpfMap<uint32_t, TetherStatsValue>&) { |
| 1037 | auto ifname = mIfaceIndexNameMap.readValue(key); |
| 1038 | if (!ifname.ok()) { |
| 1039 | // Keep on going regardless to parse as much as possible. |
| 1040 | return Result<void>(); |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 1041 | } |
Hungming Chen | 68dade3 | 2020-02-20 13:50:56 +0800 | [diff] [blame] | 1042 | // Because the same interface name can have different interface IDs over time, there might |
| 1043 | // already be a TetherStats in the list with this interface name. This is fine because |
| 1044 | // addStats will increment an existing TetherStats if there is one in the list already, |
| 1045 | // and add a new TetherStats to the list if there isn't. |
| 1046 | addStats(statsList, |
| 1047 | {kBpfOffloadInterface, ifname.value().name, static_cast<int64_t>(value.rxBytes), |
| 1048 | static_cast<int64_t>(value.rxPackets), static_cast<int64_t>(value.txBytes), |
| 1049 | static_cast<int64_t>(value.txPackets)}); |
| 1050 | return Result<void>(); |
| 1051 | }; |
| 1052 | |
| 1053 | auto ret = mBpfStatsMap.iterateWithValue(processTetherStats); |
| 1054 | if (!ret.ok()) { |
| 1055 | // Ignore error to return the non-BPF tether stats result. |
| 1056 | ALOGE("Error processing tether stats from BPF maps: %s", ret.error().message().c_str()); |
Hungming Chen | c2e95fb | 2020-02-19 17:41:30 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1059 | return statsList; |
| 1060 | } |
| 1061 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 1062 | // Use UINT64_MAX (~0uLL) for unlimited. |
| 1063 | Result<void> TetherController::setBpfLimit(uint32_t ifIndex, uint64_t limit) { |
| 1064 | // The common case is an update, where the stats already exist, |
| 1065 | // hence we read first, even though writing with BPF_NOEXIST |
| 1066 | // first would make the code simpler. |
| 1067 | uint64_t rxBytes, txBytes; |
| 1068 | auto statsEntry = mBpfStatsMap.readValue(ifIndex); |
| 1069 | |
| 1070 | if (statsEntry.ok()) { |
| 1071 | // Ok, there was a stats entry. |
| 1072 | rxBytes = statsEntry.value().rxBytes; |
| 1073 | txBytes = statsEntry.value().txBytes; |
| 1074 | } else if (statsEntry.error().code() == ENOENT) { |
| 1075 | // No stats entry - create one with zeroes. |
| 1076 | TetherStatsValue stats = {}; |
| 1077 | // This function is the *only* thing that can create entries. |
| 1078 | auto ret = mBpfStatsMap.writeValue(ifIndex, stats, BPF_NOEXIST); |
| 1079 | if (!ret.ok()) { |
| 1080 | ALOGE("mBpfStatsMap.writeValue failure: %s", strerror(ret.error().code())); |
| 1081 | return ret; |
| 1082 | } |
| 1083 | rxBytes = 0; |
| 1084 | txBytes = 0; |
| 1085 | } else { |
| 1086 | // Other error while trying to get stats entry. |
| 1087 | return statsEntry.error(); |
| 1088 | } |
| 1089 | |
| 1090 | // rxBytes + txBytes won't overflow even at 5gbps for ~936 years. |
| 1091 | uint64_t newLimit = rxBytes + txBytes + limit; |
| 1092 | |
| 1093 | // if adding limit (e.g., if limit is UINT64_MAX) caused overflow: clamp to 'infinity' |
| 1094 | if (newLimit < rxBytes + txBytes) newLimit = ~0uLL; |
| 1095 | |
| 1096 | auto ret = mBpfLimitMap.writeValue(ifIndex, newLimit, BPF_ANY); |
| 1097 | if (!ret.ok()) { |
| 1098 | ALOGE("mBpfLimitMap.writeValue failure: %s", strerror(ret.error().code())); |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | return {}; |
| 1103 | } |
| 1104 | |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 1105 | void TetherController::maybeStartBpf(const char* extIface) { |
Maciej Żenczykowski | aef1301 | 2020-02-18 17:13:09 -0800 | [diff] [blame] | 1106 | if (!bpf::isBpfSupported()) return; |
| 1107 | |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 1108 | // TODO: perhaps ignore IPv4-only interface because IPv4 traffic downstream is not supported. |
| 1109 | int ifIndex = if_nametoindex(extIface); |
| 1110 | if (!ifIndex) { |
| 1111 | ALOGE("Fail to get index for interface %s", extIface); |
| 1112 | return; |
| 1113 | } |
| 1114 | |
| 1115 | auto isEthernet = android::net::isEthernet(extIface); |
| 1116 | if (!isEthernet.ok()) { |
| 1117 | ALOGE("isEthernet(%s[%d]) failure: %s", extIface, ifIndex, |
| 1118 | isEthernet.error().message().c_str()); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | int rv = getTetherIngressProgFd(isEthernet.value()); |
| 1123 | if (rv < 0) { |
| 1124 | ALOGE("getTetherIngressProgFd(%d) failure: %s", isEthernet.value(), strerror(-rv)); |
| 1125 | return; |
| 1126 | } |
| 1127 | unique_fd tetherProgFd(rv); |
| 1128 | |
| 1129 | rv = tcFilterAddDevIngressTether(ifIndex, tetherProgFd, isEthernet.value()); |
| 1130 | if (rv) { |
| 1131 | ALOGE("tcFilterAddDevIngressTether(%d[%s], %d) failure: %s", ifIndex, extIface, |
| 1132 | isEthernet.value(), strerror(-rv)); |
| 1133 | return; |
| 1134 | } |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 1135 | |
| 1136 | // For now we just set data limit to 'infinity' ie. unlimited. |
| 1137 | setBpfLimit(ifIndex, ~0uLL); |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | void TetherController::maybeStopBpf(const char* extIface) { |
Maciej Żenczykowski | aef1301 | 2020-02-18 17:13:09 -0800 | [diff] [blame] | 1141 | if (!bpf::isBpfSupported()) return; |
| 1142 | |
Hungming Chen | 1da9008 | 2020-02-14 20:24:16 +0800 | [diff] [blame] | 1143 | // TODO: perhaps ignore IPv4-only interface because IPv4 traffic downstream is not supported. |
| 1144 | int ifIndex = if_nametoindex(extIface); |
| 1145 | if (!ifIndex) { |
| 1146 | ALOGE("Fail to get index for interface %s", extIface); |
| 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | int rv = tcFilterDelDevIngressTether(ifIndex); |
| 1151 | if (rv < 0) { |
| 1152 | ALOGE("tcFilterDelDevIngressTether(%d[%s]) failure: %s", ifIndex, extIface, strerror(-rv)); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 1156 | void TetherController::dumpIfaces(DumpWriter& dw) { |
| 1157 | dw.println("Interface pairs:"); |
| 1158 | |
| 1159 | ScopedIndent ifaceIndent(dw); |
| 1160 | for (const auto& it : mFwdIfaces) { |
| 1161 | dw.println("%s -> %s %s", it.first.c_str(), it.second.iface.c_str(), |
| 1162 | (it.second.active ? "ACTIVE" : "DISABLED")); |
| 1163 | } |
| 1164 | } |
| 1165 | |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1166 | namespace { |
| 1167 | |
| 1168 | std::string l2ToString(const uint8_t* addr, size_t len) { |
| 1169 | std::string str; |
| 1170 | |
| 1171 | if (len == 0) return str; |
| 1172 | |
| 1173 | StringAppendF(&str, "%02x", addr[0]); |
| 1174 | for (size_t i = 1; i < len; i++) { |
| 1175 | StringAppendF(&str, ":%02x", addr[i]); |
| 1176 | } |
| 1177 | |
| 1178 | return str; |
| 1179 | } |
| 1180 | |
| 1181 | } // namespace |
| 1182 | |
| 1183 | void TetherController::dumpBpf(DumpWriter& dw) { |
Maciej Żenczykowski | d25a734 | 2020-05-04 18:18:38 -0700 | [diff] [blame] | 1184 | if (!mBpfIngressMap.isValid() || !mBpfStatsMap.isValid() || !mBpfLimitMap.isValid()) { |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1185 | dw.println("BPF not supported"); |
| 1186 | return; |
| 1187 | } |
| 1188 | |
Maciej Żenczykowski | 1ec2453 | 2020-05-23 22:07:00 +0000 | [diff] [blame] | 1189 | dw.println("BPF ingress map: iif v6addr -> oif srcmac dstmac ethertype [pmtu]"); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1190 | const auto printIngressMap = [&dw](const TetherIngressKey& key, const TetherIngressValue& value, |
| 1191 | const BpfMap<TetherIngressKey, TetherIngressValue>&) { |
| 1192 | char addr[INET6_ADDRSTRLEN]; |
| 1193 | std::string src = l2ToString(value.macHeader.h_source, sizeof(value.macHeader.h_source)); |
| 1194 | std::string dst = l2ToString(value.macHeader.h_dest, sizeof(value.macHeader.h_dest)); |
| 1195 | inet_ntop(AF_INET6, &key.neigh6, addr, sizeof(addr)); |
| 1196 | |
Maciej Żenczykowski | 1ec2453 | 2020-05-23 22:07:00 +0000 | [diff] [blame] | 1197 | dw.println("%u %s -> %u %s %s %04x [%u]", key.iif, addr, value.oif, src.c_str(), |
| 1198 | dst.c_str(), ntohs(value.macHeader.h_proto), value.pmtu); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1199 | |
| 1200 | return Result<void>(); |
| 1201 | }; |
| 1202 | |
| 1203 | dw.incIndent(); |
| 1204 | auto ret = mBpfIngressMap.iterateWithValue(printIngressMap); |
| 1205 | if (!ret.ok()) { |
| 1206 | dw.println("Error printing BPF ingress map: %s", ret.error().message().c_str()); |
| 1207 | } |
| 1208 | dw.decIndent(); |
| 1209 | |
| 1210 | dw.println("BPF stats (downlink): iif -> packets bytes errors"); |
| 1211 | const auto printStatsMap = [&dw](const uint32_t& key, const TetherStatsValue& value, |
| 1212 | const BpfMap<uint32_t, TetherStatsValue>&) { |
Treehugger Robot | 15b21b7 | 2020-05-13 09:24:07 +0000 | [diff] [blame] | 1213 | dw.println("%u -> %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxPackets, value.rxBytes, |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1214 | value.rxErrors); |
| 1215 | |
| 1216 | return Result<void>(); |
| 1217 | }; |
| 1218 | |
| 1219 | dw.incIndent(); |
| 1220 | ret = mBpfStatsMap.iterateWithValue(printStatsMap); |
| 1221 | if (!ret.ok()) { |
| 1222 | dw.println("Error printing BPF stats map: %s", ret.error().message().c_str()); |
| 1223 | } |
| 1224 | dw.decIndent(); |
Maciej Żenczykowski | d25a734 | 2020-05-04 18:18:38 -0700 | [diff] [blame] | 1225 | |
| 1226 | dw.println("BPF limit: iif -> bytes"); |
| 1227 | const auto printLimitMap = [&dw](const uint32_t& key, const uint64_t& value, |
| 1228 | const BpfMap<uint32_t, uint64_t>&) { |
| 1229 | dw.println("%u -> %" PRIu64, key, value); |
| 1230 | |
| 1231 | return Result<void>(); |
| 1232 | }; |
| 1233 | |
| 1234 | dw.incIndent(); |
| 1235 | ret = mBpfLimitMap.iterateWithValue(printLimitMap); |
| 1236 | if (!ret.ok()) { |
| 1237 | dw.println("Error printing BPF limit map: %s", ret.error().message().c_str()); |
| 1238 | } |
| 1239 | dw.decIndent(); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1240 | } |
| 1241 | |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 1242 | void TetherController::dump(DumpWriter& dw) { |
| 1243 | std::lock_guard guard(lock); |
| 1244 | |
| 1245 | ScopedIndent tetherControllerIndent(dw); |
| 1246 | dw.println("TetherController"); |
| 1247 | dw.incIndent(); |
| 1248 | |
| 1249 | dw.println("Forwarding requests: " + Join(mForwardingRequests, ' ')); |
| 1250 | if (mDnsNetId != 0) { |
| 1251 | dw.println(StringPrintf("DNS: netId %d servers [%s]", mDnsNetId, |
| 1252 | Join(mDnsForwarders, ", ").c_str())); |
| 1253 | } |
| 1254 | if (mDaemonPid != 0) { |
| 1255 | dw.println("dnsmasq PID: %d", mDaemonPid); |
| 1256 | } |
| 1257 | |
| 1258 | dumpIfaces(dw); |
Lorenzo Colitti | e801d3c | 2020-02-18 00:00:35 +0900 | [diff] [blame] | 1259 | dw.println(""); |
| 1260 | dumpBpf(dw); |
Lorenzo Colitti | 52db391 | 2020-02-17 23:59:45 +0900 | [diff] [blame] | 1261 | } |
| 1262 | |
Lorenzo Colitti | e20a526 | 2017-05-09 18:30:44 +0900 | [diff] [blame] | 1263 | } // namespace net |
| 1264 | } // namespace android |