blob: 50ae9661d923107230da9531fb7c7670ffc4dc3b [file] [log] [blame]
San Mehat9d10b342010-01-18 09:51:02 -08001/*
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 Mehat9d10b342010-01-18 09:51:02 -080017#include <errno.h>
San Mehat18737842010-01-21 09:22:43 -080018#include <fcntl.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090019#include <inttypes.h>
Lorenzo Colittic2841282015-11-25 22:13:57 +090020#include <netdb.h>
Luke Huang40962442019-02-26 11:46:10 +080021#include <spawn.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080022#include <string.h>
San Mehat18737842010-01-21 09:22:43 -080023
San Mehat9d10b342010-01-18 09:51:02 -080024#include <sys/socket.h>
25#include <sys/stat.h>
San Mehat18737842010-01-21 09:22:43 -080026#include <sys/types.h>
27#include <sys/wait.h>
28
San Mehat9d10b342010-01-18 09:51:02 -080029#include <netinet/in.h>
30#include <arpa/inet.h>
31
Erik Kline5f0358b2018-02-16 15:08:09 +090032#include <array>
33#include <cstdlib>
waynema71a0b592018-11-21 13:31:34 +080034#include <regex>
Erik Kline5f0358b2018-02-16 15:08:09 +090035#include <string>
36#include <vector>
37
San Mehat9d10b342010-01-18 09:51:02 -080038#define LOG_TAG "TetherController"
Maciej Żenczykowski2cffd942019-05-05 13:40:35 -070039#include <android-base/scopeguard.h>
Lorenzo Colittia93126d2017-08-24 13:28:19 +090040#include <android-base/stringprintf.h>
George Burgess IVe3dc1312019-02-28 11:27:04 -080041#include <android-base/strings.h>
42#include <android-base/unique_fd.h>
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050043#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080044#include <log/log.h>
Hungming Chen1da90082020-02-14 20:24:16 +080045#include <net/if.h>
Lorenzo Colitti52db3912020-02-17 23:59:45 +090046#include <netdutils/DumpWriter.h>
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090047#include <netdutils/StatusOr.h>
San Mehat9d10b342010-01-18 09:51:02 -080048
Erik Klineb31fd692018-06-06 20:50:11 +090049#include "Controllers.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070050#include "Fwmark.h"
Erik Kline1d065ba2016-06-08 13:24:45 +090051#include "InterfaceController.h"
Mike Yuf0e019f2019-03-13 14:43:39 +080052#include "NetdConstants.h"
Lorenzo Colittie20a5262017-05-09 18:30:44 +090053#include "NetworkController.h"
Hungming Chen1da90082020-02-14 20:24:16 +080054#include "OffloadUtils.h"
Mike Yuf0e019f2019-03-13 14:43:39 +080055#include "Permission.h"
San Mehat9d10b342010-01-18 09:51:02 -080056#include "TetherController.h"
57
Lorenzo Colittif0e051c2020-04-06 09:19:57 +000058#include "android/net/TetherOffloadRuleParcel.h"
59
Bernie Innocenti4debf3b2018-09-12 13:54:50 +090060namespace android {
61namespace net {
62
Lorenzo Colittie801d3c2020-02-18 00:00:35 +090063using android::base::Error;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090064using android::base::Join;
George Burgess IVe3dc1312019-02-28 11:27:04 -080065using android::base::Pipe;
Lorenzo Colittie801d3c2020-02-18 00:00:35 +090066using android::base::Result;
67using android::base::StringAppendF;
Erik Klineb31fd692018-06-06 20:50:11 +090068using android::base::StringPrintf;
George Burgess IVe3dc1312019-02-28 11:27:04 -080069using android::base::unique_fd;
Lorenzo Colittif0e051c2020-04-06 09:19:57 +000070using android::net::TetherOffloadRuleParcel;
Lorenzo Colitti52db3912020-02-17 23:59:45 +090071using android::netdutils::DumpWriter;
72using android::netdutils::ScopedIndent;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090073using android::netdutils::statusFromErrno;
Erik Klineb31fd692018-06-06 20:50:11 +090074using android::netdutils::StatusOr;
Lorenzo Colittia93126d2017-08-24 13:28:19 +090075
Lorenzo Colitti799625c2015-02-25 12:52:00 +090076namespace {
77
Erik Kline1d065ba2016-06-08 13:24:45 +090078const char BP_TOOLS_MODE[] = "bp-tools";
79const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
80const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
81const char SEPARATOR[] = "|";
Erik Kline93f9b222017-10-22 21:24:58 +090082constexpr const char kTcpBeLiberal[] = "/proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal";
Hungming Chen68dade32020-02-20 13:50:56 +080083
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 Chenc2e95fb2020-02-19 17:41:30 +080086constexpr const char kBpfOffloadInterface[] = "BPFOffloadInterface";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090087
Erik Kline5f0358b2018-02-16 15:08:09 +090088// Chosen to match AID_DNS_TETHER, as made "friendly" by fs_config_generator.py.
89constexpr const char kDnsmasqUsername[] = "dns_tether";
90
Lorenzo Colitti799625c2015-02-25 12:52:00 +090091bool writeToFile(const char* filename, const char* value) {
Nick Kralevichb95c60c2016-11-19 09:09:16 -080092 int fd = open(filename, O_WRONLY | O_CLOEXEC);
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000093 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 Colitti799625c2015-02-25 12:52:00 +0900106}
107
Erik Kline93f9b222017-10-22 21:24:58 +0900108// TODO: Consider altering TCP and UDP timeouts as well.
109void configureForTethering(bool enabled) {
110 writeToFile(kTcpBeLiberal, enabled ? "1" : "0");
111}
112
Erik Kline1d065ba2016-06-08 13:24:45 +0900113bool configureForIPv6Router(const char *interface) {
114 return (InterfaceController::setEnableIPv6(interface, 0) == 0)
115 && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0)
Erik Kline6cddf512016-08-09 15:28:42 +0900116 && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0)
117 && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0)
Erik Kline1d065ba2016-06-08 13:24:45 +0900118 && (InterfaceController::setEnableIPv6(interface, 1) == 0);
119}
120
121void configureForIPv6Client(const char *interface) {
122 InterfaceController::setAcceptIPv6Ra(interface, 1);
Erik Kline6cddf512016-08-09 15:28:42 +0900123 InterfaceController::setAcceptIPv6Dad(interface, 1);
124 InterfaceController::setIPv6DadTransmits(interface, "1");
Erik Kline1d065ba2016-06-08 13:24:45 +0900125 InterfaceController::setEnableIPv6(interface, 0);
126}
127
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900128bool 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 Colittia93126d2017-08-24 13:28:19 +0900137auto TetherController::iptablesRestoreFunction = execIptablesRestoreWithOutput;
138
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900139const 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 Kline15079dd2018-05-18 23:10:56 +0900144int TetherController::DnsmasqState::sendCmd(int daemonFd, const std::string& cmd) {
145 if (cmd.empty()) return 0;
146
Erik Klineb31fd692018-06-06 20:50:11 +0900147 gLog.log("Sending update msg to dnsmasq [%s]", cmd.c_str());
Erik Kline15079dd2018-05-18 23:10:56 +0900148 // Send the trailing \0 as well.
149 if (write(daemonFd, cmd.c_str(), cmd.size() + 1) < 0) {
Erik Klineb31fd692018-06-06 20:50:11 +0900150 gLog.error("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline15079dd2018-05-18 23:10:56 +0900151 errno = EREMOTEIO;
152 return -1;
153 }
154 return 0;
155}
156
157void TetherController::DnsmasqState::clear() {
158 update_ifaces_cmd.clear();
159 update_dns_cmd.clear();
160}
161
162int TetherController::DnsmasqState::sendAllState(int daemonFd) const {
163 return sendCmd(daemonFd, update_ifaces_cmd) | sendCmd(daemonFd, update_dns_cmd);
164}
165
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700166TetherController::TetherController() {
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900167 if (inBpToolsMode()) {
168 enableForwarding(BP_TOOLS_MODE);
169 } else {
170 setIpFwdEnabled();
171 }
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800172 maybeInitMaps();
San Mehat9d10b342010-01-18 09:51:02 -0800173}
174
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900175bool TetherController::setIpFwdEnabled() {
176 bool success = true;
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900177 bool disable = mForwardingRequests.empty();
178 const char* value = disable ? "0" : "1";
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900179 ALOGD("Setting IP forward enable = %s", value);
180 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
181 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900182 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 Colitti799625c2015-02-25 12:52:00 +0900189 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800190}
191
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900192bool 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 Mehat9d10b342010-01-18 09:51:02 -0800199
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900200bool TetherController::disableForwarding(const char* requester) {
201 mForwardingRequests.erase(requester);
202 return setIpFwdEnabled();
203}
San Mehat9d10b342010-01-18 09:51:02 -0800204
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800205void 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 Żenczykowski109ac532020-02-19 10:36:44 -0800215 if (fd >= 0) {
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800216 mBpfIngressMap.reset(fd);
Maciej Żenczykowski109ac532020-02-19 10:36:44 -0800217 mBpfIngressMap.clear();
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800218 }
219 fd = getTetherStatsMapFd();
Maciej Żenczykowski109ac532020-02-19 10:36:44 -0800220 if (fd >= 0) {
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800221 mBpfStatsMap.reset(fd);
Maciej Żenczykowski109ac532020-02-19 10:36:44 -0800222 mBpfStatsMap.clear();
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800223 }
Maciej Żenczykowskid25a7342020-05-04 18:18:38 -0700224 fd = getTetherLimitMapFd();
225 if (fd >= 0) {
226 mBpfLimitMap.reset(fd);
227 mBpfLimitMap.clear();
228 }
Hungming Chenc2e95fb2020-02-19 17:41:30 +0800229}
230
Luke Huang728cf4c2019-03-14 19:43:02 +0800231const std::set<std::string>& TetherController::getIpfwdRequesterList() const {
232 return mForwardingRequests;
San Mehat9d10b342010-01-18 09:51:02 -0800233}
234
Luke Huang91bd3e12019-08-20 11:33:52 +0800235int 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 Block5ea0c052012-01-06 19:18:11 +0000244 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800245 errno = EBUSY;
Luke Huangb5733d72018-08-21 17:17:19 +0800246 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800247 }
248
Steve Block7b984e32011-12-20 16:22:42 +0000249 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800250
George Burgess IVe3dc1312019-02-28 11:27:04 -0800251 unique_fd pipeRead, pipeWrite;
252 if (!Pipe(&pipeRead, &pipeWrite, O_CLOEXEC)) {
Luke Huangb5733d72018-08-21 17:17:19 +0800253 int res = errno;
Luke Huang94c43a12019-02-14 19:51:38 +0800254 ALOGE("pipe2() failed (%s)", strerror(errno));
Luke Huangb5733d72018-08-21 17:17:19 +0800255 return -res;
San Mehat9d10b342010-01-18 09:51:02 -0800256 }
257
Luke Huang94c43a12019-02-14 19:51:38 +0800258 // 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 Mehat9d10b342010-01-18 09:51:02 -0800266
Luke Huang94c43a12019-02-14 19:51:38 +0800267 std::vector<const std::string> argVector = {
Erik Kline5f0358b2018-02-16 15:08:09 +0900268 "/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 Huang94c43a12019-02-14 19:51:38 +0800276 "--listen-mark",
277 markStr,
278 "--user",
279 kDnsmasqUsername,
280 };
San Mehat9d10b342010-01-18 09:51:02 -0800281
Luke Huang91bd3e12019-08-20 11:33:52 +0800282 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 Huang94c43a12019-02-14 19:51:38 +0800287 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 Mehat9d10b342010-01-18 09:51:02 -0800290 }
291
George Burgess IVe3dc1312019-02-28 11:27:04 -0800292 std::vector<char*> args(argVector.size() + 1);
Luke Huang94c43a12019-02-14 19:51:38 +0800293 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 Huang40962442019-02-26 11:46:10 +0800301
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 Żenczykowski2cffd942019-05-05 13:40:35 -0700307 int res = posix_spawn_file_actions_init(&fa);
Luke Huang40962442019-02-26 11:46:10 +0800308 if (res) {
Maciej Żenczykowski2cffd942019-05-05 13:40:35 -0700309 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 Huang94c43a12019-02-14 19:51:38 +0800316 return -res;
317 }
318
Luke Huang40962442019-02-26 11:46:10 +0800319 posix_spawnattr_t attr;
Maciej Żenczykowski2cffd942019-05-05 13:40:35 -0700320 res = posix_spawnattr_init(&attr);
Luke Huang40962442019-02-26 11:46:10 +0800321 if (res) {
Maciej Żenczykowski2cffd942019-05-05 13:40:35 -0700322 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 Huang40962442019-02-26 11:46:10 +0800329 return -res;
330 }
331
332 pid_t pid;
George Burgess IVe3dc1312019-02-28 11:27:04 -0800333 res = posix_spawn(&pid, args[0], &fa, &attr, &args[0], nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800334 if (res) {
335 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang40962442019-02-26 11:46:10 +0800336 return -res;
337 }
Luke Huang94c43a12019-02-14 19:51:38 +0800338 mDaemonPid = pid;
George Burgess IVe3dc1312019-02-28 11:27:04 -0800339 mDaemonFd = pipeWrite.release();
Luke Huang94c43a12019-02-14 19:51:38 +0800340 configureForTethering(true);
Luke Huang91bd3e12019-08-20 11:33:52 +0800341 mIsTetheringStarted = true;
Luke Huang94c43a12019-02-14 19:51:38 +0800342 applyDnsInterfaces();
343 ALOGD("Tethering services running");
344
San Mehat9d10b342010-01-18 09:51:02 -0800345 return 0;
346}
347
Luke Huangb5733d72018-08-21 17:17:19 +0800348std::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 Huang91bd3e12019-08-20 11:33:52 +0800357int TetherController::startTethering(bool usingLegacyDnsProxy,
358 const std::vector<std::string>& dhcpRanges) {
Luke Huangb5733d72018-08-21 17:17:19 +0800359 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 Huang91bd3e12019-08-20 11:33:52 +0800366 return startTethering(usingLegacyDnsProxy, dhcp_ranges.size(), dhcp_ranges.data());
Luke Huangb5733d72018-08-21 17:17:19 +0800367}
368
San Mehat9d10b342010-01-18 09:51:02 -0800369int TetherController::stopTethering() {
Erik Kline93f9b222017-10-22 21:24:58 +0900370 configureForTethering(false);
San Mehat9d10b342010-01-18 09:51:02 -0800371
Luke Huang91bd3e12019-08-20 11:33:52 +0800372 if (!mIsTetheringStarted) {
Steve Block5ea0c052012-01-06 19:18:11 +0000373 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800374 return 0;
375 }
376
Luke Huang91bd3e12019-08-20 11:33:52 +0800377 mIsTetheringStarted = false;
378 // dnsmasq is not started
379 if (mDaemonPid == 0) {
380 return 0;
381 }
382
Steve Block7b984e32011-12-20 16:22:42 +0000383 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800384
385 kill(mDaemonPid, SIGTERM);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700386 waitpid(mDaemonPid, nullptr, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800387 mDaemonPid = 0;
388 close(mDaemonFd);
389 mDaemonFd = -1;
Erik Kline15079dd2018-05-18 23:10:56 +0900390 mDnsmasqState.clear();
Steve Block7b984e32011-12-20 16:22:42 +0000391 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800392 return 0;
393}
Matthew Xie19944102012-07-12 16:42:07 -0700394
San Mehat9d10b342010-01-18 09:51:02 -0800395bool TetherController::isTetheringStarted() {
Luke Huang91bd3e12019-08-20 11:33:52 +0800396 return mIsTetheringStarted;
San Mehat9d10b342010-01-18 09:51:02 -0800397}
398
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900399// 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 Innocenti45238a12018-12-04 14:57:48 +0900402const size_t MAX_CMD_SIZE = 1023;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800403
Luke Huang8dc1cac2019-03-30 16:12:31 +0800404// TODO: Remove overload function and update this after NDC migration.
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700405int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700406 Fwmark fwmark;
407 fwmark.netId = netId;
408 fwmark.explicitlySelected = true;
409 fwmark.protectedFromVpn = true;
410 fwmark.permission = PERMISSION_SYSTEM;
411
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900412 std::string daemonCmd = StringPrintf("update_dns%s0x%x", SEPARATOR, fwmark.intValue);
San Mehat9d10b342010-01-18 09:51:02 -0800413
Erik Kline1d065ba2016-06-08 13:24:45 +0900414 mDnsForwarders.clear();
Bernie Innocenti45238a12018-12-04 14:57:48 +0900415 for (int i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700416 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800417
Lorenzo Colittic2841282015-11-25 22:13:57 +0900418 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
Yi Kongbdfd57e2018-07-25 13:26:10 -0700419 int ret = getaddrinfo(servers[i], nullptr, &hints, &res);
Lorenzo Colittic2841282015-11-25 22:13:57 +0900420 freeaddrinfo(res);
421 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000422 ALOGE("Failed to parse DNS server '%s'", servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900423 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900424 errno = EINVAL;
Luke Huangb5733d72018-08-21 17:17:19 +0800425 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800426 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800427
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900428 if (daemonCmd.size() + 1 + strlen(servers[i]) >= MAX_CMD_SIZE) {
429 ALOGE("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800430 break;
431 }
432
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900433 daemonCmd += SEPARATOR;
434 daemonCmd += servers[i];
Erik Kline1d065ba2016-06-08 13:24:45 +0900435 mDnsForwarders.push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800436 }
437
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700438 mDnsNetId = netId;
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900439 mDnsmasqState.update_dns_cmd = std::move(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800440 if (mDaemonFd != -1) {
Erik Kline15079dd2018-05-18 23:10:56 +0900441 if (mDnsmasqState.sendAllState(mDaemonFd) != 0) {
Erik Kline1d065ba2016-06-08 13:24:45 +0900442 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900443 errno = EREMOTEIO;
Luke Huangb5733d72018-08-21 17:17:19 +0800444 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800445 }
446 }
447 return 0;
448}
449
Luke Huangb5733d72018-08-21 17:17:19 +0800450int TetherController::setDnsForwarders(unsigned netId, const std::vector<std::string>& servers) {
Luke Huangb5733d72018-08-21 17:17:19 +0800451 auto dnsServers = toCstrVec(servers);
452 return setDnsForwarders(netId, dnsServers.data(), dnsServers.size());
453}
454
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700455unsigned TetherController::getDnsNetId() {
456 return mDnsNetId;
457}
458
Erik Kline1d065ba2016-06-08 13:24:45 +0900459const std::list<std::string> &TetherController::getDnsForwarders() const {
San Mehat9d10b342010-01-18 09:51:02 -0800460 return mDnsForwarders;
461}
462
Erik Kline1d065ba2016-06-08 13:24:45 +0900463bool TetherController::applyDnsInterfaces() {
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900464 std::string daemonCmd = "update_ifaces";
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800465 bool haveInterfaces = false;
466
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900467 for (const auto& ifname : mInterfaces) {
468 if (daemonCmd.size() + 1 + ifname.size() >= MAX_CMD_SIZE) {
469 ALOGE("Too many DNS servers listed");
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800470 break;
471 }
472
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900473 daemonCmd += SEPARATOR;
474 daemonCmd += ifname;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800475 haveInterfaces = true;
476 }
477
Erik Kline15079dd2018-05-18 23:10:56 +0900478 if (!haveInterfaces) {
479 mDnsmasqState.update_ifaces_cmd.clear();
480 } else {
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900481 mDnsmasqState.update_ifaces_cmd = std::move(daemonCmd);
Erik Kline15079dd2018-05-18 23:10:56 +0900482 if (mDaemonFd != -1) return (mDnsmasqState.sendAllState(mDaemonFd) == 0);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800483 }
Erik Kline1d065ba2016-06-08 13:24:45 +0900484 return true;
San Mehat9d10b342010-01-18 09:51:02 -0800485}
486
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800487int TetherController::tetherInterface(const char *interface) {
488 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700489 if (!isIfaceName(interface)) {
490 errno = ENOENT;
Luke Huangb5733d72018-08-21 17:17:19 +0800491 return -errno;
JP Abgrall69261cb2014-06-19 18:35:24 -0700492 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800493
Erik Kline1d065ba2016-06-08 13:24:45 +0900494 if (!configureForIPv6Router(interface)) {
495 configureForIPv6Client(interface);
Luke Huangb5733d72018-08-21 17:17:19 +0800496 return -EREMOTEIO;
Erik Kline1d065ba2016-06-08 13:24:45 +0900497 }
498 mInterfaces.push_back(interface);
499
500 if (!applyDnsInterfaces()) {
501 mInterfaces.pop_back();
502 configureForIPv6Client(interface);
Luke Huangb5733d72018-08-21 17:17:19 +0800503 return -EREMOTEIO;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800504 } else {
505 return 0;
506 }
507}
508
San Mehat9d10b342010-01-18 09:51:02 -0800509int TetherController::untetherInterface(const char *interface) {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800510 ALOGD("untetherInterface(%s)", interface);
511
Erik Kline1d065ba2016-06-08 13:24:45 +0900512 for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) {
513 if (!strcmp(interface, it->c_str())) {
514 mInterfaces.erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800515
Erik Kline1d065ba2016-06-08 13:24:45 +0900516 configureForIPv6Client(interface);
Luke Huangb5733d72018-08-21 17:17:19 +0800517 return applyDnsInterfaces() ? 0 : -EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800518 }
519 }
520 errno = ENOENT;
Luke Huangb5733d72018-08-21 17:17:19 +0800521 return -errno;
San Mehat9d10b342010-01-18 09:51:02 -0800522}
523
Erik Kline1d065ba2016-06-08 13:24:45 +0900524const std::list<std::string> &TetherController::getTetheredInterfaceList() const {
San Mehat9d10b342010-01-18 09:51:02 -0800525 return mInterfaces;
526}
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900527
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900528int 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 VAN3b47c792018-03-20 14:44:12 +0900559 mFwdIfaces.clear();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900560
561 return 0;
562}
563
564int 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 Huangae038f82018-11-05 11:17:31 +0900575 "*filter\n"
576 ":%s -\n"
577 "COMMIT\n"
578 "*raw\n"
579 ":%s -\n"
580 "COMMIT\n",
581 LOCAL_FORWARD, LOCAL_RAW_PREROUTING);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900582
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 Colittia93126d2017-08-24 13:28:19 +0900593 return 0;
594}
595
596int 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 Huang19b49c52018-10-22 12:12:05 +0900600 return -ENODEV;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900601 }
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 Huang19b49c52018-10-22 12:12:05 +0900606 return -EINVAL;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900607 }
608
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900609 if (isForwardingPairEnabled(intIface, extIface)) {
610 return 0;
611 }
612
613 // add this if we are the first enabled nat for this upstream
Maciej Żenczykowski99665c32020-04-09 10:50:42 +0000614 bool firstDownstreamForThisUpstream = !isAnyForwardingEnabledOnUpstream(extIface);
615 if (firstDownstreamForThisUpstream) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900616 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 Huangae038f82018-11-05 11:17:31 +0900622 if (iptablesRestoreFunction(V4, Join(v4Cmds, '\n'), nullptr) || setupIPv6CountersChain() ||
623 setTetherGlobalAlertRule()) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900624 ALOGE("Error setting postroute rule: iface=%s", extIface);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900625 if (!isAnyForwardingPairEnabled()) {
626 // unwind what's been done, but don't care about success - what more could we do?
627 setDefaults();
628 }
Luke Huang19b49c52018-10-22 12:12:05 +0900629 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900630 }
631 }
632
633 if (setForwardRules(true, intIface, extIface) != 0) {
634 ALOGE("Error setting forward rules");
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900635 if (!isAnyForwardingPairEnabled()) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900636 setDefaults();
637 }
Luke Huang19b49c52018-10-22 12:12:05 +0900638 return -ENODEV;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900639 }
640
Maciej Żenczykowski99665c32020-04-09 10:50:42 +0000641 if (firstDownstreamForThisUpstream) maybeStartBpf(extIface);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900642 return 0;
643}
644
Luke Huangae038f82018-11-05 11:17:31 +0900645int 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 VAN3b47c792018-03-20 14:44:12 +0900658int 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 Huangae038f82018-11-05 11:17:31 +0900668 const std::string v6Cmds =
669 "*filter\n" +
670 StringPrintf("-A %s -g %s\n", LOCAL_FORWARD, LOCAL_TETHER_COUNTERS_CHAIN) + "COMMIT\n";
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900671
Luke Huangae038f82018-11-05 11:17:31 +0900672 return iptablesRestoreFunction(V6, v6Cmds, nullptr);
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900673}
674
675// Gets a pointer to the ForwardingDownstream for an interface pair in the map, or nullptr
676TetherController::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
687void 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
700void 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
710bool 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
716bool 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
726bool TetherController::isAnyForwardingPairEnabled() {
727 for (auto& it : mFwdIfaces) {
728 if (it.second.active) {
729 return true;
730 }
731 }
732 return false;
733}
734
735bool 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 Colittia93126d2017-08-24 13:28:19 +0900742}
743
744/* static */
745std::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
749int 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 Huang19b49c52018-10-22 12:12:05 +0900757 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900758 }
759
760 std::vector<std::string> v4 = {
hiroaki.yokoyama04f2cb52018-06-13 18:47:30 +0900761 "*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 Colittia93126d2017-08-24 13:28:19 +0900774 };
775
776 std::vector<std::string> v6 = {
777 "*filter",
778 };
779
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900780 // We only ever add tethering quota rules so that they stick.
781 if (add && !tetherCountingRuleExists(intIface, extIface)) {
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900782 v4.push_back(makeTetherCountingRule(intIface, extIface));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900783 v4.push_back(makeTetherCountingRule(extIface, intIface));
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900784 v6.push_back(makeTetherCountingRule(intIface, extIface));
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900785 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 Colitti4604b4a2017-08-24 19:21:50 +0900790 // every time, starting with ":tetherctrl_FORWARD -\n". This would likely be a bit simpler.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900791 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 Huang19b49c52018-10-22 12:12:05 +0900806 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900807 }
808
Remi NGUYEN VAN3b47c792018-03-20 14:44:12 +0900809 if (add) {
810 addForwardingPair(intIface, extIface);
811 } else {
812 markForwardingPairDisabled(intIface, extIface);
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900813 }
814
815 return 0;
816}
817
818int TetherController::disableNat(const char* intIface, const char* extIface) {
819 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Luke Huangae038f82018-11-05 11:17:31 +0900820 errno = ENODEV;
821 return -errno;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900822 }
823
824 setForwardRules(false, intIface, extIface);
Maciej Żenczykowski99665c32020-04-09 10:50:42 +0000825 if (!isAnyForwardingEnabledOnUpstream(extIface)) maybeStopBpf(extIface);
826 if (!isAnyForwardingPairEnabled()) setDefaults();
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900827 return 0;
828}
829
Lorenzo Colittif0e051c2020-04-06 09:19:57 +0000830namespace {
831Result<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
856Result<void> TetherController::addOffloadRule(const TetherOffloadRuleParcel& rule) {
857 Result<void> res = validateOffloadRule(rule);
858 if (!res.ok()) return res;
859
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900860 ethhdr hdr = {
861 .h_proto = htons(ETH_P_IPV6),
862 };
Lorenzo Colittif0e051c2020-04-06 09:19:57 +0000863 memcpy(&hdr.h_dest, rule.dstL2Address.data(), sizeof(hdr.h_dest));
864 memcpy(&hdr.h_source, rule.srcL2Address.data(), sizeof(hdr.h_source));
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900865
Lorenzo Colittif0e051c2020-04-06 09:19:57 +0000866 // Only downstream supported for now.
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900867 TetherIngressKey key = {
Lorenzo Colittif0e051c2020-04-06 09:19:57 +0000868 .iif = static_cast<uint32_t>(rule.inputInterfaceIndex),
869 .neigh6 = *(const in6_addr*)rule.destination.data(),
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900870 };
871
872 TetherIngressValue value = {
Maciej Żenczykowski1ec24532020-05-23 22:07:00 +0000873 .oif = static_cast<uint32_t>(rule.outputInterfaceIndex),
874 .macHeader = hdr,
875 .pmtu = 1500, // TODO: don't just blindly use this default
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900876 };
877
878 return mBpfIngressMap.writeValue(key, value, BPF_ANY);
879}
880
Lorenzo Colittif0e051c2020-04-06 09:19:57 +0000881Result<void> TetherController::removeOffloadRule(const TetherOffloadRuleParcel& rule) {
882 Result<void> res = validateOffloadRule(rule);
883 if (!res.ok()) return res;
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900884
885 TetherIngressKey key = {
Lorenzo Colittif0e051c2020-04-06 09:19:57 +0000886 .iif = static_cast<uint32_t>(rule.inputInterfaceIndex),
887 .neigh6 = *(const in6_addr*)rule.destination.data(),
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900888 };
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 Colittia93126d2017-08-24 13:28:19 +0900898void 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 Colitti4604b4a2017-08-24 19:21:50 +0900910 * Chain tetherctrl_counters (4 references)
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900911 * 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 Colitti4604b4a2017-08-24 19:21:50 +0900917 * Chain tetherctrl_counters (0 references)
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900918 * 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 Colittia93126d2017-08-24 13:28:19 +0900922 */
Lorenzo Colitti09353392017-08-24 14:20:32 +0900923int TetherController::addForwardChainStats(TetherStatsList& statsList,
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900924 const std::string& statsOutput,
925 std::string &extraProcessingInfo) {
waynema71a0b592018-11-21 13:31:34 +0800926 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 Colittia93126d2017-08-24 13:28:19 +0900936 TetherStats stats;
Lorenzo Colitti09353392017-08-24 14:20:32 +0900937 const TetherStats empty;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900938
waynema71a0b592018-11-21 13:31:34 +0800939 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 Colitti09353392017-08-24 14:20:32 +0900946
waynema71a0b592018-11-21 13:31:34 +0800947 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 Colitti09353392017-08-24 14:20:32 +0900958 }
Lorenzo Colitti09353392017-08-24 14:20:32 +0900959
waynema71a0b592018-11-21 13:31:34 +0800960 if (line.empty()) continue;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900961
waynema71a0b592018-11-21 13:31:34 +0800962 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 Colittia93126d2017-08-24 13:28:19 +0900976
waynema71a0b592018-11-21 13:31:34 +0800977 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 Colittia93126d2017-08-24 13:28:19 +0900980 /*
981 * The following assumes that the 1st rule has in:extIface out:intIface,
982 * which is what TetherController sets up.
Lorenzo Colitti09353392017-08-24 14:20:32 +0900983 * The 1st matches rx, and sets up the pair for the tx side.
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900984 */
Lorenzo Colitti09353392017-08-24 14:20:32 +0900985 if (!stats.intIface[0]) {
waynema71a0b592018-11-21 13:31:34 +0800986 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 Colitti09353392017-08-24 14:20:32 +0900988 stats.intIface = iface0;
989 stats.extIface = iface1;
Lorenzo Colitti09353392017-08-24 14:20:32 +0900990 stats.txPackets = packets;
991 stats.txBytes = bytes;
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900992 } else if (stats.intIface == iface1 && stats.extIface == iface0) {
waynema71a0b592018-11-21 13:31:34 +0800993 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 Colitti9a65ac62017-09-04 18:07:56 +0900995 stats.rxPackets = packets;
996 stats.rxBytes = bytes;
Lorenzo Colittia93126d2017-08-24 13:28:19 +0900997 }
998 if (stats.rxBytes != -1 && stats.txBytes != -1) {
Lorenzo Colitti09353392017-08-24 14:20:32 +0900999 ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64, stats.rxBytes, stats.txBytes);
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001000 addStats(statsList, stats);
Lorenzo Colitti09353392017-08-24 14:20:32 +09001001 stats = empty;
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001002 }
1003 }
1004
1005 /* It is always an error to find only one side of the stats. */
Lorenzo Colitti38fd1362017-09-15 11:40:01 +09001006 if (((stats.rxBytes == -1) != (stats.txBytes == -1))) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +09001007 return -EREMOTEIO;
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001008 }
1009 return 0;
1010}
1011
Lorenzo Colitti5192bf72017-09-04 13:30:59 +09001012StatusOr<TetherController::TetherStatsList> TetherController::getTetherStats() {
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001013 TetherStatsList statsList;
Lorenzo Colitti5192bf72017-09-04 13:30:59 +09001014 std::string parsedIptablesOutput;
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001015
1016 for (const IptablesTarget target : {V4, V6}) {
1017 std::string statsString;
Lorenzo Colitti09353392017-08-24 14:20:32 +09001018 if (int ret = iptablesRestoreFunction(target, GET_TETHER_STATS_COMMAND, &statsString)) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +09001019 return statusFromErrno(-ret, StringPrintf("failed to fetch tether stats (%d): %d",
1020 target, ret));
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001021 }
1022
Lorenzo Colitti5192bf72017-09-04 13:30:59 +09001023 if (int ret = addForwardChainStats(statsList, statsString, parsedIptablesOutput)) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +09001024 return statusFromErrno(-ret, StringPrintf("failed to parse %s tether stats:\n%s",
1025 target == V4 ? "IPv4": "IPv6",
Lorenzo Colitti5192bf72017-09-04 13:30:59 +09001026 parsedIptablesOutput.c_str()));
Lorenzo Colittia93126d2017-08-24 13:28:19 +09001027 }
1028 }
1029
Hungming Chen68dade32020-02-20 13:50:56 +08001030 if (!mBpfStatsMap.isValid()) {
1031 return statsList;
1032 }
Hungming Chenc2e95fb2020-02-19 17:41:30 +08001033
Hungming Chen68dade32020-02-20 13:50:56 +08001034 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 Chenc2e95fb2020-02-19 17:41:30 +08001041 }
Hungming Chen68dade32020-02-20 13:50:56 +08001042 // 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 Chenc2e95fb2020-02-19 17:41:30 +08001057 }
1058
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +09001059 return statsList;
1060}
1061
Maciej Żenczykowskiac14cb52020-05-23 20:48:00 +00001062// Use UINT64_MAX (~0uLL) for unlimited.
1063Result<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 Chen1da90082020-02-14 20:24:16 +08001105void TetherController::maybeStartBpf(const char* extIface) {
Maciej Żenczykowskiaef13012020-02-18 17:13:09 -08001106 if (!bpf::isBpfSupported()) return;
1107
Hungming Chen1da90082020-02-14 20:24:16 +08001108 // 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 Żenczykowskiac14cb52020-05-23 20:48:00 +00001135
1136 // For now we just set data limit to 'infinity' ie. unlimited.
1137 setBpfLimit(ifIndex, ~0uLL);
Hungming Chen1da90082020-02-14 20:24:16 +08001138}
1139
1140void TetherController::maybeStopBpf(const char* extIface) {
Maciej Żenczykowskiaef13012020-02-18 17:13:09 -08001141 if (!bpf::isBpfSupported()) return;
1142
Hungming Chen1da90082020-02-14 20:24:16 +08001143 // 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 Colitti52db3912020-02-17 23:59:45 +09001156void 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 Colittie801d3c2020-02-18 00:00:35 +09001166namespace {
1167
1168std::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
1183void TetherController::dumpBpf(DumpWriter& dw) {
Maciej Żenczykowskid25a7342020-05-04 18:18:38 -07001184 if (!mBpfIngressMap.isValid() || !mBpfStatsMap.isValid() || !mBpfLimitMap.isValid()) {
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001185 dw.println("BPF not supported");
1186 return;
1187 }
1188
Maciej Żenczykowski1ec24532020-05-23 22:07:00 +00001189 dw.println("BPF ingress map: iif v6addr -> oif srcmac dstmac ethertype [pmtu]");
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001190 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 Żenczykowski1ec24532020-05-23 22:07:00 +00001197 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 Colittie801d3c2020-02-18 00:00:35 +09001199
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 Robot15b21b72020-05-13 09:24:07 +00001213 dw.println("%u -> %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxPackets, value.rxBytes,
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001214 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 Żenczykowskid25a7342020-05-04 18:18:38 -07001225
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 Colittie801d3c2020-02-18 00:00:35 +09001240}
1241
Lorenzo Colitti52db3912020-02-17 23:59:45 +09001242void 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 Colittie801d3c2020-02-18 00:00:35 +09001259 dw.println("");
1260 dumpBpf(dw);
Lorenzo Colitti52db3912020-02-17 23:59:45 +09001261}
1262
Lorenzo Colittie20a5262017-05-09 18:30:44 +09001263} // namespace net
1264} // namespace android