blob: 7bf4a92562b2538fb6fc321e9043288839bc3ab9 [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
17#include <stdlib.h>
18#include <errno.h>
San Mehat18737842010-01-21 09:22:43 -080019#include <fcntl.h>
Lorenzo Colittic2841282015-11-25 22:13:57 +090020#include <netdb.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080021#include <string.h>
San Mehat18737842010-01-21 09:22:43 -080022
San Mehat9d10b342010-01-18 09:51:02 -080023#include <sys/socket.h>
24#include <sys/stat.h>
San Mehat18737842010-01-21 09:22:43 -080025#include <sys/types.h>
26#include <sys/wait.h>
27
San Mehat9d10b342010-01-18 09:51:02 -080028#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#define LOG_TAG "TetherController"
32#include <cutils/log.h>
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050033#include <cutils/properties.h>
San Mehat9d10b342010-01-18 09:51:02 -080034
Lorenzo Colitti667c4772014-08-26 14:13:07 -070035#include "Fwmark.h"
JP Abgrall69261cb2014-06-19 18:35:24 -070036#include "NetdConstants.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070037#include "Permission.h"
Erik Kline1d065ba2016-06-08 13:24:45 +090038#include "InterfaceController.h"
San Mehat9d10b342010-01-18 09:51:02 -080039#include "TetherController.h"
40
Lorenzo Colitti799625c2015-02-25 12:52:00 +090041namespace {
42
Erik Kline1d065ba2016-06-08 13:24:45 +090043const char BP_TOOLS_MODE[] = "bp-tools";
44const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
45const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
46const char SEPARATOR[] = "|";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090047
48bool writeToFile(const char* filename, const char* value) {
Nick Kralevichb95c60c2016-11-19 09:09:16 -080049 int fd = open(filename, O_WRONLY | O_CLOEXEC);
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000050 if (fd < 0) {
51 ALOGE("Failed to open %s: %s", filename, strerror(errno));
52 return false;
53 }
54
55 const ssize_t len = strlen(value);
56 if (write(fd, value, len) != len) {
57 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
58 close(fd);
59 return false;
60 }
61 close(fd);
62 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090063}
64
Erik Kline1d065ba2016-06-08 13:24:45 +090065bool configureForIPv6Router(const char *interface) {
66 return (InterfaceController::setEnableIPv6(interface, 0) == 0)
67 && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0)
Erik Kline6cddf512016-08-09 15:28:42 +090068 && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0)
69 && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0)
Erik Kline1d065ba2016-06-08 13:24:45 +090070 && (InterfaceController::setEnableIPv6(interface, 1) == 0);
71}
72
73void configureForIPv6Client(const char *interface) {
74 InterfaceController::setAcceptIPv6Ra(interface, 1);
Erik Kline6cddf512016-08-09 15:28:42 +090075 InterfaceController::setAcceptIPv6Dad(interface, 1);
76 InterfaceController::setIPv6DadTransmits(interface, "1");
Erik Kline1d065ba2016-06-08 13:24:45 +090077 InterfaceController::setEnableIPv6(interface, 0);
78}
79
Lorenzo Colitti799625c2015-02-25 12:52:00 +090080bool inBpToolsMode() {
81 // In BP tools mode, do not disable IP forwarding
82 char bootmode[PROPERTY_VALUE_MAX] = {0};
83 property_get("ro.bootmode", bootmode, "unknown");
84 return !strcmp(BP_TOOLS_MODE, bootmode);
85}
86
87} // namespace
88
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070089TetherController::TetherController() {
Lorenzo Colitti667c4772014-08-26 14:13:07 -070090 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080091 mDaemonFd = -1;
92 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090093 if (inBpToolsMode()) {
94 enableForwarding(BP_TOOLS_MODE);
95 } else {
96 setIpFwdEnabled();
97 }
San Mehat9d10b342010-01-18 09:51:02 -080098}
99
100TetherController::~TetherController() {
Erik Kline1d065ba2016-06-08 13:24:45 +0900101 mInterfaces.clear();
102 mDnsForwarders.clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900103 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800104}
105
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900106bool TetherController::setIpFwdEnabled() {
107 bool success = true;
108 const char* value = mForwardingRequests.empty() ? "0" : "1";
109 ALOGD("Setting IP forward enable = %s", value);
110 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
111 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
112 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800113}
114
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900115bool TetherController::enableForwarding(const char* requester) {
116 // Don't return an error if this requester already requested forwarding. Only return errors for
117 // things that the caller caller needs to care about, such as "couldn't write to the file to
118 // enable forwarding".
119 mForwardingRequests.insert(requester);
120 return setIpFwdEnabled();
121}
San Mehat9d10b342010-01-18 09:51:02 -0800122
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900123bool TetherController::disableForwarding(const char* requester) {
124 mForwardingRequests.erase(requester);
125 return setIpFwdEnabled();
126}
San Mehat9d10b342010-01-18 09:51:02 -0800127
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900128size_t TetherController::forwardingRequestCount() {
129 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800130}
131
Erik Klinea3ec3702016-01-05 03:52:07 +0000132#define TETHER_START_CONST_ARG 8
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800133
Erik Kline13fa01f2015-11-12 17:49:23 +0900134int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800135 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000136 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800137 errno = EBUSY;
138 return -1;
139 }
140
Steve Block7b984e32011-12-20 16:22:42 +0000141 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800142
143 pid_t pid;
144 int pipefd[2];
145
146 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000147 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800148 return -1;
149 }
150
151 /*
152 * TODO: Create a monitoring thread to handle and restart
153 * the daemon if it exits prematurely
154 */
155 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000156 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800157 close(pipefd[0]);
158 close(pipefd[1]);
159 return -1;
160 }
161
162 if (!pid) {
163 close(pipefd[1]);
164 if (pipefd[0] != STDIN_FILENO) {
165 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000166 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800167 return -1;
168 }
169 close(pipefd[0]);
170 }
San Mehat9d10b342010-01-18 09:51:02 -0800171
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800172 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700173 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
174 args[num_processed_args - 1] = NULL;
175 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700176 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700177 args[2] = (char *)"--no-resolv";
178 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800179 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700180 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800181 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
182 args[6] = (char *)"--pid-file";
183 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800184
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800185 int nextArg = TETHER_START_CONST_ARG;
Erik Kline13fa01f2015-11-12 17:49:23 +0900186 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
187 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
188 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700189 }
190
191 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000192 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800193 }
Steve Block5ea0c052012-01-06 19:18:11 +0000194 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700195 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800196 } else {
197 close(pipefd[0]);
198 mDaemonPid = pid;
199 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800200 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000201 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800202 }
203
204 return 0;
205}
206
207int TetherController::stopTethering() {
208
209 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000210 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800211 return 0;
212 }
213
Steve Block7b984e32011-12-20 16:22:42 +0000214 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800215
216 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800217 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800218 mDaemonPid = 0;
219 close(mDaemonFd);
220 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000221 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800222 return 0;
223}
Matthew Xie19944102012-07-12 16:42:07 -0700224
San Mehat9d10b342010-01-18 09:51:02 -0800225bool TetherController::isTetheringStarted() {
226 return (mDaemonPid == 0 ? false : true);
227}
228
Kenny Rootcf52faf2010-02-18 09:59:55 -0800229#define MAX_CMD_SIZE 1024
230
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700231int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800232 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800233 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800234
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700235 Fwmark fwmark;
236 fwmark.netId = netId;
237 fwmark.explicitlySelected = true;
238 fwmark.protectedFromVpn = true;
239 fwmark.permission = PERMISSION_SYSTEM;
240
Erik Kline13fa01f2015-11-12 17:49:23 +0900241 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800242 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800243
Erik Kline1d065ba2016-06-08 13:24:45 +0900244 mDnsForwarders.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800245 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700246 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800247
Lorenzo Colittic2841282015-11-25 22:13:57 +0900248 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
249 int ret = getaddrinfo(servers[i], NULL, &hints, &res);
250 freeaddrinfo(res);
251 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000252 ALOGE("Failed to parse DNS server '%s'", servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900253 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900254 errno = EINVAL;
San Mehat9d10b342010-01-18 09:51:02 -0800255 return -1;
256 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800257
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700258 cmdLen += (strlen(servers[i]) + 1);
259 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000260 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800261 break;
262 }
263
Erik Kline13fa01f2015-11-12 17:49:23 +0900264 strcat(daemonCmd, SEPARATOR);
San Mehat9d10b342010-01-18 09:51:02 -0800265 strcat(daemonCmd, servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900266 mDnsForwarders.push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800267 }
268
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700269 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800270 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000271 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800272 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000273 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline1d065ba2016-06-08 13:24:45 +0900274 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900275 errno = EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800276 return -1;
277 }
278 }
279 return 0;
280}
281
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700282unsigned TetherController::getDnsNetId() {
283 return mDnsNetId;
284}
285
Erik Kline1d065ba2016-06-08 13:24:45 +0900286const std::list<std::string> &TetherController::getDnsForwarders() const {
San Mehat9d10b342010-01-18 09:51:02 -0800287 return mDnsForwarders;
288}
289
Erik Kline1d065ba2016-06-08 13:24:45 +0900290bool TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800291 char daemonCmd[MAX_CMD_SIZE];
292
293 strcpy(daemonCmd, "update_ifaces");
294 int cmdLen = strlen(daemonCmd);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800295 bool haveInterfaces = false;
296
Erik Kline1d065ba2016-06-08 13:24:45 +0900297 for (const auto &ifname : mInterfaces) {
298 cmdLen += (ifname.size() + 1);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800299 if (cmdLen + 1 >= MAX_CMD_SIZE) {
300 ALOGD("Too many DNS ifaces listed");
301 break;
302 }
303
Erik Kline13fa01f2015-11-12 17:49:23 +0900304 strcat(daemonCmd, SEPARATOR);
Erik Kline1d065ba2016-06-08 13:24:45 +0900305 strcat(daemonCmd, ifname.c_str());
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800306 haveInterfaces = true;
307 }
308
309 if ((mDaemonFd != -1) && haveInterfaces) {
310 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
311 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
312 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline1d065ba2016-06-08 13:24:45 +0900313 return false;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800314 }
315 }
Erik Kline1d065ba2016-06-08 13:24:45 +0900316 return true;
San Mehat9d10b342010-01-18 09:51:02 -0800317}
318
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800319int TetherController::tetherInterface(const char *interface) {
320 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700321 if (!isIfaceName(interface)) {
322 errno = ENOENT;
323 return -1;
324 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800325
Erik Kline1d065ba2016-06-08 13:24:45 +0900326 if (!configureForIPv6Router(interface)) {
327 configureForIPv6Client(interface);
328 return -1;
329 }
330 mInterfaces.push_back(interface);
331
332 if (!applyDnsInterfaces()) {
333 mInterfaces.pop_back();
334 configureForIPv6Client(interface);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800335 return -1;
336 } else {
337 return 0;
338 }
339}
340
San Mehat9d10b342010-01-18 09:51:02 -0800341int TetherController::untetherInterface(const char *interface) {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800342 ALOGD("untetherInterface(%s)", interface);
343
Erik Kline1d065ba2016-06-08 13:24:45 +0900344 for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) {
345 if (!strcmp(interface, it->c_str())) {
346 mInterfaces.erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800347
Erik Kline1d065ba2016-06-08 13:24:45 +0900348 configureForIPv6Client(interface);
349 return applyDnsInterfaces() ? 0 : -1;
San Mehat9d10b342010-01-18 09:51:02 -0800350 }
351 }
352 errno = ENOENT;
353 return -1;
354}
355
Erik Kline1d065ba2016-06-08 13:24:45 +0900356const std::list<std::string> &TetherController::getTetheredInterfaceList() const {
San Mehat9d10b342010-01-18 09:51:02 -0800357 return mInterfaces;
358}