blob: 3cc46368d8c4943867e62f438277a20340d492ca [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 Kline2c5aaa12016-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 Kline2c5aaa12016-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) {
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000049 int fd = open(filename, O_WRONLY);
50 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 Kline2c5aaa12016-06-08 13:24:45 +090065bool configureForIPv6Router(const char *interface) {
66 return (InterfaceController::setEnableIPv6(interface, 0) == 0)
67 && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0)
Erik Kline59d8c482016-08-09 15:28:42 +090068 && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0)
69 && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0)
Erik Kline2c5aaa12016-06-08 13:24:45 +090070 && (InterfaceController::setEnableIPv6(interface, 1) == 0);
71}
72
73void configureForIPv6Client(const char *interface) {
74 InterfaceController::setAcceptIPv6Ra(interface, 1);
Erik Kline59d8c482016-08-09 15:28:42 +090075 InterfaceController::setAcceptIPv6Dad(interface, 1);
76 InterfaceController::setIPv6DadTransmits(interface, "1");
Erik Kline2c5aaa12016-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 Kline2c5aaa12016-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".
Devi Sandeep Endluri V V893c1222016-04-27 22:31:19 +0530119 bool trigger = mForwardingRequests.empty();
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900120 mForwardingRequests.insert(requester);
Devi Sandeep Endluri V V893c1222016-04-27 22:31:19 +0530121 if (trigger) {
122 return setIpFwdEnabled();
123 }
124 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900125}
San Mehat9d10b342010-01-18 09:51:02 -0800126
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900127bool TetherController::disableForwarding(const char* requester) {
128 mForwardingRequests.erase(requester);
Devi Sandeep Endluri V V893c1222016-04-27 22:31:19 +0530129 if (mForwardingRequests.empty()) {
130 return setIpFwdEnabled();
131 }
132 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900133}
San Mehat9d10b342010-01-18 09:51:02 -0800134
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900135size_t TetherController::forwardingRequestCount() {
136 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800137}
138
Erik Klinea3ec3702016-01-05 03:52:07 +0000139#define TETHER_START_CONST_ARG 8
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800140
Erik Kline13fa01f2015-11-12 17:49:23 +0900141int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800142 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000143 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800144 errno = EBUSY;
145 return -1;
146 }
147
Steve Block7b984e32011-12-20 16:22:42 +0000148 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800149
150 pid_t pid;
151 int pipefd[2];
152
153 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000154 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800155 return -1;
156 }
157
158 /*
159 * TODO: Create a monitoring thread to handle and restart
160 * the daemon if it exits prematurely
161 */
162 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000163 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800164 close(pipefd[0]);
165 close(pipefd[1]);
166 return -1;
167 }
168
169 if (!pid) {
170 close(pipefd[1]);
171 if (pipefd[0] != STDIN_FILENO) {
172 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000173 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800174 return -1;
175 }
176 close(pipefd[0]);
177 }
San Mehat9d10b342010-01-18 09:51:02 -0800178
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800179 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700180 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
181 args[num_processed_args - 1] = NULL;
182 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700183 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700184 args[2] = (char *)"--no-resolv";
185 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800186 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700187 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800188 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
189 args[6] = (char *)"--pid-file";
190 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800191
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800192 int nextArg = TETHER_START_CONST_ARG;
Erik Kline13fa01f2015-11-12 17:49:23 +0900193 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
194 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
195 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700196 }
197
198 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000199 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800200 }
Steve Block5ea0c052012-01-06 19:18:11 +0000201 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700202 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800203 } else {
204 close(pipefd[0]);
205 mDaemonPid = pid;
206 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800207 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000208 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800209 }
210
211 return 0;
212}
213
214int TetherController::stopTethering() {
215
216 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000217 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800218 return 0;
219 }
220
Steve Block7b984e32011-12-20 16:22:42 +0000221 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800222
223 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800224 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800225 mDaemonPid = 0;
226 close(mDaemonFd);
227 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000228 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800229 return 0;
230}
Matthew Xie19944102012-07-12 16:42:07 -0700231
San Mehat9d10b342010-01-18 09:51:02 -0800232bool TetherController::isTetheringStarted() {
233 return (mDaemonPid == 0 ? false : true);
234}
235
Kenny Rootcf52faf2010-02-18 09:59:55 -0800236#define MAX_CMD_SIZE 1024
237
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700238int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800239 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800240 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800241
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700242 Fwmark fwmark;
243 fwmark.netId = netId;
244 fwmark.explicitlySelected = true;
245 fwmark.protectedFromVpn = true;
246 fwmark.permission = PERMISSION_SYSTEM;
247
Erik Kline13fa01f2015-11-12 17:49:23 +0900248 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800249 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800250
Erik Kline2c5aaa12016-06-08 13:24:45 +0900251 mDnsForwarders.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800252 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700253 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800254
Lorenzo Colittic2841282015-11-25 22:13:57 +0900255 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
256 int ret = getaddrinfo(servers[i], NULL, &hints, &res);
257 freeaddrinfo(res);
258 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000259 ALOGE("Failed to parse DNS server '%s'", servers[i]);
Erik Kline2c5aaa12016-06-08 13:24:45 +0900260 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900261 errno = EINVAL;
San Mehat9d10b342010-01-18 09:51:02 -0800262 return -1;
263 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800264
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700265 cmdLen += (strlen(servers[i]) + 1);
266 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000267 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800268 break;
269 }
270
Erik Kline13fa01f2015-11-12 17:49:23 +0900271 strcat(daemonCmd, SEPARATOR);
San Mehat9d10b342010-01-18 09:51:02 -0800272 strcat(daemonCmd, servers[i]);
Erik Kline2c5aaa12016-06-08 13:24:45 +0900273 mDnsForwarders.push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800274 }
275
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700276 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800277 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000278 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800279 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000280 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline2c5aaa12016-06-08 13:24:45 +0900281 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900282 errno = EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800283 return -1;
284 }
285 }
286 return 0;
287}
288
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700289unsigned TetherController::getDnsNetId() {
290 return mDnsNetId;
291}
292
Erik Kline2c5aaa12016-06-08 13:24:45 +0900293const std::list<std::string> &TetherController::getDnsForwarders() const {
San Mehat9d10b342010-01-18 09:51:02 -0800294 return mDnsForwarders;
295}
296
Erik Kline2c5aaa12016-06-08 13:24:45 +0900297bool TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800298 char daemonCmd[MAX_CMD_SIZE];
299
300 strcpy(daemonCmd, "update_ifaces");
301 int cmdLen = strlen(daemonCmd);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800302 bool haveInterfaces = false;
303
Erik Kline2c5aaa12016-06-08 13:24:45 +0900304 for (const auto &ifname : mInterfaces) {
305 cmdLen += (ifname.size() + 1);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800306 if (cmdLen + 1 >= MAX_CMD_SIZE) {
307 ALOGD("Too many DNS ifaces listed");
308 break;
309 }
310
Erik Kline13fa01f2015-11-12 17:49:23 +0900311 strcat(daemonCmd, SEPARATOR);
Erik Kline2c5aaa12016-06-08 13:24:45 +0900312 strcat(daemonCmd, ifname.c_str());
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800313 haveInterfaces = true;
314 }
315
316 if ((mDaemonFd != -1) && haveInterfaces) {
317 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
318 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
319 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline2c5aaa12016-06-08 13:24:45 +0900320 return false;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800321 }
322 }
Erik Kline2c5aaa12016-06-08 13:24:45 +0900323 return true;
San Mehat9d10b342010-01-18 09:51:02 -0800324}
325
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800326int TetherController::tetherInterface(const char *interface) {
327 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700328 if (!isIfaceName(interface)) {
329 errno = ENOENT;
330 return -1;
331 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800332
Erik Kline2c5aaa12016-06-08 13:24:45 +0900333 if (!configureForIPv6Router(interface)) {
334 configureForIPv6Client(interface);
335 return -1;
336 }
337 mInterfaces.push_back(interface);
338
339 if (!applyDnsInterfaces()) {
340 mInterfaces.pop_back();
341 configureForIPv6Client(interface);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800342 return -1;
343 } else {
344 return 0;
345 }
346}
347
San Mehat9d10b342010-01-18 09:51:02 -0800348int TetherController::untetherInterface(const char *interface) {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800349 ALOGD("untetherInterface(%s)", interface);
350
Erik Kline2c5aaa12016-06-08 13:24:45 +0900351 for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) {
352 if (!strcmp(interface, it->c_str())) {
353 mInterfaces.erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800354
Erik Kline2c5aaa12016-06-08 13:24:45 +0900355 configureForIPv6Client(interface);
356 return applyDnsInterfaces() ? 0 : -1;
San Mehat9d10b342010-01-18 09:51:02 -0800357 }
358 }
359 errno = ENOENT;
360 return -1;
361}
362
Erik Kline2c5aaa12016-06-08 13:24:45 +0900363const std::list<std::string> &TetherController::getTetheredInterfaceList() const {
San Mehat9d10b342010-01-18 09:51:02 -0800364 return mInterfaces;
365}